aboutsummaryrefslogtreecommitdiffstats
blob: cbc80e3c5cfaad5dbf9110166b89ded48a3bc6fe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
/*
 * Copyright (c) 2013, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <sys/mman.h>

#include "libdce.h"

#include <tilermem.h>
#include <memmgr.h>
#include <ti/sdo/ce/Engine.h>
#include <ti/sdo/ce/video2/videnc2.h>
#include <ti/sdo/codecs/h264enc/ih264enc.h>
#include <ti/sdo/codecs/mpeg4enc/impeg4enc.h>

#include "ti/shmemallocator/SharedMemoryAllocatorUsr.h"

#define PRINT_DEBUG
//#define H264_DEBUG

#define ERROR(FMT, ...)  printf("%s:%d:\t%s\terror: " FMT "\n", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
// enable below to print debug information
#ifdef PRINT_DEBUG
#define DEBUG(FMT, ...)  printf("%s:%d:\t%s\tdebug: " FMT "\n", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
#else
#define DEBUG(FMT, ...)
#endif
#define INFO(FMT, ...)  printf("%s:%d:\t%s\tinfo: " FMT "\n", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
#define MIN(a, b)        (((a) < (b)) ? (a) : (b))

/* align x to next highest multiple of 2^n */
#define ALIGN2(x, n)   (((x) + ((1 << (n)) - 1)) & ~((1 << (n)) - 1))

// Profile the init and encode calls
//#define PROFILE_TIME

// Getting codec version through XDM_GETVERSION
#define GETVERSION

enum {
    IVAHD_H264_ENCODE,
    IVAHD_MPEG4_ENCODE,
    IVAHD_H263_ENCODE
};

/*
 * A very simple VIDENC2 client which will encode raw (unstrided) NV12 YUV frames
 * and write out to either h264, MPEG4, or H.263 format.
 */

// GLOBAL VARIABLES
int                      width, height, padded_width, padded_height, num_buffers, tiler;
Engine_Handle            engine    = NULL;
VIDENC2_Handle           codec     = NULL;
VIDENC2_Params          *params    = NULL;
VIDENC2_DynamicParams   *dynParams = NULL;
VIDENC2_Status          *status    = NULL;
VIDENC2_DynamicParams   *dynParams1 = NULL;
VIDENC2_Status          *status1    = NULL;
IVIDEO2_BufDesc         *inBufs    = NULL;
XDM2_BufDesc            *outBufs   = NULL;
VIDENC2_InArgs          *inArgs    = NULL;
VIDENC2_OutArgs         *outArgs   = NULL;

// H.264 specific
IH264ENC_InArgs          *h264enc_inArgs = NULL;
IH264ENC_OutArgs         *h264enc_outArgs = NULL;
IH264ENC_Params          *h264enc_params    = NULL;
IH264ENC_DynamicParams   *h264enc_dynParams = NULL;
IH264ENC_Status          *h264enc_status    = NULL;

// MPEG4/H.263 specific
IMPEG4ENC_InArgs          *mpeg4enc_inArgs    = NULL;
IMPEG4ENC_OutArgs         *mpeg4enc_outArgs   = NULL;
IMPEG4ENC_Params          *mpeg4enc_params    = NULL;
IMPEG4ENC_DynamicParams   *mpeg4enc_dynParams = NULL;
IMPEG4ENC_Status          *mpeg4enc_status    = NULL;

unsigned int    frameSize[64000]; /* Buffer for keeping frame sizes */
static int      input_offset = 0;

static void *tiler_alloc(int width, int height)
{
    int              dimensions;
    void            *bufPtr = NULL;
    MemAllocBlock    block;
    MemAllocBlock    blocks[2];

    memset(&block, 0, sizeof(MemAllocBlock));
    memset(blocks, 0, sizeof(MemAllocBlock) * 2);

    if( !height ) {
        DEBUG("tiler alloc 1D allocation width=%d", width);
        /* 1d allocation: */
        dimensions = 1;

        block.pixelFormat = PIXEL_FMT_PAGE;
        block.dim.len = width;
        block.stride = 0;

        bufPtr = MemMgr_Alloc(&block, dimensions);
    } else {
        DEBUG("tiler alloc 2D allocation width=%d height=%d", width, height);
        /* 2d allocation: */
        dimensions = 2;
        blocks[0].pixelFormat = PIXEL_FMT_8BIT;
        blocks[0].dim.area.width  = width;
        blocks[0].dim.area.height = height;
        blocks[0].stride = 0;

        blocks[1].pixelFormat = PIXEL_FMT_16BIT;
        blocks[1].dim.area.width  = width >> 1;
        blocks[1].dim.area.height = height >> 1;
        blocks[1].stride = 0;

        bufPtr = MemMgr_Alloc(blocks, dimensions);
    }
    DEBUG("tiler alloc return bufPtr %p", bufPtr);

    return (bufPtr);
}

static int allocate_nonTiler(shm_buf* shmBuf, int size)
{
    DEBUG(" ----------------- create nonTILER size %d --------------------", size);

    // Allocation through mmap
    uint64_t   *vaddr;
    int32_t     ret, len = 0;
    int64_t     paddr = 0;

    ret = SHM_alloc(size, shmBuf);
    if( ret < 0 ) {
        ERROR("Failed to alloc shmem buffer\n");
        return (-ENOMEM);
    }
    vaddr = (uint64_t *) shmBuf->vir_addr;

    DEBUG("nonTILER shmBuf->vir_addr =%08x, shmBuf->phy_addr =%08x ", (unsigned int) shmBuf->vir_addr, (unsigned int) shmBuf->phy_addr);

    ret = mem_offset64(vaddr, NOFD, (size_t) size, &paddr, (size_t *) &len);
    if( ret ) {
        ERROR("Failed to check memory contiguous ret %d errno %d\n", ret, errno);
        SHM_release(shmBuf);
        return (-ENOMEM);
    } else if( len != (size) ) {
        ERROR("Failed to check len %d != %d\n", len, size);
        SHM_release(shmBuf);
        return (-ENOMEM);
    }

    DEBUG("shmBuf %p", shmBuf);
    return 0;
}

static void free_nonTiler(void *shmBuf)
{
    DEBUG("shmBuf %p", shmBuf);
    SHM_release(shmBuf);
}

/* ************************************************************************* */
/* utilities to allocate/manage 2d input buffers */

typedef struct InputBuffer InputBuffer;

struct InputBuffer {
    char        *buf; /* virtual address for local access, 4kb stride */
    SSPtr        y, uv; /* physical addresses of Y and UV for remote access */
    InputBuffer *next;      /* next free buffer */
    bool         tiler;
    uint32_t     len;
};

/* ************************************************************************* */

/* get file path.. return path is only valid until next time this is called */
static const char *get_path(const char *pattern, int cnt)
{
    static int     len = 0;
    static char   *path = NULL;

    /* It would be better to not assume the pattern doesn't expand to
         * less than 10 chars it's original length..
         */
    if( (strlen(pattern) + 10) > len ) {
        len  = strlen(pattern) + 10;
        path = realloc(path, len);
    }

    if( path ) {
        snprintf(path, len - 1, pattern, cnt);
    }

    return (path);
}

//#define DUMPINPUTDATA
//#define DUMPOUTPUTDATA

#ifdef DUMPINPUTDATA
FILE   *inputDump;
#endif

#ifdef DUMPOUTPUTDATA
FILE   *outputDump;
#endif


/* helper to read one frame NV12 of input */
int read_input(const char *pattern, int cnt, char *input)
{
    int    sz = 0, n = 0, num_planes, i, buf_height;

    const char   *path = get_path(pattern, cnt);
    if( path == NULL ) {
        return (sz);
    }

    int fd = open(path, O_RDONLY);

    //DEBUG("Open file fd %d errno %d", fd, errno);
    if( fd < 0 ) {
        DEBUG("open input file failed");
        return (-1);
    }

    sz = width * height * 3 / 2;

    // Filling the input in TILER 2D mode; where
    // Luma has size of Stride "4096" * height.
    // and Chroma has size of Stride "4096 * height / 2.
    for( num_planes = 0; num_planes < 2; num_planes++ ) {
        if( num_planes ) { //UV location
            buf_height = height / 2;
        } else { //Y location
            buf_height = height;
        }

        for( i = 0; i < buf_height; i++ ) {
            lseek(fd, input_offset, SEEK_SET);
            n = read(fd, input, width);
            if( n ) {
                input_offset += n;
            } else {
                close(fd);
                DEBUG("Reading REACH EOF - return -1");
                return (-1); //Cannot read anymore input
            }

            if( tiler ) {
                input = (char *) ((int)input + 4096);
            } else {
                input = (char *) ((int)input + width);
            }
        }
    }

    close(fd);

    DEBUG("sz=%d", sz);
    return (sz);
}

/* helper to write one frame of output */
int write_output(const char *pattern, int cnt, char *output, int bytesToWrite)
{
    int           sz = 0;
    const char   *path = get_path(pattern, cnt);
    if( path == NULL ) {
        return (sz);
    }

    int fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644);

    if( fd < 0 ) {
        ERROR("could open output file: %s (%d)", path, errno);
        return (0);
    }

    char   *p = output;
    sz = write(fd, p, bytesToWrite);

    DEBUG("Writing of bytesToWrite [%d] output sz %d \n", bytesToWrite, sz);

    close(fd);

    return (sz);
}

#ifdef PROFILE_TIME
/* for timing in microsecond */
uint64_t mark_microsecond(uint64_t *last)
{
    struct timespec    time;
    uint64_t           t1 = 0;

    clock_gettime(CLOCK_REALTIME, &time);
    t1 = timespec2nsec(&time);
    t1 = t1 / 1000;
    if( last ) {
        return (t1 - *last);
    }
    return (t1);
}

#endif

#ifdef GETVERSION
#define VERSION_SIZE 128
#endif

/* encoder body */
int main(int argc, char * *argv)
{
    Engine_Error    ec;
    XDAS_Int32      err;
    char           *output = NULL;
    char           *output_mvbuf =  NULL;
    shm_buf         output_mvbuf_nonTiler;
    shm_buf         output_nonTiler;
    shm_buf         input_nonTiler;
    int             output_size = 0;
    int             mvbufinfo_size = 0;
    char           *in_pattern, *out_pattern;
    int             in_cnt = 0, out_cnt = 0;
    InputBuffer    *buf = NULL;
    char            profile[10];
    int             profile_value;
    int             level;
    int             eof = 0;
    char            vid_codec[10];
    char            tilerbuffer[10];
    unsigned int    codec_switch = 0;
    int             bytesGenerated = 0;
    int             frames_to_write = 0;

#ifdef PROFILE_TIME
    uint64_t    init_start_time = 0;
    uint64_t    codec_process_time = 0;
    uint64_t    total_init_time = 0;
    uint64_t    input_alloc_time = 0;
#endif

#if 0
    int    loop = 0;

    while( loop == 0 ) {
        loop = 0;
    }

#endif

    if( (argc >= 2) && !strcmp(argv[1], "-1") ) {
        argc--;
        argv++;
    }

    if( argc != 10 ) {
        printf("usage:   %s width height frames_to_write inpattern outpattern codec baseline/high level buffertype\n", argv[0]);
        printf("example: %s 1920 1088 300 in.yuv out.h264 h264 baseline 10 tiler\n", argv[0]);
        printf("example: %s 176 144 300 in.yuv out.m4v mpeg4 simple/baseline 0 non-tiler\n", argv[0]);
        printf("example: %s 176 144 300 in.yuv out.m4v h263 simple/baseline 0 tiler\n", argv[0]);
        printf("Currently supported codecs: h264 or mpeg4 or h263\n");
        printf("Run this command for help on the use case: use dce_enc_test\n");
        return (1);
    }

    width  = atoi(argv[1]);
    height = atoi(argv[2]);
    frames_to_write = atoi(argv[3]);
    in_pattern  = argv[4];
    out_pattern = argv[5];
    strcpy(vid_codec, argv[6]);
    strcpy(profile, argv[7]);
    level = atoi(argv[8]);
    strcpy(tilerbuffer, argv[9]);

    printf("Selected codec: %s\n", vid_codec);
    printf("Selected buffer: %s\n", tilerbuffer);

    enum {
        DCE_ENC_TEST_H264  = 1,
        DCE_ENC_TEST_MPEG4 = 2,
        DCE_ENC_TEST_H263 = 3
    };

/*
 * Configuration based on the input parameters
 */

    if( (!(strcmp(vid_codec, "h264"))) ) {
        codec_switch = DCE_ENC_TEST_H264;
        if( (!(strcmp(profile, "baseline"))) ) {
            profile_value = IH264_BASELINE_PROFILE;
        } else if( (!(strcmp(profile, "high"))) ) {
            profile_value = IH264_HIGH_PROFILE;
        } else {
            printf("Wrong profile value. Please use: baseline or high. See 'use dce_enc_test'.\n");
            return (1);
        }

        switch( level ) {
            case IH264_LEVEL_10 :
            case IH264_LEVEL_1b :
            case IH264_LEVEL_11 :
            case IH264_LEVEL_12 :
            case IH264_LEVEL_13 :
            case IH264_LEVEL_20 :
            case IH264_LEVEL_21 :
            case IH264_LEVEL_22 :
            case IH264_LEVEL_30 :
            case IH264_LEVEL_31 :
            case IH264_LEVEL_32 :
            case IH264_LEVEL_40 :
            case IH264_LEVEL_41 :
            case IH264_LEVEL_42 :
            case IH264_LEVEL_50 :
            case IH264_LEVEL_51 :
                printf("Acceptable H.264 level value = %d\n", level);
                break;
            default :
                printf("Wrong level value. Please use the correct level value for H.264. See 'use dce_enc_test'.\n");
                return (1);
        }
    } else if( !(strcmp(vid_codec, "mpeg4"))) {

        codec_switch = DCE_ENC_TEST_MPEG4;

        if( (!(strcmp(profile, "simple"))) ) {
            profile_value = 3;
        } else {
            printf("Wrong profile value. Please use: simple. See 'use dce_enc_test'.\n");
            return (1);
        }

        switch( level ) {
            case IMPEG4ENC_SP_LEVEL_0 :
            case IMPEG4ENC_SP_LEVEL_0B :
            case IMPEG4ENC_SP_LEVEL_1 :
            case IMPEG4ENC_SP_LEVEL_2 :
            case IMPEG4ENC_SP_LEVEL_3 :
            case IMPEG4ENC_SP_LEVEL_4A :
            case IMPEG4ENC_SP_LEVEL_5 :
            case IMPEG4ENC_SP_LEVEL_6 :
                printf("Acceptable MPEG4 level value = %d\n", level);
                break;
            default :
                printf("Wrong level value. Please use the correct level value for MPEG4. See 'use dce_enc_test'.\n");
                return (1);
        }
    } else if( !(strcmp(vid_codec, "h263"))) {

        codec_switch = DCE_ENC_TEST_H263;

        if( (!(strcmp(profile, "simple"))) ) {
            profile_value = 3;
        } else {
            printf("Wrong profile value. Please use: simple. See 'use dce_enc_test'.\n");
            return (1);
        }

        switch( level ) {
            case IMPEG4ENC_H263_LEVEL_10 :
            case IMPEG4ENC_H263_LEVEL_20 :
            case IMPEG4ENC_H263_LEVEL_30 :
            case IMPEG4ENC_H263_LEVEL_40 :
            case IMPEG4ENC_H263_LEVEL_45 :
            case IMPEG4ENC_H263_LEVEL_50 :
            case IMPEG4ENC_H263_LEVEL_60 :
            case IMPEG4ENC_H263_LEVEL_70 :
                printf("Acceptable H263 level value = %d\n", level);
                break;
            default :
                printf("Wrong level value. Please use the correct level value for H263. See 'use dce_enc_test'.\n");
                return (1);
        }
    } else {
        printf("No valid codec entry. Please use: h264 or mpeg4 or h263\n");
        return (1);
    }

    DEBUG("width=%d, height=%d", width, height);

    /* output buffer parameters is TILER with width and height */
    width  = ALIGN2(width, 4);         /* round up to MB */
    height = ALIGN2(height, 1);        /* round up to MB */

    switch( codec_switch ) {
        case DCE_ENC_TEST_H264 :
        case DCE_ENC_TEST_MPEG4 :
        case DCE_ENC_TEST_H263 :
            num_buffers = 1;
            break;
        default :
            ERROR("Unrecognized codec to encode");
    }

    DEBUG("width=%d, height=%d, num_buffers=%d",
          width, height, num_buffers);

#ifdef PROFILE_TIME
    init_start_time = mark_microsecond(NULL);
#endif
    engine = Engine_open("ivahd_vidsvr", NULL, &ec);

    if( !engine ) {
        ERROR("DCE_ENCODE_TEST_FAIL: ENGINE OPEN FAILED");
        goto out;
    }

    DEBUG("Engine_open successful engine=%p", engine);

/*
 * inBufs handling
 */
    DEBUG("input buffer configuration width %d height %d", width, height);
    inBufs = dce_alloc(sizeof(IVIDEO2_BufDesc));

    DEBUG("Input allocate through tiler");

#ifdef PROFILE_TIME
    uint64_t    alloc_time_start = mark_microsecond(NULL);
#endif

    inBufs->numPlanes = 2;
    inBufs->imageRegion.topLeft.x = 0;
    inBufs->imageRegion.topLeft.y = 0;
    inBufs->imageRegion.bottomRight.x = width;

    inBufs->topFieldFirstFlag = 0; //Only valid for interlace content.
    inBufs->contentType = IVIDEO_PROGRESSIVE;

    inBufs->activeFrameRegion.topLeft.x = 0;
    inBufs->activeFrameRegion.topLeft.y = 0;
    inBufs->activeFrameRegion.bottomRight.x = width;
    inBufs->activeFrameRegion.bottomRight.y = height;

    inBufs->imageRegion.bottomRight.y = height;
    inBufs->chromaFormat = XDM_YUV_420SP;

    inBufs->secondFieldOffsetWidth[0] = 0;
    inBufs->secondFieldOffsetHeight[0] = 0;

    if( !(strcmp(tilerbuffer, "tiler")) ) {
        DEBUG("Input allocate through TILER 2D");
        tiler = 1;

        inBufs->imagePitch[0] = 4096;
        inBufs->planeDesc[0].memType = XDM_MEMTYPE_TILED8;
        inBufs->planeDesc[0].bufSize.tileMem.width  = width;
        inBufs->planeDesc[0].bufSize.tileMem.height = height;

        inBufs->secondFieldOffsetWidth[1] = 1;
        inBufs->secondFieldOffsetHeight[1] = 0;

        inBufs->imagePitch[1] = 4096;
        inBufs->planeDesc[1].memType = XDM_MEMTYPE_TILED16;
        inBufs->planeDesc[1].bufSize.tileMem.width  = width; /* UV interleaved width is same a Y */
        inBufs->planeDesc[1].bufSize.tileMem.height = height / 2;

        // INPUT BUFFER MUST BE TILED NV12. Encoder codec doesn't support non TILED input buffer.
        buf = calloc(sizeof(InputBuffer), 1);
        DEBUG(" ----------------- create INPUT TILER buf 0x%x --------------------", (unsigned int)buf);
        buf->buf = tiler_alloc(width, height);
        if( buf->buf ) {
            buf->y   = (SSPtr)buf->buf;
            buf->uv  = (SSPtr)buf->buf + (height * 4096);

            DEBUG("INPUT TILER buf=%p, buf->buf=%p y=%08x, uv=%08x", buf, buf->buf, buf->y, buf->uv);
        } else {
            ERROR("DCE_ENCODE_TEST_FAIL: TILER ALLOCATION FAILED");
            free(buf);
            goto shutdown;
        }
    } else {
        DEBUG("Input allocate through NON-TILER");
        tiler = 0;

        inBufs->imagePitch[0] = width;
        inBufs->planeDesc[0].memType = XDM_MEMTYPE_RAW;
        inBufs->planeDesc[0].bufSize.bytes = width * height;
        inBufs->secondFieldOffsetWidth[1] = 1;
        inBufs->secondFieldOffsetHeight[1] = 0;

        inBufs->imagePitch[1] = width;
        inBufs->planeDesc[1].memType = XDM_MEMTYPE_RAW;
        inBufs->planeDesc[1].bufSize.bytes = width * height / 2;
        buf = calloc(sizeof(InputBuffer), 1);
        DEBUG(" ----------------- create NON INPUT TILER buf 0x%x --------------------", (unsigned int)buf);
        err = allocate_nonTiler(&input_nonTiler, width * height * 3/2);
        if( err < 0 ) {
            ERROR("DCE_ENCODE_TEST_FAIL: NON-TILER ALLOCATION FAILED");
            free(buf);
            goto shutdown;
        }
        else
        {
            buf->buf = (char*)input_nonTiler.vir_addr;
            buf->y   = (SSPtr)input_nonTiler.vir_addr;
            buf->uv  = (SSPtr)input_nonTiler.vir_addr + (height * width);

            DEBUG("INPUT NON TILER buf=%p, buf->buf=%p y=%08x, uv=%08x", buf, buf->buf, buf->y, buf->uv);
        }
    }

#ifdef PROFILE_TIME
    input_alloc_time = mark_microsecond(&alloc_time_start);
#endif

    DEBUG("input buffer configuration num_buffers %d width %d height %d", num_buffers, width, height);

/*
 * inArgs and outArgs configuration for static parameter passed during codec creation.
 */
    switch( codec_switch ) {
        case DCE_ENC_TEST_H264 :
            inArgs = dce_alloc(sizeof(IH264ENC_InArgs));
            inArgs->size = sizeof(IH264ENC_InArgs);

            outArgs = dce_alloc(sizeof(IH264ENC_OutArgs));
            outArgs->size = sizeof(IH264ENC_OutArgs);
            h264enc_outArgs = (IH264ENC_OutArgs *) outArgs;

            params = dce_alloc(sizeof(IH264ENC_Params));
            params->size = sizeof(IH264ENC_Params);
            break;
        case DCE_ENC_TEST_MPEG4 :
        case DCE_ENC_TEST_H263 :
            inArgs = dce_alloc(sizeof(IMPEG4ENC_InArgs));
            inArgs->size = sizeof(IMPEG4ENC_InArgs);

            outArgs = dce_alloc(sizeof(IMPEG4ENC_OutArgs));
            outArgs->size = sizeof (IMPEG4ENC_OutArgs);
            mpeg4enc_outArgs = (IMPEG4ENC_OutArgs *) outArgs;

            params = dce_alloc(sizeof(IMPEG4ENC_Params));
            params->size = sizeof(IMPEG4ENC_Params);
            break;
        default :
            ERROR("Unrecognized codec to encode");
    }

    params->encodingPreset = XDM_USER_DEFINED; //XDM_USER_DEFINED; //XDM_EncodingPreset
    params->rateControlPreset = IVIDEO_USER_DEFINED;
    params->maxHeight = height;
    params->maxWidth = width;
    params->dataEndianness = XDM_BYTE; //XDM_DataFormat
    params->maxBitRate = -1; //IGNORED
    params->minBitRate = 0;
    params->inputChromaFormat = XDM_YUV_420SP; //XDM_ChromaFormat
    params->inputContentType = IVIDEO_PROGRESSIVE; //IVIDEO_ContentType
    params->operatingMode = IVIDEO_ENCODE_ONLY; //IVIDEO_OperatingMode
    params->profile = profile_value;
    params->level = level;
    params->inputDataMode = IVIDEO_ENTIREFRAME; //IVIDEO_DataMode
    params->outputDataMode = IVIDEO_ENTIREFRAME; //IVIDEO_DataMode
    params->numInputDataUnits = 1;
    params->numOutputDataUnits = 1;
    params->metadataType[0] = IVIDEO_METADATAPLANE_NONE;
    params->metadataType[1] = IVIDEO_METADATAPLANE_NONE;
    params->metadataType[2] = IVIDEO_METADATAPLANE_NONE;

    DEBUG("dce_alloc VIDENC2_Params successful params=%p", params);

    switch( codec_switch ) {
        case DCE_ENC_TEST_H264 :
            DEBUG("H.264 Encoding with profile_value %d level %d", profile_value, level);
            params->maxInterFrameInterval = 1; //1,31 if IVIDEO_ContentType is IVIDEO_PROGRESSIVE

            //Miscellaneous
            h264enc_params = (IH264ENC_Params *) params;
            h264enc_params->interlaceCodingType = IH264_INTERLACE_DEFAULT;
            h264enc_params->bottomFieldIntra = 0;
            h264enc_params->gopStructure = IH264ENC_GOPSTRUCTURE_DEFAULT; // IH264ENC_GOPSTRUCTURE_NONUNIFORM
            h264enc_params->entropyCodingMode = IH264_ENTROPYCODING_DEFAULT; // IH264_ENTROPYCODING_CAVLC - BASE PROFILE
            h264enc_params->transformBlockSize = IH264_TRANSFORM_4x4; // BASE PROFILE
            h264enc_params->log2MaxFNumMinus4 = 10;
            h264enc_params->picOrderCountType = IH264_POC_TYPE_DEFAULT; // IH264_POC_TYPE_0
            h264enc_params->enableWatermark = 0;
            h264enc_params->IDRFrameInterval = 1;
            h264enc_params->pConstantMemory = NULL;
            h264enc_params->maxIntraFrameInterval = 0x7FFFFFFF;
            h264enc_params->debugTraceLevel = 0;
            h264enc_params->lastNFramesToLog = 0;
            h264enc_params->enableAnalyticinfo = 0;
            h264enc_params->enableGMVSei = 0;
            h264enc_params->constraintSetFlags = 20;
            h264enc_params->enableRCDO = 0;
            h264enc_params->enableLongTermRefFrame = IH264ENC_LTRP_NONE;
            h264enc_params->LTRPPeriod = 0;

            //H-P Coding Control Params
            h264enc_params->numTemporalLayer = IH264_TEMPORAL_LAYERS_1;
            h264enc_params->referencePicMarking = IH264_LONG_TERM_PICTURE;
            h264enc_params->reservedParams[0] = 0;
            h264enc_params->reservedParams[1] = 0;
            h264enc_params->reservedParams[2] = 0;

            //rate control params
            h264enc_params->rateControlParams.rateControlParamsPreset = IH264_RATECONTROLPARAMS_USERDEFINED;
            h264enc_params->rateControlParams.scalingMatrixPreset = IH264_SCALINGMATRIX_NONE;
            h264enc_params->rateControlParams.rcAlgo = IH264_RATECONTROL_DEFAULT; // 0
            h264enc_params->rateControlParams.qpI = 28;
            h264enc_params->rateControlParams.qpMaxI = 36;
            h264enc_params->rateControlParams.qpMinI = 10;
            h264enc_params->rateControlParams.qpP = 28;
            h264enc_params->rateControlParams.qpMaxP = 40;
            h264enc_params->rateControlParams.qpMinP = 10;
            h264enc_params->rateControlParams.qpOffsetB = 4;
            h264enc_params->rateControlParams.qpMaxB = 44;
            h264enc_params->rateControlParams.qpMinB = 10;
            h264enc_params->rateControlParams.allowFrameSkip = 0;
            h264enc_params->rateControlParams.removeExpensiveCoeff = 0;
            h264enc_params->rateControlParams.chromaQPIndexOffset = 0;
            h264enc_params->rateControlParams.IPQualityFactor = IH264_QUALITY_FACTOR_DEFAULT; // 0
            h264enc_params->rateControlParams.initialBufferLevel = 64000;
            h264enc_params->rateControlParams.HRDBufferSize = 64000;
            h264enc_params->rateControlParams.minPicSizeRatioI = 0;
            h264enc_params->rateControlParams.maxPicSizeRatioI = 20;
            h264enc_params->rateControlParams.minPicSizeRatioP = 0;
            h264enc_params->rateControlParams.maxPicSizeRatioP = 0;
            h264enc_params->rateControlParams.minPicSizeRatioB = 0;
            h264enc_params->rateControlParams.maxPicSizeRatioB = 0;
            h264enc_params->rateControlParams.enablePRC = 1;
            h264enc_params->rateControlParams.enablePartialFrameSkip = 0;
            h264enc_params->rateControlParams.discardSavedBits = 0;
            h264enc_params->rateControlParams.reserved = 0;
            h264enc_params->rateControlParams.VBRDuration = 8;
            h264enc_params->rateControlParams.VBRsensitivity = 0;
            h264enc_params->rateControlParams.skipDistributionWindowLength = 5;
            h264enc_params->rateControlParams.numSkipInDistributionWindow =1;
            h264enc_params->rateControlParams.enableHRDComplianceMode = 1;
            h264enc_params->rateControlParams.frameSkipThMulQ5 = 0;
            h264enc_params->rateControlParams.vbvUseLevelThQ5 = 0;
            h264enc_params->rateControlParams.reservedRC[0] = 0;
            h264enc_params->rateControlParams.reservedRC[1] = 0;
            h264enc_params->rateControlParams.reservedRC[2] = 0;

            //intercoding coding params
            h264enc_params->interCodingParams.interCodingPreset = IH264_INTERCODING_USERDEFINED;
            h264enc_params->interCodingParams.searchRangeHorP = 144;
            h264enc_params->interCodingParams.searchRangeVerP = 32;
            h264enc_params->interCodingParams.searchRangeHorB = 144;
            h264enc_params->interCodingParams.searchRangeVerB = 16;
            h264enc_params->interCodingParams.interCodingBias = IH264_BIASFACTOR_DEFAULT;
            h264enc_params->interCodingParams.skipMVCodingBias = IH264_BIASFACTOR_MILD;
            h264enc_params->interCodingParams.minBlockSizeP = IH264_BLOCKSIZE_8x8;
            h264enc_params->interCodingParams.minBlockSizeB = IH264_BLOCKSIZE_8x8;
            h264enc_params->interCodingParams.meAlgoMode = IH264ENC_MOTIONESTMODE_DEFAULT;

            //intra coding params.
            h264enc_params->intraCodingParams.intraCodingPreset = IH264_INTRACODING_DEFAULT;
            h264enc_params->intraCodingParams.lumaIntra4x4Enable = 0;
            h264enc_params->intraCodingParams.lumaIntra8x8Enable = 0x0FF;
            h264enc_params->intraCodingParams.lumaIntra16x16Enable = 0;  // BASE PROFILE
            h264enc_params->intraCodingParams.chromaIntra8x8Enable = 0;  // BASE PROFILE
            h264enc_params->intraCodingParams.chromaComponentEnable = IH264_CHROMA_COMPONENT_CB_CR_BOTH;  // BASE PROFILE
            h264enc_params->intraCodingParams.intraRefreshMethod = IH264_INTRAREFRESH_DEFAULT;
            h264enc_params->intraCodingParams.intraRefreshRate = 0;
            h264enc_params->intraCodingParams.gdrOverlapRowsBtwFrames = 0;
            h264enc_params->intraCodingParams.constrainedIntraPredEnable = 0;
            h264enc_params->intraCodingParams.intraCodingBias = IH264ENC_INTRACODINGBIAS_DEFAULT;

            //NALU Control Params.
            h264enc_params->nalUnitControlParams.naluControlPreset = IH264_NALU_CONTROL_USERDEFINED;
            h264enc_params->nalUnitControlParams.naluPresentMaskStartOfSequence = 0x01A0; // 416
            h264enc_params->nalUnitControlParams.naluPresentMaskIDRPicture = 0x0020; //32
            h264enc_params->nalUnitControlParams.naluPresentMaskIntraPicture = 2;
            h264enc_params->nalUnitControlParams.naluPresentMaskNonIntraPicture = 2;
            h264enc_params->nalUnitControlParams.naluPresentMaskEndOfSequence = 0x0C00; // 3072

            //Slice coding params
            h264enc_params->sliceCodingParams.sliceCodingPreset = IH264_SLICECODING_DEFAULT;
            h264enc_params->sliceCodingParams.sliceMode = IH264_SLICEMODE_DEFAULT;
            h264enc_params->sliceCodingParams.sliceUnitSize = 0;
            h264enc_params->sliceCodingParams.sliceStartOffset[0] = 0;
            h264enc_params->sliceCodingParams.sliceStartOffset[1] = 0;
            h264enc_params->sliceCodingParams.sliceStartOffset[2] = 0;
            h264enc_params->sliceCodingParams.streamFormat = IH264_STREAM_FORMAT_DEFAULT;

            //Loop Filter Params
            h264enc_params->loopFilterParams.loopfilterPreset = IH264_LOOPFILTER_DEFAULT;
            h264enc_params->loopFilterParams.loopfilterDisableIDC = IH264_DISABLE_FILTER_DEFAULT;
            h264enc_params->loopFilterParams.filterOffsetA = 0;
            h264enc_params->loopFilterParams.filterOffsetB = 0;

            //fmo coding params
            h264enc_params->fmoCodingParams.fmoCodingPreset = IH264_FMOCODING_DEFAULT;
            h264enc_params->fmoCodingParams.numSliceGroups = 1;
            h264enc_params->fmoCodingParams.sliceGroupMapType = IH264_SLICE_GRP_MAP_DEFAULT; // 4
            h264enc_params->fmoCodingParams.sliceGroupChangeDirectionFlag = IH264ENC_SLICEGROUP_CHANGE_DIRECTION_DEFAULT;
            h264enc_params->fmoCodingParams.sliceGroupChangeRate = 0;
            h264enc_params->fmoCodingParams.sliceGroupChangeCycle = 0;
            h264enc_params->fmoCodingParams.sliceGroupParams[0] = 0;
            h264enc_params->fmoCodingParams.sliceGroupParams[1] = 0;

            //VUI Control Params
            h264enc_params->vuiCodingParams.vuiCodingPreset = IH264_VUICODING_DEFAULT;
            h264enc_params->vuiCodingParams.aspectRatioInfoPresentFlag = 0;
            h264enc_params->vuiCodingParams.aspectRatioIdc = 0;
            h264enc_params->vuiCodingParams.videoSignalTypePresentFlag = 0;
            h264enc_params->vuiCodingParams.videoFormat = IH264ENC_VIDEOFORMAT_NTSC;
            h264enc_params->vuiCodingParams.videoFullRangeFlag = 0;
            h264enc_params->vuiCodingParams.timingInfoPresentFlag = 0;
            h264enc_params->vuiCodingParams.hrdParamsPresentFlag = 0;
            h264enc_params->vuiCodingParams.numUnitsInTicks= 1000;

            //Stereo Info Control Params
            h264enc_params->stereoInfoParams.stereoInfoPreset = IH264_STEREOINFO_DISABLE;
            h264enc_params->stereoInfoParams.topFieldIsLeftViewFlag = 1;
            h264enc_params->stereoInfoParams.viewSelfContainedFlag = 0;

            //Frame Packing SEI Params
            h264enc_params->framePackingSEIParams.framePackingPreset = IH264_FRAMEPACK_SEI_DISABLE;
            h264enc_params->framePackingSEIParams.framePackingType = IH264_FRAMEPACK_TYPE_DEFAULT;
            h264enc_params->framePackingSEIParams.frame0PositionX = 0;
            h264enc_params->framePackingSEIParams.frame0PositionY = 0;
            h264enc_params->framePackingSEIParams.frame1PositionX = 0;
            h264enc_params->framePackingSEIParams.frame1PositionY = 0;
            h264enc_params->framePackingSEIParams.reservedByte = 0;

            //SVC coding params
            h264enc_params->svcCodingParams.svcExtensionFlag = IH264_SVC_EXTENSION_FLAG_DISABLE;
            h264enc_params->svcCodingParams.dependencyID = 0;
            h264enc_params->svcCodingParams.qualityID = 0;
            h264enc_params->svcCodingParams.enhancementProfileID = 0;
            h264enc_params->svcCodingParams.layerIndex = 0;
            h264enc_params->svcCodingParams.refLayerDQId = 0;

            DEBUG("dce_alloc VIDENC2_Params successful h264enc_params=%p", h264enc_params);

            codec = VIDENC2_create(engine, "ivahd_h264enc", (VIDENC2_Params *)h264enc_params);
            break;

        case DCE_ENC_TEST_MPEG4 :
        case DCE_ENC_TEST_H263 :
            params->maxInterFrameInterval = 0;

            mpeg4enc_params = (IMPEG4ENC_Params *) params;

            mpeg4enc_params->useDataPartitioning = 0;
            mpeg4enc_params->useRvlc = 0;
            if( codec_switch == DCE_ENC_TEST_H263 ) {
                mpeg4enc_params->useShortVideoHeader = 1;
            } else {
                mpeg4enc_params->useShortVideoHeader = 0;
            }
            mpeg4enc_params->vopTimeIncrementResolution = 30;
            mpeg4enc_params->nonMultiple16RefPadMethod = IMPEG4_PAD_METHOD_MPEG4;
            mpeg4enc_params->pixelRange = IMPEG4ENC_PR_0_255;
            mpeg4enc_params->enableSceneChangeAlgo = IMPEG4ENC_SCDA_DISABLE;
            mpeg4enc_params->useVOS = 0;
            mpeg4enc_params->enableMONA = 0;
            mpeg4enc_params->enableAnalyticinfo = -1;
            mpeg4enc_params->debugTraceLevel = 0;
            mpeg4enc_params->lastNFramesToLog = 0;

            // IMPEG4ENC_RateControlParams
            mpeg4enc_params->rateControlParams.rateControlParamsPreset = IMPEG4_RATECONTROLPARAMS_DEFAULT;
            mpeg4enc_params->rateControlParams.rcAlgo = IMPEG4_RATECONTROLALGO_VBR;
            mpeg4enc_params->rateControlParams.qpI = 5;
            mpeg4enc_params->rateControlParams.qpP = 5;
            mpeg4enc_params->rateControlParams.seIntialQP = 5;
            mpeg4enc_params->rateControlParams.qpMax = 31;
            mpeg4enc_params->rateControlParams.qpMin = 1;
            mpeg4enc_params->rateControlParams.enablePerceptualQuantMode = 0;
            mpeg4enc_params->rateControlParams.allowFrameSkip = 0;
            mpeg4enc_params->rateControlParams.initialBufferLevel = 0;
            mpeg4enc_params->rateControlParams.vbvBufferSize = 0;
            mpeg4enc_params->rateControlParams.qpMinIntra = 0;

            // IMPEG4ENC_InterCodingParams
            mpeg4enc_params->interCodingParams.interCodingPreset = IMPEG4_INTERCODING_DEFAULT;
            mpeg4enc_params->interCodingParams.searchRangeHorP = 144;
            mpeg4enc_params->interCodingParams.searchRangeVerP = 32;
            mpeg4enc_params->interCodingParams.globalOffsetME = 1;
            mpeg4enc_params->interCodingParams.earlySkipThreshold = 200;
            mpeg4enc_params->interCodingParams.enableThresholdingMethod = 1;
            mpeg4enc_params->interCodingParams.minBlockSizeP = IMPEG4_BLOCKSIZE_8x8;
            mpeg4enc_params->interCodingParams.enableRoundingControl = 1;

            // IMPEG4ENC_IntraCodingParams
            mpeg4enc_params->intraCodingParams.intraCodingPreset = IMPEG4_INTRACODING_DEFAULT;
            mpeg4enc_params->intraCodingParams.intraRefreshMethod = 0;
            mpeg4enc_params->intraCodingParams.intraRefreshRate = 0;
            mpeg4enc_params->intraCodingParams.acpredEnable = 1;
            mpeg4enc_params->intraCodingParams.insertGOVHdrBeforeIframe = 0;
            mpeg4enc_params->intraCodingParams.enableDriftControl = 1;

            // IMPEG4ENC_sliceCodingParams
            mpeg4enc_params->sliceCodingParams.sliceCodingPreset = IMPEG4_SLICECODING_DEFAULT;
            mpeg4enc_params->sliceCodingParams.sliceMode = IMPEG4_SLICEMODE_NONE;
            mpeg4enc_params->sliceCodingParams.sliceUnitSize = 0;
            mpeg4enc_params->sliceCodingParams.gobInterval = 0;
            mpeg4enc_params->sliceCodingParams.useHec = 0;

            DEBUG("dce_alloc VIDENC2_Params successful mpeg4enc_params=%p", mpeg4enc_params);

            codec = VIDENC2_create(engine, "ivahd_mpeg4enc", (VIDENC2_Params *)mpeg4enc_params);
            break;
        default :
            ERROR("Unrecognized codec to encode");
    }

    if( !codec ) {
        ERROR("DCE_ENCODE_TEST_FAIL: CODEC CREATION FAILED");
        goto out;
    }

    DEBUG("VIDENC2_create successful codec=%p", codec);

/*
 * inArgs and outArgs configuration for dynamic parameter passed during codec control.
 */
    switch( codec_switch ) {
        case DCE_ENC_TEST_H264 :
            dynParams = dce_alloc(sizeof(IH264ENC_DynamicParams));
            dynParams->size = sizeof(IH264ENC_DynamicParams);
            DEBUG("dce_alloc dynParams successful dynParams=%p size=%d", dynParams, dynParams->size);

            break;
        case DCE_ENC_TEST_MPEG4 :
        case DCE_ENC_TEST_H263 :
            dynParams = dce_alloc(sizeof(IMPEG4ENC_DynamicParams));
            dynParams->size = sizeof(IMPEG4ENC_DynamicParams);
            break;
        default :
            ERROR("Unrecognized codec to encode");
    }

    dynParams->inputHeight  = height;
    dynParams->inputWidth  = width;
    dynParams->refFrameRate = 15000; // refFrameRate in fps * 1000
    dynParams->targetFrameRate= 15000; // Target frame rate in fps * 1000
    dynParams->targetBitRate = 64000;
    dynParams->intraFrameInterval = 15; //Only 1st frame to be intra frame (I-frame)
    dynParams->generateHeader = XDM_ENCODE_AU;
    dynParams->captureWidth = width;
    dynParams->forceFrame = IVIDEO_NA_FRAME;
    dynParams->sampleAspectRatioHeight = 1;
    dynParams->sampleAspectRatioWidth = 1;
    dynParams->ignoreOutbufSizeFlag = XDAS_FALSE;  // If this is XDAS_TRUE then getBufferFxn and getBufferHandle needs to be set.
    dynParams->putDataFxn = NULL;
    dynParams->putDataHandle = NULL;
    dynParams->getDataFxn = NULL;
    dynParams->getDataHandle = NULL;
    dynParams->getBufferFxn = NULL;
    dynParams->getBufferHandle = NULL;
    dynParams->lateAcquireArg = -1;

#ifdef GETVERSION
    // Allocating TILER 1D to store the Codec version information from Codec.
    char *codec_version = NULL;
    codec_version = tiler_alloc(VERSION_SIZE, 0);
#endif

    switch( codec_switch ) {
        case DCE_ENC_TEST_H264 :
            dynParams->interFrameInterval = 1; // 2 B frames
            dynParams->mvAccuracy = IVIDENC2_MOTIONVECTOR_QUARTERPEL; //IVIDENC2_MotionVectorAccuracy

            DEBUG("dce_alloc IH264ENC_DynamicParams successful size %d dynParams=%p", dynParams->size, dynParams);
            h264enc_dynParams = (IH264ENC_DynamicParams *) dynParams;

            h264enc_dynParams->sliceGroupChangeCycle = 0;
            h264enc_dynParams->searchCenter.x = 0x7FFF; // or 32767
            h264enc_dynParams->searchCenter.y = 0x7FFF; // or 32767
            h264enc_dynParams->enableStaticMBCount = 0;
            h264enc_dynParams->enableROI = 0;
            h264enc_dynParams->reservedDynParams[0] = 0;
            h264enc_dynParams->reservedDynParams[1] = 0;
            h264enc_dynParams->reservedDynParams[2] = 0;

            //Rate Control Params
            h264enc_dynParams->rateControlParams.rateControlParamsPreset = IH264_RATECONTROLPARAMS_EXISTING;
            h264enc_dynParams->rateControlParams.scalingMatrixPreset = IH264_SCALINGMATRIX_NONE;
            h264enc_dynParams->rateControlParams.rcAlgo = IH264_RATECONTROL_DEFAULT;
            h264enc_dynParams->rateControlParams.qpI = 28;
            h264enc_dynParams->rateControlParams.qpMaxI = 36;
            h264enc_dynParams->rateControlParams.qpMinI = 10;
            h264enc_dynParams->rateControlParams.qpP = 28;
            h264enc_dynParams->rateControlParams.qpMaxP = 40;
            h264enc_dynParams->rateControlParams.qpMinP = 10;
            h264enc_dynParams->rateControlParams.qpOffsetB = 4;
            h264enc_dynParams->rateControlParams.qpMaxB = 44;
            h264enc_dynParams->rateControlParams.qpMinB = 10;
            h264enc_dynParams->rateControlParams.allowFrameSkip = 0;
            h264enc_dynParams->rateControlParams.removeExpensiveCoeff = 0;
            h264enc_dynParams->rateControlParams.IPQualityFactor = IH264_QUALITY_FACTOR_DEFAULT;
            h264enc_dynParams->rateControlParams.chromaQPIndexOffset = 0;
            h264enc_dynParams->rateControlParams.initialBufferLevel = 64000;
            h264enc_dynParams->rateControlParams.HRDBufferSize = 64000;
            h264enc_dynParams->rateControlParams.enablePartialFrameSkip = 0;
            h264enc_dynParams->rateControlParams.minPicSizeRatioI = 0;
            h264enc_dynParams->rateControlParams.maxPicSizeRatioI = 20;
            h264enc_dynParams->rateControlParams.minPicSizeRatioP = 0;
            h264enc_dynParams->rateControlParams.maxPicSizeRatioP = 0;
            h264enc_dynParams->rateControlParams.minPicSizeRatioB = 0;
            h264enc_dynParams->rateControlParams.maxPicSizeRatioB = 0;
            h264enc_dynParams->rateControlParams.enablePRC = 1;
            h264enc_dynParams->rateControlParams.enableHRDComplianceMode = 0;
            h264enc_dynParams->rateControlParams.reserved = 0;
            h264enc_dynParams->rateControlParams.VBRDuration = 8;
            h264enc_dynParams->rateControlParams.VBRsensitivity = 0;
            h264enc_dynParams->rateControlParams.skipDistributionWindowLength = 5;
            h264enc_dynParams->rateControlParams.numSkipInDistributionWindow = 1;
            h264enc_dynParams->rateControlParams.enableHRDComplianceMode = 1;
            h264enc_dynParams->rateControlParams.frameSkipThMulQ5 = 0;
            h264enc_dynParams->rateControlParams.vbvUseLevelThQ5 = 0;
            h264enc_dynParams->rateControlParams.reservedRC[0] = 0;
            h264enc_dynParams->rateControlParams.reservedRC[1] = 0;
            h264enc_dynParams->rateControlParams.reservedRC[2] = 0;

            //Inter Coding Params
            h264enc_dynParams->interCodingParams.interCodingPreset = IH264_INTERCODING_EXISTING;
            h264enc_dynParams->interCodingParams.searchRangeHorP = 144;
            h264enc_dynParams->interCodingParams.searchRangeVerP = 32;
            h264enc_dynParams->interCodingParams.searchRangeHorB = 144;
            h264enc_dynParams->interCodingParams.searchRangeVerB = 16;
            h264enc_dynParams->interCodingParams.interCodingBias= IH264_BIASFACTOR_DEFAULT;
            h264enc_dynParams->interCodingParams.skipMVCodingBias = IH264_BIASFACTOR_MILD;
            h264enc_dynParams->interCodingParams.minBlockSizeP = IH264_BLOCKSIZE_8x8;
            h264enc_dynParams->interCodingParams.minBlockSizeB = IH264_BLOCKSIZE_8x8;
            h264enc_dynParams->interCodingParams.meAlgoMode = IH264ENC_MOTIONESTMODE_DEFAULT;

            //Intra Coding Params
            h264enc_dynParams->intraCodingParams.intraCodingPreset = IH264_INTRACODING_EXISTING;
            h264enc_dynParams->intraCodingParams.lumaIntra4x4Enable = 0xFF; // or 255 BASE PROFILE
            h264enc_dynParams->intraCodingParams.lumaIntra8x8Enable = 0; // BASE PROFILE
            h264enc_dynParams->intraCodingParams.lumaIntra16x16Enable = 0;
            h264enc_dynParams->intraCodingParams.chromaIntra8x8Enable = 0;
            h264enc_dynParams->intraCodingParams.chromaComponentEnable = IH264_CHROMA_COMPONENT_CB_CR_BOTH;
            h264enc_dynParams->intraCodingParams.intraRefreshMethod = IH264_INTRAREFRESH_DEFAULT;
            h264enc_dynParams->intraCodingParams.intraRefreshRate = 0;
            h264enc_dynParams->intraCodingParams.gdrOverlapRowsBtwFrames = 0;
            h264enc_dynParams->intraCodingParams.constrainedIntraPredEnable = 0;
            h264enc_dynParams->intraCodingParams.intraCodingBias = IH264ENC_INTRACODINGBIAS_DEFAULT;

            //Slice Coding Params
            h264enc_dynParams->sliceCodingParams.sliceCodingPreset = IH264_SLICECODING_EXISTING;
            h264enc_dynParams->sliceCodingParams.sliceMode = IH264_SLICEMODE_DEFAULT;
            h264enc_dynParams->sliceCodingParams.sliceUnitSize = 0;
            h264enc_dynParams->sliceCodingParams.sliceStartOffset[0] = 0;
            h264enc_dynParams->sliceCodingParams.sliceStartOffset[1] = 0;
            h264enc_dynParams->sliceCodingParams.sliceStartOffset[2] = 0;
            h264enc_dynParams->sliceCodingParams.streamFormat = IH264_STREAM_FORMAT_DEFAULT;

            status = dce_alloc(sizeof(IH264ENC_Status));
            status->size = sizeof(IH264ENC_Status);
            DEBUG("dce_alloc IH264ENC_Status successful status=%p", status);

#ifdef GETVERSION
            status->data.buf = (XDAS_Int8 *) codec_version;
            status->data.bufSize = VERSION_SIZE;

            h264enc_status = (IH264ENC_Status *) status;
            DEBUG("IH264ENC_Status successful h264enc_status=%p", h264enc_status);
            err = VIDENC2_control(codec, XDM_GETVERSION, (VIDENC2_DynamicParams *) h264enc_dynParams, (VIDENC2_Status *) h264enc_status);
            DEBUG("VIDENC2_control IH264ENC_Status XDM_GETVERSION h264enc_status->data.buf = %s", (((VIDENC2_Status *)h264enc_status)->data.buf));
#endif

            h264enc_status = (IH264ENC_Status *) status;
            err = VIDENC2_control(codec, XDM_SETPARAMS, (VIDENC2_DynamicParams *) h264enc_dynParams, (VIDENC2_Status *) h264enc_status);
            DEBUG("dce_alloc IH264ENC_Status successful h264enc_status=%p", h264enc_status);
            break;

        case DCE_ENC_TEST_MPEG4 :
        case DCE_ENC_TEST_H263 :
            dynParams->interFrameInterval = 0;
            dynParams->mvAccuracy = IVIDENC2_MOTIONVECTOR_HALFPEL; //IVIDENC2_MotionVectorAccuracy

            DEBUG("dce_alloc IMPEG4ENC_DynamicParams successful size %d dynParams=%p", dynParams->size, dynParams);
            mpeg4enc_dynParams = (IMPEG4ENC_DynamicParams *) dynParams;

            mpeg4enc_dynParams->aspectRatioIdc = IMPEG4ENC_ASPECTRATIO_SQUARE;

            // IMPEG4ENC_RateControlParams
            memcpy(&mpeg4enc_dynParams->rateControlParams, &mpeg4enc_params->rateControlParams, sizeof(IMPEG4ENC_RateControlParams));
            // IMPEG4ENC_InterCodingParams
            memcpy(&mpeg4enc_dynParams->interCodingParams, &mpeg4enc_params->interCodingParams, sizeof(IMPEG4ENC_InterCodingParams));
            // IMPEG4ENC_sliceCodingParams
            memcpy(&mpeg4enc_dynParams->sliceCodingParams, &mpeg4enc_params->sliceCodingParams, sizeof(IMPEG4ENC_sliceCodingParams));

            status = dce_alloc(sizeof(IMPEG4ENC_Status));
            status->size = sizeof(IMPEG4ENC_Status);
            DEBUG("dce_alloc IMPEG4ENC_Status successful status=%p", status);

#ifdef GETVERSION
            status->data.buf = (XDAS_Int8 *) codec_version;
            status->data.bufSize = VERSION_SIZE;

            mpeg4enc_status = (IMPEG4ENC_Status *) status;
            DEBUG("IMPEG4ENC_Status successful mpeg4enc_status=%p status->data.buf %p ", mpeg4enc_status, status->data.buf);
            err = VIDENC2_control(codec, XDM_GETVERSION, (VIDENC2_DynamicParams *) mpeg4enc_dynParams, (VIDENC2_Status *) mpeg4enc_status);
            DEBUG("VIDENC2_control IMPEG4ENC_Status XDM_GETVERSION mpeg4enc_status->data.buf = %s ", (((VIDENC2_Status *)mpeg4enc_status)->data.buf));
#endif

            mpeg4enc_status = (IMPEG4ENC_Status *) status;
            DEBUG("dce_alloc IMPEG4ENC_Status successful mpeg4enc_status=%p", mpeg4enc_status);
            err = VIDENC2_control(codec, XDM_SETPARAMS, (VIDENC2_DynamicParams *) mpeg4enc_dynParams, (VIDENC2_Status *) mpeg4enc_status);
            DEBUG("Codec_control returned err=%d, extendedError=%08x", err, mpeg4enc_status->videnc2Status.extendedError);
            break;
        default :
            ERROR("Unrecognized codec to encode");
    }

    if( err ) {
        ERROR("DCE_ENCODE_TEST_FAIL: CODEC CONTROL FAILED %d",err);
        goto shutdown;
    }

    DEBUG("VIDENC2_control XDM_SETPARAMS successful");

#ifdef GETVERSION
    if( codec_version ) {
        MemMgr_Free(codec_version);
    }
#endif

    // XDM_GETBUFINFO
    // Send Control cmd XDM_GETBUFINFO to get min output and output size
    err = VIDENC2_control(codec, XDM_GETBUFINFO, dynParams, status);
    DEBUG("VIDENC2_control - XDM_GETBUFINFO err %d status numOutBuf %d OutBufSize %d MVBufInfo %d", err, status->bufInfo.minNumOutBufs, status->bufInfo.minOutBufSize[0].bytes, status->bufInfo.minOutBufSize[1].bytes);

/*
 * outBufs handling
 */
    outBufs = dce_alloc(sizeof(XDM2_BufDesc));
    output_size = status->bufInfo.minOutBufSize[0].bytes;
    mvbufinfo_size = status->bufInfo.minOutBufSize[1].bytes;

    if( codec_switch == DCE_ENC_TEST_H264 ) {
        outBufs->numBufs = status->bufInfo.minNumOutBufs;          // this value is 1
    } else if( (codec_switch == DCE_ENC_TEST_MPEG4) || (codec_switch == DCE_ENC_TEST_H263) ) {
        outBufs->numBufs = 1;
    }

    if( tiler ) {
        DEBUG("Output allocate through TILER 1D");
        output = tiler_alloc(output_size, 0);

    } else {
        DEBUG("Output allocate through NON-TILER");
        err = allocate_nonTiler(&output_nonTiler, output_size);
        if( err < 0 ) {
            ERROR("DCE_ENCODE_TEST_FAIL: NON_TILER ALLOCATION FAILED %d",err);
            goto shutdown;
        }
        output = (char*) output_nonTiler.vir_addr;
    }

    outBufs->descs[0].buf = (XDAS_Int8 *)output;
    outBufs->descs[0].memType = XDM_MEMTYPE_RAW;
    outBufs->descs[0].bufSize.bytes = output_size;

    DEBUG("Is TILER %d outBufs->descs[0].buf %p output %p output_nonTiler %p mvbufinfo_size %d", tiler, outBufs->descs[0].buf, output, &output_nonTiler, mvbufinfo_size);

    if( mvbufinfo_size > 0 ) {
        if( tiler ) {
            output_mvbuf = tiler_alloc(mvbufinfo_size, 0);
            DEBUG("MVBufInfo: TILER outBufs->descs[1].buf %p output_mvbuf %p", outBufs->descs[1].buf, output_mvbuf);
        }
        else {
            err = allocate_nonTiler(&output_mvbuf_nonTiler, mvbufinfo_size);
            if( err < 0 ) {
                ERROR("DCE_ENCODE_TEST_FAIL: NON_TILER ALLOCATION FAILED %d",err);
                goto shutdown;
            }
            output_mvbuf = (char*) output_mvbuf_nonTiler.vir_addr;
            DEBUG("MVBufInfo: NON-TILER outBufs->descs[1].buf %p output_mvbuf %p", outBufs->descs[1].buf, output_mvbuf);
        }
        outBufs->descs[1].buf = (XDAS_Int8 *)output_mvbuf;
        outBufs->descs[1].memType = XDM_MEMTYPE_RAW;
        outBufs->descs[1].bufSize.bytes = mvbufinfo_size;
   }
    DEBUG("output_mvbuf %p output_mvbuf_nonTiler %p", output_mvbuf, &output_mvbuf_nonTiler);

#ifdef DUMPINPUTDATA
    char          Buff1[100];
    int static    GlobalCount1 = 0;
#endif

#ifdef PROFILE_TIME
    total_init_time = (uint64_t)mark_microsecond(&init_start_time);
    INFO("total_init_time %llu output_alloc_time %llu actual init time in: %lld us", total_init_time, output_alloc_time, total_init_time  - output_alloc_time);
#endif

/*
 * codec process
 */
    while( inBufs->numPlanes && outBufs->numBufs ) {
        int    n;
        DEBUG("Looping on reading input inBufs->numPlanes %d outBufs->numBufs %d", inBufs->numPlanes, outBufs->numBufs);

        //Read the NV12 frame to input buffer to be encoded.
        n = read_input(in_pattern, in_cnt, buf->buf);

        if( n && (n != -1) ) {
            eof = 0;
            inBufs->planeDesc[0].buf = (XDAS_Int8 *)buf->y;
            inBufs->planeDesc[1].buf = (XDAS_Int8 *)buf->uv;
            DEBUG("inBufs->planeDesc[0].buf %p inBufs->planeDesc[1].buf %p", inBufs->planeDesc[0].buf, inBufs->planeDesc[1].buf);
            DEBUG("push: %d (plane[0]= %d + plane[1]= %d = %d bytes) (%p)", in_cnt, inBufs->planeDesc[0].bufSize.bytes, inBufs->planeDesc[1].bufSize.bytes, n, buf);
            in_cnt++;

            /*
                       * Input buffer has data to be encoded.
                       */
            inArgs->inputID = (XDAS_Int32)buf;
            if( codec_switch == DCE_ENC_TEST_H264 ) {
                h264enc_inArgs = (IH264ENC_InArgs *) inArgs;
                DEBUG("TEST inArgs->inputID %d h264enc_inArgs->videnc2InArgs.inputID %d", inArgs->inputID, h264enc_inArgs->videnc2InArgs.inputID);
            } else if( (codec_switch == DCE_ENC_TEST_MPEG4) || (codec_switch == DCE_ENC_TEST_H263) ) {
                mpeg4enc_inArgs = (IMPEG4ENC_InArgs *) inArgs;
                DEBUG("TEST inArgs->inputID %d mpeg4enc_inArgs->videnc2InArgs.inputID %d", inArgs->inputID, mpeg4enc_inArgs->videnc2InArgs.inputID);
            }
        } else if( n == -1 ) {

            // Set EOF as 1 to ensure flush completes
            eof = 1;
            in_cnt++;

            DEBUG("n == -1 - go to shutdown");

            goto shutdown;

            switch( codec_switch ) {
                case DCE_ENC_TEST_H264 :
                    DEBUG("Calling VIDENC2_control XDM_FLUSH h264enc_dynParams %p h264enc_status %p", h264enc_dynParams, h264enc_status);
                    err = VIDENC2_control(codec, XDM_FLUSH, (VIDENC2_DynamicParams *) h264enc_dynParams, (VIDENC2_Status *) h264enc_status);
                    break;
                case DCE_ENC_TEST_MPEG4 :
                case DCE_ENC_TEST_H263 :
                    DEBUG("Calling VIDENC2_control XDM_FLUSH mpeg4enc_dynParams %p mpeg4enc_status %p", mpeg4enc_dynParams, mpeg4enc_status);
                    err = VIDENC2_control(codec, XDM_FLUSH, (VIDENC2_DynamicParams *) mpeg4enc_dynParams, (VIDENC2_Status *) mpeg4enc_status);
                    break;
                default :
                    ERROR("Unrecognized codec to encode");
            }

            /* We have sent the XDM_FLUSH, call VIDENC2_process until we get
                       * an error of XDM_EFAIL which tells us there are no more buffers
                       * at codec level.
                       */

            inArgs->inputID = 0;
            if( codec_switch == DCE_ENC_TEST_H264 ) {
                h264enc_inArgs = (IH264ENC_InArgs *) inArgs;
            } else if( (codec_switch == DCE_ENC_TEST_MPEG4) || (codec_switch == DCE_ENC_TEST_H263) ) {
                mpeg4enc_inArgs = (IMPEG4ENC_InArgs *) inArgs;
            }
            inBufs->planeDesc[0].buf = NULL;
            inBufs->planeDesc[0].bufSize.bytes = 0;
            inBufs->planeDesc[1].buf = NULL;
            inBufs->planeDesc[1].bufSize.bytes = 0;
            outBufs->descs[0].buf = NULL;
            outBufs->descs[1].buf = NULL;
        } else {
            /* end of input..  (n == 0) */
            inBufs->numPlanes = 0;
            eof = 1;
            DEBUG("n == 0 - go to shutdown");

            goto shutdown;

            switch( codec_switch ) {
                case DCE_ENC_TEST_H264 :
                    DEBUG("Calling VIDENC2_control XDM_FLUSH h264enc_dynParams %p h264enc_status %p", h264enc_dynParams, h264enc_status);
                    err = VIDENC2_control(codec, XDM_FLUSH, (VIDENC2_DynamicParams *) h264enc_dynParams, (VIDENC2_Status *) h264enc_status);
                    break;
                case DCE_ENC_TEST_MPEG4 :
                case DCE_ENC_TEST_H263 :
                    DEBUG("Calling VIDENC2_control XDM_FLUSH mpeg4enc_dynParams %p mpeg4enc_status %p", mpeg4enc_dynParams, mpeg4enc_status);
                    err = VIDENC2_control(codec, XDM_FLUSH, (VIDENC2_DynamicParams *) mpeg4enc_dynParams, (VIDENC2_Status *) mpeg4enc_status);
                    break;
                default :
                    ERROR("Unrecognized codec to encode");
            }

            /* We have sent the XDM_FLUSH, call VIDENC2_process until we get
                       * an error of XDM_EFAIL which tells us there are no more buffers
                       * at codec level.
                       */

            inArgs->inputID = 0;
            if( codec_switch == DCE_ENC_TEST_H264 ) {
                h264enc_inArgs = (IH264ENC_InArgs *) inArgs;
            } else if( (codec_switch == DCE_ENC_TEST_MPEG4) || (codec_switch == DCE_ENC_TEST_H263) ) {
                mpeg4enc_inArgs = (IMPEG4ENC_InArgs *) inArgs;
            }
            inBufs->planeDesc[0].buf = NULL;
            inBufs->planeDesc[0].bufSize.bytes = 0;
            inBufs->planeDesc[1].buf = NULL;
            inBufs->planeDesc[1].bufSize.bytes = 0;
            outBufs->descs[0].buf = NULL;
        }

#ifdef DUMPINPUTDATA
        DEBUG("input data buf->buf[%p]", buf->buf);

        //Dump the file
        if( inputDump == NULL ) {

            if( GlobalCount1 <= 50 ) {
                DEBUG("[DCE_ENC_TEST] GlobalCount1 %d\n", GlobalCount1);
                sprintf(Buff1, "/sd/dce_enc_dump/dceinputdump%d.bin", GlobalCount1);
                inputDump = fopen(Buff1, "wb+");
                //DEBUG("input data dump file open %p errno %d", inputDump, errno);
                if( inputDump == NULL ) {
                    DEBUG("Opening input Dump /sd/dce_enc_dump/dceinputdump.bin file FAILED");
                } else {
                    GlobalCount1++;
                    //DEBUG("Before Input [%p]\n", input);
                    fwrite(buf->buf, 1, 4096 * height * 3 / 2, inputDump);
                    DEBUG("Dumping input file of NV12 format with read data of %d inside buffersize = %d", n, 4096 * height * 3 / 2);

                    fflush(inputDump);
                    fclose(inputDump);
                    inputDump = NULL;
                }
            }
            //DEBUG("input data dump file open %p Successful", inputDump);
        }
#endif

        int    iters = 0;

        do {

#ifdef H264_DEBUG

            DEBUG(">> Going to call VIDENC2_process codec=%p, inBufs=%p, outBufs=%p, h264enc_inArgs=%p, h264enc_outArgs=%p",
                  codec, inBufs, outBufs, h264enc_inArgs, h264enc_outArgs);

            DEBUG("Calling VIDENC2_process inArgs->inputID=%x inBufs->descs[0].buf %p inBufs->descs[0].bufSize %d",
                  inArgs->inputID, inBufs->planeDesc[0].buf, (int) inBufs->planeDesc[0].bufSize.bytes);

            DEBUG("Calling VIDENC2_process inArgs->inputID=%x inBufs->descs[1].buf %p inBufs->descs[1].bufSize %d",
                  inArgs->inputID, inBufs->planeDesc[1].buf, (int) inBufs->planeDesc[1].bufSize.bytes);

            DEBUG("h264enc_inArgs->videnc2InArgs.size %d inputID %d control %d ", h264enc_inArgs->videnc2InArgs.size,
                  h264enc_inArgs->videnc2InArgs.inputID, h264enc_inArgs->videnc2InArgs.control);
#endif

#ifdef PROFILE_TIME
            codec_process_time = mark_microsecond(NULL);
#endif

            if( codec_switch == DCE_ENC_TEST_H264 ) {
                err = VIDENC2_process(codec, inBufs, outBufs, (VIDENC2_InArgs *) h264enc_inArgs, (VIDENC2_OutArgs *) h264enc_outArgs);
                DEBUG("[DCE_ENC_TEST] VIDENC2_process - err %d", err);

                if( err < 0 ) {
                    int    i = 0;

                    for( i=0; i < IH264ENC_EXTERROR_NUM_MAXWORDS; i++ ) {
                        DEBUG("DETAIL EXTENDED ERROR h264enc_outArgs->extErrorCode[%d]=%08x", i, (uint)h264enc_outArgs->extErrorCode[i]);
                    }

                    err = VIDENC2_control(codec, XDM_GETSTATUS, (VIDENC2_DynamicParams *) h264enc_dynParams, (VIDENC2_Status *) h264enc_status);
                    DEBUG("[DCE_ENC_TEST] VIDENC2_control - XDM_GETSTATUS err %d", err);

                    for( i=0; i < IH264ENC_EXTERROR_NUM_MAXWORDS; i++ ) {
                        DEBUG("DETAIL EXTENDED ERROR h264enc_status->extErrorCode[%d]=%08x", i, (uint)h264enc_status->extErrorCode[i]);
                    }

                    if( XDM_ISFATALERROR(h264enc_outArgs->videnc2OutArgs.extendedError) ) {
                        ERROR("process returned error: %d\n", err);
                        //ERROR("extendedError: %08x", outArgs->extendedError);
                        ERROR("extendedError: %08x", h264enc_outArgs->videnc2OutArgs.extendedError);
                        ERROR("DCE_ENCODE_TEST_FAIL: CODEC FATAL ERROR");
                        goto shutdown;
                    } else if( eof ) {
                        //ERROR("Codec_process returned err=%d, extendedError=%08x", err, outArgs->extendedError);
                        ERROR("Codec_process returned err=%d, extendedError=%08x", err, h264enc_outArgs->videnc2OutArgs.extendedError);
                        err = XDM_EFAIL;

                        if( err == XDM_EFAIL ) {
                            DEBUG("-------------------- Flush completed------------------------");
                        }
                    } else {
                        //DEBUG("Non-fatal err=%d, extendedError=%08x", err, outArgs->extendedError);
                        DEBUG("Non-fatal err=%d, h264enc_outArgs->videnc2OutArgs.extendedError=%08x ", err, h264enc_outArgs->videnc2OutArgs.extendedError);
                        err = XDM_EOK;
                    }
                }

                DEBUG("bytesGenerated %d", h264enc_outArgs->videnc2OutArgs.bytesGenerated);
                bytesGenerated = h264enc_outArgs->videnc2OutArgs.bytesGenerated;
            } else if( codec_switch == DCE_ENC_TEST_MPEG4 || codec_switch == DCE_ENC_TEST_H263 ) {
                DEBUG("[DCE_ENC_TEST] codec %p inBufs %p outBufs %p mpeg4enc_inArgs %p mpeg4enc_outArgs %p", codec, inBufs, outBufs, mpeg4enc_inArgs, mpeg4enc_outArgs);
                err = VIDENC2_process(codec, inBufs, outBufs, (VIDENC2_InArgs *) mpeg4enc_inArgs, (VIDENC2_OutArgs *) mpeg4enc_outArgs);
                DEBUG("[DCE_ENC_TEST] VIDENC2_process - err %d", err);
                if( err < 0 ) {
                    //TODO error handling on MPEG4/H.263
                    ERROR("DCE_ENCODE_TEST_FAIL: CODEC PROCESS RETURNED ERROR : err=%d, extendedError=%08x", err, mpeg4enc_outArgs->videnc2OutArgs.extendedError);
                    goto shutdown;
                }
                DEBUG("\n bytesGenerated %d", mpeg4enc_outArgs->videnc2OutArgs.bytesGenerated);
                bytesGenerated = mpeg4enc_outArgs->videnc2OutArgs.bytesGenerated;
            }

#ifdef PROFILE_TIME
            INFO("processed returned in: %llu us", (uint64_t) mark_microsecond(&codec_process_time));
#endif

            /*
                  * Handling of output data from codec
                  */

            /* get the output buffer and write it to file */
            DEBUG("pop: %d (%p)", out_cnt, output);

            if( bytesGenerated ) {
                // write the frames to output file based on the value of frames_to_write on how many frames to write.
                if( out_cnt < frames_to_write ) {

#ifdef DUMPOUTPUTDATA
                    char          Buff2[100];
                    int static    GlobalCount2 = 0;
#endif

#ifdef DUMPOUTPUTDATA
                    DEBUG("Dumping output data output[%p]", output);

                    //Dump individual output file
                    if( outputDump == NULL ) {
                        if( GlobalCount2 <= 50 ) {
                            DEBUG("DCE_ENC_TEST GlobalCount2 %d\n", GlobalCount2);
                            sprintf(Buff2, "/sd/dce_enc_dump/dceoutputdump%d.bin", GlobalCount2);
                            outputDump = fopen(Buff2, "wb+");
                            //DEBUG("output data dump file open %p errno %d", outputDump, errno);
                            if( outputDump == NULL ) {
                                DEBUG("Opening output Dump /sd/dce_enc_dump/dceoutputdump.bin file FAILED");
                            } else {
                                GlobalCount2++;
                                fwrite(output, 1, bytesGenerated, outputDump);
                                DEBUG("Dumping output file of H.264 format of generated buffersize = %d", bytesGenerated);

                                fflush(outputDump);
                                fclose(outputDump);
                                outputDump = NULL;
                            }
                        }
                        //DEBUG("output data dump file open %p Successful", outputDump);
                    }
#endif

                    write_output(out_pattern, out_cnt++, output, bytesGenerated);
                } else {
                    out_cnt++;
                }
            }

            ++iters; // Guard for infinite VIDENC2_PROCESS loop when codec never return XDM_EFAIL
        } while( eof && (err != XDM_EFAIL) && (iters < 100));  // Multiple VIDENC2_process when eof until err == XDM_EFAIL

    }

shutdown:

    printf("\nDeleting encoder codec...\n");
    VIDENC2_delete(codec);

out:
    if( engine ) {
        Engine_close(engine);
    }
    if( params ) {
        dce_free(params);
    }
    if( dynParams ) {
        dce_free(dynParams);
    }
    if( status ) {
        dce_free(status);
    }
    if( inBufs ) {
        dce_free(inBufs);
    }
    if( outBufs ) {
        dce_free(outBufs);
    }
    if( inArgs ) {
        dce_free(inArgs);
    }
    if( outArgs ) {
        dce_free(outArgs);
    }

    printf("\nFreeing output %p... output_mvbuf %p...\n", output, output_mvbuf);
    if( tiler ) {
        if( output ) {
            MemMgr_Free(output);
        }

        if( output_mvbuf ){
            printf("\nFreeing output %p...\n", output_mvbuf);
            MemMgr_Free(output_mvbuf);
        }
    }
    else {
        if( output ) {
            printf("\nFreeing output_nonTiler %p...\n", &output_nonTiler);
            free_nonTiler(&output_nonTiler);
        }
        if( output_mvbuf ){
            printf("\nFreeing output_mvbuf_nonTiler %p...\n", &output_mvbuf_nonTiler);
            free_nonTiler(&output_mvbuf_nonTiler);
        }
    }

    printf("\nFreeing buf %p...\n", buf);
    if( buf ) {
        if( buf->buf ) {
            if( tiler ) {
                printf("\nFreeing buf->buf %p...\n", buf->buf);
                MemMgr_Free(buf->buf);
            } else {
                printf("\nFreeing input_nonTiler %p...\n", &input_nonTiler);
                free_nonTiler(&input_nonTiler);
            }
        }
        free(buf);
    }
    printf("DCE ENC test completed...\n");

    return (0);
}