summaryrefslogtreecommitdiffstats
path: root/libvideoeditor/vss/src/M4xVSS_internal.c
blob: 84959ec932db45ad892001f53344eddaf5e7ece1 (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
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
/*
 * 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    M4xVSS_internal.c
 * @brief    Internal functions of extended Video Studio Service (Video Studio 2.1)
 * @note
 ******************************************************************************
 */
#include "M4OSA_Debug.h"
#include "M4OSA_CharStar.h"

#include "NXPSW_CompilerSwitches.h"

#include "M4VSS3GPP_API.h"
#include "M4VSS3GPP_ErrorCodes.h"

#include "M4xVSS_API.h"
#include "M4xVSS_Internal.h"

/*for rgb16 color effect*/
#include "M4VIFI_Defines.h"
#include "M4VIFI_Clip.h"

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

/* Internal header file of VSS is included because of MMS use case */
#include "M4VSS3GPP_InternalTypes.h"

/*Exif header files to add image rendering support (cropping, black borders)*/
#include "M4EXIFC_CommonAPI.h"
// StageFright encoders require %16 resolution
#include "M4ENCODER_common.h"

#define TRANSPARENT_COLOR 0x7E0

/* Prototype of M4VIFI_xVSS_RGB565toYUV420 function (avoid green effect of transparency color) */
M4VIFI_UInt8 M4VIFI_xVSS_RGB565toYUV420(void *pUserData, M4VIFI_ImagePlane *pPlaneIn,
                                        M4VIFI_ImagePlane *pPlaneOut);


/*special MCS function used only in VideoArtist and VideoStudio to open the media in the normal
 mode. That way the media duration is accurate*/
extern M4OSA_ERR M4MCS_open_normalMode(M4MCS_Context pContext, M4OSA_Void* pFileIn,
                                         M4VIDEOEDITING_FileType InputFileType,
                                         M4OSA_Void* pFileOut, M4OSA_Void* pTempFile);


/**
 ******************************************************************************
 * prototype    M4OSA_ERR M4xVSS_internalStartTranscoding(M4OSA_Context pContext)
 * @brief        This function initializes MCS (3GP transcoder) with the given
 *                parameters
 * @note        The transcoding parameters are given by the internal xVSS context.
 *                This context contains a pointer on the current element of the
 *                chained list of MCS parameters.
 *
 * @param    pContext            (IN) Pointer on the xVSS edit context
 * @return    M4NO_ERROR:            No error
 * @return    M4ERR_PARAMETER:    At least one parameter is M4OSA_NULL
 * @return    M4ERR_ALLOC:        Memory allocation has failed
 ******************************************************************************
 */
M4OSA_ERR M4xVSS_internalStartTranscoding(M4OSA_Context pContext,
                                          M4OSA_UInt32 *rotationDegree)
{
    M4xVSS_Context* xVSS_context = (M4xVSS_Context*)pContext;
    M4OSA_ERR err;
    M4MCS_Context mcs_context;
    M4MCS_OutputParams Params;
    M4MCS_EncodingParams Rates;
    M4OSA_UInt32 i;
    M4VIDEOEDITING_ClipProperties clipProps;

    err = M4MCS_init(&mcs_context, xVSS_context->pFileReadPtr, xVSS_context->pFileWritePtr);
    if(err != M4NO_ERROR)
    {
        M4OSA_TRACE1_1("Error in M4MCS_init: 0x%x", err);
        return err;
    }

    err = M4MCS_open(mcs_context, xVSS_context->pMCScurrentParams->pFileIn,
         xVSS_context->pMCScurrentParams->InputFileType,
             xVSS_context->pMCScurrentParams->pFileOut,
             xVSS_context->pMCScurrentParams->pFileTemp);
    if (err != M4NO_ERROR)
    {
        M4OSA_TRACE1_1("Error in M4MCS_open: 0x%x", err);
        M4MCS_abort(mcs_context);
        return err;
    }

    /** Get the clip properties
     */
    err = M4MCS_getInputFileProperties(mcs_context, &clipProps);
    if (err != M4NO_ERROR) {
        M4OSA_TRACE1_1("Error in M4MCS_getInputFileProperties: 0x%x", err);
        M4MCS_abort(mcs_context);
        return err;
    }
    *rotationDegree = clipProps.videoRotationDegrees;

    /**
     * Fill MCS parameters with the parameters contained in the current element of the
       MCS parameters chained list */
    Params.OutputFileType = xVSS_context->pMCScurrentParams->OutputFileType;
    Params.OutputVideoFormat = xVSS_context->pMCScurrentParams->OutputVideoFormat;
    Params.outputVideoProfile= xVSS_context->pMCScurrentParams->outputVideoProfile;
    Params.outputVideoLevel = xVSS_context->pMCScurrentParams->outputVideoLevel;
    Params.OutputVideoFrameSize = xVSS_context->pMCScurrentParams->OutputVideoFrameSize;
    Params.OutputVideoFrameRate = xVSS_context->pMCScurrentParams->OutputVideoFrameRate;
    Params.OutputAudioFormat = xVSS_context->pMCScurrentParams->OutputAudioFormat;
    Params.OutputAudioSamplingFrequency =
         xVSS_context->pMCScurrentParams->OutputAudioSamplingFrequency;
    Params.bAudioMono = xVSS_context->pMCScurrentParams->bAudioMono;
    Params.pOutputPCMfile = M4OSA_NULL;
    /*FB 2008/10/20: add media rendering parameter to keep aspect ratio*/
    switch(xVSS_context->pMCScurrentParams->MediaRendering)
    {
    case M4xVSS_kResizing:
        Params.MediaRendering = M4MCS_kResizing;
        break;
    case M4xVSS_kCropping:
        Params.MediaRendering = M4MCS_kCropping;
        break;
    case M4xVSS_kBlackBorders:
        Params.MediaRendering = M4MCS_kBlackBorders;
        break;
    default:
        break;
    }
    /**/
    // new params after integrating MCS 2.0
    // Set the number of audio effects; 0 for now.
    Params.nbEffects = 0;

    // Set the audio effect; null for now.
    Params.pEffects = NULL;

    // Set the audio effect; null for now.
    Params.bDiscardExif = M4OSA_FALSE;

    // Set the audio effect; null for now.
    Params.bAdjustOrientation = M4OSA_FALSE;
    // new params after integrating MCS 2.0

    /**
     * Set output parameters */
    err = M4MCS_setOutputParams(mcs_context, &Params);
    if (err != M4NO_ERROR)
    {
        M4OSA_TRACE1_1("Error in M4MCS_setOutputParams: 0x%x", err);
        M4MCS_abort(mcs_context);
        return err;
    }

    Rates.OutputVideoBitrate = xVSS_context->pMCScurrentParams->OutputVideoBitrate;
    Rates.OutputAudioBitrate = xVSS_context->pMCScurrentParams->OutputAudioBitrate;
    Rates.BeginCutTime = 0;
    Rates.EndCutTime = 0;
    Rates.OutputFileSize = 0;

    /*FB: transcoding per parts*/
    Rates.BeginCutTime = xVSS_context->pMCScurrentParams->BeginCutTime;
    Rates.EndCutTime = xVSS_context->pMCScurrentParams->EndCutTime;
    Rates.OutputVideoTimescale = xVSS_context->pMCScurrentParams->OutputVideoTimescale;

    err = M4MCS_setEncodingParams(mcs_context, &Rates);
    if (err != M4NO_ERROR)
    {
        M4OSA_TRACE1_1("Error in M4MCS_setEncodingParams: 0x%x", err);
        M4MCS_abort(mcs_context);
        return err;
    }

    err = M4MCS_checkParamsAndStart(mcs_context);
    if (err != M4NO_ERROR)
    {
        M4OSA_TRACE1_1("Error in M4MCS_checkParamsAndStart: 0x%x", err);
        M4MCS_abort(mcs_context);
        return err;
    }

    /**
     * Save MCS context to be able to call MCS step function in M4xVSS_step function */
    xVSS_context->pMCS_Ctxt = mcs_context;

    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * prototype    M4OSA_ERR M4xVSS_internalStopTranscoding(M4OSA_Context pContext)
 * @brief        This function cleans up MCS (3GP transcoder)
 * @note
 *
 * @param    pContext            (IN) Pointer on the xVSS edit context
 * @return    M4NO_ERROR:            No error
 * @return    M4ERR_PARAMETER:    At least one parameter is M4OSA_NULL
 * @return    M4ERR_ALLOC:        Memory allocation has failed
 ******************************************************************************
 */
M4OSA_ERR M4xVSS_internalStopTranscoding(M4OSA_Context pContext)
{
    M4xVSS_Context* xVSS_context = (M4xVSS_Context*)pContext;
    M4OSA_ERR err;

    err = M4MCS_close(xVSS_context->pMCS_Ctxt);
    if (err != M4NO_ERROR)
    {
        M4OSA_TRACE1_1("M4xVSS_internalStopTranscoding: Error in M4MCS_close: 0x%x", err);
        M4MCS_abort(xVSS_context->pMCS_Ctxt);
        return err;
    }

    /**
     * Free this MCS instance */
    err = M4MCS_cleanUp(xVSS_context->pMCS_Ctxt);
    if (err != M4NO_ERROR)
    {
        M4OSA_TRACE1_1("M4xVSS_internalStopTranscoding: Error in M4MCS_cleanUp: 0x%x", err);
        return err;
    }

    xVSS_context->pMCS_Ctxt = M4OSA_NULL;

    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * M4OSA_ERR M4xVSS_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 Image 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 M4xVSS_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 = (width * height * 3); //Size of RGB888 data.
    M4OSA_UInt32 i = 0,j= 0;
    M4OSA_ERR err=M4NO_ERROR;


    M4OSA_UInt8 *pTmpData = (M4OSA_UInt8*) M4OSA_32bitAlignedMalloc(frameSize_argb,
         M4VS, (M4OSA_Char*)"Image argb data");
        M4OSA_TRACE1_0("M4xVSS_internalConvertAndResizeARGB8888toYUV420 Entering :");
    if(pTmpData == M4OSA_NULL) {
        M4OSA_TRACE1_0("M4xVSS_internalConvertAndResizeARGB8888toYUV420 :\
            Failed to allocate memory for Image clip");
        return M4ERR_ALLOC;
    }

    M4OSA_TRACE1_2("M4xVSS_internalConvertAndResizeARGB8888toYUV420 :width and height %d %d",
        width ,height);
    /* Get file size (mandatory for chunk decoding) */
    err = pFileReadPtr->openRead(&pARGBIn, pFileIn, M4OSA_kFileRead);
    if(err != M4NO_ERROR)
    {
        M4OSA_TRACE1_2("M4xVSS_internalConvertAndResizeARGB8888toYUV420 :\
            Can't open input ARGB8888 file %s, error: 0x%x\n",pFileIn, err);
        free(pTmpData);
        pTmpData = M4OSA_NULL;
        goto cleanup;
    }

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

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

    rgbPlane1.pac_data = (M4VIFI_UInt8*)M4OSA_32bitAlignedMalloc(frameSize, M4VS,
         (M4OSA_Char*)"Image clip RGB888 data");
    if(rgbPlane1.pac_data == M4OSA_NULL)
    {
        M4OSA_TRACE1_0("M4xVSS_internalConvertAndResizeARGB8888toYUV420 \
            Failed to allocate memory for Image clip");
        free(pTmpData);
        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] = pTmpData[i];
        j++;
    }
        free(pTmpData);

    /* To Check if resizing is required with color conversion */
    if(width != pImagePlanes->u_width || height != pImagePlanes->u_height)
    {
        M4OSA_TRACE1_0("M4xVSS_internalConvertAndResizeARGB8888toYUV420 Resizing :");
        frameSize =  ( pImagePlanes->u_width * pImagePlanes->u_height * 3);
        rgbPlane2.pac_data = (M4VIFI_UInt8*)M4OSA_32bitAlignedMalloc(frameSize, M4VS,
             (M4OSA_Char*)"Image clip RGB888 data");
        if(rgbPlane2.pac_data == M4OSA_NULL)
        {
            M4OSA_TRACE1_0("Failed to allocate memory for Image clip");
            free(pTmpData);
            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 RGB888 to RGB888 */
        err = M4VIFI_ResizeBilinearRGB888toRGB888(M4OSA_NULL, &rgbPlane1, &rgbPlane2);
        if(err != M4NO_ERROR)
        {
            M4OSA_TRACE1_1("error when converting from Resize RGB888 to RGB888: 0x%x\n", err);
            free(rgbPlane2.pac_data);
            free(rgbPlane1.pac_data);
            return err;
        }
        /*Converting Resized RGB888 to YUV420 */
        err = M4VIFI_RGB888toYUV420(M4OSA_NULL, &rgbPlane2, pImagePlanes);
        if(err != M4NO_ERROR)
        {
            M4OSA_TRACE1_1("error when converting from RGB888 to YUV: 0x%x\n", err);
            free(rgbPlane2.pac_data);
            free(rgbPlane1.pac_data);
            return err;
        }
            free(rgbPlane2.pac_data);
            free(rgbPlane1.pac_data);

            M4OSA_TRACE1_0("RGB to YUV done");


    }
    else
    {
        M4OSA_TRACE1_0("M4xVSS_internalConvertAndResizeARGB8888toYUV420 NO  Resizing :");
        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);

            M4OSA_TRACE1_0("RGB to YUV done");
    }
cleanup:
    M4OSA_TRACE1_0("M4xVSS_internalConvertAndResizeARGB8888toYUV420 leaving :");
    return err;
}

/**
 ******************************************************************************
 * M4OSA_ERR M4xVSS_internalConvertARGB8888toYUV420(M4OSA_Void* pFileIn,
 *                                             M4OSA_FileReadPointer* pFileReadPtr,
 *                                                M4VIFI_ImagePlane* pImagePlanes,
 *                                                 M4OSA_UInt32 width,
 *                                                M4OSA_UInt32 height);
 * @brief    It Coverts a ARGB8888 image to YUV420
 * @note
 * @param    pFileIn            (IN) The Image 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 M4xVSS_internalConvertARGB8888toYUV420(M4OSA_Void* pFileIn,
                                                 M4OSA_FileReadPointer* pFileReadPtr,
                                                 M4VIFI_ImagePlane** pImagePlanes,
                                                 M4OSA_UInt32 width,M4OSA_UInt32 height)
{
    M4OSA_ERR err = M4NO_ERROR;
    M4VIFI_ImagePlane *yuvPlane = M4OSA_NULL;

    yuvPlane = (M4VIFI_ImagePlane*)M4OSA_32bitAlignedMalloc(3*sizeof(M4VIFI_ImagePlane),
                M4VS, (M4OSA_Char*)"M4xVSS_internalConvertRGBtoYUV: Output plane YUV");
    if(yuvPlane == M4OSA_NULL) {
        M4OSA_TRACE1_0("M4xVSS_internalConvertAndResizeARGB8888toYUV420 :\
            Failed to allocate memory for Image clip");
        return M4ERR_ALLOC;
    }
    yuvPlane[0].u_height = height;
    yuvPlane[0].u_width = width;
    yuvPlane[0].u_stride = width;
    yuvPlane[0].u_topleft = 0;
    yuvPlane[0].pac_data = (M4VIFI_UInt8*)M4OSA_32bitAlignedMalloc(yuvPlane[0].u_height \
        * yuvPlane[0].u_width * 1.5, M4VS, (M4OSA_Char*)"imageClip YUV data");

    yuvPlane[1].u_height = yuvPlane[0].u_height >>1;
    yuvPlane[1].u_width = yuvPlane[0].u_width >> 1;
    yuvPlane[1].u_stride = yuvPlane[1].u_width;
    yuvPlane[1].u_topleft = 0;
    yuvPlane[1].pac_data = (M4VIFI_UInt8*)(yuvPlane[0].pac_data + yuvPlane[0].u_height \
        * yuvPlane[0].u_width);

    yuvPlane[2].u_height = yuvPlane[0].u_height >>1;
    yuvPlane[2].u_width = yuvPlane[0].u_width >> 1;
    yuvPlane[2].u_stride = yuvPlane[2].u_width;
    yuvPlane[2].u_topleft = 0;
    yuvPlane[2].pac_data = (M4VIFI_UInt8*)(yuvPlane[1].pac_data + yuvPlane[1].u_height \
        * yuvPlane[1].u_width);
    err = M4xVSS_internalConvertAndResizeARGB8888toYUV420( pFileIn,pFileReadPtr,
                                                          yuvPlane, width, height);
    if(err != M4NO_ERROR)
    {
        M4OSA_TRACE1_1("M4xVSS_internalConvertAndResizeARGB8888toYUV420 return error: 0x%x\n", err);
        free(yuvPlane);
        return err;
    }

        *pImagePlanes = yuvPlane;

    M4OSA_TRACE1_0("M4xVSS_internalConvertARGB8888toYUV420 :Leaving");
    return err;

}

/**
 ******************************************************************************
 * M4OSA_ERR M4xVSS_PictureCallbackFct (M4OSA_Void* pPictureCtxt,
 *                                        M4VIFI_ImagePlane* pImagePlanes,
 *                                        M4OSA_UInt32* pPictureDuration);
 * @brief    It feeds the PTO3GPP with YUV420 pictures.
 * @note    This function is given to the PTO3GPP in the M4PTO3GPP_Params structure
 * @param    pContext    (IN) The integrator own context
 * @param    pImagePlanes(IN/OUT) Pointer to an array of three valid image planes
 * @param    pPictureDuration(OUT) Duration of the returned picture
 *
 * @return    M4NO_ERROR:    No error
 * @return    M4PTO3GPP_WAR_LAST_PICTURE: The returned image is the last one
 * @return    M4ERR_PARAMETER: At least one of the function parameters is null
 ******************************************************************************
 */
M4OSA_ERR M4xVSS_PictureCallbackFct(M4OSA_Void* pPictureCtxt, M4VIFI_ImagePlane* pImagePlanes,
                                     M4OSA_Double* pPictureDuration)
{
    M4OSA_ERR err = M4NO_ERROR;
    M4OSA_UInt8    last_frame_flag = 0;
    M4xVSS_PictureCallbackCtxt* pC = (M4xVSS_PictureCallbackCtxt*) (pPictureCtxt);

    /*Used for pan&zoom*/
    M4OSA_UInt8 tempPanzoomXa = 0;
    M4OSA_UInt8 tempPanzoomXb = 0;
    M4AIR_Params Params;
    /**/

    /*Used for cropping and black borders*/
    M4OSA_Context    pPictureContext = M4OSA_NULL;
    M4OSA_FilePosition    pictureSize = 0 ;
    M4OSA_UInt8*    pictureBuffer = M4OSA_NULL;
    //M4EXIFC_Context pExifContext = M4OSA_NULL;
    M4EXIFC_BasicTags pBasicTags;
    M4VIFI_ImagePlane pImagePlanes1 = pImagePlanes[0];
    M4VIFI_ImagePlane pImagePlanes2 = pImagePlanes[1];
    M4VIFI_ImagePlane pImagePlanes3 = pImagePlanes[2];
    /**/

    /**
     * Check input parameters */
    M4OSA_DEBUG_IF2((M4OSA_NULL==pPictureCtxt),        M4ERR_PARAMETER,
         "M4xVSS_PictureCallbackFct: pPictureCtxt is M4OSA_NULL");
    M4OSA_DEBUG_IF2((M4OSA_NULL==pImagePlanes),        M4ERR_PARAMETER,
         "M4xVSS_PictureCallbackFct: pImagePlanes is M4OSA_NULL");
    M4OSA_DEBUG_IF2((M4OSA_NULL==pPictureDuration), M4ERR_PARAMETER,
         "M4xVSS_PictureCallbackFct: pPictureDuration is M4OSA_NULL");
    M4OSA_TRACE1_0("M4xVSS_PictureCallbackFct :Entering");
    /*PR P4ME00003181 In case the image number is 0, pan&zoom can not be used*/
    if(M4OSA_TRUE == pC->m_pPto3GPPparams->isPanZoom && pC->m_NbImage == 0)
    {
        pC->m_pPto3GPPparams->isPanZoom = M4OSA_FALSE;
    }

    /*If no cropping/black borders or pan&zoom, just decode and resize the picture*/
    if(pC->m_mediaRendering == M4xVSS_kResizing && M4OSA_FALSE == pC->m_pPto3GPPparams->isPanZoom)
    {
        /**
         * Convert and resize input ARGB8888 file to YUV420 */
        /*To support ARGB8888 : */
        M4OSA_TRACE1_2("M4xVSS_PictureCallbackFct 1: width and heght %d %d",
            pC->m_pPto3GPPparams->width,pC->m_pPto3GPPparams->height);
        err = M4xVSS_internalConvertAndResizeARGB8888toYUV420(pC->m_FileIn,
             pC->m_pFileReadPtr, pImagePlanes,pC->m_pPto3GPPparams->width,
                pC->m_pPto3GPPparams->height);
        if(err != M4NO_ERROR)
        {
            M4OSA_TRACE1_1("M4xVSS_PictureCallbackFct: Error when decoding JPEG: 0x%x\n", err);
            return err;
        }
    }
    /*In case of cropping, black borders or pan&zoom, call the EXIF reader and the AIR*/
    else
    {
        /**
         * Computes ratios */
        if(pC->m_pDecodedPlane == M4OSA_NULL)
        {
            /**
             * Convert input ARGB8888 file to YUV420 */
             M4OSA_TRACE1_2("M4xVSS_PictureCallbackFct 2: width and heght %d %d",
                pC->m_pPto3GPPparams->width,pC->m_pPto3GPPparams->height);
            err = M4xVSS_internalConvertARGB8888toYUV420(pC->m_FileIn, pC->m_pFileReadPtr,
                &(pC->m_pDecodedPlane),pC->m_pPto3GPPparams->width,pC->m_pPto3GPPparams->height);
            if(err != M4NO_ERROR)
            {
                M4OSA_TRACE1_1("M4xVSS_PictureCallbackFct: Error when decoding JPEG: 0x%x\n", err);
                if(pC->m_pDecodedPlane != M4OSA_NULL)
                {
                    /* YUV420 planar is returned but allocation is made only once
                        (contigous planes in memory) */
                    if(pC->m_pDecodedPlane->pac_data != M4OSA_NULL)
                    {
                        free(pC->m_pDecodedPlane->pac_data);
                    }
                    free(pC->m_pDecodedPlane);
                    pC->m_pDecodedPlane = M4OSA_NULL;
                }
                return err;
            }
        }

        /*Initialize AIR Params*/
        Params.m_inputCoord.m_x = 0;
        Params.m_inputCoord.m_y = 0;
        Params.m_inputSize.m_height = pC->m_pDecodedPlane->u_height;
        Params.m_inputSize.m_width = pC->m_pDecodedPlane->u_width;
        Params.m_outputSize.m_width = pImagePlanes->u_width;
        Params.m_outputSize.m_height = pImagePlanes->u_height;
        Params.m_bOutputStripe = M4OSA_FALSE;
        Params.m_outputOrientation = M4COMMON_kOrientationTopLeft;

        /*Initialize Exif params structure*/
        pBasicTags.orientation = M4COMMON_kOrientationUnknown;

        /**
        Pan&zoom params*/
        if(M4OSA_TRUE == pC->m_pPto3GPPparams->isPanZoom)
        {
            /*Save ratio values, they can be reused if the new ratios are 0*/
            tempPanzoomXa = (M4OSA_UInt8)pC->m_pPto3GPPparams->PanZoomXa;
            tempPanzoomXb = (M4OSA_UInt8)pC->m_pPto3GPPparams->PanZoomXb;
            /*Check that the ratio is not 0*/
            /*Check (a) parameters*/
            if(pC->m_pPto3GPPparams->PanZoomXa == 0)
            {
                M4OSA_UInt8 maxRatio = 0;
                if(pC->m_pPto3GPPparams->PanZoomTopleftXa >=
                     pC->m_pPto3GPPparams->PanZoomTopleftYa)
                {
                    /*The ratio is 0, that means the area of the picture defined with (a)
                    parameters is bigger than the image size*/
                    if(pC->m_pPto3GPPparams->PanZoomTopleftXa + tempPanzoomXa > 1000)
                    {
                        /*The oversize is maxRatio*/
                        maxRatio = pC->m_pPto3GPPparams->PanZoomTopleftXa + tempPanzoomXa - 1000;
                    }
                }
                else
                {
                    /*The ratio is 0, that means the area of the picture defined with (a)
                     parameters is bigger than the image size*/
                    if(pC->m_pPto3GPPparams->PanZoomTopleftYa + tempPanzoomXa > 1000)
                    {
                        /*The oversize is maxRatio*/
                        maxRatio = pC->m_pPto3GPPparams->PanZoomTopleftYa + tempPanzoomXa - 1000;
                    }
                }
                /*Modify the (a) parameters:*/
                if(pC->m_pPto3GPPparams->PanZoomTopleftXa >= maxRatio)
                {
                    /*The (a) topleft parameters can be moved to keep the same area size*/
                    pC->m_pPto3GPPparams->PanZoomTopleftXa -= maxRatio;
                }
                else
                {
                    /*Move the (a) topleft parameter to 0 but the ratio will be also further
                    modified to match the image size*/
                    pC->m_pPto3GPPparams->PanZoomTopleftXa = 0;
                }
                if(pC->m_pPto3GPPparams->PanZoomTopleftYa >= maxRatio)
                {
                    /*The (a) topleft parameters can be moved to keep the same area size*/
                    pC->m_pPto3GPPparams->PanZoomTopleftYa -= maxRatio;
                }
                else
                {
                    /*Move the (a) topleft parameter to 0 but the ratio will be also further
                     modified to match the image size*/
                    pC->m_pPto3GPPparams->PanZoomTopleftYa = 0;
                }
                /*The new ratio is the original one*/
                pC->m_pPto3GPPparams->PanZoomXa = tempPanzoomXa;
                if(pC->m_pPto3GPPparams->PanZoomXa + pC->m_pPto3GPPparams->PanZoomTopleftXa > 1000)
                {
                    /*Change the ratio if the area of the picture defined with (a) parameters is
                    bigger than the image size*/
                    pC->m_pPto3GPPparams->PanZoomXa = 1000 - pC->m_pPto3GPPparams->PanZoomTopleftXa;
                }
                if(pC->m_pPto3GPPparams->PanZoomXa + pC->m_pPto3GPPparams->PanZoomTopleftYa > 1000)
                {
                    /*Change the ratio if the area of the picture defined with (a) parameters is
                    bigger than the image size*/
                    pC->m_pPto3GPPparams->PanZoomXa = 1000 - pC->m_pPto3GPPparams->PanZoomTopleftYa;
                }
            }
            /*Check (b) parameters*/
            if(pC->m_pPto3GPPparams->PanZoomXb == 0)
            {
                M4OSA_UInt8 maxRatio = 0;
                if(pC->m_pPto3GPPparams->PanZoomTopleftXb >=
                     pC->m_pPto3GPPparams->PanZoomTopleftYb)
                {
                    /*The ratio is 0, that means the area of the picture defined with (b)
                     parameters is bigger than the image size*/
                    if(pC->m_pPto3GPPparams->PanZoomTopleftXb + tempPanzoomXb > 1000)
                    {
                        /*The oversize is maxRatio*/
                        maxRatio = pC->m_pPto3GPPparams->PanZoomTopleftXb + tempPanzoomXb - 1000;
                    }
                }
                else
                {
                    /*The ratio is 0, that means the area of the picture defined with (b)
                     parameters is bigger than the image size*/
                    if(pC->m_pPto3GPPparams->PanZoomTopleftYb + tempPanzoomXb > 1000)
                    {
                        /*The oversize is maxRatio*/
                        maxRatio = pC->m_pPto3GPPparams->PanZoomTopleftYb + tempPanzoomXb - 1000;
                    }
                }
                /*Modify the (b) parameters:*/
                if(pC->m_pPto3GPPparams->PanZoomTopleftXb >= maxRatio)
                {
                    /*The (b) topleft parameters can be moved to keep the same area size*/
                    pC->m_pPto3GPPparams->PanZoomTopleftXb -= maxRatio;
                }
                else
                {
                    /*Move the (b) topleft parameter to 0 but the ratio will be also further
                     modified to match the image size*/
                    pC->m_pPto3GPPparams->PanZoomTopleftXb = 0;
                }
                if(pC->m_pPto3GPPparams->PanZoomTopleftYb >= maxRatio)
                {
                    /*The (b) topleft parameters can be moved to keep the same area size*/
                    pC->m_pPto3GPPparams->PanZoomTopleftYb -= maxRatio;
                }
                else
                {
                    /*Move the (b) topleft parameter to 0 but the ratio will be also further
                    modified to match the image size*/
                    pC->m_pPto3GPPparams->PanZoomTopleftYb = 0;
                }
                /*The new ratio is the original one*/
                pC->m_pPto3GPPparams->PanZoomXb = tempPanzoomXb;
                if(pC->m_pPto3GPPparams->PanZoomXb + pC->m_pPto3GPPparams->PanZoomTopleftXb > 1000)
                {
                    /*Change the ratio if the area of the picture defined with (b) parameters is
                    bigger than the image size*/
                    pC->m_pPto3GPPparams->PanZoomXb = 1000 - pC->m_pPto3GPPparams->PanZoomTopleftXb;
                }
                if(pC->m_pPto3GPPparams->PanZoomXb + pC->m_pPto3GPPparams->PanZoomTopleftYb > 1000)
                {
                    /*Change the ratio if the area of the picture defined with (b) parameters is
                    bigger than the image size*/
                    pC->m_pPto3GPPparams->PanZoomXb = 1000 - pC->m_pPto3GPPparams->PanZoomTopleftYb;
                }
            }

            /**
             * Computes AIR parameters */
/*        Params.m_inputCoord.m_x = (M4OSA_UInt32)(pC->m_pDecodedPlane->u_width *
            (pC->m_pPto3GPPparams->PanZoomTopleftXa +
            (M4OSA_Int16)((pC->m_pPto3GPPparams->PanZoomTopleftXb \
                - pC->m_pPto3GPPparams->PanZoomTopleftXa) *
            pC->m_ImageCounter) / (M4OSA_Double)pC->m_NbImage)) / 100;
        Params.m_inputCoord.m_y = (M4OSA_UInt32)(pC->m_pDecodedPlane->u_height *
            (pC->m_pPto3GPPparams->PanZoomTopleftYa +
            (M4OSA_Int16)((pC->m_pPto3GPPparams->PanZoomTopleftYb\
                 - pC->m_pPto3GPPparams->PanZoomTopleftYa) *
            pC->m_ImageCounter) / (M4OSA_Double)pC->m_NbImage)) / 100;

        Params.m_inputSize.m_width = (M4OSA_UInt32)(pC->m_pDecodedPlane->u_width *
            (pC->m_pPto3GPPparams->PanZoomXa +
            (M4OSA_Int16)((pC->m_pPto3GPPparams->PanZoomXb - pC->m_pPto3GPPparams->PanZoomXa) *
            pC->m_ImageCounter) / (M4OSA_Double)pC->m_NbImage)) / 100;

        Params.m_inputSize.m_height =  (M4OSA_UInt32)(pC->m_pDecodedPlane->u_height *
            (pC->m_pPto3GPPparams->PanZoomXa +
            (M4OSA_Int16)((pC->m_pPto3GPPparams->PanZoomXb - pC->m_pPto3GPPparams->PanZoomXa) *
            pC->m_ImageCounter) / (M4OSA_Double)pC->m_NbImage)) / 100;
 */
            // Instead of using pC->m_NbImage we have to use (pC->m_NbImage-1) as pC->m_ImageCounter
            // will be x-1 max for x no. of frames
            Params.m_inputCoord.m_x = (M4OSA_UInt32)((((M4OSA_Double)pC->m_pDecodedPlane->u_width *
                (pC->m_pPto3GPPparams->PanZoomTopleftXa +
                (M4OSA_Double)((M4OSA_Double)(pC->m_pPto3GPPparams->PanZoomTopleftXb\
                     - pC->m_pPto3GPPparams->PanZoomTopleftXa) *
                pC->m_ImageCounter) / (M4OSA_Double)pC->m_NbImage-1)) / 1000));
            Params.m_inputCoord.m_y =
                 (M4OSA_UInt32)((((M4OSA_Double)pC->m_pDecodedPlane->u_height *
                (pC->m_pPto3GPPparams->PanZoomTopleftYa +
                (M4OSA_Double)((M4OSA_Double)(pC->m_pPto3GPPparams->PanZoomTopleftYb\
                     - pC->m_pPto3GPPparams->PanZoomTopleftYa) *
                pC->m_ImageCounter) / (M4OSA_Double)pC->m_NbImage-1)) / 1000));

            Params.m_inputSize.m_width =
                 (M4OSA_UInt32)((((M4OSA_Double)pC->m_pDecodedPlane->u_width *
                (pC->m_pPto3GPPparams->PanZoomXa +
                (M4OSA_Double)((M4OSA_Double)(pC->m_pPto3GPPparams->PanZoomXb\
                     - pC->m_pPto3GPPparams->PanZoomXa) *
                pC->m_ImageCounter) / (M4OSA_Double)pC->m_NbImage-1)) / 1000));

            Params.m_inputSize.m_height =
                 (M4OSA_UInt32)((((M4OSA_Double)pC->m_pDecodedPlane->u_height *
                (pC->m_pPto3GPPparams->PanZoomXa +
                (M4OSA_Double)((M4OSA_Double)(pC->m_pPto3GPPparams->PanZoomXb \
                    - pC->m_pPto3GPPparams->PanZoomXa) *
                pC->m_ImageCounter) / (M4OSA_Double)pC->m_NbImage-1)) / 1000));

            if((Params.m_inputSize.m_width + Params.m_inputCoord.m_x)\
                 > pC->m_pDecodedPlane->u_width)
            {
                Params.m_inputSize.m_width = pC->m_pDecodedPlane->u_width \
                    - Params.m_inputCoord.m_x;
            }

            if((Params.m_inputSize.m_height + Params.m_inputCoord.m_y)\
                 > pC->m_pDecodedPlane->u_height)
            {
                Params.m_inputSize.m_height = pC->m_pDecodedPlane->u_height\
                     - Params.m_inputCoord.m_y;
            }



            Params.m_inputSize.m_width = (Params.m_inputSize.m_width>>1)<<1;
            Params.m_inputSize.m_height = (Params.m_inputSize.m_height>>1)<<1;
        }



    /**
        Picture rendering: Black borders*/

        if(pC->m_mediaRendering == M4xVSS_kBlackBorders)
        {
            memset((void *)pImagePlanes[0].pac_data,Y_PLANE_BORDER_VALUE,
                (pImagePlanes[0].u_height*pImagePlanes[0].u_stride));
            memset((void *)pImagePlanes[1].pac_data,U_PLANE_BORDER_VALUE,
                (pImagePlanes[1].u_height*pImagePlanes[1].u_stride));
            memset((void *)pImagePlanes[2].pac_data,V_PLANE_BORDER_VALUE,
                (pImagePlanes[2].u_height*pImagePlanes[2].u_stride));

            /**
            First without pan&zoom*/
            if(M4OSA_FALSE == pC->m_pPto3GPPparams->isPanZoom)
            {
                switch(pBasicTags.orientation)
                {
                default:
                case M4COMMON_kOrientationUnknown:
                    Params.m_outputOrientation = M4COMMON_kOrientationTopLeft;
                case M4COMMON_kOrientationTopLeft:
                case M4COMMON_kOrientationTopRight:
                case M4COMMON_kOrientationBottomRight:
                case M4COMMON_kOrientationBottomLeft:
                    if((M4OSA_UInt32)((pC->m_pDecodedPlane->u_height * pImagePlanes->u_width)\
                         /pC->m_pDecodedPlane->u_width) <= pImagePlanes->u_height)
                         //Params.m_inputSize.m_height < Params.m_inputSize.m_width)
                    {
                        /*it is height so black borders will be on the top and on the bottom side*/
                        Params.m_outputSize.m_width = pImagePlanes->u_width;
                        Params.m_outputSize.m_height =
                             (M4OSA_UInt32)((pC->m_pDecodedPlane->u_height \
                                * pImagePlanes->u_width) /pC->m_pDecodedPlane->u_width);
                        /*number of lines at the top*/
                        pImagePlanes[0].u_topleft =
                            (M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[0].u_height\
                                -Params.m_outputSize.m_height)>>1))*pImagePlanes[0].u_stride;
                        pImagePlanes[0].u_height = Params.m_outputSize.m_height;
                        pImagePlanes[1].u_topleft =
                             (M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[1].u_height\
                                -(Params.m_outputSize.m_height>>1)))>>1)*pImagePlanes[1].u_stride;
                        pImagePlanes[1].u_height = Params.m_outputSize.m_height>>1;
                        pImagePlanes[2].u_topleft =
                             (M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[2].u_height\
                                -(Params.m_outputSize.m_height>>1)))>>1)*pImagePlanes[2].u_stride;
                        pImagePlanes[2].u_height = Params.m_outputSize.m_height>>1;
                    }
                    else
                    {
                        /*it is width so black borders will be on the left and right side*/
                        Params.m_outputSize.m_height = pImagePlanes->u_height;
                        Params.m_outputSize.m_width =
                             (M4OSA_UInt32)((pC->m_pDecodedPlane->u_width \
                                * pImagePlanes->u_height) /pC->m_pDecodedPlane->u_height);

                        pImagePlanes[0].u_topleft =
                            (M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[0].u_width\
                                -Params.m_outputSize.m_width)>>1));
                        pImagePlanes[0].u_width = Params.m_outputSize.m_width;
                        pImagePlanes[1].u_topleft =
                             (M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[1].u_width\
                                -(Params.m_outputSize.m_width>>1)))>>1);
                        pImagePlanes[1].u_width = Params.m_outputSize.m_width>>1;
                        pImagePlanes[2].u_topleft =
                             (M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[2].u_width\
                                -(Params.m_outputSize.m_width>>1)))>>1);
                        pImagePlanes[2].u_width = Params.m_outputSize.m_width>>1;
                    }
                    break;
                case M4COMMON_kOrientationLeftTop:
                case M4COMMON_kOrientationLeftBottom:
                case M4COMMON_kOrientationRightTop:
                case M4COMMON_kOrientationRightBottom:
                        if((M4OSA_UInt32)((pC->m_pDecodedPlane->u_width * pImagePlanes->u_width)\
                             /pC->m_pDecodedPlane->u_height) < pImagePlanes->u_height)
                             //Params.m_inputSize.m_height > Params.m_inputSize.m_width)
                        {
                            /*it is height so black borders will be on the top and on
                             the bottom side*/
                            Params.m_outputSize.m_height = pImagePlanes->u_width;
                            Params.m_outputSize.m_width =
                                 (M4OSA_UInt32)((pC->m_pDecodedPlane->u_width \
                                    * pImagePlanes->u_width) /pC->m_pDecodedPlane->u_height);
                            /*number of lines at the top*/
                            pImagePlanes[0].u_topleft =
                                ((M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[0].u_height\
                                    -Params.m_outputSize.m_width))>>1)*pImagePlanes[0].u_stride)+1;
                            pImagePlanes[0].u_height = Params.m_outputSize.m_width;
                            pImagePlanes[1].u_topleft =
                                ((M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[1].u_height\
                                    -(Params.m_outputSize.m_width>>1)))>>1)\
                                        *pImagePlanes[1].u_stride)+1;
                            pImagePlanes[1].u_height = Params.m_outputSize.m_width>>1;
                            pImagePlanes[2].u_topleft =
                                ((M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[2].u_height\
                                    -(Params.m_outputSize.m_width>>1)))>>1)\
                                        *pImagePlanes[2].u_stride)+1;
                            pImagePlanes[2].u_height = Params.m_outputSize.m_width>>1;
                        }
                        else
                        {
                            /*it is width so black borders will be on the left and right side*/
                            Params.m_outputSize.m_width = pImagePlanes->u_height;
                            Params.m_outputSize.m_height =
                                 (M4OSA_UInt32)((pC->m_pDecodedPlane->u_height\
                                     * pImagePlanes->u_height) /pC->m_pDecodedPlane->u_width);

                            pImagePlanes[0].u_topleft =
                                 ((M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[0].u_width\
                                    -Params.m_outputSize.m_height))>>1))+1;
                            pImagePlanes[0].u_width = Params.m_outputSize.m_height;
                            pImagePlanes[1].u_topleft =
                                 ((M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[1].u_width\
                                    -(Params.m_outputSize.m_height>>1)))>>1))+1;
                            pImagePlanes[1].u_width = Params.m_outputSize.m_height>>1;
                            pImagePlanes[2].u_topleft =
                                 ((M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[2].u_width\
                                    -(Params.m_outputSize.m_height>>1)))>>1))+1;
                            pImagePlanes[2].u_width = Params.m_outputSize.m_height>>1;
                        }
                    break;
                }
            }

            /**
            Secondly with pan&zoom*/
            else
            {
                switch(pBasicTags.orientation)
                {
                default:
                case M4COMMON_kOrientationUnknown:
                    Params.m_outputOrientation = M4COMMON_kOrientationTopLeft;
                case M4COMMON_kOrientationTopLeft:
                case M4COMMON_kOrientationTopRight:
                case M4COMMON_kOrientationBottomRight:
                case M4COMMON_kOrientationBottomLeft:
                    /*NO ROTATION*/
                    if((M4OSA_UInt32)((pC->m_pDecodedPlane->u_height * pImagePlanes->u_width)\
                         /pC->m_pDecodedPlane->u_width) <= pImagePlanes->u_height)
                            //Params.m_inputSize.m_height < Params.m_inputSize.m_width)
                    {
                        /*Black borders will be on the top and bottom of the output video*/
                        /*Maximum output height if the input image aspect ratio is kept and if
                        the output width is the screen width*/
                        M4OSA_UInt32 tempOutputSizeHeight =
                            (M4OSA_UInt32)((pC->m_pDecodedPlane->u_height\
                                 * pImagePlanes->u_width) /pC->m_pDecodedPlane->u_width);
                        M4OSA_UInt32 tempInputSizeHeightMax = 0;
                        M4OSA_UInt32 tempFinalInputHeight = 0;
                        /*The output width is the screen width*/
                        Params.m_outputSize.m_width = pImagePlanes->u_width;
                        tempOutputSizeHeight = (tempOutputSizeHeight>>1)<<1;

                        /*Maximum input height according to the maximum output height
                        (proportional to the maximum output height)*/
                        tempInputSizeHeightMax = (pImagePlanes->u_height\
                            *Params.m_inputSize.m_height)/tempOutputSizeHeight;
                        tempInputSizeHeightMax = (tempInputSizeHeightMax>>1)<<1;

                        /*Check if the maximum possible input height is contained into the
                        input image height*/
                        if(tempInputSizeHeightMax <= pC->m_pDecodedPlane->u_height)
                        {
                            /*The maximum possible input height is contained in the input
                            image height,
                            that means no black borders, the input pan zoom area will be extended
                            so that the input AIR height will be the maximum possible*/
                            if(((tempInputSizeHeightMax - Params.m_inputSize.m_height)>>1)\
                                 <= Params.m_inputCoord.m_y
                                && ((tempInputSizeHeightMax - Params.m_inputSize.m_height)>>1)\
                                     <= pC->m_pDecodedPlane->u_height -(Params.m_inputCoord.m_y\
                                         + Params.m_inputSize.m_height))
                            {
                                /*The input pan zoom area can be extended symmetrically on the
                                top and bottom side*/
                                Params.m_inputCoord.m_y -= ((tempInputSizeHeightMax \
                                    - Params.m_inputSize.m_height)>>1);
                            }
                            else if(Params.m_inputCoord.m_y < pC->m_pDecodedPlane->u_height\
                                -(Params.m_inputCoord.m_y + Params.m_inputSize.m_height))
                            {
                                /*There is not enough place above the input pan zoom area to
                                extend it symmetrically,
                                so extend it to the maximum on the top*/
                                Params.m_inputCoord.m_y = 0;
                            }
                            else
                            {
                                /*There is not enough place below the input pan zoom area to
                                extend it symmetrically,
                                so extend it to the maximum on the bottom*/
                                Params.m_inputCoord.m_y = pC->m_pDecodedPlane->u_height \
                                    - tempInputSizeHeightMax;
                            }
                            /*The input height of the AIR is the maximum possible height*/
                            Params.m_inputSize.m_height = tempInputSizeHeightMax;
                        }
                        else
                        {
                            /*The maximum possible input height is greater than the input
                            image height,
                            that means black borders are necessary to keep aspect ratio
                            The input height of the AIR is all the input image height*/
                            Params.m_outputSize.m_height =
                                (tempOutputSizeHeight*pC->m_pDecodedPlane->u_height)\
                                    /Params.m_inputSize.m_height;
                            Params.m_outputSize.m_height = (Params.m_outputSize.m_height>>1)<<1;
                            Params.m_inputCoord.m_y = 0;
                            Params.m_inputSize.m_height = pC->m_pDecodedPlane->u_height;
                            pImagePlanes[0].u_topleft =
                                 (M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[0].u_height\
                                    -Params.m_outputSize.m_height)>>1))*pImagePlanes[0].u_stride;
                            pImagePlanes[0].u_height = Params.m_outputSize.m_height;
                            pImagePlanes[1].u_topleft =
                                ((M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[1].u_height\
                                    -(Params.m_outputSize.m_height>>1)))>>1)\
                                        *pImagePlanes[1].u_stride);
                            pImagePlanes[1].u_height = Params.m_outputSize.m_height>>1;
                            pImagePlanes[2].u_topleft =
                                 ((M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[2].u_height\
                                    -(Params.m_outputSize.m_height>>1)))>>1)\
                                        *pImagePlanes[2].u_stride);
                            pImagePlanes[2].u_height = Params.m_outputSize.m_height>>1;
                        }
                    }
                    else
                    {
                        /*Black borders will be on the left and right side of the output video*/
                        /*Maximum output width if the input image aspect ratio is kept and if the
                         output height is the screen height*/
                        M4OSA_UInt32 tempOutputSizeWidth =
                             (M4OSA_UInt32)((pC->m_pDecodedPlane->u_width \
                                * pImagePlanes->u_height) /pC->m_pDecodedPlane->u_height);
                        M4OSA_UInt32 tempInputSizeWidthMax = 0;
                        M4OSA_UInt32 tempFinalInputWidth = 0;
                        /*The output height is the screen height*/
                        Params.m_outputSize.m_height = pImagePlanes->u_height;
                        tempOutputSizeWidth = (tempOutputSizeWidth>>1)<<1;

                        /*Maximum input width according to the maximum output width
                        (proportional to the maximum output width)*/
                        tempInputSizeWidthMax =
                             (pImagePlanes->u_width*Params.m_inputSize.m_width)\
                                /tempOutputSizeWidth;
                        tempInputSizeWidthMax = (tempInputSizeWidthMax>>1)<<1;

                        /*Check if the maximum possible input width is contained into the input
                         image width*/
                        if(tempInputSizeWidthMax <= pC->m_pDecodedPlane->u_width)
                        {
                            /*The maximum possible input width is contained in the input
                            image width,
                            that means no black borders, the input pan zoom area will be extended
                            so that the input AIR width will be the maximum possible*/
                            if(((tempInputSizeWidthMax - Params.m_inputSize.m_width)>>1) \
                                <= Params.m_inputCoord.m_x
                                && ((tempInputSizeWidthMax - Params.m_inputSize.m_width)>>1)\
                                     <= pC->m_pDecodedPlane->u_width -(Params.m_inputCoord.m_x \
                                        + Params.m_inputSize.m_width))
                            {
                                /*The input pan zoom area can be extended symmetrically on the
                                     right and left side*/
                                Params.m_inputCoord.m_x -= ((tempInputSizeWidthMax\
                                     - Params.m_inputSize.m_width)>>1);
                            }
                            else if(Params.m_inputCoord.m_x < pC->m_pDecodedPlane->u_width\
                                -(Params.m_inputCoord.m_x + Params.m_inputSize.m_width))
                            {
                                /*There is not enough place above the input pan zoom area to
                                    extend it symmetrically,
                                so extend it to the maximum on the left*/
                                Params.m_inputCoord.m_x = 0;
                            }
                            else
                            {
                                /*There is not enough place below the input pan zoom area
                                    to extend it symmetrically,
                                so extend it to the maximum on the right*/
                                Params.m_inputCoord.m_x = pC->m_pDecodedPlane->u_width \
                                    - tempInputSizeWidthMax;
                            }
                            /*The input width of the AIR is the maximum possible width*/
                            Params.m_inputSize.m_width = tempInputSizeWidthMax;
                        }
                        else
                        {
                            /*The maximum possible input width is greater than the input
                            image width,
                            that means black borders are necessary to keep aspect ratio
                            The input width of the AIR is all the input image width*/
                            Params.m_outputSize.m_width =\
                                 (tempOutputSizeWidth*pC->m_pDecodedPlane->u_width)\
                                    /Params.m_inputSize.m_width;
                            Params.m_outputSize.m_width = (Params.m_outputSize.m_width>>1)<<1;
                            Params.m_inputCoord.m_x = 0;
                            Params.m_inputSize.m_width = pC->m_pDecodedPlane->u_width;
                            pImagePlanes[0].u_topleft =
                                 (M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[0].u_width\
                                    -Params.m_outputSize.m_width)>>1));
                            pImagePlanes[0].u_width = Params.m_outputSize.m_width;
                            pImagePlanes[1].u_topleft =
                                 (M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[1].u_width\
                                    -(Params.m_outputSize.m_width>>1)))>>1);
                            pImagePlanes[1].u_width = Params.m_outputSize.m_width>>1;
                            pImagePlanes[2].u_topleft =
                                 (M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[2].u_width\
                                    -(Params.m_outputSize.m_width>>1)))>>1);
                            pImagePlanes[2].u_width = Params.m_outputSize.m_width>>1;
                        }
                    }
                    break;
                case M4COMMON_kOrientationLeftTop:
                case M4COMMON_kOrientationLeftBottom:
                case M4COMMON_kOrientationRightTop:
                case M4COMMON_kOrientationRightBottom:
                    /*ROTATION*/
                    if((M4OSA_UInt32)((pC->m_pDecodedPlane->u_width * pImagePlanes->u_width)\
                         /pC->m_pDecodedPlane->u_height) < pImagePlanes->u_height)
                         //Params.m_inputSize.m_height > Params.m_inputSize.m_width)
                    {
                        /*Black borders will be on the left and right side of the output video*/
                        /*Maximum output height if the input image aspect ratio is kept and if
                        the output height is the screen width*/
                        M4OSA_UInt32 tempOutputSizeHeight =
                        (M4OSA_UInt32)((pC->m_pDecodedPlane->u_width * pImagePlanes->u_width)\
                             /pC->m_pDecodedPlane->u_height);
                        M4OSA_UInt32 tempInputSizeHeightMax = 0;
                        M4OSA_UInt32 tempFinalInputHeight = 0;
                        /*The output width is the screen height*/
                        Params.m_outputSize.m_height = pImagePlanes->u_width;
                        Params.m_outputSize.m_width= pImagePlanes->u_height;
                        tempOutputSizeHeight = (tempOutputSizeHeight>>1)<<1;

                        /*Maximum input height according to the maximum output height
                             (proportional to the maximum output height)*/
                        tempInputSizeHeightMax =
                            (pImagePlanes->u_height*Params.m_inputSize.m_width)\
                                /tempOutputSizeHeight;
                        tempInputSizeHeightMax = (tempInputSizeHeightMax>>1)<<1;

                        /*Check if the maximum possible input height is contained into the
                             input image width (rotation included)*/
                        if(tempInputSizeHeightMax <= pC->m_pDecodedPlane->u_width)
                        {
                            /*The maximum possible input height is contained in the input
                            image width (rotation included),
                            that means no black borders, the input pan zoom area will be extended
                            so that the input AIR width will be the maximum possible*/
                            if(((tempInputSizeHeightMax - Params.m_inputSize.m_width)>>1) \
                                <= Params.m_inputCoord.m_x
                                && ((tempInputSizeHeightMax - Params.m_inputSize.m_width)>>1)\
                                     <= pC->m_pDecodedPlane->u_width -(Params.m_inputCoord.m_x \
                                        + Params.m_inputSize.m_width))
                            {
                                /*The input pan zoom area can be extended symmetrically on the
                                 right and left side*/
                                Params.m_inputCoord.m_x -= ((tempInputSizeHeightMax \
                                    - Params.m_inputSize.m_width)>>1);
                            }
                            else if(Params.m_inputCoord.m_x < pC->m_pDecodedPlane->u_width\
                                -(Params.m_inputCoord.m_x + Params.m_inputSize.m_width))
                            {
                                /*There is not enough place on the left of the input pan
                                zoom area to extend it symmetrically,
                                so extend it to the maximum on the left*/
                                Params.m_inputCoord.m_x = 0;
                            }
                            else
                            {
                                /*There is not enough place on the right of the input pan zoom
                                 area to extend it symmetrically,
                                so extend it to the maximum on the right*/
                                Params.m_inputCoord.m_x =
                                     pC->m_pDecodedPlane->u_width - tempInputSizeHeightMax;
                            }
                            /*The input width of the AIR is the maximum possible width*/
                            Params.m_inputSize.m_width = tempInputSizeHeightMax;
                        }
                        else
                        {
                            /*The maximum possible input height is greater than the input
                            image width (rotation included),
                            that means black borders are necessary to keep aspect ratio
                            The input width of the AIR is all the input image width*/
                            Params.m_outputSize.m_width =
                            (tempOutputSizeHeight*pC->m_pDecodedPlane->u_width)\
                                /Params.m_inputSize.m_width;
                            Params.m_outputSize.m_width = (Params.m_outputSize.m_width>>1)<<1;
                            Params.m_inputCoord.m_x = 0;
                            Params.m_inputSize.m_width = pC->m_pDecodedPlane->u_width;
                            pImagePlanes[0].u_topleft =
                                ((M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[0].u_height\
                                    -Params.m_outputSize.m_width))>>1)*pImagePlanes[0].u_stride)+1;
                            pImagePlanes[0].u_height = Params.m_outputSize.m_width;
                            pImagePlanes[1].u_topleft =
                            ((M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[1].u_height\
                                -(Params.m_outputSize.m_width>>1)))>>1)\
                                    *pImagePlanes[1].u_stride)+1;
                            pImagePlanes[1].u_height = Params.m_outputSize.m_width>>1;
                            pImagePlanes[2].u_topleft =
                            ((M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[2].u_height\
                                -(Params.m_outputSize.m_width>>1)))>>1)\
                                    *pImagePlanes[2].u_stride)+1;
                            pImagePlanes[2].u_height = Params.m_outputSize.m_width>>1;
                        }
                    }
                    else
                    {
                        /*Black borders will be on the top and bottom of the output video*/
                        /*Maximum output width if the input image aspect ratio is kept and if
                         the output width is the screen height*/
                        M4OSA_UInt32 tempOutputSizeWidth =
                        (M4OSA_UInt32)((pC->m_pDecodedPlane->u_height * pImagePlanes->u_height)\
                             /pC->m_pDecodedPlane->u_width);
                        M4OSA_UInt32 tempInputSizeWidthMax = 0;
                        M4OSA_UInt32 tempFinalInputWidth = 0, tempFinalOutputWidth = 0;
                        /*The output height is the screen width*/
                        Params.m_outputSize.m_width = pImagePlanes->u_height;
                        Params.m_outputSize.m_height= pImagePlanes->u_width;
                        tempOutputSizeWidth = (tempOutputSizeWidth>>1)<<1;

                        /*Maximum input width according to the maximum output width
                         (proportional to the maximum output width)*/
                        tempInputSizeWidthMax =
                        (pImagePlanes->u_width*Params.m_inputSize.m_height)/tempOutputSizeWidth;
                        tempInputSizeWidthMax = (tempInputSizeWidthMax>>1)<<1;

                        /*Check if the maximum possible input width is contained into the input
                         image height (rotation included)*/
                        if(tempInputSizeWidthMax <= pC->m_pDecodedPlane->u_height)
                        {
                            /*The maximum possible input width is contained in the input
                             image height (rotation included),
                            that means no black borders, the input pan zoom area will be extended
                            so that the input AIR height will be the maximum possible*/
                            if(((tempInputSizeWidthMax - Params.m_inputSize.m_height)>>1) \
                                <= Params.m_inputCoord.m_y
                                && ((tempInputSizeWidthMax - Params.m_inputSize.m_height)>>1)\
                                     <= pC->m_pDecodedPlane->u_height -(Params.m_inputCoord.m_y \
                                        + Params.m_inputSize.m_height))
                            {
                                /*The input pan zoom area can be extended symmetrically on
                                the right and left side*/
                                Params.m_inputCoord.m_y -= ((tempInputSizeWidthMax \
                                    - Params.m_inputSize.m_height)>>1);
                            }
                            else if(Params.m_inputCoord.m_y < pC->m_pDecodedPlane->u_height\
                                -(Params.m_inputCoord.m_y + Params.m_inputSize.m_height))
                            {
                                /*There is not enough place on the top of the input pan zoom
                                area to extend it symmetrically,
                                so extend it to the maximum on the top*/
                                Params.m_inputCoord.m_y = 0;
                            }
                            else
                            {
                                /*There is not enough place on the bottom of the input pan zoom
                                 area to extend it symmetrically,
                                so extend it to the maximum on the bottom*/
                                Params.m_inputCoord.m_y = pC->m_pDecodedPlane->u_height\
                                     - tempInputSizeWidthMax;
                            }
                            /*The input height of the AIR is the maximum possible height*/
                            Params.m_inputSize.m_height = tempInputSizeWidthMax;
                        }
                        else
                        {
                            /*The maximum possible input width is greater than the input\
                             image height (rotation included),
                            that means black borders are necessary to keep aspect ratio
                            The input height of the AIR is all the input image height*/
                            Params.m_outputSize.m_height =
                                (tempOutputSizeWidth*pC->m_pDecodedPlane->u_height)\
                                    /Params.m_inputSize.m_height;
                            Params.m_outputSize.m_height = (Params.m_outputSize.m_height>>1)<<1;
                            Params.m_inputCoord.m_y = 0;
                            Params.m_inputSize.m_height = pC->m_pDecodedPlane->u_height;
                            pImagePlanes[0].u_topleft =
                                ((M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[0].u_width\
                                    -Params.m_outputSize.m_height))>>1))+1;
                            pImagePlanes[0].u_width = Params.m_outputSize.m_height;
                            pImagePlanes[1].u_topleft =
                                ((M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[1].u_width\
                                    -(Params.m_outputSize.m_height>>1)))>>1))+1;
                            pImagePlanes[1].u_width = Params.m_outputSize.m_height>>1;
                            pImagePlanes[2].u_topleft =
                                 ((M4xVSS_ABS((M4OSA_Int32)(pImagePlanes[2].u_width\
                                    -(Params.m_outputSize.m_height>>1)))>>1))+1;
                            pImagePlanes[2].u_width = Params.m_outputSize.m_height>>1;
                        }
                    }
                    break;
                }
            }

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

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

        /**
        Picture rendering: Resizing and Cropping*/
        if(pC->m_mediaRendering != M4xVSS_kBlackBorders)
        {
            switch(pBasicTags.orientation)
            {
            default:
            case M4COMMON_kOrientationUnknown:
                Params.m_outputOrientation = M4COMMON_kOrientationTopLeft;
            case M4COMMON_kOrientationTopLeft:
            case M4COMMON_kOrientationTopRight:
            case M4COMMON_kOrientationBottomRight:
            case M4COMMON_kOrientationBottomLeft:
                Params.m_outputSize.m_height = pImagePlanes->u_height;
                Params.m_outputSize.m_width = pImagePlanes->u_width;
                break;
            case M4COMMON_kOrientationLeftTop:
            case M4COMMON_kOrientationLeftBottom:
            case M4COMMON_kOrientationRightTop:
            case M4COMMON_kOrientationRightBottom:
                Params.m_outputSize.m_height = pImagePlanes->u_width;
                Params.m_outputSize.m_width = pImagePlanes->u_height;
                break;
            }
        }

        /**
        Picture rendering: Cropping*/
        if(pC->m_mediaRendering == M4xVSS_kCropping)
        {
            if((Params.m_outputSize.m_height * Params.m_inputSize.m_width)\
                 /Params.m_outputSize.m_width<Params.m_inputSize.m_height)
            {
                M4OSA_UInt32 tempHeight = Params.m_inputSize.m_height;
                /*height will be cropped*/
                Params.m_inputSize.m_height =  (M4OSA_UInt32)((Params.m_outputSize.m_height \
                    * Params.m_inputSize.m_width) /Params.m_outputSize.m_width);
                Params.m_inputSize.m_height =  (Params.m_inputSize.m_height>>1)<<1;
                if(M4OSA_FALSE == pC->m_pPto3GPPparams->isPanZoom)
                {
                    Params.m_inputCoord.m_y = (M4OSA_Int32)((M4OSA_Int32)\
                        ((pC->m_pDecodedPlane->u_height - Params.m_inputSize.m_height))>>1);
                }
                else
                {
                    Params.m_inputCoord.m_y += (M4OSA_Int32)((M4OSA_Int32)\
                        ((tempHeight - Params.m_inputSize.m_height))>>1);
                }
            }
            else
            {
                M4OSA_UInt32 tempWidth= Params.m_inputSize.m_width;
                /*width will be cropped*/
                Params.m_inputSize.m_width =  (M4OSA_UInt32)((Params.m_outputSize.m_width \
                    * Params.m_inputSize.m_height) /Params.m_outputSize.m_height);
                Params.m_inputSize.m_width =  (Params.m_inputSize.m_width>>1)<<1;
                if(M4OSA_FALSE == pC->m_pPto3GPPparams->isPanZoom)
                {
                    Params.m_inputCoord.m_x = (M4OSA_Int32)((M4OSA_Int32)\
                        ((pC->m_pDecodedPlane->u_width - Params.m_inputSize.m_width))>>1);
                }
                else
                {
                    Params.m_inputCoord.m_x += (M4OSA_Int32)\
                        (((M4OSA_Int32)(tempWidth - Params.m_inputSize.m_width))>>1);
                }
            }
        }



        /**
         * Call AIR functions */
        if(M4OSA_NULL == pC->m_air_context)
        {
            err = M4AIR_create(&pC->m_air_context, M4AIR_kYUV420P);
            if(err != M4NO_ERROR)
            {
                free(pC->m_pDecodedPlane[0].pac_data);
                free(pC->m_pDecodedPlane);
                pC->m_pDecodedPlane = M4OSA_NULL;
                M4OSA_TRACE1_1("M4xVSS_PictureCallbackFct:\
                     Error when initializing AIR: 0x%x", err);
                return err;
            }
        }

        err = M4AIR_configure(pC->m_air_context, &Params);
        if(err != M4NO_ERROR)
        {
            M4OSA_TRACE1_1("M4xVSS_PictureCallbackFct:\
                 Error when configuring AIR: 0x%x", err);
            M4AIR_cleanUp(pC->m_air_context);
            free(pC->m_pDecodedPlane[0].pac_data);
            free(pC->m_pDecodedPlane);
            pC->m_pDecodedPlane = M4OSA_NULL;
            return err;
        }

        err = M4AIR_get(pC->m_air_context, pC->m_pDecodedPlane, pImagePlanes);
        if(err != M4NO_ERROR)
        {
            M4OSA_TRACE1_1("M4xVSS_PictureCallbackFct: Error when getting AIR plane: 0x%x", err);
            M4AIR_cleanUp(pC->m_air_context);
            free(pC->m_pDecodedPlane[0].pac_data);
            free(pC->m_pDecodedPlane);
            pC->m_pDecodedPlane = M4OSA_NULL;
            return err;
        }
        pImagePlanes[0] = pImagePlanes1;
        pImagePlanes[1] = pImagePlanes2;
        pImagePlanes[2] = pImagePlanes3;
    }


    /**
     * Increment the image counter */
    pC->m_ImageCounter++;

    /**
     * Check end of sequence */
    last_frame_flag    = (pC->m_ImageCounter >= pC->m_NbImage);

    /**
     * Keep the picture duration */
    *pPictureDuration = pC->m_timeDuration;

    if (1 == last_frame_flag)
    {
        if(M4OSA_NULL != pC->m_air_context)
        {
            err = M4AIR_cleanUp(pC->m_air_context);
            if(err != M4NO_ERROR)
            {
                M4OSA_TRACE1_1("M4xVSS_PictureCallbackFct: Error when cleaning AIR: 0x%x", err);
                return err;
            }
        }
        if(M4OSA_NULL != pC->m_pDecodedPlane)
        {
            free(pC->m_pDecodedPlane[0].pac_data);
            free(pC->m_pDecodedPlane);
            pC->m_pDecodedPlane = M4OSA_NULL;
        }
        return M4PTO3GPP_WAR_LAST_PICTURE;
    }

    M4OSA_TRACE1_0("M4xVSS_PictureCallbackFct: Leaving ");
    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * M4OSA_ERR M4xVSS_internalStartConvertPictureTo3gp(M4OSA_Context pContext)
 * @brief    This function initializes Pto3GPP with the given parameters
 * @note    The "Pictures to 3GPP" parameters are given by the internal xVSS
 *            context. This context contains a pointer on the current element
 *            of the chained list of Pto3GPP parameters.
 * @param    pContext    (IN) The integrator own context
 *
 * @return    M4NO_ERROR:    No error
 * @return    M4PTO3GPP_WAR_LAST_PICTURE: The returned image is the last one
 * @return    M4ERR_PARAMETER: At least one of the function parameters is null
 ******************************************************************************
 */
M4OSA_ERR M4xVSS_internalStartConvertPictureTo3gp(M4OSA_Context pContext)
{
    /************************************************************************/
    /* Definitions to generate dummy AMR file used to add AMR silence in files generated
     by Pto3GPP */
    #define M4VSS3GPP_AMR_AU_SILENCE_FRAME_048_SIZE     13
    /* This constant is defined in M4VSS3GPP_InternalConfig.h */
    extern const M4OSA_UInt8\
         M4VSS3GPP_AMR_AU_SILENCE_FRAME_048[M4VSS3GPP_AMR_AU_SILENCE_FRAME_048_SIZE];

    /* AMR silent frame used to compute dummy AMR silence file */
    #define M4VSS3GPP_AMR_HEADER_SIZE 6
    const M4OSA_UInt8 M4VSS3GPP_AMR_HEADER[M4VSS3GPP_AMR_AU_SILENCE_FRAME_048_SIZE] =
    { 0x23, 0x21, 0x41, 0x4d, 0x52, 0x0a };
    /************************************************************************/

    M4xVSS_Context* xVSS_context = (M4xVSS_Context*)pContext;
    M4OSA_ERR err;
    M4PTO3GPP_Context pM4PTO3GPP_Ctxt = M4OSA_NULL;
    M4PTO3GPP_Params Params;
     M4xVSS_PictureCallbackCtxt*    pCallBackCtxt;
    M4OSA_Bool cmpResult=M4OSA_FALSE;
    M4OSA_Context pDummyAMRFile;
    M4OSA_Char out_amr[M4XVSS_MAX_PATH_LEN];
    /*UTF conversion support*/
    M4OSA_Char* pDecodedPath = M4OSA_NULL;
    M4OSA_UInt32 i;

    /**
     * Create a M4PTO3GPP instance */
    err = M4PTO3GPP_Init( &pM4PTO3GPP_Ctxt, xVSS_context->pFileReadPtr,
         xVSS_context->pFileWritePtr);
    if (err != M4NO_ERROR)
    {
        M4OSA_TRACE1_1("M4xVSS_internalStartConvertPictureTo3gp returned %ld\n",err);
        return err;
    }

    pCallBackCtxt = (M4xVSS_PictureCallbackCtxt*)M4OSA_32bitAlignedMalloc(sizeof(M4xVSS_PictureCallbackCtxt),
         M4VS,(M4OSA_Char *) "Pto3gpp callback struct");
    if(pCallBackCtxt == M4OSA_NULL)
    {
        M4OSA_TRACE1_0("Allocation error in M4xVSS_internalStartConvertPictureTo3gp");
        return M4ERR_ALLOC;
    }

    Params.OutputVideoFrameSize = xVSS_context->pSettings->xVSS.outputVideoSize;
    Params.OutputVideoFormat = xVSS_context->pSettings->xVSS.outputVideoFormat;
    Params.videoProfile = xVSS_context->pSettings->xVSS.outputVideoProfile;
    Params.videoLevel = xVSS_context->pSettings->xVSS.outputVideoLevel;

    /**
     * Generate "dummy" amr file containing silence in temporary folder */
    M4OSA_chrNCopy(out_amr, xVSS_context->pTempPath, M4XVSS_MAX_PATH_LEN - 1);
    strncat((char *)out_amr, (const char *)"dummy.amr\0", 10);

    /**
     * UTF conversion: convert the temporary path into the customer format*/
    pDecodedPath = out_amr;

    if(xVSS_context->UTFConversionContext.pConvFromUTF8Fct != M4OSA_NULL
            && xVSS_context->UTFConversionContext.pTempOutConversionBuffer != M4OSA_NULL)
    {
        M4OSA_UInt32 length = 0;
        err = M4xVSS_internalConvertFromUTF8(xVSS_context, (M4OSA_Void*) out_amr,
             (M4OSA_Void*) xVSS_context->UTFConversionContext.pTempOutConversionBuffer, &length);
        if(err != M4NO_ERROR)
        {
            M4OSA_TRACE1_1("M4xVSS_internalStartConvertPictureTo3gp:\
                 M4xVSS_internalConvertFromUTF8 returns err: 0x%x",err);
            return err;
        }
        pDecodedPath = xVSS_context->UTFConversionContext.pTempOutConversionBuffer;
    }

    /**
    * End of the conversion, now use the converted path*/

    err = xVSS_context->pFileWritePtr->openWrite(&pDummyAMRFile, pDecodedPath, M4OSA_kFileWrite);

    /*Commented because of the use of the UTF conversion see above*/
/*    err = xVSS_context->pFileWritePtr->openWrite(&pDummyAMRFile, out_amr, M4OSA_kFileWrite);
 */
    if(err != M4NO_ERROR)
    {
        M4OSA_TRACE1_2("M4xVSS_internalConvertPictureTo3gp: Can't open output dummy amr file %s,\
             error: 0x%x\n",out_amr, err);
        return err;
    }

    err =  xVSS_context->pFileWritePtr->writeData(pDummyAMRFile,
        (M4OSA_Int8*)M4VSS3GPP_AMR_HEADER, M4VSS3GPP_AMR_HEADER_SIZE);
    if(err != M4NO_ERROR)
    {
        M4OSA_TRACE1_2("M4xVSS_internalConvertPictureTo3gp: Can't write output dummy amr file %s,\
             error: 0x%x\n",out_amr, err);
        return err;
    }

    err =  xVSS_context->pFileWritePtr->writeData(pDummyAMRFile,
         (M4OSA_Int8*)M4VSS3GPP_AMR_AU_SILENCE_FRAME_048, M4VSS3GPP_AMR_AU_SILENCE_FRAME_048_SIZE);
    if(err != M4NO_ERROR)
    {
        M4OSA_TRACE1_2("M4xVSS_internalConvertPictureTo3gp: \
            Can't write output dummy amr file %s, error: 0x%x\n",out_amr, err);
        return err;
    }

    err =  xVSS_context->pFileWritePtr->closeWrite(pDummyAMRFile);
    if(err != M4NO_ERROR)
    {
        M4OSA_TRACE1_2("M4xVSS_internalConvertPictureTo3gp: \
            Can't close output dummy amr file %s, error: 0x%x\n",out_amr, err);
        return err;
    }

    /**
     * Fill parameters for Pto3GPP with the parameters contained in the current element of the
     * Pto3GPP parameters chained list and with default parameters */
/*+ New Encoder bitrates */
    if(xVSS_context->pSettings->xVSS.outputVideoBitrate == 0) {
        Params.OutputVideoBitrate    = M4VIDEOEDITING_kVARIABLE_KBPS;
    }
    else {
          Params.OutputVideoBitrate = xVSS_context->pSettings->xVSS.outputVideoBitrate;
    }
    M4OSA_TRACE1_1("M4xVSS_internalStartConvertPicTo3GP: video bitrate = %d",
        Params.OutputVideoBitrate);
/*- New Encoder bitrates */
    Params.OutputFileMaxSize    = M4PTO3GPP_kUNLIMITED;
    Params.pPictureCallbackFct    = M4xVSS_PictureCallbackFct;
    Params.pPictureCallbackCtxt    = pCallBackCtxt;
    /*FB: change to use the converted path (UTF conversion) see the conversion above*/
    /*Fix :- Adding Audio Track in Image as input :AudioTarckFile Setting to NULL */
    Params.pInputAudioTrackFile    = M4OSA_NULL;//(M4OSA_Void*)pDecodedPath;//out_amr;
    Params.AudioPaddingMode        = M4PTO3GPP_kAudioPaddingMode_Loop;
    Params.AudioFileFormat        = M4VIDEOEDITING_kFileType_AMR;
    Params.pOutput3gppFile        = xVSS_context->pPTo3GPPcurrentParams->pFileOut;
    Params.pTemporaryFile        = xVSS_context->pPTo3GPPcurrentParams->pFileTemp;
    /*+PR No:  blrnxpsw#223*/
    /*Increasing frequency of Frame, calculating Nos of Frame = duration /FPS */
    /*Other changes made is @ M4xVSS_API.c @ line 3841 in M4xVSS_SendCommand*/
    /*If case check for PanZoom removed */
    Params.NbVideoFrames            = (M4OSA_UInt32)
        (xVSS_context->pPTo3GPPcurrentParams->duration \
            / xVSS_context->pPTo3GPPcurrentParams->framerate); /* */
    pCallBackCtxt->m_timeDuration    = xVSS_context->pPTo3GPPcurrentParams->framerate;
    /*-PR No:  blrnxpsw#223*/
    pCallBackCtxt->m_ImageCounter    = 0;
    pCallBackCtxt->m_FileIn            = xVSS_context->pPTo3GPPcurrentParams->pFileIn;
    pCallBackCtxt->m_NbImage        = Params.NbVideoFrames;
    pCallBackCtxt->m_pFileReadPtr    = xVSS_context->pFileReadPtr;
    pCallBackCtxt->m_pDecodedPlane    = M4OSA_NULL;
    pCallBackCtxt->m_pPto3GPPparams    = xVSS_context->pPTo3GPPcurrentParams;
    pCallBackCtxt->m_air_context    = M4OSA_NULL;
    pCallBackCtxt->m_mediaRendering = xVSS_context->pPTo3GPPcurrentParams->MediaRendering;

    /**
     * Set the input and output files */
    err = M4PTO3GPP_Open(pM4PTO3GPP_Ctxt, &Params);
    if (err != M4NO_ERROR)
    {
        M4OSA_TRACE1_1("M4PTO3GPP_Open returned: 0x%x\n",err);
        if(pCallBackCtxt != M4OSA_NULL)
        {
            free(pCallBackCtxt);
            pCallBackCtxt = M4OSA_NULL;
        }
        M4PTO3GPP_CleanUp(pM4PTO3GPP_Ctxt);
        return err;
    }

    /**
     * Save context to be able to call Pto3GPP step function in M4xVSS_step function */
    xVSS_context->pM4PTO3GPP_Ctxt = pM4PTO3GPP_Ctxt;
    xVSS_context->pCallBackCtxt = pCallBackCtxt;

    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * M4OSA_ERR M4xVSS_internalStopConvertPictureTo3gp(M4OSA_Context pContext)
 * @brief    This function cleans up Pto3GPP
 * @note
 * @param    pContext    (IN) The integrator own context
 *
 * @return    M4NO_ERROR:    No error
 * @return    M4ERR_PARAMETER: At least one of the function parameters is null
 ******************************************************************************
 */
M4OSA_ERR M4xVSS_internalStopConvertPictureTo3gp(M4OSA_Context pContext)
{
    M4xVSS_Context* xVSS_context = (M4xVSS_Context*)pContext;
    M4OSA_ERR err;
    M4OSA_Char out_amr[M4XVSS_MAX_PATH_LEN];
    /*UTF conversion support*/
    M4OSA_Char* pDecodedPath = M4OSA_NULL;

    /**
    * Free the PTO3GPP callback context */
    if(M4OSA_NULL != xVSS_context->pCallBackCtxt)
    {
        free(xVSS_context->pCallBackCtxt);
        xVSS_context->pCallBackCtxt = M4OSA_NULL;
    }

    /**
     * Finalize the output file */
    err = M4PTO3GPP_Close(xVSS_context->pM4PTO3GPP_Ctxt);
    if (err != M4NO_ERROR)
    {
        M4OSA_TRACE1_1("M4PTO3GPP_Close returned 0x%x\n",err);
        M4PTO3GPP_CleanUp(xVSS_context->pM4PTO3GPP_Ctxt);
        return err;
    }

    /**
     * Free this M4PTO3GPP instance */
    err = M4PTO3GPP_CleanUp(xVSS_context->pM4PTO3GPP_Ctxt);
    if (err != M4NO_ERROR)
    {
        M4OSA_TRACE1_1("M4PTO3GPP_CleanUp returned 0x%x\n",err);
        return err;
    }

    /**
     * Remove dummy.amr file */
    M4OSA_chrNCopy(out_amr, xVSS_context->pTempPath, M4XVSS_MAX_PATH_LEN - 1);
    strncat((char *)out_amr, (const char *)"dummy.amr\0", 10);

    /**
     * UTF conversion: convert the temporary path into the customer format*/
    pDecodedPath = out_amr;

    if(xVSS_context->UTFConversionContext.pConvFromUTF8Fct != M4OSA_NULL
            && xVSS_context->UTFConversionContext.pTempOutConversionBuffer != M4OSA_NULL)
    {
        M4OSA_UInt32 length = 0;
        err = M4xVSS_internalConvertFromUTF8(xVSS_context, (M4OSA_Void*) out_amr,
             (M4OSA_Void*) xVSS_context->UTFConversionContext.pTempOutConversionBuffer, &length);
        if(err != M4NO_ERROR)
        {
            M4OSA_TRACE1_1("M4xVSS_internalStopConvertPictureTo3gp:\
                 M4xVSS_internalConvertFromUTF8 returns err: 0x%x",err);
            return err;
        }
        pDecodedPath = xVSS_context->UTFConversionContext.pTempOutConversionBuffer;
    }
    /**
    * End of the conversion, now use the decoded path*/
    remove((const char *)pDecodedPath);

    /*Commented because of the use of the UTF conversion*/
/*    remove(out_amr);
 */

    xVSS_context->pM4PTO3GPP_Ctxt = M4OSA_NULL;
    xVSS_context->pCallBackCtxt = M4OSA_NULL;

    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * prototype    M4OSA_ERR M4xVSS_internalConvertRGBtoYUV(M4xVSS_FramingStruct* framingCtx)
 * @brief    This function converts an RGB565 plane to YUV420 planar
 * @note    It is used only for framing effect
 *            It allocates output YUV planes
 * @param    framingCtx    (IN) The framing struct containing input RGB565 plane
 *
 * @return    M4NO_ERROR:    No error
 * @return    M4ERR_PARAMETER: At least one of the function parameters is null
 * @return    M4ERR_ALLOC: Allocation error (no more memory)
 ******************************************************************************
 */
M4OSA_ERR M4xVSS_internalConvertRGBtoYUV(M4xVSS_FramingStruct* framingCtx)
{
    M4OSA_ERR err;

    /**
     * Allocate output YUV planes */
    framingCtx->FramingYuv = (M4VIFI_ImagePlane*)M4OSA_32bitAlignedMalloc(3*sizeof(M4VIFI_ImagePlane),
         M4VS, (M4OSA_Char *)"M4xVSS_internalConvertRGBtoYUV: Output plane YUV");
    if(framingCtx->FramingYuv == M4OSA_NULL)
    {
        M4OSA_TRACE1_0("Allocation error in M4xVSS_internalConvertRGBtoYUV");
        return M4ERR_ALLOC;
    }
    framingCtx->FramingYuv[0].u_width = framingCtx->FramingRgb->u_width;
    framingCtx->FramingYuv[0].u_height = framingCtx->FramingRgb->u_height;
    framingCtx->FramingYuv[0].u_topleft = 0;
    framingCtx->FramingYuv[0].u_stride = framingCtx->FramingRgb->u_width;
    framingCtx->FramingYuv[0].pac_data =
         (M4VIFI_UInt8*)M4OSA_32bitAlignedMalloc((framingCtx->FramingYuv[0].u_width\
            *framingCtx->FramingYuv[0].u_height*3)>>1, M4VS, (M4OSA_Char *)\
                "Alloc for the Convertion output YUV");;
    if(framingCtx->FramingYuv[0].pac_data == M4OSA_NULL)
    {
        M4OSA_TRACE1_0("Allocation error in M4xVSS_internalConvertRGBtoYUV");
        return M4ERR_ALLOC;
    }
    framingCtx->FramingYuv[1].u_width = (framingCtx->FramingRgb->u_width)>>1;
    framingCtx->FramingYuv[1].u_height = (framingCtx->FramingRgb->u_height)>>1;
    framingCtx->FramingYuv[1].u_topleft = 0;
    framingCtx->FramingYuv[1].u_stride = (framingCtx->FramingRgb->u_width)>>1;
    framingCtx->FramingYuv[1].pac_data = framingCtx->FramingYuv[0].pac_data \
        + framingCtx->FramingYuv[0].u_width * framingCtx->FramingYuv[0].u_height;
    framingCtx->FramingYuv[2].u_width = (framingCtx->FramingRgb->u_width)>>1;
    framingCtx->FramingYuv[2].u_height = (framingCtx->FramingRgb->u_height)>>1;
    framingCtx->FramingYuv[2].u_topleft = 0;
    framingCtx->FramingYuv[2].u_stride = (framingCtx->FramingRgb->u_width)>>1;
    framingCtx->FramingYuv[2].pac_data = framingCtx->FramingYuv[1].pac_data \
        + framingCtx->FramingYuv[1].u_width * framingCtx->FramingYuv[1].u_height;

    /**
     * Convert input RGB 565 to YUV 420 to be able to merge it with output video in framing
      effect */
    err = M4VIFI_xVSS_RGB565toYUV420(M4OSA_NULL, framingCtx->FramingRgb, framingCtx->FramingYuv);
    if(err != M4NO_ERROR)
    {
        M4OSA_TRACE1_1("M4xVSS_internalConvertRGBtoYUV:\
             error when converting from RGB to YUV: 0x%x\n", err);
    }

    framingCtx->duration = 0;
    framingCtx->previousClipTime = -1;
    framingCtx->previewOffsetClipTime = -1;

    /**
     * Only one element in the chained list (no animated image with RGB buffer...) */
    framingCtx->pCurrent = framingCtx;
    framingCtx->pNext = framingCtx;

    return M4NO_ERROR;
}

M4OSA_ERR M4xVSS_internalSetPlaneTransparent(M4OSA_UInt8* planeIn, M4OSA_UInt32 size)
{
    M4OSA_UInt32 i;
    M4OSA_UInt8* plane = planeIn;
    M4OSA_UInt8 transparent1 = (M4OSA_UInt8)((TRANSPARENT_COLOR & 0xFF00)>>8);
    M4OSA_UInt8 transparent2 = (M4OSA_UInt8)TRANSPARENT_COLOR;

    for(i=0; i<(size>>1); i++)
    {
        *plane++ = transparent1;
        *plane++ = transparent2;
    }

    return M4NO_ERROR;
}


/**
 ******************************************************************************
 * prototype M4OSA_ERR M4xVSS_internalConvertARBG888toYUV420_FrammingEffect(M4OSA_Context pContext,
 *                                                M4VSS3GPP_EffectSettings* pEffect,
 *                                                M4xVSS_FramingStruct* framingCtx,
                                                  M4VIDEOEDITING_VideoFrameSize OutputVideoResolution)
 *
 * @brief    This function converts ARGB8888 input file  to YUV420 whenused for framming effect
 * @note    The input ARGB8888 file path is contained in the pEffect structure
 *            If the ARGB8888 must be resized to fit output video size, this function
 *            will do it.
 * @param    pContext    (IN) The integrator own context
 * @param    pEffect        (IN) The effect structure containing all informations on
 *                        the file to decode, resizing ...
 * @param    framingCtx    (IN/OUT) Structure in which the output RGB will be stored
 *
 * @return    M4NO_ERROR:    No error
 * @return    M4ERR_PARAMETER: At least one of the function parameters is null
 * @return    M4ERR_ALLOC: Allocation error (no more memory)
 * @return    M4ERR_FILE_NOT_FOUND: File not found.
 ******************************************************************************
 */


M4OSA_ERR M4xVSS_internalConvertARGB888toYUV420_FrammingEffect(M4OSA_Context pContext,
                                                               M4VSS3GPP_EffectSettings* pEffect,
                                                               M4xVSS_FramingStruct* framingCtx,
                                                               M4VIDEOEDITING_VideoFrameSize\
                                                               OutputVideoResolution)
{
    M4OSA_ERR err = M4NO_ERROR;
    M4OSA_Context pARGBIn;
    M4OSA_UInt32 file_size;
    M4xVSS_Context* xVSS_context = (M4xVSS_Context*)pContext;
    M4OSA_UInt32 width, height, width_out, height_out;
    M4OSA_Void* pFile = pEffect->xVSS.pFramingFilePath;
    M4OSA_UInt8 transparent1 = (M4OSA_UInt8)((TRANSPARENT_COLOR & 0xFF00)>>8);
    M4OSA_UInt8 transparent2 = (M4OSA_UInt8)TRANSPARENT_COLOR;
    /*UTF conversion support*/
    M4OSA_Char* pDecodedPath = M4OSA_NULL;
    M4OSA_UInt32 i = 0,j = 0;
    M4VIFI_ImagePlane rgbPlane;
    M4OSA_UInt32 frameSize_argb=(framingCtx->width * framingCtx->height * 4);
    M4OSA_UInt32 frameSize;
    M4OSA_UInt32 tempAlphaPercent = 0;
    M4VIFI_UInt8* TempPacData = M4OSA_NULL;
    M4OSA_UInt16 *ptr = M4OSA_NULL;
    M4OSA_UInt32 z = 0;

    M4OSA_TRACE3_0("M4xVSS_internalConvertARGB888toYUV420_FrammingEffect: Entering ");

    M4OSA_TRACE1_2("M4xVSS_internalConvertARGB888toYUV420_FrammingEffect width and height %d %d ",
        framingCtx->width,framingCtx->height);

    M4OSA_UInt8 *pTmpData = (M4OSA_UInt8*) M4OSA_32bitAlignedMalloc(frameSize_argb, M4VS, (M4OSA_Char*)\
        "Image argb data");
    if(pTmpData == M4OSA_NULL) {
        M4OSA_TRACE1_0("Failed to allocate memory for Image clip");
        return M4ERR_ALLOC;
    }
    /**
     * UTF conversion: convert the file path into the customer format*/
    pDecodedPath = pFile;

    if(xVSS_context->UTFConversionContext.pConvFromUTF8Fct != M4OSA_NULL
            && xVSS_context->UTFConversionContext.pTempOutConversionBuffer != M4OSA_NULL)
    {
        M4OSA_UInt32 length = 0;
        err = M4xVSS_internalConvertFromUTF8(xVSS_context, (M4OSA_Void*) pFile,
             (M4OSA_Void*) xVSS_context->UTFConversionContext.pTempOutConversionBuffer, &length);
        if(err != M4NO_ERROR)
        {
            M4OSA_TRACE1_1("M4xVSS_internalDecodePNG:\
                 M4xVSS_internalConvertFromUTF8 returns err: 0x%x",err);
            free(pTmpData);
            pTmpData = M4OSA_NULL;
            return err;
        }
        pDecodedPath = xVSS_context->UTFConversionContext.pTempOutConversionBuffer;
    }

    /**
    * End of the conversion, now use the decoded path*/

     /* Open input ARGB8888 file and store it into memory */
    err = xVSS_context->pFileReadPtr->openRead(&pARGBIn, pDecodedPath, M4OSA_kFileRead);

    if(err != M4NO_ERROR)
    {
        M4OSA_TRACE1_2("Can't open input ARGB8888 file %s, error: 0x%x\n",pFile, err);
        free(pTmpData);
        pTmpData = M4OSA_NULL;
        return err;
    }

    err = xVSS_context->pFileReadPtr->readData(pARGBIn,(M4OSA_MemAddr8)pTmpData, &frameSize_argb);
    if(err != M4NO_ERROR)
    {
        xVSS_context->pFileReadPtr->closeRead(pARGBIn);
        free(pTmpData);
        pTmpData = M4OSA_NULL;
        return err;
    }


    err =  xVSS_context->pFileReadPtr->closeRead(pARGBIn);
    if(err != M4NO_ERROR)
    {
        M4OSA_TRACE1_2("Can't close input png file %s, error: 0x%x\n",pFile, err);
        free(pTmpData);
        pTmpData = M4OSA_NULL;
        return err;
    }


    rgbPlane.u_height = framingCtx->height;
    rgbPlane.u_width = framingCtx->width;
    rgbPlane.u_stride = rgbPlane.u_width*3;
    rgbPlane.u_topleft = 0;

    frameSize = (rgbPlane.u_width * rgbPlane.u_height * 3); //Size of RGB888 data
    rgbPlane.pac_data = (M4VIFI_UInt8*)M4OSA_32bitAlignedMalloc(((frameSize)+ (2 * framingCtx->width)),
         M4VS, (M4OSA_Char*)"Image clip RGB888 data");
    if(rgbPlane.pac_data == M4OSA_NULL)
    {
        M4OSA_TRACE1_0("Failed to allocate memory for Image clip");
        free(pTmpData);
        return M4ERR_ALLOC;
    }

    M4OSA_TRACE1_0("M4xVSS_internalConvertARGB888toYUV420_FrammingEffect:\
          Remove the alpha channel  ");

    /* premultiplied alpha % on RGB */
    for (i=0, j = 0; i < frameSize_argb; i += 4) {
        /* this is alpha value */
        if ((i % 4) == 0)
        {
            tempAlphaPercent = pTmpData[i];
        }

        /* R */
        rgbPlane.pac_data[j] = pTmpData[i+1];
        j++;

        /* G */
        if (tempAlphaPercent > 0) {
            rgbPlane.pac_data[j] = pTmpData[i+2];
            j++;
        } else {/* In case of alpha value 0, make GREEN to 255 */
            rgbPlane.pac_data[j] = 255; //pTmpData[i+2];
            j++;
        }

        /* B */
        rgbPlane.pac_data[j] = pTmpData[i+3];
        j++;
    }

    free(pTmpData);
    pTmpData = M4OSA_NULL;

    /* convert RGB888 to RGB565 */

    /* allocate temp RGB 565 buffer */
    TempPacData = (M4VIFI_UInt8*)M4OSA_32bitAlignedMalloc(frameSize +
                       (4 * (framingCtx->width + framingCtx->height + 1)),
                        M4VS, (M4OSA_Char*)"Image clip RGB565 data");
    if (TempPacData == M4OSA_NULL) {
        M4OSA_TRACE1_0("Failed to allocate memory for Image clip RGB565 data");
        free(rgbPlane.pac_data);
        return M4ERR_ALLOC;
    }

    ptr = (M4OSA_UInt16 *)TempPacData;
    z = 0;

    for (i = 0; i < j ; i += 3)
    {
        ptr[z++] = PACK_RGB565(0,   rgbPlane.pac_data[i],
                                    rgbPlane.pac_data[i+1],
                                    rgbPlane.pac_data[i+2]);
    }

    /* free the RBG888 and assign RGB565 */
    free(rgbPlane.pac_data);
    rgbPlane.pac_data = TempPacData;

    /**
     * Check if output sizes are odd */
    if(rgbPlane.u_height % 2 != 0)
    {
        M4VIFI_UInt8* output_pac_data = rgbPlane.pac_data;
        M4OSA_UInt32 i;
        M4OSA_TRACE1_0("M4xVSS_internalConvertARGB888toYUV420_FrammingEffect:\
             output height is odd  ");
        output_pac_data +=rgbPlane.u_width * rgbPlane.u_height*2;

        for(i=0;i<rgbPlane.u_width;i++)
        {
            *output_pac_data++ = transparent1;
            *output_pac_data++ = transparent2;
        }

        /**
         * We just add a white line to the PNG that will be transparent */
        rgbPlane.u_height++;
    }
    if(rgbPlane.u_width % 2 != 0)
    {
        /**
         * We add a new column of white (=transparent), but we need to parse all RGB lines ... */
        M4OSA_UInt32 i;
        M4VIFI_UInt8* newRGBpac_data;
        M4VIFI_UInt8* output_pac_data, *input_pac_data;

        rgbPlane.u_width++;
        M4OSA_TRACE1_0("M4xVSS_internalConvertARGB888toYUV420_FrammingEffect: \
             output width is odd  ");
        /**
         * We need to allocate a new RGB output buffer in which all decoded data
          + white line will be copied */
        newRGBpac_data = (M4VIFI_UInt8*)M4OSA_32bitAlignedMalloc(rgbPlane.u_height*rgbPlane.u_width*2\
            *sizeof(M4VIFI_UInt8), M4VS, (M4OSA_Char *)"New Framing GIF Output pac_data RGB");

        if(newRGBpac_data == M4OSA_NULL)
        {
            M4OSA_TRACE1_0("Allocation error in \
                M4xVSS_internalConvertARGB888toYUV420_FrammingEffect");
            free(rgbPlane.pac_data);
            return M4ERR_ALLOC;
        }

        output_pac_data= newRGBpac_data;
        input_pac_data = rgbPlane.pac_data;

        for(i=0;i<rgbPlane.u_height;i++)
        {
            memcpy((void *)output_pac_data, (void *)input_pac_data,
                 (rgbPlane.u_width-1)*2);

            output_pac_data += ((rgbPlane.u_width-1)*2);
            /* Put the pixel to transparency color */
            *output_pac_data++ = transparent1;
            *output_pac_data++ = transparent2;

            input_pac_data += ((rgbPlane.u_width-1)*2);
        }
        free(rgbPlane.pac_data);
        rgbPlane.pac_data = newRGBpac_data;
    }

    /* reset stride */
    rgbPlane.u_stride = rgbPlane.u_width*2;

    /**
     * Initialize chained list parameters */
    framingCtx->duration = 0;
    framingCtx->previousClipTime = -1;
    framingCtx->previewOffsetClipTime = -1;

    /**
     * Only one element in the chained list (no animated image ...) */
    framingCtx->pCurrent = framingCtx;
    framingCtx->pNext = framingCtx;

    /**
     * Get output width/height */
     switch(OutputVideoResolution)
    //switch(xVSS_context->pSettings->xVSS.outputVideoSize)
    {
    case M4VIDEOEDITING_kSQCIF:
        width_out = 128;
        height_out = 96;
        break;
    case M4VIDEOEDITING_kQQVGA:
        width_out = 160;
        height_out = 120;
        break;
    case M4VIDEOEDITING_kQCIF:
        width_out = 176;
        height_out = 144;
        break;
    case M4VIDEOEDITING_kQVGA:
        width_out = 320;
        height_out = 240;
        break;
    case M4VIDEOEDITING_kCIF:
        width_out = 352;
        height_out = 288;
        break;
    case M4VIDEOEDITING_kVGA:
        width_out = 640;
        height_out = 480;
        break;
    case M4VIDEOEDITING_kWVGA:
        width_out = 800;
        height_out = 480;
        break;
    case M4VIDEOEDITING_kNTSC:
        width_out = 720;
        height_out = 480;
        break;
    case M4VIDEOEDITING_k640_360:
        width_out = 640;
        height_out = 360;
        break;
    case M4VIDEOEDITING_k854_480:
        // StageFright encoders require %16 resolution
        width_out = M4ENCODER_854_480_Width;
        height_out = 480;
        break;
    case M4VIDEOEDITING_k1280_720:
        width_out = 1280;
        height_out = 720;
        break;
    case M4VIDEOEDITING_k1080_720:
        // StageFright encoders require %16 resolution
        width_out = M4ENCODER_1080_720_Width;
        height_out = 720;
        break;
    case M4VIDEOEDITING_k960_720:
        width_out = 960;
        height_out = 720;
        break;
    case M4VIDEOEDITING_k1920_1080:
        width_out = 1920;
        height_out = M4ENCODER_1920_1080_Height;
        break;
    /**
     * If output video size is not given, we take QCIF size,
     * should not happen, because already done in M4xVSS_sendCommand */
    default:
        width_out = 176;
        height_out = 144;
        break;
    }

    /**
     * Allocate output planes structures */
    framingCtx->FramingRgb = (M4VIFI_ImagePlane*)M4OSA_32bitAlignedMalloc(sizeof(M4VIFI_ImagePlane), M4VS,
         (M4OSA_Char *)"Framing Output plane RGB");
    if(framingCtx->FramingRgb == M4OSA_NULL)
    {
        M4OSA_TRACE1_0("Allocation error in M4xVSS_internalConvertARGB888toYUV420_FrammingEffect");
        return M4ERR_ALLOC;
    }
    /**
     * Resize RGB if needed */
    if((pEffect->xVSS.bResize) &&
         (rgbPlane.u_width != width_out || rgbPlane.u_height != height_out))
    {
        width = width_out;
        height = height_out;

        M4OSA_TRACE1_2("M4xVSS_internalConvertARGB888toYUV420_FrammingEffect: \
             New Width and height %d %d  ",width,height);

        framingCtx->FramingRgb->u_height = height_out;
        framingCtx->FramingRgb->u_width = width_out;
        framingCtx->FramingRgb->u_stride = framingCtx->FramingRgb->u_width*2;
        framingCtx->FramingRgb->u_topleft = 0;

        framingCtx->FramingRgb->pac_data =
             (M4VIFI_UInt8*)M4OSA_32bitAlignedMalloc(framingCtx->FramingRgb->u_height*framingCtx->\
                FramingRgb->u_width*2*sizeof(M4VIFI_UInt8), M4VS,
                  (M4OSA_Char *)"Framing Output pac_data RGB");

        if(framingCtx->FramingRgb->pac_data == M4OSA_NULL)
        {
            M4OSA_TRACE1_0("Allocation error in \
                M4xVSS_internalConvertARGB888toYUV420_FrammingEffect");
            free(framingCtx->FramingRgb);
            free(rgbPlane.pac_data);
            return M4ERR_ALLOC;
        }

        M4OSA_TRACE1_0("M4xVSS_internalConvertARGB888toYUV420_FrammingEffect:  Resizing Needed ");
        M4OSA_TRACE1_2("M4xVSS_internalConvertARGB888toYUV420_FrammingEffect:\
              rgbPlane.u_height & rgbPlane.u_width %d %d",rgbPlane.u_height,rgbPlane.u_width);

        //err = M4VIFI_ResizeBilinearRGB888toRGB888(M4OSA_NULL, &rgbPlane,framingCtx->FramingRgb);
        err = M4VIFI_ResizeBilinearRGB565toRGB565(M4OSA_NULL, &rgbPlane,framingCtx->FramingRgb);

        if(err != M4NO_ERROR)
        {
            M4OSA_TRACE1_1("M4xVSS_internalConvertARGB888toYUV420_FrammingEffect :\
                when resizing RGB plane: 0x%x\n", err);
            return err;
        }

        if(rgbPlane.pac_data != M4OSA_NULL)
        {
            free(rgbPlane.pac_data);
            rgbPlane.pac_data = M4OSA_NULL;
        }
    }
    else
    {

        M4OSA_TRACE1_0("M4xVSS_internalConvertARGB888toYUV420_FrammingEffect:\
              Resizing Not Needed ");

        width = rgbPlane.u_width;
        height = rgbPlane.u_height;
        framingCtx->FramingRgb->u_height = height;
        framingCtx->FramingRgb->u_width = width;
        framingCtx->FramingRgb->u_stride = framingCtx->FramingRgb->u_width*2;
        framingCtx->FramingRgb->u_topleft = 0;
        framingCtx->FramingRgb->pac_data = rgbPlane.pac_data;
    }


    if(pEffect->xVSS.bResize)
    {
        /**
         * Force topleft to 0 for pure framing effect */
        framingCtx->topleft_x = 0;
        framingCtx->topleft_y = 0;
    }


    /**
     * Convert  RGB output to YUV 420 to be able to merge it with output video in framing
     effect */
    framingCtx->FramingYuv = (M4VIFI_ImagePlane*)M4OSA_32bitAlignedMalloc(3*sizeof(M4VIFI_ImagePlane), M4VS,
         (M4OSA_Char *)"Framing Output plane YUV");
    if(framingCtx->FramingYuv == M4OSA_NULL)
    {
        M4OSA_TRACE1_0("Allocation error in M4xVSS_internalConvertARGB888toYUV420_FrammingEffect");
        free(framingCtx->FramingRgb->pac_data);
        return M4ERR_ALLOC;
    }

    // Alloc for Y, U and V planes
    framingCtx->FramingYuv[0].u_width = ((width+1)>>1)<<1;
    framingCtx->FramingYuv[0].u_height = ((height+1)>>1)<<1;
    framingCtx->FramingYuv[0].u_topleft = 0;
    framingCtx->FramingYuv[0].u_stride = ((width+1)>>1)<<1;
    framingCtx->FramingYuv[0].pac_data = (M4VIFI_UInt8*)M4OSA_32bitAlignedMalloc
        ((framingCtx->FramingYuv[0].u_width*framingCtx->FramingYuv[0].u_height), M4VS,
            (M4OSA_Char *)"Alloc for the output Y");
    if(framingCtx->FramingYuv[0].pac_data == M4OSA_NULL)
    {
        M4OSA_TRACE1_0("Allocation error in M4xVSS_internalConvertARGB888toYUV420_FrammingEffect");
        free(framingCtx->FramingYuv);
        free(framingCtx->FramingRgb->pac_data);
        return M4ERR_ALLOC;
    }
    framingCtx->FramingYuv[1].u_width = (((width+1)>>1)<<1)>>1;
    framingCtx->FramingYuv[1].u_height = (((height+1)>>1)<<1)>>1;
    framingCtx->FramingYuv[1].u_topleft = 0;
    framingCtx->FramingYuv[1].u_stride = (((width+1)>>1)<<1)>>1;


    framingCtx->FramingYuv[1].pac_data = (M4VIFI_UInt8*)M4OSA_32bitAlignedMalloc(
        framingCtx->FramingYuv[1].u_width * framingCtx->FramingYuv[1].u_height, M4VS,
        (M4OSA_Char *)"Alloc for the output U");
    if (framingCtx->FramingYuv[1].pac_data == M4OSA_NULL) {
        free(framingCtx->FramingYuv[0].pac_data);
        free(framingCtx->FramingYuv);
        free(framingCtx->FramingRgb->pac_data);
        return M4ERR_ALLOC;
    }

    framingCtx->FramingYuv[2].u_width = (((width+1)>>1)<<1)>>1;
    framingCtx->FramingYuv[2].u_height = (((height+1)>>1)<<1)>>1;
    framingCtx->FramingYuv[2].u_topleft = 0;
    framingCtx->FramingYuv[2].u_stride = (((width+1)>>1)<<1)>>1;


    framingCtx->FramingYuv[2].pac_data = (M4VIFI_UInt8*)M4OSA_32bitAlignedMalloc(
        framingCtx->FramingYuv[2].u_width * framingCtx->FramingYuv[0].u_height, M4VS,
        (M4OSA_Char *)"Alloc for the  output V");
    if (framingCtx->FramingYuv[2].pac_data == M4OSA_NULL) {
        free(framingCtx->FramingYuv[1].pac_data);
        free(framingCtx->FramingYuv[0].pac_data);
        free(framingCtx->FramingYuv);
        free(framingCtx->FramingRgb->pac_data);
        return M4ERR_ALLOC;
    }

    M4OSA_TRACE3_0("M4xVSS_internalConvertARGB888toYUV420_FrammingEffect:\
        convert RGB to YUV ");

    //err = M4VIFI_RGB888toYUV420(M4OSA_NULL, framingCtx->FramingRgb,  framingCtx->FramingYuv);
    err = M4VIFI_RGB565toYUV420(M4OSA_NULL, framingCtx->FramingRgb,  framingCtx->FramingYuv);

    if (err != M4NO_ERROR)
    {
        M4OSA_TRACE1_1("SPS png: error when converting from RGB to YUV: 0x%x\n", err);
    }
    M4OSA_TRACE3_0("M4xVSS_internalConvertARGB888toYUV420_FrammingEffect:  Leaving ");
    return err;
}

/**
 ******************************************************************************
 * prototype    M4OSA_ERR M4xVSS_internalGenerateEditedFile(M4OSA_Context pContext)
 *
 * @brief    This function prepares VSS for editing
 * @note    It also set special xVSS effect as external effects for the VSS
 * @param    pContext    (IN) The integrator own context
 *
 * @return    M4NO_ERROR:    No error
 * @return    M4ERR_PARAMETER: At least one of the function parameters is null
 * @return    M4ERR_ALLOC: Allocation error (no more memory)
 ******************************************************************************
 */
M4OSA_ERR M4xVSS_internalGenerateEditedFile(M4OSA_Context pContext)
{
    M4xVSS_Context* xVSS_context = (M4xVSS_Context*)pContext;
    M4VSS3GPP_EditContext pVssCtxt;
    M4OSA_UInt32 i,j;
    M4OSA_ERR err;

    /**
     * Create a VSS 3GPP edition instance */
    err = M4VSS3GPP_editInit( &pVssCtxt, xVSS_context->pFileReadPtr, xVSS_context->pFileWritePtr);
    if (err != M4NO_ERROR)
    {
        M4OSA_TRACE1_1("M4xVSS_internalGenerateEditedFile: M4VSS3GPP_editInit returned 0x%x\n",
            err);
        M4VSS3GPP_editCleanUp(pVssCtxt);
        /**
         * Set the VSS context to NULL */
        xVSS_context->pCurrentEditContext = M4OSA_NULL;
        return err;
    }

        M4VSS3GPP_InternalEditContext* pVSSContext =
            (M4VSS3GPP_InternalEditContext*)pVssCtxt;
        pVSSContext->xVSS.outputVideoFormat =
            xVSS_context->pSettings->xVSS.outputVideoFormat;
        pVSSContext->xVSS.outputVideoSize =
            xVSS_context->pSettings->xVSS.outputVideoSize ;
        pVSSContext->xVSS.outputAudioFormat =
            xVSS_context->pSettings->xVSS.outputAudioFormat;
        pVSSContext->xVSS.outputAudioSamplFreq =
            xVSS_context->pSettings->xVSS.outputAudioSamplFreq;
        pVSSContext->xVSS.outputVideoBitrate =
            xVSS_context->pSettings->xVSS.outputVideoBitrate ;
        pVSSContext->xVSS.outputAudioBitrate =
            xVSS_context->pSettings->xVSS.outputAudioBitrate ;
        pVSSContext->xVSS.bAudioMono =
            xVSS_context->pSettings->xVSS.bAudioMono;
        pVSSContext->xVSS.outputVideoProfile =
            xVSS_context->pSettings->xVSS.outputVideoProfile;
        pVSSContext->xVSS.outputVideoLevel =
            xVSS_context->pSettings->xVSS.outputVideoLevel;
    /* In case of MMS use case, we fill directly into the VSS context the targeted bitrate */
    if(xVSS_context->targetedBitrate != 0)
    {
        M4VSS3GPP_InternalEditContext* pVSSContext = (M4VSS3GPP_InternalEditContext*)pVssCtxt;

        pVSSContext->bIsMMS = M4OSA_TRUE;
        pVSSContext->uiMMSVideoBitrate = xVSS_context->targetedBitrate;
        pVSSContext->MMSvideoFramerate = xVSS_context->pSettings->videoFrameRate;
    }

    /*Warning: since the adding of the UTF conversion, pSettings has been changed in the next
    part in  pCurrentEditSettings (there is a specific current editing structure for the saving,
     as for the preview)*/

    /**
     * Set the external video effect functions, for saving mode (to be moved to
      M4xVSS_saveStart() ?)*/
    for (i=0; i<xVSS_context->pCurrentEditSettings->uiClipNumber; i++)
    {
        for (j=0; j<xVSS_context->pCurrentEditSettings->nbEffects; j++)
        {
            if (M4xVSS_kVideoEffectType_BlackAndWhite ==
            xVSS_context->pCurrentEditSettings->Effects[j].VideoEffectType)
            {
                xVSS_context->pCurrentEditSettings->Effects[j].ExtVideoEffectFct =
                 M4VSS3GPP_externalVideoEffectColor;
                //xVSS_context->pSettings->Effects[j].pExtVideoEffectFctCtxt =
                // (M4OSA_Void*)M4xVSS_kVideoEffectType_BlackAndWhite;
                /*commented FB*/
                /**
                 * We do not need to set the color context, it is already set
                 during sendCommand function */
            }
            if (M4xVSS_kVideoEffectType_Pink ==
                xVSS_context->pCurrentEditSettings->Effects[j].VideoEffectType)
            {
                xVSS_context->pCurrentEditSettings->Effects[j].ExtVideoEffectFct =
                 M4VSS3GPP_externalVideoEffectColor;
                //xVSS_context->pSettings->Effects[j].pExtVideoEffectFctCtxt =
                // (M4OSA_Void*)M4xVSS_kVideoEffectType_Pink; /**< we don't
                // use any function context */
                /*commented FB*/
                /**
                 * We do not need to set the color context,
                  it is already set during sendCommand function */
            }
            if (M4xVSS_kVideoEffectType_Green ==
                 xVSS_context->pCurrentEditSettings->Effects[j].VideoEffectType)
            {
                xVSS_context->pCurrentEditSettings->Effects[j].ExtVideoEffectFct =
                    M4VSS3GPP_externalVideoEffectColor;
                //xVSS_context->pSettings->Effects[j].pExtVideoEffectFctCtxt =
                    // (M4OSA_Void*)M4xVSS_kVideoEffectType_Green;
                     /**< we don't use any function context */
                /*commented FB*/
                /**
                 * We do not need to set the color context, it is already set during
                  sendCommand function */
            }
            if (M4xVSS_kVideoEffectType_Sepia ==
                 xVSS_context->pCurrentEditSettings->Effects[j].VideoEffectType)
            {
                xVSS_context->pCurrentEditSettings->Effects[j].ExtVideoEffectFct =
                 M4VSS3GPP_externalVideoEffectColor;
                //xVSS_context->pSettings->Effects[j].pExtVideoEffectFctCtxt =
                // (M4OSA_Void*)M4xVSS_kVideoEffectType_Sepia;
                /**< we don't use any function context */
                /*commented FB*/
                /**
                 * We do not need to set the color context, it is already set during
                 sendCommand function */
            }
            if (M4xVSS_kVideoEffectType_Fifties ==
             xVSS_context->pCurrentEditSettings->Effects[j].VideoEffectType)
            {
                xVSS_context->pCurrentEditSettings->Effects[j].ExtVideoEffectFct =
                 M4VSS3GPP_externalVideoEffectFifties;
                /**
                 * We do not need to set the framing context, it is already set during
                 sendCommand function */
            }
            if (M4xVSS_kVideoEffectType_Negative ==
             xVSS_context->pCurrentEditSettings->Effects[j].VideoEffectType)
            {
                xVSS_context->pCurrentEditSettings->Effects[j].ExtVideoEffectFct =
                 M4VSS3GPP_externalVideoEffectColor;
                //xVSS_context->pSettings->Effects[j].pExtVideoEffectFctCtxt =
                // (M4OSA_Void*)M4xVSS_kVideoEffectType_Negative;
                 /**< we don't use any function context */
                /*commented FB*/
                /**
                 * We do not need to set the color context, it is already set during
                  sendCommand function */
            }
            if (M4xVSS_kVideoEffectType_Framing ==
             xVSS_context->pCurrentEditSettings->Effects[j].VideoEffectType)
            {
                xVSS_context->pCurrentEditSettings->Effects[j].ExtVideoEffectFct =
                 M4VSS3GPP_externalVideoEffectFraming;
                /**
                 * We do not need to set the framing context, it is already set during
                 sendCommand function */
            }
            if (M4xVSS_kVideoEffectType_ZoomIn ==
             xVSS_context->pSettings->Effects[j].VideoEffectType)
            {
                xVSS_context->pCurrentEditSettings->Effects[j].ExtVideoEffectFct =
                 M4VSS3GPP_externalVideoEffectZoom;
                xVSS_context->pCurrentEditSettings->Effects[j].pExtVideoEffectFctCtxt =
                 (M4OSA_Void*)M4xVSS_kVideoEffectType_ZoomIn; /**< we don't use any
                 function context */
            }
            if (M4xVSS_kVideoEffectType_ZoomOut ==
             xVSS_context->pCurrentEditSettings->Effects[j].VideoEffectType)
            {
                xVSS_context->pCurrentEditSettings->Effects[j].ExtVideoEffectFct =
                 M4VSS3GPP_externalVideoEffectZoom;
                xVSS_context->pCurrentEditSettings->Effects[j].pExtVideoEffectFctCtxt =
                 (M4OSA_Void*)M4xVSS_kVideoEffectType_ZoomOut; /**< we don't use any
                 function context */
            }
            if (M4xVSS_kVideoEffectType_ColorRGB16 ==
             xVSS_context->pCurrentEditSettings->Effects[j].VideoEffectType)
            {
                xVSS_context->pCurrentEditSettings->Effects[j].ExtVideoEffectFct =
                 M4VSS3GPP_externalVideoEffectColor;
                //xVSS_context->pSettings->Effects[j].pExtVideoEffectFctCtxt =
                // (M4OSA_Void*)M4xVSS_kVideoEffectType_ColorRGB16;
                /**< we don't use any function context */
                /**
                 * We do not need to set the color context, it is already set during
                 sendCommand function */
            }
            if (M4xVSS_kVideoEffectType_Gradient ==
             xVSS_context->pCurrentEditSettings->Effects[j].VideoEffectType)
            {
                xVSS_context->pCurrentEditSettings->Effects[j].ExtVideoEffectFct =
                 M4VSS3GPP_externalVideoEffectColor;
                //xVSS_context->pSettings->Effects[j].pExtVideoEffectFctCtxt =
                // (M4OSA_Void*)M4xVSS_kVideoEffectType_ColorRGB16;
                /**< we don't use any function context */
                /**
                 * We do not need to set the color context, it is already set during
                 sendCommand function */
            }

        }
    }

    /**
     * Open the VSS 3GPP */
    err = M4VSS3GPP_editOpen(pVssCtxt, xVSS_context->pCurrentEditSettings);
    if (err != M4NO_ERROR)
    {
        M4OSA_TRACE1_1("M4xVSS_internalGenerateEditedFile:\
             M4VSS3GPP_editOpen returned 0x%x\n",err);
        M4VSS3GPP_editCleanUp(pVssCtxt);
        /**
         * Set the VSS context to NULL */
        xVSS_context->pCurrentEditContext = M4OSA_NULL;
        return err;
    }

    /**
     * Save VSS context to be able to close / free VSS later */
    xVSS_context->pCurrentEditContext = pVssCtxt;

    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * prototype    M4OSA_ERR M4xVSS_internalCloseEditedFile(M4OSA_Context pContext)
 *
 * @brief    This function cleans up VSS
 * @note
 * @param    pContext    (IN) The integrator own context
 *
 * @return    M4NO_ERROR:    No error
 * @return    M4ERR_PARAMETER: At least one of the function parameters is null
 ******************************************************************************
 */
M4OSA_ERR M4xVSS_internalCloseEditedFile(M4OSA_Context pContext)
{
    M4xVSS_Context* xVSS_context = (M4xVSS_Context*)pContext;
    M4VSS3GPP_EditContext pVssCtxt = xVSS_context->pCurrentEditContext;
    M4OSA_ERR err;

    if(xVSS_context->pCurrentEditContext != M4OSA_NULL)
    {
        /**
         * Close the VSS 3GPP */
        err = M4VSS3GPP_editClose(pVssCtxt);
        if (err != M4NO_ERROR)
        {
            M4OSA_TRACE1_1("M4xVSS_internalCloseEditedFile:\
                 M4VSS3GPP_editClose returned 0x%x\n",err);
            M4VSS3GPP_editCleanUp(pVssCtxt);
            /**
             * Set the VSS context to NULL */
            xVSS_context->pCurrentEditContext = M4OSA_NULL;
            return err;
        }

        /**
         * Free this VSS3GPP edition instance */
        err = M4VSS3GPP_editCleanUp(pVssCtxt);
        /**
         * Set the VSS context to NULL */
        xVSS_context->pCurrentEditContext = M4OSA_NULL;
        if (err != M4NO_ERROR)
        {
            M4OSA_TRACE1_1("M4xVSS_internalCloseEditedFile: \
                M4VSS3GPP_editCleanUp returned 0x%x\n",err);
            return err;
        }
    }

    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * prototype    M4OSA_ERR M4xVSS_internalGenerateAudioMixFile(M4OSA_Context pContext)
 *
 * @brief    This function prepares VSS for audio mixing
 * @note    It takes its parameters from the BGM settings in the xVSS internal context
 * @param    pContext    (IN) The integrator own context
 *
 * @return    M4NO_ERROR:    No error
 * @return    M4ERR_PARAMETER: At least one of the function parameters is null
 * @return    M4ERR_ALLOC: Allocation error (no more memory)
 ******************************************************************************
 */
/***
 * FB: the function has been modified since the structure used for the saving is now the
 *  pCurrentEditSettings and not the pSettings
 * This change has been added for the UTF support
 * All the "xVSS_context->pSettings" has been replaced by "xVSS_context->pCurrentEditSettings"
 ***/
M4OSA_ERR M4xVSS_internalGenerateAudioMixFile(M4OSA_Context pContext)
{
    M4xVSS_Context* xVSS_context = (M4xVSS_Context*)pContext;
    M4VSS3GPP_AudioMixingSettings* pAudioMixSettings;
    M4VSS3GPP_AudioMixingContext pAudioMixingCtxt;
    M4OSA_ERR err;
    M4VIDEOEDITING_ClipProperties fileProperties;

    /**
     * Allocate audio mixing settings structure and fill it with BGM parameters */
    pAudioMixSettings = (M4VSS3GPP_AudioMixingSettings*)M4OSA_32bitAlignedMalloc
        (sizeof(M4VSS3GPP_AudioMixingSettings), M4VS, (M4OSA_Char *)"pAudioMixSettings");
    if(pAudioMixSettings == M4OSA_NULL)
    {
        M4OSA_TRACE1_0("Allocation error in M4xVSS_internalGenerateAudioMixFile");
        return M4ERR_ALLOC;
    }

    if(xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack->FileType ==
         M4VIDEOEDITING_kFileType_3GPP)
    {
        err = M4xVSS_internalGetProperties((M4OSA_Context)xVSS_context,
             (M4OSA_Char*)xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack->pFile,
                 &fileProperties);
        if(err != M4NO_ERROR)
        {
            M4OSA_TRACE1_1("M4xVSS_internalGenerateAudioMixFile:\
                 impossible to retrieve audio BGM properties ->\
                     reencoding audio background music", err);
            fileProperties.AudioStreamType =
                 xVSS_context->pCurrentEditSettings->xVSS.outputAudioFormat+1;
                  /* To force BGM encoding */
        }
    }

    pAudioMixSettings->bRemoveOriginal = M4OSA_FALSE;
    pAudioMixSettings->AddedAudioFileType =
     xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack->FileType;
    pAudioMixSettings->pAddedAudioTrackFile =
     xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack->pFile;
    pAudioMixSettings->uiAddVolume =
     xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack->uiAddVolume;

    pAudioMixSettings->outputAudioFormat = xVSS_context->pSettings->xVSS.outputAudioFormat;
    pAudioMixSettings->outputASF = xVSS_context->pSettings->xVSS.outputAudioSamplFreq;
    pAudioMixSettings->outputAudioBitrate = xVSS_context->pSettings->xVSS.outputAudioBitrate;
    pAudioMixSettings->uiSamplingFrequency =
     xVSS_context->pSettings->xVSS.pBGMtrack->uiSamplingFrequency;
    pAudioMixSettings->uiNumChannels = xVSS_context->pSettings->xVSS.pBGMtrack->uiNumChannels;

    pAudioMixSettings->b_DuckingNeedeed =
     xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack->b_DuckingNeedeed;
    pAudioMixSettings->fBTVolLevel =
     (M4OSA_Float )xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack->uiAddVolume/100;
    pAudioMixSettings->InDucking_threshold =
     xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack->InDucking_threshold;
    pAudioMixSettings->InDucking_lowVolume =
     xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack->lowVolume/100;
    pAudioMixSettings->fPTVolLevel =
     (M4OSA_Float)xVSS_context->pSettings->PTVolLevel/100;
    pAudioMixSettings->bLoop = xVSS_context->pSettings->xVSS.pBGMtrack->bLoop;

    if(xVSS_context->pSettings->xVSS.bAudioMono)
    {
        pAudioMixSettings->outputNBChannels = 1;
    }
    else
    {
        pAudioMixSettings->outputNBChannels = 2;
    }

    /**
     * Fill audio mix settings with BGM parameters */
    pAudioMixSettings->uiBeginLoop =
     xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack->uiBeginLoop;
    pAudioMixSettings->uiEndLoop =
     xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack->uiEndLoop;
    pAudioMixSettings->uiAddCts =
     xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack->uiAddCts;

    /**
     * Output file of the audio mixer will be final file (audio mixing is the last step) */
    pAudioMixSettings->pOutputClipFile = xVSS_context->pOutputFile;
    pAudioMixSettings->pTemporaryFile = xVSS_context->pTemporaryFile;

    /**
     * Input file of the audio mixer is a temporary file containing all audio/video editions */
    pAudioMixSettings->pOriginalClipFile = xVSS_context->pCurrentEditSettings->pOutputFile;

    /**
     * Save audio mixing settings pointer to be able to free it in
     M4xVSS_internalCloseAudioMixedFile function */
    xVSS_context->pAudioMixSettings = pAudioMixSettings;

    /**
     * Create a VSS 3GPP audio mixing instance */
    err = M4VSS3GPP_audioMixingInit(&pAudioMixingCtxt, pAudioMixSettings,
         xVSS_context->pFileReadPtr, xVSS_context->pFileWritePtr);

    /**
     * Save audio mixing context to be able to call audio mixing step function in
      M4xVSS_step function */
    xVSS_context->pAudioMixContext = pAudioMixingCtxt;

    if (err != M4NO_ERROR)
    {
        M4OSA_TRACE1_1("M4xVSS_internalGenerateAudioMixFile:\
             M4VSS3GPP_audioMixingInit returned 0x%x\n",err);
        //M4VSS3GPP_audioMixingCleanUp(pAudioMixingCtxt);
        return err;
    }

    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * prototype    M4OSA_ERR M4xVSS_internalCloseAudioMixedFile(M4OSA_Context pContext)
 *
 * @brief    This function cleans up VSS for audio mixing
 * @note
 * @param    pContext    (IN) The integrator own context
 *
 * @return    M4NO_ERROR:    No error
 * @return    M4ERR_PARAMETER: At least one of the function parameters is null
 ******************************************************************************
 */
M4OSA_ERR M4xVSS_internalCloseAudioMixedFile(M4OSA_Context pContext)
{
    M4xVSS_Context* xVSS_context = (M4xVSS_Context*)pContext;
    M4OSA_ERR err;

    /**
     * Free this VSS3GPP audio mixing instance */
    if(xVSS_context->pAudioMixContext != M4OSA_NULL)
    {
        err = M4VSS3GPP_audioMixingCleanUp(xVSS_context->pAudioMixContext);
        if (err != M4NO_ERROR)
        {
            M4OSA_TRACE1_1("M4xVSS_internalCloseAudioMixedFile:\
                 M4VSS3GPP_audioMixingCleanUp returned 0x%x\n",err);
            return err;
        }
    }

    /**
     * Free VSS audio mixing settings */
    if(xVSS_context->pAudioMixSettings != M4OSA_NULL)
    {
        free(xVSS_context->pAudioMixSettings);
        xVSS_context->pAudioMixSettings = M4OSA_NULL;
    }

    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * prototype    M4OSA_ERR M4xVSS_internalFreePreview(M4OSA_Context pContext)
 *
 * @brief    This function cleans up preview edition structure used to generate
 *            preview.3gp file given to the VPS
 * @note    It also free the preview structure given to the VPS
 * @param    pContext    (IN) The integrator own context
 *
 * @return    M4NO_ERROR:    No error
 * @return    M4ERR_PARAMETER: At least one of the function parameters is null
 ******************************************************************************
 */
M4OSA_ERR M4xVSS_internalFreePreview(M4OSA_Context pContext)
{
    M4xVSS_Context* xVSS_context = (M4xVSS_Context*)pContext;
    M4OSA_UInt8 i;

    /**
     * Free clip/transition settings */
    for(i=0; i<xVSS_context->pCurrentEditSettings->uiClipNumber; i++)
    {
        M4xVSS_FreeClipSettings(xVSS_context->pCurrentEditSettings->pClipList[i]);

        free((xVSS_context->pCurrentEditSettings->pClipList[i]));
        xVSS_context->pCurrentEditSettings->pClipList[i] = M4OSA_NULL;

        /**
         * Because there is 1 less transition than clip number */
        if(i != xVSS_context->pCurrentEditSettings->uiClipNumber-1)
        {
            free((xVSS_context->pCurrentEditSettings->pTransitionList[i]));
            xVSS_context->pCurrentEditSettings->pTransitionList[i] = M4OSA_NULL;
        }
    }

    /**
     * Free clip/transition list */
    if(xVSS_context->pCurrentEditSettings->pClipList != M4OSA_NULL)
    {
        free((xVSS_context->pCurrentEditSettings->pClipList));
        xVSS_context->pCurrentEditSettings->pClipList = M4OSA_NULL;
    }
    if(xVSS_context->pCurrentEditSettings->pTransitionList != M4OSA_NULL)
    {
        free((xVSS_context->pCurrentEditSettings->pTransitionList));
        xVSS_context->pCurrentEditSettings->pTransitionList = M4OSA_NULL;
    }

    /**
     * Free output preview file path */
    if(xVSS_context->pCurrentEditSettings->pOutputFile != M4OSA_NULL)
    {
        free(xVSS_context->pCurrentEditSettings->pOutputFile);
        xVSS_context->pCurrentEditSettings->pOutputFile = M4OSA_NULL;
    }

    /**
     * Free temporary preview file path */
    if(xVSS_context->pCurrentEditSettings->pTemporaryFile != M4OSA_NULL)
    {
        remove((const char *)xVSS_context->pCurrentEditSettings->pTemporaryFile);
        free(xVSS_context->pCurrentEditSettings->pTemporaryFile);
        xVSS_context->pCurrentEditSettings->pTemporaryFile = M4OSA_NULL;
    }

    /**
     * Free "local" BGM settings */
    if(xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack != M4OSA_NULL)
    {
        if(xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack->pFile != M4OSA_NULL)
        {
            free(xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack->pFile);
            xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack->pFile = M4OSA_NULL;
        }
        free(xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack);
        xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack = M4OSA_NULL;
    }

    /**
     * Free current edit settings structure */
    if(xVSS_context->pCurrentEditSettings != M4OSA_NULL)
    {
        free(xVSS_context->pCurrentEditSettings);
        xVSS_context->pCurrentEditSettings = M4OSA_NULL;
    }

    /**
     * Free preview effects given to application */
    if(M4OSA_NULL != xVSS_context->pPreviewSettings->Effects)
    {
        free(xVSS_context->pPreviewSettings->Effects);
        xVSS_context->pPreviewSettings->Effects = M4OSA_NULL;
        xVSS_context->pPreviewSettings->nbEffects = 0;
    }

    return M4NO_ERROR;
}


/**
 ******************************************************************************
 * prototype    M4OSA_ERR M4xVSS_internalFreeSaving(M4OSA_Context pContext)
 *
 * @brief    This function cleans up saving edition structure used to generate
 *            output.3gp file given to the VPS
 * @note
 * @param    pContext    (IN) The integrator own context
 *
 * @return    M4NO_ERROR:    No error
 * @return    M4ERR_PARAMETER: At least one of the function parameters is null
 ******************************************************************************
 */
M4OSA_ERR M4xVSS_internalFreeSaving(M4OSA_Context pContext)
{
    M4xVSS_Context* xVSS_context = (M4xVSS_Context*)pContext;
    M4OSA_UInt8 i;

    if(xVSS_context->pCurrentEditSettings != M4OSA_NULL)
    {
        /**
         * Free clip/transition settings */
        for(i=0; i<xVSS_context->pCurrentEditSettings->uiClipNumber; i++)
        {
            M4xVSS_FreeClipSettings(xVSS_context->pCurrentEditSettings->pClipList[i]);

            free((xVSS_context->pCurrentEditSettings->pClipList[i]));
            xVSS_context->pCurrentEditSettings->pClipList[i] = M4OSA_NULL;

            /**
             * Because there is 1 less transition than clip number */
            if(i != xVSS_context->pCurrentEditSettings->uiClipNumber-1)
            {
                free(\
                    (xVSS_context->pCurrentEditSettings->pTransitionList[i]));
                xVSS_context->pCurrentEditSettings->pTransitionList[i] = M4OSA_NULL;
            }
        }

        /**
         * Free clip/transition list */
        if(xVSS_context->pCurrentEditSettings->pClipList != M4OSA_NULL)
        {
            free((xVSS_context->pCurrentEditSettings->pClipList));
            xVSS_context->pCurrentEditSettings->pClipList = M4OSA_NULL;
        }
        if(xVSS_context->pCurrentEditSettings->pTransitionList != M4OSA_NULL)
        {
            free((xVSS_context->pCurrentEditSettings->pTransitionList));
            xVSS_context->pCurrentEditSettings->pTransitionList = M4OSA_NULL;
        }

        if(xVSS_context->pCurrentEditSettings->Effects != M4OSA_NULL)
        {
            free((xVSS_context->pCurrentEditSettings->Effects));
            xVSS_context->pCurrentEditSettings->Effects = M4OSA_NULL;
            xVSS_context->pCurrentEditSettings->nbEffects = 0;
        }

        /**
         * Free output saving file path */
        if(xVSS_context->pCurrentEditSettings->pOutputFile != M4OSA_NULL)
        {
            if(xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack != M4OSA_NULL)
            {
                remove((const char *)xVSS_context->pCurrentEditSettings->pOutputFile);
                free(xVSS_context->pCurrentEditSettings->pOutputFile);
            }
            if(xVSS_context->pOutputFile != M4OSA_NULL)
            {
                free(xVSS_context->pOutputFile);
                xVSS_context->pOutputFile = M4OSA_NULL;
            }
            xVSS_context->pSettings->pOutputFile = M4OSA_NULL;
            xVSS_context->pCurrentEditSettings->pOutputFile = M4OSA_NULL;
        }

        /**
         * Free temporary saving file path */
        if(xVSS_context->pCurrentEditSettings->pTemporaryFile != M4OSA_NULL)
        {
            remove((const char *)xVSS_context->pCurrentEditSettings->pTemporaryFile);
            free(xVSS_context->pCurrentEditSettings->pTemporaryFile);
            xVSS_context->pCurrentEditSettings->pTemporaryFile = M4OSA_NULL;
        }

        /**
         * Free "local" BGM settings */
        if(xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack != M4OSA_NULL)
        {
            if(xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack->pFile != M4OSA_NULL)
            {
                free(xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack->pFile);
                xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack->pFile = M4OSA_NULL;
            }
            free(xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack);
            xVSS_context->pCurrentEditSettings->xVSS.pBGMtrack = M4OSA_NULL;
        }

        /**
         * Free current edit settings structure */
        free(xVSS_context->pCurrentEditSettings);
        xVSS_context->pCurrentEditSettings = M4OSA_NULL;
    }

    return M4NO_ERROR;
}


/**
 ******************************************************************************
 * prototype    M4OSA_ERR M4xVSS_freeSettings(M4OSA_Context pContext)
 *
 * @brief    This function cleans up an M4VSS3GPP_EditSettings structure
 * @note
 * @param    pSettings    (IN) Pointer on M4VSS3GPP_EditSettings structure to free
 *
 * @return    M4NO_ERROR:    No error
 * @return    M4ERR_PARAMETER: At least one of the function parameters is null
 ******************************************************************************
 */
M4OSA_ERR M4xVSS_freeSettings(M4VSS3GPP_EditSettings* pSettings)
{
    M4OSA_UInt8 i,j;

    /**
     * For each clip ... */
    for(i=0; i<pSettings->uiClipNumber; i++)
    {
        /**
         * ... free clip settings */
        if(pSettings->pClipList[i] != M4OSA_NULL)
        {
            M4xVSS_FreeClipSettings(pSettings->pClipList[i]);

            free((pSettings->pClipList[i]));
            pSettings->pClipList[i] = M4OSA_NULL;
        }

        /**
         * ... free transition settings */
        if(i < pSettings->uiClipNumber-1) /* Because there is 1 less transition than clip number */
        {
            if(pSettings->pTransitionList[i] != M4OSA_NULL)
            {
                switch (pSettings->pTransitionList[i]->VideoTransitionType)
                {
                    case M4xVSS_kVideoTransitionType_AlphaMagic:

                        /**
                         * In case of Alpha Magic transition,
                          some extra parameters need to be freed */
                        if(pSettings->pTransitionList[i]->pExtVideoTransitionFctCtxt\
                             != M4OSA_NULL)
                        {
                            free((((M4xVSS_internal_AlphaMagicSettings*)\
                                pSettings->pTransitionList[i]->pExtVideoTransitionFctCtxt)->\
                                    pPlane->pac_data));
                            ((M4xVSS_internal_AlphaMagicSettings*)pSettings->pTransitionList[i\
                                ]->pExtVideoTransitionFctCtxt)->pPlane->pac_data = M4OSA_NULL;

                            free((((M4xVSS_internal_AlphaMagicSettings*)\
                                pSettings->pTransitionList[i]->\
                                    pExtVideoTransitionFctCtxt)->pPlane));
                            ((M4xVSS_internal_AlphaMagicSettings*)pSettings->pTransitionList[i]\
                                ->pExtVideoTransitionFctCtxt)->pPlane = M4OSA_NULL;

                            free((pSettings->pTransitionList[i]->\
                                pExtVideoTransitionFctCtxt));
                            pSettings->pTransitionList[i]->pExtVideoTransitionFctCtxt = M4OSA_NULL;

                            for(j=i+1;j<pSettings->uiClipNumber-1;j++)
                            {
                                if(pSettings->pTransitionList[j] != M4OSA_NULL)
                                {
                                    if(pSettings->pTransitionList[j]->VideoTransitionType ==
                                     M4xVSS_kVideoTransitionType_AlphaMagic)
                                    {
                                        M4OSA_UInt32 pCmpResult=0;
                                        pCmpResult = strcmp((const char *)pSettings->pTransitionList[i]->\
                                            xVSS.transitionSpecific.pAlphaMagicSettings->\
                                                pAlphaFilePath,
                                                (const char *)pSettings->pTransitionList[j]->\
                                                xVSS.transitionSpecific.pAlphaMagicSettings->\
                                                pAlphaFilePath);
                                        if(pCmpResult == 0)
                                        {
                                            /* Free extra internal alpha magic structure and put
                                            it to NULL to avoid refreeing it */
                                            free((pSettings->\
                                                pTransitionList[j]->pExtVideoTransitionFctCtxt));
                                            pSettings->pTransitionList[j]->\
                                                pExtVideoTransitionFctCtxt = M4OSA_NULL;
                                        }
                                    }
                                }
                            }
                        }

                        if(pSettings->pTransitionList[i]->\
                            xVSS.transitionSpecific.pAlphaMagicSettings != M4OSA_NULL)
                        {
                            if(pSettings->pTransitionList[i]->\
                                xVSS.transitionSpecific.pAlphaMagicSettings->\
                                    pAlphaFilePath != M4OSA_NULL)
                            {
                                free(pSettings->\
                                    pTransitionList[i]->\
                                        xVSS.transitionSpecific.pAlphaMagicSettings->\
                                            pAlphaFilePath);
                                pSettings->pTransitionList[i]->\
                                    xVSS.transitionSpecific.pAlphaMagicSettings->\
                                        pAlphaFilePath = M4OSA_NULL;
                            }
                            free(pSettings->pTransitionList[i]->\
                                xVSS.transitionSpecific.pAlphaMagicSettings);
                            pSettings->pTransitionList[i]->\
                                xVSS.transitionSpecific.pAlphaMagicSettings = M4OSA_NULL;

                        }

                    break;


                    case M4xVSS_kVideoTransitionType_SlideTransition:
                        if (M4OSA_NULL != pSettings->pTransitionList[i]->\
                            xVSS.transitionSpecific.pSlideTransitionSettings)
                        {
                            free(pSettings->pTransitionList[i]->\
                                xVSS.transitionSpecific.pSlideTransitionSettings);
                            pSettings->pTransitionList[i]->\
                                xVSS.transitionSpecific.pSlideTransitionSettings = M4OSA_NULL;
                        }
                        if(pSettings->pTransitionList[i]->pExtVideoTransitionFctCtxt != M4OSA_NULL)
                        {
                            free((pSettings->pTransitionList[i]->\
                                pExtVideoTransitionFctCtxt));
                            pSettings->pTransitionList[i]->pExtVideoTransitionFctCtxt = M4OSA_NULL;
                        }
                    break;
                                        default:
                    break;

                }
                /**
                 * Free transition settings structure */
                free((pSettings->pTransitionList[i]));
                pSettings->pTransitionList[i] = M4OSA_NULL;
            }
        }
    }

    /**
     * Free clip list */
    if(pSettings->pClipList != M4OSA_NULL)
    {
        free((pSettings->pClipList));
        pSettings->pClipList = M4OSA_NULL;
    }

    /**
     * Free transition list */
    if(pSettings->pTransitionList != M4OSA_NULL)
    {
        free((pSettings->pTransitionList));
        pSettings->pTransitionList = M4OSA_NULL;
    }

    /**
     * RC: Free effects list */
    if(pSettings->Effects != M4OSA_NULL)
    {
        for(i=0; i<pSettings->nbEffects; i++)
        {
            /**
             * For each clip, free framing structure if needed */
            if(pSettings->Effects[i].VideoEffectType == M4xVSS_kVideoEffectType_Framing
                || pSettings->Effects[i].VideoEffectType == M4xVSS_kVideoEffectType_Text)
            {
#ifdef DECODE_GIF_ON_SAVING
                M4xVSS_FramingContext* framingCtx = pSettings->Effects[i].pExtVideoEffectFctCtxt;
#else
                M4xVSS_FramingStruct* framingCtx = pSettings->Effects[i].pExtVideoEffectFctCtxt;
                M4xVSS_FramingStruct* framingCtx_save;
                M4xVSS_Framing3102Struct* framingCtx_first = framingCtx;
#endif

#ifdef DECODE_GIF_ON_SAVING
                if(framingCtx != M4OSA_NULL) /* Bugfix 1.2.0: crash, trying to free non existant
                 pointer */
                {
                    if(framingCtx->aFramingCtx != M4OSA_NULL)
                    {
                        {
                            if(framingCtx->aFramingCtx->FramingRgb != M4OSA_NULL)
                            {
                                free(framingCtx->aFramingCtx->\
                                    FramingRgb->pac_data);
                                framingCtx->aFramingCtx->FramingRgb->pac_data = M4OSA_NULL;
                                free(framingCtx->aFramingCtx->FramingRgb);
                                framingCtx->aFramingCtx->FramingRgb = M4OSA_NULL;
                            }
                        }
                        if(framingCtx->aFramingCtx->FramingYuv != M4OSA_NULL)
                        {
                            free(framingCtx->aFramingCtx->\
                                FramingYuv[0].pac_data);
                            framingCtx->aFramingCtx->FramingYuv[0].pac_data = M4OSA_NULL;
                           free(framingCtx->aFramingCtx->\
                                FramingYuv[1].pac_data);
                            framingCtx->aFramingCtx->FramingYuv[1].pac_data = M4OSA_NULL;
                           free(framingCtx->aFramingCtx->\
                                FramingYuv[2].pac_data);
                            framingCtx->aFramingCtx->FramingYuv[2].pac_data = M4OSA_NULL;
                            free(framingCtx->aFramingCtx->FramingYuv);
                            framingCtx->aFramingCtx->FramingYuv = M4OSA_NULL;
                        }
                        free(framingCtx->aFramingCtx);
                        framingCtx->aFramingCtx = M4OSA_NULL;
                    }
                    if(framingCtx->aFramingCtx_last != M4OSA_NULL)
                    {
                        if(framingCtx->aFramingCtx_last->FramingRgb != M4OSA_NULL)
                        {
                            free(framingCtx->aFramingCtx_last->\
                                FramingRgb->pac_data);
                            framingCtx->aFramingCtx_last->FramingRgb->pac_data = M4OSA_NULL;
                            free(framingCtx->aFramingCtx_last->\
                                FramingRgb);
                            framingCtx->aFramingCtx_last->FramingRgb = M4OSA_NULL;
                        }
                        if(framingCtx->aFramingCtx_last->FramingYuv != M4OSA_NULL)
                        {
                            free(framingCtx->aFramingCtx_last->\
                                FramingYuv[0].pac_data);
                            framingCtx->aFramingCtx_last->FramingYuv[0].pac_data = M4OSA_NULL;
                            free(framingCtx->aFramingCtx_last->FramingYuv);
                            framingCtx->aFramingCtx_last->FramingYuv = M4OSA_NULL;
                        }
                        free(framingCtx->aFramingCtx_last);
                        framingCtx->aFramingCtx_last = M4OSA_NULL;
                    }
                    if(framingCtx->pEffectFilePath != M4OSA_NULL)
                    {
                        free(framingCtx->pEffectFilePath);
                        framingCtx->pEffectFilePath = M4OSA_NULL;
                    }
                    /*In case there are still allocated*/
                    if(framingCtx->pSPSContext != M4OSA_NULL)
                    {
                    //    M4SPS_destroy(framingCtx->pSPSContext);
                        framingCtx->pSPSContext = M4OSA_NULL;
                    }
                    /*Alpha blending structure*/
                    if(framingCtx->alphaBlendingStruct  != M4OSA_NULL)
                    {
                        free(framingCtx->alphaBlendingStruct);
                        framingCtx->alphaBlendingStruct = M4OSA_NULL;
                    }

                    free(framingCtx);
                    framingCtx = M4OSA_NULL;
                }
#else
                do
                {
                    if(framingCtx != M4OSA_NULL) /* Bugfix 1.2.0: crash, trying to free non
                    existant pointer */
                    {
                        if(framingCtx->FramingRgb != M4OSA_NULL)
                        {
                            free(framingCtx->FramingRgb->pac_data);
                            framingCtx->FramingRgb->pac_data = M4OSA_NULL;
                            free(framingCtx->FramingRgb);
                            framingCtx->FramingRgb = M4OSA_NULL;
                        }
                        if(framingCtx->FramingYuv != M4OSA_NULL)
                        {
                            free(framingCtx->FramingYuv[0].pac_data);
                            framingCtx->FramingYuv[0].pac_data = M4OSA_NULL;
                            free(framingCtx->FramingYuv);
                            framingCtx->FramingYuv = M4OSA_NULL;
                        }
                        framingCtx_save = framingCtx->pNext;
                        free(framingCtx);
                        framingCtx = M4OSA_NULL;
                        framingCtx = framingCtx_save;
                    }
                    else
                    {
                        /*FB: bug fix P4ME00003002*/
                        break;
                    }
                } while(framingCtx_first != framingCtx);
#endif
            }
            else if( M4xVSS_kVideoEffectType_Fifties == pSettings->Effects[i].VideoEffectType)
            {
                /* Free Fifties context */
                M4xVSS_FiftiesStruct* FiftiesCtx = pSettings->Effects[i].pExtVideoEffectFctCtxt;

                if(FiftiesCtx != M4OSA_NULL)
                {
                    free(FiftiesCtx);
                    FiftiesCtx = M4OSA_NULL;
                }

            }
            else if( M4xVSS_kVideoEffectType_ColorRGB16 == pSettings->Effects[i].VideoEffectType
                || M4xVSS_kVideoEffectType_BlackAndWhite == pSettings->Effects[i].VideoEffectType
                || M4xVSS_kVideoEffectType_Pink == pSettings->Effects[i].VideoEffectType
                || M4xVSS_kVideoEffectType_Green == pSettings->Effects[i].VideoEffectType
                || M4xVSS_kVideoEffectType_Sepia == pSettings->Effects[i].VideoEffectType
                || M4xVSS_kVideoEffectType_Negative== pSettings->Effects[i].VideoEffectType
                || M4xVSS_kVideoEffectType_Gradient== pSettings->Effects[i].VideoEffectType)
            {
                /* Free Color context */
                M4xVSS_ColorStruct* ColorCtx = pSettings->Effects[i].pExtVideoEffectFctCtxt;

                if(ColorCtx != M4OSA_NULL)
                {
                    free(ColorCtx);
                    ColorCtx = M4OSA_NULL;
                }
            }

            /* Free simple fields */
            if(pSettings->Effects[i].xVSS.pFramingFilePath != M4OSA_NULL)
            {
                free(pSettings->Effects[i].xVSS.pFramingFilePath);
                pSettings->Effects[i].xVSS.pFramingFilePath = M4OSA_NULL;
            }
            if(pSettings->Effects[i].xVSS.pFramingBuffer != M4OSA_NULL)
            {
                free(pSettings->Effects[i].xVSS.pFramingBuffer);
                pSettings->Effects[i].xVSS.pFramingBuffer = M4OSA_NULL;
            }
            if(pSettings->Effects[i].xVSS.pTextBuffer != M4OSA_NULL)
            {
                free(pSettings->Effects[i].xVSS.pTextBuffer);
                pSettings->Effects[i].xVSS.pTextBuffer = M4OSA_NULL;
            }
        }
        free(pSettings->Effects);
        pSettings->Effects = M4OSA_NULL;
    }

    return M4NO_ERROR;
}

M4OSA_ERR M4xVSS_freeCommand(M4OSA_Context pContext)
{
    M4xVSS_Context* xVSS_context = (M4xVSS_Context*)pContext;
//    M4OSA_UInt8 i,j;

    /* Free "local" BGM settings */
    if(xVSS_context->pSettings->xVSS.pBGMtrack != M4OSA_NULL)
    {
        if(xVSS_context->pSettings->xVSS.pBGMtrack->pFile != M4OSA_NULL)
        {
            free(xVSS_context->pSettings->xVSS.pBGMtrack->pFile);
            xVSS_context->pSettings->xVSS.pBGMtrack->pFile = M4OSA_NULL;
        }
        free(xVSS_context->pSettings->xVSS.pBGMtrack);
        xVSS_context->pSettings->xVSS.pBGMtrack = M4OSA_NULL;
    }

    M4xVSS_freeSettings(xVSS_context->pSettings);

    if(xVSS_context->pPTo3GPPparamsList != M4OSA_NULL)
    {
        M4xVSS_Pto3GPP_params* pParams = xVSS_context->pPTo3GPPparamsList;
        M4xVSS_Pto3GPP_params* pParams_sauv;

        while(pParams != M4OSA_NULL)
        {
            if(pParams->pFileIn != M4OSA_NULL)
            {
                free(pParams->pFileIn);
                pParams->pFileIn = M4OSA_NULL;
            }
            if(pParams->pFileOut != M4OSA_NULL)
            {
                /* Delete temporary file */
                remove((const char *)pParams->pFileOut);
                free(pParams->pFileOut);
                pParams->pFileOut = M4OSA_NULL;
            }
            if(pParams->pFileTemp != M4OSA_NULL)
            {
                /* Delete temporary file */
#ifdef M4xVSS_RESERVED_MOOV_DISK_SPACE
                remove((const char *)pParams->pFileTemp);
                free(pParams->pFileTemp);
#endif/*M4xVSS_RESERVED_MOOV_DISK_SPACE*/
                pParams->pFileTemp = M4OSA_NULL;
            }
            pParams_sauv = pParams;
            pParams = pParams->pNext;
            free(pParams_sauv);
            pParams_sauv = M4OSA_NULL;
        }
    }

    if(xVSS_context->pMCSparamsList != M4OSA_NULL)
    {
        M4xVSS_MCS_params* pParams = xVSS_context->pMCSparamsList;
        M4xVSS_MCS_params* pParams_sauv;

        while(pParams != M4OSA_NULL)
        {
            if(pParams->pFileIn != M4OSA_NULL)
            {
                free(pParams->pFileIn);
                pParams->pFileIn = M4OSA_NULL;
            }
            if(pParams->pFileOut != M4OSA_NULL)
            {
                /* Delete temporary file */
                remove((const char *)pParams->pFileOut);
                free(pParams->pFileOut);
                pParams->pFileOut = M4OSA_NULL;
            }
            if(pParams->pFileTemp != M4OSA_NULL)
            {
                /* Delete temporary file */
#ifdef M4xVSS_RESERVED_MOOV_DISK_SPACE
                remove((const char *)pParams->pFileTemp);
                free(pParams->pFileTemp);
#endif/*M4xVSS_RESERVED_MOOV_DISK_SPACE*/
                pParams->pFileTemp = M4OSA_NULL;
            }
            pParams_sauv = pParams;
            pParams = pParams->pNext;
            free(pParams_sauv);
            pParams_sauv = M4OSA_NULL;
        }
    }

    if(xVSS_context->pcmPreviewFile != M4OSA_NULL)
    {
        free(xVSS_context->pcmPreviewFile);
        xVSS_context->pcmPreviewFile = M4OSA_NULL;
    }
    if(xVSS_context->pSettings->pOutputFile != M4OSA_NULL
        && xVSS_context->pOutputFile != M4OSA_NULL)
    {
        free(xVSS_context->pSettings->pOutputFile);
        xVSS_context->pSettings->pOutputFile = M4OSA_NULL;
        xVSS_context->pOutputFile = M4OSA_NULL;
    }

    /* Reinit all context variables */
    xVSS_context->previousClipNumber = 0;
    xVSS_context->editingStep = M4xVSS_kMicroStateEditing;
    xVSS_context->analyseStep = M4xVSS_kMicroStateAnalysePto3GPP;
    xVSS_context->pPTo3GPPparamsList = M4OSA_NULL;
    xVSS_context->pPTo3GPPcurrentParams = M4OSA_NULL;
    xVSS_context->pMCSparamsList = M4OSA_NULL;
    xVSS_context->pMCScurrentParams = M4OSA_NULL;
    xVSS_context->tempFileIndex = 0;
    xVSS_context->targetedTimescale = 0;

    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * prototype    M4OSA_ERR M4xVSS_internalGetProperties(M4OSA_Context pContext,
 *                                    M4OSA_Char* pFile,
 *                                    M4VIDEOEDITING_ClipProperties *pFileProperties)
 *
 * @brief    This function retrieve properties of an input 3GP file using MCS
 * @note
 * @param    pContext        (IN) The integrator own context
 * @param    pFile            (IN) 3GP file to analyse
 * @param    pFileProperties    (IN/OUT) Pointer on a structure that will contain
 *                            the 3GP file properties
 *
 * @return    M4NO_ERROR:    No error
 * @return    M4ERR_PARAMETER: At least one of the function parameters is null
 ******************************************************************************
 */
M4OSA_ERR M4xVSS_internalGetProperties(M4OSA_Context pContext, M4OSA_Char* pFile,
                                       M4VIDEOEDITING_ClipProperties *pFileProperties)
{
    M4xVSS_Context* xVSS_context = (M4xVSS_Context*)pContext;
    M4OSA_ERR err;
    M4MCS_Context mcs_context;

    err = M4MCS_init(&mcs_context, xVSS_context->pFileReadPtr, xVSS_context->pFileWritePtr);
    if(err != M4NO_ERROR)
    {
        M4OSA_TRACE1_1("M4xVSS_internalGetProperties: Error in M4MCS_init: 0x%x", err);
        return err;
    }

    /*open the MCS in the "normal opening" mode to retrieve the exact duration*/
    err = M4MCS_open_normalMode(mcs_context, pFile, M4VIDEOEDITING_kFileType_3GPP,
        M4OSA_NULL, M4OSA_NULL);
    if (err != M4NO_ERROR)
    {
        M4OSA_TRACE1_1("M4xVSS_internalGetProperties: Error in M4MCS_open: 0x%x", err);
        M4MCS_abort(mcs_context);
        return err;
    }

    err = M4MCS_getInputFileProperties(mcs_context, pFileProperties);
    if(err != M4NO_ERROR)
    {
        M4OSA_TRACE1_1("Error in M4MCS_getInputFileProperties: 0x%x", err);
        M4MCS_abort(mcs_context);
        return err;
    }

    err = M4MCS_abort(mcs_context);
    if (err != M4NO_ERROR)
    {
        M4OSA_TRACE1_1("M4xVSS_internalGetProperties: Error in M4MCS_abort: 0x%x", err);
        return err;
    }

    return M4NO_ERROR;
}


/**
 ******************************************************************************
 * prototype    M4OSA_ERR M4xVSS_internalGetTargetedTimeScale(M4OSA_Context pContext,
 *                                                M4OSA_UInt32* pTargetedTimeScale)
 *
 * @brief    This function retrieve targeted time scale
 * @note
 * @param    pContext            (IN)    The integrator own context
 * @param    pTargetedTimeScale    (OUT)    Targeted time scale
 *
 * @return    M4NO_ERROR:    No error
 * @return    M4ERR_PARAMETER: At least one of the function parameters is null
 ******************************************************************************
 */
M4OSA_ERR M4xVSS_internalGetTargetedTimeScale(M4OSA_Context pContext,
                                                 M4VSS3GPP_EditSettings* pSettings,
                                                  M4OSA_UInt32* pTargetedTimeScale)
{
    M4xVSS_Context* xVSS_context = (M4xVSS_Context*)pContext;
    M4OSA_ERR err;
    M4OSA_UInt32 totalDuration = 0;
    M4OSA_UInt8 i = 0;
    M4OSA_UInt32 tempTimeScale = 0, tempDuration = 0;

    for(i=0;i<pSettings->uiClipNumber;i++)
    {
        /*search timescale only in mpeg4 case*/
        if(pSettings->pClipList[i]->FileType == M4VIDEOEDITING_kFileType_3GPP
            || pSettings->pClipList[i]->FileType == M4VIDEOEDITING_kFileType_MP4
            || pSettings->pClipList[i]->FileType == M4VIDEOEDITING_kFileType_M4V)
        {
            M4VIDEOEDITING_ClipProperties fileProperties;

            /*UTF conversion support*/
            M4OSA_Char* pDecodedPath = M4OSA_NULL;

            /**
            * UTF conversion: convert into the customer format, before being used*/
            pDecodedPath = pSettings->pClipList[i]->pFile;

            if(xVSS_context->UTFConversionContext.pConvToUTF8Fct != M4OSA_NULL
                && xVSS_context->UTFConversionContext.pTempOutConversionBuffer != M4OSA_NULL)
            {
                M4OSA_UInt32 length = 0;
                err = M4xVSS_internalConvertFromUTF8(xVSS_context,
                     (M4OSA_Void*) pSettings->pClipList[i]->pFile,
                        (M4OSA_Void*) xVSS_context->UTFConversionContext.pTempOutConversionBuffer,
                             &length);
                if(err != M4NO_ERROR)
                {
                    M4OSA_TRACE1_1("M4xVSS_Init:\
                         M4xVSS_internalConvertToUTF8 returns err: 0x%x",err);
                    return err;
                }
                pDecodedPath = xVSS_context->UTFConversionContext.pTempOutConversionBuffer;
            }

            /*End of the conversion: use the decoded path*/
            err = M4xVSS_internalGetProperties(xVSS_context, pDecodedPath, &fileProperties);

            /*get input file properties*/
            /*err = M4xVSS_internalGetProperties(xVSS_context, pSettings->\
                pClipList[i]->pFile, &fileProperties);*/
            if(M4NO_ERROR != err)
            {
                M4OSA_TRACE1_1("M4xVSS_internalGetTargetedTimeScale:\
                     M4xVSS_internalGetProperties returned: 0x%x", err);
                return err;
            }
            if(fileProperties.VideoStreamType == M4VIDEOEDITING_kMPEG4)
            {
                if(pSettings->pClipList[i]->uiEndCutTime > 0)
                {
                    if(tempDuration < (pSettings->pClipList[i]->uiEndCutTime \
                        - pSettings->pClipList[i]->uiBeginCutTime))
                    {
                        tempTimeScale = fileProperties.uiVideoTimeScale;
                        tempDuration = (pSettings->pClipList[i]->uiEndCutTime\
                             - pSettings->pClipList[i]->uiBeginCutTime);
                    }
                }
                else
                {
                    if(tempDuration < (fileProperties.uiClipDuration\
                         - pSettings->pClipList[i]->uiBeginCutTime))
                    {
                        tempTimeScale = fileProperties.uiVideoTimeScale;
                        tempDuration = (fileProperties.uiClipDuration\
                             - pSettings->pClipList[i]->uiBeginCutTime);
                    }
                }
            }
        }
        if(pSettings->pClipList[i]->FileType == M4VIDEOEDITING_kFileType_ARGB8888)
        {
            /*the timescale is 30 for PTO3GP*/
            *pTargetedTimeScale = 30;
            return M4NO_ERROR;

        }
    }

    if(tempTimeScale >= 30)/*Define a minimum time scale, otherwise if the timescale is not
    enough, there will be an infinite loop in the shell encoder*/
    {
        *pTargetedTimeScale = tempTimeScale;
    }
    else
    {
        *pTargetedTimeScale = 30;
    }

    return M4NO_ERROR;
}


/**
 ******************************************************************************
 * prototype    M4VSS3GPP_externalVideoEffectColor(M4OSA_Void *pFunctionContext,
 *                                                    M4VIFI_ImagePlane *PlaneIn,
 *                                                    M4VIFI_ImagePlane *PlaneOut,
 *                                                    M4VSS3GPP_ExternalProgress *pProgress,
 *                                                    M4OSA_UInt32 uiEffectKind)
 *
 * @brief    This function apply a color effect on an input YUV420 planar frame
 * @note
 * @param    pFunctionContext(IN) Contains which color to apply (not very clean ...)
 * @param    PlaneIn            (IN) Input YUV420 planar
 * @param    PlaneOut        (IN/OUT) Output YUV420 planar
 * @param    pProgress        (IN/OUT) Progress indication (0-100)
 * @param    uiEffectKind    (IN) Unused
 *
 * @return    M4VIFI_OK:    No error
 ******************************************************************************
 */
M4OSA_ERR M4VSS3GPP_externalVideoEffectColor(M4OSA_Void *pFunctionContext,
                                             M4VIFI_ImagePlane *PlaneIn,
                                             M4VIFI_ImagePlane *PlaneOut,
                                             M4VSS3GPP_ExternalProgress *pProgress,
                                             M4OSA_UInt32 uiEffectKind)
{
    M4VIFI_Int32 plane_number;
    M4VIFI_UInt32 i,j;
    M4VIFI_UInt8 *p_buf_src, *p_buf_dest;
    M4xVSS_ColorStruct* ColorContext = (M4xVSS_ColorStruct*)pFunctionContext;

    for (plane_number = 0; plane_number < 3; plane_number++)
    {
        p_buf_src = &(PlaneIn[plane_number].pac_data[PlaneIn[plane_number].u_topleft]);
        p_buf_dest = &(PlaneOut[plane_number].pac_data[PlaneOut[plane_number].u_topleft]);
        for (i = 0; i < PlaneOut[plane_number].u_height; i++)
        {
            /**
             * Chrominance */
            if(plane_number==1 || plane_number==2)
            {
                //switch ((M4OSA_UInt32)pFunctionContext)
                // commented because a structure for the effects context exist
                switch (ColorContext->colorEffectType)
                {
                    case M4xVSS_kVideoEffectType_BlackAndWhite:
                        memset((void *)p_buf_dest,128,
                         PlaneIn[plane_number].u_width);
                        break;
                    case M4xVSS_kVideoEffectType_Pink:
                        memset((void *)p_buf_dest,255,
                         PlaneIn[plane_number].u_width);
                        break;
                    case M4xVSS_kVideoEffectType_Green:
                        memset((void *)p_buf_dest,0,
                         PlaneIn[plane_number].u_width);
                        break;
                    case M4xVSS_kVideoEffectType_Sepia:
                        if(plane_number==1)
                        {
                            memset((void *)p_buf_dest,117,
                             PlaneIn[plane_number].u_width);
                        }
                        else
                        {
                            memset((void *)p_buf_dest,139,
                             PlaneIn[plane_number].u_width);
                        }
                        break;
                    case M4xVSS_kVideoEffectType_Negative:
                        memcpy((void *)p_buf_dest,
                         (void *)p_buf_src ,PlaneOut[plane_number].u_width);
                        break;

                    case M4xVSS_kVideoEffectType_ColorRGB16:
                        {
                            M4OSA_UInt16 r = 0,g = 0,b = 0,y = 0,u = 0,v = 0;

                            /*first get the r, g, b*/
                            b = (ColorContext->rgb16ColorData &  0x001f);
                            g = (ColorContext->rgb16ColorData &  0x07e0)>>5;
                            r = (ColorContext->rgb16ColorData &  0xf800)>>11;

                            /*keep y, but replace u and v*/
                            if(plane_number==1)
                            {
                                /*then convert to u*/
                                u = U16(r, g, b);
                                memset((void *)p_buf_dest,(M4OSA_UInt8)u,
                                 PlaneIn[plane_number].u_width);
                            }
                            if(plane_number==2)
                            {
                                /*then convert to v*/
                                v = V16(r, g, b);
                                memset((void *)p_buf_dest, (M4OSA_UInt8)v,
                                 PlaneIn[plane_number].u_width);
                            }
                        }
                        break;
                    case M4xVSS_kVideoEffectType_Gradient:
                        {
                            M4OSA_UInt16 r = 0,g = 0,b = 0,y = 0,u = 0,v = 0;

                            /*first get the r, g, b*/
                            b = (ColorContext->rgb16ColorData &  0x001f);
                            g = (ColorContext->rgb16ColorData &  0x07e0)>>5;
                            r = (ColorContext->rgb16ColorData &  0xf800)>>11;

                            /*for color gradation*/
                            b = (M4OSA_UInt16)( b - ((b*i)/PlaneIn[plane_number].u_height));
                            g = (M4OSA_UInt16)(g - ((g*i)/PlaneIn[plane_number].u_height));
                            r = (M4OSA_UInt16)(r - ((r*i)/PlaneIn[plane_number].u_height));

                            /*keep y, but replace u and v*/
                            if(plane_number==1)
                            {
                                /*then convert to u*/
                                u = U16(r, g, b);
                                memset((void *)p_buf_dest,(M4OSA_UInt8)u,
                                 PlaneIn[plane_number].u_width);
                            }
                            if(plane_number==2)
                            {
                                /*then convert to v*/
                                v = V16(r, g, b);
                                memset((void *)p_buf_dest,(M4OSA_UInt8)v,
                                 PlaneIn[plane_number].u_width);
                            }
                        }
                        break;
                        default:
                        break;
                }
            }
            /**
             * Luminance */
            else
            {
                //switch ((M4OSA_UInt32)pFunctionContext)
                // commented because a structure for the effects context exist
                switch (ColorContext->colorEffectType)
                {
                case M4xVSS_kVideoEffectType_Negative:
                    for(j=0;j<PlaneOut[plane_number].u_width;j++)
                    {
                            p_buf_dest[j] = 255 - p_buf_src[j];
                    }
                    break;
                default:
                    memcpy((void *)p_buf_dest,
                     (void *)p_buf_src ,PlaneOut[plane_number].u_width);
                    break;
                }
            }
            p_buf_src += PlaneIn[plane_number].u_stride;
            p_buf_dest += PlaneOut[plane_number].u_stride;
        }
    }

    return M4VIFI_OK;
}

/**
 ******************************************************************************
 * prototype    M4VSS3GPP_externalVideoEffectFraming(M4OSA_Void *pFunctionContext,
 *                                                    M4VIFI_ImagePlane *PlaneIn,
 *                                                    M4VIFI_ImagePlane *PlaneOut,
 *                                                    M4VSS3GPP_ExternalProgress *pProgress,
 *                                                    M4OSA_UInt32 uiEffectKind)
 *
 * @brief    This function add a fixed or animated image on an input YUV420 planar frame
 * @note
 * @param    pFunctionContext(IN) Contains which color to apply (not very clean ...)
 * @param    PlaneIn            (IN) Input YUV420 planar
 * @param    PlaneOut        (IN/OUT) Output YUV420 planar
 * @param    pProgress        (IN/OUT) Progress indication (0-100)
 * @param    uiEffectKind    (IN) Unused
 *
 * @return    M4VIFI_OK:    No error
 ******************************************************************************
 */
M4OSA_ERR M4VSS3GPP_externalVideoEffectFraming( M4OSA_Void *userData,
                                                M4VIFI_ImagePlane PlaneIn[3],
                                                M4VIFI_ImagePlane *PlaneOut,
                                                M4VSS3GPP_ExternalProgress *pProgress,
                                                M4OSA_UInt32 uiEffectKind )
{
    M4VIFI_UInt32 x,y;

    M4VIFI_UInt8 *p_in_Y = PlaneIn[0].pac_data;
    M4VIFI_UInt8 *p_in_U = PlaneIn[1].pac_data;
    M4VIFI_UInt8 *p_in_V = PlaneIn[2].pac_data;

    M4xVSS_FramingStruct* Framing = M4OSA_NULL;
    M4xVSS_FramingStruct* currentFraming = M4OSA_NULL;
    M4VIFI_UInt8 *FramingRGB = M4OSA_NULL;

    M4VIFI_UInt8 *p_out0;
    M4VIFI_UInt8 *p_out1;
    M4VIFI_UInt8 *p_out2;

    M4VIFI_UInt32 topleft[2];

    M4OSA_UInt8 transparent1 = (M4OSA_UInt8)((TRANSPARENT_COLOR & 0xFF00)>>8);
    M4OSA_UInt8 transparent2 = (M4OSA_UInt8)TRANSPARENT_COLOR;

#ifndef DECODE_GIF_ON_SAVING
    Framing = (M4xVSS_FramingStruct *)userData;
    currentFraming = (M4xVSS_FramingStruct *)Framing->pCurrent;
    FramingRGB = Framing->FramingRgb->pac_data;
#endif /*DECODE_GIF_ON_SAVING*/

    /*FB*/
#ifdef DECODE_GIF_ON_SAVING
    M4OSA_ERR err;
    Framing = (M4xVSS_FramingStruct *)((M4xVSS_FramingContext*)userData)->aFramingCtx;
    currentFraming = (M4xVSS_FramingStruct *)Framing;
    FramingRGB = Framing->FramingRgb->pac_data;
#endif /*DECODE_GIF_ON_SAVING*/
    /*end FB*/

    /**
     * Initialize input / output plane pointers */
    p_in_Y += PlaneIn[0].u_topleft;
    p_in_U += PlaneIn[1].u_topleft;
    p_in_V += PlaneIn[2].u_topleft;

    p_out0 = PlaneOut[0].pac_data;
    p_out1 = PlaneOut[1].pac_data;
    p_out2 = PlaneOut[2].pac_data;

    /**
     * Depending on time, initialize Framing frame to use */
    if(Framing->previousClipTime == -1)
    {
        Framing->previousClipTime = pProgress->uiOutputTime;
    }

    /**
     * If the current clip time has reach the duration of one frame of the framing picture
     * we need to step to next framing picture */

    Framing->previousClipTime = pProgress->uiOutputTime;
    FramingRGB = currentFraming->FramingRgb->pac_data;
    topleft[0] = currentFraming->topleft_x;
    topleft[1] = currentFraming->topleft_y;

    for( x=0 ;x < PlaneIn[0].u_height ; x++)
    {
        for( y=0 ;y < PlaneIn[0].u_width ; y++)
        {
            /**
             * To handle framing with input size != output size
             * Framing is applyed if coordinates matches between framing/topleft and input plane */
            if( y < (topleft[0] + currentFraming->FramingYuv[0].u_width)  &&
                y >= topleft[0] &&
                x < (topleft[1] + currentFraming->FramingYuv[0].u_height) &&
                x >= topleft[1])
            {
                /*Alpha blending support*/
                M4OSA_Float alphaBlending = 1;
                M4xVSS_internalEffectsAlphaBlending*  alphaBlendingStruct =\
                 (M4xVSS_internalEffectsAlphaBlending*)\
                    ((M4xVSS_FramingContext*)userData)->alphaBlendingStruct;

                if(alphaBlendingStruct != M4OSA_NULL)
                {
                    if(pProgress->uiProgress \
                    < (M4OSA_UInt32)(alphaBlendingStruct->m_fadeInTime*10))
                    {
                        if(alphaBlendingStruct->m_fadeInTime == 0) {
                            alphaBlending = alphaBlendingStruct->m_start / 100;
                        } else {
                            alphaBlending = ((M4OSA_Float)(alphaBlendingStruct->m_middle\
                             - alphaBlendingStruct->m_start)\
                                *pProgress->uiProgress/(alphaBlendingStruct->m_fadeInTime*10));
                            alphaBlending += alphaBlendingStruct->m_start;
                            alphaBlending /= 100;
                        }
                    }
                    else if(pProgress->uiProgress >= (M4OSA_UInt32)(alphaBlendingStruct->\
                    m_fadeInTime*10) && pProgress->uiProgress < 1000\
                     - (M4OSA_UInt32)(alphaBlendingStruct->m_fadeOutTime*10))
                    {
                        alphaBlending = (M4OSA_Float)\
                        ((M4OSA_Float)alphaBlendingStruct->m_middle/100);
                    }
                    else if(pProgress->uiProgress >= 1000 - (M4OSA_UInt32)\
                    (alphaBlendingStruct->m_fadeOutTime*10))
                    {
                        if(alphaBlendingStruct->m_fadeOutTime == 0) {
                            alphaBlending = alphaBlendingStruct->m_end / 100;
                        } else {
                            alphaBlending = ((M4OSA_Float)(alphaBlendingStruct->m_middle \
                            - alphaBlendingStruct->m_end))*(1000 - pProgress->uiProgress)\
                            /(alphaBlendingStruct->m_fadeOutTime*10);
                            alphaBlending += alphaBlendingStruct->m_end;
                            alphaBlending /= 100;
                        }
                    }
                }
                /**/

                if((*(FramingRGB)==transparent1) && (*(FramingRGB+1)==transparent2))
                {
                    *( p_out0+y+x*PlaneOut[0].u_stride)=(*(p_in_Y+y+x*PlaneIn[0].u_stride));
                    *( p_out1+(y>>1)+(x>>1)*PlaneOut[1].u_stride)=
                        (*(p_in_U+(y>>1)+(x>>1)*PlaneIn[1].u_stride));
                    *( p_out2+(y>>1)+(x>>1)*PlaneOut[2].u_stride)=
                        (*(p_in_V+(y>>1)+(x>>1)*PlaneIn[2].u_stride));
                }
                else
                {
                    *( p_out0+y+x*PlaneOut[0].u_stride)=
                        (*(currentFraming->FramingYuv[0].pac_data+(y-topleft[0])\
                            +(x-topleft[1])*currentFraming->FramingYuv[0].u_stride))*alphaBlending;
                    *( p_out0+y+x*PlaneOut[0].u_stride)+=
                        (*(p_in_Y+y+x*PlaneIn[0].u_stride))*(1-alphaBlending);
                    *( p_out1+(y>>1)+(x>>1)*PlaneOut[1].u_stride)=
                        (*(currentFraming->FramingYuv[1].pac_data+((y-topleft[0])>>1)\
                            +((x-topleft[1])>>1)*currentFraming->FramingYuv[1].u_stride))\
                                *alphaBlending;
                    *( p_out1+(y>>1)+(x>>1)*PlaneOut[1].u_stride)+=
                        (*(p_in_U+(y>>1)+(x>>1)*PlaneIn[1].u_stride))*(1-alphaBlending);
                    *( p_out2+(y>>1)+(x>>1)*PlaneOut[2].u_stride)=
                        (*(currentFraming->FramingYuv[2].pac_data+((y-topleft[0])>>1)\
                            +((x-topleft[1])>>1)*currentFraming->FramingYuv[2].u_stride))\
                                *alphaBlending;
                    *( p_out2+(y>>1)+(x>>1)*PlaneOut[2].u_stride)+=
                        (*(p_in_V+(y>>1)+(x>>1)*PlaneIn[2].u_stride))*(1-alphaBlending);
                }
                if( PlaneIn[0].u_width < (topleft[0] + currentFraming->FramingYuv[0].u_width) &&
                    y == PlaneIn[0].u_width-1)
                {
                    FramingRGB = FramingRGB + 2 \
                        * (topleft[0] + currentFraming->FramingYuv[0].u_width \
                            - PlaneIn[0].u_width + 1);
                }
                else
                {
                    FramingRGB = FramingRGB + 2;
                }
            }
            /**
             * Just copy input plane to output plane */
            else
            {
                *( p_out0+y+x*PlaneOut[0].u_stride)=*(p_in_Y+y+x*PlaneIn[0].u_stride);
                *( p_out1+(y>>1)+(x>>1)*PlaneOut[1].u_stride)=
                    *(p_in_U+(y>>1)+(x>>1)*PlaneIn[1].u_stride);
                *( p_out2+(y>>1)+(x>>1)*PlaneOut[2].u_stride)=
                    *(p_in_V+(y>>1)+(x>>1)*PlaneIn[2].u_stride);
            }
        }
    }


    return M4VIFI_OK;
}


/**
 ******************************************************************************
 * prototype    M4VSS3GPP_externalVideoEffectFifties(M4OSA_Void *pFunctionContext,
 *                                                    M4VIFI_ImagePlane *PlaneIn,
 *                                                    M4VIFI_ImagePlane *PlaneOut,
 *                                                    M4VSS3GPP_ExternalProgress *pProgress,
 *                                                    M4OSA_UInt32 uiEffectKind)
 *
 * @brief    This function make a video look as if it was taken in the fifties
 * @note
 * @param    pUserData       (IN) Context
 * @param    pPlaneIn        (IN) Input YUV420 planar
 * @param    pPlaneOut        (IN/OUT) Output YUV420 planar
 * @param    pProgress        (IN/OUT) Progress indication (0-100)
 * @param    uiEffectKind    (IN) Unused
 *
 * @return    M4VIFI_OK:            No error
 * @return  M4ERR_PARAMETER:    pFiftiesData, pPlaneOut or pProgress are NULL (DEBUG only)
 ******************************************************************************
 */
M4OSA_ERR M4VSS3GPP_externalVideoEffectFifties( M4OSA_Void *pUserData,
                                                M4VIFI_ImagePlane *pPlaneIn,
                                                M4VIFI_ImagePlane *pPlaneOut,
                                                M4VSS3GPP_ExternalProgress *pProgress,
                                                M4OSA_UInt32 uiEffectKind )
{
    M4VIFI_UInt32 x, y, xShift;
    M4VIFI_UInt8 *pInY = pPlaneIn[0].pac_data;
    M4VIFI_UInt8 *pOutY, *pInYbegin;
    M4VIFI_UInt8 *pInCr,* pOutCr;
    M4VIFI_Int32 plane_number;

    /* Internal context*/
    M4xVSS_FiftiesStruct* p_FiftiesData = (M4xVSS_FiftiesStruct *)pUserData;

    /* Check the inputs (debug only) */
    M4OSA_DEBUG_IF2((p_FiftiesData == M4OSA_NULL),M4ERR_PARAMETER,
         "xVSS: p_FiftiesData is M4OSA_NULL in M4VSS3GPP_externalVideoEffectFifties");
    M4OSA_DEBUG_IF2((pPlaneOut == M4OSA_NULL),M4ERR_PARAMETER,
         "xVSS: p_PlaneOut is M4OSA_NULL in M4VSS3GPP_externalVideoEffectFifties");
    M4OSA_DEBUG_IF2((pProgress == M4OSA_NULL),M4ERR_PARAMETER,
        "xVSS: p_Progress is M4OSA_NULL in M4VSS3GPP_externalVideoEffectFifties");

    /* Initialize input / output plane pointers */
    pInY += pPlaneIn[0].u_topleft;
    pOutY = pPlaneOut[0].pac_data;
    pInYbegin  = pInY;

    /* Initialize the random */
    if(p_FiftiesData->previousClipTime < 0)
    {
        M4OSA_randInit();
        M4OSA_rand((M4OSA_Int32 *)&(p_FiftiesData->shiftRandomValue), (pPlaneIn[0].u_height) >> 4);
        M4OSA_rand((M4OSA_Int32 *)&(p_FiftiesData->stripeRandomValue), (pPlaneIn[0].u_width)<< 2);
        p_FiftiesData->previousClipTime = pProgress->uiOutputTime;
    }

    /* Choose random values if we have reached the duration of a partial effect */
    else if( (pProgress->uiOutputTime - p_FiftiesData->previousClipTime)\
         > p_FiftiesData->fiftiesEffectDuration)
    {
        M4OSA_rand((M4OSA_Int32 *)&(p_FiftiesData->shiftRandomValue), (pPlaneIn[0].u_height) >> 4);
        M4OSA_rand((M4OSA_Int32 *)&(p_FiftiesData->stripeRandomValue), (pPlaneIn[0].u_width)<< 2);
        p_FiftiesData->previousClipTime = pProgress->uiOutputTime;
    }

    /* Put in Sepia the chrominance */
    for (plane_number = 1; plane_number < 3; plane_number++)
    {
        pInCr  = pPlaneIn[plane_number].pac_data  + pPlaneIn[plane_number].u_topleft;
        pOutCr = pPlaneOut[plane_number].pac_data + pPlaneOut[plane_number].u_topleft;

        for (x = 0; x < pPlaneOut[plane_number].u_height; x++)
        {
            if (1 == plane_number)
                memset((void *)pOutCr, 117,pPlaneIn[plane_number].u_width); /* U value */
            else
                memset((void *)pOutCr, 139,pPlaneIn[plane_number].u_width); /* V value */

            pInCr  += pPlaneIn[plane_number].u_stride;
            pOutCr += pPlaneOut[plane_number].u_stride;
        }
    }

    /* Compute the new pixels values */
    for( x = 0 ; x < pPlaneIn[0].u_height ; x++)
    {
        M4VIFI_UInt8 *p_outYtmp, *p_inYtmp;

        /* Compute the xShift (random value) */
        if (0 == (p_FiftiesData->shiftRandomValue % 5 ))
            xShift = (x + p_FiftiesData->shiftRandomValue ) % (pPlaneIn[0].u_height - 1);
        else
            xShift = (x + (pPlaneIn[0].u_height - p_FiftiesData->shiftRandomValue) ) \
                % (pPlaneIn[0].u_height - 1);

        /* Initialize the pointers */
        p_outYtmp = pOutY + 1;                                    /* yShift of 1 pixel */
        p_inYtmp  = pInYbegin + (xShift * pPlaneIn[0].u_stride);  /* Apply the xShift */

        for( y = 0 ; y < pPlaneIn[0].u_width ; y++)
        {
            /* Set Y value */
            if (xShift > (pPlaneIn[0].u_height - 4))
                *p_outYtmp = 40;        /* Add some horizontal black lines between the
                                        two parts of the image */
            else if ( y == p_FiftiesData->stripeRandomValue)
                *p_outYtmp = 90;        /* Add a random vertical line for the bulk */
            else
                *p_outYtmp = *p_inYtmp;


            /* Go to the next pixel */
            p_outYtmp++;
            p_inYtmp++;

            /* Restart at the beginning of the line for the last pixel*/
            if (y == (pPlaneIn[0].u_width - 2))
                p_outYtmp = pOutY;
        }

        /* Go to the next line */
        pOutY += pPlaneOut[0].u_stride;
    }

    return M4VIFI_OK;
}

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_externalVideoEffectZoom( )
 * @brief    Zoom in/out video effect functions.
 * @note    The external video function is used only if VideoEffectType is set to
 * M4VSS3GPP_kVideoEffectType_ZoomIn or M4VSS3GPP_kVideoEffectType_ZoomOut.
 *
 * @param   pFunctionContext    (IN) The function context, previously set by the integrator
 * @param    pInputPlanes        (IN) Input YUV420 image: pointer to an array of three valid
 *                                    image planes (Y, U and V)
 * @param    pOutputPlanes        (IN/OUT) Output (filtered) YUV420 image: pointer to an array of
 *                                        three valid image planes (Y, U and V)
 * @param    pProgress            (IN) Set of information about the video transition progress.
 * @return    M4NO_ERROR:            No error
 * @return    M4ERR_PARAMETER:    At least one parameter is M4OSA_NULL (debug only)
 ******************************************************************************
 */

M4OSA_ERR M4VSS3GPP_externalVideoEffectZoom(
    M4OSA_Void *pFunctionContext,
    M4VIFI_ImagePlane *pInputPlanes,
    M4VIFI_ImagePlane *pOutputPlanes,
    M4VSS3GPP_ExternalProgress *pProgress,
    M4OSA_UInt32 uiEffectKind
)
{
    M4OSA_UInt32 boxWidth;
    M4OSA_UInt32 boxHeight;
    M4OSA_UInt32 boxPosX;
    M4OSA_UInt32 boxPosY;
    M4OSA_UInt32 ratio = 0;
    /*  * 1.189207 between ratio */
    /* zoom between x1 and x16 */
    M4OSA_UInt32 ratiotab[17] ={1024,1218,1448,1722,2048,2435,2896,3444,4096,4871,5793,\
                                6889,8192,9742,11585,13777,16384};
    M4OSA_UInt32 ik;

    M4VIFI_ImagePlane boxPlane[3];

    if((M4OSA_Void *)M4xVSS_kVideoEffectType_ZoomOut == pFunctionContext)
    {
        //ratio = 16 - (15 * pProgress->uiProgress)/1000;
        ratio = 16 - pProgress->uiProgress / 66 ;
    }
    else if((M4OSA_Void *)M4xVSS_kVideoEffectType_ZoomIn == pFunctionContext)
    {
        //ratio = 1 + (15 * pProgress->uiProgress)/1000;
        ratio = 1 + pProgress->uiProgress / 66 ;
    }

    for(ik=0;ik<3;ik++){

        boxPlane[ik].u_stride = pInputPlanes[ik].u_stride;
        boxPlane[ik].pac_data = pInputPlanes[ik].pac_data;

        boxHeight = ( pInputPlanes[ik].u_height << 10 ) / ratiotab[ratio];
        boxWidth = ( pInputPlanes[ik].u_width << 10 ) / ratiotab[ratio];
        boxPlane[ik].u_height = (boxHeight)&(~1);
        boxPlane[ik].u_width = (boxWidth)&(~1);

        boxPosY = (pInputPlanes[ik].u_height >> 1) - (boxPlane[ik].u_height >> 1);
        boxPosX = (pInputPlanes[ik].u_width >> 1) - (boxPlane[ik].u_width >> 1);
        boxPlane[ik].u_topleft = boxPosY * boxPlane[ik].u_stride + boxPosX;
    }

    M4VIFI_ResizeBilinearYUV420toYUV420(M4OSA_NULL, (M4VIFI_ImagePlane*)&boxPlane, pOutputPlanes);

    /**
     * Return */
    return(M4NO_ERROR);
}

/**
 ******************************************************************************
 * prototype    M4xVSS_AlphaMagic( M4OSA_Void *userData,
 *                                    M4VIFI_ImagePlane PlaneIn1[3],
 *                                    M4VIFI_ImagePlane PlaneIn2[3],
 *                                    M4VIFI_ImagePlane *PlaneOut,
 *                                    M4VSS3GPP_ExternalProgress *pProgress,
 *                                    M4OSA_UInt32 uiTransitionKind)
 *
 * @brief    This function apply a color effect on an input YUV420 planar frame
 * @note
 * @param    userData        (IN) Contains a pointer on a settings structure
 * @param    PlaneIn1        (IN) Input YUV420 planar from video 1
 * @param    PlaneIn2        (IN) Input YUV420 planar from video 2
 * @param    PlaneOut        (IN/OUT) Output YUV420 planar
 * @param    pProgress        (IN/OUT) Progress indication (0-100)
 * @param    uiTransitionKind(IN) Unused
 *
 * @return    M4VIFI_OK:    No error
 ******************************************************************************
 */
M4OSA_ERR M4xVSS_AlphaMagic( M4OSA_Void *userData, M4VIFI_ImagePlane PlaneIn1[3],
                             M4VIFI_ImagePlane PlaneIn2[3], M4VIFI_ImagePlane *PlaneOut,
                             M4VSS3GPP_ExternalProgress *pProgress, M4OSA_UInt32 uiTransitionKind)
{

    M4OSA_ERR err;

    M4xVSS_internal_AlphaMagicSettings* alphaContext;
    M4VIFI_Int32 alphaProgressLevel;

    M4VIFI_ImagePlane* planeswap;
    M4VIFI_UInt32 x,y;

    M4VIFI_UInt8 *p_out0;
    M4VIFI_UInt8 *p_out1;
    M4VIFI_UInt8 *p_out2;
    M4VIFI_UInt8 *alphaMask;
    /* "Old image" */
    M4VIFI_UInt8 *p_in1_Y;
    M4VIFI_UInt8 *p_in1_U;
    M4VIFI_UInt8 *p_in1_V;
    /* "New image" */
    M4VIFI_UInt8 *p_in2_Y;
    M4VIFI_UInt8 *p_in2_U;
    M4VIFI_UInt8 *p_in2_V;

    err = M4NO_ERROR;

    alphaContext = (M4xVSS_internal_AlphaMagicSettings*)userData;

    alphaProgressLevel = (pProgress->uiProgress * 128)/1000;

    if( alphaContext->isreverse != M4OSA_FALSE)
    {
        alphaProgressLevel = 128 - alphaProgressLevel;
        planeswap = PlaneIn1;
        PlaneIn1 = PlaneIn2;
        PlaneIn2 = planeswap;
    }

    p_out0 = PlaneOut[0].pac_data;
    p_out1 = PlaneOut[1].pac_data;
    p_out2 = PlaneOut[2].pac_data;

    alphaMask = alphaContext->pPlane->pac_data;

    /* "Old image" */
    p_in1_Y = PlaneIn1[0].pac_data;
    p_in1_U = PlaneIn1[1].pac_data;
    p_in1_V = PlaneIn1[2].pac_data;
    /* "New image" */
    p_in2_Y = PlaneIn2[0].pac_data;
    p_in2_U = PlaneIn2[1].pac_data;
    p_in2_V = PlaneIn2[2].pac_data;

     /**
     * For each column ... */
    for( y=0; y<PlaneOut->u_height; y++ )
    {
        /**
         * ... and each row of the alpha mask */
        for( x=0; x<PlaneOut->u_width; x++ )
        {
            /**
             * If the value of the current pixel of the alpha mask is > to the current time
             * ( current time is normalized on [0-255] ) */
            if( alphaProgressLevel < alphaMask[x+y*PlaneOut->u_width] )
            {
                /* We keep "old image" in output plane */
                *( p_out0+x+y*PlaneOut[0].u_stride)=*(p_in1_Y+x+y*PlaneIn1[0].u_stride);
                *( p_out1+(x>>1)+(y>>1)*PlaneOut[1].u_stride)=
                    *(p_in1_U+(x>>1)+(y>>1)*PlaneIn1[1].u_stride);
                *( p_out2+(x>>1)+(y>>1)*PlaneOut[2].u_stride)=
                    *(p_in1_V+(x>>1)+(y>>1)*PlaneIn1[2].u_stride);
            }
            else
            {
                /* We take "new image" in output plane */
                *( p_out0+x+y*PlaneOut[0].u_stride)=*(p_in2_Y+x+y*PlaneIn2[0].u_stride);
                *( p_out1+(x>>1)+(y>>1)*PlaneOut[1].u_stride)=
                    *(p_in2_U+(x>>1)+(y>>1)*PlaneIn2[1].u_stride);
                *( p_out2+(x>>1)+(y>>1)*PlaneOut[2].u_stride)=
                    *(p_in2_V+(x>>1)+(y>>1)*PlaneIn2[2].u_stride);
            }
        }
    }

    return(err);
}

/**
 ******************************************************************************
 * prototype    M4xVSS_AlphaMagicBlending( M4OSA_Void *userData,
 *                                    M4VIFI_ImagePlane PlaneIn1[3],
 *                                    M4VIFI_ImagePlane PlaneIn2[3],
 *                                    M4VIFI_ImagePlane *PlaneOut,
 *                                    M4VSS3GPP_ExternalProgress *pProgress,
 *                                    M4OSA_UInt32 uiTransitionKind)
 *
 * @brief    This function apply a color effect on an input YUV420 planar frame
 * @note
 * @param    userData        (IN) Contains a pointer on a settings structure
 * @param    PlaneIn1        (IN) Input YUV420 planar from video 1
 * @param    PlaneIn2        (IN) Input YUV420 planar from video 2
 * @param    PlaneOut        (IN/OUT) Output YUV420 planar
 * @param    pProgress        (IN/OUT) Progress indication (0-100)
 * @param    uiTransitionKind(IN) Unused
 *
 * @return    M4VIFI_OK:    No error
 ******************************************************************************
 */
M4OSA_ERR M4xVSS_AlphaMagicBlending( M4OSA_Void *userData, M4VIFI_ImagePlane PlaneIn1[3],
                                     M4VIFI_ImagePlane PlaneIn2[3], M4VIFI_ImagePlane *PlaneOut,
                                     M4VSS3GPP_ExternalProgress *pProgress,
                                     M4OSA_UInt32 uiTransitionKind)
{
    M4OSA_ERR err;

    M4xVSS_internal_AlphaMagicSettings* alphaContext;
    M4VIFI_Int32 alphaProgressLevel;
    M4VIFI_Int32 alphaBlendLevelMin;
    M4VIFI_Int32 alphaBlendLevelMax;
    M4VIFI_Int32 alphaBlendRange;

    M4VIFI_ImagePlane* planeswap;
    M4VIFI_UInt32 x,y;
    M4VIFI_Int32 alphaMaskValue;

    M4VIFI_UInt8 *p_out0;
    M4VIFI_UInt8 *p_out1;
    M4VIFI_UInt8 *p_out2;
    M4VIFI_UInt8 *alphaMask;
    /* "Old image" */
    M4VIFI_UInt8 *p_in1_Y;
    M4VIFI_UInt8 *p_in1_U;
    M4VIFI_UInt8 *p_in1_V;
    /* "New image" */
    M4VIFI_UInt8 *p_in2_Y;
    M4VIFI_UInt8 *p_in2_U;
    M4VIFI_UInt8 *p_in2_V;


    err = M4NO_ERROR;

    alphaContext = (M4xVSS_internal_AlphaMagicSettings*)userData;

    alphaProgressLevel = (pProgress->uiProgress * 128)/1000;

    if( alphaContext->isreverse != M4OSA_FALSE)
    {
        alphaProgressLevel = 128 - alphaProgressLevel;
        planeswap = PlaneIn1;
        PlaneIn1 = PlaneIn2;
        PlaneIn2 = planeswap;
    }

    alphaBlendLevelMin = alphaProgressLevel-alphaContext->blendingthreshold;

    alphaBlendLevelMax = alphaProgressLevel+alphaContext->blendingthreshold;

    alphaBlendRange = (alphaContext->blendingthreshold)*2;

    p_out0 = PlaneOut[0].pac_data;
    p_out1 = PlaneOut[1].pac_data;
    p_out2 = PlaneOut[2].pac_data;

    alphaMask = alphaContext->pPlane->pac_data;

    /* "Old image" */
    p_in1_Y = PlaneIn1[0].pac_data;
    p_in1_U = PlaneIn1[1].pac_data;
    p_in1_V = PlaneIn1[2].pac_data;
    /* "New image" */
    p_in2_Y = PlaneIn2[0].pac_data;
    p_in2_U = PlaneIn2[1].pac_data;
    p_in2_V = PlaneIn2[2].pac_data;

    /* apply Alpha Magic on each pixel */
       for( y=0; y<PlaneOut->u_height; y++ )
    {
        for( x=0; x<PlaneOut->u_width; x++ )
        {
            alphaMaskValue = alphaMask[x+y*PlaneOut->u_width];
            if( alphaBlendLevelMax < alphaMaskValue )
            {
                /* We keep "old image" in output plane */
                *( p_out0+x+y*PlaneOut[0].u_stride)=*(p_in1_Y+x+y*PlaneIn1[0].u_stride);
                *( p_out1+(x>>1)+(y>>1)*PlaneOut[1].u_stride)=
                    *(p_in1_U+(x>>1)+(y>>1)*PlaneIn1[1].u_stride);
                *( p_out2+(x>>1)+(y>>1)*PlaneOut[2].u_stride)=
                    *(p_in1_V+(x>>1)+(y>>1)*PlaneIn1[2].u_stride);
            }
            else if( (alphaBlendLevelMin < alphaMaskValue)&&
                    (alphaMaskValue <= alphaBlendLevelMax ) )
            {
                /* We blend "old and new image" in output plane */
                *( p_out0+x+y*PlaneOut[0].u_stride)=(M4VIFI_UInt8)
                    (( (alphaMaskValue-alphaBlendLevelMin)*( *(p_in1_Y+x+y*PlaneIn1[0].u_stride))
                        +(alphaBlendLevelMax-alphaMaskValue)\
                            *( *(p_in2_Y+x+y*PlaneIn2[0].u_stride)) )/alphaBlendRange );

                *( p_out1+(x>>1)+(y>>1)*PlaneOut[1].u_stride)=(M4VIFI_UInt8)\
                    (( (alphaMaskValue-alphaBlendLevelMin)*( *(p_in1_U+(x>>1)+(y>>1)\
                        *PlaneIn1[1].u_stride))
                            +(alphaBlendLevelMax-alphaMaskValue)*( *(p_in2_U+(x>>1)+(y>>1)\
                                *PlaneIn2[1].u_stride)) )/alphaBlendRange );

                *( p_out2+(x>>1)+(y>>1)*PlaneOut[2].u_stride)=
                    (M4VIFI_UInt8)(( (alphaMaskValue-alphaBlendLevelMin)\
                        *( *(p_in1_V+(x>>1)+(y>>1)*PlaneIn1[2].u_stride))
                                +(alphaBlendLevelMax-alphaMaskValue)*( *(p_in2_V+(x>>1)+(y>>1)\
                                    *PlaneIn2[2].u_stride)) )/alphaBlendRange );

            }
            else
            {
                /* We take "new image" in output plane */
                *( p_out0+x+y*PlaneOut[0].u_stride)=*(p_in2_Y+x+y*PlaneIn2[0].u_stride);
                *( p_out1+(x>>1)+(y>>1)*PlaneOut[1].u_stride)=
                    *(p_in2_U+(x>>1)+(y>>1)*PlaneIn2[1].u_stride);
                *( p_out2+(x>>1)+(y>>1)*PlaneOut[2].u_stride)=
                    *(p_in2_V+(x>>1)+(y>>1)*PlaneIn2[2].u_stride);
            }
        }
    }

    return(err);
}

#define M4XXX_SampleAddress(plane, x, y)  ( (plane).pac_data + (plane).u_topleft + (y)\
     * (plane).u_stride + (x) )

static void M4XXX_CopyPlane(M4VIFI_ImagePlane* dest, M4VIFI_ImagePlane* source)
{
    M4OSA_UInt32    height, width, sourceStride, destStride, y;
    M4OSA_MemAddr8    sourceWalk, destWalk;

    /* cache the vars used in the loop so as to avoid them being repeatedly fetched and
     recomputed from memory. */
    height = dest->u_height;
    width = dest->u_width;

    sourceWalk = (M4OSA_MemAddr8)M4XXX_SampleAddress(*source, 0, 0);
    sourceStride = source->u_stride;

    destWalk = (M4OSA_MemAddr8)M4XXX_SampleAddress(*dest, 0, 0);
    destStride = dest->u_stride;

    for (y=0; y<height; y++)
    {
        memcpy((void *)destWalk, (void *)sourceWalk, width);
        destWalk += destStride;
        sourceWalk += sourceStride;
    }
}

static M4OSA_ERR M4xVSS_VerticalSlideTransition(M4VIFI_ImagePlane* topPlane,
                                                M4VIFI_ImagePlane* bottomPlane,
                                                M4VIFI_ImagePlane *PlaneOut,
                                                M4OSA_UInt32    shiftUV)
{
    M4OSA_UInt32 i;

    /* Do three loops, one for each plane type, in order to avoid having too many buffers
    "hot" at the same time (better for cache). */
    for (i=0; i<3; i++)
    {
        M4OSA_UInt32    topPartHeight, bottomPartHeight, width, sourceStride, destStride, y;
        M4OSA_MemAddr8    sourceWalk, destWalk;

        /* cache the vars used in the loop so as to avoid them being repeatedly fetched and
         recomputed from memory. */
        if (0 == i) /* Y plane */
        {
            bottomPartHeight = 2*shiftUV;
        }
        else /* U and V planes */
        {
            bottomPartHeight = shiftUV;
        }
        topPartHeight = PlaneOut[i].u_height - bottomPartHeight;
        width = PlaneOut[i].u_width;

        sourceWalk = (M4OSA_MemAddr8)M4XXX_SampleAddress(topPlane[i], 0, bottomPartHeight);
        sourceStride = topPlane[i].u_stride;

        destWalk = (M4OSA_MemAddr8)M4XXX_SampleAddress(PlaneOut[i], 0, 0);
        destStride = PlaneOut[i].u_stride;

        /* First the part from the top source clip frame. */
        for (y=0; y<topPartHeight; y++)
        {
            memcpy((void *)destWalk, (void *)sourceWalk, width);
            destWalk += destStride;
            sourceWalk += sourceStride;
        }

        /* and now change the vars to copy the part from the bottom source clip frame. */
        sourceWalk = (M4OSA_MemAddr8)M4XXX_SampleAddress(bottomPlane[i], 0, 0);
        sourceStride = bottomPlane[i].u_stride;

        /* destWalk is already at M4XXX_SampleAddress(PlaneOut[i], 0, topPartHeight) */

        for (y=0; y<bottomPartHeight; y++)
        {
            memcpy((void *)destWalk, (void *)sourceWalk, width);
            destWalk += destStride;
            sourceWalk += sourceStride;
        }
    }
    return M4NO_ERROR;
}

static M4OSA_ERR M4xVSS_HorizontalSlideTransition(M4VIFI_ImagePlane* leftPlane,
                                                  M4VIFI_ImagePlane* rightPlane,
                                                  M4VIFI_ImagePlane *PlaneOut,
                                                  M4OSA_UInt32    shiftUV)
{
    M4OSA_UInt32 i, y;
    /* If we shifted by exactly 0, or by the width of the target image, then we would get the left
    frame or the right frame, respectively. These cases aren't handled too well by the general
    handling, since they result in 0-size memcopies, so might as well particularize them. */

    if (0 == shiftUV)    /* output left frame */
    {
        for (i = 0; i<3; i++) /* for each YUV plane */
        {
            M4XXX_CopyPlane(&(PlaneOut[i]), &(leftPlane[i]));
        }

        return M4NO_ERROR;
    }

    if (PlaneOut[1].u_width == shiftUV) /* output right frame */
    {
        for (i = 0; i<3; i++) /* for each YUV plane */
        {
            M4XXX_CopyPlane(&(PlaneOut[i]), &(rightPlane[i]));
        }

        return M4NO_ERROR;
    }


    /* Do three loops, one for each plane type, in order to avoid having too many buffers
    "hot" at the same time (better for cache). */
    for (i=0; i<3; i++)
    {
        M4OSA_UInt32    height, leftPartWidth, rightPartWidth;
        M4OSA_UInt32    leftStride,    rightStride,    destStride;
        M4OSA_MemAddr8    leftWalk,    rightWalk,    destWalkLeft, destWalkRight;

        /* cache the vars used in the loop so as to avoid them being repeatedly fetched
        and recomputed from memory. */
        height = PlaneOut[i].u_height;

        if (0 == i) /* Y plane */
        {
            rightPartWidth = 2*shiftUV;
        }
        else /* U and V planes */
        {
            rightPartWidth = shiftUV;
        }
        leftPartWidth = PlaneOut[i].u_width - rightPartWidth;

        leftWalk = (M4OSA_MemAddr8)M4XXX_SampleAddress(leftPlane[i], rightPartWidth, 0);
        leftStride = leftPlane[i].u_stride;

        rightWalk = (M4OSA_MemAddr8)M4XXX_SampleAddress(rightPlane[i], 0, 0);
        rightStride = rightPlane[i].u_stride;

        destWalkLeft = (M4OSA_MemAddr8)M4XXX_SampleAddress(PlaneOut[i], 0, 0);
        destWalkRight = (M4OSA_MemAddr8)M4XXX_SampleAddress(PlaneOut[i], leftPartWidth, 0);
        destStride = PlaneOut[i].u_stride;

        for (y=0; y<height; y++)
        {
            memcpy((void *)destWalkLeft, (void *)leftWalk, leftPartWidth);
            leftWalk += leftStride;

            memcpy((void *)destWalkRight, (void *)rightWalk, rightPartWidth);
            rightWalk += rightStride;

            destWalkLeft += destStride;
            destWalkRight += destStride;
        }
    }

    return M4NO_ERROR;
}


M4OSA_ERR M4xVSS_SlideTransition( M4OSA_Void *userData, M4VIFI_ImagePlane PlaneIn1[3],
                                  M4VIFI_ImagePlane PlaneIn2[3], M4VIFI_ImagePlane *PlaneOut,
                                  M4VSS3GPP_ExternalProgress *pProgress,
                                  M4OSA_UInt32 uiTransitionKind)
{
    M4xVSS_internal_SlideTransitionSettings* settings =
         (M4xVSS_internal_SlideTransitionSettings*)userData;
    M4OSA_UInt32    shiftUV;

    M4OSA_TRACE1_0("inside M4xVSS_SlideTransition");
    if ((M4xVSS_SlideTransition_RightOutLeftIn == settings->direction)
        || (M4xVSS_SlideTransition_LeftOutRightIn == settings->direction) )
    {
        /* horizontal slide */
        shiftUV = ((PlaneOut[1]).u_width * pProgress->uiProgress)/1000;
        M4OSA_TRACE1_2("M4xVSS_SlideTransition upper: shiftUV = %d,progress = %d",
            shiftUV,pProgress->uiProgress );
        if (M4xVSS_SlideTransition_RightOutLeftIn == settings->direction)
        {
            /* Put the previous clip frame right, the next clip frame left, and reverse shiftUV
            (since it's a shift from the left frame) so that we start out on the right
            (i.e. not left) frame, it
            being from the previous clip. */
            return M4xVSS_HorizontalSlideTransition(PlaneIn2, PlaneIn1, PlaneOut,
                 (PlaneOut[1]).u_width - shiftUV);
        }
        else /* Left out, right in*/
        {
            return M4xVSS_HorizontalSlideTransition(PlaneIn1, PlaneIn2, PlaneOut, shiftUV);
        }
    }
    else
    {
        /* vertical slide */
        shiftUV = ((PlaneOut[1]).u_height * pProgress->uiProgress)/1000;
        M4OSA_TRACE1_2("M4xVSS_SlideTransition bottom: shiftUV = %d,progress = %d",shiftUV,
            pProgress->uiProgress );
        if (M4xVSS_SlideTransition_TopOutBottomIn == settings->direction)
        {
            /* Put the previous clip frame top, the next clip frame bottom. */
            return M4xVSS_VerticalSlideTransition(PlaneIn1, PlaneIn2, PlaneOut, shiftUV);
        }
        else /* Bottom out, top in */
        {
            return M4xVSS_VerticalSlideTransition(PlaneIn2, PlaneIn1, PlaneOut,
                (PlaneOut[1]).u_height - shiftUV);
        }
    }

    /* Note: it might be worthwhile to do some parameter checking, see if dimensions match, etc.,
    at least in debug mode. */
}


/**
 ******************************************************************************
 * prototype    M4xVSS_FadeBlackTransition(M4OSA_Void *pFunctionContext,
 *                                                    M4VIFI_ImagePlane *PlaneIn,
 *                                                    M4VIFI_ImagePlane *PlaneOut,
 *                                                    M4VSS3GPP_ExternalProgress *pProgress,
 *                                                    M4OSA_UInt32 uiEffectKind)
 *
 * @brief    This function apply a fade to black and then a fade from black
 * @note
 * @param    pFunctionContext(IN) Contains which color to apply (not very clean ...)
 * @param    PlaneIn            (IN) Input YUV420 planar
 * @param    PlaneOut        (IN/OUT) Output YUV420 planar
 * @param    pProgress        (IN/OUT) Progress indication (0-100)
 * @param    uiEffectKind    (IN) Unused
 *
 * @return    M4VIFI_OK:    No error
 ******************************************************************************
 */
M4OSA_ERR M4xVSS_FadeBlackTransition(M4OSA_Void *userData, M4VIFI_ImagePlane PlaneIn1[3],
                                     M4VIFI_ImagePlane PlaneIn2[3],
                                     M4VIFI_ImagePlane *PlaneOut,
                                     M4VSS3GPP_ExternalProgress *pProgress,
                                     M4OSA_UInt32 uiTransitionKind)
{
    M4OSA_Int32 tmp = 0;
    M4OSA_ERR err = M4NO_ERROR;


    if((pProgress->uiProgress) < 500)
    {
        /**
         * Compute where we are in the effect (scale is 0->1024) */
        tmp = (M4OSA_Int32)((1.0 - ((M4OSA_Float)(pProgress->uiProgress*2)/1000)) * 1024 );

        /**
         * Apply the darkening effect */
        err = M4VFL_modifyLumaWithScale( (M4ViComImagePlane*)PlaneIn1,
             (M4ViComImagePlane*)PlaneOut, tmp, M4OSA_NULL);
        if (M4NO_ERROR != err)
        {
            M4OSA_TRACE1_1("M4xVSS_FadeBlackTransition: M4VFL_modifyLumaWithScale returns\
                 error 0x%x, returning M4VSS3GPP_ERR_LUMA_FILTER_ERROR", err);
            return M4VSS3GPP_ERR_LUMA_FILTER_ERROR;
        }
    }
    else
    {
        /**
         * Compute where we are in the effect (scale is 0->1024). */
        tmp = (M4OSA_Int32)( (((M4OSA_Float)(((pProgress->uiProgress-500)*2))/1000)) * 1024 );

        /**
         * Apply the darkening effect */
        err = M4VFL_modifyLumaWithScale((M4ViComImagePlane*)PlaneIn2,
             (M4ViComImagePlane*)PlaneOut, tmp, M4OSA_NULL);
        if (M4NO_ERROR != err)
        {
            M4OSA_TRACE1_1("M4xVSS_FadeBlackTransition:\
                 M4VFL_modifyLumaWithScale returns error 0x%x,\
                     returning M4VSS3GPP_ERR_LUMA_FILTER_ERROR", err);
            return M4VSS3GPP_ERR_LUMA_FILTER_ERROR;
        }
    }


    return M4VIFI_OK;
}


/**
 ******************************************************************************
 * prototype    M4OSA_ERR M4xVSS_internalConvertToUTF8(M4OSA_Context pContext,
 *                                                        M4OSA_Void* pBufferIn,
 *                                                        M4OSA_Void* pBufferOut,
 *                                                        M4OSA_UInt32* convertedSize)
 *
 * @brief    This function convert from the customer format to UTF8
 * @note
 * @param    pContext        (IN)    The integrator own context
 * @param    pBufferIn        (IN)    Buffer to convert
 * @param    pBufferOut        (OUT)    Converted buffer
 * @param    convertedSize    (OUT)    Size of the converted buffer
 *
 * @return    M4NO_ERROR:    No error
 * @return    M4ERR_PARAMETER: At least one of the function parameters is null
 ******************************************************************************
 */
M4OSA_ERR M4xVSS_internalConvertToUTF8(M4OSA_Context pContext, M4OSA_Void* pBufferIn,
                                       M4OSA_Void* pBufferOut, M4OSA_UInt32* convertedSize)
{
    M4xVSS_Context* xVSS_context = (M4xVSS_Context*)pContext;
    M4OSA_ERR err;

    pBufferOut = pBufferIn;
    if(xVSS_context->UTFConversionContext.pConvToUTF8Fct != M4OSA_NULL
        && xVSS_context->UTFConversionContext.pTempOutConversionBuffer != M4OSA_NULL)
    {
        M4OSA_UInt32 ConvertedSize = xVSS_context->UTFConversionContext.m_TempOutConversionSize;

        memset((void *)xVSS_context->UTFConversionContext.pTempOutConversionBuffer,0
            ,(M4OSA_UInt32)xVSS_context->UTFConversionContext.m_TempOutConversionSize);

        err = xVSS_context->UTFConversionContext.pConvToUTF8Fct((M4OSA_Void*)pBufferIn,
            (M4OSA_UInt8*)xVSS_context->UTFConversionContext.pTempOutConversionBuffer,
                 (M4OSA_UInt32*)&ConvertedSize);
        if(err == M4xVSSWAR_BUFFER_OUT_TOO_SMALL)
        {
            M4OSA_TRACE2_1("M4xVSS_internalConvertToUTF8: pConvToUTF8Fct return 0x%x",err);

            /*free too small buffer*/
            free(xVSS_context->\
                UTFConversionContext.pTempOutConversionBuffer);

            /*re-allocate the buffer*/
            xVSS_context->UTFConversionContext.pTempOutConversionBuffer    =
                 (M4OSA_Void*)M4OSA_32bitAlignedMalloc(ConvertedSize*sizeof(M4OSA_UInt8), M4VA,
                     (M4OSA_Char *)"M4xVSS_internalConvertToUTF8: UTF conversion buffer");
            if(M4OSA_NULL == xVSS_context->UTFConversionContext.pTempOutConversionBuffer)
            {
                M4OSA_TRACE1_0("Allocation error in M4xVSS_internalConvertToUTF8");
                return M4ERR_ALLOC;
            }
            xVSS_context->UTFConversionContext.m_TempOutConversionSize = ConvertedSize;

            memset((void *)xVSS_context->\
                UTFConversionContext.pTempOutConversionBuffer,0,(M4OSA_UInt32)xVSS_context->\
                    UTFConversionContext.m_TempOutConversionSize);

            err = xVSS_context->UTFConversionContext.pConvToUTF8Fct((M4OSA_Void*)pBufferIn,
                (M4OSA_Void*)xVSS_context->UTFConversionContext.pTempOutConversionBuffer,
                    (M4OSA_UInt32*)&ConvertedSize);
            if(err != M4NO_ERROR)
            {
                M4OSA_TRACE1_1("M4xVSS_internalConvertToUTF8: pConvToUTF8Fct return 0x%x",err);
                return err;
            }
        }
        else if(err != M4NO_ERROR)
        {
            M4OSA_TRACE1_1("M4xVSS_internalConvertToUTF8: pConvToUTF8Fct return 0x%x",err);
            return err;
        }
        /*decoded path*/
        pBufferOut = xVSS_context->UTFConversionContext.pTempOutConversionBuffer;
        (*convertedSize) = ConvertedSize;
    }
    return M4NO_ERROR;
}


/**
 ******************************************************************************
 * prototype    M4OSA_ERR M4xVSS_internalConvertFromUTF8(M4OSA_Context pContext)
 *
 * @brief    This function convert from UTF8 to the customer format
 * @note
 * @param    pContext    (IN) The integrator own context
 * @param    pBufferIn        (IN)    Buffer to convert
 * @param    pBufferOut        (OUT)    Converted buffer
 * @param    convertedSize    (OUT)    Size of the converted buffer
 *
 * @return    M4NO_ERROR:    No error
 * @return    M4ERR_PARAMETER: At least one of the function parameters is null
 ******************************************************************************
 */
M4OSA_ERR M4xVSS_internalConvertFromUTF8(M4OSA_Context pContext, M4OSA_Void* pBufferIn,
                                        M4OSA_Void* pBufferOut, M4OSA_UInt32* convertedSize)
{
    M4xVSS_Context* xVSS_context = (M4xVSS_Context*)pContext;
    M4OSA_ERR err;

    pBufferOut = pBufferIn;
    if(xVSS_context->UTFConversionContext.pConvFromUTF8Fct != M4OSA_NULL
        && xVSS_context->UTFConversionContext.pTempOutConversionBuffer != M4OSA_NULL)
    {
        M4OSA_UInt32 ConvertedSize = xVSS_context->UTFConversionContext.m_TempOutConversionSize;

        memset((void *)xVSS_context->\
            UTFConversionContext.pTempOutConversionBuffer,0,(M4OSA_UInt32)xVSS_context->\
                UTFConversionContext.m_TempOutConversionSize);

        err = xVSS_context->UTFConversionContext.pConvFromUTF8Fct\
            ((M4OSA_Void*)pBufferIn,(M4OSA_UInt8*)xVSS_context->\
                UTFConversionContext.pTempOutConversionBuffer, (M4OSA_UInt32*)&ConvertedSize);
        if(err == M4xVSSWAR_BUFFER_OUT_TOO_SMALL)
        {
            M4OSA_TRACE2_1("M4xVSS_internalConvertFromUTF8: pConvFromUTF8Fct return 0x%x",err);

            /*free too small buffer*/
            free(xVSS_context->\
                UTFConversionContext.pTempOutConversionBuffer);

            /*re-allocate the buffer*/
            xVSS_context->UTFConversionContext.pTempOutConversionBuffer    =
                (M4OSA_Void*)M4OSA_32bitAlignedMalloc(ConvertedSize*sizeof(M4OSA_UInt8), M4VA,
                     (M4OSA_Char *)"M4xVSS_internalConvertFromUTF8: UTF conversion buffer");
            if(M4OSA_NULL == xVSS_context->UTFConversionContext.pTempOutConversionBuffer)
            {
                M4OSA_TRACE1_0("Allocation error in M4xVSS_internalConvertFromUTF8");
                return M4ERR_ALLOC;
            }
            xVSS_context->UTFConversionContext.m_TempOutConversionSize = ConvertedSize;

            memset((void *)xVSS_context->\
                UTFConversionContext.pTempOutConversionBuffer,0,(M4OSA_UInt32)xVSS_context->\
                    UTFConversionContext.m_TempOutConversionSize);

            err = xVSS_context->UTFConversionContext.pConvFromUTF8Fct((M4OSA_Void*)pBufferIn,
                (M4OSA_Void*)xVSS_context->UTFConversionContext.pTempOutConversionBuffer,
                     (M4OSA_UInt32*)&ConvertedSize);
            if(err != M4NO_ERROR)
            {
                M4OSA_TRACE1_1("M4xVSS_internalConvertFromUTF8: pConvFromUTF8Fct return 0x%x",err);
                return err;
            }
        }
        else if(err != M4NO_ERROR)
        {
            M4OSA_TRACE1_1("M4xVSS_internalConvertFromUTF8: pConvFromUTF8Fct return 0x%x",err);
            return err;
        }
        /*decoded path*/
        pBufferOut = xVSS_context->UTFConversionContext.pTempOutConversionBuffer;
        (*convertedSize) = ConvertedSize;
    }


    return M4NO_ERROR;
}