VideoEditor3gpReader.cpp revision b864ee128119ad995862355776599e553691e722
1e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood/* 2e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood * Copyright (C) 2011 NXP Software 3e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood * Copyright (C) 2011 The Android Open Source Project 4e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood * 5e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood * Licensed under the Apache License, Version 2.0 (the "License"); 6e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood * you may not use this file except in compliance with the License. 7e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood * You may obtain a copy of the License at 8e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood * 9e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood * http://www.apache.org/licenses/LICENSE-2.0 10e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood * 11e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood * Unless required by applicable law or agreed to in writing, software 12e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood * distributed under the License is distributed on an "AS IS" BASIS, 13e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood * See the License for the specific language governing permissions and 15e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood * limitations under the License. 16e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood */ 1700b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood/** 18e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood************************************************************************* 196fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly* @file VideoEditor3gpReader.cpp 206fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly* @brief StageFright shell 3GP Reader 216fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly************************************************************************* 226fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly*/ 23628fd6d9c11ed9806abebf34bc986247c106328fMike Lockwood 246fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly#define LOG_NDEBUG 1 25e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood#define LOG_TAG "VIDEOEDITOR_3GPREADER" 26628fd6d9c11ed9806abebf34bc986247c106328fMike Lockwood 27e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood/** 28b711d57ca4e2c6a1befbfa1a41f4b8094755a93fVictoria Lease * HEADERS 297e9f4eb2608148436cef36c9969bf8a599b39e72Dianne Hackborn * 30e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood */ 31e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood#define VIDEOEDITOR_BITSTREAM_PARSER 326fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 336fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly#include "M4OSA_Debug.h" 346fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly#include "VideoEditor3gpReader.h" 356fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly#include "M4SYS_AccessUnit.h" 366fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly#include "VideoEditorUtils.h" 3700b74270c9f136a8727c5f6cda0997a3a905f385Mike Lockwood#include "M4READER_3gpCom.h" 38e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood#include "M4_Common.h" 396fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly#include "M4OSA_FileWriter.h" 40e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood 41d03ff94fe58045b5bd1dd783fb98026778a4265dMike Lockwood#ifdef VIDEOEDITOR_BITSTREAM_PARSER 42e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood#include "M4OSA_CoreID.h" 436fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly#include "M4OSA_Error.h" 4400355d5a592533a3ecb0a5a74aef8e69dd16902aNick Pelly#include "M4OSA_Memory.h" 45628fd6d9c11ed9806abebf34bc986247c106328fMike Lockwood#include "M4_Utils.h" 4615e3d0f082d551f8819fbe4b0d502cc108627876Mike Lockwood#endif 476fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 486fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly#include "ESDS.h" 496fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly#include "utils/Log.h" 50628fd6d9c11ed9806abebf34bc986247c106328fMike Lockwood#include <media/stagefright/MediaBufferGroup.h> 516fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly#include <media/stagefright/DataSource.h> 526fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly#include <media/stagefright/FileSource.h> 532cd543aad5cc38fb3e7158f1cc5080c7e9ff5d24Mike Lockwood#include <media/stagefright/MediaBuffer.h> 546fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly#include <media/stagefright/MediaDefs.h> 556fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly#include <media/stagefright/MediaExtractor.h> 566fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly#include <media/stagefright/MediaDebug.h> 576fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly#include <media/stagefright/MediaSource.h> 58b711d57ca4e2c6a1befbfa1a41f4b8094755a93fVictoria Lease#include <media/stagefright/MetaData.h> 596fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 60b711d57ca4e2c6a1befbfa1a41f4b8094755a93fVictoria Lease/** 616fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * SOURCE CLASS 626fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly */ 636fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellynamespace android { 646fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly/** 656fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * ENGINE INTERFACE 666fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly */ 676fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 686fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly/** 69b711d57ca4e2c6a1befbfa1a41f4b8094755a93fVictoria Lease ************************************************************************ 70628fd6d9c11ed9806abebf34bc986247c106328fMike Lockwood * @brief Array of AMR NB/WB bitrates 71628fd6d9c11ed9806abebf34bc986247c106328fMike Lockwood * @note Array to match the mode and the bit rate 726fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly ************************************************************************ 73b711d57ca4e2c6a1befbfa1a41f4b8094755a93fVictoria Lease*/ 74628fd6d9c11ed9806abebf34bc986247c106328fMike Lockwoodconst M4OSA_UInt32 VideoEditor3gpReader_AmrBitRate [2 /* 8kHz / 16kHz */] 75628fd6d9c11ed9806abebf34bc986247c106328fMike Lockwood [9 /* the bitrate mode */] = 766fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly{ 776fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly {4750, 5150, 5900, 6700, 7400, 7950, 10200, 12200, 0}, 78e97ae40dffe7d7f8917c51c4f3f6c1b18c238565Mike Lockwood {6600, 8850, 12650, 14250, 15850, 18250, 19850, 23050, 23850} 79e97ae40dffe7d7f8917c51c4f3f6c1b18c238565Mike Lockwood}; 806fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 816fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly/** 826fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly ******************************************************************************* 838863c43d9efda108c880814b0a18688b75d70153Mark Vandevoorde * structure VideoEditor3gpReader_Context 846fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * @brief:This structure defines the context of the StageFright 3GP shell Reader 856fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly ******************************************************************************* 866fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly*/ 87628fd6d9c11ed9806abebf34bc986247c106328fMike Lockwoodtypedef struct { 886fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly sp<DataSource> mDataSource; 896fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly sp<MediaExtractor> mExtractor; 906fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly sp<MediaSource> mAudioSource; 916fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly sp<MediaSource> mVideoSource; 926fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4_StreamHandler* mAudioStreamHandler; 936fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4_StreamHandler* mVideoStreamHandler; 946fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4SYS_AccessUnit mAudioAu; 956fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4SYS_AccessUnit mVideoAu; 966fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4OSA_Time mMaxDuration; 976fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly int32_t mFileSize; 986fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4_StreamType mStreamType; 996fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4OSA_UInt32 mStreamId; 1006fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly int32_t mTracks; 1016fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly int32_t mCurrTrack; 1026fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4OSA_Bool mAudioSeeking; 1036fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4OSA_Time mAudioSeekTime; 1046fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4OSA_Bool mVideoSeeking; 1056fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4OSA_Time mVideoSeekTime; 1066fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 1076fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly} VideoEditor3gpReader_Context; 108628fd6d9c11ed9806abebf34bc986247c106328fMike Lockwood 109fff2fda0199dedbf1079454dca98a81190dce765Suchi Amalapurapu#ifdef VIDEOEDITOR_BITSTREAM_PARSER 1106fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly/** 1118863c43d9efda108c880814b0a18688b75d70153Mark Vandevoorde ************************************************************************ 1126fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * structure VideoEditor3gpReader_BitStreamParserContext 1136fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * @brief Internal BitStreamParser context 1146fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly ************************************************************************ 1156fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly*/ 1166fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellytypedef struct { 1176fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4OSA_UInt32* mPbitStream; /**< bitstream pointer (32bits aligned) */ 118628fd6d9c11ed9806abebf34bc986247c106328fMike Lockwood M4OSA_Int32 mSize; /**< bitstream size in bytes */ 119628fd6d9c11ed9806abebf34bc986247c106328fMike Lockwood M4OSA_Int32 mIndex; /**< byte index */ 1206fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4OSA_Int32 mBitIndex; /**< bit index */ 1216fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4OSA_Int32 mStructSize; /**< size of structure */ 1226fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly} VideoEditor3gpReader_BitStreamParserContext; 1236fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 1246fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly/** 1258863c43d9efda108c880814b0a18688b75d70153Mark Vandevoorde ******************************************************************************* 126628fd6d9c11ed9806abebf34bc986247c106328fMike Lockwood * @brief Allocates the context and initializes internal data. 1276fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * @param pContext (OUT) Pointer to the BitStreamParser context to create. 1286fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * @param bitStream A pointer to the bitstream 1296fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * @param size The size of the bitstream in bytes 1306fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly ******************************************************************************* 1316fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly*/ 1326fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellystatic void VideoEditor3gpReader_BitStreamParserInit(void** pContext, 133628fd6d9c11ed9806abebf34bc986247c106328fMike Lockwood void* pBitStream, M4OSA_Int32 size) { 1346fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly VideoEditor3gpReader_BitStreamParserContext* pStreamContext; 1356fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 136628fd6d9c11ed9806abebf34bc986247c106328fMike Lockwood *pContext=M4OSA_NULL; 137628fd6d9c11ed9806abebf34bc986247c106328fMike Lockwood pStreamContext = (VideoEditor3gpReader_BitStreamParserContext*)M4OSA_malloc( 138628fd6d9c11ed9806abebf34bc986247c106328fMike Lockwood sizeof(VideoEditor3gpReader_BitStreamParserContext), M4READER_3GP, 139628fd6d9c11ed9806abebf34bc986247c106328fMike Lockwood (M4OSA_Char*)"3GP BitStreamParser Context"); 1406fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly if (M4OSA_NULL == pStreamContext) { 14115e3d0f082d551f8819fbe4b0d502cc108627876Mike Lockwood return; 14215e3d0f082d551f8819fbe4b0d502cc108627876Mike Lockwood } 14315e3d0f082d551f8819fbe4b0d502cc108627876Mike Lockwood pStreamContext->mPbitStream=(M4OSA_UInt32*)pBitStream; 14415e3d0f082d551f8819fbe4b0d502cc108627876Mike Lockwood pStreamContext->mSize=size; 1456fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly pStreamContext->mIndex=0; 1466fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly pStreamContext->mBitIndex=0; 1476fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly pStreamContext->mStructSize = 1486fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly sizeof(VideoEditor3gpReader_BitStreamParserContext); 149e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood 150e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood *pContext=pStreamContext; 151e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood} 1526fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly/** 153e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood ********************************************************************** 1546fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * @brief Clean up context 1558863c43d9efda108c880814b0a18688b75d70153Mark Vandevoorde * @param pContext (IN/OUT) BitStreamParser context. 156e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood ********************************************************************** 1576fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly*/ 1586fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellystatic void VideoEditor3gpReader_BitStreamParserCleanUp(void* pContext) { 159e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood M4OSA_free((M4OSA_Int32*)pContext); 1606fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly} 1616fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly/** 1626fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly ***************************************************************************** 1636fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * @brief Read the next <length> bits in the bitstream. 1646fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * @note The function does not update the bitstream pointer. 1656fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * @param pContext (IN/OUT) BitStreamParser context. 1666fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * @param length (IN) The number of bits to extract from the bitstream 167e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood * @return the read bits 168e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood ***************************************************************************** 169e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood*/ 1706fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellystatic M4OSA_UInt32 VideoEditor3gpReader_BitStreamParserShowBits(void* pContext, 1716fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4OSA_Int32 length) { 1726fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly VideoEditor3gpReader_BitStreamParserContext* pStreamContext = 1736fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly (VideoEditor3gpReader_BitStreamParserContext*)pContext; 1748863c43d9efda108c880814b0a18688b75d70153Mark Vandevoorde 1756fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4OSA_UInt32 u_mask; 1766fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4OSA_UInt32 retval; 177e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood M4OSA_Int32 i_ovf; 1786fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 1796fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4OSA_DEBUG_IF1((M4OSA_NULL==pStreamContext), 0, 1806fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly "VideoEditor3gpReader_BitStreamParserShowBits:invalid context pointer"); 1816fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 1826fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly retval=(M4OSA_UInt32)GET_MEMORY32(pStreamContext->\ 1836fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly mPbitStream[ pStreamContext->mIndex ]); 1846fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly i_ovf = pStreamContext->mBitIndex + length - 32; 185e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood u_mask = (length >= 32) ? 0xffffffff: (1 << length) - 1; 186e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood 187e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood /* do we have enough bits availble in the current word(32bits)*/ 1886fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly if (i_ovf <= 0) { 1896fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly retval=(retval >> (- i_ovf)) & u_mask; 1906fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly } else { 1916fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4OSA_UInt32 u_nextword = (M4OSA_UInt32)GET_MEMORY32( 1923c8fbdf5dd19fad0b3006557716ee1026d7d5899Fred Fettinger pStreamContext->mPbitStream[ pStreamContext->mIndex + 1 ]); 1933c8fbdf5dd19fad0b3006557716ee1026d7d5899Fred Fettinger M4OSA_UInt32 u_msb_mask, u_msb_value, u_lsb_mask, u_lsb_value; 1943c8fbdf5dd19fad0b3006557716ee1026d7d5899Fred Fettinger 1956fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly u_msb_mask = ((1 << (32 - pStreamContext->mBitIndex)) - 1) << i_ovf; 1966fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly u_msb_value = retval << i_ovf; 1976fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly u_lsb_mask = (1 << i_ovf) - 1; 1986fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly u_lsb_value = u_nextword >> (32 - i_ovf); 1996fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly retval= (u_msb_value & u_msb_mask ) | (u_lsb_value & u_lsb_mask); 2008863c43d9efda108c880814b0a18688b75d70153Mark Vandevoorde } 2016fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly /* return the bits...*/ 2026fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly return retval; 20315e3d0f082d551f8819fbe4b0d502cc108627876Mike Lockwood} 2046fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly/** 2056fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly ************************************************************************ 2066fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * @brief Increment the bitstream pointer of <length> bits. 2076fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * @param pContext (IN/OUT) BitStreamParser context. 2086fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * @param length (IN) The number of bit to shift the bitstream 2096fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly ************************************************************************ 2106fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly*/ 211e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwoodstatic void VideoEditor3gpReader_BitStreamParserFlushBits(void* pContext, 212e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood M4OSA_Int32 length) { 213e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood VideoEditor3gpReader_BitStreamParserContext* pStreamContext=( 2146fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly VideoEditor3gpReader_BitStreamParserContext*)pContext; 215b711d57ca4e2c6a1befbfa1a41f4b8094755a93fVictoria Lease M4OSA_Int32 val; 216b711d57ca4e2c6a1befbfa1a41f4b8094755a93fVictoria Lease 217b711d57ca4e2c6a1befbfa1a41f4b8094755a93fVictoria Lease if (M4OSA_NULL == pStreamContext) { 218b711d57ca4e2c6a1befbfa1a41f4b8094755a93fVictoria Lease return; 219b711d57ca4e2c6a1befbfa1a41f4b8094755a93fVictoria Lease } 2206fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly val=pStreamContext->mBitIndex + length; 2216fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly /* update the bits...*/ 2226fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly pStreamContext->mBitIndex += length; 2236fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 2246fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly if (val - 32 >= 0) { 2256fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly /* update the bits...*/ 2266fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly pStreamContext->mBitIndex -= 32; 2276fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly /* update the words*/ 2286fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly pStreamContext->mIndex++; 2296fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly } 2306fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly} 2316fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 2326fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellystatic M4OSA_UInt32 VideoEditor3gpReader_BitStreamParserGetBits( 2336fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly void* pContext,M4OSA_Int32 bitPos, M4OSA_Int32 bitLength) { 2346fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly VideoEditor3gpReader_BitStreamParserContext* pStreamContext = 2356fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly (VideoEditor3gpReader_BitStreamParserContext*)pContext; 2366fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 2376fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4OSA_Int32 bitLocation, bitIndex; 2386fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4OSA_UInt32 retval=0; 2396fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 2406fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4OSA_DEBUG_IF1((M4OSA_NULL==pStreamContext), 0, 2416fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly "VideoEditor3gpReader_BitStreamParserGetBits: invalid context pointer"); 2426fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 2438863c43d9efda108c880814b0a18688b75d70153Mark Vandevoorde /* computes the word location*/ 24415e3d0f082d551f8819fbe4b0d502cc108627876Mike Lockwood bitLocation=bitPos/32; 24515e3d0f082d551f8819fbe4b0d502cc108627876Mike Lockwood bitIndex=(bitPos) % 32; 2466fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 2476fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly if (bitLocation < pStreamContext->mSize) { 2486fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4OSA_UInt32 u_mask; 2496fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4OSA_Int32 i_ovf = bitIndex + bitLength - 32; 2506fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly retval=(M4OSA_UInt32)GET_MEMORY32( 2516fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly pStreamContext->mPbitStream[ bitLocation ]); 2526fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 2536fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly u_mask = (bitLength >= 32) ? 0xffffffff: (1 << bitLength) - 1; 2546fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 2556fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly if (i_ovf <= 0) { 2566fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly retval=(retval >> (- i_ovf)) & u_mask; 2576fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly } else { 2586fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly M4OSA_UInt32 u_nextword = (M4OSA_UInt32)GET_MEMORY32( 2596fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly pStreamContext->mPbitStream[ bitLocation + 1 ]); 260e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood M4OSA_UInt32 u_msb_mask, u_msb_value, u_lsb_mask, u_lsb_value; 261e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood 2626fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly u_msb_mask = ((1 << (32 - bitIndex)) - 1) << i_ovf; 2636fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly u_msb_value = retval << i_ovf; 2646fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly u_lsb_mask = (1 << i_ovf) - 1; 2656fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly u_lsb_value = u_nextword >> (32 - i_ovf); 266e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood retval= (u_msb_value & u_msb_mask ) | (u_lsb_value & u_lsb_mask); 2676fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly } 2686fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly } 2696fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly return retval; 2706fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly} 2716fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 2726fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pellystatic void VideoEditor3gpReader_BitStreamParserRestart(void* pContext) { 2736fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly VideoEditor3gpReader_BitStreamParserContext* pStreamContext = 274fd6e5f0dda50e113db4ccc55338b6c4f09da37a4Mike Lockwood (VideoEditor3gpReader_BitStreamParserContext*)pContext; 2756fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 276fd6e5f0dda50e113db4ccc55338b6c4f09da37a4Mike Lockwood if (M4OSA_NULL == pStreamContext) { 277fd6e5f0dda50e113db4ccc55338b6c4f09da37a4Mike Lockwood return; 2786fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly } 279e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood /* resets the bitstream pointers*/ 2806fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly pStreamContext->mIndex=0; 2816fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly pStreamContext->mBitIndex=0; 282e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood} 2836fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly/** 2846fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly ******************************************************************************* 2856fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * @brief Get a pointer to the current byte pointed by the bitstream pointer. 2866fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * @note It should be used carefully as the pointer is in the bitstream itself 2876fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * and no copy is made. 2886fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * @param pContext (IN/OUT) BitStreamParser context. 2896fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly * @return Pointer to the current location in the bitstream 290e932f7f2a47b770c636443d411436bd29cc4bb43Mike Lockwood ******************************************************************************* 2916fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly*/ 2923681f2614fa8c10560c790f6b4287f65d6fd2663Mike Lockwoodstatic M4OSA_UInt8* VideoEditor3gpReader_GetCurrentbitStreamPointer( 2936fa9ad4afcd762aea519ff61811386c23d18ddb2Nick Pelly 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 pC->mDataSource = new FileSource ((char*)pFileDescriptor); 488 489 if (pC->mDataSource == NULL) { 490 LOGV("VideoEditor3gpReader_open Datasource error"); 491 return M4ERR_PARAMETER; 492 } 493 494 pC->mExtractor = MediaExtractor::Create(pC->mDataSource, 495 MEDIA_MIMETYPE_CONTAINER_MPEG4); 496 497 if (pC->mExtractor == NULL) { 498 LOGV("VideoEditor3gpReader_open extractor error"); 499 return M4ERR_PARAMETER; 500 } 501 502 LOGV("VideoEditor3gpReader_open end "); 503 return err; 504} 505 506/** 507************************************************************************ 508* @brief close the reader 509* @note close the 3GP file 510* @param context: (IN) Context of the reader 511* @return M4NO_ERROR there is no error 512* @return M4ERR_PARAMETER the context is NULL 513* @return M4ERR_BAD_CONTEXT provided context is not a valid one 514************************************************************************ 515*/ 516M4OSA_ERR VideoEditor3gpReader_close(M4OSA_Context context) { 517 VideoEditor3gpReader_Context *pC = (VideoEditor3gpReader_Context*)context; 518 M4READER_AudioSbrUserdata *pAudioSbrUserData; 519 M4_AccessUnit *pAU; 520 M4OSA_ERR err = M4NO_ERROR; 521 522 LOGV("VideoEditor3gpReader_close begin"); 523 524 M4OSA_DEBUG_IF1((M4OSA_NULL == pC), M4ERR_PARAMETER, 525 "VideoEditor3gpReader_close: invalid context pointer"); 526 527 if (pC->mAudioStreamHandler) { 528 LOGV("VideoEditor3gpReader_close Audio"); 529 530 if (M4OSA_NULL != pC->mAudioStreamHandler->m_pDecoderSpecificInfo) { 531 M4OSA_free((M4OSA_MemAddr32)pC->mAudioStreamHandler->\ 532 m_pDecoderSpecificInfo); 533 pC->mAudioStreamHandler->m_decoderSpecificInfoSize = 0; 534 pC->mAudioStreamHandler->m_pDecoderSpecificInfo = M4OSA_NULL; 535 } 536 537 if ((M4DA_StreamTypeAudioAac == pC->mAudioStreamHandler->m_streamType) 538 && (M4OSA_NULL != pC->mAudioStreamHandler->m_pUserData)) { 539 pAudioSbrUserData = (M4READER_AudioSbrUserdata*)(\ 540 pC->mAudioStreamHandler->m_pUserData); 541 542 pAU = (M4_AccessUnit*)pAudioSbrUserData->m_pFirstAU; 543 if (M4OSA_NULL != pAU) { 544 M4OSA_free((M4OSA_MemAddr32)pAU); 545 } 546 547 if (M4OSA_NULL != pAudioSbrUserData->m_pAacDecoderUserConfig) { 548 M4OSA_free((M4OSA_MemAddr32)pAudioSbrUserData->\ 549 m_pAacDecoderUserConfig); 550 } 551 M4OSA_free((M4OSA_MemAddr32)pAudioSbrUserData); 552 pC->mAudioStreamHandler->m_pUserData = M4OSA_NULL; 553 } 554 555 if (pC->mAudioStreamHandler->m_pESDSInfo != M4OSA_NULL) { 556 M4OSA_free((M4OSA_MemAddr32)pC->mAudioStreamHandler->m_pESDSInfo); 557 pC->mAudioStreamHandler->m_pESDSInfo = M4OSA_NULL; 558 pC->mAudioStreamHandler->m_ESDSInfoSize = 0; 559 } 560 /* Finally destroy the stream handler */ 561 M4OSA_free((M4OSA_MemAddr32)pC->mAudioStreamHandler); 562 pC->mAudioStreamHandler = M4OSA_NULL; 563 564 pC->mAudioSource->stop(); 565 pC->mAudioSource.clear(); 566 } 567 if (pC->mVideoStreamHandler) { 568 LOGV("VideoEditor3gpReader_close Video "); 569 570 if(M4OSA_NULL != pC->mVideoStreamHandler->m_pDecoderSpecificInfo) { 571 M4OSA_free((M4OSA_MemAddr32)pC->mVideoStreamHandler->\ 572 m_pDecoderSpecificInfo); 573 pC->mVideoStreamHandler->m_decoderSpecificInfoSize = 0; 574 pC->mVideoStreamHandler->m_pDecoderSpecificInfo = M4OSA_NULL; 575 } 576 577 if(M4OSA_NULL != pC->mVideoStreamHandler->m_pH264DecoderSpecificInfo) { 578 M4OSA_free((M4OSA_MemAddr32)pC->mVideoStreamHandler->\ 579 m_pH264DecoderSpecificInfo); 580 pC->mVideoStreamHandler->m_H264decoderSpecificInfoSize = 0; 581 pC->mVideoStreamHandler->m_pH264DecoderSpecificInfo = M4OSA_NULL; 582 } 583 584 if(pC->mVideoStreamHandler->m_pESDSInfo != M4OSA_NULL) { 585 M4OSA_free((M4OSA_MemAddr32)pC->mVideoStreamHandler->m_pESDSInfo); 586 pC->mVideoStreamHandler->m_pESDSInfo = M4OSA_NULL; 587 pC->mVideoStreamHandler->m_ESDSInfoSize = 0; 588 } 589 590 /* Finally destroy the stream handler */ 591 M4OSA_free((M4OSA_MemAddr32)pC->mVideoStreamHandler); 592 pC->mVideoStreamHandler = M4OSA_NULL; 593 594 pC->mVideoSource->stop(); 595 pC->mVideoSource.clear(); 596 } 597 pC->mExtractor.clear(); 598 pC->mDataSource.clear(); 599 600 LOGV("VideoEditor3gpReader_close end"); 601 return err; 602} 603 604/** 605************************************************************************ 606* @brief get an option from the 3gp reader 607* @note it allows the caller to retrieve a property value: 608* 609* @param context: (IN) Context of the reader 610* @param optionId: (IN) indicates the option to get 611* @param pValue: (OUT) pointer to structure or value (allocated 612* by user) where option is stored 613* 614* @return M4NO_ERROR there is no error 615* @return M4ERR_BAD_CONTEXT provided context is not a valid one 616* @return M4ERR_PARAMETER at least one parameter is not properly set 617* @return M4ERR_BAD_OPTION_ID when the option ID is not a valid one 618* @return M4ERR_VIDEO_NOT_H263 No video stream H263 in file. 619* @return M4ERR_NO_VIDEO_STREAM_RETRIEVED_YET 620* Function 3gpReader_getNextStreamHandler must be called before 621************************************************************************ 622*/ 623M4OSA_ERR VideoEditor3gpReader_getOption(M4OSA_Context context, 624 M4OSA_OptionID optionId, M4OSA_DataOption pValue) { 625 VideoEditor3gpReader_Context* pC = (VideoEditor3gpReader_Context*)context; 626 M4OSA_ERR err = M4NO_ERROR; 627 628 LOGV("VideoEditor3gpReader_getOption begin %d", optionId); 629 630 M4OSA_DEBUG_IF1((M4OSA_NULL == pC), M4ERR_PARAMETER, 631 "invalid context pointer"); 632 M4OSA_DEBUG_IF1((M4OSA_NULL == pValue), M4ERR_PARAMETER, 633 "VideoEditor3gpReader_getOption: invalid pointer on value"); 634 635 switch (optionId) { 636 case M4READER_kOptionID_Duration: 637 { 638 LOGV("VideoEditor3gpReader_getOption duration %d",pC->mMaxDuration); 639 M4OSA_TIME_SET(*(M4OSA_Time*)pValue, pC->mMaxDuration); 640 } 641 break; 642 case M4READER_kOptionID_Version: 643 /* not used */ 644 LOGV("VideoEditor3gpReader_getOption: M4READER_kOptionID_Version"); 645 break; 646 647 case M4READER_kOptionID_Copyright: 648 /* not used */ 649 LOGV(">>>>>>> M4READER_kOptionID_Copyright"); 650 break; 651 652 case M4READER_kOptionID_CreationTime: 653 /* not used */ 654 LOGV("VideoEditor3gpReader_getOption M4READER_kOptionID_CreationTime"); 655 break; 656 657 case M4READER_kOptionID_Bitrate: 658 { 659 M4OSA_UInt32* pBitrate = (M4OSA_UInt32*)pValue; 660 661 if (pC->mMaxDuration != 0) { 662 M4OSA_UInt32 ui32Tmp = (M4OSA_UInt32)pC->mMaxDuration; 663 *pBitrate = (M4OSA_UInt32)((M4OSA_Double)pC->mFileSize * \ 664 8000.0 / (M4OSA_Double)ui32Tmp); 665 LOGV("3gpReader_getOption bitrate: %d", *pBitrate); 666 } 667 *pBitrate = 384000; //check 668 LOGV("VideoEditor3gpReader_getOption bitrate %ld", *pBitrate); 669 } 670 break; 671 case M4READER_3GP_kOptionID_H263Properties: 672 { 673 if(M4OSA_NULL == pC->mVideoStreamHandler) { 674 LOGV("VideoEditor3gpReader_getOption no videoStream retrieved"); 675 676 err = M4ERR_NO_VIDEO_STREAM_RETRIEVED_YET; 677 break; 678 } 679 if((M4DA_StreamTypeVideoH263 != pC->mVideoStreamHandler->\ 680 m_streamType) || (pC->mVideoStreamHandler->\ 681 m_decoderSpecificInfoSize < 7)) { 682 LOGV("VideoEditor3gpReader_getOption DSI Size %d", 683 pC->mVideoStreamHandler->m_decoderSpecificInfoSize); 684 685 err = M4ERR_VIDEO_NOT_H263; 686 break; 687 } 688 689 /* MAGICAL in the decoder confi H263: the 7th byte is the profile 690 * number, 6th byte is the level number */ 691 ((M4READER_3GP_H263Properties *)pValue)->uiProfile = 692 pC->mVideoStreamHandler->m_pDecoderSpecificInfo[6]; 693 ((M4READER_3GP_H263Properties *)pValue)->uiLevel = 694 pC->mVideoStreamHandler->m_pDecoderSpecificInfo[5]; 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 int32_t i32Tmp = 0; 974 975 M4OSA_DEBUG_IF1((pReaderContext == 0), M4ERR_PARAMETER, 976 "VideoEditor3gpReader_getNextAu: invalid context"); 977 M4OSA_DEBUG_IF1((pStreamHandler == 0), M4ERR_PARAMETER, 978 "VideoEditor3gpReader_getNextAu: invalid pointer to M4_StreamHandler"); 979 M4OSA_DEBUG_IF1((pAccessUnit == 0), M4ERR_PARAMETER, 980 "VideoEditor3gpReader_getNextAu: invalid pointer to M4_AccessUnit"); 981 982 LOGV("VideoEditor3gpReader_getNextAu begin"); 983 984 if (pStreamHandler == (M4_StreamHandler*)pC->mAudioStreamHandler) { 985 LOGV("VideoEditor3gpReader_getNextAu audio stream"); 986 pAu = &pC->mAudioAu; 987 if (pC->mAudioSeeking == M4OSA_TRUE) { 988 LOGV("VideoEditor3gpReader_getNextAu audio seek time: %ld", 989 pC->mAudioSeekTime); 990 options.setSeekTo(pC->mAudioSeekTime); 991 pC->mAudioSource->read(&mMediaBuffer, &options); 992 993 mMediaBuffer->meta_data()->findInt64(kKeyTime, 994 (int64_t*)&tempTime64); 995 options.clearSeekTo(); 996 pC->mAudioSeeking = M4OSA_FALSE; 997 flag = M4OSA_TRUE; 998 } else { 999 LOGV("VideoEditor3gpReader_getNextAu audio no seek:"); 1000 pC->mAudioSource->read(&mMediaBuffer, &options); 1001 if (mMediaBuffer != NULL) { 1002 mMediaBuffer->meta_data()->findInt64(kKeyTime, 1003 (int64_t*)&tempTime64); 1004 } 1005 } 1006 } else if (pStreamHandler == (M4_StreamHandler*)pC->mVideoStreamHandler) { 1007 LOGV("VideoEditor3gpReader_getNextAu video steram "); 1008 pAu = &pC->mVideoAu; 1009 if(pC->mVideoSeeking == M4OSA_TRUE) { 1010 flag = M4OSA_TRUE; 1011 LOGV("VideoEditor3gpReader_getNextAu seek: %ld",pC->mVideoSeekTime); 1012 options.setSeekTo(pC->mVideoSeekTime, 1013 MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC); 1014 do 1015 { 1016 if (mMediaBuffer != NULL) { 1017 LOGV("VideoEditor3gpReader_getNextAu free the MediaBuffer"); 1018 mMediaBuffer->release(); 1019 } 1020 error = pC->mVideoSource->read(&mMediaBuffer, &options); 1021 LOGV("VE3gpReader_getNextAu MediaBuffer %x , error %d", 1022 mMediaBuffer, error); 1023 if (mMediaBuffer != NULL) 1024 { 1025 if (mMediaBuffer->meta_data()->findInt32(kKeyIsSyncFrame, 1026 &i32Tmp) && i32Tmp) { 1027 LOGV("SYNC FRAME FOUND--%d", i32Tmp); 1028 pAu->attribute = AU_RAP; 1029 } 1030 else { 1031 pAu->attribute = AU_P_Frame; 1032 } 1033 mMediaBuffer->meta_data()->findInt64(kKeyTime, 1034 (int64_t*)&tempTime64); 1035 } else { 1036 break; 1037 } 1038 options.clearSeekTo(); 1039 } while(tempTime64 < pC->mVideoSeekTime); 1040 1041 LOGV("VE3gpReader_getNextAu: video time with seek = %lld:", 1042 tempTime64); 1043 pC->mVideoSeeking = M4OSA_FALSE; 1044 } else { 1045 LOGV("VideoEditor3gpReader_getNextAu video no seek:"); 1046 pC->mVideoSource->read(&mMediaBuffer, &options); 1047 1048 if(mMediaBuffer != NULL) { 1049 if (mMediaBuffer->meta_data()->findInt32(kKeyIsSyncFrame, 1050 &i32Tmp) && i32Tmp) { 1051 LOGV("SYNC FRAME FOUND--%d", i32Tmp); 1052 pAu->attribute = AU_RAP; 1053 } 1054 else { 1055 pAu->attribute = AU_P_Frame; 1056 } 1057 mMediaBuffer->meta_data()->findInt64(kKeyTime, 1058 (int64_t*)&tempTime64); 1059 LOGV("VE3gpReader_getNextAu: video no seek time = %lld:", 1060 tempTime64); 1061 }else { 1062 LOGV("VE3gpReader_getNextAu:video no seek time buffer is NULL"); 1063 } 1064 } 1065 } else { 1066 LOGV("VideoEditor3gpReader_getNextAu M4ERR_PARAMETER"); 1067 return M4ERR_PARAMETER; 1068 } 1069 1070 if (mMediaBuffer != NULL) { 1071 if( (pAu->dataAddress == NULL) || (pAu->size < \ 1072 mMediaBuffer->range_length())) { 1073 if(pAu->dataAddress != NULL) { 1074 M4OSA_free((M4OSA_Int32*)pAu->dataAddress); 1075 pAu->dataAddress = NULL; 1076 } 1077 LOGV("Buffer lenght = %d ,%d",(mMediaBuffer->range_length() +\ 1078 3) & ~0x3,(mMediaBuffer->range_length())); 1079 1080 pAu->dataAddress = (M4OSA_Int32*)M4OSA_malloc( 1081 (mMediaBuffer->range_length() + 3) & ~0x3,M4READER_3GP, 1082 (M4OSA_Char*)"pAccessUnit->m_dataAddress" ); 1083 if(pAu->dataAddress == NULL) { 1084 LOGV("VideoEditor3gpReader_getNextAu malloc failed"); 1085 return M4ERR_ALLOC; 1086 } 1087 } 1088 pAu->size = mMediaBuffer->range_length(); 1089 1090 memcpy((M4OSA_MemAddr8)pAu->dataAddress, 1091 (const char *)mMediaBuffer->data() + mMediaBuffer->range_offset(), 1092 mMediaBuffer->range_length()); 1093 1094 if( (pStreamHandler == (M4_StreamHandler*)pC->mVideoStreamHandler) && 1095 (pStreamHandler->m_streamType == M4DA_StreamTypeVideoMpeg4Avc) ) { 1096 M4OSA_UInt32 size = mMediaBuffer->range_length(); 1097 M4OSA_UInt8 *lbuffer; 1098 1099 lbuffer = (M4OSA_UInt8 *) pAu->dataAddress; 1100 LOGV("pAccessUnit->m_dataAddress size = %x",size); 1101 1102 lbuffer[0] = (size >> 24) & 0xFF; 1103 lbuffer[1] = (size >> 16) & 0xFF; 1104 lbuffer[2] = (size >> 8) & 0xFF; 1105 lbuffer[3] = (size) & 0xFF; 1106 } 1107 1108 pAu->CTS = tempTime64; 1109 1110 pAu->CTS = pAu->CTS / 1000; //converting the microsec to millisec 1111 LOGV("VideoEditor3gpReader_getNextAu CTS = %ld",pAu->CTS); 1112 1113 pAu->DTS = pAu->CTS; 1114 if (pStreamHandler == (M4_StreamHandler*)pC->mAudioStreamHandler) { 1115 pAu->attribute = M4SYS_kFragAttrOk; 1116 } 1117 mMediaBuffer->release(); 1118 1119 pAccessUnit->m_dataAddress = (M4OSA_Int8*) pAu->dataAddress; 1120 pAccessUnit->m_size = pAu->size; 1121 pAccessUnit->m_maxsize = pAu->size; 1122 pAccessUnit->m_CTS = pAu->CTS; 1123 pAccessUnit->m_DTS = pAu->DTS; 1124 pAccessUnit->m_attribute = pAu->attribute; 1125 1126 } else { 1127 LOGV("VideoEditor3gpReader_getNextAu: M4WAR_NO_MORE_AU (EOS) reached"); 1128 pAccessUnit->m_size = 0; 1129 err = M4WAR_NO_MORE_AU; 1130 } 1131 options.clearSeekTo(); 1132 1133 pAu->nbFrag = 0; 1134 mMediaBuffer = NULL; 1135 LOGV("VideoEditor3gpReader_getNextAu end "); 1136 1137 return err; 1138} 1139/** 1140 ******************************************************************************* 1141 * @brief Split the AVC DSI in its different components and write it in 1142 * ONE memory buffer 1143 * @note 1144 * @param pStreamHandler: (IN/OUT) The MPEG4-AVC stream 1145 * @param pDecoderConfigLocal: (IN) The DSI buffer 1146 * @param decoderConfigSizeLocal: (IN) The DSI buffer size 1147 * @return M4NO_ERROR there is no error 1148 * @return ERR_FILE_SYNTAX_ERROR pDecoderConfigLocal is NULL 1149 ******************************************************************************* 1150*/ 1151static M4OSA_ERR VideoEditor3gpReader_AnalyseAvcDsi( 1152 M4_StreamHandler *pStreamHandler, M4OSA_Int32* pDecoderConfigLocal, 1153 M4OSA_Int32 decoderConfigSizeLocal) { 1154 struct _avcSpecificInfo *pAvcSpecInfo = M4OSA_NULL; 1155 M4OSA_UInt32 uiSpecInfoSize; 1156 M4OSA_Context pBitParserContext = M4OSA_NULL; 1157 M4OSA_MemAddr8 pPos; 1158 1159 /** 1160 * First parsing to get the total allocation size (we must not do 1161 * multiple malloc, but only one instead) */ 1162 { 1163 M4OSA_Int32 val; 1164 M4OSA_UInt32 i,j; 1165 M4OSA_UInt8 nalUnitLength; 1166 M4OSA_UInt8 numOfSequenceParameterSets; 1167 M4OSA_UInt32 uiTotalSizeOfSPS = 0; 1168 M4OSA_UInt8 numOfPictureParameterSets; 1169 M4OSA_UInt32 uiTotalSizeOfPPS = 0; 1170 M4OSA_UInt32 uiSize; 1171 struct _avcSpecificInfo avcSpIf; 1172 1173 avcSpIf.m_nalUnitLength = 0; 1174 1175 if (M4OSA_NULL == pDecoderConfigLocal) { 1176 return M4ERR_READER3GP_DECODER_CONFIG_ERROR; 1177 } 1178 1179 VideoEditor3gpReader_MPEG4BitStreamParserInit(&pBitParserContext, 1180 pDecoderConfigLocal, decoderConfigSizeLocal); 1181 1182 if (M4OSA_NULL == pBitParserContext) { 1183 return M4ERR_ALLOC; 1184 } 1185 1186 VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8); 1187 /* 8 bits -- configuration version */ 1188 VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8); 1189 /* 8 bits -- avc profile indication*/ 1190 VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8); 1191 /* 8 bits -- profile compatibility */ 1192 VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8); 1193 /* 8 bits -- avc level indication*/ 1194 val=VideoEditor3gpReader_BitStreamParserShowBits(pBitParserContext, 8); 1195 /* 6 bits reserved 111111b 2 bits length Size minus one*/ 1196 VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8); 1197 /* m_nalUnitLength */ 1198 1199 nalUnitLength = (M4OSA_UInt8)((val & 0x03) + 1);/*0b11111100*/ 1200 if (nalUnitLength > 4) { 1201 pStreamHandler->m_decoderSpecificInfoSize = 0; 1202 pStreamHandler->m_pDecoderSpecificInfo = M4OSA_NULL; 1203 VideoEditor3gpReader_BitStreamParserCleanUp(pBitParserContext); 1204 } else { 1205 /** 1206 * SPS table */ 1207 val=VideoEditor3gpReader_BitStreamParserShowBits(pBitParserContext, 1208 8);/* 3 bits-reserved 111b-5 bits number of sequence parameter set*/ 1209 numOfSequenceParameterSets = val & 0x1F; 1210 /*1F instead of E0*/ /*0b11100000*/ /*Number of seq parameter sets*/ 1211 VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8); 1212 for (i=0; i < numOfSequenceParameterSets; i++) { 1213 /** 1214 * Get the size of this element */ 1215 uiSize = 1216 (M4OSA_UInt32)VideoEditor3gpReader_BitStreamParserShowBits( 1217 pBitParserContext, 16); 1218 uiTotalSizeOfSPS += uiSize; 1219 VideoEditor3gpReader_BitStreamParserFlushBits( 1220 pBitParserContext, 16); 1221 /** 1222 *Read the element(dont keep it, we only want size right now) */ 1223 for (j=0; j<uiSize; j++) { 1224 VideoEditor3gpReader_BitStreamParserFlushBits( 1225 pBitParserContext, 8); 1226 } 1227 } 1228 1229 /** 1230 * SPS table */ 1231 numOfPictureParameterSets=(M4OSA_UInt8)\ 1232 VideoEditor3gpReader_BitStreamParserShowBits(pBitParserContext, 1233 8); 1234 VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8); 1235 for (i=0; i < numOfPictureParameterSets; i++) { 1236 /** 1237 * Get the size of this element */ 1238 uiSize = (M4OSA_UInt32) 1239 VideoEditor3gpReader_BitStreamParserShowBits( 1240 pBitParserContext, 16); 1241 uiTotalSizeOfPPS += uiSize; 1242 VideoEditor3gpReader_BitStreamParserFlushBits( 1243 pBitParserContext, 16); 1244 /** 1245 *Read the element(dont keep it,we only want size right now)*/ 1246 for (j=0; j<uiSize; j++) { 1247 VideoEditor3gpReader_BitStreamParserFlushBits( 1248 pBitParserContext, 8); 1249 } 1250 } 1251 1252 /** 1253 * Compute the size of the full buffer */ 1254 uiSpecInfoSize = sizeof(struct _avcSpecificInfo) + 1255 numOfSequenceParameterSets * sizeof(struct _parameterSet) 1256 + /**< size of the table of SPS elements */ 1257 numOfPictureParameterSets * sizeof(struct _parameterSet) 1258 + /**< size of the table of PPS elements */ 1259 uiTotalSizeOfSPS + 1260 uiTotalSizeOfPPS; 1261 /** 1262 * Allocate the buffer */ 1263 pAvcSpecInfo =(struct _avcSpecificInfo*)M4OSA_malloc(uiSpecInfoSize, 1264 M4READER_3GP, (M4OSA_Char*)"MPEG-4 AVC DecoderSpecific"); 1265 if (M4OSA_NULL == pAvcSpecInfo) { 1266 VideoEditor3gpReader_BitStreamParserCleanUp(pBitParserContext); 1267 return M4ERR_ALLOC; 1268 } 1269 1270 /** 1271 * Set the pointers to the correct part of the buffer */ 1272 pAvcSpecInfo->m_nalUnitLength = nalUnitLength; 1273 pAvcSpecInfo->m_numOfSequenceParameterSets = 1274 numOfSequenceParameterSets; 1275 pAvcSpecInfo->m_numOfPictureParameterSets = 1276 numOfPictureParameterSets; 1277 1278 /* We place the SPS param sets table after m_pPictureParameterSet */ 1279 pAvcSpecInfo->m_pSequenceParameterSet= (struct _parameterSet*)( 1280 (M4OSA_MemAddr8)(&pAvcSpecInfo->m_pPictureParameterSet) + 1281 sizeof(pAvcSpecInfo->m_pPictureParameterSet)); 1282 /*We place the PPS param sets table after the SPS param sets table*/ 1283 pAvcSpecInfo->m_pPictureParameterSet = (struct _parameterSet*)( 1284 (M4OSA_MemAddr8)(pAvcSpecInfo->m_pSequenceParameterSet) + 1285 (numOfSequenceParameterSets * sizeof(struct _parameterSet))); 1286 /**< The data will be placed after the PPS param sets table */ 1287 pPos = (M4OSA_MemAddr8)pAvcSpecInfo->m_pPictureParameterSet + 1288 (numOfPictureParameterSets * sizeof(struct _parameterSet)); 1289 1290 /** 1291 * reset the bit parser */ 1292 VideoEditor3gpReader_BitStreamParserCleanUp(pBitParserContext); 1293 } 1294 } 1295 1296 /** 1297 * Second parsing to copy the data */ 1298 if (M4OSA_NULL != pAvcSpecInfo) { 1299 M4OSA_Int32 i,j; 1300 1301 VideoEditor3gpReader_MPEG4BitStreamParserInit(&pBitParserContext, 1302 pDecoderConfigLocal, decoderConfigSizeLocal); 1303 1304 if (M4OSA_NULL == pBitParserContext) { 1305 M4OSA_free((M4OSA_MemAddr32)pAvcSpecInfo); 1306 return M4ERR_ALLOC; 1307 } 1308 1309 VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8); 1310 /* 8 bits -- configuration version */ 1311 VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8); 1312 /* 8 bits -- avc profile indication*/ 1313 VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8); 1314 /* 8 bits -- profile compatibility */ 1315 VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8); 1316 /* 8 bits -- avc level indication*/ 1317 VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8); 1318 /* m_nalUnitLength */ 1319 VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8); 1320 /* 3 bits -- reserved 111b -- 5 bits number of sequence parameter set*/ 1321 1322 for (i=0; i < pAvcSpecInfo->m_numOfSequenceParameterSets; i++) { 1323 pAvcSpecInfo->m_pSequenceParameterSet[i].m_length = 1324 (M4OSA_UInt16)VideoEditor3gpReader_BitStreamParserShowBits( 1325 pBitParserContext, 16); 1326 VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext,16); 1327 1328 pAvcSpecInfo->m_pSequenceParameterSet[i].m_pParameterSetUnit = 1329 (M4OSA_UInt8*)pPos; /**< current position in the buffer */ 1330 pPos += pAvcSpecInfo->m_pSequenceParameterSet[i].m_length; 1331 /**< increment the position in the buffer */ 1332 for (j=0; j<pAvcSpecInfo->m_pSequenceParameterSet[i].m_length;j++){ 1333 pAvcSpecInfo->m_pSequenceParameterSet[i].m_pParameterSetUnit[j]= 1334 (M4OSA_UInt8)VideoEditor3gpReader_BitStreamParserShowBits( 1335 pBitParserContext, 8); 1336 VideoEditor3gpReader_BitStreamParserFlushBits( 1337 pBitParserContext, 8); 1338 } 1339 } 1340 1341 VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext, 8); 1342 /* number of pîcture parameter set*/ 1343 1344 for (i=0; i < pAvcSpecInfo->m_numOfPictureParameterSets; i++) { 1345 pAvcSpecInfo->m_pPictureParameterSet[i].m_length = 1346 (M4OSA_UInt16)VideoEditor3gpReader_BitStreamParserShowBits( 1347 pBitParserContext, 16); 1348 VideoEditor3gpReader_BitStreamParserFlushBits(pBitParserContext,16); 1349 1350 pAvcSpecInfo->m_pPictureParameterSet[i].m_pParameterSetUnit = 1351 (M4OSA_UInt8*)pPos; /**< current position in the buffer */ 1352 pPos += pAvcSpecInfo->m_pPictureParameterSet[i].m_length; 1353 /**< increment the position in the buffer */ 1354 for (j=0; j<pAvcSpecInfo->m_pPictureParameterSet[i].m_length; j++) { 1355 pAvcSpecInfo->m_pPictureParameterSet[i].m_pParameterSetUnit[j] = 1356 (M4OSA_UInt8)VideoEditor3gpReader_BitStreamParserShowBits( 1357 pBitParserContext, 8); 1358 VideoEditor3gpReader_BitStreamParserFlushBits( 1359 pBitParserContext, 8); 1360 } 1361 } 1362 VideoEditor3gpReader_BitStreamParserCleanUp(pBitParserContext); 1363 pStreamHandler->m_decoderSpecificInfoSize = uiSpecInfoSize; 1364 pStreamHandler->m_pDecoderSpecificInfo = (M4OSA_UInt8*)pAvcSpecInfo; 1365 } 1366 pStreamHandler->m_H264decoderSpecificInfoSize = decoderConfigSizeLocal; 1367 pStreamHandler->m_pH264DecoderSpecificInfo = (M4OSA_UInt8*)M4OSA_malloc( 1368 decoderConfigSizeLocal, M4READER_3GP, 1369 (M4OSA_Char*)"MPEG-4 AVC DecoderSpecific"); 1370 if (M4OSA_NULL == pStreamHandler->m_pH264DecoderSpecificInfo) { 1371 goto cleanup; 1372 } 1373 1374 M4OSA_memcpy((M4OSA_MemAddr8 ) pStreamHandler->m_pH264DecoderSpecificInfo, 1375 (M4OSA_MemAddr8 )pDecoderConfigLocal, 1376 pStreamHandler->m_H264decoderSpecificInfoSize); 1377 return M4NO_ERROR; 1378cleanup: 1379 VideoEditor3gpReader_BitStreamParserCleanUp(pBitParserContext); 1380 return M4ERR_READER3GP_DECODER_CONFIG_ERROR; 1381} 1382/** 1383******************************************************************************** 1384* @brief Get the next stream found in the 3gp file 1385* @note 1386* @param context: (IN) Context of the reader 1387* @param pMediaFamily: OUT) pointer to a user allocated 1388* M4READER_MediaFamily that will be filled 1389* with the media family of the found stream 1390* @param pStreamHandler:(OUT) pointer to StreamHandler that will be allocated 1391* and filled with the found stream description 1392* @return M4NO_ERROR there is no error 1393* @return M4ERR_BAD_CONTEXT provided context is not a valid one 1394* @return M4ERR_PARAMETER at least one parameter is not properly set 1395* @return M4WAR_NO_MORE_STREAM no more available stream in the media 1396******************************************************************************** 1397*/ 1398M4OSA_ERR VideoEditor3gpReader_getNextStreamHandler(M4OSA_Context context, 1399 M4READER_MediaFamily *pMediaFamily, 1400 M4_StreamHandler **pStreamHandler) { 1401 VideoEditor3gpReader_Context* pC=(VideoEditor3gpReader_Context*)context; 1402 M4OSA_ERR err = M4NO_ERROR; 1403 M4SYS_StreamID streamIdArray[2]; 1404 M4SYS_StreamDescription streamDesc; 1405 M4_AudioStreamHandler* pAudioStreamHandler; 1406 M4_VideoStreamHandler* pVideoStreamHandler; 1407 M4OSA_Int8 *DecoderSpecificInfo = M4OSA_NULL; 1408 M4OSA_Int32 decoderSpecificInfoSize =0, maxAUSize = 0; 1409 1410 M4_StreamType streamType = M4DA_StreamTypeUnknown; 1411 M4OSA_UInt8 temp, i, trackCount; 1412 M4OSA_Bool haveAudio = M4OSA_FALSE; 1413 M4OSA_Bool haveVideo = M4OSA_FALSE; 1414 sp<MetaData> meta = NULL; 1415 int64_t Duration = 0; 1416 M4OSA_UInt8* DecoderSpecific = M4OSA_NULL ; 1417 uint32_t type; 1418 const void *data; 1419 size_t size; 1420 const void *codec_specific_data; 1421 size_t codec_specific_data_size; 1422 M4OSA_Int32 ptempTime; 1423 M4OSA_Int32 avgFPS=0; 1424 1425 LOGV("VideoEditor3gpReader_getNextStreamHandler begin"); 1426 1427 M4OSA_DEBUG_IF1((pC == 0), M4ERR_PARAMETER, 1428 "VideoEditor3gpReader_getNextStreamHandler: invalid context"); 1429 M4OSA_DEBUG_IF1((pMediaFamily == 0), M4ERR_PARAMETER, 1430 "getNextStreamHandler: invalid pointer to MediaFamily"); 1431 M4OSA_DEBUG_IF1((pStreamHandler == 0), M4ERR_PARAMETER, 1432 "getNextStreamHandler: invalid pointer to StreamHandler"); 1433 1434 trackCount = pC->mExtractor->countTracks(); 1435 temp = pC->mCurrTrack; 1436 1437 if(temp >= trackCount) { 1438 LOGV("VideoEditor3gpReader_getNextStreamHandler error = %d", 1439 M4WAR_NO_MORE_STREAM); 1440 return (M4WAR_NO_MORE_STREAM); 1441 } else { 1442 const char *mime; 1443 meta = pC->mExtractor->getTrackMetaData(temp); 1444 CHECK(meta->findCString(kKeyMIMEType, &mime)); 1445 1446 if (!haveVideo && !strncasecmp(mime, "video/", 6)) { 1447 pC->mVideoSource = pC->mExtractor->getTrack(temp); 1448 pC->mVideoSource->start(); 1449 1450 *pMediaFamily = M4READER_kMediaFamilyVideo; 1451 haveVideo = true; 1452 LOGV("VideoEditor3gpReader_getNextStreamHandler getTrack called"); 1453 if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_AVC)) { 1454 streamType = M4DA_StreamTypeVideoMpeg4Avc; 1455 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_H263)) { 1456 streamType = M4DA_StreamTypeVideoH263; 1457 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_MPEG4)) { 1458 streamType = M4DA_StreamTypeVideoMpeg4; 1459 } else { 1460 LOGV("VideoEditor3gpReaderGetNextStreamHandler streamTypeNONE"); 1461 } 1462 LOGV("VideoEditor3gpReader_getNextStreamHandler: stream type: %d ", 1463 streamType); 1464 1465 if(streamType != M4DA_StreamTypeUnknown) { 1466 pC->mStreamType = streamType; 1467 pC->mStreamId = pC->mCurrTrack; 1468 1469 pVideoStreamHandler = (M4_VideoStreamHandler*)M4OSA_malloc 1470 (sizeof(M4_VideoStreamHandler), M4READER_3GP, 1471 (M4OSA_Char*)"M4_VideoStreamHandler"); 1472 if (M4OSA_NULL == pVideoStreamHandler) { 1473 return M4ERR_ALLOC; 1474 } 1475 pVideoStreamHandler->m_structSize=sizeof(M4_VideoStreamHandler); 1476 1477 meta->findInt32(kKeyWidth, 1478 (int32_t*)&(pVideoStreamHandler->m_videoWidth)); 1479 meta->findInt32(kKeyHeight, 1480 (int32_t*)&(pVideoStreamHandler->m_videoHeight)); 1481 1482 (*pStreamHandler) = (M4_StreamHandler*)(pVideoStreamHandler); 1483 meta->findInt64(kKeyDuration, 1484 (int64_t*)&(Duration)); 1485 ((*pStreamHandler)->m_duration) = 1486 (int32_t)((Duration)/1000); // conversion to mS 1487 pC->mMaxDuration = ((*pStreamHandler)->m_duration); 1488 LOGV("VideoEditor3gpReader_getNextStreamHandler m_duration %d", 1489 (*pStreamHandler)->m_duration); 1490 1491 pC->mFileSize = 0; 1492 1493 meta->findInt32(kKeyMaxInputSize, (int32_t*)&(maxAUSize)); 1494 if(maxAUSize == 0) { 1495 maxAUSize = 70000; 1496 } 1497 (*pStreamHandler)->m_maxAUSize = maxAUSize; 1498 LOGV("<<<<<<<<<< video: mMaxAUSize from MP4 extractor: %d", 1499 (*pStreamHandler)->m_maxAUSize); 1500 1501 if( (M4DA_StreamTypeVideoH263 == streamType) || 1502 (M4DA_StreamTypeVideoMpeg4Avc == streamType)){ 1503 ((M4_StreamHandler*)pVideoStreamHandler)->m_averageBitRate = 1504 384000; 1505 } 1506 1507 meta->findInt32(kKeyFrameRate, 1508 (int32_t*)&(avgFPS)); 1509 LOGV("<<<<<<<<<< video: Average FPS from MP4 extractor: %d", 1510 avgFPS); 1511 1512 pVideoStreamHandler->m_averageFrameRate =(M4OSA_Float) avgFPS; 1513 LOGV("<<<<<<<<<< video: Average FPS from MP4 extractor in FLOAT: %f", 1514 pVideoStreamHandler->m_averageFrameRate); 1515 1516 pC->mVideoStreamHandler = 1517 (M4_StreamHandler*)(pVideoStreamHandler); 1518 1519 /* Get the DSI info */ 1520 if(M4DA_StreamTypeVideoH263 == streamType) { 1521 if (meta->findData(kKeyD263, &type, &data, &size)) { 1522 (*pStreamHandler)->m_decoderSpecificInfoSize = size; 1523 if ((*pStreamHandler)->m_decoderSpecificInfoSize != 0) { 1524 DecoderSpecific = (M4OSA_UInt8*)M4OSA_malloc( 1525 (*pStreamHandler)->m_decoderSpecificInfoSize, 1526 M4READER_3GP,(M4OSA_Char*)"H263 DSI"); 1527 if (M4OSA_NULL == DecoderSpecific) { 1528 return M4ERR_ALLOC; 1529 } 1530 M4OSA_memcpy((M4OSA_MemAddr8)DecoderSpecific, 1531 (M4OSA_MemAddr8)data, size); 1532 (*pStreamHandler)->m_pDecoderSpecificInfo = 1533 DecoderSpecific; 1534 } 1535 else { 1536 (*pStreamHandler)->m_pDecoderSpecificInfo = 1537 M4OSA_NULL; 1538 (*pStreamHandler)->m_decoderSpecificInfoSize = 0; 1539 } 1540 (*pStreamHandler)->m_pESDSInfo = M4OSA_NULL; 1541 (*pStreamHandler)->m_ESDSInfoSize = 0; 1542 (*pStreamHandler)->m_pH264DecoderSpecificInfo = M4OSA_NULL; 1543 (*pStreamHandler)->m_H264decoderSpecificInfoSize = 0; 1544 } else { 1545 LOGV("VE_getNextStreamHandler: H263 dsi not found"); 1546 (*pStreamHandler)->m_pDecoderSpecificInfo = M4OSA_NULL; 1547 (*pStreamHandler)->m_decoderSpecificInfoSize = 0; 1548 (*pStreamHandler)->m_H264decoderSpecificInfoSize = 0; 1549 (*pStreamHandler)->m_pH264DecoderSpecificInfo = 1550 M4OSA_NULL; 1551 (*pStreamHandler)->m_pESDSInfo = M4OSA_NULL; 1552 (*pStreamHandler)->m_ESDSInfoSize = 0; 1553 } 1554 } 1555 else if(M4DA_StreamTypeVideoMpeg4Avc == streamType) { 1556 if(meta->findData(kKeyAVCC, &type, &data, &size)) { 1557 decoderSpecificInfoSize = size; 1558 if (decoderSpecificInfoSize != 0) { 1559 DecoderSpecificInfo = (M4OSA_Int8*)M4OSA_malloc( 1560 decoderSpecificInfoSize, M4READER_3GP, 1561 (M4OSA_Char*)"H264 DecoderSpecific" ); 1562 if (M4OSA_NULL == DecoderSpecificInfo) { 1563 LOGV("VideoEditor3gp_getNextStream is NULL "); 1564 return M4ERR_ALLOC; 1565 } 1566 M4OSA_memcpy((M4OSA_MemAddr8)DecoderSpecificInfo, 1567 (M4OSA_MemAddr8)data, decoderSpecificInfoSize); 1568 } else { 1569 LOGV("DSI Size %d", decoderSpecificInfoSize); 1570 DecoderSpecificInfo = M4OSA_NULL; 1571 } 1572 } 1573 (*pStreamHandler)->m_pESDSInfo = M4OSA_NULL; 1574 (*pStreamHandler)->m_ESDSInfoSize = 0; 1575 1576 err = VideoEditor3gpReader_AnalyseAvcDsi(*pStreamHandler, 1577 (M4OSA_Int32*)DecoderSpecificInfo, decoderSpecificInfoSize); 1578 1579 if (M4NO_ERROR != err) { 1580 return err; 1581 } 1582 LOGV("decsize %d, h264decsize %d: %d", (*pStreamHandler)->\ 1583 m_decoderSpecificInfoSize, (*pStreamHandler)->\ 1584 m_H264decoderSpecificInfoSize); 1585 1586 if(M4OSA_NULL != DecoderSpecificInfo) { 1587 M4OSA_free((M4OSA_MemAddr32)DecoderSpecificInfo); 1588 DecoderSpecificInfo = M4OSA_NULL; 1589 } 1590 } else if( (M4DA_StreamTypeVideoMpeg4 == streamType) ) { 1591 if (meta->findData(kKeyESDS, &type, &data, &size)) { 1592 ESDS esds((const char *)data, size); 1593 CHECK_EQ(esds.InitCheck(), OK); 1594 1595 (*pStreamHandler)->m_ESDSInfoSize = size; 1596 (*pStreamHandler)->m_pESDSInfo = (M4OSA_UInt8*)\ 1597 M4OSA_malloc((*pStreamHandler)->m_ESDSInfoSize, 1598 M4READER_3GP, (M4OSA_Char*)"H263 DecoderSpecific" ); 1599 if (M4OSA_NULL == (*pStreamHandler)->m_pESDSInfo) { 1600 return M4ERR_ALLOC; 1601 } 1602 M4OSA_memcpy((M4OSA_MemAddr8)(*pStreamHandler)->\ 1603 m_pESDSInfo, (M4OSA_MemAddr8)data, size); 1604 1605 esds.getCodecSpecificInfo(&codec_specific_data, 1606 &codec_specific_data_size); 1607 LOGV("VE MP4 dsisize: %d, %x", codec_specific_data_size, 1608 codec_specific_data); 1609 1610 (*pStreamHandler)->m_decoderSpecificInfoSize = 1611 codec_specific_data_size; 1612 if ((*pStreamHandler)->m_decoderSpecificInfoSize != 0) { 1613 DecoderSpecific = (M4OSA_UInt8*)M4OSA_malloc( 1614 (*pStreamHandler)->m_decoderSpecificInfoSize, 1615 M4READER_3GP, (M4OSA_Char*)" DecoderSpecific" ); 1616 if (M4OSA_NULL == DecoderSpecific) { 1617 return M4ERR_ALLOC; 1618 } 1619 M4OSA_memcpy((M4OSA_MemAddr8)DecoderSpecific, 1620 (M4OSA_MemAddr8)codec_specific_data, 1621 codec_specific_data_size); 1622 (*pStreamHandler)->m_pDecoderSpecificInfo = 1623 DecoderSpecific; 1624 } 1625 else { 1626 (*pStreamHandler)->m_pDecoderSpecificInfo = 1627 M4OSA_NULL; 1628 } 1629 (*pStreamHandler)->m_pH264DecoderSpecificInfo = 1630 M4OSA_NULL; 1631 (*pStreamHandler)->m_H264decoderSpecificInfoSize = 0; 1632 } 1633 } else { 1634 LOGV("VideoEditor3gpReader_getNextStream NO video stream"); 1635 return M4ERR_READER_UNKNOWN_STREAM_TYPE; 1636 } 1637 } 1638 else { 1639 LOGV("VideoEditor3gpReader_getNextStream NO video stream"); 1640 return M4ERR_READER_UNKNOWN_STREAM_TYPE; 1641 } 1642 1643 } else if (!haveAudio && !strncasecmp(mime, "audio/", 6)) { 1644 LOGV("VideoEditor3gpReader_getNextStream audio getTrack called"); 1645 pC->mAudioSource = pC->mExtractor->getTrack(pC->mCurrTrack); 1646 pC->mAudioSource->start(); 1647 *pMediaFamily = M4READER_kMediaFamilyAudio; 1648 1649 if(!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_NB)) { 1650 streamType = M4DA_StreamTypeAudioAmrNarrowBand; 1651 } else if(!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_WB)) { 1652 streamType = M4DA_StreamTypeAudioAmrWideBand; 1653 } 1654 else if(!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC)) { 1655 streamType = M4DA_StreamTypeAudioAac; 1656 } else { 1657 LOGV("VideoEditor3gpReader_getNextStrea streamtype Unknown "); 1658 } 1659 if(streamType != M4DA_StreamTypeUnknown) { 1660 pC->mStreamType = streamType; 1661 pC->mStreamId = pC->mCurrTrack; 1662 1663 LOGV("VE streamtype %d ,id %d", streamType, pC->mCurrTrack); 1664 1665 pAudioStreamHandler = (M4_AudioStreamHandler*)M4OSA_malloc 1666 (sizeof(M4_AudioStreamHandler), M4READER_3GP, 1667 (M4OSA_Char*)"M4_AudioStreamHandler"); 1668 if (M4OSA_NULL == pAudioStreamHandler) { 1669 return M4ERR_ALLOC; 1670 } 1671 pAudioStreamHandler->m_structSize=sizeof(M4_AudioStreamHandler); 1672 pAudioStreamHandler->m_byteSampleSize = 0; 1673 pAudioStreamHandler->m_nbChannels = 0; 1674 pAudioStreamHandler->m_samplingFrequency= 0; 1675 pAudioStreamHandler->m_byteFrameLength = 0; 1676 1677 (*pStreamHandler) = (M4_StreamHandler*)(pAudioStreamHandler); 1678 pC->mAudioStreamHandler = 1679 (M4_StreamHandler*)(pAudioStreamHandler); 1680 (*pStreamHandler)->m_averageBitRate = 0; 1681 haveAudio = true; 1682 pC->mAudioStreamHandler=(M4_StreamHandler*)pAudioStreamHandler; 1683 pC->mAudioStreamHandler->m_pESDSInfo = M4OSA_NULL; 1684 pC->mAudioStreamHandler->m_ESDSInfoSize = 0; 1685 1686 meta->findInt32(kKeyMaxInputSize, (int32_t*)&(maxAUSize)); 1687 if(maxAUSize == 0) { 1688 maxAUSize = 70000; 1689 } 1690 (*pStreamHandler)->m_maxAUSize = maxAUSize; 1691 LOGV("VE Audio mMaxAUSize from MP4 extractor: %d", maxAUSize); 1692 } 1693 if((M4DA_StreamTypeAudioAmrNarrowBand == streamType) || 1694 (M4DA_StreamTypeAudioAmrWideBand == streamType)) { 1695 M4OSA_UInt32 freqIndex = 0; /**< AMR NB */ 1696 M4OSA_UInt32 modeSet; 1697 M4OSA_UInt32 i; 1698 M4OSA_Context pBitParserContext = M4OSA_NULL; 1699 1700 if(M4DA_StreamTypeAudioAmrWideBand == streamType) { 1701 freqIndex = 1; /**< AMR WB */ 1702 } 1703 1704 if (meta->findData(kKeyESDS, &type, &data, &size)) { 1705 ESDS esds((const char *)data, size); 1706 CHECK_EQ(esds.InitCheck(), OK); 1707 1708 esds.getCodecSpecificInfo(&codec_specific_data, 1709 &codec_specific_data_size); 1710 (*pStreamHandler)->m_decoderSpecificInfoSize = 1711 codec_specific_data_size; 1712 1713 if ((*pStreamHandler)->m_decoderSpecificInfoSize != 0) { 1714 DecoderSpecific = (M4OSA_UInt8*)M4OSA_malloc( 1715 (*pStreamHandler)->m_decoderSpecificInfoSize, 1716 M4READER_3GP, (M4OSA_Char*)"H263 DecoderSpecific" ); 1717 if (M4OSA_NULL == DecoderSpecific) { 1718 return M4ERR_ALLOC; 1719 } 1720 M4OSA_memcpy((M4OSA_MemAddr8)DecoderSpecific, 1721 (M4OSA_MemAddr8)codec_specific_data, 1722 codec_specific_data_size); 1723 (*pStreamHandler)->m_pDecoderSpecificInfo = 1724 DecoderSpecific; 1725 } else { 1726 (*pStreamHandler)->m_pDecoderSpecificInfo = M4OSA_NULL; 1727 } 1728 } else { 1729 M4OSA_UChar AmrDsi[] = 1730 {'P','H','L','P',0x00, 0x00, 0x80, 0x00, 0x01,}; 1731 (*pStreamHandler)->m_decoderSpecificInfoSize = 9; 1732 DecoderSpecific = (M4OSA_UInt8*)M4OSA_malloc( 1733 (*pStreamHandler)->m_decoderSpecificInfoSize, 1734 M4READER_3GP, (M4OSA_Char*)"H263 DecoderSpecific" ); 1735 if (M4OSA_NULL == DecoderSpecific) { 1736 return M4ERR_ALLOC; 1737 } 1738 if(freqIndex ==0) { 1739 AmrDsi[8] = 0x01; 1740 } else { 1741 AmrDsi[8] = 0x02; 1742 } 1743 for(i = 0; i< 9; i++) { 1744 DecoderSpecific[i] = AmrDsi[i]; 1745 } 1746 (*pStreamHandler)->m_pDecoderSpecificInfo = DecoderSpecific; 1747 } 1748 (*pStreamHandler)->m_averageBitRate = 1749 VideoEditor3gpReader_AmrBitRate[freqIndex][7]; 1750 } else if((M4DA_StreamTypeAudioAac == streamType)) { 1751 if (meta->findData(kKeyESDS, &type, &data, &size)) { 1752 ESDS esds((const char *)data, size); 1753 CHECK_EQ(esds.InitCheck(), OK); 1754 1755 (*pStreamHandler)->m_ESDSInfoSize = size; 1756 (*pStreamHandler)->m_pESDSInfo = (M4OSA_UInt8*)M4OSA_malloc( 1757 (*pStreamHandler)->m_ESDSInfoSize, M4READER_3GP, 1758 (M4OSA_Char*)"H263 DecoderSpecific" ); 1759 if (M4OSA_NULL == (*pStreamHandler)->m_pESDSInfo) { 1760 return M4ERR_ALLOC; 1761 } 1762 M4OSA_memcpy((M4OSA_MemAddr8)(*pStreamHandler)->m_pESDSInfo, 1763 (M4OSA_MemAddr8)data, size); 1764 esds.getCodecSpecificInfo(&codec_specific_data, 1765 &codec_specific_data_size); 1766 1767 LOGV("VEdsi %d,%x",codec_specific_data_size, 1768 codec_specific_data); 1769 1770 (*pStreamHandler)->m_decoderSpecificInfoSize = 1771 codec_specific_data_size; 1772 if ((*pStreamHandler)->m_decoderSpecificInfoSize != 0) { 1773 DecoderSpecific = (M4OSA_UInt8*)M4OSA_malloc( 1774 (*pStreamHandler)->m_decoderSpecificInfoSize, 1775 M4READER_3GP, (M4OSA_Char*)"H263 DecoderSpecific" ); 1776 if (M4OSA_NULL == DecoderSpecific) { 1777 return M4ERR_ALLOC; 1778 } 1779 M4OSA_memcpy((M4OSA_MemAddr8)DecoderSpecific, 1780 (M4OSA_MemAddr8)codec_specific_data, 1781 codec_specific_data_size); 1782 (*pStreamHandler)->m_pDecoderSpecificInfo = 1783 DecoderSpecific; 1784 } else { 1785 (*pStreamHandler)->m_pDecoderSpecificInfo = M4OSA_NULL; 1786 } 1787 } 1788 } else { 1789 LOGV("VideoEditor3gpReader_getNextStream mStreamType: none "); 1790 return M4ERR_READER_UNKNOWN_STREAM_TYPE; 1791 } 1792 } else { 1793 LOGV("VE noaudio-video stream:pC->mCurrTrack = %d ",pC->mCurrTrack); 1794 pC->mCurrTrack++; //Increment current track to get the next track 1795 return M4ERR_READER_UNKNOWN_STREAM_TYPE; 1796 } 1797 LOGV("VE StreamType: %d, stremhandler %x",streamType, *pStreamHandler ); 1798 (*pStreamHandler)->m_streamType = streamType; 1799 (*pStreamHandler)->m_streamId = pC->mStreamId; 1800 (*pStreamHandler)->m_pUserData = M4OSA_NULL; 1801 (*pStreamHandler)->m_structSize = sizeof(M4_StreamHandler); 1802 (*pStreamHandler)->m_bStreamIsOK = M4OSA_TRUE; 1803 1804 meta->findInt64(kKeyDuration, 1805 (int64_t*)&(Duration)); 1806 1807 (*pStreamHandler)->m_duration = (int32_t)(Duration / 1000); 1808 1809 pC->mMaxDuration = ((*pStreamHandler)->m_duration); 1810 LOGV("VE str duration duration: %d ", (*pStreamHandler)->m_duration); 1811 1812 /* In AAC case: Put the first AU in pAudioStreamHandler->m_pUserData 1813 *since decoder has to know if stream contains SBR data(Implicit sig) */ 1814 if(M4DA_StreamTypeAudioAac == (*pStreamHandler)->m_streamType) { 1815 M4READER_AudioSbrUserdata* pAudioSbrUserdata; 1816 1817 pAudioSbrUserdata = (M4READER_AudioSbrUserdata*)M4OSA_malloc( 1818 sizeof(M4READER_AudioSbrUserdata),M4READER_3GP, 1819 (M4OSA_Char*)"M4READER_AudioSbrUserdata"); 1820 if (M4OSA_NULL == pAudioSbrUserdata) { 1821 err = M4ERR_ALLOC; 1822 goto Error; 1823 } 1824 (*pStreamHandler)->m_pUserData = pAudioSbrUserdata; 1825 pAudioSbrUserdata->m_bIsSbrEnabled = M4OSA_FALSE; 1826 1827 pAudioSbrUserdata->m_pFirstAU = (M4_AccessUnit*)M4OSA_malloc( 1828 sizeof(M4_AccessUnit),M4READER_3GP, (M4OSA_Char*)"1st AAC AU"); 1829 if (M4OSA_NULL == pAudioSbrUserdata->m_pFirstAU) { 1830 pAudioSbrUserdata->m_pAacDecoderUserConfig = M4OSA_NULL; 1831 err = M4ERR_ALLOC; 1832 goto Error; 1833 } 1834 pAudioSbrUserdata->m_pAacDecoderUserConfig = (M4_AacDecoderConfig*)\ 1835 M4OSA_malloc(sizeof(M4_AacDecoderConfig),M4READER_3GP, 1836 (M4OSA_Char*)"m_pAacDecoderUserConfig"); 1837 if (M4OSA_NULL == pAudioSbrUserdata->m_pAacDecoderUserConfig) { 1838 err = M4ERR_ALLOC; 1839 goto Error; 1840 } 1841 } 1842 if(M4DA_StreamTypeAudioAac == (*pStreamHandler)->m_streamType) { 1843 M4_AudioStreamHandler* pAudioStreamHandler = 1844 (M4_AudioStreamHandler*)(*pStreamHandler); 1845 M4READER_AudioSbrUserdata* pUserData = (M4READER_AudioSbrUserdata*)\ 1846 (pAudioStreamHandler->m_basicProperties.m_pUserData); 1847 1848 err = VideoEditor3gpReader_fillAuStruct(pC, (*pStreamHandler), 1849 (M4_AccessUnit*)pUserData->m_pFirstAU); 1850 if (M4NO_ERROR != err) { 1851 goto Error; 1852 } 1853 err = VideoEditor3gpReader_getNextAu(pC, (*pStreamHandler), 1854 (M4_AccessUnit*)pUserData->m_pFirstAU); 1855 if (M4NO_ERROR != err) { 1856 goto Error; 1857 } 1858 err = VideoEditor3gpReader_reset(pC, (*pStreamHandler)); 1859 if (M4NO_ERROR != err) { 1860 goto Error; 1861 } 1862 } 1863 } 1864 pC->mCurrTrack++; //Increment the current track to get next track 1865 LOGV("pC->mCurrTrack = %d",pC->mCurrTrack); 1866 1867 if (!haveAudio && !haveVideo) { 1868 *pMediaFamily=M4READER_kMediaFamilyUnknown; 1869 return M4ERR_READER_UNKNOWN_STREAM_TYPE; 1870 } 1871Error: 1872 LOGV("VideoEditor3gpReader_getNextStreamHandler end error = %d",err); 1873 return err; 1874} 1875 1876M4OSA_ERR VideoEditor3gpReader_getPrevRapTime(M4OSA_Context context, 1877 M4_StreamHandler *pStreamHandler, M4OSA_Int32* pTime) 1878{ 1879 VideoEditor3gpReader_Context *pC = (VideoEditor3gpReader_Context*)context; 1880 M4OSA_ERR err = M4NO_ERROR; 1881 MediaBuffer *mMediaBuffer = M4OSA_NULL; 1882 MediaSource::ReadOptions options; 1883 M4OSA_Time time64; 1884 int64_t tempTime64 = 0; 1885 status_t error; 1886 1887 LOGV("VideoEditor3gpReader_getPrevRapTime begin"); 1888 1889 M4OSA_DEBUG_IF1((pC == 0), M4ERR_PARAMETER, 1890 "VideoEditor3gpReader_getPrevRapTime: invalid context"); 1891 M4OSA_DEBUG_IF1((pStreamHandler == 0), M4ERR_PARAMETER, 1892 "VideoEditor3gpReader_getPrevRapTime invalid pointer to StreamHandler"); 1893 M4OSA_DEBUG_IF1((pTime == 0), M4ERR_PARAMETER, 1894 "VideoEditor3gpReader_getPrevRapTime: invalid time pointer"); 1895 if (*pTime == (pStreamHandler->m_duration)) { 1896 *pTime -= 1; 1897 } 1898 M4OSA_INT64_FROM_INT32(time64, *pTime); 1899 time64 = time64 * 1000; 1900 1901 LOGV("VideoEditor3gpReader_getPrevRapTime seek time: %ld",time64); 1902 options.setSeekTo(time64, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC); 1903 error = pC->mVideoSource->read(&mMediaBuffer, &options); 1904 if (error != OK) { 1905 //Can not get the previous Sync. 1906 //Must be end of stream. 1907 return M4WAR_NO_MORE_AU; 1908 } 1909 1910 mMediaBuffer->meta_data()->findInt64(kKeyTime, (int64_t*)&tempTime64); 1911 LOGV("VideoEditor3gpReader_getPrevRapTime read time %ld, %x", tempTime64, 1912 mMediaBuffer); 1913 1914 (*pTime) = (tempTime64) / 1000; 1915 1916 if(mMediaBuffer != M4OSA_NULL) { 1917 LOGV(" mMediaBuffer size = %d length %d", mMediaBuffer->size(), 1918 mMediaBuffer->range_length()); 1919 mMediaBuffer->release(); 1920 mMediaBuffer = M4OSA_NULL; 1921 } 1922 options.clearSeekTo(); 1923 1924 if(error != OK) { 1925 LOGV("VideoEditor3gpReader_getPrevRapTime end \ 1926 M4WAR_READER_INFORMATION_NOT_PRESENT"); 1927 return M4WAR_READER_INFORMATION_NOT_PRESENT; 1928 } else { 1929 LOGV("VideoEditor3gpReader_getPrevRapTime end: err %x", err); 1930 err = M4NO_ERROR; 1931 return err; 1932 } 1933} 1934 1935extern "C" { 1936M4OSA_ERR VideoEditor3gpReader_getInterface(M4READER_MediaType *pMediaType, 1937 M4READER_GlobalInterface **pRdrGlobalInterface, 1938 M4READER_DataInterface **pRdrDataInterface) { 1939 1940 M4OSA_ERR err = M4NO_ERROR; 1941 1942 VIDEOEDITOR_CHECK(M4OSA_NULL != pMediaType, M4ERR_PARAMETER); 1943 VIDEOEDITOR_CHECK(M4OSA_NULL != pRdrGlobalInterface, M4ERR_PARAMETER); 1944 VIDEOEDITOR_CHECK(M4OSA_NULL != pRdrDataInterface, M4ERR_PARAMETER); 1945 1946 LOGV("VideoEditor3gpReader_getInterface begin"); 1947 LOGV("VideoEditor3gpReader_getInterface %d 0x%x 0x%x", *pMediaType, 1948 *pRdrGlobalInterface,*pRdrDataInterface); 1949 1950 SAFE_MALLOC(*pRdrGlobalInterface, M4READER_GlobalInterface, 1, 1951 "VideoEditor3gpReader_getInterface"); 1952 SAFE_MALLOC(*pRdrDataInterface, M4READER_DataInterface, 1, 1953 "VideoEditor3gpReader_getInterface"); 1954 1955 *pMediaType = M4READER_kMediaType3GPP; 1956 1957 (*pRdrGlobalInterface)->m_pFctCreate = VideoEditor3gpReader_create; 1958 (*pRdrGlobalInterface)->m_pFctDestroy = VideoEditor3gpReader_destroy; 1959 (*pRdrGlobalInterface)->m_pFctOpen = VideoEditor3gpReader_open; 1960 (*pRdrGlobalInterface)->m_pFctClose = VideoEditor3gpReader_close; 1961 (*pRdrGlobalInterface)->m_pFctGetOption = VideoEditor3gpReader_getOption; 1962 (*pRdrGlobalInterface)->m_pFctSetOption = VideoEditor3gpReader_setOption; 1963 (*pRdrGlobalInterface)->m_pFctGetNextStream = 1964 VideoEditor3gpReader_getNextStreamHandler; 1965 (*pRdrGlobalInterface)->m_pFctFillAuStruct = 1966 VideoEditor3gpReader_fillAuStruct; 1967 (*pRdrGlobalInterface)->m_pFctStart = M4OSA_NULL; 1968 (*pRdrGlobalInterface)->m_pFctStop = M4OSA_NULL; 1969 (*pRdrGlobalInterface)->m_pFctJump = VideoEditor3gpReader_jump; 1970 (*pRdrGlobalInterface)->m_pFctReset = VideoEditor3gpReader_reset; 1971 (*pRdrGlobalInterface)->m_pFctGetPrevRapTime = 1972 VideoEditor3gpReader_getPrevRapTime; 1973 (*pRdrDataInterface)->m_pFctGetNextAu = VideoEditor3gpReader_getNextAu; 1974 (*pRdrDataInterface)->m_readerContext = M4OSA_NULL; 1975 1976cleanUp: 1977 if( M4NO_ERROR == err ) { 1978 LOGV("VideoEditor3gpReader_getInterface no error"); 1979 } else { 1980 SAFE_FREE(*pRdrGlobalInterface); 1981 SAFE_FREE(*pRdrDataInterface); 1982 1983 LOGV("VideoEditor3gpReader_getInterface ERROR 0x%X", err); 1984 } 1985 LOGV("VideoEditor3gpReader_getInterface end"); 1986 return err; 1987} 1988 1989} /* extern "C" */ 1990 1991} /* namespace android */ 1992 1993 1994