// Select ds from ID static demux_stream_t *demux_avi_select_stream(demuxer_t *demux, unsigned int id) { int stream_id=avi_stream_id(id); if(demux->video->id==-1) if(demux->v_streams[stream_id]) demux->video->id=stream_id; if(demux->audio->id==-1) if(demux->a_streams[stream_id]) demux->audio->id=stream_id; if(stream_id==demux->audio->id){ if(!demux->audio->sh){ sh_audio_t* sh; avi_priv_t *priv=demux->priv; sh=demux->audio->sh=demux->a_streams[stream_id]; mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected AVI audio ID = %d\n",demux->audio->id); if(sh->wf){ priv->audio_block_size=sh->wf->nBlockAlign; if(!priv->audio_block_size){ // for PCM audio we can calculate the blocksize: if(sh->format==1) priv->audio_block_size=sh->wf->nChannels*(sh->wf->wBitsPerSample/8); else priv->audio_block_size=1; // hope the best... } else { // workaround old mencoder's bug: if(sh->audio.dwSampleSize==1 && sh->audio.dwScale==1 && (sh->wf->nBlockAlign==1152 || sh->wf->nBlockAlign==576)){ mp_tmsg(MSGT_DEMUX,MSGL_WARN,"AVI: Working around CBR-MP3 nBlockAlign header bug!\n"); priv->audio_block_size=1; } } } else { priv->audio_block_size=sh->audio.dwSampleSize; } } return demux->audio; } if(stream_id==demux->video->id){ if(!demux->video->sh){ demux->video->sh=demux->v_streams[stream_id]; mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected AVI video ID = %d\n",demux->video->id); } return demux->video; } if(id!=mmioFOURCC('J','U','N','K')){ // unknown mp_msg(MSGT_DEMUX,MSGL_DBG2,"Unknown chunk: %.4s (%X)\n",(char *) &id,id); //abort(); } return NULL; }
static uint32_t avi_find_id(stream_t *stream) { uint32_t id = stream_read_dword_le(stream); if (!id) { mp_msg(MSGT_DEMUX, MSGL_WARN, "Incomplete stream? Trying resync.\n"); do { id = stream_read_dword_le(stream); if (stream_eof(stream)) return 0; } while (avi_stream_id(id) > 99); } return id; }
void print_index(AVIINDEXENTRY *idx, int idx_size, int verbose_level){ int i; unsigned int pos[256]; unsigned int num[256]; for(i=0;i<256;i++) num[i]=pos[i]=0; for(i=0;i<idx_size;i++){ int id=avi_stream_id(idx[i].ckid); if(id<0 || id>255) id=255; mp_msg(MSGT_HEADER, verbose_level, "%5d: %.4s %4X %016llX len:%6ld pos:%7d->%7.3f %7d->%7.3f\n",i, (char *)&idx[i].ckid, (unsigned int)idx[i].dwFlags&0xffff, (uint64_t)AVI_IDX_OFFSET(&idx[i]), // idx[i].dwChunkOffset+demuxer->movi_start, idx[i].dwChunkLength, pos[id],(float)pos[id]/18747.0f, num[id],(float)num[id]/23.976f ); pos[id]+=idx[i].dwChunkLength; ++num[id]; } }
// Select ds from ID static demux_stream_t *demux_avi_select_stream(demuxer_t *demux, unsigned int id) { int stream_id=avi_stream_id(id); if(demux->video->id==-1) if(demux->v_streams[stream_id]) demux->video->id=stream_id; if(demux->audio->id==-1) if(demux->a_streams[stream_id]) demux->audio->id=stream_id; if(stream_id==demux->audio->id){ if(!demux->audio->sh){ demux->audio->sh=demux->a_streams[stream_id]; mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected AVI audio ID = %d\n",demux->audio->id); update_audio_block_size(demux); } return demux->audio; } if(stream_id==demux->video->id){ if(!demux->video->sh){ demux->video->sh=demux->v_streams[stream_id]; mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected AVI video ID = %d\n",demux->video->id); } return demux->video; } if(id!=mmioFOURCC('J','U','N','K')){ // unknown mp_msg(MSGT_DEMUX,MSGL_DBG2,"Unknown chunk: %.4s (%X)\n",(char *) &id,id); //abort(); } return NULL; }
static void demux_seek_avi(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags) { avi_priv_t *priv=demuxer->priv; demux_stream_t *d_audio=demuxer->audio; demux_stream_t *d_video=demuxer->video; sh_audio_t *sh_audio=d_audio->sh; sh_video_t *sh_video=d_video->sh; float skip_audio_secs=0; //FIXME: OFF_T - Didn't check AVI case yet (avi files can't be >2G anyway?) //================= seek in AVI ========================== int rel_seek_frames=rel_seek_secs*sh_video->fps; int video_chunk_pos=d_video->pos; int i; if(flags&SEEK_ABSOLUTE){ // seek absolute video_chunk_pos=0; } if(flags&SEEK_FACTOR){ rel_seek_frames=rel_seek_secs*priv->numberofframes; } priv->skip_video_frames=0; priv->avi_audio_pts=0; // ------------ STEP 1: find nearest video keyframe chunk ------------ // find nearest video keyframe chunk pos: if(rel_seek_frames>0){ // seek forward while(video_chunk_pos<priv->idx_size-1){ int id=((AVIINDEXENTRY *)priv->idx)[video_chunk_pos].ckid; if(avi_stream_id(id)==d_video->id){ // video frame if((--rel_seek_frames)<0 && ((AVIINDEXENTRY *)priv->idx)[video_chunk_pos].dwFlags&AVIIF_KEYFRAME) break; } ++video_chunk_pos; } } else { // seek backward while(video_chunk_pos>0){ int id=((AVIINDEXENTRY *)priv->idx)[video_chunk_pos].ckid; if(avi_stream_id(id)==d_video->id){ // video frame if((++rel_seek_frames)>0 && ((AVIINDEXENTRY *)priv->idx)[video_chunk_pos].dwFlags&AVIIF_KEYFRAME) break; } --video_chunk_pos; } } priv->idx_pos_a=priv->idx_pos_v=priv->idx_pos=video_chunk_pos; // re-calc video pts: d_video->pack_no=0; for(i=0;i<video_chunk_pos;i++){ int id=((AVIINDEXENTRY *)priv->idx)[i].ckid; if(avi_stream_id(id)==d_video->id) ++d_video->pack_no; } priv->video_pack_no= sh_video->num_frames=sh_video->num_frames_decoded=d_video->pack_no; priv->avi_video_pts=d_video->pack_no*(float)sh_video->video.dwScale/(float)sh_video->video.dwRate; d_video->pos=video_chunk_pos; mp_msg(MSGT_SEEK,MSGL_DBG2,"V_SEEK: pack=%d pts=%5.3f chunk=%d \n",d_video->pack_no,priv->avi_video_pts,video_chunk_pos); // ------------ STEP 2: seek audio, find the right chunk & pos ------------ d_audio->pack_no=0; priv->audio_block_no=0; d_audio->dpos=0; if(sh_audio){ int i; int len=0; int skip_audio_bytes=0; int curr_audio_pos=-1; int audio_chunk_pos=-1; int chunk_max=(demuxer->type==DEMUXER_TYPE_AVI)?video_chunk_pos:priv->idx_size; if(sh_audio->audio.dwSampleSize){ // constant rate audio stream /* immediate seeking to audio position, including when streams are delayed */ curr_audio_pos=(priv->avi_video_pts + audio_delay)*(float)sh_audio->audio.dwRate/(float)sh_audio->audio.dwScale; curr_audio_pos*=sh_audio->audio.dwSampleSize; // find audio chunk pos: for(i=0;i<chunk_max;i++){ int id=((AVIINDEXENTRY *)priv->idx)[i].ckid; if(avi_stream_id(id)==d_audio->id){ len=((AVIINDEXENTRY *)priv->idx)[i].dwChunkLength; if(d_audio->dpos<=curr_audio_pos && curr_audio_pos<(d_audio->dpos+len)){ break; } ++d_audio->pack_no; priv->audio_block_no+=priv->audio_block_size ? ((len+priv->audio_block_size-1)/priv->audio_block_size) : 1; d_audio->dpos+=len; } } audio_chunk_pos=i; skip_audio_bytes=curr_audio_pos-d_audio->dpos; mp_msg(MSGT_SEEK,MSGL_V,"SEEK: i=%d (max:%d) dpos=%d (wanted:%d) \n", i,chunk_max,(int)d_audio->dpos,curr_audio_pos); } else { // VBR audio /* immediate seeking to audio position, including when streams are delayed */ int chunks=(priv->avi_video_pts + audio_delay)*(float)sh_audio->audio.dwRate/(float)sh_audio->audio.dwScale; audio_chunk_pos=0; // find audio chunk pos: for(i=0;i<priv->idx_size && chunks>0;i++){ int id=((AVIINDEXENTRY *)priv->idx)[i].ckid; if(avi_stream_id(id)==d_audio->id){ len=((AVIINDEXENTRY *)priv->idx)[i].dwChunkLength; if(i>chunk_max){ skip_audio_bytes+=len; } else { ++d_audio->pack_no; priv->audio_block_no+=priv->audio_block_size ? ((len+priv->audio_block_size-1)/priv->audio_block_size) : 1; d_audio->dpos+=len; audio_chunk_pos=i; } if(priv->audio_block_size) chunks-=(len+priv->audio_block_size-1)/priv->audio_block_size; } } } // Now we have: // audio_chunk_pos = chunk no in index table (it's <=chunk_max) // skip_audio_bytes = bytes to be skipped after chunk seek // d-audio->pack_no = chunk_no in stream at audio_chunk_pos // d_audio->dpos = bytepos in stream at audio_chunk_pos // let's seek! // update stream position: d_audio->pos=audio_chunk_pos; if(demuxer->type==DEMUXER_TYPE_AVI){ // interleaved stream: if(audio_chunk_pos<video_chunk_pos){ // calc priv->skip_video_frames & adjust video pts counter: for(i=audio_chunk_pos;i<video_chunk_pos;i++){ int id=((AVIINDEXENTRY *)priv->idx)[i].ckid; if(avi_stream_id(id)==d_video->id) ++priv->skip_video_frames; } // requires for correct audio pts calculation (demuxer): priv->avi_video_pts-=priv->skip_video_frames*(float)sh_video->video.dwScale/(float)sh_video->video.dwRate; priv->avi_audio_pts=priv->avi_video_pts; // set index position: priv->idx_pos_a=priv->idx_pos_v=priv->idx_pos=audio_chunk_pos; } } else { // non-interleaved stream: priv->idx_pos_a=audio_chunk_pos; priv->idx_pos_v=video_chunk_pos; priv->idx_pos=(audio_chunk_pos<video_chunk_pos)?audio_chunk_pos:video_chunk_pos; } mp_msg(MSGT_SEEK,MSGL_V,"SEEK: idx=%d (a:%d v:%d) v.skip=%d a.skip=%d/%4.3f \n", (int)priv->idx_pos,audio_chunk_pos,video_chunk_pos, (int)priv->skip_video_frames,skip_audio_bytes,skip_audio_secs); if(skip_audio_bytes){ demux_read_data(d_audio,NULL,skip_audio_bytes); } } d_video->pts=priv->avi_video_pts; // OSD }
static demuxer_t* demux_open_avi(demuxer_t* demuxer){ demux_stream_t *d_audio=demuxer->audio; demux_stream_t *d_video=demuxer->video; sh_audio_t *sh_audio=NULL; sh_video_t *sh_video=NULL; avi_priv_t* priv=calloc(1, sizeof(avi_priv_t)); demuxer->priv=(void*)priv; //---- AVI header: read_avi_header(demuxer,(demuxer->stream->flags & MP_STREAM_SEEK_BW)?index_mode:-2); if(demuxer->audio->id>=0 && !demuxer->a_streams[demuxer->audio->id]){ mp_tmsg(MSGT_DEMUX,MSGL_WARN,"AVI: invalid audio stream ID: %d - ignoring (nosound)\n",demuxer->audio->id); demuxer->audio->id=-2; // disabled } if(demuxer->video->id>=0 && !demuxer->v_streams[demuxer->video->id]){ mp_tmsg(MSGT_DEMUX,MSGL_WARN,"AVI: invalid video stream ID: %d - ignoring (using default)\n",demuxer->video->id); demuxer->video->id=-1; // autodetect } stream_reset(demuxer->stream); stream_seek(demuxer->stream,demuxer->movi_start); if(priv->idx_size>1){ // decide index format: #if 1 if((AVI_IDX_OFFSET(&((AVIINDEXENTRY *)priv->idx)[0])<demuxer->movi_start || AVI_IDX_OFFSET(&((AVIINDEXENTRY *)priv->idx)[1])<demuxer->movi_start )&& !priv->isodml) priv->idx_offset=demuxer->movi_start-4; #else if(AVI_IDX_OFFSET(&((AVIINDEXENTRY *)priv->idx)[0])<demuxer->movi_start) priv->idx_offset=demuxer->movi_start-4; #endif mp_msg(MSGT_DEMUX,MSGL_V,"AVI index offset: 0x%X (movi=0x%X idx0=0x%X idx1=0x%X)\n", (int)priv->idx_offset,(int)demuxer->movi_start, (int)((AVIINDEXENTRY *)priv->idx)[0].dwChunkOffset, (int)((AVIINDEXENTRY *)priv->idx)[1].dwChunkOffset); } if(priv->idx_size>0){ // check that file is non-interleaved: int i; off_t a_pos=-1; off_t v_pos=-1; for(i=0;i<priv->idx_size;i++){ AVIINDEXENTRY* idx=&((AVIINDEXENTRY *)priv->idx)[i]; demux_stream_t* ds=demux_avi_select_stream(demuxer,idx->ckid); off_t pos = priv->idx_offset + AVI_IDX_OFFSET(idx); if(a_pos==-1 && ds==demuxer->audio){ a_pos=pos; if(v_pos!=-1) break; } if(v_pos==-1 && ds==demuxer->video){ v_pos=pos; if(a_pos!=-1) break; } } if(v_pos==-1){ mp_msg(MSGT_DEMUX, MSGL_ERR, "AVI_NI: %s", mp_gtext("No video stream found.\n")); return NULL; } if(a_pos==-1){ d_audio->sh=sh_audio=NULL; } else { if(force_ni || abs(a_pos-v_pos)>0x100000){ // distance > 1MB mp_tmsg(MSGT_DEMUX,MSGL_INFO,"%s NON-INTERLEAVED AVI file format.\n",force_ni?"Forced":"Detected"); demuxer->type=DEMUXER_TYPE_AVI_NI; // HACK!!!! demuxer->desc=&demuxer_desc_avi_ni; // HACK!!!! pts_from_bps=1; // force BPS sync! } } } else { // no index if(force_ni){ mp_tmsg(MSGT_DEMUX,MSGL_INFO,"Using NON-INTERLEAVED broken AVI file format.\n"); demuxer->type=DEMUXER_TYPE_AVI_NINI; // HACK!!!! demuxer->desc=&demuxer_desc_avi_nini; // HACK!!!! priv->idx_pos_a= priv->idx_pos_v=demuxer->movi_start; pts_from_bps=1; // force BPS sync! } demuxer->seekable=0; } if(!ds_fill_buffer(d_video)){ mp_msg(MSGT_DEMUX, MSGL_ERR, "AVI: %s", mp_gtext("Missing video stream!? Contact the author, " "it may be a bug :(\n")); return NULL; } sh_video=d_video->sh;sh_video->ds=d_video; if(d_audio->id!=-2){ mp_msg(MSGT_DEMUX,MSGL_V,"AVI: Searching for audio stream (id:%d)\n",d_audio->id); if(!priv->audio_streams || !ds_fill_buffer(d_audio)){ mp_msg(MSGT_DEMUX, MSGL_INFO, "AVI: %s", mp_gtext("No audio stream found -> no sound.\n")); d_audio->sh=sh_audio=NULL; } else { sh_audio=d_audio->sh;sh_audio->ds=d_audio; } } // calculating audio/video bitrate: if(priv->idx_size>0){ // we have index, let's count 'em! AVIINDEXENTRY *idx = priv->idx; int64_t vsize=0; int64_t asize=0; size_t vsamples=0; size_t asamples=0; int i; for(i=0;i<priv->idx_size;i++){ int id=avi_stream_id(idx[i].ckid); unsigned len=idx[i].dwChunkLength; if(sh_video->ds->id == id) { vsize+=len; ++vsamples; } else if(sh_audio && sh_audio->ds->id == id) { asize+=len; asamples+=(len+priv->audio_block_size-1)/priv->audio_block_size; } } mp_msg(MSGT_DEMUX,MSGL_V,"AVI video size=%"PRId64" (%u) audio size=%"PRId64" (%u)\n",vsize,vsamples,asize,asamples); priv->numberofframes=vsamples; sh_video->i_bps=((float)vsize/(float)vsamples)*(float)sh_video->video.dwRate/(float)sh_video->video.dwScale; if(sh_audio) sh_audio->i_bps=((float)asize/(float)asamples)*(float)sh_audio->audio.dwRate/(float)sh_audio->audio.dwScale; } else { // guessing, results may be inaccurate: int64_t vsize; int64_t asize=0; if((priv->numberofframes=sh_video->video.dwLength)<=1) // bad video header, try to get number of frames from audio if(sh_audio && sh_audio->wf->nAvgBytesPerSec) priv->numberofframes=sh_video->fps*sh_audio->audio.dwLength/sh_audio->audio.dwRate*sh_audio->audio.dwScale; if(priv->numberofframes<=1){ mp_tmsg(MSGT_SEEK,MSGL_WARN,"Could not determine number of frames (for absolute seek).\n"); priv->numberofframes=0; } if(sh_audio){ if(sh_audio->wf->nAvgBytesPerSec && sh_audio->audio.dwSampleSize!=1){ asize=(float)sh_audio->wf->nAvgBytesPerSec*sh_audio->audio.dwLength*sh_audio->audio.dwScale/sh_audio->audio.dwRate; } else { asize=sh_audio->audio.dwLength; sh_audio->i_bps=(float)asize/(sh_video->frametime*priv->numberofframes); } } vsize=demuxer->movi_end-demuxer->movi_start-asize-8*priv->numberofframes; mp_msg(MSGT_DEMUX,MSGL_V,"AVI video size=%"PRId64" (%u) audio size=%"PRId64"\n",vsize,priv->numberofframes,asize); sh_video->i_bps=(float)vsize/(sh_video->frametime*priv->numberofframes); } return demuxer; }
static int demux_avi_read_packet(demuxer_t *demux,demux_stream_t *ds,unsigned int id,unsigned int len,int idxpos,int flags){ avi_priv_t *priv=demux->priv; int skip; float pts=0; mp_dbg(MSGT_DEMUX,MSGL_DBG3,"demux_avi.read_packet: %X\n",id); if(ds==demux->audio){ if(priv->pts_corrected==0){ if(priv->pts_has_video){ // we have video pts now float delay=0; if(((sh_audio_t*)(ds->sh))->wf->nAvgBytesPerSec) delay=(float)priv->pts_corr_bytes/((sh_audio_t*)(ds->sh))->wf->nAvgBytesPerSec; mp_msg(MSGT_DEMUX,MSGL_V,"XXX initial v_pts=%5.3f a_pos=%d (%5.3f) \n",priv->avi_audio_pts,priv->pts_corr_bytes,delay); //priv->pts_correction=-priv->avi_audio_pts+delay; priv->pts_correction=delay-priv->avi_audio_pts; priv->avi_audio_pts+=priv->pts_correction; priv->pts_corrected=1; } else priv->pts_corr_bytes+=len; } if(pts_from_bps){ pts = priv->audio_block_no * (float)((sh_audio_t*)demux->audio->sh)->audio.dwScale / (float)((sh_audio_t*)demux->audio->sh)->audio.dwRate; } else pts=priv->avi_audio_pts; //+priv->pts_correction; priv->avi_audio_pts=0; // update blockcount: priv->audio_block_no+=priv->audio_block_size ? ((len+priv->audio_block_size-1)/priv->audio_block_size) : 1; } else if(ds==demux->video){ // video if(priv->skip_video_frames>0){ // drop frame (seeking) --priv->skip_video_frames; ds=NULL; } pts = priv->avi_video_pts = priv->video_pack_no * (float)((sh_video_t*)demux->video->sh)->video.dwScale / (float)((sh_video_t*)demux->video->sh)->video.dwRate; priv->avi_audio_pts=priv->avi_video_pts+priv->pts_correction; priv->pts_has_video=1; if(ds) ++priv->video_pack_no; } skip=(len+1)&(~1); // total bytes in this chunk if(ds){ mp_dbg(MSGT_DEMUX,MSGL_DBG2,"DEMUX_AVI: Read %d data bytes from packet %04X\n",len,id); ds_read_packet(ds,demux->stream,len,pts,idxpos,flags); skip-=len; } skip = FFMAX(skip, 0); if (avi_stream_id(id) > 99 && id != mmioFOURCC('J','U','N','K')) skip = FFMIN(skip, 65536); if(skip){ mp_dbg(MSGT_DEMUX,MSGL_DBG2,"DEMUX_AVI: Skipping %d bytes from packet %04X\n",skip,id); stream_skip(demux->stream,skip); } return ds?1:0; }