xref: /aosp_15_r20/external/libavc/examples/avcenc/output.c (revision 495ae853bb871d1e5a258cb02c2cc13cde8ddb9a)
1 /******************************************************************************
2  *
3  * Copyright (C) 2015 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 
21 /**
22 *******************************************************************************
23 * @file
24 *  output.c
25 *
26 * @brief
27 *  Contains functions necessary for managing output buffers
28 *
29 * @author
30 *  ittiam
31 *
32 * @remarks
33 *  none
34 *
35 *******************************************************************************
36 */
37 
38 /*****************************************************************************/
39 /* File Includes                                                             */
40 /*****************************************************************************/
41 
42 /* System include files */
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <assert.h>
46 #include <string.h>
47 
48 /* User include files */
49 #include "ih264_typedefs.h"
50 #include "iv2.h"
51 #include "ive2.h"
52 #include "ih264e.h"
53 #include "app.h"
54 
55 /*****************************************************************************/
56 /* Constant Macros                                                           */
57 /*****************************************************************************/
58 #define PEAK_WINDOW_SIZE    8
59 
60 
61 /*****************************************************************************/
62 /*  Macros                                                                   */
63 /*****************************************************************************/
64 
65 
66 /*****************************************************************************/
67 /*  Function Definitions                                                     */
68 /*****************************************************************************/
69 
70 /**
71 **************************************************************************
72 * @brief Write bitstream buffers to a file
73 **************************************************************************
74 */
write_output(FILE * fp,UWORD8 * pu1_buf,WORD32 num_bytes)75 IV_STATUS_T write_output(FILE *fp, UWORD8 *pu1_buf, WORD32 num_bytes)
76 {
77     WORD32 bytes;
78 
79     bytes = fwrite(pu1_buf, sizeof(UWORD8), num_bytes, fp);
80     if(bytes != num_bytes)
81         return IV_FAIL;
82     fflush(fp);
83 
84     return IV_SUCCESS;
85 }
86 
87 /**
88 **************************************************************************
89 * @brief allocate bitstream buffers
90 **************************************************************************
91 */
allocate_output(app_ctxt_t * ps_app_ctxt)92 void allocate_output(app_ctxt_t *ps_app_ctxt)
93 {
94     WORD32 num_bufs;
95     WORD32 i;
96     UWORD8 *pu1_buf;
97     WORD32 buf_size;
98 
99     num_bufs = MAX(DEFAULT_NUM_OUTPUT_BUFS,
100                    ps_app_ctxt->s_get_buf_info_op.s_ive_op.u4_min_out_bufs);
101     num_bufs = MIN(DEFAULT_MAX_OUTPUT_BUFS, num_bufs);
102 
103     buf_size = ps_app_ctxt->s_get_buf_info_op.s_ive_op.au4_min_out_buf_size[0];
104 
105     /* Memset the output buffer array to set is_free to 0 */
106     memset(ps_app_ctxt->as_output_buf, 0,
107            sizeof(output_buf_t) * DEFAULT_MAX_OUTPUT_BUFS);
108 
109     for(i = 0; i < num_bufs; i++)
110     {
111         pu1_buf = (UWORD8 *)ih264a_aligned_malloc(16, buf_size);
112         if(NULL == pu1_buf)
113         {
114             CHAR ac_error[STRLENGTH];
115             sprintf(ac_error,
116                     "Allocation failed for output buffer of size %d\n",
117                     buf_size);
118             codec_exit(ac_error);
119         }
120         ps_app_ctxt->as_output_buf[i].pu1_buf = pu1_buf;
121         ps_app_ctxt->as_output_buf[i].u4_buf_size = buf_size;
122         ps_app_ctxt->as_output_buf[i].u4_is_free = 1;
123     }
124 }
125 
126 /**
127 **************************************************************************
128 * @brief free bitstream buffers
129 **************************************************************************
130 */
free_output(app_ctxt_t * ps_app_ctxt)131 void free_output(app_ctxt_t *ps_app_ctxt)
132 {
133     WORD32 num_bufs;
134     WORD32 i;
135 
136     num_bufs = MAX(DEFAULT_NUM_OUTPUT_BUFS,
137                    ps_app_ctxt->s_get_buf_info_op.s_ive_op.u4_min_out_bufs);
138     num_bufs = MIN(DEFAULT_MAX_OUTPUT_BUFS, num_bufs);
139     for(i = 0; i < num_bufs; i++)
140     {
141         ih264a_aligned_free(ps_app_ctxt->as_output_buf[i].pu1_buf);
142     }
143 }
144 
145