summaryrefslogtreecommitdiffstats
path: root/libvideoeditor/vss/src/M4VSS3GPP_AudioMixing.c
blob: bf0bc06eaba895b04dea085d888015d69fb288d1 (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
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 ******************************************************************************
 * @file    M4VSS3GPP_AudioMixing.c
 * @brief    Video Studio Service 3GPP audio mixing implementation.
 * @note
 ******************************************************************************
 */

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

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

/* Put the definition of silence frames here */
#define M4VSS3GPP_SILENCE_FRAMES
#include "M4VSS3GPP_InternalConfig.h"

/**
 * OSAL headers */
#include "M4OSA_Memory.h" /**< OSAL memory management */
#include "M4OSA_Debug.h"  /**< OSAL debug management */


#include "VideoEditorResampler.h"
/**
 ******************************************************************************
 * @brief    Static functions
 ******************************************************************************
 */
static M4OSA_ERR
M4VSS3GPP_intAudioMixingOpen( M4VSS3GPP_InternalAudioMixingContext *pC,
                             M4VSS3GPP_AudioMixingSettings *pSettings );
static M4OSA_ERR M4VSS3GPP_intAudioMixingStepVideo(
    M4VSS3GPP_InternalAudioMixingContext *pC );
static M4OSA_ERR M4VSS3GPP_intAudioMixingStepAudioMix(
    M4VSS3GPP_InternalAudioMixingContext *pC );
static M4OSA_ERR M4VSS3GPP_intAudioMixingStepAudioReplace(
    M4VSS3GPP_InternalAudioMixingContext *pC );
static M4OSA_ERR M4VSS3GPP_intAudioMixingCopyOrig(
    M4VSS3GPP_InternalAudioMixingContext *pC );
static M4OSA_ERR M4VSS3GPP_intAudioMixingCopyAdded(
    M4VSS3GPP_InternalAudioMixingContext *pC );
static M4OSA_ERR M4VSS3GPP_intAudioMixingConvert(
    M4VSS3GPP_InternalAudioMixingContext *pC );
static M4OSA_ERR M4VSS3GPP_intAudioMixingDoMixing(
    M4VSS3GPP_InternalAudioMixingContext *pC );
static M4OSA_ERR M4VSS3GPP_intAudioMixingWriteSilence(
    M4VSS3GPP_InternalAudioMixingContext *pC );
static M4OSA_ERR M4VSS3GPP_intAudioMixingTransition(
    M4VSS3GPP_InternalAudioMixingContext *pC );
static M4OSA_ERR M4VSS3GPP_intAudioMixingCreateVideoEncoder(
    M4VSS3GPP_InternalAudioMixingContext *pC );
static M4OSA_ERR M4VSS3GPP_intAudioMixingDestroyVideoEncoder(
    M4VSS3GPP_InternalAudioMixingContext *pC );
static M4OSA_Bool M4VSS3GPP_isThresholdBreached( M4OSA_Int32 *averageValue,
                                                M4OSA_Int32 storeCount,
                                                M4OSA_Int32 thresholdValue );
/**
 *    Internal warning */
#define M4VSS3GPP_WAR_END_OF_ADDED_AUDIO    M4OSA_ERR_CREATE( M4_WAR, M4VSS3GPP, 0x0030)

/* A define used with SSRC 1.04 and above to avoid taking
blocks smaller that the minimal block size */
#define M4VSS_SSRC_MINBLOCKSIZE        600

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_audioMixingInit(M4VSS3GPP_AudioMixingContext* pContext,
 *                                     M4VSS3GPP_AudioMixingSettings* pSettings)
 * @brief    Initializes the VSS audio mixing operation (allocates an execution context).
 * @note
 * @param    pContext        (OUT) Pointer on the VSS audio mixing context to allocate
 * @param    pSettings        (IN) Pointer to valid audio mixing settings
 * @param    pFileReadPtrFct        (IN) Pointer to OSAL file reader functions
 * @param   pFileWritePtrFct    (IN) Pointer to OSAL file writer functions
 * @return    M4NO_ERROR:            No error
 * @return    M4ERR_PARAMETER:    At least one parameter is M4OSA_NULL (debug only)
 * @return    M4ERR_ALLOC:        There is no more available memory
 ******************************************************************************
 */

M4OSA_ERR M4VSS3GPP_audioMixingInit( M4VSS3GPP_AudioMixingContext *pContext,
                                    M4VSS3GPP_AudioMixingSettings *pSettings,
                                    M4OSA_FileReadPointer *pFileReadPtrFct,
                                    M4OSA_FileWriterPointer *pFileWritePtrFct )
{
    M4VSS3GPP_InternalAudioMixingContext *pC;
    M4OSA_ERR err;

    M4OSA_TRACE3_2(
        "M4VSS3GPP_audioMixingInit called with pContext=0x%x, pSettings=0x%x",
        pContext, pSettings);

    /**
    * Check input parameters */
    M4OSA_DEBUG_IF2((M4OSA_NULL == pContext), M4ERR_PARAMETER,
        "M4VSS3GPP_audioMixingInit: pContext is M4OSA_NULL");
    M4OSA_DEBUG_IF2((M4OSA_NULL == pSettings), M4ERR_PARAMETER,
        "M4VSS3GPP_audioMixingInit: pSettings is M4OSA_NULL");
    M4OSA_DEBUG_IF2((M4OSA_NULL == pFileReadPtrFct), M4ERR_PARAMETER,
        "M4VSS3GPP_audioMixingInit: pFileReadPtrFct is M4OSA_NULL");
    M4OSA_DEBUG_IF2((M4OSA_NULL == pFileWritePtrFct), M4ERR_PARAMETER,
        "M4VSS3GPP_audioMixingInit: pFileWritePtrFct is M4OSA_NULL");

    if( pSettings->uiBeginLoop > pSettings->uiEndLoop )
    {
        M4OSA_TRACE1_0(
            "M4VSS3GPP_audioMixingInit: Begin loop time is higher than end loop time!");
        return M4VSS3GPP_ERR_BEGINLOOP_HIGHER_ENDLOOP;
    }

    /**
    * Allocate the VSS audio mixing context and return it to the user */
    pC = (M4VSS3GPP_InternalAudioMixingContext
        *)M4OSA_32bitAlignedMalloc(sizeof(M4VSS3GPP_InternalAudioMixingContext),
        M4VSS3GPP,(M4OSA_Char *)"M4VSS3GPP_InternalAudioMixingContext");
    *pContext = pC;

    if( M4OSA_NULL == pC )
    {
        M4OSA_TRACE1_0(
            "M4VSS3GPP_audioMixingInit(): unable to allocate \
            M4VSS3GPP_InternalAudioMixingContext,returning M4ERR_ALLOC");
        return M4ERR_ALLOC;
    }

    /* Initialization of context Variables */
    memset((void *)pC ,0,
                 sizeof(M4VSS3GPP_InternalAudioMixingContext));
    /**
    * Copy this setting in context */
    pC->iAddCts = pSettings->uiAddCts;
    pC->bRemoveOriginal = pSettings->bRemoveOriginal;
    pC->b_DuckingNeedeed = pSettings->b_DuckingNeedeed;
    pC->InDucking_threshold = pSettings->InDucking_threshold;
    pC->fBTVolLevel = pSettings->fBTVolLevel;
    pC->fPTVolLevel = pSettings->fPTVolLevel;
    pC->InDucking_lowVolume = pSettings->InDucking_lowVolume;
    pC->bDoDucking = M4OSA_FALSE;
    pC->bLoop = pSettings->bLoop;
    pC->bNoLooping = M4OSA_FALSE;
    pC->bjumpflag = M4OSA_TRUE;
    /**
    * Init some context variables */

    pC->pInputClipCtxt = M4OSA_NULL;
    pC->pAddedClipCtxt = M4OSA_NULL;
    pC->fOrigFactor = 1.0F;
    pC->fAddedFactor = 0.0F;
    pC->bSupportSilence = M4OSA_FALSE;
    pC->bHasAudio = M4OSA_FALSE;
    pC->bAudioMixingIsNeeded = M4OSA_FALSE;

    /* Init PC->ewc members */
    // Decorrelate input and output encoding timestamp to handle encoder prefetch
    pC->ewc.VideoStreamType = M4SYS_kVideoUnknown;
    pC->ewc.bVideoDataPartitioning = M4OSA_FALSE;
    pC->ewc.pVideoOutputDsi = M4OSA_NULL;
    pC->ewc.AudioStreamType = M4SYS_kAudioUnknown;
    pC->ewc.uiNbChannels = 1;
    pC->ewc.pAudioOutputDsi = M4OSA_NULL;
    pC->ewc.pAudioEncCtxt = M4OSA_NULL;
    pC->ewc.pAudioEncDSI.pInfo = M4OSA_NULL;
    pC->ewc.pSilenceFrameData = M4OSA_NULL;
    pC->ewc.pEncContext = M4OSA_NULL;
    pC->ewc.pDummyAuBuffer = M4OSA_NULL;
    pC->ewc.p3gpWriterContext = M4OSA_NULL;
    pC->pLVAudioResampler = M4OSA_NULL;
    /**
    * Set the OSAL filesystem function set */
    pC->pOsaFileReadPtr = pFileReadPtrFct;
    pC->pOsaFileWritPtr = pFileWritePtrFct;

    /**
    * Ssrc stuff */
    pC->b_SSRCneeded = M4OSA_FALSE;
    pC->pSsrcBufferIn = M4OSA_NULL;
    pC->pSsrcBufferOut = M4OSA_NULL;
    pC->pTempBuffer = M4OSA_NULL;
    pC->pPosInTempBuffer = M4OSA_NULL;
    pC->pPosInSsrcBufferIn = M4OSA_NULL;
    pC->pPosInSsrcBufferOut = M4OSA_NULL;
    pC->SsrcScratch = M4OSA_NULL;
    pC->uiBeginLoop = pSettings->uiBeginLoop;
    pC->uiEndLoop = pSettings->uiEndLoop;

    /*
    * Reset pointers for media and codecs interfaces */
    err = M4VSS3GPP_clearInterfaceTables(&pC->ShellAPI);
    M4ERR_CHECK_RETURN(err);

    /*  Call the media and codecs subscription module */
    err = M4VSS3GPP_subscribeMediaAndCodec(&pC->ShellAPI);
    M4ERR_CHECK_RETURN(err);

    /**
    * Open input clip, added clip and output clip and proceed with the settings */
    err = M4VSS3GPP_intAudioMixingOpen(pC, pSettings);
    M4ERR_CHECK_RETURN(err);

    /**
    * Update main state automaton */
    if( M4OSA_NULL != pC->pInputClipCtxt->pVideoStream )
        pC->State = M4VSS3GPP_kAudioMixingState_VIDEO;
    else
        pC->State = M4VSS3GPP_kAudioMixingState_AUDIO_FIRST_SEGMENT;

    pC->ewc.iOutputDuration = (M4OSA_Int32)pC->pInputClipCtxt->pSettings->
        ClipProperties.uiClipDuration;
    /*gInputParams.lvBTChannelCount*/
    pC->pLVAudioResampler = LVAudioResamplerCreate(16,
        pC->pAddedClipCtxt->pSettings->ClipProperties.uiNbChannels,
        /* gInputParams.lvOutSampleRate*/(M4OSA_Int32)pSettings->outputASF, 1);
     if( M4OSA_NULL == pC->pLVAudioResampler )
     {
         return M4ERR_ALLOC;
     }
        LVAudiosetSampleRate(pC->pLVAudioResampler,
        /*gInputParams.lvInSampleRate*/
        pC->pAddedClipCtxt->pSettings->ClipProperties.uiSamplingFrequency);

    LVAudiosetVolume(pC->pLVAudioResampler,
                    (M4OSA_Int16)(0x1000 ),
                    (M4OSA_Int16)(0x1000 ));

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

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_audioMixingStep(M4VSS3GPP_AudioMixingContext pContext)
 * @brief    Perform one step of audio mixing.
 * @note
 * @param     pContext          (IN) VSS audio mixing context
 * @return    M4NO_ERROR:       No error
 * @return    M4ERR_PARAMETER:  pContext is M4OSA_NULL (debug only)
 * @param     pProgress         (OUT) Progress percentage (0 to 100) of the finalization operation
 * @return    M4ERR_STATE:      VSS is not in an appropriate state for this function to be called
 * @return    M4VSS3GPP_WAR_END_OF_AUDIO_MIXING: Audio mixing is over, user should now call
 *                                               M4VSS3GPP_audioMixingCleanUp()
 ******************************************************************************
 */
M4OSA_ERR M4VSS3GPP_audioMixingStep( M4VSS3GPP_AudioMixingContext pContext,
                                    M4OSA_UInt8 *pProgress )
{
    M4OSA_ERR err;
    M4VSS3GPP_InternalAudioMixingContext *pC =
        (M4VSS3GPP_InternalAudioMixingContext *)pContext;

    M4OSA_TRACE3_1("M4VSS3GPP_audioMixingStep called with pContext=0x%x",
        pContext);

    /**
    * Check input parameters */
    M4OSA_DEBUG_IF2((M4OSA_NULL == pContext), M4ERR_PARAMETER,
        "M4VSS3GPP_audioMixingStep: pContext is M4OSA_NULL");

    /**
    * State automaton */
    switch( pC->State )
    {
        case M4VSS3GPP_kAudioMixingState_VIDEO:
            err = M4VSS3GPP_intAudioMixingStepVideo(pC);

            /**
            * Compute the progress percentage
            * Note: audio and video CTS are not initialized before
            * the call of M4VSS3GPP_intAudioMixingStepVideo */

            /* P4ME00003276: First 0-50% segment is dedicated to state :
               M4VSS3GPP_kAudioMixingState_VIDEO */
            *pProgress = (M4OSA_UInt8)(50 * (pC->ewc.WriterVideoAU.CTS)
                / pC->pInputClipCtxt->pVideoStream->
                m_basicProperties.m_duration);

            /**
            * There may be no audio track (Remove audio track feature).
            * In that case we double the current percentage */
            if( M4SYS_kAudioUnknown == pC->ewc.WriterAudioStream.streamType )
            {
                ( *pProgress) <<= 1; /**< x2 */
            }
            else if( *pProgress >= 50 )
            {
                *pProgress =
                    49; /**< Video processing is not greater than 50% */
            }

            if( M4WAR_NO_MORE_AU == err )
            {
                if( pC->bHasAudio )
                {
                    /**
                    * Video is over, state transition to audio and return OK */
                    if( pC->iAddCts > 0 )
                        pC->State =
                        M4VSS3GPP_kAudioMixingState_AUDIO_FIRST_SEGMENT;
                    else
                        pC->State =
                        M4VSS3GPP_kAudioMixingState_AUDIO_SECOND_SEGMENT;
                }
                else
                {
                    /**
                    * No audio, state transition to FINISHED */
                    pC->State = M4VSS3GPP_kAudioMixingState_FINISHED;
                }

                return M4NO_ERROR;
            }
            else if( M4NO_ERROR != err )
            {
                M4OSA_TRACE1_1(
                    "M4VSS3GPP_audioMixingStep: M4VSS3GPP_intAudioMixingStepVideo returns 0x%x!",
                    err);
                return err;
            }
            else
            {
                return M4NO_ERROR;
            }
            break;

        case M4VSS3GPP_kAudioMixingState_AUDIO_FIRST_SEGMENT:
        case M4VSS3GPP_kAudioMixingState_AUDIO_SECOND_SEGMENT:
        case M4VSS3GPP_kAudioMixingState_AUDIO_THIRD_SEGMENT:
            if( pC->pAddedClipCtxt->iAudioFrameCts
                != -pC->pAddedClipCtxt->iSilenceFrameDuration
                && (pC->pAddedClipCtxt->iAudioFrameCts - 0.5)
                / pC->pAddedClipCtxt->scale_audio > pC->uiEndLoop
                && pC->uiEndLoop > 0 )
            {
            if(pC->bLoop == M4OSA_FALSE)
            {
                pC->bNoLooping = M4OSA_TRUE;
            }
            else
            {
                M4OSA_Int32 jumpCTS = (M4OSA_Int32)(pC->uiBeginLoop);

                err = pC->pAddedClipCtxt->ShellAPI.m_pReader->m_pFctJump(
                    pC->pAddedClipCtxt->pReaderContext,
                    (M4_StreamHandler *)pC->pAddedClipCtxt->
                    pAudioStream, &jumpCTS);

                if( err != M4NO_ERROR )
                {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_audioMixingStep: error when jumping in added audio clip: 0x%x",
                        err);
                    return err;
                }
                /**
                * Use offset to give a correct CTS ... */
                pC->pAddedClipCtxt->iAoffset =
                    (M4OSA_Int32)(pC->ewc.dATo * pC->ewc.scale_audio + 0.5);
            }

            }

            if( M4OSA_FALSE == pC->bRemoveOriginal )
            {
                err = M4VSS3GPP_intAudioMixingStepAudioMix(pC);
            }
            else
            {
                err = M4VSS3GPP_intAudioMixingStepAudioReplace(pC);
            }

            /**
            * Compute the progress percentage
            * Note: audio and video CTS are not initialized before
            * the call of M4VSS3GPP_intAudioMixingStepAudio */
            if( 0 != pC->ewc.iOutputDuration )
            {
                /* P4ME00003276: Second 50-100% segment is dedicated to states :
                M4VSS3GPP_kAudioMixingState_AUDIO... */
                /* For Audio the progress computation is based on dAto and offset,
                   it is more accurate */
                *pProgress = (M4OSA_UInt8)(50
                    + (50 * pC->ewc.dATo - pC->pInputClipCtxt->iVoffset)
                    / (pC->ewc.iOutputDuration)); /**< 50 for 100/2 **/

                if( *pProgress >= 100 )
                {
                    *pProgress =
                        99; /**< It's not really finished, I prefer to return less than 100% */
                }
            }
            else
            {
                *pProgress = 99;
            }

            if( M4WAR_NO_MORE_AU == err )
            {
                /**
                * Audio is over, state transition to FINISHED */
                pC->State = M4VSS3GPP_kAudioMixingState_FINISHED;
                return M4NO_ERROR;
            }
            else if( M4NO_ERROR != err )
            {
                M4OSA_TRACE1_1(
                    "M4VSS3GPP_audioMixingStep: M4VSS3GPP_intAudioMixingStepAudio returns 0x%x!",
                    err);
                return err;
            }
            else
            {
                return M4NO_ERROR;
            }
            break;

        case M4VSS3GPP_kAudioMixingState_FINISHED:

            /**
            * Progress percentage: finalize finished -> 100% */
            *pProgress = 100;

            /**
            * Audio mixing is finished, return correct warning */
            return M4VSS3GPP_WAR_END_OF_AUDIO_MIXING;

        default:
            M4OSA_TRACE1_1(
                "M4VSS3GPP_audioMixingStep: State error (0x%x)! Returning M4ERR_STATE",
                pC->State);
            return M4ERR_STATE;
    }
}

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_audioMixingCleanUp(M4VSS3GPP_AudioMixingContext pContext)
 * @brief    Free all resources used by the VSS audio mixing operation.
 * @note    The context is no more valid after this call
 * @param    pContext            (IN) VSS audio mixing context
 * @return    M4NO_ERROR:            No error
 * @return    M4ERR_PARAMETER:    pContext is M4OSA_NULL (debug only)
 ******************************************************************************
 */
M4OSA_ERR M4VSS3GPP_audioMixingCleanUp( M4VSS3GPP_AudioMixingContext pContext )
{
    M4VSS3GPP_InternalAudioMixingContext *pC =
        (M4VSS3GPP_InternalAudioMixingContext *)pContext;
    M4OSA_ERR err;
    M4OSA_UInt32 lastCTS;

    M4OSA_TRACE3_1("M4VSS3GPP_audioMixingCleanUp called with pContext=0x%x",
        pContext);

    /**
    * Check input parameters */
    M4OSA_DEBUG_IF2((M4OSA_NULL == pContext), M4ERR_PARAMETER,
        "M4VSS3GPP_audioMixingCleanUp: pContext is M4OSA_NULL");

    /**
    * Check input parameter */
    if( M4OSA_NULL == pContext )
    {
        M4OSA_TRACE1_0(
            "M4VSS3GPP_audioMixingCleanUp(): M4VSS3GPP_audioMixingCleanUp: pContext is\
             M4OSA_NULL, returning M4ERR_PARAMETER");
        return M4ERR_PARAMETER;
    }

    /**
    * Close Input 3GPP file */
    if( M4OSA_NULL != pC->pInputClipCtxt )
    {
        M4VSS3GPP_intClipCleanUp(pC->pInputClipCtxt);
        pC->pInputClipCtxt = M4OSA_NULL;
    }

    /**
    * Close Added 3GPP file */
    if( M4OSA_NULL != pC->pAddedClipCtxt )
    {
        M4VSS3GPP_intClipCleanUp(pC->pAddedClipCtxt);
        pC->pAddedClipCtxt = M4OSA_NULL;
    }

    /**
    * Close the 3GP writer. In normal use case it has already been closed,
      but not in abort use case */
    if( M4OSA_NULL != pC->ewc.p3gpWriterContext )
    {
        /* Update last Video CTS */
        lastCTS = pC->ewc.iOutputDuration;

        err = pC->ShellAPI.pWriterGlobalFcts->pFctSetOption(
            pC->ewc.p3gpWriterContext,
            (M4OSA_UInt32)M4WRITER_kMaxFileDuration, &lastCTS);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_audioMixingCleanUp: SetOption(M4WRITER_kMaxFileDuration) returns 0x%x",
                err);
        }

        err = pC->ShellAPI.pWriterGlobalFcts->pFctCloseWrite(
            pC->ewc.p3gpWriterContext);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_audioMixingCleanUp: pWriterGlobalFcts->pFctCloseWrite returns 0x%x!",
                err);
            /**< don't return the error because we have other things to free! */
        }
        pC->ewc.p3gpWriterContext = M4OSA_NULL;
    }

    /**
    * Free the Audio encoder context */
    if( M4OSA_NULL != pC->ewc.pAudioEncCtxt )
    {
        err = pC->ShellAPI.pAudioEncoderGlobalFcts->pFctClose(
            pC->ewc.pAudioEncCtxt);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_audioMixingCleanUp: pAudioEncoderGlobalFcts->pFctClose returns 0x%x",
                err);
            /**< don't return, we still have stuff to free */
        }

        err = pC->ShellAPI.pAudioEncoderGlobalFcts->pFctCleanUp(
            pC->ewc.pAudioEncCtxt);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_audioMixingCleanUp: pAudioEncoderGlobalFcts->pFctCleanUp returns 0x%x",
                err);
            /**< don't return, we still have stuff to free */
        }

        pC->ewc.pAudioEncCtxt = M4OSA_NULL;
    }

    /**
    * Free the ssrc stuff */

    if( M4OSA_NULL != pC->SsrcScratch )
    {
        free(pC->SsrcScratch);
        pC->SsrcScratch = M4OSA_NULL;
    }

    if( M4OSA_NULL != pC->pSsrcBufferIn )
    {
        free(pC->pSsrcBufferIn);
        pC->pSsrcBufferIn = M4OSA_NULL;
    }

    if( M4OSA_NULL != pC->pSsrcBufferOut
        && (M4OSA_TRUE == pC->b_SSRCneeded || pC->ChannelConversion > 0) )
    {
        free(pC->pSsrcBufferOut);
        pC->pSsrcBufferOut = M4OSA_NULL;
    }

    if( M4OSA_NULL != pC->pTempBuffer )
    {
        free(pC->pTempBuffer);
        pC->pTempBuffer = M4OSA_NULL;
    }

    if (pC->pLVAudioResampler != M4OSA_NULL)
    {
        LVDestroy(pC->pLVAudioResampler);
        pC->pLVAudioResampler = M4OSA_NULL;
    }

    /**
    * Free the shells interfaces */
    M4VSS3GPP_unRegisterAllWriters(&pC->ShellAPI);
    M4VSS3GPP_unRegisterAllEncoders(&pC->ShellAPI);
    M4VSS3GPP_unRegisterAllReaders(&pC->ShellAPI);
    M4VSS3GPP_unRegisterAllDecoders(&pC->ShellAPI);

    /**
    * Free the context */
    free(pContext);
    pContext = M4OSA_NULL;

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

/******************************************************************************/
/******************************************************************************/
/*********                  STATIC FUNCTIONS                         **********/
/******************************************************************************/
/******************************************************************************/

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intAudioMixingOpen()
 * @brief    Initializes the VSS audio mixing operation (allocates an execution context).
 * @note
 * @param    pContext        (OUT) Pointer on the VSS audio mixing context to allocate
 * @param    pSettings        (IN) Pointer to valid audio mixing settings
 * @return    M4NO_ERROR:            No error
 * @return    M4ERR_PARAMETER:    At least one parameter is M4OSA_NULL (debug only)
 * @return    M4ERR_ALLOC:        There is no more available memory
 ******************************************************************************
 */
static M4OSA_ERR
M4VSS3GPP_intAudioMixingOpen( M4VSS3GPP_InternalAudioMixingContext *pC,
                             M4VSS3GPP_AudioMixingSettings *pSettings )
{
    M4OSA_ERR err;
    M4OSA_UInt32 outputASF = 0;
    M4ENCODER_Header *encHeader;

    M4OSA_TRACE3_2(
        "M4VSS3GPP_intAudioMixingOpen called with pContext=0x%x, pSettings=0x%x",
        pC, pSettings);

    /**
    * The Add Volume must be (strictly) superior than zero */
    if( pSettings->uiAddVolume == 0 )
    {
        M4OSA_TRACE1_0(
            "M4VSS3GPP_intAudioMixingOpen(): AddVolume is zero,\
            returning M4VSS3GPP_ERR_ADDVOLUME_EQUALS_ZERO");
        return M4VSS3GPP_ERR_ADDVOLUME_EQUALS_ZERO;
    }
    /*
    else if(pSettings->uiAddVolume >= 100) // If volume is set to 100, no more original audio ...
    {
    pC->bRemoveOriginal = M4OSA_TRUE;
    }
    */
    /**
    * Build the input clip settings */
    pC->InputClipSettings.pFile =
        pSettings->pOriginalClipFile; /**< Input 3GPP file descriptor */
    pC->InputClipSettings.FileType = M4VIDEOEDITING_kFileType_3GPP;
    pC->InputClipSettings.uiBeginCutTime =
        0; /**< No notion of cut for the audio mixing feature */
    pC->InputClipSettings.uiEndCutTime =
        0; /**< No notion of cut for the audio mixing feature */

    /**
    * Open the original Audio/Video 3GPP clip */
    err = M4VSS3GPP_intClipInit(&pC->pInputClipCtxt, pC->pOsaFileReadPtr);

    if( M4NO_ERROR != err )
    {
        M4OSA_TRACE1_1(
            "M4VSS3GPP_intAudioMixingOpen(): M4VSS3GPP_intClipInit(orig) returns 0x%x",
            err);
        return err;
    }

    err = M4VSS3GPP_intClipOpen(pC->pInputClipCtxt, &pC->InputClipSettings,
        M4OSA_FALSE, M4OSA_FALSE, M4OSA_TRUE);

    if( M4NO_ERROR != err )
    {
        M4OSA_TRACE1_1(
            "M4VSS3GPP_intAudioMixingOpen(): M4VSS3GPP_intClipOpen(orig) returns 0x%x",
            err);
        return err;
    }

    if( M4OSA_NULL == pC->pInputClipCtxt->pAudioStream )
        {
        pC->bRemoveOriginal = M4OSA_TRUE;
        }
    /**
    * If there is no video, it's an error */
    if( M4OSA_NULL == pC->pInputClipCtxt->pVideoStream )
    {
        M4OSA_TRACE1_0(
            "M4VSS3GPP_intAudioMixingOpen(): no video stream in clip,\
            returning M4VSS3GPP_ERR_NO_SUPPORTED_STREAM_IN_FILE");
        return M4VSS3GPP_ERR_NO_SUPPORTED_STREAM_IN_FILE;
    }

    /**
    * Compute clip properties */
    err = M4VSS3GPP_intBuildAnalysis(pC->pInputClipCtxt,
        &pC->pInputClipCtxt->pSettings->ClipProperties);

    if( M4NO_ERROR != err )
    {
        M4OSA_TRACE1_1(
            "M4VSS3GPP_intAudioMixingOpen(): M4VSS3GPP_intBuildAnalysis(orig) returns 0x%x",
            err);
        return err;
    }

    /**
    * Build the added clip settings */
    pC->AddedClipSettings.pFile =
        pSettings->pAddedAudioTrackFile; /**< Added file descriptor */
    pC->AddedClipSettings.FileType = pSettings->AddedAudioFileType;
    pC->AddedClipSettings.uiBeginCutTime =
        0; /**< No notion of cut for the audio mixing feature */
    pC->AddedClipSettings.uiEndCutTime   = 0;/**< No notion of cut for the audio mixing feature */
    pC->AddedClipSettings.ClipProperties.uiNbChannels=
        pSettings->uiNumChannels;
    pC->AddedClipSettings.ClipProperties.uiSamplingFrequency=    pSettings->uiSamplingFrequency;

    if( M4OSA_NULL != pC->AddedClipSettings.pFile )
    {
        /**
        * Open the added Audio clip */
        err = M4VSS3GPP_intClipInit(&pC->pAddedClipCtxt, pC->pOsaFileReadPtr);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intAudioMixingOpen(): M4VSS3GPP_intClipInit(added) returns 0x%x",
                err);
            return err;
        }

        err = M4VSS3GPP_intClipOpen(pC->pAddedClipCtxt, &pC->AddedClipSettings,
            M4OSA_FALSE, M4OSA_FALSE, M4OSA_TRUE);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intAudioMixingOpen(): M4VSS3GPP_intClipOpen(added) returns 0x%x",
                err);
            return err;
        }

        /**
        * If there is no audio, it's an error */
        if( M4OSA_NULL == pC->pAddedClipCtxt->pAudioStream )
        {
            M4OSA_TRACE1_0(
                "M4VSS3GPP_intAudioMixingOpen(): no audio nor video stream in clip,\
                returning M4VSS3GPP_ERR_NO_SUPPORTED_STREAM_IN_FILE");
            return M4VSS3GPP_ERR_NO_SUPPORTED_STREAM_IN_FILE;
        }

        /**
        * Compute clip properties */
        err = M4VSS3GPP_intBuildAnalysis(pC->pAddedClipCtxt,
            &pC->pAddedClipCtxt->pSettings->ClipProperties);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intAudioMixingOpen(): M4VSS3GPP_intBuildAnalysis(added) returns 0x%x",
                err);
            return err;
        }

        switch( pSettings->outputASF )
        {
            case M4VIDEOEDITING_k8000_ASF:
                outputASF = 8000;
                break;

            case M4VIDEOEDITING_k16000_ASF:
                outputASF = 16000;
                break;

            case M4VIDEOEDITING_k22050_ASF:
                outputASF = 22050;
                break;

            case M4VIDEOEDITING_k24000_ASF:
                outputASF = 24000;
                break;

            case M4VIDEOEDITING_k32000_ASF:
                outputASF = 32000;
                break;

            case M4VIDEOEDITING_k44100_ASF:
                outputASF = 44100;
                break;

            case M4VIDEOEDITING_k48000_ASF:
                outputASF = 48000;
                break;

            default:
                M4OSA_TRACE1_0("Bad parameter in output ASF ");
                return M4ERR_PARAMETER;
                break;
        }

        if( pC->bRemoveOriginal == M4OSA_TRUE
            && (pC->pAddedClipCtxt->pSettings->ClipProperties.AudioStreamType
            == M4VIDEOEDITING_kMP3 || pC->pAddedClipCtxt->pSettings->
            ClipProperties.AudioStreamType == M4VIDEOEDITING_kPCM
            || pC->pAddedClipCtxt->pSettings->
            ClipProperties.AudioStreamType
            != pSettings->outputAudioFormat
            || pC->pAddedClipCtxt->pSettings->
            ClipProperties.uiSamplingFrequency != outputASF
            || pC->pAddedClipCtxt->pSettings->
            ClipProperties.uiNbChannels
            != pSettings->outputNBChannels) )
        {

            if( pSettings->outputAudioFormat == M4VIDEOEDITING_kAMR_NB )
            {
                pSettings->outputASF = M4VIDEOEDITING_k8000_ASF;
                pSettings->outputNBChannels = 1;
                pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize = 320;
            }
            else if( pSettings->outputAudioFormat == M4VIDEOEDITING_kAAC )
            {
                pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize =
                    2048 * pSettings->outputNBChannels;
            }

            pC->pInputClipCtxt->pSettings->ClipProperties.uiSamplingFrequency =
                outputASF;

            if( outputASF != pC->pAddedClipCtxt->pSettings->
                ClipProperties.uiSamplingFrequency )
            {
                /* We need to call SSRC in order to align ASF and/or nb of channels */
                /* Moreover, audio encoder may be needed in case of audio replacing... */
                pC->b_SSRCneeded = M4OSA_TRUE;
            }

            if( pSettings->outputNBChannels
                < pC->pAddedClipCtxt->pSettings->ClipProperties.uiNbChannels )
            {
                /* Stereo to Mono */
                pC->ChannelConversion = 1;
            }
            else if( pSettings->outputNBChannels
                > pC->pAddedClipCtxt->pSettings->ClipProperties.uiNbChannels )
            {
                /* Mono to Stereo */
                pC->ChannelConversion = 2;
            }

            pC->pInputClipCtxt->pSettings->ClipProperties.uiNbChannels =
                pSettings->outputNBChannels;
        }

        /**
        * Check compatibility chart */
        err = M4VSS3GPP_intAudioMixingCompatibility(pC,
            &pC->pInputClipCtxt->pSettings->ClipProperties,
            &pC->pAddedClipCtxt->pSettings->ClipProperties);

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

        /**
        * Check loop parameters */
        if( pC->uiBeginLoop > pC->pAddedClipCtxt->pSettings->
            ClipProperties.uiClipAudioDuration )
        {
            M4OSA_TRACE1_0(
                "M4VSS3GPP_intAudioMixingOpen():\
                begin loop time is higher than added clip audio duration");
            return M4VSS3GPP_ERR_BEGINLOOP_HIGHER_ENDLOOP;
        }

        /**
        * Ok, let's go with this audio track */
        pC->bHasAudio = M4OSA_TRUE;
    }
    else
    {
        /* No added file, force remove original */
        pC->AddedClipSettings.FileType = M4VIDEOEDITING_kFileType_Unsupported;
        pC->bRemoveOriginal = M4OSA_TRUE;
        pC->bHasAudio = M4OSA_FALSE;
    }

    /**
    * Copy the video properties of the input clip to the output properties */
    pC->ewc.uiVideoBitrate =
        pC->pInputClipCtxt->pSettings->ClipProperties.uiVideoBitrate;
    pC->ewc.uiVideoWidth =
        pC->pInputClipCtxt->pSettings->ClipProperties.uiVideoWidth;
    pC->ewc.uiVideoHeight =
        pC->pInputClipCtxt->pSettings->ClipProperties.uiVideoHeight;
    pC->ewc.uiVideoTimeScale =
        pC->pInputClipCtxt->pSettings->ClipProperties.uiVideoTimeScale;
    pC->ewc.bVideoDataPartitioning =
        pC->pInputClipCtxt->pSettings->ClipProperties.bMPEG4dataPartition;
    pC->ewc.outputVideoProfile =
        pC->pInputClipCtxt->pSettings->ClipProperties.uiVideoProfile;
    pC->ewc.outputVideoLevel =
        pC->pInputClipCtxt->pSettings->ClipProperties.uiVideoLevel;
    switch( pC->pInputClipCtxt->pSettings->ClipProperties.VideoStreamType )
    {
        case M4VIDEOEDITING_kH263:
            pC->ewc.VideoStreamType = M4SYS_kH263;
            break;

        case M4VIDEOEDITING_kMPEG4:
            pC->ewc.VideoStreamType = M4SYS_kMPEG_4;
            break;

        case M4VIDEOEDITING_kH264:
            pC->ewc.VideoStreamType = M4SYS_kH264;
            break;

        default:
            pC->ewc.VideoStreamType = M4SYS_kVideoUnknown;
            break;
    }

    /* Add a link to video dsi */
    if( M4SYS_kH264 == pC->ewc.VideoStreamType )
    {

        /* For H.264 encoder case
        * Fetch the DSI from the shell video encoder, and feed it to the writer */

        M4OSA_TRACE3_0("M4VSS3GPP_intAudioMixingOpen: get DSI for H264 stream");

        if( M4OSA_NULL == pC->ewc.pEncContext )
        {
            M4OSA_TRACE1_0(
                "M4VSS3GPP_intAudioMixingOpen: pC->ewc.pEncContext is NULL");
            err = M4VSS3GPP_intAudioMixingCreateVideoEncoder(pC);

            if( M4NO_ERROR != err )
            {
                M4OSA_TRACE1_1(
                    "M4VSS3GPP_intAudioMixingOpen:\
                    M4VSS3GPP_intAudioMixingCreateVideoEncoder returned error 0x%x",
                    err);
            }
        }

        if( M4OSA_NULL != pC->ewc.pEncContext )
        {
            err = pC->ShellAPI.pVideoEncoderGlobalFcts->pFctGetOption(
                pC->ewc.pEncContext, M4ENCODER_kOptionID_EncoderHeader,
                (M4OSA_DataOption) &encHeader);

            if( ( M4NO_ERROR != err) || (M4OSA_NULL == encHeader->pBuf) )
            {
                M4OSA_TRACE1_1(
                    "M4VSS3GPP_intAudioMixingOpen: failed to get the encoder header (err 0x%x)",
                    err);
                M4OSA_TRACE1_2(
                    "M4VSS3GPP_intAudioMixingOpen: encHeader->pBuf=0x%x, size=0x%x",
                    encHeader->pBuf, encHeader->Size);
            }
            else
            {
                M4OSA_TRACE1_0(
                    "M4VSS3GPP_intAudioMixingOpen: send DSI for H264 stream to 3GP writer");

                /**
                * Allocate and copy the new DSI */
                pC->ewc.pVideoOutputDsi =
                    (M4OSA_MemAddr8)M4OSA_32bitAlignedMalloc(encHeader->Size, M4VSS3GPP,
                    (M4OSA_Char *)"pC->ewc.pVideoOutputDsi (H264)");

                if( M4OSA_NULL == pC->ewc.pVideoOutputDsi )
                {
                    M4OSA_TRACE1_0(
                        "M4VSS3GPP_intAudioMixingOpen():\
                        unable to allocate pVideoOutputDsi (H264), returning M4ERR_ALLOC");
                    return M4ERR_ALLOC;
                }
                pC->ewc.uiVideoOutputDsiSize = (M4OSA_UInt16)encHeader->Size;
                memcpy((void *)pC->ewc.pVideoOutputDsi, (void *)encHeader->pBuf,
                    encHeader->Size);
            }

            err = M4VSS3GPP_intAudioMixingDestroyVideoEncoder(pC);

            if( M4NO_ERROR != err )
            {
                M4OSA_TRACE1_1(
                    "M4VSS3GPP_intAudioMixingOpen:\
                    M4VSS3GPP_intAudioMixingDestroyVideoEncoder returned error 0x%x",
                    err);
            }
        }
        else
        {
            M4OSA_TRACE1_0(
                "M4VSS3GPP_intAudioMixingOpen: pC->ewc.pEncContext is NULL, cannot get the DSI");
        }
    }
    else
    {
        M4OSA_TRACE3_1(
            "M4VSS3GPP_intAudioMixingOpen: input clip video stream type = 0x%x",
            pC->ewc.VideoStreamType);
        pC->ewc.uiVideoOutputDsiSize =
            (M4OSA_UInt16)pC->pInputClipCtxt->pVideoStream->
            m_basicProperties.m_decoderSpecificInfoSize;
        pC->ewc.pVideoOutputDsi = (M4OSA_MemAddr8)pC->pInputClipCtxt->pVideoStream->
            m_basicProperties.m_pDecoderSpecificInfo;
    }

    /**
    * Copy the audio properties of the added clip to the output properties */
    if( pC->bHasAudio )
    {
        if( pC->bRemoveOriginal == M4OSA_TRUE )
        {
            pC->ewc.uiNbChannels =
                pC->pAddedClipCtxt->pSettings->ClipProperties.uiNbChannels;
            pC->ewc.uiAudioBitrate =
                pC->pAddedClipCtxt->pSettings->ClipProperties.uiAudioBitrate;
            pC->ewc.uiSamplingFrequency = pC->pAddedClipCtxt->pSettings->
                ClipProperties.uiSamplingFrequency;
            pC->ewc.uiSilencePcmSize =
                pC->pAddedClipCtxt->pSettings->ClipProperties.uiDecodedPcmSize;
            pC->ewc.scale_audio = pC->ewc.uiSamplingFrequency / 1000.0;

            /* if output settings are differents from added clip settings,
            we need to reencode BGM */
            if( pC->pAddedClipCtxt->pSettings->ClipProperties.AudioStreamType
                != pSettings->outputAudioFormat
                || pC->pAddedClipCtxt->pSettings->
                ClipProperties.uiSamplingFrequency != outputASF
                || pC->pAddedClipCtxt->pSettings->
                ClipProperties.uiNbChannels
                != pSettings->outputNBChannels
                || pC->pAddedClipCtxt->pSettings->
                ClipProperties.AudioStreamType == M4VIDEOEDITING_kMP3 )
            {
                /* Set reader DSI to NULL (unknown), we will use encoder DSI later */
                if( pC->pAddedClipCtxt->pAudioStream->
                    m_basicProperties.m_pDecoderSpecificInfo != M4OSA_NULL )
                {

                    /*
                     free(pC->pAddedClipCtxt->pAudioStream->\
                       m_basicProperties.m_pDecoderSpecificInfo);
                       */
                    pC->pAddedClipCtxt->pAudioStream->
                        m_basicProperties.m_decoderSpecificInfoSize = 0;
                    pC->pAddedClipCtxt->pAudioStream->
                        m_basicProperties.m_pDecoderSpecificInfo = M4OSA_NULL;
                }

                pC->ewc.uiNbChannels =
                    pC->pInputClipCtxt->pSettings->ClipProperties.uiNbChannels;
                pC->ewc.uiSamplingFrequency = pC->pInputClipCtxt->pSettings->
                    ClipProperties.uiSamplingFrequency;
                pC->ewc.scale_audio = pC->ewc.uiSamplingFrequency / 1000.0;

                if( pSettings->outputAudioFormat == M4VIDEOEDITING_kAMR_NB )
                {
                    pC->ewc.AudioStreamType = M4SYS_kAMR;
                    pC->ewc.pSilenceFrameData =
                        (M4OSA_UInt8 *)M4VSS3GPP_AMR_AU_SILENCE_FRAME_048;
                    pC->ewc.uiSilenceFrameSize =
                        M4VSS3GPP_AMR_AU_SILENCE_FRAME_048_SIZE;
                    pC->ewc.iSilenceFrameDuration =
                        M4VSS3GPP_AMR_AU_SILENCE_FRAME_048_DURATION;
                    pC->ewc.uiAudioBitrate = 12200;
                    pC->ewc.uiSamplingFrequency = 8000;
                    pC->ewc.uiSilencePcmSize = 320;
                    pC->ewc.scale_audio = pC->ewc.uiSamplingFrequency / 1000.0;
                }
                else if( pSettings->outputAudioFormat == M4VIDEOEDITING_kAAC )
                {
                    pC->ewc.AudioStreamType = M4SYS_kAAC;

                    if( pSettings->outputAudioBitrate
                        == M4VIDEOEDITING_kUndefinedBitrate )
                    {
                        switch( pC->ewc.uiSamplingFrequency )
                        {
                            case 16000:
                                pC->ewc.uiAudioBitrate =
                                    M4VIDEOEDITING_k24_KBPS;
                                break;

                            case 22050:
                            case 24000:
                                pC->ewc.uiAudioBitrate =
                                    M4VIDEOEDITING_k32_KBPS;
                                break;

                            case 32000:
                                pC->ewc.uiAudioBitrate =
                                    M4VIDEOEDITING_k48_KBPS;
                                break;

                            case 44100:
                            case 48000:
                                pC->ewc.uiAudioBitrate =
                                    M4VIDEOEDITING_k64_KBPS;
                                break;

                            default:
                                pC->ewc.uiAudioBitrate =
                                    M4VIDEOEDITING_k64_KBPS;
                                break;
                        }

                        if( pC->ewc.uiNbChannels == 2 )
                        {
                            /* Output bitrate have to be doubled */
                            pC->ewc.uiAudioBitrate += pC->ewc.uiAudioBitrate;
                        }
                    }
                    else
                    {
                        pC->ewc.uiAudioBitrate = pSettings->outputAudioBitrate;
                    }

                    if( pC->ewc.uiNbChannels == 1 )
                    {
                        pC->ewc.pSilenceFrameData =
                            (M4OSA_UInt8 *)M4VSS3GPP_AAC_AU_SILENCE_MONO;
                        pC->ewc.uiSilenceFrameSize =
                            M4VSS3GPP_AAC_AU_SILENCE_MONO_SIZE;
                    }
                    else
                    {
                        pC->ewc.pSilenceFrameData =
                            (M4OSA_UInt8 *)M4VSS3GPP_AAC_AU_SILENCE_STEREO;
                        pC->ewc.uiSilenceFrameSize =
                            M4VSS3GPP_AAC_AU_SILENCE_STEREO_SIZE;
                    }
                    pC->ewc.iSilenceFrameDuration =
                        1024; /* AAC is always 1024/Freq sample duration */
                }
            }
            else
            {
                switch( pC->pAddedClipCtxt->pSettings->
                    ClipProperties.AudioStreamType )
                {
                    case M4VIDEOEDITING_kAMR_NB:
                        pC->ewc.AudioStreamType = M4SYS_kAMR;
                        pC->ewc.pSilenceFrameData =
                            (M4OSA_UInt8 *)M4VSS3GPP_AMR_AU_SILENCE_FRAME_048;
                        pC->ewc.uiSilenceFrameSize =
                            M4VSS3GPP_AMR_AU_SILENCE_FRAME_048_SIZE;
                        pC->ewc.iSilenceFrameDuration =
                            M4VSS3GPP_AMR_AU_SILENCE_FRAME_048_DURATION;
                        break;

                    case M4VIDEOEDITING_kAAC:
                    case M4VIDEOEDITING_kAACplus:
                    case M4VIDEOEDITING_keAACplus:
                        pC->ewc.AudioStreamType = M4SYS_kAAC;

                        if( pC->ewc.uiNbChannels == 1 )
                        {
                            pC->ewc.pSilenceFrameData =
                                (M4OSA_UInt8 *)M4VSS3GPP_AAC_AU_SILENCE_MONO;
                            pC->ewc.uiSilenceFrameSize =
                                M4VSS3GPP_AAC_AU_SILENCE_MONO_SIZE;
                        }
                        else
                        {
                            pC->ewc.pSilenceFrameData =
                                (M4OSA_UInt8 *)M4VSS3GPP_AAC_AU_SILENCE_STEREO;
                            pC->ewc.uiSilenceFrameSize =
                                M4VSS3GPP_AAC_AU_SILENCE_STEREO_SIZE;
                        }
                        pC->ewc.iSilenceFrameDuration =
                            1024; /* AAC is always 1024/Freq sample duration */
                        break;

                    case M4VIDEOEDITING_kEVRC:
                        pC->ewc.AudioStreamType = M4SYS_kEVRC;
                        pC->ewc.pSilenceFrameData = M4OSA_NULL;
                        pC->ewc.uiSilenceFrameSize = 0;
                        pC->ewc.iSilenceFrameDuration = 160; /* EVRC frames are 20 ms at 8000 Hz
                                            (makes it easier to factorize amr and evrc code) */
                        break;

                    case M4VIDEOEDITING_kPCM:
                        /* Set reader DSI to NULL (unknown), we will use encoder DSI later */
                        pC->pAddedClipCtxt->pAudioStream->
                            m_basicProperties.m_decoderSpecificInfoSize = 0;
                        pC->pAddedClipCtxt->pAudioStream->
                            m_basicProperties.m_pDecoderSpecificInfo =
                            M4OSA_NULL;

                        if( pC->pAddedClipCtxt->pSettings->
                            ClipProperties.uiSamplingFrequency == 8000 )
                        {
                            pC->ewc.AudioStreamType = M4SYS_kAMR;
                            pC->ewc.pSilenceFrameData = (M4OSA_UInt8
                                *)M4VSS3GPP_AMR_AU_SILENCE_FRAME_048;
                            pC->ewc.uiSilenceFrameSize =
                                M4VSS3GPP_AMR_AU_SILENCE_FRAME_048_SIZE;
                            pC->ewc.iSilenceFrameDuration =
                                M4VSS3GPP_AMR_AU_SILENCE_FRAME_048_DURATION;
                            pC->ewc.uiAudioBitrate = M4VIDEOEDITING_k12_2_KBPS;
                        }
                        else if( pC->pAddedClipCtxt->pSettings->
                            ClipProperties.uiSamplingFrequency == 16000 )
                        {
                            if( pC->ewc.uiNbChannels == 1 )
                            {
                                pC->ewc.AudioStreamType = M4SYS_kAAC;
                                pC->ewc.pSilenceFrameData = (M4OSA_UInt8
                                    *)M4VSS3GPP_AAC_AU_SILENCE_MONO;
                                pC->ewc.uiSilenceFrameSize =
                                    M4VSS3GPP_AAC_AU_SILENCE_MONO_SIZE;
                                pC->ewc.iSilenceFrameDuration =
                                    1024; /* AAC is always 1024/Freq sample duration */
                                pC->ewc.uiAudioBitrate =
                                    M4VIDEOEDITING_k32_KBPS;
                            }
                            else
                            {
                                pC->ewc.AudioStreamType = M4SYS_kAAC;
                                pC->ewc.pSilenceFrameData = (M4OSA_UInt8
                                    *)M4VSS3GPP_AAC_AU_SILENCE_STEREO;
                                pC->ewc.uiSilenceFrameSize =
                                    M4VSS3GPP_AAC_AU_SILENCE_STEREO_SIZE;
                                pC->ewc.iSilenceFrameDuration =
                                    1024; /* AAC is always 1024/Freq sample duration */
                                pC->ewc.uiAudioBitrate =
                                    M4VIDEOEDITING_k64_KBPS;
                            }
                        }
                        else
                        {
                            pC->ewc.AudioStreamType = M4SYS_kAudioUnknown;
                        }
                        break;

                    default:
                        pC->ewc.AudioStreamType = M4SYS_kAudioUnknown;
                        break;
                }
            }

            /* Add a link to audio dsi */
            pC->ewc.uiAudioOutputDsiSize =
                (M4OSA_UInt16)pC->pAddedClipCtxt->pAudioStream->
                m_basicProperties.m_decoderSpecificInfoSize;
            pC->ewc.pAudioOutputDsi = (M4OSA_MemAddr8)pC->pAddedClipCtxt->pAudioStream->
                m_basicProperties.m_pDecoderSpecificInfo;
        }
        else
        {
            pC->ewc.uiNbChannels =
                pC->pInputClipCtxt->pSettings->ClipProperties.uiNbChannels;
            pC->ewc.uiAudioBitrate =
                pC->pInputClipCtxt->pSettings->ClipProperties.uiAudioBitrate;
            pC->ewc.uiSamplingFrequency = pC->pInputClipCtxt->pSettings->
                ClipProperties.uiSamplingFrequency;
            pC->ewc.uiSilencePcmSize =
                pC->pInputClipCtxt->pSettings->ClipProperties.uiDecodedPcmSize;
            pC->ewc.scale_audio = pC->ewc.uiSamplingFrequency / 1000.0;

            switch( pC->pInputClipCtxt->pSettings->
                ClipProperties.AudioStreamType )
            {
                case M4VIDEOEDITING_kAMR_NB:
                    pC->ewc.AudioStreamType = M4SYS_kAMR;
                    pC->ewc.pSilenceFrameData =
                        (M4OSA_UInt8 *)M4VSS3GPP_AMR_AU_SILENCE_FRAME_048;
                    pC->ewc.uiSilenceFrameSize =
                        M4VSS3GPP_AMR_AU_SILENCE_FRAME_048_SIZE;
                    pC->ewc.iSilenceFrameDuration =
                        M4VSS3GPP_AMR_AU_SILENCE_FRAME_048_DURATION;
                    break;

                case M4VIDEOEDITING_kAAC:
                case M4VIDEOEDITING_kAACplus:
                case M4VIDEOEDITING_keAACplus:
                    pC->ewc.AudioStreamType = M4SYS_kAAC;

                    if( pC->ewc.uiNbChannels == 1 )
                    {
                        pC->ewc.pSilenceFrameData =
                            (M4OSA_UInt8 *)M4VSS3GPP_AAC_AU_SILENCE_MONO;
                        pC->ewc.uiSilenceFrameSize =
                            M4VSS3GPP_AAC_AU_SILENCE_MONO_SIZE;
                    }
                    else
                    {
                        pC->ewc.pSilenceFrameData =
                            (M4OSA_UInt8 *)M4VSS3GPP_AAC_AU_SILENCE_STEREO;
                        pC->ewc.uiSilenceFrameSize =
                            M4VSS3GPP_AAC_AU_SILENCE_STEREO_SIZE;
                    }
                    pC->ewc.iSilenceFrameDuration =
                        1024; /* AAC is always 1024/Freq sample duration */
                    break;

                default:
                    pC->ewc.AudioStreamType = M4SYS_kAudioUnknown;
                    M4OSA_TRACE1_0(
                        "M4VSS3GPP_intAudioMixingOpen: No audio track in input file.");
                    return M4VSS3GPP_ERR_AUDIO_CANNOT_BE_MIXED;
                    break;
            }

            /* Add a link to audio dsi */
            pC->ewc.uiAudioOutputDsiSize =
                (M4OSA_UInt16)pC->pInputClipCtxt->pAudioStream->
                m_basicProperties.m_decoderSpecificInfoSize;
            pC->ewc.pAudioOutputDsi = (M4OSA_MemAddr8)pC->pInputClipCtxt->pAudioStream->
                m_basicProperties.m_pDecoderSpecificInfo;
        }
    }

    /**
    * Copy common 'silence frame stuff' to ClipContext */
    pC->pInputClipCtxt->uiSilencePcmSize = pC->ewc.uiSilencePcmSize;
    pC->pInputClipCtxt->pSilenceFrameData = pC->ewc.pSilenceFrameData;
    pC->pInputClipCtxt->uiSilenceFrameSize = pC->ewc.uiSilenceFrameSize;
    pC->pInputClipCtxt->iSilenceFrameDuration = pC->ewc.iSilenceFrameDuration;
    pC->pInputClipCtxt->scale_audio = pC->ewc.scale_audio;

    pC->pInputClipCtxt->iAudioFrameCts =
        -pC->pInputClipCtxt->iSilenceFrameDuration; /* Reset time */

    /**
    * Copy common 'silence frame stuff' to ClipContext */
    if( pC->bHasAudio )
    {
        pC->pAddedClipCtxt->uiSilencePcmSize = pC->ewc.uiSilencePcmSize;
        pC->pAddedClipCtxt->pSilenceFrameData = pC->ewc.pSilenceFrameData;
        pC->pAddedClipCtxt->uiSilenceFrameSize = pC->ewc.uiSilenceFrameSize;
        pC->pAddedClipCtxt->iSilenceFrameDuration =
            pC->ewc.iSilenceFrameDuration;
        pC->pAddedClipCtxt->scale_audio = pC->ewc.scale_audio;

        pC->pAddedClipCtxt->iAudioFrameCts =
            -pC->pAddedClipCtxt->iSilenceFrameDuration; /* Reset time */
    }

    /**
    * Check AddCts is lower than original clip duration */
    if( ( M4OSA_NULL != pC->pInputClipCtxt->pVideoStream)
        && (pC->iAddCts > (M4OSA_Int32)pC->pInputClipCtxt->pVideoStream->
        m_basicProperties.m_duration) )
    {
        M4OSA_TRACE1_0(
            "M4VSS3GPP_intAudioMixingOpen(): uiAddCts is larger than video duration,\
            returning M4VSS3GPP_ERR_ADDCTS_HIGHER_THAN_VIDEO_DURATION");
        return M4VSS3GPP_ERR_ADDCTS_HIGHER_THAN_VIDEO_DURATION;
    }

    /**
    * If the audio tracks are not compatible, replace input track by silence */
    if( M4OSA_FALSE == pC->pInputClipCtxt->pSettings->
        ClipProperties.bAudioIsCompatibleWithMasterClip )
    {
        M4VSS3GPP_intClipDeleteAudioTrack(pC->pInputClipCtxt);
    }

    /**
    * Check if audio mixing is required */
    if( ( ( pC->bHasAudio) && (M4OSA_FALSE
        == pC->pAddedClipCtxt->pSettings->ClipProperties.bAudioIsEditable))
        || (M4OSA_TRUE == pC->bRemoveOriginal) ) /*||
                                                 (pSettings->uiAddVolume >= 100)) */
    {
        pC->bAudioMixingIsNeeded = M4OSA_FALSE;
    }
    else
    {
        pC->bAudioMixingIsNeeded = M4OSA_TRUE;
    }

    /**
    * Check if output audio can support silence frames
    Trick i use bAudioIsCompatibleWithMasterClip filed to store that  */
    if( pC->bHasAudio )
    {
        pC->bSupportSilence = pC->pAddedClipCtxt->pSettings->
            ClipProperties.bAudioIsCompatibleWithMasterClip;

        if( M4OSA_FALSE == pC->bSupportSilence )
        {
            if( pC->iAddCts > 0 )
            {
                M4OSA_TRACE1_0(
                    "M4VSS3GPP_intAudioMixingOpen():\
                    iAddCts should be set to 0 with this audio track !");
                return M4VSS3GPP_ERR_FEATURE_UNSUPPORTED_WITH_AUDIO_TRACK;
            }

            if( 0 < pC->uiEndLoop )
            {
                M4OSA_TRACE1_0(
                    "M4VSS3GPP_intAudioMixingOpen():\
                    uiEndLoop should be set to 0 with this audio track !");
                return M4VSS3GPP_ERR_FEATURE_UNSUPPORTED_WITH_AUDIO_TRACK;
            }
        }
    }
    if( pC->b_DuckingNeedeed == M4OSA_FALSE)
    {
        /**
        * Compute the factor to apply to sample to do the mixing */
        pC->fAddedFactor = 0.50F;
        pC->fOrigFactor = 0.50F;
    }


    /**
    * Check if SSRC is needed */
    if( M4OSA_TRUE == pC->b_SSRCneeded )
    {
        M4OSA_UInt32 numerator, denominator, ratio, ratioBuffer;

        /**
        * Init the SSRC module */
        SSRC_ReturnStatus_en
            ReturnStatus; /* Function return status                       */
        LVM_INT16 NrSamplesMin =
            0; /* Minimal number of samples on the input or on the output */
        LVM_INT32
            ScratchSize; /* The size of the scratch memory               */
        LVM_INT16
            *pInputInScratch; /* Pointer to input in the scratch buffer       */
        LVM_INT16
            *
            pOutputInScratch; /* Pointer to the output in the scratch buffer  */
        SSRC_Params_t ssrcParams;          /* Memory for init parameters                    */

        switch( pC->pAddedClipCtxt->pSettings->
            ClipProperties.uiSamplingFrequency )
        {
            case 8000:
                ssrcParams.SSRC_Fs_In = LVM_FS_8000;
                break;

            case 11025:
                ssrcParams.SSRC_Fs_In = LVM_FS_11025;
                break;

            case 12000:
                ssrcParams.SSRC_Fs_In = LVM_FS_12000;
                break;

            case 16000:
                ssrcParams.SSRC_Fs_In = LVM_FS_16000;
                break;

            case 22050:
                ssrcParams.SSRC_Fs_In = LVM_FS_22050;
                break;

            case 24000:
                ssrcParams.SSRC_Fs_In = LVM_FS_24000;
                break;

            case 32000:
                ssrcParams.SSRC_Fs_In = LVM_FS_32000;
                break;

            case 44100:
                ssrcParams.SSRC_Fs_In = LVM_FS_44100;
                break;

            case 48000:
                ssrcParams.SSRC_Fs_In = LVM_FS_48000;
                break;

            default:
                M4OSA_TRACE1_1(
                    "M4VSS3GPP_intAudioMixingOpen: invalid added clip sampling frequency (%d Hz),\
                    returning M4VSS3GPP_ERR_UNSUPPORTED_ADDED_AUDIO_STREAM",
                    pC->pAddedClipCtxt->pSettings->
                    ClipProperties.uiSamplingFrequency);
                return M4VSS3GPP_ERR_UNSUPPORTED_ADDED_AUDIO_STREAM;
        }

        if( 1 == pC->pAddedClipCtxt->pSettings->ClipProperties.uiNbChannels )
        {
            ssrcParams.SSRC_NrOfChannels = LVM_MONO;
        }
        else
        {
            ssrcParams.SSRC_NrOfChannels = LVM_STEREO;
        }

        switch( pC->ewc.uiSamplingFrequency )
        {
            case 8000:
                ssrcParams.SSRC_Fs_Out = LVM_FS_8000;
                break;

            case 16000:
                ssrcParams.SSRC_Fs_Out = LVM_FS_16000;
                break;

            case 22050:
                ssrcParams.SSRC_Fs_Out = LVM_FS_22050;
                break;

            case 24000:
                ssrcParams.SSRC_Fs_Out = LVM_FS_24000;
                break;

            case 32000:
                ssrcParams.SSRC_Fs_Out = LVM_FS_32000;
                break;

            case 44100:
                ssrcParams.SSRC_Fs_Out = LVM_FS_44100;
                break;

            case 48000:
                ssrcParams.SSRC_Fs_Out = LVM_FS_48000;
                break;

            default:
                M4OSA_TRACE1_1(
                    "M4VSS3GPP_intAudioMixingOpen: invalid output sampling frequency (%d Hz),\
                    returning M4VSS3GPP_ERR_AUDIO_CANNOT_BE_MIXED",
                    pC->ewc.uiSamplingFrequency);
                return M4VSS3GPP_ERR_AUDIO_CANNOT_BE_MIXED;
                break;
        }
        ReturnStatus = 0;

        switch (ssrcParams.SSRC_Fs_In){
        case LVM_FS_8000:
            ssrcParams.NrSamplesIn = 320;
            break;
        case LVM_FS_11025:
            ssrcParams.NrSamplesIn =441;
            break;
        case LVM_FS_12000:
            ssrcParams.NrSamplesIn =    480;
            break;
        case LVM_FS_16000:
            ssrcParams.NrSamplesIn =    640;
            break;
        case LVM_FS_22050:
            ssrcParams.NrSamplesIn =    882;
            break;
        case LVM_FS_24000:
            ssrcParams.NrSamplesIn =    960;
            break;
        case LVM_FS_32000:
            ssrcParams.NrSamplesIn = 1280;
            break;
        case LVM_FS_44100:
            ssrcParams.NrSamplesIn = 1764;
            break;
        case LVM_FS_48000:
            ssrcParams.NrSamplesIn = 1920;
            break;
        default:
            ReturnStatus = -1;
            break;
        }

        switch (ssrcParams.SSRC_Fs_Out){
        case LVM_FS_8000:
            ssrcParams.NrSamplesOut= 320;
            break;
        case LVM_FS_11025:
            ssrcParams.NrSamplesOut =441;
            break;
        case LVM_FS_12000:
            ssrcParams.NrSamplesOut=    480;
            break;
        case LVM_FS_16000:
            ssrcParams.NrSamplesOut=    640;
            break;
        case LVM_FS_22050:
            ssrcParams.NrSamplesOut=    882;
            break;
        case LVM_FS_24000:
            ssrcParams.NrSamplesOut=    960;
            break;
        case LVM_FS_32000:
            ssrcParams.NrSamplesOut = 1280;
            break;
        case LVM_FS_44100:
            ssrcParams.NrSamplesOut= 1764;
            break;
        case LVM_FS_48000:
            ssrcParams.NrSamplesOut = 1920;
            break;
        default:
            ReturnStatus = -1;
            break;
        }
        if( ReturnStatus != SSRC_OK )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intAudioMixingOpen:\
                Error code %d returned by the SSRC_GetNrSamples function",
                ReturnStatus);
            return M4VSS3GPP_ERR_AUDIO_CANNOT_BE_MIXED;
        }

        NrSamplesMin =
            (LVM_INT16)((ssrcParams.NrSamplesIn > ssrcParams.NrSamplesOut)
            ? ssrcParams.NrSamplesOut : ssrcParams.NrSamplesIn);

        while( NrSamplesMin < M4VSS_SSRC_MINBLOCKSIZE )
        { /* Don't take blocks smaller that the minimal block size */
            ssrcParams.NrSamplesIn = (LVM_INT16)(ssrcParams.NrSamplesIn << 1);
            ssrcParams.NrSamplesOut = (LVM_INT16)(ssrcParams.NrSamplesOut << 1);
            NrSamplesMin = (LVM_INT16)(NrSamplesMin << 1);
        }
        pC->iSsrcNbSamplIn = (LVM_INT16)(
            ssrcParams.
            NrSamplesIn); /* multiplication by NrOfChannels is done below */
        pC->iSsrcNbSamplOut = (LVM_INT16)(ssrcParams.NrSamplesOut);

        numerator =
            pC->pAddedClipCtxt->pSettings->ClipProperties.uiSamplingFrequency
            * pC->pAddedClipCtxt->pSettings->ClipProperties.uiNbChannels;
        denominator =
            pC->pInputClipCtxt->pSettings->ClipProperties.uiSamplingFrequency
            * pC->pInputClipCtxt->pSettings->ClipProperties.uiNbChannels;

        if( numerator % denominator == 0 )
        {
            ratioBuffer = (M4OSA_UInt32)(numerator / denominator);
        }
        else
        {
            ratioBuffer = (M4OSA_UInt32)(numerator / denominator) + 1;
        }

        ratio =
            (M4OSA_UInt32)(( pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize
            * ratioBuffer) / (pC->iSsrcNbSamplIn * sizeof(short)
            * pC->pAddedClipCtxt->pSettings->
            ClipProperties.uiNbChannels));

        if( ratio == 0 )
        {
            /* It means that the input size of SSRC bufferIn is bigger than the asked buffer */
            pC->minimumBufferIn = pC->iSsrcNbSamplIn * sizeof(short)
                * pC->pAddedClipCtxt->pSettings->
                ClipProperties.uiNbChannels;
        }
        else
        {
            ratio++; /* We use the immediate superior integer */
            pC->minimumBufferIn = ratio * (pC->iSsrcNbSamplIn * sizeof(short)
                * pC->pAddedClipCtxt->pSettings->
                ClipProperties.uiNbChannels);
        }

        /**
        * Allocate buffer for the input of the SSRC */
        pC->pSsrcBufferIn =
            (M4OSA_MemAddr8)M4OSA_32bitAlignedMalloc(pC->minimumBufferIn
            + pC->pAddedClipCtxt->
            AudioDecBufferOut.
            m_bufferSize,
            M4VSS3GPP, (M4OSA_Char *)"pSsrcBufferIn");

        if( M4OSA_NULL == pC->pSsrcBufferIn )
        {
            M4OSA_TRACE1_0(
                "M4VSS3GPP_intAudioMixingOpen():\
                unable to allocate pSsrcBufferIn, returning M4ERR_ALLOC");
            return M4ERR_ALLOC;
        }
        pC->pPosInSsrcBufferIn = (M4OSA_MemAddr8)pC->pSsrcBufferIn;

        /**
        * Allocate buffer for the output of the SSRC */
        /* The "3" value below should be optimized ... one day ... */
        pC->pSsrcBufferOut =
            (M4OSA_MemAddr8)M4OSA_32bitAlignedMalloc(3 * pC->iSsrcNbSamplOut * sizeof(short)
            * pC->ewc.uiNbChannels, M4VSS3GPP, (M4OSA_Char *)"pSsrcBufferOut");

        if( M4OSA_NULL == pC->pSsrcBufferOut )
        {
            M4OSA_TRACE1_0(
                "M4VSS3GPP_intAudioMixingOpen():\
                unable to allocate pSsrcBufferOut, returning M4ERR_ALLOC");
            return M4ERR_ALLOC;
        }
        pC->pPosInSsrcBufferOut = pC->pSsrcBufferOut;

        /**
        * Allocate temporary buffer needed in case of channel conversion */
        if( pC->ChannelConversion > 0 )
        {
            /* The "3" value below should be optimized ... one day ... */
            pC->pTempBuffer =
                (M4OSA_MemAddr8)M4OSA_32bitAlignedMalloc(3 * pC->iSsrcNbSamplOut
                * sizeof(short) * pC->pAddedClipCtxt->pSettings->
                ClipProperties.uiNbChannels, M4VSS3GPP, (M4OSA_Char *)"pSsrcBufferOut");

            if( M4OSA_NULL == pC->pTempBuffer )
            {
                M4OSA_TRACE1_0(
                    "M4VSS3GPP_intAudioMixingOpen():\
                    unable to allocate pTempBuffer, returning M4ERR_ALLOC");
                return M4ERR_ALLOC;
            }
            pC->pPosInTempBuffer = pC->pTempBuffer;
        }
    }
    else if( pC->ChannelConversion > 0 )
    {
        pC->minimumBufferIn =
            pC->pAddedClipCtxt->AudioDecBufferOut.m_bufferSize;

        /**
        * Allocate buffer for the input of the SSRC */
        pC->pSsrcBufferIn =
            (M4OSA_MemAddr8)M4OSA_32bitAlignedMalloc(pC->minimumBufferIn
            + pC->pAddedClipCtxt->
            AudioDecBufferOut.
            m_bufferSize,
            M4VSS3GPP, (M4OSA_Char *)"pSsrcBufferIn");

        if( M4OSA_NULL == pC->pSsrcBufferIn )
        {
            M4OSA_TRACE1_0(
                "M4VSS3GPP_intAudioMixingOpen(): \
                unable to allocate pSsrcBufferIn, returning M4ERR_ALLOC");
            return M4ERR_ALLOC;
        }
        pC->pPosInSsrcBufferIn = (M4OSA_MemAddr8)pC->pSsrcBufferIn;

        /**
        * Allocate buffer for the output of the SSRC */
        /* The "3" value below should be optimized ... one day ... */
        pC->pSsrcBufferOut = (M4OSA_MemAddr8)M4OSA_32bitAlignedMalloc(
            pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize,
            M4VSS3GPP, (M4OSA_Char *)"pSsrcBufferOut");

        if( M4OSA_NULL == pC->pSsrcBufferOut )
        {
            M4OSA_TRACE1_0(
                "M4VSS3GPP_intAudioMixingOpen():\
                unable to allocate pSsrcBufferOut, returning M4ERR_ALLOC");
            return M4ERR_ALLOC;
        }
        pC->pPosInSsrcBufferOut = pC->pSsrcBufferOut;
    }
    else if( (pC->pAddedClipCtxt->pSettings->ClipProperties.AudioStreamType == M4VIDEOEDITING_kMP3)||
         (pC->pAddedClipCtxt->pSettings->ClipProperties.AudioStreamType == M4VIDEOEDITING_kPCM))
    {
        M4OSA_UInt32 minbuffer = 0;

        if( pSettings->outputAudioFormat == M4VIDEOEDITING_kAAC )
        {
            pC->minimumBufferIn = 2048 * pC->ewc.uiNbChannels;
            minbuffer = pC->minimumBufferIn;
        }
        else if( pSettings->outputAudioFormat == M4VIDEOEDITING_kAMR_NB )
        {
            pC->minimumBufferIn = 320;

            if( pC->pAddedClipCtxt->AudioDecBufferOut.m_bufferSize > 320 )
            {
                minbuffer = pC->pAddedClipCtxt->AudioDecBufferOut.m_bufferSize;
            }
            else
            {
                minbuffer = pC->minimumBufferIn; /* Not really possible ...*/
            }
        }
        else
        {
            M4OSA_TRACE1_0("Bad output audio format, in case of MP3 replacing");
            return M4ERR_PARAMETER;
        }

        /**
        * Allocate buffer for the input of the SSRC */
        pC->pSsrcBufferIn =
            (M4OSA_MemAddr8)M4OSA_32bitAlignedMalloc(2 * minbuffer, M4VSS3GPP,
            (M4OSA_Char *)"pSsrcBufferIn");

        if( M4OSA_NULL == pC->pSsrcBufferIn )
        {
            M4OSA_TRACE1_0(
                "M4VSS3GPP_intAudioMixingOpen(): unable to allocate pSsrcBufferIn,\
                returning M4ERR_ALLOC");
            return M4ERR_ALLOC;
        }
        pC->pPosInSsrcBufferIn = (M4OSA_MemAddr8)pC->pSsrcBufferIn;

        pC->pPosInSsrcBufferOut = pC->pPosInSsrcBufferIn;
        pC->pSsrcBufferOut = pC->pSsrcBufferIn;
    }

    /**
    * Check if audio encoder is needed to do audio mixing or audio resampling */
    if( M4OSA_TRUE == pC->bAudioMixingIsNeeded || M4VIDEOEDITING_kPCM
        == pC->pAddedClipCtxt->pSettings->ClipProperties.AudioStreamType
        || M4VIDEOEDITING_kMP3
        == pC->pAddedClipCtxt->pSettings->ClipProperties.AudioStreamType
        || pC->pAddedClipCtxt->pSettings->ClipProperties.AudioStreamType
        != pSettings->outputAudioFormat
        || pC->pAddedClipCtxt->pSettings->ClipProperties.uiSamplingFrequency
        != outputASF
        || pC->pAddedClipCtxt->pSettings->ClipProperties.uiNbChannels
        != pSettings->outputNBChannels )
    {
        /**
        * Init the audio encoder */
        err = M4VSS3GPP_intCreateAudioEncoder(&pC->ewc, &pC->ShellAPI,
            pC->ewc.uiAudioBitrate);

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

        /* In case of PCM, MP3 or audio replace with reencoding, use encoder DSI */
        if( pC->ewc.uiAudioOutputDsiSize == 0 && (M4VIDEOEDITING_kPCM
            == pC->pAddedClipCtxt->pSettings->ClipProperties.AudioStreamType
            || M4VIDEOEDITING_kMP3 == pC->pAddedClipCtxt->pSettings->
            ClipProperties.AudioStreamType
            || pC->pAddedClipCtxt->pSettings->
            ClipProperties.AudioStreamType
            != pSettings->outputAudioFormat
            || pC->pAddedClipCtxt->pSettings->
            ClipProperties.uiSamplingFrequency != outputASF
            || pC->pAddedClipCtxt->pSettings->
            ClipProperties.uiNbChannels
            != pSettings->outputNBChannels) )
        {
            pC->ewc.uiAudioOutputDsiSize =
                (M4OSA_UInt16)pC->ewc.pAudioEncDSI.infoSize;
            pC->ewc.pAudioOutputDsi = pC->ewc.pAudioEncDSI.pInfo;
        }
    }

    /**
    * Init the output 3GPP file */
    /*11/12/2008 CR3283 add the max output file size for the MMS use case in VideoArtist*/
    err = M4VSS3GPP_intCreate3GPPOutputFile(&pC->ewc, &pC->ShellAPI,
        pC->pOsaFileWritPtr, pSettings->pOutputClipFile,
        pC->pOsaFileReadPtr, pSettings->pTemporaryFile, 0);

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

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

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intAudioMixingWriteSilence()
 * @brief    Write an audio silence frame into the writer
 * @note    Mainly used when padding with silence
 * @param    pC            (IN) VSS audio mixing internal context
 * @return    M4NO_ERROR:    No error
 ******************************************************************************
 */
static M4OSA_ERR M4VSS3GPP_intAudioMixingWriteSilence(
    M4VSS3GPP_InternalAudioMixingContext *pC )
{
    M4OSA_ERR err;

    err = pC->ShellAPI.pWriterDataFcts->pStartAU(pC->ewc.p3gpWriterContext,
        M4VSS3GPP_WRITER_AUDIO_STREAM_ID, &pC->ewc.WriterAudioAU);

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

    M4OSA_TRACE2_0("A #### silence AU");

    memcpy((void *)pC->ewc.WriterAudioAU.dataAddress,
        (void *)pC->ewc.pSilenceFrameData, pC->ewc.uiSilenceFrameSize);

    pC->ewc.WriterAudioAU.size = pC->ewc.uiSilenceFrameSize;
    pC->ewc.WriterAudioAU.CTS =
        (M4OSA_Time)(pC->ewc.dATo * pC->ewc.scale_audio + 0.5);

    M4OSA_TRACE2_2("B ---- write : cts  = %ld [ 0x%x ]",
        (M4OSA_Int32)(pC->ewc.dATo), pC->ewc.WriterAudioAU.size);

    err = pC->ShellAPI.pWriterDataFcts->pProcessAU(pC->ewc.p3gpWriterContext,
        M4VSS3GPP_WRITER_AUDIO_STREAM_ID, &pC->ewc.WriterAudioAU);

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

    pC->ewc.dATo += pC->ewc.iSilenceFrameDuration / pC->ewc.scale_audio;

    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intAudioMixingStepVideo(M4VSS3GPP_InternalAudioMixingContext *pC)
 * @brief    Perform one step of video.
 * @note
 * @param    pC            (IN) VSS audio mixing internal context
 * @return    M4NO_ERROR:    No error
 ******************************************************************************
 */
static M4OSA_ERR M4VSS3GPP_intAudioMixingStepVideo(
    M4VSS3GPP_InternalAudioMixingContext *pC )
{
    M4OSA_ERR err;
    M4OSA_UInt16 offset;

    M4OSA_TRACE2_3("  VIDEO step : dVTo = %f  state = %d  offset = %ld",
        pC->ewc.dOutputVidCts, pC->State, pC->pInputClipCtxt->iVoffset);

    /**
    * Read the input video AU */
    err = pC->pInputClipCtxt->ShellAPI.m_pReaderDataIt->m_pFctGetNextAu(
        pC->pInputClipCtxt->pReaderContext,
        (M4_StreamHandler *)pC->pInputClipCtxt->pVideoStream,
        &pC->pInputClipCtxt->VideoAU);

    if( M4NO_ERROR != err )
    {
        M4OSA_TRACE3_1(
            "M4VSS3GPP_intAudioMixingStepVideo(): m_pFctGetNextAu(video) returns 0x%x",
            err);
        return err;
    }

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

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

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

    offset = 0;
    /* for h.264 stream do not read the 1st 4 bytes as they are header indicators */
    if( pC->pInputClipCtxt->pVideoStream->m_basicProperties.m_streamType
        == M4DA_StreamTypeVideoMpeg4Avc )
    {
        M4OSA_TRACE3_0(
            "M4VSS3GPP_intAudioMixingStepVideo(): input stream type H264");
        offset = 4;
    }
    pC->pInputClipCtxt->VideoAU.m_size  -=  offset;
    /**
    * Check that the video AU is not larger than expected */
    if( pC->pInputClipCtxt->VideoAU.m_size > pC->ewc.uiVideoMaxAuSize )
    {
        M4OSA_TRACE1_2(
            "M4VSS3GPP_intAudioMixingStepVideo: AU size greater than MaxAuSize (%d>%d)!\
            returning M4VSS3GPP_ERR_INPUT_VIDEO_AU_TOO_LARGE",
            pC->pInputClipCtxt->VideoAU.m_size, pC->ewc.uiVideoMaxAuSize);
        return M4VSS3GPP_ERR_INPUT_VIDEO_AU_TOO_LARGE;
    }

    /**
    * Copy the input AU payload to the output AU */
    memcpy((void *)pC->ewc.WriterVideoAU.dataAddress,
        (void *)(pC->pInputClipCtxt->VideoAU.m_dataAddress + offset),
        (pC->pInputClipCtxt->VideoAU.m_size));

    /**
    * Copy the input AU parameters to the output AU */
    pC->ewc.WriterVideoAU.size = pC->pInputClipCtxt->VideoAU.m_size;
    pC->ewc.WriterVideoAU.CTS =
        (M4OSA_UInt32)(pC->pInputClipCtxt->VideoAU.m_CTS + 0.5);
    pC->ewc.WriterVideoAU.attribute = pC->pInputClipCtxt->VideoAU.m_attribute;

    /**
    * Write the AU */
    M4OSA_TRACE2_2("D ---- write : cts  = %lu [ 0x%x ]",
        pC->ewc.WriterVideoAU.CTS, pC->ewc.WriterVideoAU.size);

    err = pC->ShellAPI.pWriterDataFcts->pProcessAU(pC->ewc.p3gpWriterContext,
        M4VSS3GPP_WRITER_VIDEO_STREAM_ID, &pC->ewc.WriterVideoAU);

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

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

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intAudioMixingStepAudioMix(M4VSS3GPP_InternalAudioMixingContext *pC)
 * @brief    Perform one step of audio.
 * @note
 * @param    pC            (IN) VSS audio mixing internal context
 * @return    M4NO_ERROR:    No error
 ******************************************************************************
 */
static M4OSA_ERR M4VSS3GPP_intAudioMixingStepAudioMix(
    M4VSS3GPP_InternalAudioMixingContext *pC )
{
    M4OSA_ERR err;

    M4OSA_TRACE2_3("  AUDIO mix  : dATo = %f  state = %d  offset = %ld",
        pC->ewc.dATo, pC->State, pC->pInputClipCtxt->iAoffset);

    switch( pC->State )
    {
        /**********************************************************/
        case M4VSS3GPP_kAudioMixingState_AUDIO_FIRST_SEGMENT:
            {
                err = M4VSS3GPP_intAudioMixingCopyOrig(pC);

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

                /**
                * Check if we reached the AddCts */
                if( pC->ewc.dATo >= pC->iAddCts )
                {
                    /**
                    * First segment is over, state transition to second and return OK */
                    pC->State = M4VSS3GPP_kAudioMixingState_AUDIO_SECOND_SEGMENT;

                    /* Transition from reading state to encoding state */
                    err = M4VSS3GPP_intAudioMixingTransition(pC);

                    if( M4NO_ERROR != err )
                    {
                        M4OSA_TRACE1_1(
                            "M4VSS3GPP_intAudioMixingStepAudioMix(): pre-encode fails err = 0x%x",
                            err);
                        return err;
                    }

                    /**
                    * Return with no error so the step function will be called again */
                    pC->pAddedClipCtxt->iAoffset =
                        (M4OSA_Int32)(pC->ewc.dATo * pC->ewc.scale_audio + 0.5);

                    M4OSA_TRACE2_0(
                        "M4VSS3GPP_intAudioMixingStepAudioMix(): returning M4NO_ERROR (1->2)");

                    return M4NO_ERROR;
                }
            }
            break;

            /**********************************************************/
        case M4VSS3GPP_kAudioMixingState_AUDIO_SECOND_SEGMENT:
            {
                if( M4OSA_TRUE == pC->bAudioMixingIsNeeded ) /**< Mix */
                {
                    /**
                    * Read the added audio AU */
                    if( pC->ChannelConversion > 0 || pC->b_SSRCneeded == M4OSA_TRUE
                        || pC->pAddedClipCtxt->pSettings->
                        ClipProperties.AudioStreamType == M4VIDEOEDITING_kMP3 )
                    {
                        /* In case of sampling freq conversion and/or channel conversion,
                           the read next AU will be    called by the
                           M4VSS3GPP_intAudioMixingDoMixing function */
                    }
                    else
                    {
                        err =
                            M4VSS3GPP_intClipReadNextAudioFrame(pC->pAddedClipCtxt);

                        M4OSA_TRACE2_3("E .... read  : cts  = %.0f + %.0f [ 0x%x ]",
                            pC->pAddedClipCtxt->iAudioFrameCts
                            / pC->pAddedClipCtxt->scale_audio,
                            pC->pAddedClipCtxt->iAoffset
                            / pC->pAddedClipCtxt->scale_audio,
                            pC->pAddedClipCtxt->uiAudioFrameSize);

                        if( M4WAR_NO_MORE_AU == err )
                        {
                            /**
                            * Decide what to do when audio is over */
                            if( pC->uiEndLoop > 0 )
                            {
                                /**
                                * Jump at the Begin loop time */
                                M4OSA_Int32 time = (M4OSA_Int32)(pC->uiBeginLoop);

                                err = pC->pAddedClipCtxt->ShellAPI.m_pReader->
                                    m_pFctJump(
                                    pC->pAddedClipCtxt->pReaderContext,
                                    (M4_StreamHandler
                                    *)pC->pAddedClipCtxt->pAudioStream,
                                    &time);

                                if( M4NO_ERROR != err )
                                {
                                    M4OSA_TRACE1_1(
                                        "M4VSS3GPP_intAudioMixingStepAudioMix():\
                                        m_pReader->m_pFctJump(audio returns 0x%x",
                                        err);
                                    return err;
                                }
                            }
                            else
                            {
                                /* Transition from encoding state to reading state */
                                err = M4VSS3GPP_intAudioMixingTransition(pC);

                                if( M4NO_ERROR != err )
                                {
                                    M4OSA_TRACE1_1(
                                        "M4VSS3GPP_intAudioMixingStepAudioMix():\
                                        pre-encode fails err = 0x%x",
                                        err);
                                    return err;
                                }

                                /**
                                * Second segment is over, state transition to third and
                                 return OK */
                                pC->State =
                                    M4VSS3GPP_kAudioMixingState_AUDIO_THIRD_SEGMENT;

                                /**
                                * Return with no error so the step function will be
                                 called again */
                                M4OSA_TRACE2_0(
                                    "M4VSS3GPP_intAudioMixingStepAudioMix():\
                                    returning M4NO_ERROR (2->3) a");
                                return M4NO_ERROR;
                            }
                        }
                        else if( M4NO_ERROR != err )
                        {
                            M4OSA_TRACE1_1(
                                "M4VSS3GPP_intAudioMixingStepAudioMix():\
                                m_pFctGetNextAu(audio) returns 0x%x",
                                err);
                            return err;
                        }
                    }

                    /**
                    * Read the original audio AU */
                    err = M4VSS3GPP_intClipReadNextAudioFrame(pC->pInputClipCtxt);

                    M4OSA_TRACE2_3("F .... read  : cts  = %.0f + %.0f [ 0x%x ]",
                        pC->pInputClipCtxt->iAudioFrameCts
                        / pC->pInputClipCtxt->scale_audio,
                        pC->pInputClipCtxt->iAoffset
                        / pC->pInputClipCtxt->scale_audio,
                        pC->pInputClipCtxt->uiAudioFrameSize);

                    if( M4NO_ERROR != err )
                    {
                        M4OSA_TRACE3_1(
                            "M4VSS3GPP_intAudioMixingStepAudioMix():\
                            m_pFctGetNextAu(audio) returns 0x%x",
                            err);
                        return err;
                    }

                    if( pC->ChannelConversion == 0
                        && pC->b_SSRCneeded == M4OSA_FALSE
                        && pC->pAddedClipCtxt->pSettings->
                        ClipProperties.AudioStreamType != M4VIDEOEDITING_kMP3 )
                    {
                        /**
                        * Get the output AU to write into */
                        err = pC->ShellAPI.pWriterDataFcts->pStartAU(
                            pC->ewc.p3gpWriterContext,
                            M4VSS3GPP_WRITER_AUDIO_STREAM_ID,
                            &pC->ewc.WriterAudioAU);

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

                    /**
                    * Perform the audio mixing */
                    err = M4VSS3GPP_intAudioMixingDoMixing(pC);

                    if( err == M4VSS3GPP_WAR_END_OF_ADDED_AUDIO )
                    {
                        return M4NO_ERROR;
                    }

                    if( M4NO_ERROR != err )
                    {
                        M4OSA_TRACE1_1(
                            "M4VSS3GPP_intAudioMixingStepAudioMix:\
                            M4VSS3GPP_intAudioMixingDoMixing returns 0x%x!",
                            err);
                        return err;
                    }
                }
                else /**< No mix, just copy added audio */
                {
                    err = M4VSS3GPP_intAudioMixingCopyAdded(pC);

                    if( M4WAR_NO_MORE_AU == err )
                    {
                        /**
                        * Decide what to do when audio is over */
                        if( pC->uiEndLoop > 0 )
                        {
                            /**
                            * Jump at the Begin loop time */
                            M4OSA_Int32 time = (M4OSA_Int32)(pC->uiBeginLoop);

                            err =
                                pC->pAddedClipCtxt->ShellAPI.m_pReader->m_pFctJump(
                                pC->pAddedClipCtxt->pReaderContext,
                                (M4_StreamHandler
                                *)pC->pAddedClipCtxt->pAudioStream,
                                &time);

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

                            /**
                            * 'BZZZ' bug fix:
                            * add a silence frame */
                            err = M4VSS3GPP_intAudioMixingWriteSilence(pC);

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

                            /**
                            * Return with no error so the step function will be called again to
                              read audio data */
                            pC->pAddedClipCtxt->iAoffset =
                                (M4OSA_Int32)(pC->ewc.dATo * pC->ewc.scale_audio
                                + 0.5);

                            M4OSA_TRACE2_0(
                                "M4VSS3GPP_intAudioMixingStepAudioMix():\
                                    returning M4NO_ERROR (loop)");
                            return M4NO_ERROR;
                        }
                        else
                        {
                            /* Transition to begin cut */
                            err = M4VSS3GPP_intAudioMixingTransition(pC);

                            if( M4NO_ERROR != err )
                            {
                                M4OSA_TRACE1_1(
                                    "M4VSS3GPP_intAudioMixingStepAudioMix():\
                                    pre-encode fails err = 0x%x",
                                    err);
                                return err;
                            }

                            /**
                            * Second segment is over, state transition to third */
                            pC->State =
                                M4VSS3GPP_kAudioMixingState_AUDIO_THIRD_SEGMENT;

                            /**
                            * Return with no error so the step function will be called again */
                            M4OSA_TRACE2_0(
                                "M4VSS3GPP_intAudioMixingStepAudioMix():\
                                returning M4NO_ERROR (2->3) b");
                            return M4NO_ERROR;
                        }
                    }
                    else if( M4NO_ERROR != err )
                    {
                        M4OSA_TRACE1_1(
                            "M4VSS3GPP_intAudioMixingStepAudioMix():\
                            M4VSS3GPP_intAudioMixingCopyOrig(2) returns 0x%x",
                            err);
                        return err;
                    }
                }

                /**
                * Check if we reached the end of the video */
                if( pC->ewc.dATo >= pC->ewc.iOutputDuration )
                {
                    M4OSA_TRACE3_0(
                        "M4VSS3GPP_intAudioMixingStepAudioMix(): Video duration reached,\
                        returning M4WAR_NO_MORE_AU");
                    return M4WAR_NO_MORE_AU; /**< Simulate end of file error */
                }
            }
            break;

            /**********************************************************/
        case M4VSS3GPP_kAudioMixingState_AUDIO_THIRD_SEGMENT:
            {
                err = M4VSS3GPP_intAudioMixingCopyOrig(pC);

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

                /**
                * Check if we reached the end of the video */
                if( pC->ewc.dATo >= pC->ewc.iOutputDuration )
                {
                    M4OSA_TRACE3_0(
                        "M4VSS3GPP_intAudioMixingStepAudioMix():\
                        Video duration reached, returning M4WAR_NO_MORE_AU");
                    return M4WAR_NO_MORE_AU; /**< Simulate end of file error */
                }
            }
            break;
       default:
            break;
    }

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

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intAudioMixingStepAudioReplace(M4VSS3GPP_InternalAudioMixingContext *pC)
 * @brief    Perform one step of audio.
 * @note
 * @param    pC            (IN) VSS audio mixing internal context
 * @return    M4NO_ERROR:    No error
 ******************************************************************************
 */
static M4OSA_ERR M4VSS3GPP_intAudioMixingStepAudioReplace(
    M4VSS3GPP_InternalAudioMixingContext *pC )
{
    M4OSA_ERR err;

    M4OSA_TRACE2_3("  AUDIO repl : dATo = %f  state = %d  offset = %ld",
        pC->ewc.dATo, pC->State, pC->pInputClipCtxt->iAoffset);

    switch( pC->State )
    {
        /**********************************************************/
        case M4VSS3GPP_kAudioMixingState_AUDIO_FIRST_SEGMENT:
            {
                /**
                * Replace the SID (silence) payload in the writer AU */
                err = M4VSS3GPP_intAudioMixingWriteSilence(pC);

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

                /**
                * Check if we reached the AddCts */
                if( pC->ewc.dATo >= pC->iAddCts )
                {
                    /**
                    * First segment is over, state transition to second and return OK */
                    pC->State = M4VSS3GPP_kAudioMixingState_AUDIO_SECOND_SEGMENT;

                    /**
                    * Return with no error so the step function will be called again */
                    pC->pAddedClipCtxt->iAoffset =
                        (M4OSA_Int32)(pC->ewc.dATo * pC->ewc.scale_audio + 0.5);

                    M4OSA_TRACE2_0("M4VSS3GPP_intAudioMixingStepAudioReplace():\
                         returning M4NO_ERROR (1->2)");
                    return M4NO_ERROR;
                }
            }
            break;

            /**********************************************************/
        case M4VSS3GPP_kAudioMixingState_AUDIO_SECOND_SEGMENT:
            {
                err = M4VSS3GPP_intAudioMixingCopyAdded(pC);

                if( M4WAR_NO_MORE_AU == err )
                {
                    /**
                    * Decide what to do when audio is over */

                    if( pC->uiEndLoop > 0 )
                    {
                        /**
                        * Jump at the Begin loop time */
                        M4OSA_Int32 time = (M4OSA_Int32)(pC->uiBeginLoop);

                        err = pC->pAddedClipCtxt->ShellAPI.m_pReader->m_pFctJump(
                            pC->pAddedClipCtxt->pReaderContext,
                            (M4_StreamHandler
                            *)pC->pAddedClipCtxt->pAudioStream, &time);

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

                        /**
                        * 'BZZZ' bug fix:
                        * add a silence frame */
                        err = M4VSS3GPP_intAudioMixingWriteSilence(pC);

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

                        /**
                        * Return with no error so the step function will be called again to
                          read audio data */
                        pC->pAddedClipCtxt->iAoffset =
                            (M4OSA_Int32)(pC->ewc.dATo * pC->ewc.scale_audio + 0.5);

                        M4OSA_TRACE2_0(
                            "M4VSS3GPP_intAudioMixingStepAudioReplace():\
                            returning M4NO_ERROR (loop)");

                        return M4NO_ERROR;
                    }
                    else if( M4OSA_TRUE == pC->bSupportSilence )
                    {
                        /**
                        * Second segment is over, state transition to third and return OK */
                        pC->State = M4VSS3GPP_kAudioMixingState_AUDIO_THIRD_SEGMENT;

                        /**
                        * Return with no error so the step function will be called again */
                        M4OSA_TRACE2_0(
                            "M4VSS3GPP_intAudioMixingStepAudioReplace():\
                                 returning M4NO_ERROR (2->3)");
                        return M4NO_ERROR;
                    }
                    else
                    {
                        /**
                        * The third segment (silence) is only done if supported.
                        * In other case, we finish here. */
                        pC->State = M4VSS3GPP_kAudioMixingState_FINISHED;

                        /**
                        * Return with no error so the step function will be called again */
                        M4OSA_TRACE2_0(
                            "M4VSS3GPP_intAudioMixingStepAudioReplace():\
                                 returning M4NO_ERROR (2->F)");
                        return M4NO_ERROR;
                    }
                }
                else if( M4NO_ERROR != err )
                {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_intAudioMixingStepAudioReplace():\
                        M4VSS3GPP_intAudioMixingCopyOrig(2) returns 0x%x",
                        err);
                    return err;
                }

                /**
                * Check if we reached the end of the clip */
                if( pC->ewc.dATo >= pC->ewc.iOutputDuration )
                {
                    M4OSA_TRACE3_0(
                        "M4VSS3GPP_intAudioMixingStepAudioReplace(): Clip duration reached,\
                        returning M4WAR_NO_MORE_AU");
                    return M4WAR_NO_MORE_AU; /**< Simulate end of file error */
                }
            }
            break;

            /**********************************************************/
        case M4VSS3GPP_kAudioMixingState_AUDIO_THIRD_SEGMENT:
            {
                /**
                * Replace the SID (silence) payload in the writer AU */
                err = M4VSS3GPP_intAudioMixingWriteSilence(pC);

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

                /**
                * Check if we reached the end of the video */
                if( pC->ewc.dATo >= pC->ewc.iOutputDuration )
                {
                    M4OSA_TRACE3_0(
                        "M4VSS3GPP_intAudioMixingStepAudioReplace():\
                        Video duration reached, returning M4WAR_NO_MORE_AU");
                    return M4WAR_NO_MORE_AU; /**< Simulate end of file error */
                }
            }
            break;
        default:
            break;
    }

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

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intAudioMixingCopyOrig(M4VSS3GPP_InternalAudioMixingContext *pC)
 * @brief    Read one AU from the original audio file and write it to the output
 * @note
 * @param    pC    (IN) VSS audio mixing internal context
 ******************************************************************************
 */
static M4OSA_ERR M4VSS3GPP_intAudioMixingCopyOrig(
    M4VSS3GPP_InternalAudioMixingContext *pC )
{
    M4OSA_ERR err;

    /**
    * Read the input original audio AU */
    err = M4VSS3GPP_intClipReadNextAudioFrame(pC->pInputClipCtxt);

    M4OSA_TRACE2_3("G .... read  : cts  = %.0f + %.0f [ 0x%x ]",
        pC->pInputClipCtxt->iAudioFrameCts / pC->pInputClipCtxt->scale_audio,
        pC->pInputClipCtxt->iAoffset / pC->pInputClipCtxt->scale_audio,
        pC->pInputClipCtxt->uiAudioFrameSize);

    if( M4NO_ERROR != err )
    {
        M4OSA_TRACE3_1(
            "M4VSS3GPP_intAudioMixingCopyOrig(): m_pFctGetNextAu(audio) returns 0x%x",
            err);
        return err;
    }

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

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

    /**
    * Copy the input AU properties to the output AU */
    pC->ewc.WriterAudioAU.size = pC->pInputClipCtxt->uiAudioFrameSize;
    pC->ewc.WriterAudioAU.CTS =
        pC->pInputClipCtxt->iAudioFrameCts + pC->pInputClipCtxt->iAoffset;

    /**
    * Copy the AU itself */
    memcpy((void *)pC->ewc.WriterAudioAU.dataAddress,
        (void *)pC->pInputClipCtxt->pAudioFramePtr, pC->ewc.WriterAudioAU.size);

    /**
    * Write the mixed AU */
    M4OSA_TRACE2_2("H ---- write : cts  = %ld [ 0x%x ]",
        (M4OSA_Int32)(pC->ewc.WriterAudioAU.CTS / pC->ewc.scale_audio),
        pC->ewc.WriterAudioAU.size);

    err = pC->ShellAPI.pWriterDataFcts->pProcessAU(pC->ewc.p3gpWriterContext,
        M4VSS3GPP_WRITER_AUDIO_STREAM_ID, &pC->ewc.WriterAudioAU);

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

    /**
    * Increment the audio CTS for the next step */
    pC->ewc.dATo += pC->ewc.iSilenceFrameDuration / pC->ewc.scale_audio;

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

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intAudioMixingCopyAdded(M4VSS3GPP_InternalAudioMixingContext *pC)
 * @brief    Read one AU from the added audio file and write it to the output
 * @note
 * @param    pC    (IN) VSS audio mixing internal context
 ******************************************************************************
 */
static M4OSA_ERR M4VSS3GPP_intAudioMixingCopyAdded(
    M4VSS3GPP_InternalAudioMixingContext *pC )
{
    M4OSA_ERR err;

    if(pC->pAddedClipCtxt->pSettings->ClipProperties.AudioStreamType == M4VIDEOEDITING_kMP3 ||
        pC->pAddedClipCtxt->pSettings->ClipProperties.AudioStreamType == M4VIDEOEDITING_kPCM ||
        pC->b_SSRCneeded == M4OSA_TRUE ||
        pC->ChannelConversion > 0)
    {
        M4ENCODER_AudioBuffer pEncInBuffer; /**< Encoder input buffer for api */
        M4ENCODER_AudioBuffer
            pEncOutBuffer; /**< Encoder output buffer for api */
        M4OSA_Time
            frameTimeDelta; /**< Duration of the encoded (then written) data */
        M4OSA_MemAddr8 tempPosBuffer;

        err = M4VSS3GPP_intAudioMixingConvert(pC);

        if( err == M4VSS3GPP_WAR_END_OF_ADDED_AUDIO )
        {
            M4OSA_TRACE2_0(
                "M4VSS3GPP_intAudioMixingCopyAdded:\
                M4VSS3GPP_intAudioMixingConvert end of added file");
            return M4NO_ERROR;
        }
        else if( err != M4NO_ERROR )
        {
            M4OSA_TRACE1_1("M4VSS3GPP_intAudioMixingCopyAdded:\
                M4VSS3GPP_intAudioMixingConvert returned 0x%x", err);
            return err;
        }

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

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

        /* [Mono] or [Stereo interleaved] : all is in one buffer */
        pEncInBuffer.pTableBuffer[0] = pC->pSsrcBufferOut;
        pEncInBuffer.pTableBufferSize[0] =
            pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize;
        pEncInBuffer.pTableBuffer[1] = M4OSA_NULL;
        pEncInBuffer.pTableBufferSize[1] = 0;

        /* Time in ms from data size, because it is PCM16 samples */
        frameTimeDelta = pEncInBuffer.pTableBufferSize[0] / sizeof(short)
            / pC->ewc.uiNbChannels;

        /**
        * Prepare output buffer */
        pEncOutBuffer.pTableBuffer[0] =
            (M4OSA_MemAddr8)pC->ewc.WriterAudioAU.dataAddress;
        pEncOutBuffer.pTableBufferSize[0] = 0;

        M4OSA_TRACE2_0("K **** blend AUs");
        /**
        * Encode the PCM audio */

        err = pC->ShellAPI.pAudioEncoderGlobalFcts->pFctStep(
            pC->ewc.pAudioEncCtxt, &pEncInBuffer, &pEncOutBuffer);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intAudioMixingDoMixing():\
                pAudioEncoderGlobalFcts->pFctStep returns 0x%x",
                err);
            return err;
        }

        /**
        * Set AU cts and size */
        pC->ewc.WriterAudioAU.size =
            pEncOutBuffer.
            pTableBufferSize[0]; /**< Get the size of encoded data */
        pC->ewc.WriterAudioAU.CTS += frameTimeDelta;

        /* Update decoded buffer here */
        if( M4OSA_TRUE == pC->b_SSRCneeded || pC->ChannelConversion > 0 )
        {
            tempPosBuffer = pC->pSsrcBufferOut
                + pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize;
            memmove((void *)pC->pSsrcBufferOut, (void *)tempPosBuffer,
                pC->pPosInSsrcBufferOut - tempPosBuffer);
            pC->pPosInSsrcBufferOut -=
                pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize;
        }
        else
        {
            tempPosBuffer = pC->pSsrcBufferIn + pC->minimumBufferIn;
            memmove((void *)pC->pSsrcBufferIn, (void *)tempPosBuffer,
                pC->pPosInSsrcBufferIn - tempPosBuffer);
            pC->pPosInSsrcBufferIn -= pC->minimumBufferIn;
        }

        /**
        * Write the mixed AU */
        M4OSA_TRACE2_2("J ---- write : cts  = %ld [ 0x%x ]",
            (M4OSA_Int32)(pC->ewc.WriterAudioAU.CTS / pC->ewc.scale_audio),
            pC->ewc.WriterAudioAU.size);

        err =
            pC->ShellAPI.pWriterDataFcts->pProcessAU(pC->ewc.p3gpWriterContext,
            M4VSS3GPP_WRITER_AUDIO_STREAM_ID, &pC->ewc.WriterAudioAU);

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

        /**
        * Increment the audio CTS for the next step */
        pC->ewc.dATo += frameTimeDelta / pC->ewc.scale_audio;
    }
    else
    {
        /**
        * Read the added audio AU */
        err = M4VSS3GPP_intClipReadNextAudioFrame(pC->pAddedClipCtxt);

        M4OSA_TRACE2_3("I .... read  : cts  = %.0f + %.0f [ 0x%x ]",
            pC->pAddedClipCtxt->iAudioFrameCts
            / pC->pAddedClipCtxt->scale_audio,
            pC->pAddedClipCtxt->iAoffset / pC->pAddedClipCtxt->scale_audio,
            pC->pAddedClipCtxt->uiAudioFrameSize);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE3_1(
                "M4VSS3GPP_intAudioMixingCopyAdded(): m_pFctGetNextAu(audio) returns 0x%x",
                err);
            return err;
        }

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

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

        /**
        * Copy the input AU properties to the output AU */

        /** THE CHECK BELOW IS ADDED TO PREVENT ISSUES LINKED TO PRE-ALLOCATED MAX AU SIZE
        max AU size is set based on M4VSS3GPP_AUDIO_MAX_AU_SIZE defined in file
        M4VSS3GPP_InternalConfig.h, If this error occurs increase the limit set in this file
        */
        if( pC->pAddedClipCtxt->uiAudioFrameSize > pC->ewc.WriterAudioAU.size )
        {
            M4OSA_TRACE1_2(
                "ERROR: audio AU size (%d) to copy larger than allocated one (%d) => abort",
                pC->pAddedClipCtxt->uiAudioFrameSize,
                pC->ewc.WriterAudioAU.size);
            M4OSA_TRACE1_0(
                "PLEASE CONTACT SUPPORT TO EXTEND MAX AU SIZE IN THE PRODUCT LIBRARY");
            err = M4ERR_UNSUPPORTED_MEDIA_TYPE;
            return err;
        }
        pC->ewc.WriterAudioAU.size = pC->pAddedClipCtxt->uiAudioFrameSize;
        pC->ewc.WriterAudioAU.CTS =
            pC->pAddedClipCtxt->iAudioFrameCts + pC->pAddedClipCtxt->iAoffset;

        /**
        * Copy the AU itself */
        memcpy((void *)pC->ewc.WriterAudioAU.dataAddress,
            (void *)pC->pAddedClipCtxt->pAudioFramePtr, pC->ewc.WriterAudioAU.size);

        /**
        * Write the mixed AU */
        M4OSA_TRACE2_2("J ---- write : cts  = %ld [ 0x%x ]",
            (M4OSA_Int32)(pC->ewc.WriterAudioAU.CTS / pC->ewc.scale_audio),
            pC->ewc.WriterAudioAU.size);

        err =
            pC->ShellAPI.pWriterDataFcts->pProcessAU(pC->ewc.p3gpWriterContext,
            M4VSS3GPP_WRITER_AUDIO_STREAM_ID, &pC->ewc.WriterAudioAU);

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

        /**
        * Increment the audio CTS for the next step */
        pC->ewc.dATo += pC->ewc.iSilenceFrameDuration / pC->ewc.scale_audio;
    }

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

/**
 ******************************************************************************
 * M4OSA_ERR  M4VSS3GPP_intAudioMixingConvert(M4VSS3GPP_InternalAudioMixingContext *pC)
 * @brief    Convert PCM of added track to the right ASF / nb of Channels
 * @note
 * @param    pC    (IN) VSS audio mixing internal context
 * @return    M4NO_ERROR:    No error
 ******************************************************************************
 */
static M4OSA_ERR M4VSS3GPP_intAudioMixingConvert(
    M4VSS3GPP_InternalAudioMixingContext *pC )
{
    M4OSA_ERR err;
    int ssrcErr; /**< Error while ssrc processing */
    M4OSA_UInt32 uiChannelConvertorNbSamples =
        pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize / sizeof(short)
        / pC->pInputClipCtxt->pSettings->ClipProperties.uiNbChannels;
    M4OSA_MemAddr8 tempPosBuffer;

    M4OSA_UInt32 outFrameCount = uiChannelConvertorNbSamples;
    /* Do we need to feed SSRC buffer In ? */
    /**
    * RC: This is not really optimum (memmove). We should handle this with linked list. */
    while( pC->pPosInSsrcBufferIn - pC->pSsrcBufferIn < (M4OSA_Int32)pC->minimumBufferIn )
    {
        /* We need to get more PCM data */
        if (pC->bNoLooping == M4OSA_TRUE)
        {
            err = M4WAR_NO_MORE_AU;
        }
        else
        {
        err = M4VSS3GPP_intClipReadNextAudioFrame(pC->pAddedClipCtxt);
        }
        if(pC->bjumpflag)
        {
        /**
            * Jump at the Begin loop time */
            M4OSA_Int32 time = (M4OSA_Int32)(pC->uiBeginLoop);

            err =
                pC->pAddedClipCtxt->ShellAPI.m_pReader->m_pFctJump\
                    (pC->pAddedClipCtxt->pReaderContext,
                     (M4_StreamHandler*)pC->pAddedClipCtxt->pAudioStream, &time);
            if (M4NO_ERROR != err)
            {
                M4OSA_TRACE1_1("M4VSS3GPP_intAudioMixingConvert():\
                     m_pReader->m_pFctJump(audio returns 0x%x", err);
                return err;
            }
            pC->bjumpflag = M4OSA_FALSE;
        }
        M4OSA_TRACE2_3("E .... read  : cts  = %.0f + %.0f [ 0x%x ]",
             pC->pAddedClipCtxt->iAudioFrameCts / pC->pAddedClipCtxt->scale_audio,
                 pC->pAddedClipCtxt->iAoffset / pC->pAddedClipCtxt->scale_audio,
                     pC->pAddedClipCtxt->uiAudioFrameSize);
        if( M4WAR_NO_MORE_AU == err )
        {
            if(pC->bNoLooping == M4OSA_TRUE)
            {
                pC->uiEndLoop =0; /* Value 0 means no looping is required */
            }
            /**
            * Decide what to do when audio is over */
            if( pC->uiEndLoop > 0 )
            {
                /**
                * Jump at the Begin loop time */
                M4OSA_Int32 time = (M4OSA_Int32)(pC->uiBeginLoop);

                err = pC->pAddedClipCtxt->ShellAPI.m_pReader->m_pFctJump(
                    pC->pAddedClipCtxt->pReaderContext,
                    (M4_StreamHandler *)pC->pAddedClipCtxt->
                    pAudioStream, &time);

                if( M4NO_ERROR != err )
                {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_intAudioMixingConvert():\
                        m_pReader->m_pFctJump(audio returns 0x%x",
                        err);
                    return err;
                }
            }
            else
            {
                /* Transition from encoding state to reading state */
                err = M4VSS3GPP_intAudioMixingTransition(pC);

                if( M4NO_ERROR != err )
                {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_intAudioMixingStepAudioMix(): pre-encode fails err = 0x%x",
                        err);
                    return err;
                }

                /**
                * Second segment is over, state transition to third and return OK */
                pC->State = M4VSS3GPP_kAudioMixingState_AUDIO_THIRD_SEGMENT;

                /**
                * Return with no error so the step function will be called again */
                M4OSA_TRACE2_0(
                    "M4VSS3GPP_intAudioMixingConvert():\
                    returning M4VSS3GPP_WAR_END_OF_ADDED_AUDIO (2->3) a");
                return M4VSS3GPP_WAR_END_OF_ADDED_AUDIO;
            }
        }
        else if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intAudioMixingConvert(): m_pFctGetNextAu(audio) returns 0x%x",
                err);
            return err;
        }

        err = M4VSS3GPP_intClipDecodeCurrentAudioFrame(pC->pAddedClipCtxt);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intAudioMixingDoMixing:\
                M4VSS3GPP_intClipDecodeCurrentAudioFrame(added) returns 0x%x",
                err);
            return M4VSS3GPP_ERR_INPUT_AUDIO_CORRUPTED_AU;
        }

        /* Copy decoded data into SSRC buffer in */
        memcpy((void *)pC->pPosInSsrcBufferIn,
            (void *)pC->pAddedClipCtxt->AudioDecBufferOut.m_dataAddress,
            pC->pAddedClipCtxt->AudioDecBufferOut.m_bufferSize);
        /* Update position pointer into SSRC buffer In */

        pC->pPosInSsrcBufferIn +=
            pC->pAddedClipCtxt->AudioDecBufferOut.m_bufferSize;
    }

    /* Do the resampling / channel conversion if needed (=feed buffer out) */
    if( pC->b_SSRCneeded == M4OSA_TRUE )
    {
        pC->ChannelConversion = 0;
        if( pC->ChannelConversion > 0 )
        {
            while( pC->pPosInTempBuffer - pC->pTempBuffer
                < (M4OSA_Int32)(pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize
                *pC->pAddedClipCtxt->pSettings->ClipProperties.uiNbChannels)
                / pC->ChannelConversion )
                /* We use ChannelConversion variable because in case 2, we need twice less data */
            {
                ssrcErr = 0;
                memset((void *)pC->pPosInTempBuffer,0,
                    (pC->iSsrcNbSamplOut * sizeof(short) * pC->ewc.uiNbChannels));

                LVAudioresample_LowQuality((short*)pC->pPosInTempBuffer,
                    (short*)pC->pSsrcBufferIn,
                    pC->iSsrcNbSamplOut,
                    pC->pLVAudioResampler);
                if( 0 != ssrcErr )
                {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_intAudioMixingConvert: SSRC_Process returns 0x%x, returning ",
                        ssrcErr);
                    return ssrcErr;
                }

                pC->pPosInTempBuffer += pC->iSsrcNbSamplOut * sizeof(short)
                    * pC->pAddedClipCtxt->pSettings->
                    ClipProperties.uiNbChannels;

                /* Update SSRC bufferIn */
                tempPosBuffer =
                    pC->pSsrcBufferIn + (pC->iSsrcNbSamplIn * sizeof(short)
                    * pC->pAddedClipCtxt->pSettings->
                    ClipProperties.uiNbChannels);
                memmove((void *)pC->pSsrcBufferIn, (void *)tempPosBuffer,
                    pC->pPosInSsrcBufferIn - tempPosBuffer);
                pC->pPosInSsrcBufferIn -= pC->iSsrcNbSamplIn * sizeof(short)
                    * pC->pAddedClipCtxt->pSettings->
                    ClipProperties.uiNbChannels;
            }
        }
        else
        {
            while( pC->pPosInSsrcBufferOut - pC->pSsrcBufferOut
                < (M4OSA_Int32)pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize )
            {
                ssrcErr = 0;
                memset((void *)pC->pPosInSsrcBufferOut,0,
                    (pC->iSsrcNbSamplOut * sizeof(short) * pC->ewc.uiNbChannels));

                LVAudioresample_LowQuality((short*)pC->pPosInSsrcBufferOut,
                    (short*)pC->pSsrcBufferIn,
                    pC->iSsrcNbSamplOut,
                    pC->pLVAudioResampler);
                if( 0 != ssrcErr )
                {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_intAudioMixingConvert: SSRC_Process returns 0x%x, returning ",
                        ssrcErr);
                    return ssrcErr;
                }
                pC->pPosInSsrcBufferOut +=
                    pC->iSsrcNbSamplOut * sizeof(short) * pC->ewc.uiNbChannels;

                /* Update SSRC bufferIn */
                tempPosBuffer =
                    pC->pSsrcBufferIn + (pC->iSsrcNbSamplIn * sizeof(short)
                    * pC->pAddedClipCtxt->pSettings->
                    ClipProperties.uiNbChannels);
                memmove((void *)pC->pSsrcBufferIn, (void *)tempPosBuffer,
                    pC->pPosInSsrcBufferIn - tempPosBuffer);
                pC->pPosInSsrcBufferIn -= pC->iSsrcNbSamplIn * sizeof(short)
                    * pC->pAddedClipCtxt->pSettings->
                    ClipProperties.uiNbChannels;
            }
        }

        /* Convert Stereo<->Mono */
        switch( pC->ChannelConversion )
        {
            case 0: /* No channel conversion */
                break;

            case 1: /* stereo to mono */
                if( pC->pPosInSsrcBufferOut - pC->pSsrcBufferOut
                    < (M4OSA_Int32)pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize )
                {
                    From2iToMono_16((short *)pC->pTempBuffer,
                        (short *)pC->pSsrcBufferOut,
                        (short)(uiChannelConvertorNbSamples));
                    /* Update pTempBuffer */
                    tempPosBuffer = pC->pTempBuffer
                        + (uiChannelConvertorNbSamples * sizeof(short)
                        * pC->pAddedClipCtxt->pSettings->
                        ClipProperties.
                        uiNbChannels); /* Buffer is in bytes */
                    memmove((void *)pC->pTempBuffer, (void *)tempPosBuffer,
                        pC->pPosInTempBuffer - tempPosBuffer);
                    pC->pPosInTempBuffer -=
                        (uiChannelConvertorNbSamples * sizeof(short)
                        * pC->pAddedClipCtxt->pSettings->
                        ClipProperties.uiNbChannels);
                    pC->pPosInSsrcBufferOut +=
                        pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize;
                }
                break;

            case 2: /* mono to stereo */
                if( pC->pPosInSsrcBufferOut - pC->pSsrcBufferOut
                    < (M4OSA_Int32)pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize )
                {
                    MonoTo2I_16((short *)pC->pTempBuffer,
                        (short *)pC->pSsrcBufferOut,
                        (short)uiChannelConvertorNbSamples);
                    tempPosBuffer = pC->pTempBuffer
                        + (uiChannelConvertorNbSamples * sizeof(short)
                        * pC->pAddedClipCtxt->pSettings->
                        ClipProperties.uiNbChannels);
                    memmove((void *)pC->pTempBuffer, (void *)tempPosBuffer,
                        pC->pPosInTempBuffer - tempPosBuffer);
                    pC->pPosInTempBuffer -=
                        (uiChannelConvertorNbSamples * sizeof(short)
                        * pC->pAddedClipCtxt->pSettings->
                        ClipProperties.uiNbChannels);
                    pC->pPosInSsrcBufferOut +=
                        pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize;
                }
                break;
        }
    }
    else if( pC->ChannelConversion > 0 )
    {
        //M4OSA_UInt32 uiChannelConvertorNbSamples =
        // pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize / sizeof(short) /
        // pC->pInputClipCtxt->pSettings->ClipProperties.uiNbChannels;
        /* Convert Stereo<->Mono */
        switch( pC->ChannelConversion )
        {
            case 0: /* No channel conversion */
                break;

            case 1: /* stereo to mono */
                if( pC->pPosInSsrcBufferOut - pC->pSsrcBufferOut
                    < (M4OSA_Int32)pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize )
                {
                    From2iToMono_16((short *)pC->pSsrcBufferIn,
                        (short *)pC->pSsrcBufferOut,
                        (short)(uiChannelConvertorNbSamples));
                    /* Update pTempBuffer */
                    tempPosBuffer = pC->pSsrcBufferIn
                        + (uiChannelConvertorNbSamples * sizeof(short)
                        * pC->pAddedClipCtxt->pSettings->
                        ClipProperties.
                        uiNbChannels); /* Buffer is in bytes */
                    memmove((void *)pC->pSsrcBufferIn, (void *)tempPosBuffer,
                        pC->pPosInSsrcBufferIn - tempPosBuffer);
                    pC->pPosInSsrcBufferIn -=
                        (uiChannelConvertorNbSamples * sizeof(short)
                        * pC->pAddedClipCtxt->pSettings->
                        ClipProperties.uiNbChannels);
                    pC->pPosInSsrcBufferOut +=
                        pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize;
                }
                break;

            case 2: /* mono to stereo */
                if( pC->pPosInSsrcBufferOut - pC->pSsrcBufferOut
                    < (M4OSA_Int32)pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize )
                {
                    MonoTo2I_16((short *)pC->pSsrcBufferIn,
                        (short *)pC->pSsrcBufferOut,
                        (short)uiChannelConvertorNbSamples);
                    tempPosBuffer = pC->pSsrcBufferIn
                        + (uiChannelConvertorNbSamples * sizeof(short)
                        * pC->pAddedClipCtxt->pSettings->
                        ClipProperties.uiNbChannels);
                    memmove((void *)pC->pSsrcBufferIn, (void *)tempPosBuffer,
                        pC->pPosInSsrcBufferIn - tempPosBuffer);
                    pC->pPosInSsrcBufferIn -=
                        (uiChannelConvertorNbSamples * sizeof(short)
                        * pC->pAddedClipCtxt->pSettings->
                        ClipProperties.uiNbChannels);
                    pC->pPosInSsrcBufferOut +=
                        pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize;
                }
                break;
        }
    }
    else
    {
        /* No channel conversion nor sampl. freq. conversion needed, just buffer management */
        pC->pPosInSsrcBufferOut = pC->pPosInSsrcBufferIn;
    }

    return M4NO_ERROR;
}

M4OSA_Int32 M4VSS3GPP_getDecibelSound( M4OSA_UInt32 value )
    {
    int dbSound = 1;

    if( value == 0 )
        return 0;

    if( value > 0x4000 && value <= 0x8000 )      // 32768
        dbSound = 90;

    else if( value > 0x2000 && value <= 0x4000 ) // 16384
        dbSound = 84;

    else if( value > 0x1000 && value <= 0x2000 ) // 8192
        dbSound = 78;

    else if( value > 0x0800 && value <= 0x1000 ) // 4028
        dbSound = 72;

    else if( value > 0x0400 && value <= 0x0800 ) // 2048
        dbSound = 66;

    else if( value > 0x0200 && value <= 0x0400 ) // 1024
        dbSound = 60;

    else if( value > 0x0100 && value <= 0x0200 ) // 512
        dbSound = 54;

    else if( value > 0x0080 && value <= 0x0100 ) // 256
        dbSound = 48;

    else if( value > 0x0040 && value <= 0x0080 ) // 128
        dbSound = 42;

    else if( value > 0x0020 && value <= 0x0040 ) // 64
        dbSound = 36;

    else if( value > 0x0010 && value <= 0x0020 ) // 32
        dbSound = 30;

    else if( value > 0x0008 && value <= 0x0010 ) //16
        dbSound = 24;

    else if( value > 0x0007 && value <= 0x0008 ) //8
        dbSound = 24;

    else if( value > 0x0003 && value <= 0x0007 ) // 4
        dbSound = 18;

    else if( value > 0x0001 && value <= 0x0003 ) //2
        dbSound = 12;

    else if( value > 0x000 && value <= 0x0001 )  // 1
        dbSound = 6;

    else
        dbSound = 0;

    return dbSound;
    }
/**
 ******************************************************************************
 * M4OSA_ERR  M4VSS3GPP_intAudioMixingDoMixing(M4VSS3GPP_InternalAudioMixingContext *pC)
 * @brief    Mix the current audio AUs (decoder, mix, encode)
 * @note
 * @param    pC    (IN) VSS audio mixing internal context
 * @return    M4NO_ERROR:    No error
 ******************************************************************************
 */
static M4OSA_ERR M4VSS3GPP_intAudioMixingDoMixing(
    M4VSS3GPP_InternalAudioMixingContext *pC )
{
    M4OSA_ERR err;
    M4OSA_Int16 *pPCMdata1;
    M4OSA_Int16 *pPCMdata2;
    M4OSA_UInt32 uiPCMsize;

    M4ENCODER_AudioBuffer pEncInBuffer;  /**< Encoder input buffer for api */
    M4ENCODER_AudioBuffer pEncOutBuffer; /**< Encoder output buffer for api */
    M4OSA_Time
        frameTimeDelta; /**< Duration of the encoded (then written) data */
    M4OSA_MemAddr8 tempPosBuffer;
    /* ducking variable */
    M4OSA_UInt16 loopIndex = 0;
    M4OSA_Int16 *pPCM16Sample = M4OSA_NULL;
    M4OSA_Int32 peakDbValue = 0;
    M4OSA_Int32 previousDbValue = 0;
    M4OSA_UInt32 i;

    /**
    * Decode original audio track AU */

    err = M4VSS3GPP_intClipDecodeCurrentAudioFrame(pC->pInputClipCtxt);

    if( M4NO_ERROR != err )
    {
        M4OSA_TRACE1_1(
            "M4VSS3GPP_intAudioMixingDoMixing:\
            M4VSS3GPP_intClipDecodeCurrentAudioFrame(orig) returns 0x%x",
            err);
        return M4VSS3GPP_ERR_INPUT_AUDIO_CORRUPTED_AU;
    }

    if( M4OSA_TRUE == pC->b_SSRCneeded || pC->ChannelConversion > 0
        || pC->pAddedClipCtxt->pSettings->ClipProperties.AudioStreamType
        == M4VIDEOEDITING_kMP3 )
    {
        err = M4VSS3GPP_intAudioMixingConvert(pC);

        if( err == M4VSS3GPP_WAR_END_OF_ADDED_AUDIO )
        {
            return err;
        }

        if( err != M4NO_ERROR )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intAudioMixingDoMixing: M4VSS3GPP_intAudioMixingConvert returned 0x%x",
                err);
            return M4VSS3GPP_ERR_AUDIO_DECODED_PCM_SIZE_ISSUE;
        }

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

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

        pPCMdata2 = (M4OSA_Int16 *)pC->pSsrcBufferOut;
    }
    else
    {
        /**
        * Decode added audio track AU */
        err = M4VSS3GPP_intClipDecodeCurrentAudioFrame(pC->pAddedClipCtxt);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intAudioMixingDoMixing:\
                M4VSS3GPP_intClipDecodeCurrentAudioFrame(added) returns 0x%x",
                err);
            return M4VSS3GPP_ERR_INPUT_AUDIO_CORRUPTED_AU;
        }

        /**
        * Check both clips decoded the same amount of PCM samples */
        if( pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize
            != pC->pAddedClipCtxt->AudioDecBufferOut.m_bufferSize )
        {
            M4OSA_TRACE1_0(
                "M4VSS3GPP_intAudioMixingDoMixing:\
                both clips AU must have the same decoded PCM size!");
            return M4VSS3GPP_ERR_AUDIO_DECODED_PCM_SIZE_ISSUE;
        }
        pPCMdata2 = (M4OSA_Int16 *)pC->pAddedClipCtxt->AudioDecBufferOut.m_dataAddress;
    }

    /**
    * Mix the two decoded PCM audios */
    pPCMdata1 =
        (M4OSA_Int16 *)pC->pInputClipCtxt->AudioDecBufferOut.m_dataAddress;
    uiPCMsize = pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize
        / 2; /*buffer size (bytes) to number of sample (int16)*/

    if( pC->b_DuckingNeedeed )
    {
        loopIndex = 0;
        peakDbValue = 0;
        previousDbValue = peakDbValue;

        pPCM16Sample = (M4OSA_Int16 *)pC->pInputClipCtxt->
            AudioDecBufferOut.m_dataAddress;

        //Calculate the peak value
         while( loopIndex
             < pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize
            / sizeof(M4OSA_Int16) )
        {
            if( pPCM16Sample[loopIndex] >= 0 )
            {
                peakDbValue = previousDbValue > pPCM16Sample[loopIndex]
                ? previousDbValue : pPCM16Sample[loopIndex];
                previousDbValue = peakDbValue;
            }
            else
            {
                peakDbValue = previousDbValue > -pPCM16Sample[loopIndex]
                ? previousDbValue : -pPCM16Sample[loopIndex];
                previousDbValue = peakDbValue;
            }
            loopIndex++;
        }

        pC->audioVolumeArray[pC->audVolArrIndex] =
            M4VSS3GPP_getDecibelSound(peakDbValue);

        /* WINDOW_SIZE is 10 by default and check for threshold is done after 10 cycles */
        if( pC->audVolArrIndex >= WINDOW_SIZE - 1 )
        {
            pC->bDoDucking =
                M4VSS3GPP_isThresholdBreached((M4OSA_Int32 *)&(pC->audioVolumeArray),
                pC->audVolArrIndex, pC->InDucking_threshold);

            pC->audVolArrIndex = 0;
        }
        else
        {
            pC->audVolArrIndex++;
        }

        /*
        *Below logic controls the mixing weightage for Background Track and Primary Track
        *for the duration of window under analysis to give fade-out for Background and fade-in
        *for primary
        *
        *Current fading factor is distributed in equal range over the defined window size.
        *
        *For a window size = 25 (500 ms (window under analysis) / 20 ms (sample duration))
        *
        */

        if( pC->bDoDucking )
        {
            if( pC->duckingFactor
                > pC->InDucking_lowVolume ) // FADE OUT BG Track
            {
                    // decrement ducking factor in total steps in factor of low volume steps to reach
                    // low volume level
                pC->duckingFactor -= (pC->InDucking_lowVolume);
            }
            else
            {
                pC->duckingFactor = pC->InDucking_lowVolume;
            }
        }
        else
        {
            if( pC->duckingFactor < 1.0 ) // FADE IN BG Track
            {
                // increment ducking factor in total steps of low volume factor to reach
                // orig.volume level
                pC->duckingFactor += (pC->InDucking_lowVolume);
            }
        else
           {
                pC->duckingFactor = 1.0;
            }
        }
        /* endif - ducking_enable */

        /* Mixing Logic */

        while( uiPCMsize-- > 0 )
        {
            M4OSA_Int32 temp;

           /* set vol factor for BT and PT */
            *pPCMdata2 = (M4OSA_Int16)(*pPCMdata2 * pC->fBTVolLevel);

            *pPCMdata1 = (M4OSA_Int16)(*pPCMdata1 * pC->fPTVolLevel);

            /* mix the two samples */

            *pPCMdata2 = (M4OSA_Int16)(( *pPCMdata2) * (pC->duckingFactor));
            *pPCMdata1 = (M4OSA_Int16)(*pPCMdata2 / 2 + *pPCMdata1 / 2);


            if( *pPCMdata1 < 0 )
            {
                temp = -( *pPCMdata1)
                    * 2; // bring to same Amplitude level as it was original

                if( temp > 32767 )
                {
                    *pPCMdata1 = -32766; // less then max allowed value
                }
                else
                {
                    *pPCMdata1 = (M4OSA_Int16)(-temp);
               }
        }
        else
        {
            temp = ( *pPCMdata1)
                * 2; // bring to same Amplitude level as it was original

            if( temp > 32768 )
            {
                *pPCMdata1 = 32767; // less than max allowed value
            }
            else
            {
                *pPCMdata1 = (M4OSA_Int16)temp;
            }
        }

            pPCMdata2++;
            pPCMdata1++;
        }
    }
    else
    {
        while( uiPCMsize-- > 0 )
       {
        /* mix the two samples */
            *pPCMdata1 = (M4OSA_Int16)(*pPCMdata1 * pC->fOrigFactor * pC->fPTVolLevel
               + *pPCMdata2 * pC->fAddedFactor * pC->fBTVolLevel );

            pPCMdata1++;
            pPCMdata2++;
        }
    }

    /* Update pC->pSsrcBufferOut buffer */

    if( M4OSA_TRUE == pC->b_SSRCneeded || pC->ChannelConversion > 0 )
    {
        tempPosBuffer = pC->pSsrcBufferOut
            + pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize;
        memmove((void *)pC->pSsrcBufferOut, (void *)tempPosBuffer,
            pC->pPosInSsrcBufferOut - tempPosBuffer);
        pC->pPosInSsrcBufferOut -=
            pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize;
    }
    else if( pC->pAddedClipCtxt->pSettings->ClipProperties.AudioStreamType
        == M4VIDEOEDITING_kMP3 )
    {
        tempPosBuffer = pC->pSsrcBufferIn
            + pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize;
        memmove((void *)pC->pSsrcBufferIn, (void *)tempPosBuffer,
            pC->pPosInSsrcBufferIn - tempPosBuffer);
        pC->pPosInSsrcBufferIn -=
            pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize;
    }

    /* [Mono] or [Stereo interleaved] : all is in one buffer */
    pEncInBuffer.pTableBuffer[0] =
        pC->pInputClipCtxt->AudioDecBufferOut.m_dataAddress;
    pEncInBuffer.pTableBufferSize[0] =
        pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize;
    pEncInBuffer.pTableBuffer[1] = M4OSA_NULL;
    pEncInBuffer.pTableBufferSize[1] = 0;

    /* Time in ms from data size, because it is PCM16 samples */
    frameTimeDelta =
        pEncInBuffer.pTableBufferSize[0] / sizeof(short) / pC->ewc.uiNbChannels;

    /**
    * Prepare output buffer */
    pEncOutBuffer.pTableBuffer[0] =
        (M4OSA_MemAddr8)pC->ewc.WriterAudioAU.dataAddress;
    pEncOutBuffer.pTableBufferSize[0] = 0;

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

    /**
    * Encode the PCM audio */
    err = pC->ShellAPI.pAudioEncoderGlobalFcts->pFctStep(pC->ewc.pAudioEncCtxt,
        &pEncInBuffer, &pEncOutBuffer);

    if( M4NO_ERROR != err )
    {
        M4OSA_TRACE1_1(
            "M4VSS3GPP_intAudioMixingDoMixing(): pAudioEncoderGlobalFcts->pFctStep returns 0x%x",
            err);
        return err;
    }

    /**
    * Set AU cts and size */
    pC->ewc.WriterAudioAU.size =
        pEncOutBuffer.pTableBufferSize[0]; /**< Get the size of encoded data */
    pC->ewc.WriterAudioAU.CTS += frameTimeDelta;

    /**
    * Write the AU */
    M4OSA_TRACE2_2("L ---- write : cts  = %ld [ 0x%x ]",
        (M4OSA_Int32)(pC->ewc.WriterAudioAU.CTS / pC->ewc.scale_audio),
        pC->ewc.WriterAudioAU.size);

    err = pC->ShellAPI.pWriterDataFcts->pProcessAU(pC->ewc.p3gpWriterContext,
        M4VSS3GPP_WRITER_AUDIO_STREAM_ID, &pC->ewc.WriterAudioAU);

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

    /**
    * Increment the audio CTS for the next step */
    pC->ewc.dATo += frameTimeDelta / pC->ewc.scale_audio;

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

/**
 ******************************************************************************
 * M4OSA_ERR  M4VSS3GPP_intAudioMixingTransition(M4VSS3GPP_InternalAudioMixingContext *pC)
 * @brief    Decode/encode a few AU backward to initiate the encoder for later Mix segment.
 * @note
 * @param    pC    (IN) VSS audio mixing internal context
 * @return    M4NO_ERROR:    No error
 ******************************************************************************
 */
static M4OSA_ERR M4VSS3GPP_intAudioMixingTransition(
    M4VSS3GPP_InternalAudioMixingContext *pC )
{
    M4OSA_ERR err;

    M4ENCODER_AudioBuffer pEncInBuffer;  /**< Encoder input buffer for api */
    M4ENCODER_AudioBuffer pEncOutBuffer; /**< Encoder output buffer for api */
    M4OSA_Time
        frameTimeDelta = 0; /**< Duration of the encoded (then written) data */

    M4OSA_Int32 iTargetCts, iCurrentCts;

    /**
    * 'BZZZ' bug fix:
    * add a silence frame */
    err = M4VSS3GPP_intAudioMixingWriteSilence(pC);

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

    iCurrentCts = (M4OSA_Int32)(pC->ewc.dATo * pC->ewc.scale_audio + 0.5);

    /* Do not do pre-encode step if there is no mixing (remove, 100 %, or not editable) */
    if( M4OSA_FALSE == pC->bAudioMixingIsNeeded )
    {
        /**
        * Advance in the original audio stream to reach the current time
        * (We don't want iAudioCTS to be modified by the jump function,
        * so we have to use a local variable). */
        err = M4VSS3GPP_intClipJumpAudioAt(pC->pInputClipCtxt, &iCurrentCts);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1("M4VSS3GPP_intAudioMixingTransition:\
             M4VSS3GPP_intClipJumpAudioAt() returns 0x%x!", err);
            return err;
        }
    }
    else
    {
        /**< don't try to pre-decode if clip is at its beginning... */
        if( iCurrentCts > 0 )
        {
            /**
            * Get the output AU to write into */
            err = pC->ShellAPI.pWriterDataFcts->pStartAU(
                pC->ewc.p3gpWriterContext, M4VSS3GPP_WRITER_AUDIO_STREAM_ID,
                &pC->ewc.WriterAudioAU);

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

            /**
            * Jump a few AUs backward */
            iTargetCts = iCurrentCts - M4VSS3GPP_NB_AU_PREFETCH
                * pC->ewc.iSilenceFrameDuration;

            if( iTargetCts < 0 )
            {
                iTargetCts = 0; /**< Sanity check */
            }

            err = M4VSS3GPP_intClipJumpAudioAt(pC->pInputClipCtxt, &iTargetCts);

            if( M4NO_ERROR != err )
            {
                M4OSA_TRACE1_1(
                    "M4VSS3GPP_intAudioMixingTransition: DECODE_ENCODE-prefetch:\
                    M4VSS3GPP_intClipJumpAudioAt returns 0x%x!",
                    err);
                return err;
            }

            /**
            * Decode/encode up to the wanted position */
            while( pC->pInputClipCtxt->iAudioFrameCts < iCurrentCts )
            {
                err = M4VSS3GPP_intClipReadNextAudioFrame(pC->pInputClipCtxt);

                M4OSA_TRACE2_3("M .... read  : cts  = %.0f + %.0f [ 0x%x ]",
                    pC->pInputClipCtxt->iAudioFrameCts
                    / pC->pInputClipCtxt->scale_audio,
                    pC->pInputClipCtxt->iAoffset
                    / pC->pInputClipCtxt->scale_audio,
                    pC->pInputClipCtxt->uiAudioFrameSize);

                if( M4OSA_ERR_IS_ERROR(err) )
                {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_intAudioMixingTransition: DECODE_ENCODE-prefetch:\
                        M4VSS3GPP_intClipReadNextAudioFrame(b) returns 0x%x!",
                        err);
                    return err;
                }

                err = M4VSS3GPP_intClipDecodeCurrentAudioFrame(
                    pC->pInputClipCtxt);

                if( M4NO_ERROR != err )
                {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_intAudioMixingTransition: DECODE_ENCODE-prefetch:\
                        M4VSS3GPP_intClipDecodeCurrentAudioFrame returns 0x%x!",
                        err);
                    return err;
                }

                /* [Mono] or [Stereo interleaved] : all is in one buffer */
                pEncInBuffer.pTableBuffer[0] =
                    pC->pInputClipCtxt->AudioDecBufferOut.m_dataAddress;
                pEncInBuffer.pTableBufferSize[0] =
                    pC->pInputClipCtxt->AudioDecBufferOut.m_bufferSize;
                pEncInBuffer.pTableBuffer[1] = M4OSA_NULL;
                pEncInBuffer.pTableBufferSize[1] = 0;

                /* Time in ms from data size, because it is PCM16 samples */
                frameTimeDelta =
                    pEncInBuffer.pTableBufferSize[0] / sizeof(short)
                    / pC->ewc.uiNbChannels;

                /**
                * Prepare output buffer */
                pEncOutBuffer.pTableBuffer[0] =
                    (M4OSA_MemAddr8)pC->ewc.WriterAudioAU.dataAddress;
                pEncOutBuffer.pTableBufferSize[0] = 0;

                M4OSA_TRACE2_0("N **** pre-encode");

                /**
                * Encode the PCM audio */
                err = pC->ShellAPI.pAudioEncoderGlobalFcts->pFctStep(
                    pC->ewc.pAudioEncCtxt, &pEncInBuffer, &pEncOutBuffer);

                if( M4NO_ERROR != err )
                {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_intAudioMixingTransition():\
                        pAudioEncoderGlobalFcts->pFctStep returns 0x%x",
                        err);
                    return err;
                }
            }

            /**
            * Set AU cts and size */
            pC->ewc.WriterAudioAU.size = pEncOutBuffer.pTableBufferSize[
                0]; /**< Get the size of encoded data */
                pC->ewc.WriterAudioAU.CTS += frameTimeDelta;

                /**
                * Write the AU */
                M4OSA_TRACE2_2("O ---- write : cts  = %ld [ 0x%x ]",
                    (M4OSA_Int32)(pC->ewc.WriterAudioAU.CTS / pC->ewc.scale_audio),
                    pC->ewc.WriterAudioAU.size);

                err = pC->ShellAPI.pWriterDataFcts->pProcessAU(
                    pC->ewc.p3gpWriterContext, M4VSS3GPP_WRITER_AUDIO_STREAM_ID,
                    &pC->ewc.WriterAudioAU);

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

                /**
                * Increment the audio CTS for the next step */
                pC->ewc.dATo += pC->ewc.iSilenceFrameDuration / pC->ewc.scale_audio;
        }
    }

    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intAudioMixingCreateVideoEncoder()
 * @brief    Creates the video encoder
 * @note
 ******************************************************************************
 */
static M4OSA_ERR M4VSS3GPP_intAudioMixingCreateVideoEncoder(
    M4VSS3GPP_InternalAudioMixingContext *pC )
{
    M4OSA_ERR err;
    M4ENCODER_AdvancedParams EncParams;

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

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

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

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

    /* No strict regulation in video editor */
    /* Because of the effects and transitions we should allow more flexibility */
    /* Also it prevents to drop important frames
      (with a bad result on sheduling and block effetcs) */
    EncParams.bInternalRegulation = M4OSA_FALSE;
    EncParams.FrameRate = M4ENCODER_kVARIABLE_FPS;

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

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

            EncParams.Format = M4ENCODER_kH263;

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

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

        case M4SYS_kMPEG_4:

            EncParams.Format = M4ENCODER_kMPEG4;

            EncParams.uiStartingQuantizerValue = 8;
            EncParams.uiRateFactor = 1;

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

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

            EncParams.Format = M4ENCODER_kH264;

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

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

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

    EncParams.Bitrate =
        pC->pInputClipCtxt->pSettings->ClipProperties.uiVideoBitrate;

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

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

    pC->ewc.encoderState = M4VSS3GPP_kEncoderClosed;
    M4OSA_TRACE1_0(
        "M4VSS3GPP_intAudioMixingCreateVideoEncoder: calling encoder pFctOpen");
    M4OSA_TRACE1_2("vss: audio mix encoder open profile :%d, level %d",
        EncParams.videoProfile, EncParams.videoLevel);
    err = pC->ShellAPI.pVideoEncoderGlobalFcts->pFctOpen(pC->ewc.pEncContext,
        &pC->ewc.WriterVideoAU, &EncParams);

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

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

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

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

    pC->ewc.encoderState = M4VSS3GPP_kEncoderRunning;

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

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intAudioMixingDestroyVideoEncoder()
 * @brief    Destroy the video encoder
 * @note
 ******************************************************************************
 */
static M4OSA_ERR M4VSS3GPP_intAudioMixingDestroyVideoEncoder(
    M4VSS3GPP_InternalAudioMixingContext *pC )
{
    M4OSA_ERR err = M4NO_ERROR;

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

                if( M4NO_ERROR != err )
                {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_intAudioMixingDestroyVideoEncoder:\
                        pVideoEncoderGlobalFcts->pFctStop returns 0x%x",
                        err);
                }
            }

            pC->ewc.encoderState = M4VSS3GPP_kEncoderStopped;
        }

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

            if( M4NO_ERROR != err )
            {
                M4OSA_TRACE1_1(
                    "M4VSS3GPP_intAudioMixingDestroyVideoEncoder:\
                    pVideoEncoderGlobalFcts->pFctClose returns 0x%x",
                    err);
            }

            pC->ewc.encoderState = M4VSS3GPP_kEncoderClosed;
        }

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

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

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

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

M4OSA_Bool M4VSS3GPP_isThresholdBreached( M4OSA_Int32 *averageValue,
                                         M4OSA_Int32 storeCount, M4OSA_Int32 thresholdValue )
{
    M4OSA_Bool result = 0;
    int i;
    int finalValue = 0;

    for ( i = 0; i < storeCount; i++ )
        finalValue += averageValue[i];

    finalValue = finalValue / storeCount;


    if( finalValue > thresholdValue )
        result = M4OSA_TRUE;
    else
        result = M4OSA_FALSE;

    return result;
}