summaryrefslogtreecommitdiffstats
path: root/libvideoeditor/vss/src/M4PCMR_CoreReader.c
blob: 3343254d851151682a75c8a38f5e3527c1189038 (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
/*
 * 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    M4PCM_PCMReader.c
 * @brief   PCM reader implementation
 * @note    This file implements functions of the PCM reader
 ************************************************************************
 */
#include "M4OSA_CharStar.h"
#include "M4PCMR_CoreReader.h"
#include "M4OSA_Debug.h"
#include "M4OSA_CharStar.h"
/**
 ******************************************************************************
 * PCM reader version numbers
 ******************************************************************************
 */
/* CHANGE_VERSION_HERE */
#define M4PCMR_VERSION_MAJOR 1
#define M4PCMR_VERSION_MINOR 0
#define M4PCMR_VERSION_REVISION 0

/**
 ************************************************************************
 * M4OSA_ERR M4PCMR_openRead(M4OSA_Context* pContext, M4OSA_Void* pUrl,
 *                             M4OSA_FileReaderPointer* pFileFunction)
 * @brief   This function opens a PCM file
 * @note    This function :
 *          - opens a PCM file
 *          - initializes PCM context,
 *          - verifies PCM file format
 *          - Fill decoder config structure
 *          - Changes state of the reader in 'Opening'
 * @param   pContext: (OUT) Pointer on the PCM Reader context
 * @param   pUrl: (IN) Name of the PCM file
 * @param   pFileFunctions: (IN) Pointer on the file access functions
 * @return  M4NO_ERROR                      there is no error during the opening
 * @return  M4ERR_PARAMETER                 pContext and/or pUrl and/or pFileFunction is NULL
 * @return  M4ERR_ALLOC                     there is no more memory available
 * @return  M4ERR_FILE_NOT_FOUND            the file cannot be found
 * @return  M4PCMC_ERR_PCM_NOT_COMPLIANT    the file does not seem to be compliant, no RIFF,
 *                                             or lack of any mandatory chunk.
 * @return  M4PCMC_ERR_PCM_NOT_SUPPORTED    the PCM format of this file is not supported by the
 *                                           reader
 * @return  Any M4OSA_FILE errors           see OSAL File specification for detailed errors
 ************************************************************************
 */
M4OSA_ERR M4PCMR_openRead(M4OSA_Context* pContext, M4OSA_Void* pUrl,
                             M4OSA_FileReadPointer* pFileFunction)
{
    M4OSA_ERR       err;
    M4PCMR_Context *context;
    M4OSA_Char*        pTempURL;
    M4OSA_Char        value[6];

    /* Check parameters */
    if((M4OSA_NULL == pContext)|| (M4OSA_NULL == pUrl) ||(M4OSA_NULL == pFileFunction))
    {
        return M4ERR_PARAMETER;
    }

    /* Allocates the context */
    context = M4OSA_NULL;
    context = (M4PCMR_Context *)M4OSA_32bitAlignedMalloc(sizeof(M4PCMR_Context), M4WAV_READER,
         (M4OSA_Char *)"M4PCMR_openRead");
    if (M4OSA_NULL == context)
    {
        return M4ERR_ALLOC;
    }
    *pContext = (M4OSA_Context)context;

    /* Initialize the context */
    context->m_offset = 0;

    context->m_state            = M4PCMR_kInit;
    context->m_microState       = M4PCMR_kInit;
    context->m_pFileReadFunc    = M4OSA_NULL;
    context->m_fileContext      = M4OSA_NULL;
    context->m_pAuBuffer        = M4OSA_NULL;
    context->m_pDecoderSpecInfo = M4OSA_NULL;

    /* Set sample frequency */
    pTempURL = (M4OSA_Char*)pUrl + (strlen((const char *)pUrl)-11);
    M4OSA_chrNCopy(value, pTempURL, 5);
    M4OSA_chrGetUInt32(pTempURL, &(context->m_decoderConfig.SampleFrequency),
         M4OSA_NULL, M4OSA_kchrDec);

    /* Set number of channels */
    pTempURL += 6;
    M4OSA_chrNCopy(value, pTempURL, 1);
    M4OSA_chrGetUInt16(pTempURL, &(context->m_decoderConfig.nbChannels),
         M4OSA_NULL, M4OSA_kchrDec);

    M4OSA_chrNCopy(pUrl,pUrl, (strlen((const char *)pUrl)-12));
    /* Open the file */
    context->m_fileContext = M4OSA_NULL;
    err = pFileFunction->openRead(&(context->m_fileContext), pUrl, M4OSA_kFileRead);
    if(M4NO_ERROR != err)
    {
        return err;
    }
    context->m_decoderConfig.BitsPerSample = 16;
    context->m_decoderConfig.AvgBytesPerSec = context->m_decoderConfig.SampleFrequency * 2 \
        * context->m_decoderConfig.nbChannels;
    err = pFileFunction->getOption(context->m_fileContext, M4OSA_kFileReadGetFileSize,
         (M4OSA_DataOption*)&(context->m_decoderConfig.DataLength));
    if(M4NO_ERROR != err)
    {
        return err;
    }
    context->m_blockSize = 2048 * context->m_decoderConfig.nbChannels;  // Raw PCM.  Hence, get a
                                                                        // chunk of data

    if(context->m_decoderConfig.SampleFrequency == 8000)
    {
        /* AMR case, no pb */
        context->m_blockSize = context->m_decoderConfig.nbChannels *\
             (context->m_decoderConfig.SampleFrequency / 50) * \
                (context->m_decoderConfig.BitsPerSample / 8);
    }
    if(context->m_decoderConfig.SampleFrequency == 16000)
    {
        /* AAC case, we can't read only 20 ms blocks */
        context->m_blockSize = 2048 * context->m_decoderConfig.nbChannels;
    }
    context->m_dataStartOffset = 0;
    context->m_pFileReadFunc = pFileFunction;

    context->m_pAuBuffer = (M4OSA_MemAddr32)M4OSA_32bitAlignedMalloc(context->m_blockSize, M4WAV_READER,
         (M4OSA_Char *)"Core PCM reader Access Unit");
    if (M4OSA_NULL == context->m_pAuBuffer)
    {
        err = M4ERR_ALLOC;
        goto cleanup;
    }

    /* Change state */
    context->m_state = M4PCMR_kOpening;

    return M4NO_ERROR;

cleanup:

    /* Close the file */
    if(context->m_pFileReadFunc != M4OSA_NULL)
        context->m_pFileReadFunc->closeRead(context->m_fileContext);

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

    return err;
}

/**
 ************************************************************************
 * M4OSA_ERR M4PCMR_getNextStream(M4OSA_Context context, M4SYS_StreamDescription* pStreamDesc)
 * @brief   This function get the (unique) stream of a PCM file
 * @note    This function :
 *          - Allocates and fills the decoder specific info structure
 *          - Fills decoder specific infos structure
 *          - Fills pStreamDesc structure allocated by the caller
 * @param   context: (IN/OUT) PCM Reader context
 * @param   pStreamDesc: (IN) Stream Description context
 * @return  M4NO_ERROR          there is no error
 * @return  M4ERR_PARAMETER     at least one parameter is NULL
 * @return  M4ERR_ALLOC         there is no more memory available
 * @return  M4ERR_STATE         this function cannot be called now
 * @return  Any M4OSA_FILE      errors see OSAL File specification for detailed errors
 ************************************************************************
 */
M4OSA_ERR M4PCMR_getNextStream(M4OSA_Context context, M4SYS_StreamDescription* pStreamDesc)
{
    M4PCMR_Context *c = (M4PCMR_Context *)context;

    /* Check parameters */
    if((M4OSA_NULL == context)|| (M4OSA_NULL == pStreamDesc))
    {
        return M4ERR_PARAMETER;
    }

    if (c->m_state == M4PCMR_kOpening_streamRetrieved)
    {
        return M4WAR_NO_MORE_STREAM;
    }
    /* Check Reader's m_state */
    if(c->m_state != M4PCMR_kOpening)
    {
        return M4ERR_STATE;
    }

    /* Only one stream is contained in PCM file */
    pStreamDesc->streamID = 1;
    /* Not used */
    pStreamDesc->profileLevel = 0;
    pStreamDesc->decoderSpecificInfoSize = sizeof(M4PCMC_DecoderSpecificInfo);

    /* Allocates decoder specific info structure */
    pStreamDesc->decoderSpecificInfo = M4OSA_NULL;
    pStreamDesc->decoderSpecificInfo =
        (M4OSA_MemAddr32)M4OSA_32bitAlignedMalloc( sizeof(M4PCMC_DecoderSpecificInfo), M4WAV_READER,
             (M4OSA_Char *)"M4PCMR_getNextStream");
    if(pStreamDesc->decoderSpecificInfo == M4OSA_NULL)
    {
        return M4ERR_ALLOC;
    }
    /* Fill decoderSpecificInfo structure, with decoder config structure filled in 'openread'
         function */
    memcpy((void *)pStreamDesc->decoderSpecificInfo,
         (void *)&c->m_decoderConfig, sizeof(M4PCMC_DecoderSpecificInfo));

    /* Fill other fields of pStreamDesc structure */
    pStreamDesc->timeScale = 1000;
    pStreamDesc->duration = (M4OSA_Time)(((M4OSA_Double)(c->m_decoderConfig.DataLength)\
         / (M4OSA_Double)(c->m_decoderConfig.AvgBytesPerSec))*pStreamDesc->timeScale);
    pStreamDesc->averageBitrate = c->m_decoderConfig.AvgBytesPerSec * 8;/* in bits, multiply by 8*/
    pStreamDesc->maxBitrate = pStreamDesc->averageBitrate; /* PCM stream has constant bitrate */

    /* Determines Stream type */
    switch(c->m_decoderConfig.BitsPerSample)
    {
        case 8:
            switch(c->m_decoderConfig.nbChannels)
            {
                case 1:
                    pStreamDesc->streamType = M4SYS_kPCM_8bitsU;
                    break;
//                case 2:
//                    pStreamDesc->streamType = M4SYS_kPCM_8bitsS; /* ??? 8bits stereo not
                                                                  //   defined ? */
//                    break;
                default:
                    pStreamDesc->streamType = M4SYS_kAudioUnknown;
            }
            break;

        case 16:
            switch(c->m_decoderConfig.nbChannels)
            {
                case 1:
                    pStreamDesc->streamType = M4SYS_kPCM_16bitsU;
                    break;
                case 2:
                    pStreamDesc->streamType = M4SYS_kPCM_16bitsS;
                    break;
                default:
                    pStreamDesc->streamType = M4SYS_kAudioUnknown;
            }
            break;

        default:
            pStreamDesc->streamType = M4SYS_kAudioUnknown;
    }

    c->m_pDecoderSpecInfo = pStreamDesc->decoderSpecificInfo;

    c->m_state = M4PCMR_kOpening_streamRetrieved;

    return M4NO_ERROR;
}

/**
 ************************************************************************
 * M4OSA_ERR M4PCMR_startReading(M4OSA_Context context, M4SYS_StreamID* pStreamIDs)
 * @brief   This function starts reading the unique stream of a PCM file
 * @note    This function :
 *          - Verifies that the current reader's state allows to start reading a stream
 *          - Check that provided StreamId is correct (always true, only one stream...)
 *            In the player application, a StreamId table is initialized as follow:
 *              M4SYS_StreamID pStreamID[2]={1,0};
 *          - Change state of the reader in 'Reading'
 * @param   context: (IN/OUT) PCM Reader context
 * @param   streamID: (IN) Stream selection
 * @return  M4NO_ERROR          there is no error
 * @return  M4ERR_PARAMETER     at least one parameter is NULL
 * @return  M4ERR_STATE         this function cannot be called now
 * @return  M4ERR_BAD_STREAM_ID at least one of the streamID does not exist
 *          (should never happen if table pStreamID is correctly initialized as above)
 ************************************************************************
 */
M4OSA_ERR M4PCMR_startReading(M4OSA_Context context, M4SYS_StreamID* pStreamIDs)
{
    M4PCMR_Context *c = (M4PCMR_Context *)context;

    /* Check parameters */
    if((M4OSA_NULL == context) || (M4OSA_NULL == pStreamIDs))
    {
        return M4ERR_PARAMETER;
    }

    /* Check Reader's state */
    if(c->m_state != M4PCMR_kOpening_streamRetrieved)
    {
        return M4ERR_STATE;
    }

    /* Check pStreamID and if they're OK, change reader's state */
    if(pStreamIDs[0] == 1 || pStreamIDs[0] == 0)
    /* First and unique stream contained in PCM file */
    {
        c->m_state = M4PCMR_kReading;
        c->m_microState = M4PCMR_kReading;
    }
    else
    {
        return M4ERR_BAD_STREAM_ID;
    }

    return M4NO_ERROR;
}

/**
 ************************************************************************
 * M4OSA_ERR M4PCMR_nextAU(M4OSA_Context context, M4SYS_StreamID streamID, M4SYS_AccessUnit* pAU)
 * @brief   This function reads the next AU contained in the PCM file
 * @note    This function :
 *          - Verifies that the current reader's state allows to read an AU
 *          - Allocates memory to store read AU
 *          - Read data from file and store them into previously allocated memory
 *          - Fill AU structure fileds (CTS...)
 *          - Change state of the reader in 'Reading' (not useful...)
 *          - Change Micro state 'Reading' in M4PCMR_kReading_nextAU
 *            (AU is read and can be deleted)
 *          - Check if the last AU has been read or if we're about to read it
 * @param   context: (IN/OUT) PCM Reader context
 * @param   streamID: (IN) Stream selection
 * @param   pAU: (IN/OUT) Acces Unit Structure
 * @return  M4NO_ERROR          there is no error
 * @return  M4ERR_PARAMETER     at least one parameter is NULL
 * @return  M4ERR_ALLOC         there is no more memory available
 * @return  M4ERR_STATE         this function cannot be called now
 * @return  M4M4WAR_NO_DATA_YET there is no enough data in the file to provide a new access unit.
 * @return  M4WAR_END_OF_STREAM There is no more access unit in the stream,
 *                              or the sample number is bigger the maximum one.
 ************************************************************************
 */
M4OSA_ERR M4PCMR_nextAU(M4OSA_Context context, M4SYS_StreamID streamID, M4SYS_AccessUnit* pAU)
{
    M4PCMR_Context *c = (M4PCMR_Context *)context;
    M4OSA_ERR err = M4NO_ERROR;
    M4OSA_UInt32 size_read;

    /* Check parameters */
    if((M4OSA_NULL == context) || (M4OSA_NULL == pAU))
    {
        return M4ERR_PARAMETER;
    }

    /* Check Reader's state */
    if(c->m_state != M4PCMR_kReading && c->m_microState != M4PCMR_kReading)
    {
        return M4ERR_STATE;
    }

    /* Allocates AU dataAdress */
    pAU->dataAddress = c->m_pAuBuffer;
    size_read        = c->m_blockSize;

    if((c->m_offset + size_read) >= c->m_decoderConfig.DataLength)
    {
        size_read = c->m_decoderConfig.DataLength - c->m_offset;
    }

    /* Read data in file, and copy it to AU Structure */
    err = c->m_pFileReadFunc->readData(c->m_fileContext, (M4OSA_MemAddr8)pAU->dataAddress,
         (M4OSA_UInt32 *)&size_read);
    if(M4NO_ERROR != err)
    {
        return err;
    }

    /* Calculates the new m_offset, used to determine whether we're at end of reading or not */
    c->m_offset = c->m_offset + size_read;

    /* Fill others parameters of AU structure */
    pAU->CTS =
         (M4OSA_Time)(((M4OSA_Double)c->m_offset/(M4OSA_Double)c->m_decoderConfig.AvgBytesPerSec)\
            *1000);
    pAU->DTS = pAU->CTS;

    pAU->attribute  = 0;
    pAU->frag       = M4OSA_NULL;
    pAU->nbFrag     = 0;
    pAU->stream     = M4OSA_NULL;
    pAU->size       = size_read;

    /* Change states */
    c->m_state = M4PCMR_kReading; /* Not changed ... */
    c->m_microState = M4PCMR_kReading_nextAU; /* AU is read and can be deleted */

    /* Check if there is another AU to read */
    /* ie: if decoded nb of bytes = nb of bytes to decode,
         it means there is no more AU to decode */
    if(c->m_offset >= c->m_decoderConfig.DataLength)
    {
        return M4WAR_NO_MORE_AU;
    }

    return M4NO_ERROR;
}

/**
 ************************************************************************
 * M4OSA_ERR M4PCMR_freeAU(M4OSA_Context context, M4SYS_StreamID streamID, M4SYS_AccessUnit* pAU)
 * @brief   This function frees the AU provided in parameter
 * @note    This function :
 *          - Verifies that the current reader's state allows to free an AU
 *          - Free dataAddress field of AU structure
 *          - Change state of the reader in 'Reading' (not useful...)
 *          - Change Micro state 'Reading' in M4PCMR_kReading (another AU can be read)
 * @param   context: (IN/OUT) PCM Reader context
 * @param   streamID: (IN) Stream selection
 * @param   pAU: (IN) Acces Unit Structure
 * @return  M4NO_ERROR  there is no error
 * @return  M4ERR_PARAMETER at least one parameter is NULL
 * @return  M4ERR_STATE this function cannot be called now
 ************************************************************************
 */
M4OSA_ERR M4PCMR_freeAU(M4OSA_Context context, M4SYS_StreamID streamID, M4SYS_AccessUnit* pAU)
{
    M4PCMR_Context *c = (M4PCMR_Context *)context;

    /* Check parameters */
    if((M4OSA_NULL == context ) || (M4OSA_NULL == pAU))
    {
        return M4ERR_PARAMETER;
    }

    /* Check Reader's state */
    if(c->m_state != M4PCMR_kReading && c->m_microState != M4PCMR_kReading_nextAU)
    {
        return M4ERR_STATE;
    }

    pAU->dataAddress = M4OSA_NULL;

    /* Change states */
    c->m_state = M4PCMR_kReading; /* Not changed ... */
    c->m_microState = M4PCMR_kReading; /* AU is deleted, another AU can be read */

    return M4NO_ERROR;
}

/**
 ************************************************************************
 * M4OSA_ERR M4PCMR_seek(M4OSA_Context context, M4SYS_StreamID* pStreamID,
                         M4OSA_Time time, M4SYS_seekAccessMode seekAccessMode,
                         M4OSA_Time* pObtainCTS[])
 * @brief   This function seeks into the PCM file at the provided time
 * @note    This function :
 *          - Verifies that the current reader's state allows to seek
 *          - Determines from provided time m_offset to seek in file
 *          - If m_offset is correct, seek in file
 *          - Update new m_offset in PCM reader context
 * @param   context: (IN/OUT) PCM Reader context
 * @param   pStreamID: (IN) Stream selection (not used, only 1 stream)
 * @param   time: (IN) Targeted time
 * @param   seekMode: (IN) Selects the seek access mode
 * @param   pObtainCTS[]: (OUT) Returned Time (not used)
 * @return  M4NO_ERROR              there is no error
 * @return  M4ERR_PARAMETER         at least one parameter is NULL
 * @return  M4ERR_ALLOC             there is no more memory available
 * @return  M4ERR_STATE             this function cannot be called now
 * @return  M4WAR_INVALID_TIME      Specified time is not reachable
 * @param   M4ERR_NOT_IMPLEMENTED   This seek mode is not implemented yet
 ************************************************************************
 */
M4OSA_ERR M4PCMR_seek(M4OSA_Context context, M4SYS_StreamID* pStreamID, M4OSA_Time time,
                      M4SYS_SeekAccessMode seekAccessMode, M4OSA_Time* pObtainCTS)
{
    M4PCMR_Context *c = (M4PCMR_Context *)context;
    M4OSA_ERR err = M4NO_ERROR;
    M4OSA_UInt32 offset;
    M4OSA_UInt32 alignment;
    M4OSA_UInt32 size_read;

    /* Check parameters */
    if((M4OSA_NULL == context) || (M4OSA_NULL == pStreamID))
    {
        return M4ERR_PARAMETER;
    }

    /* Check Reader's state */
    if(c->m_state != M4PCMR_kOpening_streamRetrieved && c->m_state != M4PCMR_kReading)
    {
        return M4ERR_STATE;
    }

    switch(seekAccessMode)
    {
        case M4SYS_kBeginning:
            /* Determine m_offset from time*/
            offset =
                (M4OSA_UInt32)(time * ((M4OSA_Double)(c->m_decoderConfig.AvgBytesPerSec) / 1000));
            /** check the alignment on sample boundary */
            alignment = c->m_decoderConfig.nbChannels*c->m_decoderConfig.BitsPerSample/8;
            if (offset%alignment != 0)
            {
                offset -= offset%alignment;
            }
            /*add the header offset*/
            offset += c->m_dataStartOffset;
            /* If m_offset is over file size -> Invalid time */
            if (offset > (c->m_dataStartOffset + c->m_decoderConfig.DataLength))
            {
                return M4WAR_INVALID_TIME;
            }
            else
            {
                /* Seek file */
                size_read = offset;
                err = c->m_pFileReadFunc->seek(c->m_fileContext, M4OSA_kFileSeekBeginning,
                    (M4OSA_FilePosition *) &size_read);
                if(M4NO_ERROR != err)
                {
                    return err;
                }
                /* Update m_offset in M4PCMR_context */
                c->m_offset = offset - c->m_dataStartOffset;
            }
            break;

        default:
            return M4ERR_NOT_IMPLEMENTED;
    }

    return M4NO_ERROR;
}

/**
 ************************************************************************
 * M4OSA_ERR M4PCMR_closeRead(M4OSA_Context context)
 * @brief   This function closes PCM file, and frees context
 * @note    This function :
 *          - Verifies that the current reader's state allows close the PCM file
 *          - Closes the file
 *          - Free structures
 * @param   context: (IN/OUT) PCM Reader context
 * @return  M4NO_ERROR              there is no error
 * @return  M4ERR_PARAMETER         at least one parameter is NULL
 * @return  M4ERR_STATE             this function cannot be called now
 ************************************************************************
 */
M4OSA_ERR M4PCMR_closeRead(M4OSA_Context context)
{
    M4PCMR_Context *c = (M4PCMR_Context *)context;
    M4OSA_ERR err = M4NO_ERROR;

    /* Check parameters */
    if(M4OSA_NULL == context)
    {
        return M4ERR_PARAMETER;
    }

    if(c->m_pDecoderSpecInfo != M4OSA_NULL)
    {
        free(c->m_pDecoderSpecInfo);
    }

    /* Check Reader's state */
    if(c->m_state != M4PCMR_kReading)
    {
        return M4ERR_STATE;
    }
    else if(c->m_microState == M4PCMR_kReading_nextAU)
    {
        return M4ERR_STATE;
    }

    if (M4OSA_NULL != c->m_pAuBuffer)
    {
        free(c->m_pAuBuffer);
    }

    /* Close the file */
    if (M4OSA_NULL != c->m_pFileReadFunc)
    {
        err = c->m_pFileReadFunc->closeRead(c->m_fileContext);
    }

    /* Free internal context */
    if (M4OSA_NULL != c)
    {
        free(c);
    }

    return err;
}

/**
 ************************************************************************
 * M4OSA_ERR M4PCMR_getOption(M4OSA_Context context, M4PCMR_OptionID optionID,
 *                                M4OSA_DataOption* pValue)
 * @brief   This function get option of the PCM Reader
 * @note    This function :
 *          - Verifies that the current reader's state allows to get an option
 *          - Return corresponding option value
 * @param   context: (IN/OUT) PCM Reader context
 * @param   optionID: (IN) ID of the option to get
 * @param   pValue: (OUT) Variable where the option value is returned
 * @return  M4NO_ERROR              there is no error.
 * @return  M4ERR_PARAMETER         at least one parameter is NULL.
 * @return  M4ERR_BAD_OPTION_ID     the optionID is not a valid one.
 * @return  M4ERR_STATE             this option is not available now.
 * @return  M4ERR_NOT_IMPLEMENTED   this option is not implemented
 ************************************************************************
 */
M4OSA_ERR M4PCMR_getOption(M4OSA_Context context, M4PCMR_OptionID optionID,
                             M4OSA_DataOption* pValue)
{
    M4PCMR_Context *c =(M4PCMR_Context *)context;

    /* Check parameters */
    if(M4OSA_NULL == context)
    {
        return M4ERR_PARAMETER;
    }

    /* Check reader's state */
    if((c->m_state != M4PCMR_kOpening) && (c->m_state != M4PCMR_kOpening_streamRetrieved)\
         && (c->m_state != M4PCMR_kReading))
    {
        return M4ERR_STATE;
    }

    /* Depend of the OptionID, the value to return is different */
    switch(optionID)
    {
        case M4PCMR_kPCMblockSize:
            *pValue = &c->m_blockSize;
            break;

        default:
            return M4ERR_BAD_OPTION_ID;
    }

    return M4NO_ERROR;
}

/**
 ************************************************************************
 * M4OSA_ERR M4PCMR_setOption(M4OSA_Context context, M4PCMR_OptionID optionID,
 *                                 M4OSA_DataOption Value)
 * @brief   This function set option of the PCM Reader
 * @note    This function :
 *          - Verifies that the current reader's state allows to set an option
 *          - Set corresponding option value
 * @param   context: (IN/OUT) PCM Reader context
 * @param   optionID: (IN) ID of the option to get
 * @param   Value: (IN) Variable where the option value is stored
 * @return  M4NO_ERROR              there is no error.
 * @return  M4ERR_PARAMETER         at least one parameter is NULL.
 * @return  M4ERR_BAD_OPTION_ID     the optionID is not a valid one.
 * @return  M4ERR_STATE             this option is not available now.
 * @return  M4ERR_NOT_IMPLEMENTED   this option is not implemented
 ************************************************************************
 */
M4OSA_ERR M4PCMR_setOption(M4OSA_Context context, M4PCMR_OptionID optionID, M4OSA_DataOption Value)
{
    M4PCMR_Context *c =(M4PCMR_Context *)context;

    /* Check parameters */
    if(context == M4OSA_NULL)
    {
        return M4ERR_PARAMETER;
    }

    /* Check reader's state */
    if((c->m_state != M4PCMR_kOpening) && (c->m_state != M4PCMR_kOpening_streamRetrieved)\
         && (c->m_state != M4PCMR_kReading))
    {
        return M4ERR_STATE;
    }

    /* Depend of the OptionID, the value to set is different */
    switch(optionID)
    {
        case M4PCMR_kPCMblockSize:
            c->m_blockSize = (M4OSA_UInt32)Value;
            break;

        default:
            return M4ERR_BAD_OPTION_ID;
    }

    return M4NO_ERROR;
}

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

    pVersion->m_major = M4PCMR_VERSION_MAJOR;
    pVersion->m_minor = M4PCMR_VERSION_MINOR;
    pVersion->m_revision = M4PCMR_VERSION_REVISION;

    return M4NO_ERROR;
}