📄 mp4parse.cpp
字号:
{ // not enough data in input buffer, seek to proper position and read more data
filepos += bytesprocessed;
bSeek = 1;
bytesread = 8; // get out of the while loop
break;
}
while(dwLen > 8)
{
Len =input_stream.GetUINT();
mid = input_stream.GetUINT();
dwLen -= Len;
switch(mid)
{
case FOURCC('m','v','h','d'): // movie header, overall declarations
pMovHdrBox = new CMovieHeaderBox(dwLen);
pMovHdrBox->ReadData(input_stream);
break;
case FOURCC('t','r','a','k'): // container for an individual trak or stream
while(Len > 8)
{
Len1 = input_stream.GetUINT();
mid1 = input_stream.GetUINT();
Len -= Len1;
switch(mid1)
{
case FOURCC('t','k','h','d'):
TrackList[TrackCount].pTrkHdrBox = new CTrackHeaderBox(Len1);
TrackList[TrackCount].pTrkHdrBox->ReadData(input_stream,Len1-8);
TrackCount++;
break;
case FOURCC('m','d','i','a'):
Parse_mdia(input_stream,Len1,M_TRUE,TrackCount-1);
break;
default:
input_stream.Advance(Len1-8);
break;
}
}
break;
case FOURCC('i','o','d','s'):
input_stream.Advance(Len-8);
break;
default:
input_stream.Advance(Len-8);
break;
}
}
return;
break; // moov
case FOURCC('m','d','a','t'): // media data container
if(dwLen > lbytesLeft)
{ // not enough data in input buffer, skip this box and read more data
filepos += bytesprocessed + dwLen ;
bSeek = 1;
bytesread = 8; // get out of the while loop
} else
input_stream.Advance(dwLen-8);
break;
case FOURCC('f','r','e','e'): // free space
if(dwLen > lbytesLeft)
{ // not enough data in input buffer, skip this box and read more data
filepos += bytesprocessed +dwLen ;
bSeek = 1;
bytesread = 8; // get out of the while loop
} else
input_stream.Advance(dwLen-8);
break;
case FOURCC('s','k','i','p'): // free space
if(dwLen > lbytesLeft)
{ // not enough data in input buffer, skip this box and read more data
filepos += bytesprocessed +dwLen ;
bSeek = 1;
bytesread = 8; // get out of the while loop
} else
input_stream.Advance(dwLen-8);
break;
default:
// skip unknown atom type
if(dwLen > lbytesLeft)
{ // not enough data in input buffer, skip this box and read more data
filepos += bytesprocessed +dwLen ;
bSeek = 1;
bytesread = 8; // get out of the while loop
} else
input_stream.Advance(dwLen-8);
break;
}
bytesprocessed += boxlength;
}
}
}
extern "C" void ParseISOM(MAIOSFile_t g_hFile)
{
unsigned int bytesread,bytesprocessed=0;
unsigned long filepos=0,boxlength;
long lbytesLeft;
Byte_Stream input_stream;
unsigned char *ptemp;
long offset = 0;
long Len ,Len1 ;
unsigned int mid,mid1 ;
int TrackCount=0;
m_bool bSeek = 1;
g_bSeenMetaData = 0;
InitParser();
while(1)
{
if(bSeek = 1)
inputseek(filepos);
inputread(inBuffer, BUFF_SIZE, &bytesread);
if(bytesread <= 8 || g_bSeenMetaData )
break;
offset = 0;
ptemp = inBuffer;
input_stream.Initialize(ptemp, bytesread);
bytesprocessed = 0;
while(bytesprocessed < bytesread - 8)
{
long dwLen =input_stream.GetUINT();
unsigned int id = input_stream.GetUINT();
lbytesLeft = bytesread - bytesprocessed;
boxlength = dwLen;
bSeek = 0;
if(g_bSeenMetaData ) // got the required meta data
break;
switch(id)
{
case FOURCC('m','o','o','v'): // all meta-data
g_bSeenMetaData = 1;
if(dwLen > lbytesLeft)
{ // not enough data in input buffer, seek to proper position and read more data
filepos += bytesprocessed;
bSeek = 1;
bytesread = 8; // get out of the while loop
break;
}
while(dwLen > 8)
{
Len =input_stream.GetUINT();
mid = input_stream.GetUINT();
dwLen -= Len;
switch(mid)
{
case FOURCC('m','v','h','d'): // movie header, overall declarations
pMovHdrBox = new CMovieHeaderBox(dwLen);
pMovHdrBox->ReadData(input_stream);
break;
case FOURCC('t','r','a','k'): // container for an individual trak or stream
while(Len > 8)
{
Len1 = input_stream.GetUINT();
mid1 = input_stream.GetUINT();
Len -= Len1;
switch(mid1)
{
case FOURCC('t','k','h','d'):
TrackList[TrackCount].pTrkHdrBox = new CTrackHeaderBox(Len1);
TrackList[TrackCount].pTrkHdrBox->ReadData(input_stream,Len1-8);
TrackCount++;
break;
case FOURCC('m','d','i','a'):
Parse_mdia(input_stream,Len1,M_FALSE,TrackCount-1);
break;
default:
input_stream.Advance(Len1-8);
break;
}
}
break;
default:
input_stream.Advance(Len-8);
break;
}
}
return;
break; // moov
case FOURCC('m','d','a','t'): // media data container
if(dwLen > lbytesLeft)
{ // not enough data in input buffer, skip this box and read more data
filepos += bytesprocessed + dwLen ;
bSeek = 1;
bytesread = 8; // get out of the while loop
} else
input_stream.Advance(dwLen-8);
break;
case FOURCC('f','r','e','e'): // free space
if(dwLen > lbytesLeft)
{ // not enough data in input buffer, skip this box and read more data
filepos += bytesprocessed +dwLen ;
bSeek = 1;
bytesread = 8; // get out of the while loop
} else
input_stream.Advance(dwLen-8);
break;
case FOURCC('s','k','i','p'): // free space
if(dwLen > lbytesLeft)
{ // not enough data in input buffer, skip this box and read more data
filepos += bytesprocessed +dwLen ;
bSeek = 1;
bytesread = 8; // get out of the while loop
} else
input_stream.Advance(dwLen-8);
break;
default:
// skip unknown atom type
if(dwLen > lbytesLeft)
{ // not enough data in input buffer, skip this box and read more data
filepos += bytesprocessed +dwLen ;
bSeek = 1;
bytesread = 8; // get out of the while loop
} else
input_stream.Advance(dwLen-8);
break;
}
bytesprocessed += boxlength;
}
}
}
extern "C" m_u32 sample_info(long SampleNum, TRACK_TYPES index, m_u32 *size,m_u32 *filepos)
{
m_u32 result = 0;
m_u32 pos;
*size = GetSampleSize(SampleNum,index);
if (*size==0)
return 0;
GetSampleOffset(SampleNum,index,&pos);
*filepos = pos;
return *size;
}
// return required no. of samples for 100 msec and PTS in milli sec.
extern "C" m_u32 time_to_sample(TRACK_TYPES index, m_u32 PTS)
{
m_u32 tscale,sdelta,samplenum=0;
float sample_duration,tunit;
int i,j=0;
static int extraAudio=0;
static int extraVideo=0;
for(i=0;i<MAX_TRACKS;i++)
if(TrackList[i].type == index)
break;
if (i==MAX_TRACKS)
return 0; /* track not found */
tscale = TrackList[i].pMediaHdr->GetTimescale();
pTimeToSample[index]->GetTimmingInfo(0,&sdelta);
tunit = ((float)sdelta/(float)tscale);
sample_duration = 1000 * tunit; // sample duration in milli sec.
samplenum = PTS/sample_duration;
if (samplenum>=pSampleSize[index]->GetSampleCount())
samplenum=pSampleSize[index]->GetSampleCount();
return samplenum;
};
// return required no. of samples for 100 msec and PTS in milli sec.
extern "C" m_u32 sample_timming_info(long lSampleNum, TRACK_TYPES index,m_u32 *PTS)
{
m_u32 tscale,sdelta,nsamples;
float sample_duration,tunit;
int i,j=0;
static int extraAudio=0;
static int extraVideo=0;
for(i=0;i<MAX_TRACKS;i++)
if(TrackList[i].type == index)
break;
if (i==MAX_TRACKS)
return 0; /* track not found */
tscale = TrackList[i].pMediaHdr->GetTimescale();
pTimeToSample[index]->GetTimmingInfo(lSampleNum,&sdelta);
tunit = ((float)sdelta/(float)tscale);
sample_duration = 1000 * tunit; // sample duration in milli sec.
*PTS = 1000 * (lSampleNum*tunit);
nsamples = ceil(100/sample_duration); // approximate no. of samples in 100 msec
switch(index) {
case AUDIO:
extraAudio += (nsamples*sample_duration - 100);
if(extraVideo > sample_duration && extraVideo > extraAudio) // adjust audio quanity to send
{
extraVideo -= extraAudio;
nsamples += (extraVideo/sample_duration);
extraVideo = (extraVideo % (int) sample_duration);
extraAudio = 0;
}
break;
case VIDEO:
extraVideo += (nsamples*sample_duration - 100);
if(extraAudio > sample_duration && extraAudio > extraVideo) // adjust video quanity to send
{
extraAudio -= extraVideo;
nsamples += (extraAudio/sample_duration);
extraAudio = (extraAudio % (int) sample_duration);
extraVideo = 0;
}
break;
default:
break;
}
return nsamples;
};
extern "C" m_u32 GetSampeCount(TRACK_TYPES index)
{
m_u32 count=0;
switch(index) {
case VIDEO:
if(pSampleSize[VIDEO])
count = pSampleSize[VIDEO]->GetSampleCount();
break;
case AUDIO:
if(pSampleSize[AUDIO])
count = pSampleSize[AUDIO]->GetSampleCount();
break;
default:
break;
}
return count;
}
extern "C" void GetVideoInfo(Video_Info_t *VideoInfo)
{
if(pSampleDescription[VIDEO])
{
VideoInfo->height = pSampleDescription[VIDEO]->GetVideoHeight();
VideoInfo->width = pSampleDescription[VIDEO]->GetVideoWidth();
}
}
void GetAudioInfo(m_u16 * ChannelCount, m_u16 *SampleRate)
{
pSampleDescription[AUDIO]->GetAudioInfo(ChannelCount,SampleRate);
}
extern "C" void GetConfigInfo(m_u8 *pData, long *lData)
{
if(pSampleDescription[VIDEO])
{
pSampleDescription[VIDEO]->GetConfigInfo(pData,lData);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -