main.c revision b0aadd0c45cb959d102cbe39ab870e4e3292ce97
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
51void 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/*****************************************************************************/
59typedef 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
130typedef struct
131{
132    CHAR        argument_shortname[8];
133    CHAR        argument_name[128];
134    ARGUMENT_T  argument;
135    CHAR        description[512];
136} argument_t;
137
138static 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
250void * ih264a_aligned_malloc(WORD32 alignment, WORD32 size)
251{
252    return _aligned_malloc(size, alignment);
253}
254
255void ih264a_aligned_free(void *pv_buf)
256{
257    _aligned_free(pv_buf);
258    return;
259}
260
261#elif IOS
262
263void * ih264a_aligned_malloc(WORD32 alignment, WORD32 size)
264{
265    return malloc(size);
266}
267
268void ih264a_aligned_free(void *pv_buf)
269{
270    free(pv_buf);
271    return;
272}
273
274#else
275
276void * ih264a_aligned_malloc(WORD32 alignment, WORD32 size)
277{
278    return memalign(alignment, size);
279}
280
281void 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/*****************************************************************************/
308void 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/*****************************************************************************/
333IV_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/*****************************************************************************/
375IVE_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
421void 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/*****************************************************************************/
457ARGUMENT_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/*****************************************************************************/
497void 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/*****************************************************************************/
834void 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
855void invalid_argument_exit(CHAR *pc_err_message)
856{
857    print_usage();
858    codec_exit(pc_err_message);
859}
860
861void 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
907void 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->u4_total_bytes          = 0;
922    ps_app_ctxt->u4_pics_cnt             = 0;
923    ps_app_ctxt->e_inp_color_fmt         = DEFAULT_INP_COLOR_FMT;
924    ps_app_ctxt->e_recon_color_fmt       = DEFAULT_RECON_COLOR_FMT;
925    ps_app_ctxt->e_arch                  = ARCH_ARM_A9Q;
926    ps_app_ctxt->e_soc                   = SOC_GENERIC;
927    ps_app_ctxt->header_generated        = 0;
928    ps_app_ctxt->pv_codec_obj            = NULL;
929    ps_app_ctxt->u4_num_cores            = DEFAULT_NUM_CORES;
930    ps_app_ctxt->u4_pre_enc_me           = 0;
931    ps_app_ctxt->u4_pre_enc_ipe          = 0;
932    ps_app_ctxt->ac_ip_fname[0]          = '\0';
933    ps_app_ctxt->ac_op_fname[0]          = '\0';
934    ps_app_ctxt->ac_recon_fname[0]       = '\0';
935    ps_app_ctxt->ac_chksum_fname[0]      = '\0';
936    ps_app_ctxt->ac_mb_info_fname[0]     = '\0';
937    ps_app_ctxt->fp_ip                   = NULL;
938    ps_app_ctxt->fp_op                   = NULL;
939    ps_app_ctxt->fp_recon                = NULL;
940    ps_app_ctxt->fp_chksum               = NULL;
941    ps_app_ctxt->fp_psnr_ip              = NULL;
942    ps_app_ctxt->fp_mb_info              = NULL;
943    ps_app_ctxt->fp_pic_info             = NULL;
944    ps_app_ctxt->u4_loopback             = DEFAULT_LOOPBACK;
945    ps_app_ctxt->u4_max_frame_rate       = DEFAULT_MAX_FRAMERATE;
946    ps_app_ctxt->u4_src_frame_rate       = DEFAULT_SRC_FRAME_RATE;
947    ps_app_ctxt->u4_tgt_frame_rate       = DEFAULT_TGT_FRAME_RATE;
948    ps_app_ctxt->u4_max_wd               = DEFAULT_MAX_WD;
949    ps_app_ctxt->u4_max_ht               = DEFAULT_MAX_HT;
950    ps_app_ctxt->u4_max_level            = DEFAULT_MAX_LEVEL;
951    ps_app_ctxt->u4_strd                 = DEFAULT_STRIDE;
952    ps_app_ctxt->u4_wd                   = DEFAULT_WD;
953    ps_app_ctxt->u4_ht                   = DEFAULT_HT;
954    ps_app_ctxt->u4_psnr_enable          = DEFAULT_PSNR_ENABLE;
955    ps_app_ctxt->u4_enc_speed            = IVE_FASTEST;
956    ps_app_ctxt->u4_me_speed             = DEFAULT_ME_SPEED;
957    ps_app_ctxt->u4_enable_fast_sad      = DEFAULT_ENABLE_FAST_SAD;
958    ps_app_ctxt->u4_enable_alt_ref       = DEFAULT_ENABLE_ALT_REF;
959    ps_app_ctxt->u4_rc                   = DEFAULT_RC;
960    ps_app_ctxt->u4_max_bitrate          = DEFAULT_MAX_BITRATE;
961    ps_app_ctxt->u4_num_bframes          = DEFAULT_NUM_BFRAMES;
962    ps_app_ctxt->u4_bitrate              = DEFAULT_BITRATE;
963    ps_app_ctxt->u4_i_qp                 = DEFAULT_I_QP;
964    ps_app_ctxt->u4_p_qp                 = DEFAULT_P_QP;
965    ps_app_ctxt->u4_b_qp                 = DEFAULT_B_QP;
966    ps_app_ctxt->u4_i_qp_min             = DEFAULT_QP_MIN;
967    ps_app_ctxt->u4_i_qp_max             = DEFAULT_QP_MAX;
968    ps_app_ctxt->u4_p_qp_min             = DEFAULT_QP_MIN;
969    ps_app_ctxt->u4_p_qp_max             = DEFAULT_QP_MAX;
970    ps_app_ctxt->u4_b_qp_min             = DEFAULT_QP_MIN;
971    ps_app_ctxt->u4_b_qp_max             = DEFAULT_QP_MAX;
972    ps_app_ctxt->u4_air                  = DEFAULT_AIR;
973    ps_app_ctxt->u4_air_refresh_period   = DEFAULT_AIR_REFRESH_PERIOD;
974    ps_app_ctxt->u4_srch_rng_x           = DEFAULT_SRCH_RNG_X;
975    ps_app_ctxt->u4_srch_rng_y           = DEFAULT_SRCH_RNG_Y;
976    ps_app_ctxt->u4_i_interval           = DEFAULT_I_INTERVAL;
977    ps_app_ctxt->u4_idr_interval         = DEFAULT_IDR_INTERVAL;
978    ps_app_ctxt->u4_disable_deblk_level  = DEFAULT_DISABLE_DEBLK_LEVEL;
979    ps_app_ctxt->u4_hpel                 = DEFAULT_HPEL;
980    ps_app_ctxt->u4_qpel                 = DEFAULT_QPEL;
981    ps_app_ctxt->u4_enable_intra_4x4     = DEFAULT_I4;
982    ps_app_ctxt->e_profile               = DEFAULT_EPROFILE;
983    ps_app_ctxt->u4_slice_mode           = DEFAULT_SLICE_MODE;
984    ps_app_ctxt->u4_slice_param          = DEFAULT_SLICE_PARAM;
985    ps_app_ctxt->pv_input_thread_handle  = NULL;
986    ps_app_ctxt->pv_output_thread_handle = NULL;
987    ps_app_ctxt->pv_recon_thread_handle  = NULL;
988    ps_app_ctxt->u4_vbv_buf_size         = 0;
989    ps_app_ctxt->u4_vbv_buffer_delay     = 1000;
990    ps_app_ctxt->adbl_psnr[0]            = 0.0;
991    ps_app_ctxt->adbl_psnr[1]            = 0.0;
992    ps_app_ctxt->adbl_psnr[2]            = 0.0;
993    ps_app_ctxt->u4_psnr_cnt             = 0;
994    ps_app_ctxt->pu1_psnr_buf            = NULL;
995    ps_app_ctxt->u4_psnr_buf_size        = 0;
996    ps_app_ctxt->u4_entropy_coding_mode  = DEFAULT_ENTROPY_CODING_MODE;
997
998    return;
999}
1000
1001void set_dimensions(app_ctxt_t *ps_app_ctxt,
1002                    UWORD32 u4_timestamp_low,
1003                    UWORD32 u4_timestamp_high)
1004{
1005    ih264e_ctl_set_dimensions_ip_t s_frame_dimensions_ip;
1006    ih264e_ctl_set_dimensions_op_t s_frame_dimensions_op;
1007    IV_STATUS_T status;
1008
1009    s_frame_dimensions_ip.s_ive_ip.e_cmd = IVE_CMD_VIDEO_CTL;
1010    s_frame_dimensions_ip.s_ive_ip.e_sub_cmd = IVE_CMD_CTL_SET_DIMENSIONS;
1011
1012    s_frame_dimensions_ip.s_ive_ip.u4_ht = ps_app_ctxt->u4_ht;
1013    s_frame_dimensions_ip.s_ive_ip.u4_wd = ps_app_ctxt->u4_wd;
1014
1015    s_frame_dimensions_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1016    s_frame_dimensions_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1017
1018    s_frame_dimensions_ip.s_ive_ip.u4_size =
1019                    sizeof(ih264e_ctl_set_dimensions_ip_t);
1020    s_frame_dimensions_op.s_ive_op.u4_size =
1021                    sizeof(ih264e_ctl_set_dimensions_op_t);
1022
1023    status = ih264e_api_function(ps_app_ctxt->ps_enc,
1024                                 &s_frame_dimensions_ip,
1025                                 &s_frame_dimensions_op);
1026    if(status != IV_SUCCESS)
1027    {
1028        CHAR ac_error[STRLENGTH];
1029        sprintf(ac_error, "Unable to set frame dimensions = 0x%x\n",
1030                s_frame_dimensions_op.s_ive_op.u4_error_code);
1031        codec_exit(ac_error);
1032    }
1033    return;
1034}
1035
1036void set_frame_rate(app_ctxt_t *ps_app_ctxt,
1037                    UWORD32 u4_timestamp_low,
1038                    UWORD32 u4_timestamp_high)
1039{
1040    ih264e_ctl_set_frame_rate_ip_t s_frame_rate_ip;
1041    ih264e_ctl_set_frame_rate_op_t s_frame_rate_op;
1042    IV_STATUS_T status;
1043
1044    s_frame_rate_ip.s_ive_ip.e_cmd  =   IVE_CMD_VIDEO_CTL;
1045    s_frame_rate_ip.s_ive_ip.e_sub_cmd  =   IVE_CMD_CTL_SET_FRAMERATE;
1046
1047    s_frame_rate_ip.s_ive_ip.u4_src_frame_rate  =
1048                    ps_app_ctxt->u4_src_frame_rate;
1049    s_frame_rate_ip.s_ive_ip.u4_tgt_frame_rate  =
1050                    ps_app_ctxt->u4_tgt_frame_rate;
1051
1052    s_frame_rate_ip.s_ive_ip.u4_timestamp_high  =   u4_timestamp_high;
1053    s_frame_rate_ip.s_ive_ip.u4_timestamp_low   =   u4_timestamp_low;
1054
1055    s_frame_rate_ip.s_ive_ip.u4_size    =   sizeof(ih264e_ctl_set_frame_rate_ip_t);
1056    s_frame_rate_op.s_ive_op.u4_size    =   sizeof(ih264e_ctl_set_frame_rate_op_t);
1057
1058    status = ih264e_api_function(ps_app_ctxt->ps_enc,&s_frame_rate_ip,&s_frame_rate_op);
1059    if(status != IV_SUCCESS)
1060       {
1061           CHAR ac_error[STRLENGTH];
1062           sprintf(ac_error, "Unable to set frame rate = 0x%x\n",
1063                   s_frame_rate_op.s_ive_op.u4_error_code);
1064           codec_exit(ac_error);
1065       }
1066       return;
1067}
1068
1069
1070void set_ipe_params(app_ctxt_t *ps_app_ctxt,
1071                    UWORD32 u4_timestamp_low,
1072                    UWORD32 u4_timestamp_high)
1073{
1074    ih264e_ctl_set_ipe_params_ip_t s_ipe_params_ip;
1075    ih264e_ctl_set_ipe_params_op_t s_ipe_params_op;
1076    IV_STATUS_T status;
1077
1078    s_ipe_params_ip.s_ive_ip.e_cmd  =   IVE_CMD_VIDEO_CTL;
1079    s_ipe_params_ip.s_ive_ip.e_sub_cmd  =   IVE_CMD_CTL_SET_IPE_PARAMS;
1080
1081    s_ipe_params_ip.s_ive_ip.u4_enable_intra_4x4  = ps_app_ctxt->u4_enable_intra_4x4;
1082    s_ipe_params_ip.s_ive_ip.u4_enc_speed_preset  = ps_app_ctxt->u4_enc_speed;
1083
1084    s_ipe_params_ip.s_ive_ip.u4_timestamp_high  =   u4_timestamp_high;
1085    s_ipe_params_ip.s_ive_ip.u4_timestamp_low   =   u4_timestamp_low;
1086
1087    s_ipe_params_ip.s_ive_ip.u4_size    =   sizeof(ih264e_ctl_set_ipe_params_ip_t);
1088    s_ipe_params_op.s_ive_op.u4_size    =   sizeof(ih264e_ctl_set_ipe_params_op_t);
1089
1090    status = ih264e_api_function(ps_app_ctxt->ps_enc,&s_ipe_params_ip,&s_ipe_params_op);
1091    if(status != IV_SUCCESS)
1092    {
1093        CHAR ac_error[STRLENGTH];
1094        sprintf(ac_error, "Unable to set ipe params = 0x%x\n",
1095                s_ipe_params_op.s_ive_op.u4_error_code);
1096        codec_exit(ac_error);
1097    }
1098    return;
1099}
1100
1101void set_bit_rate(app_ctxt_t *ps_app_ctxt,
1102                  UWORD32 u4_timestamp_low, UWORD32 u4_timestamp_high)
1103{
1104    ih264e_ctl_set_bitrate_ip_t s_bitrate_ip;
1105    ih264e_ctl_set_bitrate_op_t s_bitrate_op;
1106    IV_STATUS_T status;
1107
1108    s_bitrate_ip.s_ive_ip.e_cmd  =   IVE_CMD_VIDEO_CTL;
1109    s_bitrate_ip.s_ive_ip.e_sub_cmd  =   IVE_CMD_CTL_SET_BITRATE;
1110
1111    s_bitrate_ip.s_ive_ip.u4_target_bitrate  =   ps_app_ctxt->u4_bitrate;
1112
1113    s_bitrate_ip.s_ive_ip.u4_timestamp_high  =   u4_timestamp_high;
1114    s_bitrate_ip.s_ive_ip.u4_timestamp_low   =   u4_timestamp_low;
1115
1116    s_bitrate_ip.s_ive_ip.u4_size    =   sizeof(ih264e_ctl_set_bitrate_ip_t);
1117    s_bitrate_op.s_ive_op.u4_size    =   sizeof(ih264e_ctl_set_bitrate_op_t);
1118
1119    status = ih264e_api_function(ps_app_ctxt->ps_enc,&s_bitrate_ip,&s_bitrate_op);
1120    if(status != IV_SUCCESS)
1121       {
1122           CHAR ac_error[STRLENGTH];
1123           sprintf(ac_error, "Unable to set bit rate = 0x%x\n",
1124                   s_bitrate_op.s_ive_op.u4_error_code);
1125           codec_exit(ac_error);
1126       }
1127       return;
1128}
1129
1130
1131void set_frame_type(app_ctxt_t *ps_app_ctxt,
1132                    UWORD32 u4_timestamp_low,
1133                    UWORD32 u4_timestamp_high,
1134                    IV_PICTURE_CODING_TYPE_T  e_frame_type)
1135{
1136    ih264e_ctl_set_frame_type_ip_t s_frame_type_ip;
1137    ih264e_ctl_set_frame_type_op_t s_frame_type_op;
1138    IV_STATUS_T status;
1139
1140    s_frame_type_ip.s_ive_ip.e_cmd  =   IVE_CMD_VIDEO_CTL;
1141    s_frame_type_ip.s_ive_ip.e_sub_cmd  =   IVE_CMD_CTL_SET_FRAMETYPE;
1142
1143    s_frame_type_ip.s_ive_ip.e_frame_type  =   e_frame_type;
1144
1145    s_frame_type_ip.s_ive_ip.u4_timestamp_high  =   u4_timestamp_high;
1146    s_frame_type_ip.s_ive_ip.u4_timestamp_low   =   u4_timestamp_low;
1147
1148    s_frame_type_ip.s_ive_ip.u4_size    =   sizeof(ih264e_ctl_set_frame_type_ip_t);
1149    s_frame_type_op.s_ive_op.u4_size    =   sizeof(ih264e_ctl_set_frame_type_op_t);
1150
1151    status = ih264e_api_function(ps_app_ctxt->ps_enc,&s_frame_type_ip,&s_frame_type_op);
1152    if(status != IV_SUCCESS)
1153    {
1154        CHAR ac_error[STRLENGTH];
1155        sprintf(ac_error, "Unable to set frame type = 0x%x\n",
1156                s_frame_type_op.s_ive_op.u4_error_code);
1157        codec_exit(ac_error);
1158    }
1159    return;
1160}
1161
1162void set_qp(app_ctxt_t *ps_app_ctxt,
1163            UWORD32 u4_timestamp_low, UWORD32 u4_timestamp_high)
1164{
1165    ih264e_ctl_set_qp_ip_t s_qp_ip;
1166    ih264e_ctl_set_qp_op_t s_qp_op;
1167    IV_STATUS_T status;
1168
1169    s_qp_ip.s_ive_ip.e_cmd  =   IVE_CMD_VIDEO_CTL;
1170    s_qp_ip.s_ive_ip.e_sub_cmd  =   IVE_CMD_CTL_SET_QP;
1171
1172    s_qp_ip.s_ive_ip.u4_i_qp = ps_app_ctxt->u4_i_qp;
1173    s_qp_ip.s_ive_ip.u4_i_qp_max = ps_app_ctxt->u4_i_qp_max;
1174    s_qp_ip.s_ive_ip.u4_i_qp_min = ps_app_ctxt->u4_i_qp_min;
1175
1176    s_qp_ip.s_ive_ip.u4_p_qp = ps_app_ctxt->u4_p_qp;
1177    s_qp_ip.s_ive_ip.u4_p_qp_max = ps_app_ctxt->u4_p_qp_max;
1178    s_qp_ip.s_ive_ip.u4_p_qp_min = ps_app_ctxt->u4_p_qp_min;
1179
1180    s_qp_ip.s_ive_ip.u4_b_qp = ps_app_ctxt->u4_b_qp;
1181    s_qp_ip.s_ive_ip.u4_b_qp_max = ps_app_ctxt->u4_b_qp_max;
1182    s_qp_ip.s_ive_ip.u4_b_qp_min = ps_app_ctxt->u4_b_qp_min;
1183
1184    s_qp_ip.s_ive_ip.u4_timestamp_high  =   u4_timestamp_high;
1185    s_qp_ip.s_ive_ip.u4_timestamp_low   =   u4_timestamp_low;
1186
1187    s_qp_ip.s_ive_ip.u4_size    =   sizeof(ih264e_ctl_set_qp_ip_t);
1188    s_qp_op.s_ive_op.u4_size    =   sizeof(ih264e_ctl_set_qp_op_t);
1189
1190    status = ih264e_api_function(ps_app_ctxt->ps_enc,&s_qp_ip,&s_qp_op);
1191    if(status != IV_SUCCESS)
1192       {
1193           CHAR ac_error[STRLENGTH];
1194           sprintf(ac_error, "Unable to set qp 0x%x\n",
1195                   s_qp_op.s_ive_op.u4_error_code);
1196           codec_exit(ac_error);
1197       }
1198       return;
1199}
1200
1201void set_enc_mode(app_ctxt_t *ps_app_ctxt,
1202                  UWORD32 u4_timestamp_low, UWORD32 u4_timestamp_high,
1203                  IVE_ENC_MODE_T e_enc_mode)
1204{
1205    IV_STATUS_T status;
1206
1207    ih264e_ctl_set_enc_mode_ip_t s_enc_mode_ip;
1208    ih264e_ctl_set_enc_mode_op_t s_enc_mode_op;
1209
1210    s_enc_mode_ip.s_ive_ip.e_cmd = IVE_CMD_VIDEO_CTL;
1211    s_enc_mode_ip.s_ive_ip.e_sub_cmd = IVE_CMD_CTL_SET_ENC_MODE;
1212
1213    s_enc_mode_ip.s_ive_ip.e_enc_mode = e_enc_mode;
1214
1215    s_enc_mode_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1216    s_enc_mode_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1217
1218    s_enc_mode_ip.s_ive_ip.u4_size = sizeof(ih264e_ctl_set_enc_mode_ip_t);
1219    s_enc_mode_op.s_ive_op.u4_size = sizeof(ih264e_ctl_set_enc_mode_op_t);
1220
1221    status = ih264e_api_function(ps_app_ctxt->ps_enc, &s_enc_mode_ip,
1222                                      &s_enc_mode_op);
1223    if(status != IV_SUCCESS)
1224    {
1225        CHAR ac_error[STRLENGTH];
1226        sprintf(ac_error, "Unable to set in header encode mode = 0x%x\n",
1227                s_enc_mode_op.s_ive_op.u4_error_code);
1228        codec_exit(ac_error);
1229    }
1230    return;
1231}
1232
1233
1234void set_vbv_params(app_ctxt_t *ps_app_ctxt,
1235                    UWORD32 u4_timestamp_low,
1236                    UWORD32 u4_timestamp_high)
1237{
1238    ih264e_ctl_set_vbv_params_ip_t s_vbv_ip;
1239    ih264e_ctl_set_vbv_params_op_t s_vbv_op;
1240    IV_STATUS_T status;
1241
1242    s_vbv_ip.s_ive_ip.e_cmd  =   IVE_CMD_VIDEO_CTL;
1243    s_vbv_ip.s_ive_ip.e_sub_cmd  =   IVE_CMD_CTL_SET_VBV_PARAMS;
1244
1245    s_vbv_ip.s_ive_ip.u4_vbv_buf_size = ps_app_ctxt->u4_vbv_buf_size;
1246    s_vbv_ip.s_ive_ip.u4_vbv_buffer_delay  =
1247                    ps_app_ctxt->u4_vbv_buffer_delay;
1248
1249    s_vbv_ip.s_ive_ip.u4_timestamp_high  =   u4_timestamp_high;
1250    s_vbv_ip.s_ive_ip.u4_timestamp_low   =   u4_timestamp_low;
1251
1252    s_vbv_ip.s_ive_ip.u4_size    =   sizeof(ih264e_ctl_set_vbv_params_ip_t);
1253    s_vbv_op.s_ive_op.u4_size    =   sizeof(ih264e_ctl_set_vbv_params_op_t);
1254
1255    status = ih264e_api_function(ps_app_ctxt->ps_enc,&s_vbv_ip,&s_vbv_op);
1256    if(status != IV_SUCCESS)
1257    {
1258        CHAR ac_error[STRLENGTH];
1259        sprintf(ac_error, "Unable to set VBC params = 0x%x\n",
1260                s_vbv_op.s_ive_op.u4_error_code);
1261        codec_exit(ac_error);
1262    }
1263    return;
1264}
1265
1266void set_air_params(app_ctxt_t *ps_app_ctxt,
1267                    UWORD32 u4_timestamp_low,
1268                    UWORD32 u4_timestamp_high)
1269{
1270    ih264e_ctl_set_air_params_ip_t s_air_ip;
1271    ih264e_ctl_set_air_params_op_t s_air_op;
1272    IV_STATUS_T status;
1273
1274    s_air_ip.s_ive_ip.e_cmd  =   IVE_CMD_VIDEO_CTL;
1275    s_air_ip.s_ive_ip.e_sub_cmd  =   IVE_CMD_CTL_SET_AIR_PARAMS;
1276
1277    s_air_ip.s_ive_ip.e_air_mode = ps_app_ctxt->u4_air;
1278    s_air_ip.s_ive_ip.u4_air_refresh_period = ps_app_ctxt->u4_air_refresh_period;
1279
1280    s_air_ip.s_ive_ip.u4_timestamp_high  =   u4_timestamp_high;
1281    s_air_ip.s_ive_ip.u4_timestamp_low   =   u4_timestamp_low;
1282
1283    s_air_ip.s_ive_ip.u4_size    =   sizeof(ih264e_ctl_set_air_params_ip_t);
1284    s_air_op.s_ive_op.u4_size    =   sizeof(ih264e_ctl_set_air_params_op_t);
1285
1286    status = ih264e_api_function(ps_app_ctxt->ps_enc,&s_air_ip,&s_air_op);
1287    if(status != IV_SUCCESS)
1288       {
1289           CHAR ac_error[STRLENGTH];
1290           sprintf(ac_error, "Unable to set air params = 0x%x\n",
1291                   s_air_op.s_ive_op.u4_error_code);
1292           codec_exit(ac_error);
1293       }
1294       return;
1295}
1296
1297void set_me_params(app_ctxt_t *ps_app_ctxt,
1298                    UWORD32 u4_timestamp_low,
1299                    UWORD32 u4_timestamp_high)
1300{
1301    IV_STATUS_T status;
1302
1303    ih264e_ctl_set_me_params_ip_t s_me_params_ip;
1304    ih264e_ctl_set_me_params_op_t s_me_params_op;
1305
1306    s_me_params_ip.s_ive_ip.e_cmd = IVE_CMD_VIDEO_CTL;
1307    s_me_params_ip.s_ive_ip.e_sub_cmd = IVE_CMD_CTL_SET_ME_PARAMS;
1308
1309    s_me_params_ip.s_ive_ip.u4_enable_fast_sad = ps_app_ctxt->u4_enable_fast_sad;
1310    s_me_params_ip.s_ive_ip.u4_enable_alt_ref = ps_app_ctxt->u4_enable_alt_ref;
1311
1312    s_me_params_ip.s_ive_ip.u4_enable_hpel  =   ps_app_ctxt->u4_hpel;
1313    s_me_params_ip.s_ive_ip.u4_enable_qpel  =   ps_app_ctxt->u4_qpel;
1314    s_me_params_ip.s_ive_ip.u4_me_speed_preset  =   ps_app_ctxt->u4_me_speed;
1315    s_me_params_ip.s_ive_ip.u4_srch_rng_x   =   ps_app_ctxt->u4_srch_rng_x;
1316    s_me_params_ip.s_ive_ip.u4_srch_rng_y   =   ps_app_ctxt->u4_srch_rng_y;
1317
1318    s_me_params_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1319    s_me_params_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1320
1321    s_me_params_ip.s_ive_ip.u4_size = sizeof(ih264e_ctl_set_me_params_ip_t);
1322    s_me_params_op.s_ive_op.u4_size = sizeof(ih264e_ctl_set_me_params_op_t);
1323
1324    status = ih264e_api_function(ps_app_ctxt->ps_enc, &s_me_params_ip,
1325                                 &s_me_params_op);
1326    if(status != IV_SUCCESS)
1327    {
1328        CHAR ac_error[STRLENGTH];
1329        sprintf(ac_error, "Unable to set me params = 0x%x\n",
1330                s_me_params_op.s_ive_op.u4_error_code);
1331        codec_exit(ac_error);
1332    }
1333    return;
1334}
1335
1336
1337void set_gop_params(app_ctxt_t *ps_app_ctxt,
1338                    UWORD32 u4_timestamp_low,
1339                    UWORD32 u4_timestamp_high)
1340{
1341    IV_STATUS_T status;
1342
1343    ih264e_ctl_set_gop_params_ip_t s_gop_params_ip;
1344    ih264e_ctl_set_gop_params_op_t s_gop_params_op;
1345
1346    s_gop_params_ip.s_ive_ip.e_cmd = IVE_CMD_VIDEO_CTL;
1347    s_gop_params_ip.s_ive_ip.e_sub_cmd = IVE_CMD_CTL_SET_GOP_PARAMS;
1348
1349    s_gop_params_ip.s_ive_ip.u4_i_frm_interval = ps_app_ctxt->u4_i_interval;
1350    s_gop_params_ip.s_ive_ip.u4_idr_frm_interval = ps_app_ctxt->u4_idr_interval;
1351
1352    s_gop_params_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1353    s_gop_params_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1354
1355    s_gop_params_ip.s_ive_ip.u4_size = sizeof(ih264e_ctl_set_gop_params_ip_t);
1356    s_gop_params_op.s_ive_op.u4_size = sizeof(ih264e_ctl_set_gop_params_op_t);
1357
1358    status = ih264e_api_function(ps_app_ctxt->ps_enc, &s_gop_params_ip,
1359                                      &s_gop_params_op);
1360    if(status != IV_SUCCESS)
1361    {
1362        CHAR ac_error[STRLENGTH];
1363        sprintf(ac_error, "Unable to set ME params = 0x%x\n",
1364                s_gop_params_op.s_ive_op.u4_error_code);
1365        codec_exit(ac_error);
1366    }
1367    return;
1368}
1369
1370void set_profile_params(app_ctxt_t *ps_app_ctxt,
1371                    UWORD32 u4_timestamp_low,
1372                    UWORD32 u4_timestamp_high)
1373{
1374    IV_STATUS_T status;
1375
1376    ih264e_ctl_set_profile_params_ip_t s_profile_params_ip;
1377    ih264e_ctl_set_profile_params_op_t s_profile_params_op;
1378
1379    s_profile_params_ip.s_ive_ip.e_cmd = IVE_CMD_VIDEO_CTL;
1380    s_profile_params_ip.s_ive_ip.e_sub_cmd = IVE_CMD_CTL_SET_PROFILE_PARAMS;
1381
1382    s_profile_params_ip.s_ive_ip.e_profile = ps_app_ctxt->e_profile;
1383
1384    s_profile_params_ip.s_ive_ip.u4_entropy_coding_mode = ps_app_ctxt->u4_entropy_coding_mode;
1385
1386    s_profile_params_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1387    s_profile_params_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1388
1389    s_profile_params_ip.s_ive_ip.u4_size = sizeof(ih264e_ctl_set_profile_params_ip_t);
1390    s_profile_params_op.s_ive_op.u4_size = sizeof(ih264e_ctl_set_profile_params_op_t);
1391
1392    status = ih264e_api_function(ps_app_ctxt->ps_enc, &s_profile_params_ip,
1393                                      &s_profile_params_op);
1394    if(status != IV_SUCCESS)
1395    {
1396        CHAR ac_error[STRLENGTH];
1397        sprintf(ac_error, "Unable to set profile params = 0x%x\n",
1398                s_profile_params_op.s_ive_op.u4_error_code);
1399        codec_exit(ac_error);
1400    }
1401    return;
1402}
1403
1404void set_deblock_params(app_ctxt_t *ps_app_ctxt,
1405                    UWORD32 u4_timestamp_low,
1406                    UWORD32 u4_timestamp_high)
1407{
1408    IV_STATUS_T status;
1409
1410    ih264e_ctl_set_deblock_params_ip_t s_deblock_params_ip;
1411    ih264e_ctl_set_deblock_params_op_t s_deblock_params_op;
1412
1413    s_deblock_params_ip.s_ive_ip.e_cmd = IVE_CMD_VIDEO_CTL;
1414    s_deblock_params_ip.s_ive_ip.e_sub_cmd = IVE_CMD_CTL_SET_DEBLOCK_PARAMS;
1415
1416    s_deblock_params_ip.s_ive_ip.u4_disable_deblock_level =
1417                    ps_app_ctxt->u4_disable_deblk_level;
1418
1419    s_deblock_params_ip.s_ive_ip.u4_timestamp_high = u4_timestamp_high;
1420    s_deblock_params_ip.s_ive_ip.u4_timestamp_low = u4_timestamp_low;
1421
1422    s_deblock_params_ip.s_ive_ip.u4_size = sizeof(ih264e_ctl_set_deblock_params_ip_t);
1423    s_deblock_params_op.s_ive_op.u4_size = sizeof(ih264e_ctl_set_deblock_params_op_t);
1424
1425    status = ih264e_api_function(ps_app_ctxt->ps_enc, &s_deblock_params_ip,
1426                                      &s_deblock_params_op);
1427    if(status != IV_SUCCESS)
1428    {
1429        CHAR ac_error[STRLENGTH];
1430        sprintf(ac_error, "Unable to enable/disable deblock params = 0x%x\n",
1431                s_deblock_params_op.s_ive_op.u4_error_code);
1432        codec_exit(ac_error);
1433    }
1434    return;
1435}
1436
1437#define PEAK_WINDOW_SIZE    8
1438
1439void synchronous_encode(iv_obj_t *ps_enc, app_ctxt_t *ps_app_ctxt)
1440{
1441    ih264e_video_encode_ip_t ih264e_video_encode_ip;
1442    ih264e_video_encode_op_t ih264e_video_encode_op;
1443
1444    ive_video_encode_ip_t *ps_video_encode_ip = &ih264e_video_encode_ip.s_ive_ip;
1445    ive_video_encode_op_t *ps_video_encode_op = &ih264e_video_encode_op.s_ive_op;
1446
1447    iv_raw_buf_t *ps_inp_raw_buf = &ps_video_encode_ip->s_inp_buf;
1448
1449    IV_STATUS_T status = IV_SUCCESS;
1450
1451    WORD32 i, is_last = 0, buff_size = 0, num_bytes = 0;
1452    UWORD32 u4_total_time = 0;
1453    UWORD8 *pu1_buf = NULL;
1454    UWORD32 u4_timestamp_low, u4_timestamp_high;
1455    void *pv_mb_info = NULL, *pv_pic_info = NULL;
1456
1457    TIMER curtime ;
1458#ifdef WINDOWS_TIMER
1459    TIMER frequency;
1460#endif
1461    WORD32 peak_window[PEAK_WINDOW_SIZE] = {0};
1462    WORD32 peak_window_idx = 0;
1463    WORD32 peak_avg_max = 0, timetaken = 0;
1464    iv_raw_buf_t s_inp_buf, s_recon_buf;
1465    CHAR ac_error[STRLENGTH];
1466    WORD32 end_of_frames=0;
1467    WORD32 i4_inp_done =0;
1468
1469    u4_timestamp_low = 0;
1470    u4_timestamp_high = 0;
1471
1472    /*************************************************************************/
1473    /*                         Allocate I/O Buffers                          */
1474    /*************************************************************************/
1475    allocate_input(ps_app_ctxt);
1476    allocate_output(ps_app_ctxt);
1477    allocate_recon(ps_app_ctxt);
1478
1479    /* init psnr */
1480    init_psnr(ps_app_ctxt);
1481
1482    /* open file pointers */
1483    ps_app_ctxt->fp_ip = fopen(ps_app_ctxt->ac_ip_fname, "rb");
1484    if(NULL == ps_app_ctxt->fp_ip)
1485    {
1486        sprintf(ac_error, "Unable to open input file for reading: %s", ps_app_ctxt->ac_ip_fname);
1487        invalid_argument_exit(ac_error);
1488    }
1489
1490    ps_app_ctxt->fp_op = fopen(ps_app_ctxt->ac_op_fname, "wb");
1491    if(NULL == ps_app_ctxt->fp_op)
1492    {
1493        sprintf(ac_error, "Unable to open output file for writing: %s", ps_app_ctxt->ac_op_fname);
1494        invalid_argument_exit(ac_error);
1495    }
1496
1497    if(1 == ps_app_ctxt->u4_recon_enable)
1498    {
1499        ps_app_ctxt->fp_recon = fopen(ps_app_ctxt->ac_recon_fname, "wb");
1500        if(NULL == ps_app_ctxt->fp_recon)
1501        {
1502            sprintf(ac_error, "Unable to open recon file for writing: %s", ps_app_ctxt->ac_recon_fname);
1503            invalid_argument_exit(ac_error);
1504        }
1505    }
1506
1507    if(1 == ps_app_ctxt->u4_chksum_enable)
1508    {
1509        ps_app_ctxt->fp_chksum               = fopen(ps_app_ctxt->ac_chksum_fname, "wb");
1510        if(NULL == ps_app_ctxt->fp_chksum)
1511        {
1512            sprintf(ac_error, "Unable to open checksum file for writing: %s", ps_app_ctxt->ac_chksum_fname);
1513            invalid_argument_exit(ac_error);
1514        }
1515    }
1516
1517    /* If PSNR is enabled, open input file again and hold a different file pointer
1518     * This makes it easy to compute PSNR without adding dependency between input and recon threads
1519     */
1520    if(1 == ps_app_ctxt->u4_psnr_enable)
1521    {
1522        ps_app_ctxt->fp_psnr_ip              = fopen(ps_app_ctxt->ac_ip_fname, "rb");
1523        if(NULL == ps_app_ctxt->fp_psnr_ip)
1524        {
1525            sprintf(ac_error, "Unable to open input file for reading: %s", ps_app_ctxt->ac_ip_fname);
1526            invalid_argument_exit(ac_error);
1527        }
1528    }
1529
1530    if(0 != ps_app_ctxt->u4_mb_info_type)
1531    {
1532        ps_app_ctxt->fp_mb_info  = fopen(ps_app_ctxt->ac_mb_info_fname, "rb");
1533        if(NULL == ps_app_ctxt->fp_mb_info)
1534        {
1535            sprintf(ac_error, "Unable to open MB info file for reading: %s", ps_app_ctxt->ac_mb_info_fname);
1536            invalid_argument_exit(ac_error);
1537        }
1538    }
1539    if (ps_app_ctxt->u4_pic_info_type)
1540    {
1541        ps_app_ctxt->fp_pic_info  = fopen(ps_app_ctxt->ac_pic_info_fname, "rb");
1542        if(NULL == ps_app_ctxt->fp_pic_info)
1543        {
1544            sprintf(ac_error, "Unable to open Pic info file for reading: %s", ps_app_ctxt->ac_pic_info_fname);
1545            invalid_argument_exit(ac_error);
1546        }
1547    }
1548
1549    GETTIME(&ps_app_ctxt->enc_start_time);
1550    ps_app_ctxt->enc_last_time = ps_app_ctxt->enc_start_time;
1551
1552    while(1)
1553    {
1554
1555        /******************************************************************************/
1556        /****************** Input Initialization **************************************/
1557        /******************************************************************************/
1558
1559        for(i = 0; i < DEFAULT_MAX_INPUT_BUFS; i++)
1560        {
1561            if(ps_app_ctxt->as_input_buf[i].u4_is_free)
1562            {
1563                pu1_buf = ps_app_ctxt->as_input_buf[i].pu1_buf;
1564                pv_mb_info = ps_app_ctxt->as_input_buf[i].pv_mb_info;
1565                pv_pic_info = ps_app_ctxt->as_input_buf[i].pv_pic_info;
1566                ps_app_ctxt->as_input_buf[i].u4_is_free = 0;
1567                break;
1568            }
1569        }
1570
1571        if (i == DEFAULT_MAX_INPUT_BUFS)
1572        {
1573            printf("\n Unable to find a free input buffer!!");
1574            exit(0);
1575        }
1576
1577        ps_video_encode_ip->u4_size = sizeof(ih264e_video_encode_ip_t);
1578        ps_video_encode_op->u4_size = sizeof(ih264e_video_encode_op_t);
1579
1580        ps_video_encode_ip->e_cmd = IVE_CMD_VIDEO_ENCODE;
1581        ps_video_encode_ip->pv_bufs = pu1_buf;
1582        ps_video_encode_ip->pv_mb_info = pv_mb_info;
1583        ps_video_encode_ip->pv_pic_info = pv_pic_info;
1584        ps_video_encode_ip->u4_pic_info_type = ps_app_ctxt->u4_pic_info_type;
1585        /*
1586         * Since the buffers are used for reading,
1587         * And after each row we have a stride we nned to calculate
1588         * the luma size according to the stride
1589         */
1590        ps_inp_raw_buf->e_color_fmt = ps_app_ctxt->e_inp_color_fmt;
1591
1592        /* Initialize for 420SP */
1593        if(IV_YUV_420SP_UV == ps_app_ctxt->e_inp_color_fmt||
1594                        IV_YUV_420SP_VU == ps_app_ctxt->e_inp_color_fmt)
1595        {
1596            /*init luma buffer*/
1597            ps_inp_raw_buf->apv_bufs[0] = pu1_buf;
1598
1599            /*Init chroma buffer*/
1600            pu1_buf += ps_app_ctxt->u4_strd * ps_app_ctxt->u4_ht;
1601            ps_inp_raw_buf->apv_bufs[1] = pu1_buf;
1602
1603            ps_inp_raw_buf->au4_wd[0] =  ps_app_ctxt->u4_wd;
1604            ps_inp_raw_buf->au4_wd[1] =  ps_app_ctxt->u4_wd;
1605
1606            ps_inp_raw_buf->au4_ht[0] =  ps_app_ctxt->u4_ht;
1607            ps_inp_raw_buf->au4_ht[1] =  ps_app_ctxt->u4_ht / 2;
1608
1609            ps_inp_raw_buf->au4_strd[0] =  ps_app_ctxt->u4_strd;
1610            ps_inp_raw_buf->au4_strd[1] =  ps_app_ctxt->u4_strd;
1611        }
1612        else if(IV_YUV_420P == ps_app_ctxt->e_inp_color_fmt)
1613        {
1614            /* init buffers */
1615            ps_inp_raw_buf->apv_bufs[0] = pu1_buf;
1616            pu1_buf += (ps_app_ctxt->u4_wd) * ps_app_ctxt->u4_ht;
1617            ps_inp_raw_buf->apv_bufs[1] = pu1_buf;
1618            pu1_buf += (ps_app_ctxt->u4_wd >> 1) * (ps_app_ctxt->u4_ht >> 1);
1619            ps_inp_raw_buf->apv_bufs[2] = pu1_buf;
1620
1621            ps_inp_raw_buf->au4_wd[0] =  ps_app_ctxt->u4_wd;
1622            ps_inp_raw_buf->au4_wd[1] =  ps_app_ctxt->u4_wd / 2;
1623            ps_inp_raw_buf->au4_wd[2] =  ps_app_ctxt->u4_wd / 2;
1624
1625            ps_inp_raw_buf->au4_ht[0] =  ps_app_ctxt->u4_ht;
1626            ps_inp_raw_buf->au4_ht[1] =  ps_app_ctxt->u4_ht / 2;
1627            ps_inp_raw_buf->au4_ht[2] =  ps_app_ctxt->u4_ht / 2;
1628
1629            ps_inp_raw_buf->au4_strd[0] =  ps_app_ctxt->u4_strd;
1630            ps_inp_raw_buf->au4_strd[1] =  ps_app_ctxt->u4_strd / 2;
1631            ps_inp_raw_buf->au4_strd[2] =  ps_app_ctxt->u4_strd / 2;
1632
1633        }
1634        else if(IV_YUV_422ILE == ps_app_ctxt->e_inp_color_fmt)
1635        {
1636            /*init luma buffer*/
1637            ps_inp_raw_buf->apv_bufs[0] = pu1_buf;
1638
1639            ps_inp_raw_buf->au4_wd[0] =  ps_app_ctxt->u4_wd * 2;
1640
1641            ps_inp_raw_buf->au4_ht[0] =  ps_app_ctxt->u4_ht;
1642
1643            ps_inp_raw_buf->au4_strd[0] = ps_app_ctxt->u4_strd *2;
1644        }
1645
1646        /*
1647         * Here we read input and other associated buffers. Regardless of success
1648         * we will proceed from here as we will need extra calls to flush out
1649         * input queue in encoder. Note that this is not necessary. You can just
1650         * send encode calls till with valid output and recon buffers till the
1651         * queue is flushed.
1652         */
1653        while(1)
1654        {
1655            IV_STATUS_T mb_info_status = IV_SUCCESS, pic_info_status = IV_SUCCESS;
1656
1657            status = read_input(ps_app_ctxt->fp_ip, ps_inp_raw_buf);
1658
1659            if (ps_app_ctxt->u4_mb_info_type != 0)
1660            {
1661                mb_info_status = read_mb_info(ps_app_ctxt, pv_mb_info);
1662            }
1663            if (ps_app_ctxt->u4_pic_info_type != 0)
1664            {
1665                pic_info_status = read_pic_info(ps_app_ctxt, pv_pic_info);
1666            }
1667            if((IV_SUCCESS != status) || (IV_SUCCESS != mb_info_status)
1668                            || (IV_SUCCESS != pic_info_status))
1669            {
1670                if(0 == ps_app_ctxt->u4_loopback)
1671                {
1672                    is_last = 1;
1673                    break;
1674                }
1675                else
1676                    fseek(ps_app_ctxt->fp_ip, 0, SEEK_SET);
1677            }
1678            break;
1679        }
1680
1681        /******************************************************************************/
1682        /****************** Output Initialization *************************************/
1683        /******************************************************************************/
1684
1685        for(i = 0; i < DEFAULT_MAX_OUTPUT_BUFS; i++)
1686        {
1687            if(ps_app_ctxt->as_output_buf[i].u4_is_free)
1688            {
1689                pu1_buf = ps_app_ctxt->as_output_buf[i].pu1_buf;
1690                buff_size = ps_app_ctxt->as_output_buf[i].u4_buf_size;
1691                ps_app_ctxt->as_output_buf[i].u4_is_free = 0;
1692                break;
1693            }
1694        }
1695        ps_video_encode_ip->s_out_buf.pv_buf = pu1_buf;
1696        ps_video_encode_ip->s_out_buf.u4_bytes = 0;
1697        ps_video_encode_ip->s_out_buf.u4_bufsize = buff_size;
1698
1699        /******************************************************************************/
1700        /****************** Recon Initialization **************************************/
1701        /******************************************************************************/
1702        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);
1703
1704        if(ps_app_ctxt->u4_psnr_enable)
1705            init_raw_buf_descr(ps_app_ctxt, &s_inp_buf, ps_app_ctxt->pu1_psnr_buf, ps_app_ctxt->e_inp_color_fmt);
1706
1707        ps_video_encode_ip->s_recon_buf = s_recon_buf;
1708
1709        /******************************************************************************/
1710        /************************* Un Initialized *************************************/
1711        /******************************************************************************/
1712        if(0 == ps_app_ctxt->u4_loopback)
1713        {
1714            /* If input file is read completely and loopback is not enabled,
1715             *  then exit the loop */
1716            if(feof(ps_app_ctxt->fp_ip))
1717            {
1718                is_last = 1;
1719            }
1720        }
1721
1722
1723        /* If last frame, send input null to get back encoded frames */
1724        if ( is_last == 1 || ((ps_app_ctxt->u4_max_num_frms) <= u4_timestamp_low) )
1725        {
1726            is_last = 1;
1727            ps_inp_raw_buf->apv_bufs[0] = NULL;
1728            ps_inp_raw_buf->apv_bufs[1] = NULL;
1729            ps_inp_raw_buf->apv_bufs[2] = NULL;
1730        }
1731
1732        ps_video_encode_ip->u4_is_last = is_last;
1733        ps_video_encode_ip->u4_mb_info_type = ps_app_ctxt->u4_mb_info_type;
1734        ps_video_encode_ip->u4_pic_info_type = ps_app_ctxt->u4_pic_info_type;;
1735        ps_video_encode_op->s_out_buf.pv_buf= NULL;
1736        ps_video_encode_ip->u4_timestamp_high = u4_timestamp_high;
1737        ps_video_encode_ip->u4_timestamp_low = u4_timestamp_low;
1738
1739
1740        GETTIME(&ps_app_ctxt->enc_last_time);
1741
1742        status = ih264e_api_function(ps_enc, &ih264e_video_encode_ip, &ih264e_video_encode_op);
1743
1744        if (IV_SUCCESS != status)
1745        {
1746            printf("Encode Frame failed = 0x%x\n", ih264e_video_encode_op.s_ive_op.u4_error_code);
1747            break;
1748        }
1749
1750#ifdef WINDOWS_TIMER
1751        QueryPerformanceFrequency ( &frequency);
1752#endif
1753        GETTIME(&curtime);
1754        ELAPSEDTIME(ps_app_ctxt->enc_last_time, curtime, timetaken, frequency);
1755        ps_app_ctxt->enc_last_time = curtime;
1756
1757#ifdef PROFILE_ENABLE
1758        {
1759            WORD32 peak_avg, id;
1760            u4_total_time += timetaken;
1761            peak_window[peak_window_idx++] = timetaken;
1762            if(peak_window_idx == PEAK_WINDOW_SIZE)
1763                peak_window_idx = 0;
1764            peak_avg = 0;
1765            for(id = 0; id < PEAK_WINDOW_SIZE; id++)
1766            {
1767                peak_avg += peak_window[id];
1768            }
1769            peak_avg /= PEAK_WINDOW_SIZE;
1770            if (peak_avg > peak_avg_max)
1771                peak_avg_max = peak_avg;
1772        }
1773#endif
1774
1775        /******************************************************************************/
1776        /****************** Writing Output ********************************************/
1777        /******************************************************************************/
1778        num_bytes = 0;
1779
1780        if(1 == ps_video_encode_op->output_present)
1781        {
1782            num_bytes = ps_video_encode_op->s_out_buf.u4_bytes;
1783            buff_size = ps_video_encode_op->s_out_buf.u4_bufsize;
1784            pu1_buf = (UWORD8*)ps_video_encode_op->s_out_buf.pv_buf;
1785
1786            status = write_output(ps_app_ctxt->fp_op, pu1_buf, num_bytes);
1787            if(IV_SUCCESS != status)
1788            {
1789                printf("Error: Unable to write to output file\n");
1790                break;
1791            }
1792        }
1793
1794        /* free input bufer if codec returns a valid input buffer */
1795        if (ps_video_encode_op->s_inp_buf.apv_bufs[0])
1796        {
1797            /* Reuse of freed input buffer */
1798            for(i = 0; i < DEFAULT_MAX_INPUT_BUFS; i++)
1799            {
1800                if(ps_app_ctxt->as_input_buf[i].pu1_buf == ps_video_encode_op->s_inp_buf.apv_bufs[0])
1801                {
1802                    ps_app_ctxt->as_input_buf[i].u4_is_free = 1;
1803                    break;
1804                }
1805            }
1806        }
1807
1808        /* free output buffer if codec returns a valid output buffer */
1809        // if(ps_video_encode_op->s_out_buf.pv_buf)
1810        {
1811            for(i = 0; i < DEFAULT_MAX_OUTPUT_BUFS; i++)
1812            {
1813                if(ps_app_ctxt->as_output_buf[i].pu1_buf == ps_video_encode_op->s_out_buf.pv_buf)
1814                {
1815                    ps_app_ctxt->as_output_buf[i].u4_is_free = 1;
1816                    break;
1817                }
1818            }
1819        }
1820
1821        /**********************************************************************
1822         *  Print stats
1823         **********************************************************************/
1824        {
1825            UWORD8 u1_pic_type[][5] =
1826                { "IDR", "I", "P", "B", "NA" };
1827            WORD32 lookup_idx = 0;
1828
1829            if (ih264e_video_encode_op.s_ive_op.u4_encoded_frame_type
1830                            == IV_IDR_FRAME)
1831            {
1832                lookup_idx = 0;
1833            }
1834            else if(ih264e_video_encode_op.s_ive_op.u4_encoded_frame_type
1835                            == IV_I_FRAME)
1836            {
1837                lookup_idx = 1;
1838            }
1839            else if(ih264e_video_encode_op.s_ive_op.u4_encoded_frame_type
1840                            == IV_P_FRAME)
1841            {
1842                lookup_idx = 2;
1843            }
1844            else if(ih264e_video_encode_op.s_ive_op.u4_encoded_frame_type
1845                            == IV_B_FRAME)
1846            {
1847                lookup_idx = 3;
1848            }
1849            else if(ih264e_video_encode_op.s_ive_op.u4_encoded_frame_type
1850                            == IV_NA_FRAME)
1851            {
1852                lookup_idx = 4;
1853            }
1854
1855            if (ih264e_video_encode_op.s_ive_op.u4_encoded_frame_type
1856                            != IV_NA_FRAME)
1857            {
1858                ps_app_ctxt->u4_pics_cnt++;
1859                ps_app_ctxt->avg_time = u4_total_time / ps_app_ctxt->u4_pics_cnt;
1860                ps_app_ctxt->u4_total_bytes += num_bytes;
1861            }
1862
1863            if (ps_app_ctxt->u4_psnr_enable == 0)
1864            {
1865                printf("[%s] PicNum %4d Bytes Generated %6d TimeTaken(microsec): %6d AvgTime: %6d PeakAvgTimeMax: %6d\n",
1866                       u1_pic_type[lookup_idx], ps_app_ctxt->u4_pics_cnt,
1867                       num_bytes, timetaken, ps_app_ctxt->avg_time,
1868                       peak_avg_max);
1869            }
1870        }
1871
1872
1873        /* For psnr computation, we need to read the correct input frame and
1874         * compare with recon. The difficulty with doing it is that we only know
1875         * that the frame number of recon is monotonically increasing. There
1876         * may be gaps in the recon if any pre or post enc skip happens. There are
1877         * 3 senarios
1878         *  1) A frame is encoded -> returns the pic type
1879         *  2) A frame is not encoded -> Encoder is waiting, the frame may get
1880         *     encoded later
1881         *  3) A frame is not encoded -> A post enc or pre enc skip happend. The
1882         *     frame is not going to be encoded
1883         *
1884         *     The 1st and 2nd scenarios are easy, since we just needs to increment
1885         *     recon cnt whenever we get a valid recon. This cnt can we used to
1886         *     sync the recon and input
1887         *     3rd scenario in conjuction with 2nd will pose problems. Even if
1888         *     the returning frame is NA, we donot know we should increment the
1889         *     recon cnt or not becasue it can be case 2 or case 3.
1890         *
1891         *  Solutions:
1892         *  -------------------------
1893         *   One way to over come this will be to return more information as of
1894         *   the frame type. We can send if a frame was skipped as a part of the
1895         *   return frame type.
1896         *   This will not work. Since the output and recon are not in sync, we
1897         *   cannot use the current output frame type to determine if a recon
1898         *   is present currently or not. We need some other way to acheive this.
1899         *
1900         *   Other way to do this which is cleaner and maintains the seperation
1901         *   between recon and the ouptut is to set the width [& height] of output recon
1902         *   buffer to be zero. Hence we will in effect be saying :"look there
1903         *   is a recon, but due to frame not being encoded it is having a width 0".
1904         *   To be more clear we need to make height also to be zero.
1905         *
1906         *   But are we using these variables for allocating and deallocating
1907         *   the buffers some where ? No we are not. The buffer gets re-init
1908         *   at every encode call
1909         *
1910         *   Fixes
1911         *   ------------------------
1912         *   Currently the recon buff width and height are set in the encoder.
1913         *   This will not work now because since recon and input are not
1914         *   in sync. Hence a recon buff sent at time stamp x will get used to
1915         *   fill recon of input at time stamp y (x > y). If we reduced the
1916         *   frame dimensions in between, the recon buffer will not have enough
1917         *   space. Hence we need to set the with and height appropriatley inside
1918         *   lib itself.
1919         */
1920
1921        if (ps_app_ctxt->u4_recon_enable || ps_app_ctxt->u4_chksum_enable
1922                        || ps_app_ctxt->u4_psnr_enable)
1923        {
1924            if (ps_video_encode_op->dump_recon)
1925            {
1926                s_recon_buf = ps_video_encode_op->s_recon_buf;
1927
1928                /* Read input for psnr computuation */
1929                if (ps_app_ctxt->u4_psnr_enable)
1930                    read_input(ps_app_ctxt->fp_psnr_ip, &s_inp_buf);
1931
1932                /* if we have a valid recon buffer do the assocated tasks */
1933                if (s_recon_buf.au4_wd[0])
1934                {
1935                    /* Dump recon when enabled, and output bytes != 0 */
1936                    if (ps_app_ctxt->u4_recon_enable)
1937                    {
1938                        status = write_recon(ps_app_ctxt->fp_recon, &s_recon_buf);
1939                        if (IV_SUCCESS != status)
1940                        {
1941                            printf("Error: Unable to write to recon file\n");
1942                            break;
1943                        }
1944                    }
1945
1946                    if (ps_app_ctxt->u4_psnr_enable)
1947                    {
1948                        compute_psnr(ps_app_ctxt, &s_recon_buf, &s_inp_buf);
1949                    }
1950
1951
1952                    if (ps_app_ctxt->u4_chksum_enable)
1953                    {
1954                        WORD32 comp, num_comp = 2;
1955
1956                        if (IV_YUV_420P == s_recon_buf.e_color_fmt)
1957                            num_comp = 3;
1958
1959                        for (comp = 0; comp < num_comp; comp++)
1960                        {
1961                            UWORD8 au1_chksum[16];
1962                            calc_md5_cksum((UWORD8 *)s_recon_buf.apv_bufs[comp],
1963                                           s_recon_buf.au4_strd[comp],
1964                                           s_recon_buf.au4_wd[comp],
1965                                           s_recon_buf.au4_ht[comp],
1966                                           au1_chksum);
1967                            fwrite(au1_chksum, sizeof(UWORD8), 16, ps_app_ctxt->fp_chksum);
1968                        }
1969                    }
1970                }
1971            }
1972        }
1973
1974        u4_timestamp_low++;
1975
1976        /* Break if all the encoded frames are taken from encoder */
1977        if (1 == ps_video_encode_op->u4_is_last)
1978        {
1979            break;
1980        }
1981    }
1982
1983    /* Pic count is 1 more than actual num frames encoded, because last call is to just get the output  */
1984    ps_app_ctxt->u4_pics_cnt--;
1985
1986    if(ps_app_ctxt->u4_psnr_enable)
1987    {
1988        print_average_psnr(ps_app_ctxt);
1989    }
1990
1991    /* house keeping operations */
1992    fclose(ps_app_ctxt->fp_ip);
1993    fclose(ps_app_ctxt->fp_op);
1994    if(1 == ps_app_ctxt->u4_recon_enable)
1995    {
1996        fclose(ps_app_ctxt->fp_recon);
1997    }
1998    if(1 == ps_app_ctxt->u4_chksum_enable)
1999    {
2000        fclose(ps_app_ctxt->fp_chksum);
2001    }
2002    if(1 == ps_app_ctxt->u4_psnr_enable)
2003    {
2004        fclose(ps_app_ctxt->fp_psnr_ip);
2005    }
2006
2007    if(0 != ps_app_ctxt->u4_mb_info_type)
2008    {
2009        fclose(ps_app_ctxt->fp_mb_info);
2010    }
2011    if (ps_app_ctxt->u4_pic_info_type)
2012    {
2013        fclose(ps_app_ctxt->fp_pic_info);
2014    }
2015
2016    free_input(ps_app_ctxt);
2017    free_output(ps_app_ctxt);
2018    free_recon(ps_app_ctxt);
2019}
2020
2021/*****************************************************************************/
2022/*                                                                           */
2023/*  Function Name : main                                                     */
2024/*                                                                           */
2025/*  Description   : Application to demonstrate codec API                     */
2026/*                                                                           */
2027/*                                                                           */
2028/*  Inputs        : argc    - Number of arguments                            */
2029/*                  argv[]  - Arguments                                      */
2030/*  Globals       :                                                          */
2031/*  Processing    : Shows how to use create, process, control and delete     */
2032/*                                                                           */
2033/*  Outputs       : Codec output in a file                                   */
2034/*  Returns       :                                                          */
2035/*                                                                           */
2036/*  Issues        : Assumes both PROFILE_ENABLE to be                        */
2037/*                  defined for multithread decode-display working           */
2038/*                                                                           */
2039/*  Revision History:                                                        */
2040/*                                                                           */
2041/*         DD MM YYYY   Author(s)       Changes                              */
2042/*         20 11 2013   100189          Initial Version                      */
2043/*****************************************************************************/
2044#ifdef IOS
2045int h264enc_main(char * homedir,char *documentdir, int screen_wd, int screen_ht)
2046#else
2047int main(int argc, char *argv[])
2048#endif
2049{
2050    /* Config Parameters for Encoding */
2051    app_ctxt_t s_app_ctxt;
2052
2053    /* error string */
2054    CHAR ac_error[STRLENGTH];
2055
2056    /* config file name */
2057    CHAR ac_cfg_fname[STRLENGTH];
2058
2059    /* error status */
2060    IV_STATUS_T status = IV_SUCCESS;
2061#ifdef IOS
2062    /* temp var */
2063    CHAR filename_with_path[STRLENGTH];
2064#endif
2065    WORD32 num_mem_recs;
2066    iv_obj_t *ps_enc;
2067    WORD32 i;
2068    FILE *fp_cfg = NULL;
2069
2070#ifdef X86_MINGW
2071
2072    /* For getting printfs without any delay in eclipse */
2073    setvbuf(stdout, NULL, _IONBF, 0);
2074    setvbuf(stderr, NULL, _IONBF, 0);
2075
2076#endif
2077
2078    init_default_params(&s_app_ctxt);
2079
2080#ifndef IOS
2081
2082    /* Usage */
2083    if(argc < 2)
2084    {
2085        printf("Using enc.cfg as configuration file \n");
2086        strcpy(ac_cfg_fname, "enc.cfg");
2087    }
2088    else if(argc == 2)
2089    {
2090        if (!strcmp(argv[1], "--help"))
2091        {
2092            print_usage();
2093            exit(-1);
2094        }
2095        strcpy(ac_cfg_fname, argv[1]);
2096    }
2097
2098#else
2099    strcpy(ac_cfg_fname, "test.cfg");
2100
2101#endif
2102
2103    /*************************************************************************/
2104    /* Parse arguments                                                       */
2105    /*************************************************************************/
2106
2107#ifndef IOS
2108
2109    /* Read command line arguments */
2110    if(argc > 2)
2111    {
2112        for(i = 1; i + 1 < argc; i += 2)
2113        {
2114            if(CONFIG == get_argument(argv[i]))
2115            {
2116                strcpy(ac_cfg_fname, argv[i + 1]);
2117                if((fp_cfg = fopen(ac_cfg_fname, "r")) == NULL)
2118                {
2119                    sprintf(ac_error,
2120                            "Could not open Configuration file %s",
2121                            ac_cfg_fname);
2122                    codec_exit(ac_error);
2123                }
2124                read_cfg_file(&s_app_ctxt, fp_cfg);
2125                fclose(fp_cfg);
2126            }
2127            else
2128            {
2129                parse_argument(&s_app_ctxt, argv[i], argv[i + 1]);
2130            }
2131        }
2132    }
2133    else
2134    {
2135        if((fp_cfg = fopen(ac_cfg_fname, "r")) == NULL)
2136        {
2137            sprintf(ac_error, "Could not open Configuration file %s",
2138                    ac_cfg_fname);
2139            codec_exit(ac_error);
2140        }
2141        read_cfg_file(&s_app_ctxt, fp_cfg);
2142        fclose(fp_cfg);
2143    }
2144
2145#else
2146
2147    sprintf(filename_with_path, "%s/%s", homedir, "enc.cfg");
2148    if((fp_cfg = fopen(filename_with_path, "r")) == NULL)
2149    {
2150        sprintf(ac_error, "Could not open Configuration file %s",
2151                ac_cfg_fname);
2152        codec_exit(ac_error);
2153
2154    }
2155    read_cfg_file(&s_app_ctxt, fp_cfg);
2156    fclose(fp_cfg);
2157
2158#endif
2159
2160
2161    validate_params(&s_app_ctxt);
2162
2163
2164    /*************************************************************************/
2165    /*                      Getting Number of MemRecords                     */
2166    /*************************************************************************/
2167    {
2168        ih264e_num_mem_rec_ip_t s_num_mem_rec_ip;
2169        ih264e_num_mem_rec_op_t s_num_mem_rec_op;
2170
2171        s_num_mem_rec_ip.s_ive_ip.u4_size = sizeof(ih264e_num_mem_rec_ip_t);
2172        s_num_mem_rec_op.s_ive_op.u4_size = sizeof(ih264e_num_mem_rec_op_t);
2173
2174        s_num_mem_rec_ip.s_ive_ip.e_cmd = IV_CMD_GET_NUM_MEM_REC;
2175
2176        status = ih264e_api_function(0, &s_num_mem_rec_ip, &s_num_mem_rec_op);
2177
2178        if(status != IV_SUCCESS)
2179        {
2180            sprintf(ac_error, "Get number of memory records failed = 0x%x\n", s_num_mem_rec_op.s_ive_op.u4_error_code);
2181            codec_exit(ac_error);
2182        }
2183
2184        s_app_ctxt.u4_num_mem_rec = num_mem_recs = s_num_mem_rec_op.s_ive_op.u4_num_mem_rec;
2185    }
2186
2187    /* Allocate array to hold memory records */
2188    s_app_ctxt.ps_mem_rec = (iv_mem_rec_t *) malloc(num_mem_recs * sizeof(iv_mem_rec_t));
2189    if(NULL == s_app_ctxt.ps_mem_rec)
2190    {
2191
2192        sprintf(ac_error, "Unable to allocate memory for hold memory records: Size %d", (WORD32)(num_mem_recs * sizeof(iv_mem_rec_t)));
2193        codec_exit(ac_error);
2194    }
2195
2196    {
2197        iv_mem_rec_t *ps_mem_rec;
2198        ps_mem_rec = s_app_ctxt.ps_mem_rec;
2199        for(i = 0; i < num_mem_recs; i++)
2200        {
2201            ps_mem_rec->u4_size = sizeof(iv_mem_rec_t);
2202            ps_mem_rec->pv_base = NULL;
2203            ps_mem_rec->u4_mem_size = 0;
2204            ps_mem_rec->u4_mem_alignment = 0;
2205            ps_mem_rec->e_mem_type = IV_NA_MEM_TYPE;
2206
2207            ps_mem_rec++;
2208        }
2209    }
2210
2211    /*************************************************************************/
2212    /*                      Getting MemRecords Attributes                    */
2213    /*************************************************************************/
2214    {
2215        ih264e_fill_mem_rec_ip_t s_fill_mem_rec_ip;
2216        ih264e_fill_mem_rec_op_t s_fill_mem_rec_op;
2217
2218        s_fill_mem_rec_ip.s_ive_ip.u4_size = sizeof(ih264e_fill_mem_rec_ip_t);
2219        s_fill_mem_rec_op.s_ive_op.u4_size = sizeof(ih264e_fill_mem_rec_op_t);
2220
2221        s_fill_mem_rec_ip.s_ive_ip.e_cmd = IV_CMD_FILL_NUM_MEM_REC;
2222        s_fill_mem_rec_ip.s_ive_ip.ps_mem_rec = s_app_ctxt.ps_mem_rec;
2223        s_fill_mem_rec_ip.s_ive_ip.u4_num_mem_rec = s_app_ctxt.u4_num_mem_rec;
2224        s_fill_mem_rec_ip.s_ive_ip.u4_max_wd = s_app_ctxt.u4_max_wd;
2225        s_fill_mem_rec_ip.s_ive_ip.u4_max_ht = s_app_ctxt.u4_max_ht;
2226        s_fill_mem_rec_ip.s_ive_ip.u4_max_level = s_app_ctxt.u4_max_level;
2227        s_fill_mem_rec_ip.s_ive_ip.e_color_format = DEFAULT_INP_COLOR_FMT;
2228        s_fill_mem_rec_ip.s_ive_ip.u4_max_ref_cnt = DEFAULT_MAX_REF_FRM;
2229        s_fill_mem_rec_ip.s_ive_ip.u4_max_reorder_cnt = DEFAULT_MAX_REORDER_FRM;
2230        s_fill_mem_rec_ip.s_ive_ip.u4_max_srch_rng_x = DEFAULT_MAX_SRCH_RANGE_X;
2231        s_fill_mem_rec_ip.s_ive_ip.u4_max_srch_rng_y = DEFAULT_MAX_SRCH_RANGE_Y;
2232
2233        status = ih264e_api_function(0, &s_fill_mem_rec_ip, &s_fill_mem_rec_op);
2234
2235        if(status != IV_SUCCESS)
2236        {
2237            sprintf(ac_error, "Fill memory records failed = 0x%x\n",
2238                    s_fill_mem_rec_op.s_ive_op.u4_error_code);
2239            codec_exit(ac_error);
2240        }
2241    }
2242
2243    /*************************************************************************/
2244    /*                      Allocating Memory for Mem Records                */
2245    /*************************************************************************/
2246    {
2247        WORD32 total_size;
2248        iv_mem_rec_t *ps_mem_rec;
2249        total_size = 0;
2250
2251        ps_mem_rec = s_app_ctxt.ps_mem_rec;
2252        for(i = 0; i < num_mem_recs; i++)
2253        {
2254            ps_mem_rec->pv_base = ih264a_aligned_malloc(ps_mem_rec->u4_mem_alignment,
2255                                           ps_mem_rec->u4_mem_size);
2256            if(ps_mem_rec->pv_base == NULL)
2257            {
2258                sprintf(ac_error, "Allocation failure for mem record id %d size %d\n",
2259                        i, ps_mem_rec->u4_mem_size);
2260                codec_exit(ac_error);
2261            }
2262            total_size += ps_mem_rec->u4_mem_size;
2263
2264            ps_mem_rec++;
2265        }
2266        printf("\nTotal memory for codec %d\n", total_size);
2267    }
2268
2269
2270    /*************************************************************************/
2271    /*                        Codec Instance Creation                        */
2272    /*************************************************************************/
2273    {
2274        ih264e_init_ip_t s_init_ip;
2275        ih264e_init_op_t s_init_op;
2276
2277        ps_enc = s_app_ctxt.ps_mem_rec[0].pv_base;
2278        ps_enc->u4_size = sizeof(iv_obj_t);
2279        ps_enc->pv_fxns = ih264e_api_function;
2280        s_app_ctxt.ps_enc = ps_enc;
2281
2282        s_init_ip.s_ive_ip.u4_size = sizeof(ih264e_init_ip_t);
2283        s_init_op.s_ive_op.u4_size = sizeof(ih264e_init_op_t);
2284
2285        s_init_ip.s_ive_ip.e_cmd = IV_CMD_INIT;
2286        s_init_ip.s_ive_ip.u4_num_mem_rec = s_app_ctxt.u4_num_mem_rec;
2287        s_init_ip.s_ive_ip.ps_mem_rec = s_app_ctxt.ps_mem_rec;
2288        s_init_ip.s_ive_ip.u4_max_wd = s_app_ctxt.u4_max_wd;
2289        s_init_ip.s_ive_ip.u4_max_ht = s_app_ctxt.u4_max_ht;
2290        s_init_ip.s_ive_ip.u4_max_ref_cnt = DEFAULT_MAX_REF_FRM;
2291        s_init_ip.s_ive_ip.u4_max_reorder_cnt = DEFAULT_MAX_REORDER_FRM;
2292        s_init_ip.s_ive_ip.u4_max_level = s_app_ctxt.u4_max_level;
2293        s_init_ip.s_ive_ip.e_inp_color_fmt = s_app_ctxt.e_inp_color_fmt;
2294        if(s_app_ctxt.u4_recon_enable || s_app_ctxt.u4_psnr_enable || s_app_ctxt.u4_chksum_enable)
2295        {
2296            s_init_ip.s_ive_ip.u4_enable_recon           = 1;
2297        }
2298        else
2299        {
2300            s_init_ip.s_ive_ip.u4_enable_recon           = 0;
2301        }
2302        s_init_ip.s_ive_ip.e_recon_color_fmt    = s_app_ctxt.e_recon_color_fmt;
2303        s_init_ip.s_ive_ip.e_rc_mode            = s_app_ctxt.u4_rc;
2304        s_init_ip.s_ive_ip.u4_max_framerate     = s_app_ctxt.u4_max_frame_rate;
2305        s_init_ip.s_ive_ip.u4_max_bitrate       = s_app_ctxt.u4_max_bitrate;
2306        s_init_ip.s_ive_ip.u4_num_bframes       = s_app_ctxt.u4_num_bframes;
2307        s_init_ip.s_ive_ip.e_content_type       = IV_PROGRESSIVE;
2308        s_init_ip.s_ive_ip.u4_max_srch_rng_x    = DEFAULT_MAX_SRCH_RANGE_X;
2309        s_init_ip.s_ive_ip.u4_max_srch_rng_y    = DEFAULT_MAX_SRCH_RANGE_Y;
2310        s_init_ip.s_ive_ip.e_slice_mode         = s_app_ctxt.u4_slice_mode;
2311        s_init_ip.s_ive_ip.u4_slice_param       = s_app_ctxt.u4_slice_param;
2312        s_init_ip.s_ive_ip.e_arch               = s_app_ctxt.e_arch;
2313        s_init_ip.s_ive_ip.e_soc                = s_app_ctxt.e_soc;
2314
2315        status = ih264e_api_function(ps_enc, &s_init_ip, &s_init_op);
2316
2317        if(status != IV_SUCCESS)
2318        {
2319            sprintf(ac_error, "Init memory records failed = 0x%x\n",
2320                    s_init_op.s_ive_op.u4_error_code);
2321            codec_exit(ac_error);
2322        }
2323    }
2324
2325    /*************************************************************************/
2326    /*                        set processor details                          */
2327    /*************************************************************************/
2328    {
2329        ih264e_ctl_set_num_cores_ip_t s_ctl_set_num_cores_ip;
2330        ih264e_ctl_set_num_cores_op_t s_ctl_set_num_cores_op;
2331        s_ctl_set_num_cores_ip.s_ive_ip.e_cmd = IVE_CMD_VIDEO_CTL;
2332        s_ctl_set_num_cores_ip.s_ive_ip.e_sub_cmd = IVE_CMD_CTL_SET_NUM_CORES;
2333        s_ctl_set_num_cores_ip.s_ive_ip.u4_num_cores = s_app_ctxt.u4_num_cores;
2334        s_ctl_set_num_cores_ip.s_ive_ip.u4_timestamp_high = 0;
2335        s_ctl_set_num_cores_ip.s_ive_ip.u4_timestamp_low = 0;
2336        s_ctl_set_num_cores_ip.s_ive_ip.u4_size = sizeof(ih264e_ctl_set_num_cores_ip_t);
2337
2338        s_ctl_set_num_cores_op.s_ive_op.u4_size = sizeof(ih264e_ctl_set_num_cores_op_t);
2339
2340        status = ih264e_api_function(ps_enc, (void *) &s_ctl_set_num_cores_ip,
2341                (void *) &s_ctl_set_num_cores_op);
2342        if(status != IV_SUCCESS)
2343        {
2344            sprintf(ac_error, "Unable to set processor params = 0x%x\n",
2345                    s_ctl_set_num_cores_op.s_ive_op.u4_error_code);
2346            codec_exit(ac_error);
2347        }
2348
2349    }
2350
2351    /*************************************************************************/
2352    /*                        Get Codec Version                              */
2353    /*************************************************************************/
2354    {
2355        ih264e_ctl_getversioninfo_ip_t s_ctl_set_getversioninfo_ip;
2356        ih264e_ctl_getversioninfo_op_t s_ctl_set_getversioninfo_op;
2357        CHAR ac_version_string[STRLENGTH];
2358        s_ctl_set_getversioninfo_ip.s_ive_ip.e_cmd = IVE_CMD_VIDEO_CTL;
2359        s_ctl_set_getversioninfo_ip.s_ive_ip.e_sub_cmd = IVE_CMD_CTL_GETVERSION;
2360        s_ctl_set_getversioninfo_ip.s_ive_ip.pu1_version = (UWORD8 *)ac_version_string;
2361        s_ctl_set_getversioninfo_ip.s_ive_ip.u4_version_bufsize = sizeof(ac_version_string);
2362        s_ctl_set_getversioninfo_ip.s_ive_ip.u4_size = sizeof(ih264e_ctl_getversioninfo_ip_t);
2363        s_ctl_set_getversioninfo_op.s_ive_op.u4_size = sizeof(ih264e_ctl_getversioninfo_op_t);
2364
2365        status = ih264e_api_function(ps_enc, (void *) &s_ctl_set_getversioninfo_ip,
2366                (void *) &s_ctl_set_getversioninfo_op);
2367        if(status != IV_SUCCESS)
2368        {
2369            sprintf(ac_error, "Unable to get codec version = 0x%x\n",
2370                    s_ctl_set_getversioninfo_op.s_ive_op.u4_error_code);
2371            codec_exit(ac_error);
2372        }
2373        printf("CODEC VERSION %s\n", ac_version_string);
2374    }
2375
2376    /*************************************************************************/
2377    /*                      Get I/O Buffer Requirement                       */
2378    /*************************************************************************/
2379    {
2380        ih264e_ctl_getbufinfo_ip_t s_get_buf_info_ip;
2381        ih264e_ctl_getbufinfo_op_t s_get_buf_info_op;
2382
2383        s_get_buf_info_ip.s_ive_ip.u4_size = sizeof(ih264e_ctl_getbufinfo_ip_t);
2384        s_get_buf_info_op.s_ive_op.u4_size = sizeof(ih264e_ctl_getbufinfo_op_t);
2385
2386        s_get_buf_info_ip.s_ive_ip.e_cmd = IVE_CMD_VIDEO_CTL;
2387        s_get_buf_info_ip.s_ive_ip.e_sub_cmd = IVE_CMD_CTL_GETBUFINFO;
2388        s_get_buf_info_ip.s_ive_ip.u4_max_ht = s_app_ctxt.u4_max_ht;
2389        s_get_buf_info_ip.s_ive_ip.u4_max_wd = s_app_ctxt.u4_max_wd;
2390        s_get_buf_info_ip.s_ive_ip.e_inp_color_fmt = s_app_ctxt.e_inp_color_fmt;
2391
2392        status = ih264e_api_function(ps_enc, &s_get_buf_info_ip, &s_get_buf_info_op);
2393
2394        if (status != IV_SUCCESS)
2395        {
2396            sprintf(ac_error, "Unable to get I/O buffer requirements = 0x%x\n",
2397                    s_get_buf_info_op.s_ive_op.u4_error_code);
2398            codec_exit(ac_error);
2399        }
2400        s_app_ctxt.s_get_buf_info_op = s_get_buf_info_op;
2401    }
2402
2403    /*****************************************************************************/
2404    /* Add the following initializations based on the parameters in context      */
2405    /*****************************************************************************/
2406
2407
2408    /*****************************************************************************/
2409    /*   Video control  Set Frame dimensions                                     */
2410    /*****************************************************************************/
2411    s_app_ctxt.u4_strd = s_app_ctxt.u4_wd;
2412    set_dimensions(&s_app_ctxt, 0, 0);
2413
2414    /*****************************************************************************/
2415    /*   Video control  Set Frame rates                                          */
2416    /*****************************************************************************/
2417    set_frame_rate(&s_app_ctxt, 0, 0);
2418
2419    /*****************************************************************************/
2420    /*   Video control  Set IPE Params                                           */
2421    /*****************************************************************************/
2422    set_ipe_params(&s_app_ctxt, 0, 0);
2423
2424    /*****************************************************************************/
2425    /*   Video control  Set Bitrate                                              */
2426    /*****************************************************************************/
2427    set_bit_rate(&s_app_ctxt, 0, 0);
2428
2429    /*****************************************************************************/
2430    /*   Video control  Set QP                                                   */
2431    /*****************************************************************************/
2432    set_qp(&s_app_ctxt,0,0);
2433
2434    /*****************************************************************************/
2435    /*   Video control  Set AIR params                                           */
2436    /*****************************************************************************/
2437    set_air_params(&s_app_ctxt,0,0);
2438
2439    /*****************************************************************************/
2440    /*   Video control  Set VBV params                                           */
2441    /*****************************************************************************/
2442    set_vbv_params(&s_app_ctxt,0,0);
2443
2444    /*****************************************************************************/
2445    /*   Video control  Set Motion estimation params                             */
2446    /*****************************************************************************/
2447    set_me_params(&s_app_ctxt,0,0);
2448
2449    /*****************************************************************************/
2450    /*   Video control  Set GOP params                                           */
2451    /*****************************************************************************/
2452    set_gop_params(&s_app_ctxt, 0, 0);
2453
2454    /*****************************************************************************/
2455    /*   Video control  Set Deblock params                                       */
2456    /*****************************************************************************/
2457    set_deblock_params(&s_app_ctxt, 0, 0);
2458
2459    /*****************************************************************************/
2460    /*   Video control  Set Profile params                                       */
2461    /*****************************************************************************/
2462    set_profile_params(&s_app_ctxt, 0, 0);
2463
2464    /*****************************************************************************/
2465    /*   Video control Set in Encode header mode                                 */
2466    /*****************************************************************************/
2467    set_enc_mode(&s_app_ctxt, 0, 0, IVE_ENC_MODE_PICTURE);
2468
2469#ifdef IOS
2470    /* Correct file paths */
2471    sprintf(filename_with_path, "%s/%s", documentdir, s_app_ctxt.ac_ip_fname);
2472    strcpy (s_app_ctxt.ac_ip_fname, filename_with_path);
2473
2474    sprintf(filename_with_path, "%s/%s", documentdir, s_app_ctxt.ac_op_fname);
2475    strcpy (s_app_ctxt.ac_op_fname, filename_with_path);
2476
2477    sprintf(filename_with_path, "%s/%s", documentdir, s_app_ctxt.ac_recon_fname);
2478    strcpy (s_app_ctxt.ac_recon_fname, filename_with_path);
2479
2480    sprintf(filename_with_path, "%s/%s", documentdir, s_app_ctxt.ac_chksum_fname);
2481    strcpy (s_app_ctxt.ac_chksum_fname, filename_with_path);
2482
2483    sprintf(filename_with_path, "%s/%s", documentdir, s_app_ctxt.ac_mb_info_fname);
2484    strcpy (s_app_ctxt.ac_mb_info_fname, filename_with_path);
2485
2486    sprintf(filename_with_path, "%s/%s", documentdir, s_app_ctxt.ac_pic_info_fname);
2487    strcpy (s_app_ctxt.ac_pic_info_fname, filename_with_path);
2488#endif
2489
2490    /*************************************************************************/
2491    /*               begin encoding                                          */
2492    /*************************************************************************/
2493
2494    synchronous_encode(ps_enc, &s_app_ctxt);
2495
2496    {
2497        DOUBLE bytes_per_frame;
2498        DOUBLE bytes_per_second;
2499        WORD32 achieved_bitrate;
2500
2501        if(s_app_ctxt.u4_pics_cnt != 0)
2502        {
2503            bytes_per_frame = (s_app_ctxt.u4_total_bytes) / (s_app_ctxt.u4_pics_cnt);
2504        }
2505        else
2506        {
2507            bytes_per_frame = 0;
2508        }
2509        bytes_per_second = (bytes_per_frame * s_app_ctxt.u4_tgt_frame_rate);
2510
2511        achieved_bitrate = bytes_per_second * 8;
2512
2513        printf("\nEncoding Completed\n");
2514        printf("Summary\n");
2515        printf("Input filename                  : %s\n", s_app_ctxt.ac_ip_fname);
2516        printf("Output filename                 : %s\n", s_app_ctxt.ac_op_fname);
2517        printf("Output Width                    : %-4d\n", s_app_ctxt.u4_wd);
2518        printf("Output Height                   : %-4d\n", s_app_ctxt.u4_ht);
2519        printf("Target Bitrate (bps)            : %-4d\n", s_app_ctxt.u4_bitrate);
2520        printf("Achieved Bitrate (bps)          : %-4d\n", achieved_bitrate);
2521        printf("Average Time per Frame          : %-4d\n", s_app_ctxt.avg_time);
2522        printf("Achieved FPS                    : %-4.2f\n", 1000000.0 / s_app_ctxt.avg_time);
2523    }
2524
2525
2526    /*************************************************************************/
2527    /*                         Close Codec Instance                         */
2528    /*************************************************************************/
2529    {
2530        ih264e_retrieve_mem_rec_ip_t s_retrieve_mem_ip;
2531        ih264e_retrieve_mem_rec_op_t s_retrieve_mem_op;
2532        iv_mem_rec_t *ps_mem_rec;
2533        s_retrieve_mem_ip.s_ive_ip.u4_size =
2534                        sizeof(ih264e_retrieve_mem_rec_ip_t);
2535        s_retrieve_mem_op.s_ive_op.u4_size =
2536                        sizeof(ih264e_retrieve_mem_rec_op_t);
2537
2538        s_retrieve_mem_ip.s_ive_ip.e_cmd = IV_CMD_RETRIEVE_MEMREC;
2539        s_retrieve_mem_ip.s_ive_ip.ps_mem_rec = s_app_ctxt.ps_mem_rec;
2540
2541        status = ih264e_api_function(ps_enc, &s_retrieve_mem_ip,
2542                                          &s_retrieve_mem_op);
2543
2544        if(status != IV_SUCCESS)
2545        {
2546            sprintf(ac_error, "Unable to retrieve memory records = 0x%x\n",
2547                    s_retrieve_mem_op.s_ive_op.u4_error_code);
2548            codec_exit(ac_error);
2549        }
2550
2551        /* Free memory records */
2552        ps_mem_rec = s_app_ctxt.ps_mem_rec;
2553        for(i = 0; i < num_mem_recs; i++)
2554        {
2555            ih264a_aligned_free(ps_mem_rec->pv_base);
2556            ps_mem_rec++;
2557        }
2558
2559        free(s_app_ctxt.ps_mem_rec);
2560
2561    }
2562
2563    return 0;
2564}
2565
2566
2567#ifdef  ANDROID_NDK
2568int raise(int a)
2569{
2570    printf("Divide by zero\n");
2571    return 0;
2572}
2573void __aeabi_assert(const char *assertion, const char *file, unsigned int line)
2574{
2575    return;
2576}
2577#endif
2578