📄 mp4ifc.c
字号:
/**********************************************************************MPEG-4 Audio VMBit stream moduleThis software module was originally developed byand edited byin the course of development of the MPEG-2 NBC/MPEG-4 Audio standardISO/IEC 13818-7, 14496-1,2 and 3. This software module is animplementation of a part of one or more MPEG-2 NBC/MPEG-4 Audio toolsas specified by the MPEG-2 NBC/MPEG-4 Audio standard. ISO/IEC givesusers of the MPEG-2 NBC/MPEG-4 Audio standards free license to thissoftware module or modifications thereof for use in hardware orsoftware products claiming conformance to the MPEG-2 NBC/ MPEG-4 Audiostandards. Those intending to use this software module in hardware orsoftware products are advised that this use may infringe existingpatents. The original developer of this software module and his/hercompany, the subsequent editors and their companies, and ISO/IEC haveno liability for use of this software module or modifications thereofin an implementation. Copyright is not released for non MPEG-2NBC/MPEG-4 Audio conforming products. The original developer retainsfull right to use the code for his/her own purpose, assign or donatethe code to a third party and to inhibit third party from using thecode for non MPEG-2 NBC/MPEG-4 Audio conforming products. Thiscopyright notice must be included in all copies or derivative works.Copyright (c) 1999.*/#include <stdio.h>#include <string.h>#include "MP4Movies.h"#include "mp4ifc.h"#include "bitstreamHandle.h" /* handler, defines, enums */#include "block.h" /* handler, defines, enums */#include "buffersHandle.h" /* handler, defines, enums */#include "concealmentHandle.h" /* handler, defines, enums */#include "interface.h" /* handler, defines, enums */#include "mod_bufHandle.h" /* handler, defines, enums */#include "reorderspecHandle.h" /* handler, defines, enums */#include "resilienceHandle.h" /* handler, defines, enums */#include "tf_mainHandle.h" /* handler, defines, enums */#include "nok_ltp_common.h" /* structs */#include "bitstreamStruct.h" /* structs */#include "obj_descr.h" /* structs */#include "tf_mainStruct.h" /* structs */#include "lpc_common.h"#include "dec_tf.h"#include "bitstream.h"#include "common_m4a.h"#include "audio.h" /* audio i/o module */typedef struct{ unsigned long objectTypeIndication ; unsigned long streamType ; unsigned long upStream ; unsigned long bufferSizeDB ; unsigned long maxBitrate ; unsigned long avgBitrate ; /* Audio */ unsigned long audioObjectType ; unsigned long samplingFrequencyIndex ; unsigned long samplingFrequency ; unsigned long channelConfiguration ; /* GA */ unsigned long frameLengthFlag ; unsigned long dependsOnCoreCoder ; unsigned long coreCoderDelay ; unsigned long extensionFlag ;} DecoderConfigDescr ;#ifndef min#define min(a,b) ((a) < (b) ? (a) : (b))#endif#ifndef max#define max(a,b) ((a) > (b) ? (a) : (b))#endifstatic const unsigned long initialObjectDescrTag = 0x02 ;static const unsigned long decoderConfigDescrTag = 0x04 ;static const unsigned long decSpecificInfoTag = 0x05 ;#define maxTracks 8static int MP4Audio_ReadInitialOD (MP4Movie theMovie, int ignoreProfiles) ;static int MP4Audio_ReadDecoderConfig (MP4Handle decConfigH, DecoderConfigDescr *decConfig) ;static int MP4Audio_LookForSuitableTracks (MP4Movie theMovie, unsigned long trackNumber [maxTracks]) ;int MP4Audio_SetupDecoders (FRAME_DATA *fD, DecoderConfigDescr decConfig [maxTracks], int maxLayer, /* in: number of Layers */ char *audioFileName, /* in: audio file name */ char *audioFileFormat, /* in: audio file format (au, wav, aif, raw) */ int numChannelOut, /* in: num audio channels */ float fSampleOut, /* in: sampling frequency [Hz] */ char *decPara) ; /* in: decoder parameter string *//*static int MP4Audio_DecodeFrame (FRAME_DATA *fD, int maxLayers, MP4Handle theSample, unsigned long auLength, int numChannelOut) ;*/static BsBitStream *MP4Audio_BsFromHandle (MP4Handle theHandle) ;static FRAME_DATA frameData;static OBJECT_DESCRIPTOR objDescr;static TF_DATA tfData ;static LPC_DATA lpcData ;static ntt_DATA nttData ;static FAULT_TOLERANT_DATA faultData;static HANDLE_FAULT_TOLERANT hFault = &faultData;static AudioFile *audioFile = NULL;int MP4Audio_ProbeFile (char *inFile){ MP4Movie theMovie = NULL ;
MP4Err err = MP4OpenMovieFile ( &theMovie, inFile, MP4OpenMovieNormal ) ;
if (err == MP4NoErr)
{
MP4DisposeMovie (theMovie) ;
return 1 ;
}
else
return 0 ;
}int MP4Audio_DecodeFile (char *inFile, /* in: bitstream input file name */ char *audioFileName, /* in: audio file name */ char *audioFileFormat, /* in: audio file format (au, wav, aif, raw) */ int numChannelOut, /* in: num audio channels */ float fSampleOut, /* in: sampling frequency [Hz] */ char *decPara) /* in: decoder parameter string */{ MP4Err err; unsigned long framesDone ; unsigned long trackNumber [maxTracks] ; DecoderConfigDescr decConfig [maxTracks] ; MP4TrackReader theReader [maxTracks] ; unsigned long idx, suitableTracks = 0 ; MP4Movie theMovie = NULL; MP4Track theTrack; MP4Media theMedia; MP4Handle sampleH = NULL ; MP4Handle decoderConfigH = NULL ; err = MP4NoErr; err = MP4OpenMovieFile( &theMovie, inFile, MP4OpenMovieNormal ); if (err) goto bail; suitableTracks = MP4Audio_LookForSuitableTracks (theMovie, trackNumber) ; if ( strstr( decPara, "-out") ){ char *outLayNr = strstr( decPara, "-out") ; sscanf(&outLayNr[4],"%d",&frameData.scalOutSelect); suitableTracks = min(suitableTracks, frameData.scalOutSelect) ; } if (!suitableTracks) return MP4BadDataErr ; frameData.scalOutSelect = suitableTracks ; /* if we got here, we found at least one audio track */ BsInit (0,0,0); /* check the iod */ if (MP4Audio_ReadInitialOD (theMovie, 0)) return MP4BadDataErr ; err = MP4NewHandle( 0, &decoderConfigH ); if (err) goto bail; /* read the theTrack */ for (idx = 0 ; idx < suitableTracks ; idx++) { err = MP4GetMovieIndTrack( theMovie, trackNumber [idx], &theTrack ); if (err) goto bail; err = MP4GetTrackMedia( theTrack, &theMedia ); if (err) goto bail; err = MP4CreateTrackReader( theTrack, &theReader [idx] ); if (err) goto bail; err = MP4TrackReaderGetCurrentDecoderConfig( theReader [idx], decoderConfigH ); if (err) goto bail; if (MP4Audio_ReadDecoderConfig (decoderConfigH, &decConfig [idx])) return MP4BadDataErr ; } /* instantiate proper decoder for decoderconfig */ fSampleOut = (float) decConfig [suitableTracks-1].samplingFrequency ; numChannelOut = decConfig [suitableTracks-1].channelConfiguration ; MP4Audio_SetupDecoders (&frameData, decConfig, suitableTracks, audioFileName, audioFileFormat, numChannelOut, fSampleOut, decPara) ; err = MP4NewHandle( 0, &sampleH ); if (err) goto bail; /* loop through the file */ for (framesDone = 0 ;; framesDone++) { for (idx = 0 ; idx < suitableTracks ; idx++) { u32 unitSize, cts, dts, sampleFlags; err = MP4TrackReaderGetNextAccessUnit( theReader [idx], sampleH, &unitSize,&sampleFlags, &cts, &dts ); if ( err ) { if ( err == MP4EOF ) err = MP4NoErr; goto bail; } memcpy (frameData.layer [idx].bitBuf->data, *sampleH, unitSize) ; frameData.layer [idx].bitBuf->numBit = unitSize * 8 ; frameData.layer [idx].NoAUInBuffer = 1 ; } /* send AU to decoder */ DecTfFrameNew(numChannelOut, &frameData, &tfData, &lpcData, &nttData, hFault , AAC_SCALABLE); { int i, i_ch ; /* convert time data from double to float */ for (i_ch=0; i_ch<numChannelOut; i_ch++){ for( i=0; i<(int)tfData.block_size_samples; i++ ) { tfData.sampleBuf[i_ch][i] = (float)tfData.time_sample_vector[i_ch][i]; } } } AudioWriteData(audioFile,tfData.sampleBuf,tfData.block_size_samples); } bail: if (audioFile) AudioClose(audioFile); if ( decoderConfigH) MP4DisposeHandle (decoderConfigH) ; if ( theMovie ) MP4DisposeMovie (theMovie) ; if ( sampleH ) MP4DisposeHandle (sampleH) ; return err;}static int MP4Audio_LookForSuitableTracks (MP4Movie theMovie, unsigned long trackNumber [maxTracks]){ u32 trackCount; unsigned long idx ; unsigned long suitableTracks = 0 ; MP4Track baseTrack = NULL ; MP4Err err = MP4GetMovieTrackCount (theMovie, &trackCount) ; if (!err) { /* look for base layer */ for (idx = 1 ; idx <= trackCount ; idx++) { u32 enabled = 0; err = MP4GetMovieIndTrack (theMovie, idx, &baseTrack) ; if (err) goto bail; err = MP4GetTrackEnabled (baseTrack, &enabled ); if (err) goto bail; if (enabled) { MP4Media theMedia; u32 handlerType; u32 outRefIndex = 0 ; err = MP4GetTrackMedia(baseTrack, &theMedia ); if (err) goto bail; err = MP4GetMediaHandlerDescription(theMedia, &handlerType, NULL ); if (err) goto bail; err = MP4GetTrackReferenceCount (baseTrack, MP4StreamDependencyReferenceType, &outRefIndex) ; if ((handlerType == MP4AudioHandlerType) && (outRefIndex == 0)) { trackNumber [suitableTracks++] = idx ; break ; } } } if (suitableTracks) { /* then look for extension layers */ do { for (idx = 1 ; idx <= trackCount ; idx++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -