summaryrefslogtreecommitdiffstats
path: root/test/decoder/main.c
blob: 89a54c5d5183681efea5f17c273b5cb7b47bbffe (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
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
/******************************************************************************
 *
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/
/*****************************************************************************/
/*                                                                           */
/*  File Name         : main.c                                               */
/*                                                                           */
/*  Description       : Contains an application that demonstrates use of H264*/
/*                      decoder API                                          */
/*                                                                           */
/*  List of Functions :                                                      */
/*                                                                           */
/*  Issues / Problems : None                                                 */
/*                                                                           */
/*  Revision History  :                                                      */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes                              */
/*         07 09 2012   Harish          Initial Version                      */
/*****************************************************************************/
/*****************************************************************************/
/* File Includes                                                             */
/*****************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#ifdef X86_MINGW
#include <signal.h>
#endif

#ifndef IOS
#include <malloc.h>
#endif
#ifdef IOS_DISPLAY
#include "cast_types.h"
#else
#include "ih264_typedefs.h"
#endif

#include "iv.h"
#include "ivd.h"
#include "ih264d.h"
#include "ithread.h"

#ifdef WINDOWS_TIMER
#include <windows.h>
#else
#include <sys/time.h>
#endif

//#define ADAPTIVE_TEST
#define ADAPTIVE_MAX_WD 1920
#define ADAPTIVE_MAX_HT 1088

#define ALIGN8(x) ((((x) + 7) >> 3) << 3)
#define NUM_DISPLAY_BUFFERS 4
#define DEFAULT_FPS         30

#define ENABLE_DEGRADE 0
#define MAX_DISP_BUFFERS    64
#define EXTRA_DISP_BUFFERS  8
#define STRLENGTH 1000

//#define TEST_FLUSH
#define FLUSH_FRM_CNT 100
//#define APP_EXTRA_BUFS 1

#ifdef IOS
#define PATHLENMAX 500
char filename_with_path[PATHLENMAX];
#endif

#ifdef PROFILE_ENABLE
    #ifdef WINDOWS_TIMER
        typedef  LARGE_INTEGER TIMER;
    #else
        //#ifdef GCC_TIMER
        typedef struct timeval TIMER;
        //#endif
    #endif
#else
        typedef WORD32 TIMER;
#endif

#ifdef PROFILE_ENABLE
    #ifdef WINDOWS_TIMER
        #define GETTIME(timer) QueryPerformanceCounter(timer);
    #else
    //#ifdef GCC_TIMER
        #define GETTIME(timer) gettimeofday(timer,NULL);
    //#endif
    #endif

    #ifdef WINDOWS_TIMER
        #define ELAPSEDTIME(s_start_timer,s_end_timer, s_elapsed_time, frequency) \
                  { \
                   TIMER s_temp_time;   \
                   s_temp_time.LowPart = s_end_timer.LowPart - s_start_timer.LowPart ; \
                   s_elapsed_time = (UWORD32) ( ((DOUBLE)s_temp_time.LowPart / (DOUBLE)frequency.LowPart )  * 1000000); \
                }
    #else
    //#ifdef GCC_TIMER
        #define ELAPSEDTIME(s_start_timer,s_end_timer, s_elapsed_time, frequency) \
                   s_elapsed_time = ((s_end_timer.tv_sec - s_start_timer.tv_sec) * 1000000) + (s_end_timer.tv_usec - s_start_timer.tv_usec);
    //#endif
    #endif

#else
    #define GETTIME(timer)
    #define ELAPSEDTIME(s_start_timer,s_end_timer, s_elapsed_time, frequency)
#endif


/* Function declarations */
#ifndef MD5_DISABLE
void calc_md5_cksum(UWORD8 *pu1_inbuf,UWORD32 u4_stride,UWORD32 u4_width,UWORD32 u4_height,UWORD8 *pu1_cksum_p );
#else
#define calc_md5_cksum(a, b, c, d, e)
#endif
#ifdef SDL_DISPLAY
void* sdl_disp_init(UWORD32, UWORD32, WORD32, WORD32, WORD32, WORD32, WORD32, WORD32 *, WORD32 *);
void sdl_alloc_disp_buffers(void *);
void sdl_display(void *, WORD32 );
void sdl_set_disp_buffers(void *, WORD32, UWORD8 **, UWORD8 **, UWORD8 **);
void sdl_disp_deinit(void *);
void sdl_disp_usleep(UWORD32);
IV_COLOR_FORMAT_T sdl_get_color_fmt(void);
UWORD32 sdl_get_stride(void);
#endif

#ifdef INTEL_CE5300
void* gdl_disp_init(UWORD32, UWORD32, WORD32, WORD32, WORD32, WORD32, WORD32, WORD32 *, WORD32 *);
void gdl_alloc_disp_buffers(void *);
void gdl_display(void *, WORD32 );
void gdl_set_disp_buffers(void *, WORD32, UWORD8 **, UWORD8 **, UWORD8 **);
void gdl_disp_deinit(void *);
void gdl_disp_usleep(UWORD32);
IV_COLOR_FORMAT_T gdl_get_color_fmt(void);
UWORD32 gdl_get_stride(void);
#endif

#ifdef FBDEV_DISPLAY
void* fbd_disp_init(UWORD32, UWORD32, WORD32, WORD32, WORD32, WORD32, WORD32, WORD32 *, WORD32 *);
void fbd_alloc_disp_buffers(void *);
void fbd_display(void *, WORD32 );
void fbd_set_disp_buffers(void *, WORD32, UWORD8 **, UWORD8 **, UWORD8 **);
void fbd_disp_deinit(void *);
void fbd_disp_usleep(UWORD32);
IV_COLOR_FORMAT_T fbd_get_color_fmt(void);
UWORD32 fbd_get_stride(void);
#endif

#ifdef IOS_DISPLAY
void* ios_disp_init(UWORD32, UWORD32, WORD32, WORD32, WORD32, WORD32, WORD32, WORD32 *, WORD32 *);
void ios_alloc_disp_buffers(void *);
void ios_display(void *, WORD32 );
void ios_set_disp_buffers(void *, WORD32, UWORD8 **, UWORD8 **, UWORD8 **);
void ios_disp_deinit(void *);
void ios_disp_usleep(UWORD32);
IV_COLOR_FORMAT_T ios_get_color_fmt(void);
UWORD32 ios_get_stride(void);
#endif

typedef struct
{
    UWORD32 u4_piclen_flag;
    UWORD32 u4_file_save_flag;
    UWORD32 u4_chksum_save_flag;
    UWORD32 u4_max_frm_ts;
    IV_COLOR_FORMAT_T e_output_chroma_format;
    IVD_ARCH_T e_arch;
    IVD_SOC_T e_soc;
    UWORD32 dump_q_rd_idx;
    UWORD32 dump_q_wr_idx;
    WORD32  disp_q_wr_idx;
    WORD32  disp_q_rd_idx;

    void *cocodec_obj;
    UWORD32 u4_share_disp_buf;
    UWORD32 num_disp_buf;
    UWORD32 b_pic_present;
    UWORD32 u4_disable_dblk_level;
    WORD32 i4_degrade_type;
    WORD32 i4_degrade_pics;
    UWORD32 u4_num_cores;
    UWORD32 disp_delay;
    WORD32 trace_enable;
    CHAR ac_trace_fname[STRLENGTH];
    CHAR ac_piclen_fname[STRLENGTH];
    CHAR ac_ip_fname[STRLENGTH];
    CHAR ac_op_fname[STRLENGTH];
    CHAR ac_op_chksum_fname[STRLENGTH];
    ivd_out_bufdesc_t s_disp_buffers[MAX_DISP_BUFFERS];
    iv_yuv_buf_t s_disp_frm_queue[MAX_DISP_BUFFERS];
    UWORD32 s_disp_frm_id_queue[MAX_DISP_BUFFERS];
    UWORD32 loopback;
    UWORD32 display;
    UWORD32 full_screen;
    UWORD32 fps;

    UWORD32 u4_strd;

    /* For signalling to display thread */
    UWORD32 u4_pic_wd;
    UWORD32 u4_pic_ht;

    /* For IOS diplay */
    WORD32 i4_screen_wd;
    WORD32 i4_screen_ht;

    //UWORD32 u4_output_present;
    WORD32  quit;
    WORD32  paused;


    void *pv_disp_ctx;
    void *display_thread_handle;
    WORD32 display_thread_created;
    volatile WORD32 display_init_done;
    volatile WORD32 display_deinit_flag;

    void *(*disp_init)(UWORD32, UWORD32, WORD32, WORD32, WORD32, WORD32, WORD32, WORD32 *, WORD32 *);
    void (*alloc_disp_buffers)(void *);
    void (*display_buffer)(void *, WORD32);
    void (*set_disp_buffers)(void *, WORD32, UWORD8 **, UWORD8 **, UWORD8 **);
    void (*disp_deinit)(void *);
    void (*disp_usleep)(UWORD32);
    IV_COLOR_FORMAT_T (*get_color_fmt)(void);
    UWORD32 (*get_stride)(void);
} vid_dec_ctx_t;



typedef enum
{
    INVALID,
    HELP,
    VERSION,
    INPUT_FILE,
    OUTPUT,
    CHKSUM,
    SAVE_OUTPUT,
    SAVE_CHKSUM,
    CHROMA_FORMAT,
    NUM_FRAMES,
    NUM_CORES,
    DISABLE_DEBLOCK_LEVEL,
    SHARE_DISPLAY_BUF,
    LOOPBACK,
    DISPLAY,
    FULLSCREEN,
    FPS,
    TRACE,
    CONFIG,

    DEGRADE_TYPE,
    DEGRADE_PICS,
    ARCH,
    SOC,
    PICLEN,
    PICLEN_FILE,
} ARGUMENT_T;

typedef struct
{
    CHAR argument_shortname[4];
    CHAR argument_name[128];
    ARGUMENT_T argument;
    CHAR description[512];
} argument_t;

static const argument_t argument_mapping[] =
{
    {"-h",  "--help",                   HELP,
         "Print this help\n"},
    { "-c", "--config",      CONFIG,
         "config file (Default: test.cfg)\n" },

    {"-v",  "--version",                VERSION,
         "Version information\n"},
    {"-i",  "--input",                  INPUT_FILE,
         "Input file\n"},
    {"-o",  "--output",                 OUTPUT,
         "Output file\n"},
    {"--",  "--piclen",                 PICLEN,
          "Flag to signal if the decoder has to use a file containing number of bytes in each picture to be fed in each call\n"},
    {"--",  "--piclen_file",                 PICLEN_FILE,
            "File containing number of bytes in each picture - each line containing one i4_size\n"},
    {"--",  "--chksum",          CHKSUM,
         "Output MD5 Checksum file\n"},
    { "-s", "--save_output",            SAVE_OUTPUT,
          "Save Output file\n" },
    { "--", "--save_chksum",            SAVE_CHKSUM,
          "Save Check sum file\n" },
    {"--",  "--chroma_format",          CHROMA_FORMAT,
         "Output Chroma format Supported values YUV_420P, YUV_422ILE, RGB_565, YUV_420SP_UV, YUV_420SP_VU\n" },
    { "-n", "--num_frames",             NUM_FRAMES,
         "Number of frames to be decoded\n" },
    { "--", "--num_cores",              NUM_CORES,
          "Number of cores to be used\n" },
    { "--", "--share_display_buf",      SHARE_DISPLAY_BUF,
          "Enable shared display buffer mode\n" },
    {"--", "--disable_deblock_level", DISABLE_DEBLOCK_LEVEL,
         "Disable deblocking level : 0 to 4 - 0 Enable deblocking 4 Disable deblocking completely\n"},
    { "--", "--loopback",      LOOPBACK,
        "Enable playback in a loop\n" },
    { "--", "--display",      DISPLAY,
        "Enable display (uses SDL)\n" },
     { "--", "--fullscreen",      FULLSCREEN,
        "Enable full screen (Only for GDL and SDL)\n" },
    { "--", "--fps",      FPS,
        "FPS to be used for display \n" },
    {"-i",  "--trace",                   TRACE,
         "Trace file\n"},

    {"--",  "--degrade_type",  DEGRADE_TYPE,
         "Degrade type : 0: No degrade 0th bit set : Disable SAO 1st bit set : Disable deblocking 2nd bit set : Faster inter prediction filters 3rd bit set : Fastest inter prediction filters\n" },
    {"--",  "--degrade_pics",  DEGRADE_PICS,
         "Degrade pics : 0 : No degrade  1 : Only on non-reference frames  2 : Do not degrade every 4th or key frames  3 : All non-key frames  4 : All frames"},

    {"--",  "--arch", ARCH,
         "Set Architecture. Supported values  ARM_NONEON, ARM_A9Q, ARM_A7, ARM_A5, ARM_NEONINTR,ARMV8_GENERIC, X86_GENERIC, X86_SSSE3, X86_SSE4 \n" },
    {"--",  "--soc", SOC,
         "Set SOC. Supported values  GENERIC, HISI_37X \n" },

};

#define PEAK_WINDOW_SIZE            8
#define DEFAULT_SHARE_DISPLAY_BUF   0
#define STRIDE                      0
#define DEFAULT_NUM_CORES           1


#define DUMP_SINGLE_BUF 0
#define IV_ISFATALERROR(x)         (((x) >> IVD_FATALERROR) & 0x1)

#define ivd_api_function        ih264d_api_function

#ifdef IOS
char filename_trace[PATHLENMAX];
#endif

#if ANDROID_NDK
/*****************************************************************************/
/*                                                                           */
/*  Function Name : raise                                                    */
/*                                                                           */
/*  Description   : Needed as a workaround when the application is built in  */
/*                  Android NDK. This is an exception to be called for divide*/
/*                  by zero error                                            */
/*                                                                           */
/*  Inputs        : a                                                        */
/*  Globals       :                                                          */
/*  Processing    : None                                                     */
/*                                                                           */
/*  Outputs       :                                                          */
/*  Returns       :                                                          */
/*                                                                           */
/*  Issues        :                                                          */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes                              */
/*         07 09 2012   100189          Initial Version                      */
/*                                                                           */
/*****************************************************************************/
int raise(int a)
{
    printf("Divide by zero\n");
    return 0;
}
#endif

#ifdef _WIN32
/*****************************************************************************/
/* Function to print library calls                                           */
/*****************************************************************************/
/*****************************************************************************/
/*                                                                           */
/*  Function Name : memalign                                                 */
/*                                                                           */
/*  Description   : Returns malloc data. Ideally should return aligned memory*/
/*                  support alignment will be added later                    */
/*                                                                           */
/*  Inputs        : alignment                                                */
/*                  i4_size                                                     */
/*  Globals       :                                                          */
/*  Processing    :                                                          */
/*                                                                           */
/*  Outputs       :                                                          */
/*  Returns       :                                                          */
/*                                                                           */
/*  Issues        :                                                          */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes                              */
/*         07 09 2012   100189          Initial Version                      */
/*                                                                           */
/*****************************************************************************/

void * ih264a_aligned_malloc(void *pv_ctxt, WORD32 alignment, WORD32 i4_size)
{
    (void)pv_ctxt;
    return (void *)_aligned_malloc(i4_size, alignment);
}

void ih264a_aligned_free(void *pv_ctxt, void *pv_buf)
{
    (void)pv_ctxt;
    _aligned_free(pv_buf);
    return;
}
#endif

#if IOS
void * ih264a_aligned_malloc(void *pv_ctxt, WORD32 alignment, WORD32 i4_size)
{
    (void)pv_ctxt;
    return malloc(i4_size);
}

void ih264a_aligned_free(void *pv_ctxt, void *pv_buf)
{
    (void)pv_ctxt;
    free(pv_buf);
    return;
}
#endif

#if (!defined(IOS)) && (!defined(_WIN32))
void * ih264a_aligned_malloc(void *pv_ctxt, WORD32 alignment, WORD32 i4_size)
{
   (void)pv_ctxt;
    return memalign(alignment, i4_size);
}

void ih264a_aligned_free(void *pv_ctxt, void *pv_buf)
{
    (void)pv_ctxt;
    free(pv_buf);
    return;
}
#endif
/*****************************************************************************/
/*                                                                           */
/*  Function Name : set_degrade                                 */
/*                                                                           */
/*  Description   : Control call to set degrade level       */
/*                                                                           */
/*                                                                           */
/*  Inputs        : codec_obj  - Codec Handle                                */
/*                  type - degrade level value between 0 to 4                */
/*                    0 : No degrade                                         */
/*                    1st bit : Disable SAO                                  */
/*                    2nd bit : Disable Deblock                              */
/*                    3rd bit : Faster MC for non-ref                        */
/*                    4th bit : Fastest MC for non-ref                       */
/*                  pics - Pictures that are are degraded                    */
/*                    0 : No degrade                                         */
/*                    1 : Non-ref pictures                                   */
/*                    2 : Pictures at given interval are not degraded        */
/*                    3 : All non-key pictures                               */
/*                    4 : All pictures                                       */
/*  Globals       :                                                          */
/*  Processing    : Calls degrade control to the codec                       */
/*                                                                           */
/*  Outputs       :                                                          */
/*  Returns       : Control call return i4_status                               */
/*                                                                           */
/*  Issues        :                                                          */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes                              */
/*         07 09 2012   100189          Initial Version                      */
/*                                                                           */
/*****************************************************************************/

IV_API_CALL_STATUS_T set_degrade(void *codec_obj, UWORD32 type, WORD32 pics)
{
    ih264d_ctl_degrade_ip_t s_ctl_ip;
    ih264d_ctl_degrade_op_t s_ctl_op;
    void *pv_api_ip, *pv_api_op;
    IV_API_CALL_STATUS_T e_dec_status;

    s_ctl_ip.u4_size = sizeof(ih264d_ctl_degrade_ip_t);
    s_ctl_ip.i4_degrade_type = type;
    s_ctl_ip.i4_nondegrade_interval = 4;
    s_ctl_ip.i4_degrade_pics = pics;

    s_ctl_op.u4_size = sizeof(ih264d_ctl_degrade_op_t);

    pv_api_ip = (void *)&s_ctl_ip;
    pv_api_op = (void *)&s_ctl_op;

    s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
    s_ctl_ip.e_sub_cmd = (IVD_CONTROL_API_COMMAND_TYPE_T) IH264D_CMD_CTL_DEGRADE;

    e_dec_status = ivd_api_function((iv_obj_t *)codec_obj, pv_api_ip, pv_api_op);

    if(IV_SUCCESS != e_dec_status)
    {
        printf("Error in setting degrade level \n");
    }
    return (e_dec_status);

}



/*****************************************************************************/
/*                                                                           */
/*  Function Name : enable_skipb_frames                                      */
/*                                                                           */
/*  Description   : Control call to enable skipping of b frames              */
/*                                                                           */
/*                                                                           */
/*  Inputs        : codec_obj : Codec handle                                 */
/*  Globals       :                                                          */
/*  Processing    : Calls enable skip B frames control                       */
/*                                                                           */
/*  Outputs       :                                                          */
/*  Returns       : Control call return i4_status                               */
/*                                                                           */
/*  Issues        :                                                          */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes                              */
/*         07 09 2012   100189          Initial Version                      */
/*                                                                           */
/*****************************************************************************/

IV_API_CALL_STATUS_T enable_skipb_frames(void *codec_obj,
                                         vid_dec_ctx_t *ps_app_ctx)
{
    ivd_ctl_set_config_ip_t s_ctl_ip;
    ivd_ctl_set_config_op_t s_ctl_op;
    IV_API_CALL_STATUS_T e_dec_status;

    s_ctl_ip.u4_disp_wd = ps_app_ctx->u4_strd;
    s_ctl_ip.e_frm_skip_mode = IVD_SKIP_B;

    s_ctl_ip.e_frm_out_mode = IVD_DISPLAY_FRAME_OUT;
    s_ctl_ip.e_vid_dec_mode = IVD_DECODE_FRAME;
    s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
    s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_SETPARAMS;
    s_ctl_ip.u4_size = sizeof(ivd_ctl_set_config_ip_t);
    s_ctl_op.u4_size = sizeof(ivd_ctl_set_config_op_t);

    e_dec_status = ivd_api_function((iv_obj_t *)codec_obj, (void *)&s_ctl_ip,
                                        (void *)&s_ctl_op);

    if(IV_SUCCESS != e_dec_status)
    {
        printf("Error in Enable SkipB frames \n");
    }

    return e_dec_status;
}
/*****************************************************************************/
/*                                                                           */
/*  Function Name : disable_skipb_frames                                     */
/*                                                                           */
/*  Description   : Control call to disable skipping of b frames             */
/*                                                                           */
/*                                                                           */
/*  Inputs        : codec_obj : Codec handle                                 */
/*  Globals       :                                                          */
/*  Processing    : Calls disable B frame skip control                       */
/*                                                                           */
/*  Outputs       :                                                          */
/*  Returns       : Control call return i4_status                               */
/*                                                                           */
/*  Issues        :                                                          */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes                              */
/*         07 09 2012   100189          Initial Version                      */
/*                                                                           */
/*****************************************************************************/

IV_API_CALL_STATUS_T disable_skipb_frames(void *codec_obj,
                                          vid_dec_ctx_t *ps_app_ctx)
{
    ivd_ctl_set_config_ip_t s_ctl_ip;
    ivd_ctl_set_config_op_t s_ctl_op;
    IV_API_CALL_STATUS_T e_dec_status;

    s_ctl_ip.u4_disp_wd = ps_app_ctx->u4_strd;
    s_ctl_ip.e_frm_skip_mode = IVD_SKIP_NONE;

    s_ctl_ip.e_frm_out_mode = IVD_DISPLAY_FRAME_OUT;
    s_ctl_ip.e_vid_dec_mode = IVD_DECODE_FRAME;
    s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
    s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_SETPARAMS;
    s_ctl_ip.u4_size = sizeof(ivd_ctl_set_config_ip_t);
    s_ctl_op.u4_size = sizeof(ivd_ctl_set_config_op_t);

    e_dec_status = ivd_api_function((iv_obj_t *)codec_obj, (void *)&s_ctl_ip,
                                        (void *)&s_ctl_op);

    if(IV_SUCCESS != e_dec_status)
    {
        printf("Error in Disable SkipB frames\n");
    }

    return e_dec_status;
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : enable_skippb_frames                                     */
/*                                                                           */
/*  Description   : Control call to enable skipping of P & B frames          */
/*                                                                           */
/*                                                                           */
/*  Inputs        : codec_obj : Codec handle                                 */
/*  Globals       :                                                          */
/*  Processing    : Calls enable skip P and B frames control                 */
/*                                                                           */
/*  Outputs       :                                                          */
/*  Returns       : Control call return i4_status                               */
/*                                                                           */
/*  Issues        :                                                          */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes                              */
/*         07 09 2012   100189          Initial Version                      */
/*                                                                           */
/*****************************************************************************/

IV_API_CALL_STATUS_T enable_skippb_frames(void *codec_obj,
                                          vid_dec_ctx_t *ps_app_ctx)
{
    ivd_ctl_set_config_ip_t s_ctl_ip;
    ivd_ctl_set_config_op_t s_ctl_op;
    IV_API_CALL_STATUS_T e_dec_status;

    s_ctl_ip.u4_disp_wd = ps_app_ctx->u4_strd;
    s_ctl_ip.e_frm_skip_mode = IVD_SKIP_PB;

    s_ctl_ip.e_frm_out_mode = IVD_DISPLAY_FRAME_OUT;
    s_ctl_ip.e_vid_dec_mode = IVD_DECODE_FRAME;
    s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
    s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_SETPARAMS;
    s_ctl_ip.u4_size = sizeof(ivd_ctl_set_config_ip_t);
    s_ctl_op.u4_size = sizeof(ivd_ctl_set_config_op_t);

    e_dec_status = ivd_api_function((iv_obj_t *)codec_obj, (void *)&s_ctl_ip,
                                        (void *)&s_ctl_op);
    if(IV_SUCCESS != e_dec_status)
    {
        printf("Error in Enable SkipPB frames\n");
    }

    return e_dec_status;
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : disable_skippb_frames                                    */
/*                                                                           */
/*  Description   : Control call to disable skipping of P and B frames       */
/*                                                                           */
/*                                                                           */
/*  Inputs        : codec_obj : Codec handle                                 */
/*  Globals       :                                                          */
/*  Processing    : Calls disable P and B frame skip control                 */
/*                                                                           */
/*  Outputs       :                                                          */
/*  Returns       : Control call return i4_status                               */
/*                                                                           */
/*  Issues        :                                                          */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes                              */
/*         07 09 2012   100189          Initial Version                      */
/*                                                                           */
/*****************************************************************************/

IV_API_CALL_STATUS_T disable_skippb_frames(void *codec_obj,
                                           vid_dec_ctx_t *ps_app_ctx)
{
    ivd_ctl_set_config_ip_t s_ctl_ip;
    ivd_ctl_set_config_op_t s_ctl_op;
    IV_API_CALL_STATUS_T e_dec_status;

    s_ctl_ip.u4_disp_wd = ps_app_ctx->u4_strd;
    s_ctl_ip.e_frm_skip_mode = IVD_SKIP_NONE;

    s_ctl_ip.e_frm_out_mode = IVD_DISPLAY_FRAME_OUT;
    s_ctl_ip.e_vid_dec_mode = IVD_DECODE_FRAME;
    s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
    s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_SETPARAMS;
    s_ctl_ip.u4_size = sizeof(ivd_ctl_set_config_ip_t);
    s_ctl_op.u4_size = sizeof(ivd_ctl_set_config_op_t);

    e_dec_status = ivd_api_function((iv_obj_t *)codec_obj, (void *)&s_ctl_ip,
                                        (void *)&s_ctl_op);
    if(IV_SUCCESS != e_dec_status)
    {
        printf("Error in Disable SkipPB frames\n");
    }

    return e_dec_status;
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : release_disp_frame                                       */
/*                                                                           */
/*  Description   : Calls release display control - Used to signal to the    */
/*                  decoder that this particular buffer has been displayed   */
/*                  and that the codec is now free to write to this buffer   */
/*                                                                           */
/*                                                                           */
/*  Inputs        : codec_obj : Codec Handle                                 */
/*                  buf_id    : Buffer Id of the buffer to be released       */
/*                              This id would have been returned earlier by  */
/*                              the codec                                    */
/*  Globals       :                                                          */
/*  Processing    : Calls Release Display call                               */
/*                                                                           */
/*  Outputs       :                                                          */
/*  Returns       : Status of release display call                           */
/*                                                                           */
/*  Issues        :                                                          */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes                              */
/*         07 09 2012   100189          Initial Version                      */
/*                                                                           */
/*****************************************************************************/

IV_API_CALL_STATUS_T release_disp_frame(void *codec_obj, UWORD32 buf_id)
{
    ivd_rel_display_frame_ip_t s_video_rel_disp_ip;
    ivd_rel_display_frame_op_t s_video_rel_disp_op;
    IV_API_CALL_STATUS_T e_dec_status;

    s_video_rel_disp_ip.e_cmd = IVD_CMD_REL_DISPLAY_FRAME;
    s_video_rel_disp_ip.u4_size = sizeof(ivd_rel_display_frame_ip_t);
    s_video_rel_disp_op.u4_size = sizeof(ivd_rel_display_frame_op_t);
    s_video_rel_disp_ip.u4_disp_buf_id = buf_id;

    e_dec_status = ivd_api_function((iv_obj_t *)codec_obj, (void *)&s_video_rel_disp_ip,
                                        (void *)&s_video_rel_disp_op);
    if(IV_SUCCESS != e_dec_status)
    {
        printf("Error in Release Disp frame\n");
    }


    return (e_dec_status);
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : get_version                                      */
/*                                                                           */
/*  Description   : Control call to get codec version              */
/*                                                                           */
/*                                                                           */
/*  Inputs        : codec_obj : Codec handle                                 */
/*  Globals       :                                                          */
/*  Processing    : Calls enable skip B frames control                       */
/*                                                                           */
/*  Outputs       :                                                          */
/*  Returns       : Control call return i4_status                               */
/*                                                                           */
/*  Issues        :                                                          */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes                              */
/*         07 09 2012   100189          Initial Version                      */
/*                                                                           */
/*****************************************************************************/

IV_API_CALL_STATUS_T get_version(void *codec_obj)
{
    ivd_ctl_getversioninfo_ip_t ps_ctl_ip;
    ivd_ctl_getversioninfo_op_t ps_ctl_op;
    UWORD8 au1_buf[512];
    IV_API_CALL_STATUS_T i4_status;
    ps_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
    ps_ctl_ip.e_sub_cmd = IVD_CMD_CTL_GETVERSION;
    ps_ctl_ip.u4_size = sizeof(ivd_ctl_getversioninfo_ip_t);
    ps_ctl_op.u4_size = sizeof(ivd_ctl_getversioninfo_op_t);
    ps_ctl_ip.pv_version_buffer = au1_buf;
    ps_ctl_ip.u4_version_buffer_size = sizeof(au1_buf);

    i4_status = ivd_api_function((iv_obj_t *)codec_obj,
                                     (void *)&(ps_ctl_ip),
                                     (void *)&(ps_ctl_op));

    if(i4_status != IV_SUCCESS)
    {
        printf("Error in Getting Version number e_dec_status = %d u4_error_code = %x\n",
                     i4_status, ps_ctl_op.u4_error_code);
    }
    else
    {
        printf("Ittiam Decoder Version number: %s\n",
              (char *)ps_ctl_ip.pv_version_buffer);
    }
    return i4_status;
}
/*****************************************************************************/
/*                                                                           */
/*  Function Name : codec_exit                                               */
/*                                                                           */
/*  Description   : handles unrecoverable errors                             */
/*  Inputs        : Error message                                            */
/*  Globals       : None                                                     */
/*  Processing    : Prints error message to console and exits.               */
/*  Outputs       : Error mesage to the console                              */
/*  Returns       : None                                                     */
/*                                                                           */
/*  Issues        :                                                          */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         07 06 2006   Sankar          Creation                             */
/*                                                                           */
/*****************************************************************************/
void codec_exit(CHAR *pc_err_message)
{
    printf("%s\n", pc_err_message);
    exit(-1);
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : dump_output                                              */
/*                                                                           */
/*  Description   : Used to dump output YUV                                  */
/*  Inputs        : App context, disp output desc, File pointer              */
/*  Globals       : None                                                     */
/*  Processing    : Dumps to a file                                          */
/*  Returns       : None                                                     */
/*                                                                           */
/*  Issues        :                                                          */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         07 06 2006   Sankar          Creation                             */
/*                                                                           */
/*****************************************************************************/
void dump_output(vid_dec_ctx_t *ps_app_ctx,
                 iv_yuv_buf_t *ps_disp_frm_buf,
                 UWORD32 u4_disp_frm_id,
                 FILE *ps_op_file,
                 FILE *ps_op_chksum_file,
                 WORD32 i4_op_frm_ts,
                 UWORD32 file_save,
                 UWORD32 chksum_save)

{

    UWORD32 i;
    iv_yuv_buf_t s_dump_disp_frm_buf;
    UWORD32 u4_disp_id;

    memset(&s_dump_disp_frm_buf, 0, sizeof(iv_yuv_buf_t));

    if(ps_app_ctx->u4_share_disp_buf)
    {
        if(ps_app_ctx->dump_q_wr_idx == MAX_DISP_BUFFERS)
            ps_app_ctx->dump_q_wr_idx = 0;

        if(ps_app_ctx->dump_q_rd_idx == MAX_DISP_BUFFERS)
            ps_app_ctx->dump_q_rd_idx = 0;

        ps_app_ctx->s_disp_frm_queue[ps_app_ctx->dump_q_wr_idx] =
                        *ps_disp_frm_buf;
        ps_app_ctx->s_disp_frm_id_queue[ps_app_ctx->dump_q_wr_idx] =
                        u4_disp_frm_id;
        ps_app_ctx->dump_q_wr_idx++;

        if((WORD32)i4_op_frm_ts >= (WORD32)(ps_app_ctx->disp_delay - 1))
        {
            s_dump_disp_frm_buf =
                            ps_app_ctx->s_disp_frm_queue[ps_app_ctx->dump_q_rd_idx];
            u4_disp_id =
                            ps_app_ctx->s_disp_frm_id_queue[ps_app_ctx->dump_q_rd_idx];
            ps_app_ctx->dump_q_rd_idx++;
        }
        else
        {
            return;
        }
    }
    else
    {
        s_dump_disp_frm_buf = *ps_disp_frm_buf;
        u4_disp_id = u4_disp_frm_id;
    }

    release_disp_frame(ps_app_ctx->cocodec_obj, u4_disp_id);

    if(0 == file_save && 0 == chksum_save)
        return;

    if(NULL == s_dump_disp_frm_buf.pv_y_buf)
        return;

    if(ps_app_ctx->e_output_chroma_format == IV_YUV_420P)
    {
#if DUMP_SINGLE_BUF
        {
            UWORD8 *buf = s_dump_disp_frm_buf.pv_y_buf - 80 - (s_dump_disp_frm_buf.u4_y_strd * 80);

            UWORD32 i4_size = s_dump_disp_frm_buf.u4_y_strd * ((s_dump_disp_frm_buf.u4_y_ht + 160) + (s_dump_disp_frm_buf.u4_u_ht + 80));
            fwrite(buf, 1, i4_size ,ps_op_file);

        }
#else
        if(0 != file_save)
        {
            UWORD8 *buf;


            buf = (UWORD8 *)s_dump_disp_frm_buf.pv_y_buf;
            for(i = 0; i < s_dump_disp_frm_buf.u4_y_ht; i++)
            {
                fwrite(buf, 1, s_dump_disp_frm_buf.u4_y_wd, ps_op_file);
                buf += s_dump_disp_frm_buf.u4_y_strd;
            }

            buf = (UWORD8 *)s_dump_disp_frm_buf.pv_u_buf;
            for(i = 0; i < s_dump_disp_frm_buf.u4_u_ht; i++)
            {
                fwrite(buf, 1, s_dump_disp_frm_buf.u4_u_wd, ps_op_file);
                buf += s_dump_disp_frm_buf.u4_u_strd;
            }
            buf = (UWORD8 *)s_dump_disp_frm_buf.pv_v_buf;
            for(i = 0; i < s_dump_disp_frm_buf.u4_v_ht; i++)
            {
                fwrite(buf, 1, s_dump_disp_frm_buf.u4_v_wd, ps_op_file);
                buf += s_dump_disp_frm_buf.u4_v_strd;
            }

        }

        if(0 != chksum_save)
        {
            UWORD8 au1_y_chksum[16];
            UWORD8 au1_u_chksum[16];
            UWORD8 au1_v_chksum[16];
            calc_md5_cksum((UWORD8 *)s_dump_disp_frm_buf.pv_y_buf,
                           s_dump_disp_frm_buf.u4_y_strd,
                           s_dump_disp_frm_buf.u4_y_wd,
                           s_dump_disp_frm_buf.u4_y_ht,
                           au1_y_chksum);
            calc_md5_cksum((UWORD8 *)s_dump_disp_frm_buf.pv_u_buf,
                           s_dump_disp_frm_buf.u4_u_strd,
                           s_dump_disp_frm_buf.u4_u_wd,
                           s_dump_disp_frm_buf.u4_u_ht,
                           au1_u_chksum);
            calc_md5_cksum((UWORD8 *)s_dump_disp_frm_buf.pv_v_buf,
                           s_dump_disp_frm_buf.u4_v_strd,
                           s_dump_disp_frm_buf.u4_v_wd,
                           s_dump_disp_frm_buf.u4_v_ht,
                           au1_v_chksum);

            fwrite(au1_y_chksum, sizeof(UWORD8), 16, ps_op_chksum_file);
            fwrite(au1_u_chksum, sizeof(UWORD8), 16, ps_op_chksum_file);
            fwrite(au1_v_chksum, sizeof(UWORD8), 16, ps_op_chksum_file);
        }
#endif
    }
    else if((ps_app_ctx->e_output_chroma_format == IV_YUV_420SP_UV)
                    || (ps_app_ctx->e_output_chroma_format == IV_YUV_420SP_VU))
    {
#if DUMP_SINGLE_BUF
        {

            UWORD8 *buf = s_dump_disp_frm_buf.pv_y_buf - 24 - (s_dump_disp_frm_buf.u4_y_strd * 40);

            UWORD32 i4_size = s_dump_disp_frm_buf.u4_y_strd * ((s_dump_disp_frm_buf.u4_y_ht + 80) + (s_dump_disp_frm_buf.u4_u_ht + 40));
            fwrite(buf, 1, i4_size ,ps_op_file);
        }
#else
        {
            UWORD8 *buf;

            buf = (UWORD8 *)s_dump_disp_frm_buf.pv_y_buf;
            for(i = 0; i < s_dump_disp_frm_buf.u4_y_ht; i++)
            {
                fwrite(buf, 1, s_dump_disp_frm_buf.u4_y_wd, ps_op_file);
                buf += s_dump_disp_frm_buf.u4_y_strd;
            }

            buf = (UWORD8 *)s_dump_disp_frm_buf.pv_u_buf;
            for(i = 0; i < s_dump_disp_frm_buf.u4_u_ht; i++)
            {
                fwrite(buf, 1, s_dump_disp_frm_buf.u4_u_wd, ps_op_file);
                buf += s_dump_disp_frm_buf.u4_u_strd;
            }
        }
#endif
    }
    else if(ps_app_ctx->e_output_chroma_format == IV_RGBA_8888)
    {
        UWORD8 *buf;

        buf = (UWORD8 *)s_dump_disp_frm_buf.pv_y_buf;
        for(i = 0; i < s_dump_disp_frm_buf.u4_y_ht; i++)
        {
            fwrite(buf, 1, s_dump_disp_frm_buf.u4_y_wd * 4, ps_op_file);
            buf += s_dump_disp_frm_buf.u4_y_strd * 4;
        }
    }
    else
    {
        UWORD8 *buf;

        buf = (UWORD8 *)s_dump_disp_frm_buf.pv_y_buf;
        for(i = 0; i < s_dump_disp_frm_buf.u4_y_ht; i++)
        {
            fwrite(buf, 1, s_dump_disp_frm_buf.u4_y_strd * 2, ps_op_file);
            buf += s_dump_disp_frm_buf.u4_y_strd * 2;
        }
    }

    fflush(ps_op_file);
    fflush(ps_op_chksum_file);

}


/*****************************************************************************/
/*                                                                           */
/*  Function Name : print_usage                                              */
/*                                                                           */
/*  Description   : Prints argument format                                   */
/*                                                                           */
/*                                                                           */
/*  Inputs        :                                                          */
/*  Globals       :                                                          */
/*  Processing    : Prints argument format                                   */
/*                                                                           */
/*  Outputs       :                                                          */
/*  Returns       :                                                          */
/*                                                                           */
/*  Issues        :                                                          */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes                              */
/*         07 09 2012   100189          Initial Version                      */
/*                                                                           */
/*****************************************************************************/

void print_usage(void)
{
    WORD32 i = 0;
    WORD32 num_entries = sizeof(argument_mapping) / sizeof(argument_t);
    printf("\nUsage:\n");
    while(i < num_entries)
    {
        printf("%-32s\t %s", argument_mapping[i].argument_name,
               argument_mapping[i].description);
        i++;
    }
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : get_argument                                             */
/*                                                                           */
/*  Description   : Gets argument for a given string                         */
/*                                                                           */
/*                                                                           */
/*  Inputs        : name                                                     */
/*  Globals       :                                                          */
/*  Processing    : Searches the given string in the array and returns       */
/*                  appropriate argument ID                                  */
/*                                                                           */
/*  Outputs       : Argument ID                                              */
/*  Returns       : Argument ID                                              */
/*                                                                           */
/*  Issues        :                                                          */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes                              */
/*         07 09 2012   100189          Initial Version                      */
/*                                                                           */
/*****************************************************************************/

ARGUMENT_T get_argument(CHAR *name)
{
    WORD32 i = 0;
    WORD32 num_entries = sizeof(argument_mapping) / sizeof(argument_t);
    while(i < num_entries)
    {
        if((0 == strcmp(argument_mapping[i].argument_name, name))       ||
          ((0 == strcmp(argument_mapping[i].argument_shortname, name))  &&
           (0 != strcmp(argument_mapping[i].argument_shortname, "--"))))
        {
            return argument_mapping[i].argument;
        }
        i++;
    }
    return INVALID;
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : get_argument                                             */
/*                                                                           */
/*  Description   : Gets argument for a given string                         */
/*                                                                           */
/*                                                                           */
/*  Inputs        : name                                                     */
/*  Globals       :                                                          */
/*  Processing    : Searches the given string in the array and returns       */
/*                  appropriate argument ID                                  */
/*                                                                           */
/*  Outputs       : Argument ID                                              */
/*  Returns       : Argument ID                                              */
/*                                                                           */
/*  Issues        :                                                          */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes                              */
/*         07 09 2012   100189          Initial Version                      */
/*                                                                           */
/*****************************************************************************/

void parse_argument(vid_dec_ctx_t *ps_app_ctx, CHAR *argument, CHAR *value)
{
    ARGUMENT_T arg;

    arg = get_argument(argument);
    switch(arg)
    {
        case HELP:
            print_usage();
            exit(-1);
        case VERSION:
            break;
        case INPUT_FILE:
            sscanf(value, "%s", ps_app_ctx->ac_ip_fname);
            //input_passed = 1;
            break;

        case OUTPUT:
            sscanf(value, "%s", ps_app_ctx->ac_op_fname);
            break;

        case CHKSUM:
            sscanf(value, "%s", ps_app_ctx->ac_op_chksum_fname);
            break;

        case SAVE_OUTPUT:
            sscanf(value, "%d", &ps_app_ctx->u4_file_save_flag);
            break;

        case SAVE_CHKSUM:
            sscanf(value, "%d", &ps_app_ctx->u4_chksum_save_flag);
            break;

        case CHROMA_FORMAT:
            if((strcmp(value, "YUV_420P")) == 0)
                ps_app_ctx->e_output_chroma_format = IV_YUV_420P;
            else if((strcmp(value, "YUV_422ILE")) == 0)
                ps_app_ctx->e_output_chroma_format = IV_YUV_422ILE;
            else if((strcmp(value, "RGB_565")) == 0)
                ps_app_ctx->e_output_chroma_format = IV_RGB_565;
            else if((strcmp(value, "RGBA_8888")) == 0)
                ps_app_ctx->e_output_chroma_format = IV_RGBA_8888;
            else if((strcmp(value, "YUV_420SP_UV")) == 0)
                ps_app_ctx->e_output_chroma_format = IV_YUV_420SP_UV;
            else if((strcmp(value, "YUV_420SP_VU")) == 0)
                ps_app_ctx->e_output_chroma_format = IV_YUV_420SP_VU;
            else
            {
                printf("\nInvalid colour format setting it to IV_YUV_420P\n");
                ps_app_ctx->e_output_chroma_format = IV_YUV_420P;
            }

            break;
        case NUM_FRAMES:
            sscanf(value, "%d", &ps_app_ctx->u4_max_frm_ts);
            break;

        case NUM_CORES:
            sscanf(value, "%d", &ps_app_ctx->u4_num_cores);
            break;
        case DEGRADE_PICS:
            sscanf(value, "%d", &ps_app_ctx->i4_degrade_pics);
            break;
        case DEGRADE_TYPE:
            sscanf(value, "%d", &ps_app_ctx->i4_degrade_type);
            break;
        case SHARE_DISPLAY_BUF:
            sscanf(value, "%d", &ps_app_ctx->u4_share_disp_buf);
            break;
        case LOOPBACK:
            sscanf(value, "%d", &ps_app_ctx->loopback);
            break;
        case DISPLAY:
#if defined(SDL_DISPLAY) || defined(FBDEV_DISPLAY) || defined(INTEL_CE5300) || defined(IOS_DISPLAY)
            sscanf(value, "%d", &ps_app_ctx->display);
#else
            ps_app_ctx->display = 0;
#endif
            break;
        case FULLSCREEN:
            sscanf(value, "%d", &ps_app_ctx->full_screen);
            break;
        case FPS:
            sscanf(value, "%d", &ps_app_ctx->fps);
            if(ps_app_ctx->fps <= 0)
                ps_app_ctx->fps = DEFAULT_FPS;
            break;
        case ARCH:
            if((strcmp(value, "ARM_NONEON")) == 0)
                ps_app_ctx->e_arch = ARCH_ARM_NONEON;
            else if((strcmp(value, "ARM_A9Q")) == 0)
                ps_app_ctx->e_arch = ARCH_ARM_A9Q;
            else if((strcmp(value, "ARM_A7")) == 0)
                ps_app_ctx->e_arch = ARCH_ARM_A7;
            else if((strcmp(value, "ARM_A5")) == 0)
                ps_app_ctx->e_arch = ARCH_ARM_A5;
            else if((strcmp(value, "ARM_NEONINTR")) == 0)
                ps_app_ctx->e_arch = ARCH_ARM_NEONINTR;
            else if((strcmp(value, "X86_GENERIC")) == 0)
                ps_app_ctx->e_arch = ARCH_X86_GENERIC;
            else if((strcmp(value, "X86_SSSE3")) == 0)
                ps_app_ctx->e_arch = ARCH_X86_SSSE3;
            else if((strcmp(value, "X86_SSE42")) == 0)
                ps_app_ctx->e_arch = ARCH_X86_SSE42;
            else if((strcmp(value, "X86_AVX2")) == 0)
                ps_app_ctx->e_arch = ARCH_X86_AVX2;
            else if((strcmp(value, "MIPS_GENERIC")) == 0)
                ps_app_ctx->e_arch = ARCH_MIPS_GENERIC;
            else if((strcmp(value, "MIPS_32")) == 0)
                ps_app_ctx->e_arch = ARCH_MIPS_32;
            else if((strcmp(value, "ARMV8_GENERIC")) == 0)
                ps_app_ctx->e_arch = ARCH_ARMV8_GENERIC;
            else
            {
                printf("\nInvalid Arch. Setting it to ARM_A9Q\n");
                ps_app_ctx->e_arch = ARCH_ARM_A9Q;
            }

            break;
        case SOC:
            if((strcmp(value, "GENERIC")) == 0)
                ps_app_ctx->e_soc = SOC_GENERIC;
            else if((strcmp(value, "HISI_37X")) == 0)
                ps_app_ctx->e_soc = SOC_HISI_37X;
            else
            {
                ps_app_ctx->e_soc = atoi(value);
/*
                printf("\nInvalid SOC. Setting it to GENERIC\n");
                ps_app_ctx->e_soc = SOC_GENERIC;
*/
            }
            break;
        case PICLEN:
            sscanf(value, "%d", &ps_app_ctx->u4_piclen_flag);
            break;

        case PICLEN_FILE:
            sscanf(value, "%s", ps_app_ctx->ac_piclen_fname);
            break;
        case DISABLE_DEBLOCK_LEVEL:
            sscanf(value, "%d", &ps_app_ctx->u4_disable_dblk_level);
            break;

        case INVALID:
        default:
            printf("Ignoring argument :  %s\n", argument);
            break;
    }
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : read_cfg_file                                            */
/*                                                                           */
/*  Description   : Reads arguments from a configuration file                */
/*                                                                           */
/*                                                                           */
/*  Inputs        : ps_app_ctx  : Application context                        */
/*                  fp_cfg_file : Configuration file handle                  */
/*  Globals       :                                                          */
/*  Processing    : Parses the arguments and fills in the application context*/
/*                                                                           */
/*  Outputs       : Arguments parsed                                         */
/*  Returns       : None                                                     */
/*                                                                           */
/*  Issues        :                                                          */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes                              */
/*         07 09 2012   100189          Initial Version                      */
/*                                                                           */
/*****************************************************************************/

void read_cfg_file(vid_dec_ctx_t *ps_app_ctx, FILE *fp_cfg_file)
{

    CHAR line[STRLENGTH];
    CHAR description[STRLENGTH];
    CHAR value[STRLENGTH];
    CHAR argument[STRLENGTH];
    void *ret;
    while(0 == feof(fp_cfg_file))
    {
        line[0] = '\0';
        ret = fgets(line, STRLENGTH, fp_cfg_file);
        if(NULL == ret)
            break;
        argument[0] = '\0';
        /* Reading Input File Name */
        sscanf(line, "%s %s %s", argument, value, description);
        if(argument[0] == '\0')
            continue;

        parse_argument(ps_app_ctx, argument, value);
    }


}

/*!
**************************************************************************
* \if Function name : dispq_producer_dequeue \endif
*
* \brief
*    This function gets a free buffer index where display data can be written
*    This is a blocking call and can be exited by setting quit to true in
*    the application context
*
* \param[in]  ps_app_ctx  : Pointer to application context
*
* \return
*    returns Next free buffer index for producer
*
* \author
*  Ittiam
*
**************************************************************************
*/
WORD32 dispq_producer_dequeue(vid_dec_ctx_t *ps_app_ctx)
{
    WORD32 idx;

    /* If there is no free buffer wait */

    while(((ps_app_ctx->disp_q_wr_idx + 1) % NUM_DISPLAY_BUFFERS) == ps_app_ctx->disp_q_rd_idx)
    {

        ithread_msleep(1);

        if(ps_app_ctx->quit)
            return(-1);
    }

    idx = ps_app_ctx->disp_q_wr_idx;
    return (idx);
}

/*!
**************************************************************************
* \if Function name : dispq_producer_queue \endif
*
* \brief
*    This function adds buffer which can be displayed
*
* \param[in]  ps_app_ctx  : Pointer to application context
*
* \return
*    returns Next free buffer index for producer
*
* \author
*  Ittiam
*
**************************************************************************
*/
WORD32 dispq_producer_queue(vid_dec_ctx_t *ps_app_ctx)
{
    ps_app_ctx->disp_q_wr_idx++;
    if(ps_app_ctx->disp_q_wr_idx == NUM_DISPLAY_BUFFERS)
        ps_app_ctx->disp_q_wr_idx = 0;

    return (0);
}
/*!
**************************************************************************
* \if Function name : dispq_consumer_dequeue \endif
*
* \brief
*    This function gets a free buffer index where display data can be written
*    This is a blocking call and can be exited by setting quit to true in
*    the application context
*
* \param[in]  ps_app_ctx  : Pointer to application context
*
* \return
*    returns Next free buffer index for producer
*
* \author
*  Ittiam
*
**************************************************************************
*/
WORD32 dispq_consumer_dequeue(vid_dec_ctx_t *ps_app_ctx)
{
    WORD32 idx;

    /* If there is no free buffer wait */

    while(ps_app_ctx->disp_q_wr_idx == ps_app_ctx->disp_q_rd_idx)
    {

        ithread_msleep(1);

        if(ps_app_ctx->quit)
            return(-1);
    }

    idx = ps_app_ctx->disp_q_rd_idx;
    return (idx);
}

/*!
**************************************************************************
* \if Function name : dispq_producer_queue \endif
*
* \brief
*    This function adds buffer which can be displayed
*
* \param[in]  ps_app_ctx  : Pointer to application context
*
* \return
*    returns Next free buffer index for producer
*
* \author
*  Ittiam
*
**************************************************************************
*/
WORD32 dispq_consumer_queue(vid_dec_ctx_t *ps_app_ctx)
{
    ps_app_ctx->disp_q_rd_idx++;
    if(ps_app_ctx->disp_q_rd_idx == NUM_DISPLAY_BUFFERS)
        ps_app_ctx->disp_q_rd_idx = 0;

    return (0);
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : display_thread                                           */
/*                                                                           */
/*  Description   : Thread to display the frame                              */
/*                                                                           */
/*                                                                           */
/*  Inputs        : pv_ctx  : Application context                            */
/*                                                                           */
/*  Globals       :                                                          */
/*  Processing    : Wait for a buffer to get produced by decoder and display */
/*                  that frame                                               */
/*                                                                           */
/*  Outputs       :                                                          */
/*  Returns       : None                                                     */
/*                                                                           */
/*  Issues        : Pause followed by quit is making some deadlock condn     */
/*                  If decoder was lagging initially and then fasten up,     */
/*                  display will also go at faster rate till it reaches      */
/*                  equilibrium wrt the initial time                         */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes                              */
/*         07 05 2013   100578          Initial Version                      */
/*                                                                           */
/*****************************************************************************/

WORD32 display_thread(void *pv_ctx)
{
    vid_dec_ctx_t *ps_app_ctx = (vid_dec_ctx_t *) pv_ctx;


    UWORD32 frm_duration; /* in us */
    UWORD32 current_time;
    UWORD32 expected_time;
    TIMER   s_end_timer;
    TIMER   s_first_frame_time;
    UWORD32 first_frame_displayed;

#ifdef WINDOWS_TIMER
    TIMER frequency;
#endif

#ifdef WINDOWS_TIMER
        QueryPerformanceFrequency ( &frequency);
#endif
    first_frame_displayed = 0;
    expected_time = 0;
    frm_duration = 1000000/ps_app_ctx->fps;

    /* Init display and allocate display buffers */
    ps_app_ctx->pv_disp_ctx = (void *)ps_app_ctx->disp_init(ps_app_ctx->u4_pic_wd,
                                                            ps_app_ctx->u4_pic_ht,
                                                            ps_app_ctx->i4_screen_wd,
                                                            ps_app_ctx->i4_screen_ht,
                                                            ps_app_ctx->u4_pic_wd,
                                                            ps_app_ctx->u4_pic_ht,
                                                            ps_app_ctx->full_screen,
                                                            &ps_app_ctx->quit,
                                                            &ps_app_ctx->paused);
    ps_app_ctx->alloc_disp_buffers(ps_app_ctx->pv_disp_ctx);

    ps_app_ctx->display_init_done = 1;

    while(1)
    {
        WORD32 rd_idx;

        rd_idx = dispq_consumer_dequeue(ps_app_ctx);
        if (ps_app_ctx->quit)
                break;

        ps_app_ctx->display_buffer(ps_app_ctx->pv_disp_ctx, rd_idx);

        if(0 == first_frame_displayed)
        {
            GETTIME(&s_first_frame_time);
            first_frame_displayed = 1;
        }

        /*********************************************************************/
        /* Sleep based on the expected time of arrival of current buffer and */
        /* the Current frame                                                 */
        /*********************************************************************/

        GETTIME(&s_end_timer);
        ELAPSEDTIME(s_first_frame_time,s_end_timer,current_time,frequency);

        /* time in micro second */
        expected_time += frm_duration;

        //printf("current_time %d expected_time %d diff %d \n", current_time, expected_time, (expected_time - current_time));
        /* sleep for the diff. in time */
        if(current_time < expected_time)
            ps_app_ctx->disp_usleep((expected_time - current_time));
        else
            expected_time += (current_time - expected_time);

        dispq_consumer_queue(ps_app_ctx);

    }


    while(0 == ps_app_ctx->display_deinit_flag)
    {
        ps_app_ctx->disp_usleep(1000);
    }
    ps_app_ctx->disp_deinit(ps_app_ctx->pv_disp_ctx);

    return 0;
}

void output_write_stall(CHAR *fname, UWORD32 cur_frm_idx)
{
    const UWORD8 threshold = 64;
    CHAR past_fname[1000];
    FILE *fp_fast_file = NULL;

    if (cur_frm_idx >= threshold)
    {
        sprintf(past_fname, fname, cur_frm_idx - threshold);
        do
        {
            fp_fast_file = fopen(past_fname,"rb");
            if (fp_fast_file != NULL)
            {
                fclose(fp_fast_file);
                /* Wait until the resource is released by a third party app*/
                ithread_msleep(5);
            }
            else
                break;
        } while(1);
    }
}

void flush_output(iv_obj_t *codec_obj,
                  vid_dec_ctx_t *ps_app_ctx,
                  ivd_out_bufdesc_t *ps_out_buf,
                  UWORD8 *pu1_bs_buf,
                  UWORD32 *pu4_op_frm_ts,
                  FILE *ps_op_file,
                  FILE *ps_op_chksum_file,
                  UWORD32 u4_ip_frm_ts,
                  UWORD32 u4_bytes_remaining)
{
    WORD32 ret;

    do
    {

        ivd_ctl_flush_ip_t s_ctl_ip;
        ivd_ctl_flush_op_t s_ctl_op;

        if(*pu4_op_frm_ts >= (ps_app_ctx->u4_max_frm_ts + ps_app_ctx->disp_delay))
            break;

        s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
        s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_FLUSH;
        s_ctl_ip.u4_size = sizeof(ivd_ctl_flush_ip_t);
        s_ctl_op.u4_size = sizeof(ivd_ctl_flush_op_t);
        ret = ivd_api_function((iv_obj_t *)codec_obj, (void *)&s_ctl_ip,
                                   (void *)&s_ctl_op);

        if(ret != IV_SUCCESS)
        {
            printf("Error in Setting the decoder in flush mode\n");
        }

        if(IV_SUCCESS == ret)
        {
            ivd_video_decode_ip_t s_video_decode_ip;
            ivd_video_decode_op_t s_video_decode_op;

            s_video_decode_ip.e_cmd = IVD_CMD_VIDEO_DECODE;
            s_video_decode_ip.u4_ts = u4_ip_frm_ts;
            s_video_decode_ip.pv_stream_buffer = pu1_bs_buf;
            s_video_decode_ip.u4_num_Bytes = u4_bytes_remaining;
            s_video_decode_ip.u4_size = sizeof(ivd_video_decode_ip_t);
            s_video_decode_ip.s_out_buffer.u4_min_out_buf_size[0] =
                            ps_out_buf->u4_min_out_buf_size[0];
            s_video_decode_ip.s_out_buffer.u4_min_out_buf_size[1] =
                            ps_out_buf->u4_min_out_buf_size[1];
            s_video_decode_ip.s_out_buffer.u4_min_out_buf_size[2] =
                            ps_out_buf->u4_min_out_buf_size[2];

            s_video_decode_ip.s_out_buffer.pu1_bufs[0] =
                            ps_out_buf->pu1_bufs[0];
            s_video_decode_ip.s_out_buffer.pu1_bufs[1] =
                            ps_out_buf->pu1_bufs[1];
            s_video_decode_ip.s_out_buffer.pu1_bufs[2] =
                            ps_out_buf->pu1_bufs[2];
            s_video_decode_ip.s_out_buffer.u4_num_bufs =
                            ps_out_buf->u4_num_bufs;

            s_video_decode_op.u4_size = sizeof(ivd_video_decode_op_t);

            /*****************************************************************************/
            /*   API Call: Video Decode                                                  */
            /*****************************************************************************/
            ret = ivd_api_function((iv_obj_t *)codec_obj, (void *)&s_video_decode_ip,
                                       (void *)&s_video_decode_op);

            if(1 == s_video_decode_op.u4_output_present)
            {
                CHAR cur_fname[1000];
                CHAR *extn = NULL;
                /* The objective is to dump the decoded frames into separate files instead of
                 * dumping all the frames in one common file. Also, the number of dumped frames
                 * at any given instance of time cannot exceed 'frame_memory'
                 */
                if(ps_app_ctx->u4_file_save_flag)
                {
                    /* Locate the position of extension yuv */
                    extn = strstr(ps_app_ctx->ac_op_fname,"%d");
                    if (extn != NULL)
                    {
                        output_write_stall(ps_app_ctx->ac_op_fname,*pu4_op_frm_ts);
                        /* Generate output file names */
                        sprintf(cur_fname,ps_app_ctx->ac_op_fname,*pu4_op_frm_ts);
                        /* Open Output file */
                        ps_op_file = fopen(cur_fname,"wb");
                        if (NULL == ps_op_file)
                        {
                            CHAR ac_error_str[STRLENGTH];
                            sprintf(ac_error_str, "Could not open output file %s",
                                    cur_fname);

                            codec_exit(ac_error_str);
                        }
                    }
                }

                dump_output(ps_app_ctx, &(s_video_decode_op.s_disp_frm_buf),
                            s_video_decode_op.u4_disp_buf_id, ps_op_file,
                            ps_op_chksum_file,
                            *pu4_op_frm_ts, ps_app_ctx->u4_file_save_flag,
                            ps_app_ctx->u4_chksum_save_flag);
                if (extn != NULL)
                    fclose(ps_op_file);
                (*pu4_op_frm_ts)++;
            }
        }
    }
    while(IV_SUCCESS == ret);

}

#ifdef X86_MINGW
void sigsegv_handler()
{
    printf("Segmentation fault, Exiting.. \n");
    exit(-1);
}
#endif

UWORD32 default_get_stride(void)
{
    return 0;
}


IV_COLOR_FORMAT_T default_get_color_fmt(void)
{
    return IV_YUV_420P;
}
/*****************************************************************************/
/*                                                                           */
/*  Function Name : main                                                     */
/*                                                                           */
/*  Description   : Application to demonstrate codec API                     */
/*                                                                           */
/*                                                                           */
/*  Inputs        : argc    - Number of arguments                            */
/*                  argv[]  - Arguments                                      */
/*  Globals       :                                                          */
/*  Processing    : Shows how to use create, process, control and delete     */
/*                                                                           */
/*  Outputs       : Codec output in a file                                   */
/*  Returns       :                                                          */
/*                                                                           */
/*  Issues        : Assumes both PROFILE_ENABLE to be                        */
/*                  defined for multithread decode-display working           */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes                              */
/*         07 09 2012   100189          Initial Version                      */
/*         09 05 2013   100578          Multithread decode-display           */
/*****************************************************************************/
#ifdef IOS
int h264dec_main(char * homedir,char *documentdir, int screen_wd, int screen_ht)
#else
int main(WORD32 argc, CHAR *argv[])
#endif
{
    CHAR ac_cfg_fname[STRLENGTH];
    FILE *fp_cfg_file = NULL;
    FILE *ps_piclen_file = NULL;
    FILE *ps_ip_file = NULL;
    FILE *ps_op_file = NULL;
    FILE *ps_op_chksum_file = NULL;
    WORD32 ret;
    CHAR ac_error_str[STRLENGTH];
    vid_dec_ctx_t s_app_ctx;
    UWORD8 *pu1_bs_buf;

    ivd_out_bufdesc_t *ps_out_buf;
    UWORD32 u4_num_bytes_dec = 0;
    UWORD32 file_pos = 0;

    UWORD32 u4_ip_frm_ts = 0, u4_op_frm_ts = 0;

    WORD32 u4_bytes_remaining = 0;
    UWORD32 i;
    UWORD32 u4_ip_buf_len;
    UWORD32 frm_cnt = 0;
    WORD32 total_bytes_comsumed;
    UWORD32 max_op_frm_ts;
    UWORD32 u4_num_disp_bufs_with_dec;

#ifdef PROFILE_ENABLE
    UWORD32 u4_tot_cycles = 0;
    UWORD32 u4_tot_fmt_cycles = 0;
    UWORD32 peak_window[PEAK_WINDOW_SIZE];
    UWORD32 peak_window_idx = 0;
    UWORD32 peak_avg_max = 0;
#ifdef INTEL_CE5300
    UWORD32 time_consumed = 0;
    UWORD32 bytes_consumed = 0;
#endif
#endif

#ifdef WINDOWS_TIMER
    TIMER frequency;
#endif
    WORD32 width = 0, height = 0;
    iv_obj_t *codec_obj;
#if defined(GPU_BUILD) && !defined(X86)
//    int ioctl_init();
//    ioctl_init();
#endif

#ifdef X86_MINGW
    //For getting printfs without any delay
    setvbuf(stdout, NULL, _IONBF, 0);
    setvbuf(stderr, NULL, _IONBF, 0);
#endif
#ifdef IOS
        sprintf(filename_trace, "%s/iostrace.txt", homedir );
        printf("\ntrace file name = %s",filename_trace);
#endif

#ifdef X86_MINGW
    {
        signal(SIGSEGV, sigsegv_handler);
    }
#endif


#ifndef IOS
    /* Usage */
    if(argc < 2)
    {
        printf("Using test.cfg as configuration file \n");
        strcpy(ac_cfg_fname, "test.cfg");
    }
    else if(argc == 2)
    {
        strcpy(ac_cfg_fname, argv[1]);
    }

#else
    strcpy(ac_cfg_fname, "test.cfg");

#endif


    /***********************************************************************/
    /*                  Initialize Application parameters                  */
    /***********************************************************************/

    strcpy(s_app_ctx.ac_ip_fname, "\0");
    s_app_ctx.dump_q_wr_idx = 0;
    s_app_ctx.dump_q_rd_idx = 0;
    s_app_ctx.display_thread_created = 0;
    s_app_ctx.disp_q_wr_idx = 0;
    s_app_ctx.disp_q_rd_idx = 0;
    s_app_ctx.disp_delay = 0;
    s_app_ctx.loopback = 0;
    s_app_ctx.display = 0;
    s_app_ctx.full_screen = 0;
    s_app_ctx.u4_piclen_flag = 0;
    s_app_ctx.fps = DEFAULT_FPS;
    file_pos = 0;
    total_bytes_comsumed = 0;
    u4_ip_frm_ts = 0;
    u4_op_frm_ts = 0;
#ifdef PROFILE_ENABLE
    memset(peak_window, 0, sizeof(WORD32) * PEAK_WINDOW_SIZE);
#endif
    s_app_ctx.u4_share_disp_buf = DEFAULT_SHARE_DISPLAY_BUF;
    s_app_ctx.u4_num_cores = DEFAULT_NUM_CORES;
    s_app_ctx.i4_degrade_type = 0;
    s_app_ctx.i4_degrade_pics = 0;
    s_app_ctx.e_arch = ARCH_ARM_A9Q;
    s_app_ctx.e_soc = SOC_GENERIC;

    s_app_ctx.u4_strd = STRIDE;

    s_app_ctx.display_thread_handle           = malloc(ithread_get_handle_size());
    s_app_ctx.quit          = 0;
    s_app_ctx.paused        = 0;
    //s_app_ctx.u4_output_present = 0;

    s_app_ctx.get_stride = &default_get_stride;

    s_app_ctx.get_color_fmt = &default_get_color_fmt;

    /* Set function pointers for display */
#ifdef SDL_DISPLAY
    s_app_ctx.disp_init = &sdl_disp_init;
    s_app_ctx.alloc_disp_buffers = &sdl_alloc_disp_buffers;
    s_app_ctx.display_buffer = &sdl_display;
    s_app_ctx.set_disp_buffers = &sdl_set_disp_buffers;
    s_app_ctx.disp_deinit = &sdl_disp_deinit;
    s_app_ctx.disp_usleep = &sdl_disp_usleep;
    s_app_ctx.get_color_fmt = &sdl_get_color_fmt;
    s_app_ctx.get_stride = &sdl_get_stride;
#endif

#ifdef FBDEV_DISPLAY
    s_app_ctx.disp_init = &fbd_disp_init;
    s_app_ctx.alloc_disp_buffers = &fbd_alloc_disp_buffers;
    s_app_ctx.display_buffer = &fbd_display;
    s_app_ctx.set_disp_buffers = &fbd_set_disp_buffers;
    s_app_ctx.disp_deinit = &fbd_disp_deinit;
    s_app_ctx.disp_usleep = &fbd_disp_usleep;
    s_app_ctx.get_color_fmt = &fbd_get_color_fmt;
    s_app_ctx.get_stride = &fbd_get_stride;
#endif

#ifdef INTEL_CE5300
    s_app_ctx.disp_init = &gdl_disp_init;
    s_app_ctx.alloc_disp_buffers = &gdl_alloc_disp_buffers;
    s_app_ctx.display_buffer = &gdl_display;
    s_app_ctx.set_disp_buffers = &gdl_set_disp_buffers;
    s_app_ctx.disp_deinit = &gdl_disp_deinit;
    s_app_ctx.disp_usleep = &gdl_disp_usleep;
    s_app_ctx.get_color_fmt = &gdl_get_color_fmt;
    s_app_ctx.get_stride = &gdl_get_stride;
#endif

#ifdef IOS_DISPLAY
    s_app_ctx.disp_init = &ios_disp_init;
    s_app_ctx.alloc_disp_buffers = &ios_alloc_disp_buffers;
    s_app_ctx.display_buffer = &ios_display;
    s_app_ctx.set_disp_buffers = &ios_set_disp_buffers;
    s_app_ctx.disp_deinit = &ios_disp_deinit;
    s_app_ctx.disp_usleep = &ios_disp_usleep;
    s_app_ctx.get_color_fmt = &ios_get_color_fmt;
    s_app_ctx.get_stride = &ios_get_stride;
#endif

    s_app_ctx.display_deinit_flag = 0;
    s_app_ctx.e_output_chroma_format = IV_YUV_420SP_UV;
    /*************************************************************************/
    /* Parse arguments                                                       */
    /*************************************************************************/

#ifndef IOS
    /* Read command line arguments */
    if(argc > 2)
    {
        for(i = 1; i < (UWORD32)argc; i += 2)
        {
            if(CONFIG == get_argument(argv[i]))
            {
                strcpy(ac_cfg_fname, argv[i + 1]);
                if((fp_cfg_file = fopen(ac_cfg_fname, "r")) == NULL)
                {
                    sprintf(ac_error_str, "Could not open Configuration file %s",
                            ac_cfg_fname);
                    codec_exit(ac_error_str);
                }
                read_cfg_file(&s_app_ctx, fp_cfg_file);
                fclose(fp_cfg_file);
            }
            else
            {
                parse_argument(&s_app_ctx, argv[i], argv[i + 1]);
            }
        }
    }
    else
    {
        if((fp_cfg_file = fopen(ac_cfg_fname, "r")) == NULL)
        {
            sprintf(ac_error_str, "Could not open Configuration file %s",
                    ac_cfg_fname);
            codec_exit(ac_error_str);
        }
        read_cfg_file(&s_app_ctx, fp_cfg_file);
        fclose(fp_cfg_file);
    }
#else
    sprintf(filename_with_path, "%s/%s", homedir, ac_cfg_fname);
    if((fp_cfg_file = fopen(filename_with_path, "r")) == NULL)
    {
        sprintf(ac_error_str, "Could not open Configuration file %s",
                ac_cfg_fname);
        codec_exit(ac_error_str);

    }
    read_cfg_file(&s_app_ctx, fp_cfg_file);
    fclose(fp_cfg_file);

#endif
#ifdef PRINT_PICSIZE
    /* If the binary is used for only getting number of bytes in each picture, then disable the following features */
    s_app_ctx.u4_piclen_flag = 0;
    s_app_ctx.u4_file_save_flag = 0;
    s_app_ctx.u4_chksum_save_flag = 0;
    s_app_ctx.i4_degrade_pics = 0;
    s_app_ctx.i4_degrade_type = 0;
    s_app_ctx.loopback = 0;
    s_app_ctx.u4_share_disp_buf = 0;
    s_app_ctx.display = 0;
#endif

    /* If display is enabled, then turn off shared mode and get color format that is supported by display */
    if(1 == s_app_ctx.display)
    {
        s_app_ctx.u4_share_disp_buf = 0;
        s_app_ctx.e_output_chroma_format = s_app_ctx.get_color_fmt();
    }
    if(strcmp(s_app_ctx.ac_ip_fname, "\0") == 0)
    {
        printf("\nNo input file given for decoding\n");
        exit(-1);
    }



    /***********************************************************************/
    /*          create the file object for input file                      */
    /***********************************************************************/
#ifdef IOS
  sprintf(filename_with_path, "%s/%s", homedir, s_app_ctx.ac_ip_fname);
   ps_ip_file = fopen(filename_with_path, "rb");
#else
    ps_ip_file = fopen(s_app_ctx.ac_ip_fname, "rb");
#endif
    if(NULL == ps_ip_file)
    {
        sprintf(ac_error_str, "Could not open input file %s",
                s_app_ctx.ac_ip_fname);
        codec_exit(ac_error_str);
    }
    /***********************************************************************/
    /*          create the file object for input file                      */
    /***********************************************************************/
    if(1 == s_app_ctx.u4_piclen_flag)
    {
#ifdef IOS
        sprintf(filename_with_path, "%s/%s", homedir, s_app_ctx.ac_piclen_fname);
        ps_piclen_file = fopen(filename_with_path, "rb");
#else
        ps_piclen_file = fopen(s_app_ctx.ac_piclen_fname, "rb");
#endif
        if(NULL == ps_piclen_file)
        {
            sprintf(ac_error_str, "Could not open piclen file %s",
                s_app_ctx.ac_piclen_fname);
            codec_exit(ac_error_str);
        }
    }

    /***********************************************************************/
    /*          create the file object for output file                     */
    /***********************************************************************/

    /* If the filename does not contain %d, then output will be dumped to
       a single file and it is opened here */
    if((1 == s_app_ctx.u4_file_save_flag) && (strstr(s_app_ctx.ac_op_fname,"%d") == NULL))
    {
#ifdef IOS
    sprintf(filename_with_path, "%s/%s", documentdir, s_app_ctx.ac_op_fname);
    ps_op_file = fopen(filename_with_path,"wb");
#else
        ps_op_file = fopen(s_app_ctx.ac_op_fname, "wb");
#endif

        if(NULL == ps_op_file)
        {
            sprintf(ac_error_str, "Could not open output file %s",
                    s_app_ctx.ac_op_fname);
            codec_exit(ac_error_str);
        }
    }

    /***********************************************************************/
    /*          create the file object for check sum file                  */
    /***********************************************************************/
    if(1 == s_app_ctx.u4_chksum_save_flag)
    {
#if IOS
        sprintf(filename_with_path, "%s/%s", documentdir, s_app_ctx.ac_op_chksum_fname);
        ps_op_chksum_file = fopen(filename_with_path,"wb");
#else
        ps_op_chksum_file = fopen(s_app_ctx.ac_op_chksum_fname, "wb");
#endif
        if(NULL == ps_op_chksum_file)
        {
            sprintf(ac_error_str, "Could not open check sum file %s",
                    s_app_ctx.ac_op_chksum_fname);
            codec_exit(ac_error_str);
        }
    }
    /***********************************************************************/
    /*                      Create decoder instance                        */
    /***********************************************************************/
    {

        ps_out_buf = (ivd_out_bufdesc_t *)malloc(sizeof(ivd_out_bufdesc_t));

        /*****************************************************************************/
        /*   API Call: Initialize the Decoder                                        */
        /*****************************************************************************/
        {
            ih264d_create_ip_t s_create_ip;
            ih264d_create_op_t s_create_op;
            void *fxns = &ivd_api_function;

            s_create_ip.s_ivd_create_ip_t.e_cmd = IVD_CMD_CREATE;
            s_create_ip.s_ivd_create_ip_t.u4_share_disp_buf = s_app_ctx.u4_share_disp_buf;
            s_create_ip.s_ivd_create_ip_t.e_output_format = (IV_COLOR_FORMAT_T)s_app_ctx.e_output_chroma_format;
            s_create_ip.s_ivd_create_ip_t.pf_aligned_alloc = ih264a_aligned_malloc;
            s_create_ip.s_ivd_create_ip_t.pf_aligned_free = ih264a_aligned_free;
            s_create_ip.s_ivd_create_ip_t.pv_mem_ctxt = NULL;
            s_create_ip.s_ivd_create_ip_t.u4_size = sizeof(ih264d_create_ip_t);
            s_create_op.s_ivd_create_op_t.u4_size = sizeof(ih264d_create_op_t);



            ret = ivd_api_function(NULL, (void *)&s_create_ip,
                                       (void *)&s_create_op);
            if(ret != IV_SUCCESS)
            {
                sprintf(ac_error_str, "Error in Create %8x\n",
                        s_create_op.s_ivd_create_op_t.u4_error_code);
                codec_exit(ac_error_str);
            }
            codec_obj = (iv_obj_t*)s_create_op.s_ivd_create_op_t.pv_handle;
            codec_obj->pv_fxns = fxns;
            codec_obj->u4_size = sizeof(iv_obj_t);
            s_app_ctx.cocodec_obj = codec_obj;

            /*****************************************************************************/
            /*  set stride                                                               */
            /*****************************************************************************/
            {
                ivd_ctl_set_config_ip_t s_ctl_ip;
                ivd_ctl_set_config_op_t s_ctl_op;


                s_ctl_ip.u4_disp_wd = STRIDE;
                if(1 == s_app_ctx.display)
                    s_ctl_ip.u4_disp_wd = s_app_ctx.get_stride();

                s_ctl_ip.e_frm_skip_mode = IVD_SKIP_NONE;
                s_ctl_ip.e_frm_out_mode = IVD_DISPLAY_FRAME_OUT;
                s_ctl_ip.e_vid_dec_mode = IVD_DECODE_HEADER;
                s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
                s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_SETPARAMS;
                s_ctl_ip.u4_size = sizeof(ivd_ctl_set_config_ip_t);
                s_ctl_op.u4_size = sizeof(ivd_ctl_set_config_op_t);

                ret = ivd_api_function((iv_obj_t*)codec_obj, (void *)&s_ctl_ip,
                                           (void *)&s_ctl_op);
                if(ret != IV_SUCCESS)
                {
                    sprintf(ac_error_str,
                            "\nError in setting the stride");
                    codec_exit(ac_error_str);
                }
            }



        }

    }


    /*************************************************************************/
    /* set num of cores                                                      */
    /*************************************************************************/
    {

        ih264d_ctl_set_num_cores_ip_t s_ctl_set_cores_ip;
        ih264d_ctl_set_num_cores_op_t s_ctl_set_cores_op;

        s_ctl_set_cores_ip.e_cmd = IVD_CMD_VIDEO_CTL;
        s_ctl_set_cores_ip.e_sub_cmd =(IVD_CONTROL_API_COMMAND_TYPE_T) IH264D_CMD_CTL_SET_NUM_CORES;
        s_ctl_set_cores_ip.u4_num_cores = s_app_ctx.u4_num_cores;
        s_ctl_set_cores_ip.u4_size = sizeof(ih264d_ctl_set_num_cores_ip_t);
        s_ctl_set_cores_op.u4_size = sizeof(ih264d_ctl_set_num_cores_op_t);

        ret = ivd_api_function((iv_obj_t*)codec_obj, (void *)&s_ctl_set_cores_ip,
                                   (void *)&s_ctl_set_cores_op);
        if(ret != IV_SUCCESS)
        {
            sprintf(ac_error_str, "\nError in setting number of cores");
            codec_exit(ac_error_str);
        }

    }

    /*************************************************************************/
    /* set processsor                                                        */
    /*************************************************************************/
    {

        ih264d_ctl_set_processor_ip_t s_ctl_set_num_processor_ip;
        ih264d_ctl_set_processor_op_t s_ctl_set_num_processor_op;

        s_ctl_set_num_processor_ip.e_cmd = IVD_CMD_VIDEO_CTL;
        s_ctl_set_num_processor_ip.e_sub_cmd =(IVD_CONTROL_API_COMMAND_TYPE_T) IH264D_CMD_CTL_SET_PROCESSOR;
        s_ctl_set_num_processor_ip.u4_arch = s_app_ctx.e_arch;
        s_ctl_set_num_processor_ip.u4_soc = s_app_ctx.e_soc;
        s_ctl_set_num_processor_ip.u4_size = sizeof(ih264d_ctl_set_processor_ip_t);
        s_ctl_set_num_processor_op.u4_size = sizeof(ih264d_ctl_set_processor_op_t);

        ret = ivd_api_function((iv_obj_t*)codec_obj, (void *)&s_ctl_set_num_processor_ip,
                                   (void *)&s_ctl_set_num_processor_op);
        if(ret != IV_SUCCESS)
        {
            sprintf(ac_error_str, "\nError in setting Processor type");
            codec_exit(ac_error_str);
        }

    }


    /*****************************************************************************/
    /*   Decode header to get width and height and buffer sizes                  */
    /*****************************************************************************/
    {
        ivd_video_decode_ip_t s_video_decode_ip;
        ivd_video_decode_op_t s_video_decode_op;



        {
            ivd_ctl_set_config_ip_t s_ctl_ip;
            ivd_ctl_set_config_op_t s_ctl_op;


            s_ctl_ip.u4_disp_wd = STRIDE;
            if(1 == s_app_ctx.display)
                s_ctl_ip.u4_disp_wd = s_app_ctx.get_stride();

            s_ctl_ip.e_frm_skip_mode = IVD_SKIP_NONE;
            s_ctl_ip.e_frm_out_mode = IVD_DISPLAY_FRAME_OUT;
            s_ctl_ip.e_vid_dec_mode = IVD_DECODE_HEADER;
            s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
            s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_SETPARAMS;
            s_ctl_ip.u4_size = sizeof(ivd_ctl_set_config_ip_t);
            s_ctl_op.u4_size = sizeof(ivd_ctl_set_config_op_t);

            ret = ivd_api_function((iv_obj_t*)codec_obj, (void *)&s_ctl_ip,
                                       (void *)&s_ctl_op);
            if(ret != IV_SUCCESS)
            {
                sprintf(ac_error_str,
                        "\nError in setting the codec in header decode mode");
                codec_exit(ac_error_str);
            }
        }

        /* Allocate input buffer for header */
        u4_ip_buf_len = 256 * 1024;
        pu1_bs_buf = (UWORD8 *)malloc(u4_ip_buf_len);

        if(pu1_bs_buf == NULL)
        {
            sprintf(ac_error_str,
                    "\nAllocation failure for input buffer of i4_size %d",
                    u4_ip_buf_len);
            codec_exit(ac_error_str);
        }

        do
        {
            WORD32 numbytes;

            if(0 == s_app_ctx.u4_piclen_flag)
            {
                fseek(ps_ip_file, file_pos, SEEK_SET);
                numbytes = u4_ip_buf_len;
            }
            else
            {
                WORD32 entries;
                entries = fscanf(ps_piclen_file, "%d\n", &numbytes);
                if(1 != entries)
                    numbytes = u4_ip_buf_len;
            }

            u4_bytes_remaining = fread(pu1_bs_buf, sizeof(UWORD8), numbytes,
                                       ps_ip_file);

            if(0 == u4_bytes_remaining)
            {
                sprintf(ac_error_str, "\nUnable to read from input file");
                codec_exit(ac_error_str);
            }

            s_video_decode_ip.e_cmd = IVD_CMD_VIDEO_DECODE;
            s_video_decode_ip.u4_ts = u4_ip_frm_ts;
            s_video_decode_ip.pv_stream_buffer = pu1_bs_buf;
            s_video_decode_ip.u4_num_Bytes = u4_bytes_remaining;
            s_video_decode_ip.u4_size = sizeof(ivd_video_decode_ip_t);
            s_video_decode_op.u4_size = sizeof(ivd_video_decode_op_t);

            /*****************************************************************************/
            /*   API Call: Header Decode                                                  */
            /*****************************************************************************/
            ret = ivd_api_function((iv_obj_t *)codec_obj, (void *)&s_video_decode_ip,
                                       (void *)&s_video_decode_op);

            if(ret != IV_SUCCESS)
            {
                printf("Error in header decode 0x%x\n",  s_video_decode_op.u4_error_code);
                // codec_exit(ac_error_str);
            }

            u4_num_bytes_dec = s_video_decode_op.u4_num_bytes_consumed;
#ifndef PROFILE_ENABLE
            printf("%d\n",s_video_decode_op.u4_num_bytes_consumed);
#endif
            file_pos += u4_num_bytes_dec;
            total_bytes_comsumed += u4_num_bytes_dec;
        }while(ret != IV_SUCCESS);

        /* copy pic_wd and pic_ht to initialize buffers */
        s_app_ctx.u4_pic_wd = s_video_decode_op.u4_pic_wd;
        s_app_ctx.u4_pic_ht = s_video_decode_op.u4_pic_ht;

        /* Allocate input buffer */
        u4_ip_buf_len = 2048 * 2048;
        free(pu1_bs_buf);

#if IOS_DISPLAY
        s_app_ctx.i4_screen_wd = screen_wd;
        s_app_ctx.i4_screen_ht = screen_ht;
#endif
        {

            ivd_ctl_getbufinfo_ip_t s_ctl_ip;
            ivd_ctl_getbufinfo_op_t s_ctl_op;
            WORD32 outlen = 0;

            s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
            s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_GETBUFINFO;
            s_ctl_ip.u4_size = sizeof(ivd_ctl_getbufinfo_ip_t);
            s_ctl_op.u4_size = sizeof(ivd_ctl_getbufinfo_op_t);
            ret = ivd_api_function((iv_obj_t*)codec_obj, (void *)&s_ctl_ip,
                                       (void *)&s_ctl_op);
            if(ret != IV_SUCCESS)
            {
                sprintf(ac_error_str, "Error in Get Buf Info %x", s_ctl_op.u4_error_code);
                codec_exit(ac_error_str);
            }

            /* Allocate bitstream buffer */
            u4_ip_buf_len = s_ctl_op.u4_min_in_buf_size[0];
#ifdef ADAPTIVE_TEST
            u4_ip_buf_len = ADAPTIVE_MAX_WD * ADAPTIVE_MAX_HT * 3 >> 1;
#endif
            pu1_bs_buf = (UWORD8 *)malloc(u4_ip_buf_len);

            if(pu1_bs_buf == NULL)
            {
                sprintf(ac_error_str,
                        "\nAllocation failure for input buffer of i4_size %d",
                        u4_ip_buf_len);
                codec_exit(ac_error_str);
            }

            s_app_ctx.num_disp_buf = s_ctl_op.u4_num_disp_bufs;
            /* Allocate output buffer only if display buffers are not shared */
            /* Or if shared and output is 420P */
            if((0 == s_app_ctx.u4_share_disp_buf) || (IV_YUV_420P == s_app_ctx.e_output_chroma_format))
            {
#ifdef ADAPTIVE_TEST
                switch(s_app_ctx.e_output_chroma_format)
                {
                    case IV_YUV_420P:
                    {
                        s_ctl_op.u4_min_out_buf_size[0] = ADAPTIVE_MAX_WD * ADAPTIVE_MAX_HT;
                        s_ctl_op.u4_min_out_buf_size[1] = ADAPTIVE_MAX_WD * ADAPTIVE_MAX_HT >> 2;
                        s_ctl_op.u4_min_out_buf_size[2] = ADAPTIVE_MAX_WD * ADAPTIVE_MAX_HT >> 2;
                        break;
                    }
                    case IV_YUV_420SP_UV:
                    case IV_YUV_420SP_VU:
                    {
                        s_ctl_op.u4_min_out_buf_size[0] = ADAPTIVE_MAX_WD * ADAPTIVE_MAX_HT;
                        s_ctl_op.u4_min_out_buf_size[1] = ADAPTIVE_MAX_WD * ADAPTIVE_MAX_HT >> 1;
                        s_ctl_op.u4_min_out_buf_size[2] = 0;
                        break;
                    }
                    case IV_YUV_422ILE:
                    {
                        s_ctl_op.u4_min_out_buf_size[0] = ADAPTIVE_MAX_WD * ADAPTIVE_MAX_HT * 2;
                        s_ctl_op.u4_min_out_buf_size[1] = 0;
                        s_ctl_op.u4_min_out_buf_size[2] = 0;
                        break;
                    }
                    case IV_RGBA_8888:
                    {
                        s_ctl_op.u4_min_out_buf_size[0] = ADAPTIVE_MAX_WD * ADAPTIVE_MAX_HT * 4;
                        s_ctl_op.u4_min_out_buf_size[1] = 0;
                        s_ctl_op.u4_min_out_buf_size[2] = 0;
                        break;
                    }
                    case IV_RGB_565:
                    {
                        s_ctl_op.u4_min_out_buf_size[0] = ADAPTIVE_MAX_WD * ADAPTIVE_MAX_HT * 2;
                        s_ctl_op.u4_min_out_buf_size[1] = 0;
                        s_ctl_op.u4_min_out_buf_size[2] = 0;
                        break;
                    }
                    default:
                        break;

                }
#endif
                ps_out_buf->u4_min_out_buf_size[0] =
                                s_ctl_op.u4_min_out_buf_size[0];
                ps_out_buf->u4_min_out_buf_size[1] =
                                s_ctl_op.u4_min_out_buf_size[1];
                ps_out_buf->u4_min_out_buf_size[2] =
                                s_ctl_op.u4_min_out_buf_size[2];

                outlen = s_ctl_op.u4_min_out_buf_size[0];
                if(s_ctl_op.u4_min_num_out_bufs > 1)
                    outlen += s_ctl_op.u4_min_out_buf_size[1];

                if(s_ctl_op.u4_min_num_out_bufs > 2)
                    outlen += s_ctl_op.u4_min_out_buf_size[2];

                ps_out_buf->pu1_bufs[0] = (UWORD8 *)malloc(outlen);
                if(ps_out_buf->pu1_bufs[0] == NULL)
                {
                    sprintf(ac_error_str,
                            "\nAllocation failure for output buffer of i4_size %d",
                            outlen);
                    codec_exit(ac_error_str);
                }

                if(s_ctl_op.u4_min_num_out_bufs > 1)
                    ps_out_buf->pu1_bufs[1] = ps_out_buf->pu1_bufs[0]
                                    + (s_ctl_op.u4_min_out_buf_size[0]);

                if(s_ctl_op.u4_min_num_out_bufs > 2)
                    ps_out_buf->pu1_bufs[2] = ps_out_buf->pu1_bufs[1]
                                    + (s_ctl_op.u4_min_out_buf_size[1]);

                ps_out_buf->u4_num_bufs = s_ctl_op.u4_min_num_out_bufs;
            }

#ifdef APP_EXTRA_BUFS
            s_app_ctx.disp_delay = EXTRA_DISP_BUFFERS;
            s_ctl_op.u4_num_disp_bufs += EXTRA_DISP_BUFFERS;
#endif

            /*****************************************************************************/
            /*   API Call: Allocate display buffers for display buffer shared case       */
            /*****************************************************************************/

            for(i = 0; i < s_ctl_op.u4_num_disp_bufs; i++)
            {

                s_app_ctx.s_disp_buffers[i].u4_min_out_buf_size[0] =
                                s_ctl_op.u4_min_out_buf_size[0];
                s_app_ctx.s_disp_buffers[i].u4_min_out_buf_size[1] =
                                s_ctl_op.u4_min_out_buf_size[1];
                s_app_ctx.s_disp_buffers[i].u4_min_out_buf_size[2] =
                                s_ctl_op.u4_min_out_buf_size[2];

                outlen = s_ctl_op.u4_min_out_buf_size[0];
                if(s_ctl_op.u4_min_num_out_bufs > 1)
                    outlen += s_ctl_op.u4_min_out_buf_size[1];

                if(s_ctl_op.u4_min_num_out_bufs > 2)
                    outlen += s_ctl_op.u4_min_out_buf_size[2];

                s_app_ctx.s_disp_buffers[i].pu1_bufs[0] = (UWORD8 *)malloc(outlen);

                if(s_app_ctx.s_disp_buffers[i].pu1_bufs[0] == NULL)
                {
                    sprintf(ac_error_str,
                            "\nAllocation failure for output buffer of i4_size %d",
                            outlen);
                    codec_exit(ac_error_str);
                }

                if(s_ctl_op.u4_min_num_out_bufs > 1)
                    s_app_ctx.s_disp_buffers[i].pu1_bufs[1] =
                                    s_app_ctx.s_disp_buffers[i].pu1_bufs[0]
                                                    + (s_ctl_op.u4_min_out_buf_size[0]);

                if(s_ctl_op.u4_min_num_out_bufs > 2)
                    s_app_ctx.s_disp_buffers[i].pu1_bufs[2] =
                                    s_app_ctx.s_disp_buffers[i].pu1_bufs[1]
                                                    + (s_ctl_op.u4_min_out_buf_size[1]);

                s_app_ctx.s_disp_buffers[i].u4_num_bufs =
                                s_ctl_op.u4_min_num_out_bufs;
            }
            s_app_ctx.num_disp_buf = s_ctl_op.u4_num_disp_bufs;
        }

        /* Create display thread and wait for the display buffers to be initialized */
        if(1 == s_app_ctx.display)
        {
            if(0 == s_app_ctx.display_thread_created)
            {
                s_app_ctx.display_init_done = 0;
                ithread_create(s_app_ctx.display_thread_handle, NULL,
                                                    (void *) &display_thread, (void *) &s_app_ctx);
                s_app_ctx.display_thread_created = 1;

                while(1)
                {
                    if(s_app_ctx.display_init_done)
                        break;

                    ithread_msleep(1);
                }
            }

            s_app_ctx.u4_strd = s_app_ctx.get_stride();
        }
    }

    /*************************************************************************/
    /* Get actual number of output buffers requried, which is dependent      */
    /* on ps_bitstrm properties such as width, height and level etc              */
    /* This is needed mainly for shared display mode                         */
    /*************************************************************************/
    //if(1 == s_app_ctx.u4_share_disp_buf)
    {
        /*****************************************************************************/
        /*   API Call: Send the allocated display buffers to codec                   */
        /*****************************************************************************/
        {
            ivd_set_display_frame_ip_t s_set_display_frame_ip;
            ivd_set_display_frame_op_t s_set_display_frame_op;

            s_set_display_frame_ip.e_cmd = IVD_CMD_SET_DISPLAY_FRAME;
            s_set_display_frame_ip.u4_size = sizeof(ivd_set_display_frame_ip_t);
            s_set_display_frame_op.u4_size = sizeof(ivd_set_display_frame_op_t);

            s_set_display_frame_ip.num_disp_bufs = s_app_ctx.num_disp_buf;

            memcpy(&(s_set_display_frame_ip.s_disp_buffer),
                   &(s_app_ctx.s_disp_buffers),
                   s_app_ctx.num_disp_buf * sizeof(ivd_out_bufdesc_t));

            ret = ivd_api_function((iv_obj_t *)codec_obj,
                                       (void *)&s_set_display_frame_ip,
                                       (void *)&s_set_display_frame_op);

            if(IV_SUCCESS != ret)
            {
                sprintf(ac_error_str, "Error in Set display frame");
                codec_exit(ac_error_str);
            }

        }

    }

    /*************************************************************************/
    /* Get frame dimensions for display buffers such as x_offset,y_offset    */
    /* etc. This information might be needed to set display buffer           */
    /* offsets in case of shared display buffer mode                         */
    /*************************************************************************/
    {

        ih264d_ctl_get_frame_dimensions_ip_t s_ctl_get_frame_dimensions_ip;
        ih264d_ctl_get_frame_dimensions_op_t s_ctl_get_frame_dimensions_op;

        s_ctl_get_frame_dimensions_ip.e_cmd = IVD_CMD_VIDEO_CTL;
        s_ctl_get_frame_dimensions_ip.e_sub_cmd =
                        (IVD_CONTROL_API_COMMAND_TYPE_T)IH264D_CMD_CTL_GET_BUFFER_DIMENSIONS;
        s_ctl_get_frame_dimensions_ip.u4_size =
                        sizeof(ih264d_ctl_get_frame_dimensions_ip_t);
        s_ctl_get_frame_dimensions_op.u4_size =
                        sizeof(ih264d_ctl_get_frame_dimensions_op_t);

        ret = ivd_api_function((iv_obj_t *)codec_obj, (void *)&s_ctl_get_frame_dimensions_ip,
                             (void *)&s_ctl_get_frame_dimensions_op);
        if(IV_SUCCESS != ret)
        {
            sprintf(ac_error_str, "Error in Get buffer Dimensions");
            codec_exit(ac_error_str);
        }

/*
        printf("Frame offsets due to padding\n");
        printf("s_ctl_get_frame_dimensions_op.x_offset[0] %d s_ctl_get_frame_dimensions_op.y_offset[0] %d\n",
               s_ctl_get_frame_dimensions_op.u4_x_offset[0],
               s_ctl_get_frame_dimensions_op.u4_y_offset[0]);
*/
    }



    /*************************************************************************/
    /* Set the decoder in frame decode mode. It was set in header decode     */
    /* mode earlier                                                          */
    /*************************************************************************/
    {

        ivd_ctl_set_config_ip_t s_ctl_ip;
        ivd_ctl_set_config_op_t s_ctl_op;

        s_ctl_ip.u4_disp_wd = STRIDE;
        if(1 == s_app_ctx.display)
            s_ctl_ip.u4_disp_wd = s_app_ctx.get_stride();
        s_ctl_ip.e_frm_skip_mode = IVD_SKIP_NONE;

        s_ctl_ip.e_frm_out_mode = IVD_DISPLAY_FRAME_OUT;
        s_ctl_ip.e_vid_dec_mode = IVD_DECODE_FRAME;
        s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
        s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_SETPARAMS;
        s_ctl_ip.u4_size = sizeof(ivd_ctl_set_config_ip_t);

        s_ctl_op.u4_size = sizeof(ivd_ctl_set_config_op_t);

        ret = ivd_api_function((iv_obj_t *)codec_obj, (void *)&s_ctl_ip, (void *)&s_ctl_op);

        if(IV_SUCCESS != ret)
        {
            sprintf(ac_error_str, "Error in Set Parameters");
            //codec_exit(ac_error_str);
        }

    }
    /*************************************************************************/
    /* If required disable deblocking and sao at given level                 */
    /*************************************************************************/

    set_degrade(codec_obj, s_app_ctx.i4_degrade_type, s_app_ctx.i4_degrade_pics);
#ifdef WINDOWS_TIMER
        QueryPerformanceFrequency ( &frequency);
#endif
#ifndef PRINT_PICSIZE
    get_version(codec_obj);
#endif


    max_op_frm_ts = s_app_ctx.u4_max_frm_ts + s_app_ctx.disp_delay;

    if(max_op_frm_ts <  s_app_ctx.disp_delay)
        max_op_frm_ts = 0xffffffff;/* clip as overflow has occured*/

    max_op_frm_ts = (s_app_ctx.u4_max_frm_ts > 0)? (max_op_frm_ts): 0xffffffff;

    u4_num_disp_bufs_with_dec = 0;

    while(u4_op_frm_ts < max_op_frm_ts)
    {

#ifdef TEST_FLUSH
        if(u4_ip_frm_ts == FLUSH_FRM_CNT)
        {
            ivd_ctl_flush_ip_t s_ctl_ip;
            ivd_ctl_flush_op_t s_ctl_op;

            s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
            s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_FLUSH;
            s_ctl_ip.u4_size = sizeof(ivd_ctl_flush_ip_t);
            s_ctl_op.u4_size = sizeof(ivd_ctl_flush_op_t);
            ret = ivd_api_function((iv_obj_t *)codec_obj, (void *)&s_ctl_ip,
                                       (void *)&s_ctl_op);

            if(ret != IV_SUCCESS)
            {
                printf("Error in Setting the decoder in flush mode\n");
            }
//            file_pos = 0;

//          fseek(ps_ip_file, file_pos, SEEK_SET);

        }
#endif
        if(u4_num_disp_bufs_with_dec < s_app_ctx.num_disp_buf)
        {
            release_disp_frame(codec_obj, u4_num_disp_bufs_with_dec);
            u4_num_disp_bufs_with_dec ++;
        }


        /*************************************************************************/
        /* set num of cores                                                      */
        /*************************************************************************/
#ifdef DYNAMIC_NUMCORES
        {

            ih264d_ctl_set_num_cores_ip_t s_ctl_set_cores_ip;
            ih264d_ctl_set_num_cores_op_t s_ctl_set_cores_op;

            s_ctl_set_cores_ip.e_cmd = IVD_CMD_VIDEO_CTL;
            s_ctl_set_cores_ip.e_sub_cmd = IH264D_CMD_CTL_SET_NUM_CORES;
            s_ctl_set_cores_ip.u4_num_cores =  1 + 3 * (u4_ip_frm_ts % 2);
            s_ctl_set_cores_ip.u4_size = sizeof(ih264d_ctl_set_num_cores_ip_t);
            s_ctl_set_cores_op.u4_size = sizeof(ih264d_ctl_set_num_cores_op_t);

            ret = ivd_api_function((iv_obj_t *)codec_obj, (void *)&s_ctl_set_cores_ip,
                                       (void *)&s_ctl_set_cores_op);
            if(ret != IV_SUCCESS)
            {
                sprintf(ac_error_str, "\nError in setting number of cores");
                codec_exit(ac_error_str);
            }

        }
#endif
        /***********************************************************************/
        /*   Seek the file to start of current frame, this is equavelent of    */
        /*   having a parcer which tells the start of current frame            */
        /***********************************************************************/
        {
            WORD32 numbytes;

            if(0 == s_app_ctx.u4_piclen_flag)
            {
                fseek(ps_ip_file, file_pos, SEEK_SET);
                numbytes = u4_ip_buf_len;
            }
            else
            {
                WORD32 entries;
                entries = fscanf(ps_piclen_file, "%d\n", &numbytes);
                if(1 != entries)
                    numbytes = u4_ip_buf_len;
            }

            u4_bytes_remaining = fread(pu1_bs_buf, sizeof(UWORD8),
                                       numbytes, ps_ip_file);

            if(u4_bytes_remaining == 0)
            {
                if(1 == s_app_ctx.loopback)
                {
                    file_pos = 0;
                    if(0 == s_app_ctx.u4_piclen_flag)
                    {
                        fseek(ps_ip_file, file_pos, SEEK_SET);
                        numbytes = u4_ip_buf_len;
                    }
                    else
                    {
                        WORD32 entries;
                        entries = fscanf(ps_piclen_file, "%d\n", &numbytes);
                        if(1 != entries)
                            numbytes = u4_ip_buf_len;
                    }


                    u4_bytes_remaining = fread(pu1_bs_buf, sizeof(UWORD8),
                                               numbytes, ps_ip_file);
                }
                else
                    break;
            }
        }

        /*********************************************************************/
        /* Following calls can be enabled at diffent times                   */
        /*********************************************************************/
#if ENABLE_DEGRADE
        if(u4_op_frm_ts >= 10000)
            disable_deblocking(codec_obj, 4);

        if(u4_op_frm_ts == 30000)
            enable_deblocking(codec_obj);

        if(u4_op_frm_ts == 10000)
            enable_skippb_frames(codec_obj);

        if(u4_op_frm_ts == 60000)
            disable_skippb_frames(codec_obj);

        if(u4_op_frm_ts == 30000)
            enable_skipb_frames(codec_obj);

        if(u4_op_frm_ts == 60000)
            disable_skipb_frames(codec_obj);
#endif


        {
            ivd_video_decode_ip_t s_video_decode_ip;
            ivd_video_decode_op_t s_video_decode_op;
#ifdef PROFILE_ENABLE
            UWORD32 s_elapsed_time;
            TIMER s_start_timer;
            TIMER s_end_timer;
#endif


            s_video_decode_ip.e_cmd = IVD_CMD_VIDEO_DECODE;
            s_video_decode_ip.u4_ts = u4_ip_frm_ts;
            s_video_decode_ip.pv_stream_buffer = pu1_bs_buf;
            s_video_decode_ip.u4_num_Bytes = u4_bytes_remaining;
            s_video_decode_ip.u4_size = sizeof(ivd_video_decode_ip_t);
            s_video_decode_ip.s_out_buffer.u4_min_out_buf_size[0] =
                            ps_out_buf->u4_min_out_buf_size[0];
            s_video_decode_ip.s_out_buffer.u4_min_out_buf_size[1] =
                            ps_out_buf->u4_min_out_buf_size[1];
            s_video_decode_ip.s_out_buffer.u4_min_out_buf_size[2] =
                            ps_out_buf->u4_min_out_buf_size[2];

            s_video_decode_ip.s_out_buffer.pu1_bufs[0] =
                            ps_out_buf->pu1_bufs[0];
            s_video_decode_ip.s_out_buffer.pu1_bufs[1] =
                            ps_out_buf->pu1_bufs[1];
            s_video_decode_ip.s_out_buffer.pu1_bufs[2] =
                            ps_out_buf->pu1_bufs[2];
            s_video_decode_ip.s_out_buffer.u4_num_bufs =
                            ps_out_buf->u4_num_bufs;
            s_video_decode_op.u4_size = sizeof(ivd_video_decode_op_t);

            /* Get display buffer pointers */
            if(1 == s_app_ctx.display)
            {
                WORD32 wr_idx;

                wr_idx = dispq_producer_dequeue(&s_app_ctx);

                if(s_app_ctx.quit)
                    break;

                s_app_ctx.set_disp_buffers(s_app_ctx.pv_disp_ctx, wr_idx,
                                     &s_video_decode_ip.s_out_buffer.pu1_bufs[0],
                                     &s_video_decode_ip.s_out_buffer.pu1_bufs[1],
                                     &s_video_decode_ip.s_out_buffer.pu1_bufs[2]);
            }

            /*****************************************************************************/
            /*   API Call: Video Decode                                                  */
            /*****************************************************************************/

            GETTIME(&s_start_timer);

            ret = ivd_api_function((iv_obj_t *)codec_obj, (void *)&s_video_decode_ip,
                                       (void *)&s_video_decode_op);


            GETTIME(&s_end_timer);
            ELAPSEDTIME(s_start_timer,s_end_timer,s_elapsed_time,frequency);
#ifdef PROFILE_ENABLE
            {
                    UWORD32 peak_avg, id;
                    u4_tot_cycles += s_elapsed_time;
                    peak_window[peak_window_idx++] = s_elapsed_time;
                    if(peak_window_idx == PEAK_WINDOW_SIZE)
                    peak_window_idx = 0;
                    peak_avg = 0;
                    for(id = 0; id < PEAK_WINDOW_SIZE; id++)
                    {
                            peak_avg += peak_window[id];
                    }
                    peak_avg /= PEAK_WINDOW_SIZE;
                    if(peak_avg > peak_avg_max)
                    peak_avg_max = peak_avg;
                    frm_cnt++;

                    printf("FrameNum: %4d TimeTaken(microsec): %6d AvgTime: %6d PeakAvgTimeMax: %6d Output: %2d NumBytes: %6d \n",
                                    frm_cnt, s_elapsed_time, u4_tot_cycles / frm_cnt, peak_avg_max, s_video_decode_op.u4_output_present, s_video_decode_op.u4_num_bytes_consumed);

            }
#ifdef INTEL_CE5300
        time_consumed += s_elapsed_time;
        bytes_consumed += s_video_decode_op.u4_num_bytes_consumed;
        if (!(frm_cnt % (s_app_ctx.fps)))
        {
            time_consumed = time_consumed/s_app_ctx.fps;
            printf("Average decode time(micro sec) for the last second = %6d\n",time_consumed);
            printf("Average bitrate(kb) for the last second = %6d\n",(bytes_consumed * 8) / 1024);
            time_consumed = 0;
            bytes_consumed = 0;

        }
#endif
#else
        printf("%d\n",s_video_decode_op.u4_num_bytes_consumed);
#endif

            if(ret != IV_SUCCESS)
            {
                printf("Error in video Frame decode : ret %x Error %x\n", ret,
                       s_video_decode_op.u4_error_code);
            }

            if((IV_SUCCESS != ret) &&
                            ((s_video_decode_op.u4_error_code & 0xFF) == IVD_RES_CHANGED))
            {
                ivd_ctl_reset_ip_t s_ctl_ip;
                ivd_ctl_reset_op_t s_ctl_op;

                flush_output(codec_obj, &s_app_ctx, ps_out_buf,
                             pu1_bs_buf, &u4_op_frm_ts,
                             ps_op_file, ps_op_chksum_file,
                             u4_ip_frm_ts, u4_bytes_remaining);

                s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL;
                s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_RESET;
                s_ctl_ip.u4_size = sizeof(ivd_ctl_reset_ip_t);
                s_ctl_op.u4_size = sizeof(ivd_ctl_reset_op_t);

                ret = ivd_api_function((iv_obj_t *)codec_obj, (void *)&s_ctl_ip,
                                                   (void *)&s_ctl_op);
                if(IV_SUCCESS != ret)
                {
                    sprintf(ac_error_str, "Error in Reset");
                    codec_exit(ac_error_str);
                }

                /*when reset all buffers are released by lib*/
                u4_num_disp_bufs_with_dec = 0;
                /*************************************************************************/
                /* set num of cores                                                      */
                /*************************************************************************/
                {

                    ih264d_ctl_set_num_cores_ip_t s_ctl_set_cores_ip;
                    ih264d_ctl_set_num_cores_op_t s_ctl_set_cores_op;

                    s_ctl_set_cores_ip.e_cmd = IVD_CMD_VIDEO_CTL;
                    s_ctl_set_cores_ip.e_sub_cmd =(IVD_CONTROL_API_COMMAND_TYPE_T) IH264D_CMD_CTL_SET_NUM_CORES;
                    s_ctl_set_cores_ip.u4_num_cores = s_app_ctx.u4_num_cores;
                    s_ctl_set_cores_ip.u4_size = sizeof(ih264d_ctl_set_num_cores_ip_t);
                    s_ctl_set_cores_op.u4_size = sizeof(ih264d_ctl_set_num_cores_op_t);

                    ret = ivd_api_function((iv_obj_t*)codec_obj, (void *)&s_ctl_set_cores_ip,
                                               (void *)&s_ctl_set_cores_op);
                    if(ret != IV_SUCCESS)
                    {
                        sprintf(ac_error_str, "\nError in setting number of cores");
                        codec_exit(ac_error_str);
                    }

                }
                /*************************************************************************/
                /* set processsor                                                        */
                /*************************************************************************/

                {

                    ih264d_ctl_set_processor_ip_t s_ctl_set_num_processor_ip;
                    ih264d_ctl_set_processor_op_t s_ctl_set_num_processor_op;

                    s_ctl_set_num_processor_ip.e_cmd = IVD_CMD_VIDEO_CTL;
                    s_ctl_set_num_processor_ip.e_sub_cmd =(IVD_CONTROL_API_COMMAND_TYPE_T) IH264D_CMD_CTL_SET_PROCESSOR;
                    s_ctl_set_num_processor_ip.u4_arch = s_app_ctx.e_arch;
                    s_ctl_set_num_processor_ip.u4_soc = s_app_ctx.e_soc;
                    s_ctl_set_num_processor_ip.u4_size = sizeof(ih264d_ctl_set_processor_ip_t);
                    s_ctl_set_num_processor_op.u4_size = sizeof(ih264d_ctl_set_processor_op_t);

                    ret = ivd_api_function((iv_obj_t*)codec_obj, (void *)&s_ctl_set_num_processor_ip,
                                               (void *)&s_ctl_set_num_processor_op);
                    if(ret != IV_SUCCESS)
                    {
                        sprintf(ac_error_str, "\nError in setting Processor type");
                        codec_exit(ac_error_str);
                    }

                }
            }


            if((1 == s_app_ctx.display) &&
                            (1 == s_video_decode_op.u4_output_present))
            {
                dispq_producer_queue(&s_app_ctx);
            }

            if(IV_B_FRAME == s_video_decode_op.e_pic_type)
                s_app_ctx.b_pic_present |= 1;

            u4_num_bytes_dec = s_video_decode_op.u4_num_bytes_consumed;

            file_pos += u4_num_bytes_dec;
            total_bytes_comsumed += u4_num_bytes_dec;
            u4_ip_frm_ts++;


            if(1 == s_video_decode_op.u4_output_present)
            {

                CHAR cur_fname[1000];
                CHAR *extn = NULL;
                /* The objective is to dump the decoded frames into separate files instead of
                 * dumping all the frames in one common file. Also, the number of dumped frames
                 * at any given instance of time cannot exceed 'frame_memory'
                 */
                if(s_app_ctx.u4_file_save_flag)
                {
                    /* Locate the position of extension yuv */
                    extn = strstr(s_app_ctx.ac_op_fname,"%d");
                    if (extn != NULL)
                    {
                        output_write_stall(s_app_ctx.ac_op_fname,u4_op_frm_ts);
                        /* Generate output file names */
                        sprintf(cur_fname,s_app_ctx.ac_op_fname,u4_op_frm_ts);
                        /* Open Output file */
                        ps_op_file = fopen(cur_fname,"wb");
                        if (NULL == ps_op_file)
                        {
                            sprintf(ac_error_str, "Could not open output file %s",
                                    cur_fname);

                            codec_exit(ac_error_str);
                        }
                    }
                }

                width = s_video_decode_op.s_disp_frm_buf.u4_y_wd;
                height = s_video_decode_op.s_disp_frm_buf.u4_y_ht;
                dump_output(&s_app_ctx, &(s_video_decode_op.s_disp_frm_buf),
                            s_video_decode_op.u4_disp_buf_id, ps_op_file,
                            ps_op_chksum_file,
                            u4_op_frm_ts, s_app_ctx.u4_file_save_flag,
                            s_app_ctx.u4_chksum_save_flag);

                u4_op_frm_ts++;
                if (extn != NULL)
                    fclose(ps_op_file);

            }
            else
            {
                if((s_video_decode_op.u4_error_code >> IVD_FATALERROR) & 1)
                {
                    printf("Fatal error\n");
                    break;
                }
            }

        }
    }

    /***********************************************************************/
    /*      To get the last decoded frames, call process with NULL input    */
    /***********************************************************************/
    flush_output(codec_obj, &s_app_ctx, ps_out_buf,
                 pu1_bs_buf, &u4_op_frm_ts,
                 ps_op_file, ps_op_chksum_file,
                 u4_ip_frm_ts, u4_bytes_remaining);

    /* set disp_end u4_flag */
    s_app_ctx.quit = 1;


#ifdef PROFILE_ENABLE
    printf("Summary\n");
    printf("Input filename                  : %s\n", s_app_ctx.ac_ip_fname);
    printf("Output Width                    : %-4d\n", width);
    printf("Output Height                   : %-4d\n", height);

    if(frm_cnt)
    {
        double avg = u4_tot_cycles / frm_cnt;
        double bytes_avg = total_bytes_comsumed / frm_cnt;
        double bitrate = (bytes_avg * 8 * s_app_ctx.fps)/1000000;
        printf("Bitrate @ %2d fps(mbps)          : %-6.2f\n", s_app_ctx.fps, bitrate);
        printf("Average decode time(micro sec)  : %-6d\n", (WORD32)avg);
        printf("Avg Peak decode time(%2d frames) : %-6d\n", PEAK_WINDOW_SIZE, (WORD32)peak_avg_max);
        avg = (u4_tot_cycles + u4_tot_fmt_cycles)* 1.0 / frm_cnt;

        if(0 == s_app_ctx.u4_share_disp_buf)
            printf("FPS achieved (with format conv) : %-3.2f\n", 1000000/avg);
        else
            printf("FPS achieved                    : %-3.2f\n", 1000000/avg);
    }
#endif
    /***********************************************************************/
    /*   Clear the decoder, close all the files, free all the memory       */
    /***********************************************************************/
    if(1 == s_app_ctx.display)
    {
        s_app_ctx.display_deinit_flag = 1;
        /* wait for display to finish */
        if(s_app_ctx.display_thread_created)
        {
            ithread_join(s_app_ctx.display_thread_handle, NULL);
        }
        free(s_app_ctx.display_thread_handle);
    }

    {
        ivd_delete_ip_t s_delete_dec_ip;
        ivd_delete_op_t s_delete_dec_op;

        s_delete_dec_ip.e_cmd = IVD_CMD_DELETE;
        s_delete_dec_ip.u4_size = sizeof(ivd_delete_ip_t);
        s_delete_dec_op.u4_size = sizeof(ivd_delete_op_t);

        ret = ivd_api_function((iv_obj_t *)codec_obj, (void *)&s_delete_dec_ip,
                                   (void *)&s_delete_dec_op);

        if(IV_SUCCESS != ret)
        {
            sprintf(ac_error_str, "Error in Codec delete");
            codec_exit(ac_error_str);
        }
    }
    /***********************************************************************/
    /*              Close all the files and free all the memory            */
    /***********************************************************************/
    {
        fclose(ps_ip_file);

        if((1 == s_app_ctx.u4_file_save_flag) && (strstr(s_app_ctx.ac_op_fname,"%d") == NULL))
        {
            fclose(ps_op_file);
        }
        if(1 == s_app_ctx.u4_chksum_save_flag)
        {
            fclose(ps_op_chksum_file);
        }

    }

    if(0 == s_app_ctx.u4_share_disp_buf)
    {
        free(ps_out_buf->pu1_bufs[0]);
    }

    for(i = 0; i < s_app_ctx.num_disp_buf; i++)
    {
        free(s_app_ctx.s_disp_buffers[i].pu1_bufs[0]);
    }

    free(ps_out_buf);
    free(pu1_bs_buf);

    return (0);
}