xref: /aosp_15_r20/external/libavc/examples/svcenc/main.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 <stdio.h>
22 #include <stdlib.h>
23 #include <stddef.h>
24 #include <assert.h>
25 #include <string.h>
26 #include <sys/time.h>
27 
28 #include "ih264_typedefs.h"
29 #include "iv2.h"
30 #include "ive2.h"
31 #include "isvce.h"
32 #include "app.h"
33 #include "psnr.h"
34 
35 #ifndef MD5_DISABLE
36 void calc_md5_cksum(UWORD8 *pu1_inbuf, UWORD32 u4_stride, UWORD32 u4_width, UWORD32 u4_height,
37                     UWORD8 *pu1_cksum_p);
38 #else
39 #define calc_md5_cksum(a, b, c, d, e)
40 #endif
41 
42 /*****************************************************************************/
43 /* Enums                                                                     */
44 /*****************************************************************************/
45 typedef enum
46 {
47     INVALID,
48     HELP,
49     INPUT_FILE,
50     OUTPUT_FILE,
51     RECON_FILE,
52     RECON_ENABLE,
53     NALU_INFO_EXPORT_ENABLE,
54     NALU_INFO_CSV,
55     CHKSUM_ENABLE,
56     CHKSUM_FILE,
57     INPUT_CHROMA_FORMAT,
58     RECON_CHROMA_FORMAT,
59     MAX_WD,
60     MAX_HT,
61     WD,
62     HT,
63     MAX_LEVEL,
64     ENC_SPEED,
65     ME_SPEED,
66     START_FRM,
67     NUM_FRMS,
68     MAX_FRAMERATE,
69     SRC_FRAMERATE,
70     TGT_FRAMERATE,
71     RC,
72     MAX_BITRATE,
73     BITRATE,
74     I_QP,
75     P_QP,
76     B_QP,
77     I_QP_MAX,
78     P_QP_MAX,
79     B_QP_MAX,
80     I_QP_MIN,
81     P_QP_MIN,
82     B_QP_MIN,
83     ENTROPY,
84     AIR,
85     AIR_REFRESH_PERIOD,
86     ARCH,
87     SOC,
88     NUMCORES,
89     PRE_ENC_ME,
90     PRE_ENC_IPE,
91     HPEL,
92     QPEL,
93     SRCH_RNG_X,
94     SRCH_RNG_Y,
95     I_INTERVAL,
96     IDR_INTERVAL,
97     B_FRMS,
98     NUM_B_FRMS,
99     DISABLE_DBLK,
100     PROFILE,
101     FAST_SAD,
102     ALT_REF,
103     DISABLE_DEBLOCK_LEVEL,
104     PSNR,
105     SLICE_MODE,
106     SLICE_PARAM,
107     CONFIG,
108     LOOPBACK,
109     VBV_DELAY,
110     INTRA_4x4_ENABLE,
111     MB_INFO_FILE,
112     MB_INFO_TYPE,
113     PIC_INFO_FILE,
114     PIC_INFO_TYPE,
115     DISABLE_VUI,
116     NUM_TEMPORAL_LAYERS,
117     NUM_SPATIAL_LAYERS,
118     SPATIAL_RES_RATIO,
119 } ARGUMENT_T;
120 
121 typedef struct
122 {
123     CHAR argument_shortname[8];
124     CHAR argument_name[128];
125     ARGUMENT_T argument;
126     CHAR description[512];
127 } argument_t;
128 
129 static const argument_t argument_mapping[] = {
130     {"--", "--help", HELP, "Print this help\n"},
131     {"-i", "--input", INPUT_FILE, "Input file\n"},
132     {"-o", "--output", OUTPUT_FILE, "Output file\n"},
133     {"--", "--recon_enable", RECON_ENABLE, "Recon enable flag\n"},
134     {"--", "--nalu_info_export", NALU_INFO_EXPORT_ENABLE, "Enable NALU Info export\n"},
135     {"--", "--nalu_info_csv", NALU_INFO_CSV, "Path to NALU Info CSV File\n"},
136     {"-r", "--recon", RECON_FILE, "Recon file \n"},
137     {"--", "--input_chroma_format", INPUT_CHROMA_FORMAT,
138      "Input Chroma format Supported values YUV_420P, YUV_420SP_UV, "
139      "YUV_420SP_VU\n"},
140     {"--", "--recon_chroma_format", RECON_CHROMA_FORMAT,
141      "Recon Chroma format Supported values YUV_420P, YUV_420SP_UV, "
142      "YUV_420SP_VU\n"},
143     {"-w", "--width", WD, "Width of input  file\n"},
144     {"-h", "--height", HT, "Height file\n"},
145     {"--", "--start_frame", START_FRM, "Starting frame number\n"},
146     {"-f", "--num_frames", NUM_FRMS, "Number of frames to be encoded\n"},
147     {"--", "--rc", RC,
148      "Rate control mode 0: Constant Qp, 1: Storage, 2: CBR non low delay, 3: "
149      "CBR low delay \n"},
150     {"--", "--max_framerate", MAX_FRAMERATE, "Maximum frame rate \n"},
151     {"--", "--tgt_framerate", TGT_FRAMERATE, "Target frame rate \n"},
152     {"--", "--src_framerate", SRC_FRAMERATE, "Source frame rate \n"},
153     {"--", "--i_interval", I_INTERVAL, "Intra frame interval \n"},
154     {"--", "--idr_interval", IDR_INTERVAL, "IDR frame interval \n"},
155     {"--", "--bframes", NUM_B_FRMS, "Maximum number of consecutive B frames \n"},
156     {"--", "--speed", ENC_SPEED, "Encoder speed preset 0 (slowest) and 100 (fastest)\n"},
157     {"--", "--me_speed", ME_SPEED, "Encoder speed preset 0 (slowest) and 100 (fastest)\n"},
158     {"--", "--fast_sad", FAST_SAD, " Flag for faster sad execution\n"},
159     {"--", "--alt_ref", ALT_REF, "Flag to enable alternate refernce frames\n"},
160     {"--", "--hpel", HPEL, "Flag to enable/disable Quarter pel estimation \n"},
161     {"--", "--qpel", QPEL, "Flag to enable/disable Quarter pel estimation \n"},
162     {"--", "--disable_deblock_level", DISABLE_DEBLOCK_LEVEL,
163      "Disable deblock level - 0 : Enables deblock completely, 1: enables for I "
164      "and 8th frame , 2: Enables for I only, 3 : disables completely\n"},
165     {"--", "--search_range_x", SRCH_RNG_X, "Search range for X  \n"},
166     {"--", "--search_range_y", SRCH_RNG_Y, "Search range for Y \n"},
167     {"--", "--psnr", PSNR, "Enable PSNR computation (Disable while benchmarking performance) \n"},
168     {"--", "--pre_enc_me", PRE_ENC_ME, "Flag to enable/disable Pre Enc Motion Estimation\n"},
169     {"--", "--pre_enc_ipe", PRE_ENC_IPE,
170      "Flag to enable/disable Pre Enc Intra prediction Estimation\n"},
171     {"-n", "--num_cores", NUMCORES, "Number of cores to be used\n"},
172     {"--", "--adaptive_intra_refresh", AIR, "Adaptive Intra Refresh enable/disable\n"},
173     {"--", "--air_refresh_period", AIR_REFRESH_PERIOD, "adaptive intra refresh period\n"},
174     {"--", "--slice", SLICE_MODE,
175      "Slice mode-  0 :No slice, 1: Bytes per slice, 2: MB/CTB per slice  \n"},
176     {"--", "--slice_param", SLICE_PARAM,
177      "Slice param value based on slice mode. Slice mode of 1 implies number of "
178      "bytes per slice, 2 implies number of MBs/CTBs, for 0 value is neglected "
179      "\n"},
180     {"--", "--max_wd", MAX_WD, "Maximum width (Default: 1920) \n"},
181     {"--", "--max_ht", MAX_HT, "Maximum height (Default: 1088)\n"},
182     {"--", "--max_level", MAX_LEVEL, "Maximum Level (Default: 50)\n"},
183     {"--", "--arch", ARCH,
184      "Set Architecture. Supported values  ARCH_GENERIC, ARM_A9Q, ARM_A7, "
185      "ARM_A5, "
186      "ARM_NEONINTR, X86_SSSE3, X86_SSE42 \n"},
187     {"--", "--soc", SOC, "Set SOC. Supported values  GENERIC, HISI_37X \n"},
188     {"--", "--chksum", CHKSUM_FILE, "Save Check sum file for recon data\n"},
189     {"--", "--chksum_enable", CHKSUM_ENABLE, "Recon MD5 Checksum file\n"},
190     {"-c", "--config", CONFIG, "config file (Default: enc.cfg)\n"},
191     {"--", "--loopback", LOOPBACK, "Enable encoding in a loop\n"},
192     {"--", "--profile", PROFILE, "Profile mode: Supported values BASE, MAIN, HIGH\n"},
193     {"--", "--max_bitrate", MAX_BITRATE, "Max bitrate\n"},
194     {"--", "--bitrate", BITRATE, "Target bitrate\n"},
195     {"--", "--qp_i", I_QP, "QP for I frames\n"},
196     {"--", "--qp_p", P_QP, "QP for P frames\n"},
197     {"--", "--qp_b", B_QP, "QP for B frames\n"},
198     {"--", "--qp_i_max", I_QP_MAX, "Max QP for I frames\n"},
199     {"--", "--qp_p_max", P_QP_MAX, "Max QP for P frames\n"},
200     {"--", "--qp_b_max", B_QP_MAX, "Max QP for B frames\n"},
201     {"--", "--qp_i_min", I_QP_MIN, "Min QP for I frames\n"},
202     {"--", "--qp_p_min", P_QP_MIN, "Min QP for P frames\n"},
203     {"--", "--qp_b_min", B_QP_MIN, "Min QP for B frames\n"},
204     {"--", "--entropy", ENTROPY, "Entropy coding mode(0: CAVLC or 1: CABAC)\n"},
205     {"--", "--vbv_delay", VBV_DELAY, "VBV buffer delay\n"},
206     {"-i4", "--intra_4x4_enable", INTRA_4x4_ENABLE, "Intra 4x4 enable \n"},
207     {"--", "--mb_info_file", MB_INFO_FILE, "MB info file\n"},
208     {"--", "--mb_info_type", MB_INFO_TYPE, "MB info type\n"},
209     {"--", "--pic_info_file", PIC_INFO_FILE, "Pic info file\n"},
210     {"--", "--pic_info_type", PIC_INFO_TYPE, "Pic info type\n"},
211     {"--", "--num_temporal_layers", NUM_TEMPORAL_LAYERS, "SVC Parameter : Num temporal layers\n"},
212     {"--", "--num_spatial_layers", NUM_SPATIAL_LAYERS, "SVC Parameter : Num spatial layers\n"},
213     {"--", "--spatial_res_ratio", SPATIAL_RES_RATIO,
214      "SVC Parameter : Resolution ratio between successive spatial layers\n"},
215     {"--", "--disable_vui", DISABLE_VUI, "disable vui\n"},
216 };
217 
isvca_aligned_malloc(WORD32 alignment,WORD32 size)218 void *isvca_aligned_malloc(WORD32 alignment, WORD32 size)
219 {
220     void *buf = NULL;
221 
222     if(0 != posix_memalign(&buf, alignment, size))
223     {
224         return NULL;
225     }
226     return buf;
227 }
228 
isvca_aligned_free(void * pv_buf)229 void isvca_aligned_free(void *pv_buf) { free(pv_buf); }
230 
231 /*****************************************************************************/
232 /*                                                                           */
233 /*  Function Name : codec_exit                                               */
234 /*                                                                           */
235 /*  Description   : handles unrecoverable errors                             */
236 /*  Inputs        : Error message                                            */
237 /*  Globals       : None                                                     */
238 /*  Processing    : Prints error message to console and exits.               */
239 /*  Outputs       : Error message to the console                             */
240 /*  Returns       : None                                                     */
241 /*                                                                           */
242 /*  Issues        :                                                          */
243 /*                                                                           */
244 /*  Revision History:                                                        */
245 /*                                                                           */
246 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
247 /*         07 06 2006   Sankar          Creation                             */
248 /*                                                                           */
249 /*****************************************************************************/
codec_exit(CHAR * pc_err_message)250 void codec_exit(CHAR *pc_err_message)
251 {
252     printf("%s\n", pc_err_message);
253     exit(-1);
254 }
255 
256 /*****************************************************************************/
257 /*                                                                           */
258 /*  Function Name : codec_exit                                               */
259 /*                                                                           */
260 /*  Description   : handles unrecoverable errors                             */
261 /*  Inputs        : Error message                                            */
262 /*  Globals       : None                                                     */
263 /*  Processing    : Prints error message to console and exits.               */
264 /*  Outputs       : Error mesage to the console                              */
265 /*  Returns       : None                                                     */
266 /*                                                                           */
267 /*  Issues        :                                                          */
268 /*                                                                           */
269 /*  Revision History:                                                        */
270 /*                                                                           */
271 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
272 /*         07 06 2006   Sankar          Creation                             */
273 /*                                                                           */
274 /*****************************************************************************/
get_chroma_fmt(CHAR * value)275 IV_COLOR_FORMAT_T get_chroma_fmt(CHAR *value)
276 {
277     IV_COLOR_FORMAT_T e_chroma_format;
278     if((strcmp(value, "YUV_420P")) == 0)
279         e_chroma_format = IV_YUV_420P;
280     else if((strcmp(value, "YUV_422ILE")) == 0)
281         e_chroma_format = IV_YUV_422ILE;
282     else if((strcmp(value, "RGB_565")) == 0)
283         e_chroma_format = IV_RGB_565;
284     else if((strcmp(value, "RGBA_8888")) == 0)
285         e_chroma_format = IV_RGBA_8888;
286     else if((strcmp(value, "YUV_420SP_UV")) == 0)
287         e_chroma_format = IV_YUV_420SP_UV;
288     else if((strcmp(value, "YUV_420SP_VU")) == 0)
289         e_chroma_format = IV_YUV_420SP_VU;
290     else
291     {
292         printf("\nInvalid colour format setting it to IV_YUV_420P\n");
293         e_chroma_format = IV_YUV_420P;
294     }
295     return e_chroma_format;
296 }
297 
298 /*****************************************************************************/
299 /*                                                                           */
300 /*  Function Name : codec_exit                                               */
301 /*                                                                           */
302 /*  Description   : handles unrecoverable errors                             */
303 /*  Inputs        : Error message                                            */
304 /*  Globals       : None                                                     */
305 /*  Processing    : Prints error message to console and exits.               */
306 /*  Outputs       : Error mesage to the console                              */
307 /*  Returns       : None                                                     */
308 /*                                                                           */
309 /*  Issues        :                                                          */
310 /*                                                                           */
311 /*  Revision History:                                                        */
312 /*                                                                           */
313 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
314 /*         07 06 2006   Sankar          Creation                             */
315 /*                                                                           */
316 /*****************************************************************************/
get_speed_preset(CHAR * value)317 IVE_SPEED_CONFIG get_speed_preset(CHAR *value)
318 {
319     IVE_SPEED_CONFIG e_enc_speed_preset;
320     if((strcmp(value, "CONFIG")) == 0)
321         e_enc_speed_preset = IVE_CONFIG;
322     else if((strcmp(value, "SLOWEST")) == 0)
323         e_enc_speed_preset = IVE_SLOWEST;
324     else if((strcmp(value, "NORMAL")) == 0)
325         e_enc_speed_preset = IVE_NORMAL;
326     else if((strcmp(value, "FAST")) == 0)
327         e_enc_speed_preset = IVE_FAST;
328     else if((strcmp(value, "HIGH_SPEED")) == 0)
329         e_enc_speed_preset = IVE_HIGH_SPEED;
330     else if((strcmp(value, "FASTEST")) == 0)
331         e_enc_speed_preset = IVE_FASTEST;
332     else
333     {
334         printf("\nInvalid speed preset, setting it to IVE_FASTEST\n");
335         e_enc_speed_preset = IVE_FASTEST;
336     }
337     return e_enc_speed_preset;
338 }
339 
340 /*****************************************************************************/
341 /*                                                                           */
342 /*  Function Name : print_usage                                              */
343 /*                                                                           */
344 /*  Description   : Prints argument format                                   */
345 /*                                                                           */
346 /*                                                                           */
347 /*  Inputs        :                                                          */
348 /*  Globals       :                                                          */
349 /*  Processing    : Prints argument format                                   */
350 /*                                                                           */
351 /*  Outputs       :                                                          */
352 /*  Returns       :                                                          */
353 /*                                                                           */
354 /*  Issues        :                                                          */
355 /*                                                                           */
356 /*  Revision History:                                                        */
357 /*                                                                           */
358 /*         DD MM YYYY   Author(s)       Changes                              */
359 /*         07 09 2012   100189          Initial Version                      */
360 /*                                                                           */
361 /*****************************************************************************/
362 
print_usage(void)363 void print_usage(void)
364 {
365     WORD32 i = 0;
366     WORD32 num_entries = sizeof(argument_mapping) / sizeof(argument_t);
367     printf("\nUsage:\n");
368     while(i < num_entries)
369     {
370         printf("%-32s\t %s", argument_mapping[i].argument_name, argument_mapping[i].description);
371         i++;
372     }
373 }
374 
375 /*****************************************************************************/
376 /*                                                                           */
377 /*  Function Name : get_argument                                             */
378 /*                                                                           */
379 /*  Description   : Gets argument for a given string                         */
380 /*                                                                           */
381 /*                                                                           */
382 /*  Inputs        : name                                                     */
383 /*  Globals       :                                                          */
384 /*  Processing    : Searches the given string in the array and returns       */
385 /*                  appropriate argument ID                                  */
386 /*                                                                           */
387 /*  Outputs       : Argument ID                                              */
388 /*  Returns       : Argument ID                                              */
389 /*                                                                           */
390 /*  Issues        :                                                          */
391 /*                                                                           */
392 /*  Revision History:                                                        */
393 /*                                                                           */
394 /*         DD MM YYYY   Author(s)       Changes                              */
395 /*         07 09 2012   100189          Initial Version                      */
396 /*                                                                           */
397 /*****************************************************************************/
get_argument(CHAR * name)398 ARGUMENT_T get_argument(CHAR *name)
399 {
400     WORD32 i = 0;
401     WORD32 num_entries = sizeof(argument_mapping) / sizeof(argument_t);
402     while(i < num_entries)
403     {
404         if((0 == strcmp(argument_mapping[i].argument_name, name)) ||
405            ((0 == strcmp(argument_mapping[i].argument_shortname, name)) &&
406             (0 != strcmp(argument_mapping[i].argument_shortname, "--"))))
407         {
408             return argument_mapping[i].argument;
409         }
410         i++;
411     }
412     return INVALID;
413 }
414 
415 /*****************************************************************************/
416 /*                                                                           */
417 /*  Function Name : get_argument                                             */
418 /*                                                                           */
419 /*  Description   : Gets argument for a given string                         */
420 /*                                                                           */
421 /*                                                                           */
422 /*  Inputs        : name                                                     */
423 /*  Globals       :                                                          */
424 /*  Processing    : Searches the given string in the array and returns       */
425 /*                  appropriate argument ID                                  */
426 /*                                                                           */
427 /*  Outputs       : Argument ID                                              */
428 /*  Returns       : Argument ID                                              */
429 /*                                                                           */
430 /*  Issues        :                                                          */
431 /*                                                                           */
432 /*  Revision History:                                                        */
433 /*                                                                           */
434 /*         DD MM YYYY   Author(s)       Changes                              */
435 /*         07 09 2012   100189          Initial Version                      */
436 /*                                                                           */
437 /*****************************************************************************/
parse_argument(app_ctxt_t * ps_app_ctxt,CHAR * argument,CHAR * value)438 void parse_argument(app_ctxt_t *ps_app_ctxt, CHAR *argument, CHAR *value)
439 {
440     ARGUMENT_T arg = get_argument(argument);
441 
442     switch(arg)
443     {
444         case HELP:
445             print_usage();
446             exit(-1);
447             break;
448         case SLICE_MODE:
449             sscanf(value, "%d", &ps_app_ctxt->u4_slice_mode);
450             break;
451         case SLICE_PARAM:
452             sscanf(value, "%d", &ps_app_ctxt->u4_slice_param);
453             break;
454         case INPUT_FILE:
455             sscanf(value, "%s", ps_app_ctxt->ac_ip_fname);
456             break;
457 
458         case OUTPUT_FILE:
459             sscanf(value, "%s", ps_app_ctxt->ac_op_fname);
460             break;
461 
462         case RECON_FILE:
463             sscanf(value, "%s", ps_app_ctxt->ac_recon_fname);
464             break;
465 
466         case RECON_ENABLE:
467             sscanf(value, "%d", &ps_app_ctxt->u4_recon_enable);
468             break;
469 
470         case NALU_INFO_EXPORT_ENABLE:
471             sscanf(value, "%d", &ps_app_ctxt->u4_nalu_info_export_enable);
472             break;
473 
474         case NALU_INFO_CSV:
475             sscanf(value, "%s", ps_app_ctxt->ac_nalu_info_csv_fname);
476             break;
477 
478         case CHKSUM_FILE:
479             sscanf(value, "%s", ps_app_ctxt->ac_chksum_fname);
480             break;
481 
482         case CHKSUM_ENABLE:
483             sscanf(value, "%d", &ps_app_ctxt->u4_chksum_enable);
484             break;
485 
486         case MB_INFO_FILE:
487             sscanf(value, "%s", ps_app_ctxt->ac_mb_info_fname);
488             break;
489 
490         case MB_INFO_TYPE:
491             sscanf(value, "%d", &ps_app_ctxt->u4_mb_info_type);
492             break;
493 
494         case PIC_INFO_FILE:
495             sscanf(value, "%s", ps_app_ctxt->ac_pic_info_fname);
496             break;
497 
498         case PIC_INFO_TYPE:
499             sscanf(value, "%d", &ps_app_ctxt->u4_pic_info_type);
500             break;
501 
502         case INPUT_CHROMA_FORMAT:
503             ps_app_ctxt->e_inp_color_fmt = get_chroma_fmt(value);
504             break;
505 
506         case RECON_CHROMA_FORMAT:
507             ps_app_ctxt->e_recon_color_fmt = get_chroma_fmt(value);
508             break;
509 
510         case MAX_WD:
511             sscanf(value, "%d", &ps_app_ctxt->u4_max_wd);
512             break;
513 
514         case MAX_HT:
515             sscanf(value, "%d", &ps_app_ctxt->u4_max_ht);
516             break;
517 
518         case WD:
519             sscanf(value, "%d", &ps_app_ctxt->u4_wd);
520             break;
521 
522         case HT:
523             sscanf(value, "%d", &ps_app_ctxt->u4_ht);
524             break;
525 
526         case MAX_LEVEL:
527             sscanf(value, "%d", &ps_app_ctxt->u4_max_level);
528             break;
529 
530         case ENC_SPEED:
531             ps_app_ctxt->u4_enc_speed = get_speed_preset(value);
532             break;
533 
534         case ME_SPEED:
535             sscanf(value, "%d", &ps_app_ctxt->u4_me_speed);
536             break;
537 
538         case START_FRM:
539             sscanf(value, "%d", &ps_app_ctxt->u4_start_frm);
540             break;
541 
542         case NUM_FRMS:
543             sscanf(value, "%d", &ps_app_ctxt->u4_max_num_frms);
544             break;
545 
546         case MAX_FRAMERATE:
547             sscanf(value, "%d", &ps_app_ctxt->u4_max_frame_rate);
548             if(ps_app_ctxt->u4_max_frame_rate <= 0)
549                 ps_app_ctxt->u4_max_frame_rate = DEFAULT_MAX_FRAMERATE;
550             break;
551 
552         case SRC_FRAMERATE:
553             sscanf(value, "%d", &ps_app_ctxt->u4_src_frame_rate);
554             if(ps_app_ctxt->u4_src_frame_rate <= 0)
555                 ps_app_ctxt->u4_src_frame_rate = DEFAULT_SRC_FRAME_RATE;
556             break;
557 
558         case TGT_FRAMERATE:
559             sscanf(value, "%d", &ps_app_ctxt->u4_tgt_frame_rate);
560             if(ps_app_ctxt->u4_tgt_frame_rate <= 0)
561                 ps_app_ctxt->u4_tgt_frame_rate = DEFAULT_TGT_FRAME_RATE;
562             break;
563 
564         case RC:
565             sscanf(value, "%d", &ps_app_ctxt->u4_rc);
566             break;
567 
568         case ENTROPY:
569             sscanf(value, "%d", &ps_app_ctxt->u4_entropy_coding_mode);
570             break;
571 
572         case AIR:
573             sscanf(value, "%d", &ps_app_ctxt->u4_air);
574             break;
575 
576         case ARCH:
577             if((strcmp(value, "ARCH_GENERIC")) == 0)
578                 ps_app_ctxt->e_arch = ARCH_X86_GENERIC;
579             else if((strcmp(value, "ARM_A9Q")) == 0)
580                 ps_app_ctxt->e_arch = ARCH_ARM_A9Q;
581             else if((strcmp(value, "ARM_A7")) == 0)
582                 ps_app_ctxt->e_arch = ARCH_ARM_A7;
583             else if((strcmp(value, "ARM_A5")) == 0)
584                 ps_app_ctxt->e_arch = ARCH_ARM_A5;
585             else if((strcmp(value, "ARM_NEONINTR")) == 0)
586                 ps_app_ctxt->e_arch = ARCH_ARM_NEONINTR;
587             else if((strcmp(value, "X86_SSSE3")) == 0)
588                 ps_app_ctxt->e_arch = ARCH_X86_SSSE3;
589             else if((strcmp(value, "X86_SSE42")) == 0)
590                 ps_app_ctxt->e_arch = ARCH_X86_SSE42;
591             else if((strcmp(value, "ARM_A53")) == 0)
592                 ps_app_ctxt->e_arch = ARCH_ARM_A53;
593             else if((strcmp(value, "ARM_A57")) == 0)
594                 ps_app_ctxt->e_arch = ARCH_ARM_A57;
595             else if((strcmp(value, "ARM_V8_NEON")) == 0)
596                 ps_app_ctxt->e_arch = ARCH_ARM_V8_NEON;
597             else
598             {
599                 printf("\nInvalid Arch. Setting it to ARM_A9Q\n");
600                 ps_app_ctxt->e_arch = ARCH_ARM_A9Q;
601             }
602 
603             break;
604         case SOC:
605             if((strcmp(value, "GENERIC")) == 0)
606                 ps_app_ctxt->e_soc = SOC_GENERIC;
607             else if((strcmp(value, "HISI_37X")) == 0)
608                 ps_app_ctxt->e_soc = SOC_HISI_37X;
609             else
610             {
611                 ps_app_ctxt->e_soc = SOC_GENERIC;
612             }
613             break;
614 
615         case NUMCORES:
616             sscanf(value, "%d", &ps_app_ctxt->u4_num_cores);
617             break;
618 
619         case LOOPBACK:
620             sscanf(value, "%d", &ps_app_ctxt->u4_loopback);
621             break;
622 
623         case PRE_ENC_ME:
624             sscanf(value, "%d", &ps_app_ctxt->u4_pre_enc_me);
625             break;
626 
627         case PRE_ENC_IPE:
628             sscanf(value, "%d", &ps_app_ctxt->u4_pre_enc_ipe);
629             break;
630 
631         case HPEL:
632             sscanf(value, "%d", &ps_app_ctxt->u4_hpel);
633             break;
634 
635         case QPEL:
636             sscanf(value, "%d", &ps_app_ctxt->u4_qpel);
637             break;
638 
639         case SRCH_RNG_X:
640             sscanf(value, "%d", &ps_app_ctxt->u4_srch_rng_x);
641             break;
642 
643         case SRCH_RNG_Y:
644             sscanf(value, "%d", &ps_app_ctxt->u4_srch_rng_y);
645             break;
646 
647         case I_INTERVAL:
648             sscanf(value, "%d", &ps_app_ctxt->u4_i_interval);
649             break;
650 
651         case IDR_INTERVAL:
652             sscanf(value, "%d", &ps_app_ctxt->u4_idr_interval);
653             break;
654 
655         case NUM_B_FRMS:
656             sscanf(value, "%d", &ps_app_ctxt->u4_num_bframes);
657             break;
658 
659         case DISABLE_DEBLOCK_LEVEL:
660             sscanf(value, "%d", &ps_app_ctxt->u4_disable_deblock_level);
661             break;
662 
663         case FAST_SAD:
664             sscanf(value, "%d", &ps_app_ctxt->u4_enable_fast_sad);
665             break;
666 
667         case ALT_REF:
668             sscanf(value, "%d", &ps_app_ctxt->u4_enable_alt_ref);
669             break;
670 
671         case AIR_REFRESH_PERIOD:
672             sscanf(value, "%d", &ps_app_ctxt->u4_air_refresh_period);
673             break;
674 
675         case PROFILE:
676             if((strcmp(value, "BASE")) == 0)
677                 ps_app_ctxt->e_profile = IV_PROFILE_BASE;
678             else if((strcmp(value, "MAIN")) == 0)
679                 ps_app_ctxt->e_profile = IV_PROFILE_MAIN;
680             else if((strcmp(value, "HIGH")) == 0)
681                 ps_app_ctxt->e_profile = IV_PROFILE_HIGH;
682             else
683             {
684                 printf("\nInvalid profile. Setting it to BASE\n");
685                 ps_app_ctxt->e_profile = IV_PROFILE_BASE;
686             }
687             break;
688 
689         case PSNR:
690             sscanf(value, "%d", &ps_app_ctxt->u4_psnr_enable);
691             break;
692 
693         case INTRA_4x4_ENABLE:
694             sscanf(value, "%d", &ps_app_ctxt->u4_enable_intra_4x4);
695             break;
696 
697         case DISABLE_VUI:
698             sscanf(value, "%d", &ps_app_ctxt->u4_use_default_vui);
699             break;
700 
701         case NUM_TEMPORAL_LAYERS:
702         {
703             sscanf(value, "%hhu", &ps_app_ctxt->u1_num_temporal_layers);
704             break;
705         }
706 
707         case NUM_SPATIAL_LAYERS:
708         {
709             sscanf(value, "%hhu", &ps_app_ctxt->u1_num_spatial_layers);
710             break;
711         }
712 
713         case SPATIAL_RES_RATIO:
714         {
715             sscanf(value, "%lf", &ps_app_ctxt->d_spatial_res_ratio);
716             break;
717         }
718 
719         case MAX_BITRATE:
720         case BITRATE:
721         case I_QP:
722         case I_QP_MAX:
723         case I_QP_MIN:
724         case P_QP:
725         case P_QP_MAX:
726         case P_QP_MIN:
727         case B_QP:
728         case B_QP_MAX:
729         case B_QP_MIN:
730         case VBV_DELAY:
731             break;
732 
733         case INVALID:
734         default:
735             printf("Ignoring argument :  %s\n", argument);
736             break;
737     }
738 }
739 
parse_rc_argument(app_ctxt_t * ps_app_ctxt,CHAR * argument,CHAR * value)740 void parse_rc_argument(app_ctxt_t *ps_app_ctxt, CHAR *argument, CHAR *value)
741 {
742 #define ITERATE_TO_NEXT_ARG()                                                     \
743     while(*value != ',' && *value != '-' && *value != '\0')                       \
744     {                                                                             \
745         value++;                                                                  \
746     }                                                                             \
747     if((*value == '-' || *value == '\0') && i + 1 < u1_num_spatial_layers) break; \
748     value++;
749 
750     ARGUMENT_T arg;
751     int i = 0;
752     UWORD8 u1_num_spatial_layers = ps_app_ctxt->u1_num_spatial_layers;
753 
754     arg = get_argument(argument);
755     switch(arg)
756     {
757         case MAX_BITRATE:
758             for(i = 0; i < u1_num_spatial_layers; i++)
759             {
760                 sscanf(value, "%d", &ps_app_ctxt->pu4_max_bitrate[i]);
761                 ITERATE_TO_NEXT_ARG();
762             }
763             break;
764 
765         case BITRATE:
766             for(i = 0; i < u1_num_spatial_layers; i++)
767             {
768                 sscanf(value, "%d", &ps_app_ctxt->pu4_bitrate[i]);
769                 ITERATE_TO_NEXT_ARG();
770             }
771             break;
772 
773         case I_QP:
774             for(i = 0; i < u1_num_spatial_layers; i++)
775             {
776                 sscanf(value, "%d", &ps_app_ctxt->pu4_i_qp[i]);
777                 ITERATE_TO_NEXT_ARG();
778             }
779             break;
780 
781         case I_QP_MAX:
782             for(i = 0; i < u1_num_spatial_layers; i++)
783             {
784                 sscanf(value, "%d", &ps_app_ctxt->pu4_i_qp_max[i]);
785                 ITERATE_TO_NEXT_ARG();
786             }
787             break;
788 
789         case I_QP_MIN:
790             for(i = 0; i < u1_num_spatial_layers; i++)
791             {
792                 sscanf(value, "%d", &ps_app_ctxt->pu4_i_qp_min[i]);
793                 ITERATE_TO_NEXT_ARG();
794             }
795             break;
796 
797         case P_QP:
798             for(i = 0; i < u1_num_spatial_layers; i++)
799             {
800                 sscanf(value, "%d", &ps_app_ctxt->pu4_p_qp[i]);
801                 ITERATE_TO_NEXT_ARG();
802             }
803             break;
804 
805         case P_QP_MAX:
806             for(i = 0; i < u1_num_spatial_layers; i++)
807             {
808                 sscanf(value, "%d", &ps_app_ctxt->pu4_p_qp_max[i]);
809                 ITERATE_TO_NEXT_ARG();
810             }
811             break;
812 
813         case P_QP_MIN:
814             for(i = 0; i < u1_num_spatial_layers; i++)
815             {
816                 sscanf(value, "%d", &ps_app_ctxt->pu4_p_qp_min[i]);
817                 ITERATE_TO_NEXT_ARG();
818             }
819             break;
820 
821         case B_QP:
822             for(i = 0; i < u1_num_spatial_layers; i++)
823             {
824                 sscanf(value, "%d", &ps_app_ctxt->pu4_b_qp[i]);
825                 ITERATE_TO_NEXT_ARG();
826             }
827             break;
828 
829         case B_QP_MAX:
830             for(i = 0; i < u1_num_spatial_layers; i++)
831             {
832                 sscanf(value, "%d", &ps_app_ctxt->pu4_b_qp_max[i]);
833                 ITERATE_TO_NEXT_ARG();
834             }
835             break;
836 
837         case B_QP_MIN:
838             for(i = 0; i < u1_num_spatial_layers; i++)
839             {
840                 sscanf(value, "%d", &ps_app_ctxt->pu4_b_qp_min[i]);
841                 ITERATE_TO_NEXT_ARG();
842             }
843             break;
844 
845         case VBV_DELAY:
846             for(i = 0; i < u1_num_spatial_layers; i++)
847             {
848                 sscanf(value, "%d", &ps_app_ctxt->pu4_vbv_buffer_delay[i]);
849                 ITERATE_TO_NEXT_ARG();
850             }
851             break;
852 
853         default:
854             break;
855     }
856 }
857 
858 /*****************************************************************************/
859 /*                                                                           */
860 /*  Function Name : read_cfg_file                                            */
861 /*                                                                           */
862 /*  Description   : Reads arguments from a configuration file                */
863 /*                                                                           */
864 /*                                                                           */
865 /*  Inputs        : ps_app_ctxt  : Application context                        */
866 /*                  fp_cfg_file : Configuration file handle                  */
867 /*  Globals       :                                                          */
868 /*  Processing    : Parses the arguments and fills in the application context*/
869 /*                                                                           */
870 /*  Outputs       : Arguments parsed                                         */
871 /*  Returns       : None                                                     */
872 /*                                                                           */
873 /*  Issues        :                                                          */
874 /*                                                                           */
875 /*  Revision History:                                                        */
876 /*                                                                           */
877 /*         DD MM YYYY   Author(s)       Changes                              */
878 /*         07 09 2012   100189          Initial Version                      */
879 /*                                                                           */
880 /*****************************************************************************/
read_cfg_file(app_ctxt_t * ps_app_ctxt,FILE * fp_cfg)881 void read_cfg_file(app_ctxt_t *ps_app_ctxt, FILE *fp_cfg)
882 {
883     CHAR line[STRLENGTH];
884     CHAR description[STRLENGTH];
885     CHAR value[STRLENGTH];
886     CHAR argument[STRLENGTH];
887 
888     while(0 == (feof(fp_cfg)))
889     {
890         int ret;
891         line[0] = '\0';
892         if(NULL == fgets(line, sizeof(line), fp_cfg)) break;
893         argument[0] = '\0';
894         /* Reading Input File Name */
895         ret = sscanf(line, "%s %s %s", argument, value, description);
896         if(ret < 2) continue;
897 
898         parse_argument(ps_app_ctxt, argument, value);
899     }
900 }
901 
902 /*****************************************************************************/
903 /*                                                                           */
904 /*  Function Name : read_cfg_file_rc_params                                  */
905 /*                                                                           */
906 /*  Description   : Reads RC specific arguments from a configuration file    */
907 /*                                                                           */
908 /*                                                                           */
909 /*  Inputs        : ps_app_ctxt  : Application context                        */
910 /*                  fp_cfg_file : Configuration file handle                  */
911 /*  Globals       :                                                          */
912 /*  Processing    : Parses the arguments and fills in the application context*/
913 /*                                                                           */
914 /*  Outputs       : Arguments parsed                                         */
915 /*  Returns       : None                                                     */
916 /*                                                                           */
917 /*  Issues        :                                                          */
918 /*                                                                           */
919 /*  Revision History:                                                        */
920 /*                                                                           */
921 /*         DD MM YYYY   Author(s)       Changes                              */
922 /*         07 09 2012   100189          Initial Version                      */
923 /*                                                                           */
924 /*****************************************************************************/
read_cfg_file_rc_params(app_ctxt_t * ps_app_ctxt,FILE * fp_cfg)925 void read_cfg_file_rc_params(app_ctxt_t *ps_app_ctxt, FILE *fp_cfg)
926 {
927     CHAR line[STRLENGTH];
928     CHAR description[STRLENGTH];
929     CHAR value[STRLENGTH];
930     CHAR argument[STRLENGTH];
931 
932     while(0 == (feof(fp_cfg)))
933     {
934         int ret;
935         line[0] = '\0';
936         if(NULL == fgets(line, sizeof(line), fp_cfg)) break;
937         argument[0] = '\0';
938         /* Reading Input File Name */
939         ret = sscanf(line, "%s %s %s", argument, value, description);
940         if(ret < 2) continue;
941 
942         parse_rc_argument(ps_app_ctxt, argument, value);
943     }
944 }
945 
invalid_argument_exit(CHAR * pc_err_message)946 void invalid_argument_exit(CHAR *pc_err_message)
947 {
948     print_usage();
949     codec_exit(pc_err_message);
950 }
951 
validate_params(app_ctxt_t * ps_app_ctxt)952 void validate_params(app_ctxt_t *ps_app_ctxt)
953 {
954     CHAR ac_error[STRLENGTH];
955 
956     if(ps_app_ctxt->ac_ip_fname[0] == '\0')
957     {
958         invalid_argument_exit("Specify input file");
959     }
960     if(ps_app_ctxt->ac_op_fname[0] == '\0')
961     {
962         invalid_argument_exit("Specify output file");
963     }
964     if((1 == ps_app_ctxt->u4_recon_enable) && (ps_app_ctxt->ac_recon_fname[0] == '\0'))
965     {
966         invalid_argument_exit("Specify recon file");
967     }
968     if((1 == ps_app_ctxt->u4_nalu_info_export_enable) &&
969        (ps_app_ctxt->ac_nalu_info_csv_fname[0] == '\0'))
970     {
971         invalid_argument_exit("Specify NALU Info CSV File");
972     }
973     if((1 == ps_app_ctxt->u4_chksum_enable) && (ps_app_ctxt->ac_chksum_fname[0] == '\0'))
974     {
975         invalid_argument_exit("Specify checksum file");
976     }
977     if(0 >= (WORD32) ps_app_ctxt->u4_wd)
978     {
979         snprintf(ac_error, sizeof(ac_error) - 1, "Invalid width: %d", ps_app_ctxt->u4_wd);
980         invalid_argument_exit(ac_error);
981     }
982     if(0 >= (WORD32) ps_app_ctxt->u4_ht)
983     {
984         snprintf(ac_error, sizeof(ac_error) - 1, "Invalid height: %d", ps_app_ctxt->u4_ht);
985         invalid_argument_exit(ac_error);
986     }
987 
988     if(0 == (WORD32) ps_app_ctxt->u4_max_num_frms)
989     {
990         snprintf(ac_error, sizeof(ac_error) - 1, "Invalid number of frames to be encoded: %d",
991                  ps_app_ctxt->u4_max_num_frms);
992         invalid_argument_exit(ac_error);
993     }
994     if((0 != (WORD32) ps_app_ctxt->u4_entropy_coding_mode) &&
995        (1 != (WORD32) ps_app_ctxt->u4_entropy_coding_mode))
996     {
997         snprintf(ac_error, sizeof(ac_error) - 1, "Invalid entropy codeing mode: %d",
998                  ps_app_ctxt->u4_entropy_coding_mode);
999         invalid_argument_exit(ac_error);
1000     }
1001 }
1002 
init_default_params(app_ctxt_t * ps_app_ctxt)1003 void init_default_params(app_ctxt_t *ps_app_ctxt)
1004 {
1005     ps_app_ctxt->ps_enc = NULL;
1006     ps_app_ctxt->ps_mem_rec = NULL;
1007     ps_app_ctxt->u4_num_mem_rec = DEFAULT_MEM_REC_CNT;
1008     ps_app_ctxt->u4_recon_enable = DEFAULT_RECON_ENABLE;
1009     ps_app_ctxt->u4_nalu_info_export_enable = DEFAULT_NALU_INFO_EXPORT_ENABLE;
1010     ps_app_ctxt->u4_chksum_enable = DEFAULT_CHKSUM_ENABLE;
1011     ps_app_ctxt->u4_mb_info_type = 0;
1012     ps_app_ctxt->u4_pic_info_type = 0;
1013     ps_app_ctxt->u4_mb_info_size = 0;
1014     ps_app_ctxt->u4_pic_info_size = 0;
1015     ps_app_ctxt->u4_start_frm = DEFAULT_START_FRM;
1016     ps_app_ctxt->u4_max_num_frms = DEFAULT_NUM_FRMS;
1017     ps_app_ctxt->avg_time = 0;
1018     ps_app_ctxt->u4_pics_cnt = 0;
1019     ps_app_ctxt->e_inp_color_fmt = DEFAULT_INP_COLOR_FMT;
1020     ps_app_ctxt->e_recon_color_fmt = DEFAULT_RECON_COLOR_FMT;
1021     ps_app_ctxt->e_arch = ARCH_X86_SSE42;
1022     ps_app_ctxt->e_soc = SOC_GENERIC;
1023     ps_app_ctxt->header_generated = 0;
1024     ps_app_ctxt->pv_codec_obj = NULL;
1025     ps_app_ctxt->u4_num_cores = DEFAULT_NUM_CORES;
1026     ps_app_ctxt->u4_pre_enc_me = 0;
1027     ps_app_ctxt->u4_pre_enc_ipe = 0;
1028     ps_app_ctxt->ac_ip_fname[0] = '\0';
1029     ps_app_ctxt->ac_op_fname[0] = '\0';
1030     ps_app_ctxt->ac_recon_fname[0] = '\0';
1031     ps_app_ctxt->ac_nalu_info_csv_fname[0] = '\0';
1032     ps_app_ctxt->ac_chksum_fname[0] = '\0';
1033     ps_app_ctxt->ac_mb_info_fname[0] = '\0';
1034     ps_app_ctxt->fp_ip = NULL;
1035     ps_app_ctxt->fp_op = NULL;
1036     ps_app_ctxt->fp_recon = NULL;
1037     ps_app_ctxt->fp_nalu_info = NULL;
1038     ps_app_ctxt->fp_chksum = NULL;
1039     ps_app_ctxt->fp_psnr_ip = NULL;
1040     ps_app_ctxt->fp_mb_info = NULL;
1041     ps_app_ctxt->fp_pic_info = NULL;
1042     ps_app_ctxt->u4_loopback = DEFAULT_LOOPBACK;
1043     ps_app_ctxt->u4_max_frame_rate = DEFAULT_MAX_FRAMERATE;
1044     ps_app_ctxt->u4_src_frame_rate = DEFAULT_SRC_FRAME_RATE;
1045     ps_app_ctxt->u4_tgt_frame_rate = DEFAULT_TGT_FRAME_RATE;
1046     ps_app_ctxt->u4_max_wd = DEFAULT_MAX_WD;
1047     ps_app_ctxt->u4_max_ht = DEFAULT_MAX_HT;
1048     ps_app_ctxt->u4_max_level = DEFAULT_MAX_LEVEL;
1049     ps_app_ctxt->u4_strd = DEFAULT_STRIDE;
1050     ps_app_ctxt->u4_wd = DEFAULT_WD;
1051     ps_app_ctxt->u4_ht = DEFAULT_HT;
1052     ps_app_ctxt->u4_psnr_enable = DEFAULT_PSNR_ENABLE;
1053     ps_app_ctxt->u4_enc_speed = IVE_SLOWEST;
1054     ps_app_ctxt->u4_me_speed = DEFAULT_ME_SPEED;
1055     ps_app_ctxt->u4_enable_fast_sad = DEFAULT_ENABLE_FAST_SAD;
1056     ps_app_ctxt->u4_enable_alt_ref = DEFAULT_ENABLE_ALT_REF;
1057     ps_app_ctxt->u4_rc = DEFAULT_RC;
1058     ps_app_ctxt->u4_num_bframes = DEFAULT_NUM_BFRAMES;
1059     ps_app_ctxt->u4_air = DEFAULT_AIR;
1060     ps_app_ctxt->u4_air_refresh_period = DEFAULT_AIR_REFRESH_PERIOD;
1061     ps_app_ctxt->u4_srch_rng_x = DEFAULT_SRCH_RNG_X;
1062     ps_app_ctxt->u4_srch_rng_y = DEFAULT_SRCH_RNG_Y;
1063     ps_app_ctxt->u4_i_interval = DEFAULT_I_INTERVAL;
1064     ps_app_ctxt->u4_idr_interval = DEFAULT_IDR_INTERVAL;
1065     ps_app_ctxt->u4_disable_deblock_level = DEFAULT_DISABLE_DEBLK_LEVEL;
1066     ps_app_ctxt->u4_hpel = DEFAULT_HPEL;
1067     ps_app_ctxt->u4_qpel = DEFAULT_QPEL;
1068     ps_app_ctxt->u4_enable_intra_4x4 = DEFAULT_I4;
1069     ps_app_ctxt->e_profile = DEFAULT_EPROFILE;
1070     ps_app_ctxt->u4_slice_mode = DEFAULT_SLICE_MODE;
1071     ps_app_ctxt->u4_slice_param = DEFAULT_SLICE_PARAM;
1072     ps_app_ctxt->pv_input_thread_handle = NULL;
1073     ps_app_ctxt->pv_output_thread_handle = NULL;
1074     ps_app_ctxt->pv_recon_thread_handle = NULL;
1075     ps_app_ctxt->adbl_psnr[0] = 0.0;
1076     ps_app_ctxt->adbl_psnr[1] = 0.0;
1077     ps_app_ctxt->adbl_psnr[2] = 0.0;
1078     ps_app_ctxt->u4_psnr_cnt = 0;
1079     ps_app_ctxt->pu1_psnr_buf = NULL;
1080     ps_app_ctxt->u4_psnr_buf_size = 0;
1081     ps_app_ctxt->u4_entropy_coding_mode = DEFAULT_ENTROPY_CODING_MODE;
1082     ps_app_ctxt->u1_num_temporal_layers = DEFAULT_NUM_TEMPORAL_LAYERS;
1083     ps_app_ctxt->u1_num_spatial_layers = DEFAULT_NUM_SPATIAL_LAYERS;
1084     ps_app_ctxt->d_spatial_res_ratio = DEFAULT_SPATIAL_RES_RATIO;
1085     ps_app_ctxt->u4_use_default_vui = 1;
1086 }
1087 
set_dimensions(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1088 static void set_dimensions(app_ctxt_t *ps_app_ctxt, UWORD32 u4_timestamp_low,
1089                            UWORD32 u4_timestamp_high)
1090 {
1091     isvce_ctl_set_dimensions_ip_t s_frame_dimensions_ip;
1092     isvce_ctl_set_dimensions_op_t s_frame_dimensions_op;
1093 
1094     IV_STATUS_T status;
1095 
1096     isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_VIDEO_CTL, ISVCE_CMD_CTL_SET_DIMENSIONS};
1097 
1098     s_frame_dimensions_ip.s_ive_ip.u4_ht = ps_app_ctxt->u4_ht;
1099     s_frame_dimensions_ip.s_ive_ip.u4_wd = ps_app_ctxt->u4_wd;
1100 
1101     s_frame_dimensions_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1102     s_frame_dimensions_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1103 
1104     s_frame_dimensions_ip.s_ive_ip.u4_size = sizeof(isvce_ctl_set_dimensions_ip_t);
1105     s_frame_dimensions_op.s_ive_op.u4_size = sizeof(isvce_ctl_set_dimensions_op_t);
1106 
1107     status = isvce_api_function(ps_app_ctxt->ps_enc, &s_frame_dimensions_ip, &s_frame_dimensions_op,
1108                                 &s_api_cmds);
1109     if(status != IV_SUCCESS)
1110     {
1111         CHAR ac_error[STRLENGTH];
1112         snprintf(ac_error, sizeof(ac_error) - 1, "Unable to set frame dimensions = 0x%x\n",
1113                  s_frame_dimensions_op.s_ive_op.u4_error_code);
1114         codec_exit(ac_error);
1115     }
1116 }
1117 
get_enc_dimensions(app_ctxt_t * ps_app_ctxt)1118 static void get_enc_dimensions(app_ctxt_t *ps_app_ctxt)
1119 {
1120     isvce_ctl_get_enc_dimensions_ip_t s_ip;
1121     isvce_ctl_get_enc_dimensions_op_t s_op;
1122 
1123     IV_STATUS_T status;
1124 
1125     isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_VIDEO_CTL, ISVCE_CMD_CTL_GET_ENC_FRAME_DIMENSIONS};
1126 
1127     s_ip.u4_inp_frame_wd = ps_app_ctxt->u4_wd;
1128     s_ip.u4_inp_frame_ht = ps_app_ctxt->u4_ht;
1129 
1130     status = isvce_api_function(ps_app_ctxt->ps_enc, &s_ip, &s_op, &s_api_cmds);
1131 
1132     if(status != IV_SUCCESS)
1133     {
1134         CHAR ac_error[STRLENGTH];
1135 
1136         snprintf(ac_error, sizeof(ac_error) - 1, "Unable to get enc frame dimensions = 0x%x\n",
1137                  s_op.u4_error_code);
1138 
1139         codec_exit(ac_error);
1140     }
1141 
1142     ps_app_ctxt->u4_enc_wd = s_op.u4_enc_frame_wd;
1143     ps_app_ctxt->u4_enc_ht = s_op.u4_enc_frame_ht;
1144 }
1145 
set_frame_rate(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1146 void set_frame_rate(app_ctxt_t *ps_app_ctxt, UWORD32 u4_timestamp_low, UWORD32 u4_timestamp_high)
1147 {
1148     isvce_ctl_set_frame_rate_ip_t s_frame_rate_ip;
1149     isvce_ctl_set_frame_rate_op_t s_frame_rate_op;
1150 
1151     IV_STATUS_T status;
1152 
1153     isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_VIDEO_CTL, ISVCE_CMD_CTL_SET_FRAMERATE};
1154 
1155     s_frame_rate_ip.s_ive_ip.u4_src_frame_rate = ps_app_ctxt->u4_src_frame_rate;
1156     s_frame_rate_ip.s_ive_ip.u4_tgt_frame_rate = ps_app_ctxt->u4_tgt_frame_rate;
1157 
1158     s_frame_rate_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1159     s_frame_rate_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1160 
1161     s_frame_rate_ip.s_ive_ip.u4_size = sizeof(isvce_ctl_set_frame_rate_ip_t);
1162     s_frame_rate_op.s_ive_op.u4_size = sizeof(isvce_ctl_set_frame_rate_op_t);
1163 
1164     status =
1165         isvce_api_function(ps_app_ctxt->ps_enc, &s_frame_rate_ip, &s_frame_rate_op, &s_api_cmds);
1166     if(status != IV_SUCCESS)
1167     {
1168         CHAR ac_error[STRLENGTH];
1169         snprintf(ac_error, sizeof(ac_error) - 1, "Unable to set frame rate = 0x%x\n",
1170                  s_frame_rate_op.s_ive_op.u4_error_code);
1171         codec_exit(ac_error);
1172     }
1173 }
1174 
set_ipe_params(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1175 void set_ipe_params(app_ctxt_t *ps_app_ctxt, UWORD32 u4_timestamp_low, UWORD32 u4_timestamp_high)
1176 {
1177     isvce_ctl_set_ipe_params_ip_t s_ipe_params_ip;
1178     isvce_ctl_set_ipe_params_op_t s_ipe_params_op;
1179 
1180     IV_STATUS_T status;
1181 
1182     isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_VIDEO_CTL, ISVCE_CMD_CTL_SET_IPE_PARAMS};
1183 
1184     s_ipe_params_ip.s_ive_ip.u4_enable_intra_4x4 = ps_app_ctxt->u4_enable_intra_4x4;
1185     s_ipe_params_ip.s_ive_ip.u4_enc_speed_preset = ps_app_ctxt->u4_enc_speed;
1186 
1187     s_ipe_params_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1188     s_ipe_params_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1189 
1190     s_ipe_params_ip.s_ive_ip.u4_size = sizeof(isvce_ctl_set_ipe_params_ip_t);
1191     s_ipe_params_op.s_ive_op.u4_size = sizeof(isvce_ctl_set_ipe_params_op_t);
1192 
1193     status =
1194         isvce_api_function(ps_app_ctxt->ps_enc, &s_ipe_params_ip, &s_ipe_params_op, &s_api_cmds);
1195     if(status != IV_SUCCESS)
1196     {
1197         CHAR ac_error[STRLENGTH];
1198         snprintf(ac_error, sizeof(ac_error) - 1, "Unable to set ipe params = 0x%x\n",
1199                  s_ipe_params_op.s_ive_op.u4_error_code);
1200         codec_exit(ac_error);
1201     }
1202 }
1203 
set_bit_rate(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1204 void set_bit_rate(app_ctxt_t *ps_app_ctxt, UWORD32 u4_timestamp_low, UWORD32 u4_timestamp_high)
1205 {
1206     isvce_ctl_set_bitrate_ip_t s_bitrate_ip;
1207     isvce_ctl_set_bitrate_op_t s_bitrate_op;
1208 
1209     IV_STATUS_T status;
1210     WORD8 i;
1211     isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_VIDEO_CTL, ISVCE_CMD_CTL_SET_BITRATE};
1212 
1213     s_bitrate_ip.pu4_target_bitrate =
1214         isvca_aligned_malloc(16, sizeof(UWORD32) * ps_app_ctxt->u1_num_spatial_layers);
1215 
1216     for(i = 0; i < ps_app_ctxt->u1_num_spatial_layers; i++)
1217     {
1218         s_bitrate_ip.pu4_target_bitrate[i] = ps_app_ctxt->pu4_bitrate[i];
1219     }
1220 
1221     s_bitrate_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1222     s_bitrate_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1223 
1224     s_bitrate_ip.s_ive_ip.u4_size = sizeof(isvce_ctl_set_bitrate_ip_t);
1225     s_bitrate_op.s_ive_op.u4_size = sizeof(isvce_ctl_set_bitrate_op_t);
1226 
1227     status = isvce_api_function(ps_app_ctxt->ps_enc, &s_bitrate_ip, &s_bitrate_op, &s_api_cmds);
1228 
1229     isvca_aligned_free(s_bitrate_ip.pu4_target_bitrate);
1230 
1231     if(status != IV_SUCCESS)
1232     {
1233         CHAR ac_error[STRLENGTH];
1234         snprintf(ac_error, sizeof(ac_error) - 1, "Unable to set bit rate = 0x%x\n",
1235                  s_bitrate_op.s_ive_op.u4_error_code);
1236         codec_exit(ac_error);
1237     }
1238 }
1239 
set_frame_type(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high,IV_PICTURE_CODING_TYPE_T e_frame_type)1240 void set_frame_type(app_ctxt_t *ps_app_ctxt, UWORD32 u4_timestamp_low, UWORD32 u4_timestamp_high,
1241                     IV_PICTURE_CODING_TYPE_T e_frame_type)
1242 {
1243     isvce_ctl_set_frame_type_ip_t s_frame_type_ip;
1244     isvce_ctl_set_frame_type_op_t s_frame_type_op;
1245 
1246     IV_STATUS_T status;
1247 
1248     isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_VIDEO_CTL, ISVCE_CMD_CTL_SET_FRAMETYPE};
1249 
1250     s_frame_type_ip.s_ive_ip.e_frame_type = e_frame_type;
1251 
1252     s_frame_type_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1253     s_frame_type_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1254 
1255     s_frame_type_ip.s_ive_ip.u4_size = sizeof(isvce_ctl_set_frame_type_ip_t);
1256     s_frame_type_op.s_ive_op.u4_size = sizeof(isvce_ctl_set_frame_type_op_t);
1257 
1258     status =
1259         isvce_api_function(ps_app_ctxt->ps_enc, &s_frame_type_ip, &s_frame_type_op, &s_api_cmds);
1260     if(status != IV_SUCCESS)
1261     {
1262         CHAR ac_error[STRLENGTH];
1263         snprintf(ac_error, sizeof(ac_error) - 1, "Unable to set frame type = 0x%x\n",
1264                  s_frame_type_op.s_ive_op.u4_error_code);
1265         codec_exit(ac_error);
1266     }
1267 }
1268 
set_qp(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1269 void set_qp(app_ctxt_t *ps_app_ctxt, UWORD32 u4_timestamp_low, UWORD32 u4_timestamp_high)
1270 {
1271     isvce_ctl_set_qp_ip_t s_qp_ip;
1272     isvce_ctl_set_qp_op_t s_qp_op;
1273 
1274     IV_STATUS_T status;
1275     WORD8 i;
1276 
1277     isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_VIDEO_CTL, ISVCE_CMD_CTL_SET_QP};
1278 
1279     s_qp_ip.pu4_i_qp =
1280         isvca_aligned_malloc(16, sizeof(UWORD32) * ps_app_ctxt->u1_num_spatial_layers);
1281     s_qp_ip.pu4_i_qp_max =
1282         isvca_aligned_malloc(16, sizeof(UWORD32) * ps_app_ctxt->u1_num_spatial_layers);
1283     s_qp_ip.pu4_i_qp_min =
1284         isvca_aligned_malloc(16, sizeof(UWORD32) * ps_app_ctxt->u1_num_spatial_layers);
1285 
1286     s_qp_ip.pu4_p_qp =
1287         isvca_aligned_malloc(16, sizeof(UWORD32) * ps_app_ctxt->u1_num_spatial_layers);
1288     s_qp_ip.pu4_p_qp_max =
1289         isvca_aligned_malloc(16, sizeof(UWORD32) * ps_app_ctxt->u1_num_spatial_layers);
1290     s_qp_ip.pu4_p_qp_min =
1291         isvca_aligned_malloc(16, sizeof(UWORD32) * ps_app_ctxt->u1_num_spatial_layers);
1292 
1293     s_qp_ip.pu4_b_qp =
1294         isvca_aligned_malloc(16, sizeof(UWORD32) * ps_app_ctxt->u1_num_spatial_layers);
1295     s_qp_ip.pu4_b_qp_max =
1296         isvca_aligned_malloc(16, sizeof(UWORD32) * ps_app_ctxt->u1_num_spatial_layers);
1297     s_qp_ip.pu4_b_qp_min =
1298         isvca_aligned_malloc(16, sizeof(UWORD32) * ps_app_ctxt->u1_num_spatial_layers);
1299 
1300     for(i = 0; i < ps_app_ctxt->u1_num_spatial_layers; i++)
1301     {
1302         s_qp_ip.pu4_i_qp[i] = ps_app_ctxt->pu4_i_qp[i];
1303         s_qp_ip.pu4_i_qp_max[i] = ps_app_ctxt->pu4_i_qp_max[i];
1304         s_qp_ip.pu4_i_qp_min[i] = ps_app_ctxt->pu4_i_qp_min[i];
1305 
1306         s_qp_ip.pu4_p_qp[i] = ps_app_ctxt->pu4_p_qp[i];
1307         s_qp_ip.pu4_p_qp_max[i] = ps_app_ctxt->pu4_p_qp_max[i];
1308         s_qp_ip.pu4_p_qp_min[i] = ps_app_ctxt->pu4_p_qp_min[i];
1309 
1310         s_qp_ip.pu4_b_qp[i] = ps_app_ctxt->pu4_b_qp[i];
1311         s_qp_ip.pu4_b_qp_max[i] = ps_app_ctxt->pu4_b_qp_max[i];
1312         s_qp_ip.pu4_b_qp_min[i] = ps_app_ctxt->pu4_b_qp_min[i];
1313     }
1314 
1315     s_qp_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1316     s_qp_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1317 
1318     s_qp_ip.s_ive_ip.u4_size = sizeof(isvce_ctl_set_qp_ip_t);
1319     s_qp_op.s_ive_op.u4_size = sizeof(isvce_ctl_set_qp_op_t);
1320 
1321     status = isvce_api_function(ps_app_ctxt->ps_enc, &s_qp_ip, &s_qp_op, &s_api_cmds);
1322 
1323     isvca_aligned_free(s_qp_ip.pu4_i_qp);
1324     isvca_aligned_free(s_qp_ip.pu4_i_qp_min);
1325     isvca_aligned_free(s_qp_ip.pu4_i_qp_max);
1326 
1327     isvca_aligned_free(s_qp_ip.pu4_p_qp);
1328     isvca_aligned_free(s_qp_ip.pu4_p_qp_min);
1329     isvca_aligned_free(s_qp_ip.pu4_p_qp_max);
1330 
1331     isvca_aligned_free(s_qp_ip.pu4_b_qp);
1332     isvca_aligned_free(s_qp_ip.pu4_b_qp_min);
1333     isvca_aligned_free(s_qp_ip.pu4_b_qp_max);
1334 
1335     if(status != IV_SUCCESS)
1336     {
1337         CHAR ac_error[STRLENGTH];
1338         snprintf(ac_error, sizeof(ac_error) - 1, "Unable to set qp 0x%x\n",
1339                  s_qp_op.s_ive_op.u4_error_code);
1340         codec_exit(ac_error);
1341     }
1342 }
1343 
set_enc_mode(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high,IVE_ENC_MODE_T e_enc_mode)1344 void set_enc_mode(app_ctxt_t *ps_app_ctxt, UWORD32 u4_timestamp_low, UWORD32 u4_timestamp_high,
1345                   IVE_ENC_MODE_T e_enc_mode)
1346 {
1347     IV_STATUS_T status;
1348 
1349     isvce_ctl_set_enc_mode_ip_t s_enc_mode_ip;
1350     isvce_ctl_set_enc_mode_op_t s_enc_mode_op;
1351 
1352     isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_VIDEO_CTL, ISVCE_CMD_CTL_SET_ENC_MODE};
1353 
1354     s_enc_mode_ip.s_ive_ip.e_enc_mode = e_enc_mode;
1355 
1356     s_enc_mode_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1357     s_enc_mode_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1358 
1359     s_enc_mode_ip.s_ive_ip.u4_size = sizeof(isvce_ctl_set_enc_mode_ip_t);
1360     s_enc_mode_op.s_ive_op.u4_size = sizeof(isvce_ctl_set_enc_mode_op_t);
1361 
1362     status = isvce_api_function(ps_app_ctxt->ps_enc, &s_enc_mode_ip, &s_enc_mode_op, &s_api_cmds);
1363     if(status != IV_SUCCESS)
1364     {
1365         CHAR ac_error[STRLENGTH];
1366         snprintf(ac_error, sizeof(ac_error) - 1, "Unable to set in header encode mode = 0x%x\n",
1367                  s_enc_mode_op.s_ive_op.u4_error_code);
1368         codec_exit(ac_error);
1369     }
1370 }
1371 
set_vbv_params(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1372 void set_vbv_params(app_ctxt_t *ps_app_ctxt, UWORD32 u4_timestamp_low, UWORD32 u4_timestamp_high)
1373 {
1374     isvce_ctl_set_vbv_params_ip_t s_vbv_ip;
1375     isvce_ctl_set_vbv_params_op_t s_vbv_op;
1376 
1377     IV_STATUS_T status;
1378     int i;
1379 
1380     isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_VIDEO_CTL, ISVCE_CMD_CTL_SET_VBV_PARAMS};
1381 
1382     s_vbv_ip.pu4_vbv_buffer_delay =
1383         isvca_aligned_malloc(16, sizeof(UWORD32) * ps_app_ctxt->u1_num_spatial_layers);
1384 
1385     for(i = 0; i < ps_app_ctxt->u1_num_spatial_layers; i++)
1386     {
1387         s_vbv_ip.pu4_vbv_buffer_delay[i] = ps_app_ctxt->pu4_vbv_buffer_delay[i];
1388     }
1389 
1390     s_vbv_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1391     s_vbv_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1392 
1393     s_vbv_ip.s_ive_ip.u4_size = sizeof(isvce_ctl_set_vbv_params_ip_t);
1394     s_vbv_op.s_ive_op.u4_size = sizeof(isvce_ctl_set_vbv_params_op_t);
1395 
1396     status = isvce_api_function(ps_app_ctxt->ps_enc, &s_vbv_ip, &s_vbv_op, &s_api_cmds);
1397 
1398     isvca_aligned_free(s_vbv_ip.pu4_vbv_buffer_delay);
1399 
1400     if(status != IV_SUCCESS)
1401     {
1402         CHAR ac_error[STRLENGTH];
1403         snprintf(ac_error, sizeof(ac_error) - 1, "Unable to set VBC params = 0x%x\n",
1404                  s_vbv_op.s_ive_op.u4_error_code);
1405         codec_exit(ac_error);
1406     }
1407 }
1408 
set_air_params(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1409 void set_air_params(app_ctxt_t *ps_app_ctxt, UWORD32 u4_timestamp_low, UWORD32 u4_timestamp_high)
1410 {
1411     isvce_ctl_set_air_params_ip_t s_air_ip;
1412     isvce_ctl_set_air_params_op_t s_air_op;
1413 
1414     IV_STATUS_T status;
1415 
1416     isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_VIDEO_CTL, ISVCE_CMD_CTL_SET_AIR_PARAMS};
1417 
1418     s_air_ip.s_ive_ip.e_air_mode = ps_app_ctxt->u4_air;
1419     s_air_ip.s_ive_ip.u4_air_refresh_period = ps_app_ctxt->u4_air_refresh_period;
1420 
1421     s_air_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1422     s_air_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1423 
1424     s_air_ip.s_ive_ip.u4_size = sizeof(isvce_ctl_set_air_params_ip_t);
1425     s_air_op.s_ive_op.u4_size = sizeof(isvce_ctl_set_air_params_op_t);
1426 
1427     status = isvce_api_function(ps_app_ctxt->ps_enc, &s_air_ip, &s_air_op, &s_api_cmds);
1428 
1429     if(status != IV_SUCCESS)
1430     {
1431         CHAR ac_error[STRLENGTH];
1432         snprintf(ac_error, sizeof(ac_error) - 1, "Unable to set air params = 0x%x\n",
1433                  s_air_op.s_ive_op.u4_error_code);
1434         codec_exit(ac_error);
1435     }
1436 }
1437 
set_me_params(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1438 void set_me_params(app_ctxt_t *ps_app_ctxt, UWORD32 u4_timestamp_low, UWORD32 u4_timestamp_high)
1439 {
1440     IV_STATUS_T status;
1441 
1442     isvce_ctl_set_me_params_ip_t s_me_params_ip;
1443     isvce_ctl_set_me_params_op_t s_me_params_op;
1444 
1445     isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_VIDEO_CTL, ISVCE_CMD_CTL_SET_ME_PARAMS};
1446 
1447     s_me_params_ip.s_ive_ip.u4_enable_fast_sad = ps_app_ctxt->u4_enable_fast_sad;
1448     s_me_params_ip.s_ive_ip.u4_enable_alt_ref = ps_app_ctxt->u4_enable_alt_ref;
1449 
1450     s_me_params_ip.s_ive_ip.u4_enable_hpel = ps_app_ctxt->u4_hpel;
1451     s_me_params_ip.s_ive_ip.u4_enable_qpel = ps_app_ctxt->u4_qpel;
1452     s_me_params_ip.s_ive_ip.u4_me_speed_preset = ps_app_ctxt->u4_me_speed;
1453     s_me_params_ip.s_ive_ip.u4_srch_rng_x = ps_app_ctxt->u4_srch_rng_x;
1454     s_me_params_ip.s_ive_ip.u4_srch_rng_y = ps_app_ctxt->u4_srch_rng_y;
1455 
1456     s_me_params_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1457     s_me_params_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1458 
1459     s_me_params_ip.s_ive_ip.u4_size = sizeof(isvce_ctl_set_me_params_ip_t);
1460     s_me_params_op.s_ive_op.u4_size = sizeof(isvce_ctl_set_me_params_op_t);
1461 
1462     status = isvce_api_function(ps_app_ctxt->ps_enc, &s_me_params_ip, &s_me_params_op, &s_api_cmds);
1463 
1464     if(status != IV_SUCCESS)
1465     {
1466         CHAR ac_error[STRLENGTH];
1467         snprintf(ac_error, sizeof(ac_error) - 1, "Unable to set me params = 0x%x\n",
1468                  s_me_params_op.s_ive_op.u4_error_code);
1469         codec_exit(ac_error);
1470     }
1471 }
1472 
set_gop_params(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1473 void set_gop_params(app_ctxt_t *ps_app_ctxt, UWORD32 u4_timestamp_low, UWORD32 u4_timestamp_high)
1474 {
1475     IV_STATUS_T status;
1476 
1477     isvce_ctl_set_gop_params_ip_t s_gop_params_ip;
1478     isvce_ctl_set_gop_params_op_t s_gop_params_op;
1479 
1480     isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_VIDEO_CTL, ISVCE_CMD_CTL_SET_GOP_PARAMS};
1481 
1482     s_gop_params_ip.s_ive_ip.u4_i_frm_interval = ps_app_ctxt->u4_i_interval;
1483     s_gop_params_ip.s_ive_ip.u4_idr_frm_interval = ps_app_ctxt->u4_idr_interval;
1484 
1485     s_gop_params_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1486     s_gop_params_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1487 
1488     s_gop_params_ip.s_ive_ip.u4_size = sizeof(isvce_ctl_set_gop_params_ip_t);
1489     s_gop_params_op.s_ive_op.u4_size = sizeof(isvce_ctl_set_gop_params_op_t);
1490 
1491     status =
1492         isvce_api_function(ps_app_ctxt->ps_enc, &s_gop_params_ip, &s_gop_params_op, &s_api_cmds);
1493     if(status != IV_SUCCESS)
1494     {
1495         CHAR ac_error[STRLENGTH];
1496         snprintf(ac_error, sizeof(ac_error) - 1, "Unable to set ME params = 0x%x\n",
1497                  s_gop_params_op.s_ive_op.u4_error_code);
1498         codec_exit(ac_error);
1499     }
1500 }
1501 
set_profile_params(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1502 void set_profile_params(app_ctxt_t *ps_app_ctxt, UWORD32 u4_timestamp_low,
1503                         UWORD32 u4_timestamp_high)
1504 {
1505     IV_STATUS_T status;
1506 
1507     isvce_ctl_set_profile_params_ip_t s_profile_params_ip;
1508     isvce_ctl_set_profile_params_op_t s_profile_params_op;
1509 
1510     isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_VIDEO_CTL, ISVCE_CMD_CTL_SET_PROFILE_PARAMS};
1511 
1512     s_profile_params_ip.s_ive_ip.e_profile = ps_app_ctxt->e_profile;
1513 
1514     s_profile_params_ip.s_ive_ip.u4_entropy_coding_mode = ps_app_ctxt->u4_entropy_coding_mode;
1515 
1516     s_profile_params_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1517     s_profile_params_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1518 
1519     s_profile_params_ip.s_ive_ip.u4_size = sizeof(isvce_ctl_set_profile_params_ip_t);
1520     s_profile_params_op.s_ive_op.u4_size = sizeof(isvce_ctl_set_profile_params_op_t);
1521 
1522     status = isvce_api_function(ps_app_ctxt->ps_enc, &s_profile_params_ip, &s_profile_params_op,
1523                                 &s_api_cmds);
1524     if(status != IV_SUCCESS)
1525     {
1526         CHAR ac_error[STRLENGTH];
1527         snprintf(ac_error, sizeof(ac_error) - 1, "Unable to set profile params = 0x%x\n",
1528                  s_profile_params_op.s_ive_op.u4_error_code);
1529         codec_exit(ac_error);
1530     }
1531 }
1532 
set_deblock_params(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1533 void set_deblock_params(app_ctxt_t *ps_app_ctxt, UWORD32 u4_timestamp_low,
1534                         UWORD32 u4_timestamp_high)
1535 {
1536     IV_STATUS_T status;
1537 
1538     isvce_ctl_set_deblock_params_ip_t s_deblock_params_ip;
1539     isvce_ctl_set_deblock_params_op_t s_deblock_params_op;
1540 
1541     isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_VIDEO_CTL, ISVCE_CMD_CTL_SET_DEBLOCK_PARAMS};
1542 
1543     s_deblock_params_ip.s_ive_ip.u4_disable_deblock_level = ps_app_ctxt->u4_disable_deblock_level;
1544 
1545     s_deblock_params_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1546     s_deblock_params_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1547 
1548     s_deblock_params_ip.s_ive_ip.u4_size = sizeof(isvce_ctl_set_deblock_params_ip_t);
1549     s_deblock_params_op.s_ive_op.u4_size = sizeof(isvce_ctl_set_deblock_params_op_t);
1550 
1551     status = isvce_api_function(ps_app_ctxt->ps_enc, &s_deblock_params_ip, &s_deblock_params_op,
1552                                 &s_api_cmds);
1553     if(status != IV_SUCCESS)
1554     {
1555         CHAR ac_error[STRLENGTH];
1556         snprintf(ac_error, sizeof(ac_error) - 1, "Unable to enable/disable deblock params = 0x%x\n",
1557                  s_deblock_params_op.s_ive_op.u4_error_code);
1558         codec_exit(ac_error);
1559     }
1560 }
1561 
set_vui_params(app_ctxt_t * ps_app_ctxt)1562 void set_vui_params(app_ctxt_t *ps_app_ctxt)
1563 {
1564     IV_STATUS_T status;
1565 
1566     isvce_vui_ip_t s_vui_params_ip;
1567     isvce_vui_op_t s_vui_params_op;
1568 
1569     isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_VIDEO_CTL, ISVCE_CMD_CTL_SET_VUI_PARAMS};
1570 
1571     s_vui_params_ip.u1_aspect_ratio_info_present_flag = 0;
1572     s_vui_params_ip.u1_aspect_ratio_idc = 0;
1573     s_vui_params_ip.u2_sar_width = 0;
1574     s_vui_params_ip.u2_sar_height = 0;
1575     s_vui_params_ip.u1_overscan_info_present_flag = 0;
1576     s_vui_params_ip.u1_overscan_appropriate_flag = 0;
1577     s_vui_params_ip.u1_video_signal_type_present_flag = 1;
1578     s_vui_params_ip.u1_video_format = 0;
1579     s_vui_params_ip.u1_video_full_range_flag = 0;
1580     s_vui_params_ip.u1_colour_description_present_flag = 0;
1581     s_vui_params_ip.u1_colour_primaries = 0;
1582     s_vui_params_ip.u1_transfer_characteristics = 0;
1583     s_vui_params_ip.u1_matrix_coefficients = 0;
1584     s_vui_params_ip.u1_chroma_loc_info_present_flag = 0;
1585     s_vui_params_ip.u1_chroma_sample_loc_type_top_field = 0;
1586     s_vui_params_ip.u1_chroma_sample_loc_type_bottom_field = 0;
1587     s_vui_params_ip.u1_vui_timing_info_present_flag = 0;
1588     s_vui_params_ip.u4_vui_num_units_in_tick = 0;
1589     s_vui_params_ip.u4_vui_time_scale = 0;
1590     s_vui_params_ip.u1_fixed_frame_rate_flag = 0;
1591     s_vui_params_ip.u1_nal_hrd_parameters_present_flag = 0;
1592     s_vui_params_ip.u1_vcl_hrd_parameters_present_flag = 0;
1593     s_vui_params_ip.u1_low_delay_hrd_flag = 0;
1594     s_vui_params_ip.u1_pic_struct_present_flag = 0;
1595     s_vui_params_ip.u1_bitstream_restriction_flag = 0;
1596     s_vui_params_ip.u1_motion_vectors_over_pic_boundaries_flag = 0;
1597     s_vui_params_ip.u1_max_bytes_per_pic_denom = 0;
1598     s_vui_params_ip.u1_max_bits_per_mb_denom = 0;
1599     s_vui_params_ip.u1_log2_max_mv_length_horizontal = 0;
1600     s_vui_params_ip.u1_log2_max_mv_length_vertical = 0;
1601     s_vui_params_ip.u1_num_reorder_frames = 0;
1602     s_vui_params_ip.u1_max_dec_frame_buffering = 0;
1603 
1604     s_vui_params_ip.u4_size = sizeof(isvce_vui_ip_t);
1605     s_vui_params_op.u4_size = sizeof(isvce_vui_op_t);
1606 
1607     status =
1608         isvce_api_function(ps_app_ctxt->ps_enc, &s_vui_params_ip, &s_vui_params_op, &s_api_cmds);
1609     if(status != IV_SUCCESS)
1610     {
1611         CHAR ac_error[STRLENGTH];
1612         snprintf(ac_error, sizeof(ac_error) - 1, "Unable to set vui params = 0x%x\n",
1613                  s_vui_params_op.u4_error_code);
1614         codec_exit(ac_error);
1615     }
1616 }
1617 
set_sei_mdcv_params(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1618 void set_sei_mdcv_params(app_ctxt_t *ps_app_ctxt, UWORD32 u4_timestamp_low,
1619                          UWORD32 u4_timestamp_high)
1620 {
1621     WORD32 i4_count;
1622     IV_STATUS_T status;
1623 
1624     isvce_ctl_set_sei_mdcv_params_ip_t s_sei_mdcv_params_ip;
1625     isvce_ctl_set_sei_mdcv_params_op_t s_sei_mdcv_params_op;
1626 
1627     isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_VIDEO_CTL, ISVCE_CMD_CTL_SET_SEI_MDCV_PARAMS};
1628 
1629     s_sei_mdcv_params_ip.u1_sei_mdcv_params_present_flag =
1630         (UWORD8) ps_app_ctxt->u4_sei_mdcv_params_present_flag;
1631 
1632     for(i4_count = 0; i4_count < NUM_SEI_MDCV_PRIMARIES; i4_count++)
1633     {
1634         s_sei_mdcv_params_ip.au2_display_primaries_x[i4_count] =
1635             (UWORD16) ps_app_ctxt->au4_display_primaries_x[i4_count];
1636         s_sei_mdcv_params_ip.au2_display_primaries_y[i4_count] =
1637             (UWORD16) ps_app_ctxt->au4_display_primaries_y[i4_count];
1638     }
1639 
1640     s_sei_mdcv_params_ip.u2_white_point_x = (UWORD16) ps_app_ctxt->u4_white_point_x;
1641     s_sei_mdcv_params_ip.u2_white_point_y = (UWORD16) ps_app_ctxt->u4_white_point_y;
1642     s_sei_mdcv_params_ip.u4_max_display_mastering_luminance =
1643         ps_app_ctxt->u4_max_display_mastering_luminance;
1644     s_sei_mdcv_params_ip.u4_min_display_mastering_luminance =
1645         ps_app_ctxt->u4_min_display_mastering_luminance;
1646 
1647     s_sei_mdcv_params_ip.u4_timestamp_high = u4_timestamp_high;
1648     s_sei_mdcv_params_ip.u4_timestamp_low = u4_timestamp_low;
1649 
1650     s_sei_mdcv_params_ip.u4_size = sizeof(isvce_ctl_set_sei_mdcv_params_ip_t);
1651     s_sei_mdcv_params_op.u4_size = sizeof(isvce_ctl_set_sei_mdcv_params_op_t);
1652 
1653     if((ps_app_ctxt->s_sei_mdcv_params.au2_display_primaries_x[0] !=
1654         s_sei_mdcv_params_ip.au2_display_primaries_x[0]) ||
1655        (ps_app_ctxt->s_sei_mdcv_params.au2_display_primaries_x[1] !=
1656         s_sei_mdcv_params_ip.au2_display_primaries_x[1]) ||
1657        (ps_app_ctxt->s_sei_mdcv_params.au2_display_primaries_x[2] !=
1658         s_sei_mdcv_params_ip.au2_display_primaries_x[2]) ||
1659        (ps_app_ctxt->s_sei_mdcv_params.au2_display_primaries_y[0] !=
1660         s_sei_mdcv_params_ip.au2_display_primaries_y[0]) ||
1661        (ps_app_ctxt->s_sei_mdcv_params.au2_display_primaries_y[1] !=
1662         s_sei_mdcv_params_ip.au2_display_primaries_y[1]) ||
1663        (ps_app_ctxt->s_sei_mdcv_params.au2_display_primaries_y[2] !=
1664         s_sei_mdcv_params_ip.au2_display_primaries_y[2]) ||
1665        (ps_app_ctxt->s_sei_mdcv_params.u2_white_point_x != s_sei_mdcv_params_ip.u2_white_point_x) ||
1666        (ps_app_ctxt->s_sei_mdcv_params.u2_white_point_y != s_sei_mdcv_params_ip.u2_white_point_x) ||
1667        (ps_app_ctxt->s_sei_mdcv_params.u4_max_display_mastering_luminance !=
1668         s_sei_mdcv_params_ip.u4_max_display_mastering_luminance) ||
1669        (ps_app_ctxt->s_sei_mdcv_params.u4_min_display_mastering_luminance !=
1670         s_sei_mdcv_params_ip.u4_min_display_mastering_luminance))
1671     {
1672         status = isvce_api_function(ps_app_ctxt->ps_enc, &s_sei_mdcv_params_ip,
1673                                     &s_sei_mdcv_params_op, &s_api_cmds);
1674         if(status != IV_SUCCESS)
1675         {
1676             printf("Unable to set sei mdcv params = 0x%x\n", s_sei_mdcv_params_op.u4_error_code);
1677         }
1678         ps_app_ctxt->s_sei_mdcv_params = s_sei_mdcv_params_ip;
1679     }
1680 }
1681 
set_sei_cll_params(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1682 void set_sei_cll_params(app_ctxt_t *ps_app_ctxt, UWORD32 u4_timestamp_low,
1683                         UWORD32 u4_timestamp_high)
1684 {
1685     IV_STATUS_T status;
1686 
1687     isvce_ctl_set_sei_cll_params_ip_t s_sei_cll_params_ip;
1688     isvce_ctl_set_sei_cll_params_op_t s_sei_cll_params_op;
1689 
1690     isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_VIDEO_CTL, ISVCE_CMD_CTL_SET_SEI_CLL_PARAMS};
1691 
1692     s_sei_cll_params_ip.u1_sei_cll_params_present_flag =
1693         (UWORD8) ps_app_ctxt->u4_sei_cll_params_present_flag;
1694 
1695     s_sei_cll_params_ip.u2_max_content_light_level =
1696         (UWORD16) ps_app_ctxt->u4_max_content_light_level;
1697     s_sei_cll_params_ip.u2_max_pic_average_light_level =
1698         (UWORD16) ps_app_ctxt->u4_max_pic_average_light_level;
1699 
1700     s_sei_cll_params_ip.u4_timestamp_high = u4_timestamp_high;
1701     s_sei_cll_params_ip.u4_timestamp_low = u4_timestamp_low;
1702 
1703     s_sei_cll_params_ip.u4_size = sizeof(isvce_ctl_set_sei_cll_params_ip_t);
1704     s_sei_cll_params_op.u4_size = sizeof(isvce_ctl_set_sei_cll_params_op_t);
1705 
1706     if((ps_app_ctxt->s_sei_cll_params.u2_max_content_light_level !=
1707         s_sei_cll_params_ip.u2_max_content_light_level) ||
1708        (ps_app_ctxt->s_sei_cll_params.u2_max_pic_average_light_level !=
1709         s_sei_cll_params_ip.u2_max_pic_average_light_level))
1710     {
1711         status = isvce_api_function(ps_app_ctxt->ps_enc, &s_sei_cll_params_ip, &s_sei_cll_params_op,
1712                                     &s_api_cmds);
1713         if(status != IV_SUCCESS)
1714         {
1715             printf("Unable to set sei cll params = 0x%x\n", s_sei_cll_params_op.u4_error_code);
1716         }
1717         ps_app_ctxt->s_sei_cll_params = s_sei_cll_params_ip;
1718     }
1719 }
1720 
set_sei_ave_params(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1721 void set_sei_ave_params(app_ctxt_t *ps_app_ctxt, UWORD32 u4_timestamp_low,
1722                         UWORD32 u4_timestamp_high)
1723 {
1724     IV_STATUS_T status;
1725 
1726     isvce_ctl_set_sei_ave_params_ip_t s_sei_ave_params_ip;
1727     isvce_ctl_set_sei_ave_params_op_t s_sei_ave_params_op;
1728 
1729     isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_VIDEO_CTL, ISVCE_CMD_CTL_SET_SEI_AVE_PARAMS};
1730 
1731     s_sei_ave_params_ip.u1_sei_ave_params_present_flag =
1732         (UWORD8) ps_app_ctxt->u4_sei_ave_params_present_flag;
1733 
1734     s_sei_ave_params_ip.u4_ambient_illuminance = ps_app_ctxt->u4_ambient_illuminance;
1735     s_sei_ave_params_ip.u2_ambient_light_x = (UWORD16) ps_app_ctxt->u4_ambient_light_x;
1736     s_sei_ave_params_ip.u2_ambient_light_y = (UWORD16) ps_app_ctxt->u4_ambient_light_y;
1737 
1738     s_sei_ave_params_ip.u4_timestamp_high = u4_timestamp_high;
1739     s_sei_ave_params_ip.u4_timestamp_low = u4_timestamp_low;
1740 
1741     s_sei_ave_params_ip.u4_size = sizeof(isvce_ctl_set_sei_ave_params_ip_t);
1742     s_sei_ave_params_op.u4_size = sizeof(isvce_ctl_set_sei_ave_params_op_t);
1743 
1744     if((ps_app_ctxt->s_sei_ave_params.u4_ambient_illuminance !=
1745         s_sei_ave_params_ip.u4_ambient_illuminance) ||
1746        (ps_app_ctxt->s_sei_ave_params.u2_ambient_light_x !=
1747         s_sei_ave_params_ip.u2_ambient_light_x) ||
1748        (ps_app_ctxt->s_sei_ave_params.u2_ambient_light_y != s_sei_ave_params_ip.u2_ambient_light_y))
1749     {
1750         status = isvce_api_function(ps_app_ctxt->ps_enc, &s_sei_ave_params_ip, &s_sei_ave_params_op,
1751                                     &s_api_cmds);
1752         if(status != IV_SUCCESS)
1753         {
1754             printf("Unable to set sei ave params = 0x%x\n", s_sei_ave_params_op.u4_error_code);
1755         }
1756         ps_app_ctxt->s_sei_ave_params = s_sei_ave_params_ip;
1757     }
1758 }
1759 
set_sei_ccv_params(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1760 void set_sei_ccv_params(app_ctxt_t *ps_app_ctxt, UWORD32 u4_timestamp_low,
1761                         UWORD32 u4_timestamp_high)
1762 {
1763     WORD32 i4_count;
1764     IV_STATUS_T status;
1765 
1766     isvce_ctl_set_sei_ccv_params_ip_t s_sei_ccv_params_ip;
1767     isvce_ctl_set_sei_ccv_params_op_t s_sei_ccv_params_op;
1768 
1769     isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_VIDEO_CTL, ISVCE_CMD_CTL_SET_SEI_CCV_PARAMS};
1770 
1771     s_sei_ccv_params_ip.u1_sei_ccv_params_present_flag =
1772         (UWORD8) ps_app_ctxt->u4_sei_ccv_params_present_flag;
1773 
1774     s_sei_ccv_params_ip.u1_ccv_cancel_flag = (UWORD8) ps_app_ctxt->u4_ccv_cancel_flag;
1775     s_sei_ccv_params_ip.u1_ccv_persistence_flag = (UWORD8) ps_app_ctxt->u4_ccv_persistence_flag;
1776     s_sei_ccv_params_ip.u1_ccv_primaries_present_flag =
1777         (UWORD8) ps_app_ctxt->u4_ccv_primaries_present_flag;
1778     s_sei_ccv_params_ip.u1_ccv_min_luminance_value_present_flag =
1779         (UWORD8) ps_app_ctxt->u4_ccv_min_luminance_value_present_flag;
1780     s_sei_ccv_params_ip.u1_ccv_max_luminance_value_present_flag =
1781         (UWORD8) ps_app_ctxt->u4_ccv_max_luminance_value_present_flag;
1782     s_sei_ccv_params_ip.u1_ccv_avg_luminance_value_present_flag =
1783         (UWORD8) ps_app_ctxt->u4_ccv_avg_luminance_value_present_flag;
1784     s_sei_ccv_params_ip.u1_ccv_reserved_zero_2bits =
1785         (UWORD8) ps_app_ctxt->u4_ccv_reserved_zero_2bits;
1786 
1787     for(i4_count = 0; i4_count < NUM_SEI_CCV_PRIMARIES; i4_count++)
1788     {
1789         s_sei_ccv_params_ip.ai4_ccv_primaries_x[i4_count] =
1790             ps_app_ctxt->ai4_ccv_primaries_x[i4_count];
1791         s_sei_ccv_params_ip.ai4_ccv_primaries_y[i4_count] =
1792             ps_app_ctxt->ai4_ccv_primaries_y[i4_count];
1793     }
1794 
1795     s_sei_ccv_params_ip.u4_ccv_min_luminance_value = ps_app_ctxt->u4_ccv_min_luminance_value;
1796     s_sei_ccv_params_ip.u4_ccv_max_luminance_value = ps_app_ctxt->u4_ccv_max_luminance_value;
1797     s_sei_ccv_params_ip.u4_ccv_avg_luminance_value = ps_app_ctxt->u4_ccv_avg_luminance_value;
1798 
1799     s_sei_ccv_params_ip.u4_timestamp_high = u4_timestamp_high;
1800     s_sei_ccv_params_ip.u4_timestamp_low = u4_timestamp_low;
1801 
1802     s_sei_ccv_params_ip.u4_size = sizeof(isvce_ctl_set_sei_ccv_params_ip_t);
1803     s_sei_ccv_params_op.u4_size = sizeof(isvce_ctl_set_sei_ccv_params_op_t);
1804 
1805     status = isvce_api_function(ps_app_ctxt->ps_enc, &s_sei_ccv_params_ip, &s_sei_ccv_params_op,
1806                                 &s_api_cmds);
1807     if(status != IV_SUCCESS)
1808     {
1809         printf("Unable to set sei ccv params = 0x%x\n", s_sei_ccv_params_op.u4_error_code);
1810     }
1811 }
1812 
allocate_nalu_info_bufs(app_ctxt_t * ps_app_ctxt)1813 static void allocate_nalu_info_bufs(app_ctxt_t *ps_app_ctxt)
1814 {
1815     UWORD32 i;
1816 
1817     UWORD32 u4_num_bufs =
1818         sizeof(ps_app_ctxt->as_nalu_info_bufs) / sizeof(ps_app_ctxt->as_nalu_info_bufs[0]);
1819 
1820     for(i = 0; i < u4_num_bufs; i++)
1821     {
1822         ps_app_ctxt->as_nalu_info_bufs[i].u4_buf_size =
1823             ps_app_ctxt->s_get_buf_info_op.u4_min_nalu_info_buf_size;
1824         ps_app_ctxt->as_nalu_info_bufs[i].b_is_free = true;
1825 
1826         ps_app_ctxt->as_nalu_info_bufs[i].pu1_buf =
1827             (UWORD8 *) isvca_aligned_malloc(16, ps_app_ctxt->as_nalu_info_bufs[i].u4_buf_size);
1828     }
1829 }
1830 
free_nalu_info_bufs(app_ctxt_t * ps_app_ctxt)1831 static void free_nalu_info_bufs(app_ctxt_t *ps_app_ctxt)
1832 {
1833     UWORD32 i;
1834 
1835     UWORD32 u4_num_bufs =
1836         sizeof(ps_app_ctxt->as_nalu_info_bufs) / sizeof(ps_app_ctxt->as_nalu_info_bufs[0]);
1837 
1838     for(i = 0; i < u4_num_bufs; i++)
1839     {
1840         isvca_aligned_free(ps_app_ctxt->as_nalu_info_bufs[i].pu1_buf);
1841         ps_app_ctxt->as_nalu_info_bufs[i].b_is_free = false;
1842     }
1843 }
1844 
1845 #define PEAK_WINDOW_SIZE 8
1846 
synchronous_encode(iv_obj_t * ps_enc,app_ctxt_t * ps_app_ctxt)1847 void synchronous_encode(iv_obj_t *ps_enc, app_ctxt_t *ps_app_ctxt)
1848 {
1849     isvce_video_encode_ip_t s_video_encode_ip;
1850     isvce_video_encode_op_t s_video_encode_op;
1851 
1852     ive_video_encode_ip_t *ps_video_encode_ip = &s_video_encode_ip.s_ive_ip;
1853     ive_video_encode_op_t *ps_video_encode_op = &s_video_encode_op.s_ive_op;
1854 
1855     iv_raw_buf_t *ps_inp_raw_buf = &ps_video_encode_ip->s_inp_buf;
1856 
1857     IV_STATUS_T status = IV_SUCCESS;
1858     isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_VIDEO_ENCODE, ISVCE_CMD_CT_NA};
1859 
1860     WORD32 i, is_last = 0, num_bytes = 0;
1861     UWORD32 u4_total_time = 0;
1862     UWORD8 *pu1_buf = NULL;
1863     UWORD32 u4_timestamp_low, u4_timestamp_high;
1864     void *pv_mb_info = NULL, *pv_pic_info = NULL;
1865     WORD32 *pi4_nalu_info_buf_ids = isvca_aligned_malloc(
1866         16, ps_app_ctxt->u1_num_spatial_layers * sizeof(pi4_nalu_info_buf_ids[0]));
1867 
1868 #ifdef PROFILE_ENABLE
1869     TIMER curtime;
1870     WORD32 peak_window[PEAK_WINDOW_SIZE] = {0};
1871     WORD32 peak_window_idx = 0;
1872 #endif
1873     WORD32 peak_avg_max = 0, timetaken = 0;
1874     iv_raw_buf_t s_inp_buf, s_recon_buf;
1875     CHAR ac_error[2 * STRLENGTH];
1876 
1877     u4_timestamp_low = 0;
1878     u4_timestamp_high = 0;
1879 
1880     /*************************************************************************/
1881     /*                         Allocate I/O Buffers                          */
1882     /*************************************************************************/
1883     allocate_input(ps_app_ctxt);
1884     allocate_output(ps_app_ctxt);
1885     allocate_recon(ps_app_ctxt);
1886     allocate_nalu_info_bufs(ps_app_ctxt);
1887 
1888     s_video_encode_ip.ps_nalu_info_buf = isvca_aligned_malloc(
1889         16, sizeof(isvce_nalu_info_buf_t) * ps_app_ctxt->u1_num_spatial_layers);
1890     s_video_encode_op.ps_nalu_info_buf = isvca_aligned_malloc(
1891         16, sizeof(isvce_nalu_info_buf_t) * ps_app_ctxt->u1_num_spatial_layers);
1892 
1893     /* init psnr */
1894     init_psnr(ps_app_ctxt);
1895 
1896     /* open file pointers */
1897     ps_app_ctxt->fp_ip = fopen(ps_app_ctxt->ac_ip_fname, "rb");
1898     if(NULL == ps_app_ctxt->fp_ip)
1899     {
1900         snprintf(ac_error, sizeof(ac_error) - 1, "Unable to open input file for reading: %s",
1901                  ps_app_ctxt->ac_ip_fname);
1902         invalid_argument_exit(ac_error);
1903     }
1904 
1905     ps_app_ctxt->fp_op = fopen(ps_app_ctxt->ac_op_fname, "wb");
1906     if(NULL == ps_app_ctxt->fp_op)
1907     {
1908         snprintf(ac_error, sizeof(ac_error) - 1, "Unable to open output file for writing: %s",
1909                  ps_app_ctxt->ac_op_fname);
1910         invalid_argument_exit(ac_error);
1911     }
1912 
1913     if(1 == ps_app_ctxt->u4_recon_enable)
1914     {
1915         ps_app_ctxt->fp_recon = fopen(ps_app_ctxt->ac_recon_fname, "wb");
1916         if(NULL == ps_app_ctxt->fp_recon)
1917         {
1918             snprintf(ac_error, sizeof(ac_error) - 1, "Unable to open recon file for writing: %s",
1919                      ps_app_ctxt->ac_recon_fname);
1920             invalid_argument_exit(ac_error);
1921         }
1922     }
1923 
1924     if(1 == ps_app_ctxt->u4_nalu_info_export_enable)
1925     {
1926         ps_app_ctxt->fp_nalu_info = fopen(ps_app_ctxt->ac_nalu_info_csv_fname, "w");
1927 
1928         if(NULL == ps_app_ctxt->fp_nalu_info)
1929         {
1930             snprintf(ac_error, sizeof(ac_error) - 1,
1931                      "Unable to open NALU Info CSV file for writing: %s",
1932                      ps_app_ctxt->ac_nalu_info_csv_fname);
1933             invalid_argument_exit(ac_error);
1934         }
1935 
1936         fprintf(ps_app_ctxt->fp_nalu_info,
1937                 "type,length,SId,TID,isIDR,isFirstSliceInLayer,isLastSliceInLayer\n");
1938     }
1939 
1940     if(1 == ps_app_ctxt->u4_chksum_enable)
1941     {
1942         ps_app_ctxt->fp_chksum = fopen(ps_app_ctxt->ac_chksum_fname, "wb");
1943         if(NULL == ps_app_ctxt->fp_chksum)
1944         {
1945             snprintf(ac_error, sizeof(ac_error) - 1, "Unable to open checksum file for writing: %s",
1946                      ps_app_ctxt->ac_chksum_fname);
1947             invalid_argument_exit(ac_error);
1948         }
1949     }
1950 
1951     /* If PSNR is enabled, open input file again and hold a different file pointer
1952      * This makes it easy to compute PSNR without adding dependency between input
1953      * and recon threads
1954      */
1955     if(1 == ps_app_ctxt->u4_psnr_enable)
1956     {
1957         ps_app_ctxt->fp_psnr_ip = fopen(ps_app_ctxt->ac_ip_fname, "rb");
1958         if(NULL == ps_app_ctxt->fp_psnr_ip)
1959         {
1960             snprintf(ac_error, sizeof(ac_error) - 1, "Unable to open input file for reading: %s",
1961                      ps_app_ctxt->ac_ip_fname);
1962             invalid_argument_exit(ac_error);
1963         }
1964     }
1965 
1966     if(0 != ps_app_ctxt->u4_mb_info_type)
1967     {
1968         ps_app_ctxt->fp_mb_info = fopen(ps_app_ctxt->ac_mb_info_fname, "rb");
1969         if(NULL == ps_app_ctxt->fp_mb_info)
1970         {
1971             snprintf(ac_error, sizeof(ac_error) - 1, "Unable to open MB info file for reading: %s",
1972                      ps_app_ctxt->ac_mb_info_fname);
1973             invalid_argument_exit(ac_error);
1974         }
1975     }
1976     if(ps_app_ctxt->u4_pic_info_type)
1977     {
1978         ps_app_ctxt->fp_pic_info = fopen(ps_app_ctxt->ac_pic_info_fname, "rb");
1979         if(NULL == ps_app_ctxt->fp_pic_info)
1980         {
1981             snprintf(ac_error, sizeof(ac_error) - 1, "Unable to open Pic info file for reading: %s",
1982                      ps_app_ctxt->ac_pic_info_fname);
1983             invalid_argument_exit(ac_error);
1984         }
1985     }
1986 
1987     GETTIME(&ps_app_ctxt->enc_start_time);
1988     ps_app_ctxt->enc_last_time = ps_app_ctxt->enc_start_time;
1989 
1990     while(1)
1991     {
1992         WORD32 i4_count;
1993 
1994         /* Default sei params values*/
1995         ps_app_ctxt->u4_sei_mdcv_params_present_flag = 0;
1996         if(1 == ps_app_ctxt->u4_sei_mdcv_params_present_flag)
1997         {
1998             for(i4_count = 0; i4_count < NUM_SEI_MDCV_PRIMARIES; i4_count++)
1999             {
2000                 ps_app_ctxt->au4_display_primaries_x[i4_count] = 0;
2001                 ps_app_ctxt->au4_display_primaries_y[i4_count] = 0;
2002             }
2003             ps_app_ctxt->u4_white_point_x = 0;
2004             ps_app_ctxt->u4_white_point_y = 0;
2005             ps_app_ctxt->u4_max_display_mastering_luminance =
2006                 DEFAULT_MAX_DISPLAY_MASTERING_LUMINANCE;
2007             ps_app_ctxt->u4_min_display_mastering_luminance =
2008                 DEFAULT_MIN_DISPLAY_MASTERING_LUMINANCE;
2009             set_sei_mdcv_params(ps_app_ctxt, u4_timestamp_low, u4_timestamp_high);
2010         }
2011 
2012         ps_app_ctxt->u4_sei_cll_params_present_flag = 0;
2013         if(1 == ps_app_ctxt->u4_sei_cll_params_present_flag)
2014         {
2015             ps_app_ctxt->u4_max_content_light_level = 0;
2016             ps_app_ctxt->u4_max_pic_average_light_level = 0;
2017             set_sei_cll_params(ps_app_ctxt, u4_timestamp_low, u4_timestamp_high);
2018         }
2019 
2020         ps_app_ctxt->u4_sei_ave_params_present_flag = 0;
2021         if(1 == ps_app_ctxt->u4_sei_ave_params_present_flag)
2022         {
2023             ps_app_ctxt->u4_ambient_illuminance = 1;
2024             ps_app_ctxt->u4_ambient_light_x = 0;
2025             ps_app_ctxt->u4_ambient_light_y = 0;
2026             set_sei_ave_params(ps_app_ctxt, u4_timestamp_low, u4_timestamp_high);
2027         }
2028 
2029         ps_app_ctxt->u4_sei_ccv_params_present_flag = 0;
2030         if(1 == ps_app_ctxt->u4_sei_ccv_params_present_flag)
2031         {
2032             ps_app_ctxt->u4_ccv_cancel_flag = 0;
2033             ps_app_ctxt->u4_ccv_persistence_flag = 1;
2034             ps_app_ctxt->u4_ccv_primaries_present_flag = 1;
2035             ps_app_ctxt->u4_ccv_min_luminance_value_present_flag = 1;
2036             ps_app_ctxt->u4_ccv_max_luminance_value_present_flag = 1;
2037             ps_app_ctxt->u4_ccv_avg_luminance_value_present_flag = 1;
2038             ps_app_ctxt->u4_ccv_reserved_zero_2bits = 0;
2039             for(i4_count = 0; i4_count < NUM_SEI_CCV_PRIMARIES; i4_count++)
2040             {
2041                 ps_app_ctxt->ai4_ccv_primaries_x[i4_count] = 1;
2042                 ps_app_ctxt->ai4_ccv_primaries_y[i4_count] = 1;
2043             }
2044             ps_app_ctxt->u4_ccv_min_luminance_value = 1;
2045             ps_app_ctxt->u4_ccv_max_luminance_value = 1;
2046             ps_app_ctxt->u4_ccv_avg_luminance_value = 1;
2047             set_sei_ccv_params(ps_app_ctxt, u4_timestamp_low, u4_timestamp_high);
2048         }
2049 
2050         /******************************************************************************/
2051         /****************** Input Initialization
2052          * **************************************/
2053         /******************************************************************************/
2054 
2055         for(i = 0; i < DEFAULT_MAX_INPUT_BUFS; i++)
2056         {
2057             if(ps_app_ctxt->as_input_buf[i].u4_is_free)
2058             {
2059                 pu1_buf = ps_app_ctxt->as_input_buf[i].pu1_buf;
2060                 pv_mb_info = ps_app_ctxt->as_input_buf[i].pv_mb_info;
2061                 pv_pic_info = ps_app_ctxt->as_input_buf[i].pv_pic_info;
2062                 ps_app_ctxt->as_input_buf[i].u4_is_free = 0;
2063                 break;
2064             }
2065         }
2066 
2067         if(i == DEFAULT_MAX_INPUT_BUFS)
2068         {
2069             printf("\n Unable to find a free input buffer!!");
2070             exit(0);
2071         }
2072 
2073         ps_video_encode_ip->u4_size = sizeof(isvce_video_encode_ip_t);
2074         ps_video_encode_op->u4_size = sizeof(isvce_video_encode_op_t);
2075 
2076         ps_video_encode_ip->pv_bufs = pu1_buf;
2077         ps_video_encode_ip->pv_mb_info = pv_mb_info;
2078         ps_video_encode_ip->pv_pic_info = pv_pic_info;
2079         ps_video_encode_ip->u4_pic_info_type = ps_app_ctxt->u4_pic_info_type;
2080         /*
2081          * Since the buffers are used for reading,
2082          * And after each row we have a stride we nned to calculate
2083          * the luma size according to the stride
2084          */
2085         ps_inp_raw_buf->e_color_fmt = ps_app_ctxt->e_inp_color_fmt;
2086 
2087         /* Initialize for 420SP */
2088         if(IV_YUV_420SP_UV == ps_app_ctxt->e_inp_color_fmt ||
2089            IV_YUV_420SP_VU == ps_app_ctxt->e_inp_color_fmt)
2090         {
2091             /*init luma buffer*/
2092             ps_inp_raw_buf->apv_bufs[0] = pu1_buf;
2093 
2094             /*Init chroma buffer*/
2095             pu1_buf += ps_app_ctxt->u4_strd * ps_app_ctxt->u4_ht;
2096             ps_inp_raw_buf->apv_bufs[1] = pu1_buf;
2097 
2098             ps_inp_raw_buf->au4_wd[0] = ps_app_ctxt->u4_wd;
2099             ps_inp_raw_buf->au4_wd[1] = ps_app_ctxt->u4_wd;
2100 
2101             ps_inp_raw_buf->au4_ht[0] = ps_app_ctxt->u4_ht;
2102             ps_inp_raw_buf->au4_ht[1] = ps_app_ctxt->u4_ht / 2;
2103 
2104             ps_inp_raw_buf->au4_strd[0] = ps_app_ctxt->u4_strd;
2105             ps_inp_raw_buf->au4_strd[1] = ps_app_ctxt->u4_strd;
2106         }
2107         else if(IV_YUV_420P == ps_app_ctxt->e_inp_color_fmt)
2108         {
2109             /* init buffers */
2110             ps_inp_raw_buf->apv_bufs[0] = pu1_buf;
2111             pu1_buf += (ps_app_ctxt->u4_wd) * ps_app_ctxt->u4_ht;
2112             ps_inp_raw_buf->apv_bufs[1] = pu1_buf;
2113             pu1_buf += (ps_app_ctxt->u4_wd >> 1) * (ps_app_ctxt->u4_ht >> 1);
2114             ps_inp_raw_buf->apv_bufs[2] = pu1_buf;
2115 
2116             ps_inp_raw_buf->au4_wd[0] = ps_app_ctxt->u4_wd;
2117             ps_inp_raw_buf->au4_wd[1] = ps_app_ctxt->u4_wd / 2;
2118             ps_inp_raw_buf->au4_wd[2] = ps_app_ctxt->u4_wd / 2;
2119 
2120             ps_inp_raw_buf->au4_ht[0] = ps_app_ctxt->u4_ht;
2121             ps_inp_raw_buf->au4_ht[1] = ps_app_ctxt->u4_ht / 2;
2122             ps_inp_raw_buf->au4_ht[2] = ps_app_ctxt->u4_ht / 2;
2123 
2124             ps_inp_raw_buf->au4_strd[0] = ps_app_ctxt->u4_strd;
2125             ps_inp_raw_buf->au4_strd[1] = ps_app_ctxt->u4_strd / 2;
2126             ps_inp_raw_buf->au4_strd[2] = ps_app_ctxt->u4_strd / 2;
2127         }
2128         else if(IV_YUV_422ILE == ps_app_ctxt->e_inp_color_fmt)
2129         {
2130             /*init luma buffer*/
2131             ps_inp_raw_buf->apv_bufs[0] = pu1_buf;
2132 
2133             ps_inp_raw_buf->au4_wd[0] = ps_app_ctxt->u4_wd * 2;
2134 
2135             ps_inp_raw_buf->au4_ht[0] = ps_app_ctxt->u4_ht;
2136 
2137             ps_inp_raw_buf->au4_strd[0] = ps_app_ctxt->u4_strd * 2;
2138         }
2139 
2140         /*
2141          * Here we read input and other associated buffers. Regardless of success
2142          * we will proceed from here as we will need extra calls to flush out
2143          * input queue in encoder. Note that this is not necessary. You can just
2144          * send encode calls till with valid output and recon buffers till the
2145          * queue is flushed.
2146          */
2147         while(1)
2148         {
2149             IV_STATUS_T mb_info_status = IV_SUCCESS, pic_info_status = IV_SUCCESS;
2150 
2151             status = read_input(ps_app_ctxt->fp_ip, ps_inp_raw_buf);
2152 
2153             if(ps_app_ctxt->u4_mb_info_type != 0)
2154             {
2155                 mb_info_status = read_mb_info(ps_app_ctxt, pv_mb_info);
2156             }
2157             if(ps_app_ctxt->u4_pic_info_type != 0)
2158             {
2159                 pic_info_status = read_pic_info(ps_app_ctxt, pv_pic_info);
2160             }
2161             if((IV_SUCCESS != status) || (IV_SUCCESS != mb_info_status) ||
2162                (IV_SUCCESS != pic_info_status))
2163             {
2164                 if(0 == ps_app_ctxt->u4_loopback)
2165                 {
2166                     is_last = 1;
2167                     break;
2168                 }
2169                 else
2170                     fseek(ps_app_ctxt->fp_ip, 0, SEEK_SET);
2171             }
2172             break;
2173         }
2174 
2175         /******************************************************************************/
2176         /****************** Output Initialization
2177          * *************************************/
2178         /******************************************************************************/
2179         for(i = 0; i < DEFAULT_MAX_OUTPUT_BUFS; i++)
2180         {
2181             if(ps_app_ctxt->as_output_buf[i].u4_is_free)
2182             {
2183                 ps_app_ctxt->as_output_buf[i].u4_is_free = 0;
2184 
2185                 ps_video_encode_ip->s_out_buf.pv_buf = ps_app_ctxt->as_output_buf[i].pu1_buf;
2186                 ps_video_encode_ip->s_out_buf.u4_bytes = 0;
2187                 ps_video_encode_ip->s_out_buf.u4_bufsize =
2188                     ps_app_ctxt->as_output_buf[i].u4_buf_size;
2189             }
2190         }
2191 
2192         /******************************************************************************/
2193         /****************** Recon Initialization
2194          * **************************************/
2195         /******************************************************************************/
2196         init_raw_buf_descr(ps_app_ctxt, &s_recon_buf, ps_app_ctxt->as_recon_buf[0].pu1_buf,
2197                            ps_app_ctxt->e_recon_color_fmt);
2198 
2199         /******************************************************************************/
2200         /****************** Output Initialization
2201          * *************************************/
2202         /******************************************************************************/
2203         {
2204             UWORD8 u1_num_layer_bufs_assigned = 0;
2205 
2206             for(i = 0; i < DEFAULT_MAX_NALU_INFO_BUFS; i++)
2207             {
2208                 if(ps_app_ctxt->as_nalu_info_bufs[i].b_is_free)
2209                 {
2210                     ps_app_ctxt->as_nalu_info_bufs[i].b_is_free = false;
2211                     pi4_nalu_info_buf_ids[u1_num_layer_bufs_assigned] = i;
2212 
2213                     s_video_encode_ip.ps_nalu_info_buf[u1_num_layer_bufs_assigned].pu1_buf =
2214                         ps_app_ctxt->as_nalu_info_bufs[i].pu1_buf;
2215                     s_video_encode_ip.ps_nalu_info_buf[u1_num_layer_bufs_assigned].u4_num_bytes = 0;
2216                     s_video_encode_ip.ps_nalu_info_buf[u1_num_layer_bufs_assigned].u4_buf_size =
2217                         ps_app_ctxt->as_nalu_info_bufs[i].u4_buf_size;
2218 
2219                     u1_num_layer_bufs_assigned++;
2220 
2221                     if(u1_num_layer_bufs_assigned >= ps_app_ctxt->u1_num_spatial_layers)
2222                     {
2223                         break;
2224                     }
2225                 }
2226             }
2227         }
2228 
2229         if(ps_app_ctxt->u4_psnr_enable)
2230             init_raw_buf_descr(ps_app_ctxt, &s_inp_buf, ps_app_ctxt->pu1_psnr_buf,
2231                                ps_app_ctxt->e_inp_color_fmt);
2232 
2233         ps_video_encode_ip->s_recon_buf = s_recon_buf;
2234 
2235         /******************************************************************************/
2236         /************************* Un Initialized
2237          * *************************************/
2238         /******************************************************************************/
2239         if(0 == ps_app_ctxt->u4_loopback)
2240         {
2241             /* If input file is read completely and loopback is not enabled,
2242              *  then exit the loop */
2243             if(feof(ps_app_ctxt->fp_ip))
2244             {
2245                 is_last = 1;
2246             }
2247         }
2248 
2249         /* If last frame, send input null to get back encoded frames */
2250         if(is_last == 1 || ((ps_app_ctxt->u4_max_num_frms) <= u4_timestamp_low))
2251         {
2252             is_last = 1;
2253             ps_inp_raw_buf->apv_bufs[0] = NULL;
2254             ps_inp_raw_buf->apv_bufs[1] = NULL;
2255             ps_inp_raw_buf->apv_bufs[2] = NULL;
2256         }
2257 
2258         ps_video_encode_ip->u4_is_last = is_last;
2259         ps_video_encode_ip->u4_mb_info_type = ps_app_ctxt->u4_mb_info_type;
2260         ps_video_encode_ip->u4_pic_info_type = ps_app_ctxt->u4_pic_info_type;
2261 
2262         ps_video_encode_op->s_out_buf.pv_buf = NULL;
2263 
2264         for(i = 0; i < ps_app_ctxt->u1_num_spatial_layers; i++)
2265         {
2266             s_video_encode_op.ps_nalu_info_buf[i].pu1_buf = NULL;
2267         }
2268 
2269         ps_video_encode_ip->u4_timestamp_high = u4_timestamp_high;
2270         ps_video_encode_ip->u4_timestamp_low = u4_timestamp_low;
2271 
2272         GETTIME(&ps_app_ctxt->enc_last_time);
2273 
2274         status = isvce_api_function(ps_enc, &s_video_encode_ip, &s_video_encode_op, &s_api_cmds);
2275 
2276         if(IV_SUCCESS != status)
2277         {
2278             printf("Encode Frame failed = 0x%x\n", s_video_encode_op.s_ive_op.u4_error_code);
2279             break;
2280         }
2281 
2282 #ifdef PROFILE_ENABLE
2283         GETTIME(&curtime);
2284         ELAPSEDTIME(ps_app_ctxt->enc_last_time, curtime, timetaken, frequency);
2285         ps_app_ctxt->enc_last_time = curtime;
2286 
2287         {
2288             WORD32 peak_avg, id;
2289             u4_total_time += timetaken;
2290             peak_window[peak_window_idx++] = timetaken;
2291             if(peak_window_idx == PEAK_WINDOW_SIZE) peak_window_idx = 0;
2292             peak_avg = 0;
2293             for(id = 0; id < PEAK_WINDOW_SIZE; id++)
2294             {
2295                 peak_avg += peak_window[id];
2296             }
2297             peak_avg /= PEAK_WINDOW_SIZE;
2298             if(peak_avg > peak_avg_max) peak_avg_max = peak_avg;
2299         }
2300 #endif
2301 
2302         /******************************************************************************/
2303         /****************** Writing Output
2304          * ********************************************/
2305         /******************************************************************************/
2306         num_bytes = 0;
2307 
2308         if(ps_video_encode_op->output_present)
2309         {
2310             num_bytes = ps_video_encode_op->s_out_buf.u4_bytes;
2311             pu1_buf = (UWORD8 *) ps_video_encode_op->s_out_buf.pv_buf;
2312 
2313             status = write_output(ps_app_ctxt->fp_op, pu1_buf, num_bytes);
2314 
2315             if(IV_SUCCESS != status)
2316             {
2317                 printf("Error: Unable to write to output file\n");
2318                 break;
2319             }
2320         }
2321 
2322         for(i = 0; i < DEFAULT_MAX_OUTPUT_BUFS; i++)
2323         {
2324             if(ps_app_ctxt->as_output_buf[i].pu1_buf == ps_video_encode_op->s_out_buf.pv_buf)
2325             {
2326                 ps_app_ctxt->as_output_buf[i].u4_is_free = 1;
2327 
2328                 break;
2329             }
2330         }
2331 
2332         if(ps_app_ctxt->u4_nalu_info_export_enable && s_video_encode_op.b_is_nalu_info_present)
2333         {
2334             for(i = 0; i < ps_app_ctxt->u1_num_spatial_layers; i++)
2335             {
2336                 fprintf(ps_app_ctxt->fp_nalu_info, "%s",
2337                         s_video_encode_op.ps_nalu_info_buf[i].pu1_buf);
2338             }
2339         }
2340 
2341         if(ps_video_encode_op->s_inp_buf.apv_bufs[0])
2342         {
2343             for(i = 0; i < DEFAULT_MAX_INPUT_BUFS; i++)
2344             {
2345                 if(ps_app_ctxt->as_input_buf[i].pu1_buf ==
2346                    ps_video_encode_op->s_inp_buf.apv_bufs[0])
2347                 {
2348                     ps_app_ctxt->as_input_buf[i].u4_is_free = 1;
2349                     break;
2350                 }
2351             }
2352         }
2353 
2354         for(i = 0; i < ps_app_ctxt->u1_num_spatial_layers; i++)
2355         {
2356             ps_app_ctxt->as_nalu_info_bufs[pi4_nalu_info_buf_ids[i]].b_is_free = true;
2357         }
2358 
2359         /**********************************************************************
2360          *  Print stats
2361          **********************************************************************/
2362         {
2363             UWORD8 u1_pic_type[][5] = {"IDR", "I", "P", "B", "NA"};
2364             WORD32 lookup_idx = 0;
2365 
2366             if(s_video_encode_op.s_ive_op.u4_encoded_frame_type == IV_IDR_FRAME)
2367             {
2368                 lookup_idx = 0;
2369             }
2370             else if(s_video_encode_op.s_ive_op.u4_encoded_frame_type == IV_I_FRAME)
2371             {
2372                 lookup_idx = 1;
2373             }
2374             else if(s_video_encode_op.s_ive_op.u4_encoded_frame_type == IV_P_FRAME)
2375             {
2376                 lookup_idx = 2;
2377             }
2378             else if(s_video_encode_op.s_ive_op.u4_encoded_frame_type == IV_B_FRAME)
2379             {
2380                 lookup_idx = 3;
2381             }
2382             else if(s_video_encode_op.s_ive_op.u4_encoded_frame_type == IV_NA_FRAME)
2383             {
2384                 lookup_idx = 4;
2385             }
2386 
2387             if(s_video_encode_op.s_ive_op.u4_encoded_frame_type != IV_NA_FRAME)
2388             {
2389                 ps_app_ctxt->u4_pics_cnt++;
2390                 ps_app_ctxt->avg_time = u4_total_time / ps_app_ctxt->u4_pics_cnt;
2391                 ps_app_ctxt->u4_total_bytes = ps_video_encode_op->s_out_buf.u4_bytes;
2392             }
2393 
2394             if(ps_app_ctxt->u4_psnr_enable == 0)
2395             {
2396                 printf("[%s] PicNum %4d ", u1_pic_type[lookup_idx], ps_app_ctxt->u4_pics_cnt);
2397                 printf(" Bytes : %6d \t", ps_app_ctxt->u4_total_bytes);
2398                 printf(
2399                     "TimeTaken(microsec): %6d "
2400                     "AvgTime: %6d PeakAvgTimeMax: %6d\n",
2401                     timetaken, ps_app_ctxt->avg_time, peak_avg_max);
2402             }
2403         }
2404 
2405         /* For psnr computation, we need to read the correct input frame and
2406          * compare with recon. The difficulty with doing it is that we only know
2407          * that the frame number of recon is monotonically increasing. There
2408          * may be gaps in the recon if any pre or post enc skip happens. There are
2409          * 3 senarios
2410          *  1) A frame is encoded -> returns the pic type
2411          *  2) A frame is not encoded -> Encoder is waiting, the frame may get
2412          *     encoded later
2413          *  3) A frame is not encoded -> A post enc or pre enc skip happend. The
2414          *     frame is not going to be encoded
2415          *
2416          *     The 1st and 2nd scenarios are easy, since we just needs to increment
2417          *     recon cnt whenever we get a valid recon. This cnt can we used to
2418          *     sync the recon and input
2419          *     3rd scenario in conjuction with 2nd will pose problems. Even if
2420          *     the returning frame is NA, we donot know we should increment the
2421          *     recon cnt or not becasue it can be case 2 or case 3.
2422          *
2423          *  Solutions:
2424          *  -------------------------
2425          *   One way to over come this will be to return more information as of
2426          *   the frame type. We can send if a frame was skipped as a part of the
2427          *   return frame type.
2428          *   This will not work. Since the output and recon are not in sync, we
2429          *   cannot use the current output frame type to determine if a recon
2430          *   is present currently or not. We need some other way to acheive this.
2431          *
2432          *   Other way to do this which is cleaner and maintains the seperation
2433          *   between recon and the ouptut is to set the width [& height] of output
2434          * recon buffer to be zero. Hence we will in effect be saying :"look there
2435          *   is a recon, but due to frame not being encoded it is having a width 0".
2436          *   To be more clear we need to make height also to be zero.
2437          *
2438          *   But are we using these variables for allocating and deallocating
2439          *   the buffers some where ? No we are not. The buffer gets re-init
2440          *   at every encode call
2441          *
2442          *   Fixes
2443          *   ------------------------
2444          *   Currently the recon buff width and height are set in the encoder.
2445          *   This will not work now because since recon and input are not
2446          *   in sync. Hence a recon buff sent at time stamp x will get used to
2447          *   fill recon of input at time stamp y (x > y). If we reduced the
2448          *   frame dimensions in between, the recon buffer will not have enough
2449          *   space. Hence we need to set the with and height appropriatley inside
2450          *   lib itself.
2451          */
2452 
2453         if(ps_app_ctxt->u4_recon_enable || ps_app_ctxt->u4_chksum_enable ||
2454            ps_app_ctxt->u4_psnr_enable)
2455         {
2456             if(ps_video_encode_op->dump_recon)
2457             {
2458                 s_recon_buf = ps_video_encode_op->s_recon_buf;
2459 
2460                 /* Read input for psnr computuation */
2461                 if(ps_app_ctxt->u4_psnr_enable) read_input(ps_app_ctxt->fp_psnr_ip, &s_inp_buf);
2462 
2463                 /* if we have a valid recon buffer do the assocated tasks */
2464                 if(s_recon_buf.au4_wd[0])
2465                 {
2466                     /* Dump recon when enabled, and output bytes != 0 */
2467                     if(ps_app_ctxt->u4_recon_enable)
2468                     {
2469                         status = write_recon(ps_app_ctxt->fp_recon, &s_recon_buf);
2470                         if(IV_SUCCESS != status)
2471                         {
2472                             printf("Error: Unable to write to recon file\n");
2473                             break;
2474                         }
2475                     }
2476 
2477                     if(ps_app_ctxt->u4_psnr_enable)
2478                     {
2479                         compute_psnr(ps_app_ctxt, &s_recon_buf, &s_inp_buf);
2480                     }
2481 
2482                     if(ps_app_ctxt->u4_chksum_enable)
2483                     {
2484                         WORD32 comp, num_comp = 2;
2485 
2486                         if(IV_YUV_420P == s_recon_buf.e_color_fmt) num_comp = 3;
2487 
2488                         for(comp = 0; comp < num_comp; comp++)
2489                         {
2490                             UWORD8 au1_chksum[16];
2491                             calc_md5_cksum((UWORD8 *) s_recon_buf.apv_bufs[comp],
2492                                            s_recon_buf.au4_strd[comp], s_recon_buf.au4_wd[comp],
2493                                            s_recon_buf.au4_ht[comp], au1_chksum);
2494                             fwrite(au1_chksum, sizeof(UWORD8), 16, ps_app_ctxt->fp_chksum);
2495                         }
2496                     }
2497                 }
2498             }
2499         }
2500 
2501         u4_timestamp_low++;
2502 
2503         /* Break if all the encoded frames are taken from encoder */
2504         if(1 == ps_video_encode_op->u4_is_last)
2505         {
2506             break;
2507         }
2508     }
2509 
2510     /* Pic count is 1 more than actual num frames encoded, because last call is to
2511      * just get the output  */
2512     ps_app_ctxt->u4_pics_cnt--;
2513 
2514     if(ps_app_ctxt->u4_psnr_enable)
2515     {
2516         print_average_psnr(ps_app_ctxt);
2517     }
2518 
2519     /* house keeping operations */
2520     fclose(ps_app_ctxt->fp_ip);
2521     fclose(ps_app_ctxt->fp_op);
2522     if(1 == ps_app_ctxt->u4_recon_enable)
2523     {
2524         fclose(ps_app_ctxt->fp_recon);
2525     }
2526     if(1 == ps_app_ctxt->u4_nalu_info_export_enable)
2527     {
2528         fclose(ps_app_ctxt->fp_nalu_info);
2529     }
2530     if(1 == ps_app_ctxt->u4_chksum_enable)
2531     {
2532         fclose(ps_app_ctxt->fp_chksum);
2533     }
2534     if(1 == ps_app_ctxt->u4_psnr_enable)
2535     {
2536         fclose(ps_app_ctxt->fp_psnr_ip);
2537     }
2538 
2539     if(0 != ps_app_ctxt->u4_mb_info_type)
2540     {
2541         fclose(ps_app_ctxt->fp_mb_info);
2542     }
2543     if(ps_app_ctxt->u4_pic_info_type)
2544     {
2545         fclose(ps_app_ctxt->fp_pic_info);
2546     }
2547 
2548     free_input(ps_app_ctxt);
2549     free_output(ps_app_ctxt);
2550     free_recon(ps_app_ctxt);
2551     free_nalu_info_bufs(ps_app_ctxt);
2552 
2553     isvca_aligned_free(s_video_encode_ip.ps_nalu_info_buf);
2554     isvca_aligned_free(s_video_encode_op.ps_nalu_info_buf);
2555 
2556     isvca_aligned_free(pi4_nalu_info_buf_ids);
2557 }
2558 
init_default_rc_params(app_ctxt_t * ps_app_ctxt)2559 void init_default_rc_params(app_ctxt_t *ps_app_ctxt)
2560 {
2561     UWORD8 i;
2562     for(i = 0; i < ps_app_ctxt->u1_num_spatial_layers; i++)
2563     {
2564         ps_app_ctxt->pu4_max_bitrate[i] = DEFAULT_MAX_BITRATE;
2565         ps_app_ctxt->pu4_bitrate[i] = DEFAULT_BITRATE;
2566         ps_app_ctxt->pu4_i_qp[i] = DEFAULT_I_QP;
2567         ps_app_ctxt->pu4_p_qp[i] = DEFAULT_P_QP;
2568         ps_app_ctxt->pu4_b_qp[i] = DEFAULT_B_QP;
2569         ps_app_ctxt->pu4_i_qp_min[i] = DEFAULT_QP_MIN;
2570         ps_app_ctxt->pu4_i_qp_max[i] = DEFAULT_QP_MAX;
2571         ps_app_ctxt->pu4_p_qp_min[i] = DEFAULT_QP_MIN;
2572         ps_app_ctxt->pu4_p_qp_max[i] = DEFAULT_QP_MAX;
2573         ps_app_ctxt->pu4_b_qp_min[i] = DEFAULT_QP_MIN;
2574         ps_app_ctxt->pu4_b_qp_max[i] = DEFAULT_QP_MAX;
2575         ps_app_ctxt->pu4_vbv_buffer_delay[i] = 1000;
2576     }
2577 
2578     ps_app_ctxt->u4_total_bytes = 0;
2579 }
2580 
2581 /* Allocate memory to dynamic arrays holding RC information */
alloc_rc_params(app_ctxt_t * ps_app_ctxt)2582 void alloc_rc_params(app_ctxt_t *ps_app_ctxt)
2583 {
2584     UWORD8 u1_num_spatial_layers = ps_app_ctxt->u1_num_spatial_layers;
2585     ps_app_ctxt->pu4_max_bitrate =
2586         (UWORD32 *) isvca_aligned_malloc(16, sizeof(UWORD32) * u1_num_spatial_layers);
2587     ps_app_ctxt->pu4_bitrate =
2588         (UWORD32 *) isvca_aligned_malloc(16, sizeof(UWORD32) * u1_num_spatial_layers);
2589     ps_app_ctxt->pu4_i_qp =
2590         (UWORD32 *) isvca_aligned_malloc(16, sizeof(UWORD32) * u1_num_spatial_layers);
2591     ps_app_ctxt->pu4_p_qp =
2592         (UWORD32 *) isvca_aligned_malloc(16, sizeof(UWORD32) * u1_num_spatial_layers);
2593     ps_app_ctxt->pu4_b_qp =
2594         (UWORD32 *) isvca_aligned_malloc(16, sizeof(UWORD32) * u1_num_spatial_layers);
2595     ps_app_ctxt->pu4_i_qp_min =
2596         (UWORD32 *) isvca_aligned_malloc(16, sizeof(UWORD32) * u1_num_spatial_layers);
2597     ps_app_ctxt->pu4_i_qp_max =
2598         (UWORD32 *) isvca_aligned_malloc(16, sizeof(UWORD32) * u1_num_spatial_layers);
2599     ps_app_ctxt->pu4_p_qp_min =
2600         (UWORD32 *) isvca_aligned_malloc(16, sizeof(UWORD32) * u1_num_spatial_layers);
2601     ps_app_ctxt->pu4_p_qp_max =
2602         (UWORD32 *) isvca_aligned_malloc(16, sizeof(UWORD32) * u1_num_spatial_layers);
2603     ps_app_ctxt->pu4_b_qp_min =
2604         (UWORD32 *) isvca_aligned_malloc(16, sizeof(UWORD32) * u1_num_spatial_layers);
2605     ps_app_ctxt->pu4_b_qp_max =
2606         (UWORD32 *) isvca_aligned_malloc(16, sizeof(UWORD32) * u1_num_spatial_layers);
2607     ps_app_ctxt->pu4_vbv_buffer_delay =
2608         (UWORD32 *) isvca_aligned_malloc(16, sizeof(UWORD32) * u1_num_spatial_layers);
2609 }
2610 
2611 /* Free memory allocated to dynamic arrays holding RC information */
free_rc_params(app_ctxt_t * ps_app_ctxt)2612 void free_rc_params(app_ctxt_t *ps_app_ctxt)
2613 {
2614     isvca_aligned_free(ps_app_ctxt->pu4_max_bitrate);
2615     isvca_aligned_free(ps_app_ctxt->pu4_bitrate);
2616     isvca_aligned_free(ps_app_ctxt->pu4_i_qp);
2617     isvca_aligned_free(ps_app_ctxt->pu4_p_qp);
2618     isvca_aligned_free(ps_app_ctxt->pu4_b_qp);
2619     isvca_aligned_free(ps_app_ctxt->pu4_i_qp_min);
2620     isvca_aligned_free(ps_app_ctxt->pu4_i_qp_max);
2621     isvca_aligned_free(ps_app_ctxt->pu4_p_qp_min);
2622     isvca_aligned_free(ps_app_ctxt->pu4_p_qp_max);
2623     isvca_aligned_free(ps_app_ctxt->pu4_b_qp_min);
2624     isvca_aligned_free(ps_app_ctxt->pu4_b_qp_max);
2625     isvca_aligned_free(ps_app_ctxt->pu4_vbv_buffer_delay);
2626 }
2627 
2628 /*****************************************************************************/
2629 /*                                                                           */
2630 /*  Function Name : main                                                     */
2631 /*                                                                           */
2632 /*  Description   : Application to demonstrate codec API                     */
2633 /*                                                                           */
2634 /*                                                                           */
2635 /*  Inputs        : argc    - Number of arguments                            */
2636 /*                  argv[]  - Arguments                                      */
2637 /*  Globals       :                                                          */
2638 /*  Processing    : Shows how to use create, process, control and delete     */
2639 /*                                                                           */
2640 /*  Outputs       : Codec output in a file                                   */
2641 /*  Returns       :                                                          */
2642 /*                                                                           */
2643 /*  Issues        : Assumes both PROFILE_ENABLE to be                        */
2644 /*                  defined for multithread decode-display working           */
2645 /*                                                                           */
2646 /*  Revision History:                                                        */
2647 /*                                                                           */
2648 /*         DD MM YYYY   Author(s)       Changes                              */
2649 /*         20 11 2013   100189          Initial Version                      */
2650 /*****************************************************************************/
2651 #ifdef IOS
h264enc_main(char * homedir,char * documentdir,int screen_wd,int screen_ht)2652 int h264enc_main(char *homedir, char *documentdir, int screen_wd, int screen_ht)
2653 #else
2654 int main(int argc, char *argv[])
2655 #endif
2656 {
2657     /* Config Parameters for Encoding */
2658     app_ctxt_t s_app_ctxt;
2659 
2660     /* error string */
2661     CHAR ac_error[2 * STRLENGTH];
2662 
2663     /* config file name */
2664     CHAR ac_cfg_fname[STRLENGTH];
2665 
2666     /* error status */
2667     IV_STATUS_T status = IV_SUCCESS;
2668 #ifdef IOS
2669     /* temp var */
2670     CHAR filename_with_path[STRLENGTH];
2671 #endif
2672     WORD32 num_mem_recs;
2673     iv_obj_t *ps_enc;
2674     WORD32 i;
2675     FILE *fp_cfg = NULL;
2676 
2677 #ifdef X86_MINGW
2678 
2679     /* For getting printfs without any delay in eclipse */
2680     setvbuf(stdout, NULL, _IONBF, 0);
2681     setvbuf(stderr, NULL, _IONBF, 0);
2682 
2683 #endif
2684 
2685     init_default_params(&s_app_ctxt);
2686 
2687 #ifndef IOS
2688 
2689     /* Usage */
2690     if(argc < 2)
2691     {
2692         printf("Using enc.cfg as configuration file \n");
2693         strcpy(ac_cfg_fname, "enc.cfg");
2694     }
2695     else if(argc == 2)
2696     {
2697         if(!strcmp(argv[1], "--help"))
2698         {
2699             print_usage();
2700             exit(-1);
2701         }
2702         strcpy(ac_cfg_fname, argv[1]);
2703     }
2704 
2705 #else
2706     strcpy(ac_cfg_fname, "test.cfg");
2707 
2708 #endif
2709 
2710     /*************************************************************************/
2711     /* Parse arguments                                                       */
2712     /*************************************************************************/
2713 
2714 #ifndef IOS
2715 
2716     /* Read command line arguments */
2717     if(argc > 2)
2718     {
2719         for(i = 1; i + 1 < argc; i += 2)
2720         {
2721             if(CONFIG == get_argument(argv[i]))
2722             {
2723                 strcpy(ac_cfg_fname, argv[i + 1]);
2724                 if((fp_cfg = fopen(ac_cfg_fname, "r")) == NULL)
2725                 {
2726                     snprintf(ac_error, sizeof(ac_error) - 1, "Could not open Configuration file %s",
2727                              ac_cfg_fname);
2728                     codec_exit(ac_error);
2729                 }
2730                 read_cfg_file(&s_app_ctxt, fp_cfg);
2731                 fclose(fp_cfg);
2732             }
2733             else
2734             {
2735                 parse_argument(&s_app_ctxt, argv[i], argv[i + 1]);
2736             }
2737         }
2738     }
2739     else
2740     {
2741         if((fp_cfg = fopen(ac_cfg_fname, "r")) == NULL)
2742         {
2743             snprintf(ac_error, sizeof(ac_error) - 1, "Could not open Configuration file %s",
2744                      ac_cfg_fname);
2745             codec_exit(ac_error);
2746         }
2747         read_cfg_file(&s_app_ctxt, fp_cfg);
2748         fclose(fp_cfg);
2749     }
2750 
2751     alloc_rc_params(&s_app_ctxt);
2752     init_default_rc_params(&s_app_ctxt);
2753 
2754     if(argc > 2)
2755     {
2756         for(i = 1; i + 1 < argc; i += 2)
2757         {
2758             if(CONFIG == get_argument(argv[i]))
2759             {
2760                 strcpy(ac_cfg_fname, argv[i + 1]);
2761                 if((fp_cfg = fopen(ac_cfg_fname, "r")) == NULL)
2762                 {
2763                     snprintf(ac_error, sizeof(ac_error) - 1, "Could not open Configuration file %s",
2764                              ac_cfg_fname);
2765                     codec_exit(ac_error);
2766                 }
2767                 read_cfg_file_rc_params(&s_app_ctxt, fp_cfg);
2768                 fclose(fp_cfg);
2769             }
2770             else
2771             {
2772                 parse_rc_argument(&s_app_ctxt, argv[i], argv[i + 1]);
2773             }
2774         }
2775     }
2776     else
2777     {
2778         if((fp_cfg = fopen(ac_cfg_fname, "r")) == NULL)
2779         {
2780             snprintf(ac_error, sizeof(ac_error) - 1, "Could not open Configuration file %s",
2781                      ac_cfg_fname);
2782             codec_exit(ac_error);
2783         }
2784         read_cfg_file_rc_params(&s_app_ctxt, fp_cfg);
2785         fclose(fp_cfg);
2786     }
2787 
2788 #else
2789 
2790     sprintf(filename_with_path, "%s/%s", homedir, "enc.cfg");
2791     if((fp_cfg = fopen(filename_with_path, "r")) == NULL)
2792     {
2793         snprintf(ac_error, sizeof(ac_error) - 1, "Could not open Configuration file %s",
2794                  ac_cfg_fname);
2795         codec_exit(ac_error);
2796     }
2797     read_cfg_file(&s_app_ctxt, fp_cfg);
2798     fclose(fp_cfg);
2799 
2800 #endif
2801 
2802     validate_params(&s_app_ctxt);
2803 
2804     s_app_ctxt.u4_max_wd = MAX(s_app_ctxt.u4_max_wd, s_app_ctxt.u4_wd);
2805     s_app_ctxt.u4_max_ht = MAX(s_app_ctxt.u4_max_ht, s_app_ctxt.u4_ht);
2806 
2807     /*************************************************************************/
2808     /*                      Getting Number of MemRecords                     */
2809     /*************************************************************************/
2810     {
2811         isvce_num_mem_rec_ip_t s_num_mem_rec_ip;
2812         isvce_num_mem_rec_op_t s_num_mem_rec_op;
2813 
2814         isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_GET_NUM_MEM_REC, ISVCE_CMD_CT_NA};
2815 
2816         s_num_mem_rec_ip.s_ive_ip.u4_size = sizeof(isvce_num_mem_rec_ip_t);
2817         s_num_mem_rec_op.s_ive_op.u4_size = sizeof(isvce_num_mem_rec_op_t);
2818 
2819         status = isvce_api_function(0, &s_num_mem_rec_ip, &s_num_mem_rec_op, &s_api_cmds);
2820 
2821         if(status != IV_SUCCESS)
2822         {
2823             snprintf(ac_error, sizeof(ac_error) - 1, "Get number of memory records failed = 0x%x\n",
2824                      s_num_mem_rec_op.s_ive_op.u4_error_code);
2825             codec_exit(ac_error);
2826         }
2827 
2828         s_app_ctxt.u4_num_mem_rec = num_mem_recs = s_num_mem_rec_op.s_ive_op.u4_num_mem_rec;
2829     }
2830 
2831     /* Allocate array to hold memory records */
2832     s_app_ctxt.ps_mem_rec = (iv_mem_rec_t *) malloc(num_mem_recs * sizeof(iv_mem_rec_t));
2833     if(NULL == s_app_ctxt.ps_mem_rec)
2834     {
2835         snprintf(ac_error, sizeof(ac_error) - 1,
2836                  "Unable to allocate memory for hold memory records: Size %d",
2837                  (WORD32) (num_mem_recs * sizeof(iv_mem_rec_t)));
2838         codec_exit(ac_error);
2839     }
2840 
2841     {
2842         iv_mem_rec_t *ps_mem_rec;
2843         ps_mem_rec = s_app_ctxt.ps_mem_rec;
2844         for(i = 0; i < num_mem_recs; i++)
2845         {
2846             ps_mem_rec->u4_size = sizeof(iv_mem_rec_t);
2847             ps_mem_rec->pv_base = NULL;
2848             ps_mem_rec->u4_mem_size = 0;
2849             ps_mem_rec->u4_mem_alignment = 0;
2850             ps_mem_rec->e_mem_type = IV_NA_MEM_TYPE;
2851 
2852             ps_mem_rec++;
2853         }
2854     }
2855 
2856     /*************************************************************************/
2857     /*                      Getting MemRecords Attributes                    */
2858     /*************************************************************************/
2859     {
2860         isvce_fill_mem_rec_ip_t s_fill_mem_rec_ip;
2861         isvce_fill_mem_rec_op_t s_fill_mem_rec_op;
2862 
2863         isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_FILL_NUM_MEM_REC, ISVCE_CMD_CT_NA};
2864 
2865         s_fill_mem_rec_ip.s_ive_ip.u4_size = sizeof(isvce_fill_mem_rec_ip_t);
2866         s_fill_mem_rec_op.s_ive_op.u4_size = sizeof(isvce_fill_mem_rec_op_t);
2867 
2868         s_fill_mem_rec_ip.s_ive_ip.ps_mem_rec = s_app_ctxt.ps_mem_rec;
2869         s_fill_mem_rec_ip.s_ive_ip.u4_num_mem_rec = s_app_ctxt.u4_num_mem_rec;
2870         s_fill_mem_rec_ip.s_ive_ip.u4_max_wd = s_app_ctxt.u4_max_wd;
2871         s_fill_mem_rec_ip.s_ive_ip.u4_max_ht = s_app_ctxt.u4_max_ht;
2872         s_fill_mem_rec_ip.u4_wd = s_app_ctxt.u4_wd;
2873         s_fill_mem_rec_ip.u4_ht = s_app_ctxt.u4_ht;
2874         s_fill_mem_rec_ip.s_ive_ip.u4_max_level = s_app_ctxt.u4_max_level;
2875         s_fill_mem_rec_ip.s_ive_ip.e_color_format = DEFAULT_INP_COLOR_FMT;
2876         s_fill_mem_rec_ip.s_ive_ip.u4_max_ref_cnt = DEFAULT_MAX_REF_FRM;
2877         s_fill_mem_rec_ip.s_ive_ip.u4_max_reorder_cnt = DEFAULT_MAX_REORDER_FRM;
2878         s_fill_mem_rec_ip.s_ive_ip.u4_max_srch_rng_x = DEFAULT_MAX_SRCH_RANGE_X;
2879         s_fill_mem_rec_ip.s_ive_ip.u4_max_srch_rng_y = DEFAULT_MAX_SRCH_RANGE_Y;
2880         s_fill_mem_rec_ip.s_svc_inp_params.u1_num_temporal_layers =
2881             s_app_ctxt.u1_num_temporal_layers;
2882         s_fill_mem_rec_ip.s_svc_inp_params.u1_num_spatial_layers = s_app_ctxt.u1_num_spatial_layers;
2883         s_fill_mem_rec_ip.s_svc_inp_params.d_spatial_res_ratio = s_app_ctxt.d_spatial_res_ratio;
2884 
2885         status = isvce_api_function(0, &s_fill_mem_rec_ip, &s_fill_mem_rec_op, &s_api_cmds);
2886 
2887         if(status != IV_SUCCESS)
2888         {
2889             snprintf(ac_error, sizeof(ac_error) - 1, "Fill memory records failed = 0x%x\n",
2890                      s_fill_mem_rec_op.s_ive_op.u4_error_code);
2891             codec_exit(ac_error);
2892         }
2893     }
2894 
2895     /*************************************************************************/
2896     /*                      Allocating Memory for Mem Records                */
2897     /*************************************************************************/
2898     {
2899         WORD32 total_size;
2900         iv_mem_rec_t *ps_mem_rec;
2901         total_size = 0;
2902 
2903         ps_mem_rec = s_app_ctxt.ps_mem_rec;
2904         for(i = 0; i < num_mem_recs; i++)
2905         {
2906             ps_mem_rec->pv_base =
2907                 isvca_aligned_malloc(ps_mem_rec->u4_mem_alignment, ps_mem_rec->u4_mem_size);
2908             if(ps_mem_rec->pv_base == NULL)
2909             {
2910                 snprintf(ac_error, sizeof(ac_error) - 1,
2911                          "Allocation failure for mem record id %d size %d\n", i,
2912                          ps_mem_rec->u4_mem_size);
2913                 codec_exit(ac_error);
2914             }
2915             total_size += ps_mem_rec->u4_mem_size;
2916 
2917             ps_mem_rec++;
2918         }
2919         printf("\nTotal memory for codec %d\n", total_size);
2920     }
2921 
2922     /*************************************************************************/
2923     /*                        Codec Instance Creation                        */
2924     /*************************************************************************/
2925     {
2926         isvce_init_ip_t s_init_ip;
2927         isvce_init_op_t s_init_op;
2928 
2929         isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_INIT, ISVCE_CMD_CT_NA};
2930 
2931         ps_enc = s_app_ctxt.ps_mem_rec[0].pv_base;
2932         ps_enc->u4_size = sizeof(iv_obj_t);
2933         ps_enc->pv_fxns = isvce_api_function;
2934         s_app_ctxt.ps_enc = ps_enc;
2935 
2936         s_init_ip.pu4_max_bitrate =
2937             isvca_aligned_malloc(16, sizeof(UWORD32) * s_app_ctxt.u1_num_spatial_layers);
2938 
2939         s_init_ip.s_ive_ip.u4_size = sizeof(isvce_init_ip_t);
2940         s_init_op.s_ive_op.u4_size = sizeof(isvce_init_op_t);
2941 
2942         s_init_ip.s_ive_ip.u4_num_mem_rec = s_app_ctxt.u4_num_mem_rec;
2943         s_init_ip.s_ive_ip.ps_mem_rec = s_app_ctxt.ps_mem_rec;
2944         s_init_ip.s_ive_ip.u4_max_wd = s_app_ctxt.u4_max_wd;
2945         s_init_ip.s_ive_ip.u4_max_ht = s_app_ctxt.u4_max_ht;
2946         s_init_ip.u4_wd = s_app_ctxt.u4_wd;
2947         s_init_ip.u4_ht = s_app_ctxt.u4_ht;
2948         s_init_ip.s_ive_ip.u4_max_ref_cnt = DEFAULT_MAX_REF_FRM;
2949         s_init_ip.s_ive_ip.u4_max_reorder_cnt = DEFAULT_MAX_REORDER_FRM;
2950         s_init_ip.s_ive_ip.u4_max_level = s_app_ctxt.u4_max_level;
2951         s_init_ip.s_ive_ip.e_inp_color_fmt = s_app_ctxt.e_inp_color_fmt;
2952 
2953         if(s_app_ctxt.u4_recon_enable || s_app_ctxt.u4_psnr_enable || s_app_ctxt.u4_chksum_enable)
2954         {
2955             s_init_ip.s_ive_ip.u4_enable_recon = 1;
2956         }
2957         else
2958         {
2959             s_init_ip.s_ive_ip.u4_enable_recon = 0;
2960         }
2961 
2962         s_init_ip.b_nalu_info_export_enable = !!s_app_ctxt.u4_nalu_info_export_enable;
2963         s_init_ip.s_ive_ip.e_recon_color_fmt = s_app_ctxt.e_recon_color_fmt;
2964         s_init_ip.s_ive_ip.e_rc_mode = s_app_ctxt.u4_rc;
2965         s_init_ip.s_ive_ip.u4_max_framerate = s_app_ctxt.u4_max_frame_rate;
2966         for(i = 0; i < s_app_ctxt.u1_num_spatial_layers; i++)
2967         {
2968             s_init_ip.pu4_max_bitrate[i] = s_app_ctxt.pu4_max_bitrate[i];
2969         }
2970         s_init_ip.s_ive_ip.u4_num_bframes = s_app_ctxt.u4_num_bframes;
2971         s_init_ip.s_ive_ip.e_content_type = IV_PROGRESSIVE;
2972         s_init_ip.s_ive_ip.u4_max_srch_rng_x = DEFAULT_MAX_SRCH_RANGE_X;
2973         s_init_ip.s_ive_ip.u4_max_srch_rng_y = DEFAULT_MAX_SRCH_RANGE_Y;
2974         s_init_ip.s_ive_ip.e_slice_mode = s_app_ctxt.u4_slice_mode;
2975         s_init_ip.s_ive_ip.u4_slice_param = s_app_ctxt.u4_slice_param;
2976         s_init_ip.s_ive_ip.e_arch = s_app_ctxt.e_arch;
2977         s_init_ip.s_ive_ip.e_soc = s_app_ctxt.e_soc;
2978         s_init_ip.b_use_default_vui = s_app_ctxt.u4_use_default_vui;
2979 
2980         s_init_ip.s_svc_inp_params.u1_num_temporal_layers = s_app_ctxt.u1_num_temporal_layers;
2981         s_init_ip.s_svc_inp_params.u1_num_spatial_layers = s_app_ctxt.u1_num_spatial_layers;
2982         s_init_ip.s_svc_inp_params.d_spatial_res_ratio = s_app_ctxt.d_spatial_res_ratio;
2983 
2984         status = isvce_api_function(ps_enc, &s_init_ip, &s_init_op, &s_api_cmds);
2985 
2986         isvca_aligned_free(s_init_ip.pu4_max_bitrate);
2987 
2988         if(status != IV_SUCCESS)
2989         {
2990             snprintf(ac_error, sizeof(ac_error) - 1, "Init memory records failed = 0x%x\n",
2991                      s_init_op.s_ive_op.u4_error_code);
2992             codec_exit(ac_error);
2993         }
2994     }
2995 
2996     /*************************************************************************/
2997     /*                        set processor details                          */
2998     /*************************************************************************/
2999     {
3000         isvce_ctl_set_num_cores_ip_t s_ctl_set_num_cores_ip;
3001         isvce_ctl_set_num_cores_op_t s_ctl_set_num_cores_op;
3002 
3003         isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_VIDEO_CTL, ISVCE_CMD_CTL_SET_NUM_CORES};
3004 
3005         s_ctl_set_num_cores_ip.s_ive_ip.u4_num_cores = s_app_ctxt.u4_num_cores;
3006         s_ctl_set_num_cores_ip.s_ive_ip.u4_timestamp_high = 0;
3007         s_ctl_set_num_cores_ip.s_ive_ip.u4_timestamp_low = 0;
3008         s_ctl_set_num_cores_ip.s_ive_ip.u4_size = sizeof(isvce_ctl_set_num_cores_ip_t);
3009 
3010         s_ctl_set_num_cores_op.s_ive_op.u4_size = sizeof(isvce_ctl_set_num_cores_op_t);
3011 
3012         status = isvce_api_function(ps_enc, (void *) &s_ctl_set_num_cores_ip,
3013                                     (void *) &s_ctl_set_num_cores_op, &s_api_cmds);
3014         if(status != IV_SUCCESS)
3015         {
3016             snprintf(ac_error, sizeof(ac_error) - 1, "Unable to set processor params = 0x%x\n",
3017                      s_ctl_set_num_cores_op.s_ive_op.u4_error_code);
3018             codec_exit(ac_error);
3019         }
3020     }
3021 
3022     /*************************************************************************/
3023     /*                        Get Codec Version                              */
3024     /*************************************************************************/
3025     {
3026         isvce_ctl_getversioninfo_ip_t s_ctl_set_getversioninfo_ip;
3027         isvce_ctl_getversioninfo_op_t s_ctl_set_getversioninfo_op;
3028 
3029         CHAR ac_version_string[STRLENGTH];
3030 
3031         isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_VIDEO_CTL, ISVCE_CMD_CTL_GETVERSION};
3032 
3033         s_ctl_set_getversioninfo_ip.s_ive_ip.pu1_version = (UWORD8 *) ac_version_string;
3034         s_ctl_set_getversioninfo_ip.s_ive_ip.u4_version_bufsize = sizeof(ac_version_string);
3035         s_ctl_set_getversioninfo_ip.s_ive_ip.u4_size = sizeof(isvce_ctl_getversioninfo_ip_t);
3036         s_ctl_set_getversioninfo_op.s_ive_op.u4_size = sizeof(isvce_ctl_getversioninfo_op_t);
3037 
3038         status = isvce_api_function(ps_enc, (void *) &s_ctl_set_getversioninfo_ip,
3039                                     (void *) &s_ctl_set_getversioninfo_op, &s_api_cmds);
3040         if(status != IV_SUCCESS)
3041         {
3042             snprintf(ac_error, sizeof(ac_error) - 1, "Unable to get codec version = 0x%x\n",
3043                      s_ctl_set_getversioninfo_op.s_ive_op.u4_error_code);
3044             codec_exit(ac_error);
3045         }
3046         printf("CODEC VERSION %s\n", ac_version_string);
3047     }
3048 
3049     /*************************************************************************/
3050     /*                      Get I/O Buffer Requirement                       */
3051     /*************************************************************************/
3052     {
3053         isvce_ctl_getbufinfo_ip_t s_get_buf_info_ip;
3054         isvce_ctl_getbufinfo_op_t s_get_buf_info_op;
3055 
3056         isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_VIDEO_CTL, ISVCE_CMD_CTL_GETBUFINFO};
3057 
3058         s_get_buf_info_ip.s_ive_ip.u4_size = sizeof(isvce_ctl_getbufinfo_ip_t);
3059         s_get_buf_info_op.s_ive_op.u4_size = sizeof(isvce_ctl_getbufinfo_op_t);
3060 
3061         s_get_buf_info_ip.s_ive_ip.u4_max_ht = s_app_ctxt.u4_max_ht;
3062         s_get_buf_info_ip.s_ive_ip.u4_max_wd = s_app_ctxt.u4_max_wd;
3063         s_get_buf_info_ip.s_ive_ip.e_inp_color_fmt = s_app_ctxt.e_inp_color_fmt;
3064 
3065         status = isvce_api_function(ps_enc, &s_get_buf_info_ip, &s_get_buf_info_op, &s_api_cmds);
3066 
3067         if(status != IV_SUCCESS)
3068         {
3069             snprintf(ac_error, sizeof(ac_error) - 1,
3070                      "Unable to get I/O buffer requirements = 0x%x\n",
3071                      s_get_buf_info_op.s_ive_op.u4_error_code);
3072             codec_exit(ac_error);
3073         }
3074         s_app_ctxt.s_get_buf_info_op = s_get_buf_info_op;
3075     }
3076 
3077     /*****************************************************************************/
3078     /* Add the following initializations based on the parameters in context */
3079     /*****************************************************************************/
3080 
3081     /*****************************************************************************/
3082     /*   Video control  Set Frame dimensions */
3083     /*****************************************************************************/
3084     set_dimensions(&s_app_ctxt, 0, 0);
3085     s_app_ctxt.u4_strd = s_app_ctxt.u4_wd;
3086 
3087     /*****************************************************************************/
3088     /*   Video control  Set Frame rates */
3089     /*****************************************************************************/
3090     set_frame_rate(&s_app_ctxt, 0, 0);
3091 
3092     /*****************************************************************************/
3093     /*   Video control  Set IPE Params */
3094     /*****************************************************************************/
3095     set_ipe_params(&s_app_ctxt, 0, 0);
3096 
3097     /*****************************************************************************/
3098     /*   Video control  Set Bitrate */
3099     /*****************************************************************************/
3100     set_bit_rate(&s_app_ctxt, 0, 0);
3101 
3102     /*****************************************************************************/
3103     /*   Video control  Set QP */
3104     /*****************************************************************************/
3105     set_qp(&s_app_ctxt, 0, 0);
3106 
3107     /*****************************************************************************/
3108     /*   Video control  Set AIR params */
3109     /*****************************************************************************/
3110     set_air_params(&s_app_ctxt, 0, 0);
3111 
3112     /*****************************************************************************/
3113     /*   Video control  Set VBV params */
3114     /*****************************************************************************/
3115     set_vbv_params(&s_app_ctxt, 0, 0);
3116 
3117     /*****************************************************************************/
3118     /*   Video control  Set Motion estimation params */
3119     /*****************************************************************************/
3120     set_me_params(&s_app_ctxt, 0, 0);
3121 
3122     /*****************************************************************************/
3123     /*   Video control  Set GOP params */
3124     /*****************************************************************************/
3125     set_gop_params(&s_app_ctxt, 0, 0);
3126 
3127     /*****************************************************************************/
3128     /*   Video control  Set Deblock params */
3129     /*****************************************************************************/
3130     set_deblock_params(&s_app_ctxt, 0, 0);
3131 
3132     /*****************************************************************************/
3133     /*   Video control  Set Profile params */
3134     /*****************************************************************************/
3135     set_profile_params(&s_app_ctxt, 0, 0);
3136 
3137     /*****************************************************************************/
3138     /*   Video control Set in Encode header mode */
3139     /*****************************************************************************/
3140     set_enc_mode(&s_app_ctxt, 0, 0, IVE_ENC_MODE_PICTURE);
3141 
3142     /*****************************************************************************/
3143     /*   Video usability information */
3144     /*****************************************************************************/
3145     set_vui_params(&s_app_ctxt);
3146 
3147 #ifdef IOS
3148     /* Correct file paths */
3149     sprintf(filename_with_path, "%s/%s", documentdir, s_app_ctxt.ac_ip_fname);
3150     strcpy(s_app_ctxt.ac_ip_fname, filename_with_path);
3151 
3152     sprintf(filename_with_path, "%s/%s", documentdir, s_app_ctxt.ac_op_fname);
3153     strcpy(s_app_ctxt.ac_op_fname, filename_with_path);
3154 
3155     sprintf(filename_with_path, "%s/%s", documentdir, s_app_ctxt.ac_recon_fname);
3156     strcpy(s_app_ctxt.ac_recon_fname, filename_with_path);
3157 
3158     sprintf(filename_with_path, "%s/%s", documentdir, s_app_ctxt.ac_chksum_fname);
3159     strcpy(s_app_ctxt.ac_chksum_fname, filename_with_path);
3160 
3161     sprintf(filename_with_path, "%s/%s", documentdir, s_app_ctxt.ac_mb_info_fname);
3162     strcpy(s_app_ctxt.ac_mb_info_fname, filename_with_path);
3163 
3164     sprintf(filename_with_path, "%s/%s", documentdir, s_app_ctxt.ac_pic_info_fname);
3165     strcpy(s_app_ctxt.ac_pic_info_fname, filename_with_path);
3166 #endif
3167 
3168     get_enc_dimensions(&s_app_ctxt);
3169 
3170     /*************************************************************************/
3171     /*               begin encoding                                          */
3172     /*************************************************************************/
3173 
3174     synchronous_encode(ps_enc, &s_app_ctxt);
3175 
3176     {
3177         printf("\nEncoding Completed\n");
3178         printf("Summary\n");
3179         printf("Input filename                  : %s\n", s_app_ctxt.ac_ip_fname);
3180         printf("Output filename                 : %s\n", s_app_ctxt.ac_op_fname);
3181         printf("Output Width                    : %-4d\n", s_app_ctxt.u4_wd);
3182         printf("Output Height                   : %-4d\n", s_app_ctxt.u4_ht);
3183 
3184         {
3185             DOUBLE bytes_per_frame;
3186             DOUBLE bytes_per_second;
3187             WORD32 achieved_bitrate;
3188             if(s_app_ctxt.u4_pics_cnt != 0)
3189             {
3190                 bytes_per_frame = (s_app_ctxt.u4_total_bytes) / (s_app_ctxt.u4_pics_cnt);
3191             }
3192             else
3193             {
3194                 bytes_per_frame = 0;
3195             }
3196             bytes_per_second = (bytes_per_frame * s_app_ctxt.u4_tgt_frame_rate);
3197             achieved_bitrate = (WORD32) (bytes_per_second * 8);
3198             printf("Target Bitrate (bps)            : %-4d\n", s_app_ctxt.pu4_bitrate[0]);
3199             printf("Achieved Bitrate (bps)          : %-4d\n", achieved_bitrate);
3200         }
3201 
3202         printf("Average Time per Frame          : %-4d\n", s_app_ctxt.avg_time);
3203         printf("Achieved FPS                    : %-4.2f\n", 1000000.0 / s_app_ctxt.avg_time);
3204     }
3205 
3206     free_rc_params(&s_app_ctxt);
3207 
3208     /*************************************************************************/
3209     /*                         Close Codec Instance                         */
3210     /*************************************************************************/
3211     {
3212         isvce_retrieve_mem_rec_ip_t s_retrieve_mem_ip;
3213         isvce_retrieve_mem_rec_op_t s_retrieve_mem_op;
3214         iv_mem_rec_t *ps_mem_rec;
3215 
3216         isvce_api_cmds_t s_api_cmds = {ISVCE_CMD_RETRIEVE_MEMREC, ISVCE_CMD_CT_NA};
3217 
3218         s_retrieve_mem_ip.s_ive_ip.u4_size = sizeof(isvce_retrieve_mem_rec_ip_t);
3219         s_retrieve_mem_op.s_ive_op.u4_size = sizeof(isvce_retrieve_mem_rec_op_t);
3220 
3221         s_retrieve_mem_ip.s_ive_ip.ps_mem_rec = s_app_ctxt.ps_mem_rec;
3222 
3223         status = isvce_api_function(ps_enc, &s_retrieve_mem_ip, &s_retrieve_mem_op, &s_api_cmds);
3224 
3225         if(status != IV_SUCCESS)
3226         {
3227             snprintf(ac_error, sizeof(ac_error) - 1, "Unable to retrieve memory records = 0x%x\n",
3228                      s_retrieve_mem_op.s_ive_op.u4_error_code);
3229             codec_exit(ac_error);
3230         }
3231 
3232         /* Free memory records */
3233         ps_mem_rec = s_app_ctxt.ps_mem_rec;
3234         for(i = 0; i < num_mem_recs; i++)
3235         {
3236             isvca_aligned_free(ps_mem_rec->pv_base);
3237             ps_mem_rec++;
3238         }
3239 
3240         free(s_app_ctxt.ps_mem_rec);
3241     }
3242 
3243     return 0;
3244 }
3245 
3246 #ifdef ANDROID_NDK
raise(int a)3247 int raise(int a)
3248 {
3249     printf("Divide by zero\n");
3250     return 0;
3251 }
__aeabi_assert(const char * assertion,const char * file,unsigned int line)3252 void __aeabi_assert(const char *assertion, const char *file, unsigned int line) {}
3253 #endif
3254