xref: /aosp_15_r20/external/libavc/examples/svcenc/output.c (revision 495ae853bb871d1e5a258cb02c2cc13cde8ddb9a)
1 /******************************************************************************
2  *
3  * Copyright (C) 2022 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 #include <stdlib.h>
22 #include <stdio.h>
23 #include <assert.h>
24 #include <string.h>
25 
26 #include "ih264_typedefs.h"
27 #include "iv2.h"
28 #include "ive2.h"
29 #include "isvce.h"
30 #include "app.h"
31 
32 /*****************************************************************************/
33 /* Constant Macros                                                           */
34 /*****************************************************************************/
35 #define PEAK_WINDOW_SIZE 8
36 /*****************************************************************************/
37 /*  Macros                                                                   */
38 /*****************************************************************************/
39 /*****************************************************************************/
40 /*  Function Declarations                                                    */
41 /*****************************************************************************/
write_output(FILE * fp,UWORD8 * pu1_buf,WORD32 num_bytes)42 IV_STATUS_T write_output(FILE *fp, UWORD8 *pu1_buf, WORD32 num_bytes)
43 {
44     WORD32 bytes;
45 
46     bytes = (WORD32) fwrite(pu1_buf, sizeof(UWORD8), num_bytes, fp);
47     if(bytes != num_bytes) return IV_FAIL;
48     fflush(fp);
49 
50     return IV_SUCCESS;
51 }
52 
allocate_output(app_ctxt_t * ps_app_ctxt)53 void allocate_output(app_ctxt_t *ps_app_ctxt)
54 {
55     WORD32 num_bufs;
56     WORD32 i;
57     UWORD8 *pu1_buf;
58     WORD32 buf_size;
59     num_bufs =
60         MAX(DEFAULT_NUM_OUTPUT_BUFS, ps_app_ctxt->s_get_buf_info_op.s_ive_op.u4_min_out_bufs);
61     num_bufs = MIN(DEFAULT_MAX_OUTPUT_BUFS, num_bufs);
62 
63     buf_size = ps_app_ctxt->s_get_buf_info_op.s_ive_op.au4_min_out_buf_size[0];
64     /* Memset the output buffer array to set is_free to 0 */
65     memset(ps_app_ctxt->as_output_buf, 0, sizeof(ps_app_ctxt->as_output_buf));
66 
67     for(i = 0; i < num_bufs; i++)
68     {
69         pu1_buf = (UWORD8 *) isvca_aligned_malloc(16, buf_size);
70         if(NULL == pu1_buf)
71         {
72             CHAR ac_error[2 * STRLENGTH];
73             snprintf(ac_error, sizeof(ac_error) - 1,
74                      "Allocation failed for output buffer of size %d\n", buf_size);
75             codec_exit(ac_error);
76         }
77         ps_app_ctxt->as_output_buf[i].pu1_buf = pu1_buf;
78         ps_app_ctxt->as_output_buf[i].u4_buf_size = buf_size;
79         ps_app_ctxt->as_output_buf[i].u4_is_free = 1;
80     }
81 }
82 
free_output(app_ctxt_t * ps_app_ctxt)83 void free_output(app_ctxt_t *ps_app_ctxt)
84 {
85     WORD32 num_bufs;
86     WORD32 i;
87 
88     num_bufs =
89         MAX(DEFAULT_NUM_OUTPUT_BUFS, ps_app_ctxt->s_get_buf_info_op.s_ive_op.u4_min_out_bufs);
90     num_bufs = MIN(DEFAULT_MAX_OUTPUT_BUFS, num_bufs);
91     for(i = 0; i < num_bufs; i++)
92     {
93         isvca_aligned_free(ps_app_ctxt->as_output_buf[i].pu1_buf);
94     }
95 }
96