1 /******************************************************************************
2  *
3  * Copyright (C) 2015 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 
21 /*****************************************************************************/
22 /* File Includes                                                             */
23 /*****************************************************************************/
24 
25 /* System include files */
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stddef.h>
29 #include <assert.h>
30 #include <string.h>
31 
32 #ifndef IOS
33 #include <malloc.h>
34 #endif
35 
36 #ifdef WINDOWS_TIMER
37 #include "windows.h"
38 #else
39 #include <sys/time.h>
40 #endif
41 /* User include files */
42 #include "ih264_typedefs.h"
43 #include "iv2.h"
44 #include "ive2.h"
45 #include "ih264e.h"
46 #include "app.h"
47 #include "psnr.h"
48 
49 /* Function declarations */
50 #ifndef MD5_DISABLE
51 void calc_md5_cksum(UWORD8 *pu1_inbuf,UWORD32 u4_stride,UWORD32 u4_width,UWORD32 u4_height,UWORD8 *pu1_cksum_p );
52 #else
53 #define calc_md5_cksum(a, b, c, d, e)
54 #endif
55 
56 /*****************************************************************************/
57 /* Enums                                                                     */
58 /*****************************************************************************/
59 typedef enum
60 {
61     INVALID,
62     HELP,
63     INPUT_FILE,
64     OUTPUT_FILE,
65     RECON_FILE,
66     RECON_ENABLE,
67     CHKSUM_ENABLE,
68     CHKSUM_FILE,
69     INPUT_CHROMA_FORMAT,
70     RECON_CHROMA_FORMAT,
71     MAX_WD,
72     MAX_HT,
73     WD,
74     HT,
75     MAX_LEVEL,
76     ENC_SPEED,
77     ME_SPEED,
78     START_FRM,
79     NUM_FRMS,
80     MAX_FRAMERATE,
81     SRC_FRAMERATE,
82     TGT_FRAMERATE,
83     RC,
84     MAX_BITRATE,
85     BITRATE,
86     I_QP,
87     P_QP,
88     B_QP,
89     I_QP_MAX,
90     P_QP_MAX,
91     B_QP_MAX,
92     I_QP_MIN,
93     P_QP_MIN,
94     B_QP_MIN,
95     ENTROPY,
96     AIR,
97     AIR_REFRESH_PERIOD,
98     ARCH,
99     SOC,
100     NUMCORES,
101     PRE_ENC_ME,
102     PRE_ENC_IPE,
103     HPEL,
104     QPEL,
105     SRCH_RNG_X,
106     SRCH_RNG_Y,
107     I_INTERVAL,
108     IDR_INTERVAL,
109     B_FRMS,
110     NUM_B_FRMS,
111     DISABLE_DBLK,
112     PROFILE,
113     FAST_SAD,
114     ALT_REF,
115     DISABLE_DEBLOCK_LEVEL,
116     PSNR,
117     SLICE_MODE,
118     SLICE_PARAM,
119     CONFIG,
120     LOOPBACK,
121     VBV_DELAY,
122     VBV_SIZE,
123     INTRA_4x4_ENABLE,
124     MB_INFO_FILE,
125     MB_INFO_TYPE,
126     PIC_INFO_FILE,
127     PIC_INFO_TYPE,
128 } ARGUMENT_T;
129 
130 typedef struct
131 {
132     CHAR        argument_shortname[8];
133     CHAR        argument_name[128];
134     ARGUMENT_T  argument;
135     CHAR        description[512];
136 } argument_t;
137 
138 static const argument_t argument_mapping[] =
139         {
140                 { "--", "--help", HELP, "Print this help\n" },
141                 { "-i", "--input", INPUT_FILE, "Input file\n" },
142                 { "-o", "--output", OUTPUT_FILE, "Output file\n" },
143                 { "--", "--recon_enable", RECON_ENABLE, "Recon enable flag\n" },
144                 { "-r", "--recon", RECON_FILE, "Recon file \n" },
145                 { "--",  "--input_chroma_format",  INPUT_CHROMA_FORMAT,
146                             "Input Chroma format Supported values YUV_420P, YUV_420SP_UV, YUV_420SP_VU\n" },
147                 { "--",  "--recon_chroma_format",  RECON_CHROMA_FORMAT,
148                             "Recon Chroma format Supported values YUV_420P, YUV_420SP_UV, YUV_420SP_VU\n" },
149                 { "-w", "--width", WD, "Width of input  file\n" },
150                 { "-h", "--height", HT, "Height file\n" },
151                 { "--", "--start_frame", START_FRM,  "Starting frame number\n" },
152                 { "-f", "--num_frames", NUM_FRMS,  "Number of frames to be encoded\n" },
153                 { "--", "--rc", RC, "Rate control mode 0: Constant Qp, 1: Storage, 2: CBR non low delay, 3: CBR low delay \n" },
154                 { "--", "--max_framerate", MAX_FRAMERATE, "Maximum frame rate \n" },
155                 { "--", "--tgt_framerate", TGT_FRAMERATE, "Target frame rate \n" },
156                 { "--", "--src_framerate", SRC_FRAMERATE, "Source frame rate \n" },
157                 { "--", "--i_interval", I_INTERVAL,  "Intra frame interval \n" },
158                 { "--", "--idr_interval", IDR_INTERVAL,  "IDR frame interval \n" },
159                 { "--", "--bframes", NUM_B_FRMS, "Maximum number of consecutive B frames \n" },
160                 { "--", "--speed", ENC_SPEED, "Encoder speed preset 0 (slowest) and 100 (fastest)\n" },
161                 { "--", "--me_speed", ME_SPEED, "Encoder speed preset 0 (slowest) and 100 (fastest)\n" },
162                 { "--", "--fast_sad", FAST_SAD, " Flag for faster sad execution\n" },
163                 { "--", "--alt_ref", ALT_REF , "Flag to enable alternate refernce frames"},
164                 { "--", "--hpel", HPEL, "Flag to enable/disable Quarter pel estimation \n" },
165                 { "--", "--qpel", QPEL, "Flag to enable/disable Quarter pel estimation \n" },
166                 { "--", "--disable_deblock_level",  DISABLE_DEBLOCK_LEVEL,
167                         "Disable deblock level - 0 : Enables deblock completely, 1: enables for I and 8th frame , 2: Enables for I only, 3 : disables completely\n" },
168                 { "--", "--search_range_x", SRCH_RNG_X,     "Search range for X  \n" },
169                 { "--", "--search_range_y", SRCH_RNG_Y,     "Search range for Y \n" },
170                 { "--", "--psnr", PSNR, "Enable PSNR computation (Disable while benchmarking performance) \n" },
171                 { "--", "--pre_enc_me", PRE_ENC_ME, "Flag to enable/disable Pre Enc Motion Estimation\n" },
172                 { "--", "--pre_enc_ipe", PRE_ENC_IPE, "Flag to enable/disable Pre Enc Intra prediction Estimation\n" },
173                 { "-n", "--num_cores", NUMCORES, "Number of cores to be used\n" },
174                 { "--", "--adaptive_intra_refresh", AIR ,"Adaptive Intra Refresh enable/disable\n"},
175                 { "--", "--air_refresh_period", AIR_REFRESH_PERIOD,"adaptive intra refresh period\n"},
176                 { "--", "--slice", SLICE_MODE,  "Slice mode-  0 :No slice, 1: Bytes per slice, 2: MB/CTB per slice  \n" },
177                 { "--", "--slice_param",  SLICE_PARAM, "Slice param value based on slice mode. Slice mode of 1 implies number of bytes per slice, 2 implies number of MBs/CTBs, for 0 value is neglected \n" },
178                 { "--", "--max_wd",      MAX_WD,                "Maximum width (Default: 1920) \n" },
179                 { "--", "--max_ht",      MAX_HT,                "Maximum height (Default: 1088)\n" },
180                 { "--", "--max_level",   MAX_LEVEL,             "Maximum Level (Default: 50)\n" },
181                 { "--", "--arch", ARCH, "Set Architecture. Supported values  ARM_NONEON, ARM_A9Q, ARM_A7, ARM_A5, ARM_NEONINTR, X86_GENERIC, X86_SSSE3, X86_SSE4 \n" },
182                 { "--", "--soc", SOC, "Set SOC. Supported values  GENERIC, HISI_37X \n" },
183                 { "--", "--chksum",            CHKSUM_FILE,              "Save Check sum file for recon data\n" },
184                 { "--", "--chksum_enable",          CHKSUM_ENABLE,               "Recon MD5 Checksum file\n"},
185                 { "-c", "--config",      CONFIG,              "config file (Default: enc.cfg)\n" },
186                 { "--", "--loopback",      LOOPBACK,             "Enable encoding in a loop\n" },
187                 { "--", "--profile",      PROFILE,               "Profile mode: Supported values BASE, MAIN, HIGH\n" },
188                 { "--", "--max_bitrate",  MAX_BITRATE,           "Max bitrate\n"},
189                 { "--", "--bitrate",      BITRATE,               "Target bitrate\n"},
190                 { "--", "--qp_i",         I_QP,                  "QP for I frames\n"},
191                 { "--", "--qp_p",         P_QP,                  "QP for P frames\n"},
192                 { "--", "--qp_b",         B_QP,                  "QP for B frames\n"},
193                 { "--", "--qp_i_max",     I_QP_MAX,              "Max QP for I frames\n"},
194                 { "--", "--qp_p_max",     P_QP_MAX,              "Max QP for P frames\n"},
195                 { "--", "--qp_b_max",     B_QP_MAX,              "Max QP for B frames\n"},
196                 { "--", "--qp_i_min",     I_QP_MIN,              "Min QP for I frames\n"},
197                 { "--", "--qp_p_min",     P_QP_MIN,              "Min QP for P frames\n"},
198                 { "--", "--qp_b_min",     B_QP_MIN,              "Min QP for B frames\n"},
199                 { "--", "--entropy",      ENTROPY,              "Entropy coding mode(0: CAVLC or 1: CABAC)\n"},
200                 { "--", "--vbv_delay",    VBV_DELAY,             "VBV buffer delay\n"},
201                 { "--", "--vbv_size",     VBV_SIZE,              "VBV buffer size\n"},
202                 { "-i4", "--intra_4x4_enable", INTRA_4x4_ENABLE, "Intra 4x4 enable \n" },
203                 { "--", "--mb_info_file",     MB_INFO_FILE,              "MB info file\n"},
204                 { "--", "--mb_info_type",     MB_INFO_TYPE,              "MB info type\n"},
205                 { "--", "--pic_info_file",     PIC_INFO_FILE,              "Pic info file\n"},
206                 { "--", "--pic_info_type",     PIC_INFO_TYPE,              "Pic info type\n"},
207         };
208 
209 
210 
211 /*****************************************************************************/
212 /*  Function Declarations                                                    */
213 /*****************************************************************************/
214 
215 
216 
217 /*****************************************************************************/
218 /*  Function Definitions                                                     */
219 /*****************************************************************************/
220 
221 
222 #if(defined X86) && (defined X86_MINGW)
223 /*****************************************************************************/
224 /* Function to print library calls                                           */
225 /*****************************************************************************/
226 /*****************************************************************************/
227 /*                                                                           */
228 /*  Function Name : memalign                                                 */
229 /*                                                                           */
230 /*  Description   : Returns malloc data. Ideally should return aligned memory*/
231 /*                  support alignment will be added later                    */
232 /*                                                                           */
233 /*  Inputs        : alignment                                                */
234 /*                  size                                                     */
235 /*  Globals       :                                                          */
236 /*  Processing    :                                                          */
237 /*                                                                           */
238 /*  Outputs       :                                                          */
239 /*  Returns       :                                                          */
240 /*                                                                           */
241 /*  Issues        :                                                          */
242 /*                                                                           */
243 /*  Revision History:                                                        */
244 /*                                                                           */
245 /*         DD MM YYYY   Author(s)       Changes                              */
246 /*         07 09 2012   100189          Initial Version                      */
247 /*                                                                           */
248 /*****************************************************************************/
249 
ih264a_aligned_malloc(WORD32 alignment,WORD32 size)250 void * ih264a_aligned_malloc(WORD32 alignment, WORD32 size)
251 {
252     return _aligned_malloc(size, alignment);
253 }
254 
ih264a_aligned_free(void * pv_buf)255 void ih264a_aligned_free(void *pv_buf)
256 {
257     _aligned_free(pv_buf);
258     return;
259 }
260 
261 #elif IOS
262 
ih264a_aligned_malloc(WORD32 alignment,WORD32 size)263 void * ih264a_aligned_malloc(WORD32 alignment, WORD32 size)
264 {
265     return malloc(size);
266 }
267 
ih264a_aligned_free(void * pv_buf)268 void ih264a_aligned_free(void *pv_buf)
269 {
270     free(pv_buf);
271     return;
272 }
273 
274 #else
275 
ih264a_aligned_malloc(WORD32 alignment,WORD32 size)276 void * ih264a_aligned_malloc(WORD32 alignment, WORD32 size)
277 {
278     return memalign(alignment, size);
279 }
280 
ih264a_aligned_free(void * pv_buf)281 void ih264a_aligned_free(void *pv_buf)
282 {
283     free(pv_buf);
284     return;
285 }
286 
287 #endif
288 
289 /*****************************************************************************/
290 /*                                                                           */
291 /*  Function Name : codec_exit                                               */
292 /*                                                                           */
293 /*  Description   : handles unrecoverable errors                             */
294 /*  Inputs        : Error message                                            */
295 /*  Globals       : None                                                     */
296 /*  Processing    : Prints error message to console and exits.               */
297 /*  Outputs       : Error message to the console                             */
298 /*  Returns       : None                                                     */
299 /*                                                                           */
300 /*  Issues        :                                                          */
301 /*                                                                           */
302 /*  Revision History:                                                        */
303 /*                                                                           */
304 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
305 /*         07 06 2006   Sankar          Creation                             */
306 /*                                                                           */
307 /*****************************************************************************/
codec_exit(CHAR * pc_err_message)308 void codec_exit(CHAR *pc_err_message)
309 {
310     printf("%s\n", pc_err_message);
311     exit(-1);
312 }
313 
314 /*****************************************************************************/
315 /*                                                                           */
316 /*  Function Name : codec_exit                                               */
317 /*                                                                           */
318 /*  Description   : handles unrecoverable errors                             */
319 /*  Inputs        : Error message                                            */
320 /*  Globals       : None                                                     */
321 /*  Processing    : Prints error message to console and exits.               */
322 /*  Outputs       : Error mesage to the console                              */
323 /*  Returns       : None                                                     */
324 /*                                                                           */
325 /*  Issues        :                                                          */
326 /*                                                                           */
327 /*  Revision History:                                                        */
328 /*                                                                           */
329 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
330 /*         07 06 2006   Sankar          Creation                             */
331 /*                                                                           */
332 /*****************************************************************************/
get_chroma_fmt(CHAR * value)333 IV_COLOR_FORMAT_T get_chroma_fmt(CHAR *value)
334 {
335     IV_COLOR_FORMAT_T e_chroma_format;
336     if((strcmp(value, "YUV_420P")) == 0)
337         e_chroma_format = IV_YUV_420P;
338     else if((strcmp(value, "YUV_422ILE")) == 0)
339         e_chroma_format = IV_YUV_422ILE;
340     else if((strcmp(value, "RGB_565")) == 0)
341         e_chroma_format = IV_RGB_565;
342     else if((strcmp(value, "RGBA_8888")) == 0)
343         e_chroma_format = IV_RGBA_8888;
344     else if((strcmp(value, "YUV_420SP_UV")) == 0)
345         e_chroma_format = IV_YUV_420SP_UV;
346     else if((strcmp(value, "YUV_420SP_VU")) == 0)
347         e_chroma_format = IV_YUV_420SP_VU;
348     else
349     {
350         printf("\nInvalid colour format setting it to IV_YUV_420P\n");
351         e_chroma_format = IV_YUV_420P;
352     }
353     return e_chroma_format;
354 }
355 
356 /*****************************************************************************/
357 /*                                                                           */
358 /*  Function Name : codec_exit                                               */
359 /*                                                                           */
360 /*  Description   : handles unrecoverable errors                             */
361 /*  Inputs        : Error message                                            */
362 /*  Globals       : None                                                     */
363 /*  Processing    : Prints error message to console and exits.               */
364 /*  Outputs       : Error mesage to the console                              */
365 /*  Returns       : None                                                     */
366 /*                                                                           */
367 /*  Issues        :                                                          */
368 /*                                                                           */
369 /*  Revision History:                                                        */
370 /*                                                                           */
371 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
372 /*         07 06 2006   Sankar          Creation                             */
373 /*                                                                           */
374 /*****************************************************************************/
get_speed_preset(CHAR * value)375 IVE_SPEED_CONFIG get_speed_preset(CHAR *value)
376 {
377     IVE_SPEED_CONFIG e_enc_speed_preset;
378     if((strcmp(value, "CONFIG")) == 0)
379         e_enc_speed_preset = IVE_CONFIG;
380     else if((strcmp(value, "SLOWEST")) == 0)
381         e_enc_speed_preset = IVE_SLOWEST;
382     else if((strcmp(value, "NORMAL")) == 0)
383         e_enc_speed_preset = IVE_NORMAL;
384     else if((strcmp(value, "FAST")) == 0)
385           e_enc_speed_preset = IVE_FAST;
386     else if((strcmp(value, "HIGH_SPEED")) == 0)
387         e_enc_speed_preset = IVE_HIGH_SPEED;
388     else if((strcmp(value, "FASTEST")) == 0)
389         e_enc_speed_preset = IVE_FASTEST;
390     else
391     {
392         printf("\nInvalid speed preset, setting it to IVE_FASTEST\n");
393         e_enc_speed_preset = IVE_FASTEST;
394     }
395     return e_enc_speed_preset;
396 }
397 
398 /*****************************************************************************/
399 /*                                                                           */
400 /*  Function Name : print_usage                                              */
401 /*                                                                           */
402 /*  Description   : Prints argument format                                   */
403 /*                                                                           */
404 /*                                                                           */
405 /*  Inputs        :                                                          */
406 /*  Globals       :                                                          */
407 /*  Processing    : Prints argument format                                   */
408 /*                                                                           */
409 /*  Outputs       :                                                          */
410 /*  Returns       :                                                          */
411 /*                                                                           */
412 /*  Issues        :                                                          */
413 /*                                                                           */
414 /*  Revision History:                                                        */
415 /*                                                                           */
416 /*         DD MM YYYY   Author(s)       Changes                              */
417 /*         07 09 2012   100189          Initial Version                      */
418 /*                                                                           */
419 /*****************************************************************************/
420 
print_usage(void)421 void print_usage(void)
422 {
423     WORD32 i = 0;
424     WORD32 num_entries = sizeof(argument_mapping) / sizeof(argument_t);
425     printf("\nUsage:\n");
426     while(i < num_entries)
427     {
428         printf("%-32s\t %s", argument_mapping[i].argument_name,
429                argument_mapping[i].description);
430         i++;
431     }
432 }
433 
434 /*****************************************************************************/
435 /*                                                                           */
436 /*  Function Name : get_argument                                             */
437 /*                                                                           */
438 /*  Description   : Gets argument for a given string                         */
439 /*                                                                           */
440 /*                                                                           */
441 /*  Inputs        : name                                                     */
442 /*  Globals       :                                                          */
443 /*  Processing    : Searches the given string in the array and returns       */
444 /*                  appropriate argument ID                                  */
445 /*                                                                           */
446 /*  Outputs       : Argument ID                                              */
447 /*  Returns       : Argument ID                                              */
448 /*                                                                           */
449 /*  Issues        :                                                          */
450 /*                                                                           */
451 /*  Revision History:                                                        */
452 /*                                                                           */
453 /*         DD MM YYYY   Author(s)       Changes                              */
454 /*         07 09 2012   100189          Initial Version                      */
455 /*                                                                           */
456 /*****************************************************************************/
get_argument(CHAR * name)457 ARGUMENT_T get_argument(CHAR *name)
458 {
459     WORD32 i = 0;
460     WORD32 num_entries = sizeof(argument_mapping) / sizeof(argument_t);
461     while(i < num_entries)
462     {
463         if((0 == strcmp(argument_mapping[i].argument_name, name))       ||
464           ((0 == strcmp(argument_mapping[i].argument_shortname, name))  &&
465            (0 != strcmp(argument_mapping[i].argument_shortname, "--"))))
466         {
467             return argument_mapping[i].argument;
468         }
469         i++;
470     }
471     return INVALID;
472 }
473 
474 /*****************************************************************************/
475 /*                                                                           */
476 /*  Function Name : get_argument                                             */
477 /*                                                                           */
478 /*  Description   : Gets argument for a given string                         */
479 /*                                                                           */
480 /*                                                                           */
481 /*  Inputs        : name                                                     */
482 /*  Globals       :                                                          */
483 /*  Processing    : Searches the given string in the array and returns       */
484 /*                  appropriate argument ID                                  */
485 /*                                                                           */
486 /*  Outputs       : Argument ID                                              */
487 /*  Returns       : Argument ID                                              */
488 /*                                                                           */
489 /*  Issues        :                                                          */
490 /*                                                                           */
491 /*  Revision History:                                                        */
492 /*                                                                           */
493 /*         DD MM YYYY   Author(s)       Changes                              */
494 /*         07 09 2012   100189          Initial Version                      */
495 /*                                                                           */
496 /*****************************************************************************/
parse_argument(app_ctxt_t * ps_app_ctxt,CHAR * argument,CHAR * value)497 void parse_argument(app_ctxt_t *ps_app_ctxt, CHAR *argument, CHAR *value)
498 {
499     ARGUMENT_T arg;
500 
501     arg = get_argument(argument);
502     switch(arg)
503     {
504       case HELP:
505         print_usage();
506         exit(-1);
507         break;
508       case SLICE_MODE:
509         sscanf(value, "%d", &ps_app_ctxt->u4_slice_mode);
510         break;
511       case SLICE_PARAM:
512         sscanf(value, "%d", &ps_app_ctxt->u4_slice_param);
513         break;
514       case INPUT_FILE:
515         sscanf(value, "%s", ps_app_ctxt->ac_ip_fname);
516         break;
517 
518       case OUTPUT_FILE:
519         sscanf(value, "%s", ps_app_ctxt->ac_op_fname);
520         break;
521 
522       case RECON_FILE:
523         sscanf(value, "%s", ps_app_ctxt->ac_recon_fname);
524         break;
525 
526       case RECON_ENABLE:
527         sscanf(value, "%d", &ps_app_ctxt->u4_recon_enable);
528         break;
529 
530       case CHKSUM_FILE:
531           sscanf(value, "%s", ps_app_ctxt->ac_chksum_fname);
532           break;
533 
534       case CHKSUM_ENABLE:
535           sscanf(value, "%d", &ps_app_ctxt->u4_chksum_enable);
536           break;
537 
538       case MB_INFO_FILE:
539         sscanf(value, "%s", ps_app_ctxt->ac_mb_info_fname);
540         break;
541 
542       case MB_INFO_TYPE:
543         sscanf(value, "%d", &ps_app_ctxt->u4_mb_info_type);
544         break;
545 
546       case PIC_INFO_FILE:
547         sscanf(value, "%s", ps_app_ctxt->ac_pic_info_fname);
548         break;
549 
550       case PIC_INFO_TYPE:
551         sscanf(value, "%d", &ps_app_ctxt->u4_pic_info_type);
552         break;
553 
554       case INPUT_CHROMA_FORMAT:
555         ps_app_ctxt->e_inp_color_fmt = get_chroma_fmt(value);
556         break;
557 
558       case RECON_CHROMA_FORMAT:
559         ps_app_ctxt->e_recon_color_fmt = get_chroma_fmt(value);
560         break;
561 
562       case MAX_WD:
563         sscanf(value, "%d", &ps_app_ctxt->u4_max_wd);
564         break;
565 
566       case MAX_HT:
567         sscanf(value, "%d", &ps_app_ctxt->u4_max_ht);
568         break;
569 
570       case WD:
571         sscanf(value, "%d", &ps_app_ctxt->u4_wd);
572         break;
573 
574       case HT:
575         sscanf(value, "%d", &ps_app_ctxt->u4_ht);
576         break;
577 
578       case MAX_LEVEL:
579         sscanf(value, "%d", &ps_app_ctxt->u4_max_level);
580         break;
581 
582       case ENC_SPEED:
583         ps_app_ctxt->u4_enc_speed = get_speed_preset(value);
584         break;
585 
586       case ME_SPEED:
587         sscanf(value, "%d", &ps_app_ctxt->u4_me_speed);
588         break;
589 
590       case START_FRM:
591         sscanf(value, "%d", &ps_app_ctxt->u4_start_frm);
592         break;
593 
594       case NUM_FRMS:
595         sscanf(value, "%d", &ps_app_ctxt->u4_max_num_frms);
596         break;
597 
598       case MAX_FRAMERATE:
599           sscanf(value, "%d", &ps_app_ctxt->u4_max_frame_rate);
600           if(ps_app_ctxt->u4_max_frame_rate <= 0)
601               ps_app_ctxt->u4_max_frame_rate = DEFAULT_MAX_FRAMERATE;
602           break;
603 
604       case SRC_FRAMERATE:
605         sscanf(value, "%d", &ps_app_ctxt->u4_src_frame_rate);
606         if(ps_app_ctxt->u4_src_frame_rate <= 0)
607             ps_app_ctxt->u4_src_frame_rate = DEFAULT_SRC_FRAME_RATE;
608         break;
609 
610       case TGT_FRAMERATE:
611         sscanf(value, "%d", &ps_app_ctxt->u4_tgt_frame_rate);
612         if(ps_app_ctxt->u4_tgt_frame_rate <= 0)
613             ps_app_ctxt->u4_tgt_frame_rate = DEFAULT_TGT_FRAME_RATE;
614         break;
615 
616       case RC:
617         sscanf(value, "%d", &ps_app_ctxt->u4_rc);
618         break;
619 
620       case MAX_BITRATE:
621           sscanf(value, "%d", &ps_app_ctxt->u4_max_bitrate);
622           break;
623 
624       case BITRATE:
625         sscanf(value, "%d", &ps_app_ctxt->u4_bitrate);
626         break;
627 
628       case I_QP:
629           sscanf(value, "%d", &ps_app_ctxt->u4_i_qp);
630           break;
631 
632       case I_QP_MAX:
633           sscanf(value, "%d", &ps_app_ctxt->u4_i_qp_max);
634           break;
635 
636       case I_QP_MIN:
637           sscanf(value, "%d", &ps_app_ctxt->u4_i_qp_min);
638           break;
639 
640       case P_QP:
641           sscanf(value, "%d", &ps_app_ctxt->u4_p_qp);
642           break;
643 
644       case P_QP_MAX:
645           sscanf(value, "%d", &ps_app_ctxt->u4_p_qp_max);
646           break;
647 
648       case P_QP_MIN:
649           sscanf(value, "%d", &ps_app_ctxt->u4_p_qp_min);
650           break;
651 
652       case B_QP:
653           sscanf(value, "%d", &ps_app_ctxt->u4_b_qp);
654           break;
655 
656       case B_QP_MAX:
657           sscanf(value, "%d", &ps_app_ctxt->u4_b_qp_max);
658           break;
659 
660       case B_QP_MIN:
661           sscanf(value, "%d", &ps_app_ctxt->u4_b_qp_min);
662           break;
663 
664       case ENTROPY:
665           sscanf(value, "%d", &ps_app_ctxt->u4_entropy_coding_mode);
666           break;
667 
668       case AIR:
669           sscanf(value, "%d", &ps_app_ctxt->u4_air);
670           break;
671 
672       case ARCH:
673           if((strcmp(value, "ARM_NONEON")) == 0)
674               ps_app_ctxt->e_arch = ARCH_ARM_NONEON;
675           else if((strcmp(value, "ARM_A9Q")) == 0)
676               ps_app_ctxt->e_arch = ARCH_ARM_A9Q;
677           else if((strcmp(value, "ARM_A7")) == 0)
678               ps_app_ctxt->e_arch = ARCH_ARM_A7;
679           else if((strcmp(value, "ARM_A5")) == 0)
680               ps_app_ctxt->e_arch = ARCH_ARM_A5;
681           else if((strcmp(value, "ARM_NEONINTR")) == 0)
682               ps_app_ctxt->e_arch = ARCH_ARM_NEONINTR;
683           else if((strcmp(value, "X86_GENERIC")) == 0)
684               ps_app_ctxt->e_arch = ARCH_X86_GENERIC;
685           else if((strcmp(value, "X86_SSSE3")) == 0)
686               ps_app_ctxt->e_arch = ARCH_X86_SSSE3;
687           else if((strcmp(value, "X86_SSE42")) == 0)
688               ps_app_ctxt->e_arch = ARCH_X86_SSE42;
689           else if((strcmp(value, "ARM_A53")) == 0)
690               ps_app_ctxt->e_arch = ARCH_ARM_A53;
691           else if((strcmp(value, "ARM_A57")) == 0)
692               ps_app_ctxt->e_arch = ARCH_ARM_A57;
693           else if((strcmp(value, "ARM_V8_NEON")) == 0)
694               ps_app_ctxt->e_arch = ARCH_ARM_V8_NEON;
695           else
696           {
697               printf("\nInvalid Arch. Setting it to ARM_A9Q\n");
698               ps_app_ctxt->e_arch = ARCH_ARM_A9Q;
699           }
700 
701           break;
702       case SOC:
703           if((strcmp(value, "GENERIC")) == 0)
704               ps_app_ctxt->e_soc = SOC_GENERIC;
705           else if((strcmp(value, "HISI_37X")) == 0)
706               ps_app_ctxt->e_soc = SOC_HISI_37X;
707           else
708           {
709               ps_app_ctxt->e_soc = SOC_GENERIC;
710           }
711           break;
712 
713       case NUMCORES:
714         sscanf(value, "%d", &ps_app_ctxt->u4_num_cores);
715         break;
716 
717       case LOOPBACK:
718         sscanf(value, "%d", &ps_app_ctxt->u4_loopback);
719         break;
720 
721       case PRE_ENC_ME:
722         sscanf(value, "%d", &ps_app_ctxt->u4_pre_enc_me);
723         break;
724 
725       case PRE_ENC_IPE:
726         sscanf(value, "%d", &ps_app_ctxt->u4_pre_enc_ipe);
727         break;
728 
729       case HPEL:
730         sscanf(value, "%d", &ps_app_ctxt->u4_hpel);
731         break;
732 
733       case QPEL:
734         sscanf(value, "%d", &ps_app_ctxt->u4_qpel);
735         break;
736 
737       case SRCH_RNG_X:
738         sscanf(value, "%d", &ps_app_ctxt->u4_srch_rng_x);
739         break;
740 
741       case SRCH_RNG_Y:
742         sscanf(value, "%d", &ps_app_ctxt->u4_srch_rng_y);
743         break;
744 
745       case I_INTERVAL:
746         sscanf(value, "%d", &ps_app_ctxt->u4_i_interval);
747         break;
748 
749       case IDR_INTERVAL:
750         sscanf(value, "%d", &ps_app_ctxt->u4_idr_interval);
751         break;
752 
753       case NUM_B_FRMS:
754         sscanf(value, "%d", &ps_app_ctxt->u4_num_bframes);
755         break;
756 
757       case DISABLE_DEBLOCK_LEVEL:
758         sscanf(value, "%d", &ps_app_ctxt->u4_disable_deblk_level);
759         break;
760 
761       case VBV_DELAY:
762          sscanf(value, "%d", &ps_app_ctxt->u4_vbv_buffer_delay);
763          break;
764 
765       case VBV_SIZE:
766          sscanf(value, "%d", &ps_app_ctxt->u4_vbv_buf_size);
767          break;
768 
769       case FAST_SAD:
770           sscanf(value, "%d", &ps_app_ctxt->u4_enable_fast_sad);
771           break;
772 
773       case ALT_REF:
774           sscanf(value, "%d", &ps_app_ctxt->u4_enable_alt_ref);
775           break;
776 
777       case AIR_REFRESH_PERIOD:
778           sscanf(value, "%d", &ps_app_ctxt->u4_air_refresh_period);
779                    break;
780 
781       case PROFILE:
782             if((strcmp(value, "BASE")) == 0)
783                 ps_app_ctxt->e_profile = IV_PROFILE_BASE;
784             else if((strcmp(value, "MAIN")) == 0)
785               ps_app_ctxt->e_profile = IV_PROFILE_MAIN;
786             else if((strcmp(value, "HIGH")) == 0)
787               ps_app_ctxt->e_profile = IV_PROFILE_HIGH;
788             else
789             {
790                 printf("\nInvalid profile. Setting it to BASE\n");
791                 ps_app_ctxt->e_profile = IV_PROFILE_BASE;
792             }
793             break;
794 
795       case PSNR:
796           sscanf(value, "%d", &ps_app_ctxt->u4_psnr_enable);
797           break;
798 
799       case INTRA_4x4_ENABLE:
800           sscanf(value, "%d", &ps_app_ctxt->u4_enable_intra_4x4);
801           break;
802 
803 
804       case INVALID:
805         default:
806             printf("Ignoring argument :  %s\n", argument);
807             break;
808     }
809 }
810 
811 /*****************************************************************************/
812 /*                                                                           */
813 /*  Function Name : read_cfg_file                                            */
814 /*                                                                           */
815 /*  Description   : Reads arguments from a configuration file                */
816 /*                                                                           */
817 /*                                                                           */
818 /*  Inputs        : ps_app_ctxt  : Application context                        */
819 /*                  fp_cfg_file : Configuration file handle                  */
820 /*  Globals       :                                                          */
821 /*  Processing    : Parses the arguments and fills in the application context*/
822 /*                                                                           */
823 /*  Outputs       : Arguments parsed                                         */
824 /*  Returns       : None                                                     */
825 /*                                                                           */
826 /*  Issues        :                                                          */
827 /*                                                                           */
828 /*  Revision History:                                                        */
829 /*                                                                           */
830 /*         DD MM YYYY   Author(s)       Changes                              */
831 /*         07 09 2012   100189          Initial Version                      */
832 /*                                                                           */
833 /*****************************************************************************/
read_cfg_file(app_ctxt_t * ps_app_ctxt,FILE * fp_cfg)834 void read_cfg_file(app_ctxt_t *ps_app_ctxt, FILE *fp_cfg)
835 {
836     CHAR line[STRLENGTH];
837     CHAR description[STRLENGTH];
838     CHAR value[STRLENGTH];
839     CHAR argument[STRLENGTH];
840 
841     while(0 == (feof(fp_cfg)))
842     {
843         line[0] = '\0';
844         fgets(line, STRLENGTH, fp_cfg);
845         argument[0] = '\0';
846         /* Reading Input File Name */
847         sscanf(line, "%s %s %s", argument, value, description);
848         if(argument[0] == '\0')
849             continue;
850 
851         parse_argument(ps_app_ctxt, argument, value);
852     }
853 }
854 
invalid_argument_exit(CHAR * pc_err_message)855 void invalid_argument_exit(CHAR *pc_err_message)
856 {
857     print_usage();
858     codec_exit(pc_err_message);
859 }
860 
validate_params(app_ctxt_t * ps_app_ctxt)861 void validate_params(app_ctxt_t *ps_app_ctxt)
862 {
863     CHAR ac_error[STRLENGTH];
864 
865     if(ps_app_ctxt->ac_ip_fname[0] == '\0')
866     {
867         invalid_argument_exit("Specify input file");
868     }
869     if(ps_app_ctxt->ac_op_fname[0] == '\0')
870     {
871         invalid_argument_exit("Specify output file");
872     }
873     if((1 == ps_app_ctxt->u4_recon_enable) && (ps_app_ctxt->ac_recon_fname[0] == '\0'))
874     {
875         invalid_argument_exit("Specify recon file");
876     }
877     if((1 == ps_app_ctxt->u4_chksum_enable) && (ps_app_ctxt->ac_chksum_fname[0] == '\0'))
878     {
879         invalid_argument_exit("Specify checksum file");
880     }
881     if(0 >= (WORD32)ps_app_ctxt->u4_wd)
882     {
883         sprintf(ac_error, "Invalid width: %d", ps_app_ctxt->u4_wd);
884         invalid_argument_exit(ac_error);
885     }
886     if(0 >= (WORD32)ps_app_ctxt->u4_ht)
887     {
888         sprintf(ac_error, "Invalid height: %d", ps_app_ctxt->u4_ht);
889         invalid_argument_exit(ac_error);
890     }
891 
892     if(0 == (WORD32)ps_app_ctxt->u4_max_num_frms)
893     {
894         sprintf(ac_error, "Invalid number of frames to be encoded: %d", ps_app_ctxt->u4_max_num_frms);
895         invalid_argument_exit(ac_error);
896     }
897     if ((0 != (WORD32)ps_app_ctxt->u4_entropy_coding_mode)
898                     && (1 != (WORD32)ps_app_ctxt->u4_entropy_coding_mode))
899     {
900         sprintf(ac_error, "Invalid entropy codeing mode: %d",
901                 ps_app_ctxt->u4_entropy_coding_mode);
902         invalid_argument_exit(ac_error);
903     }
904     return;
905 }
906 
init_default_params(app_ctxt_t * ps_app_ctxt)907 void init_default_params(app_ctxt_t *ps_app_ctxt)
908 {
909 
910     ps_app_ctxt->ps_enc                  = NULL;
911     ps_app_ctxt->ps_mem_rec              = NULL;
912     ps_app_ctxt->u4_num_mem_rec          = DEFAULT_MEM_REC_CNT;
913     ps_app_ctxt->u4_recon_enable         = DEFAULT_RECON_ENABLE;
914     ps_app_ctxt->u4_chksum_enable        = DEFAULT_CHKSUM_ENABLE;
915     ps_app_ctxt->u4_mb_info_type         = 0;
916     ps_app_ctxt->u4_pic_info_type        = 0;
917     ps_app_ctxt->u4_mb_info_size         = 0;
918     ps_app_ctxt->u4_pic_info_size        = 0;
919     ps_app_ctxt->u4_start_frm            = DEFAULT_START_FRM;
920     ps_app_ctxt->u4_max_num_frms         = DEFAULT_NUM_FRMS;
921     ps_app_ctxt->avg_time                = 0;
922     ps_app_ctxt->u4_total_bytes          = 0;
923     ps_app_ctxt->u4_pics_cnt             = 0;
924     ps_app_ctxt->e_inp_color_fmt         = DEFAULT_INP_COLOR_FMT;
925     ps_app_ctxt->e_recon_color_fmt       = DEFAULT_RECON_COLOR_FMT;
926     ps_app_ctxt->e_arch                  = ARCH_ARM_A9Q;
927     ps_app_ctxt->e_soc                   = SOC_GENERIC;
928     ps_app_ctxt->header_generated        = 0;
929     ps_app_ctxt->pv_codec_obj            = NULL;
930     ps_app_ctxt->u4_num_cores            = DEFAULT_NUM_CORES;
931     ps_app_ctxt->u4_pre_enc_me           = 0;
932     ps_app_ctxt->u4_pre_enc_ipe          = 0;
933     ps_app_ctxt->ac_ip_fname[0]          = '\0';
934     ps_app_ctxt->ac_op_fname[0]          = '\0';
935     ps_app_ctxt->ac_recon_fname[0]       = '\0';
936     ps_app_ctxt->ac_chksum_fname[0]      = '\0';
937     ps_app_ctxt->ac_mb_info_fname[0]     = '\0';
938     ps_app_ctxt->fp_ip                   = NULL;
939     ps_app_ctxt->fp_op                   = NULL;
940     ps_app_ctxt->fp_recon                = NULL;
941     ps_app_ctxt->fp_chksum               = NULL;
942     ps_app_ctxt->fp_psnr_ip              = NULL;
943     ps_app_ctxt->fp_mb_info              = NULL;
944     ps_app_ctxt->fp_pic_info             = NULL;
945     ps_app_ctxt->u4_loopback             = DEFAULT_LOOPBACK;
946     ps_app_ctxt->u4_max_frame_rate       = DEFAULT_MAX_FRAMERATE;
947     ps_app_ctxt->u4_src_frame_rate       = DEFAULT_SRC_FRAME_RATE;
948     ps_app_ctxt->u4_tgt_frame_rate       = DEFAULT_TGT_FRAME_RATE;
949     ps_app_ctxt->u4_max_wd               = DEFAULT_MAX_WD;
950     ps_app_ctxt->u4_max_ht               = DEFAULT_MAX_HT;
951     ps_app_ctxt->u4_max_level            = DEFAULT_MAX_LEVEL;
952     ps_app_ctxt->u4_strd                 = DEFAULT_STRIDE;
953     ps_app_ctxt->u4_wd                   = DEFAULT_WD;
954     ps_app_ctxt->u4_ht                   = DEFAULT_HT;
955     ps_app_ctxt->u4_psnr_enable          = DEFAULT_PSNR_ENABLE;
956     ps_app_ctxt->u4_enc_speed            = IVE_FASTEST;
957     ps_app_ctxt->u4_me_speed             = DEFAULT_ME_SPEED;
958     ps_app_ctxt->u4_enable_fast_sad      = DEFAULT_ENABLE_FAST_SAD;
959     ps_app_ctxt->u4_enable_alt_ref       = DEFAULT_ENABLE_ALT_REF;
960     ps_app_ctxt->u4_rc                   = DEFAULT_RC;
961     ps_app_ctxt->u4_max_bitrate          = DEFAULT_MAX_BITRATE;
962     ps_app_ctxt->u4_num_bframes          = DEFAULT_NUM_BFRAMES;
963     ps_app_ctxt->u4_bitrate              = DEFAULT_BITRATE;
964     ps_app_ctxt->u4_i_qp                 = DEFAULT_I_QP;
965     ps_app_ctxt->u4_p_qp                 = DEFAULT_P_QP;
966     ps_app_ctxt->u4_b_qp                 = DEFAULT_B_QP;
967     ps_app_ctxt->u4_i_qp_min             = DEFAULT_QP_MIN;
968     ps_app_ctxt->u4_i_qp_max             = DEFAULT_QP_MAX;
969     ps_app_ctxt->u4_p_qp_min             = DEFAULT_QP_MIN;
970     ps_app_ctxt->u4_p_qp_max             = DEFAULT_QP_MAX;
971     ps_app_ctxt->u4_b_qp_min             = DEFAULT_QP_MIN;
972     ps_app_ctxt->u4_b_qp_max             = DEFAULT_QP_MAX;
973     ps_app_ctxt->u4_air                  = DEFAULT_AIR;
974     ps_app_ctxt->u4_air_refresh_period   = DEFAULT_AIR_REFRESH_PERIOD;
975     ps_app_ctxt->u4_srch_rng_x           = DEFAULT_SRCH_RNG_X;
976     ps_app_ctxt->u4_srch_rng_y           = DEFAULT_SRCH_RNG_Y;
977     ps_app_ctxt->u4_i_interval           = DEFAULT_I_INTERVAL;
978     ps_app_ctxt->u4_idr_interval         = DEFAULT_IDR_INTERVAL;
979     ps_app_ctxt->u4_disable_deblk_level  = DEFAULT_DISABLE_DEBLK_LEVEL;
980     ps_app_ctxt->u4_hpel                 = DEFAULT_HPEL;
981     ps_app_ctxt->u4_qpel                 = DEFAULT_QPEL;
982     ps_app_ctxt->u4_enable_intra_4x4     = DEFAULT_I4;
983     ps_app_ctxt->e_profile               = DEFAULT_EPROFILE;
984     ps_app_ctxt->u4_slice_mode           = DEFAULT_SLICE_MODE;
985     ps_app_ctxt->u4_slice_param          = DEFAULT_SLICE_PARAM;
986     ps_app_ctxt->pv_input_thread_handle  = NULL;
987     ps_app_ctxt->pv_output_thread_handle = NULL;
988     ps_app_ctxt->pv_recon_thread_handle  = NULL;
989     ps_app_ctxt->u4_vbv_buf_size         = 0;
990     ps_app_ctxt->u4_vbv_buffer_delay     = 1000;
991     ps_app_ctxt->adbl_psnr[0]            = 0.0;
992     ps_app_ctxt->adbl_psnr[1]            = 0.0;
993     ps_app_ctxt->adbl_psnr[2]            = 0.0;
994     ps_app_ctxt->u4_psnr_cnt             = 0;
995     ps_app_ctxt->pu1_psnr_buf            = NULL;
996     ps_app_ctxt->u4_psnr_buf_size        = 0;
997     ps_app_ctxt->u4_entropy_coding_mode  = DEFAULT_ENTROPY_CODING_MODE;
998 
999     return;
1000 }
1001 
set_dimensions(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1002 void set_dimensions(app_ctxt_t *ps_app_ctxt,
1003                     UWORD32 u4_timestamp_low,
1004                     UWORD32 u4_timestamp_high)
1005 {
1006     ih264e_ctl_set_dimensions_ip_t s_frame_dimensions_ip;
1007     ih264e_ctl_set_dimensions_op_t s_frame_dimensions_op;
1008     IV_STATUS_T status;
1009 
1010     s_frame_dimensions_ip.s_ive_ip.e_cmd = IVE_CMD_VIDEO_CTL;
1011     s_frame_dimensions_ip.s_ive_ip.e_sub_cmd = IVE_CMD_CTL_SET_DIMENSIONS;
1012 
1013     s_frame_dimensions_ip.s_ive_ip.u4_ht = ps_app_ctxt->u4_ht;
1014     s_frame_dimensions_ip.s_ive_ip.u4_wd = ps_app_ctxt->u4_wd;
1015 
1016     s_frame_dimensions_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1017     s_frame_dimensions_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1018 
1019     s_frame_dimensions_ip.s_ive_ip.u4_size =
1020                     sizeof(ih264e_ctl_set_dimensions_ip_t);
1021     s_frame_dimensions_op.s_ive_op.u4_size =
1022                     sizeof(ih264e_ctl_set_dimensions_op_t);
1023 
1024     status = ih264e_api_function(ps_app_ctxt->ps_enc,
1025                                  &s_frame_dimensions_ip,
1026                                  &s_frame_dimensions_op);
1027     if(status != IV_SUCCESS)
1028     {
1029         CHAR ac_error[STRLENGTH];
1030         sprintf(ac_error, "Unable to set frame dimensions = 0x%x\n",
1031                 s_frame_dimensions_op.s_ive_op.u4_error_code);
1032         codec_exit(ac_error);
1033     }
1034     return;
1035 }
1036 
set_frame_rate(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1037 void set_frame_rate(app_ctxt_t *ps_app_ctxt,
1038                     UWORD32 u4_timestamp_low,
1039                     UWORD32 u4_timestamp_high)
1040 {
1041     ih264e_ctl_set_frame_rate_ip_t s_frame_rate_ip;
1042     ih264e_ctl_set_frame_rate_op_t s_frame_rate_op;
1043     IV_STATUS_T status;
1044 
1045     s_frame_rate_ip.s_ive_ip.e_cmd  =   IVE_CMD_VIDEO_CTL;
1046     s_frame_rate_ip.s_ive_ip.e_sub_cmd  =   IVE_CMD_CTL_SET_FRAMERATE;
1047 
1048     s_frame_rate_ip.s_ive_ip.u4_src_frame_rate  =
1049                     ps_app_ctxt->u4_src_frame_rate;
1050     s_frame_rate_ip.s_ive_ip.u4_tgt_frame_rate  =
1051                     ps_app_ctxt->u4_tgt_frame_rate;
1052 
1053     s_frame_rate_ip.s_ive_ip.u4_timestamp_high  =   u4_timestamp_high;
1054     s_frame_rate_ip.s_ive_ip.u4_timestamp_low   =   u4_timestamp_low;
1055 
1056     s_frame_rate_ip.s_ive_ip.u4_size    =   sizeof(ih264e_ctl_set_frame_rate_ip_t);
1057     s_frame_rate_op.s_ive_op.u4_size    =   sizeof(ih264e_ctl_set_frame_rate_op_t);
1058 
1059     status = ih264e_api_function(ps_app_ctxt->ps_enc,&s_frame_rate_ip,&s_frame_rate_op);
1060     if(status != IV_SUCCESS)
1061        {
1062            CHAR ac_error[STRLENGTH];
1063            sprintf(ac_error, "Unable to set frame rate = 0x%x\n",
1064                    s_frame_rate_op.s_ive_op.u4_error_code);
1065            codec_exit(ac_error);
1066        }
1067        return;
1068 }
1069 
1070 
set_ipe_params(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1071 void set_ipe_params(app_ctxt_t *ps_app_ctxt,
1072                     UWORD32 u4_timestamp_low,
1073                     UWORD32 u4_timestamp_high)
1074 {
1075     ih264e_ctl_set_ipe_params_ip_t s_ipe_params_ip;
1076     ih264e_ctl_set_ipe_params_op_t s_ipe_params_op;
1077     IV_STATUS_T status;
1078 
1079     s_ipe_params_ip.s_ive_ip.e_cmd  =   IVE_CMD_VIDEO_CTL;
1080     s_ipe_params_ip.s_ive_ip.e_sub_cmd  =   IVE_CMD_CTL_SET_IPE_PARAMS;
1081 
1082     s_ipe_params_ip.s_ive_ip.u4_enable_intra_4x4  = ps_app_ctxt->u4_enable_intra_4x4;
1083     s_ipe_params_ip.s_ive_ip.u4_enc_speed_preset  = ps_app_ctxt->u4_enc_speed;
1084 
1085     s_ipe_params_ip.s_ive_ip.u4_timestamp_high  =   u4_timestamp_high;
1086     s_ipe_params_ip.s_ive_ip.u4_timestamp_low   =   u4_timestamp_low;
1087 
1088     s_ipe_params_ip.s_ive_ip.u4_size    =   sizeof(ih264e_ctl_set_ipe_params_ip_t);
1089     s_ipe_params_op.s_ive_op.u4_size    =   sizeof(ih264e_ctl_set_ipe_params_op_t);
1090 
1091     status = ih264e_api_function(ps_app_ctxt->ps_enc,&s_ipe_params_ip,&s_ipe_params_op);
1092     if(status != IV_SUCCESS)
1093     {
1094         CHAR ac_error[STRLENGTH];
1095         sprintf(ac_error, "Unable to set ipe params = 0x%x\n",
1096                 s_ipe_params_op.s_ive_op.u4_error_code);
1097         codec_exit(ac_error);
1098     }
1099     return;
1100 }
1101 
set_bit_rate(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1102 void set_bit_rate(app_ctxt_t *ps_app_ctxt,
1103                   UWORD32 u4_timestamp_low, UWORD32 u4_timestamp_high)
1104 {
1105     ih264e_ctl_set_bitrate_ip_t s_bitrate_ip;
1106     ih264e_ctl_set_bitrate_op_t s_bitrate_op;
1107     IV_STATUS_T status;
1108 
1109     s_bitrate_ip.s_ive_ip.e_cmd  =   IVE_CMD_VIDEO_CTL;
1110     s_bitrate_ip.s_ive_ip.e_sub_cmd  =   IVE_CMD_CTL_SET_BITRATE;
1111 
1112     s_bitrate_ip.s_ive_ip.u4_target_bitrate  =   ps_app_ctxt->u4_bitrate;
1113 
1114     s_bitrate_ip.s_ive_ip.u4_timestamp_high  =   u4_timestamp_high;
1115     s_bitrate_ip.s_ive_ip.u4_timestamp_low   =   u4_timestamp_low;
1116 
1117     s_bitrate_ip.s_ive_ip.u4_size    =   sizeof(ih264e_ctl_set_bitrate_ip_t);
1118     s_bitrate_op.s_ive_op.u4_size    =   sizeof(ih264e_ctl_set_bitrate_op_t);
1119 
1120     status = ih264e_api_function(ps_app_ctxt->ps_enc,&s_bitrate_ip,&s_bitrate_op);
1121     if(status != IV_SUCCESS)
1122        {
1123            CHAR ac_error[STRLENGTH];
1124            sprintf(ac_error, "Unable to set bit rate = 0x%x\n",
1125                    s_bitrate_op.s_ive_op.u4_error_code);
1126            codec_exit(ac_error);
1127        }
1128        return;
1129 }
1130 
1131 
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)1132 void set_frame_type(app_ctxt_t *ps_app_ctxt,
1133                     UWORD32 u4_timestamp_low,
1134                     UWORD32 u4_timestamp_high,
1135                     IV_PICTURE_CODING_TYPE_T  e_frame_type)
1136 {
1137     ih264e_ctl_set_frame_type_ip_t s_frame_type_ip;
1138     ih264e_ctl_set_frame_type_op_t s_frame_type_op;
1139     IV_STATUS_T status;
1140 
1141     s_frame_type_ip.s_ive_ip.e_cmd  =   IVE_CMD_VIDEO_CTL;
1142     s_frame_type_ip.s_ive_ip.e_sub_cmd  =   IVE_CMD_CTL_SET_FRAMETYPE;
1143 
1144     s_frame_type_ip.s_ive_ip.e_frame_type  =   e_frame_type;
1145 
1146     s_frame_type_ip.s_ive_ip.u4_timestamp_high  =   u4_timestamp_high;
1147     s_frame_type_ip.s_ive_ip.u4_timestamp_low   =   u4_timestamp_low;
1148 
1149     s_frame_type_ip.s_ive_ip.u4_size    =   sizeof(ih264e_ctl_set_frame_type_ip_t);
1150     s_frame_type_op.s_ive_op.u4_size    =   sizeof(ih264e_ctl_set_frame_type_op_t);
1151 
1152     status = ih264e_api_function(ps_app_ctxt->ps_enc,&s_frame_type_ip,&s_frame_type_op);
1153     if(status != IV_SUCCESS)
1154     {
1155         CHAR ac_error[STRLENGTH];
1156         sprintf(ac_error, "Unable to set frame type = 0x%x\n",
1157                 s_frame_type_op.s_ive_op.u4_error_code);
1158         codec_exit(ac_error);
1159     }
1160     return;
1161 }
1162 
set_qp(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1163 void set_qp(app_ctxt_t *ps_app_ctxt,
1164             UWORD32 u4_timestamp_low, UWORD32 u4_timestamp_high)
1165 {
1166     ih264e_ctl_set_qp_ip_t s_qp_ip;
1167     ih264e_ctl_set_qp_op_t s_qp_op;
1168     IV_STATUS_T status;
1169 
1170     s_qp_ip.s_ive_ip.e_cmd  =   IVE_CMD_VIDEO_CTL;
1171     s_qp_ip.s_ive_ip.e_sub_cmd  =   IVE_CMD_CTL_SET_QP;
1172 
1173     s_qp_ip.s_ive_ip.u4_i_qp = ps_app_ctxt->u4_i_qp;
1174     s_qp_ip.s_ive_ip.u4_i_qp_max = ps_app_ctxt->u4_i_qp_max;
1175     s_qp_ip.s_ive_ip.u4_i_qp_min = ps_app_ctxt->u4_i_qp_min;
1176 
1177     s_qp_ip.s_ive_ip.u4_p_qp = ps_app_ctxt->u4_p_qp;
1178     s_qp_ip.s_ive_ip.u4_p_qp_max = ps_app_ctxt->u4_p_qp_max;
1179     s_qp_ip.s_ive_ip.u4_p_qp_min = ps_app_ctxt->u4_p_qp_min;
1180 
1181     s_qp_ip.s_ive_ip.u4_b_qp = ps_app_ctxt->u4_b_qp;
1182     s_qp_ip.s_ive_ip.u4_b_qp_max = ps_app_ctxt->u4_b_qp_max;
1183     s_qp_ip.s_ive_ip.u4_b_qp_min = ps_app_ctxt->u4_b_qp_min;
1184 
1185     s_qp_ip.s_ive_ip.u4_timestamp_high  =   u4_timestamp_high;
1186     s_qp_ip.s_ive_ip.u4_timestamp_low   =   u4_timestamp_low;
1187 
1188     s_qp_ip.s_ive_ip.u4_size    =   sizeof(ih264e_ctl_set_qp_ip_t);
1189     s_qp_op.s_ive_op.u4_size    =   sizeof(ih264e_ctl_set_qp_op_t);
1190 
1191     status = ih264e_api_function(ps_app_ctxt->ps_enc,&s_qp_ip,&s_qp_op);
1192     if(status != IV_SUCCESS)
1193        {
1194            CHAR ac_error[STRLENGTH];
1195            sprintf(ac_error, "Unable to set qp 0x%x\n",
1196                    s_qp_op.s_ive_op.u4_error_code);
1197            codec_exit(ac_error);
1198        }
1199        return;
1200 }
1201 
set_enc_mode(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high,IVE_ENC_MODE_T e_enc_mode)1202 void set_enc_mode(app_ctxt_t *ps_app_ctxt,
1203                   UWORD32 u4_timestamp_low, UWORD32 u4_timestamp_high,
1204                   IVE_ENC_MODE_T e_enc_mode)
1205 {
1206     IV_STATUS_T status;
1207 
1208     ih264e_ctl_set_enc_mode_ip_t s_enc_mode_ip;
1209     ih264e_ctl_set_enc_mode_op_t s_enc_mode_op;
1210 
1211     s_enc_mode_ip.s_ive_ip.e_cmd = IVE_CMD_VIDEO_CTL;
1212     s_enc_mode_ip.s_ive_ip.e_sub_cmd = IVE_CMD_CTL_SET_ENC_MODE;
1213 
1214     s_enc_mode_ip.s_ive_ip.e_enc_mode = e_enc_mode;
1215 
1216     s_enc_mode_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1217     s_enc_mode_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1218 
1219     s_enc_mode_ip.s_ive_ip.u4_size = sizeof(ih264e_ctl_set_enc_mode_ip_t);
1220     s_enc_mode_op.s_ive_op.u4_size = sizeof(ih264e_ctl_set_enc_mode_op_t);
1221 
1222     status = ih264e_api_function(ps_app_ctxt->ps_enc, &s_enc_mode_ip,
1223                                       &s_enc_mode_op);
1224     if(status != IV_SUCCESS)
1225     {
1226         CHAR ac_error[STRLENGTH];
1227         sprintf(ac_error, "Unable to set in header encode mode = 0x%x\n",
1228                 s_enc_mode_op.s_ive_op.u4_error_code);
1229         codec_exit(ac_error);
1230     }
1231     return;
1232 }
1233 
1234 
set_vbv_params(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1235 void set_vbv_params(app_ctxt_t *ps_app_ctxt,
1236                     UWORD32 u4_timestamp_low,
1237                     UWORD32 u4_timestamp_high)
1238 {
1239     ih264e_ctl_set_vbv_params_ip_t s_vbv_ip;
1240     ih264e_ctl_set_vbv_params_op_t s_vbv_op;
1241     IV_STATUS_T status;
1242 
1243     s_vbv_ip.s_ive_ip.e_cmd  =   IVE_CMD_VIDEO_CTL;
1244     s_vbv_ip.s_ive_ip.e_sub_cmd  =   IVE_CMD_CTL_SET_VBV_PARAMS;
1245 
1246     s_vbv_ip.s_ive_ip.u4_vbv_buf_size = ps_app_ctxt->u4_vbv_buf_size;
1247     s_vbv_ip.s_ive_ip.u4_vbv_buffer_delay  =
1248                     ps_app_ctxt->u4_vbv_buffer_delay;
1249 
1250     s_vbv_ip.s_ive_ip.u4_timestamp_high  =   u4_timestamp_high;
1251     s_vbv_ip.s_ive_ip.u4_timestamp_low   =   u4_timestamp_low;
1252 
1253     s_vbv_ip.s_ive_ip.u4_size    =   sizeof(ih264e_ctl_set_vbv_params_ip_t);
1254     s_vbv_op.s_ive_op.u4_size    =   sizeof(ih264e_ctl_set_vbv_params_op_t);
1255 
1256     status = ih264e_api_function(ps_app_ctxt->ps_enc,&s_vbv_ip,&s_vbv_op);
1257     if(status != IV_SUCCESS)
1258     {
1259         CHAR ac_error[STRLENGTH];
1260         sprintf(ac_error, "Unable to set VBC params = 0x%x\n",
1261                 s_vbv_op.s_ive_op.u4_error_code);
1262         codec_exit(ac_error);
1263     }
1264     return;
1265 }
1266 
set_air_params(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1267 void set_air_params(app_ctxt_t *ps_app_ctxt,
1268                     UWORD32 u4_timestamp_low,
1269                     UWORD32 u4_timestamp_high)
1270 {
1271     ih264e_ctl_set_air_params_ip_t s_air_ip;
1272     ih264e_ctl_set_air_params_op_t s_air_op;
1273     IV_STATUS_T status;
1274 
1275     s_air_ip.s_ive_ip.e_cmd  =   IVE_CMD_VIDEO_CTL;
1276     s_air_ip.s_ive_ip.e_sub_cmd  =   IVE_CMD_CTL_SET_AIR_PARAMS;
1277 
1278     s_air_ip.s_ive_ip.e_air_mode = ps_app_ctxt->u4_air;
1279     s_air_ip.s_ive_ip.u4_air_refresh_period = ps_app_ctxt->u4_air_refresh_period;
1280 
1281     s_air_ip.s_ive_ip.u4_timestamp_high  =   u4_timestamp_high;
1282     s_air_ip.s_ive_ip.u4_timestamp_low   =   u4_timestamp_low;
1283 
1284     s_air_ip.s_ive_ip.u4_size    =   sizeof(ih264e_ctl_set_air_params_ip_t);
1285     s_air_op.s_ive_op.u4_size    =   sizeof(ih264e_ctl_set_air_params_op_t);
1286 
1287     status = ih264e_api_function(ps_app_ctxt->ps_enc,&s_air_ip,&s_air_op);
1288     if(status != IV_SUCCESS)
1289        {
1290            CHAR ac_error[STRLENGTH];
1291            sprintf(ac_error, "Unable to set air params = 0x%x\n",
1292                    s_air_op.s_ive_op.u4_error_code);
1293            codec_exit(ac_error);
1294        }
1295        return;
1296 }
1297 
set_me_params(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1298 void set_me_params(app_ctxt_t *ps_app_ctxt,
1299                     UWORD32 u4_timestamp_low,
1300                     UWORD32 u4_timestamp_high)
1301 {
1302     IV_STATUS_T status;
1303 
1304     ih264e_ctl_set_me_params_ip_t s_me_params_ip;
1305     ih264e_ctl_set_me_params_op_t s_me_params_op;
1306 
1307     s_me_params_ip.s_ive_ip.e_cmd = IVE_CMD_VIDEO_CTL;
1308     s_me_params_ip.s_ive_ip.e_sub_cmd = IVE_CMD_CTL_SET_ME_PARAMS;
1309 
1310     s_me_params_ip.s_ive_ip.u4_enable_fast_sad = ps_app_ctxt->u4_enable_fast_sad;
1311     s_me_params_ip.s_ive_ip.u4_enable_alt_ref = ps_app_ctxt->u4_enable_alt_ref;
1312 
1313     s_me_params_ip.s_ive_ip.u4_enable_hpel  =   ps_app_ctxt->u4_hpel;
1314     s_me_params_ip.s_ive_ip.u4_enable_qpel  =   ps_app_ctxt->u4_qpel;
1315     s_me_params_ip.s_ive_ip.u4_me_speed_preset  =   ps_app_ctxt->u4_me_speed;
1316     s_me_params_ip.s_ive_ip.u4_srch_rng_x   =   ps_app_ctxt->u4_srch_rng_x;
1317     s_me_params_ip.s_ive_ip.u4_srch_rng_y   =   ps_app_ctxt->u4_srch_rng_y;
1318 
1319     s_me_params_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1320     s_me_params_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1321 
1322     s_me_params_ip.s_ive_ip.u4_size = sizeof(ih264e_ctl_set_me_params_ip_t);
1323     s_me_params_op.s_ive_op.u4_size = sizeof(ih264e_ctl_set_me_params_op_t);
1324 
1325     status = ih264e_api_function(ps_app_ctxt->ps_enc, &s_me_params_ip,
1326                                  &s_me_params_op);
1327     if(status != IV_SUCCESS)
1328     {
1329         CHAR ac_error[STRLENGTH];
1330         sprintf(ac_error, "Unable to set me params = 0x%x\n",
1331                 s_me_params_op.s_ive_op.u4_error_code);
1332         codec_exit(ac_error);
1333     }
1334     return;
1335 }
1336 
1337 
set_gop_params(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1338 void set_gop_params(app_ctxt_t *ps_app_ctxt,
1339                     UWORD32 u4_timestamp_low,
1340                     UWORD32 u4_timestamp_high)
1341 {
1342     IV_STATUS_T status;
1343 
1344     ih264e_ctl_set_gop_params_ip_t s_gop_params_ip;
1345     ih264e_ctl_set_gop_params_op_t s_gop_params_op;
1346 
1347     s_gop_params_ip.s_ive_ip.e_cmd = IVE_CMD_VIDEO_CTL;
1348     s_gop_params_ip.s_ive_ip.e_sub_cmd = IVE_CMD_CTL_SET_GOP_PARAMS;
1349 
1350     s_gop_params_ip.s_ive_ip.u4_i_frm_interval = ps_app_ctxt->u4_i_interval;
1351     s_gop_params_ip.s_ive_ip.u4_idr_frm_interval = ps_app_ctxt->u4_idr_interval;
1352 
1353     s_gop_params_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1354     s_gop_params_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1355 
1356     s_gop_params_ip.s_ive_ip.u4_size = sizeof(ih264e_ctl_set_gop_params_ip_t);
1357     s_gop_params_op.s_ive_op.u4_size = sizeof(ih264e_ctl_set_gop_params_op_t);
1358 
1359     status = ih264e_api_function(ps_app_ctxt->ps_enc, &s_gop_params_ip,
1360                                       &s_gop_params_op);
1361     if(status != IV_SUCCESS)
1362     {
1363         CHAR ac_error[STRLENGTH];
1364         sprintf(ac_error, "Unable to set ME params = 0x%x\n",
1365                 s_gop_params_op.s_ive_op.u4_error_code);
1366         codec_exit(ac_error);
1367     }
1368     return;
1369 }
1370 
set_profile_params(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1371 void set_profile_params(app_ctxt_t *ps_app_ctxt,
1372                     UWORD32 u4_timestamp_low,
1373                     UWORD32 u4_timestamp_high)
1374 {
1375     IV_STATUS_T status;
1376 
1377     ih264e_ctl_set_profile_params_ip_t s_profile_params_ip;
1378     ih264e_ctl_set_profile_params_op_t s_profile_params_op;
1379 
1380     s_profile_params_ip.s_ive_ip.e_cmd = IVE_CMD_VIDEO_CTL;
1381     s_profile_params_ip.s_ive_ip.e_sub_cmd = IVE_CMD_CTL_SET_PROFILE_PARAMS;
1382 
1383     s_profile_params_ip.s_ive_ip.e_profile = ps_app_ctxt->e_profile;
1384 
1385     s_profile_params_ip.s_ive_ip.u4_entropy_coding_mode = ps_app_ctxt->u4_entropy_coding_mode;
1386 
1387     s_profile_params_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1388     s_profile_params_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1389 
1390     s_profile_params_ip.s_ive_ip.u4_size = sizeof(ih264e_ctl_set_profile_params_ip_t);
1391     s_profile_params_op.s_ive_op.u4_size = sizeof(ih264e_ctl_set_profile_params_op_t);
1392 
1393     status = ih264e_api_function(ps_app_ctxt->ps_enc, &s_profile_params_ip,
1394                                       &s_profile_params_op);
1395     if(status != IV_SUCCESS)
1396     {
1397         CHAR ac_error[STRLENGTH];
1398         sprintf(ac_error, "Unable to set profile params = 0x%x\n",
1399                 s_profile_params_op.s_ive_op.u4_error_code);
1400         codec_exit(ac_error);
1401     }
1402     return;
1403 }
1404 
set_deblock_params(app_ctxt_t * ps_app_ctxt,UWORD32 u4_timestamp_low,UWORD32 u4_timestamp_high)1405 void set_deblock_params(app_ctxt_t *ps_app_ctxt,
1406                     UWORD32 u4_timestamp_low,
1407                     UWORD32 u4_timestamp_high)
1408 {
1409     IV_STATUS_T status;
1410 
1411     ih264e_ctl_set_deblock_params_ip_t s_deblock_params_ip;
1412     ih264e_ctl_set_deblock_params_op_t s_deblock_params_op;
1413 
1414     s_deblock_params_ip.s_ive_ip.e_cmd = IVE_CMD_VIDEO_CTL;
1415     s_deblock_params_ip.s_ive_ip.e_sub_cmd = IVE_CMD_CTL_SET_DEBLOCK_PARAMS;
1416 
1417     s_deblock_params_ip.s_ive_ip.u4_disable_deblock_level =
1418                     ps_app_ctxt->u4_disable_deblk_level;
1419 
1420     s_deblock_params_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1421     s_deblock_params_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1422 
1423     s_deblock_params_ip.s_ive_ip.u4_size = sizeof(ih264e_ctl_set_deblock_params_ip_t);
1424     s_deblock_params_op.s_ive_op.u4_size = sizeof(ih264e_ctl_set_deblock_params_op_t);
1425 
1426     status = ih264e_api_function(ps_app_ctxt->ps_enc, &s_deblock_params_ip,
1427                                       &s_deblock_params_op);
1428     if(status != IV_SUCCESS)
1429     {
1430         CHAR ac_error[STRLENGTH];
1431         sprintf(ac_error, "Unable to enable/disable deblock params = 0x%x\n",
1432                 s_deblock_params_op.s_ive_op.u4_error_code);
1433         codec_exit(ac_error);
1434     }
1435     return;
1436 }
1437 
1438 #define PEAK_WINDOW_SIZE    8
1439 
synchronous_encode(iv_obj_t * ps_enc,app_ctxt_t * ps_app_ctxt)1440 void synchronous_encode(iv_obj_t *ps_enc, app_ctxt_t *ps_app_ctxt)
1441 {
1442     ih264e_video_encode_ip_t ih264e_video_encode_ip;
1443     ih264e_video_encode_op_t ih264e_video_encode_op;
1444 
1445     ive_video_encode_ip_t *ps_video_encode_ip = &ih264e_video_encode_ip.s_ive_ip;
1446     ive_video_encode_op_t *ps_video_encode_op = &ih264e_video_encode_op.s_ive_op;
1447 
1448     iv_raw_buf_t *ps_inp_raw_buf = &ps_video_encode_ip->s_inp_buf;
1449 
1450     IV_STATUS_T status = IV_SUCCESS;
1451 
1452     WORD32 i, is_last = 0, buff_size = 0, num_bytes = 0;
1453     UWORD32 u4_total_time = 0;
1454     UWORD8 *pu1_buf = NULL;
1455     UWORD32 u4_timestamp_low, u4_timestamp_high;
1456     void *pv_mb_info = NULL, *pv_pic_info = NULL;
1457 
1458     TIMER curtime ;
1459 #ifdef WINDOWS_TIMER
1460     TIMER frequency;
1461 #endif
1462     WORD32 peak_window[PEAK_WINDOW_SIZE] = {0};
1463     WORD32 peak_window_idx = 0;
1464     WORD32 peak_avg_max = 0, timetaken = 0;
1465     iv_raw_buf_t s_inp_buf, s_recon_buf;
1466     CHAR ac_error[STRLENGTH];
1467     WORD32 end_of_frames=0;
1468     WORD32 i4_inp_done =0;
1469 
1470     u4_timestamp_low = 0;
1471     u4_timestamp_high = 0;
1472 
1473     /*************************************************************************/
1474     /*                         Allocate I/O Buffers                          */
1475     /*************************************************************************/
1476     allocate_input(ps_app_ctxt);
1477     allocate_output(ps_app_ctxt);
1478     allocate_recon(ps_app_ctxt);
1479 
1480     /* init psnr */
1481     init_psnr(ps_app_ctxt);
1482 
1483     /* open file pointers */
1484     ps_app_ctxt->fp_ip = fopen(ps_app_ctxt->ac_ip_fname, "rb");
1485     if(NULL == ps_app_ctxt->fp_ip)
1486     {
1487         sprintf(ac_error, "Unable to open input file for reading: %s", ps_app_ctxt->ac_ip_fname);
1488         invalid_argument_exit(ac_error);
1489     }
1490 
1491     ps_app_ctxt->fp_op = fopen(ps_app_ctxt->ac_op_fname, "wb");
1492     if(NULL == ps_app_ctxt->fp_op)
1493     {
1494         sprintf(ac_error, "Unable to open output file for writing: %s", ps_app_ctxt->ac_op_fname);
1495         invalid_argument_exit(ac_error);
1496     }
1497 
1498     if(1 == ps_app_ctxt->u4_recon_enable)
1499     {
1500         ps_app_ctxt->fp_recon = fopen(ps_app_ctxt->ac_recon_fname, "wb");
1501         if(NULL == ps_app_ctxt->fp_recon)
1502         {
1503             sprintf(ac_error, "Unable to open recon file for writing: %s", ps_app_ctxt->ac_recon_fname);
1504             invalid_argument_exit(ac_error);
1505         }
1506     }
1507 
1508     if(1 == ps_app_ctxt->u4_chksum_enable)
1509     {
1510         ps_app_ctxt->fp_chksum               = fopen(ps_app_ctxt->ac_chksum_fname, "wb");
1511         if(NULL == ps_app_ctxt->fp_chksum)
1512         {
1513             sprintf(ac_error, "Unable to open checksum file for writing: %s", ps_app_ctxt->ac_chksum_fname);
1514             invalid_argument_exit(ac_error);
1515         }
1516     }
1517 
1518     /* If PSNR is enabled, open input file again and hold a different file pointer
1519      * This makes it easy to compute PSNR without adding dependency between input and recon threads
1520      */
1521     if(1 == ps_app_ctxt->u4_psnr_enable)
1522     {
1523         ps_app_ctxt->fp_psnr_ip              = fopen(ps_app_ctxt->ac_ip_fname, "rb");
1524         if(NULL == ps_app_ctxt->fp_psnr_ip)
1525         {
1526             sprintf(ac_error, "Unable to open input file for reading: %s", ps_app_ctxt->ac_ip_fname);
1527             invalid_argument_exit(ac_error);
1528         }
1529     }
1530 
1531     if(0 != ps_app_ctxt->u4_mb_info_type)
1532     {
1533         ps_app_ctxt->fp_mb_info  = fopen(ps_app_ctxt->ac_mb_info_fname, "rb");
1534         if(NULL == ps_app_ctxt->fp_mb_info)
1535         {
1536             sprintf(ac_error, "Unable to open MB info file for reading: %s", ps_app_ctxt->ac_mb_info_fname);
1537             invalid_argument_exit(ac_error);
1538         }
1539     }
1540     if (ps_app_ctxt->u4_pic_info_type)
1541     {
1542         ps_app_ctxt->fp_pic_info  = fopen(ps_app_ctxt->ac_pic_info_fname, "rb");
1543         if(NULL == ps_app_ctxt->fp_pic_info)
1544         {
1545             sprintf(ac_error, "Unable to open Pic info file for reading: %s", ps_app_ctxt->ac_pic_info_fname);
1546             invalid_argument_exit(ac_error);
1547         }
1548     }
1549 
1550     GETTIME(&ps_app_ctxt->enc_start_time);
1551     ps_app_ctxt->enc_last_time = ps_app_ctxt->enc_start_time;
1552 
1553     while(1)
1554     {
1555 
1556         /******************************************************************************/
1557         /****************** Input Initialization **************************************/
1558         /******************************************************************************/
1559 
1560         for(i = 0; i < DEFAULT_MAX_INPUT_BUFS; i++)
1561         {
1562             if(ps_app_ctxt->as_input_buf[i].u4_is_free)
1563             {
1564                 pu1_buf = ps_app_ctxt->as_input_buf[i].pu1_buf;
1565                 pv_mb_info = ps_app_ctxt->as_input_buf[i].pv_mb_info;
1566                 pv_pic_info = ps_app_ctxt->as_input_buf[i].pv_pic_info;
1567                 ps_app_ctxt->as_input_buf[i].u4_is_free = 0;
1568                 break;
1569             }
1570         }
1571 
1572         if (i == DEFAULT_MAX_INPUT_BUFS)
1573         {
1574             printf("\n Unable to find a free input buffer!!");
1575             exit(0);
1576         }
1577 
1578         ps_video_encode_ip->u4_size = sizeof(ih264e_video_encode_ip_t);
1579         ps_video_encode_op->u4_size = sizeof(ih264e_video_encode_op_t);
1580 
1581         ps_video_encode_ip->e_cmd = IVE_CMD_VIDEO_ENCODE;
1582         ps_video_encode_ip->pv_bufs = pu1_buf;
1583         ps_video_encode_ip->pv_mb_info = pv_mb_info;
1584         ps_video_encode_ip->pv_pic_info = pv_pic_info;
1585         ps_video_encode_ip->u4_pic_info_type = ps_app_ctxt->u4_pic_info_type;
1586         /*
1587          * Since the buffers are used for reading,
1588          * And after each row we have a stride we nned to calculate
1589          * the luma size according to the stride
1590          */
1591         ps_inp_raw_buf->e_color_fmt = ps_app_ctxt->e_inp_color_fmt;
1592 
1593         /* Initialize for 420SP */
1594         if(IV_YUV_420SP_UV == ps_app_ctxt->e_inp_color_fmt||
1595                         IV_YUV_420SP_VU == ps_app_ctxt->e_inp_color_fmt)
1596         {
1597             /*init luma buffer*/
1598             ps_inp_raw_buf->apv_bufs[0] = pu1_buf;
1599 
1600             /*Init chroma buffer*/
1601             pu1_buf += ps_app_ctxt->u4_strd * ps_app_ctxt->u4_ht;
1602             ps_inp_raw_buf->apv_bufs[1] = pu1_buf;
1603 
1604             ps_inp_raw_buf->au4_wd[0] =  ps_app_ctxt->u4_wd;
1605             ps_inp_raw_buf->au4_wd[1] =  ps_app_ctxt->u4_wd;
1606 
1607             ps_inp_raw_buf->au4_ht[0] =  ps_app_ctxt->u4_ht;
1608             ps_inp_raw_buf->au4_ht[1] =  ps_app_ctxt->u4_ht / 2;
1609 
1610             ps_inp_raw_buf->au4_strd[0] =  ps_app_ctxt->u4_strd;
1611             ps_inp_raw_buf->au4_strd[1] =  ps_app_ctxt->u4_strd;
1612         }
1613         else if(IV_YUV_420P == ps_app_ctxt->e_inp_color_fmt)
1614         {
1615             /* init buffers */
1616             ps_inp_raw_buf->apv_bufs[0] = pu1_buf;
1617             pu1_buf += (ps_app_ctxt->u4_wd) * ps_app_ctxt->u4_ht;
1618             ps_inp_raw_buf->apv_bufs[1] = pu1_buf;
1619             pu1_buf += (ps_app_ctxt->u4_wd >> 1) * (ps_app_ctxt->u4_ht >> 1);
1620             ps_inp_raw_buf->apv_bufs[2] = pu1_buf;
1621 
1622             ps_inp_raw_buf->au4_wd[0] =  ps_app_ctxt->u4_wd;
1623             ps_inp_raw_buf->au4_wd[1] =  ps_app_ctxt->u4_wd / 2;
1624             ps_inp_raw_buf->au4_wd[2] =  ps_app_ctxt->u4_wd / 2;
1625 
1626             ps_inp_raw_buf->au4_ht[0] =  ps_app_ctxt->u4_ht;
1627             ps_inp_raw_buf->au4_ht[1] =  ps_app_ctxt->u4_ht / 2;
1628             ps_inp_raw_buf->au4_ht[2] =  ps_app_ctxt->u4_ht / 2;
1629 
1630             ps_inp_raw_buf->au4_strd[0] =  ps_app_ctxt->u4_strd;
1631             ps_inp_raw_buf->au4_strd[1] =  ps_app_ctxt->u4_strd / 2;
1632             ps_inp_raw_buf->au4_strd[2] =  ps_app_ctxt->u4_strd / 2;
1633 
1634         }
1635         else if(IV_YUV_422ILE == ps_app_ctxt->e_inp_color_fmt)
1636         {
1637             /*init luma buffer*/
1638             ps_inp_raw_buf->apv_bufs[0] = pu1_buf;
1639 
1640             ps_inp_raw_buf->au4_wd[0] =  ps_app_ctxt->u4_wd * 2;
1641 
1642             ps_inp_raw_buf->au4_ht[0] =  ps_app_ctxt->u4_ht;
1643 
1644             ps_inp_raw_buf->au4_strd[0] = ps_app_ctxt->u4_strd *2;
1645         }
1646 
1647         /*
1648          * Here we read input and other associated buffers. Regardless of success
1649          * we will proceed from here as we will need extra calls to flush out
1650          * input queue in encoder. Note that this is not necessary. You can just
1651          * send encode calls till with valid output and recon buffers till the
1652          * queue is flushed.
1653          */
1654         while(1)
1655         {
1656             IV_STATUS_T mb_info_status = IV_SUCCESS, pic_info_status = IV_SUCCESS;
1657 
1658             status = read_input(ps_app_ctxt->fp_ip, ps_inp_raw_buf);
1659 
1660             if (ps_app_ctxt->u4_mb_info_type != 0)
1661             {
1662                 mb_info_status = read_mb_info(ps_app_ctxt, pv_mb_info);
1663             }
1664             if (ps_app_ctxt->u4_pic_info_type != 0)
1665             {
1666                 pic_info_status = read_pic_info(ps_app_ctxt, pv_pic_info);
1667             }
1668             if((IV_SUCCESS != status) || (IV_SUCCESS != mb_info_status)
1669                             || (IV_SUCCESS != pic_info_status))
1670             {
1671                 if(0 == ps_app_ctxt->u4_loopback)
1672                 {
1673                     is_last = 1;
1674                     break;
1675                 }
1676                 else
1677                     fseek(ps_app_ctxt->fp_ip, 0, SEEK_SET);
1678             }
1679             break;
1680         }
1681 
1682         /******************************************************************************/
1683         /****************** Output Initialization *************************************/
1684         /******************************************************************************/
1685 
1686         for(i = 0; i < DEFAULT_MAX_OUTPUT_BUFS; i++)
1687         {
1688             if(ps_app_ctxt->as_output_buf[i].u4_is_free)
1689             {
1690                 pu1_buf = ps_app_ctxt->as_output_buf[i].pu1_buf;
1691                 buff_size = ps_app_ctxt->as_output_buf[i].u4_buf_size;
1692                 ps_app_ctxt->as_output_buf[i].u4_is_free = 0;
1693                 break;
1694             }
1695         }
1696         ps_video_encode_ip->s_out_buf.pv_buf = pu1_buf;
1697         ps_video_encode_ip->s_out_buf.u4_bytes = 0;
1698         ps_video_encode_ip->s_out_buf.u4_bufsize = buff_size;
1699 
1700         /******************************************************************************/
1701         /****************** Recon Initialization **************************************/
1702         /******************************************************************************/
1703         init_raw_buf_descr(ps_app_ctxt, &s_recon_buf, ps_app_ctxt->as_recon_buf[0].pu1_buf, ps_app_ctxt->e_recon_color_fmt);
1704 
1705         if(ps_app_ctxt->u4_psnr_enable)
1706             init_raw_buf_descr(ps_app_ctxt, &s_inp_buf, ps_app_ctxt->pu1_psnr_buf, ps_app_ctxt->e_inp_color_fmt);
1707 
1708         ps_video_encode_ip->s_recon_buf = s_recon_buf;
1709 
1710         /******************************************************************************/
1711         /************************* Un Initialized *************************************/
1712         /******************************************************************************/
1713         if(0 == ps_app_ctxt->u4_loopback)
1714         {
1715             /* If input file is read completely and loopback is not enabled,
1716              *  then exit the loop */
1717             if(feof(ps_app_ctxt->fp_ip))
1718             {
1719                 is_last = 1;
1720             }
1721         }
1722 
1723 
1724         /* If last frame, send input null to get back encoded frames */
1725         if ( is_last == 1 || ((ps_app_ctxt->u4_max_num_frms) <= u4_timestamp_low) )
1726         {
1727             is_last = 1;
1728             ps_inp_raw_buf->apv_bufs[0] = NULL;
1729             ps_inp_raw_buf->apv_bufs[1] = NULL;
1730             ps_inp_raw_buf->apv_bufs[2] = NULL;
1731         }
1732 
1733         ps_video_encode_ip->u4_is_last = is_last;
1734         ps_video_encode_ip->u4_mb_info_type = ps_app_ctxt->u4_mb_info_type;
1735         ps_video_encode_ip->u4_pic_info_type = ps_app_ctxt->u4_pic_info_type;;
1736         ps_video_encode_op->s_out_buf.pv_buf= NULL;
1737         ps_video_encode_ip->u4_timestamp_high = u4_timestamp_high;
1738         ps_video_encode_ip->u4_timestamp_low = u4_timestamp_low;
1739 
1740 
1741         GETTIME(&ps_app_ctxt->enc_last_time);
1742 
1743         status = ih264e_api_function(ps_enc, &ih264e_video_encode_ip, &ih264e_video_encode_op);
1744 
1745         if (IV_SUCCESS != status)
1746         {
1747             printf("Encode Frame failed = 0x%x\n", ih264e_video_encode_op.s_ive_op.u4_error_code);
1748             break;
1749         }
1750 
1751 #ifdef WINDOWS_TIMER
1752         QueryPerformanceFrequency ( &frequency);
1753 #endif
1754         GETTIME(&curtime);
1755         ELAPSEDTIME(ps_app_ctxt->enc_last_time, curtime, timetaken, frequency);
1756         ps_app_ctxt->enc_last_time = curtime;
1757 
1758 #ifdef PROFILE_ENABLE
1759         {
1760             WORD32 peak_avg, id;
1761             u4_total_time += timetaken;
1762             peak_window[peak_window_idx++] = timetaken;
1763             if(peak_window_idx == PEAK_WINDOW_SIZE)
1764                 peak_window_idx = 0;
1765             peak_avg = 0;
1766             for(id = 0; id < PEAK_WINDOW_SIZE; id++)
1767             {
1768                 peak_avg += peak_window[id];
1769             }
1770             peak_avg /= PEAK_WINDOW_SIZE;
1771             if (peak_avg > peak_avg_max)
1772                 peak_avg_max = peak_avg;
1773         }
1774 #endif
1775 
1776         /******************************************************************************/
1777         /****************** Writing Output ********************************************/
1778         /******************************************************************************/
1779         num_bytes = 0;
1780 
1781         if(1 == ps_video_encode_op->output_present)
1782         {
1783             num_bytes = ps_video_encode_op->s_out_buf.u4_bytes;
1784             buff_size = ps_video_encode_op->s_out_buf.u4_bufsize;
1785             pu1_buf = (UWORD8*)ps_video_encode_op->s_out_buf.pv_buf;
1786 
1787             status = write_output(ps_app_ctxt->fp_op, pu1_buf, num_bytes);
1788             if(IV_SUCCESS != status)
1789             {
1790                 printf("Error: Unable to write to output file\n");
1791                 break;
1792             }
1793         }
1794 
1795         /* free input bufer if codec returns a valid input buffer */
1796         if (ps_video_encode_op->s_inp_buf.apv_bufs[0])
1797         {
1798             /* Reuse of freed input buffer */
1799             for(i = 0; i < DEFAULT_MAX_INPUT_BUFS; i++)
1800             {
1801                 if(ps_app_ctxt->as_input_buf[i].pu1_buf == ps_video_encode_op->s_inp_buf.apv_bufs[0])
1802                 {
1803                     ps_app_ctxt->as_input_buf[i].u4_is_free = 1;
1804                     break;
1805                 }
1806             }
1807         }
1808 
1809         /* free output buffer if codec returns a valid output buffer */
1810         // if(ps_video_encode_op->s_out_buf.pv_buf)
1811         {
1812             for(i = 0; i < DEFAULT_MAX_OUTPUT_BUFS; i++)
1813             {
1814                 if(ps_app_ctxt->as_output_buf[i].pu1_buf == ps_video_encode_op->s_out_buf.pv_buf)
1815                 {
1816                     ps_app_ctxt->as_output_buf[i].u4_is_free = 1;
1817                     break;
1818                 }
1819             }
1820         }
1821 
1822         /**********************************************************************
1823          *  Print stats
1824          **********************************************************************/
1825         {
1826             UWORD8 u1_pic_type[][5] =
1827                 { "IDR", "I", "P", "B", "NA" };
1828             WORD32 lookup_idx = 0;
1829 
1830             if (ih264e_video_encode_op.s_ive_op.u4_encoded_frame_type
1831                             == IV_IDR_FRAME)
1832             {
1833                 lookup_idx = 0;
1834             }
1835             else if(ih264e_video_encode_op.s_ive_op.u4_encoded_frame_type
1836                             == IV_I_FRAME)
1837             {
1838                 lookup_idx = 1;
1839             }
1840             else if(ih264e_video_encode_op.s_ive_op.u4_encoded_frame_type
1841                             == IV_P_FRAME)
1842             {
1843                 lookup_idx = 2;
1844             }
1845             else if(ih264e_video_encode_op.s_ive_op.u4_encoded_frame_type
1846                             == IV_B_FRAME)
1847             {
1848                 lookup_idx = 3;
1849             }
1850             else if(ih264e_video_encode_op.s_ive_op.u4_encoded_frame_type
1851                             == IV_NA_FRAME)
1852             {
1853                 lookup_idx = 4;
1854             }
1855 
1856             if (ih264e_video_encode_op.s_ive_op.u4_encoded_frame_type
1857                             != IV_NA_FRAME)
1858             {
1859                 ps_app_ctxt->u4_pics_cnt++;
1860                 ps_app_ctxt->avg_time = u4_total_time / ps_app_ctxt->u4_pics_cnt;
1861                 ps_app_ctxt->u4_total_bytes += num_bytes;
1862             }
1863 
1864             if (ps_app_ctxt->u4_psnr_enable == 0)
1865             {
1866                 printf("[%s] PicNum %4d Bytes Generated %6d TimeTaken(microsec): %6d AvgTime: %6d PeakAvgTimeMax: %6d\n",
1867                        u1_pic_type[lookup_idx], ps_app_ctxt->u4_pics_cnt,
1868                        num_bytes, timetaken, ps_app_ctxt->avg_time,
1869                        peak_avg_max);
1870             }
1871         }
1872 
1873 
1874         /* For psnr computation, we need to read the correct input frame and
1875          * compare with recon. The difficulty with doing it is that we only know
1876          * that the frame number of recon is monotonically increasing. There
1877          * may be gaps in the recon if any pre or post enc skip happens. There are
1878          * 3 senarios
1879          *  1) A frame is encoded -> returns the pic type
1880          *  2) A frame is not encoded -> Encoder is waiting, the frame may get
1881          *     encoded later
1882          *  3) A frame is not encoded -> A post enc or pre enc skip happend. The
1883          *     frame is not going to be encoded
1884          *
1885          *     The 1st and 2nd scenarios are easy, since we just needs to increment
1886          *     recon cnt whenever we get a valid recon. This cnt can we used to
1887          *     sync the recon and input
1888          *     3rd scenario in conjuction with 2nd will pose problems. Even if
1889          *     the returning frame is NA, we donot know we should increment the
1890          *     recon cnt or not becasue it can be case 2 or case 3.
1891          *
1892          *  Solutions:
1893          *  -------------------------
1894          *   One way to over come this will be to return more information as of
1895          *   the frame type. We can send if a frame was skipped as a part of the
1896          *   return frame type.
1897          *   This will not work. Since the output and recon are not in sync, we
1898          *   cannot use the current output frame type to determine if a recon
1899          *   is present currently or not. We need some other way to acheive this.
1900          *
1901          *   Other way to do this which is cleaner and maintains the seperation
1902          *   between recon and the ouptut is to set the width [& height] of output recon
1903          *   buffer to be zero. Hence we will in effect be saying :"look there
1904          *   is a recon, but due to frame not being encoded it is having a width 0".
1905          *   To be more clear we need to make height also to be zero.
1906          *
1907          *   But are we using these variables for allocating and deallocating
1908          *   the buffers some where ? No we are not. The buffer gets re-init
1909          *   at every encode call
1910          *
1911          *   Fixes
1912          *   ------------------------
1913          *   Currently the recon buff width and height are set in the encoder.
1914          *   This will not work now because since recon and input are not
1915          *   in sync. Hence a recon buff sent at time stamp x will get used to
1916          *   fill recon of input at time stamp y (x > y). If we reduced the
1917          *   frame dimensions in between, the recon buffer will not have enough
1918          *   space. Hence we need to set the with and height appropriatley inside
1919          *   lib itself.
1920          */
1921 
1922         if (ps_app_ctxt->u4_recon_enable || ps_app_ctxt->u4_chksum_enable
1923                         || ps_app_ctxt->u4_psnr_enable)
1924         {
1925             if (ps_video_encode_op->dump_recon)
1926             {
1927                 s_recon_buf = ps_video_encode_op->s_recon_buf;
1928 
1929                 /* Read input for psnr computuation */
1930                 if (ps_app_ctxt->u4_psnr_enable)
1931                     read_input(ps_app_ctxt->fp_psnr_ip, &s_inp_buf);
1932 
1933                 /* if we have a valid recon buffer do the assocated tasks */
1934                 if (s_recon_buf.au4_wd[0])
1935                 {
1936                     /* Dump recon when enabled, and output bytes != 0 */
1937                     if (ps_app_ctxt->u4_recon_enable)
1938                     {
1939                         status = write_recon(ps_app_ctxt->fp_recon, &s_recon_buf);
1940                         if (IV_SUCCESS != status)
1941                         {
1942                             printf("Error: Unable to write to recon file\n");
1943                             break;
1944                         }
1945                     }
1946 
1947                     if (ps_app_ctxt->u4_psnr_enable)
1948                     {
1949                         compute_psnr(ps_app_ctxt, &s_recon_buf, &s_inp_buf);
1950                     }
1951 
1952 
1953                     if (ps_app_ctxt->u4_chksum_enable)
1954                     {
1955                         WORD32 comp, num_comp = 2;
1956 
1957                         if (IV_YUV_420P == s_recon_buf.e_color_fmt)
1958                             num_comp = 3;
1959 
1960                         for (comp = 0; comp < num_comp; comp++)
1961                         {
1962                             UWORD8 au1_chksum[16];
1963                             calc_md5_cksum((UWORD8 *)s_recon_buf.apv_bufs[comp],
1964                                            s_recon_buf.au4_strd[comp],
1965                                            s_recon_buf.au4_wd[comp],
1966                                            s_recon_buf.au4_ht[comp],
1967                                            au1_chksum);
1968                             fwrite(au1_chksum, sizeof(UWORD8), 16, ps_app_ctxt->fp_chksum);
1969                         }
1970                     }
1971                 }
1972             }
1973         }
1974 
1975         u4_timestamp_low++;
1976 
1977         /* Break if all the encoded frames are taken from encoder */
1978         if (1 == ps_video_encode_op->u4_is_last)
1979         {
1980             break;
1981         }
1982     }
1983 
1984     /* Pic count is 1 more than actual num frames encoded, because last call is to just get the output  */
1985     ps_app_ctxt->u4_pics_cnt--;
1986 
1987     if(ps_app_ctxt->u4_psnr_enable)
1988     {
1989         print_average_psnr(ps_app_ctxt);
1990     }
1991 
1992     /* house keeping operations */
1993     fclose(ps_app_ctxt->fp_ip);
1994     fclose(ps_app_ctxt->fp_op);
1995     if(1 == ps_app_ctxt->u4_recon_enable)
1996     {
1997         fclose(ps_app_ctxt->fp_recon);
1998     }
1999     if(1 == ps_app_ctxt->u4_chksum_enable)
2000     {
2001         fclose(ps_app_ctxt->fp_chksum);
2002     }
2003     if(1 == ps_app_ctxt->u4_psnr_enable)
2004     {
2005         fclose(ps_app_ctxt->fp_psnr_ip);
2006     }
2007 
2008     if(0 != ps_app_ctxt->u4_mb_info_type)
2009     {
2010         fclose(ps_app_ctxt->fp_mb_info);
2011     }
2012     if (ps_app_ctxt->u4_pic_info_type)
2013     {
2014         fclose(ps_app_ctxt->fp_pic_info);
2015     }
2016 
2017     free_input(ps_app_ctxt);
2018     free_output(ps_app_ctxt);
2019     free_recon(ps_app_ctxt);
2020 }
2021 
2022 /*****************************************************************************/
2023 /*                                                                           */
2024 /*  Function Name : main                                                     */
2025 /*                                                                           */
2026 /*  Description   : Application to demonstrate codec API                     */
2027 /*                                                                           */
2028 /*                                                                           */
2029 /*  Inputs        : argc    - Number of arguments                            */
2030 /*                  argv[]  - Arguments                                      */
2031 /*  Globals       :                                                          */
2032 /*  Processing    : Shows how to use create, process, control and delete     */
2033 /*                                                                           */
2034 /*  Outputs       : Codec output in a file                                   */
2035 /*  Returns       :                                                          */
2036 /*                                                                           */
2037 /*  Issues        : Assumes both PROFILE_ENABLE to be                        */
2038 /*                  defined for multithread decode-display working           */
2039 /*                                                                           */
2040 /*  Revision History:                                                        */
2041 /*                                                                           */
2042 /*         DD MM YYYY   Author(s)       Changes                              */
2043 /*         20 11 2013   100189          Initial Version                      */
2044 /*****************************************************************************/
2045 #ifdef IOS
h264enc_main(char * homedir,char * documentdir,int screen_wd,int screen_ht)2046 int h264enc_main(char * homedir,char *documentdir, int screen_wd, int screen_ht)
2047 #else
2048 int main(int argc, char *argv[])
2049 #endif
2050 {
2051     /* Config Parameters for Encoding */
2052     app_ctxt_t s_app_ctxt;
2053 
2054     /* error string */
2055     CHAR ac_error[STRLENGTH];
2056 
2057     /* config file name */
2058     CHAR ac_cfg_fname[STRLENGTH];
2059 
2060     /* error status */
2061     IV_STATUS_T status = IV_SUCCESS;
2062 #ifdef IOS
2063     /* temp var */
2064     CHAR filename_with_path[STRLENGTH];
2065 #endif
2066     WORD32 num_mem_recs;
2067     iv_obj_t *ps_enc;
2068     WORD32 i;
2069     FILE *fp_cfg = NULL;
2070 
2071 #ifdef X86_MINGW
2072 
2073     /* For getting printfs without any delay in eclipse */
2074     setvbuf(stdout, NULL, _IONBF, 0);
2075     setvbuf(stderr, NULL, _IONBF, 0);
2076 
2077 #endif
2078 
2079     init_default_params(&s_app_ctxt);
2080 
2081 #ifndef IOS
2082 
2083     /* Usage */
2084     if(argc < 2)
2085     {
2086         printf("Using enc.cfg as configuration file \n");
2087         strcpy(ac_cfg_fname, "enc.cfg");
2088     }
2089     else if(argc == 2)
2090     {
2091         if (!strcmp(argv[1], "--help"))
2092         {
2093             print_usage();
2094             exit(-1);
2095         }
2096         strcpy(ac_cfg_fname, argv[1]);
2097     }
2098 
2099 #else
2100     strcpy(ac_cfg_fname, "test.cfg");
2101 
2102 #endif
2103 
2104     /*************************************************************************/
2105     /* Parse arguments                                                       */
2106     /*************************************************************************/
2107 
2108 #ifndef IOS
2109 
2110     /* Read command line arguments */
2111     if(argc > 2)
2112     {
2113         for(i = 1; i + 1 < argc; i += 2)
2114         {
2115             if(CONFIG == get_argument(argv[i]))
2116             {
2117                 strcpy(ac_cfg_fname, argv[i + 1]);
2118                 if((fp_cfg = fopen(ac_cfg_fname, "r")) == NULL)
2119                 {
2120                     sprintf(ac_error,
2121                             "Could not open Configuration file %s",
2122                             ac_cfg_fname);
2123                     codec_exit(ac_error);
2124                 }
2125                 read_cfg_file(&s_app_ctxt, fp_cfg);
2126                 fclose(fp_cfg);
2127             }
2128             else
2129             {
2130                 parse_argument(&s_app_ctxt, argv[i], argv[i + 1]);
2131             }
2132         }
2133     }
2134     else
2135     {
2136         if((fp_cfg = fopen(ac_cfg_fname, "r")) == NULL)
2137         {
2138             sprintf(ac_error, "Could not open Configuration file %s",
2139                     ac_cfg_fname);
2140             codec_exit(ac_error);
2141         }
2142         read_cfg_file(&s_app_ctxt, fp_cfg);
2143         fclose(fp_cfg);
2144     }
2145 
2146 #else
2147 
2148     sprintf(filename_with_path, "%s/%s", homedir, "enc.cfg");
2149     if((fp_cfg = fopen(filename_with_path, "r")) == NULL)
2150     {
2151         sprintf(ac_error, "Could not open Configuration file %s",
2152                 ac_cfg_fname);
2153         codec_exit(ac_error);
2154 
2155     }
2156     read_cfg_file(&s_app_ctxt, fp_cfg);
2157     fclose(fp_cfg);
2158 
2159 #endif
2160 
2161 
2162     validate_params(&s_app_ctxt);
2163 
2164 
2165     /*************************************************************************/
2166     /*                      Getting Number of MemRecords                     */
2167     /*************************************************************************/
2168     {
2169         ih264e_num_mem_rec_ip_t s_num_mem_rec_ip;
2170         ih264e_num_mem_rec_op_t s_num_mem_rec_op;
2171 
2172         s_num_mem_rec_ip.s_ive_ip.u4_size = sizeof(ih264e_num_mem_rec_ip_t);
2173         s_num_mem_rec_op.s_ive_op.u4_size = sizeof(ih264e_num_mem_rec_op_t);
2174 
2175         s_num_mem_rec_ip.s_ive_ip.e_cmd = IV_CMD_GET_NUM_MEM_REC;
2176 
2177         status = ih264e_api_function(0, &s_num_mem_rec_ip, &s_num_mem_rec_op);
2178 
2179         if(status != IV_SUCCESS)
2180         {
2181             sprintf(ac_error, "Get number of memory records failed = 0x%x\n", s_num_mem_rec_op.s_ive_op.u4_error_code);
2182             codec_exit(ac_error);
2183         }
2184 
2185         s_app_ctxt.u4_num_mem_rec = num_mem_recs = s_num_mem_rec_op.s_ive_op.u4_num_mem_rec;
2186     }
2187 
2188     /* Allocate array to hold memory records */
2189     s_app_ctxt.ps_mem_rec = (iv_mem_rec_t *) malloc(num_mem_recs * sizeof(iv_mem_rec_t));
2190     if(NULL == s_app_ctxt.ps_mem_rec)
2191     {
2192 
2193         sprintf(ac_error, "Unable to allocate memory for hold memory records: Size %d", (WORD32)(num_mem_recs * sizeof(iv_mem_rec_t)));
2194         codec_exit(ac_error);
2195     }
2196 
2197     {
2198         iv_mem_rec_t *ps_mem_rec;
2199         ps_mem_rec = s_app_ctxt.ps_mem_rec;
2200         for(i = 0; i < num_mem_recs; i++)
2201         {
2202             ps_mem_rec->u4_size = sizeof(iv_mem_rec_t);
2203             ps_mem_rec->pv_base = NULL;
2204             ps_mem_rec->u4_mem_size = 0;
2205             ps_mem_rec->u4_mem_alignment = 0;
2206             ps_mem_rec->e_mem_type = IV_NA_MEM_TYPE;
2207 
2208             ps_mem_rec++;
2209         }
2210     }
2211 
2212     /*************************************************************************/
2213     /*                      Getting MemRecords Attributes                    */
2214     /*************************************************************************/
2215     {
2216         ih264e_fill_mem_rec_ip_t s_fill_mem_rec_ip;
2217         ih264e_fill_mem_rec_op_t s_fill_mem_rec_op;
2218 
2219         s_fill_mem_rec_ip.s_ive_ip.u4_size = sizeof(ih264e_fill_mem_rec_ip_t);
2220         s_fill_mem_rec_op.s_ive_op.u4_size = sizeof(ih264e_fill_mem_rec_op_t);
2221 
2222         s_fill_mem_rec_ip.s_ive_ip.e_cmd = IV_CMD_FILL_NUM_MEM_REC;
2223         s_fill_mem_rec_ip.s_ive_ip.ps_mem_rec = s_app_ctxt.ps_mem_rec;
2224         s_fill_mem_rec_ip.s_ive_ip.u4_num_mem_rec = s_app_ctxt.u4_num_mem_rec;
2225         s_fill_mem_rec_ip.s_ive_ip.u4_max_wd = s_app_ctxt.u4_max_wd;
2226         s_fill_mem_rec_ip.s_ive_ip.u4_max_ht = s_app_ctxt.u4_max_ht;
2227         s_fill_mem_rec_ip.s_ive_ip.u4_max_level = s_app_ctxt.u4_max_level;
2228         s_fill_mem_rec_ip.s_ive_ip.e_color_format = DEFAULT_INP_COLOR_FMT;
2229         s_fill_mem_rec_ip.s_ive_ip.u4_max_ref_cnt = DEFAULT_MAX_REF_FRM;
2230         s_fill_mem_rec_ip.s_ive_ip.u4_max_reorder_cnt = DEFAULT_MAX_REORDER_FRM;
2231         s_fill_mem_rec_ip.s_ive_ip.u4_max_srch_rng_x = DEFAULT_MAX_SRCH_RANGE_X;
2232         s_fill_mem_rec_ip.s_ive_ip.u4_max_srch_rng_y = DEFAULT_MAX_SRCH_RANGE_Y;
2233 
2234         status = ih264e_api_function(0, &s_fill_mem_rec_ip, &s_fill_mem_rec_op);
2235 
2236         if(status != IV_SUCCESS)
2237         {
2238             sprintf(ac_error, "Fill memory records failed = 0x%x\n",
2239                     s_fill_mem_rec_op.s_ive_op.u4_error_code);
2240             codec_exit(ac_error);
2241         }
2242     }
2243 
2244     /*************************************************************************/
2245     /*                      Allocating Memory for Mem Records                */
2246     /*************************************************************************/
2247     {
2248         WORD32 total_size;
2249         iv_mem_rec_t *ps_mem_rec;
2250         total_size = 0;
2251 
2252         ps_mem_rec = s_app_ctxt.ps_mem_rec;
2253         for(i = 0; i < num_mem_recs; i++)
2254         {
2255             ps_mem_rec->pv_base = ih264a_aligned_malloc(ps_mem_rec->u4_mem_alignment,
2256                                            ps_mem_rec->u4_mem_size);
2257             if(ps_mem_rec->pv_base == NULL)
2258             {
2259                 sprintf(ac_error, "Allocation failure for mem record id %d size %d\n",
2260                         i, ps_mem_rec->u4_mem_size);
2261                 codec_exit(ac_error);
2262             }
2263             total_size += ps_mem_rec->u4_mem_size;
2264 
2265             ps_mem_rec++;
2266         }
2267         printf("\nTotal memory for codec %d\n", total_size);
2268     }
2269 
2270 
2271     /*************************************************************************/
2272     /*                        Codec Instance Creation                        */
2273     /*************************************************************************/
2274     {
2275         ih264e_init_ip_t s_init_ip;
2276         ih264e_init_op_t s_init_op;
2277 
2278         ps_enc = s_app_ctxt.ps_mem_rec[0].pv_base;
2279         ps_enc->u4_size = sizeof(iv_obj_t);
2280         ps_enc->pv_fxns = ih264e_api_function;
2281         s_app_ctxt.ps_enc = ps_enc;
2282 
2283         s_init_ip.s_ive_ip.u4_size = sizeof(ih264e_init_ip_t);
2284         s_init_op.s_ive_op.u4_size = sizeof(ih264e_init_op_t);
2285 
2286         s_init_ip.s_ive_ip.e_cmd = IV_CMD_INIT;
2287         s_init_ip.s_ive_ip.u4_num_mem_rec = s_app_ctxt.u4_num_mem_rec;
2288         s_init_ip.s_ive_ip.ps_mem_rec = s_app_ctxt.ps_mem_rec;
2289         s_init_ip.s_ive_ip.u4_max_wd = s_app_ctxt.u4_max_wd;
2290         s_init_ip.s_ive_ip.u4_max_ht = s_app_ctxt.u4_max_ht;
2291         s_init_ip.s_ive_ip.u4_max_ref_cnt = DEFAULT_MAX_REF_FRM;
2292         s_init_ip.s_ive_ip.u4_max_reorder_cnt = DEFAULT_MAX_REORDER_FRM;
2293         s_init_ip.s_ive_ip.u4_max_level = s_app_ctxt.u4_max_level;
2294         s_init_ip.s_ive_ip.e_inp_color_fmt = s_app_ctxt.e_inp_color_fmt;
2295         if(s_app_ctxt.u4_recon_enable || s_app_ctxt.u4_psnr_enable || s_app_ctxt.u4_chksum_enable)
2296         {
2297             s_init_ip.s_ive_ip.u4_enable_recon           = 1;
2298         }
2299         else
2300         {
2301             s_init_ip.s_ive_ip.u4_enable_recon           = 0;
2302         }
2303         s_init_ip.s_ive_ip.e_recon_color_fmt    = s_app_ctxt.e_recon_color_fmt;
2304         s_init_ip.s_ive_ip.e_rc_mode            = s_app_ctxt.u4_rc;
2305         s_init_ip.s_ive_ip.u4_max_framerate     = s_app_ctxt.u4_max_frame_rate;
2306         s_init_ip.s_ive_ip.u4_max_bitrate       = s_app_ctxt.u4_max_bitrate;
2307         s_init_ip.s_ive_ip.u4_num_bframes       = s_app_ctxt.u4_num_bframes;
2308         s_init_ip.s_ive_ip.e_content_type       = IV_PROGRESSIVE;
2309         s_init_ip.s_ive_ip.u4_max_srch_rng_x    = DEFAULT_MAX_SRCH_RANGE_X;
2310         s_init_ip.s_ive_ip.u4_max_srch_rng_y    = DEFAULT_MAX_SRCH_RANGE_Y;
2311         s_init_ip.s_ive_ip.e_slice_mode         = s_app_ctxt.u4_slice_mode;
2312         s_init_ip.s_ive_ip.u4_slice_param       = s_app_ctxt.u4_slice_param;
2313         s_init_ip.s_ive_ip.e_arch               = s_app_ctxt.e_arch;
2314         s_init_ip.s_ive_ip.e_soc                = s_app_ctxt.e_soc;
2315 
2316         status = ih264e_api_function(ps_enc, &s_init_ip, &s_init_op);
2317 
2318         if(status != IV_SUCCESS)
2319         {
2320             sprintf(ac_error, "Init memory records failed = 0x%x\n",
2321                     s_init_op.s_ive_op.u4_error_code);
2322             codec_exit(ac_error);
2323         }
2324     }
2325 
2326     /*************************************************************************/
2327     /*                        set processor details                          */
2328     /*************************************************************************/
2329     {
2330         ih264e_ctl_set_num_cores_ip_t s_ctl_set_num_cores_ip;
2331         ih264e_ctl_set_num_cores_op_t s_ctl_set_num_cores_op;
2332         s_ctl_set_num_cores_ip.s_ive_ip.e_cmd = IVE_CMD_VIDEO_CTL;
2333         s_ctl_set_num_cores_ip.s_ive_ip.e_sub_cmd = IVE_CMD_CTL_SET_NUM_CORES;
2334         s_ctl_set_num_cores_ip.s_ive_ip.u4_num_cores = s_app_ctxt.u4_num_cores;
2335         s_ctl_set_num_cores_ip.s_ive_ip.u4_timestamp_high = 0;
2336         s_ctl_set_num_cores_ip.s_ive_ip.u4_timestamp_low = 0;
2337         s_ctl_set_num_cores_ip.s_ive_ip.u4_size = sizeof(ih264e_ctl_set_num_cores_ip_t);
2338 
2339         s_ctl_set_num_cores_op.s_ive_op.u4_size = sizeof(ih264e_ctl_set_num_cores_op_t);
2340 
2341         status = ih264e_api_function(ps_enc, (void *) &s_ctl_set_num_cores_ip,
2342                 (void *) &s_ctl_set_num_cores_op);
2343         if(status != IV_SUCCESS)
2344         {
2345             sprintf(ac_error, "Unable to set processor params = 0x%x\n",
2346                     s_ctl_set_num_cores_op.s_ive_op.u4_error_code);
2347             codec_exit(ac_error);
2348         }
2349 
2350     }
2351 
2352     /*************************************************************************/
2353     /*                        Get Codec Version                              */
2354     /*************************************************************************/
2355     {
2356         ih264e_ctl_getversioninfo_ip_t s_ctl_set_getversioninfo_ip;
2357         ih264e_ctl_getversioninfo_op_t s_ctl_set_getversioninfo_op;
2358         CHAR ac_version_string[STRLENGTH];
2359         s_ctl_set_getversioninfo_ip.s_ive_ip.e_cmd = IVE_CMD_VIDEO_CTL;
2360         s_ctl_set_getversioninfo_ip.s_ive_ip.e_sub_cmd = IVE_CMD_CTL_GETVERSION;
2361         s_ctl_set_getversioninfo_ip.s_ive_ip.pu1_version = (UWORD8 *)ac_version_string;
2362         s_ctl_set_getversioninfo_ip.s_ive_ip.u4_version_bufsize = sizeof(ac_version_string);
2363         s_ctl_set_getversioninfo_ip.s_ive_ip.u4_size = sizeof(ih264e_ctl_getversioninfo_ip_t);
2364         s_ctl_set_getversioninfo_op.s_ive_op.u4_size = sizeof(ih264e_ctl_getversioninfo_op_t);
2365 
2366         status = ih264e_api_function(ps_enc, (void *) &s_ctl_set_getversioninfo_ip,
2367                 (void *) &s_ctl_set_getversioninfo_op);
2368         if(status != IV_SUCCESS)
2369         {
2370             sprintf(ac_error, "Unable to get codec version = 0x%x\n",
2371                     s_ctl_set_getversioninfo_op.s_ive_op.u4_error_code);
2372             codec_exit(ac_error);
2373         }
2374         printf("CODEC VERSION %s\n", ac_version_string);
2375     }
2376 
2377     /*************************************************************************/
2378     /*                      Get I/O Buffer Requirement                       */
2379     /*************************************************************************/
2380     {
2381         ih264e_ctl_getbufinfo_ip_t s_get_buf_info_ip;
2382         ih264e_ctl_getbufinfo_op_t s_get_buf_info_op;
2383 
2384         s_get_buf_info_ip.s_ive_ip.u4_size = sizeof(ih264e_ctl_getbufinfo_ip_t);
2385         s_get_buf_info_op.s_ive_op.u4_size = sizeof(ih264e_ctl_getbufinfo_op_t);
2386 
2387         s_get_buf_info_ip.s_ive_ip.e_cmd = IVE_CMD_VIDEO_CTL;
2388         s_get_buf_info_ip.s_ive_ip.e_sub_cmd = IVE_CMD_CTL_GETBUFINFO;
2389         s_get_buf_info_ip.s_ive_ip.u4_max_ht = s_app_ctxt.u4_max_ht;
2390         s_get_buf_info_ip.s_ive_ip.u4_max_wd = s_app_ctxt.u4_max_wd;
2391         s_get_buf_info_ip.s_ive_ip.e_inp_color_fmt = s_app_ctxt.e_inp_color_fmt;
2392 
2393         status = ih264e_api_function(ps_enc, &s_get_buf_info_ip, &s_get_buf_info_op);
2394 
2395         if (status != IV_SUCCESS)
2396         {
2397             sprintf(ac_error, "Unable to get I/O buffer requirements = 0x%x\n",
2398                     s_get_buf_info_op.s_ive_op.u4_error_code);
2399             codec_exit(ac_error);
2400         }
2401         s_app_ctxt.s_get_buf_info_op = s_get_buf_info_op;
2402     }
2403 
2404     /*****************************************************************************/
2405     /* Add the following initializations based on the parameters in context      */
2406     /*****************************************************************************/
2407 
2408 
2409     /*****************************************************************************/
2410     /*   Video control  Set Frame dimensions                                     */
2411     /*****************************************************************************/
2412     s_app_ctxt.u4_strd = s_app_ctxt.u4_wd;
2413     set_dimensions(&s_app_ctxt, 0, 0);
2414 
2415     /*****************************************************************************/
2416     /*   Video control  Set Frame rates                                          */
2417     /*****************************************************************************/
2418     set_frame_rate(&s_app_ctxt, 0, 0);
2419 
2420     /*****************************************************************************/
2421     /*   Video control  Set IPE Params                                           */
2422     /*****************************************************************************/
2423     set_ipe_params(&s_app_ctxt, 0, 0);
2424 
2425     /*****************************************************************************/
2426     /*   Video control  Set Bitrate                                              */
2427     /*****************************************************************************/
2428     set_bit_rate(&s_app_ctxt, 0, 0);
2429 
2430     /*****************************************************************************/
2431     /*   Video control  Set QP                                                   */
2432     /*****************************************************************************/
2433     set_qp(&s_app_ctxt,0,0);
2434 
2435     /*****************************************************************************/
2436     /*   Video control  Set AIR params                                           */
2437     /*****************************************************************************/
2438     set_air_params(&s_app_ctxt,0,0);
2439 
2440     /*****************************************************************************/
2441     /*   Video control  Set VBV params                                           */
2442     /*****************************************************************************/
2443     set_vbv_params(&s_app_ctxt,0,0);
2444 
2445     /*****************************************************************************/
2446     /*   Video control  Set Motion estimation params                             */
2447     /*****************************************************************************/
2448     set_me_params(&s_app_ctxt,0,0);
2449 
2450     /*****************************************************************************/
2451     /*   Video control  Set GOP params                                           */
2452     /*****************************************************************************/
2453     set_gop_params(&s_app_ctxt, 0, 0);
2454 
2455     /*****************************************************************************/
2456     /*   Video control  Set Deblock params                                       */
2457     /*****************************************************************************/
2458     set_deblock_params(&s_app_ctxt, 0, 0);
2459 
2460     /*****************************************************************************/
2461     /*   Video control  Set Profile params                                       */
2462     /*****************************************************************************/
2463     set_profile_params(&s_app_ctxt, 0, 0);
2464 
2465     /*****************************************************************************/
2466     /*   Video control Set in Encode header mode                                 */
2467     /*****************************************************************************/
2468     set_enc_mode(&s_app_ctxt, 0, 0, IVE_ENC_MODE_PICTURE);
2469 
2470 #ifdef IOS
2471     /* Correct file paths */
2472     sprintf(filename_with_path, "%s/%s", documentdir, s_app_ctxt.ac_ip_fname);
2473     strcpy (s_app_ctxt.ac_ip_fname, filename_with_path);
2474 
2475     sprintf(filename_with_path, "%s/%s", documentdir, s_app_ctxt.ac_op_fname);
2476     strcpy (s_app_ctxt.ac_op_fname, filename_with_path);
2477 
2478     sprintf(filename_with_path, "%s/%s", documentdir, s_app_ctxt.ac_recon_fname);
2479     strcpy (s_app_ctxt.ac_recon_fname, filename_with_path);
2480 
2481     sprintf(filename_with_path, "%s/%s", documentdir, s_app_ctxt.ac_chksum_fname);
2482     strcpy (s_app_ctxt.ac_chksum_fname, filename_with_path);
2483 
2484     sprintf(filename_with_path, "%s/%s", documentdir, s_app_ctxt.ac_mb_info_fname);
2485     strcpy (s_app_ctxt.ac_mb_info_fname, filename_with_path);
2486 
2487     sprintf(filename_with_path, "%s/%s", documentdir, s_app_ctxt.ac_pic_info_fname);
2488     strcpy (s_app_ctxt.ac_pic_info_fname, filename_with_path);
2489 #endif
2490 
2491     /*************************************************************************/
2492     /*               begin encoding                                          */
2493     /*************************************************************************/
2494 
2495     synchronous_encode(ps_enc, &s_app_ctxt);
2496 
2497     {
2498         DOUBLE bytes_per_frame;
2499         DOUBLE bytes_per_second;
2500         WORD32 achieved_bitrate;
2501 
2502         if(s_app_ctxt.u4_pics_cnt != 0)
2503         {
2504             bytes_per_frame = (s_app_ctxt.u4_total_bytes) / (s_app_ctxt.u4_pics_cnt);
2505         }
2506         else
2507         {
2508             bytes_per_frame = 0;
2509         }
2510         bytes_per_second = (bytes_per_frame * s_app_ctxt.u4_tgt_frame_rate);
2511 
2512         achieved_bitrate = bytes_per_second * 8;
2513 
2514         printf("\nEncoding Completed\n");
2515         printf("Summary\n");
2516         printf("Input filename                  : %s\n", s_app_ctxt.ac_ip_fname);
2517         printf("Output filename                 : %s\n", s_app_ctxt.ac_op_fname);
2518         printf("Output Width                    : %-4d\n", s_app_ctxt.u4_wd);
2519         printf("Output Height                   : %-4d\n", s_app_ctxt.u4_ht);
2520         printf("Target Bitrate (bps)            : %-4d\n", s_app_ctxt.u4_bitrate);
2521         printf("Achieved Bitrate (bps)          : %-4d\n", achieved_bitrate);
2522         printf("Average Time per Frame          : %-4d\n", s_app_ctxt.avg_time);
2523         printf("Achieved FPS                    : %-4.2f\n", 1000000.0 / s_app_ctxt.avg_time);
2524     }
2525 
2526 
2527     /*************************************************************************/
2528     /*                         Close Codec Instance                         */
2529     /*************************************************************************/
2530     {
2531         ih264e_retrieve_mem_rec_ip_t s_retrieve_mem_ip;
2532         ih264e_retrieve_mem_rec_op_t s_retrieve_mem_op;
2533         iv_mem_rec_t *ps_mem_rec;
2534         s_retrieve_mem_ip.s_ive_ip.u4_size =
2535                         sizeof(ih264e_retrieve_mem_rec_ip_t);
2536         s_retrieve_mem_op.s_ive_op.u4_size =
2537                         sizeof(ih264e_retrieve_mem_rec_op_t);
2538 
2539         s_retrieve_mem_ip.s_ive_ip.e_cmd = IV_CMD_RETRIEVE_MEMREC;
2540         s_retrieve_mem_ip.s_ive_ip.ps_mem_rec = s_app_ctxt.ps_mem_rec;
2541 
2542         status = ih264e_api_function(ps_enc, &s_retrieve_mem_ip,
2543                                           &s_retrieve_mem_op);
2544 
2545         if(status != IV_SUCCESS)
2546         {
2547             sprintf(ac_error, "Unable to retrieve memory records = 0x%x\n",
2548                     s_retrieve_mem_op.s_ive_op.u4_error_code);
2549             codec_exit(ac_error);
2550         }
2551 
2552         /* Free memory records */
2553         ps_mem_rec = s_app_ctxt.ps_mem_rec;
2554         for(i = 0; i < num_mem_recs; i++)
2555         {
2556             ih264a_aligned_free(ps_mem_rec->pv_base);
2557             ps_mem_rec++;
2558         }
2559 
2560         free(s_app_ctxt.ps_mem_rec);
2561 
2562     }
2563 
2564     return 0;
2565 }
2566 
2567 
2568 #ifdef  ANDROID_NDK
raise(int a)2569 int raise(int a)
2570 {
2571     printf("Divide by zero\n");
2572     return 0;
2573 }
__aeabi_assert(const char * assertion,const char * file,unsigned int line)2574 void __aeabi_assert(const char *assertion, const char *file, unsigned int line)
2575 {
2576     return;
2577 }
2578 #endif
2579