⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mp4_parser.h

📁 最新MTK手机软件源码
💻 H
📖 第 1 页 / 共 2 页
字号:
  // This is a warning. The file can still be handled.
  MP4_PARSER_READ_EOF,
  // This means there is no such audio or video track. Some APIs have an argument 'track type', if the
  // track is not existed, this value will be returned.
  // This is a warning. The file can still be handled.
  MP4_PARSER_NO_SUCH_TRACK,
  MP4_PARSER_INVALID_ARGUMENT,
  MP4_PARSER_INTERNAL_ERROR,
  // This is a warning. The specified sample number is invalid.
  MP4_PARSER_NO_SUCH_SAMPLE
} MP4_Parser_Status;



/* --- private definitions --- */

#define MP4_NUM_TRACKS 3

/* Definition for STTS cache table */
typedef struct {
   kal_uint32 accumulated_sample_count;
   kal_uint64 accumulated_decode_time;
} STTS_Cache_Entry;

/* Definition for STSC cache table */
typedef struct {
   kal_uint32 accumulated_sample_count;
} STSC_Cache_Entry;

/* Definition for STSS cache table */
typedef struct {
   kal_uint32 sample_number;
} STSS_Cache_Entry;

typedef struct {
   kal_uint32 track_ID;

   /* Track Header */
   kal_uint32 width;
   kal_uint32 height;

   /* Media Header */
   kal_uint32 uMediaTimeScale;
   kal_uint32 uMediaDuration;

   /* Handler Reference Box */
   kal_uint32 handler_type;

   /* Time to Sample Box (STTS) */
   kal_uint32 uTimeToSampleTableEntryCount;
   kal_uint32 uOffsetTimeToSampleTable;

   kal_uint32 uSTTSCurSampleCount;
   kal_uint64 uSTTSCurDecodeTime;
   kal_uint32 uSTTSIndex;
   kal_uint16 uSTTSStepCounter;
   kal_uint16 uSTTSCacheIndex;
   kal_uint16 uSTTSCacheTableEntryCount;     // Number of entries in STTS cache table
   kal_uint16 uSTTSCacheTableStepSize;       // The step size of STTS index of STTS cache table entry
   STTS_Cache_Entry *pSTTSCacheTable;        // The STTS cache table

   /* Sample Size Box (STSZ) */
   kal_bool   bSampleCountUpdated;     // flag to record if the sample count is updated or not
   kal_uint32 uSampleCount;            // number of samples in a track
   kal_uint32 uConstantSampleSize;
   kal_uint32 uOffsetSampleSizeTable;  // file offset of the first entry_size in sample size box

   /* Chunk Offset Box (STCO) */
   kal_uint32 uChunkCount;
   kal_uint32 uOffsetChunkOffsetTable; // file offset of the first chunk_offset in chunk offset box

   /* Sample To Chunk Box (STSC) */
   kal_uint32 uSampleToChunkEntryCount;
   kal_uint32 uOffsetSampleToChunkTable;

   kal_uint16 uSTSCCacheTableEntryCount;
   kal_uint16 uSTSCCacheTableStepSize;
   STSC_Cache_Entry *pSTSCCacheTable;

   /* Sync Sample Box (STSS) */
   kal_uint32 uSyncCount;
   kal_uint32 uOffsetSyncSampleTable;  // file offset of the first sync sample in sync sample box

   kal_uint32 uSTSSIndex;
   kal_uint16 uSTSSCacheIndex;
   kal_uint16 uSTSSCacheTableEntryCount;
   kal_uint16 uSTSSCacheTableStepSize;
   STSS_Cache_Entry *pSTSSCacheTable;

   /* Marks for checking the mandatory atoms */
   kal_bool bHasTKHD;
   kal_bool bHasMDIA;
   kal_bool bHasMDHD;
   kal_bool bHasHDLR;
   kal_bool bHasMINF;
   kal_bool bHasSTBL;
   kal_bool bHasSTSD;
   kal_bool bHasSTTS;
   kal_bool bHasSTSC;
   kal_bool bHasSTCO;

   /* cache some values for optimization in MP4_GetChunkNoAndOffset_Next */
   kal_uint32 uCurSampleNo;
   kal_uint32 uSampleToChunkIndex;
   kal_uint32 uSamplePerChunk;
   kal_uint32 uFirstChunk;
   kal_uint32 uSampleSum;
   kal_uint32 uSampleSumPrev;

   /* cache some values for optimization in MP4_GetChunkNoAndOffset_Next */
   kal_uint32 uSampleInChunk_Cache;
   kal_uint32 uSampleOffset_Cache;

   /* cache some values for optimization in MP4_GetDecodeTimeDelta_Next */
   kal_uint32 uDecodeTimeDelta_SampleNo;
   kal_uint32 uDecodeTimeDelta_TableIndex;
   kal_uint32 uDecodeTimeDelta_ThisSampleDelta;
   kal_uint32 uDecodeTimeDelta_CurSampleCount;

   /* cache some values for optimization in MP4_GetDecodeTime_Next */
   kal_uint32 uDecodeTime_SampleNo;
   kal_uint32 uDecodeTime_TableIndex;
   kal_uint64 uDecodeTime_PrevDecodeTime;
   kal_uint32 uDecodeTime_ThisSampleDelta;
   kal_uint32 uDecodeTime_CurSampleCount;

   STFSAL *pstFSAL_STSZ;
   STFSAL *pstFSAL_STCO;
} STMp4Track;

typedef struct {
   /* file system abstration layer for accessing sample tables */
   STFSAL *pstFSAL;
   FSAL_Status eFSALErr;

   STMp4Track stMp4Track[MP4_NUM_TRACKS];

   kal_uint8 bCurTrack;                // the track we're currently parsing
   kal_uint8 bAudioTrack;              // track no. of audio track
   kal_uint8 bVideoTrack;              // track no. of video track

   kal_uint32 timescale;               // in Movie Header
   kal_uint32 duration;                // in Movie Header
   kal_uint32 next_track_ID;           // in Movie Header

   MP4_Audio_Type bAudioType;          // in Audio Sample Entry
   MP4_Video_Type bVideoType;          // in Video Sample Entry

   kal_uint8 bAudioSamplingFreqIndex;  // sampling frequency index (defined in 14496-3)
   kal_uint8 bAudioChannelConfig;      // channel configuration (14496-3)
   kal_uint8 bFramesPerSample;         // in AMR Decoder Specific Structure

   kal_uint32 uAudioAvgBitrate;        // in Decoder Config Descriptor
   kal_uint32 uAudioMaxBitrate;        // in Decoder Config Descriptor
   kal_uint32 uAudioPCEFileOffset;     // for 14496-3 GA Specific Config
   kal_uint32 uAudioPCEBitLength;      // for 14496-3 GA Specific Config

   /* Marks for checking the mandatory atoms */
   kal_bool bHasMOOV;
   kal_bool bHasMVHD;
   kal_bool bHasTRAK;

   /* for MP4_Audio_Read */
   STFSAL *pstFSALAudio;

   kal_uint32 uAudioReadSampleNo;
   kal_uint32 uAudioReadSampleOffset;
   kal_uint32 uAudioReadSampleFileOffset;
   kal_uint64 uAudioSeekPointTime;     // the time of the previous seek point, in mili-second */

   kal_bool   bAddADTSFrameHeader;
   kal_uint8  pbADTSFrameHeader[ADTS_HEADER_SIZE];
   kal_uint32 uADTSHeaderReadOffset;

   kal_bool   bAddADIFFrameHeader;
   kal_uint8  pbADIFFrameHeader[ADIF_HEADER_SIZE];
   kal_uint32 uADIFFrameHeaderLen;
   kal_uint32 uADIFHeaderReadOffset;

   kal_uint32 uMOOVOffset;                // file offset of movie header
   kal_uint32 uVOSOffset;                 // file offset of video header
   kal_uint32 uVOSSize;                   // length of video header

   /* These two variable is used to keep the MP4 playback time */
   kal_uint64 uPlaybackTimeBase;          // the time base of the playback, in mili-second
   kal_uint32 uPreviousInterruptCount;    // the interrupt count kept when the previous MP4_GetPlayTime is called
} STMp4Parser;


/* ------ Private Macros ------ */

#define MP4_PARSER_FILE_GET_CUR_POS(pos) \
   if (FSAL_GetCurPos(pstMp4Parser->pstFSAL, &(pos))!=FSAL_OK) \
      return MP4_PARSER_FILE_ACCESS_ERROR

#define MP4_PARSER_CHECK_ARG(exp) \
   if (!(exp)) \
      return MP4_PARSER_INVALID_ARGUMENT

#define MP4_PARSER_ASSERT(exp) \
   if (!(exp)) \
      return MP4_PARSER_INTERNAL_ERROR

#define MP4_PARSER_ASSERT_NO_RET_VAL(exp) \
   if (!(exp)) \
      return

/* ------ Private Functions - file I/O ------ */

MP4_Parser_Status mp4_next_start_code(STMp4Parser *pstMp4Parser, kal_uint32 *puBitCnt);

/* ------ Private Functions ------ */

MP4_Parser_Status mp4_parse_box(STMp4Parser *pstMp4Parser, kal_uint32 *size, kal_uint32 *type);
MP4_Parser_Status mp4_parse_esds(STMp4Parser *pstMp4Parser, kal_int32 esds_size);

/* ------ Private Functions - audio ------ */

MP4_Parser_Status mp4_parse_mp4a(STMp4Parser *pstMp4Parser, long mp4a_size);
MP4_Parser_Status mp4_parse_decoder_config_14496_3(STMp4Parser *pstMp4Parser, long box_size);
MP4_Parser_Status mp4_parse_samr(STMp4Parser *pstMp4Parser, long samr_size);
MP4_Parser_Status prepareAACFrameHeader(STMp4Parser *pstMp4Parser);
kal_uint8 mp4_audio_amr_get_frame_per_sample(STMp4Parser *pstMp4Parser);

/* ------ Private Functions - video ------ */

MP4_Parser_Status mp4_parse_mp4v(STMp4Parser *pstMp4Parser, long mp4v_size);
MP4_Parser_Status mp4_parse_s263(STMp4Parser *pstMp4Parser, long s263_size);
/* extern int mp4_parse_decoder_config_14496_2(STMp4Parser *pstMp4Parser, long box_size); */

/* ------ Public Functions ------ */

MP4_Parser_Status MP4_Parse(STMp4Parser *pstMp4Parser, STFSAL *pstFSAL);
MP4_Parser_Status MP4_STBL_CacheTablePreprocess(STMp4Parser *pstMp4Parser, kal_uint32 *pCachePool, kal_uint32 uCachePoolSize);

MP4_Audio_Type MP4_GetAudioType(STMp4Parser *pstMp4Parser);
MP4_Video_Type MP4_GetVideoType(STMp4Parser *pstMp4Parser);

/* Media Header Box related functions */

MP4_Parser_Status MP4_GetMediaTimeScale(STMp4Parser *pstMp4Parser, kal_uint32 *puTimeScale, MP4_Track_Type eTrackType);
MP4_Parser_Status MP4_GetMediaDuration(STMp4Parser *pstMp4Parser, kal_uint32 *puDuration, MP4_Track_Type eTrackType);

MP4_Parser_Status MP4_SetFSAL_STCO(STMp4Parser *pstMp4Parser, MP4_Track_Type eTrackType, STFSAL *pstFSAL);
MP4_Parser_Status MP4_SetFSAL_STSZ(STMp4Parser *pstMp4Parser, MP4_Track_Type eTrackType, STFSAL *pstFSAL);

MP4_Parser_Status MP4_GetSampleCount(STMp4Parser *pstMp4Parser, MP4_Track_Type eTrackType, kal_uint32 *puSampleCount);
MP4_Parser_Status MP4_UpdateSampleCount(STMp4Parser *pstMp4Parser, MP4_Track_Type eTrackType);

MP4_Parser_Status MP4_GetSampleSize(STMp4Parser *pstMp4Parser, kal_uint32 uSampleNo, kal_uint32 *puSampleSize, MP4_Track_Type eTrackType);
MP4_Parser_Status MP4_GetSampleOffset(STMp4Parser *pstMp4Parser, kal_uint32 uSampleNo, kal_uint32 *puSampleOffset, MP4_Track_Type eTrackType);
MP4_Parser_Status MP4_GetSampleOffset_Next(STMp4Parser *pstMp4Parser, kal_uint32 *puSampleOffset, MP4_Track_Type eTrackType);
MP4_Parser_Status MP4_GetSampleOffset_Previous(STMp4Parser *pstMp4Parser, kal_uint32 *puSampleOffset, MP4_Track_Type eTrackType);
MP4_Parser_Status MP4_GetSampleOffset_Array(STMp4Parser *pstMp4Parser, kal_uint32 uSampleNo, kal_uint32 *puSampleOffsetArray, kal_uint32 *pArraySize, MP4_Track_Type eTrackType);

/* Decoding Time to Sample Box related functions */

MP4_Parser_Status MP4_GetDecodeTimeDelta(STMp4Parser *pstMp4Parser, kal_uint32 uSampleNo, kal_uint64 *puDecodeTimeDelta, MP4_Track_Type eTrackType);
MP4_Parser_Status MP4_GetDecodeTimeDelta_Next(STMp4Parser *pstMp4Parser, kal_uint64 *puDecodeTimeDelta, MP4_Track_Type eTrackType);
MP4_Parser_Status MP4_GetDecodeTimeDelta_Array(STMp4Parser *pstMp4Parser, kal_uint32 uSampleNo, kal_uint64 *puDecodeTimeDeltaArray, kal_uint32 *pArraySize, MP4_Track_Type eTrackType);

MP4_Parser_Status MP4_GetDecodeTime(STMp4Parser *pstMp4Parser, kal_uint32 uSampleNo, kal_uint64 *puDecodeTime, MP4_Track_Type eTrackType );
MP4_Parser_Status MP4_GetDecodeTime_Next(STMp4Parser *pstMp4Parser, kal_uint64 *puDecodeTime, MP4_Track_Type eTrackType );
MP4_Parser_Status MP4_GetDecodeTime_Array(STMp4Parser *pstMp4Parser, kal_uint32 uSampleNo, kal_uint64 *puDecodeTimeArray, kal_uint32 *pArraySize, MP4_Track_Type eTrackType );

MP4_Parser_Status MP4_GetSampleNumber(STMp4Parser *pstMp4Parser, kal_uint32 *puSampleNo, kal_uint64 uDecodeTime, MP4_Track_Type eTrackType );

/* Sync Sample Box related functions */

MP4_Parser_Status MP4_GetPrevSyncSampleNo(STMp4Parser *pstMp4Parser, kal_uint32 uCurSampleNo, kal_uint32 *puPrevSyncSampleNo, MP4_Track_Type eTrackType);

kal_uint8 MP4_Audio_GetFreqIndex(STMp4Parser *pstMp4Parser);

/* Audio related function */
MP4_Parser_Status MP4_Audio_SetFSAL(STMp4Parser *pstMp4Parser, STFSAL *pstFSALAudio);
MP4_Parser_Status MP4_Audio_Seek_To_Video(STMp4Parser *pstMp4Parser, kal_uint64 uVideoDecodeTime, kal_uint32 *puAudioSampleNo);
MP4_Parser_Status MP4_Audio_Seek(STMp4Parser *pstMp4Parser, kal_uint32 uSampleNo);
MP4_Parser_Status MP4_Audio_Read(STMp4Parser *pstMp4Parser, kal_uint8* pBuf, kal_uint32 uSize, kal_uint32 *uReadSize);
MP4_Parser_Status MP4_Audio_TimeToSampleNo(STMp4Parser *pstMp4Parser, kal_uint64 uMiliSecond, kal_uint32 *puAudioSampleNo );

/* Platform dependent function */
#include "l1audio.h"
MP4_Parser_Status MP4_Audio_GetPlayTime(STMp4Parser *pstMp4Parser, kal_uint64 *puMiliSecond, kal_uint32 speed_scale);
Media_Format MP4_Audio_Type_To_Media_Format(MP4_Audio_Type eAudioType);
MP4_Parser_Status MP4_GetPlayTime(STMp4Parser *pstMp4Parser, kal_uint64 *puMiliSecond, kal_uint32 speed_scale );
void MP4_SetPlaybackTimerBase(STMp4Parser *pstMp4Parser, kal_uint32 uPlaybackTimeBase, kal_bool bResetPreviousInterruptCount);

/* Video related function */
kal_uint32 MP4_Video_GetVOSOffset(STMp4Parser *pstMp4Parser);
kal_uint32 MP4_Video_GetVOSSize(STMp4Parser *pstMp4Parser);

kal_uint32 MP4_GetMovieHeaderOffset(STMp4Parser *pstMp4Parser);

/* Special function */
void MP4_Parser_Clone(STMp4Parser *pstMp4Parser_Src, STMp4Parser *pstMp4Parser_Dst, STFSAL *pstFSAL_Dst);

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -