summaryrefslogtreecommitdiffstats
path: root/libvideoeditor/vss/src/M4AMRR_CoreReader.c
blob: 630f9dc3af6ca4dea244a5b5e491c390cdc2f750 (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
/*
 * 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        M4AMRR_CoreReader.c
 * @brief       Implementation of AMR parser
 * @note        This file contains the API Implementation for
 *              AMR Parser.
 ******************************************************************************
*/
#include "M4AMRR_CoreReader.h"
#include "M4OSA_Debug.h"
#include "M4OSA_CoreID.h"

/**
 ******************************************************************************
 * Maximum bitrate per amr type
 ******************************************************************************
*/
#define M4AMRR_NB_MAX_BIT_RATE    12200
#define M4AMRR_WB_MAX_BIT_RATE    23850

/**
 ******************************************************************************
 * AMR reader context ID
 ******************************************************************************
*/
#define M4AMRR_CONTEXTID    0x414d5252

/**
 ******************************************************************************
 * An AMR frame is 20ms
 ******************************************************************************
*/
#define M4AMRR_FRAME_LENGTH     20

/**
 ******************************************************************************
 * For the seek, the file is splitted in 40 segments for faster search
 ******************************************************************************
*/
#define    M4AMRR_NUM_SEEK_ENTRIES 40

#define M4AMRR_NB_SAMPLE_FREQUENCY 8000        /**< Narrow band sampling rate */
#define M4AMRR_WB_SAMPLE_FREQUENCY 16000    /**< Wide band sampling rate */

/**
 ******************************************************************************
 * AMR reader version numbers
 ******************************************************************************
*/
/* CHANGE_VERSION_HERE */
#define M4AMRR_VERSION_MAJOR 1
#define M4AMRR_VERSION_MINOR 11
#define M4AMRR_VERSION_REVISION 3

/**
 ******************************************************************************
 * structure    M4_AMRR_Context
 * @brief        Internal AMR reader context structure
 ******************************************************************************
*/
typedef struct
{
    M4OSA_UInt32             m_contextId ;      /* Fixed Id. to check for valid Context*/
    M4OSA_FileReadPointer*   m_pOsaFilePtrFct;  /* File function pointer */
    M4SYS_StreamDescription* m_pStreamHandler;  /* Stream Description */
    M4OSA_UInt32*            m_pSeekIndex;      /* Seek Index Table */
    M4OSA_UInt32             m_seekInterval;    /* Stores the seek Interval stored in the Index */
    M4OSA_UInt32             m_maxAuSize;       /* Stores the max Au Size */
    M4OSA_MemAddr32          m_pdataAddress;    /* Pointer to store AU data */
    M4SYS_StreamType         m_streamType;      /* Stores the stream type AMR NB or WB */
    M4OSA_Context            m_pAMRFile;        /* Data storage */
    M4AMRR_State             m_status;          /* AMR Reader Status */
    M4OSA_Int32              m_structSize;      /* size of structure*/
} M4_AMRR_Context;

/**
 ******************************************************************************
 * Parser internal functions, not usable from outside the reader context
 ******************************************************************************
*/
M4OSA_UInt32    M4AMRR_getAuSize(M4OSA_UInt32 frameType,  M4SYS_StreamType streamType);
M4OSA_UInt32    M4AMRR_getBitrate(M4OSA_UInt32 frameType,  M4SYS_StreamType streamType);

/**
 ******************************************************************************
 * M4OSA_UInt32    M4AMRR_getAuSize(M4OSA_UInt32 frameType,  M4SYS_StreamType streamType)
 * @brief    Internal function to the AMR Parser, returns the AU size of the Frame
 * @note     This function takes the stream type and the frametype and returns the
 *           frame lenght
 * @param    frameType(IN)    : AMR frame type
 * @param    streamType(IN)    : AMR stream type NB or WB
 * @returns  The frame size based on the frame type.
 ******************************************************************************
 */
M4OSA_UInt32    M4AMRR_getAuSize(M4OSA_UInt32 frameType,  M4SYS_StreamType streamType)
{
    const M4OSA_UInt32    M4AMRR_NB_AUSIZE[]={13,14,16,18,20,21,27,32,6,6,6};
    const M4OSA_UInt32    M4AMRR_WB_AUSIZE[]={18,24,33,37,41,47,51,59,61,6};

    if ( streamType == M4SYS_kAMR )
    {
            return M4AMRR_NB_AUSIZE[frameType];
    }
    else /* M4SYS_kAMR_WB */
    {
            return M4AMRR_WB_AUSIZE[frameType];
    }
}

/**
 ******************************************************************************
 * M4OSA_UInt32    M4AMRR_getBitrate(M4OSA_UInt32 frameType,  M4SYS_StreamType streamType)
 * @brief    Internal function to the AMR Parser, returns the Bit rate of the Frame
 * @note     This function takes the stream type and the frametype and returns the
 *           bit rate for the given frame.
 * @param    frameType(IN)    : AMR frame type
 * @param    streamType(IN)    : AMR stream type NB or WB
 * @returns  The frame's bit rate based on the frame type.
 ******************************************************************************
 */
M4OSA_UInt32    M4AMRR_getBitrate(M4OSA_UInt32 frameType,  M4SYS_StreamType streamType)
{
    const M4OSA_UInt32    M4AMRR_NB_BITRATE[]=
        {4750,5150,5900,6700,7400,7950,10200,12200,12200,12200,12200};
    const M4OSA_UInt32    M4AMRR_WB_BITRATE[]=
        {6600,8850,12650,14250,15850,18250,19850,23050,23850,12200};

    if ( streamType == M4SYS_kAMR )
    {
            return M4AMRR_NB_BITRATE[frameType];
    }
    else /* M4SYS_kAMR_WB */
    {
            return M4AMRR_WB_BITRATE[frameType];
    }
}

/*********************************************************/
M4OSA_ERR M4AMRR_openRead(M4OSA_Context* pContext, M4OSA_Void* pFileDescriptor,
                        M4OSA_FileReadPointer* pFileFunction)
/*********************************************************/
{
    M4_AMRR_Context*    pStreamContext;
    M4OSA_FilePosition  filePos;

    M4OSA_ERR err = M4ERR_FILE_NOT_FOUND ;
    M4OSA_UInt32 size ;
    M4OSA_UInt32 data ;
    M4OSA_Char *M4_Token;
    M4OSA_UInt32 *tokenPtr;

    /* Header for AMR NB */
    M4OSA_UInt32 M4_AMR_1       = 0x4d412123;
    M4OSA_UInt32 M4_AMR_NB_2    = 0x00000a52;

    /* Header for AMR WB */
    M4OSA_UInt32 M4_AMR_WB_2    = 0x42572d52;
    M4OSA_UInt32 M4_AMR_WB_3    = 0x0000000a;
    *pContext = M4OSA_NULL ;

    M4OSA_DEBUG_IF2((M4OSA_NULL == pContext),M4ERR_PARAMETER,"Context M4OSA_NULL");
    M4OSA_DEBUG_IF2((M4OSA_NULL == pFileDescriptor),M4ERR_PARAMETER,"File Desc. M4OSA_NULL");

    M4_Token = (M4OSA_Char*)M4OSA_32bitAlignedMalloc(sizeof(M4OSA_MemAddr32)*3, M4AMR_READER,
                 (M4OSA_Char *)("M4_Token"));
    if(M4OSA_NULL == M4_Token)
    {
        M4OSA_DEBUG_IF3((M4OSA_NULL == M4_Token),M4ERR_ALLOC,"Mem Alloc failed - M4_Token");
        return M4ERR_ALLOC ;
    }

    pStreamContext= (M4_AMRR_Context*)M4OSA_32bitAlignedMalloc(sizeof(M4_AMRR_Context), M4AMR_READER,
                     (M4OSA_Char *)("pStreamContext"));
    if(M4OSA_NULL == pStreamContext)
    {
        free(M4_Token);
        *pContext = M4OSA_NULL ;
        return M4ERR_ALLOC ;
    }

    /* Initialize the context */
    pStreamContext->m_contextId = M4AMRR_CONTEXTID;
    pStreamContext->m_structSize=sizeof(M4_AMRR_Context);
    pStreamContext->m_pOsaFilePtrFct=pFileFunction ;
    pStreamContext->m_pStreamHandler = M4OSA_NULL ;
    pStreamContext->m_pAMRFile = M4OSA_NULL ;
    pStreamContext->m_status = M4AMRR_kOpening ;
    pStreamContext->m_pSeekIndex = M4OSA_NULL ;
    pStreamContext->m_seekInterval = 0;
    pStreamContext->m_maxAuSize = 0 ;
    pStreamContext->m_pdataAddress = M4OSA_NULL;
    err=pStreamContext->m_pOsaFilePtrFct->openRead(&pStreamContext->m_pAMRFile,
        (M4OSA_Char*)pFileDescriptor,M4OSA_kFileRead );
    if ( err != M4NO_ERROR )
    {
        /* M4OSA_DEBUG_IF3((err != M4NO_ERROR),err,"File open failed"); */
        free(pStreamContext);
        free(M4_Token);
        *pContext = M4OSA_NULL ;
        return err ;
    }

    pStreamContext->m_status = M4AMRR_kOpening ;

    size = 6;
    pStreamContext->m_pOsaFilePtrFct->readData(pStreamContext->m_pAMRFile,
                (M4OSA_MemAddr8)M4_Token, &size);
    if(size != 6)
    {
        goto cleanup;
    }

    tokenPtr = (M4OSA_UInt32*)M4_Token ;
    /* Check for the first 4 bytes of the header common to WB and NB*/
    if (*tokenPtr != M4_AMR_1)
    {
        goto cleanup;
    }

    tokenPtr++;
    data = *tokenPtr & 0x0000FFFF ;
    /* Check if the next part is Narrow band header */
    if (data!= M4_AMR_NB_2)
    {
        /* Stream is AMR Wide Band */
        filePos = 4;
        pStreamContext->m_pOsaFilePtrFct->seek(pStreamContext->m_pAMRFile,
             M4OSA_kFileSeekBeginning, &filePos);
        size = 5;
        pStreamContext->m_pOsaFilePtrFct->readData(pStreamContext->m_pAMRFile,
             (M4OSA_MemAddr8)M4_Token, &size);
        if(size != 5)
            goto cleanup;
        tokenPtr=(M4OSA_UInt32*)M4_Token;
        /* Check for the Wide band hader */
        if(*tokenPtr!= M4_AMR_WB_2)
            goto cleanup;
        tokenPtr++;
        data = *tokenPtr & 0x000000FF ;
        if(data!= M4_AMR_WB_3)
            goto cleanup;
        pStreamContext->m_streamType = M4SYS_kAMR_WB ;
    }
    else
    {
        /* Stream is a Narrow band stream */
        pStreamContext->m_streamType = M4SYS_kAMR ;
    }
    /*  No Profile level defined */
    pStreamContext->m_status = M4AMRR_kOpened;

    free(M4_Token);
    *pContext = pStreamContext ;
    return M4NO_ERROR;

cleanup:

    if(M4OSA_NULL != pStreamContext->m_pAMRFile)
    {
        pStreamContext->m_pOsaFilePtrFct->closeRead(pStreamContext->m_pAMRFile);
    }

    free(M4_Token);
    free(pStreamContext);

    *pContext = M4OSA_NULL ;

    return (M4OSA_ERR)M4ERR_AMR_NOT_COMPLIANT;
}


/*********************************************************/
M4OSA_ERR M4AMRR_getNextStream(M4OSA_Context Context, M4SYS_StreamDescription* pStreamDesc )
/*********************************************************/
{
    M4_AMRR_Context*    pStreamContext=(M4_AMRR_Context*)Context;
    M4OSA_Char            frameHeader, frameType ;
    M4OSA_UInt32        size, auCount=0;
    M4OSA_FilePosition  filePos;

    M4OSA_DEBUG_IF2((M4OSA_NULL == Context),M4ERR_PARAMETER,"Context M4OSA_NULL");
    M4OSA_DEBUG_IF2((M4OSA_NULL == pStreamDesc),M4ERR_PARAMETER,"Stream Desc. M4OSA_NULL");
    M4OSA_DEBUG_IF2((pStreamContext->m_contextId != M4AMRR_CONTEXTID),M4ERR_BAD_CONTEXT,
         "Bad Context");
    M4OSA_DEBUG_IF1(( pStreamContext->m_status != M4AMRR_kOpened), M4ERR_STATE, "Invalid State");

    if (M4OSA_NULL != pStreamContext->m_pStreamHandler)
    {
        return M4WAR_NO_MORE_STREAM ;
    }

    size = 1;
    pStreamContext->m_pOsaFilePtrFct->readData(pStreamContext->m_pAMRFile,
         (M4OSA_MemAddr8)&frameHeader, &size);

    /* XFFF FXXX -> F is the Frame type */
    frameType = ( frameHeader & 0x78 ) >> 3 ;

    if ( frameType == 15 )
    {
        return M4WAR_NO_DATA_YET ;
    }

    if (( pStreamContext->m_streamType == M4SYS_kAMR ) && ( frameType > 11 ))
    {
        return (M4OSA_ERR)M4ERR_AMR_INVALID_FRAME_TYPE;
    }

    if (( pStreamContext->m_streamType == M4SYS_kAMR_WB ) && ( frameType > 9 ))
    {
        return (M4OSA_ERR)M4ERR_AMR_INVALID_FRAME_TYPE;
    }

    /* Average bit rate is assigned the bitrate of the first frame */
    pStreamDesc->averageBitrate = M4AMRR_getBitrate(frameType,pStreamContext->m_streamType);

    filePos = -1;
    pStreamContext->m_pOsaFilePtrFct->seek(pStreamContext->m_pAMRFile, M4OSA_kFileSeekCurrent,
         &filePos);

    /* Initialize pStreamDesc */
    pStreamDesc->profileLevel = 0xFF ;
    pStreamDesc->decoderSpecificInfoSize = 0 ;
    pStreamDesc->decoderSpecificInfo = M4OSA_NULL ;
    pStreamDesc->maxBitrate = (pStreamContext->m_streamType ==
        M4SYS_kAMR )?M4AMRR_NB_MAX_BIT_RATE:M4AMRR_WB_MAX_BIT_RATE;
    pStreamDesc->profileLevel = 0xFF ;
    pStreamDesc->streamID = 1;
    pStreamDesc->streamType = pStreamContext->m_streamType;

    /* Timescale equals Sampling Frequency: NB-8000 Hz, WB-16000 Hz */
    pStreamDesc->timeScale = (pStreamContext->m_streamType == M4SYS_kAMR )?8000:16000;
    pStreamDesc->duration = M4OSA_TIME_UNKNOWN;

    pStreamContext->m_pStreamHandler =
         (M4SYS_StreamDescription*)M4OSA_32bitAlignedMalloc(sizeof(M4SYS_StreamDescription),
             M4AMR_READER, (M4OSA_Char *)("pStreamContext->m_pStreamHandler"));
    if(M4OSA_NULL == pStreamContext->m_pStreamHandler)
    {
        return M4ERR_ALLOC;
    }

    /* Copy the Stream Desc. into the Context */
    pStreamContext->m_pStreamHandler->averageBitrate = pStreamDesc->averageBitrate;
    pStreamContext->m_pStreamHandler->decoderSpecificInfo = M4OSA_NULL ;
    pStreamContext->m_pStreamHandler->decoderSpecificInfoSize = 0 ;
    pStreamContext->m_pStreamHandler->duration = M4OSA_TIME_UNKNOWN;
    pStreamContext->m_pStreamHandler->profileLevel = 0xFF ;
    pStreamContext->m_pStreamHandler->streamID = 1;
    pStreamContext->m_pStreamHandler->streamType = pStreamDesc->streamType ;
    pStreamContext->m_pStreamHandler->timeScale = pStreamDesc->timeScale ;

    /* Count the number of Access Unit in the File to get the */
    /* duration of the stream = 20 ms * number of access unit */
    while(1)
    {
        size = 1;
        pStreamContext->m_pOsaFilePtrFct->readData(pStreamContext->m_pAMRFile,
             (M4OSA_MemAddr8)&frameHeader, &size);
        if ( size == 0)
            break ;
        frameType = (frameHeader & 0x78) >> 3 ;
        /* Get the frame size and skip so many bytes */
        if(frameType != 15){
            /* GLA 20050628 when frametype is >10 we read over a table */
            if(frameType > 10)
                continue ;

            size = M4AMRR_getAuSize(frameType, pStreamContext->m_streamType);
            if(size > pStreamContext->m_maxAuSize )
            {
                pStreamContext->m_maxAuSize = size ;
            }
            filePos = size-1;
            pStreamContext->m_pOsaFilePtrFct->seek(pStreamContext->m_pAMRFile,
                 M4OSA_kFileSeekCurrent, &filePos);
            auCount++;
        }
    }

    /* Each Frame is 20 m Sec. */
    pStreamContext->m_pStreamHandler->duration = auCount * M4AMRR_FRAME_LENGTH ;
    pStreamDesc->duration = pStreamContext->m_pStreamHandler->duration ;

    /* Put the file pointer back at the first Access unit */
    if( pStreamContext->m_streamType == M4SYS_kAMR )
    {
        filePos = 6;
        pStreamContext->m_pOsaFilePtrFct->seek(pStreamContext->m_pAMRFile,
             M4OSA_kFileSeekBeginning, &filePos);
    }
    if ( pStreamContext->m_streamType == M4SYS_kAMR_WB )
    {
        filePos = 9;
        pStreamContext->m_pOsaFilePtrFct->seek(pStreamContext->m_pAMRFile,
             M4OSA_kFileSeekBeginning, &filePos);
    }
    return M4NO_ERROR ;
}

/*********************************************************/
M4OSA_ERR M4AMRR_startReading(M4OSA_Context Context, M4SYS_StreamID* pStreamIDs )
/*********************************************************/
{
    M4_AMRR_Context* pStreamContext=(M4_AMRR_Context*)Context;
    M4OSA_Int32 size = 0 ;

    M4OSA_DEBUG_IF2((M4OSA_NULL == Context),M4ERR_PARAMETER,"Context M4OSA_NULL");
    M4OSA_DEBUG_IF2((M4OSA_NULL == pStreamIDs),M4ERR_PARAMETER,"Stream Ids. M4OSA_NULL");
    M4OSA_DEBUG_IF2((pStreamContext->m_contextId != M4AMRR_CONTEXTID),M4ERR_BAD_CONTEXT,
         "Bad Context");
    M4OSA_DEBUG_IF1(( pStreamContext->m_status != M4AMRR_kOpened), M4ERR_STATE, "Invalid State");

    while( pStreamIDs[size] != 0 )
    {
        if( pStreamIDs[size++] != 1 )
        {
            return M4ERR_BAD_STREAM_ID ;
        }
    }

    /* Allocate memory for data Address for use in NextAU() */
    if(M4OSA_NULL == pStreamContext->m_pdataAddress)
    {
        size = pStreamContext->m_maxAuSize ;
        /* dataAddress is owned by Parser, application should not delete or free it */
        pStreamContext->m_pdataAddress =(M4OSA_MemAddr32)M4OSA_32bitAlignedMalloc(size + (4 - size % 4),
            M4AMR_READER, (M4OSA_Char *)("pStreamContext->m_pdataAddress"));
        if(M4OSA_NULL == pStreamContext->m_pdataAddress)
        {
                M4OSA_DEBUG_IF3((M4OSA_NULL == pStreamContext->m_pdataAddress),M4ERR_ALLOC,
                    "Mem Alloc failed - dataAddress");
                return M4ERR_ALLOC;
        }
    }

    /* Set the state of context to Reading */
    pStreamContext->m_status = M4AMRR_kReading ;

    return M4NO_ERROR ;
}


/*********************************************************/
M4OSA_ERR M4AMRR_nextAU(M4OSA_Context Context, M4SYS_StreamID StreamID, M4SYS_AccessUnit* pAu)
/*********************************************************/
{
    M4_AMRR_Context* pStreamContext=(M4_AMRR_Context*)Context;
    M4OSA_Char        frameHeader ;
    M4OSA_Char        frameType ;
    M4OSA_Int32        auSize;
    M4OSA_UInt32    size ;
    M4OSA_FilePosition  filePos;

    M4OSA_DEBUG_IF2((M4OSA_NULL == Context),M4ERR_PARAMETER,"Context M4OSA_NULL");
    M4OSA_DEBUG_IF2((M4OSA_NULL == pAu),M4ERR_PARAMETER,"Access Unit . M4OSA_NULL");
    M4OSA_DEBUG_IF2((pStreamContext->m_contextId != M4AMRR_CONTEXTID),M4ERR_BAD_CONTEXT,
         "Bad Context");
    M4OSA_DEBUG_IF1(( pStreamContext->m_status != M4AMRR_kReading), M4ERR_STATE, "Invalid State");

    if ( StreamID != 1 )
    {
            return M4ERR_BAD_STREAM_ID;
    }

    /* Read the frame header byte */
    size = pStreamContext->m_maxAuSize;
    pStreamContext->m_pOsaFilePtrFct->readData(pStreamContext->m_pAMRFile,
         (M4OSA_MemAddr8)pStreamContext->m_pdataAddress, &size);
    if(size != pStreamContext->m_maxAuSize)
    {
        return M4WAR_NO_MORE_AU;
    }

    frameHeader = ((M4OSA_MemAddr8)pStreamContext->m_pdataAddress)[0];

    frameType = ( frameHeader & 0x78 ) >> 3 ;

    if (( pStreamContext->m_streamType == M4SYS_kAMR ) &&
        ( frameType > 11 ) && ( frameType != 15 ))
    {
        return (M4OSA_ERR)M4ERR_AMR_INVALID_FRAME_TYPE;
    }

    if (( pStreamContext->m_streamType == M4SYS_kAMR_WB ) &&
        ( frameType > 9 ) && ( frameType != 15 ))
    {
        return (M4OSA_ERR)M4ERR_AMR_INVALID_FRAME_TYPE;
    }

    /* Get the frame size */
    if(frameType == 15)
    {
        auSize = 1;
    }
    else
    {
        auSize = M4AMRR_getAuSize(frameType, pStreamContext->m_streamType);
    }

    size -= auSize ;
    if(size != 0)
    {
        filePos = -((M4OSA_FilePosition)size);
        pStreamContext->m_pOsaFilePtrFct->seek(pStreamContext->m_pAMRFile,
             M4OSA_kFileSeekCurrent, &filePos);
    }

    pAu->size = auSize ;

    /* even when frameType == 15 (no data frame), ARM core decoder outputs full PCM buffer */
    /*if(frameType == 15 )
    {
        pAu->CTS += 0;
    }*/
    /*else*/
    {
        pAu->CTS += M4AMRR_FRAME_LENGTH ;
    }


    pAu->DTS = pAu->CTS ;
    pAu->attribute = M4SYS_kFragAttrOk;

    pAu->stream = pStreamContext->m_pStreamHandler;
    pAu->dataAddress = pStreamContext->m_pdataAddress ;

    if(frameHeader & 0x80)
    {
        return M4WAR_NO_MORE_AU;
    }

    /* Change the state to implement NextAu->freeAu->NextAu FSM */
    pStreamContext->m_status = M4AMRR_kReading_nextAU ;

    return M4NO_ERROR ;
}

/*********************************************************/
M4OSA_ERR M4AMRR_freeAU(M4OSA_Context Context, M4SYS_StreamID StreamID, M4SYS_AccessUnit* pAu)
/*********************************************************/
{
    M4_AMRR_Context* pStreamContext=(M4_AMRR_Context*)Context;
    M4OSA_DEBUG_IF2((M4OSA_NULL == Context),M4ERR_PARAMETER,"Context M4OSA_NULL");
    M4OSA_DEBUG_IF2((M4OSA_NULL == pAu),M4ERR_PARAMETER,"Access Unit . M4OSA_NULL");
    M4OSA_DEBUG_IF2((pStreamContext->m_contextId != M4AMRR_CONTEXTID),M4ERR_BAD_CONTEXT,
         "Bad Context");
    M4OSA_DEBUG_IF1(( pStreamContext->m_status != M4AMRR_kReading_nextAU), M4ERR_STATE,
         "Invalid State");

    if (( StreamID != 1 ) && ( StreamID != 0))
    {
            return M4ERR_BAD_STREAM_ID;
    }

    /* Change the state to Reading so as to allow access to next AU */
    pStreamContext->m_status = M4AMRR_kReading ;

    return M4NO_ERROR ;
}

/*********************************************************/
M4OSA_ERR M4AMRR_seek(M4OSA_Context Context, M4SYS_StreamID* pStreamID, M4OSA_Time time,
                         M4SYS_SeekAccessMode seekMode, M4OSA_Time* pObtainCTS)
/*********************************************************/
{
    M4_AMRR_Context* pStreamContext=(M4_AMRR_Context*)Context;
    M4OSA_UInt32 count, prevAU, nextAU ;
    M4OSA_UInt32 size ;
    M4OSA_UInt32 auSize ;
    M4OSA_UInt32 position, partSeekTime;
    M4OSA_UInt32 auCount = 0, skipAuCount = 0 ;
    M4OSA_Char    frameHeader ;
    M4OSA_Char    frameType ;
    M4OSA_FilePosition  filePos;
    M4OSA_Double time_double;

    /*Make explicit time cast, but take care that timescale is not used !!!*/
    M4OSA_TIME_TO_MS(time_double, time, 1000);

    *pObtainCTS = 0;

    M4OSA_DEBUG_IF2((M4OSA_NULL == Context),M4ERR_PARAMETER,"Context M4OSA_NULL");
    M4OSA_DEBUG_IF2((pStreamContext->m_contextId != M4AMRR_CONTEXTID),M4ERR_BAD_CONTEXT,
         "Bad Context");
    M4OSA_DEBUG_IF1(( pStreamContext->m_status != M4AMRR_kReading) && \
        ( pStreamContext->m_status != M4AMRR_kOpened), M4ERR_STATE, "Invalid State");
    M4OSA_DEBUG_IF1((time_double < 0),M4ERR_PARAMETER,"negative time");

    /* Coming to seek for the first time, need to build the seekIndex Table */
    if(M4OSA_NULL == pStreamContext->m_pSeekIndex)
    {
        M4OSA_Double duration_double;

        count = 0 ;
        pStreamContext->m_pSeekIndex =
             (M4OSA_UInt32*)M4OSA_32bitAlignedMalloc(M4AMRR_NUM_SEEK_ENTRIES * sizeof(M4OSA_UInt32),
                 M4AMR_READER, (M4OSA_Char *)("pStreamContext->m_pSeekIndex"));

        if(M4OSA_NULL == pStreamContext->m_pSeekIndex)
        {
            M4OSA_DEBUG_IF3((M4OSA_NULL == pStreamContext->m_pSeekIndex),M4ERR_ALLOC,
                "Mem Alloc Failed - SeekIndex");
            return M4ERR_ALLOC ;
        }

        /* point to the first AU */
        if( pStreamContext->m_streamType == M4SYS_kAMR )
        {
            filePos = 6;
        }
        else /*if ( pStreamContext->m_streamType == M4SYS_kAMR_WB )*/
        {
            filePos = 9;
        }

        pStreamContext->m_pOsaFilePtrFct->seek(pStreamContext->m_pAMRFile,
             M4OSA_kFileSeekBeginning, &filePos);

        /* Set the postion to begining of first AU */
        position = (pStreamContext->m_streamType != M4SYS_kAMR)?9:6;

        /*Make explicit time cast, but take care that timescale is not used !!!*/
        M4OSA_TIME_TO_MS(duration_double, pStreamContext->m_pStreamHandler->duration, 1000);

        /* Calculate the seek Interval duration based on total dutation */
        /* Interval = (duration / ENTRIES) in multiples of AU frame length */
        pStreamContext->m_seekInterval =
             (M4OSA_UInt32)(duration_double / M4AMRR_NUM_SEEK_ENTRIES) ;
        pStreamContext->m_seekInterval /= M4AMRR_FRAME_LENGTH ;
        pStreamContext->m_seekInterval *= M4AMRR_FRAME_LENGTH ;
        skipAuCount = pStreamContext->m_seekInterval / M4AMRR_FRAME_LENGTH ;

        pStreamContext->m_pSeekIndex[count++]=position;
        while(count < M4AMRR_NUM_SEEK_ENTRIES )
        {
            size = 1;
            pStreamContext->m_pOsaFilePtrFct->readData(pStreamContext->m_pAMRFile,
                 (M4OSA_MemAddr8)&frameHeader, &size);
            if ( size == 0)
            {
                break ;
            }
            frameType = (frameHeader & 0x78) >> 3 ;
            if(frameType != 15)
            {
                /**< bugfix Ronan Cousyn 05/04/2006: In the core reader AMR, the
                 * function M4AMRR_seek doesn't check the frameType */
                if (( pStreamContext->m_streamType == M4SYS_kAMR ) && ( frameType > 10 ))
                {
                    return M4ERR_AMR_INVALID_FRAME_TYPE;
                }
                if (( pStreamContext->m_streamType == M4SYS_kAMR_WB ) && ( frameType > 9 ))
                {
                    return M4ERR_AMR_INVALID_FRAME_TYPE;
                }
                auSize = M4AMRR_getAuSize(frameType, pStreamContext->m_streamType);
                position += auSize ;
                filePos = auSize-1;
                pStreamContext->m_pOsaFilePtrFct->seek(pStreamContext->m_pAMRFile,
                     M4OSA_kFileSeekCurrent, &filePos);
                auCount++;
            }
            else
            {
                position ++;
            }
            /* Skip the number of AU's as per interval and store in the Index table */
            if ( (skipAuCount != 0) && !(auCount % skipAuCount))
            {
                pStreamContext->m_pSeekIndex[count++] = position;
            }
        }
    }/* End of Building the seek table */

    /* Use the seek table to seek the required time in the stream */

    /* If we are seeking the begining of the file point to first AU */
    if ( seekMode == M4SYS_kBeginning )
    {
        if( pStreamContext->m_streamType == M4SYS_kAMR )
        {
            filePos = 6;
        }
        else /*if ( pStreamContext->m_streamType == M4SYS_kAMR_WB )*/
        {
            filePos = 9;
        }
        pStreamContext->m_pOsaFilePtrFct->seek(pStreamContext->m_pAMRFile,
             M4OSA_kFileSeekBeginning, &filePos );
        return M4NO_ERROR ;
    }

    /* Get the Nearest Second */
    if (0 != pStreamContext->m_seekInterval)
    {
        position = (M4OSA_UInt32)(time_double / pStreamContext->m_seekInterval);
    }
    else
    {
        /*avoid division by 0*/
        position = 0;
    }

    /* We have only 40 seek Index. */
    position=(position >= M4AMRR_NUM_SEEK_ENTRIES)?M4AMRR_NUM_SEEK_ENTRIES-1:position;

    /* SeekIndex will point to nearest Au, we need to search for the
    required time form that position */
    partSeekTime = (M4OSA_UInt32)time_double - position * pStreamContext->m_seekInterval;

    position = pStreamContext->m_pSeekIndex[position];

    if(!position)
    {
        return M4WAR_INVALID_TIME ;
    }

    /* point the file pointer to nearest AU */
    filePos = position;
    pStreamContext->m_pOsaFilePtrFct->seek(pStreamContext->m_pAMRFile, M4OSA_kFileSeekBeginning,
         &filePos );

    if ( partSeekTime == 0)
    {
        *pObtainCTS = time;
        return M4NO_ERROR;
    }

    *pObtainCTS = (M4OSA_Time)(time_double - (M4OSA_Double)partSeekTime);

    switch(seekMode)
    {
        /* Get the AU before the target time */
        case M4SYS_kPreviousRAP:
        case M4SYS_kNoRAPprevious:
            position = partSeekTime / M4AMRR_FRAME_LENGTH ;
            if ( !(partSeekTime % M4AMRR_FRAME_LENGTH) )
            {
                position -- ;
            }
        break;
        /* Get the Closest AU following the target time */
        case M4SYS_kNextRAP:
        case M4SYS_kNoRAPnext:
            position = (partSeekTime + M4AMRR_FRAME_LENGTH )/ M4AMRR_FRAME_LENGTH ;
        break;
        /*  Get the closest AU to target time */
        case M4SYS_kClosestRAP:
        case M4SYS_kNoRAPclosest:
            prevAU = partSeekTime-(partSeekTime/M4AMRR_FRAME_LENGTH)*M4AMRR_FRAME_LENGTH;
            nextAU =
                 ((partSeekTime+M4AMRR_FRAME_LENGTH)/M4AMRR_FRAME_LENGTH)*M4AMRR_FRAME_LENGTH -\
                     partSeekTime ;
            if(prevAU < nextAU)
            {
                position = partSeekTime / M4AMRR_FRAME_LENGTH ;
            }
            else
            {
                position = (partSeekTime + M4AMRR_FRAME_LENGTH )/ M4AMRR_FRAME_LENGTH ;
            }
        break;
        case M4SYS_kBeginning:
        break;
    }

    count = 0 ;
    /* Skip the Access unit in the stream to skip the part seek time,
       to reach the required target time */
    while(count < position )
    {
        size = 1;
        pStreamContext->m_pOsaFilePtrFct->readData(pStreamContext->m_pAMRFile,
             (M4OSA_MemAddr8)&frameHeader, &size);
        if ( size == 0)
        {
            /* If the target time is invalid, point to begining and return */
            *pObtainCTS = 0;
            filePos = pStreamContext->m_pSeekIndex[0];
            pStreamContext->m_pOsaFilePtrFct->seek(pStreamContext->m_pAMRFile,
                 M4OSA_kFileSeekBeginning, &filePos);
            return M4WAR_INVALID_TIME ;
        }
        *pObtainCTS += M4AMRR_FRAME_LENGTH; /*Should use M4OSA_INT64_ADD !!*/
        count++;
        frameType = (frameHeader & 0x78) >> 3 ;
        if(frameType == 15)
        {
            auSize = 1 ;
        }
        else
        {
            auSize = M4AMRR_getAuSize(frameType, pStreamContext->m_streamType);
        }

        filePos = auSize-1;
        pStreamContext->m_pOsaFilePtrFct->seek(pStreamContext->m_pAMRFile,
             M4OSA_kFileSeekCurrent, &filePos);
    }

    return M4NO_ERROR;
}

/*********************************************************/
M4OSA_ERR M4AMRR_closeRead(M4OSA_Context Context)
/*********************************************************/
{
    M4_AMRR_Context* pStreamContext=(M4_AMRR_Context*)Context;
    M4OSA_DEBUG_IF2((M4OSA_NULL == Context),M4ERR_PARAMETER,"Context M4OSA_NULL");

    /* Close the AMR stream */
    pStreamContext->m_pOsaFilePtrFct->closeRead(pStreamContext->m_pAMRFile);

    pStreamContext->m_status=M4AMRR_kClosed ;

    /* Check if AU data Address is allocated memory and free it */
    if(M4OSA_NULL != pStreamContext->m_pdataAddress)
    {
        free(pStreamContext->m_pdataAddress);
    }

    /* Check if the stream handler is allocated memory */
    if(M4OSA_NULL != pStreamContext->m_pStreamHandler)
    {
        free(pStreamContext->m_pStreamHandler);
    }

    /* Seek table is created only when seek is used, so check if memory is allocated */
    if(M4OSA_NULL != pStreamContext->m_pSeekIndex)
    {
        free(pStreamContext->m_pSeekIndex);
    }

    /* Free the context */
    free(pStreamContext);

    return M4NO_ERROR ;
}

/*********************************************************/
M4OSA_ERR M4AMRR_getState(M4OSA_Context Context, M4AMRR_State* pState, M4SYS_StreamID streamId)
/*********************************************************/
{
    M4_AMRR_Context* pStreamContext=(M4_AMRR_Context*)Context;
    M4OSA_DEBUG_IF2((M4OSA_NULL == Context),M4ERR_PARAMETER,"Context M4OSA_NULL");
    M4OSA_DEBUG_IF2((pStreamContext->m_contextId != M4AMRR_CONTEXTID),M4ERR_BAD_CONTEXT,
         "Bad Context");

    if (( streamId != 1 ) && ( streamId != 0))
    {
            return M4ERR_BAD_STREAM_ID;
    }

    *pState = pStreamContext->m_status ;

    return M4NO_ERROR ;
}


/*********************************************************/
M4OSA_ERR M4AMRR_getVersion    (M4_VersionInfo *pVersion)
/*********************************************************/
{
    M4OSA_TRACE1_1("M4AMRR_getVersion called with pVersion: 0x%x\n", pVersion);
    M4OSA_DEBUG_IF1(((M4OSA_UInt32) pVersion == 0),M4ERR_PARAMETER,
         "pVersion is NULL in M4AMRR_getVersion");

    pVersion->m_major = M4AMRR_VERSION_MAJOR;
    pVersion->m_minor = M4AMRR_VERSION_MINOR;
    pVersion->m_revision = M4AMRR_VERSION_REVISION;

    return M4NO_ERROR;
}

/*********************************************************/
M4OSA_ERR M4AMRR_getmaxAUsize(M4OSA_Context Context, M4OSA_UInt32 *pMaxAuSize)
/*********************************************************/
{
    M4_AMRR_Context* pStreamContext=(M4_AMRR_Context*)Context;

    /**
     * Check input parameters */
    M4OSA_DEBUG_IF1((M4OSA_NULL == Context),  M4ERR_PARAMETER,
                "M4AMRR_getmaxAUsize: Context is M4OSA_NULL");
    M4OSA_DEBUG_IF1((M4OSA_NULL == pMaxAuSize),M4ERR_PARAMETER,
                "M4AMRR_getmaxAUsize: pMaxAuSize is M4OSA_NULL");

    *pMaxAuSize = pStreamContext->m_maxAuSize;

    return M4NO_ERROR;
}