summaryrefslogtreecommitdiffstats
path: root/libvideoeditor/vss/src/M4VSS3GPP_Clip.c
blob: 40612f3f6707e10ce0b6028c2624c28994c2de33 (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
/*
 * 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_Clip.c
 * @brief    Implementation of functions related to input clip management.
 * @note    All functions in this file are static, i.e. non public
 ******************************************************************************
 */

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

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

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


/**
 * Common headers (for aac) */
#include "M4_Common.h"

#ifdef M4VSS_ENABLE_EXTERNAL_DECODERS
#include "M4VD_EXTERNAL_Interface.h"

#endif /* M4VSS_ENABLE_EXTERNAL_DECODERS */

/* Osal header fileno */
#include "M4OSA_CharStar.h"

/**
 ******************************************************************************
 * define    Static function prototypes
 ******************************************************************************
 */

static M4OSA_ERR M4VSS3GPP_intClipPrepareAudioDecoder(
    M4VSS3GPP_ClipContext *pClipCtxt );

static M4OSA_ERR M4VSS3GPP_intCheckAndGetCodecAacProperties(
        M4VSS3GPP_ClipContext *pClipCtxt);

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intClipOpen()
 * @brief    Open a clip. Creates a clip context.
 * @note
 * @param   hClipCtxt            (OUT) Return the internal clip context
 * @param   pClipSettings        (IN) Edit settings of this clip. The module will keep a
 *                               reference to this pointer
 * @param    pFileReadPtrFct        (IN) Pointer to OSAL file reader functions
 * @param    bSkipAudioTrack        (IN) If true, do not open the audio
 * @param    bFastOpenMode        (IN) If true, use the fast mode of the 3gpp reader
 *                             (only the first AU is read)
 * @return    M4NO_ERROR:                No error
 * @return    M4ERR_ALLOC:            There is no more available memory
 ******************************************************************************
 */

M4OSA_ERR M4VSS3GPP_intClipInit( M4VSS3GPP_ClipContext ** hClipCtxt,
                                M4OSA_FileReadPointer *pFileReadPtrFct )
{
    M4VSS3GPP_ClipContext *pClipCtxt;
    M4OSA_ERR err;

    M4OSA_DEBUG_IF2((M4OSA_NULL == hClipCtxt), M4ERR_PARAMETER,
        "M4VSS3GPP_intClipInit: hClipCtxt is M4OSA_NULL");
    M4OSA_DEBUG_IF2((M4OSA_NULL == pFileReadPtrFct), M4ERR_PARAMETER,
        "M4VSS3GPP_intClipInit: pFileReadPtrFct is M4OSA_NULL");

    /**
    * Allocate the clip context */
    *hClipCtxt =
        (M4VSS3GPP_ClipContext *)M4OSA_32bitAlignedMalloc(sizeof(M4VSS3GPP_ClipContext),
        M4VSS3GPP, (M4OSA_Char *)"M4VSS3GPP_ClipContext");

    if( M4OSA_NULL == *hClipCtxt )
    {
        M4OSA_TRACE1_0(
            "M4VSS3GPP_intClipInit(): unable to allocate M4VSS3GPP_ClipContext,\
            returning M4ERR_ALLOC");
        return M4ERR_ALLOC;
    }
    M4OSA_TRACE3_1("M4VSS3GPP_intClipInit(): clipCtxt=0x%x", *hClipCtxt);


    /**
    * Use this shortcut to simplify the code */
    pClipCtxt = *hClipCtxt;

    /* Inialization of context Variables */
    memset((void *)pClipCtxt, 0,sizeof(M4VSS3GPP_ClipContext));

    pClipCtxt->pSettings = M4OSA_NULL;

    /**
    * Init the clip context */
    pClipCtxt->iVoffset = 0;
    pClipCtxt->iAoffset = 0;
    pClipCtxt->Vstatus = M4VSS3GPP_kClipStatus_READ;
    pClipCtxt->Astatus = M4VSS3GPP_kClipStatus_READ;

    pClipCtxt->pReaderContext = M4OSA_NULL;
    pClipCtxt->pVideoStream = M4OSA_NULL;
    pClipCtxt->pAudioStream = M4OSA_NULL;
    pClipCtxt->VideoAU.m_dataAddress = M4OSA_NULL;
    pClipCtxt->AudioAU.m_dataAddress = M4OSA_NULL;

    pClipCtxt->pViDecCtxt = M4OSA_NULL;
    pClipCtxt->iVideoDecCts = 0;
    pClipCtxt->iVideoRenderCts = 0;
    pClipCtxt->lastDecodedPlane = M4OSA_NULL;
    pClipCtxt->iActualVideoBeginCut = 0;
    pClipCtxt->iActualAudioBeginCut = 0;
    pClipCtxt->bVideoAuAvailable = M4OSA_FALSE;
    pClipCtxt->bFirstAuWritten = M4OSA_FALSE;

    pClipCtxt->bMpeg4GovState = M4OSA_FALSE;

    pClipCtxt->bAudioFrameAvailable = M4OSA_FALSE;
    pClipCtxt->pAudioFramePtr = M4OSA_NULL;
    pClipCtxt->iAudioFrameCts = 0;
    pClipCtxt->pAudioDecCtxt = 0;
    pClipCtxt->AudioDecBufferOut.m_bufferSize = 0;
    pClipCtxt->AudioDecBufferOut.m_dataAddress = M4OSA_NULL;

    pClipCtxt->pFileReadPtrFct = pFileReadPtrFct;
    pClipCtxt->pPlaneYuv   = M4OSA_NULL;
    pClipCtxt->pPlaneYuvWithEffect = M4OSA_NULL;
    pClipCtxt->m_pPreResizeFrame = M4OSA_NULL;
    pClipCtxt->bGetYuvDataFromDecoder = M4OSA_TRUE;

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

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

    return M4NO_ERROR;
}

// This method maps the frequency value to a string.
static const char* freqToString(int freq) {
    switch (freq) {
    case 8000:
        return "_8000";
    case 11025:
        return "_11025";
    case 12000:
        return "_12000";
    case 16000:
        return "_16000";
    case 22050:
        return "_22050";
    case 24000:
        return "_24000";
    case 32000:
        return "_32000";
    case 44100:
        return "_44100";
    case 48000:
        return "_48000";
    default:
        M4OSA_TRACE1_1("Unsupported sampling rate: %d Hz", freq);
        return NULL;
    }
}

// This method maps the number of channel value to
// a string that will be part of a file name extension
static const char* channelToStringAndFileExt(int channels) {
    switch (channels) {
    case 1:
        return "_1.pcm";
    case 2:
        return "_2.pcm";
    default:
        M4OSA_TRACE1_1("Unsupported %d channels", channels);
        return NULL;
    }
}

/* Note: if the clip is opened in fast mode, it can only be used for analysis and nothing else. */
M4OSA_ERR M4VSS3GPP_intClipOpen( M4VSS3GPP_ClipContext *pClipCtxt,
                                M4VSS3GPP_ClipSettings *pClipSettings, M4OSA_Bool bSkipAudioTrack,
                                M4OSA_Bool bFastOpenMode, M4OSA_Bool bAvoidOpeningVideoDec )
{
    M4OSA_ERR err;
    M4READER_MediaFamily mediaFamily;
    M4_StreamHandler *pStreamHandler;
    M4_StreamHandler  dummyStreamHandler;
    M4OSA_Int32 iDuration;
    M4OSA_Void *decoderUserData;
#ifdef M4VSS_ENABLE_EXTERNAL_DECODERS

    M4DECODER_MPEG4_DecoderConfigInfo dummy;
    M4DECODER_VideoSize videoSizeFromDSI;
#endif /* M4VSS_ENABLE_EXTERNAL_DECODERS */

    M4DECODER_OutputFilter FilterOption;

    /**
    *    Check input parameters */
    M4OSA_DEBUG_IF2((M4OSA_NULL == pClipCtxt), M4ERR_PARAMETER,
        "M4VSS3GPP_intClipOpen: pClipCtxt is M4OSA_NULL");
    M4OSA_DEBUG_IF2((M4OSA_NULL == pClipSettings), M4ERR_PARAMETER,
        "M4VSS3GPP_intClipOpen: pClipSettings is M4OSA_NULL");

    M4OSA_TRACE3_2(
        "M4VSS3GPP_intClipOpen: called with pClipCtxt: 0x%x, bAvoidOpeningVideoDec=0x%x",
        pClipCtxt, bAvoidOpeningVideoDec);
    /**
    * Keep a pointer to the clip settings. Remember that we don't possess it! */
    pClipCtxt->pSettings = pClipSettings;
    if(M4VIDEOEDITING_kFileType_ARGB8888 == pClipCtxt->pSettings->FileType) {
        M4OSA_TRACE3_0("M4VSS3GPP_intClipOpen: Image stream; set current vid dec");
        err = M4VSS3GPP_setCurrentVideoDecoder(
                  &pClipCtxt->ShellAPI, M4DA_StreamTypeVideoARGB8888);
        M4ERR_CHECK_RETURN(err);

        decoderUserData = M4OSA_NULL;

        err = pClipCtxt->ShellAPI.m_pVideoDecoder->m_pFctCreate(
                  &pClipCtxt->pViDecCtxt,
                  &dummyStreamHandler,
                  pClipCtxt->ShellAPI.m_pReader,
                  pClipCtxt->ShellAPI.m_pReaderDataIt,
                  &pClipCtxt->VideoAU,
                  decoderUserData);

        if (M4NO_ERROR != err) {
            M4OSA_TRACE1_1("M4VSS3GPP_intClipOpen: \
                m_pVideoDecoder->m_pFctCreate returns 0x%x", err);
            return err;
        }
        M4OSA_TRACE3_1("M4VSS3GPP_intClipOpen: \
            Vid dec started; pViDecCtxt=0x%x", pClipCtxt->pViDecCtxt);

        return M4NO_ERROR;
    }

    /**
    * Get the correct reader interface */
    err = M4VSS3GPP_setCurrentReader(&pClipCtxt->ShellAPI,
        pClipCtxt->pSettings->FileType);
    M4ERR_CHECK_RETURN(err);

    /**
    * Init the 3GPP or MP3 reader */
    err =
        pClipCtxt->ShellAPI.m_pReader->m_pFctCreate(&pClipCtxt->pReaderContext);

    if( M4NO_ERROR != err )
    {
        M4OSA_TRACE1_1(
            "M4VSS3GPP_intClipOpen(): m_pReader->m_pFctCreate returns 0x%x",
            err);
        return err;
    }

    /**
    * Link the reader interface to the reader context (used by the decoder to know the reader) */
    pClipCtxt->ShellAPI.m_pReaderDataIt->m_readerContext =
        pClipCtxt->pReaderContext;

    /**
    * Set the OSAL read function set */
    err = pClipCtxt->ShellAPI.m_pReader->m_pFctSetOption(
        pClipCtxt->pReaderContext,
        M4READER_kOptionID_SetOsaFileReaderFctsPtr,
        (M4OSA_DataOption)(pClipCtxt->pFileReadPtrFct));

    if( M4NO_ERROR != err )
    {
        M4OSA_TRACE1_1(
            "M4VSS3GPP_intClipOpen(): m_pReader->m_pFctSetOption returns 0x%x",
            err);
        return err;
    }

    /**
    * Set the fast open mode if asked (3GPP only) */
    if( M4VIDEOEDITING_kFileType_3GPP == pClipCtxt->pSettings->FileType )
    {
        if( M4OSA_TRUE == bFastOpenMode )
        {
            err = pClipCtxt->ShellAPI.m_pReader->m_pFctSetOption(
                pClipCtxt->pReaderContext,
                M4READER_3GP_kOptionID_FastOpenMode, M4OSA_NULL);

            if( M4NO_ERROR != err )
            {
                M4OSA_TRACE1_1(
                    "M4VSS3GPP_intClipOpen():\
                    m_pReader->m_pFctSetOption(FastOpenMode) returns 0x%x",
                    err);
                return err;
            }
        }

        /**
        * Set the skip audio option if asked */
        if( M4OSA_TRUE == bSkipAudioTrack )
        {
            err = pClipCtxt->ShellAPI.m_pReader->m_pFctSetOption(
                pClipCtxt->pReaderContext,
                M4READER_3GP_kOptionID_VideoOnly, M4OSA_NULL);

            if( M4NO_ERROR != err )
            {
                M4OSA_TRACE1_1(
                    "M4VSS3GPP_intClipOpen(): m_pReader->m_pFctSetOption(VideoOnly) returns 0x%x",
                    err);
                return err;
            }
        }
    }
    if (pClipCtxt->pSettings->FileType == M4VIDEOEDITING_kFileType_PCM) {
        // Compose the temp filename with sample rate and channel information.
        const char* freqStr = freqToString(
                    pClipCtxt->pSettings->ClipProperties.uiSamplingFrequency);

        if (freqStr == NULL) {
            return M4VSS3GPP_WAR_INCOMPATIBLE_AUDIO_SAMPLING_FREQUENCY;
        }

        const char* chanStr = channelToStringAndFileExt(
                    pClipCtxt->pSettings->ClipProperties.uiNbChannels);

        if (chanStr == NULL) {
                return M4VSS3GPP_WAR_INCOMPATIBLE_AUDIO_NB_OF_CHANNELS;
        }

        // Allocate one byte more to hold the null terminator
        M4OSA_UInt32 length =
            strlen(pClipSettings->pFile) + strlen(freqStr) + strlen(chanStr) + 1;

        char* pTempFile = (char *) malloc(length);
        if (pTempFile == NULL) {
            M4OSA_TRACE1_1("M4VSS3GPP_intClipOpen(): malloc %d bytes fail",length);
            return M4ERR_ALLOC;
        }
        memset(pTempFile, 0, length);
        memcpy(pTempFile, pClipSettings->pFile, strlen(pClipSettings->pFile));
        strncat(pTempFile, freqStr, strlen(freqStr));
        strncat(pTempFile, chanStr, strlen(chanStr));

        err = pClipCtxt->ShellAPI.m_pReader->m_pFctOpen( pClipCtxt->pReaderContext, pTempFile);
        if (pTempFile != NULL) {
            free(pTempFile);
            pTempFile = NULL;
        }
        if ( M4NO_ERROR != err ) {
            M4OSA_TRACE1_1("M4VSS3GPP_intClipOpen(): open pcm file returns error : 0x%x", err);
            return err;
        }
    }
    else
    {
    /**
        * Open the 3GPP/MP3 clip file */
        err = pClipCtxt->ShellAPI.m_pReader->m_pFctOpen( pClipCtxt->pReaderContext,
             pClipSettings->pFile);
    }
    if( M4NO_ERROR != err )
    {
        M4OSA_UInt32 uiDummy, uiCoreId;
        M4OSA_TRACE1_1(
            "M4VSS3GPP_intClipOpen(): m_pReader->m_pFctOpen returns 0x%x", err);

        /**
        * If the error is from the core reader, we change it to a public VSS3GPP error */
        M4OSA_ERR_SPLIT(err, uiDummy, uiCoreId, uiDummy);

        if( M4MP4_READER == uiCoreId )
        {
            M4OSA_TRACE1_0(
                "M4VSS3GPP_intClipOpen(): returning M4VSS3GPP_ERR_INVALID_3GPP_FILE");
            return M4VSS3GPP_ERR_INVALID_3GPP_FILE;
        }
        return err;
    }

    /**
    * Get the audio and video streams */
    while( err == M4NO_ERROR )
    {
        err = pClipCtxt->ShellAPI.m_pReader->m_pFctGetNextStream(
            pClipCtxt->pReaderContext, &mediaFamily, &pStreamHandler);

        /*in case we found a BIFS stream or something else...*/
        if( ( err == ((M4OSA_UInt32)M4ERR_READER_UNKNOWN_STREAM_TYPE))
            || (err == ((M4OSA_UInt32)M4WAR_TOO_MUCH_STREAMS)) )
        {
            err = M4NO_ERROR;
            continue;
        }

        if( M4NO_ERROR == err ) /**< One stream found */
        {
            /**
            * Found a video stream */
            if( ( mediaFamily == M4READER_kMediaFamilyVideo)
                && (M4OSA_NULL == pClipCtxt->pVideoStream) )
            {
                if( ( M4DA_StreamTypeVideoH263 == pStreamHandler->m_streamType)
                    || (M4DA_StreamTypeVideoMpeg4
                    == pStreamHandler->m_streamType)
                    || (M4DA_StreamTypeVideoMpeg4Avc
                    == pStreamHandler->m_streamType) )
                {
                    M4OSA_TRACE3_1(
                        "M4VSS3GPP_intClipOpen():\
                        Found a H263 or MPEG-4 or H264 video stream in input 3gpp clip; %d",
                        pStreamHandler->m_streamType);

                    /**
                    * Keep pointer to the video stream */
                    pClipCtxt->pVideoStream =
                        (M4_VideoStreamHandler *)pStreamHandler;
                    pStreamHandler->m_bStreamIsOK = M4OSA_TRUE;

                    /**
                    * Reset the stream reader */
                    err = pClipCtxt->ShellAPI.m_pReader->m_pFctReset(
                        pClipCtxt->pReaderContext,
                        (M4_StreamHandler *)pClipCtxt->pVideoStream);

                    if( M4NO_ERROR != err )
                    {
                        M4OSA_TRACE1_1(
                            "M4VSS3GPP_intClipOpen(): m_pReader->m_pFctReset(video) returns 0x%x",
                            err);
                        return err;
                    }

                    /**
                    * Initializes an access Unit */
                    err = pClipCtxt->ShellAPI.m_pReader->m_pFctFillAuStruct(
                        pClipCtxt->pReaderContext,
                        (M4_StreamHandler *)pClipCtxt->pVideoStream,
                        &pClipCtxt->VideoAU);

                    if( M4NO_ERROR != err )
                    {
                        M4OSA_TRACE1_1(
                            "M4VSS3GPP_intClipOpen():\
                            m_pReader->m_pFctFillAuStruct(video) returns 0x%x",
                            err);
                        return err;
                    }
                }
                else /**< Not H263 or MPEG-4 (H264, etc.) */
                {
                    M4OSA_TRACE1_1(
                        "M4VSS_editClipOpen():\
                        Found an unsupported video stream (0x%x) in input 3gpp clip",
                        pStreamHandler->m_streamType);

                    pStreamHandler->m_bStreamIsOK = M4OSA_FALSE;
                }
            }
            /**
            * Found an audio stream */
            else if( ( mediaFamily == M4READER_kMediaFamilyAudio)
                && (M4OSA_NULL == pClipCtxt->pAudioStream) )
            {
                if( ( M4DA_StreamTypeAudioAmrNarrowBand
                    == pStreamHandler->m_streamType)
                    || (M4DA_StreamTypeAudioAac == pStreamHandler->m_streamType)
                    || (M4DA_StreamTypeAudioMp3
                    == pStreamHandler->m_streamType)
                    || (M4DA_StreamTypeAudioEvrc
                    == pStreamHandler->m_streamType)
                    || (M4DA_StreamTypeAudioPcm
                    == pStreamHandler->m_streamType) )
                {
                    M4OSA_TRACE3_1(
                        "M4VSS3GPP_intClipOpen(): \
                        Found an AMR-NB or AAC or MP3 audio stream in input clip; %d",
                        pStreamHandler->m_streamType);

                    /**
                    * Keep pointer to the audio stream */
                    pClipCtxt->pAudioStream =
                        (M4_AudioStreamHandler *)pStreamHandler;
                    pStreamHandler->m_bStreamIsOK = M4OSA_TRUE;

                    /**
                    * Reset the stream reader */
                    err = pClipCtxt->ShellAPI.m_pReader->m_pFctReset(
                        pClipCtxt->pReaderContext,
                        (M4_StreamHandler *)pClipCtxt->pAudioStream);

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

                    /**
                    * Initializes an access Unit */
                    err = pClipCtxt->ShellAPI.m_pReader->m_pFctFillAuStruct(
                        pClipCtxt->pReaderContext,
                        (M4_StreamHandler *)pClipCtxt->pAudioStream,
                        &pClipCtxt->AudioAU);

                    if( M4NO_ERROR != err )
                    {
                        M4OSA_TRACE1_1(
                            "M4VSS3GPP_intClipOpen():\
                            m_pReader->m_pFctFillAuStruct(audio) returns 0x%x",
                            err);
                        return err;
                    }
                }
                else /**< Not AMR-NB or AAC (AMR-WB...) */
                {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_intClipOpen():\
                        Found an unsupported audio stream (0x%x) in input 3gpp/mp3 clip",
                        pStreamHandler->m_streamType);

                    pStreamHandler->m_bStreamIsOK = M4OSA_FALSE;
                }
            }
        }
        else if( M4OSA_ERR_IS_ERROR(err) )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intClipOpen(): m_pReader->m_pFctGetNextStream() returns 0x%x!",
                err);
            return err;
        }
    }

    /**
    * Init Video decoder */
    if( M4OSA_NULL != pClipCtxt->pVideoStream )
    {
#ifdef M4VSS_ENABLE_EXTERNAL_DECODERS
  /* If external decoders are possible, it's best to avoid opening the decoder if the clip is only
  going to be used for analysis, as we're not going to use it for the analysis in the case of a
  possible external decoder anyway, and either there could be no decoder at this point or the HW
  decoder could be present, which we want to avoid opening for that. See comments in
  intBuildAnalysis for more details. */

  /* CHANGEME Temporarily only do this for MPEG4, since for now only MPEG4 external decoders are
  supported, and the following wouldn't work for H263 so a release where external decoders are
  possible, but not used, wouldn't work with H263 stuff. */

        if( bAvoidOpeningVideoDec && M4DA_StreamTypeVideoMpeg4
            == pClipCtxt->pVideoStream->m_basicProperties.m_streamType )
        {
            /* Oops! The mere act of opening the decoder also results in the image size being
            filled in the video stream! Compensate for this by using ParseVideoDSI to fill
            this info. */
            M4OSA_TRACE3_0(
                "M4VSS3GPP_intClipOpen: Mpeg4 stream; vid dec not started");
            err = M4DECODER_EXTERNAL_ParseVideoDSI(pClipCtxt->pVideoStream->
                m_basicProperties.m_pDecoderSpecificInfo,
                pClipCtxt->pVideoStream->
                m_basicProperties.m_decoderSpecificInfoSize,
                &dummy, &videoSizeFromDSI);

            pClipCtxt->pVideoStream->m_videoWidth = videoSizeFromDSI.m_uiWidth;
            pClipCtxt->pVideoStream->m_videoHeight =
                videoSizeFromDSI.m_uiHeight;
        }
        else
        {

#endif

            M4OSA_TRACE3_0(
                "M4VSS3GPP_intClipOpen: Mp4/H263/H264 stream; set current vid dec");
            err = M4VSS3GPP_setCurrentVideoDecoder(&pClipCtxt->ShellAPI,
                pClipCtxt->pVideoStream->m_basicProperties.m_streamType);
            M4ERR_CHECK_RETURN(err);

#ifdef M4VSS_ENABLE_EXTERNAL_DECODERS

            decoderUserData =
                pClipCtxt->ShellAPI.m_pCurrentVideoDecoderUserData;

#else

            decoderUserData = M4OSA_NULL;

#endif

            err = pClipCtxt->ShellAPI.m_pVideoDecoder->m_pFctCreate(
                &pClipCtxt->pViDecCtxt,
                &pClipCtxt->pVideoStream->m_basicProperties,
                pClipCtxt->ShellAPI.m_pReader,
                pClipCtxt->ShellAPI.m_pReaderDataIt,
                &pClipCtxt->VideoAU, decoderUserData);

            if( ( ((M4OSA_UInt32)M4ERR_DECODER_H263_PROFILE_NOT_SUPPORTED) == err)
                || (((M4OSA_UInt32)M4ERR_DECODER_H263_NOT_BASELINE) == err) )
            {
                /**
                * Our decoder is not compatible with H263 profile other than 0.
                * So it returns this internal error code.
                * We translate it to our own error code */
                return M4VSS3GPP_ERR_H263_PROFILE_NOT_SUPPORTED;
            }
            else if( M4NO_ERROR != err )
            {
                M4OSA_TRACE1_1(
                    "M4VSS3GPP_intClipOpen: m_pVideoDecoder->m_pFctCreate returns 0x%x",
                    err);
                return err;
            }
            M4OSA_TRACE3_1(
                "M4VSS3GPP_intClipOpen: Vid dec started; pViDecCtxt=0x%x",
                pClipCtxt->pViDecCtxt);

            if( M4DA_StreamTypeVideoMpeg4Avc
                == pClipCtxt->pVideoStream->m_basicProperties.m_streamType )
            {
                FilterOption.m_pFilterFunction =
                    (M4OSA_Void *) &M4VIFI_ResizeBilinearYUV420toYUV420;
                FilterOption.m_pFilterUserData = M4OSA_NULL;
                err = pClipCtxt->ShellAPI.m_pVideoDecoder->m_pFctSetOption(
                    pClipCtxt->pViDecCtxt, M4DECODER_kOptionID_OutputFilter,
                    (M4OSA_DataOption) &FilterOption);

                if( M4NO_ERROR != err )
                {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_intClipOpen: m_pVideoDecoder->m_pFctSetOption returns 0x%x",
                        err);
                    return err;
                }
                else
                {
                    M4OSA_TRACE3_0(
                        "M4VSS3GPP_intClipOpen: m_pVideoDecoder->m_pFctSetOption\
                        M4DECODER_kOptionID_OutputFilter OK");
                }
            }
#ifdef M4VSS_ENABLE_EXTERNAL_DECODERS

        }

#endif

    }

    /**
    * Init Audio decoder */
    if( M4OSA_NULL != pClipCtxt->pAudioStream )
    {
        err = M4VSS3GPP_intClipPrepareAudioDecoder(pClipCtxt);
        M4ERR_CHECK_RETURN(err);
        M4OSA_TRACE3_1("M4VSS3GPP_intClipOpen: Audio dec started; context=0x%x",
            pClipCtxt->pAudioDecCtxt);
    }
    else
    {
        pClipCtxt->AudioAU.m_streamID = 0;
        pClipCtxt->AudioAU.m_dataAddress = M4OSA_NULL;
        pClipCtxt->AudioAU.m_size = 0;
        pClipCtxt->AudioAU.m_CTS = 0;
        pClipCtxt->AudioAU.m_DTS = 0;
        pClipCtxt->AudioAU.m_attribute = 0;
        pClipCtxt->AudioAU.m_maxsize = 0;
        pClipCtxt->AudioAU.m_structSize = sizeof(pClipCtxt->AudioAU);
    }

    /**
    * Get the duration of the longest stream */
    if( M4OSA_TRUE == pClipCtxt->pSettings->ClipProperties.bAnalysed )
    {
        /* If already calculated set it to previous value */
        /* Because fast open and full open can return a different value,
           it can mismatch user settings */
        /* Video track is more important than audio track (if video track is shorter than
           audio track, it can led to cut larger than expected) */
        iDuration = pClipCtxt->pSettings->ClipProperties.uiClipVideoDuration;

        if( iDuration == 0 )
        {
            iDuration = pClipCtxt->pSettings->ClipProperties.uiClipDuration;
        }
    }
    else
    {
        /* Else compute it from streams */
        iDuration = 0;

        if( M4OSA_NULL != pClipCtxt->pVideoStream )
        {
            iDuration = (M4OSA_Int32)(
                pClipCtxt->pVideoStream->m_basicProperties.m_duration);
        }

        if( ( M4OSA_NULL != pClipCtxt->pAudioStream) && ((M4OSA_Int32)(
            pClipCtxt->pAudioStream->m_basicProperties.m_duration)
            > iDuration) && iDuration == 0 )
        {
            iDuration = (M4OSA_Int32)(
                pClipCtxt->pAudioStream->m_basicProperties.m_duration);
        }
    }

    /**
    * If end time is not used, we set it to the video track duration */
    if( 0 == pClipCtxt->pSettings->uiEndCutTime )
    {
        pClipCtxt->pSettings->uiEndCutTime = (M4OSA_UInt32)iDuration;
    }

    pClipCtxt->iEndTime = (M4OSA_Int32)pClipCtxt->pSettings->uiEndCutTime;

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

/**
 ******************************************************************************
 * M4OSA_Void M4VSS3GPP_intClipDeleteAudioTrack()
 * @brief    Delete the audio track. Clip will be like if it had no audio track
 * @note
 * @param   pClipCtxt            (IN) Internal clip context
 ******************************************************************************
 */
M4OSA_Void M4VSS3GPP_intClipDeleteAudioTrack( M4VSS3GPP_ClipContext *pClipCtxt )
{
    /**
    * But we don't have to free the audio stream. It will be freed by the reader when closing it*/
    pClipCtxt->pAudioStream = M4OSA_NULL;

    /**
    * We will return a constant silence AMR AU.
    * We set it here once, instead of at each read audio step. */
    pClipCtxt->pAudioFramePtr = (M4OSA_MemAddr8)pClipCtxt->pSilenceFrameData;
    pClipCtxt->uiAudioFrameSize = pClipCtxt->uiSilenceFrameSize;

    /**
    * Free the decoded audio buffer (it needs to be re-allocated to store silence
      frame eventually)*/
    if( M4OSA_NULL != pClipCtxt->AudioDecBufferOut.m_dataAddress )
    {
        free(pClipCtxt->AudioDecBufferOut.m_dataAddress);
        pClipCtxt->AudioDecBufferOut.m_dataAddress = M4OSA_NULL;
    }

    return;
}

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intClipDecodeVideoUpToCurrentTime()
 * @brief    Jump to the previous RAP and decode up to the current video time
 * @param   pClipCtxt    (IN) Internal clip context
 * @param   iCts        (IN) Target CTS
 ******************************************************************************
 */
M4OSA_ERR M4VSS3GPP_intClipDecodeVideoUpToCts( M4VSS3GPP_ClipContext *pClipCtxt,
                                              M4OSA_Int32 iCts )
{
    M4OSA_Int32 iRapCts, iClipCts;
    M4_MediaTime dDecodeTime;
    M4OSA_Bool bClipJump = M4OSA_FALSE;
    M4OSA_ERR err;

    /**
    * Compute the time in the clip base */
    iClipCts = iCts - pClipCtxt->iVoffset;

    /**
    * If we were reading the clip, we must jump to the previous RAP
    * to decode from that point. */
    if( M4VSS3GPP_kClipStatus_READ == pClipCtxt->Vstatus )
    {
        /**
        * The decoder must be told to jump */
        bClipJump = M4OSA_TRUE;
        pClipCtxt->iVideoDecCts = iClipCts;

        /**
        * Remember the clip reading state */
        pClipCtxt->Vstatus = M4VSS3GPP_kClipStatus_DECODE_UP_TO;
    }

    /**
    * If we are in decodeUpTo() process, check if we need to do
    one more step or if decoding is finished */
    if( M4VSS3GPP_kClipStatus_DECODE_UP_TO == pClipCtxt->Vstatus )
    {
        /* Do a step of 500 ms decoding */
        pClipCtxt->iVideoDecCts += 500;

        if( pClipCtxt->iVideoDecCts > iClipCts )
        {
            /* Target time reached, we switch back to DECODE mode */
            pClipCtxt->iVideoDecCts = iClipCts;
            pClipCtxt->Vstatus = M4VSS3GPP_kClipStatus_DECODE;
        }

        M4OSA_TRACE2_1("c ,,,, decode up to : %ld", pClipCtxt->iVideoDecCts);
    }
    else
    {
        /* Just decode at current clip cts */
        pClipCtxt->iVideoDecCts = iClipCts;

        M4OSA_TRACE2_1("d ,,,, decode up to : %ld", pClipCtxt->iVideoDecCts);
    }

    /**
    * Decode up to the target */
    M4OSA_TRACE3_2(
        "M4VSS3GPP_intClipDecodeVideoUpToCts: Decoding upTo CTS %.3f, pClipCtxt=0x%x",
        dDecodeTime, pClipCtxt);

    dDecodeTime = (M4OSA_Double)pClipCtxt->iVideoDecCts;
    pClipCtxt->isRenderDup = M4OSA_FALSE;
    err =
        pClipCtxt->ShellAPI.m_pVideoDecoder->m_pFctDecode(pClipCtxt->pViDecCtxt,
        &dDecodeTime, bClipJump, 0);

    if( ( M4NO_ERROR != err) && (M4WAR_NO_MORE_AU != err)
        && (err != M4WAR_VIDEORENDERER_NO_NEW_FRAME) )
    {
        M4OSA_TRACE1_1(
            "M4VSS3GPP_intClipDecodeVideoUpToCts: m_pFctDecode returns 0x%x!",
            err);
        return err;
    }

    if( err == M4WAR_VIDEORENDERER_NO_NEW_FRAME )
    {
        pClipCtxt->isRenderDup = M4OSA_TRUE;
    }

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

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intClipReadNextAudioFrame()
 * @brief    Read one AU frame in the clip
 * @note
 * @param   pClipCtxt            (IN) Internal clip context
 * @return    M4NO_ERROR:            No error
 ******************************************************************************
 */
M4OSA_ERR M4VSS3GPP_intClipReadNextAudioFrame(
    M4VSS3GPP_ClipContext *pClipCtxt )
{
    M4OSA_ERR err;

    /* ------------------------------ */
    /* ---------- NO AUDIO ---------- */
    /* ------------------------------ */

    if( M4OSA_NULL == pClipCtxt->pAudioStream )
    {
        /* If there is no audio track, we return silence AUs */
        pClipCtxt->pAudioFramePtr =
            (M4OSA_MemAddr8)pClipCtxt->pSilenceFrameData;
        pClipCtxt->uiAudioFrameSize = pClipCtxt->uiSilenceFrameSize;
        pClipCtxt->iAudioFrameCts += pClipCtxt->iSilenceFrameDuration;

        M4OSA_TRACE2_0("b #### blank track");
    }

    /* ---------------------------------- */
    /* ---------- AMR-NB, EVRC ---------- */
    /* ---------------------------------- */

    else if( ( M4VIDEOEDITING_kAMR_NB
        == pClipCtxt->pSettings->ClipProperties.AudioStreamType)
        || (M4VIDEOEDITING_kEVRC
        == pClipCtxt->pSettings->ClipProperties.AudioStreamType) )
    {
        if( M4OSA_FALSE == pClipCtxt->bAudioFrameAvailable )
        {
            /**
            * No AU available, so we must must read one from the original track reader */
            err = pClipCtxt->ShellAPI.m_pReaderDataIt->m_pFctGetNextAu(
                pClipCtxt->pReaderContext,
                (M4_StreamHandler *)pClipCtxt->pAudioStream,
                &pClipCtxt->AudioAU);

            if( M4NO_ERROR == err )
            {
                /**
                * Set the current AMR frame position at the beginning of the read AU */
                pClipCtxt->pAudioFramePtr = pClipCtxt->AudioAU.m_dataAddress;

                /**
                * Set the AMR frame CTS */
                pClipCtxt->iAudioFrameCts =
                    (M4OSA_Int32)(pClipCtxt->AudioAU.m_CTS
                    * pClipCtxt->scale_audio + 0.5);
            }
            else if( ( M4WAR_NO_MORE_AU == err) && (M4VIDEOEDITING_kAMR_NB
                == pClipCtxt->pSettings->ClipProperties.AudioStreamType) )
            {
                /**
                * If there is less audio than the stream duration indicated,
                * we return silence at the end of the stream. */
                pClipCtxt->pAudioFramePtr =
                    (M4OSA_MemAddr8)pClipCtxt->pSilenceFrameData;
                pClipCtxt->uiAudioFrameSize = pClipCtxt->uiSilenceFrameSize;
                pClipCtxt->iAudioFrameCts += pClipCtxt->iSilenceFrameDuration;

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

                /**
                * Return with M4WAR_NO_MORE_AU */
                M4OSA_TRACE3_0(
                    "M4VSS3GPP_intClipReadNextAudioFrame()-AMR: \
                    returning M4WAR_NO_MORE_AU (silence)");
                return M4WAR_NO_MORE_AU;
            }
            else /**< fatal error (or no silence in EVRC) */
            {
                M4OSA_TRACE3_1(
                    "M4VSS3GPP_intClipReadNextAudioFrame()-AMR: m_pFctGetNextAu() returns 0x%x",
                    err);
                return err;
            }
        }
        else /* bAudioFrameAvailable */
        {
            /**
            * Go to the next AMR frame in the AU */
            pClipCtxt->pAudioFramePtr += pClipCtxt->uiAudioFrameSize;

            /**
            * Increment CTS: one AMR frame is 20 ms long */
            pClipCtxt->iAudioFrameCts += pClipCtxt->iSilenceFrameDuration;
        }

        /**
        * Get the size of the pointed AMR frame */
        switch( pClipCtxt->pSettings->ClipProperties.AudioStreamType )
        {
            case M4VIDEOEDITING_kAMR_NB:
                pClipCtxt->uiAudioFrameSize =
                    (M4OSA_UInt16)M4VSS3GPP_intGetFrameSize_AMRNB(
                    pClipCtxt->pAudioFramePtr);
                break;

            case M4VIDEOEDITING_kEVRC:
                pClipCtxt->uiAudioFrameSize =
                    (M4OSA_UInt16)M4VSS3GPP_intGetFrameSize_EVRC(
                    pClipCtxt->pAudioFramePtr);
                break;
            default:
                break;
        }

        if( 0 == pClipCtxt->uiAudioFrameSize )
        {
            M4OSA_TRACE3_0(
                "M4VSS3GPP_intClipReadNextAudioFrame()-AMR: AU frame size == 0,\
                returning M4VSS3GPP_ERR_INPUT_AUDIO_CORRUPTED_AMR_AU");
            return M4VSS3GPP_ERR_INPUT_AUDIO_CORRUPTED_AU;
        }
        else if( pClipCtxt->uiAudioFrameSize > pClipCtxt->AudioAU.m_size )
        {
            M4OSA_TRACE3_0(
                "M4VSS3GPP_intClipReadNextAudioFrame()-AMR: AU frame size greater than AU size!,\
                returning M4VSS3GPP_ERR_INPUT_AUDIO_CORRUPTED_AMR_AU");
            return M4VSS3GPP_ERR_INPUT_AUDIO_CORRUPTED_AU;
        }

        /**
        * Check if the end of the current AU has been reached or not */
        if( ( pClipCtxt->pAudioFramePtr + pClipCtxt->uiAudioFrameSize)
            < (pClipCtxt->AudioAU.m_dataAddress + pClipCtxt->AudioAU.m_size) )
        {
            pClipCtxt->bAudioFrameAvailable = M4OSA_TRUE;
        }
        else
        {
            pClipCtxt->bAudioFrameAvailable =
                M4OSA_FALSE; /**< will be used for next call */
        }
    }

    /* ------------------------- */
    /* ---------- AAC ---------- */
    /* ------------------------- */

    else if( ( M4VIDEOEDITING_kAAC
        == pClipCtxt->pSettings->ClipProperties.AudioStreamType)
        || (M4VIDEOEDITING_kAACplus
        == pClipCtxt->pSettings->ClipProperties.AudioStreamType)
        || (M4VIDEOEDITING_keAACplus
        == pClipCtxt->pSettings->ClipProperties.AudioStreamType) )
    {
        err = pClipCtxt->ShellAPI.m_pReaderDataIt->m_pFctGetNextAu(
            pClipCtxt->pReaderContext,
            (M4_StreamHandler *)pClipCtxt->pAudioStream,
            &pClipCtxt->AudioAU);

        if( M4NO_ERROR == err )
        {
            pClipCtxt->pAudioFramePtr = pClipCtxt->AudioAU.m_dataAddress;
            pClipCtxt->uiAudioFrameSize =
                (M4OSA_UInt16)pClipCtxt->AudioAU.m_size;
            pClipCtxt->iAudioFrameCts =
                (M4OSA_Int32)(pClipCtxt->AudioAU.m_CTS * pClipCtxt->scale_audio
                + 0.5);

            /* Patch because m_CTS is unfortunately rounded in 3gp reader shell */
            /* (cts is not an integer with frequency 24 kHz for example) */
            pClipCtxt->iAudioFrameCts = ( ( pClipCtxt->iAudioFrameCts
                + pClipCtxt->iSilenceFrameDuration / 2)
                / pClipCtxt->iSilenceFrameDuration)
                * pClipCtxt->iSilenceFrameDuration;
        }
        else if( M4WAR_NO_MORE_AU == err )
        {
            /**
            * If there is less audio than the stream duration indicated,
            * we return silence at the end of the stream. */
            pClipCtxt->pAudioFramePtr =
                (M4OSA_MemAddr8)pClipCtxt->pSilenceFrameData;
            pClipCtxt->uiAudioFrameSize = pClipCtxt->uiSilenceFrameSize;
            pClipCtxt->iAudioFrameCts += pClipCtxt->iSilenceFrameDuration;

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

            /**
            * Return with M4WAR_NO_MORE_AU */
            M4OSA_TRACE3_0(
                "M4VSS3GPP_intClipReadNextAudioFrame()-AAC:\
                returning M4WAR_NO_MORE_AU (silence)");
            return M4WAR_NO_MORE_AU;
        }
        else /**< fatal error */
        {
            M4OSA_TRACE3_1(
                "M4VSS3GPP_intClipReadNextAudioFrame()-AAC: m_pFctGetNextAu() returns 0x%x",
                err);
            return err;
        }
    }

    /* --------------------------------- */
    /* ---------- MP3, others ---------- */
    /* --------------------------------- */

    else
    {
        err = pClipCtxt->ShellAPI.m_pReaderDataIt->m_pFctGetNextAu(
            pClipCtxt->pReaderContext,
            (M4_StreamHandler *)pClipCtxt->pAudioStream,
            &pClipCtxt->AudioAU);

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

        pClipCtxt->pAudioFramePtr = pClipCtxt->AudioAU.m_dataAddress;
        pClipCtxt->uiAudioFrameSize = (M4OSA_UInt16)pClipCtxt->AudioAU.m_size;
        pClipCtxt->iAudioFrameCts =
            (M4OSA_Int32)(pClipCtxt->AudioAU.m_CTS * pClipCtxt->scale_audio
            + 0.5);
    }

    /**
    * Return with no error */
    M4OSA_TRACE3_0(
        "M4VSS3GPP_intClipReadNextAudioFrame(): returning M4NO_ERROR");

    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intClipPrepareAudioDecoder()
 * @brief    Creates and initialize the audio decoder for the clip.
 * @note
 * @param   pClipCtxt        (IN) internal clip context
 * @return    M4NO_ERROR:            No error
 ******************************************************************************
 */
static M4OSA_ERR M4VSS3GPP_intClipPrepareAudioDecoder(
    M4VSS3GPP_ClipContext *pClipCtxt )
{
    M4OSA_ERR err = M4NO_ERROR;
    M4_StreamType audiotype;
#ifdef M4VSS_SUPPORT_OMX_CODECS

    M4_AACType iAacType = 0;

#endif

    /**
    * Set the proper audio decoder */

    audiotype = pClipCtxt->pAudioStream->m_basicProperties.m_streamType;

    //EVRC
    if( M4DA_StreamTypeAudioEvrc
        != audiotype ) /* decoder not supported yet, but allow to do null encoding */

        err = M4VSS3GPP_setCurrentAudioDecoder(&pClipCtxt->ShellAPI, audiotype);
    M4ERR_CHECK_RETURN(err);

    /**
    * Creates the audio decoder */
    if( M4OSA_NULL == pClipCtxt->ShellAPI.m_pAudioDecoder )
    {
        M4OSA_TRACE1_0(
            "M4VSS3GPP_intClipPrepareAudioDecoder(): Fails to initiate the audio decoder.");
        return M4VSS3GPP_ERR_AUDIO_DECODER_INIT_FAILED;
    }

    if( M4OSA_NULL == pClipCtxt->pAudioDecCtxt )
    {
#ifdef M4VSS_SUPPORT_OMX_CODECS

        if( M4OSA_TRUE == pClipCtxt->ShellAPI.bAllowFreeingOMXCodecInterface )
        {
            if( M4DA_StreamTypeAudioAac == audiotype ) {
                err = M4VSS3GPP_intCheckAndGetCodecAacProperties(
                       pClipCtxt);
            } else if (M4DA_StreamTypeAudioPcm != audiotype) {
                err = pClipCtxt->ShellAPI.m_pAudioDecoder->m_pFctCreateAudioDec(
                &pClipCtxt->pAudioDecCtxt, pClipCtxt->pAudioStream,
                M4OSA_NULL);
            } else {
                err = M4NO_ERROR;
            }
            if( M4NO_ERROR != err )
            {
                M4OSA_TRACE1_1(
                    "M4VSS3GPP_intClipPrepareAudioDecoder: m_pAudioDecoder->m_pFctCreateAudioDec\
                    returns 0x%x", err);
                return err;
            }
        }
        else
        {
            M4OSA_TRACE3_1(
                "M4VSS3GPP_intClipPrepareAudioDecoder:\
                Creating external audio decoder of type 0x%x", audiotype);
            /* External OMX codecs are used*/
            if( M4DA_StreamTypeAudioAac == audiotype )
            {
                err = pClipCtxt->ShellAPI.m_pAudioDecoder->m_pFctCreateAudioDec(
                    &pClipCtxt->pAudioDecCtxt, pClipCtxt->pAudioStream,
                    pClipCtxt->ShellAPI.pCurrentAudioDecoderUserData);

                if( M4NO_ERROR == err )
                {
                    /* AAC properties*/
                    /*get from Reader; temporary, till Audio decoder shell API
                      available to get the AAC properties*/
                    pClipCtxt->AacProperties.aNumChan =
                        pClipCtxt->pAudioStream->m_nbChannels;
                    pClipCtxt->AacProperties.aSampFreq =
                        pClipCtxt->pAudioStream->m_samplingFrequency;

                    err = pClipCtxt->ShellAPI.m_pAudioDecoder->
                        m_pFctGetOptionAudioDec(pClipCtxt->pAudioDecCtxt,
                        M4AD_kOptionID_StreamType,
                        (M4OSA_DataOption) &iAacType);

                    if( M4NO_ERROR != err )
                    {
                        M4OSA_TRACE1_1(
                            "M4VSS3GPP_intClipPrepareAudioDecoder:\
                            m_pAudioDecoder->m_pFctGetOptionAudioDec returns err 0x%x", err);
                        iAacType = M4_kAAC; //set to default
                        err = M4NO_ERROR;
                    }
                    else {
                        M4OSA_TRACE3_1(
                        "M4VSS3GPP_intClipPrepareAudioDecoder: \
                        m_pAudioDecoder->m_pFctGetOptionAudioDec returns streamType %d",
                        iAacType);
                       }
                    switch( iAacType )
                    {
                        case M4_kAAC:
                            pClipCtxt->AacProperties.aSBRPresent = 0;
                            pClipCtxt->AacProperties.aPSPresent = 0;
                            break;

                        case M4_kAACplus:
                            pClipCtxt->AacProperties.aSBRPresent = 1;
                            pClipCtxt->AacProperties.aPSPresent = 0;
                            pClipCtxt->AacProperties.aExtensionSampFreq =
                                pClipCtxt->pAudioStream->m_samplingFrequency;
                            break;

                        case M4_keAACplus:
                            pClipCtxt->AacProperties.aSBRPresent = 1;
                            pClipCtxt->AacProperties.aPSPresent = 1;
                            pClipCtxt->AacProperties.aExtensionSampFreq =
                                pClipCtxt->pAudioStream->m_samplingFrequency;
                            break;
                        default:
                            break;
                    }
                    M4OSA_TRACE3_2(
                        "M4VSS3GPP_intClipPrepareAudioDecoder: AAC NBChans=%d, SamplFreq=%d",
                        pClipCtxt->AacProperties.aNumChan,
                        pClipCtxt->AacProperties.aSampFreq);
                }
            }
            else
                err = pClipCtxt->ShellAPI.m_pAudioDecoder->m_pFctCreateAudioDec(
                &pClipCtxt->pAudioDecCtxt, pClipCtxt->pAudioStream,
                pClipCtxt->ShellAPI.pCurrentAudioDecoderUserData);

            if( M4NO_ERROR != err )
            {
                M4OSA_TRACE1_1(
                    "M4VSS3GPP_intClipPrepareAudioDecoder:\
                    m_pAudioDecoder->m_pFctCreateAudioDec returns 0x%x",
                    err);
                return err;
            }
        }

#else
        /* Trick, I use pUserData to retrieve aac properties,
           waiting for some better implementation... */

        if( M4DA_StreamTypeAudioAac == audiotype )
            err = pClipCtxt->ShellAPI.m_pAudioDecoder->m_pFctCreateAudioDec(
            &pClipCtxt->pAudioDecCtxt,
            pClipCtxt->pAudioStream, &(pClipCtxt->AacProperties));
        else
            err = pClipCtxt->ShellAPI.m_pAudioDecoder->m_pFctCreateAudioDec(
            &pClipCtxt->pAudioDecCtxt, pClipCtxt->pAudioStream,
            M4OSA_NULL /* to be changed with HW interfaces */);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intClipPrepareAudioDecoder:\
                m_pAudioDecoder->m_pFctCreateAudioDec returns 0x%x",
                err);
            return err;
        }

#endif

    }

    if( M4DA_StreamTypeAudioAmrNarrowBand == audiotype ) {
        /* AMR DECODER CONFIGURATION */

        /* nothing specific to do */
    }
    else if( M4DA_StreamTypeAudioEvrc == audiotype ) {
        /* EVRC DECODER CONFIGURATION */

        /* nothing specific to do */
    }
    else if( M4DA_StreamTypeAudioMp3 == audiotype ) {
        /* MP3 DECODER CONFIGURATION */

        /* nothing specific to do */
    }
    else if( M4DA_StreamTypeAudioAac == audiotype )
    {
        /* AAC DECODER CONFIGURATION */

        /* Decode high quality aac but disable PS and SBR */
        /* Because we have to mix different kind of AAC so we must take the lowest capability */
        /* In MCS it was not needed because there is only one stream */
        M4_AacDecoderConfig AacDecParam;

        AacDecParam.m_AACDecoderProfile = AAC_kAAC;
        AacDecParam.m_DownSamplingMode = AAC_kDS_OFF;

        if( M4ENCODER_kMono == pClipCtxt->pAudioStream->m_nbChannels )
        {
            AacDecParam.m_OutputMode = AAC_kMono;
        }
        else
        {
            AacDecParam.m_OutputMode = AAC_kStereo;
        }

        err = pClipCtxt->ShellAPI.m_pAudioDecoder->m_pFctSetOptionAudioDec(
            pClipCtxt->pAudioDecCtxt,
            M4AD_kOptionID_UserParam, (M4OSA_DataOption) &AacDecParam);
    }

    if( M4OSA_NULL != pClipCtxt->ShellAPI.m_pAudioDecoder->m_pFctSetOptionAudioDec ) {
        pClipCtxt->ShellAPI.m_pAudioDecoder->m_pFctSetOptionAudioDec(
         pClipCtxt->pAudioDecCtxt, M4AD_kOptionID_3gpReaderInterface,
         (M4OSA_DataOption) pClipCtxt->ShellAPI.m_pReaderDataIt);

        pClipCtxt->ShellAPI.m_pAudioDecoder->m_pFctSetOptionAudioDec(
         pClipCtxt->pAudioDecCtxt, M4AD_kOptionID_AudioAU,
         (M4OSA_DataOption) &pClipCtxt->AudioAU);
    }

    if( M4OSA_NULL != pClipCtxt->ShellAPI.m_pAudioDecoder->m_pFctStartAudioDec )
    {
        /* Not implemented in all decoders */
        err = pClipCtxt->ShellAPI.m_pAudioDecoder->m_pFctStartAudioDec(
            pClipCtxt->pAudioDecCtxt);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intClipPrepareAudioDecoder:\
                m_pAudioDecoder->m_pFctStartAudioDec returns 0x%x",
                err);
            return err;
        }
    }

    /**
    * Allocate output buffer for the audio decoder */
    pClipCtxt->AudioDecBufferOut.m_bufferSize =
        pClipCtxt->pAudioStream->m_byteFrameLength
        * pClipCtxt->pAudioStream->m_byteSampleSize
        * pClipCtxt->pAudioStream->m_nbChannels;
    pClipCtxt->AudioDecBufferOut.m_dataAddress =
        (M4OSA_MemAddr8)M4OSA_32bitAlignedMalloc(pClipCtxt->AudioDecBufferOut.m_bufferSize
        * sizeof(M4OSA_Int16),
        M4VSS3GPP, (M4OSA_Char *)"AudioDecBufferOut.m_bufferSize");

    if( M4OSA_NULL == pClipCtxt->AudioDecBufferOut.m_dataAddress )
    {
        M4OSA_TRACE1_0(
            "M4VSS3GPP_intClipPrepareAudioDecoder():\
            unable to allocate AudioDecBufferOut.m_dataAddress, returning M4ERR_ALLOC");
        return M4ERR_ALLOC;
    }

    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intClipDecodeCurrentAudioFrame()
 * @brief    Decode the current AUDIO frame.
 * @note
 * @param   pClipCtxt        (IN) internal clip context
 * @return    M4NO_ERROR:            No error
 ******************************************************************************
 */
M4OSA_ERR M4VSS3GPP_intClipDecodeCurrentAudioFrame(
    M4VSS3GPP_ClipContext *pClipCtxt )
{
    M4OSA_ERR err;

    /**
    * Silence mode */
    if( pClipCtxt->pSilenceFrameData
        == (M4OSA_UInt8 *)pClipCtxt->pAudioFramePtr )
    {
        if( pClipCtxt->AudioDecBufferOut.m_dataAddress == M4OSA_NULL )
        {
            /**
            * Allocate output buffer for the audio decoder */
            pClipCtxt->AudioDecBufferOut.m_bufferSize =
                pClipCtxt->uiSilencePcmSize;
            pClipCtxt->AudioDecBufferOut.m_dataAddress =
                (M4OSA_MemAddr8)M4OSA_32bitAlignedMalloc(
                pClipCtxt->AudioDecBufferOut.m_bufferSize
                * sizeof(M4OSA_Int16),
                M4VSS3GPP,(M4OSA_Char *) "AudioDecBufferOut.m_bufferSize");

            if( M4OSA_NULL == pClipCtxt->AudioDecBufferOut.m_dataAddress )
            {
                M4OSA_TRACE1_0(
                    "M4VSS3GPP_intClipDecodeCurrentAudioFrame():\
                    unable to allocate AudioDecBufferOut.m_dataAddress, returning M4ERR_ALLOC");
                return M4ERR_ALLOC;
            }
        }

        /* Fill it with 0 (= pcm silence) */
        memset(pClipCtxt->AudioDecBufferOut.m_dataAddress,0,
             pClipCtxt->AudioDecBufferOut.m_bufferSize * sizeof(M4OSA_Int16));
    }
    else if (pClipCtxt->pSettings->FileType == M4VIDEOEDITING_kFileType_PCM)
    {
        pClipCtxt->AudioDecBufferIn.m_dataAddress = (M4OSA_MemAddr8) pClipCtxt->pAudioFramePtr;
        pClipCtxt->AudioDecBufferIn.m_bufferSize  = pClipCtxt->uiAudioFrameSize;

        memcpy((void *)pClipCtxt->AudioDecBufferOut.m_dataAddress,
            (void *)pClipCtxt->AudioDecBufferIn.m_dataAddress, pClipCtxt->AudioDecBufferIn.m_bufferSize);
        pClipCtxt->AudioDecBufferOut.m_bufferSize = pClipCtxt->AudioDecBufferIn.m_bufferSize;
        /**
        * Return with no error */

        M4OSA_TRACE3_0("M4VSS3GPP_intClipDecodeCurrentAudioFrame(): returning M4NO_ERROR");
        return M4NO_ERROR;
    }
    /**
    * Standard decoding mode */
    else
    {
        /**
        * Decode current AMR frame */
        if ( pClipCtxt->pAudioFramePtr != M4OSA_NULL ) {
            pClipCtxt->AudioDecBufferIn.m_dataAddress =
             (M4OSA_MemAddr8)pClipCtxt->pAudioFramePtr;
            pClipCtxt->AudioDecBufferIn.m_bufferSize =
             pClipCtxt->uiAudioFrameSize;
            pClipCtxt->AudioDecBufferIn.m_timeStampUs =
             (int64_t) (pClipCtxt->iAudioFrameCts * 1000LL);

            err = pClipCtxt->ShellAPI.m_pAudioDecoder->m_pFctStepAudioDec(
             pClipCtxt->pAudioDecCtxt,
             &pClipCtxt->AudioDecBufferIn, &pClipCtxt->AudioDecBufferOut,
             M4OSA_FALSE);
        } else {
            // Pass Null input buffer
            // Reader invoked from Audio decoder source
            err = pClipCtxt->ShellAPI.m_pAudioDecoder->m_pFctStepAudioDec(
             pClipCtxt->pAudioDecCtxt,
             M4OSA_NULL, &pClipCtxt->AudioDecBufferOut,
             M4OSA_FALSE);
        }

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intClipDecodeCurrentAudioFrame():\
                m_pAudioDecoder->m_pFctStepAudio returns 0x%x",
                err);
            return err;
        }
    }

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

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intClipJumpAudioAt()
 * @brief    Jump in the audio track of the clip.
 * @note
 * @param   pClipCtxt            (IN) internal clip context
 * @param   pJumpCts            (IN/OUT) in:target CTS, out: reached CTS
 * @return    M4NO_ERROR:            No error
 ******************************************************************************
 */
M4OSA_ERR M4VSS3GPP_intClipJumpAudioAt( M4VSS3GPP_ClipContext *pClipCtxt,
                                       M4OSA_Int32 *pJumpCts )
{
    M4OSA_ERR err;
    M4OSA_Int32 iTargetCts;
    M4OSA_Int32 iJumpCtsMs;

    /**
    *    Check input parameters */
    M4OSA_DEBUG_IF2((M4OSA_NULL == pClipCtxt), M4ERR_PARAMETER,
        "M4VSS3GPP_intClipJumpAudioAt: pClipCtxt is M4OSA_NULL");
    M4OSA_DEBUG_IF2((M4OSA_NULL == pJumpCts), M4ERR_PARAMETER,
        "M4VSS3GPP_intClipJumpAudioAt: pJumpCts is M4OSA_NULL");

    iTargetCts = *pJumpCts;

    /**
    * If there is no audio stream, we simulate a jump at the target jump CTS */
    if( M4OSA_NULL == pClipCtxt->pAudioStream )
    {
        /**
        * the target CTS will be reached at next ReadFrame call (thus the -20) */
        *pJumpCts = iTargetCts - pClipCtxt->iSilenceFrameDuration;

        /* Patch because m_CTS is unfortunately rounded in 3gp reader shell */
        /* (cts is not an integer with frequency 24 kHz for example) */
        *pJumpCts = ( ( *pJumpCts + pClipCtxt->iSilenceFrameDuration / 2)
            / pClipCtxt->iSilenceFrameDuration)
            * pClipCtxt->iSilenceFrameDuration;
        pClipCtxt->iAudioFrameCts =
            *
            pJumpCts; /* simulate a read at jump position for later silence AUs */
    }
    else
    {
        M4OSA_Int32 current_time = 0;
        M4OSA_Int32 loop_counter = 0;

        if( (M4DA_StreamTypeAudioMp3
            == pClipCtxt->pAudioStream->m_basicProperties.m_streamType) )
        {
            while( ( loop_counter < M4VSS3GPP_MP3_JUMPED_AU_NUMBER_MAX)
                && (current_time < iTargetCts) )
            {
                err = pClipCtxt->ShellAPI.m_pReaderDataIt->m_pFctGetNextAu(
                    pClipCtxt->pReaderContext,
                    (M4_StreamHandler *)pClipCtxt->pAudioStream,
                    &pClipCtxt->AudioAU);

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

                current_time = (M4OSA_Int32)pClipCtxt->AudioAU.m_CTS;
                loop_counter++;
            }

            /**
            * The current AU is stored */
            pClipCtxt->pAudioFramePtr = pClipCtxt->AudioAU.m_dataAddress;
            pClipCtxt->uiAudioFrameSize =
                (M4OSA_UInt16)pClipCtxt->AudioAU.m_size;
            pClipCtxt->iAudioFrameCts =
                (M4OSA_Int32)(pClipCtxt->AudioAU.m_CTS * pClipCtxt->scale_audio
                + 0.5);

            *pJumpCts = pClipCtxt->iAudioFrameCts;
        }
        else
        {
            /**
            * Jump in the audio stream */
            iJumpCtsMs =
                (M4OSA_Int32)(*pJumpCts / pClipCtxt->scale_audio + 0.5);

            err = pClipCtxt->ShellAPI.m_pReader->m_pFctJump(
                pClipCtxt->pReaderContext,
                (M4_StreamHandler *)pClipCtxt->pAudioStream,
                &iJumpCtsMs);

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

            *pJumpCts =
                (M4OSA_Int32)(iJumpCtsMs * pClipCtxt->scale_audio + 0.5);

            /* Patch because m_CTS is unfortunately rounded in 3gp reader shell */
            /* (cts is not an integer with frequency 24 kHz for example) */
            *pJumpCts = ( ( *pJumpCts + pClipCtxt->iSilenceFrameDuration / 2)
                / pClipCtxt->iSilenceFrameDuration)
                * pClipCtxt->iSilenceFrameDuration;
            pClipCtxt->iAudioFrameCts = 0; /* No frame read yet */

            /**
            * To detect some may-be bugs, I prefer to reset all these after a jump */
            pClipCtxt->bAudioFrameAvailable = M4OSA_FALSE;
            pClipCtxt->pAudioFramePtr = M4OSA_NULL;

            /**
            * In AMR, we have to manage multi-framed AUs,
            but also in AAC the jump can be 1 AU too much backward */
            if( *pJumpCts < iTargetCts )
            {
                /**
                * Jump doesn't read any AU, we must read at least one */
                err = M4VSS3GPP_intClipReadNextAudioFrame(pClipCtxt);

                if( M4OSA_ERR_IS_ERROR(err) )
                {
                    M4OSA_TRACE1_1(
                        "M4VSS3GPP_intClipJumpAudioAt():\
                        M4VSS3GPP_intClipReadNextAudioFrame(a) returns 0x%x",
                        err);
                    return err;
                }

                /**
                * Read AU frames as long as we reach the AU before the target CTS
                * (so the target will be reached when the user call ReadNextAudioFrame). */
                while( pClipCtxt->iAudioFrameCts
                    < (iTargetCts - pClipCtxt->iSilenceFrameDuration) )
                {
                    err = M4VSS3GPP_intClipReadNextAudioFrame(pClipCtxt);

                    if( M4OSA_ERR_IS_ERROR(err) )
                    {
                        M4OSA_TRACE1_1(
                            "M4VSS3GPP_intClipJumpAudioAt():\
                            M4VSS3GPP_intClipReadNextAudioFrame(b) returns 0x%x",
                            err);
                        return err;
                    }
                }

                /**
                * Return the CTS that will be reached at next ReadFrame */
                *pJumpCts = pClipCtxt->iAudioFrameCts
                    + pClipCtxt->iSilenceFrameDuration;
            }
        }
    }

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

/**
 ******************************************************************************
 * M4OSA_ERR M4VSS3GPP_intClipClose()
 * @brief    Close a clip. Destroy the context.
 * @note
 * @param   pClipCtxt            (IN) Internal clip context
 * @return    M4NO_ERROR:            No error
 ******************************************************************************
 */
M4OSA_ERR M4VSS3GPP_intClipClose( M4VSS3GPP_ClipContext *pClipCtxt )
{
    M4OSA_ERR err;

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

    /**
    * Free the video decoder context */
    if( M4OSA_NULL != pClipCtxt->pViDecCtxt )
    {
        pClipCtxt->ShellAPI.m_pVideoDecoder->m_pFctDestroy(
            pClipCtxt->pViDecCtxt);
        pClipCtxt->pViDecCtxt = M4OSA_NULL;
    }

    /**
    * Free the audio decoder context  */
    if( M4OSA_NULL != pClipCtxt->pAudioDecCtxt )
    {
        err = pClipCtxt->ShellAPI.m_pAudioDecoder->m_pFctDestroyAudioDec(
            pClipCtxt->pAudioDecCtxt);

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

        pClipCtxt->pAudioDecCtxt = M4OSA_NULL;
    }

    /**
    * Free the decoded audio buffer */
    if( M4OSA_NULL != pClipCtxt->AudioDecBufferOut.m_dataAddress )
    {
        free(pClipCtxt->AudioDecBufferOut.m_dataAddress);
        pClipCtxt->AudioDecBufferOut.m_dataAddress = M4OSA_NULL;
    }

    /**
    * Audio AU is allocated by reader.
    * If no audio track, audio AU is set at 'silent' (SID) by VSS.
    * As a consequence, if audio AU is set to 'silent' (static)
    it can't be free unless it is set to NULL */
    if( ( (M4OSA_MemAddr8)M4VSS3GPP_AMR_AU_SILENCE_FRAME_048
        == pClipCtxt->AudioAU.m_dataAddress)
        || ((M4OSA_MemAddr8)pClipCtxt->pSilenceFrameData
        == pClipCtxt->AudioAU.m_dataAddress) )
    {
        pClipCtxt->AudioAU.m_dataAddress = M4OSA_NULL;
    }

    if( M4OSA_NULL != pClipCtxt->pReaderContext )
    {
        /**
        * Close the 3GPP or MP3 reader */
        err = pClipCtxt->ShellAPI.m_pReader->m_pFctClose(
            pClipCtxt->pReaderContext);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intClipClose(): m_pReader->m_pFctClose returns 0x%x",
                err);
        }

        /**
        * Destroy the 3GPP or MP3 reader context */
        err = pClipCtxt->ShellAPI.m_pReader->m_pFctDestroy(
            pClipCtxt->pReaderContext);

        if( M4NO_ERROR != err )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intClipClose(): m_pReader->m_pFctDestroy returns 0x%x",
                err);
        }

        pClipCtxt->pReaderContext = M4OSA_NULL;
    }

    /**
    * Return with no error */
    M4OSA_TRACE3_1("M4VSS3GPP_intClipClose(Ctxt=0x%x): returning M4NO_ERROR",
        pClipCtxt);
    return M4NO_ERROR;
}

M4OSA_ERR M4VSS3GPP_intClipCleanUp( M4VSS3GPP_ClipContext *pClipCtxt )
{
    M4OSA_ERR err = M4NO_ERROR, err2;

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

    /**
    * Free the video decoder context */
    if( M4OSA_NULL != pClipCtxt->pViDecCtxt )
    {
        pClipCtxt->ShellAPI.m_pVideoDecoder->m_pFctDestroy(
            pClipCtxt->pViDecCtxt);
        pClipCtxt->pViDecCtxt = M4OSA_NULL;
    }

    /**
    * Free the audio decoder context  */
    if( M4OSA_NULL != pClipCtxt->pAudioDecCtxt )
    {
        err2 = pClipCtxt->ShellAPI.m_pAudioDecoder->m_pFctDestroyAudioDec(
            pClipCtxt->pAudioDecCtxt);

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

        pClipCtxt->pAudioDecCtxt = M4OSA_NULL;
    }

    /**
    * Free the decoded audio buffer */
    if( M4OSA_NULL != pClipCtxt->AudioDecBufferOut.m_dataAddress )
    {
        free(pClipCtxt->AudioDecBufferOut.m_dataAddress);
        pClipCtxt->AudioDecBufferOut.m_dataAddress = M4OSA_NULL;
    }

    /**
    * Audio AU is allocated by reader.
    * If no audio track, audio AU is set at 'silent' (SID) by VSS.
    * As a consequence, if audio AU is set to 'silent' (static)
    it can't be free unless it is set to NULL */
    if( ( (M4OSA_MemAddr8)M4VSS3GPP_AMR_AU_SILENCE_FRAME_048
        == pClipCtxt->AudioAU.m_dataAddress)
        || ((M4OSA_MemAddr8)pClipCtxt->pSilenceFrameData
        == pClipCtxt->AudioAU.m_dataAddress) )
    {
        pClipCtxt->AudioAU.m_dataAddress = M4OSA_NULL;
    }

    if( M4OSA_NULL != pClipCtxt->pReaderContext )
    {
        /**
        * Close the 3GPP or MP3 reader */
        err2 = pClipCtxt->ShellAPI.m_pReader->m_pFctClose(
            pClipCtxt->pReaderContext);

        if( M4NO_ERROR != err2 )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intClipCleanUp(): m_pReader->m_pFctClose returns 0x%x",
                err);

            if( M4NO_ERROR != err )
                err = err2;
        }

        /**
        * Destroy the 3GPP or MP3 reader context */
        err2 = pClipCtxt->ShellAPI.m_pReader->m_pFctDestroy(
            pClipCtxt->pReaderContext);

        if( M4NO_ERROR != err2 )
        {
            M4OSA_TRACE1_1(
                "M4VSS3GPP_intClipCleanUp(): m_pReader->m_pFctDestroy returns 0x%x",
                err);

            if( M4NO_ERROR != err )
                err = err2;
        }

        pClipCtxt->pReaderContext = M4OSA_NULL;
    }

    if(pClipCtxt->pPlaneYuv != M4OSA_NULL) {
        if(pClipCtxt->pPlaneYuv[0].pac_data != M4OSA_NULL) {
            free(pClipCtxt->pPlaneYuv[0].pac_data);
            pClipCtxt->pPlaneYuv[0].pac_data = M4OSA_NULL;
        }
        free(pClipCtxt->pPlaneYuv);
        pClipCtxt->pPlaneYuv = M4OSA_NULL;
    }

    if(pClipCtxt->pPlaneYuvWithEffect != M4OSA_NULL) {
        if(pClipCtxt->pPlaneYuvWithEffect[0].pac_data != M4OSA_NULL) {
            free(pClipCtxt->pPlaneYuvWithEffect[0].pac_data);
            pClipCtxt->pPlaneYuvWithEffect[0].pac_data = M4OSA_NULL;
        }
        free(pClipCtxt->pPlaneYuvWithEffect);
        pClipCtxt->pPlaneYuvWithEffect = M4OSA_NULL;
    }
    /**
    * Free the shells interfaces */
    M4VSS3GPP_unRegisterAllWriters(&pClipCtxt->ShellAPI);
    M4VSS3GPP_unRegisterAllEncoders(&pClipCtxt->ShellAPI);
    M4VSS3GPP_unRegisterAllReaders(&pClipCtxt->ShellAPI);
    M4VSS3GPP_unRegisterAllDecoders(&pClipCtxt->ShellAPI);

    M4OSA_TRACE3_1("M4VSS3GPP_intClipCleanUp: pClipCtxt=0x%x", pClipCtxt);
    /**
    * Free the clip context */
    free(pClipCtxt);

    return err;
}

/**
 ******************************************************************************
 * M4OSA_UInt32 M4VSS3GPP_intGetFrameSize_AMRNB()
 * @brief   Return the length, in bytes, of the AMR Narrow-Band frame contained in the given buffer
 * @note
 * @param   pAudioFrame   (IN) AMRNB frame
 * @return  M4NO_ERROR: No error
 ******************************************************************************
 */

M4OSA_UInt32 M4VSS3GPP_intGetFrameSize_AMRNB( M4OSA_MemAddr8 pAudioFrame )
{
    M4OSA_UInt32 frameSize = 0;
    M4OSA_UInt32 frameType = ( ( *pAudioFrame) &(0xF << 3)) >> 3;

    switch( frameType )
    {
        case 0:
            frameSize = 95;
            break; /*  4750 bps */

        case 1:
            frameSize = 103;
            break; /*  5150 bps */

        case 2:
            frameSize = 118;
            break; /*  5900 bps */

        case 3:
            frameSize = 134;
            break; /*  6700 bps */

        case 4:
            frameSize = 148;
            break; /*  7400 bps */

        case 5:
            frameSize = 159;
            break; /*  7950 bps */

        case 6:
            frameSize = 204;
            break; /* 10200 bps */

        case 7:
            frameSize = 244;
            break; /* 12000 bps */

        case 8:
            frameSize = 39;
            break; /* SID (Silence) */

        case 15:
            frameSize = 0;
            break; /* No data */

        default:
            M4OSA_TRACE3_0(
                "M4VSS3GPP_intGetFrameSize_AMRNB(): Corrupted AMR frame! returning 0.");
            return 0;
    }

    return (1 + (( frameSize + 7) / 8));
}

/**
 ******************************************************************************
 * M4OSA_UInt32 M4VSS3GPP_intGetFrameSize_EVRC()
 * @brief   Return the length, in bytes, of the EVRC frame contained in the given buffer
 * @note
 *     0 1 2 3
 *    +-+-+-+-+
 *    |fr type|              RFC 3558
 *    +-+-+-+-+
 *
 * Frame Type: 4 bits
 *    The frame type indicates the type of the corresponding codec data
 *    frame in the RTP packet.
 *
 * For EVRC and SMV codecs, the frame type values and size of the
 * associated codec data frame are described in the table below:
 *
 * Value   Rate      Total codec data frame size (in octets)
 * ---------------------------------------------------------
 *   0     Blank      0    (0 bit)
 *   1     1/8        2    (16 bits)
 *   2     1/4        5    (40 bits; not valid for EVRC)
 *   3     1/2       10    (80 bits)
 *   4     1         22    (171 bits; 5 padded at end with zeros)
 *   5     Erasure    0    (SHOULD NOT be transmitted by sender)
 *
 * @param   pCpAudioFrame   (IN) EVRC frame
 * @return  M4NO_ERROR: No error
 ******************************************************************************
 */
M4OSA_UInt32 M4VSS3GPP_intGetFrameSize_EVRC( M4OSA_MemAddr8 pAudioFrame )
{
    M4OSA_UInt32 frameSize = 0;
    M4OSA_UInt32 frameType = ( *pAudioFrame) &0x0F;

    switch( frameType )
    {
        case 0:
            frameSize = 0;
            break; /*  blank */

        case 1:
            frameSize = 16;
            break; /*  1/8 */

        case 2:
            frameSize = 40;
            break; /*  1/4 */

        case 3:
            frameSize = 80;
            break; /*  1/2 */

        case 4:
            frameSize = 171;
            break; /*  1 */

        case 5:
            frameSize = 0;
            break; /*  erasure */

        default:
            M4OSA_TRACE3_0(
                "M4VSS3GPP_intGetFrameSize_EVRC(): Corrupted EVRC frame! returning 0.");
            return 0;
    }

    return (1 + (( frameSize + 7) / 8));
}

M4OSA_ERR M4VSS3GPP_intCheckAndGetCodecAacProperties(
                                 M4VSS3GPP_ClipContext *pClipCtxt) {

    M4OSA_ERR err = M4NO_ERROR;
    M4AD_Buffer outputBuffer;
    uint32_t optionValue =0;

    // Decode first audio frame from clip to get properties from codec

    err = pClipCtxt->ShellAPI.m_pAudioDecoder->m_pFctCreateAudioDec(
                    &pClipCtxt->pAudioDecCtxt,
                    pClipCtxt->pAudioStream, &(pClipCtxt->AacProperties));

    pClipCtxt->ShellAPI.m_pAudioDecoder->m_pFctSetOptionAudioDec(
     pClipCtxt->pAudioDecCtxt, M4AD_kOptionID_3gpReaderInterface,
     (M4OSA_DataOption) pClipCtxt->ShellAPI.m_pReaderDataIt);

    pClipCtxt->ShellAPI.m_pAudioDecoder->m_pFctSetOptionAudioDec(
     pClipCtxt->pAudioDecCtxt, M4AD_kOptionID_AudioAU,
     (M4OSA_DataOption) &pClipCtxt->AudioAU);

    if( pClipCtxt->ShellAPI.m_pAudioDecoder->m_pFctStartAudioDec != M4OSA_NULL ) {

        err = pClipCtxt->ShellAPI.m_pAudioDecoder->m_pFctStartAudioDec(
         pClipCtxt->pAudioDecCtxt);
        if( M4NO_ERROR != err ) {

            M4OSA_TRACE1_1(
                "M4VSS3GPP_intCheckAndGetCodecAacProperties: \
                 m_pFctStartAudioDec returns 0x%x", err);
            return err;
        }
    }

    /**
    * Allocate output buffer for the audio decoder */
    outputBuffer.m_bufferSize =
        pClipCtxt->pAudioStream->m_byteFrameLength
        * pClipCtxt->pAudioStream->m_byteSampleSize
        * pClipCtxt->pAudioStream->m_nbChannels;

    if( outputBuffer.m_bufferSize > 0 ) {

        outputBuffer.m_dataAddress =
            (M4OSA_MemAddr8)M4OSA_32bitAlignedMalloc(outputBuffer.m_bufferSize \
            *sizeof(short), M4VSS3GPP, (M4OSA_Char *)"outputBuffer.m_bufferSize");

        if( M4OSA_NULL == outputBuffer.m_dataAddress ) {

            M4OSA_TRACE1_0(
                "M4VSS3GPP_intCheckAndGetCodecAacProperties():\
                 unable to allocate outputBuffer.m_dataAddress");
            return M4ERR_ALLOC;
        }
    }

    err = pClipCtxt->ShellAPI.m_pAudioDecoder->m_pFctStepAudioDec(
            pClipCtxt->pAudioDecCtxt, M4OSA_NULL, &outputBuffer, M4OSA_FALSE);

    if ( err == M4WAR_INFO_FORMAT_CHANGE ) {

        // Get the properties from codec node
        pClipCtxt->ShellAPI.m_pAudioDecoder->m_pFctGetOptionAudioDec(
         pClipCtxt->pAudioDecCtxt,
           M4AD_kOptionID_AudioNbChannels, (M4OSA_DataOption) &optionValue);

        pClipCtxt->AacProperties.aNumChan = optionValue;
        // Reset Reader structure value also
        pClipCtxt->pAudioStream->m_nbChannels = optionValue;

        pClipCtxt->ShellAPI.m_pAudioDecoder->m_pFctGetOptionAudioDec(
         pClipCtxt->pAudioDecCtxt,
          M4AD_kOptionID_AudioSampFrequency, (M4OSA_DataOption) &optionValue);

        pClipCtxt->AacProperties.aSampFreq = optionValue;
        // Reset Reader structure value also
        pClipCtxt->pAudioStream->m_samplingFrequency = optionValue;

    } else if( err != M4NO_ERROR) {
        M4OSA_TRACE1_1("M4VSS3GPP_intCheckAndGetCodecAacProperties:\
            m_pFctStepAudioDec returns err = 0x%x", err);
    }

    free(outputBuffer.m_dataAddress);

    // Reset the stream reader
    err = pClipCtxt->ShellAPI.m_pReader->m_pFctReset(
     pClipCtxt->pReaderContext,
     (M4_StreamHandler *)pClipCtxt->pAudioStream);

    if (M4NO_ERROR != err) {
        M4OSA_TRACE1_1("M4VSS3GPP_intCheckAndGetCodecAacProperties\
            Error in reseting reader: 0x%x", err);
    }

    return err;

}