summaryrefslogtreecommitdiffstats
path: root/libvideoeditor/vss/src/M4VSS3GPP_EditVideo.c
blob: f19f41218dd1f83f987b5d6c5fb081be42df3f1c (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
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
/*
 * Copyright (C) 2011 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.
 */
/**
 ******************************************************************************
 * @file    M4VSS3GPP_EditVideo.c
 * @brief    Video Studio Service 3GPP edit API implementation.
 * @note
 ******************************************************************************
 */
#undef M4OSA_TRACE_LEVEL
#define M4OSA_TRACE_LEVEL 1

/****************/
/*** Includes ***/
/****************/

#include "NXPSW_CompilerSwitches.h"
/**
 * Our header */
#include "M4VSS3GPP_API.h"
#include "M4VSS3GPP_InternalTypes.h"
#include "M4VSS3GPP_InternalFunctions.h"
#include "M4VSS3GPP_InternalConfig.h"
#include "M4VSS3GPP_ErrorCodes.h"

// StageFright encoders require %16 resolution
#include "M4ENCODER_common.h"
/**
 * OSAL headers */
#include "M4OSA_Memory.h" /**< OSAL memory management */
#include "M4OSA_Debug.h"  /**< OSAL debug management */

/**
 * component includes */
#include "M4VFL_transition.h" /**< video effects */

/*for transition behaviour*/
#include <math.h>
#include "M4AIR_API.h"
#include "M4VSS3GPP_Extended_API.h"
/** Determine absolute value of a. */
#define M4xVSS_ABS(a) ( ( (a) < (0) ) ? (-(a)) : (a) )
#define Y_PLANE_BORDER_VALUE    0x00
#define U_PLANE_BORDER_VALUE    0x80
#define V_PLANE_BORDER_VALUE    0x80

/************************************************************************/
/* Static local functions                                               */
/************************************************************************/

static M4OSA_ERR M4VSS3GPP_intCheckVideoMode(
    M4VSS3GPP_InternalEditContext *pC );
static M4OSA_Void
M4VSS3GPP_intCheckVideoEffects( M4VSS3GPP_InternalEditContext *pC,
                               M4OSA_UInt8 uiClipNumber );
static M4OSA_ERR M4VSS3GPP_intApplyVideoEffect(
          M4VSS3GPP_InternalEditContext *pC, M4VIFI_ImagePlane *pPlaneIn,
          M4VIFI_ImagePlane *pPlaneOut, M4OSA_Bool bSkipFramingEffect);

static M4OSA_ERR
M4VSS3GPP_intVideoTransition( M4VSS3GPP_InternalEditContext *pC,
                             M4VIFI_ImagePlane *pPlaneOut );

static M4OSA_Void
M4VSS3GPP_intUpdateTimeInfo( M4VSS3GPP_InternalEditContext *pC,
                            M4SYS_AccessUnit *pAU );
static M4OSA_Void M4VSS3GPP_intSetH263TimeCounter( M4OSA_MemAddr8 pAuDataBuffer,
                                                  M4OSA_UInt8 uiCts );
static M4OSA_Void M4VSS3GPP_intSetMPEG4Gov( M4OSA_MemAddr8 pAuDataBuffer,
                                           M4OSA_UInt32 uiCtsSec );
static M4OSA_Void M4VSS3GPP_intGetMPEG4Gov( M4OSA_MemAddr8 pAuDataBuffer,
                                           M4OSA_UInt32 *pCtsSec );
static M4OSA_ERR M4VSS3GPP_intAllocateYUV420( M4VIFI_ImagePlane *pPlanes,
                                             M4OSA_UInt32 uiWidth, M4OSA_UInt32 uiHeight );
static M4OSA_ERR M4VSS3GPP_internalConvertAndResizeARGB8888toYUV420(
          M4OSA_Void* pFileIn, M4OSA_FileReadPointer* pFileReadPtr,
          M4VIFI_ImagePlane* pImagePlanes,
          M4OSA_UInt32 width,M4OSA_UInt32 height);
static M4OSA_ERR M4VSS3GPP_intApplyRenderingMode(
          M4VSS3GPP_InternalEditContext *pC,
          M4xVSS_MediaRendering renderingMode,
          M4VIFI_ImagePlane* pInplane,
          M4VIFI_ImagePlane* pOutplane);

static M4OSA_ERR M4VSS3GPP_intSetYuv420PlaneFromARGB888 (
                                        M4VSS3GPP_InternalEditContext *pC,
                                        M4VSS3GPP_ClipContext* pClipCtxt);
static M4OSA_ERR M4VSS3GPP_intRenderFrameWithEffect(
                                             M4VSS3GPP_InternalEditContext *pC,
                                             M4VSS3GPP_ClipContext* pClipCtxt,
                                             M4_MediaTime ts,
                                             M4OSA_Bool bIsClip1,
                                             M4VIFI_ImagePlane *pResizePlane,
                                             M4VIFI_ImagePlane *pPlaneNoResize,
                                             M4VIFI_ImagePlane *pPlaneOut);

static M4OSA_ERR M4VSS3GPP_intRotateVideo(M4VIFI_ImagePlane* pPlaneIn,
                                      M4OSA_UInt32 rotationDegree);

static M4OSA_ERR M4VSS3GPP_intSetYUV420Plane(M4VIFI_ImagePlane* planeIn,
                                      M4OSA_UInt32 width, M4OSA_UInt32 height);

static M4OSA_ERR M4VSS3GPP_intApplyVideoOverlay (
                                      M4VSS3GPP_InternalEditContext *pC,
                                      M4VIFI_ImagePlane *pPlaneIn,
                                      M4VIFI_ImagePlane *pPlaneOut);

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intEditStepVideo()
 * @brief    One step of video processing
 * @param   pC    (IN/OUT) Internal edit context
 ******************************************************************************
 */
M4OSA_ERR M4VSS3GPP_intEditStepVideo( M4VSS3GPP_InternalEditContext *pC )
{
    M4OSA_ERR err;
    M4OSA_Int32 iCts, iNextCts;
    M4ENCODER_FrameMode FrameMode;
    M4OSA_Bool bSkipFrame;
    M4OSA_UInt16 offset;

    /**
     * Check if we reached end cut. Decorrelate input and output encoding
     * timestamp to handle encoder prefetch
     */
    if ( ((M4OSA_Int32)(pC->ewc.dInputVidCts) - pC->pC1->iVoffset
        + pC->iInOutTimeOffset) >= pC->pC1->iEndTime )
    {
        /* Re-adjust video to precise cut time */
        pC->iInOutTimeOffset = ((M4OSA_Int32)(pC->ewc.dInputVidCts))
            - pC->pC1->iVoffset + pC->iInOutTimeOffset - pC->pC1->iEndTime;
        if ( pC->iInOutTimeOffset < 0 ) {
            pC->iInOutTimeOffset = 0;
        }

        /**
        * Video is done for this clip */
        err = M4VSS3GPP_intReachedEndOfVideo(pC);

        /* RC: to know when a file has been processed */
        if (M4NO_ERROR != err && err != M4VSS3GPP_WAR_SWITCH_CLIP)
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intEditStepVideo: M4VSS3GPP_intReachedEndOfVideo returns 0x%x",
                err);
        }

        return err;
    }

    /* Don't change the states if we are in decodeUpTo() */
    if ( (M4VSS3GPP_kClipStatus_DECODE_UP_TO != pC->pC1->Vstatus)
        && (( pC->pC2 == M4OSA_NULL)
        || (M4VSS3GPP_kClipStatus_DECODE_UP_TO != pC->pC2->Vstatus)) )
    {
        /**
        * Check Video Mode, depending on the current output CTS */
        err = M4VSS3GPP_intCheckVideoMode(
            pC); /**< This function change the pC->Vstate variable! */

        if (M4NO_ERROR != err)
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intEditStepVideo: M4VSS3GPP_intCheckVideoMode returns 0x%x!",
                err);
            return err;
        }
    }


    switch( pC->Vstate )
    {
        /* _________________ */
        /*|                 |*/
        /*| READ_WRITE MODE |*/
        /*|_________________|*/

        case M4VSS3GPP_kEditVideoState_READ_WRITE:
        case M4VSS3GPP_kEditVideoState_AFTER_CUT:
            {
                M4OSA_TRACE3_0("M4VSS3GPP_intEditStepVideo READ_WRITE");

                bSkipFrame = M4OSA_FALSE;

                /**
                * If we were decoding the clip, we must jump to be sure
                * to get to the good position. */
                if( M4VSS3GPP_kClipStatus_READ != pC->pC1->Vstatus )
                {
                    /**
                    * Jump to target video time (tc = to-T) */
                // Decorrelate input and output encoding timestamp to handle encoder prefetch
                iCts = (M4OSA_Int32)(pC->ewc.dInputVidCts) - pC->pC1->iVoffset;
                    err = pC->pC1->ShellAPI.m_pReader->m_pFctJump(
                        pC->pC1->pReaderContext,
                        (M4_StreamHandler *)pC->pC1->pVideoStream, &iCts);

                    if( M4NO_ERROR != err )
                    {
                        M4OSA_TRACE1_1(
                            "M4VSS3GPP_intEditStepVideo:\
                            READ_WRITE: m_pReader->m_pFctJump(V1) returns 0x%x!",
                            err);
                        return err;
                    }

                    err = pC->pC1->ShellAPI.m_pReaderDataIt->m_pFctGetNextAu(
                        pC->pC1->pReaderContext,
                        (M4_StreamHandler *)pC->pC1->pVideoStream,
                        &pC->pC1->VideoAU);

                    if( ( M4NO_ERROR != err) && (M4WAR_NO_MORE_AU != err) )
                    {
                        M4OSA_TRACE1_1(
                            "M4VSS3GPP_intEditStepVideo:\
                            READ_WRITE: m_pReader->m_pFctGetNextAu returns 0x%x!",
                            err);
                        return err;
                    }

                    M4OSA_TRACE2_3("A .... read  : cts  = %.0f + %ld [ 0x%x ]",
                        pC->pC1->VideoAU.m_CTS, pC->pC1->iVoffset,
                        pC->pC1->VideoAU.m_size);

                    /* This frame has been already written in BEGIN CUT step -> skip it */
                    if( pC->pC1->VideoAU.m_CTS == iCts
                        && pC->pC1->iVideoRenderCts >= iCts )
                    {
                        bSkipFrame = M4OSA_TRUE;
                    }
                }

                /* This frame has been already written in BEGIN CUT step -> skip it */
                if( ( pC->Vstate == M4VSS3GPP_kEditVideoState_AFTER_CUT)
                    && (pC->pC1->VideoAU.m_CTS
                    + pC->pC1->iVoffset <= pC->ewc.WriterVideoAU.CTS) )
                {
                    bSkipFrame = M4OSA_TRUE;
                }

                /**
                * Remember the clip reading state */
                pC->pC1->Vstatus = M4VSS3GPP_kClipStatus_READ;
                // Decorrelate input and output encoding timestamp to handle encoder prefetch
                // Rounding is to compensate reader imprecision (m_CTS is actually an integer)
                iCts = ((M4OSA_Int32)pC->ewc.dInputVidCts) - pC->pC1->iVoffset - 1;
                iNextCts = iCts + ((M4OSA_Int32)pC->dOutputFrameDuration) + 1;
                /* Avoid to write a last frame of duration 0 */
                if( iNextCts > pC->pC1->iEndTime )
                    iNextCts = pC->pC1->iEndTime;

                /**
                * If the AU is good to be written, write it, else just skip it */
                if( ( M4OSA_FALSE == bSkipFrame)
                    && (( pC->pC1->VideoAU.m_CTS >= iCts)
                    && (pC->pC1->VideoAU.m_CTS < iNextCts)
                    && (pC->pC1->VideoAU.m_size > 0)) )
                {
                    /**
                    * Get the output AU to write into */
                    err = pC->ShellAPI.pWriterDataFcts->pStartAU(
                        pC->ewc.p3gpWriterContext,
                        M4VSS3GPP_WRITER_VIDEO_STREAM_ID,
                        &pC->ewc.WriterVideoAU);

                    if( M4NO_ERROR != err )
                    {
                        M4OSA_TRACE1_1(
                            "M4VSS3GPP_intEditStepVideo: READ_WRITE:\
                            pWriterDataFcts->pStartAU(Video) returns 0x%x!",
                            err);
                        return err;
                    }

                    /**
                    * Copy the input AU to the output AU */
                    pC->ewc.WriterVideoAU.attribute = pC->pC1->VideoAU.m_attribute;
                    // Decorrelate input and output encoding timestamp to handle encoder prefetch
                    pC->ewc.WriterVideoAU.CTS = (M4OSA_Time)pC->pC1->VideoAU.m_CTS +
                        (M4OSA_Time)pC->pC1->iVoffset;
                    pC->ewc.dInputVidCts += pC->dOutputFrameDuration;
                    offset = 0;
                    /* for h.264 stream do not read the 1st 4 bytes as they are header
                     indicators */
                    if( pC->pC1->pVideoStream->m_basicProperties.m_streamType
                        == M4DA_StreamTypeVideoMpeg4Avc )
                        offset = 4;

                    pC->ewc.WriterVideoAU.size = pC->pC1->VideoAU.m_size - offset;
                    if( pC->ewc.WriterVideoAU.size > pC->ewc.uiVideoMaxAuSize )
                    {
                        M4OSA_TRACE1_2(
                            "M4VSS3GPP_intEditStepVideo: READ_WRITE: AU size greater than\
                             MaxAuSize (%d>%d)! returning M4VSS3GPP_ERR_INPUT_VIDEO_AU_TOO_LARGE",
                            pC->ewc.WriterVideoAU.size, pC->ewc.uiVideoMaxAuSize);
                        return M4VSS3GPP_ERR_INPUT_VIDEO_AU_TOO_LARGE;
                    }

                    memcpy((void *)pC->ewc.WriterVideoAU.dataAddress,
                        (void *)(pC->pC1->VideoAU.m_dataAddress + offset),
                        (pC->ewc.WriterVideoAU.size));

                    /**
                    * Update time info for the Counter Time System to be equal to the bit
                    -stream time*/
                    M4VSS3GPP_intUpdateTimeInfo(pC, &pC->ewc.WriterVideoAU);
                    M4OSA_TRACE2_2("B ---- write : cts  = %lu [ 0x%x ]",
                        pC->ewc.WriterVideoAU.CTS, pC->ewc.WriterVideoAU.size);

                    /**
                    * Write the AU */
                    err = pC->ShellAPI.pWriterDataFcts->pProcessAU(
                        pC->ewc.p3gpWriterContext,
                        M4VSS3GPP_WRITER_VIDEO_STREAM_ID,
                        &pC->ewc.WriterVideoAU);

                    if( M4NO_ERROR != err )
                    {
                        /* the warning M4WAR_WRITER_STOP_REQ is returned when the targeted output
                         file size is reached
                        The editing is then finished, the warning M4VSS3GPP_WAR_EDITING_DONE
                        is returned*/
                        if( M4WAR_WRITER_STOP_REQ == err )
                        {
                            M4OSA_TRACE1_0(
                                "M4VSS3GPP_intEditStepVideo: File was cut to avoid oversize");
                            return M4VSS3GPP_WAR_EDITING_DONE;
                        }
                        else
                        {
                            M4OSA_TRACE1_1(
                                "M4VSS3GPP_intEditStepVideo: READ_WRITE:\
                                pWriterDataFcts->pProcessAU(Video) returns 0x%x!",
                                err);
                            return err;
                        }
                    }

                    /**
                    * Read next AU for next step */
                    err = pC->pC1->ShellAPI.m_pReaderDataIt->m_pFctGetNextAu(
                        pC->pC1->pReaderContext,
                        (M4_StreamHandler *)pC->pC1->pVideoStream,
                        &pC->pC1->VideoAU);

                    if( ( M4NO_ERROR != err) && (M4WAR_NO_MORE_AU != err) )
                    {
                        M4OSA_TRACE1_1(
                            "M4VSS3GPP_intEditStepVideo: READ_WRITE:\
                            m_pReaderDataIt->m_pFctGetNextAu returns 0x%x!",
                            err);
                        return err;
                    }

                    M4OSA_TRACE2_3("C .... read  : cts  = %.0f + %ld [ 0x%x ]",
                        pC->pC1->VideoAU.m_CTS, pC->pC1->iVoffset,
                        pC->pC1->VideoAU.m_size);
                }
                else
                {
                    /**
                    * Decide wether to read or to increment time increment */
                    if( ( pC->pC1->VideoAU.m_size == 0)
                        || (pC->pC1->VideoAU.m_CTS >= iNextCts) )
                    {
                        /*Increment time by the encoding period (NO_MORE_AU or reader in advance */
                       // Decorrelate input and output encoding timestamp to handle encoder prefetch
                       pC->ewc.dInputVidCts += pC->dOutputFrameDuration;

                        /* Switch (from AFTER_CUT) to normal mode because time is
                        no more frozen */
                        pC->Vstate = M4VSS3GPP_kEditVideoState_READ_WRITE;
                    }
                    else
                    {
                        /* In other cases (reader late), just let the reader catch up
                         pC->ewc.dVTo */
                        err = pC->pC1->ShellAPI.m_pReaderDataIt->m_pFctGetNextAu(
                            pC->pC1->pReaderContext,
                            (M4_StreamHandler *)pC->pC1->pVideoStream,
                            &pC->pC1->VideoAU);

                        if( ( M4NO_ERROR != err) && (M4WAR_NO_MORE_AU != err) )
                        {
                            M4OSA_TRACE1_1(
                                "M4VSS3GPP_intEditStepVideo: READ_WRITE:\
                                m_pReaderDataIt->m_pFctGetNextAu returns 0x%x!",
                                err);
                            return err;
                        }

                        M4OSA_TRACE2_3("D .... read  : cts  = %.0f + %ld [ 0x%x ]",
                            pC->pC1->VideoAU.m_CTS, pC->pC1->iVoffset,
                            pC->pC1->VideoAU.m_size);
                    }
                }
            }
            break;

            /* ____________________ */
            /*|                    |*/
            /*| DECODE_ENCODE MODE |*/
            /*|   BEGIN_CUT MODE   |*/
            /*|____________________|*/

        case M4VSS3GPP_kEditVideoState_DECODE_ENCODE:
        case M4VSS3GPP_kEditVideoState_BEGIN_CUT:
            {
                M4OSA_TRACE3_0(
                    "M4VSS3GPP_intEditStepVideo DECODE_ENCODE / BEGIN_CUT");

            if ((pC->pC1->pSettings->FileType ==
                     M4VIDEOEDITING_kFileType_ARGB8888) &&
                (M4OSA_FALSE ==
                    pC->pC1->pSettings->ClipProperties.bSetImageData)) {

                err = M4VSS3GPP_intSetYuv420PlaneFromARGB888(pC, pC->pC1);
                if( M4NO_ERROR != err ) {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_intEditStepVideo: DECODE_ENCODE:\
                        M4VSS3GPP_intSetYuv420PlaneFromARGB888 err=%x", err);
                    return err;
                }
            }
                /**
                * Decode the video up to the target time
                (will jump to the previous RAP if needed ) */
                // Decorrelate input and output encoding timestamp to handle encoder prefetch
                err = M4VSS3GPP_intClipDecodeVideoUpToCts(pC->pC1, (M4OSA_Int32)pC->ewc.dInputVidCts);
                if( M4NO_ERROR != err )
                {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_intEditStepVideo: DECODE_ENCODE:\
                        M4VSS3GPP_intDecodeVideoUpToCts returns err=0x%x",
                        err);
                    return err;
                }

                /* If the decoding is not completed, do one more step with time frozen */
                if( M4VSS3GPP_kClipStatus_DECODE_UP_TO == pC->pC1->Vstatus )
                {
                    return M4NO_ERROR;
                }

                /**
                * Reset the video pre-processing error before calling the encoder */
                pC->ewc.VppError = M4NO_ERROR;

                M4OSA_TRACE2_0("E ++++ encode AU");

                /**
                * Encode the frame(rendering,filtering and writing will be done
                 in encoder callbacks)*/
                if( pC->Vstate == M4VSS3GPP_kEditVideoState_BEGIN_CUT )
                    FrameMode = M4ENCODER_kIFrame;
                else
                    FrameMode = M4ENCODER_kNormalFrame;

                // Decorrelate input and output encoding timestamp to handle encoder prefetch
                err = pC->ShellAPI.pVideoEncoderGlobalFcts->pFctEncode(pC->ewc.pEncContext, M4OSA_NULL,
                pC->ewc.dInputVidCts, FrameMode);
                /**
                * Check if we had a VPP error... */
                if( M4NO_ERROR != pC->ewc.VppError )
                {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_intEditStepVideo: DECODE_ENCODE:\
                        pVideoEncoderGlobalFcts->pFctEncode, returning VppErr=0x%x",
                        pC->ewc.VppError);
#ifdef M4VSS_SUPPORT_OMX_CODECS

                    if( M4WAR_VIDEORENDERER_NO_NEW_FRAME != pC->ewc.VppError )
                    {
#endif //M4VSS_SUPPORT_OMX_CODECS

                        return pC->ewc.VppError;
#ifdef M4VSS_SUPPORT_OMX_CODECS

                    }

#endif                                   //M4VSS_SUPPORT_OMX_CODECS
                }
                else if( M4NO_ERROR != err ) /**< ...or an encoder error */
                {
                    if( ((M4OSA_UInt32)M4ERR_ALLOC) == err )
                    {
                        M4OSA_TRACE1_0(
                            "M4VSS3GPP_intEditStepVideo: DECODE_ENCODE:\
                            returning M4VSS3GPP_ERR_ENCODER_ACCES_UNIT_ERROR");
                        return M4VSS3GPP_ERR_ENCODER_ACCES_UNIT_ERROR;
                    }
                    /* the warning M4WAR_WRITER_STOP_REQ is returned when the targeted output
                    file size is reached
                    The editing is then finished, the warning M4VSS3GPP_WAR_EDITING_DONE
                    is returned*/
                    else if( M4WAR_WRITER_STOP_REQ == err )
                    {
                        M4OSA_TRACE1_0(
                            "M4VSS3GPP_intEditStepVideo: File was cut to avoid oversize");
                        return M4VSS3GPP_WAR_EDITING_DONE;
                    }
                    else
                    {
                        M4OSA_TRACE1_1(
                            "M4VSS3GPP_intEditStepVideo: DECODE_ENCODE:\
                            pVideoEncoderGlobalFcts->pFctEncode returns 0x%x",
                            err);
                        return err;
                    }
                }

                /**
                * Increment time by the encoding period (for begin cut, do not increment to not
                loose P-frames) */
                if( M4VSS3GPP_kEditVideoState_DECODE_ENCODE == pC->Vstate )
                {
                    // Decorrelate input and output encoding timestamp to handle encoder prefetch
                    pC->ewc.dInputVidCts += pC->dOutputFrameDuration;
                }
            }
            break;

            /* _________________ */
            /*|                 |*/
            /*| TRANSITION MODE |*/
            /*|_________________|*/

        case M4VSS3GPP_kEditVideoState_TRANSITION:
            {
                M4OSA_TRACE3_0("M4VSS3GPP_intEditStepVideo TRANSITION");

                /* Don't decode more than needed */
                if( !(( M4VSS3GPP_kClipStatus_DECODE_UP_TO != pC->pC1->Vstatus)
                    && (M4VSS3GPP_kClipStatus_DECODE_UP_TO == pC->pC2->Vstatus)) )
                {
                    /**
                    * Decode the clip1 video up to the target time
                    (will jump to the previous RAP if needed */
                    if ((pC->pC1->pSettings->FileType ==
                          M4VIDEOEDITING_kFileType_ARGB8888) &&
                        (M4OSA_FALSE ==
                         pC->pC1->pSettings->ClipProperties.bSetImageData)) {

                        err = M4VSS3GPP_intSetYuv420PlaneFromARGB888(pC, pC->pC1);
                        if( M4NO_ERROR != err ) {
                            M4OSA_TRACE1_1(
                                "M4VSS3GPP_intEditStepVideo: TRANSITION:\
                                M4VSS3GPP_intSetYuv420PlaneFromARGB888 err=%x", err);
                            return err;
                        }
                    }
                    // Decorrelate input and output encoding timestamp to handle encoder prefetch
                    err = M4VSS3GPP_intClipDecodeVideoUpToCts(pC->pC1,
                         (M4OSA_Int32)pC->ewc.dInputVidCts);
                    if( M4NO_ERROR != err )
                    {
                        M4OSA_TRACE1_1(
                            "M4VSS3GPP_intEditStepVideo: TRANSITION:\
                            M4VSS3GPP_intDecodeVideoUpToCts(C1) returns err=0x%x",
                            err);
                        return err;
                    }

                    /* If the decoding is not completed, do one more step with time frozen */
                    if( M4VSS3GPP_kClipStatus_DECODE_UP_TO == pC->pC1->Vstatus )
                    {
                        return M4NO_ERROR;
                    }
                }

                /* Don't decode more than needed */
                if( !(( M4VSS3GPP_kClipStatus_DECODE_UP_TO != pC->pC2->Vstatus)
                    && (M4VSS3GPP_kClipStatus_DECODE_UP_TO == pC->pC1->Vstatus)) )
                {
                    /**
                    * Decode the clip2 video up to the target time
                        (will jump to the previous RAP if needed) */
                    if ((pC->pC2->pSettings->FileType ==
                          M4VIDEOEDITING_kFileType_ARGB8888) &&
                        (M4OSA_FALSE ==
                          pC->pC2->pSettings->ClipProperties.bSetImageData)) {

                        err = M4VSS3GPP_intSetYuv420PlaneFromARGB888(pC, pC->pC2);
                        if( M4NO_ERROR != err ) {
                            M4OSA_TRACE1_1(
                                "M4VSS3GPP_intEditStepVideo: TRANSITION:\
                                M4VSS3GPP_intSetYuv420PlaneFromARGB888 err=%x", err);
                            return err;
                        }
                    }

                    // Decorrelate input and output encoding timestamp to handle encoder prefetch
                    err = M4VSS3GPP_intClipDecodeVideoUpToCts(pC->pC2,
                         (M4OSA_Int32)pC->ewc.dInputVidCts);
                    if( M4NO_ERROR != err )
                    {
                        M4OSA_TRACE1_1(
                            "M4VSS3GPP_intEditStepVideo: TRANSITION:\
                            M4VSS3GPP_intDecodeVideoUpToCts(C2) returns err=0x%x",
                            err);
                        return err;
                    }

                    /* If the decoding is not completed, do one more step with time frozen */
                    if( M4VSS3GPP_kClipStatus_DECODE_UP_TO == pC->pC2->Vstatus )
                    {
                        return M4NO_ERROR;
                    }
                }

                /**
                * Reset the video pre-processing error before calling the encoder */
                pC->ewc.VppError = M4NO_ERROR;

                M4OSA_TRACE2_0("F **** blend AUs");

                /**
                * Encode the frame (rendering, filtering and writing will be done
                in encoder callbacks */
                // Decorrelate input and output encoding timestamp to handle encoder prefetch
                err = pC->ShellAPI.pVideoEncoderGlobalFcts->pFctEncode(pC->ewc.pEncContext, M4OSA_NULL,
                    pC->ewc.dInputVidCts, M4ENCODER_kNormalFrame);

                /**
                * If encode returns a process frame error, it is likely to be a VPP error */
                if( M4NO_ERROR != pC->ewc.VppError )
                {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_intEditStepVideo: TRANSITION:\
                        pVideoEncoderGlobalFcts->pFctEncode, returning VppErr=0x%x",
                        pC->ewc.VppError);
#ifdef M4VSS_SUPPORT_OMX_CODECS

                    if( M4WAR_VIDEORENDERER_NO_NEW_FRAME != pC->ewc.VppError )
                    {

#endif //M4VSS_SUPPORT_OMX_CODECS

                        return pC->ewc.VppError;
#ifdef M4VSS_SUPPORT_OMX_CODECS

                    }

#endif //M4VSS_SUPPORT_OMX_CODECS
                }
                else if( M4NO_ERROR != err ) /**< ...or an encoder error */
                {
                    if( ((M4OSA_UInt32)M4ERR_ALLOC) == err )
                    {
                        M4OSA_TRACE1_0(
                            "M4VSS3GPP_intEditStepVideo: TRANSITION:\
                            returning M4VSS3GPP_ERR_ENCODER_ACCES_UNIT_ERROR");
                        return M4VSS3GPP_ERR_ENCODER_ACCES_UNIT_ERROR;
                    }

                    /* the warning M4WAR_WRITER_STOP_REQ is returned when the targeted output
                     file size is reached
                    The editing is then finished, the warning M4VSS3GPP_WAR_EDITING_DONE is
                     returned*/
                    else if( M4WAR_WRITER_STOP_REQ == err )
                    {
                        M4OSA_TRACE1_0(
                            "M4VSS3GPP_intEditStepVideo: File was cut to avoid oversize");
                        return M4VSS3GPP_WAR_EDITING_DONE;
                    }
                    else
                    {
                        M4OSA_TRACE1_1(
                            "M4VSS3GPP_intEditStepVideo: TRANSITION:\
                            pVideoEncoderGlobalFcts->pFctEncode returns 0x%x",
                            err);
                        return err;
                    }
                }

                /**
                * Increment time by the encoding period */
                // Decorrelate input and output encoding timestamp to handle encoder prefetch
                pC->ewc.dInputVidCts += pC->dOutputFrameDuration;
            }
            break;

            /* ____________ */
            /*|            |*/
            /*| ERROR CASE |*/
            /*|____________|*/

        default:
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intEditStepVideo: invalid internal state (0x%x),\
                returning M4VSS3GPP_ERR_INTERNAL_STATE",
                pC->Vstate);
            return M4VSS3GPP_ERR_INTERNAL_STATE;
    }

    /**
    * Return with no error */
    M4OSA_TRACE3_0("M4VSS3GPP_intEditStepVideo: returning M4NO_ERROR");
    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intCheckVideoMode()
 * @brief    Check which video process mode we must use, depending on the output CTS.
 * @param   pC    (IN/OUT) Internal edit context
 ******************************************************************************
 */
static M4OSA_ERR M4VSS3GPP_intCheckVideoMode(
    M4VSS3GPP_InternalEditContext *pC )
{
    M4OSA_ERR err;
    // Decorrelate input and output encoding timestamp to handle encoder prefetch
    const M4OSA_Int32  t = (M4OSA_Int32)pC->ewc.dInputVidCts;
    /**< Transition duration */
    const M4OSA_Int32 TD = pC->pTransitionList[pC->uiCurrentClip].uiTransitionDuration;

    M4OSA_Int32 iTmp;

    const M4VSS3GPP_EditVideoState previousVstate = pC->Vstate;

    /**
    * Check if Clip1 is on its begin cut, or in an effect zone */
    M4VSS3GPP_intCheckVideoEffects(pC, 1);

    /**
    * Check if we are in the transition with next clip */
    if( ( TD > 0) && (( t - pC->pC1->iVoffset) >= (pC->pC1->iEndTime - TD)) )
    {
        /**
        * We are in a transition */
        pC->Vstate = M4VSS3GPP_kEditVideoState_TRANSITION;
        pC->bTransitionEffect = M4OSA_TRUE;

        /**
        * Open second clip for transition, if not yet opened */
        if( M4OSA_NULL == pC->pC2 )
        {
            pC->pC1->bGetYuvDataFromDecoder = M4OSA_TRUE;

            err = M4VSS3GPP_intOpenClip(pC, &pC->pC2,
                &pC->pClipList[pC->uiCurrentClip + 1]);

            if( M4NO_ERROR != err )
            {
                M4OSA_TRACE1_1(
                    "M4VSS3GPP_intCheckVideoMode: M4VSS3GPP_editOpenClip returns 0x%x!",
                    err);
                return err;
            }

            /**
            * Add current video output CTS to the clip offset
            * (audio output CTS is not yet at the transition, so audio
            *  offset can't be updated yet). */
            // Decorrelate input and output encoding timestamp to handle encoder prefetch
            pC->pC2->iVoffset += (M4OSA_UInt32)pC->ewc.dInputVidCts;

            /**
            * 2005-03-24: BugFix for audio-video synchro:
            * Update transition duration due to the actual video transition beginning time.
            * It will avoid desynchronization when doing the audio transition. */
           // Decorrelate input and output encoding timestamp to handle encoder prefetch
            iTmp = ((M4OSA_Int32)pC->ewc.dInputVidCts)\
             - (pC->pC1->iEndTime - TD + pC->pC1->iVoffset);
            if (iTmp < (M4OSA_Int32)pC->pTransitionList[pC->uiCurrentClip].uiTransitionDuration)
            /**< Test in case of a very short transition */
            {
                pC->pTransitionList[pC->
                    uiCurrentClip].uiTransitionDuration -= iTmp;

                /**
                * Don't forget to also correct the total duration used for the progress bar
                * (it was computed with the original transition duration). */
                pC->ewc.iOutputDuration += iTmp;
            }
            /**< No "else" here because it's hard predict the effect of 0 duration transition...*/
        }

        /**
        * Check effects for clip2 */
        M4VSS3GPP_intCheckVideoEffects(pC, 2);
    }
    else
    {
        /**
        * We are not in a transition */
        pC->bTransitionEffect = M4OSA_FALSE;

        /* If there is an effect we go to decode/encode mode */
        if((pC->nbActiveEffects > 0) || (pC->nbActiveEffects1 > 0) ||
            (pC->pC1->pSettings->FileType ==
             M4VIDEOEDITING_kFileType_ARGB8888) ||
            (pC->pC1->pSettings->bTranscodingRequired == M4OSA_TRUE)) {
            pC->Vstate = M4VSS3GPP_kEditVideoState_DECODE_ENCODE;
        }
        /* We do a begin cut, except if already done (time is not progressing because we want
        to catch all P-frames after the cut) */
        else if( M4OSA_TRUE == pC->bClip1AtBeginCut )
        {
            if(pC->pC1->pSettings->ClipProperties.VideoStreamType == M4VIDEOEDITING_kH264) {
                pC->Vstate = M4VSS3GPP_kEditVideoState_DECODE_ENCODE;
                pC->bEncodeTillEoF = M4OSA_TRUE;
            } else if( ( M4VSS3GPP_kEditVideoState_BEGIN_CUT == previousVstate)
                || (M4VSS3GPP_kEditVideoState_AFTER_CUT == previousVstate) ) {
                pC->Vstate = M4VSS3GPP_kEditVideoState_AFTER_CUT;
            } else {
                pC->Vstate = M4VSS3GPP_kEditVideoState_BEGIN_CUT;
            }
        }
        /* Else we are in default copy/paste mode */
        else
        {
            if( ( M4VSS3GPP_kEditVideoState_BEGIN_CUT == previousVstate)
                || (M4VSS3GPP_kEditVideoState_AFTER_CUT == previousVstate) )
            {
                pC->Vstate = M4VSS3GPP_kEditVideoState_AFTER_CUT;
            }
            else if( pC->bIsMMS == M4OSA_TRUE )
            {
                M4OSA_UInt32 currentBitrate;
                M4OSA_ERR err = M4NO_ERROR;

                /* Do we need to reencode the video to downgrade the bitrate or not ? */
                /* Let's compute the cirrent bitrate of the current edited clip */
                err = pC->pC1->ShellAPI.m_pReader->m_pFctGetOption(
                    pC->pC1->pReaderContext,
                    M4READER_kOptionID_Bitrate, &currentBitrate);

                if( err != M4NO_ERROR )
                {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_intCheckVideoMode:\
                        Error when getting next bitrate of edited clip: 0x%x",
                        err);
                    return err;
                }

                /* Remove audio bitrate */
                currentBitrate -= 12200;

                /* Test if we go into copy/paste mode or into decode/encode mode */
                if( currentBitrate > pC->uiMMSVideoBitrate )
                {
                    pC->Vstate = M4VSS3GPP_kEditVideoState_DECODE_ENCODE;
                }
                else
                {
                    pC->Vstate = M4VSS3GPP_kEditVideoState_READ_WRITE;
                }
            }
            else if(!((pC->m_bClipExternalHasStarted == M4OSA_TRUE) &&
                    (pC->Vstate == M4VSS3GPP_kEditVideoState_DECODE_ENCODE)) &&
                    pC->bEncodeTillEoF == M4OSA_FALSE)
            {
                /**
                 * Test if we go into copy/paste mode or into decode/encode mode
                 * If an external effect has been applied on the current clip
                 * then continue to be in decode/encode mode till end of
                 * clip to avoid H.264 distortion.
                 */
                pC->Vstate = M4VSS3GPP_kEditVideoState_READ_WRITE;
            }
        }
    }

    /**
    * Check if we create an encoder */
    if( ( ( M4VSS3GPP_kEditVideoState_READ_WRITE == previousVstate)
        || (M4VSS3GPP_kEditVideoState_AFTER_CUT
        == previousVstate)) /**< read mode */
        && (( M4VSS3GPP_kEditVideoState_DECODE_ENCODE == pC->Vstate)
        || (M4VSS3GPP_kEditVideoState_BEGIN_CUT == pC->Vstate)
        || (M4VSS3GPP_kEditVideoState_TRANSITION
        == pC->Vstate)) /**< encode mode */
        && pC->bIsMMS == M4OSA_FALSE )
    {
        /**
        * Create the encoder, if not created already*/
        if (pC->ewc.encoderState == M4VSS3GPP_kNoEncoder) {
            err = M4VSS3GPP_intCreateVideoEncoder(pC);

            if( M4NO_ERROR != err )
            {
                M4OSA_TRACE1_1(
                    "M4VSS3GPP_intCheckVideoMode: M4VSS3GPP_intCreateVideoEncoder \
                     returns 0x%x!", err);
                return err;
            }
        }
    }
    else if( pC->bIsMMS == M4OSA_TRUE && pC->ewc.pEncContext == M4OSA_NULL )
    {
        /**
        * Create the encoder */
        err = M4VSS3GPP_intCreateVideoEncoder(pC);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intCheckVideoMode: M4VSS3GPP_intCreateVideoEncoder returns 0x%x!",
                err);
            return err;
        }
    }

    /**
    * When we go from filtering to read/write, we must act like a begin cut,
    * because the last filtered image may be different than the original image. */
    else if( ( ( M4VSS3GPP_kEditVideoState_DECODE_ENCODE == previousVstate)
        || (M4VSS3GPP_kEditVideoState_TRANSITION
        == previousVstate)) /**< encode mode */
        && (M4VSS3GPP_kEditVideoState_READ_WRITE == pC->Vstate) /**< read mode */
        && (pC->bEncodeTillEoF == M4OSA_FALSE) )
    {
        pC->Vstate = M4VSS3GPP_kEditVideoState_BEGIN_CUT;
    }

    /**
    * Check if we destroy an encoder */
    else if( ( ( M4VSS3GPP_kEditVideoState_DECODE_ENCODE == previousVstate)
        || (M4VSS3GPP_kEditVideoState_BEGIN_CUT == previousVstate)
        || (M4VSS3GPP_kEditVideoState_TRANSITION
        == previousVstate)) /**< encode mode */
        && (( M4VSS3GPP_kEditVideoState_READ_WRITE == pC->Vstate)
        || (M4VSS3GPP_kEditVideoState_AFTER_CUT
        == pC->Vstate)) /**< read mode */
        && pC->bIsMMS == M4OSA_FALSE )
    {
        /**
        * Destroy the previously created encoder */
        err = M4VSS3GPP_intDestroyVideoEncoder(pC);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intCheckVideoMode: M4VSS3GPP_intDestroyVideoEncoder returns 0x%x!",
                err);
            return err;
        }
    }

    /**
    * Return with no error */
    M4OSA_TRACE3_0("M4VSS3GPP_intCheckVideoMode: returning M4NO_ERROR");
    return M4NO_ERROR;
}

/******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intStartAU()
 * @brief    StartAU writer-like interface used for the VSS 3GPP only
 * @note
 * @param    pContext: (IN) It is the VSS 3GPP context in our case
 * @param    streamID: (IN) Id of the stream to which the Access Unit is related.
 * @param    pAU:      (IN/OUT) Access Unit to be prepared.
 * @return    M4NO_ERROR: there is no error
 ******************************************************************************
 */
M4OSA_ERR M4VSS3GPP_intStartAU( M4WRITER_Context pContext,
                               M4SYS_StreamID streamID, M4SYS_AccessUnit *pAU )
{
    M4OSA_ERR err;
    M4OSA_UInt32 uiMaxAuSize;

    /**
    * Given context is actually the VSS3GPP context */
    M4VSS3GPP_InternalEditContext *pC =
        (M4VSS3GPP_InternalEditContext *)pContext;

    /**
    * Get the output AU to write into */
    err = pC->ShellAPI.pWriterDataFcts->pStartAU(pC->ewc.p3gpWriterContext,
        M4VSS3GPP_WRITER_VIDEO_STREAM_ID, pAU);

    if( M4NO_ERROR != err )
    {
        M4OSA_TRACE1_1(
            "M4VSS3GPP_intStartAU: pWriterDataFcts->pStartAU(Video) returns 0x%x!",
            err);
        return err;
    }

    /**
    *    Return */
    M4OSA_TRACE3_0("M4VSS3GPP_intStartAU: returning M4NO_ERROR");
    return M4NO_ERROR;
}

/******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intProcessAU()
 * @brief    ProcessAU writer-like interface used for the VSS 3GPP only
 * @note
 * @param    pContext: (IN) It is the VSS 3GPP context in our case
 * @param    streamID: (IN) Id of the stream to which the Access Unit is related.
 * @param    pAU:      (IN/OUT) Access Unit to be written
 * @return    M4NO_ERROR: there is no error
 ******************************************************************************
 */
M4OSA_ERR M4VSS3GPP_intProcessAU( M4WRITER_Context pContext,
                                 M4SYS_StreamID streamID, M4SYS_AccessUnit *pAU )
{
    M4OSA_ERR err;

    /**
    * Given context is actually the VSS3GPP context */
    M4VSS3GPP_InternalEditContext *pC =
        (M4VSS3GPP_InternalEditContext *)pContext;

    /**
    * Fix the encoded AU time */
    // Decorrelate input and output encoding timestamp to handle encoder prefetch
    pC->ewc.dOutputVidCts = pAU->CTS;
    /**
    * Update time info for the Counter Time System to be equal to the bit-stream time */
    M4VSS3GPP_intUpdateTimeInfo(pC, pAU);

    /**
    * Write the AU */
    err = pC->ShellAPI.pWriterDataFcts->pProcessAU(pC->ewc.p3gpWriterContext,
        M4VSS3GPP_WRITER_VIDEO_STREAM_ID, pAU);

    if( M4NO_ERROR != err )
    {
        M4OSA_TRACE1_1(
            "M4VSS3GPP_intProcessAU: pWriterDataFcts->pProcessAU(Video) returns 0x%x!",
            err);
        return err;
    }

    /**
    *    Return */
    M4OSA_TRACE3_0("M4VSS3GPP_intProcessAU: returning M4NO_ERROR");
    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intVPP()
 * @brief    We implement our own VideoPreProcessing function
 * @note    It is called by the video encoder
 * @param    pContext    (IN) VPP context, which actually is the VSS 3GPP context in our case
 * @param    pPlaneIn    (IN)
 * @param    pPlaneOut    (IN/OUT) Pointer to an array of 3 planes that will contain the output
 *                                  YUV420 image
 * @return    M4NO_ERROR:    No error
 ******************************************************************************
 */
M4OSA_ERR M4VSS3GPP_intVPP( M4VPP_Context pContext, M4VIFI_ImagePlane *pPlaneIn,
                           M4VIFI_ImagePlane *pPlaneOut )
{
    M4OSA_ERR err = M4NO_ERROR;
    M4_MediaTime ts;
    M4VIFI_ImagePlane *pTmp = M4OSA_NULL;
    M4VIFI_ImagePlane *pLastDecodedFrame = M4OSA_NULL ;
    M4VIFI_ImagePlane *pDecoderRenderFrame = M4OSA_NULL;
    M4VIFI_ImagePlane pTemp1[3],pTemp2[3];
    M4VIFI_ImagePlane pTempPlaneClip1[3],pTempPlaneClip2[3];
    M4OSA_UInt32  i = 0, yuvFrameWidth = 0, yuvFrameHeight = 0;
    M4OSA_Bool bSkipFrameEffect = M4OSA_FALSE;
    /**
    * VPP context is actually the VSS3GPP context */
    M4VSS3GPP_InternalEditContext *pC =
        (M4VSS3GPP_InternalEditContext *)pContext;

    memset((void *)pTemp1, 0, 3*sizeof(M4VIFI_ImagePlane));
    memset((void *)pTemp2, 0, 3*sizeof(M4VIFI_ImagePlane));
    memset((void *)pTempPlaneClip1, 0, 3*sizeof(M4VIFI_ImagePlane));
    memset((void *)pTempPlaneClip2, 0, 3*sizeof(M4VIFI_ImagePlane));

    /**
    * Reset VPP error remembered in context */
    pC->ewc.VppError = M4NO_ERROR;

    /**
    * At the end of the editing, we may be called when no more clip is loaded.
    * (because to close the encoder properly it must be stepped one or twice...) */
    if( M4OSA_NULL == pC->pC1 )
    {
        /**
        * We must fill the input of the encoder with a dummy image, because
        * encoding noise leads to a huge video AU, and thus a writer buffer overflow. */
        memset((void *)pPlaneOut[0].pac_data,0,
            pPlaneOut[0].u_stride * pPlaneOut[0].u_height);
        memset((void *)pPlaneOut[1].pac_data,0,
            pPlaneOut[1].u_stride * pPlaneOut[1].u_height);
        memset((void *)pPlaneOut[2].pac_data,0,
            pPlaneOut[2].u_stride * pPlaneOut[2].u_height);

        M4OSA_TRACE3_0("M4VSS3GPP_intVPP: returning M4NO_ERROR (abort)");
        return M4NO_ERROR;
    }

    /**
    **************** Transition case ****************/
    if( M4OSA_TRUE == pC->bTransitionEffect )
    {

        err = M4VSS3GPP_intAllocateYUV420(pTemp1, pC->ewc.uiVideoWidth,
                                          pC->ewc.uiVideoHeight);
        if (M4NO_ERROR != err)
        {
            M4OSA_TRACE1_1("M4VSS3GPP_intVPP: M4VSS3GPP_intAllocateYUV420(1) returns 0x%x, \
                           returning M4NO_ERROR", err);
            pC->ewc.VppError = err;
            return M4NO_ERROR; /**< Return no error to the encoder core
                               (else it may leak in some situations...) */
        }

        err = M4VSS3GPP_intAllocateYUV420(pTemp2, pC->ewc.uiVideoWidth,
                                          pC->ewc.uiVideoHeight);
        if (M4NO_ERROR != err)
        {
            M4OSA_TRACE1_1("M4VSS3GPP_intVPP: M4VSS3GPP_intAllocateYUV420(2) returns 0x%x, \
                           returning M4NO_ERROR", err);
            pC->ewc.VppError = err;
            return M4NO_ERROR; /**< Return no error to the encoder core
                              (else it may leak in some situations...) */
        }

        err = M4VSS3GPP_intAllocateYUV420(pC->yuv1, pC->ewc.uiVideoWidth,
            pC->ewc.uiVideoHeight);
        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intVPP: M4VSS3GPP_intAllocateYUV420(3) returns 0x%x,\
                returning M4NO_ERROR",
                err);
            pC->ewc.VppError = err;
            return
                M4NO_ERROR; /**< Return no error to the encoder core
                            (else it may leak in some situations...) */
        }

        err = M4VSS3GPP_intAllocateYUV420(pC->yuv2, pC->ewc.uiVideoWidth,
            pC->ewc.uiVideoHeight);
        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intVPP: M4VSS3GPP_intAllocateYUV420(4) returns 0x%x,\
                returning M4NO_ERROR",
                err);
            pC->ewc.VppError = err;
            return
                M4NO_ERROR; /**< Return no error to the encoder core
                            (else it may leak in some situations...) */
        }

        err = M4VSS3GPP_intAllocateYUV420(pC->yuv3, pC->ewc.uiVideoWidth,
            pC->ewc.uiVideoHeight);
        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intVPP: M4VSS3GPP_intAllocateYUV420(3) returns 0x%x,\
                returning M4NO_ERROR",
                err);
            pC->ewc.VppError = err;
            return
                M4NO_ERROR; /**< Return no error to the encoder core
                            (else it may leak in some situations...) */
        }

        /**
        * Compute the time in the clip1 base: ts = to - Offset */
        // Decorrelate input and output encoding timestamp to handle encoder prefetch
        ts = pC->ewc.dInputVidCts - pC->pC1->iVoffset;

        /**
        * Render Clip1 */
        if( pC->pC1->isRenderDup == M4OSA_FALSE )
        {
            err = M4VSS3GPP_intRenderFrameWithEffect(pC, pC->pC1, ts, M4OSA_TRUE,
                                                pTempPlaneClip1, pTemp1,
                                                pPlaneOut);
            if ((M4NO_ERROR != err) &&
                 (M4WAR_VIDEORENDERER_NO_NEW_FRAME != err)) {
                M4OSA_TRACE1_1("M4VSS3GPP_intVPP: \
                    M4VSS3GPP_intRenderFrameWithEffect returns 0x%x", err);
                pC->ewc.VppError = err;
                /** Return no error to the encoder core
                  * else it may leak in some situations.*/
                return M4NO_ERROR;
            }
        }
        if ((pC->pC1->isRenderDup == M4OSA_TRUE) ||
             (M4WAR_VIDEORENDERER_NO_NEW_FRAME == err)) {
            pTmp = pC->yuv1;
            if (pC->pC1->lastDecodedPlane != M4OSA_NULL) {
                /* Copy last decoded plane to output plane */
                memcpy((void *)pTmp[0].pac_data,
                    (void *)pC->pC1->lastDecodedPlane[0].pac_data,
                    (pTmp[0].u_height * pTmp[0].u_width));
                memcpy((void *)pTmp[1].pac_data,
                    (void *)pC->pC1->lastDecodedPlane[1].pac_data,
                    (pTmp[1].u_height * pTmp[1].u_width));
                memcpy((void *)pTmp[2].pac_data,
                    (void *)pC->pC1->lastDecodedPlane[2].pac_data,
                    (pTmp[2].u_height * pTmp[2].u_width));
            } else {
                err = M4VSS3GPP_ERR_NO_VALID_VID_FRAME;
                M4OSA_TRACE1_3("Can not find an input frame. Set error 0x%x in %s (%d)",
                   err, __FILE__, __LINE__);
                pC->ewc.VppError = err;
                return M4NO_ERROR;
            }
            pC->pC1->lastDecodedPlane = pTmp;
        }

        /**
        * Compute the time in the clip2 base: ts = to - Offset */
        // Decorrelate input and output encoding timestamp to handle encoder prefetch
        ts = pC->ewc.dInputVidCts - pC->pC2->iVoffset;
        /**
        * Render Clip2 */
        if( pC->pC2->isRenderDup == M4OSA_FALSE )
        {

            err = M4VSS3GPP_intRenderFrameWithEffect(pC, pC->pC2, ts, M4OSA_FALSE,
                                                pTempPlaneClip2, pTemp2,
                                                pPlaneOut);
            if ((M4NO_ERROR != err) &&
                 (M4WAR_VIDEORENDERER_NO_NEW_FRAME != err)) {
                M4OSA_TRACE1_1("M4VSS3GPP_intVPP: \
                    M4VSS3GPP_intRenderFrameWithEffect returns 0x%x", err);
                pC->ewc.VppError = err;
                /** Return no error to the encoder core
                  * else it may leak in some situations.*/
                return M4NO_ERROR;
            }
        }
        if ((pC->pC2->isRenderDup == M4OSA_TRUE) ||
             (M4WAR_VIDEORENDERER_NO_NEW_FRAME == err)) {
            pTmp = pC->yuv2;
            if (pC->pC2->lastDecodedPlane != M4OSA_NULL) {
                /* Copy last decoded plane to output plane */
                memcpy((void *)pTmp[0].pac_data,
                    (void *)pC->pC2->lastDecodedPlane[0].pac_data,
                    (pTmp[0].u_height * pTmp[0].u_width));
                memcpy((void *)pTmp[1].pac_data,
                    (void *)pC->pC2->lastDecodedPlane[1].pac_data,
                    (pTmp[1].u_height * pTmp[1].u_width));
                memcpy((void *)pTmp[2].pac_data,
                    (void *)pC->pC2->lastDecodedPlane[2].pac_data,
                    (pTmp[2].u_height * pTmp[2].u_width));
            } else {
                err = M4VSS3GPP_ERR_NO_VALID_VID_FRAME;
                M4OSA_TRACE1_3("Can not find an input frame. Set error 0x%x in %s (%d)",
                   err, __FILE__, __LINE__);
                pC->ewc.VppError = err;
                return M4NO_ERROR;
            }
            pC->pC2->lastDecodedPlane = pTmp;
        }


        pTmp = pPlaneOut;
        err = M4VSS3GPP_intVideoTransition(pC, pTmp);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intVPP: M4VSS3GPP_intVideoTransition returns 0x%x,\
                returning M4NO_ERROR",
                err);
            pC->ewc.VppError = err;
            return  M4NO_ERROR; /**< Return no error to the encoder core
                                (else it may leak in some situations...) */
        }
        for (i=0; i < 3; i++)
        {
            if(pTempPlaneClip2[i].pac_data != M4OSA_NULL) {
                free(pTempPlaneClip2[i].pac_data);
                pTempPlaneClip2[i].pac_data = M4OSA_NULL;
            }

            if(pTempPlaneClip1[i].pac_data != M4OSA_NULL) {
                free(pTempPlaneClip1[i].pac_data);
                pTempPlaneClip1[i].pac_data = M4OSA_NULL;
            }

            if (pTemp2[i].pac_data != M4OSA_NULL) {
                free(pTemp2[i].pac_data);
                pTemp2[i].pac_data = M4OSA_NULL;
            }

            if (pTemp1[i].pac_data != M4OSA_NULL) {
                free(pTemp1[i].pac_data);
                pTemp1[i].pac_data = M4OSA_NULL;
            }
        }
    }
    /**
    **************** No Transition case ****************/
    else
    {
        M4OSA_TRACE3_0("M4VSS3GPP_intVPP: NO transition case");
        /**
        * Compute the time in the clip base: ts = to - Offset */
        ts = pC->ewc.dInputVidCts - pC->pC1->iVoffset;
        pC->bIssecondClip = M4OSA_FALSE;
        /**
        * Render */
        if (pC->pC1->isRenderDup == M4OSA_FALSE) {
            M4OSA_TRACE3_0("M4VSS3GPP_intVPP: renderdup false");
            /**
            *   Check if resizing is needed */
            if (M4OSA_NULL != pC->pC1->m_pPreResizeFrame) {
                if ((pC->pC1->pSettings->FileType ==
                            M4VIDEOEDITING_kFileType_ARGB8888) &&
                        (pC->nbActiveEffects == 0) &&
                        (pC->pC1->bGetYuvDataFromDecoder == M4OSA_FALSE)) {
                    err = pC->pC1->ShellAPI.m_pVideoDecoder->m_pFctSetOption(
                              pC->pC1->pViDecCtxt,
                              M4DECODER_kOptionID_EnableYuvWithEffect,
                              (M4OSA_DataOption)M4OSA_TRUE);
                    if (M4NO_ERROR == err ) {
                        err = pC->pC1->ShellAPI.m_pVideoDecoder->m_pFctRender(
                                  pC->pC1->pViDecCtxt, &ts,
                                  pPlaneOut, M4OSA_TRUE);
                    }
                } else {
                    if (pC->pC1->pSettings->FileType ==
                            M4VIDEOEDITING_kFileType_ARGB8888) {
                        err = pC->pC1->ShellAPI.m_pVideoDecoder->m_pFctSetOption(
                                  pC->pC1->pViDecCtxt,
                                  M4DECODER_kOptionID_EnableYuvWithEffect,
                                  (M4OSA_DataOption)M4OSA_FALSE);
                    }
                    if (M4NO_ERROR == err) {
                        err = pC->pC1->ShellAPI.m_pVideoDecoder->m_pFctRender(
                                  pC->pC1->pViDecCtxt, &ts,
                                  pC->pC1->m_pPreResizeFrame, M4OSA_TRUE);
                    }
                }
                if (M4NO_ERROR != err) {
                    M4OSA_TRACE1_1("M4VSS3GPP_intVPP: \
                        m_pFctRender() returns error 0x%x", err);
                    pC->ewc.VppError = err;
                    return M4NO_ERROR;
                }
                if (pC->pC1->pSettings->FileType !=
                        M4VIDEOEDITING_kFileType_ARGB8888) {
                    if (0 != pC->pC1->pSettings->ClipProperties.videoRotationDegrees) {
                        // Save width and height of un-rotated frame
                        yuvFrameWidth = pC->pC1->m_pPreResizeFrame[0].u_width;
                        yuvFrameHeight = pC->pC1->m_pPreResizeFrame[0].u_height;
                        err = M4VSS3GPP_intRotateVideo(pC->pC1->m_pPreResizeFrame,
                                pC->pC1->pSettings->ClipProperties.videoRotationDegrees);
                        if (M4NO_ERROR != err) {
                            M4OSA_TRACE1_1("M4VSS3GPP_intVPP: \
                                rotateVideo() returns error 0x%x", err);
                            pC->ewc.VppError = err;
                            return M4NO_ERROR;
                        }
                    }
                }

                if (pC->nbActiveEffects > 0) {
                    pC->pC1->bGetYuvDataFromDecoder = M4OSA_TRUE;
                    /**
                    * If we do modify the image, we need an intermediate
                    * image plane */
                    err = M4VSS3GPP_intAllocateYUV420(pTemp1,
                            pC->pC1->m_pPreResizeFrame[0].u_width,
                            pC->pC1->m_pPreResizeFrame[0].u_height);
                    if (M4NO_ERROR != err) {
                        M4OSA_TRACE1_1("M4VSS3GPP_intVPP: \
                            M4VSS3GPP_intAllocateYUV420 error 0x%x", err);
                        pC->ewc.VppError = err;
                        return M4NO_ERROR;
                    }
                    /* If video frame need to be resized, then apply the overlay after
                     * the frame was rendered with rendering mode.
                     * Here skip the framing(overlay) effect when applying video Effect. */
                    bSkipFrameEffect = M4OSA_TRUE;
                    err = M4VSS3GPP_intApplyVideoEffect(pC,
                            pC->pC1->m_pPreResizeFrame, pTemp1, bSkipFrameEffect);
                    if (M4NO_ERROR != err) {
                        M4OSA_TRACE1_1("M4VSS3GPP_intVPP: \
                            M4VSS3GPP_intApplyVideoEffect() error 0x%x", err);
                        pC->ewc.VppError = err;
                        return M4NO_ERROR;
                    }
                    pDecoderRenderFrame= pTemp1;

                } else {
                    pDecoderRenderFrame = pC->pC1->m_pPreResizeFrame;
                }
                /* Prepare overlay temporary buffer if overlay exist */
                if (pC->bClip1ActiveFramingEffect) {
                    err = M4VSS3GPP_intAllocateYUV420(pTemp2,
                        pPlaneOut[0].u_width, pPlaneOut[0].u_height);
                    if (M4NO_ERROR != err) {
                        M4OSA_TRACE1_1("M4VSS3GPP_intVPP: M4VSS3GPP_intAllocateYUV420 \
                            returns 0x%x, returning M4NO_ERROR", err);
                        pC->ewc.VppError = err;
                        return M4NO_ERROR;
                    }
                    pTmp = pTemp2;
                } else {
                    pTmp = pPlaneOut;
                }

                /* Do rendering mode. */
                if ((pC->pC1->bGetYuvDataFromDecoder == M4OSA_TRUE) ||
                    (pC->pC1->pSettings->FileType !=
                        M4VIDEOEDITING_kFileType_ARGB8888)) {

                    err = M4VSS3GPP_intApplyRenderingMode(pC,
                              pC->pC1->pSettings->xVSS.MediaRendering,
                              pDecoderRenderFrame, pTmp);
                    if (M4NO_ERROR != err) {
                        M4OSA_TRACE1_1("M4VSS3GPP_intVPP: \
                            M4VSS3GPP_intApplyRenderingMode) error 0x%x ", err);
                        pC->ewc.VppError = err;
                        return M4NO_ERROR;
                    }
                }

                /* Apply overlay if overlay is exist */
                if (pC->bClip1ActiveFramingEffect) {
                    pDecoderRenderFrame = pTmp;
                    pTmp = pPlaneOut;
                    err = M4VSS3GPP_intApplyVideoOverlay(pC,
                        pDecoderRenderFrame, pTmp);
                    if (M4NO_ERROR != err) {
                        M4OSA_TRACE1_1("M4VSS3GPP_intVPP: \
                            M4VSS3GPP_intApplyVideoOverlay) error 0x%x ", err);
                        pC->ewc.VppError = err;
                        return M4NO_ERROR;
                    }
                }

                if ((pC->pC1->pSettings->FileType ==
                        M4VIDEOEDITING_kFileType_ARGB8888) &&
                    (pC->nbActiveEffects == 0) &&
                    (pC->pC1->bGetYuvDataFromDecoder == M4OSA_TRUE)) {

                    err = pC->pC1->ShellAPI.m_pVideoDecoder->m_pFctSetOption(
                              pC->pC1->pViDecCtxt,
                              M4DECODER_kOptionID_YuvWithEffectNonContiguous,
                              (M4OSA_DataOption)pTmp);
                    if (M4NO_ERROR != err) {
                        pC->ewc.VppError = err;
                        return M4NO_ERROR;
                    }
                    pC->pC1->bGetYuvDataFromDecoder = M4OSA_FALSE;
                }

                // Reset original width and height for resize frame plane
                if (0 != pC->pC1->pSettings->ClipProperties.videoRotationDegrees &&
                    180 != pC->pC1->pSettings->ClipProperties.videoRotationDegrees) {

                    M4VSS3GPP_intSetYUV420Plane(pC->pC1->m_pPreResizeFrame,
                                                yuvFrameWidth, yuvFrameHeight);
                }
            }
            else
            {
                M4OSA_TRACE3_0("M4VSS3GPP_intVPP: NO resize required");
                if (pC->nbActiveEffects > 0) {
                    /** If we do modify the image, we need an
                     * intermediate image plane */
                    err = M4VSS3GPP_intAllocateYUV420(pTemp1,
                              pC->ewc.uiVideoWidth,
                              pC->ewc.uiVideoHeight);
                    if (M4NO_ERROR != err) {
                        pC->ewc.VppError = err;
                        return M4NO_ERROR;
                    }
                    pDecoderRenderFrame = pTemp1;
                }
                else {
                    pDecoderRenderFrame = pPlaneOut;
                }

                pTmp = pPlaneOut;
                err = pC->pC1->ShellAPI.m_pVideoDecoder->m_pFctRender(
                          pC->pC1->pViDecCtxt, &ts,
                          pDecoderRenderFrame, M4OSA_TRUE);
                if (M4NO_ERROR != err) {
                    pC->ewc.VppError = err;
                    return M4NO_ERROR;
                }

                if (pC->nbActiveEffects > 0) {
                    /* Here we do not skip the overlay effect since
                     * overlay and video frame are both of same resolution */
                    bSkipFrameEffect = M4OSA_FALSE;
                    err = M4VSS3GPP_intApplyVideoEffect(pC,
                              pDecoderRenderFrame,pPlaneOut,bSkipFrameEffect);
                    }
                    if (M4NO_ERROR != err) {
                        pC->ewc.VppError = err;
                        return M4NO_ERROR;
                    }
            }
            pC->pC1->lastDecodedPlane = pTmp;
            pC->pC1->iVideoRenderCts = (M4OSA_Int32)ts;

        } else {
            M4OSA_TRACE3_0("M4VSS3GPP_intVPP: renderdup true");

            if (M4OSA_NULL != pC->pC1->m_pPreResizeFrame) {
                /**
                * Copy last decoded plane to output plane */
                if (pC->pC1->lastDecodedPlane != M4OSA_NULL) {

                    memcpy((void *)pC->pC1->m_pPreResizeFrame[0].pac_data,
                        (void *)pC->pC1->lastDecodedPlane[0].pac_data,
                        (pC->pC1->m_pPreResizeFrame[0].u_height * \
                         pC->pC1->m_pPreResizeFrame[0].u_width));

                    memcpy((void *)pC->pC1->m_pPreResizeFrame[1].pac_data,
                        (void *)pC->pC1->lastDecodedPlane[1].pac_data,
                        (pC->pC1->m_pPreResizeFrame[1].u_height * \
                         pC->pC1->m_pPreResizeFrame[1].u_width));

                    memcpy((void *)pC->pC1->m_pPreResizeFrame[2].pac_data,
                        (void *)pC->pC1->lastDecodedPlane[2].pac_data,
                        (pC->pC1->m_pPreResizeFrame[2].u_height * \
                         pC->pC1->m_pPreResizeFrame[2].u_width));
                } else {
                    err = M4VSS3GPP_ERR_NO_VALID_VID_FRAME;
                    M4OSA_TRACE1_3("Can not find an input frame. Set error 0x%x in %s (%d)",
                        err, __FILE__, __LINE__);
                    pC->ewc.VppError = err;
                    return M4NO_ERROR;
                }

                if(pC->nbActiveEffects > 0) {
                    /**
                    * If we do modify the image, we need an
                    * intermediate image plane */
                    err = M4VSS3GPP_intAllocateYUV420(pTemp1,
                              pC->pC1->m_pPreResizeFrame[0].u_width,
                              pC->pC1->m_pPreResizeFrame[0].u_height);
                    if (M4NO_ERROR != err) {
                        pC->ewc.VppError = err;
                        return M4NO_ERROR;
                    }
                    /* If video frame need to be resized, then apply the overlay after
                     * the frame was rendered with rendering mode.
                     * Here skip the framing(overlay) effect when applying video Effect. */
                    bSkipFrameEffect = M4OSA_TRUE;
                    err = M4VSS3GPP_intApplyVideoEffect(pC,
                              pC->pC1->m_pPreResizeFrame,pTemp1, bSkipFrameEffect);
                    if (M4NO_ERROR != err) {
                        pC->ewc.VppError = err;
                        return M4NO_ERROR;
                    }
                    pDecoderRenderFrame= pTemp1;
                } else {
                    pDecoderRenderFrame = pC->pC1->m_pPreResizeFrame;
                }
                /* Prepare overlay temporary buffer if overlay exist */
                if (pC->bClip1ActiveFramingEffect) {
                    err = M4VSS3GPP_intAllocateYUV420(
                        pTemp2, pC->ewc.uiVideoWidth, pC->ewc.uiVideoHeight);
                    if (M4NO_ERROR != err) {
                        M4OSA_TRACE1_1("M4VSS3GPP_intVPP: M4VSS3GPP_intAllocateYUV420 \
                            returns 0x%x, returning M4NO_ERROR", err);
                        pC->ewc.VppError = err;
                        return M4NO_ERROR;
                    }
                    pTmp = pTemp2;
                } else {
                    pTmp = pPlaneOut;
                }
                /* Do rendering mode */
                err = M4VSS3GPP_intApplyRenderingMode(pC,
                          pC->pC1->pSettings->xVSS.MediaRendering,
                          pDecoderRenderFrame, pTmp);
                if (M4NO_ERROR != err) {
                    pC->ewc.VppError = err;
                    return M4NO_ERROR;
                }
                /* Apply overlay if overlay is exist */
                pTmp = pPlaneOut;
                if (pC->bClip1ActiveFramingEffect) {
                    err = M4VSS3GPP_intApplyVideoOverlay(pC,
                        pTemp2, pTmp);
                    if (M4NO_ERROR != err) {
                        M4OSA_TRACE1_1("M4VSS3GPP_intVPP: \
                            M4VSS3GPP_intApplyRenderingMode) error 0x%x ", err);
                        pC->ewc.VppError = err;
                        return M4NO_ERROR;
                    }
                }
            } else {

                err = M4VSS3GPP_intAllocateYUV420(pTemp1,
                          pC->ewc.uiVideoWidth,
                          pC->ewc.uiVideoHeight);
                if (M4NO_ERROR != err) {
                    pC->ewc.VppError = err;
                    return M4NO_ERROR;
                }
                /**
                 * Copy last decoded plane to output plane */
                if (pC->pC1->lastDecodedPlane != M4OSA_NULL &&
                    pLastDecodedFrame != M4OSA_NULL) {
                    memcpy((void *)pLastDecodedFrame[0].pac_data,
                        (void *)pC->pC1->lastDecodedPlane[0].pac_data,
                        (pLastDecodedFrame[0].u_height * pLastDecodedFrame[0].u_width));

                    memcpy((void *)pLastDecodedFrame[1].pac_data,
                        (void *)pC->pC1->lastDecodedPlane[1].pac_data,
                        (pLastDecodedFrame[1].u_height * pLastDecodedFrame[1].u_width));

                    memcpy((void *)pLastDecodedFrame[2].pac_data,
                        (void *)pC->pC1->lastDecodedPlane[2].pac_data,
                        (pLastDecodedFrame[2].u_height * pLastDecodedFrame[2].u_width));
                } else {
                    err = M4VSS3GPP_ERR_NO_VALID_VID_FRAME;
                    M4OSA_TRACE1_3("Can not find an input frame. Set error 0x%x in %s (%d)",
                        err, __FILE__, __LINE__);
                    pC->ewc.VppError = err;
                    return M4NO_ERROR;
                }

                pTmp = pPlaneOut;
                /**
                * Check if there is a effect */
                if(pC->nbActiveEffects > 0) {
                    /* Here we do not skip the overlay effect since
                     * overlay and video are both of same resolution */
                    bSkipFrameEffect = M4OSA_FALSE;
                    err = M4VSS3GPP_intApplyVideoEffect(pC,
                              pLastDecodedFrame, pTmp,bSkipFrameEffect);
                    if (M4NO_ERROR != err) {
                        pC->ewc.VppError = err;
                        return M4NO_ERROR;
                    }
                }
            }
            pC->pC1->lastDecodedPlane = pTmp;
        }

        M4OSA_TRACE3_1("M4VSS3GPP_intVPP: Rendered at CTS %.3f", ts);

        for (i=0; i<3; i++) {
            if (pTemp1[i].pac_data != M4OSA_NULL) {
                free(pTemp1[i].pac_data);
                pTemp1[i].pac_data = M4OSA_NULL;
            }
        }
        for (i=0; i<3; i++) {
            if (pTemp2[i].pac_data != M4OSA_NULL) {
                free(pTemp2[i].pac_data);
                pTemp2[i].pac_data = M4OSA_NULL;
            }
        }
    }

    /**
    *    Return */
    M4OSA_TRACE3_0("M4VSS3GPP_intVPP: returning M4NO_ERROR");
    return M4NO_ERROR;
}
/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intApplyVideoOverlay()
 * @brief    Apply video overlay from pPlaneIn to pPlaneOut
 * @param    pC               (IN/OUT) Internal edit context
 * @param    pInputPlanes    (IN) Input raw YUV420 image
 * @param    pOutputPlanes   (IN/OUT) Output raw YUV420 image
 * @return   M4NO_ERROR:    No error
 ******************************************************************************
 */
static M4OSA_ERR
M4VSS3GPP_intApplyVideoOverlay (M4VSS3GPP_InternalEditContext *pC,
    M4VIFI_ImagePlane *pPlaneIn, M4VIFI_ImagePlane *pPlaneOut) {

    M4VSS3GPP_ClipContext *pClip;
    M4VSS3GPP_EffectSettings *pFx;
    M4VSS3GPP_ExternalProgress extProgress;
    M4OSA_Double VideoEffectTime;
    M4OSA_Double PercentageDone;
    M4OSA_UInt8 NumActiveEffects =0;
    M4OSA_UInt32 Cts = 0;
    M4OSA_Int32 nextEffectTime;
    M4OSA_Int32 tmp;
    M4OSA_UInt8 i;
    M4OSA_ERR err;

    pClip = pC->pC1;
    if (pC->bIssecondClip == M4OSA_TRUE) {
        NumActiveEffects = pC->nbActiveEffects1;
    } else {
        NumActiveEffects = pC->nbActiveEffects;
    }
    for (i=0; i<NumActiveEffects; i++) {
        if (pC->bIssecondClip == M4OSA_TRUE) {
            pFx = &(pC->pEffectsList[pC->pActiveEffectsList1[i]]);
            /* Compute how far from the beginning of the effect we are, in clip-base time. */
            // Decorrelate input and output encoding timestamp to handle encoder prefetch
            VideoEffectTime = ((M4OSA_Int32)pC->ewc.dInputVidCts) +
                pC->pTransitionList[pC->uiCurrentClip].uiTransitionDuration - pFx->uiStartTime;
        } else {
            pFx = &(pC->pEffectsList[pC->pActiveEffectsList[i]]);
            /* Compute how far from the beginning of the effect we are, in clip-base time. */
            // Decorrelate input and output encoding timestamp to handle encoder prefetch
            VideoEffectTime = ((M4OSA_Int32)pC->ewc.dInputVidCts) - pFx->uiStartTime;
        }
        /* Do the framing(overlay) effect only,
         * skip other color effect which had been applied */
        if (pFx->xVSS.pFramingBuffer == M4OSA_NULL) {
            continue;
        }

        /* To calculate %, substract timeIncrement because effect should finish
         * on the last frame which is presented from CTS = eof-timeIncrement till CTS = eof */
        PercentageDone = VideoEffectTime / ((M4OSA_Float)pFx->uiDuration);

        if (PercentageDone < 0.0) {
            PercentageDone = 0.0;
        }
        if (PercentageDone > 1.0) {
            PercentageDone = 1.0;
        }
        /**
        * Compute where we are in the effect (scale is 0->1000) */
        tmp = (M4OSA_Int32)(PercentageDone * 1000);

        /**
        * Set the progress info provided to the external function */
        extProgress.uiProgress = (M4OSA_UInt32)tmp;
        // Decorrelate input and output encoding timestamp to handle encoder prefetch
        extProgress.uiOutputTime = (M4OSA_UInt32)pC->ewc.dInputVidCts;
        extProgress.uiClipTime = extProgress.uiOutputTime - pClip->iVoffset;
        extProgress.bIsLast = M4OSA_FALSE;
        // Decorrelate input and output encoding timestamp to handle encoder prefetch
        nextEffectTime = (M4OSA_Int32)(pC->ewc.dInputVidCts \
            + pC->dOutputFrameDuration);
        if (nextEffectTime >= (M4OSA_Int32)(pFx->uiStartTime + pFx->uiDuration)) {
            extProgress.bIsLast = M4OSA_TRUE;
        }
        err = pFx->ExtVideoEffectFct(pFx->pExtVideoEffectFctCtxt,
            pPlaneIn, pPlaneOut, &extProgress,
            pFx->VideoEffectType - M4VSS3GPP_kVideoEffectType_External);

        if (M4NO_ERROR != err) {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intApplyVideoOverlay: \
                External video effect function returns 0x%x!",
                err);
            return err;
        }
    }

    /**
    *    Return */
    M4OSA_TRACE3_0("M4VSS3GPP_intApplyVideoOverlay: returning M4NO_ERROR");
    return M4NO_ERROR;
}
/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intApplyVideoEffect()
 * @brief    Apply video effect from pPlaneIn to pPlaneOut
 * @param   pC                (IN/OUT) Internal edit context
 * @param   uiClip1orClip2    (IN/OUT) 1 for first clip, 2 for second clip
 * @param    pInputPlanes    (IN) Input raw YUV420 image
 * @param    pOutputPlanes    (IN/OUT) Output raw YUV420 image
 * @param    bSkipFramingEffect (IN) skip framing effect flag
 * @return    M4NO_ERROR:                        No error
 ******************************************************************************
 */
static M4OSA_ERR
M4VSS3GPP_intApplyVideoEffect (M4VSS3GPP_InternalEditContext *pC,
    M4VIFI_ImagePlane *pPlaneIn, M4VIFI_ImagePlane *pPlaneOut,
    M4OSA_Bool bSkipFramingEffect) {

    M4OSA_ERR err;

    M4VSS3GPP_ClipContext *pClip;
    M4VSS3GPP_EffectSettings *pFx;
    M4VSS3GPP_ExternalProgress extProgress;

    M4OSA_Double VideoEffectTime;
    M4OSA_Double PercentageDone;
    M4OSA_Int32 tmp;

    M4VIFI_ImagePlane *pPlaneTempIn;
    M4VIFI_ImagePlane *pPlaneTempOut;
    M4VIFI_ImagePlane  pTempYuvPlane[3];
    M4OSA_UInt8 i;
    M4OSA_UInt8 NumActiveEffects =0;


    pClip = pC->pC1;
    if (pC->bIssecondClip == M4OSA_TRUE)
    {
        NumActiveEffects = pC->nbActiveEffects1;
    }
    else
    {
        NumActiveEffects = pC->nbActiveEffects;
    }

    memset((void *)pTempYuvPlane, 0, 3*sizeof(M4VIFI_ImagePlane));

    /**
    * Allocate temporary plane if needed RC */
    if (NumActiveEffects > 1) {
        err = M4VSS3GPP_intAllocateYUV420(pTempYuvPlane, pPlaneOut->u_width,
                  pPlaneOut->u_height);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intApplyVideoEffect: M4VSS3GPP_intAllocateYUV420(4) returns 0x%x,\
                returning M4NO_ERROR",
                err);
            pC->ewc.VppError = err;
            return
                M4NO_ERROR; /**< Return no error to the encoder core
                            (else it may leak in some situations...) */
        }
    }

    if (NumActiveEffects  % 2 == 0)
    {
        pPlaneTempIn = pPlaneIn;
        pPlaneTempOut = pTempYuvPlane;
    }
    else
    {
        pPlaneTempIn = pPlaneIn;
        pPlaneTempOut = pPlaneOut;
    }

    for (i=0; i<NumActiveEffects; i++)
    {
        if (pC->bIssecondClip == M4OSA_TRUE)
        {


            pFx = &(pC->pEffectsList[pC->pActiveEffectsList1[i]]);
            /* Compute how far from the beginning of the effect we are, in clip-base time. */
            // Decorrelate input and output encoding timestamp to handle encoder prefetch
            VideoEffectTime = ((M4OSA_Int32)pC->ewc.dInputVidCts) +
                              pC->pTransitionList[pC->uiCurrentClip].
                              uiTransitionDuration- pFx->uiStartTime;
        }
        else
        {
            pFx = &(pC->pEffectsList[pC->pActiveEffectsList[i]]);
            /* Compute how far from the beginning of the effect we are, in clip-base time. */
            // Decorrelate input and output encoding timestamp to handle encoder prefetch
            VideoEffectTime = ((M4OSA_Int32)pC->ewc.dInputVidCts) - pFx->uiStartTime;
        }



        /* To calculate %, substract timeIncrement because effect should finish on the last frame*/
        /* which is presented from CTS = eof-timeIncrement till CTS = eof */
        PercentageDone = VideoEffectTime
            / ((M4OSA_Float)pFx->uiDuration/*- pC->dOutputFrameDuration*/);

        if( PercentageDone < 0.0 )
            PercentageDone = 0.0;

        if( PercentageDone > 1.0 )
            PercentageDone = 1.0;

        switch( pFx->VideoEffectType )
        {
            case M4VSS3GPP_kVideoEffectType_FadeFromBlack:
                /**
                * Compute where we are in the effect (scale is 0->1024). */
                tmp = (M4OSA_Int32)(PercentageDone * 1024);

                /**
                * Apply the darkening effect */
                err =
                    M4VFL_modifyLumaWithScale((M4ViComImagePlane *)pPlaneTempIn,
                    (M4ViComImagePlane *)pPlaneTempOut, tmp, M4OSA_NULL);

                if( M4NO_ERROR != err )
                {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_intApplyVideoEffect:\
                        M4VFL_modifyLumaWithScale returns error 0x%x,\
                        returning M4VSS3GPP_ERR_LUMA_FILTER_ERROR",
                        err);
                    return M4VSS3GPP_ERR_LUMA_FILTER_ERROR;
                }
                break;

            case M4VSS3GPP_kVideoEffectType_FadeToBlack:
                /**
                * Compute where we are in the effect (scale is 0->1024) */
                tmp = (M4OSA_Int32)(( 1.0 - PercentageDone) * 1024);

                /**
                * Apply the darkening effect */
                err =
                    M4VFL_modifyLumaWithScale((M4ViComImagePlane *)pPlaneTempIn,
                    (M4ViComImagePlane *)pPlaneTempOut, tmp, M4OSA_NULL);

                if( M4NO_ERROR != err )
                {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_intApplyVideoEffect:\
                        M4VFL_modifyLumaWithScale returns error 0x%x,\
                        returning M4VSS3GPP_ERR_LUMA_FILTER_ERROR",
                        err);
                    return M4VSS3GPP_ERR_LUMA_FILTER_ERROR;
                }
                break;

            default:
                if( pFx->VideoEffectType
                    >= M4VSS3GPP_kVideoEffectType_External )
                {
                    M4OSA_UInt32 Cts = 0;
                    M4OSA_Int32 nextEffectTime;

                    /**
                    * Compute where we are in the effect (scale is 0->1000) */
                    tmp = (M4OSA_Int32)(PercentageDone * 1000);

                    /**
                    * Set the progress info provided to the external function */
                    extProgress.uiProgress = (M4OSA_UInt32)tmp;
                    // Decorrelate input and output encoding timestamp to handle encoder prefetch
                    extProgress.uiOutputTime = (M4OSA_UInt32)pC->ewc.dInputVidCts;
                    extProgress.uiClipTime = extProgress.uiOutputTime - pClip->iVoffset;
                    extProgress.bIsLast = M4OSA_FALSE;
                    // Decorrelate input and output encoding timestamp to handle encoder prefetch
                    nextEffectTime = (M4OSA_Int32)(pC->ewc.dInputVidCts \
                        + pC->dOutputFrameDuration);
                    if(nextEffectTime >= (M4OSA_Int32)(pFx->uiStartTime + pFx->uiDuration))
                    {
                        extProgress.bIsLast = M4OSA_TRUE;
                    }
                    /* Here skip the framing effect,
                     * do the framing effect after apply rendering mode */
                    if ((pFx->xVSS.pFramingBuffer != M4OSA_NULL) &&
                        bSkipFramingEffect == M4OSA_TRUE) {
                        memcpy(pPlaneTempOut[0].pac_data, pPlaneTempIn[0].pac_data,
                            pPlaneTempIn[0].u_height * pPlaneTempIn[0].u_width);
                        memcpy(pPlaneTempOut[1].pac_data, pPlaneTempIn[1].pac_data,
                            pPlaneTempIn[1].u_height * pPlaneTempIn[1].u_width);
                        memcpy(pPlaneTempOut[2].pac_data, pPlaneTempIn[2].pac_data,
                            pPlaneTempIn[2].u_height * pPlaneTempIn[2].u_width);

                    } else {
                        err = pFx->ExtVideoEffectFct(pFx->pExtVideoEffectFctCtxt,
                            pPlaneTempIn, pPlaneTempOut, &extProgress,
                            pFx->VideoEffectType
                            - M4VSS3GPP_kVideoEffectType_External);
                    }
                    if( M4NO_ERROR != err )
                    {
                        M4OSA_TRACE1_1(
                            "M4VSS3GPP_intApplyVideoEffect: \
                            External video effect function returns 0x%x!",
                            err);
                        return err;
                    }
                    break;
                }
                else
                {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_intApplyVideoEffect: unknown effect type (0x%x),\
                        returning M4VSS3GPP_ERR_INVALID_VIDEO_EFFECT_TYPE",
                        pFx->VideoEffectType);
                    return M4VSS3GPP_ERR_INVALID_VIDEO_EFFECT_TYPE;
                }
        }
        /**
        * RC Updates pTempPlaneIn and pTempPlaneOut depending on current effect */
        if (((i % 2 == 0) && (NumActiveEffects  % 2 == 0))
            || ((i % 2 != 0) && (NumActiveEffects % 2 != 0)))
        {
            pPlaneTempIn = pTempYuvPlane;
            pPlaneTempOut = pPlaneOut;
        }
        else
        {
            pPlaneTempIn = pPlaneOut;
            pPlaneTempOut = pTempYuvPlane;
        }
    }

    for(i=0; i<3; i++) {
        if(pTempYuvPlane[i].pac_data != M4OSA_NULL) {
            free(pTempYuvPlane[i].pac_data);
            pTempYuvPlane[i].pac_data = M4OSA_NULL;
        }
    }

    /**
    *    Return */
    M4OSA_TRACE3_0("M4VSS3GPP_intApplyVideoEffect: returning M4NO_ERROR");
    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intVideoTransition()
 * @brief    Apply video transition effect pC1+pC2->pPlaneOut
 * @param   pC                (IN/OUT) Internal edit context
 * @param    pOutputPlanes    (IN/OUT) Output raw YUV420 image
 * @return    M4NO_ERROR:                        No error
 ******************************************************************************
 */
static M4OSA_ERR
M4VSS3GPP_intVideoTransition( M4VSS3GPP_InternalEditContext *pC,
                             M4VIFI_ImagePlane *pPlaneOut )
{
    M4OSA_ERR err;
    M4OSA_Int32 iProgress;
    M4VSS3GPP_ExternalProgress extProgress;
    M4VIFI_ImagePlane *pPlane;
    M4OSA_Int32 i;
    const M4OSA_Int32 iDur = (M4OSA_Int32)pC->
        pTransitionList[pC->uiCurrentClip].uiTransitionDuration;

    /**
    * Compute how far from the end cut we are, in clip-base time.
    * It is done with integers because the offset and begin cut have been rounded already. */
    // Decorrelate input and output encoding timestamp to handle encoder prefetch
    iProgress = (M4OSA_Int32)((M4OSA_Double)pC->pC1->iEndTime) - pC->ewc.dInputVidCts +
        ((M4OSA_Double)pC->pC1->iVoffset);
    /**
    * We must remove the duration of one frame, else we would almost never reach the end
    * (It's kind of a "pile and intervals" issue). */
    iProgress -= (M4OSA_Int32)pC->dOutputFrameDuration;

    if( iProgress < 0 ) /**< Sanity checks */
    {
        iProgress = 0;
    }

    /**
    * Compute where we are in the transition, on a base 1000 */
    iProgress = ( ( iDur - iProgress) * 1000) / iDur;

    /**
    * Sanity checks */
    if( iProgress < 0 )
    {
        iProgress = 0;
    }
    else if( iProgress > 1000 )
    {
        iProgress = 1000;
    }

    switch( pC->pTransitionList[pC->uiCurrentClip].TransitionBehaviour )
    {
        case M4VSS3GPP_TransitionBehaviour_SpeedUp:
            iProgress = ( iProgress * iProgress) / 1000;
            break;

        case M4VSS3GPP_TransitionBehaviour_Linear:
            /*do nothing*/
            break;

        case M4VSS3GPP_TransitionBehaviour_SpeedDown:
            iProgress = (M4OSA_Int32)(sqrt(iProgress * 1000));
            break;

        case M4VSS3GPP_TransitionBehaviour_SlowMiddle:
            if( iProgress < 500 )
            {
                iProgress = (M4OSA_Int32)(sqrt(iProgress * 500));
            }
            else
            {
                iProgress =
                    (M4OSA_Int32)(( ( ( iProgress - 500) * (iProgress - 500))
                    / 500) + 500);
            }
            break;

        case M4VSS3GPP_TransitionBehaviour_FastMiddle:
            if( iProgress < 500 )
            {
                iProgress = (M4OSA_Int32)(( iProgress * iProgress) / 500);
            }
            else
            {
                iProgress = (M4OSA_Int32)(sqrt(( iProgress - 500) * 500) + 500);
            }
            break;

        default:
            /*do nothing*/
            break;
    }

    switch( pC->pTransitionList[pC->uiCurrentClip].VideoTransitionType )
    {
        case M4VSS3GPP_kVideoTransitionType_CrossFade:
            /**
            * Apply the transition effect */
            err = M4VIFI_ImageBlendingonYUV420(M4OSA_NULL,
                (M4ViComImagePlane *)pC->yuv1,
                (M4ViComImagePlane *)pC->yuv2,
                (M4ViComImagePlane *)pPlaneOut, iProgress);

            if( M4NO_ERROR != err )
            {
                M4OSA_TRACE1_1(
                    "M4VSS3GPP_intVideoTransition:\
                     M4VIFI_ImageBlendingonYUV420 returns error 0x%x,\
                    returning M4VSS3GPP_ERR_TRANSITION_FILTER_ERROR",
                    err);
                return M4VSS3GPP_ERR_TRANSITION_FILTER_ERROR;
            }
            break;

        case M4VSS3GPP_kVideoTransitionType_None:
            /**
            * This is a stupid-non optimized version of the None transition...
            * We copy the YUV frame */
            if( iProgress < 500 ) /**< first half of transition */
            {
                pPlane = pC->yuv1;
            }
            else /**< second half of transition */
            {
                pPlane = pC->yuv2;
            }
            /**
            * Copy the input YUV frames */
            i = 3;

            while( i-- > 0 )
            {
                memcpy((void *)pPlaneOut[i].pac_data,
                 (void *)pPlane[i].pac_data,
                    pPlaneOut[i].u_stride * pPlaneOut[i].u_height);
            }
            break;

        default:
            if( pC->pTransitionList[pC->uiCurrentClip].VideoTransitionType
                >= M4VSS3GPP_kVideoTransitionType_External )
            {
                /**
                * Set the progress info provided to the external function */
                extProgress.uiProgress = (M4OSA_UInt32)iProgress;
                // Decorrelate input and output encoding timestamp to handle encoder prefetch
                extProgress.uiOutputTime = (M4OSA_UInt32)pC->ewc.dInputVidCts;
                extProgress.uiClipTime = extProgress.uiOutputTime - pC->pC1->iVoffset;

                err = pC->pTransitionList[pC->
                    uiCurrentClip].ExtVideoTransitionFct(
                    pC->pTransitionList[pC->
                    uiCurrentClip].pExtVideoTransitionFctCtxt,
                    pC->yuv1, pC->yuv2, pPlaneOut, &extProgress,
                    pC->pTransitionList[pC->
                    uiCurrentClip].VideoTransitionType
                    - M4VSS3GPP_kVideoTransitionType_External);

                if( M4NO_ERROR != err )
                {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_intVideoTransition:\
                        External video transition function returns 0x%x!",
                        err);
                    return err;
                }
                break;
            }
            else
            {
                M4OSA_TRACE1_1(
                    "M4VSS3GPP_intVideoTransition: unknown transition type (0x%x),\
                    returning M4VSS3GPP_ERR_INVALID_VIDEO_TRANSITION_TYPE",
                    pC->pTransitionList[pC->uiCurrentClip].VideoTransitionType);
                return M4VSS3GPP_ERR_INVALID_VIDEO_TRANSITION_TYPE;
            }
    }

    /**
    *    Return */
    M4OSA_TRACE3_0("M4VSS3GPP_intVideoTransition: returning M4NO_ERROR");
    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * M4OSA_Void M4VSS3GPP_intUpdateTimeInfo()
 * @brief    Update bit stream time info by Counter Time System to be compliant with
 *          players using bit stream time info
 * @note    H263 uses an absolute time counter unlike MPEG4 which uses Group Of Vops
 *          (GOV, see the standard)
 * @param   pC                    (IN/OUT) returns time updated video AU,
 *                                the offset between system and video time (MPEG4 only)
 *                                and the state of the current clip (MPEG4 only)
 * @return    nothing
 ******************************************************************************
 */
static M4OSA_Void
M4VSS3GPP_intUpdateTimeInfo( M4VSS3GPP_InternalEditContext *pC,
                            M4SYS_AccessUnit *pAU )
{
    M4OSA_UInt8 uiTmp;
    M4OSA_UInt32 uiCts = 0;
    M4OSA_MemAddr8 pTmp;
    M4OSA_UInt32 uiAdd;
    M4OSA_UInt32 uiCurrGov;
    M4OSA_Int8 iDiff;

    M4VSS3GPP_ClipContext *pClipCtxt = pC->pC1;
    M4OSA_Int32 *pOffset = &(pC->ewc.iMpeg4GovOffset);

    /**
    * Set H263 time counter from system time */
    if( M4SYS_kH263 == pAU->stream->streamType )
    {
        uiTmp = (M4OSA_UInt8)((M4OSA_UInt32)( ( pAU->CTS * 30) / 1001 + 0.5)
            % M4VSS3GPP_EDIT_H263_MODULO_TIME);
        M4VSS3GPP_intSetH263TimeCounter((M4OSA_MemAddr8)(pAU->dataAddress),
            uiTmp);
    }
    /*
    * Set MPEG4 GOV time counter regarding video and system time */
    else if( M4SYS_kMPEG_4 == pAU->stream->streamType )
    {
        /*
        * If GOV.
        * beware of little/big endian! */
        /* correction: read 8 bits block instead of one 32 bits block */
        M4OSA_UInt8 *temp8 = (M4OSA_UInt8 *)(pAU->dataAddress);
        M4OSA_UInt32 temp32 = 0;

        temp32 = ( 0x000000ff & (M4OSA_UInt32)(*temp8))
            + (0x0000ff00 & ((M4OSA_UInt32)(*(temp8 + 1))) << 8)
            + (0x00ff0000 & ((M4OSA_UInt32)(*(temp8 + 2))) << 16)
            + (0xff000000 & ((M4OSA_UInt32)(*(temp8 + 3))) << 24);

        M4OSA_TRACE3_2("RC: Temp32: 0x%x, dataAddress: 0x%x\n", temp32,
            *(pAU->dataAddress));

        if( M4VSS3GPP_EDIT_GOV_HEADER == temp32 )
        {
            pTmp =
                (M4OSA_MemAddr8)(pAU->dataAddress
                + 1); /**< Jump to the time code (just after the 32 bits header) */
            uiAdd = (M4OSA_UInt32)(pAU->CTS)+( *pOffset);

            switch( pClipCtxt->bMpeg4GovState )
            {
                case M4OSA_FALSE: /*< INIT */
                    {
                        /* video time = ceil (system time + offset) */
                        uiCts = ( uiAdd + 999) / 1000;

                        /* offset update */
                        ( *pOffset) += (( uiCts * 1000) - uiAdd);

                        /* Save values */
                        pClipCtxt->uiMpeg4PrevGovValueSet = uiCts;

                        /* State to 'first' */
                        pClipCtxt->bMpeg4GovState = M4OSA_TRUE;
                    }
                    break;

                case M4OSA_TRUE: /*< UPDATE */
                    {
                        /* Get current Gov value */
                        M4VSS3GPP_intGetMPEG4Gov(pTmp, &uiCurrGov);

                        /* video time = floor or ceil (system time + offset) */
                        uiCts = (uiAdd / 1000);
                        iDiff = (M4OSA_Int8)(uiCurrGov
                            - pClipCtxt->uiMpeg4PrevGovValueGet - uiCts
                            + pClipCtxt->uiMpeg4PrevGovValueSet);

                        /* ceiling */
                        if( iDiff > 0 )
                        {
                            uiCts += (M4OSA_UInt32)(iDiff);

                            /* offset update */
                            ( *pOffset) += (( uiCts * 1000) - uiAdd);
                        }

                        /* Save values */
                        pClipCtxt->uiMpeg4PrevGovValueGet = uiCurrGov;
                        pClipCtxt->uiMpeg4PrevGovValueSet = uiCts;
                    }
                    break;
            }

            M4VSS3GPP_intSetMPEG4Gov(pTmp, uiCts);
        }
    }
    return;
}

/**
 ******************************************************************************
 * M4OSA_Void M4VSS3GPP_intCheckVideoEffects()
 * @brief    Check which video effect must be applied at the current time
 ******************************************************************************
 */
static M4OSA_Void
M4VSS3GPP_intCheckVideoEffects( M4VSS3GPP_InternalEditContext *pC,
                               M4OSA_UInt8 uiClipNumber )
{
    M4OSA_UInt8 uiClipIndex;
    M4OSA_UInt8 uiFxIndex, i;
    M4VSS3GPP_ClipContext *pClip;
    M4VSS3GPP_EffectSettings *pFx;
    M4OSA_Int32 Off, BC, EC;
    // Decorrelate input and output encoding timestamp to handle encoder prefetch
    M4OSA_Int32 t = (M4OSA_Int32)pC->ewc.dInputVidCts;

    uiClipIndex = pC->uiCurrentClip;
    if (uiClipNumber == 1) {
        pClip = pC->pC1;
        pC->bClip1ActiveFramingEffect = M4OSA_FALSE;
    } else {
        pClip = pC->pC2;
        pC->bClip2ActiveFramingEffect = M4OSA_FALSE;
    }
    /**
    * Shortcuts for code readability */
    Off = pClip->iVoffset;
    BC = pClip->iActualVideoBeginCut;
    EC = pClip->iEndTime;

    i = 0;

    for ( uiFxIndex = 0; uiFxIndex < pC->nbEffects; uiFxIndex++ )
    {
        /** Shortcut, reverse order because of priority between effects(EndEffect always clean )*/
        pFx = &(pC->pEffectsList[pC->nbEffects - 1 - uiFxIndex]);

        if( M4VSS3GPP_kVideoEffectType_None != pFx->VideoEffectType )
        {
            /**
            * Check if there is actually a video effect */

             if(uiClipNumber ==1)
             {
                /**< Are we after the start time of the effect?
                 * or Are we into the effect duration?
                 */
                if ( (t >= (M4OSA_Int32)(pFx->uiStartTime)) &&
                    (t <= (M4OSA_Int32)(pFx->uiStartTime + pFx->uiDuration)) ) {
                    /**
                     * Set the active effect(s) */
                    pC->pActiveEffectsList[i] = pC->nbEffects-1-uiFxIndex;

                    /**
                     * Update counter of active effects */
                    i++;
                    if (pFx->xVSS.pFramingBuffer != M4OSA_NULL) {
                        pC->bClip1ActiveFramingEffect = M4OSA_TRUE;
                    }

                    /**
                     * For all external effects set this flag to true. */
                    if(pFx->VideoEffectType > M4VSS3GPP_kVideoEffectType_External)
                    {
                        pC->m_bClipExternalHasStarted = M4OSA_TRUE;
                    }
                }

            }
            else
            {
                /**< Are we into the effect duration? */
                if ( ((M4OSA_Int32)(t + pC->pTransitionList[uiClipIndex].uiTransitionDuration)
                    >= (M4OSA_Int32)(pFx->uiStartTime))
                    && ( (M4OSA_Int32)(t + pC->pTransitionList[uiClipIndex].uiTransitionDuration)
                    <= (M4OSA_Int32)(pFx->uiStartTime + pFx->uiDuration)) ) {
                    /**
                     * Set the active effect(s) */
                    pC->pActiveEffectsList1[i] = pC->nbEffects-1-uiFxIndex;

                    /**
                     * Update counter of active effects */
                    i++;
                    if (pFx->xVSS.pFramingBuffer != M4OSA_NULL) {
                        pC->bClip2ActiveFramingEffect = M4OSA_TRUE;
                    }
                    /**
                     * For all external effects set this flag to true. */
                    if(pFx->VideoEffectType > M4VSS3GPP_kVideoEffectType_External)
                    {
                        pC->m_bClipExternalHasStarted = M4OSA_TRUE;
                    }

                    /**
                     * The third effect has the highest priority, then the second one, then the first one.
                     * Hence, as soon as we found an active effect, we can get out of this loop */
                }
            }
            if (M4VIDEOEDITING_kH264 !=
                    pC->pC1->pSettings->ClipProperties.VideoStreamType) {

                    // For Mpeg4 and H263 clips, full decode encode not required
                    pC->m_bClipExternalHasStarted = M4OSA_FALSE;
            }
        }
    }
    if(1==uiClipNumber)
    {
    /**
     * Save number of active effects */
        pC->nbActiveEffects = i;
    }
    else
    {
        pC->nbActiveEffects1 = i;
    }

    /**
    * Change the absolut time to clip related time */
    t -= Off;

    /**
    * Check if we are on the begin cut (for clip1 only) */
    if( ( 0 != BC) && (t == BC) && (1 == uiClipNumber) )
    {
        pC->bClip1AtBeginCut = M4OSA_TRUE;
    }
    else
    {
        pC->bClip1AtBeginCut = M4OSA_FALSE;
    }

    return;
}

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intCreateVideoEncoder()
 * @brief    Creates the video encoder
 * @note
 ******************************************************************************
 */
M4OSA_ERR M4VSS3GPP_intCreateVideoEncoder( M4VSS3GPP_InternalEditContext *pC )
{
    M4OSA_ERR err;
    M4ENCODER_AdvancedParams EncParams;

    /**
    * Simulate a writer interface with our specific function */
    pC->ewc.OurWriterDataInterface.pProcessAU =
        M4VSS3GPP_intProcessAU; /**< This function is VSS 3GPP specific,
                                but it follow the writer interface */
    pC->ewc.OurWriterDataInterface.pStartAU =
        M4VSS3GPP_intStartAU; /**< This function is VSS 3GPP specific,
                              but it follow the writer interface */
    pC->ewc.OurWriterDataInterface.pWriterContext =
        (M4WRITER_Context)
        pC; /**< We give the internal context as writer context */

    /**
    * Get the encoder interface, if not already done */
    if( M4OSA_NULL == pC->ShellAPI.pVideoEncoderGlobalFcts )
    {
        err = M4VSS3GPP_setCurrentVideoEncoder(&pC->ShellAPI,
            pC->ewc.VideoStreamType);
        M4OSA_TRACE1_1(
            "M4VSS3GPP_intCreateVideoEncoder: setCurrentEncoder returns 0x%x",
            err);
        M4ERR_CHECK_RETURN(err);
    }

    /**
    * Set encoder shell parameters according to VSS settings */

    /* Common parameters */
    EncParams.InputFormat = M4ENCODER_kIYUV420;
    EncParams.FrameWidth = pC->ewc.uiVideoWidth;
    EncParams.FrameHeight = pC->ewc.uiVideoHeight;
    EncParams.uiTimeScale = pC->ewc.uiVideoTimeScale;

    if( pC->bIsMMS == M4OSA_FALSE )
    {
        /* No strict regulation in video editor */
        /* Because of the effects and transitions we should allow more flexibility */
        /* Also it prevents to drop important frames (with a bad result on sheduling and
        block effetcs) */
        EncParams.bInternalRegulation = M4OSA_FALSE;
        // Variable framerate is not supported by StageFright encoders
        EncParams.FrameRate = M4ENCODER_k30_FPS;
    }
    else
    {
        /* In case of MMS mode, we need to enable bitrate regulation to be sure */
        /* to reach the targeted output file size */
        EncParams.bInternalRegulation = M4OSA_TRUE;
        EncParams.FrameRate = pC->MMSvideoFramerate;
    }

    /**
    * Other encoder settings (defaults) */
    EncParams.uiHorizontalSearchRange = 0;     /* use default */
    EncParams.uiVerticalSearchRange = 0;       /* use default */
    EncParams.bErrorResilience = M4OSA_FALSE;  /* no error resilience */
    EncParams.uiIVopPeriod = 0;                /* use default */
    EncParams.uiMotionEstimationTools = 0;     /* M4V_MOTION_EST_TOOLS_ALL */
    EncParams.bAcPrediction = M4OSA_TRUE;      /* use AC prediction */
    EncParams.uiStartingQuantizerValue = 10;   /* initial QP = 10 */
    EncParams.bDataPartitioning = M4OSA_FALSE; /* no data partitioning */

    /**
    * Set the video profile and level */
    EncParams.videoProfile = pC->ewc.outputVideoProfile;
    EncParams.videoLevel= pC->ewc.outputVideoLevel;

    switch ( pC->ewc.VideoStreamType )
    {
        case M4SYS_kH263:

            EncParams.Format = M4ENCODER_kH263;

            EncParams.uiStartingQuantizerValue = 10;
            EncParams.uiRateFactor = 1; /* default */

            EncParams.bErrorResilience = M4OSA_FALSE;
            EncParams.bDataPartitioning = M4OSA_FALSE;
            break;

        case M4SYS_kMPEG_4:

            EncParams.Format = M4ENCODER_kMPEG4;

            EncParams.uiStartingQuantizerValue = 8;
            EncParams.uiRateFactor = (M4OSA_UInt8)(( pC->dOutputFrameDuration
                * pC->ewc.uiVideoTimeScale) / 1000.0 + 0.5);

            if( EncParams.uiRateFactor == 0 )
                EncParams.uiRateFactor = 1; /* default */

            if( M4OSA_FALSE == pC->ewc.bVideoDataPartitioning )
            {
                EncParams.bErrorResilience = M4OSA_FALSE;
                EncParams.bDataPartitioning = M4OSA_FALSE;
            }
            else
            {
                EncParams.bErrorResilience = M4OSA_TRUE;
                EncParams.bDataPartitioning = M4OSA_TRUE;
            }
            break;

        case M4SYS_kH264:
            M4OSA_TRACE1_0("M4VSS3GPP_intCreateVideoEncoder: M4SYS_H264");

            EncParams.Format = M4ENCODER_kH264;

            EncParams.uiStartingQuantizerValue = 10;
            EncParams.uiRateFactor = 1; /* default */

            EncParams.bErrorResilience = M4OSA_FALSE;
            EncParams.bDataPartitioning = M4OSA_FALSE;
            //EncParams.FrameRate = M4VIDEOEDITING_k5_FPS;
            break;

        default:
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intCreateVideoEncoder: Unknown videoStreamType 0x%x",
                pC->ewc.VideoStreamType);
            return M4VSS3GPP_ERR_EDITING_UNSUPPORTED_VIDEO_FORMAT;
    }

    if( pC->bIsMMS == M4OSA_FALSE )
    {
        EncParams.Bitrate = pC->xVSS.outputVideoBitrate;

    }
    else
    {
        EncParams.Bitrate = pC->uiMMSVideoBitrate; /* RC */
        EncParams.uiTimeScale = 0; /* We let the encoder choose the timescale */
    }

    M4OSA_TRACE1_0("M4VSS3GPP_intCreateVideoEncoder: calling encoder pFctInit");
    /**
    * Init the video encoder (advanced settings version of the encoder Open function) */
    err = pC->ShellAPI.pVideoEncoderGlobalFcts->pFctInit(&pC->ewc.pEncContext,
        &pC->ewc.OurWriterDataInterface, M4VSS3GPP_intVPP, pC,
        pC->ShellAPI.pCurrentVideoEncoderExternalAPI,
        pC->ShellAPI.pCurrentVideoEncoderUserData);

    if( M4NO_ERROR != err )
    {
        M4OSA_TRACE1_1(
            "M4VSS3GPP_intCreateVideoEncoder: pVideoEncoderGlobalFcts->pFctInit returns 0x%x",
            err);
        return err;
    }

    pC->ewc.encoderState = M4VSS3GPP_kEncoderClosed;
    M4OSA_TRACE1_0("M4VSS3GPP_intCreateVideoEncoder: calling encoder pFctOpen");

    err = pC->ShellAPI.pVideoEncoderGlobalFcts->pFctOpen(pC->ewc.pEncContext,
        &pC->ewc.WriterVideoAU, &EncParams);

    if( M4NO_ERROR != err )
    {
        M4OSA_TRACE1_1(
            "M4VSS3GPP_intCreateVideoEncoder: pVideoEncoderGlobalFcts->pFctOpen returns 0x%x",
            err);
        return err;
    }

    pC->ewc.encoderState = M4VSS3GPP_kEncoderStopped;
    M4OSA_TRACE1_0(
        "M4VSS3GPP_intCreateVideoEncoder: calling encoder pFctStart");

    if( M4OSA_NULL != pC->ShellAPI.pVideoEncoderGlobalFcts->pFctStart )
    {
        err = pC->ShellAPI.pVideoEncoderGlobalFcts->pFctStart(
            pC->ewc.pEncContext);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intCreateVideoEncoder: pVideoEncoderGlobalFcts->pFctStart returns 0x%x",
                err);
            return err;
        }
    }

    pC->ewc.encoderState = M4VSS3GPP_kEncoderRunning;

    /**
    *    Return */
    M4OSA_TRACE3_0("M4VSS3GPP_intCreateVideoEncoder: returning M4NO_ERROR");
    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intDestroyVideoEncoder()
 * @brief    Destroy the video encoder
 * @note
 ******************************************************************************
 */
M4OSA_ERR M4VSS3GPP_intDestroyVideoEncoder( M4VSS3GPP_InternalEditContext *pC )
{
    M4OSA_ERR err = M4NO_ERROR;

    if( M4OSA_NULL != pC->ewc.pEncContext )
    {
        if( M4VSS3GPP_kEncoderRunning == pC->ewc.encoderState )
        {
            if( pC->ShellAPI.pVideoEncoderGlobalFcts->pFctStop != M4OSA_NULL )
            {
                err = pC->ShellAPI.pVideoEncoderGlobalFcts->pFctStop(
                    pC->ewc.pEncContext);

                if( M4NO_ERROR != err )
                {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_intDestroyVideoEncoder:\
                        pVideoEncoderGlobalFcts->pFctStop returns 0x%x",
                        err);
                    /* Well... how the heck do you handle a failed cleanup? */
                }
            }

            pC->ewc.encoderState = M4VSS3GPP_kEncoderStopped;
        }

        /* Has the encoder actually been opened? Don't close it if that's not the case. */
        if( M4VSS3GPP_kEncoderStopped == pC->ewc.encoderState )
        {
            err = pC->ShellAPI.pVideoEncoderGlobalFcts->pFctClose(
                pC->ewc.pEncContext);

            if( M4NO_ERROR != err )
            {
                M4OSA_TRACE1_1(
                    "M4VSS3GPP_intDestroyVideoEncoder:\
                    pVideoEncoderGlobalFcts->pFctClose returns 0x%x",
                    err);
                /* Well... how the heck do you handle a failed cleanup? */
            }

            pC->ewc.encoderState = M4VSS3GPP_kEncoderClosed;
        }

        err = pC->ShellAPI.pVideoEncoderGlobalFcts->pFctCleanup(
            pC->ewc.pEncContext);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intDestroyVideoEncoder:\
                pVideoEncoderGlobalFcts->pFctCleanup returns 0x%x!",
                err);
            /**< We do not return the error here because we still have stuff to free */
        }

        pC->ewc.encoderState = M4VSS3GPP_kNoEncoder;
        /**
        * Reset variable */
        pC->ewc.pEncContext = M4OSA_NULL;
    }

    M4OSA_TRACE3_1("M4VSS3GPP_intDestroyVideoEncoder: returning 0x%x", err);
    return err;
}

/**
 ******************************************************************************
 * M4OSA_Void M4VSS3GPP_intSetH263TimeCounter()
 * @brief    Modify the time counter of the given H263 video AU
 * @note
 * @param    pAuDataBuffer    (IN/OUT) H263 Video AU to modify
 * @param    uiCts            (IN)     New time counter value
 * @return    nothing
 ******************************************************************************
 */
static M4OSA_Void M4VSS3GPP_intSetH263TimeCounter( M4OSA_MemAddr8 pAuDataBuffer,
                                                  M4OSA_UInt8 uiCts )
{
    /*
    *  The H263 time counter is 8 bits located on the "x" below:
    *
    *   |--------|--------|--------|--------|
    *    ???????? ???????? ??????xx xxxxxx??
    */

    /**
    * Write the 2 bits on the third byte */
    pAuDataBuffer[2] = ( pAuDataBuffer[2] & 0xFC) | (( uiCts >> 6) & 0x3);

    /**
    * Write the 6 bits on the fourth byte */
    pAuDataBuffer[3] = ( ( uiCts << 2) & 0xFC) | (pAuDataBuffer[3] & 0x3);

    return;
}

/**
 ******************************************************************************
 * M4OSA_Void M4VSS3GPP_intSetMPEG4Gov()
 * @brief    Modify the time info from Group Of VOP video AU
 * @note
 * @param    pAuDataBuffer    (IN)    MPEG4 Video AU to modify
 * @param    uiCtsSec            (IN)     New GOV time info in second unit
 * @return    nothing
 ******************************************************************************
 */
static M4OSA_Void M4VSS3GPP_intSetMPEG4Gov( M4OSA_MemAddr8 pAuDataBuffer,
                                           M4OSA_UInt32 uiCtsSec )
{
    /*
    *  The MPEG-4 time code length is 18 bits:
    *
    *     hh     mm    marker    ss
    *    xxxxx|xxx xxx     1    xxxx xx ??????
    *   |----- ---|---     -    ----|-- ------|
    */
    M4OSA_UInt8 uiHh;
    M4OSA_UInt8 uiMm;
    M4OSA_UInt8 uiSs;
    M4OSA_UInt8 uiTmp;

    /**
    * Write the 2 last bits ss */
    uiSs = (M4OSA_UInt8)(uiCtsSec % 60); /**< modulo part */
    pAuDataBuffer[2] = (( ( uiSs & 0x03) << 6) | (pAuDataBuffer[2] & 0x3F));

    if( uiCtsSec < 60 )
    {
        /**
        * Write the 3 last bits of mm, the marker bit (0x10 */
        pAuDataBuffer[1] = (( 0x10) | (uiSs >> 2));

        /**
        * Write the 5 bits of hh and 3 of mm (out of 6) */
        pAuDataBuffer[0] = 0;
    }
    else
    {
        /**
        * Write the 3 last bits of mm, the marker bit (0x10 */
        uiTmp = (M4OSA_UInt8)(uiCtsSec / 60); /**< integer part */
        uiMm = (M4OSA_UInt8)(uiTmp % 60);
        pAuDataBuffer[1] = (( uiMm << 5) | (0x10) | (uiSs >> 2));

        if( uiTmp < 60 )
        {
            /**
            * Write the 5 bits of hh and 3 of mm (out of 6) */
            pAuDataBuffer[0] = ((uiMm >> 3));
        }
        else
        {
            /**
            * Write the 5 bits of hh and 3 of mm (out of 6) */
            uiHh = (M4OSA_UInt8)(uiTmp / 60);
            pAuDataBuffer[0] = (( uiHh << 3) | (uiMm >> 3));
        }
    }
    return;
}

/**
 ******************************************************************************
 * M4OSA_Void M4VSS3GPP_intGetMPEG4Gov()
 * @brief    Get the time info from Group Of VOP video AU
 * @note
 * @param    pAuDataBuffer    (IN)    MPEG4 Video AU to modify
 * @param    pCtsSec            (OUT)    Current GOV time info in second unit
 * @return    nothing
 ******************************************************************************
 */
static M4OSA_Void M4VSS3GPP_intGetMPEG4Gov( M4OSA_MemAddr8 pAuDataBuffer,
                                           M4OSA_UInt32 *pCtsSec )
{
    /*
    *  The MPEG-4 time code length is 18 bits:
    *
    *     hh     mm    marker    ss
    *    xxxxx|xxx xxx     1    xxxx xx ??????
    *   |----- ---|---     -    ----|-- ------|
    */
    M4OSA_UInt8 uiHh;
    M4OSA_UInt8 uiMm;
    M4OSA_UInt8 uiSs;
    M4OSA_UInt8 uiTmp;
    M4OSA_UInt32 uiCtsSec;

    /**
    * Read ss */
    uiSs = (( pAuDataBuffer[2] & 0xC0) >> 6);
    uiTmp = (( pAuDataBuffer[1] & 0x0F) << 2);
    uiCtsSec = uiSs + uiTmp;

    /**
    * Read mm */
    uiMm = (( pAuDataBuffer[1] & 0xE0) >> 5);
    uiTmp = (( pAuDataBuffer[0] & 0x07) << 3);
    uiMm = uiMm + uiTmp;
    uiCtsSec = ( uiMm * 60) + uiCtsSec;

    /**
    * Read hh */
    uiHh = (( pAuDataBuffer[0] & 0xF8) >> 3);

    if( uiHh )
    {
        uiCtsSec = ( uiHh * 3600) + uiCtsSec;
    }

    /*
    * in sec */
    *pCtsSec = uiCtsSec;

    return;
}

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intAllocateYUV420()
 * @brief    Allocate the three YUV 4:2:0 planes
 * @note
 * @param    pPlanes    (IN/OUT) valid pointer to 3 M4VIFI_ImagePlane structures
 * @param    uiWidth    (IN)     Image width
 * @param    uiHeight(IN)     Image height
 ******************************************************************************
 */
static M4OSA_ERR M4VSS3GPP_intAllocateYUV420( M4VIFI_ImagePlane *pPlanes,
                                             M4OSA_UInt32 uiWidth, M4OSA_UInt32 uiHeight )
{
    if (pPlanes == M4OSA_NULL) {
        M4OSA_TRACE1_0("M4VSS3GPP_intAllocateYUV420: Invalid pPlanes pointer");
        return M4ERR_PARAMETER;
    }
    /* if the buffer is not NULL and same size with target size,
     * do not malloc again*/
    if (pPlanes[0].pac_data != M4OSA_NULL &&
        pPlanes[0].u_width == uiWidth &&
        pPlanes[0].u_height == uiHeight) {
        return M4NO_ERROR;
    }

    pPlanes[0].u_width = uiWidth;
    pPlanes[0].u_height = uiHeight;
    pPlanes[0].u_stride = uiWidth;
    pPlanes[0].u_topleft = 0;

    if (pPlanes[0].pac_data != M4OSA_NULL) {
        free(pPlanes[0].pac_data);
        pPlanes[0].pac_data = M4OSA_NULL;
    }
    pPlanes[0].pac_data = (M4VIFI_UInt8 *)M4OSA_32bitAlignedMalloc(pPlanes[0].u_stride
        * pPlanes[0].u_height, M4VSS3GPP, (M4OSA_Char *)"pPlanes[0].pac_data");

    if( M4OSA_NULL == pPlanes[0].pac_data )
    {
        M4OSA_TRACE1_0(
            "M4VSS3GPP_intAllocateYUV420: unable to allocate pPlanes[0].pac_data,\
            returning M4ERR_ALLOC");
        return M4ERR_ALLOC;
    }

    pPlanes[1].u_width = pPlanes[0].u_width >> 1;
    pPlanes[1].u_height = pPlanes[0].u_height >> 1;
    pPlanes[1].u_stride = pPlanes[1].u_width;
    pPlanes[1].u_topleft = 0;
    if (pPlanes[1].pac_data != M4OSA_NULL) {
        free(pPlanes[1].pac_data);
        pPlanes[1].pac_data = M4OSA_NULL;
    }
    pPlanes[1].pac_data = (M4VIFI_UInt8 *)M4OSA_32bitAlignedMalloc(pPlanes[1].u_stride
        * pPlanes[1].u_height, M4VSS3GPP,(M4OSA_Char *) "pPlanes[1].pac_data");

    if( M4OSA_NULL == pPlanes[1].pac_data )
    {
        M4OSA_TRACE1_0(
            "M4VSS3GPP_intAllocateYUV420: unable to allocate pPlanes[1].pac_data,\
            returning M4ERR_ALLOC");
        free((void *)pPlanes[0].pac_data);
        pPlanes[0].pac_data = M4OSA_NULL;
        return M4ERR_ALLOC;
    }

    pPlanes[2].u_width = pPlanes[1].u_width;
    pPlanes[2].u_height = pPlanes[1].u_height;
    pPlanes[2].u_stride = pPlanes[2].u_width;
    pPlanes[2].u_topleft = 0;
    if (pPlanes[2].pac_data != M4OSA_NULL) {
        free(pPlanes[2].pac_data);
        pPlanes[2].pac_data = M4OSA_NULL;
    }
    pPlanes[2].pac_data = (M4VIFI_UInt8 *)M4OSA_32bitAlignedMalloc(pPlanes[2].u_stride
        * pPlanes[2].u_height, M4VSS3GPP, (M4OSA_Char *)"pPlanes[2].pac_data");

    if( M4OSA_NULL == pPlanes[2].pac_data )
    {
        M4OSA_TRACE1_0(
            "M4VSS3GPP_intAllocateYUV420: unable to allocate pPlanes[2].pac_data,\
            returning M4ERR_ALLOC");
        free((void *)pPlanes[0].pac_data);
        free((void *)pPlanes[1].pac_data);
        pPlanes[0].pac_data = M4OSA_NULL;
        pPlanes[1].pac_data = M4OSA_NULL;
        return M4ERR_ALLOC;
    }

    memset((void *)pPlanes[0].pac_data, 0, pPlanes[0].u_stride*pPlanes[0].u_height);
    memset((void *)pPlanes[1].pac_data, 0, pPlanes[1].u_stride*pPlanes[1].u_height);
    memset((void *)pPlanes[2].pac_data, 0, pPlanes[2].u_stride*pPlanes[2].u_height);
    /**
    *    Return */
    M4OSA_TRACE3_0("M4VSS3GPP_intAllocateYUV420: returning M4NO_ERROR");
    return M4NO_ERROR;
}

/**
******************************************************************************
* M4OSA_ERR M4VSS3GPP_internalConvertAndResizeARGB8888toYUV420(M4OSA_Void* pFileIn,
*                                            M4OSA_FileReadPointer* pFileReadPtr,
*                                               M4VIFI_ImagePlane* pImagePlanes,
*                                               M4OSA_UInt32 width,
*                                               M4OSA_UInt32 height);
* @brief    It Coverts and resizes a ARGB8888 image to YUV420
* @note
* @param    pFileIn         (IN) The ARGB888 input file
* @param    pFileReadPtr    (IN) Pointer on filesystem functions
* @param    pImagePlanes    (IN/OUT) Pointer on YUV420 output planes allocated by the user.
*                           ARGB8888 image  will be converted and resized to output
*                           YUV420 plane size
* @param width       (IN) width of the ARGB8888
* @param height      (IN) height of the ARGB8888
* @return   M4NO_ERROR: No error
* @return   M4ERR_ALLOC: memory error
* @return   M4ERR_PARAMETER: At least one of the function parameters is null
******************************************************************************
*/

M4OSA_ERR M4VSS3GPP_internalConvertAndResizeARGB8888toYUV420(M4OSA_Void* pFileIn,
                           M4OSA_FileReadPointer* pFileReadPtr,
                           M4VIFI_ImagePlane* pImagePlanes,
                           M4OSA_UInt32 width,M4OSA_UInt32 height) {
    M4OSA_Context pARGBIn;
    M4VIFI_ImagePlane rgbPlane1 ,rgbPlane2;
    M4OSA_UInt32 frameSize_argb = width * height * 4;
    M4OSA_UInt32 frameSize_rgb888 = width * height * 3;
    M4OSA_UInt32 i = 0,j= 0;
    M4OSA_ERR err = M4NO_ERROR;

    M4OSA_UInt8 *pArgbPlane =
        (M4OSA_UInt8*) M4OSA_32bitAlignedMalloc(frameSize_argb,
                                                M4VS, (M4OSA_Char*)"argb data");
    if (pArgbPlane == M4OSA_NULL) {
        M4OSA_TRACE1_0("M4VSS3GPP_internalConvertAndResizeARGB8888toYUV420: \
            Failed to allocate memory for ARGB plane");
        return M4ERR_ALLOC;
    }

    /* Get file size */
    err = pFileReadPtr->openRead(&pARGBIn, pFileIn, M4OSA_kFileRead);
    if (err != M4NO_ERROR) {
        M4OSA_TRACE1_2("M4VSS3GPP_internalConvertAndResizeARGB8888toYUV420 : \
            Can not open input ARGB8888 file %s, error: 0x%x\n",pFileIn, err);
        free(pArgbPlane);
        pArgbPlane = M4OSA_NULL;
        goto cleanup;
    }

    err = pFileReadPtr->readData(pARGBIn,(M4OSA_MemAddr8)pArgbPlane,
                                 &frameSize_argb);
    if (err != M4NO_ERROR) {
        M4OSA_TRACE1_2("M4VSS3GPP_internalConvertAndResizeARGB8888toYUV420 \
            Can not read ARGB8888 file %s, error: 0x%x\n",pFileIn, err);
        pFileReadPtr->closeRead(pARGBIn);
        free(pArgbPlane);
        pArgbPlane = M4OSA_NULL;
        goto cleanup;
    }

    err = pFileReadPtr->closeRead(pARGBIn);
    if(err != M4NO_ERROR) {
        M4OSA_TRACE1_2("M4VSS3GPP_internalConvertAndResizeARGB8888toYUV420 \
            Can not close ARGB8888  file %s, error: 0x%x\n",pFileIn, err);
        free(pArgbPlane);
        pArgbPlane = M4OSA_NULL;
        goto cleanup;
    }

    rgbPlane1.pac_data =
        (M4VIFI_UInt8*)M4OSA_32bitAlignedMalloc(frameSize_rgb888,
                                            M4VS, (M4OSA_Char*)"RGB888 plane1");
    if(rgbPlane1.pac_data == M4OSA_NULL) {
        M4OSA_TRACE1_0("M4VSS3GPP_internalConvertAndResizeARGB8888toYUV420 \
            Failed to allocate memory for rgb plane1");
        free(pArgbPlane);
        return M4ERR_ALLOC;
    }

    rgbPlane1.u_height = height;
    rgbPlane1.u_width = width;
    rgbPlane1.u_stride = width*3;
    rgbPlane1.u_topleft = 0;


    /** Remove the alpha channel */
    for (i=0, j = 0; i < frameSize_argb; i++) {
        if ((i % 4) == 0) continue;
        rgbPlane1.pac_data[j] = pArgbPlane[i];
        j++;
    }
    free(pArgbPlane);

    /**
     * Check if resizing is required with color conversion */
    if(width != pImagePlanes->u_width || height != pImagePlanes->u_height) {

        frameSize_rgb888 = pImagePlanes->u_width * pImagePlanes->u_height * 3;
        rgbPlane2.pac_data =
            (M4VIFI_UInt8*)M4OSA_32bitAlignedMalloc(frameSize_rgb888, M4VS,
                                                   (M4OSA_Char*)"rgb Plane2");
        if(rgbPlane2.pac_data == M4OSA_NULL) {
            M4OSA_TRACE1_0("Failed to allocate memory for rgb plane2");
            free(rgbPlane1.pac_data);
            return M4ERR_ALLOC;
        }
        rgbPlane2.u_height =  pImagePlanes->u_height;
        rgbPlane2.u_width = pImagePlanes->u_width;
        rgbPlane2.u_stride = pImagePlanes->u_width*3;
        rgbPlane2.u_topleft = 0;

        /* Resizing */
        err = M4VIFI_ResizeBilinearRGB888toRGB888(M4OSA_NULL,
                                                  &rgbPlane1, &rgbPlane2);
        free(rgbPlane1.pac_data);
        if(err != M4NO_ERROR) {
            M4OSA_TRACE1_1("error resizing RGB888 to RGB888: 0x%x\n", err);
            free(rgbPlane2.pac_data);
            return err;
        }

        /*Converting Resized RGB888 to YUV420 */
        err = M4VIFI_RGB888toYUV420(M4OSA_NULL, &rgbPlane2, pImagePlanes);
        free(rgbPlane2.pac_data);
        if(err != M4NO_ERROR) {
            M4OSA_TRACE1_1("error converting from RGB888 to YUV: 0x%x\n", err);
            return err;
        }
    } else {
        err = M4VIFI_RGB888toYUV420(M4OSA_NULL, &rgbPlane1, pImagePlanes);
        if(err != M4NO_ERROR) {
            M4OSA_TRACE1_1("error when converting from RGB to YUV: 0x%x\n", err);
        }
        free(rgbPlane1.pac_data);
    }
cleanup:
    M4OSA_TRACE3_0("M4VSS3GPP_internalConvertAndResizeARGB8888toYUV420 exit");
    return err;
}

M4OSA_ERR M4VSS3GPP_intApplyRenderingMode(M4VSS3GPP_InternalEditContext *pC,
                                          M4xVSS_MediaRendering renderingMode,
                                          M4VIFI_ImagePlane* pInplane,
                                          M4VIFI_ImagePlane* pOutplane) {

    M4OSA_ERR err = M4NO_ERROR;
    M4AIR_Params airParams;
    M4VIFI_ImagePlane pImagePlanesTemp[3];
    M4OSA_UInt32 i = 0;

    if (renderingMode == M4xVSS_kBlackBorders) {
        memset((void *)pOutplane[0].pac_data, Y_PLANE_BORDER_VALUE,
               (pOutplane[0].u_height*pOutplane[0].u_stride));
        memset((void *)pOutplane[1].pac_data, U_PLANE_BORDER_VALUE,
               (pOutplane[1].u_height*pOutplane[1].u_stride));
        memset((void *)pOutplane[2].pac_data, V_PLANE_BORDER_VALUE,
               (pOutplane[2].u_height*pOutplane[2].u_stride));
    }

    if (renderingMode == M4xVSS_kResizing) {
        /**
        * Call the resize filter.
        * From the intermediate frame to the encoder image plane */
        err = M4VIFI_ResizeBilinearYUV420toYUV420(M4OSA_NULL,
                                                  pInplane, pOutplane);
        if (M4NO_ERROR != err) {
            M4OSA_TRACE1_1("M4VSS3GPP_intApplyRenderingMode: \
                M4ViFilResizeBilinearYUV420toYUV420 returns 0x%x!", err);
            return err;
        }
    } else {
        M4VIFI_ImagePlane* pPlaneTemp = M4OSA_NULL;
        M4OSA_UInt8* pOutPlaneY =
            pOutplane[0].pac_data + pOutplane[0].u_topleft;
        M4OSA_UInt8* pOutPlaneU =
            pOutplane[1].pac_data + pOutplane[1].u_topleft;
        M4OSA_UInt8* pOutPlaneV =
            pOutplane[2].pac_data + pOutplane[2].u_topleft;
        M4OSA_UInt8* pInPlaneY = M4OSA_NULL;
        M4OSA_UInt8* pInPlaneU = M4OSA_NULL;
        M4OSA_UInt8* pInPlaneV = M4OSA_NULL;

        /* To keep media aspect ratio*/
        /* Initialize AIR Params*/
        airParams.m_inputCoord.m_x = 0;
        airParams.m_inputCoord.m_y = 0;
        airParams.m_inputSize.m_height = pInplane->u_height;
        airParams.m_inputSize.m_width = pInplane->u_width;
        airParams.m_outputSize.m_width = pOutplane->u_width;
        airParams.m_outputSize.m_height = pOutplane->u_height;
        airParams.m_bOutputStripe = M4OSA_FALSE;
        airParams.m_outputOrientation = M4COMMON_kOrientationTopLeft;

        /**
        Media rendering: Black borders*/
        if (renderingMode == M4xVSS_kBlackBorders) {
            pImagePlanesTemp[0].u_width = pOutplane[0].u_width;
            pImagePlanesTemp[0].u_height = pOutplane[0].u_height;
            pImagePlanesTemp[0].u_stride = pOutplane[0].u_width;
            pImagePlanesTemp[0].u_topleft = 0;

            pImagePlanesTemp[1].u_width = pOutplane[1].u_width;
            pImagePlanesTemp[1].u_height = pOutplane[1].u_height;
            pImagePlanesTemp[1].u_stride = pOutplane[1].u_width;
            pImagePlanesTemp[1].u_topleft = 0;

            pImagePlanesTemp[2].u_width = pOutplane[2].u_width;
            pImagePlanesTemp[2].u_height = pOutplane[2].u_height;
            pImagePlanesTemp[2].u_stride = pOutplane[2].u_width;
            pImagePlanesTemp[2].u_topleft = 0;

            /**
             * Allocates plan in local image plane structure */
            pImagePlanesTemp[0].pac_data =
                (M4OSA_UInt8*)M4OSA_32bitAlignedMalloc(
                    pImagePlanesTemp[0].u_width * pImagePlanesTemp[0].u_height,
                    M4VS, (M4OSA_Char *)"pImagePlaneTemp Y") ;
            if (pImagePlanesTemp[0].pac_data == M4OSA_NULL) {
                M4OSA_TRACE1_0("M4VSS3GPP_intApplyRenderingMode: Alloc Error");
                return M4ERR_ALLOC;
            }
            pImagePlanesTemp[1].pac_data =
                (M4OSA_UInt8*)M4OSA_32bitAlignedMalloc(
                    pImagePlanesTemp[1].u_width * pImagePlanesTemp[1].u_height,
                    M4VS, (M4OSA_Char *)"pImagePlaneTemp U") ;
            if (pImagePlanesTemp[1].pac_data == M4OSA_NULL) {
                M4OSA_TRACE1_0("M4VSS3GPP_intApplyRenderingMode: Alloc Error");
                free(pImagePlanesTemp[0].pac_data);
                return M4ERR_ALLOC;
            }
            pImagePlanesTemp[2].pac_data =
                (M4OSA_UInt8*)M4OSA_32bitAlignedMalloc(
                    pImagePlanesTemp[2].u_width * pImagePlanesTemp[2].u_height,
                    M4VS, (M4OSA_Char *)"pImagePlaneTemp V") ;
            if (pImagePlanesTemp[2].pac_data == M4OSA_NULL) {
                M4OSA_TRACE1_0("M4VSS3GPP_intApplyRenderingMode: Alloc Error");
                free(pImagePlanesTemp[0].pac_data);
                free(pImagePlanesTemp[1].pac_data);
                return M4ERR_ALLOC;
            }

            pInPlaneY = pImagePlanesTemp[0].pac_data ;
            pInPlaneU = pImagePlanesTemp[1].pac_data ;
            pInPlaneV = pImagePlanesTemp[2].pac_data ;

            memset((void *)pImagePlanesTemp[0].pac_data, Y_PLANE_BORDER_VALUE,
                (pImagePlanesTemp[0].u_height*pImagePlanesTemp[0].u_stride));
            memset((void *)pImagePlanesTemp[1].pac_data, U_PLANE_BORDER_VALUE,
                (pImagePlanesTemp[1].u_height*pImagePlanesTemp[1].u_stride));
            memset((void *)pImagePlanesTemp[2].pac_data, V_PLANE_BORDER_VALUE,
                (pImagePlanesTemp[2].u_height*pImagePlanesTemp[2].u_stride));

            M4OSA_UInt32 height =
                (pInplane->u_height * pOutplane->u_width) /pInplane->u_width;

            if (height <= pOutplane->u_height) {
                /**
                 * Black borders will be on the top and the bottom side */
                airParams.m_outputSize.m_width = pOutplane->u_width;
                airParams.m_outputSize.m_height = height;
                /**
                 * Number of lines at the top */
                pImagePlanesTemp[0].u_topleft =
                    (M4xVSS_ABS((M4OSA_Int32)(pImagePlanesTemp[0].u_height -
                      airParams.m_outputSize.m_height)>>1)) *
                      pImagePlanesTemp[0].u_stride;
                pImagePlanesTemp[0].u_height = airParams.m_outputSize.m_height;
                pImagePlanesTemp[1].u_topleft =
                    (M4xVSS_ABS((M4OSA_Int32)(pImagePlanesTemp[1].u_height -
                     (airParams.m_outputSize.m_height>>1)))>>1) *
                     pImagePlanesTemp[1].u_stride;
                pImagePlanesTemp[1].u_height =
                    airParams.m_outputSize.m_height>>1;
                pImagePlanesTemp[2].u_topleft =
                    (M4xVSS_ABS((M4OSA_Int32)(pImagePlanesTemp[2].u_height -
                     (airParams.m_outputSize.m_height>>1)))>>1) *
                     pImagePlanesTemp[2].u_stride;
                pImagePlanesTemp[2].u_height =
                    airParams.m_outputSize.m_height>>1;
            } else {
                /**
                 * Black borders will be on the left and right side */
                airParams.m_outputSize.m_height = pOutplane->u_height;
                airParams.m_outputSize.m_width =
                    (M4OSA_UInt32)((pInplane->u_width * pOutplane->u_height)/pInplane->u_height);

                pImagePlanesTemp[0].u_topleft =
                    (M4xVSS_ABS((M4OSA_Int32)(pImagePlanesTemp[0].u_width -
                     airParams.m_outputSize.m_width)>>1));
                pImagePlanesTemp[0].u_width = airParams.m_outputSize.m_width;
                pImagePlanesTemp[1].u_topleft =
                    (M4xVSS_ABS((M4OSA_Int32)(pImagePlanesTemp[1].u_width -
                     (airParams.m_outputSize.m_width>>1)))>>1);
                pImagePlanesTemp[1].u_width = airParams.m_outputSize.m_width>>1;
                pImagePlanesTemp[2].u_topleft =
                    (M4xVSS_ABS((M4OSA_Int32)(pImagePlanesTemp[2].u_width -
                     (airParams.m_outputSize.m_width>>1)))>>1);
                pImagePlanesTemp[2].u_width = airParams.m_outputSize.m_width>>1;
            }

            /**
             * Width and height have to be even */
            airParams.m_outputSize.m_width =
                (airParams.m_outputSize.m_width>>1)<<1;
            airParams.m_outputSize.m_height =
                (airParams.m_outputSize.m_height>>1)<<1;
            airParams.m_inputSize.m_width =
                (airParams.m_inputSize.m_width>>1)<<1;
            airParams.m_inputSize.m_height =
                (airParams.m_inputSize.m_height>>1)<<1;
            pImagePlanesTemp[0].u_width =
                (pImagePlanesTemp[0].u_width>>1)<<1;
            pImagePlanesTemp[1].u_width =
                (pImagePlanesTemp[1].u_width>>1)<<1;
            pImagePlanesTemp[2].u_width =
                (pImagePlanesTemp[2].u_width>>1)<<1;
            pImagePlanesTemp[0].u_height =
                (pImagePlanesTemp[0].u_height>>1)<<1;
            pImagePlanesTemp[1].u_height =
                (pImagePlanesTemp[1].u_height>>1)<<1;
            pImagePlanesTemp[2].u_height =
                (pImagePlanesTemp[2].u_height>>1)<<1;

            /**
             * Check that values are coherent */
            if (airParams.m_inputSize.m_height ==
                   airParams.m_outputSize.m_height) {
                airParams.m_inputSize.m_width =
                    airParams.m_outputSize.m_width;
            } else if (airParams.m_inputSize.m_width ==
                          airParams.m_outputSize.m_width) {
                airParams.m_inputSize.m_height =
                    airParams.m_outputSize.m_height;
            }
            pPlaneTemp = pImagePlanesTemp;
        }

        /**
         * Media rendering: Cropping*/
        if (renderingMode == M4xVSS_kCropping) {
            airParams.m_outputSize.m_height = pOutplane->u_height;
            airParams.m_outputSize.m_width = pOutplane->u_width;
            if ((airParams.m_outputSize.m_height *
                 airParams.m_inputSize.m_width)/airParams.m_outputSize.m_width <
                  airParams.m_inputSize.m_height) {
                /* Height will be cropped */
                airParams.m_inputSize.m_height =
                    (M4OSA_UInt32)((airParams.m_outputSize.m_height *
                     airParams.m_inputSize.m_width)/airParams.m_outputSize.m_width);
                airParams.m_inputSize.m_height =
                    (airParams.m_inputSize.m_height>>1)<<1;
                airParams.m_inputCoord.m_y =
                    (M4OSA_Int32)((M4OSA_Int32)((pInplane->u_height -
                     airParams.m_inputSize.m_height))>>1);
            } else {
                /* Width will be cropped */
                airParams.m_inputSize.m_width =
                    (M4OSA_UInt32)((airParams.m_outputSize.m_width *
                     airParams.m_inputSize.m_height)/airParams.m_outputSize.m_height);
                airParams.m_inputSize.m_width =
                    (airParams.m_inputSize.m_width>>1)<<1;
                airParams.m_inputCoord.m_x =
                    (M4OSA_Int32)((M4OSA_Int32)((pInplane->u_width -
                     airParams.m_inputSize.m_width))>>1);
            }
            pPlaneTemp = pOutplane;
        }
        /**
        * Call AIR functions */
        if (M4OSA_NULL == pC->m_air_context) {
            err = M4AIR_create(&pC->m_air_context, M4AIR_kYUV420P);
            if(err != M4NO_ERROR) {
                M4OSA_TRACE1_1("M4VSS3GPP_intApplyRenderingMode: \
                    M4AIR_create returned error 0x%x", err);
                goto cleanUp;
            }
        }

        err = M4AIR_configure(pC->m_air_context, &airParams);
        if (err != M4NO_ERROR) {
            M4OSA_TRACE1_1("M4VSS3GPP_intApplyRenderingMode: \
                Error when configuring AIR: 0x%x", err);
            M4AIR_cleanUp(pC->m_air_context);
            goto cleanUp;
        }

        err = M4AIR_get(pC->m_air_context, pInplane, pPlaneTemp);
        if (err != M4NO_ERROR) {
            M4OSA_TRACE1_1("M4VSS3GPP_intApplyRenderingMode: \
                Error when getting AIR plane: 0x%x", err);
            M4AIR_cleanUp(pC->m_air_context);
            goto cleanUp;
        }

        if (renderingMode == M4xVSS_kBlackBorders) {
            for (i=0; i<pOutplane[0].u_height; i++) {
                memcpy((void *)pOutPlaneY, (void *)pInPlaneY,
                        pOutplane[0].u_width);
                pInPlaneY += pOutplane[0].u_width;
                pOutPlaneY += pOutplane[0].u_stride;
            }
            for (i=0; i<pOutplane[1].u_height; i++) {
                memcpy((void *)pOutPlaneU, (void *)pInPlaneU,
                        pOutplane[1].u_width);
                pInPlaneU += pOutplane[1].u_width;
                pOutPlaneU += pOutplane[1].u_stride;
            }
            for (i=0; i<pOutplane[2].u_height; i++) {
                memcpy((void *)pOutPlaneV, (void *)pInPlaneV,
                        pOutplane[2].u_width);
                pInPlaneV += pOutplane[2].u_width;
                pOutPlaneV += pOutplane[2].u_stride;
            }
        }
    }
cleanUp:
    if (renderingMode == M4xVSS_kBlackBorders) {
        for (i=0; i<3; i++) {
            if (pImagePlanesTemp[i].pac_data != M4OSA_NULL) {
                free(pImagePlanesTemp[i].pac_data);
                pImagePlanesTemp[i].pac_data = M4OSA_NULL;
            }
        }
    }
    return err;
}

M4OSA_ERR M4VSS3GPP_intSetYuv420PlaneFromARGB888 (
                                        M4VSS3GPP_InternalEditContext *pC,
                                        M4VSS3GPP_ClipContext* pClipCtxt) {

    M4OSA_ERR err= M4NO_ERROR;

    // Allocate memory for YUV plane
    pClipCtxt->pPlaneYuv =
     (M4VIFI_ImagePlane*)M4OSA_32bitAlignedMalloc(
        3*sizeof(M4VIFI_ImagePlane), M4VS,
        (M4OSA_Char*)"pPlaneYuv");

    if (pClipCtxt->pPlaneYuv == M4OSA_NULL) {
        return M4ERR_ALLOC;
    }

    pClipCtxt->pPlaneYuv[0].u_height =
        pClipCtxt->pSettings->ClipProperties.uiStillPicHeight;
    pClipCtxt->pPlaneYuv[0].u_width =
        pClipCtxt->pSettings->ClipProperties.uiStillPicWidth;
    pClipCtxt->pPlaneYuv[0].u_stride = pClipCtxt->pPlaneYuv[0].u_width;
    pClipCtxt->pPlaneYuv[0].u_topleft = 0;

    pClipCtxt->pPlaneYuv[0].pac_data =
     (M4VIFI_UInt8*)M4OSA_32bitAlignedMalloc(
         pClipCtxt->pPlaneYuv[0].u_height * pClipCtxt->pPlaneYuv[0].u_width * 1.5,
         M4VS, (M4OSA_Char*)"imageClip YUV data");
    if (pClipCtxt->pPlaneYuv[0].pac_data == M4OSA_NULL) {
        free(pClipCtxt->pPlaneYuv);
        return M4ERR_ALLOC;
    }

    pClipCtxt->pPlaneYuv[1].u_height = pClipCtxt->pPlaneYuv[0].u_height >>1;
    pClipCtxt->pPlaneYuv[1].u_width = pClipCtxt->pPlaneYuv[0].u_width >> 1;
    pClipCtxt->pPlaneYuv[1].u_stride = pClipCtxt->pPlaneYuv[1].u_width;
    pClipCtxt->pPlaneYuv[1].u_topleft = 0;
    pClipCtxt->pPlaneYuv[1].pac_data = (M4VIFI_UInt8*)(
     pClipCtxt->pPlaneYuv[0].pac_data +
      pClipCtxt->pPlaneYuv[0].u_height * pClipCtxt->pPlaneYuv[0].u_width);

    pClipCtxt->pPlaneYuv[2].u_height = pClipCtxt->pPlaneYuv[0].u_height >>1;
    pClipCtxt->pPlaneYuv[2].u_width = pClipCtxt->pPlaneYuv[0].u_width >> 1;
    pClipCtxt->pPlaneYuv[2].u_stride = pClipCtxt->pPlaneYuv[2].u_width;
    pClipCtxt->pPlaneYuv[2].u_topleft = 0;
    pClipCtxt->pPlaneYuv[2].pac_data = (M4VIFI_UInt8*)(
     pClipCtxt->pPlaneYuv[1].pac_data +
      pClipCtxt->pPlaneYuv[1].u_height * pClipCtxt->pPlaneYuv[1].u_width);

    err = M4VSS3GPP_internalConvertAndResizeARGB8888toYUV420 (
        pClipCtxt->pSettings->pFile,
        pC->pOsaFileReadPtr,
        pClipCtxt->pPlaneYuv,
        pClipCtxt->pSettings->ClipProperties.uiStillPicWidth,
        pClipCtxt->pSettings->ClipProperties.uiStillPicHeight);
    if (M4NO_ERROR != err) {
        free(pClipCtxt->pPlaneYuv[0].pac_data);
        free(pClipCtxt->pPlaneYuv);
        return err;
    }

    // Set the YUV data to the decoder using setoption
    err = pClipCtxt->ShellAPI.m_pVideoDecoder->m_pFctSetOption (
        pClipCtxt->pViDecCtxt,
        M4DECODER_kOptionID_DecYuvData,
        (M4OSA_DataOption)pClipCtxt->pPlaneYuv);
    if (M4NO_ERROR != err) {
        free(pClipCtxt->pPlaneYuv[0].pac_data);
        free(pClipCtxt->pPlaneYuv);
        return err;
    }

    pClipCtxt->pSettings->ClipProperties.bSetImageData = M4OSA_TRUE;

    // Allocate Yuv plane with effect
    pClipCtxt->pPlaneYuvWithEffect =
     (M4VIFI_ImagePlane*)M4OSA_32bitAlignedMalloc(
         3*sizeof(M4VIFI_ImagePlane), M4VS,
         (M4OSA_Char*)"pPlaneYuvWithEffect");
    if (pClipCtxt->pPlaneYuvWithEffect == M4OSA_NULL) {
        free(pClipCtxt->pPlaneYuv[0].pac_data);
        free(pClipCtxt->pPlaneYuv);
        return M4ERR_ALLOC;
    }

    pClipCtxt->pPlaneYuvWithEffect[0].u_height = pC->ewc.uiVideoHeight;
    pClipCtxt->pPlaneYuvWithEffect[0].u_width = pC->ewc.uiVideoWidth;
    pClipCtxt->pPlaneYuvWithEffect[0].u_stride = pC->ewc.uiVideoWidth;
    pClipCtxt->pPlaneYuvWithEffect[0].u_topleft = 0;

    pClipCtxt->pPlaneYuvWithEffect[0].pac_data =
     (M4VIFI_UInt8*)M4OSA_32bitAlignedMalloc(
         pC->ewc.uiVideoHeight * pC->ewc.uiVideoWidth * 1.5,
         M4VS, (M4OSA_Char*)"imageClip YUV data");
    if (pClipCtxt->pPlaneYuvWithEffect[0].pac_data == M4OSA_NULL) {
        free(pClipCtxt->pPlaneYuv[0].pac_data);
        free(pClipCtxt->pPlaneYuv);
        free(pClipCtxt->pPlaneYuvWithEffect);
        return M4ERR_ALLOC;
    }

    pClipCtxt->pPlaneYuvWithEffect[1].u_height =
        pClipCtxt->pPlaneYuvWithEffect[0].u_height >>1;
    pClipCtxt->pPlaneYuvWithEffect[1].u_width =
        pClipCtxt->pPlaneYuvWithEffect[0].u_width >> 1;
    pClipCtxt->pPlaneYuvWithEffect[1].u_stride =
        pClipCtxt->pPlaneYuvWithEffect[1].u_width;
    pClipCtxt->pPlaneYuvWithEffect[1].u_topleft = 0;
    pClipCtxt->pPlaneYuvWithEffect[1].pac_data = (M4VIFI_UInt8*)(
        pClipCtxt->pPlaneYuvWithEffect[0].pac_data +
         pClipCtxt->pPlaneYuvWithEffect[0].u_height * pClipCtxt->pPlaneYuvWithEffect[0].u_width);

    pClipCtxt->pPlaneYuvWithEffect[2].u_height =
        pClipCtxt->pPlaneYuvWithEffect[0].u_height >>1;
    pClipCtxt->pPlaneYuvWithEffect[2].u_width =
        pClipCtxt->pPlaneYuvWithEffect[0].u_width >> 1;
    pClipCtxt->pPlaneYuvWithEffect[2].u_stride =
        pClipCtxt->pPlaneYuvWithEffect[2].u_width;
    pClipCtxt->pPlaneYuvWithEffect[2].u_topleft = 0;
    pClipCtxt->pPlaneYuvWithEffect[2].pac_data = (M4VIFI_UInt8*)(
        pClipCtxt->pPlaneYuvWithEffect[1].pac_data +
         pClipCtxt->pPlaneYuvWithEffect[1].u_height * pClipCtxt->pPlaneYuvWithEffect[1].u_width);

    err = pClipCtxt->ShellAPI.m_pVideoDecoder->m_pFctSetOption(
        pClipCtxt->pViDecCtxt, M4DECODER_kOptionID_YuvWithEffectContiguous,
        (M4OSA_DataOption)pClipCtxt->pPlaneYuvWithEffect);
    if (M4NO_ERROR != err) {
        free(pClipCtxt->pPlaneYuv[0].pac_data);
        free(pClipCtxt->pPlaneYuv);
        free(pClipCtxt->pPlaneYuvWithEffect);
        return err;
    }

    return M4NO_ERROR;
}

M4OSA_ERR M4VSS3GPP_intRenderFrameWithEffect(M4VSS3GPP_InternalEditContext *pC,
                                             M4VSS3GPP_ClipContext* pClipCtxt,
                                             M4_MediaTime ts,
                                             M4OSA_Bool bIsClip1,
                                             M4VIFI_ImagePlane *pResizePlane,
                                             M4VIFI_ImagePlane *pPlaneNoResize,
                                             M4VIFI_ImagePlane *pPlaneOut) {

    M4OSA_ERR err = M4NO_ERROR;
    M4OSA_UInt8 numEffects = 0;
    M4VIFI_ImagePlane *pDecoderRenderFrame = M4OSA_NULL;
    M4OSA_UInt32 yuvFrameWidth = 0, yuvFrameHeight = 0;
    M4VIFI_ImagePlane* pTmp = M4OSA_NULL;
    M4VIFI_ImagePlane pTemp[3];
    M4OSA_UInt8 i = 0;
    M4OSA_Bool bSkipFramingEffect = M4OSA_FALSE;

    memset((void *)pTemp, 0, 3*sizeof(M4VIFI_ImagePlane));
    /* Resize or rotate case */
    if (M4OSA_NULL != pClipCtxt->m_pPreResizeFrame) {
        /**
        * If we do modify the image, we need an intermediate image plane */
        err = M4VSS3GPP_intAllocateYUV420(pResizePlane,
            pClipCtxt->m_pPreResizeFrame[0].u_width,
            pClipCtxt->m_pPreResizeFrame[0].u_height);
        if (M4NO_ERROR != err) {
            M4OSA_TRACE1_1("M4VSS3GPP_intRenderFrameWithEffect: \
             M4VSS3GPP_intAllocateYUV420 returns 0x%x", err);
            return err;
        }

        if ((pClipCtxt->pSettings->FileType ==
              M4VIDEOEDITING_kFileType_ARGB8888) &&
            (pC->nbActiveEffects == 0) &&
            (pClipCtxt->bGetYuvDataFromDecoder == M4OSA_FALSE)) {

            err = pClipCtxt->ShellAPI.m_pVideoDecoder->m_pFctSetOption(
                      pClipCtxt->pViDecCtxt,
                      M4DECODER_kOptionID_EnableYuvWithEffect,
                      (M4OSA_DataOption)M4OSA_TRUE);
            if (M4NO_ERROR == err) {
                pClipCtxt->ShellAPI.m_pVideoDecoder->m_pFctRender(
                    pClipCtxt->pViDecCtxt, &ts,
                    pClipCtxt->pPlaneYuvWithEffect, M4OSA_TRUE);
            }

        } else {
            if (pClipCtxt->pSettings->FileType ==
              M4VIDEOEDITING_kFileType_ARGB8888) {
                err = pClipCtxt->ShellAPI.m_pVideoDecoder->m_pFctSetOption(
                          pClipCtxt->pViDecCtxt,
                          M4DECODER_kOptionID_EnableYuvWithEffect,
                          (M4OSA_DataOption)M4OSA_FALSE);
            }
            if (M4NO_ERROR == err) {
                err = pClipCtxt->ShellAPI.m_pVideoDecoder->m_pFctRender(
                    pClipCtxt->pViDecCtxt, &ts,
                    pClipCtxt->m_pPreResizeFrame, M4OSA_TRUE);
            }

        }
        if (M4NO_ERROR != err) {
            M4OSA_TRACE1_1("M4VSS3GPP_intRenderFrameWithEffect: \
                returns error 0x%x", err);
            return err;
        }

        if (pClipCtxt->pSettings->FileType !=
                M4VIDEOEDITING_kFileType_ARGB8888) {
            if (0 != pClipCtxt->pSettings->ClipProperties.videoRotationDegrees) {
                // Save width and height of un-rotated frame
                yuvFrameWidth = pClipCtxt->m_pPreResizeFrame[0].u_width;
                yuvFrameHeight = pClipCtxt->m_pPreResizeFrame[0].u_height;
                err = M4VSS3GPP_intRotateVideo(pClipCtxt->m_pPreResizeFrame,
                    pClipCtxt->pSettings->ClipProperties.videoRotationDegrees);
                if (M4NO_ERROR != err) {
                    M4OSA_TRACE1_1("M4VSS3GPP_intRenderFrameWithEffect: \
                        rotateVideo() returns error 0x%x", err);
                    return err;
                }
                /* Set the new video size for temporary buffer */
                M4VSS3GPP_intSetYUV420Plane(pResizePlane,
                    pClipCtxt->m_pPreResizeFrame[0].u_width,
                    pClipCtxt->m_pPreResizeFrame[0].u_height);
            }
        }

        if (bIsClip1 == M4OSA_TRUE) {
            pC->bIssecondClip = M4OSA_FALSE;
            numEffects = pC->nbActiveEffects;
        } else {
            numEffects = pC->nbActiveEffects1;
            pC->bIssecondClip = M4OSA_TRUE;
        }

        if ( numEffects > 0) {
            pClipCtxt->bGetYuvDataFromDecoder = M4OSA_TRUE;
            /* If video frame need to be resized or rotated,
             * then apply the overlay after the frame was rendered with rendering mode.
             * Here skip the framing(overlay) effect when applying video Effect. */
            bSkipFramingEffect = M4OSA_TRUE;
            err = M4VSS3GPP_intApplyVideoEffect(pC,
                      pClipCtxt->m_pPreResizeFrame, pResizePlane, bSkipFramingEffect);
            if (M4NO_ERROR != err) {
                M4OSA_TRACE1_1("M4VSS3GPP_intRenderFrameWithEffect: \
                    M4VSS3GPP_intApplyVideoEffect() err 0x%x", err);
                return err;
            }
            pDecoderRenderFrame= pResizePlane;
        } else {
            pDecoderRenderFrame = pClipCtxt->m_pPreResizeFrame;
        }
        /* Do rendering mode */
        if ((pClipCtxt->bGetYuvDataFromDecoder == M4OSA_TRUE) ||
            (pClipCtxt->pSettings->FileType !=
             M4VIDEOEDITING_kFileType_ARGB8888)) {
            if (bIsClip1 == M4OSA_TRUE) {
                if (pC->bClip1ActiveFramingEffect == M4OSA_TRUE) {
                    err = M4VSS3GPP_intAllocateYUV420(pTemp,
                            pPlaneOut[0].u_width, pPlaneOut[0].u_height);
                    if (M4NO_ERROR != err) {
                        M4OSA_TRACE1_1("M4VSS3GPP_intVPP: \
                            M4VSS3GPP_intAllocateYUV420 error 0x%x", err);
                        pC->ewc.VppError = err;
                        return M4NO_ERROR;
                    }
                    pTmp = pTemp;
                } else {
                    pTmp = pC->yuv1;
                }
                err = M4VSS3GPP_intApplyRenderingMode (pC,
                        pClipCtxt->pSettings->xVSS.MediaRendering,
                        pDecoderRenderFrame,pTmp);
            } else {
                if (pC->bClip2ActiveFramingEffect == M4OSA_TRUE) {
                    err = M4VSS3GPP_intAllocateYUV420(pTemp,
                            pPlaneOut[0].u_width, pPlaneOut[0].u_height);
                    if (M4NO_ERROR != err) {
                        M4OSA_TRACE1_1("M4VSS3GPP_intVPP: \
                            M4VSS3GPP_intAllocateYUV420 error 0x%x", err);
                        pC->ewc.VppError = err;
                        return M4NO_ERROR;
                    }
                    pTmp = pTemp;
                } else {
                    pTmp = pC->yuv2;
                }
                err = M4VSS3GPP_intApplyRenderingMode (pC,
                        pClipCtxt->pSettings->xVSS.MediaRendering,
                        pDecoderRenderFrame,pTmp);
            }
            if (M4NO_ERROR != err) {
                M4OSA_TRACE1_1("M4VSS3GPP_intRenderFrameWithEffect: \
                    M4VSS3GPP_intApplyRenderingMode error 0x%x ", err);
                for (i=0; i<3; i++) {
                    if (pTemp[i].pac_data != M4OSA_NULL) {
                        free(pTemp[i].pac_data);
                        pTemp[i].pac_data = M4OSA_NULL;
                    }
                }
                return err;
            }
            /* Apply overlay if overlay exist*/
            if (bIsClip1 == M4OSA_TRUE) {
                if (pC->bClip1ActiveFramingEffect == M4OSA_TRUE) {
                    err = M4VSS3GPP_intApplyVideoOverlay(pC,
                        pTemp, pC->yuv1);
                }
                pClipCtxt->lastDecodedPlane = pC->yuv1;
            } else {
                if (pC->bClip2ActiveFramingEffect == M4OSA_TRUE) {
                    err = M4VSS3GPP_intApplyVideoOverlay(pC,
                        pTemp, pC->yuv2);
                }
                pClipCtxt->lastDecodedPlane = pC->yuv2;
            }
            if (M4NO_ERROR != err) {
                M4OSA_TRACE1_1("M4VSS3GPP_intVPP: \
                    M4VSS3GPP_intApplyVideoOverlay) error 0x%x ", err);
                pC->ewc.VppError = err;
                for (i=0; i<3; i++) {
                    if (pTemp[i].pac_data != M4OSA_NULL) {
                        free(pTemp[i].pac_data);
                        pTemp[i].pac_data = M4OSA_NULL;
                    }
                }
                return M4NO_ERROR;
            }
        } else {
            pClipCtxt->lastDecodedPlane = pClipCtxt->pPlaneYuvWithEffect;
        }
        // free the temp buffer
        for (i=0; i<3; i++) {
            if (pTemp[i].pac_data != M4OSA_NULL) {
                free(pTemp[i].pac_data);
                pTemp[i].pac_data = M4OSA_NULL;
            }
        }

        if ((pClipCtxt->pSettings->FileType ==
                 M4VIDEOEDITING_kFileType_ARGB8888) &&
             (pC->nbActiveEffects == 0) &&
             (pClipCtxt->bGetYuvDataFromDecoder == M4OSA_TRUE)) {
            if (bIsClip1 == M4OSA_TRUE) {
                err = pClipCtxt->ShellAPI.m_pVideoDecoder->m_pFctSetOption(
                    pClipCtxt->pViDecCtxt,
                    M4DECODER_kOptionID_YuvWithEffectNonContiguous,
                    (M4OSA_DataOption)pC->yuv1);
            } else {
                err = pClipCtxt->ShellAPI.m_pVideoDecoder->m_pFctSetOption(
                    pClipCtxt->pViDecCtxt,
                    M4DECODER_kOptionID_YuvWithEffectNonContiguous,
                    (M4OSA_DataOption)pC->yuv2);
            }
            if (M4NO_ERROR != err) {
                M4OSA_TRACE1_1("M4VSS3GPP_intRenderFrameWithEffect: \
                    null decoder setOption error 0x%x ", err);
                return err;
            }
            pClipCtxt->bGetYuvDataFromDecoder = M4OSA_FALSE;
        }

        // Reset original width and height for resize frame plane
        if (0 != pClipCtxt->pSettings->ClipProperties.videoRotationDegrees &&
            180 != pClipCtxt->pSettings->ClipProperties.videoRotationDegrees) {

            M4VSS3GPP_intSetYUV420Plane(pClipCtxt->m_pPreResizeFrame,
                                        yuvFrameWidth, yuvFrameHeight);
        }

    } else {
        /* No rotate or no resize case*/
        if (bIsClip1 == M4OSA_TRUE) {
            numEffects = pC->nbActiveEffects;
        } else {
            numEffects = pC->nbActiveEffects1;
        }

        if(numEffects > 0) {
            err = pClipCtxt->ShellAPI.m_pVideoDecoder->m_pFctRender(
                      pClipCtxt->pViDecCtxt, &ts, pPlaneNoResize, M4OSA_TRUE);
            if (M4NO_ERROR != err) {
                M4OSA_TRACE1_1("M4VSS3GPP_intRenderFrameWithEffect: \
                    Render returns error 0x%x", err);
                return err;
            }

            bSkipFramingEffect = M4OSA_FALSE;
            if (bIsClip1 == M4OSA_TRUE) {
                pC->bIssecondClip = M4OSA_FALSE;
                err = M4VSS3GPP_intApplyVideoEffect(pC, pPlaneNoResize,
                            pC->yuv1, bSkipFramingEffect);
                pClipCtxt->lastDecodedPlane = pC->yuv1;
            } else {
                pC->bIssecondClip = M4OSA_TRUE;
                err = M4VSS3GPP_intApplyVideoEffect(pC, pPlaneNoResize,
                            pC->yuv2, bSkipFramingEffect);
                pClipCtxt->lastDecodedPlane = pC->yuv2;
            }

            if (M4NO_ERROR != err) {
                M4OSA_TRACE1_1("M4VSS3GPP_intRenderFrameWithEffect: \
                    M4VSS3GPP_intApplyVideoEffect error 0x%x", err);
                return err;
            }
        } else {

            if (bIsClip1 == M4OSA_TRUE) {
                pTmp = pC->yuv1;
            } else {
                pTmp = pC->yuv2;
            }
            err = pClipCtxt->ShellAPI.m_pVideoDecoder->m_pFctRender(
                      pClipCtxt->pViDecCtxt, &ts, pTmp, M4OSA_TRUE);
            if (M4NO_ERROR != err) {
                M4OSA_TRACE1_1("M4VSS3GPP_intRenderFrameWithEffect: \
                    Render returns error 0x%x,", err);
                return err;
            }
            pClipCtxt->lastDecodedPlane = pTmp;
        }
        pClipCtxt->iVideoRenderCts = (M4OSA_Int32)ts;
    }

    return err;
}

M4OSA_ERR M4VSS3GPP_intRotateVideo(M4VIFI_ImagePlane* pPlaneIn,
                                   M4OSA_UInt32 rotationDegree) {

    M4OSA_ERR err = M4NO_ERROR;
    M4VIFI_ImagePlane outPlane[3];

    if (rotationDegree != 180) {
        // Swap width and height of in plane
        outPlane[0].u_width = pPlaneIn[0].u_height;
        outPlane[0].u_height = pPlaneIn[0].u_width;
        outPlane[0].u_stride = outPlane[0].u_width;
        outPlane[0].u_topleft = 0;
        outPlane[0].pac_data = (M4OSA_UInt8 *)M4OSA_32bitAlignedMalloc(
            (outPlane[0].u_stride*outPlane[0].u_height), M4VS,
            (M4OSA_Char*)("out Y plane for rotation"));
        if (outPlane[0].pac_data == M4OSA_NULL) {
            return M4ERR_ALLOC;
        }

        outPlane[1].u_width = pPlaneIn[0].u_height/2;
        outPlane[1].u_height = pPlaneIn[0].u_width/2;
        outPlane[1].u_stride = outPlane[1].u_width;
        outPlane[1].u_topleft = 0;
        outPlane[1].pac_data = (M4OSA_UInt8 *)M4OSA_32bitAlignedMalloc(
            (outPlane[1].u_stride*outPlane[1].u_height), M4VS,
            (M4OSA_Char*)("out U plane for rotation"));
        if (outPlane[1].pac_data == M4OSA_NULL) {
            free((void *)outPlane[0].pac_data);
            return M4ERR_ALLOC;
        }

        outPlane[2].u_width = pPlaneIn[0].u_height/2;
        outPlane[2].u_height = pPlaneIn[0].u_width/2;
        outPlane[2].u_stride = outPlane[2].u_width;
        outPlane[2].u_topleft = 0;
        outPlane[2].pac_data = (M4OSA_UInt8 *)M4OSA_32bitAlignedMalloc(
            (outPlane[2].u_stride*outPlane[2].u_height), M4VS,
            (M4OSA_Char*)("out V plane for rotation"));
        if (outPlane[2].pac_data == M4OSA_NULL) {
            free((void *)outPlane[0].pac_data);
            free((void *)outPlane[1].pac_data);
            return M4ERR_ALLOC;
        }
    }

    switch(rotationDegree) {
        case 90:
            M4VIFI_Rotate90RightYUV420toYUV420(M4OSA_NULL, pPlaneIn, outPlane);
            break;

        case 180:
            // In plane rotation, so planeOut = planeIn
            M4VIFI_Rotate180YUV420toYUV420(M4OSA_NULL, pPlaneIn, pPlaneIn);
            break;

        case 270:
            M4VIFI_Rotate90LeftYUV420toYUV420(M4OSA_NULL, pPlaneIn, outPlane);
            break;

        default:
            M4OSA_TRACE1_1("invalid rotation param %d", (int)rotationDegree);
            err = M4ERR_PARAMETER;
            break;
    }

    if (rotationDegree != 180) {
        memset((void *)pPlaneIn[0].pac_data, 0,
            (pPlaneIn[0].u_width*pPlaneIn[0].u_height));
        memset((void *)pPlaneIn[1].pac_data, 0,
            (pPlaneIn[1].u_width*pPlaneIn[1].u_height));
        memset((void *)pPlaneIn[2].pac_data, 0,
            (pPlaneIn[2].u_width*pPlaneIn[2].u_height));
        // Copy Y, U and V planes
        memcpy((void *)pPlaneIn[0].pac_data, (void *)outPlane[0].pac_data,
            (pPlaneIn[0].u_width*pPlaneIn[0].u_height));
        memcpy((void *)pPlaneIn[1].pac_data, (void *)outPlane[1].pac_data,
            (pPlaneIn[1].u_width*pPlaneIn[1].u_height));
        memcpy((void *)pPlaneIn[2].pac_data, (void *)outPlane[2].pac_data,
            (pPlaneIn[2].u_width*pPlaneIn[2].u_height));

        free((void *)outPlane[0].pac_data);
        free((void *)outPlane[1].pac_data);
        free((void *)outPlane[2].pac_data);

        // Swap the width and height of the in plane
        uint32_t temp = 0;
        temp = pPlaneIn[0].u_width;
        pPlaneIn[0].u_width = pPlaneIn[0].u_height;
        pPlaneIn[0].u_height = temp;
        pPlaneIn[0].u_stride = pPlaneIn[0].u_width;

        temp = pPlaneIn[1].u_width;
        pPlaneIn[1].u_width = pPlaneIn[1].u_height;
        pPlaneIn[1].u_height = temp;
        pPlaneIn[1].u_stride = pPlaneIn[1].u_width;

        temp = pPlaneIn[2].u_width;
        pPlaneIn[2].u_width = pPlaneIn[2].u_height;
        pPlaneIn[2].u_height = temp;
        pPlaneIn[2].u_stride = pPlaneIn[2].u_width;
    }

    return err;
}

M4OSA_ERR M4VSS3GPP_intSetYUV420Plane(M4VIFI_ImagePlane* planeIn,
                                      M4OSA_UInt32 width, M4OSA_UInt32 height) {

    M4OSA_ERR err = M4NO_ERROR;

    if (planeIn == M4OSA_NULL) {
        M4OSA_TRACE1_0("NULL in plane, error");
        return M4ERR_PARAMETER;
    }

    planeIn[0].u_width = width;
    planeIn[0].u_height = height;
    planeIn[0].u_stride = planeIn[0].u_width;

    planeIn[1].u_width = width/2;
    planeIn[1].u_height = height/2;
    planeIn[1].u_stride = planeIn[1].u_width;

    planeIn[2].u_width = width/2;
    planeIn[2].u_height = height/2;
    planeIn[2].u_stride = planeIn[1].u_width;

    return err;
}