summaryrefslogtreecommitdiff
path: root/mm-video/vidc/venc/test/venc_test.cpp
blob: 7afb5bd9045e2393d41c163cff6f669685eb75e1 (plain)
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
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
/*--------------------------------------------------------------------------
Copyright (c) 2010-2013, The Linux Foundation. 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 The Linux Foundation 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, FITNESS FOR A PARTICULAR PURPOSE AND
NON-INFRINGEMENT 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.
--------------------------------------------------------------------------*/
/*============================================================================
                    V E N C _ T E S T. C P P

DESCRIPTION

 This is the OMX test app .

REFERENCES

============================================================================*/

//usage
// FILE QVGA MP4 24 384000 100 enc_qvga.yuv QVGA_24.m4v
// FILE QCIF MP4 15 96000 0 foreman.qcif.yuv output_qcif.m4v
// FILE VGA MP4 24 1200000 218 enc_vga.yuv vga_output.m4v
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/mman.h>
//#include <sys/time.h>
#include <time.h>
#include <sys/ioctl.h>
#include <limits.h>
#include <string.h>
//#include <sys/stat.h>
#include "OMX_QCOMExtns.h"
#include "OMX_Core.h"

#define QCOM_EXT 1

#include "OMX_Core.h"
#include "OMX_Video.h"
#include "OMX_Component.h"
#include "camera_test.h"
#include "fb_test.h"
#include "venc_util.h"
#include "extra_data_handler.h"
#ifdef USE_ION
#include <linux/msm_ion.h>
#endif
#ifdef _MSM8974_
#include <media/msm_media_info.h>
#endif

//////////////////////////
// MACROS
//////////////////////////

#define CHK(result) if ((result != OMX_ErrorNone) && (result != OMX_ErrorNoMore)) { E("*************** error *************"); exit(0); }
#define TEST_LOG
#ifdef VENC_SYSLOG
#include <cutils/log.h>
/// Debug message macro
#define D(fmt, ...) ALOGE("venc_test Debug %s::%d "fmt,              \
                         __FUNCTION__, __LINE__,                        \
                         ## __VA_ARGS__)

/// Error message macro
#define E(fmt, ...) ALOGE("venc_test Error %s::%d "fmt,            \
                         __FUNCTION__, __LINE__,                      \
                         ## __VA_ARGS__)

#else
     #ifdef TEST_LOG
       #define D(fmt, ...) fprintf(stderr, "venc_test Debug %s::%d "fmt"\n",   \
                            __FUNCTION__, __LINE__,                     \
                            ## __VA_ARGS__)

     /// Error message macro
      #define E(fmt, ...) fprintf(stderr, "venc_test Error %s::%d "fmt"\n", \
                            __FUNCTION__, __LINE__,                   \
                            ## __VA_ARGS__)
     #else
      #define D(fmt, ...)
      #define E(fmt, ...)
         #endif

#endif

//////////////////////////
// CONSTANTS
//////////////////////////
static const int MAX_MSG = 100;
//#warning do not hardcode these use port definition
static const int PORT_INDEX_IN = 0;
static const int PORT_INDEX_OUT = 1;

static const int NUM_IN_BUFFERS = 10;
static const int NUM_OUT_BUFFERS = 10;

unsigned int num_in_buffers = 0;
unsigned int num_out_buffers = 0;

//////////////////////////
/* MPEG4 profile and level table*/
static const unsigned int mpeg4_profile_level_table[][5]=
{
    /*max mb per frame, max mb per sec, max bitrate, level, profile*/
    {99,1485,64000,OMX_VIDEO_MPEG4Level0,OMX_VIDEO_MPEG4ProfileSimple},
    {99,1485,64000,OMX_VIDEO_MPEG4Level1,OMX_VIDEO_MPEG4ProfileSimple},
    {396,5940,128000,OMX_VIDEO_MPEG4Level2,OMX_VIDEO_MPEG4ProfileSimple},
    {396,11880,384000,OMX_VIDEO_MPEG4Level3,OMX_VIDEO_MPEG4ProfileSimple},
    {1200,36000,4000000,OMX_VIDEO_MPEG4Level4a,OMX_VIDEO_MPEG4ProfileSimple},
    {1620,40500,8000000,OMX_VIDEO_MPEG4Level5,OMX_VIDEO_MPEG4ProfileSimple},
    {3600,108000,12000000,OMX_VIDEO_MPEG4Level5,OMX_VIDEO_MPEG4ProfileSimple},
#ifdef _MSM8974_
    {32400,972000,20000000,OMX_VIDEO_MPEG4Level5,OMX_VIDEO_MPEG4ProfileSimple},
    {34560,1036800,20000000,OMX_VIDEO_MPEG4Level5,OMX_VIDEO_MPEG4ProfileSimple},
#endif
    {0,0,0,0,0},

    {99,1485,128000,OMX_VIDEO_MPEG4Level0,OMX_VIDEO_MPEG4ProfileAdvancedSimple},
    {99,1485,128000,OMX_VIDEO_MPEG4Level1,OMX_VIDEO_MPEG4ProfileAdvancedSimple},
    {396,5940,384000,OMX_VIDEO_MPEG4Level2,OMX_VIDEO_MPEG4ProfileAdvancedSimple},
    {396,11880,768000,OMX_VIDEO_MPEG4Level3,OMX_VIDEO_MPEG4ProfileAdvancedSimple},
    {792,23760,3000000,OMX_VIDEO_MPEG4Level4,OMX_VIDEO_MPEG4ProfileAdvancedSimple},
    {1620,48600,8000000,OMX_VIDEO_MPEG4Level5,OMX_VIDEO_MPEG4ProfileAdvancedSimple},
#ifdef _MSM8974_
    {32400,972000,20000000,OMX_VIDEO_MPEG4Level5,OMX_VIDEO_MPEG4ProfileAdvancedSimple},
    {34560,1036800,20000000,OMX_VIDEO_MPEG4Level5,OMX_VIDEO_MPEG4ProfileAdvancedSimple},
#endif
    {0,0,0,0,0},
};

/* H264 profile and level table*/
static const unsigned int h264_profile_level_table[][5]=
{
     /*max mb per frame, max mb per sec, max bitrate, level, profile*/
    {99,1485,64000,OMX_VIDEO_AVCLevel1,OMX_VIDEO_AVCProfileBaseline},
    {99,1485,128000,OMX_VIDEO_AVCLevel1b,OMX_VIDEO_AVCProfileBaseline},
    {396,3000,192000,OMX_VIDEO_AVCLevel11,OMX_VIDEO_AVCProfileBaseline},
    {396,6000,384000,OMX_VIDEO_AVCLevel12,OMX_VIDEO_AVCProfileBaseline},
    {396,11880,768000,OMX_VIDEO_AVCLevel13,OMX_VIDEO_AVCProfileBaseline},
    {396,11880,2000000,OMX_VIDEO_AVCLevel2,OMX_VIDEO_AVCProfileBaseline},
    {792,19800,4000000,OMX_VIDEO_AVCLevel21,OMX_VIDEO_AVCProfileBaseline},
    {1620,20250,4000000,OMX_VIDEO_AVCLevel22,OMX_VIDEO_AVCProfileBaseline},
    {1620,40500,10000000,OMX_VIDEO_AVCLevel3,OMX_VIDEO_AVCProfileBaseline},
    {3600,108000,14000000,OMX_VIDEO_AVCLevel31,OMX_VIDEO_AVCProfileBaseline},
    {5120,216000,20000000,OMX_VIDEO_AVCLevel32,OMX_VIDEO_AVCProfileBaseline},
    {8192,245760,20000000,OMX_VIDEO_AVCLevel4,OMX_VIDEO_AVCProfileBaseline},
#ifdef _MSM8974_
    {32400,972000,20000000,OMX_VIDEO_AVCLevel4,OMX_VIDEO_AVCProfileBaseline},
    {34560,1036800,20000000,OMX_VIDEO_AVCLevel4,OMX_VIDEO_AVCProfileBaseline},
#endif
    {0,0,0,0,0},

    {99,1485,64000,OMX_VIDEO_AVCLevel1,OMX_VIDEO_AVCProfileHigh},
    {99,1485,160000,OMX_VIDEO_AVCLevel1b,OMX_VIDEO_AVCProfileHigh},
    {396,3000,240000,OMX_VIDEO_AVCLevel11,OMX_VIDEO_AVCProfileHigh},
    {396,6000,480000,OMX_VIDEO_AVCLevel12,OMX_VIDEO_AVCProfileHigh},
    {396,11880,960000,OMX_VIDEO_AVCLevel13,OMX_VIDEO_AVCProfileHigh},
    {396,11880,2500000,OMX_VIDEO_AVCLevel2,OMX_VIDEO_AVCProfileHigh},
    {792,19800,5000000,OMX_VIDEO_AVCLevel21,OMX_VIDEO_AVCProfileHigh},
    {1620,20250,5000000,OMX_VIDEO_AVCLevel22,OMX_VIDEO_AVCProfileHigh},
    {1620,40500,12500000,OMX_VIDEO_AVCLevel3,OMX_VIDEO_AVCProfileHigh},
    {3600,108000,17500000,OMX_VIDEO_AVCLevel31,OMX_VIDEO_AVCProfileHigh},
    {5120,216000,25000000,OMX_VIDEO_AVCLevel32,OMX_VIDEO_AVCProfileHigh},
    {8192,245760,25000000,OMX_VIDEO_AVCLevel4,OMX_VIDEO_AVCProfileHigh},
#ifdef _MSM8974_
    {32400,972000,20000000,OMX_VIDEO_AVCLevel4,OMX_VIDEO_AVCProfileHigh},
    {34560,1036800,20000000,OMX_VIDEO_AVCLevel4,OMX_VIDEO_AVCProfileHigh},
#endif
    {0,0,0,0,0},

    {99,1485,64000,OMX_VIDEO_AVCLevel1,OMX_VIDEO_AVCProfileMain},
    {99,1485,128000,OMX_VIDEO_AVCLevel1b,OMX_VIDEO_AVCProfileMain},
    {396,3000,192000,OMX_VIDEO_AVCLevel11,OMX_VIDEO_AVCProfileMain},
    {396,6000,384000,OMX_VIDEO_AVCLevel12,OMX_VIDEO_AVCProfileMain},
    {396,11880,768000,OMX_VIDEO_AVCLevel13,OMX_VIDEO_AVCProfileMain},
    {396,11880,2000000,OMX_VIDEO_AVCLevel2,OMX_VIDEO_AVCProfileMain},
    {792,19800,4000000,OMX_VIDEO_AVCLevel21,OMX_VIDEO_AVCProfileMain},
    {1620,20250,4000000,OMX_VIDEO_AVCLevel22,OMX_VIDEO_AVCProfileMain},
    {1620,40500,10000000,OMX_VIDEO_AVCLevel3,OMX_VIDEO_AVCProfileMain},
    {3600,108000,14000000,OMX_VIDEO_AVCLevel31,OMX_VIDEO_AVCProfileMain},
    {5120,216000,20000000,OMX_VIDEO_AVCLevel32,OMX_VIDEO_AVCProfileMain},
    {8192,245760,20000000,OMX_VIDEO_AVCLevel4,OMX_VIDEO_AVCProfileMain},
#ifdef _MSM8974_
    {32400,972000,20000000,OMX_VIDEO_AVCLevel4,OMX_VIDEO_AVCProfileMain},
    {34560,1036800,20000000,OMX_VIDEO_AVCLevel4,OMX_VIDEO_AVCProfileMain},
#endif
    {0,0,0,0,0}

};

/* H263 profile and level table*/
static const unsigned int h263_profile_level_table[][5]=
{
    /*max mb per frame, max mb per sec, max bitrate, level, profile*/
    {99,1485,64000,OMX_VIDEO_H263Level10,OMX_VIDEO_H263ProfileBaseline},
    {396,5940,128000,OMX_VIDEO_H263Level20,OMX_VIDEO_H263ProfileBaseline},
    {396,11880,384000,OMX_VIDEO_H263Level30,OMX_VIDEO_H263ProfileBaseline},
    {396,11880,2048000,OMX_VIDEO_H263Level40,OMX_VIDEO_H263ProfileBaseline},
    {99,1485,128000,OMX_VIDEO_H263Level45,OMX_VIDEO_H263ProfileBaseline},
    {396,19800,4096000,OMX_VIDEO_H263Level50,OMX_VIDEO_H263ProfileBaseline},
    {810,40500,8192000,OMX_VIDEO_H263Level60,OMX_VIDEO_H263ProfileBaseline},
    {1620,81000,16384000,OMX_VIDEO_H263Level70,OMX_VIDEO_H263ProfileBaseline},
#ifdef _MSM8974_
    {32400,972000,20000000,OMX_VIDEO_H263Level60,OMX_VIDEO_H263ProfileBaseline},
    {34560,1036800,20000000,OMX_VIDEO_H263Level70,OMX_VIDEO_H263ProfileBaseline},
#endif
    {0,0,0,0,0}
};
#ifdef _MSM8974_
static const unsigned int VP8_profile_level_table[][5]=
{
    /*max mb per frame, max mb per sec, max bitrate, level, profile*/
    {99,1485,64000,OMX_VIDEO_H263Level10,OMX_VIDEO_H263ProfileBaseline},
    {396,5940,128000,OMX_VIDEO_H263Level20,OMX_VIDEO_H263ProfileBaseline},
    {396,11880,384000,OMX_VIDEO_H263Level30,OMX_VIDEO_H263ProfileBaseline},
    {396,11880,2048000,OMX_VIDEO_H263Level40,OMX_VIDEO_H263ProfileBaseline},
    {99,1485,128000,OMX_VIDEO_H263Level45,OMX_VIDEO_H263ProfileBaseline},
    {396,19800,4096000,OMX_VIDEO_H263Level50,OMX_VIDEO_H263ProfileBaseline},
    {810,40500,8192000,OMX_VIDEO_H263Level60,OMX_VIDEO_H263ProfileBaseline},
    {1620,81000,16384000,OMX_VIDEO_H263Level70,OMX_VIDEO_H263ProfileBaseline},
    {32400,972000,20000000,OMX_VIDEO_H263Level70,OMX_VIDEO_H263ProfileBaseline},
    {34560,1036800,20000000,OMX_VIDEO_H263Level70,OMX_VIDEO_H263ProfileBaseline},
    {0,0,0,0,0}
};
#endif

#define Log2(number, power)  { OMX_U32 temp = number; power = 0; while( (0 == (temp & 0x1)) &&  power < 16) { temp >>=0x1; power++; } }
#define FractionToQ16(q,num,den) { OMX_U32 power; Log2(den,power); q = num << (16 - power); }

//////////////////////////
// TYPES
//////////////////////////
struct ProfileType
{
   OMX_VIDEO_CODINGTYPE eCodec;
   OMX_VIDEO_MPEG4LEVELTYPE eLevel;
   OMX_VIDEO_CONTROLRATETYPE eControlRate;
   OMX_VIDEO_AVCSLICEMODETYPE eSliceMode;
   OMX_U32 nFrameWidth;
   OMX_U32 nFrameHeight;
   OMX_U32 nFrameBytes;
#ifdef _MSM8974_
   OMX_U32 nFramestride;
   OMX_U32 nFrameScanlines;
   OMX_U32 nFrameRead;
#endif
   OMX_U32 nBitrate;
   float nFramerate;
   char* cInFileName;
   char* cOutFileName;
   OMX_U32 nUserProfile;
};

enum MsgId
{
   MSG_ID_OUTPUT_FRAME_DONE,
   MSG_ID_INPUT_FRAME_DONE,
   MSG_ID_MAX
};
union MsgData
{
   struct
   {
      OMX_BUFFERHEADERTYPE* pBuffer;
   } sBitstreamData;
};
struct Msg
{
   MsgId id;
   MsgData data;
};
struct MsgQ
{
   Msg q[MAX_MSG];
   int head;
   int size;
};

enum Mode
{
   MODE_PREVIEW,
   MODE_DISPLAY,
   MODE_PROFILE,
   MODE_FILE_ENCODE,
   MODE_LIVE_ENCODE
};

enum ResyncMarkerType
{
   RESYNC_MARKER_NONE,     ///< No resync marker
   RESYNC_MARKER_BYTE,     ///< BYTE Resync marker for MPEG4, H.264
   RESYNC_MARKER_MB,       ///< MB resync marker for MPEG4, H.264
   RESYNC_MARKER_GOB       ///< GOB resync marker for H.263
};

union DynamicConfigData
{
   OMX_VIDEO_CONFIG_BITRATETYPE bitrate;
   OMX_CONFIG_FRAMERATETYPE framerate;
   QOMX_VIDEO_INTRAPERIODTYPE intraperiod;
   OMX_CONFIG_INTRAREFRESHVOPTYPE intravoprefresh;
   OMX_CONFIG_ROTATIONTYPE rotation;
   float f_framerate;
};

struct DynamicConfig
{
   bool pending;
   unsigned frame_num;
   OMX_INDEXTYPE config_param;
   union DynamicConfigData config_data;
};

#ifdef USE_ION
struct enc_ion
{
   int ion_device_fd;
   struct ion_allocation_data alloc_data;
   struct ion_fd_data ion_alloc_fd;
};
#endif

//////////////////////////
// MODULE VARS
//////////////////////////
static pthread_mutex_t m_mutex;
static pthread_cond_t m_signal;
static MsgQ m_sMsgQ;

//#warning determine how many buffers we really have
OMX_STATETYPE m_eState = OMX_StateInvalid;
OMX_COMPONENTTYPE m_sComponent;
OMX_HANDLETYPE m_hHandle = NULL;
OMX_BUFFERHEADERTYPE* m_pOutBuffers[NUM_OUT_BUFFERS] = {NULL};
OMX_BUFFERHEADERTYPE* m_pInBuffers[NUM_IN_BUFFERS] = {NULL};
OMX_BOOL m_bInFrameFree[NUM_IN_BUFFERS];

ProfileType m_sProfile;

static int m_nFramePlay = 0;
static int m_eMode = MODE_PREVIEW;
static int m_nInFd = -1;
static int m_nOutFd = -1;
static int m_nTimeStamp = 0;
static int m_nFrameIn = 0; // frames pushed to encoder
static int m_nFrameOut = 0; // frames returned by encoder
static int m_nAVCSliceMode = 0;
static bool m_bWatchDogKicked = false;
FILE  *m_pDynConfFile = NULL;
static struct DynamicConfig dynamic_config;

/* Statistics Logging */
static long long tot_bufsize = 0;
int ebd_cnt=0, fbd_cnt=0;

#ifdef USE_ION
static const char* PMEM_DEVICE = "/dev/ion";
#elif MAX_RES_720P
static const char* PMEM_DEVICE = "/dev/pmem_adsp";
#elif MAX_RES_1080P_EBI
static const char* PMEM_DEVICE  = "/dev/pmem_adsp";
#elif MAX_RES_1080P
static const char* PMEM_DEVICE = "/dev/pmem_smipool";
#else
#error PMEM_DEVICE cannot be determined.
#endif

#ifdef USE_ION
struct enc_ion ion_data;
#endif
//////////////////////////
// MODULE FUNCTIONS
//////////////////////////

void* PmemMalloc(OMX_QCOM_PLATFORM_PRIVATE_PMEM_INFO* pMem, int nSize)
{
   void *pvirt = NULL;
   int rc = 0;

   if (!pMem)
      return NULL;

#ifdef USE_ION
  ion_data.ion_device_fd = open (PMEM_DEVICE, O_RDONLY);
  if(ion_data.ion_device_fd < 0)
  {
      E("\nERROR: ION Device open() Failed");
      return NULL;
  }
  nSize = (nSize + 4095) & (~4095);
  ion_data.alloc_data.len = nSize;
  ion_data.alloc_data.heap_mask = 0x1 << ION_CP_MM_HEAP_ID;
  ion_data.alloc_data.align = 4096;
  ion_data.alloc_data.flags = ION_SECURE;

  rc = ioctl(ion_data.ion_device_fd,ION_IOC_ALLOC,&ion_data.alloc_data);
  if(rc || !ion_data.alloc_data.handle) {
         E("\n ION ALLOC memory failed rc: %d, handle: %p", rc, ion_data.alloc_data.handle);
         ion_data.alloc_data.handle=NULL;
         return NULL;
  }

  ion_data.ion_alloc_fd.handle = ion_data.alloc_data.handle;
  rc = ioctl(ion_data.ion_device_fd,ION_IOC_MAP,&ion_data.ion_alloc_fd);
  if(rc) {
        E("\n ION MAP failed ");
        ion_data.ion_alloc_fd.fd =-1;
        ion_data.ion_alloc_fd.fd =-1;
        return NULL;
  }
  pMem->pmem_fd = ion_data.ion_alloc_fd.fd;
#else
   pMem->pmem_fd = open(PMEM_DEVICE, O_RDWR);
   if ((int)(pMem->pmem_fd) < 0)
      return NULL;
   nSize = (nSize + 4095) & (~4095);
#endif
   pMem->offset = 0;
   pvirt = mmap(NULL, nSize,
                PROT_READ | PROT_WRITE,
                MAP_SHARED, pMem->pmem_fd, pMem->offset);
   if (pvirt == (void*) MAP_FAILED)
   {
      close(pMem->pmem_fd);
      pMem->pmem_fd = -1;
#ifdef USE_ION
    if(ioctl(ion_data.ion_device_fd,ION_IOC_FREE,
       &ion_data.alloc_data.handle)) {
      E("ion recon buffer free failed");
    }
    ion_data.alloc_data.handle = NULL;
    ion_data.ion_alloc_fd.fd =-1;
    close(ion_data.ion_device_fd);
    ion_data.ion_device_fd =-1;
#endif
      return NULL;
   }
   D("allocated pMem->fd = %lu pvirt=0x%p, pMem->phys=0x%lx, size = %d", pMem->pmem_fd,
       pvirt, pMem->offset, nSize);
   return pvirt;
}

int PmemFree(OMX_QCOM_PLATFORM_PRIVATE_PMEM_INFO* pMem, void* pvirt, int nSize)
{
   if (!pMem || !pvirt)
      return -1;

   nSize = (nSize + 4095) & (~4095);
   munmap(pvirt, nSize);
   close(pMem->pmem_fd);
   pMem->pmem_fd = -1;
#ifdef USE_ION
   if(ioctl(ion_data.ion_device_fd,ION_IOC_FREE,
         &ion_data.alloc_data.handle)) {
        E("ion recon buffer free failed");
   }
   ion_data.alloc_data.handle = NULL;
   ion_data.ion_alloc_fd.fd =-1;
   close(ion_data.ion_device_fd);
   ion_data.ion_device_fd =-1;
#endif
   return 0;
}
void PrintFramePackArrangement(OMX_QCOM_FRAME_PACK_ARRANGEMENT framePackingArrangement)
{
    printf("id (%lu)\n",
           framePackingArrangement.id);
    printf("cancel_flag (%lu)\n",
           framePackingArrangement.cancel_flag);
    printf("type (%lu)\n",
           framePackingArrangement.type);
    printf("quincunx_sampling_flag (%lu)\n",
           framePackingArrangement.quincunx_sampling_flag);
   printf("content_interpretation_type (%lu)\n",
          framePackingArrangement.content_interpretation_type);
   printf("spatial_flipping_flag (%lu)\n",
          framePackingArrangement.spatial_flipping_flag);
   printf("frame0_flipped_flag (%lu)\n",
          framePackingArrangement.frame0_flipped_flag);
   printf("field_views_flag (%lu)\n",
          framePackingArrangement.field_views_flag);
   printf("current_frame_is_frame0_flag (%lu)\n",
          framePackingArrangement.current_frame_is_frame0_flag);
   printf("frame0_self_contained_flag (%lu)\n",
          framePackingArrangement.frame0_self_contained_flag);
   printf("frame1_self_contained_flag (%lu)\n",
          framePackingArrangement.frame1_self_contained_flag);
   printf("frame0_grid_position_x (%lu)\n",
          framePackingArrangement.frame0_grid_position_x);
   printf("frame0_grid_position_y (%lu)\n",
          framePackingArrangement.frame0_grid_position_y);
   printf("frame1_grid_position_x (%lu)\n",
          framePackingArrangement.frame1_grid_position_x);
   printf("frame1_grid_position_y (%lu)\n",
          framePackingArrangement.frame1_grid_position_y);
   printf("reserved_byte (%lu)\n",
          framePackingArrangement.reserved_byte);
   printf("repetition_period (%lu)\n",
          framePackingArrangement.repetition_period);
   printf("extension_flag (%lu)\n",
          framePackingArrangement.extension_flag);
}
void SetState(OMX_STATETYPE eState)
{
#define GOTO_STATE(eState)                      \
   case eState:                                 \
      {                                         \
         D("Going to state " # eState"...");            \
         OMX_SendCommand(m_hHandle,                     \
                         OMX_CommandStateSet,           \
                         (OMX_U32) eState,              \
                         NULL);                         \
         while (m_eState != eState)                     \
         {                                              \
            sleep(1);                               \
         }                                              \
         D("Now in state " # eState);                   \
         break;                                         \
      }

   switch (eState)
   {
      GOTO_STATE(OMX_StateLoaded);
      GOTO_STATE(OMX_StateIdle);
      GOTO_STATE(OMX_StateExecuting);
      GOTO_STATE(OMX_StateInvalid);
      GOTO_STATE(OMX_StateWaitForResources);
      GOTO_STATE(OMX_StatePause);
   }
}
////////////////////////////////////////////////////////////////////////////////
OMX_ERRORTYPE ConfigureEncoder()
{
   OMX_ERRORTYPE result = OMX_ErrorNone;
   unsigned const int *profile_tbl = (unsigned int const *)mpeg4_profile_level_table;
   OMX_U32 mb_per_sec, mb_per_frame;
   bool profile_level_found = false;
   OMX_U32 eProfile,eLevel;

   OMX_PARAM_PORTDEFINITIONTYPE portdef; // OMX_IndexParamPortDefinition
#ifdef QCOM_EXT
      OMX_QCOM_PARAM_PORTDEFINITIONTYPE qPortDefnType;
#endif
   portdef.nPortIndex = (OMX_U32) 0; // input
   result = OMX_GetParameter(m_hHandle,
                             OMX_IndexParamPortDefinition,
                             &portdef);
   E("\n OMX_IndexParamPortDefinition Get Paramter on input port");
   CHK(result);
   portdef.format.video.nFrameWidth = m_sProfile.nFrameWidth;
   portdef.format.video.nFrameHeight = m_sProfile.nFrameHeight;

   E ("\n Height %lu width %lu bit rate %lu",portdef.format.video.nFrameHeight
      ,portdef.format.video.nFrameWidth,portdef.format.video.nBitrate);
   result = OMX_SetParameter(m_hHandle,
                             OMX_IndexParamPortDefinition,
                             &portdef);
   E("\n OMX_IndexParamPortDefinition Set Paramter on input port");
   CHK(result);
   // once more to get proper buffer size
   result = OMX_GetParameter(m_hHandle,
                             OMX_IndexParamPortDefinition,
                             &portdef);
   E("\n OMX_IndexParamPortDefinition Get Paramter on input port, 2nd pass");
   CHK(result);
   // update size accordingly
   m_sProfile.nFrameBytes = portdef.nBufferSize;
   portdef.nPortIndex = (OMX_U32) 1; // output
   result = OMX_GetParameter(m_hHandle,
                             OMX_IndexParamPortDefinition,
                             &portdef);
   E("\n OMX_IndexParamPortDefinition Get Paramter on output port");
   CHK(result);
   portdef.format.video.nFrameWidth = m_sProfile.nFrameWidth;
   portdef.format.video.nFrameHeight = m_sProfile.nFrameHeight;
   portdef.format.video.nBitrate = m_sProfile.nBitrate;
   FractionToQ16(portdef.format.video.xFramerate,(int) (m_sProfile.nFramerate * 2),2);
   result = OMX_SetParameter(m_hHandle,
                             OMX_IndexParamPortDefinition,
                             &portdef);
   E("\n OMX_IndexParamPortDefinition Set Paramter on output port");
   CHK(result);

#ifdef QCOM_EXT

qPortDefnType.nPortIndex = PORT_INDEX_IN;
qPortDefnType.nMemRegion = OMX_QCOM_MemRegionEBI1;
qPortDefnType.nSize = sizeof(OMX_QCOM_PARAM_PORTDEFINITIONTYPE);

result = OMX_SetParameter(m_hHandle,
                             (OMX_INDEXTYPE)OMX_QcomIndexPortDefn,
                             &qPortDefnType);

#endif
   if (!m_sProfile.nUserProfile) // profile not set by user, go ahead with table calculation
   {
   //validate the ht,width,fps,bitrate and set the appropriate profile and level
   if(m_sProfile.eCodec == OMX_VIDEO_CodingMPEG4)
   {
     profile_tbl = (unsigned int const *)mpeg4_profile_level_table;
   }
   else if(m_sProfile.eCodec == OMX_VIDEO_CodingAVC)
   {
     profile_tbl = (unsigned int const *)h264_profile_level_table;
   }
   else if(m_sProfile.eCodec == OMX_VIDEO_CodingH263)
   {
     profile_tbl = (unsigned int const *)h263_profile_level_table;
   }
#ifdef _MSM8974_
   else if(m_sProfile.eCodec == OMX_VIDEO_CodingVPX)
   {
     profile_tbl = (unsigned int const *)VP8_profile_level_table;
   }
#endif
   mb_per_frame = ((m_sProfile.nFrameHeight+15)>>4)*
                ((m_sProfile.nFrameWidth+15)>>4);

   mb_per_sec = mb_per_frame*(m_sProfile.nFramerate);

   do{
      if(mb_per_frame <= (unsigned int)profile_tbl[0])
      {
          if(mb_per_sec <= (unsigned int)profile_tbl[1])
          {
            if(m_sProfile.nBitrate <= (unsigned int)profile_tbl[2])
            {
              eLevel = (int)profile_tbl[3];
              eProfile = (int)profile_tbl[4];
              E("\n profile/level found: %lu/%lu\n",eProfile, eLevel);
              profile_level_found = true;
              break;
            }
          }
      }
      profile_tbl = profile_tbl + 5;
   }while(profile_tbl[0] != 0);

   if ( profile_level_found != true )
   {
     E("\n Error: Unsupported profile/level\n");
     return OMX_ErrorNone;
   }
   }
   else // Profile set by user!
   {
      eProfile = m_sProfile.nUserProfile;
      eLevel = 0;
   }
   if (m_sProfile.eCodec == OMX_VIDEO_CodingH263)
   {
      D("Configuring H263...");

      OMX_VIDEO_PARAM_H263TYPE h263;
      result = OMX_GetParameter(m_hHandle,
                                OMX_IndexParamVideoH263,
                                &h263);
      CHK(result);
      h263.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
      h263.nPFrames = m_sProfile.nFramerate * 2 - 1; // intra period
      h263.nBFrames = 0;
      h263.eProfile = (OMX_VIDEO_H263PROFILETYPE)eProfile;
      h263.eLevel = (OMX_VIDEO_H263LEVELTYPE)eLevel;
      h263.bPLUSPTYPEAllowed = OMX_FALSE;
      h263.nAllowedPictureTypes = 2;
      h263.bForceRoundingTypeToZero = OMX_TRUE;
      h263.nPictureHeaderRepetition = 0;
      h263.nGOBHeaderInterval = 1;
      result = OMX_SetParameter(m_hHandle,
                                OMX_IndexParamVideoH263,
                                &h263);
   }
   else
   {
      D("Configuring MP4/H264...");

      OMX_VIDEO_PARAM_PROFILELEVELTYPE profileLevel; // OMX_IndexParamVideoProfileLevelCurrent
      profileLevel.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
      profileLevel.eProfile = eProfile;
      profileLevel.eLevel =  eLevel;
      result = OMX_SetParameter(m_hHandle,
                                OMX_IndexParamVideoProfileLevelCurrent,
                                &profileLevel);
      E("\n OMX_IndexParamVideoProfileLevelCurrent Set Paramter port");
      CHK(result);
      //profileLevel.eLevel = (OMX_U32) m_sProfile.eLevel;
      result = OMX_GetParameter(m_hHandle,
                                OMX_IndexParamVideoProfileLevelCurrent,
                                &profileLevel);
      E("\n OMX_IndexParamVideoProfileLevelCurrent Get Paramter port");
      D ("\n Profile = %lu level = %lu",profileLevel.eProfile,profileLevel.eLevel);
      CHK(result);

        if (m_sProfile.eCodec == OMX_VIDEO_CodingMPEG4)
        {
        OMX_VIDEO_PARAM_MPEG4TYPE mp4; // OMX_IndexParamVideoMpeg4
       result = OMX_GetParameter(m_hHandle,
                                 OMX_IndexParamVideoMpeg4,
                                 &mp4);
       CHK(result);
       mp4.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
       mp4.nTimeIncRes = 1000;
       result = OMX_SetParameter(m_hHandle,
                                 OMX_IndexParamVideoMpeg4,
                                 &mp4);
       CHK(result);
         }
   }
   if (m_sProfile.eCodec == OMX_VIDEO_CodingAVC)
   {
#if 1
/////////////C A B A C ///A N D/////D E B L O C K I N G /////////////////

      OMX_VIDEO_PARAM_AVCTYPE avcdata;
      avcdata.nPortIndex = (OMX_U32)PORT_INDEX_OUT;
      result = OMX_GetParameter(m_hHandle,
                                OMX_IndexParamVideoAvc,
                                &avcdata);
      CHK(result);
// TEST VALUES (CHANGE FOR DIFF CONFIG's)
    avcdata.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
//      avcdata.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterDisable;
//    avcdata.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterDisableSliceBoundary;
   avcdata.bEntropyCodingCABAC = OMX_FALSE;
//   avcdata.bEntropyCodingCABAC = OMX_TRUE;
   avcdata.nCabacInitIdc = 1;
///////////////////////////////////////////////

      result = OMX_SetParameter(m_hHandle,
                                OMX_IndexParamVideoAvc,
                                &avcdata);
      CHK(result);

/////////////C A B A C ///A N D/////D E B L O C K I N G /////////////////
#endif
   }

   OMX_VIDEO_PARAM_BITRATETYPE bitrate; // OMX_IndexParamVideoBitrate
   bitrate.nPortIndex = (OMX_U32)PORT_INDEX_OUT;
   result = OMX_GetParameter(m_hHandle,
                             OMX_IndexParamVideoBitrate,
                             &bitrate);
   E("\n OMX_IndexParamVideoBitrate Get Paramter port");
   CHK(result);
   bitrate.eControlRate = m_sProfile.eControlRate;
   bitrate.nTargetBitrate = m_sProfile.nBitrate;
   result = OMX_SetParameter(m_hHandle,
                             OMX_IndexParamVideoBitrate,
                             &bitrate);
   E("\n OMX_IndexParamVideoBitrate Set Paramter port");
   CHK(result);

   OMX_VIDEO_PARAM_PORTFORMATTYPE framerate; // OMX_IndexParamVidePortFormat
   framerate.nPortIndex = 0;
   result = OMX_GetParameter(m_hHandle,
                             OMX_IndexParamVideoPortFormat,
                             &framerate);
   E("\n OMX_IndexParamVideoPortFormat Get Paramter port");
   CHK(result);
   FractionToQ16(framerate.xFramerate,(int) (m_sProfile.nFramerate * 2),2);
   result = OMX_SetParameter(m_hHandle,
                             OMX_IndexParamVideoPortFormat,
                             &framerate);
   E("\n OMX_IndexParamVideoPortFormat Set Paramter port");
   CHK(result);

#if 1
///////////////////I N T R A P E R I O D ///////////////////

      QOMX_VIDEO_INTRAPERIODTYPE intra;

      intra.nPortIndex = (OMX_U32) PORT_INDEX_OUT; // output
      result = OMX_GetConfig(m_hHandle,
                             (OMX_INDEXTYPE) QOMX_IndexConfigVideoIntraperiod,
                             (OMX_PTR) &intra);

      if (result == OMX_ErrorNone)
      {
         intra.nPFrames = (OMX_U32) (2 * m_sProfile.nFramerate - 1); //setting I
                                                                     //frame interval to
                                                                     //2 x framerate
         intra.nIDRPeriod = 1; //every I frame is an IDR
         intra.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
         result = OMX_SetConfig(m_hHandle,
                                (OMX_INDEXTYPE) QOMX_IndexConfigVideoIntraperiod,
                                (OMX_PTR) &intra);
      }
      else
      {
         E("failed to get state", 0, 0, 0);
      }


///////////////////I N T R A P E R I O D ///////////////////
#endif

#if 1
///////////////////E R R O R C O R R E C T I O N ///////////////////

      ResyncMarkerType eResyncMarkerType = RESYNC_MARKER_NONE;
      unsigned long int nResyncMarkerSpacing = 0;
      OMX_BOOL enableHEC = OMX_FALSE;

//For Testing ONLY
   if (m_sProfile.eCodec == OMX_VIDEO_CodingMPEG4)
   {
// MPEG4
//      eResyncMarkerType = RESYNC_MARKER_BYTE;
//      nResyncMarkerSpacing = 1920;
      eResyncMarkerType = RESYNC_MARKER_MB;
      nResyncMarkerSpacing = 50;
      enableHEC = OMX_TRUE;
   }
   else if (m_sProfile.eCodec == OMX_VIDEO_CodingH263)
   {
//H263
      //eResyncMarkerType = RESYNC_MARKER_GOB;
	  eResyncMarkerType = RESYNC_MARKER_NONE;
      nResyncMarkerSpacing = 0;
   }
   else if (m_sProfile.eCodec == OMX_VIDEO_CodingAVC)
   {
//H264
//      eResyncMarkerType = RESYNC_MARKER_BYTE;
//      nResyncMarkerSpacing = 1920;

      //nResyncMarkerSpacing sets the slice size in venc_set_multislice_cfg
      //
      //As of 9/24/10, it is known that the firmware has a bitstream
      //corruption issue when RateControl and multislice are enabled for 720P
      //So, disabling multislice for 720P when ratecontrol is enabled until
      //the firmware issue is resolved.

      if ( ( (m_sProfile.nFrameWidth == 1280) && (m_sProfile.nFrameHeight = 720) ) &&
           (m_sProfile.eControlRate  != OMX_Video_ControlRateDisable) )
      {
         eResyncMarkerType = RESYNC_MARKER_NONE;
         nResyncMarkerSpacing = 0;
      }
      else
      {
         eResyncMarkerType = RESYNC_MARKER_NONE;
          nResyncMarkerSpacing = 0;
      }
   }

   OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrection; //OMX_IndexParamVideoErrorCorrection
   errorCorrection.nPortIndex = (OMX_U32) PORT_INDEX_OUT; // output
   result = OMX_GetParameter(m_hHandle,
                             (OMX_INDEXTYPE) OMX_IndexParamVideoErrorCorrection,
                             (OMX_PTR) &errorCorrection);

   errorCorrection.bEnableRVLC = OMX_FALSE;
   errorCorrection.bEnableDataPartitioning = OMX_FALSE;

      if ((eResyncMarkerType == RESYNC_MARKER_BYTE) &&
         (m_sProfile.eCodec == OMX_VIDEO_CodingMPEG4)){
            errorCorrection.bEnableResync = OMX_TRUE;
            errorCorrection.nResynchMarkerSpacing = nResyncMarkerSpacing;
            errorCorrection.bEnableHEC = enableHEC;
            }
      else if ((eResyncMarkerType == RESYNC_MARKER_BYTE) &&
               (m_sProfile.eCodec == OMX_VIDEO_CodingAVC)){
         errorCorrection.bEnableResync = OMX_TRUE;
         errorCorrection.nResynchMarkerSpacing = nResyncMarkerSpacing;
         }
      else if ((eResyncMarkerType == RESYNC_MARKER_GOB) &&
               (m_sProfile.eCodec == OMX_VIDEO_CodingH263)){
         errorCorrection.bEnableResync = OMX_FALSE;
         errorCorrection.nResynchMarkerSpacing = nResyncMarkerSpacing;
         errorCorrection.bEnableDataPartitioning = OMX_TRUE;
         }

      result = OMX_SetParameter(m_hHandle,
                            (OMX_INDEXTYPE) OMX_IndexParamVideoErrorCorrection,
                            (OMX_PTR) &errorCorrection);
   CHK(result);

      if (eResyncMarkerType == RESYNC_MARKER_MB){
         if (m_sProfile.eCodec == OMX_VIDEO_CodingAVC){
            OMX_VIDEO_PARAM_AVCTYPE avcdata;
            avcdata.nPortIndex = (OMX_U32) PORT_INDEX_OUT; // output
            result = OMX_GetParameter(m_hHandle,
                                      OMX_IndexParamVideoAvc,
                                      (OMX_PTR) &avcdata);
            CHK(result);
            if (result == OMX_ErrorNone)
            {
               avcdata.nSliceHeaderSpacing = nResyncMarkerSpacing;
               result = OMX_SetParameter(m_hHandle,
                                         OMX_IndexParamVideoAvc,
                                         (OMX_PTR) &avcdata);
               CHK(result);

            }
         }
         else if(m_sProfile.eCodec == OMX_VIDEO_CodingMPEG4){
            OMX_VIDEO_PARAM_MPEG4TYPE mp4;
            mp4.nPortIndex = (OMX_U32) PORT_INDEX_OUT; // output
            result = OMX_GetParameter(m_hHandle,
                                      OMX_IndexParamVideoMpeg4,
                                      (OMX_PTR) &mp4);
            CHK(result);

            if (result == OMX_ErrorNone)
            {
               mp4.nSliceHeaderSpacing = nResyncMarkerSpacing;
               result = OMX_SetParameter(m_hHandle,
                                         OMX_IndexParamVideoMpeg4,
                                         (OMX_PTR) &mp4);
               CHK(result);
            }
         }
         }

///////////////////E R R O R C O R R E C T I O N ///////////////////
#endif

#if 1
///////////////////I N T R A R E F R E S H///////////////////
      bool bEnableIntraRefresh = OMX_TRUE;

      if (result == OMX_ErrorNone)
      {
         OMX_VIDEO_PARAM_INTRAREFRESHTYPE ir; // OMX_IndexParamVideoIntraRefresh
         ir.nPortIndex = (OMX_U32) PORT_INDEX_OUT; // output
         result = OMX_GetParameter(m_hHandle,
                                   OMX_IndexParamVideoIntraRefresh,
                                   (OMX_PTR) &ir);
         if (result == OMX_ErrorNone)
         {
            if (bEnableIntraRefresh)
            {
               ir.eRefreshMode = OMX_VIDEO_IntraRefreshCyclic;
               ir.nCirMBs = 5;
               result = OMX_SetParameter(m_hHandle,
                                         OMX_IndexParamVideoIntraRefresh,
                                         (OMX_PTR) &ir);
               CHK(result);
            }
         }
      }
#endif
#if 1
///////////////////FRAMEPACKING DATA///////////////////
      OMX_QCOM_FRAME_PACK_ARRANGEMENT framePackingArrangement;
      FILE *m_pConfigFile;
      char m_configFilename [128] = "/data/configFile.cfg";
      memset(&framePackingArrangement, 0, sizeof(framePackingArrangement));
      m_pConfigFile = fopen(m_configFilename, "r");
      if (m_pConfigFile != NULL)
      {
         //read all frame packing data
         framePackingArrangement.nPortIndex = (OMX_U32)PORT_INDEX_OUT;
         int totalSizeToRead = FRAME_PACK_SIZE * sizeof(OMX_U32);
         char *pFramePack = (char *) &(framePackingArrangement.id);
         while ( ( (fscanf(m_pConfigFile, "%d", pFramePack)) != EOF ) &&
                 (totalSizeToRead != 0) )
         {
            //printf("Addr = %p, Value read = %d, sizeToRead remaining=%d\n",
            //       pFramePack, *pFramePack, totalSizeToRead);
            pFramePack += sizeof(OMX_U32);
            totalSizeToRead -= sizeof(OMX_U32);
         }
         //close the file.
         fclose(m_pConfigFile);

         printf("Frame Packing data from config file:\n");
         PrintFramePackArrangement(framePackingArrangement);
      }
      else
      {
         D("\n Config file does not exist or could not be opened.");
         //set the default values
         framePackingArrangement.nPortIndex = (OMX_U32)PORT_INDEX_OUT;
         framePackingArrangement.id = 123;
         framePackingArrangement.cancel_flag = false;
         framePackingArrangement.type = 3;
         framePackingArrangement.quincunx_sampling_flag = false;
         framePackingArrangement.content_interpretation_type = 0;
         framePackingArrangement.spatial_flipping_flag = true;
         framePackingArrangement.frame0_flipped_flag = false;
         framePackingArrangement.field_views_flag = false;
         framePackingArrangement.current_frame_is_frame0_flag = false;
         framePackingArrangement.frame0_self_contained_flag = true;
         framePackingArrangement.frame1_self_contained_flag = false;
         framePackingArrangement.frame0_grid_position_x = 3;
         framePackingArrangement.frame0_grid_position_y = 15;
         framePackingArrangement.frame1_grid_position_x = 11;
         framePackingArrangement.frame1_grid_position_y = 7;
         framePackingArrangement.reserved_byte = 0;
         framePackingArrangement.repetition_period = 16381;
         framePackingArrangement.extension_flag = false;

         printf("Frame Packing Defaults :\n");
         PrintFramePackArrangement(framePackingArrangement);
      }
      result = OMX_SetConfig(m_hHandle,
                (OMX_INDEXTYPE)OMX_QcomIndexConfigVideoFramePackingArrangement,
                (OMX_PTR) &framePackingArrangement);
      CHK(result);

//////////////////////OMX_VIDEO_PARAM_INTRAREFRESHTYPE///////////////////
#endif

   OMX_CONFIG_FRAMERATETYPE enc_framerate; // OMX_IndexConfigVideoFramerate
   enc_framerate.nPortIndex = (OMX_U32)PORT_INDEX_OUT;
   result = OMX_GetConfig(m_hHandle,
                          OMX_IndexConfigVideoFramerate,
                          &enc_framerate);
   CHK(result);
   FractionToQ16(enc_framerate.xEncodeFramerate,(int) (m_sProfile.nFramerate * 2),2);
   result = OMX_SetConfig(m_hHandle,
                          OMX_IndexConfigVideoFramerate,
                          &enc_framerate);
   CHK(result);
   return OMX_ErrorNone;
}
////////////////////////////////////////////////////////////////////////////////
void SendMessage(MsgId id, MsgData* data)
{
   pthread_mutex_lock(&m_mutex);
   if (m_sMsgQ.size >= MAX_MSG)
   {
      E("main msg m_sMsgQ is full");
      return;
   }
   m_sMsgQ.q[(m_sMsgQ.head + m_sMsgQ.size) % MAX_MSG].id = id;
   if (data)
      m_sMsgQ.q[(m_sMsgQ.head + m_sMsgQ.size) % MAX_MSG].data = *data;
   ++m_sMsgQ.size;
   pthread_cond_signal(&m_signal);
   pthread_mutex_unlock(&m_mutex);
}
////////////////////////////////////////////////////////////////////////////////
void PopMessage(Msg* msg)
{
   pthread_mutex_lock(&m_mutex);
   while (m_sMsgQ.size == 0)
   {
      pthread_cond_wait(&m_signal, &m_mutex);
   }
   *msg = m_sMsgQ.q[m_sMsgQ.head];
   --m_sMsgQ.size;
   m_sMsgQ.head = (m_sMsgQ.head + 1) % MAX_MSG;
   pthread_mutex_unlock(&m_mutex);
}
////////////////////////////////////////////////////////////////////////////////
OMX_ERRORTYPE EVT_CB(OMX_IN OMX_HANDLETYPE hComponent,
                     OMX_IN OMX_PTR pAppData,
                     OMX_IN OMX_EVENTTYPE eEvent,
                     OMX_IN OMX_U32 nData1,
                     OMX_IN OMX_U32 nData2,
                     OMX_IN OMX_PTR pEventData)
{
#define SET_STATE(eState)                                   \
   case eState:                                             \
      {                                                     \
         D("" # eState " complete");                        \
         m_eState = eState;                                 \
         break;                                             \
      }

   if (eEvent == OMX_EventCmdComplete)
   {
      if ((OMX_COMMANDTYPE) nData1 == OMX_CommandStateSet)
      {
         switch ((OMX_STATETYPE) nData2)
         {
            SET_STATE(OMX_StateLoaded);
            SET_STATE(OMX_StateIdle);
            SET_STATE(OMX_StateExecuting);
            SET_STATE(OMX_StateInvalid);
            SET_STATE(OMX_StateWaitForResources);
            SET_STATE(OMX_StatePause);
         default:
            E("invalid state %d", (int) nData2);
          }
      }
   }

   else if (eEvent == OMX_EventError)
   {
      E("OMX_EventError");
   }

   else
   {
      E("unexpected event %d", (int) eEvent);
   }
   return OMX_ErrorNone;
}
////////////////////////////////////////////////////////////////////////////////
OMX_ERRORTYPE EBD_CB(OMX_IN OMX_HANDLETYPE hComponent,
                     OMX_IN OMX_PTR pAppData,
                     OMX_IN OMX_BUFFERHEADERTYPE* pBuffer)
{
   D("Got EBD callback ts=%lld", pBuffer->nTimeStamp);

   for (int i = 0; i < num_in_buffers; i++)
   {
      // mark this buffer ready for use again
      if (m_pInBuffers[i] == pBuffer)
      {

         D("Marked input buffer idx %d as free, buf %p", i, pBuffer->pBuffer);
         m_bInFrameFree[i] = OMX_TRUE;
         break;
      }
   }

   if (m_eMode == MODE_LIVE_ENCODE)
   {
      CameraTest_ReleaseFrame(pBuffer->pBuffer,
                              ((OMX_QCOM_PLATFORM_PRIVATE_PMEM_INFO*)pBuffer->pAppPrivate));
   }
   else
   {
      // wake up main thread and tell it to send next frame
      MsgData data;
      data.sBitstreamData.pBuffer = pBuffer;
      SendMessage(MSG_ID_INPUT_FRAME_DONE,
                  &data);

   }
   return OMX_ErrorNone;
}
////////////////////////////////////////////////////////////////////////////////
OMX_ERRORTYPE FBD_CB(OMX_OUT OMX_HANDLETYPE hComponent,
                     OMX_OUT OMX_PTR pAppData,
                     OMX_OUT OMX_BUFFERHEADERTYPE* pBuffer)
{
   D("Got FBD callback ts=%lld", pBuffer->nTimeStamp);

   static long long prevTime = 0;
   long long currTime = GetTimeStamp();

   m_bWatchDogKicked = true;

   /* Empty Buffers should not be counted */
   if(pBuffer->nFilledLen !=0)
   {
      /* Counting Buffers supplied from OpneMax Encoder */
      fbd_cnt++;
      tot_bufsize += pBuffer->nFilledLen;
   }
   if (prevTime != 0)
   {
      long long currTime = GetTimeStamp();
      D("FBD_DELTA = %lld\n", currTime - prevTime);
   }
   prevTime = currTime;

   if (m_eMode == MODE_PROFILE)
   {
      // if we are profiling we are not doing file I/O
      // so just give back to encoder
      if (OMX_FillThisBuffer(m_hHandle, pBuffer) != OMX_ErrorNone)
      {
         E("empty buffer failed for profiling");
      }
   }
   else
   {
      // wake up main thread and tell it to write to file
      MsgData data;
      data.sBitstreamData.pBuffer = pBuffer;
      SendMessage(MSG_ID_OUTPUT_FRAME_DONE,
                  &data);
   }
   return OMX_ErrorNone;
}
////////////////////////////////////////////////////////////////////////////////
OMX_ERRORTYPE VencTest_Initialize()
{
   OMX_ERRORTYPE result = OMX_ErrorNone;
   static OMX_CALLBACKTYPE sCallbacks = {EVT_CB, EBD_CB, FBD_CB};
   int i;

   for (i = 0; i < num_in_buffers; i++)
   {
      m_pInBuffers[i] = NULL;
   }

   result = OMX_Init();
   CHK(result);

   if (m_sProfile.eCodec == OMX_VIDEO_CodingMPEG4)
   {
        result = OMX_GetHandle(&m_hHandle,
                             "OMX.qcom.video.encoder.mpeg4",
                             NULL,
                             &sCallbacks);
     // CHK(result);
   }
   else if (m_sProfile.eCodec == OMX_VIDEO_CodingH263)
   {
      result = OMX_GetHandle(&m_hHandle,
                             "OMX.qcom.video.encoder.h263",
                             NULL,
                             &sCallbacks);
      CHK(result);
   }
#ifdef _MSM8974_
   else if (m_sProfile.eCodec == OMX_VIDEO_CodingVPX)
   {
      result = OMX_GetHandle(&m_hHandle,
                             "OMX.qcom.video.encoder.vp8",
                             NULL,
                             &sCallbacks);
      CHK(result);
   }
#endif
   else
   {
      result = OMX_GetHandle(&m_hHandle,
                             "OMX.qcom.video.encoder.avc",
                             NULL,
                             &sCallbacks);
      CHK(result);
   }


   result = ConfigureEncoder();
   CHK(result);

   return result;
}

////////////////////////////////////////////////////////////////////////////////
OMX_ERRORTYPE VencTest_RegisterYUVBuffer(OMX_BUFFERHEADERTYPE** ppBufferHeader,
                                         OMX_U8 *pBuffer,
                                         OMX_PTR pAppPrivate)
{
   OMX_ERRORTYPE result = OMX_ErrorNone;
#if 0
   D("register buffer");
   if ((result = OMX_AllocateBuffer(m_hHandle,
                               ppBufferHeader,
                               (OMX_U32) PORT_INDEX_IN,
                               pAppPrivate,
                               m_sProfile.nFrameBytes
                               )) != OMX_ErrorNone)
   {
      E("use buffer failed");
   }
   else
   {
     E("Allocate Buffer Success %x", (*ppBufferHeader)->pBuffer);
   }
  #endif
   D("register buffer");
   D("Calling UseBuffer for Input port");
   if ((result = OMX_UseBuffer(m_hHandle,
                               ppBufferHeader,
                               (OMX_U32) PORT_INDEX_IN,
                               pAppPrivate,
                               m_sProfile.nFrameBytes,
                               pBuffer)) != OMX_ErrorNone)
   {
      E("use buffer failed");
   }

   return result;
}
////////////////////////////////////////////////////////////////////////////////
OMX_ERRORTYPE VencTest_EncodeFrame(void* pYUVBuff,
                                   long long nTimeStamp)
{
   OMX_ERRORTYPE result = OMX_ErrorUndefined;
   D("calling OMX empty this buffer");
   for (int i = 0; i < num_in_buffers; i++)
   {
      if (pYUVBuff == m_pInBuffers[i]->pBuffer)
      {
         m_pInBuffers[i]->nTimeStamp = nTimeStamp;
    D("Sending Buffer - %x", m_pInBuffers[i]->pBuffer);
         result = OMX_EmptyThisBuffer(m_hHandle,
                                      m_pInBuffers[i]);
         /* Counting Buffers supplied to OpenMax Encoder */
         if(OMX_ErrorNone == result)
            ebd_cnt++;
         CHK(result);
         break;
      }
   }
   return result;
}
////////////////////////////////////////////////////////////////////////////////
OMX_ERRORTYPE VencTest_Exit(void)
{
   int i;
   OMX_ERRORTYPE result = OMX_ErrorNone;
   D("trying to exit venc");

   D("going to idle state");
   SetState(OMX_StateIdle);


   D("going to loaded state");
   //SetState(OMX_StateLoaded);
      OMX_SendCommand(m_hHandle,
                      OMX_CommandStateSet,
                      (OMX_U32) OMX_StateLoaded,
                       NULL);

      for (i = 0; i < num_in_buffers; i++)
   {
      D("free buffer");
      if (m_pInBuffers[i]->pBuffer)
      {
        // free(m_pInBuffers[i]->pBuffer);
         result = OMX_FreeBuffer(m_hHandle,
                                 PORT_INDEX_IN,
                                 m_pInBuffers[i]);
         CHK(result);
      }
      else
      {
         E("buffer %d is null", i);
         result = OMX_ErrorUndefined;
         CHK(result);
      }
   }
   for (i = 0; i < num_out_buffers; i++)
   {
      D("free buffer");
      if (m_pOutBuffers[i]->pBuffer)
      {
         free(m_pOutBuffers[i]->pBuffer);
         result = OMX_FreeBuffer(m_hHandle,
                                 PORT_INDEX_OUT,
                                 m_pOutBuffers[i]);
         CHK(result);

      }
      else
      {
         E("buffer %d is null", i);
         result = OMX_ErrorUndefined;
         CHK(result);
      }
   }

     while (m_eState != OMX_StateLoaded)
     {
        sleep(1);
     }
   D("component_deinit...");
   result = OMX_Deinit();
   CHK(result);

   D("venc is exiting...");
   return result;
}
////////////////////////////////////////////////////////////////////////////////

void VencTest_ReadDynamicConfigMsg()
{
  char frame_n[8], config[16], param[8];
  char *dest = frame_n;
  bool end = false;
  int cntr, nparam = 0;
  memset(&dynamic_config, 0, sizeof(struct DynamicConfig));
  do
  {
    cntr = -1;
    do
    {
      dest[++cntr] = fgetc(m_pDynConfFile);
    } while(dest[cntr] != ' ' && dest[cntr] != '\t' && dest[cntr] != '\n' && dest[cntr] != '\r' && !feof(m_pDynConfFile));
    if (dest[cntr] == '\n' || dest[cntr] == '\r')
      end = true;
    dest[cntr] = NULL;
    if (dest == frame_n)
      dest = config;
    else if (dest == config)
      dest = param;
    else
      end = true;
    nparam++;
  } while (!end && !feof(m_pDynConfFile));

  if (nparam > 1)
  {
    dynamic_config.pending = true;
    dynamic_config.frame_num = atoi(frame_n);
    if (!strcmp(config, "bitrate"))
    {
      dynamic_config.config_param = OMX_IndexConfigVideoBitrate;
      dynamic_config.config_data.bitrate.nPortIndex = PORT_INDEX_OUT;
      dynamic_config.config_data.bitrate.nEncodeBitrate = strtoul(param, NULL, 10);
    }
    else if (!strcmp(config, "framerate"))
    {
      dynamic_config.config_param = OMX_IndexConfigVideoFramerate;
      dynamic_config.config_data.framerate.nPortIndex = PORT_INDEX_OUT;
      dynamic_config.config_data.f_framerate = atof(param);
    }
    else if (!strcmp(config, "iperiod"))
    {
      dynamic_config.config_param = (OMX_INDEXTYPE)QOMX_IndexConfigVideoIntraperiod;
      dynamic_config.config_data.intraperiod.nPortIndex = PORT_INDEX_OUT;
      dynamic_config.config_data.intraperiod.nPFrames = strtoul(param, NULL, 10) - 1;
      dynamic_config.config_data.intraperiod.nIDRPeriod = 1; // This value is ignored in OMX component
    }
    else if (!strcmp(config, "ivoprefresh"))
    {
      dynamic_config.config_param = OMX_IndexConfigVideoIntraVOPRefresh;
      dynamic_config.config_data.intravoprefresh.nPortIndex = PORT_INDEX_OUT;
      dynamic_config.config_data.intravoprefresh.IntraRefreshVOP = OMX_TRUE;
    }
    else if (!strcmp(config, "rotation"))
    {
      dynamic_config.config_param = OMX_IndexConfigCommonRotate;
      dynamic_config.config_data.rotation.nPortIndex = PORT_INDEX_OUT;
      dynamic_config.config_data.rotation.nRotation = strtoul(param, NULL, 10);
    }
    else
    {
      E("UNKNOWN CONFIG PARAMETER: %s!", config);
      dynamic_config.pending = false;
    }
  }
  else if (feof(m_pDynConfFile))
  {
    fclose(m_pDynConfFile);
    m_pDynConfFile = NULL;
  }
}

void VencTest_ProcessDynamicConfigurationFile()
{
  do
  {
    if (dynamic_config.pending)
    {
      if(m_nFrameIn == dynamic_config.frame_num)
      {
        if (dynamic_config.config_param == OMX_IndexConfigVideoFramerate)
        {
          m_sProfile.nFramerate = dynamic_config.config_data.f_framerate;
          FractionToQ16(dynamic_config.config_data.framerate.xEncodeFramerate,
                        (int)(m_sProfile.nFramerate * 2), 2);
        }
        if (OMX_SetConfig(m_hHandle, dynamic_config.config_param,
            &dynamic_config.config_data) != OMX_ErrorNone)
          E("ERROR: Setting dynamic config to OMX param[0x%x]", dynamic_config.config_param);
        dynamic_config.pending = false;
      }
      else if (m_nFrameIn > dynamic_config.frame_num)
      {
        E("WARNING: Config change requested in passed frame(%d)", dynamic_config.frame_num);
        dynamic_config.pending = false;
      }
    }
    if (!dynamic_config.pending)
      VencTest_ReadDynamicConfigMsg();
  } while (!dynamic_config.pending && m_pDynConfFile);
}

////////////////////////////////////////////////////////////////////////////////
OMX_ERRORTYPE VencTest_ReadAndEmpty(OMX_BUFFERHEADERTYPE* pYUVBuffer)
{
   OMX_ERRORTYPE result = OMX_ErrorNone;
#ifdef T_ARM
#if defined(MAX_RES_720P) && !defined(_MSM8974_)
   if (read(m_nInFd,
            pYUVBuffer->pBuffer,
            m_sProfile.nFrameBytes) != m_sProfile.nFrameBytes)
   {
      return OMX_ErrorUndefined;
   }
#elif _MSM8974_
   int i, lscanl, lstride, cscanl, cstride, height, width;
   int bytes = 0, read_bytes = 0;
   OMX_U8 *yuv = pYUVBuffer->pBuffer;
   height = m_sProfile.nFrameHeight;
   width = m_sProfile.nFrameWidth;
   lstride = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
   lscanl = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
   cstride = VENUS_UV_STRIDE(COLOR_FMT_NV12, width);
   cscanl = VENUS_UV_SCANLINES(COLOR_FMT_NV12, height);

   for(i = 0; i < height; i++) {
	   bytes = read(m_nInFd, yuv, width);
	   if (bytes != width) {
		   E("read failed: %d != %d\n", read, width);
		   return OMX_ErrorUndefined;
	   }
	   read_bytes += bytes;
	   yuv += lstride;
   }
   yuv = pYUVBuffer->pBuffer + (lscanl * lstride);
   for (i = 0; i < ((height + 1) >> 1); i++) {
	   bytes = read(m_nInFd, yuv, width);
	   if (bytes != width) {
		   E("read failed: %d != %d\n", read, width);
		   return OMX_ErrorUndefined;
	   }
	   read_bytes += bytes;
	   yuv += cstride;
   }
   m_sProfile.nFrameRead = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
   E("\n\nActual read bytes: %d, NV12 buffer size: %d\n\n\n", read_bytes, m_sProfile.nFrameRead);
#else
         OMX_U32 bytestoread = m_sProfile.nFrameWidth*m_sProfile.nFrameHeight;
         // read Y first
         if (read(m_nInFd,
              pYUVBuffer->pBuffer,
              bytestoread) != bytestoread)
            return OMX_ErrorUndefined;

         // check alignment for offset to C
         OMX_U32 offset_to_c = m_sProfile.nFrameWidth * m_sProfile.nFrameHeight;

         const OMX_U32 C_2K = (1024*2),
            MASK_2K = C_2K-1,
            IMASK_2K = ~MASK_2K;

         if (offset_to_c & MASK_2K)
         {
            // offset to C is not 2k aligned, adjustment is required
            offset_to_c = (offset_to_c & IMASK_2K) + C_2K;
         }

         bytestoread = m_sProfile.nFrameWidth*m_sProfile.nFrameHeight/2;
         // read C
         if (read(m_nInFd,
              pYUVBuffer->pBuffer + offset_to_c,
              bytestoread)!= bytestoread)
            return OMX_ErrorUndefined;
#endif
#else
   {
	  char * pInputbuf = (char *)(pYUVBuffer->pBuffer) ;
	      read(m_nInFd,pInputbuf,m_sProfile.nFrameBytes) ;

   }
#endif
   if (m_pDynConfFile)
     VencTest_ProcessDynamicConfigurationFile();
   D("about to call VencTest_EncodeFrame...");
   pthread_mutex_lock(&m_mutex);
   ++m_nFrameIn;
#ifdef _MSM8974_
   pYUVBuffer->nFilledLen = m_sProfile.nFrameRead;
#else
   pYUVBuffer->nFilledLen = m_sProfile.nFrameBytes;
#endif
   D("Called Buffer with Data filled length %d",pYUVBuffer->nFilledLen);

      result = VencTest_EncodeFrame(pYUVBuffer->pBuffer,
                                 m_nTimeStamp);

   m_nTimeStamp += (1000000) / m_sProfile.nFramerate;
   CHK(result);
   pthread_mutex_unlock(&m_mutex);
   return result;
}
////////////////////////////////////////////////////////////////////////////////
void PreviewCallback(int nFD,
                     int nOffset,
                     void* pPhys,
                     void* pVirt,
                     long long nTimeStamp)
{

   D("================= preview frame %d, phys=0x%x, nTimeStamp(millis)=%lld",
     m_nFrameIn+1, pPhys, (nTimeStamp / 1000));

   if (m_nFrameIn == m_nFramePlay &&
       m_nFramePlay != 0)
   {
      // we will stop camera after last frame is encoded.
      // for now just ignore input frames

      CameraTest_ReleaseFrame(pPhys, pVirt);
      return;
   }

   // see if we should stop
   pthread_mutex_lock(&m_mutex);
   ++m_nFrameIn;
   pthread_mutex_unlock(&m_mutex);


   if (m_eMode == MODE_LIVE_ENCODE)
   {

      OMX_ERRORTYPE result;

      // register new camera buffers with encoder
      int i;
      for (i = 0; i < num_in_buffers; i++)
      {
         if (m_pInBuffers[i] != NULL &&
             m_pInBuffers[i]->pBuffer == pPhys)
         {
            break;
         }
         else if (m_pInBuffers[i] == NULL)
         {
            D("registering buffer...");
            result = VencTest_RegisterYUVBuffer(&m_pInBuffers[i],
                                                (OMX_U8*) pPhys,
                                                (OMX_PTR) pVirt); // store virt in app private field
            D("register done");
            CHK(result);
            break;
         }
      }

      if (i == num_in_buffers)
      {
         E("There are more camera buffers than we thought");
         CHK(1);
      }

      // encode the yuv frame

      D("StartEncodeTime=%lld", GetTimeStamp());
      result = VencTest_EncodeFrame(pPhys,
                                    nTimeStamp);
      CHK(result);
     // FBTest_DisplayImage(nFD, nOffset);
   }
   else
   {
     // FBTest_DisplayImage(nFD, nOffset);
      CameraTest_ReleaseFrame(pPhys, pVirt);
   }
}
////////////////////////////////////////////////////////////////////////////////
void usage(char* filename)
{
   char* fname = strrchr(filename, (int) '/');
   fname = (fname == NULL) ? filename : fname;

   fprintf(stderr, "usage: %s LIVE <QCIF|QVGA> <MP4|H263> <FPS> <BITRATE> <NFRAMES> <OUTFILE>\n", fname);
   fprintf(stderr, "usage: %s FILE <QCIF|QVGA> <MP4|H263 <FPS> <BITRATE> <NFRAMES> <INFILE> <OUTFILE> ", fname);
   fprintf(stderr, "<Dynamic config file - opt> <Rate Control - opt> <AVC Slice Mode - opt>\n", fname);
   fprintf(stderr, "usage: %s PROFILE <QCIF|QVGA> <MP4|H263 <FPS> <BITRATE> <NFRAMES> <INFILE>\n", fname);
   fprintf(stderr, "usage: %s PREVIEW <QCIF|QVGA> <FPS> <NFRAMES>\n", fname);
   fprintf(stderr, "usage: %s DISPLAY <QCIF|QVGA> <FPS> <NFRAMES> <INFILE>\n", fname);
   fprintf(stderr, "\n       BITRATE - bitrate in kbps\n");
   fprintf(stderr, "       FPS - frames per second\n");
   fprintf(stderr, "       NFRAMES - number of frames to play, 0 for infinite\n");
   fprintf(stderr, "       RateControl (Values 0 - 4 for RC_OFF, RC_CBR_CFR, RC_CBR_VFR, RC_VBR_CFR, RC_VBR_VFR\n");
   exit(1);
}

bool parseWxH(char *str, OMX_U32 *width, OMX_U32 *height)
{
   bool parseOK = false;
   const char delimiters[] = " x*,";
   char *token, *dupstr, *temp;
   OMX_U32 w, h;

   dupstr = strdup(str);
   token = strtok_r(dupstr, delimiters, &temp);
   if (token)
   {
       w = strtoul(token, NULL, 10);
       token = strtok_r(NULL, delimiters, &temp);
       if (token)
       {
           h = strtoul(token, NULL, 10);
           if (w != ULONG_MAX && h != ULONG_MAX)
           {
#ifdef MAX_RES_720P
              if ((w * h >> 8) <= 3600)
              {
                 parseOK = true;
                 *width = w;
                 *height = h;
                 }
#else
              if ((w * h >> 8) <= 8160)
              {
                 parseOK = true;
                 *width = w;
                 *height = h;
                 }
#endif
              else
                 E("\nInvalid dimensions %dx%d",w,h);
              }
           }
       }
   free(dupstr);
   return parseOK;
}

////////////////////////////////////////////////////////////////////////////////
void parseArgs(int argc, char** argv)
{
   int dyn_file_arg = argc;
   if (argc == 1)
   {
      usage(argv[0]);
   }
   else if (strcmp("PREVIEW", argv[1]) == 0 ||
            strcmp("preview", argv[1]) == 0)
   {
      m_eMode = MODE_PREVIEW;
      if (argc != 5)
      {
         usage(argv[0]);
      }
   }
   else if (strcmp("DISPLAY", argv[1]) == 0 ||
            strcmp("display", argv[1]) == 0)
   {
      m_eMode = MODE_DISPLAY;
      if (argc != 6)
      {
         usage(argv[0]);
      }
      m_sProfile.cInFileName = argv[5];
      m_sProfile.cOutFileName = NULL;
   }
   else if (strcmp("LIVE", argv[1]) == 0 ||
            strcmp("live", argv[1]) == 0)
   {//263
      m_eMode = MODE_LIVE_ENCODE;
      if (argc != 8)
      {
         usage(argv[0]);
      }
      m_sProfile.cInFileName = NULL;
      m_sProfile.cOutFileName = argv[7];
   }
   else if (strcmp("FILE", argv[1]) == 0 ||
            strcmp("file", argv[1]) == 0)
   {//263
      m_eMode = MODE_FILE_ENCODE;

      if(argc < 9 || argc > 13)
      {
          usage(argv[0]);
      }
      else
      {
         if (argc > 9)
            dyn_file_arg = 9;

         if (argc > 10)
         {
           m_sProfile.eControlRate = OMX_Video_ControlRateVariable;
            int RC = atoi(argv[10]);

            switch (RC)
            {
            case 0:
               m_sProfile.eControlRate  = OMX_Video_ControlRateDisable ;//VENC_RC_NONE
               break;
            case 1:
               m_sProfile.eControlRate  = OMX_Video_ControlRateConstant;//VENC_RC_CBR_CFR
               break;

            case 2:
               m_sProfile.eControlRate  = OMX_Video_ControlRateConstantSkipFrames;//VENC_RC_CBR_VFR
               break;

            case 3:
               m_sProfile.eControlRate  =OMX_Video_ControlRateVariable ;//VENC_RC_VBR_CFR
               break;

            case 4:
               m_sProfile.eControlRate  = OMX_Video_ControlRateVariableSkipFrames;//VENC_RC_VBR_VFR
               break;

           default:
               E("invalid rate control selection");
               m_sProfile.eControlRate = OMX_Video_ControlRateVariable; //VENC_RC_VBR_CFR
               break;
            }
         }

         if (argc > 11)
         {
            int profile_argi = 11;
            if(!strcmp(argv[3], "H264") || !strcmp(argv[3], "h264"))
            {
               profile_argi = 12;
               D("\nSetting AVCSliceMode ... ");
               int AVCSliceMode = atoi(argv[11]);
               switch(AVCSliceMode)
               {
               case 0:
                  m_sProfile.eSliceMode = OMX_VIDEO_SLICEMODE_AVCDefault;
                  break;

               case 1:
                  m_sProfile.eSliceMode = OMX_VIDEO_SLICEMODE_AVCMBSlice;
                  break;

               case 2:
                  m_sProfile.eSliceMode = OMX_VIDEO_SLICEMODE_AVCByteSlice;
                  break;

               default:
                  E("invalid Slice Mode");
                  m_sProfile.eSliceMode = OMX_VIDEO_SLICEMODE_AVCDefault;
                  break;
              }
            }
            if (profile_argi < argc)
            {
               if (!strncmp(argv[profile_argi], "0x", 2) || !strncmp(argv[profile_argi], "0x", 2))
               {
                  m_sProfile.nUserProfile = strtoul(argv[profile_argi], NULL, 16);
               }
               else
               {
                  m_sProfile.nUserProfile = strtoul(argv[profile_argi], NULL, 10);
               }
               if (!m_sProfile.nUserProfile || m_sProfile.nUserProfile == ULONG_MAX)
               {
                  E("invalid specified Profile %s, using default", argv[profile_argi]);
                  m_sProfile.nUserProfile = 0;
               }
            }
         }
      }
      m_sProfile.cInFileName = argv[7];
      m_sProfile.cOutFileName = argv[8];
   }
   else if (strcmp("PROFILE", argv[1]) == 0 ||
            strcmp("profile", argv[1]) == 0)
   {//263
      m_eMode = MODE_PROFILE;
      if (argc != 8)
      {
         usage(argv[0]);
      }
      m_sProfile.cInFileName = argv[7];
      m_sProfile.cOutFileName = NULL;
   }
   else
   {
      usage(argv[0]);
   }


   if (strcmp("QCIF", argv[2]) == 0 ||
       strcmp("qcif", argv[2]) == 0)
   {
      m_sProfile.nFrameWidth = 176;
      m_sProfile.nFrameHeight = 144;
      m_sProfile.nFrameBytes = 176*144*3/2;
      m_sProfile.eLevel = OMX_VIDEO_MPEG4Level0;
   }
   else if (strcmp("QVGA", argv[2]) == 0 ||
            strcmp("qvga", argv[2]) == 0)
   {
      m_sProfile.nFrameWidth = 320;
      m_sProfile.nFrameHeight = 240;
      m_sProfile.nFrameBytes = 320*240*3/2;
      m_sProfile.eLevel = OMX_VIDEO_MPEG4Level1;
   }


    else if (strcmp("VGA", argv[2]) == 0 ||
            strcmp("vga", argv[2]) == 0)
   {
      m_sProfile.nFrameWidth = 640;
      m_sProfile.nFrameHeight = 480;
      m_sProfile.nFrameBytes = 640*480*3/2;
      m_sProfile.eLevel = OMX_VIDEO_MPEG4Level1;
   }

    else if (strcmp("WVGA", argv[2]) == 0 ||
            strcmp("wvga", argv[2]) == 0)
   {
      m_sProfile.nFrameWidth = 800;
      m_sProfile.nFrameHeight = 480;
      m_sProfile.nFrameBytes = 800*480*3/2;
      m_sProfile.eLevel = OMX_VIDEO_MPEG4Level1;
   }
  else if (strcmp("CIF", argv[2]) == 0 ||
            strcmp("cif", argv[2]) == 0)
   {
      m_sProfile.nFrameWidth = 352;
      m_sProfile.nFrameHeight = 288;
      m_sProfile.nFrameBytes = 352*288*3/2;
      m_sProfile.eLevel = OMX_VIDEO_MPEG4Level1;
   }
   else if (strcmp("720", argv[2]) == 0)
   {
      m_sProfile.nFrameWidth = 1280;
      m_sProfile.nFrameHeight = 720;
      m_sProfile.nFrameBytes = 720*1280*3/2;
      m_sProfile.eLevel = OMX_VIDEO_MPEG4Level1;
   }
   else if (strcmp("1080", argv[2]) == 0)
   {
      m_sProfile.nFrameWidth = 1920;
      m_sProfile.nFrameHeight = 1080;
      m_sProfile.nFrameBytes = 1920*1080*3/2;
      m_sProfile.eLevel = OMX_VIDEO_MPEG4Level1;
   }
#ifdef _MSM8974_
   else if (strcmp("4K2K", argv[2]) == 0)
   {
      m_sProfile.nFrameWidth = 4096;
      m_sProfile.nFrameHeight = 2160;
      m_sProfile.nFrameBytes = 4096*2160*3/2;
      m_sProfile.eLevel = OMX_VIDEO_MPEG4Level1;
   }
   else if (strcmp("2160P", argv[2]) == 0)
   {
      m_sProfile.nFrameWidth = 3840;
      m_sProfile.nFrameHeight = 2160;
      m_sProfile.nFrameBytes = 3840*2160*3/2;
      m_sProfile.eLevel = OMX_VIDEO_MPEG4Level1;
   }
#endif
   else if (parseWxH(argv[2], &m_sProfile.nFrameWidth, &m_sProfile.nFrameHeight))
   {
      m_sProfile.nFrameBytes = m_sProfile.nFrameWidth*m_sProfile.nFrameHeight*3/2;
      m_sProfile.eLevel = OMX_VIDEO_MPEG4Level1;
   }
   else
   {
      usage(argv[0]);
   }

#ifdef _MSM8974_
   m_sProfile.nFramestride =  (m_sProfile.nFrameWidth + 31) & (~31);
   m_sProfile.nFrameScanlines = (m_sProfile.nFrameHeight + 31) & (~31);
   m_sProfile.nFrameBytes = ((m_sProfile.nFramestride * m_sProfile.nFrameScanlines * 3/2) + 4095) & (~4095);
   m_sProfile.nFrameRead = m_sProfile.nFramestride * m_sProfile.nFrameScanlines * 3/2;
#endif
   if (m_eMode == MODE_DISPLAY ||
       m_eMode == MODE_PREVIEW)
   {
      m_sProfile.nFramerate = atof(argv[3]);
      m_nFramePlay = atoi(argv[4]);

   }
   else if (m_eMode == MODE_LIVE_ENCODE ||
            m_eMode == MODE_FILE_ENCODE ||
            m_eMode == MODE_PROFILE)
   {
      if ((!strcmp(argv[3], "MP4")) || (!strcmp(argv[3], "mp4")))
      {
         m_sProfile.eCodec = OMX_VIDEO_CodingMPEG4;
      }
      else if ((!strcmp(argv[3], "H263")) || (!strcmp(argv[3], "h263")))
      {
         m_sProfile.eCodec = OMX_VIDEO_CodingH263;
      }
      else if ((!strcmp(argv[3], "H264")) || (!strcmp(argv[3], "h264")))
      {
         m_sProfile.eCodec = OMX_VIDEO_CodingAVC;
      }
#ifdef _MSM8974_
      else if ((!strcmp(argv[3], "VP8")) || (!strcmp(argv[3], "vp8")))
      {
         m_sProfile.eCodec = OMX_VIDEO_CodingVPX;
      }
#endif
      else
      {
         usage(argv[0]);
      }

      m_sProfile.nFramerate = atof(argv[4]);
      m_sProfile.nBitrate = atoi(argv[5]);
//      m_sProfile.eControlRate = OMX_Video_ControlRateVariable;
      m_nFramePlay = atoi(argv[6]);
      if (dyn_file_arg < argc)
      {
        m_pDynConfFile = fopen(argv[dyn_file_arg], "r");
        if (!m_pDynConfFile)
          E("ERROR: Cannot open dynamic config file: %s", argv[dyn_file_arg]);
        else
        {
          memset(&dynamic_config, 0, sizeof(struct DynamicConfig));
        }
      }
   }
}

void* Watchdog(void* data)
{
   while (1)
   {
      sleep(1000);
      if (m_bWatchDogKicked == true)
         m_bWatchDogKicked = false;
      else
         E("watchdog has not been kicked. we may have a deadlock");
   }
   return NULL;
}

int main(int argc, char** argv)
{
   OMX_U8* pvirt = NULL;
   int result;
   float enc_time_sec=0.0,enc_time_usec=0.0;

   m_nInFd = -1;
   m_nOutFd = -1;
   m_nTimeStamp = 0;
   m_nFrameIn = 0;
   m_nFrameOut = 0;

   memset(&m_sMsgQ, 0, sizeof(MsgQ));
   memset(&m_sProfile, 0, sizeof(m_sProfile));
   parseArgs(argc, argv);

   D("fps=%f, bitrate=%u, width=%u, height=%u, frame bytes=%u",
     m_sProfile.nFramerate,
     m_sProfile.nBitrate,
     m_sProfile.nFrameWidth,
     m_sProfile.nFrameHeight,
     m_sProfile.nFrameBytes);
#ifdef _MSM8974_
   D("Frame stride=%u, scanlines=%u, read=%u",
		   m_sProfile.nFramestride,
		   m_sProfile.nFrameScanlines,
		   m_sProfile.nFrameRead);
#endif


   //if (m_eMode != MODE_PREVIEW && m_eMode != MODE_DISPLAY)
   //{
     // pthread_t wd;
     // pthread_create(&wd, NULL, Watchdog, NULL);
   //}

   for (int x = 0; x < num_in_buffers; x++)
   {
      // mark all buffers as ready to use
      m_bInFrameFree[x] = OMX_TRUE;
   }


    if (m_eMode != MODE_PROFILE)
   {
      #if T_ARM
	   m_nOutFd = open(m_sProfile.cOutFileName, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO);
      #else
	  m_nOutFd = open(m_sProfile.cOutFileName,0);
      #endif
      if (m_nOutFd < 0)
      {
         E("could not open output file %s", m_sProfile.cOutFileName);
         CHK(1);
      }
   }

   pthread_mutex_init(&m_mutex, NULL);
   pthread_cond_init(&m_signal, NULL);

   if (m_eMode != MODE_PREVIEW)
   {
      VencTest_Initialize();
   }

   ////////////////////////////////////////
   // Camera + Encode
   ////////////////////////////////////////
   if (m_eMode == MODE_LIVE_ENCODE)
   {
     CameraTest_Initialize(m_sProfile.nFramerate,
                            m_sProfile.nFrameWidth,
                            m_sProfile.nFrameHeight,
                            PreviewCallback);
      CameraTest_Run();
   }

   if (m_eMode == MODE_FILE_ENCODE ||
       m_eMode == MODE_PROFILE)
   {
      int i;
      #if T_ARM
      m_nInFd = open(m_sProfile.cInFileName, O_RDONLY);
      #else
      m_nInFd = open(m_sProfile.cInFileName,1);
      #endif
	  if (m_nInFd < 0)
      {
         E("could not open input file");
         CHK(1);

      }
      D("going to idle state");
      //SetState(OMX_StateIdle);
      OMX_SendCommand(m_hHandle,
                      OMX_CommandStateSet,
                      (OMX_U32) OMX_StateIdle,
                       NULL);

      OMX_PARAM_PORTDEFINITIONTYPE portDef;

      portDef.nPortIndex = 0;
      result = OMX_GetParameter(m_hHandle, OMX_IndexParamPortDefinition, &portDef);
      CHK(result);

      D("allocating Input buffers");
      num_in_buffers = portDef.nBufferCountActual;
      for (i = 0; i < portDef.nBufferCountActual; i++)
      {
         OMX_QCOM_PLATFORM_PRIVATE_PMEM_INFO* pMem = new OMX_QCOM_PLATFORM_PRIVATE_PMEM_INFO;
         pvirt = (OMX_U8*)PmemMalloc(pMem, m_sProfile.nFrameBytes);

         if(pvirt == NULL)
         {
            CHK(1);
         }
         result = VencTest_RegisterYUVBuffer(&m_pInBuffers[i],
                                             (OMX_U8*) pvirt,
                                             (OMX_PTR) pMem);
         CHK(result);
      }
   }
   else if (m_eMode == MODE_LIVE_ENCODE)
   {
       D("going to idle state");
       //SetState(OMX_StateIdle);
       OMX_SendCommand(m_hHandle,
                       OMX_CommandStateSet,
                       (OMX_U32) OMX_StateIdle,
                        NULL);
   }

   int i;
   OMX_PARAM_PORTDEFINITIONTYPE portDef;

   portDef.nPortIndex = 1;
   result = OMX_GetParameter(m_hHandle, OMX_IndexParamPortDefinition, &portDef);
   CHK(result);

   D("allocating & calling usebuffer for Output port");
   num_out_buffers = portDef.nBufferCountActual;
   for (i = 0; i < portDef.nBufferCountActual; i++)
   {
      void* pBuff;

      pBuff = malloc(portDef.nBufferSize);
     D("portDef.nBufferSize = %d ",portDef.nBufferSize);
      result = OMX_UseBuffer(m_hHandle,
                             &m_pOutBuffers[i],
                             (OMX_U32) PORT_INDEX_OUT,
                             NULL,
                             portDef.nBufferSize,
                             (OMX_U8*) pBuff);
      CHK(result);
   }
   D("allocate done");

        // D("Going to state " # eState"...");

         while (m_eState != OMX_StateIdle)
         {
            sleep(1);
         }
         //D("Now in state " # eState);


   D("going to executing state");
   SetState(OMX_StateExecuting);
   for (i = 0; i < num_out_buffers; i++)
   {
      D("filling buffer %d", i);
      result = OMX_FillThisBuffer(m_hHandle, m_pOutBuffers[i]);
      //sleep(1000);
      CHK(result);
   }

   if (m_eMode == MODE_FILE_ENCODE)
   {
      // encode the first frame to kick off the whole process
      VencTest_ReadAndEmpty(m_pInBuffers[0]);
    //  FBTest_DisplayImage(((PmemBuffer*) m_pInBuffers[0]->pAppPrivate)->fd,0);
   }

   if (m_eMode == MODE_PROFILE)
   {
      int i;

      // read several frames into memory
      D("reading frames into memory");
      for (i = 0; i < num_in_buffers; i++)
      {
        D("[%d] address 0x%x",i, m_pInBuffers[i]->pBuffer);
#ifdef MAX_RES_720P
         read(m_nInFd,
              m_pInBuffers[i]->pBuffer,
              m_sProfile.nFrameBytes);
#else
         // read Y first
         read(m_nInFd,
              m_pInBuffers[i]->pBuffer,
              m_sProfile.nFrameWidth*m_sProfile.nFrameHeight);

         // check alignment for offset to C
         OMX_U32 offset_to_c = m_sProfile.nFrameWidth * m_sProfile.nFrameHeight;

         const OMX_U32 C_2K = (1024*2),
            MASK_2K = C_2K-1,
            IMASK_2K = ~MASK_2K;

         if (offset_to_c & MASK_2K)
         {
            // offset to C is not 2k aligned, adjustment is required
            offset_to_c = (offset_to_c & IMASK_2K) + C_2K;
         }

         // read C
         read(m_nInFd,
              m_pInBuffers[i]->pBuffer + offset_to_c,
              m_sProfile.nFrameWidth*m_sProfile.nFrameHeight/2);
#endif

      }

     // FBTest_Initialize(m_sProfile.nFrameWidth, m_sProfile.nFrameHeight);

      // loop over the mem-resident frames and encode them
      D("beging playing mem-resident frames...");
      for (i = 0; m_nFramePlay == 0 || i < m_nFramePlay; i++)
      {
         int idx = i % num_in_buffers;
         if (m_bInFrameFree[idx] == OMX_FALSE)
         {
            int j;
            E("the expected buffer is not free, but lets find another");

            idx = -1;

            // lets see if we can find another free buffer
            for (j = 0; j < num_in_buffers; j++)
            {
               if(m_bInFrameFree[j])
               {
                  idx = j;
                  break;
               }
            }
         }

         // if we have a free buffer let's encode it
         if (idx >= 0)
         {
            D("encode frame %d...m_pInBuffers[idx]->pBuffer=0x%x", i,m_pInBuffers[idx]->pBuffer);
            m_bInFrameFree[idx] = OMX_FALSE;
            VencTest_EncodeFrame(m_pInBuffers[idx]->pBuffer,
                                 m_nTimeStamp);
            D("display frame %d...", i);
        //    FBTest_DisplayImage(((PmemBuffer*) m_pInBuffers[idx]->pAppPrivate)->fd,0);
            m_nTimeStamp += 1000000 / m_sProfile.nFramerate;
         }
         else
         {
            E("wow, no buffers are free, performance "
              "is not so good. lets just sleep some more");

         }
         D("sleep for %d microsec", 1000000/m_sProfile.nFramerate);
         sleep (1000000 / m_sProfile.nFramerate);
      }
     // FBTest_Exit();
   }

   Msg msg;
   bool bQuit = false;
   while ((m_eMode == MODE_FILE_ENCODE || m_eMode == MODE_LIVE_ENCODE) &&
          !bQuit)
   {
      PopMessage(&msg);
      switch (msg.id)
      {
      //////////////////////////////////
      // FRAME IS ENCODED
      //////////////////////////////////
      case MSG_ID_INPUT_FRAME_DONE:
         /*pthread_mutex_lock(&m_mutex);
         ++m_nFrameOut;
         if (m_nFrameOut == m_nFramePlay && m_nFramePlay != 0)
         {
            bQuit = true;
         }
         pthread_mutex_unlock(&m_mutex);*/

         if (!bQuit && m_eMode == MODE_FILE_ENCODE)
         {
            D("pushing another frame down to encoder");
            if (VencTest_ReadAndEmpty(msg.data.sBitstreamData.pBuffer))
            {
               // we have read the last frame
               D("main is exiting...");
               bQuit = true;
            }
         }
       break;
      case MSG_ID_OUTPUT_FRAME_DONE:
         D("================ writing frame %d = %d bytes to output file",
           m_nFrameOut+1,
           msg.data.sBitstreamData.pBuffer->nFilledLen);
         D("StopEncodeTime=%lld", GetTimeStamp());


		 write(m_nOutFd,
               msg.data.sBitstreamData.pBuffer->pBuffer,
               msg.data.sBitstreamData.pBuffer->nFilledLen);


         result = OMX_FillThisBuffer(m_hHandle,
                                     msg.data.sBitstreamData.pBuffer);

         if (result != OMX_ErrorNone)
         {
            CHK(result);
         }

         pthread_mutex_lock(&m_mutex);
         ++m_nFrameOut;
         if (m_nFrameOut == m_nFramePlay && m_nFramePlay != 0)
         {
            bQuit = true;
         }
         pthread_mutex_unlock(&m_mutex);
         break;

      default:
         E("invalid msg id %d", (int) msg.id);
      } // end switch (msg.id)

/*  // TO UNCOMMENT FOR PAUSE TESTINGS
      if(m_nFrameOut == 10)
      {
         E("\nGoing to Pause state\n");
         SetState(OMX_StatePause);
         sleep(3);
//REQUEST AN I FRAME AFTER PAUSE
         OMX_CONFIG_INTRAREFRESHVOPTYPE voprefresh;
         voprefresh.nPortIndex = (OMX_U32)PORT_INDEX_OUT;
         voprefresh.IntraRefreshVOP = OMX_TRUE;
         result = OMX_SetConfig(m_hHandle,
                                   OMX_IndexConfigVideoIntraVOPRefresh,
                                   &voprefresh);
         E("\n OMX_IndexConfigVideoIntraVOPRefresh Set Paramter port");
         CHK(result);
         E("\nGoing to executing state\n");
         SetState(OMX_StateExecuting);
      }
*/
   } // end while (!bQuit)


   if (m_eMode == MODE_LIVE_ENCODE)
   {
      CameraTest_Exit();
      close(m_nOutFd);
   }
   else if (m_eMode == MODE_FILE_ENCODE ||
            m_eMode == MODE_PROFILE)
   {
      // deallocate pmem buffers
      for (int i = 0; i < num_in_buffers; i++)
      {
         PmemFree((OMX_QCOM_PLATFORM_PRIVATE_PMEM_INFO*)m_pInBuffers[i]->pAppPrivate,
                  m_pInBuffers[i]->pBuffer,
                  m_sProfile.nFrameBytes);
         delete (OMX_QCOM_PLATFORM_PRIVATE_PMEM_INFO*) m_pInBuffers[i]->pAppPrivate;
      }
      close(m_nInFd);

      if (m_eMode == MODE_FILE_ENCODE)
      {
         close(m_nOutFd);
      }
      if (m_pDynConfFile)
      {
        fclose(m_pDynConfFile);
        m_pDynConfFile = NULL;
      }
   }

   if (m_eMode != MODE_PREVIEW)
   {
      D("exit encoder test");
      VencTest_Exit();
   }

   pthread_mutex_destroy(&m_mutex);
   pthread_cond_destroy(&m_signal);

   /* Time Statistics Logging */
   if(0 != m_sProfile.nFramerate)
   {
      enc_time_usec = m_nTimeStamp - (1000000 / m_sProfile.nFramerate);
      enc_time_sec =enc_time_usec/1000000;
      if(0 != enc_time_sec)
      {
         printf("Total Frame Rate: %f",ebd_cnt/enc_time_sec);
         printf("\nEncoder Bitrate :%lf Kbps",(tot_bufsize*8)/(enc_time_sec*1000));
      }
   }
   else
   {
      printf("\n\n Encode Time is zero");
   }
   printf("\nTotal Number of Frames :%d",ebd_cnt);
   printf("\nNumber of dropped frames during encoding:%d\n",ebd_cnt-fbd_cnt);
   /* End of Time Statistics Logging */

   D("main has exited");
   return 0;
}