VideoEditor3gpReader.cpp revision 7c9d8018755adf1857571125ba1b3598c96ea506
1/*
2 * Copyright (C) 2011 NXP Software
3 * Copyright (C) 2011 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17/**
18*************************************************************************
19* @file   VideoEditor3gpReader.cpp
20* @brief  StageFright shell 3GP Reader
21*************************************************************************
22*/
23
24#define LOG_NDEBUG 1
25#define LOG_TAG "VIDEOEDITOR_3GPREADER"
26
27/**
28 * HEADERS
29 *
30 */
31#define VIDEOEDITOR_BITSTREAM_PARSER
32
33#include "M4OSA_Debug.h"
34#include "VideoEditor3gpReader.h"
35#include "M4SYS_AccessUnit.h"
36#include "VideoEditorUtils.h"
37#include "M4READER_3gpCom.h"
38#include "M4_Common.h"
39#include "M4OSA_FileWriter.h"
40
41#ifdef VIDEOEDITOR_BITSTREAM_PARSER
42#include "M4OSA_CoreID.h"
43#include "M4OSA_Error.h"
44#include "M4OSA_Memory.h"
45#include "M4_Utils.h"
46#endif
47
48#include "ESDS.h"
49#include "utils/Log.h"
50#include <media/stagefright/MediaBufferGroup.h>
51#include <media/stagefright/DataSource.h>
52#include <media/stagefright/FileSource.h>
53#include <media/stagefright/MediaBuffer.h>
54#include <media/stagefright/MediaDefs.h>
55#include <media/stagefright/MediaExtractor.h>
56#include <media/stagefright/MediaDebug.h>
57#include <media/stagefright/MediaSource.h>
58#include <media/stagefright/MetaData.h>
59
60/**
61 * SOURCE CLASS
62 */
63namespace android {
64/**
65 * ENGINE INTERFACE
66 */
67
68/**
69 ************************************************************************
70 * @brief   Array of AMR NB/WB bitrates
71 * @note    Array to match the mode and the bit rate
72 ************************************************************************
73*/
74const M4OSA_UInt32 VideoEditor3gpReader_AmrBitRate [2 /* 8kHz / 16kHz     */]
75                                                   [9 /* the bitrate mode */] =
76{
77    {4750, 5150, 5900,  6700,  7400,  7950,  10200, 12200, 0},
78    {6600, 8850, 12650, 14250, 15850, 18250, 19850, 23050, 23850}
79};
80
81/**
82 *******************************************************************************
83 * structure VideoEditor3gpReader_Context
84 * @brief:This structure defines the context of the StageFright 3GP shell Reader
85 *******************************************************************************
86*/
87typedef struct {
88    sp<DataSource>              mDataSource;
89    sp<MediaExtractor>          mExtractor;
90    sp<MediaSource>             mAudioSource;
91    sp<MediaSource>             mVideoSource;
92    M4_StreamHandler*           mAudioStreamHandler;
93    M4_StreamHandler*           mVideoStreamHandler;
94    M4SYS_AccessUnit            mAudioAu;
95    M4SYS_AccessUnit            mVideoAu;
96    M4OSA_Time                  mMaxDuration;
97    int32_t                     mFileSize;
98    M4_StreamType               mStreamType;
99    M4OSA_UInt32                mStreamId;
100    int32_t                     mTracks;
101    int32_t                     mCurrTrack;
102    M4OSA_Bool                  mAudioSeeking;
103    M4OSA_Time                  mAudioSeekTime;
104    M4OSA_Bool                  mVideoSeeking;
105    M4OSA_Time                  mVideoSeekTime;
106
107} VideoEditor3gpReader_Context;
108
109#ifdef VIDEOEDITOR_BITSTREAM_PARSER
110/**
111 ************************************************************************
112 * structure    VideoEditor3gpReader_BitStreamParserContext
113 * @brief       Internal BitStreamParser context
114 ************************************************************************
115*/
116typedef struct {
117    M4OSA_UInt32*   mPbitStream;   /**< bitstream pointer (32bits aligned) */
118    M4OSA_Int32     mSize;         /**< bitstream size in bytes */
119    M4OSA_Int32     mIndex;        /**< byte index */
120    M4OSA_Int32     mBitIndex;     /**< bit index */
121    M4OSA_Int32     mStructSize;   /**< size of structure */
122} VideoEditor3gpReader_BitStreamParserContext;
123
124/**
125 *******************************************************************************
126 * @brief   Allocates the context and initializes internal data.
127 * @param   pContext    (OUT)  Pointer to the BitStreamParser context to create.
128 * @param   bitStream   A pointer to the bitstream
129 * @param   size        The size of the bitstream in bytes
130 *******************************************************************************
131*/
132static void VideoEditor3gpReader_BitStreamParserInit(void** pContext,
133        void* pBitStream, M4OSA_Int32 size) {
134    VideoEditor3gpReader_BitStreamParserContext* pStreamContext;
135
136    *pContext=M4OSA_NULL;
137    pStreamContext = (VideoEditor3gpReader_BitStreamParserContext*)M4OSA_malloc(
138        sizeof(VideoEditor3gpReader_BitStreamParserContext), M4READER_3GP,
139            (M4OSA_Char*)"3GP BitStreamParser Context");
140    if (M4OSA_NULL == pStreamContext) {
141        return;
142    }
143    pStreamContext->mPbitStream=(M4OSA_UInt32*)pBitStream;
144    pStreamContext->mSize=size;
145    pStreamContext->mIndex=0;
146    pStreamContext->mBitIndex=0;
147    pStreamContext->mStructSize =
148        sizeof(VideoEditor3gpReader_BitStreamParserContext);
149
150    *pContext=pStreamContext;
151}
152/**
153 **********************************************************************
154 * @brief   Clean up context
155 * @param   pContext    (IN/OUT)  BitStreamParser context.
156 **********************************************************************
157*/
158static void VideoEditor3gpReader_BitStreamParserCleanUp(void* pContext) {
159    M4OSA_free((M4OSA_Int32*)pContext);
160}
161/**
162 *****************************************************************************
163 * @brief   Read the next <length> bits in the bitstream.
164 * @note    The function does not update the bitstream pointer.
165 * @param   pContext    (IN/OUT) BitStreamParser context.
166 * @param   length      (IN) The number of bits to extract from the bitstream
167 * @return  the read bits
168 *****************************************************************************
169*/
170static M4OSA_UInt32 VideoEditor3gpReader_BitStreamParserShowBits(void* pContext,
171        M4OSA_Int32 length) {
172    VideoEditor3gpReader_BitStreamParserContext* pStreamContext =
173        (VideoEditor3gpReader_BitStreamParserContext*)pContext;
174
175    M4OSA_UInt32 u_mask;
176    M4OSA_UInt32 retval;
177    M4OSA_Int32 i_ovf;
178
179    M4OSA_DEBUG_IF1((M4OSA_NULL==pStreamContext), 0,
180        "VideoEditor3gpReader_BitStreamParserShowBits:invalid context pointer");
181
182    retval=(M4OSA_UInt32)GET_MEMORY32(pStreamContext->\
183        mPbitStream[ pStreamContext->mIndex ]);
184    i_ovf = pStreamContext->mBitIndex + length - 32;
185    u_mask = (length >= 32) ? 0xffffffff: (1 << length) - 1;
186
187    /* do we have enough bits availble in the current word(32bits)*/
188    if (i_ovf <= 0) {
189        retval=(retval >> (- i_ovf)) & u_mask;
190    } else {
191        M4OSA_UInt32 u_nextword = (M4OSA_UInt32)GET_MEMORY32(
192            pStreamContext->mPbitStream[ pStreamContext->mIndex + 1 ]);
193        M4OSA_UInt32 u_msb_mask, u_msb_value, u_lsb_mask, u_lsb_value;
194
195        u_msb_mask = ((1 << (32 - pStreamContext->mBitIndex)) - 1) << i_ovf;
196        u_msb_value = retval << i_ovf;
197        u_lsb_mask = (1 << i_ovf) - 1;
198        u_lsb_value = u_nextword >> (32 - i_ovf);
199        retval= (u_msb_value & u_msb_mask ) | (u_lsb_value & u_lsb_mask);
200    }
201    /* return the bits...*/
202    return retval;
203}
204/**
205 ************************************************************************
206 * @brief   Increment the bitstream pointer of <length> bits.
207 * @param   pContext    (IN/OUT) BitStreamParser context.
208 * @param   length      (IN) The number of bit to shift the bitstream
209 ************************************************************************
210*/
211static void VideoEditor3gpReader_BitStreamParserFlushBits(void* pContext,
212        M4OSA_Int32 length) {
213    VideoEditor3gpReader_BitStreamParserContext* pStreamContext=(
214        VideoEditor3gpReader_BitStreamParserContext*)pContext;
215    M4OSA_Int32 val;
216
217    if (M4OSA_NULL == pStreamContext) {
218        return;
219    }
220    val=pStreamContext->mBitIndex + length;
221    /* update the bits...*/
222    pStreamContext->mBitIndex += length;
223
224    if (val - 32 >= 0) {
225        /* update the bits...*/
226        pStreamContext->mBitIndex -= 32;
227        /* update the words*/
228        pStreamContext->mIndex++;
229    }
230}
231
232static M4OSA_UInt32 VideoEditor3gpReader_BitStreamParserGetBits(
233        void* pContext,M4OSA_Int32 bitPos, M4OSA_Int32 bitLength) {
234    VideoEditor3gpReader_BitStreamParserContext* pStreamContext =
235        (VideoEditor3gpReader_BitStreamParserContext*)pContext;
236
237    M4OSA_Int32 bitLocation, bitIndex;
238    M4OSA_UInt32 retval=0;
239
240    M4OSA_DEBUG_IF1((M4OSA_NULL==pStreamContext), 0,
241        "VideoEditor3gpReader_BitStreamParserGetBits: invalid context pointer");
242
243    /* computes the word location*/
244    bitLocation=bitPos/32;
245    bitIndex=(bitPos) % 32;
246
247    if (bitLocation < pStreamContext->mSize) {
248        M4OSA_UInt32 u_mask;
249        M4OSA_Int32 i_ovf = bitIndex + bitLength - 32;
250        retval=(M4OSA_UInt32)GET_MEMORY32(
251            pStreamContext->mPbitStream[ bitLocation ]);
252
253        u_mask = (bitLength >= 32) ? 0xffffffff: (1 << bitLength) - 1;
254
255        if (i_ovf <= 0) {
256            retval=(retval >> (- i_ovf)) & u_mask;
257        } else {
258            M4OSA_UInt32 u_nextword = (M4OSA_UInt32)GET_MEMORY32(
259                pStreamContext->mPbitStream[ bitLocation + 1 ]);
260            M4OSA_UInt32 u_msb_mask, u_msb_value, u_lsb_mask, u_lsb_value;
261
262            u_msb_mask = ((1 << (32 - bitIndex)) - 1) << i_ovf;
263            u_msb_value = retval << i_ovf;
264            u_lsb_mask = (1 << i_ovf) - 1;
265            u_lsb_value = u_nextword >> (32 - i_ovf);
266            retval= (u_msb_value & u_msb_mask ) | (u_lsb_value & u_lsb_mask);
267        }
268    }
269    return retval;
270}
271
272static void VideoEditor3gpReader_BitStreamParserRestart(void* pContext) {
273    VideoEditor3gpReader_BitStreamParserContext* pStreamContext =
274        (VideoEditor3gpReader_BitStreamParserContext*)pContext;
275
276    if (M4OSA_NULL == pStreamContext) {
277        return;
278    }
279    /* resets the bitstream pointers*/
280    pStreamContext->mIndex=0;
281    pStreamContext->mBitIndex=0;
282}
283/**
284 *******************************************************************************
285 * @brief  Get a pointer to the current byte pointed by the bitstream pointer.
286 * @note   It should be used carefully as the pointer is in the bitstream itself
287 *         and no copy is made.
288 * @param  pContext    (IN/OUT)  BitStreamParser context.
289 * @return Pointer to the current location in the bitstream
290 *******************************************************************************
291*/
292static M4OSA_UInt8*  VideoEditor3gpReader_GetCurrentbitStreamPointer(
293        void* pContext) {
294    VideoEditor3gpReader_BitStreamParserContext* pStreamContext =
295        (VideoEditor3gpReader_BitStreamParserContext*)pContext;
296    M4OSA_DEBUG_IF1((M4OSA_NULL==pStreamContext), 0, "invalid context pointer");
297
298    return (M4OSA_UInt8*)((M4OSA_UInt8*)pStreamContext->mPbitStream + \
299        pStreamContext->mIndex * sizeof(M4OSA_UInt32) + \
300        pStreamContext->mBitIndex/8) ;
301}
302
303static M4OSA_Int32 VideoEditor3gpReader_BitStreamParserGetSize(void* pContext) {
304    VideoEditor3gpReader_BitStreamParserContext* pStreamContext =
305        (VideoEditor3gpReader_BitStreamParserContext*)pContext;
306    M4OSA_DEBUG_IF1((M4OSA_NULL==pStreamContext), 0, "invalid context pointer");
307
308    return pStreamContext->mSize;
309}
310
311
312static void VideoEditor3gpReader_MPEG4BitStreamParserInit(void** pContext,
313        void* pBitStream, M4OSA_Int32 size) {
314    VideoEditor3gpReader_BitStreamParserInit(pContext, pBitStream, size);
315}
316static M4OSA_Int32 VideoEditor3gpReader_GetMpegLengthFromInteger(void* pContext,
317        M4OSA_UInt32 val) {
318    M4OSA_UInt32 length=0;
319    M4OSA_UInt32 numBytes=0;
320    M4OSA_UInt32 b=0;
321
322    M4OSA_DEBUG_IF1((M4OSA_NULL==pContext), 0, "invalid context pointer");
323
324    /* the length is encoded as a sequence of bytes. The highest bit is used
325    to indicate that the length continues on the next byte.
326
327    The length can be: 0x80 0x80 0x80 0x22
328    of just            0x22 (highest bit not set)
329
330    */
331
332    do {
333        b=(val & ((0xff)<< (8 * numBytes)))>> (8 * numBytes);
334        length=(length << 7) | (b & 0x7f);
335        numBytes++;
336    } while ((b & 0x80) && numBytes < 4);
337
338    return length;
339}
340
341/**
342 *******************************************************************************
343 * @brief  Decode an MPEG4 Systems descriptor size from an encoded SDL size data
344 * @note   The value is read from the current bitstream location.
345 * @param  pContext    (IN/OUT)  BitStreamParser context.
346 * @return Size in a human readable form
347 *******************************************************************************
348*/
349static M4OSA_Int32 VideoEditor3gpReader_GetMpegLengthFromStream(void* pContext){
350    M4OSA_UInt32 length=0;
351    M4OSA_UInt32 numBytes=0;
352    M4OSA_UInt32 b=0;
353
354    M4OSA_DEBUG_IF1((M4OSA_NULL==pContext), 0, "invalid context pointer");
355
356    /* the length is encoded as a sequence of bytes. The highest bit is used
357    to indicate that the length continues on the next byte.
358
359    The length can be: 0x80 0x80 0x80 0x22
360    of just            0x22 (highest bit not set)
361    */
362
363    do {
364        b=VideoEditor3gpReader_BitStreamParserShowBits(pContext, 8);
365        VideoEditor3gpReader_BitStreamParserFlushBits(pContext, 8);
366        length=(length << 7) | (b & 0x7f);
367        numBytes++;
368    } while ((b & 0x80) && numBytes < 4);
369
370    return length;
371}
372#endif /* VIDEOEDITOR_BITSTREAM_PARSER */
373/**
374************************************************************************
375* @brief    create an instance of the 3gp reader
376 * @note    allocates the context
377 *
378 * @param   pContext:       (OUT)   pointer on a reader context
379 *
380 * @return  M4NO_ERROR              there is no error
381 * @return  M4ERR_ALLOC             a memory allocation has failed
382 * @return  M4ERR_PARAMETER         at least one parameter is not valid
383************************************************************************
384*/
385
386M4OSA_ERR VideoEditor3gpReader_create(M4OSA_Context *pContext) {
387    VideoEditor3gpReader_Context* pC = NULL;
388    M4OSA_ERR err = M4NO_ERROR;
389    VIDEOEDITOR_CHECK(M4OSA_NULL != pContext , M4ERR_PARAMETER);
390
391    LOGV("VideoEditor3gpReader_create begin");
392
393    /* Context allocation & initialization */
394    SAFE_MALLOC(pC, VideoEditor3gpReader_Context, 1, "VideoEditor3gpReader");
395
396    memset(pC, sizeof(VideoEditor3gpReader_Context), 0);
397
398    pC->mAudioStreamHandler  = M4OSA_NULL;
399    pC->mAudioAu.dataAddress = M4OSA_NULL;
400    pC->mVideoStreamHandler  = M4OSA_NULL;
401    pC->mVideoAu.dataAddress = M4OSA_NULL;
402
403    pC->mAudioSeeking = M4OSA_FALSE;
404    pC->mAudioSeekTime = 0;
405
406    pC->mVideoSeeking = M4OSA_FALSE;
407    pC->mVideoSeekTime = 0;
408
409    M4OSA_INT64_FROM_INT32(pC->mMaxDuration, 0);
410    *pContext=pC;
411
412cleanUp:
413    if ( M4NO_ERROR == err ) {
414        LOGV("VideoEditor3gpReader_create no error");
415    } else {
416        LOGV("VideoEditor3gpReader_create ERROR 0x%X", err);
417    }
418    LOGV("VideoEditor3gpReader_create end ");
419    return err;
420}
421
422/**
423**************************************************************************
424* @brief    destroy the instance of the 3gp reader
425* @note after this call the context is invalid
426* @param    context:        (IN)    Context of the reader
427* @return   M4NO_ERROR              there is no error
428* @return   M4ERR_PARAMETER         pContext parameter is not properly set
429**************************************************************************
430*/
431
432M4OSA_ERR VideoEditor3gpReader_destroy(M4OSA_Context pContext) {
433    M4OSA_ERR err = M4NO_ERROR;
434    VideoEditor3gpReader_Context* pC = M4OSA_NULL;
435
436    LOGV("VideoEditor3gpReader_destroy begin");
437
438    VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER);
439    pC = (VideoEditor3gpReader_Context*)pContext;
440
441    SAFE_FREE(pC->mAudioAu.dataAddress);
442    pC->mAudioAu.dataAddress = M4OSA_NULL;
443    SAFE_FREE(pC->mVideoAu.dataAddress);
444    pC->mVideoAu.dataAddress = M4OSA_NULL;
445    SAFE_FREE(pC);
446    pContext = M4OSA_NULL;
447
448cleanUp:
449    if( M4NO_ERROR == err ) {
450        LOGV("VideoEditor3gpReader_destroy no error");
451    }
452    else
453    {
454        LOGV("VideoEditor3gpReader_destroy ERROR 0x%X", err);
455    }
456
457    LOGV("VideoEditor3gpReader_destroy end ");
458    return err;
459}
460
461/**
462************************************************************************
463* @brief    open the reader and initializes its created instance
464* @note     this function open the media file
465* @param    context:            (IN)    Context of the reader
466* @param    pFileDescriptor:    (IN)    Pointer to proprietary data identifying
467*                                       the media to open
468* @return   M4NO_ERROR                  there is no error
469* @return   M4ERR_PARAMETER             the context is NULL
470************************************************************************
471*/
472
473M4OSA_ERR VideoEditor3gpReader_open(M4OSA_Context pContext,
474        M4OSA_Void* pFileDescriptor) {
475    VideoEditor3gpReader_Context* pC = (VideoEditor3gpReader_Context*)pContext;
476    M4OSA_ERR err = M4NO_ERROR;
477
478    LOGV("VideoEditor3gpReader_open start ");
479    M4OSA_DEBUG_IF1((M4OSA_NULL == pC),  M4ERR_PARAMETER,
480        "VideoEditor3gpReader_open: invalid context pointer");
481    M4OSA_DEBUG_IF1((M4OSA_NULL == pFileDescriptor), M4ERR_PARAMETER,
482        "VideoEditor3gpReader_open: invalid pointer pFileDescriptor");
483
484    LOGV("VideoEditor3gpReader_open Datasource start %s",
485        (char*)pFileDescriptor);
486    pC->mDataSource = DataSource::CreateFromURI((char*)pFileDescriptor);
487
488    if (pC->mDataSource == NULL) {
489        LOGV("VideoEditor3gpReader_open Datasource error");
490        return M4ERR_PARAMETER;
491    }
492
493    pC->mExtractor = MediaExtractor::Create(pC->mDataSource,
494        MEDIA_MIMETYPE_CONTAINER_MPEG4);
495
496    if (pC->mExtractor == NULL) {
497        LOGV("VideoEditor3gpReader_open extractor error");
498        return M4ERR_PARAMETER;
499    }
500
501    LOGV("VideoEditor3gpReader_open end ");
502    return err;
503}
504
505/**
506************************************************************************
507* @brief    close the reader
508* @note     close the 3GP file
509* @param    context:        (IN)    Context of the reader
510* @return   M4NO_ERROR              there is no error
511* @return   M4ERR_PARAMETER         the context is NULL
512* @return   M4ERR_BAD_CONTEXT       provided context is not a valid one
513************************************************************************
514*/
515M4OSA_ERR VideoEditor3gpReader_close(M4OSA_Context context) {
516    VideoEditor3gpReader_Context *pC = (VideoEditor3gpReader_Context*)context;
517    M4READER_AudioSbrUserdata *pAudioSbrUserData;
518    M4_AccessUnit *pAU;
519    M4OSA_ERR err = M4NO_ERROR;
520
521    LOGV("VideoEditor3gpReader_close begin");
522
523    M4OSA_DEBUG_IF1((M4OSA_NULL == pC), M4ERR_PARAMETER,
524        "VideoEditor3gpReader_close: invalid context pointer");
525
526    if (pC->mAudioStreamHandler) {
527        LOGV("VideoEditor3gpReader_close Audio");
528
529        if (M4OSA_NULL != pC->mAudioStreamHandler->m_pDecoderSpecificInfo) {
530            M4OSA_free((M4OSA_MemAddr32)pC->mAudioStreamHandler->\
531                m_pDecoderSpecificInfo);
532            pC->mAudioStreamHandler->m_decoderSpecificInfoSize = 0;
533            pC->mAudioStreamHandler->m_pDecoderSpecificInfo = M4OSA_NULL;
534        }
535
536        if ((M4DA_StreamTypeAudioAac == pC->mAudioStreamHandler->m_streamType)
537            && (M4OSA_NULL != pC->mAudioStreamHandler->m_pUserData)) {
538            pAudioSbrUserData = (M4READER_AudioSbrUserdata*)(\
539                pC->mAudioStreamHandler->m_pUserData);
540
541            pAU = (M4_AccessUnit*)pAudioSbrUserData->m_pFirstAU;
542            if (M4OSA_NULL != pAU) {
543                M4OSA_free((M4OSA_MemAddr32)pAU);
544            }
545
546            if (M4OSA_NULL != pAudioSbrUserData->m_pAacDecoderUserConfig) {
547                M4OSA_free((M4OSA_MemAddr32)pAudioSbrUserData->\
548                    m_pAacDecoderUserConfig);
549            }
550            M4OSA_free((M4OSA_MemAddr32)pAudioSbrUserData);
551            pC->mAudioStreamHandler->m_pUserData = M4OSA_NULL;
552        }
553
554        if (pC->mAudioStreamHandler->m_pESDSInfo != M4OSA_NULL) {
555            M4OSA_free((M4OSA_MemAddr32)pC->mAudioStreamHandler->m_pESDSInfo);
556            pC->mAudioStreamHandler->m_pESDSInfo = M4OSA_NULL;
557            pC->mAudioStreamHandler->m_ESDSInfoSize = 0;
558        }
559        /* Finally destroy the stream handler */
560        M4OSA_free((M4OSA_MemAddr32)pC->mAudioStreamHandler);
561        pC->mAudioStreamHandler = M4OSA_NULL;
562
563        pC->mAudioSource->stop();
564        pC->mAudioSource.clear();
565    }
566    if (pC->mVideoStreamHandler) {
567        LOGV("VideoEditor3gpReader_close Video ");
568
569        if(M4OSA_NULL != pC->mVideoStreamHandler->m_pDecoderSpecificInfo) {
570            M4OSA_free((M4OSA_MemAddr32)pC->mVideoStreamHandler->\
571                m_pDecoderSpecificInfo);
572            pC->mVideoStreamHandler->m_decoderSpecificInfoSize = 0;
573            pC->mVideoStreamHandler->m_pDecoderSpecificInfo = M4OSA_NULL;
574        }
575
576        if(M4OSA_NULL != pC->mVideoStreamHandler->m_pH264DecoderSpecificInfo) {
577            M4OSA_free((M4OSA_MemAddr32)pC->mVideoStreamHandler->\
578                m_pH264DecoderSpecificInfo);
579            pC->mVideoStreamHandler->m_H264decoderSpecificInfoSize = 0;
580            pC->mVideoStreamHandler->m_pH264DecoderSpecificInfo = M4OSA_NULL;
581        }
582
583        if(pC->mVideoStreamHandler->m_pESDSInfo != M4OSA_NULL) {
584            M4OSA_free((M4OSA_MemAddr32)pC->mVideoStreamHandler->m_pESDSInfo);
585            pC->mVideoStreamHandler->m_pESDSInfo = M4OSA_NULL;
586            pC->mVideoStreamHandler->m_ESDSInfoSize = 0;
587        }
588
589        /* Finally destroy the stream handler */
590        M4OSA_free((M4OSA_MemAddr32)pC->mVideoStreamHandler);
591        pC->mVideoStreamHandler = M4OSA_NULL;
592
593        pC->mVideoSource->stop();
594        pC->mVideoSource.clear();
595    }
596    pC->mDataSource.clear();
597
598    LOGV("VideoEditor3gpReader_close end");
599    return err;
600}
601
602/**
603************************************************************************
604* @brief    get an option from the 3gp reader
605* @note     it allows the caller to retrieve a property value:
606*
607* @param    context:        (IN)    Context of the reader
608* @param    optionId:       (IN)    indicates the option to get
609* @param    pValue:         (OUT)   pointer to structure or value (allocated
610*                                   by user) where option is stored
611*
612* @return   M4NO_ERROR              there is no error
613* @return   M4ERR_BAD_CONTEXT       provided context is not a valid one
614* @return   M4ERR_PARAMETER         at least one parameter is not properly set
615* @return   M4ERR_BAD_OPTION_ID     when the option ID is not a valid one
616* @return   M4ERR_VIDEO_NOT_H263    No video stream H263 in file.
617* @return   M4ERR_NO_VIDEO_STREAM_RETRIEVED_YET
618*           Function 3gpReader_getNextStreamHandler must be called before
619************************************************************************
620*/
621M4OSA_ERR VideoEditor3gpReader_getOption(M4OSA_Context context,
622        M4OSA_OptionID optionId, M4OSA_DataOption pValue) {
623    VideoEditor3gpReader_Context* pC = (VideoEditor3gpReader_Context*)context;
624    M4OSA_ERR err = M4NO_ERROR;
625
626    LOGV("VideoEditor3gpReader_getOption begin %d", optionId);
627
628    M4OSA_DEBUG_IF1((M4OSA_NULL == pC), M4ERR_PARAMETER,
629        "invalid context pointer");
630    M4OSA_DEBUG_IF1((M4OSA_NULL == pValue), M4ERR_PARAMETER,
631        "VideoEditor3gpReader_getOption: invalid pointer on value");
632
633    switch (optionId) {
634    case M4READER_kOptionID_Duration:
635        {
636            LOGV("VideoEditor3gpReader_getOption duration %d",pC->mMaxDuration);
637            M4OSA_TIME_SET(*(M4OSA_Time*)pValue, pC->mMaxDuration);
638        }
639        break;
640    case M4READER_kOptionID_Version:
641        /* not used */
642        LOGV("VideoEditor3gpReader_getOption: M4READER_kOptionID_Version");
643        break;
644
645    case M4READER_kOptionID_Copyright:
646        /* not used */
647        LOGV(">>>>>>>   M4READER_kOptionID_Copyright");
648        break;
649
650    case M4READER_kOptionID_CreationTime:
651        /* not used */
652        LOGV("VideoEditor3gpReader_getOption M4READER_kOptionID_CreationTime");
653    break;
654
655    case M4READER_kOptionID_Bitrate:
656        {
657            M4OSA_UInt32* pBitrate = (M4OSA_UInt32*)pValue;
658
659            if (pC->mMaxDuration != 0) {
660                M4OSA_UInt32 ui32Tmp = (M4OSA_UInt32)pC->mMaxDuration;
661                *pBitrate = (M4OSA_UInt32)((M4OSA_Double)pC->mFileSize * \
662                    8000.0 / (M4OSA_Double)ui32Tmp);
663                LOGV("3gpReader_getOption bitrate:  %d", *pBitrate);
664            }
665            *pBitrate = 384000; //check
666            LOGV("VideoEditor3gpReader_getOption bitrate %ld", *pBitrate);
667        }
668    break;
669    case M4READER_3GP_kOptionID_H263Properties:
670        {
671#if 0
672            if(M4OSA_NULL == pC->mVideoStreamHandler) {
673                LOGV("VideoEditor3gpReader_getOption no videoStream retrieved");
674
675                err = M4ERR_NO_VIDEO_STREAM_RETRIEVED_YET;
676                break;
677            }
678            if((M4DA_StreamTypeVideoH263 != pC->mVideoStreamHandler->\
679                mStreamType) || (pC->mVideoStreamHandler->\
680                m_decoderSpecificInfoSize < 7)) {
681                LOGV("VideoEditor3gpReader_getOption DSI Size %d",
682                    pC->mVideoStreamHandler->m_decoderSpecificInfoSize);
683
684                err = M4ERR_VIDEO_NOT_H263;
685                break;
686            }
687
688            /* MAGICAL in the decoder confi H263: the 7th byte is the profile
689             * number, 6th byte is the level number */
690            ((M4READER_3GP_H263Properties *)pValue)->uiProfile =
691                pC->mVideoStreamHandler->m_pDecoderSpecificInfo[6];
692            ((M4READER_3GP_H263Properties *)pValue)->uiLevel =
693                pC->mVideoStreamHandler->m_pDecoderSpecificInfo[5];
694#endif
695            LOGV("VideoEditor3gpReader_getOption M4READER_3GP_kOptionID_\
696            H263Properties end");
697        }
698        break;
699    case M4READER_3GP_kOptionID_PurpleLabsDrm:
700        LOGV("VideoEditor3gpReaderOption M4READER_3GP_kOptionID_PurpleLabsDrm");
701        /* not used */
702        break;
703
704    case M4READER_kOptionID_GetNumberOfAudioAu:
705        /* not used */
706        LOGV("VideoEditor3gpReadeOption M4READER_kOptionID_GetNumberOfAudioAu");
707    break;
708
709    case M4READER_kOptionID_GetNumberOfVideoAu:
710        /* not used */
711        LOGV("VideoEditor3gpReader_getOption :GetNumberOfVideoAu");
712    break;
713
714    case M4READER_kOptionID_GetMetadata:
715        /* not used */
716        LOGV("VideoEditor3gpReader_getOption M4READER_kOptionID_GetMetadata");
717    break;
718
719    case M4READER_kOptionID_3gpFtypBox:
720        /* used only for SEMC */
721        LOGV("VideoEditor3gpReader_getOption M4READER_kOptionID_3gpFtypBox");
722        err = M4ERR_BAD_OPTION_ID; //check this
723        break;
724
725#ifdef OPTIONID_GET_NEXT_VIDEO_CTS
726    case M4READER_3GP_kOptionID_getNextVideoCTS:
727        /* not used */
728        LOGV("VideoEditor3gpReader_getOption: getNextVideoCTS");
729        break;
730#endif
731    default:
732        {
733            err = M4ERR_BAD_OPTION_ID;
734            LOGV("VideoEditor3gpReader_getOption M4ERR_BAD_OPTION_ID");
735        }
736        break;
737    }
738    LOGV("VideoEditor3gpReader_getOption end: optionID: x%x", optionId);
739    return err;
740}
741/**
742************************************************************************
743* @brief    set an option on the 3gp reader
744* @note No option can be set yet.
745* @param    context:        (IN)    Context of the reader
746* @param    optionId:       (IN)    indicates the option to set
747* @param    pValue:         (IN)    pointer to structure or value (allocated
748*                                   by user) where option is stored
749* @return   M4NO_ERROR              there is no error
750* @return   M4ERR_BAD_CONTEXT       provided context is not a valid one
751* @return   M4ERR_PARAMETER         at least one parameter is not properly set
752* @return   M4ERR_BAD_OPTION_ID     when the option ID is not a valid one
753************************************************************************
754*/
755M4OSA_ERR VideoEditor3gpReader_setOption(M4OSA_Context context,
756        M4OSA_OptionID optionId, M4OSA_DataOption pValue) {
757    VideoEditor3gpReader_Context* pC = (VideoEditor3gpReader_Context*)context;
758    M4OSA_ERR err = M4NO_ERROR;
759
760    /* Check function parameters */
761    M4OSA_DEBUG_IF1((M4OSA_NULL == pC), M4ERR_PARAMETER,
762        "invalid context pointer");
763    M4OSA_DEBUG_IF1((M4OSA_NULL == pValue), M4ERR_PARAMETER,
764        "invalid value pointer");
765
766    LOGV("VideoEditor3gpReader_setOption begin %d",optionId);
767
768    switch(optionId) {
769        case M4READER_kOptionID_SetOsaFileReaderFctsPtr:
770        break;
771
772        case M4READER_3GP_kOptionID_AudioOnly:
773        break;
774
775        case M4READER_3GP_kOptionID_VideoOnly:
776        break;
777
778        case M4READER_3GP_kOptionID_FastOpenMode:
779        break;
780
781        case M4READER_kOptionID_MaxMetadataSize:
782        break;
783
784        default:
785        {
786            LOGV("VideoEditor3gpReader_setOption: returns M4ERR_BAD_OPTION_ID");
787            err = M4ERR_BAD_OPTION_ID;
788        }
789        break;
790    }
791    LOGV("VideoEditor3gpReader_setOption end ");
792    return err;
793}
794/**
795 ************************************************************************
796 * @brief   fill the access unit structure with initialization values
797 * @param   context:        (IN)     Context of the reader
798 * @param   pStreamHandler: (IN)     pointer to the stream handler to which
799 *                                   the access unit will be associated
800 * @param   pAccessUnit:    (IN/OUT) pointer to the access unit (allocated
801 *                                   by the caller) to initialize
802 * @return  M4NO_ERROR               there is no error
803 * @return  M4ERR_PARAMETER          at least one parameter is not properly set
804 ************************************************************************
805*/
806M4OSA_ERR VideoEditor3gpReader_fillAuStruct(M4OSA_Context context,
807        M4_StreamHandler *pStreamHandler, M4_AccessUnit *pAccessUnit) {
808    VideoEditor3gpReader_Context* pC = (VideoEditor3gpReader_Context*)context;
809    M4OSA_ERR err= M4NO_ERROR;
810
811    M4OSA_DEBUG_IF1((pC == 0),             M4ERR_PARAMETER,
812        "VideoEditor3gpReader_fillAuStruct: invalid context");
813    M4OSA_DEBUG_IF1((pStreamHandler == 0), M4ERR_PARAMETER,
814        "VideoEditor3gpReader_fillAuStruc invalid pointer to M4_StreamHandler");
815    M4OSA_DEBUG_IF1((pAccessUnit == 0),    M4ERR_PARAMETER,
816        "VideoEditor3gpReader_fillAuStruct: invalid pointer to M4_AccessUnit");
817
818    LOGV("VideoEditor3gpReader_fillAuStruct begin");
819
820    /* Initialize pAccessUnit structure */
821    pAccessUnit->m_size         = 0;
822    pAccessUnit->m_CTS          = 0;
823    pAccessUnit->m_DTS          = 0;
824    pAccessUnit->m_attribute    = 0;
825    pAccessUnit->m_dataAddress  = M4OSA_NULL;
826    pAccessUnit->m_maxsize      = pStreamHandler->m_maxAUSize;
827    pAccessUnit->m_streamID     = pStreamHandler->m_streamId;
828    pAccessUnit->m_structSize   = sizeof(M4_AccessUnit);
829
830    LOGV("VideoEditor3gpReader_fillAuStruct end");
831    return M4NO_ERROR;
832}
833
834/**
835********************************************************************************
836* @brief    jump into the stream at the specified time
837* @note
838* @param    context:        (IN)   Context of the reader
839* @param    pStreamHandler  (IN)   the stream handler of the stream to make jump
840* @param    pTime           (I/O)IN  the time to jump to (in ms)
841*                                OUT the time to which the stream really jumped
842* @return   M4NO_ERROR             there is no error
843* @return   M4ERR_PARAMETER        at least one parameter is not properly set
844********************************************************************************
845*/
846M4OSA_ERR VideoEditor3gpReader_jump(M4OSA_Context context,
847        M4_StreamHandler *pStreamHandler, M4OSA_Int32* pTime) {
848    VideoEditor3gpReader_Context* pC = (VideoEditor3gpReader_Context*)context;
849    M4OSA_ERR err = M4NO_ERROR;
850    M4SYS_AccessUnit* pAu;
851    M4OSA_Time time64;
852    M4OSA_Double timeDouble;
853
854    M4OSA_DEBUG_IF1((pC == 0), M4ERR_PARAMETER,
855        "VideoEditor3gpReader_jump: invalid context");
856    M4OSA_DEBUG_IF1((pStreamHandler == 0), M4ERR_PARAMETER,
857        "VideoEditor3gpReader_jump: invalid pointer to M4_StreamHandler");
858    M4OSA_DEBUG_IF1((pTime == 0), M4ERR_PARAMETER,
859        "VideoEditor3gpReader_jump: invalid time pointer");
860
861    LOGV("VideoEditor3gpReader_jump begin");
862
863    if (*pTime == (pStreamHandler->m_duration)) {
864		*pTime -= 1;
865	}
866    M4OSA_INT64_FROM_INT32(time64, *pTime);
867
868    LOGV("VideoEditor3gpReader_jump time us %ld ", time64);
869
870    if ((pC->mAudioStreamHandler != M4OSA_NULL) &&
871            (pStreamHandler->m_streamId == pC->mAudioStreamHandler->m_streamId))
872            {
873        pAu = &pC->mAudioAu;
874        pAu->CTS = time64;
875        pAu->DTS = time64;
876
877        time64 = time64 * 1000; /* Convert the time into micro sec */
878        pC->mAudioSeeking = M4OSA_TRUE;
879        pC->mAudioSeekTime = time64;
880        LOGV("VideoEditor3gpReader_jump AUDIO time us %ld ", time64);
881    } else if ((pC->mVideoStreamHandler != M4OSA_NULL) &&
882            (pStreamHandler->m_streamId == pC->mVideoStreamHandler->m_streamId))
883            {
884        pAu = &pC->mVideoAu;
885        pAu->CTS = time64;
886        pAu->DTS = time64;
887
888        time64 = time64 * 1000; /* Convert the time into micro sec */
889        pC->mVideoSeeking = M4OSA_TRUE;
890        pC->mVideoSeekTime = time64;
891        LOGV("VideoEditor3gpReader_jump VIDEO time us %ld ", time64);
892    } else {
893        LOGV("VideoEditor3gpReader_jump passed StreamHandler is not known\n");
894        return M4ERR_PARAMETER;
895    }
896    time64 = time64 / 1000; /* Convert the time into milli sec */
897    LOGV("VideoEditor3gpReader_jump time ms before seekset %ld ", time64);
898
899    M4OSA_INT64_TO_DOUBLE(timeDouble, time64);
900    *pTime = (M4OSA_Int32)timeDouble;
901
902    LOGV("VideoEditor3gpReader_jump end");
903    err = M4NO_ERROR;
904    return err;
905}
906/**
907********************************************************************************
908* @brief    reset the stream, that is seek it to beginning and make it ready
909* @note
910* @param    context:        (IN)    Context of the reader
911* @param    pStreamHandler  (IN)    The stream handler of the stream to reset
912* @return   M4NO_ERROR              there is no error
913* @return   M4ERR_PARAMETER         at least one parameter is not properly set
914********************************************************************************
915*/
916M4OSA_ERR VideoEditor3gpReader_reset(M4OSA_Context context,
917        M4_StreamHandler *pStreamHandler) {
918    VideoEditor3gpReader_Context* pC = (VideoEditor3gpReader_Context*)context;
919    M4OSA_ERR err = M4NO_ERROR;
920    M4SYS_StreamID streamIdArray[2];
921    M4SYS_AccessUnit* pAu;
922    M4OSA_Time time64;
923
924    M4OSA_DEBUG_IF1((pC == 0), M4ERR_PARAMETER,
925        "VideoEditor3gpReader_reset: invalid context");
926    M4OSA_DEBUG_IF1((pStreamHandler == 0), M4ERR_PARAMETER,
927        "VideoEditor3gpReader_reset: invalid pointer to M4_StreamHandler");
928
929    M4OSA_INT64_FROM_INT32(time64, 0);
930
931    LOGV("VideoEditor3gpReader_reset begin");
932
933    if (pStreamHandler == (M4_StreamHandler*)pC->mAudioStreamHandler) {
934        pAu = &pC->mAudioAu;
935    } else if (pStreamHandler == (M4_StreamHandler*)pC->mVideoStreamHandler) {
936        pAu = &pC->mVideoAu;
937    } else {
938        LOGV("VideoEditor3gpReader_reset passed StreamHandler is not known\n");
939        return M4ERR_PARAMETER;
940    }
941
942    pAu->CTS = time64;
943    pAu->DTS = time64;
944
945    LOGV("VideoEditor3gpReader_reset end");
946    return err;
947}
948
949/**
950********************************************************************************
951* @brief  Gets an access unit (AU) from the stream handler source.
952* @note   An AU is the smallest possible amount of data to be decoded by decoder
953*
954* @param    context:        (IN) Context of the reader
955* @param    pStreamHandler  (IN) The stream handler of the stream to make jump
956* @param    pAccessUnit     (IO) Pointer to access unit to fill with read data
957* @return   M4NO_ERROR           there is no error
958* @return   M4ERR_PARAMETER      at least one parameter is not properly set
959* @returns  M4ERR_ALLOC          memory allocation failed
960* @returns  M4WAR_NO_MORE_AU     there are no more access unit in the stream
961********************************************************************************
962*/
963M4OSA_ERR VideoEditor3gpReader_getNextAu(M4OSA_Context context,
964        M4_StreamHandler *pStreamHandler, M4_AccessUnit *pAccessUnit) {
965    VideoEditor3gpReader_Context* pC=(VideoEditor3gpReader_Context*)context;
966    M4OSA_ERR err = M4NO_ERROR;
967    M4SYS_AccessUnit* pAu;
968    int64_t tempTime64 = 0;
969    MediaBuffer *mMediaBuffer = NULL;
970    MediaSource::ReadOptions options;
971    M4OSA_Bool flag = M4OSA_FALSE;
972    status_t error;
973
974    M4OSA_DEBUG_IF1((pReaderContext == 0), M4ERR_PARAMETER,
975        "VideoEditor3gpReader_getNextAu: invalid context");
976    M4OSA_DEBUG_IF1((pStreamHandler == 0), M4ERR_PARAMETER,
977        "VideoEditor3gpReader_getNextAu: invalid pointer to M4_StreamHandler");
978    M4OSA_DEBUG_IF1((pAccessUnit == 0),    M4ERR_PARAMETER,
979        "VideoEditor3gpReader_getNextAu: invalid pointer to M4_AccessUnit");
980
981    LOGV("VideoEditor3gpReader_getNextAu begin");
982
983    if (pStreamHandler == (M4_StreamHandler*)pC->mAudioStreamHandler) {
984        LOGV("VideoEditor3gpReader_getNextAu audio stream");
985        pAu = &pC->mAudioAu;
986        if (pC->mAudioSeeking == M4OSA_TRUE) {
987            LOGV("VideoEditor3gpReader_getNextAu audio seek time: %ld",
988                pC->mAudioSeekTime);
989            options.setSeekTo(pC->mAudioSeekTime);
990            pC->mAudioSource->read(&mMediaBuffer, &options);
991
992            mMediaBuffer->meta_data()->findInt64(kKeyTime,
993                (int64_t*)&tempTime64);
994            options.clearSeekTo();
995            pC->mAudioSeeking = M4OSA_FALSE;
996            flag = M4OSA_TRUE;
997        } else {
998            LOGV("VideoEditor3gpReader_getNextAu audio no seek:");
999            pC->mAudioSource->read(&mMediaBuffer, &options);
1000            if (mMediaBuffer != NULL) {
1001                mMediaBuffer->meta_data()->findInt64(kKeyTime,
1002                    (int64_t*)&tempTime64);
1003            }
1004        }
1005    } else if (pStreamHandler == (M4_StreamHandler*)pC->mVideoStreamHandler) {
1006        LOGV("VideoEditor3gpReader_getNextAu video steram ");
1007        pAu = &pC->mVideoAu;
1008        if(pC->mVideoSeeking == M4OSA_TRUE) {
1009            flag = M4OSA_TRUE;
1010            LOGV("VideoEditor3gpReader_getNextAu seek: %ld",pC->mVideoSeekTime);
1011            options.setSeekTo(pC->mVideoSeekTime,
1012                MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
1013            do
1014            {
1015                if (mMediaBuffer != NULL) {
1016                    LOGV("VideoEditor3gpReader_getNextAu free the MediaBuffer");
1017                    mMediaBuffer->release();
1018                }
1019                error = pC->mVideoSource->read(&mMediaBuffer, &options);
1020                LOGV("VE3gpReader_getNextAu MediaBuffer %x , error %d",
1021                    mMediaBuffer, error);
1022                if (mMediaBuffer != NULL)
1023                {
1024                    mMediaBuffer->meta_data()->findInt64(kKeyTime,
1025                        (int64_t*)&tempTime64);
1026                } else {
1027                    break;
1028                }
1029                options.clearSeekTo();
1030            } while(tempTime64 < pC->mVideoSeekTime);
1031
1032            LOGV("VE3gpReader_getNextAu: video  time with seek  = %lld:",
1033                tempTime64);
1034            pC->mVideoSeeking = M4OSA_FALSE;
1035        } else {
1036            LOGV("VideoEditor3gpReader_getNextAu video no seek:");
1037            pC->mVideoSource->read(&mMediaBuffer, &options);
1038
1039            if(mMediaBuffer != NULL) {
1040                mMediaBuffer->meta_data()->findInt64(kKeyTime,
1041                    (int64_t*)&tempTime64);
1042                LOGV("VE3gpReader_getNextAu: video no seek time = %lld:",
1043                    tempTime64);
1044            }else {
1045                LOGV("VE3gpReader_getNextAu:video no seek time buffer is NULL");
1046            }
1047        }
1048    } else {
1049        LOGV("VideoEditor3gpReader_getNextAu M4ERR_PARAMETER");
1050        return M4ERR_PARAMETER;
1051    }
1052
1053    if (mMediaBuffer != NULL) {
1054        if( (pAu->dataAddress == NULL) ||  (pAu->size < \
1055            mMediaBuffer->range_length())) {
1056            if(pAu->dataAddress != NULL) {
1057                M4OSA_free((M4OSA_Int32*)pAu->dataAddress);
1058                pAu->dataAddress = NULL;
1059            }
1060            LOGV("Buffer lenght = %d ,%d",(mMediaBuffer->range_length() +\
1061                3) & ~0x3,(mMediaBuffer->range_length()));
1062
1063            pAu->dataAddress = (M4OSA_Int32*)M4OSA_malloc(
1064                (mMediaBuffer->range_length() + 3) & ~0x3,M4READER_3GP,
1065                    (M4OSA_Char*)"pAccessUnit->m_dataAddress" );
1066            if(pAu->dataAddress == NULL) {
1067                LOGV("VideoEditor3gpReader_getNextAu malloc failed");
1068                return M4ERR_ALLOC;
1069            }
1070        }
1071        pAu->size = mMediaBuffer->range_length();
1072
1073        memcpy((M4OSA_MemAddr8)pAu->dataAddress,
1074            (const char *)mMediaBuffer->data() + mMediaBuffer->range_offset(),
1075            mMediaBuffer->range_length());
1076
1077        if( (pStreamHandler == (M4_StreamHandler*)pC->mVideoStreamHandler)  &&
1078            (pStreamHandler->m_streamType == M4DA_StreamTypeVideoMpeg4Avc) ) {
1079            M4OSA_UInt32 size = mMediaBuffer->range_length();
1080            M4OSA_UInt8 *lbuffer;
1081
1082            lbuffer = (M4OSA_UInt8 *) pAu->dataAddress;
1083            LOGV("pAccessUnit->m_dataAddress size = %x",size);
1084
1085            lbuffer[0] = (size >> 24) & 0xFF;
1086            lbuffer[1] = (size >> 16) & 0xFF;
1087            lbuffer[2] = (size >> 8) & 0xFF;
1088            lbuffer[3] = (size) & 0xFF;
1089        }
1090
1091        pAu->CTS = tempTime64;
1092
1093        pAu->CTS = pAu->CTS / 1000; //converting the microsec to millisec
1094        LOGV("VideoEditor3gpReader_getNextAu CTS = %ld",pAu->CTS);
1095
1096        pAu->DTS  = pAu->CTS;
1097        pAu->attribute = M4SYS_kFragAttrOk;
1098        mMediaBuffer->release();
1099
1100        pAccessUnit->m_dataAddress = (M4OSA_Int8*) pAu->dataAddress;
1101        pAccessUnit->m_size = pAu->size;
1102        pAccessUnit->m_maxsize = pAu->size;
1103        pAccessUnit->m_CTS = pAu->CTS;
1104        pAccessUnit->m_DTS = pAu->DTS;
1105        pAccessUnit->m_attribute = pAu->attribute;
1106
1107    } else {
1108        LOGV("VideoEditor3gpReader_getNextAu: M4WAR_NO_MORE_AU (EOS) reached");
1109        pAccessUnit->m_size = 0;
1110        err = M4WAR_NO_MORE_AU;
1111    }
1112    options.clearSeekTo();
1113
1114    pAu->nbFrag = 0;
1115    mMediaBuffer = NULL;
1116    LOGV("VideoEditor3gpReader_getNextAu end ");
1117
1118    return err;
1119}
1120/**
1121 *******************************************************************************
1122 * @brief   Split the AVC DSI in its different components and write it in
1123 *          ONE memory buffer
1124 * @note
1125 * @param   pStreamHandler:         (IN/OUT) The MPEG4-AVC stream
1126 * @param   pDecoderConfigLocal:    (IN) The DSI buffer
1127 * @param   decoderConfigSizeLocal: (IN) The DSI buffer size
1128 * @return  M4NO_ERROR              there is no error
1129 * @return  ERR_FILE_SYNTAX_ERROR   pDecoderConfigLocal is NULL
1130 *******************************************************************************
1131*/
1132static M4OSA_ERR VideoEditor3gpReader_AnalyseAvcDsi(
1133        M4_StreamHandler *pStreamHandler, M4OSA_Int32* pDecoderConfigLocal,
1134        M4OSA_Int32 decoderConfigSizeLocal) {
1135    struct _avcSpecificInfo *pAvcSpecInfo = M4OSA_NULL;
1136    M4OSA_UInt32 uiSpecInfoSize;
1137    M4OSA_Context pBitParserContext = M4OSA_NULL;
1138    M4OSA_MemAddr8 pPos;
1139
1140    /**
1141     * First parsing to get the total allocation size (we must not do
1142     * multiple malloc, but only one instead) */
1143    {
1144        M4OSA_Int32 val;
1145        M4OSA_UInt32 i,j;
1146        M4OSA_UInt8 nalUnitLength;
1147        M4OSA_UInt8  numOfSequenceParameterSets;
1148        M4OSA_UInt32 uiTotalSizeOfSPS = 0;
1149        M4OSA_UInt8  numOfPictureParameterSets;
1150        M4OSA_UInt32 uiTotalSizeOfPPS = 0;
1151        M4OSA_UInt32 uiSize;
1152        struct _avcSpecificInfo avcSpIf;
1153
1154        avcSpIf.m_nalUnitLength = 0;
1155
1156        if (M4OSA_NULL == pDecoderConfigLocal) {
1157            return M4ERR_READER3GP_DECODER_CONFIG_ERROR;
1158        }
1159
1160        VideoEditor3gpReader_MPEG4BitStreamParserInit(&pBitParserContext,
1161            pDecoderConfigLocal, decoderConfigSizeLocal);
1162
1163        if (M4OSA_NULL == pBitParserContext) {
1164            return M4ERR_ALLOC;
1165        }
1166
1167        VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8);
1168                                       /* 8 bits -- configuration version */
1169        VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8);
1170                                       /* 8 bits -- avc profile indication*/
1171        VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8);
1172                                       /* 8 bits -- profile compatibility */
1173        VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8);
1174                                       /* 8 bits -- avc level indication*/
1175        val=VideoEditor3gpReader_BitStreamParserShowBits(pBitParserContext, 8);
1176                       /* 6 bits reserved 111111b 2 bits length Size minus one*/
1177        VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8);
1178                                       /* m_nalUnitLength */
1179
1180        nalUnitLength = (M4OSA_UInt8)((val & 0x03) + 1);/*0b11111100*/
1181        if (nalUnitLength > 4) {
1182            pStreamHandler->m_decoderSpecificInfoSize = 0;
1183            pStreamHandler->m_pDecoderSpecificInfo = M4OSA_NULL;
1184            VideoEditor3gpReader_BitStreamParserCleanUp(pBitParserContext);
1185        } else {
1186            /**
1187             * SPS table */
1188            val=VideoEditor3gpReader_BitStreamParserShowBits(pBitParserContext,
1189            8);/* 3 bits-reserved 111b-5 bits number of sequence parameter set*/
1190            numOfSequenceParameterSets = val & 0x1F;
1191            /*1F instead of E0*/ /*0b11100000*/ /*Number of seq parameter sets*/
1192            VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8);
1193            for (i=0; i < numOfSequenceParameterSets; i++) {
1194                /**
1195                 * Get the size of this element */
1196                uiSize =
1197                    (M4OSA_UInt32)VideoEditor3gpReader_BitStreamParserShowBits(
1198                    pBitParserContext, 16);
1199                uiTotalSizeOfSPS += uiSize;
1200                VideoEditor3gpReader_BitStreamParserFlushBits(
1201                    pBitParserContext, 16);
1202                /**
1203                 *Read the element(dont keep it, we only want size right now) */
1204                for (j=0; j<uiSize; j++) {
1205                    VideoEditor3gpReader_BitStreamParserFlushBits(
1206                        pBitParserContext, 8);
1207                }
1208            }
1209
1210            /**
1211             * SPS table */
1212            numOfPictureParameterSets=(M4OSA_UInt8)\
1213                VideoEditor3gpReader_BitStreamParserShowBits(pBitParserContext,
1214                    8);
1215            VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8);
1216            for (i=0; i < numOfPictureParameterSets; i++) {
1217                /**
1218                 * Get the size of this element */
1219                uiSize = (M4OSA_UInt32)
1220                    VideoEditor3gpReader_BitStreamParserShowBits(
1221                    pBitParserContext, 16);
1222                uiTotalSizeOfPPS += uiSize;
1223                VideoEditor3gpReader_BitStreamParserFlushBits(
1224                    pBitParserContext, 16);
1225                /**
1226                 *Read the element(dont keep it,we only want size right now)*/
1227                for (j=0; j<uiSize; j++) {
1228                    VideoEditor3gpReader_BitStreamParserFlushBits(
1229                        pBitParserContext, 8);
1230                }
1231            }
1232
1233            /**
1234             * Compute the size of the full buffer */
1235            uiSpecInfoSize = sizeof(struct _avcSpecificInfo) +
1236                     numOfSequenceParameterSets * sizeof(struct _parameterSet)
1237                     + /**< size of the table of SPS elements */
1238                     numOfPictureParameterSets  * sizeof(struct _parameterSet)
1239                     + /**< size of the table of PPS elements */
1240                     uiTotalSizeOfSPS +
1241                     uiTotalSizeOfPPS;
1242            /**
1243             * Allocate the buffer */
1244            pAvcSpecInfo =(struct _avcSpecificInfo*)M4OSA_malloc(uiSpecInfoSize,
1245                M4READER_3GP, (M4OSA_Char*)"MPEG-4 AVC DecoderSpecific");
1246            if (M4OSA_NULL == pAvcSpecInfo) {
1247                VideoEditor3gpReader_BitStreamParserCleanUp(pBitParserContext);
1248                return M4ERR_ALLOC;
1249            }
1250
1251            /**
1252             * Set the pointers to the correct part of the buffer */
1253            pAvcSpecInfo->m_nalUnitLength = nalUnitLength;
1254            pAvcSpecInfo->m_numOfSequenceParameterSets =
1255                numOfSequenceParameterSets;
1256            pAvcSpecInfo->m_numOfPictureParameterSets  =
1257                numOfPictureParameterSets;
1258
1259            /* We place the SPS param sets table after m_pPictureParameterSet */
1260            pAvcSpecInfo->m_pSequenceParameterSet= (struct _parameterSet*)(
1261                (M4OSA_MemAddr8)(&pAvcSpecInfo->m_pPictureParameterSet) +
1262                sizeof(pAvcSpecInfo->m_pPictureParameterSet));
1263            /*We place the PPS param sets table after the SPS param sets table*/
1264            pAvcSpecInfo->m_pPictureParameterSet = (struct _parameterSet*)(
1265                (M4OSA_MemAddr8)(pAvcSpecInfo->m_pSequenceParameterSet) +
1266                (numOfSequenceParameterSets * sizeof(struct _parameterSet)));
1267            /**< The data will be placed after the PPS param sets table */
1268            pPos = (M4OSA_MemAddr8)pAvcSpecInfo->m_pPictureParameterSet +
1269                (numOfPictureParameterSets * sizeof(struct _parameterSet));
1270
1271            /**
1272             * reset the bit parser */
1273            VideoEditor3gpReader_BitStreamParserCleanUp(pBitParserContext);
1274        }
1275    }
1276
1277    /**
1278     * Second parsing to copy the data */
1279    if (M4OSA_NULL != pAvcSpecInfo) {
1280        M4OSA_Int32 i,j;
1281
1282        VideoEditor3gpReader_MPEG4BitStreamParserInit(&pBitParserContext,
1283            pDecoderConfigLocal, decoderConfigSizeLocal);
1284
1285        if (M4OSA_NULL == pBitParserContext) {
1286            M4OSA_free((M4OSA_MemAddr32)pAvcSpecInfo);
1287            return M4ERR_ALLOC;
1288        }
1289
1290        VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8);
1291            /* 8 bits -- configuration version */
1292        VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8);
1293            /* 8 bits -- avc profile indication*/
1294        VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8);
1295            /* 8 bits -- profile compatibility */
1296        VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8);
1297            /* 8 bits -- avc level indication*/
1298        VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8);
1299            /* m_nalUnitLength */
1300        VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8);
1301        /* 3 bits -- reserved 111b -- 5 bits number of sequence parameter set*/
1302
1303        for (i=0; i < pAvcSpecInfo->m_numOfSequenceParameterSets; i++) {
1304            pAvcSpecInfo->m_pSequenceParameterSet[i].m_length =
1305                (M4OSA_UInt16)VideoEditor3gpReader_BitStreamParserShowBits(
1306                pBitParserContext, 16);
1307            VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext,16);
1308
1309            pAvcSpecInfo->m_pSequenceParameterSet[i].m_pParameterSetUnit =
1310                (M4OSA_UInt8*)pPos;  /**< current position in the buffer */
1311            pPos += pAvcSpecInfo->m_pSequenceParameterSet[i].m_length;
1312                /**< increment the position in the buffer */
1313            for (j=0; j<pAvcSpecInfo->m_pSequenceParameterSet[i].m_length;j++){
1314                pAvcSpecInfo->m_pSequenceParameterSet[i].m_pParameterSetUnit[j]=
1315                    (M4OSA_UInt8)VideoEditor3gpReader_BitStreamParserShowBits(
1316                    pBitParserContext, 8);
1317                VideoEditor3gpReader_BitStreamParserFlushBits(
1318                    pBitParserContext, 8);
1319            }
1320        }
1321
1322        VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8);
1323            /* number of pîcture parameter set*/
1324
1325        for (i=0; i < pAvcSpecInfo->m_numOfPictureParameterSets; i++) {
1326            pAvcSpecInfo->m_pPictureParameterSet[i].m_length =
1327                (M4OSA_UInt16)VideoEditor3gpReader_BitStreamParserShowBits(
1328                pBitParserContext, 16);
1329            VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext,16);
1330
1331            pAvcSpecInfo->m_pPictureParameterSet[i].m_pParameterSetUnit =
1332                (M4OSA_UInt8*)pPos;   /**< current position in the buffer */
1333            pPos += pAvcSpecInfo->m_pPictureParameterSet[i].m_length;
1334                /**< increment the position in the buffer */
1335            for (j=0; j<pAvcSpecInfo->m_pPictureParameterSet[i].m_length; j++) {
1336                pAvcSpecInfo->m_pPictureParameterSet[i].m_pParameterSetUnit[j] =
1337                    (M4OSA_UInt8)VideoEditor3gpReader_BitStreamParserShowBits(
1338                    pBitParserContext, 8);
1339                VideoEditor3gpReader_BitStreamParserFlushBits(
1340                    pBitParserContext, 8);
1341            }
1342        }
1343        VideoEditor3gpReader_BitStreamParserCleanUp(pBitParserContext);
1344        pStreamHandler->m_decoderSpecificInfoSize = uiSpecInfoSize;
1345        pStreamHandler->m_pDecoderSpecificInfo = (M4OSA_UInt8*)pAvcSpecInfo;
1346    }
1347    pStreamHandler->m_H264decoderSpecificInfoSize  =  decoderConfigSizeLocal;
1348    pStreamHandler->m_pH264DecoderSpecificInfo  = (M4OSA_UInt8*)M4OSA_malloc(
1349        decoderConfigSizeLocal, M4READER_3GP,
1350        (M4OSA_Char*)"MPEG-4 AVC DecoderSpecific");
1351    if (M4OSA_NULL == pStreamHandler->m_pH264DecoderSpecificInfo) {
1352        goto cleanup;
1353    }
1354
1355    M4OSA_memcpy((M4OSA_MemAddr8 ) pStreamHandler->m_pH264DecoderSpecificInfo,
1356        (M4OSA_MemAddr8 )pDecoderConfigLocal,
1357        pStreamHandler->m_H264decoderSpecificInfoSize);
1358    return M4NO_ERROR;
1359cleanup:
1360    VideoEditor3gpReader_BitStreamParserCleanUp(pBitParserContext);
1361    return M4ERR_READER3GP_DECODER_CONFIG_ERROR;
1362}
1363/**
1364********************************************************************************
1365* @brief    Get the next stream found in the 3gp file
1366* @note
1367* @param    context:     (IN)    Context of the reader
1368* @param    pMediaFamily: OUT)   pointer to a user allocated
1369*                                M4READER_MediaFamily that will be filled
1370*                                with the media family of the found stream
1371* @param    pStreamHandler:(OUT) pointer to StreamHandler that will be allocated
1372*                                and filled with the found stream description
1373* @return   M4NO_ERROR              there is no error
1374* @return   M4ERR_BAD_CONTEXT       provided context is not a valid one
1375* @return   M4ERR_PARAMETER         at least one parameter is not properly set
1376* @return   M4WAR_NO_MORE_STREAM    no more available stream in the media
1377********************************************************************************
1378*/
1379M4OSA_ERR VideoEditor3gpReader_getNextStreamHandler(M4OSA_Context context,
1380        M4READER_MediaFamily *pMediaFamily,
1381        M4_StreamHandler **pStreamHandler) {
1382    VideoEditor3gpReader_Context* pC=(VideoEditor3gpReader_Context*)context;
1383    M4OSA_ERR err = M4NO_ERROR;
1384    M4SYS_StreamID streamIdArray[2];
1385    M4SYS_StreamDescription streamDesc;
1386    M4_AudioStreamHandler* pAudioStreamHandler;
1387    M4_VideoStreamHandler* pVideoStreamHandler;
1388    M4OSA_Int8 *DecoderSpecificInfo = M4OSA_NULL;
1389    M4OSA_Int32 decoderSpecificInfoSize =0, maxAUSize = 0;
1390
1391    M4_StreamType streamType = M4DA_StreamTypeUnknown;
1392    M4OSA_UInt8 temp, i, trackCount;
1393    M4OSA_Bool haveAudio = M4OSA_FALSE;
1394    M4OSA_Bool haveVideo = M4OSA_FALSE;
1395    sp<MetaData> meta  = NULL;
1396    int64_t Duration = 0;
1397    M4OSA_UInt8* DecoderSpecific = M4OSA_NULL ;
1398    uint32_t type;
1399    const void *data;
1400    size_t size;
1401    const void *codec_specific_data;
1402    size_t codec_specific_data_size;
1403    M4OSA_Int32  ptempTime;
1404
1405    LOGV("VideoEditor3gpReader_getNextStreamHandler begin");
1406
1407    M4OSA_DEBUG_IF1((pC == 0), M4ERR_PARAMETER,
1408        "VideoEditor3gpReader_getNextStreamHandler: invalid context");
1409    M4OSA_DEBUG_IF1((pMediaFamily   == 0), M4ERR_PARAMETER,
1410        "getNextStreamHandler: invalid pointer to MediaFamily");
1411    M4OSA_DEBUG_IF1((pStreamHandler == 0), M4ERR_PARAMETER,
1412        "getNextStreamHandler: invalid pointer to StreamHandler");
1413
1414    trackCount = pC->mExtractor->countTracks();
1415    temp = pC->mCurrTrack;
1416
1417    if(temp >= trackCount) {
1418        LOGV("VideoEditor3gpReader_getNextStreamHandler error = %d",
1419            M4WAR_NO_MORE_STREAM);
1420        return (M4WAR_NO_MORE_STREAM);
1421    } else {
1422        const char *mime;
1423        meta = pC->mExtractor->getTrackMetaData(temp);
1424        CHECK(meta->findCString(kKeyMIMEType, &mime));
1425
1426        if (!haveVideo && !strncasecmp(mime, "video/", 6)) {
1427            pC->mVideoSource = pC->mExtractor->getTrack(temp);
1428            pC->mVideoSource->start();
1429
1430            *pMediaFamily = M4READER_kMediaFamilyVideo;
1431            haveVideo = true;
1432            LOGV("VideoEditor3gpReader_getNextStreamHandler getTrack called");
1433            if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_AVC)) {
1434                streamType = M4DA_StreamTypeVideoMpeg4Avc;
1435            } else if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_H263)) {
1436                streamType = M4DA_StreamTypeVideoH263;
1437            } else if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_MPEG4)) {
1438                streamType = M4DA_StreamTypeVideoMpeg4;
1439            } else {
1440                LOGV("VideoEditor3gpReaderGetNextStreamHandler streamTypeNONE");
1441            }
1442            LOGV("VideoEditor3gpReader_getNextStreamHandler: stream type: %d ",
1443                streamType);
1444
1445            if(streamType != M4DA_StreamTypeUnknown) {
1446                pC->mStreamType = streamType;
1447                pC->mStreamId = pC->mCurrTrack;
1448
1449                pVideoStreamHandler = (M4_VideoStreamHandler*)M4OSA_malloc
1450                    (sizeof(M4_VideoStreamHandler), M4READER_3GP,
1451                    (M4OSA_Char*)"M4_VideoStreamHandler");
1452                if (M4OSA_NULL == pVideoStreamHandler) {
1453                    return M4ERR_ALLOC;
1454                }
1455                pVideoStreamHandler->m_structSize=sizeof(M4_VideoStreamHandler);
1456
1457                meta->findInt32(kKeyWidth,
1458                    (int32_t*)&(pVideoStreamHandler->m_videoWidth));
1459                meta->findInt32(kKeyHeight,
1460                    (int32_t*)&(pVideoStreamHandler->m_videoHeight));
1461
1462                (*pStreamHandler)  = (M4_StreamHandler*)(pVideoStreamHandler);
1463                meta->findInt64(kKeyDuration,
1464                    (int64_t*)&(Duration));
1465                ((*pStreamHandler)->m_duration) =
1466                    (int32_t)((Duration)/1000); // conversion to mS
1467                pC->mMaxDuration = ((*pStreamHandler)->m_duration);
1468                LOGV("VideoEditor3gpReader_getNextStreamHandler m_duration %d",
1469                    (*pStreamHandler)->m_duration);
1470
1471                pC->mFileSize  = 0;
1472
1473                meta->findInt32(kKeyMaxInputSize, (int32_t*)&(maxAUSize));
1474                if(maxAUSize == 0) {
1475                    maxAUSize = 70000;
1476                }
1477                (*pStreamHandler)->m_maxAUSize = maxAUSize;
1478                LOGV("<<<<<<<<<<   video: mMaxAUSize from MP4 extractor: %d",
1479                    (*pStreamHandler)->m_maxAUSize);
1480
1481                //check this
1482                pVideoStreamHandler->m_averageFrameRate = 15;
1483                if( (M4DA_StreamTypeVideoH263       == streamType) ||
1484                    (M4DA_StreamTypeVideoMpeg4Avc   == streamType)){
1485                    ((M4_StreamHandler*)pVideoStreamHandler)->m_averageBitRate =
1486                        384000;
1487                }
1488                pC->mVideoStreamHandler =
1489                    (M4_StreamHandler*)(pVideoStreamHandler);
1490
1491                /* Get the DSI info */
1492                if(M4DA_StreamTypeVideoH263 == streamType) {
1493                    if (meta->findData(kKeyESDS, &type, &data, &size)) {
1494                        ESDS esds((const char *)data, size);
1495                        CHECK_EQ(esds.InitCheck(), OK);
1496
1497                        esds.getCodecSpecificInfo(
1498                            &codec_specific_data, &codec_specific_data_size);
1499                        (*pStreamHandler)->m_decoderSpecificInfoSize =
1500                            codec_specific_data_size;
1501                        if ((*pStreamHandler)->m_decoderSpecificInfoSize != 0) {
1502                            DecoderSpecific = (M4OSA_UInt8*)M4OSA_malloc(
1503                                (*pStreamHandler)->m_decoderSpecificInfoSize,
1504                                M4READER_3GP,(M4OSA_Char*)"H263 DSI");
1505                            if (M4OSA_NULL == DecoderSpecific) {
1506                                return M4ERR_ALLOC;
1507                            }
1508                            M4OSA_memcpy((M4OSA_MemAddr8)DecoderSpecific,
1509                                (M4OSA_MemAddr8)codec_specific_data,
1510                                codec_specific_data_size);
1511                            (*pStreamHandler)->m_pDecoderSpecificInfo =
1512                                DecoderSpecific;
1513                        }
1514                        else {
1515                            (*pStreamHandler)->m_pDecoderSpecificInfo =
1516                                M4OSA_NULL;
1517                        }
1518                    } else {
1519                        LOGV("VE_getNextStreamHandler: H263 dsi not found");
1520                        (*pStreamHandler)->m_pDecoderSpecificInfo = M4OSA_NULL;
1521                        (*pStreamHandler)->m_decoderSpecificInfoSize = 0;
1522                        (*pStreamHandler)->m_H264decoderSpecificInfoSize = 0;
1523                        (*pStreamHandler)->m_pH264DecoderSpecificInfo =
1524                            M4OSA_NULL;
1525                        (*pStreamHandler)->m_pESDSInfo = M4OSA_NULL;
1526                        (*pStreamHandler)->m_ESDSInfoSize = 0;
1527                    }
1528                }
1529                else if(M4DA_StreamTypeVideoMpeg4Avc == streamType) {
1530                    if(meta->findData(kKeyAVCC, &type, &data, &size)) {
1531                        decoderSpecificInfoSize = size;
1532                        if (decoderSpecificInfoSize != 0) {
1533                            DecoderSpecificInfo = (M4OSA_Int8*)M4OSA_malloc(
1534                                decoderSpecificInfoSize, M4READER_3GP,
1535                                (M4OSA_Char*)"H264 DecoderSpecific" );
1536                            if (M4OSA_NULL == DecoderSpecificInfo) {
1537                                LOGV("VideoEditor3gp_getNextStream is NULL ");
1538                                return M4ERR_ALLOC;
1539                            }
1540                            M4OSA_memcpy((M4OSA_MemAddr8)DecoderSpecificInfo,
1541                                (M4OSA_MemAddr8)data, decoderSpecificInfoSize);
1542                        } else {
1543                            LOGV("DSI Size %d", decoderSpecificInfoSize);
1544                            DecoderSpecificInfo = M4OSA_NULL;
1545                        }
1546                    }
1547                    (*pStreamHandler)->m_pESDSInfo = M4OSA_NULL;
1548                    (*pStreamHandler)->m_ESDSInfoSize = 0;
1549
1550                    err = VideoEditor3gpReader_AnalyseAvcDsi(*pStreamHandler,
1551                    (M4OSA_Int32*)DecoderSpecificInfo, decoderSpecificInfoSize);
1552
1553                    if (M4NO_ERROR != err) {
1554                        return err;
1555                    }
1556                    LOGV("decsize %d, h264decsize %d: %d", (*pStreamHandler)->\
1557                        m_decoderSpecificInfoSize, (*pStreamHandler)->\
1558                        m_H264decoderSpecificInfoSize);
1559
1560                    if(M4OSA_NULL != DecoderSpecificInfo) {
1561                        M4OSA_free((M4OSA_MemAddr32)DecoderSpecificInfo);
1562                        DecoderSpecificInfo = M4OSA_NULL;
1563                    }
1564                } else if( (M4DA_StreamTypeVideoMpeg4 == streamType) ) {
1565                    if (meta->findData(kKeyESDS, &type, &data, &size)) {
1566                        ESDS esds((const char *)data, size);
1567                        CHECK_EQ(esds.InitCheck(), OK);
1568
1569                        (*pStreamHandler)->m_ESDSInfoSize = size;
1570                        (*pStreamHandler)->m_pESDSInfo = (M4OSA_UInt8*)\
1571                        M4OSA_malloc((*pStreamHandler)->m_ESDSInfoSize,
1572                        M4READER_3GP, (M4OSA_Char*)"H263 DecoderSpecific" );
1573                        if (M4OSA_NULL == (*pStreamHandler)->m_pESDSInfo) {
1574                            return M4ERR_ALLOC;
1575                        }
1576                        M4OSA_memcpy((M4OSA_MemAddr8)(*pStreamHandler)->\
1577                            m_pESDSInfo, (M4OSA_MemAddr8)data, size);
1578
1579                        esds.getCodecSpecificInfo(&codec_specific_data,
1580                            &codec_specific_data_size);
1581                        LOGV("VE MP4 dsisize: %d, %x", codec_specific_data_size,
1582                            codec_specific_data);
1583
1584                        (*pStreamHandler)->m_decoderSpecificInfoSize =
1585                            codec_specific_data_size;
1586                        if ((*pStreamHandler)->m_decoderSpecificInfoSize != 0) {
1587                            DecoderSpecific = (M4OSA_UInt8*)M4OSA_malloc(
1588                                (*pStreamHandler)->m_decoderSpecificInfoSize,
1589                                M4READER_3GP, (M4OSA_Char*)" DecoderSpecific" );
1590                            if (M4OSA_NULL == DecoderSpecific) {
1591                                return M4ERR_ALLOC;
1592                            }
1593                            M4OSA_memcpy((M4OSA_MemAddr8)DecoderSpecific,
1594                                (M4OSA_MemAddr8)codec_specific_data,
1595                                codec_specific_data_size);
1596                            (*pStreamHandler)->m_pDecoderSpecificInfo =
1597                                DecoderSpecific;
1598                        }
1599                        else {
1600                            (*pStreamHandler)->m_pDecoderSpecificInfo =
1601                                M4OSA_NULL;
1602                        }
1603                        (*pStreamHandler)->m_pH264DecoderSpecificInfo =
1604                            M4OSA_NULL;
1605                        (*pStreamHandler)->m_H264decoderSpecificInfoSize = 0;
1606                    }
1607                } else {
1608                    LOGV("VideoEditor3gpReader_getNextStream NO video stream");
1609                    return M4ERR_READER_UNKNOWN_STREAM_TYPE;
1610                }
1611            }
1612            else {
1613                LOGV("VideoEditor3gpReader_getNextStream NO video stream");
1614                return M4ERR_READER_UNKNOWN_STREAM_TYPE;
1615            }
1616
1617        } else if (!haveAudio && !strncasecmp(mime, "audio/", 6)) {
1618            LOGV("VideoEditor3gpReader_getNextStream audio getTrack called");
1619            pC->mAudioSource = pC->mExtractor->getTrack(pC->mCurrTrack);
1620            pC->mAudioSource->start();
1621            *pMediaFamily = M4READER_kMediaFamilyAudio;
1622
1623            if(!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_NB)) {
1624                streamType = M4DA_StreamTypeAudioAmrNarrowBand;
1625            } else if(!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_WB)) {
1626                streamType = M4DA_StreamTypeAudioAmrWideBand;
1627            }
1628            else if(!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC)) {
1629                streamType = M4DA_StreamTypeAudioAac;
1630            } else {
1631                LOGV("VideoEditor3gpReader_getNextStrea streamtype Unknown ");
1632            }
1633            if(streamType != M4DA_StreamTypeUnknown) {
1634                pC->mStreamType = streamType;
1635                pC->mStreamId = pC->mCurrTrack;
1636
1637                LOGV("VE streamtype %d ,id %d",  streamType, pC->mCurrTrack);
1638
1639                pAudioStreamHandler = (M4_AudioStreamHandler*)M4OSA_malloc
1640                    (sizeof(M4_AudioStreamHandler), M4READER_3GP,
1641                    (M4OSA_Char*)"M4_AudioStreamHandler");
1642                if (M4OSA_NULL == pAudioStreamHandler) {
1643                    return M4ERR_ALLOC;
1644                }
1645                pAudioStreamHandler->m_structSize=sizeof(M4_AudioStreamHandler);
1646                pAudioStreamHandler->m_byteSampleSize   = 0;
1647                pAudioStreamHandler->m_nbChannels       = 0;
1648                pAudioStreamHandler->m_samplingFrequency= 0;
1649                pAudioStreamHandler->m_byteFrameLength  = 0;
1650
1651                (*pStreamHandler) = (M4_StreamHandler*)(pAudioStreamHandler);
1652                pC->mAudioStreamHandler =
1653                    (M4_StreamHandler*)(pAudioStreamHandler);
1654                (*pStreamHandler)->m_averageBitRate = 0;
1655                haveAudio = true;
1656                pC->mAudioStreamHandler=(M4_StreamHandler*)pAudioStreamHandler;
1657                pC->mAudioStreamHandler->m_pESDSInfo = M4OSA_NULL;
1658                pC->mAudioStreamHandler->m_ESDSInfoSize = 0;
1659
1660                meta->findInt32(kKeyMaxInputSize, (int32_t*)&(maxAUSize));
1661                if(maxAUSize == 0) {
1662                    maxAUSize = 70000;
1663                }
1664                (*pStreamHandler)->m_maxAUSize = maxAUSize;
1665                LOGV("VE Audio mMaxAUSize from MP4 extractor: %d", maxAUSize);
1666            }
1667            if((M4DA_StreamTypeAudioAmrNarrowBand == streamType) ||
1668                (M4DA_StreamTypeAudioAmrWideBand == streamType)) {
1669                M4OSA_UInt32 freqIndex = 0; /**< AMR NB */
1670                M4OSA_UInt32 modeSet;
1671                M4OSA_UInt32 i;
1672                M4OSA_Context pBitParserContext = M4OSA_NULL;
1673
1674                if(M4DA_StreamTypeAudioAmrWideBand == streamType) {
1675                    freqIndex = 1; /**< AMR WB */
1676                }
1677
1678                if (meta->findData(kKeyESDS, &type, &data, &size)) {
1679                    ESDS esds((const char *)data, size);
1680                    CHECK_EQ(esds.InitCheck(), OK);
1681
1682                    esds.getCodecSpecificInfo(&codec_specific_data,
1683                        &codec_specific_data_size);
1684                    (*pStreamHandler)->m_decoderSpecificInfoSize =
1685                        codec_specific_data_size;
1686
1687                    if ((*pStreamHandler)->m_decoderSpecificInfoSize != 0) {
1688                        DecoderSpecific = (M4OSA_UInt8*)M4OSA_malloc(
1689                            (*pStreamHandler)->m_decoderSpecificInfoSize,
1690                            M4READER_3GP, (M4OSA_Char*)"H263 DecoderSpecific" );
1691                        if (M4OSA_NULL == DecoderSpecific) {
1692                            return M4ERR_ALLOC;
1693                        }
1694                        M4OSA_memcpy((M4OSA_MemAddr8)DecoderSpecific,
1695                            (M4OSA_MemAddr8)codec_specific_data,
1696                            codec_specific_data_size);
1697                        (*pStreamHandler)->m_pDecoderSpecificInfo =
1698                            DecoderSpecific;
1699                    } else {
1700                        (*pStreamHandler)->m_pDecoderSpecificInfo = M4OSA_NULL;
1701                    }
1702                } else {
1703                    M4OSA_UChar AmrDsi[] =
1704                        {'P','H','L','P',0x00, 0x00, 0x80, 0x00, 0x01,};
1705                    (*pStreamHandler)->m_decoderSpecificInfoSize = 9;
1706                    DecoderSpecific = (M4OSA_UInt8*)M4OSA_malloc(
1707                        (*pStreamHandler)->m_decoderSpecificInfoSize,
1708                        M4READER_3GP, (M4OSA_Char*)"H263 DecoderSpecific" );
1709                    if (M4OSA_NULL == DecoderSpecific) {
1710                        return M4ERR_ALLOC;
1711                    }
1712                    if(freqIndex ==0) {
1713                        AmrDsi[8] = 0x01;
1714                    } else {
1715                        AmrDsi[8] = 0x02;
1716                    }
1717                    for(i = 0; i< 9; i++) {
1718                        DecoderSpecific[i] = AmrDsi[i];
1719                    }
1720                    (*pStreamHandler)->m_pDecoderSpecificInfo = DecoderSpecific;
1721                }
1722                (*pStreamHandler)->m_averageBitRate =
1723                    VideoEditor3gpReader_AmrBitRate[freqIndex][7];
1724            } else if((M4DA_StreamTypeAudioAac == streamType)) {
1725                if (meta->findData(kKeyESDS, &type, &data, &size)) {
1726                    ESDS esds((const char *)data, size);
1727                    CHECK_EQ(esds.InitCheck(), OK);
1728
1729                    (*pStreamHandler)->m_ESDSInfoSize = size;
1730                    (*pStreamHandler)->m_pESDSInfo = (M4OSA_UInt8*)M4OSA_malloc(
1731                        (*pStreamHandler)->m_ESDSInfoSize, M4READER_3GP,
1732                        (M4OSA_Char*)"H263 DecoderSpecific" );
1733                    if (M4OSA_NULL == (*pStreamHandler)->m_pESDSInfo) {
1734                        return M4ERR_ALLOC;
1735                    }
1736                    M4OSA_memcpy((M4OSA_MemAddr8)(*pStreamHandler)->m_pESDSInfo,
1737                    (M4OSA_MemAddr8)data, size);
1738                    esds.getCodecSpecificInfo(&codec_specific_data,
1739                        &codec_specific_data_size);
1740
1741                    LOGV("VEdsi %d,%x",codec_specific_data_size,
1742                        codec_specific_data);
1743
1744                    (*pStreamHandler)->m_decoderSpecificInfoSize =
1745                        codec_specific_data_size;
1746                    if ((*pStreamHandler)->m_decoderSpecificInfoSize != 0) {
1747                        DecoderSpecific = (M4OSA_UInt8*)M4OSA_malloc(
1748                            (*pStreamHandler)->m_decoderSpecificInfoSize,
1749                            M4READER_3GP, (M4OSA_Char*)"H263 DecoderSpecific" );
1750                        if (M4OSA_NULL == DecoderSpecific) {
1751                            return M4ERR_ALLOC;
1752                        }
1753                        M4OSA_memcpy((M4OSA_MemAddr8)DecoderSpecific,
1754                            (M4OSA_MemAddr8)codec_specific_data,
1755                            codec_specific_data_size);
1756                        (*pStreamHandler)->m_pDecoderSpecificInfo =
1757                            DecoderSpecific;
1758                    } else {
1759                        (*pStreamHandler)->m_pDecoderSpecificInfo = M4OSA_NULL;
1760                    }
1761                }
1762            } else {
1763                LOGV("VideoEditor3gpReader_getNextStream mStreamType: none ");
1764                return M4ERR_READER_UNKNOWN_STREAM_TYPE;
1765            }
1766        } else {
1767            LOGV("VE noaudio-video stream:pC->mCurrTrack = %d ",pC->mCurrTrack);
1768            pC->mCurrTrack++; //Increment current track to get the next track
1769            return M4ERR_READER_UNKNOWN_STREAM_TYPE;
1770        }
1771        LOGV("VE StreamType: %d, stremhandler %x",streamType, *pStreamHandler );
1772        (*pStreamHandler)->m_streamType = streamType;
1773        (*pStreamHandler)->m_streamId   = pC->mStreamId;
1774        (*pStreamHandler)->m_pUserData  = M4OSA_NULL;
1775        (*pStreamHandler)->m_structSize = sizeof(M4_StreamHandler);
1776        (*pStreamHandler)->m_bStreamIsOK = M4OSA_TRUE;
1777
1778        meta->findInt64(kKeyDuration,
1779            (int64_t*)&(Duration));
1780
1781        (*pStreamHandler)->m_duration = (int32_t)(Duration / 1000);
1782
1783        pC->mMaxDuration = ((*pStreamHandler)->m_duration);
1784        LOGV("VE str duration duration: %d ", (*pStreamHandler)->m_duration);
1785
1786        /* In AAC case: Put the first AU in pAudioStreamHandler->m_pUserData
1787         *since decoder has to know if stream contains SBR data(Implicit sig) */
1788        if(M4DA_StreamTypeAudioAac == (*pStreamHandler)->m_streamType) {
1789            M4READER_AudioSbrUserdata*  pAudioSbrUserdata;
1790
1791            pAudioSbrUserdata = (M4READER_AudioSbrUserdata*)M4OSA_malloc(
1792                sizeof(M4READER_AudioSbrUserdata),M4READER_3GP,
1793                (M4OSA_Char*)"M4READER_AudioSbrUserdata");
1794            if (M4OSA_NULL == pAudioSbrUserdata) {
1795                err = M4ERR_ALLOC;
1796                goto Error;
1797            }
1798            (*pStreamHandler)->m_pUserData = pAudioSbrUserdata;
1799            pAudioSbrUserdata->m_bIsSbrEnabled = M4OSA_FALSE;
1800
1801            pAudioSbrUserdata->m_pFirstAU = (M4_AccessUnit*)M4OSA_malloc(
1802                sizeof(M4_AccessUnit),M4READER_3GP, (M4OSA_Char*)"1st AAC AU");
1803            if (M4OSA_NULL == pAudioSbrUserdata->m_pFirstAU) {
1804                pAudioSbrUserdata->m_pAacDecoderUserConfig = M4OSA_NULL;
1805                err = M4ERR_ALLOC;
1806                goto Error;
1807            }
1808            pAudioSbrUserdata->m_pAacDecoderUserConfig = (M4_AacDecoderConfig*)\
1809                M4OSA_malloc(sizeof(M4_AacDecoderConfig),M4READER_3GP,
1810                (M4OSA_Char*)"m_pAacDecoderUserConfig");
1811            if (M4OSA_NULL == pAudioSbrUserdata->m_pAacDecoderUserConfig) {
1812                err = M4ERR_ALLOC;
1813                goto Error;
1814            }
1815        }
1816        if(M4DA_StreamTypeAudioAac == (*pStreamHandler)->m_streamType) {
1817            M4_AudioStreamHandler* pAudioStreamHandler =
1818                (M4_AudioStreamHandler*)(*pStreamHandler);
1819            M4READER_AudioSbrUserdata* pUserData = (M4READER_AudioSbrUserdata*)\
1820                (pAudioStreamHandler->m_basicProperties.m_pUserData);
1821
1822            err = VideoEditor3gpReader_fillAuStruct(pC, (*pStreamHandler),
1823                (M4_AccessUnit*)pUserData->m_pFirstAU);
1824            if (M4NO_ERROR != err) {
1825                goto Error;
1826            }
1827            err = VideoEditor3gpReader_getNextAu(pC, (*pStreamHandler),
1828                (M4_AccessUnit*)pUserData->m_pFirstAU);
1829            if (M4NO_ERROR != err) {
1830                goto Error;
1831            }
1832            err = VideoEditor3gpReader_reset(pC, (*pStreamHandler));
1833            if (M4NO_ERROR != err) {
1834                goto Error;
1835            }
1836        }
1837    }
1838    pC->mCurrTrack++; //Increment the current track to get next track
1839    LOGV("pC->mCurrTrack = %d",pC->mCurrTrack);
1840
1841    if (!haveAudio && !haveVideo) {
1842        *pMediaFamily=M4READER_kMediaFamilyUnknown;
1843        return M4ERR_READER_UNKNOWN_STREAM_TYPE;
1844    }
1845Error:
1846    LOGV("VideoEditor3gpReader_getNextStreamHandler end error = %d",err);
1847    return err;
1848}
1849
1850M4OSA_ERR VideoEditor3gpReader_getPrevRapTime(M4OSA_Context context,
1851    M4_StreamHandler *pStreamHandler, M4OSA_Int32* pTime)
1852{
1853    VideoEditor3gpReader_Context *pC = (VideoEditor3gpReader_Context*)context;
1854    M4OSA_ERR err = M4NO_ERROR;
1855    MediaBuffer *mMediaBuffer = M4OSA_NULL;
1856    MediaSource::ReadOptions options;
1857    M4OSA_Time time64;
1858    int64_t tempTime64 = 0;
1859    status_t error;
1860
1861    LOGV("VideoEditor3gpReader_getPrevRapTime begin");
1862
1863    M4OSA_DEBUG_IF1((pC == 0), M4ERR_PARAMETER,
1864        "VideoEditor3gpReader_getPrevRapTime: invalid context");
1865    M4OSA_DEBUG_IF1((pStreamHandler == 0), M4ERR_PARAMETER,
1866        "VideoEditor3gpReader_getPrevRapTime invalid pointer to StreamHandler");
1867    M4OSA_DEBUG_IF1((pTime == 0), M4ERR_PARAMETER,
1868        "VideoEditor3gpReader_getPrevRapTime: invalid time pointer");
1869    if (*pTime == (pStreamHandler->m_duration)) {
1870		*pTime -= 1;
1871	}
1872    M4OSA_INT64_FROM_INT32(time64, *pTime);
1873    time64 = time64 * 1000;
1874
1875    LOGV("VideoEditor3gpReader_getPrevRapTime seek time: %ld",time64);
1876    options.setSeekTo(time64, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
1877    error = pC->mVideoSource->read(&mMediaBuffer, &options);
1878    if (error != OK) {
1879        //Can not get the previous Sync.
1880        //Must be end of stream.
1881		return M4WAR_NO_MORE_AU;
1882    }
1883
1884    mMediaBuffer->meta_data()->findInt64(kKeyTime, (int64_t*)&tempTime64);
1885    LOGV("VideoEditor3gpReader_getPrevRapTime read time %ld, %x", tempTime64,
1886        mMediaBuffer);
1887
1888    (*pTime) =  (tempTime64) / 1000;
1889
1890    if(mMediaBuffer != M4OSA_NULL) {
1891        LOGV(" mMediaBuffer size = %d length %d", mMediaBuffer->size(),
1892            mMediaBuffer->range_length());
1893        mMediaBuffer->release();
1894        mMediaBuffer = M4OSA_NULL;
1895    }
1896    options.clearSeekTo();
1897
1898    if(error != OK) {
1899        LOGV("VideoEditor3gpReader_getPrevRapTime end \
1900            M4WAR_READER_INFORMATION_NOT_PRESENT");
1901        return M4WAR_READER_INFORMATION_NOT_PRESENT;
1902    } else {
1903        LOGV("VideoEditor3gpReader_getPrevRapTime end: err %x", err);
1904        err = M4NO_ERROR;
1905        return err;
1906    }
1907}
1908
1909extern "C" {
1910M4OSA_ERR VideoEditor3gpReader_getInterface(M4READER_MediaType *pMediaType,
1911        M4READER_GlobalInterface **pRdrGlobalInterface,
1912        M4READER_DataInterface **pRdrDataInterface) {
1913
1914    M4OSA_ERR err = M4NO_ERROR;
1915
1916    VIDEOEDITOR_CHECK(M4OSA_NULL != pMediaType,      M4ERR_PARAMETER);
1917    VIDEOEDITOR_CHECK(M4OSA_NULL != pRdrGlobalInterface, M4ERR_PARAMETER);
1918    VIDEOEDITOR_CHECK(M4OSA_NULL != pRdrDataInterface, M4ERR_PARAMETER);
1919
1920    LOGV("VideoEditor3gpReader_getInterface begin");
1921    LOGV("VideoEditor3gpReader_getInterface %d 0x%x 0x%x", *pMediaType,
1922        *pRdrGlobalInterface,*pRdrDataInterface);
1923
1924    SAFE_MALLOC(*pRdrGlobalInterface, M4READER_GlobalInterface, 1,
1925        "VideoEditor3gpReader_getInterface");
1926    SAFE_MALLOC(*pRdrDataInterface, M4READER_DataInterface, 1,
1927        "VideoEditor3gpReader_getInterface");
1928
1929    *pMediaType = M4READER_kMediaType3GPP;
1930
1931    (*pRdrGlobalInterface)->m_pFctCreate       = VideoEditor3gpReader_create;
1932    (*pRdrGlobalInterface)->m_pFctDestroy      = VideoEditor3gpReader_destroy;
1933    (*pRdrGlobalInterface)->m_pFctOpen         = VideoEditor3gpReader_open;
1934    (*pRdrGlobalInterface)->m_pFctClose        = VideoEditor3gpReader_close;
1935    (*pRdrGlobalInterface)->m_pFctGetOption    = VideoEditor3gpReader_getOption;
1936    (*pRdrGlobalInterface)->m_pFctSetOption    = VideoEditor3gpReader_setOption;
1937    (*pRdrGlobalInterface)->m_pFctGetNextStream =
1938        VideoEditor3gpReader_getNextStreamHandler;
1939    (*pRdrGlobalInterface)->m_pFctFillAuStruct =
1940        VideoEditor3gpReader_fillAuStruct;
1941    (*pRdrGlobalInterface)->m_pFctStart        = M4OSA_NULL;
1942    (*pRdrGlobalInterface)->m_pFctStop         = M4OSA_NULL;
1943    (*pRdrGlobalInterface)->m_pFctJump         = VideoEditor3gpReader_jump;
1944    (*pRdrGlobalInterface)->m_pFctReset        = VideoEditor3gpReader_reset;
1945    (*pRdrGlobalInterface)->m_pFctGetPrevRapTime =
1946        VideoEditor3gpReader_getPrevRapTime;
1947    (*pRdrDataInterface)->m_pFctGetNextAu      = VideoEditor3gpReader_getNextAu;
1948    (*pRdrDataInterface)->m_readerContext      = M4OSA_NULL;
1949
1950cleanUp:
1951    if( M4NO_ERROR == err ) {
1952        LOGV("VideoEditor3gpReader_getInterface no error");
1953    } else {
1954        SAFE_FREE(*pRdrGlobalInterface);
1955        SAFE_FREE(*pRdrDataInterface);
1956
1957        LOGV("VideoEditor3gpReader_getInterface ERROR 0x%X", err);
1958    }
1959    LOGV("VideoEditor3gpReader_getInterface end");
1960    return err;
1961}
1962
1963}  /* extern "C" */
1964
1965}  /* namespace android */
1966
1967
1968