Skip to content

Commit

Permalink
[demuxers/Mp4] Estimate average audio byterate when we have no exact …
Browse files Browse the repository at this point in the history
…value
  • Loading branch information
eumagga0x2a committed Jul 28, 2019
1 parent a8dc8c1 commit f8400d6
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 34 deletions.
2 changes: 1 addition & 1 deletion avidemux_plugins/ADM_demuxers/Mp4/ADM_mp4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ MP4Track::MP4Track(void)
id=0;
memset(&_rdWav,0,sizeof(_rdWav));
delay=0;

totalDataSize=0;
}
MP4Track::~MP4Track()
{
Expand Down
2 changes: 2 additions & 0 deletions avidemux_plugins/ADM_demuxers/Mp4/ADM_mp4.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class MP4Track
uint32_t extraDataSize;
uint8_t *extraData;
WAVHeader _rdWav;
uint64_t totalDataSize;
int64_t delay; // in movie scale units
int64_t startOffset; // media time in track scale units
std::vector <mp4Fragment> fragments;
Expand Down Expand Up @@ -122,6 +123,7 @@ class ADM_mp4AudioAccess : public ADM_audioAccess
#define _3GP_MAX_TRACKS 8
#define VDEO _tracks[0]
#define ADIO _tracks[nbAudioTrack+1]._rdWav
#define AUDIO_BYTERATE_UNSET 0xFFFFFFFF
/**
*
*/
Expand Down
9 changes: 2 additions & 7 deletions avidemux_plugins/ADM_demuxers/Mp4/ADM_mp4Analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ uint8_t MP4Header::parseStbl(void *ztom,uint32_t trackType,uint32_t trackScale)
// Put some defaults
ADIO.encoding=1234;
ADIO.frequency=44100;
ADIO.byterate=128000>>3;
ADIO.byterate=AUDIO_BYTERATE_UNSET;
ADIO.channels=2;
ADIO.bitspersample=16;

Expand Down Expand Up @@ -1036,12 +1036,10 @@ uint8_t MP4Header::parseStbl(void *ztom,uint32_t trackType,uint32_t trackScale)
case MKFCCR('a','c','-','3'):
case MKFCCR('s','a','c','3'):
audioCodec(AC3);
ADIO.byterate=128000>>3;

break;
case MKFCCR('e','c','-','3'):
audioCodec(EAC3);
ADIO.byterate=128000>>3;

break;
case MKFCCR('l','p','c','m'):
Expand Down Expand Up @@ -1080,7 +1078,6 @@ uint8_t MP4Header::parseStbl(void *ztom,uint32_t trackType,uint32_t trackScale)
break;
case MKFCCR('.','m','p','3'): //.mp3
audioCodec(MP3);
ADIO.byterate=128000>>3;

break;
case MKFCCR('r','a','w',' '):
Expand Down Expand Up @@ -1305,9 +1302,7 @@ uint8_t MP4Header::parseStbl(void *ztom,uint32_t trackType,uint32_t trackScale)
if(r)
{
nbo=_tracks[nbAudioTrack].nbIndex;
if(nbo)
_tracks[nbAudioTrack].nbIndex=nbo;
else
if(!nbo)
_tracks[nbAudioTrack].nbIndex=info.nbSz;
ADM_info("Indexed audio, nb blocks:%u (final)\n",_tracks[nbAudioTrack].nbIndex);
}else
Expand Down
13 changes: 8 additions & 5 deletions avidemux_plugins/ADM_demuxers/Mp4/ADM_mp4Indexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ bool MP4Header::splitAudio(MP4Track *track,MPsampleinfo *info, uint32_t trackSca
bool MP4Header::processAudio( MP4Track *track, uint32_t trackScale,
MPsampleinfo *info,uint32_t *nbOut)
{
uint64_t audioClock=0;

uint32_t totalBytes=info->SzIndentical*info->nbSz;
uint64_t totalBytes=info->SzIndentical*info->nbSz;
uint32_t totalSamples=0;
double skewFactor=1;
ADM_info("All the same size: %u (total size %u bytes)\n",info->SzIndentical,totalBytes);
ADM_info("All the same size: %u (total size %" PRIu64" bytes)\n",info->SzIndentical,totalBytes);
ADM_info("Byte per frame =%d\n",(int)info->bytePerFrame);
ADM_info("SttsC[0] = %d, sttsN[0]=%d\n",info->SttsC[0],info->SttsN[0]);

track->totalDataSize=totalBytes;

if(info->nbStts!=1)
{
Expand Down Expand Up @@ -204,7 +204,8 @@ bool MP4Header::processAudio( MP4Track *track, uint32_t trackScale,
if(info->nbCo)
track->index[0].pts=0;
ADM_info("Found %u bytes, spread over %d blocks\n",totalBytes,info->nbCo);
//
track->totalDataSize=totalBytes;

// split large chunk into smaller ones if needed
splitAudio(track,info, trackScale);

Expand Down Expand Up @@ -291,12 +292,14 @@ uint32_t i,j,cur;
aprintf("\t size for all %u frames : %u\n",info->nbSz,info->SzIndentical);
for(i=0;i<info->nbSz;i++)
track->index[i].size=info->SzIndentical;
track->totalDataSize+=info->nbSz*info->SzIndentical;
}else // Different size
{
for(i=0;i<info->nbSz;i++)
{
track->index[i].size=info->Sz[i];
aprintf("\t size : %d : %u\n",i,info->Sz[i]);
track->totalDataSize+=info->Sz[i];
}
}
// if no sample to chunk we map directly
Expand Down
56 changes: 35 additions & 21 deletions avidemux_plugins/ADM_demuxers/Mp4/ADM_mp4audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,48 @@
*/
ADM_mp4AudioAccess::ADM_mp4AudioAccess(const char *name,MP4Track *track)
{
_nb_chunks=track->nbIndex;
_fd=ADM_fopen(name,"rb");
_nb_chunks=track->nbIndex;
_fd=ADM_fopen(name,"rb");
ADM_assert(_fd);
_current_index=0;
_index=track->index;
_msg_counter = 0;
_msg_ratelimit = new ADMCountdown(200);
_msg_ratelimit->reset();
_current_index=0;
_index=track->index;
_msg_counter = 0;
_msg_ratelimit = new ADMCountdown(200);
_msg_ratelimit->reset();

extraDataLen=track->extraDataSize;
extraData=track->extraData;
extraDataLen=track->extraDataSize;
extraData=track->extraData;

// Check if MP3 track is actually MP2
if (track->_rdWav.encoding == WAV_MP3 && _nb_chunks && _index[0].size >= 4)
{
uint8_t sample[4];

fseeko(_fd, _index[0].offset, SEEK_SET);
if(fread(&sample, 1, 4, _fd) < 4) return;
// Check if MP3 track is actually MP2
if(track->_rdWav.encoding == WAV_MP3 && _nb_chunks && _index[0].size >= 4)
{
uint8_t sample[4];

uint32_t fcc = sample[0] << 24 | sample[1] << 16 | sample[2] << 8 | sample[3];
int layer = 4 - ((fcc >> 17) & 0x3);
fseeko(_fd, _index[0].offset, SEEK_SET);
if(fread(&sample, 1, 4, _fd) < 4) return;

if (layer == 2)
track->_rdWav.encoding = WAV_MP2;
}
uint32_t fcc = sample[0] << 24 | sample[1] << 16 | sample[2] << 8 | sample[3];
int layer = 4 - ((fcc >> 17) & 0x3);

if (layer == 2)
track->_rdWav.encoding = WAV_MP2;
}

if(track->_rdWav.byterate == AUDIO_BYTERATE_UNSET)
{
track->_rdWav.byterate = 128000 >> 3; // dummy value
ADM_info("Estimating audio byterate...\n");
uint64_t duration = _index[_nb_chunks-1].dts;
if(duration != ADM_NO_PTS && duration > 100000) // at least 0.1 s
{
double d = duration;
d /= 1000;
d = track->totalDataSize / d;
d *= 1000;
if(d > 0 && d < (192000 * 32)) // anything beyond 7.1 at 192 kHz float must be clearly bogus
track->_rdWav.byterate = (uint32_t)d;
}
}
}
/**
\fn ADM_mp4AudioAccess
Expand Down

0 comments on commit f8400d6

Please sign in to comment.