2009-05-08 21:51:13 +00:00
|
|
|
/*
|
|
|
|
* AVI file parser for DEMUXER v2.9
|
|
|
|
* Copyright (c) 2001 A'rpi/ESP-team
|
|
|
|
*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-04-23 03:42:17 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2001-08-01 09:14:02 +00:00
|
|
|
#include <unistd.h>
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-08-17 00:40:25 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "mp_msg.h"
|
2001-09-26 21:35:14 +00:00
|
|
|
#include "help_mp.h"
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2007-03-15 18:36:36 +00:00
|
|
|
#include "stream/stream.h"
|
2001-04-23 03:42:17 +00:00
|
|
|
#include "demuxer.h"
|
|
|
|
#include "stheader.h"
|
|
|
|
|
2001-08-11 20:37:33 +00:00
|
|
|
#include "aviheader.h"
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2008-12-03 23:01:03 +00:00
|
|
|
demuxer_t* init_avi_with_ogg(demuxer_t* demuxer);
|
|
|
|
int demux_ogg_open(demuxer_t* demuxer);
|
2005-08-05 19:57:47 +00:00
|
|
|
|
2008-01-13 16:00:39 +00:00
|
|
|
extern const demuxer_desc_t demuxer_desc_avi_ni;
|
|
|
|
extern const demuxer_desc_t demuxer_desc_avi_nini;
|
2005-08-13 12:20:07 +00:00
|
|
|
|
2002-11-02 00:45:12 +00:00
|
|
|
// PTS: 0=interleaved 1=BPS-based
|
|
|
|
int pts_from_bps=1;
|
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
// Select ds from ID
|
2001-04-23 03:42:17 +00:00
|
|
|
demux_stream_t* demux_avi_select_stream(demuxer_t *demux,unsigned int id){
|
2001-02-24 20:28:24 +00:00
|
|
|
int stream_id=avi_stream_id(id);
|
2001-04-15 03:40:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
if(demux->video->id==-1)
|
2001-04-23 03:42:17 +00:00
|
|
|
if(demux->v_streams[stream_id])
|
2001-04-15 03:40:37 +00:00
|
|
|
demux->video->id=stream_id;
|
|
|
|
|
|
|
|
if(demux->audio->id==-1)
|
2001-04-23 03:42:17 +00:00
|
|
|
if(demux->a_streams[stream_id])
|
2001-04-15 03:40:37 +00:00
|
|
|
demux->audio->id=stream_id;
|
|
|
|
|
|
|
|
if(stream_id==demux->audio->id){
|
|
|
|
if(!demux->audio->sh){
|
2002-10-17 22:12:32 +00:00
|
|
|
sh_audio_t* sh;
|
2002-11-02 00:45:12 +00:00
|
|
|
avi_priv_t *priv=demux->priv;
|
2002-10-17 22:12:32 +00:00
|
|
|
sh=demux->audio->sh=demux->a_streams[stream_id];
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected AVI audio ID = %d\n",demux->audio->id);
|
2002-10-17 22:12:32 +00:00
|
|
|
if(sh->wf){
|
2002-11-02 00:45:12 +00:00
|
|
|
priv->audio_block_size=sh->wf->nBlockAlign;
|
|
|
|
if(!priv->audio_block_size){
|
2002-10-17 22:12:32 +00:00
|
|
|
// for PCM audio we can calculate the blocksize:
|
2002-11-02 00:45:12 +00:00
|
|
|
if(sh->format==1)
|
|
|
|
priv->audio_block_size=sh->wf->nChannels*(sh->wf->wBitsPerSample/8);
|
|
|
|
else
|
|
|
|
priv->audio_block_size=1; // hope the best...
|
2002-11-02 21:52:48 +00:00
|
|
|
} else {
|
|
|
|
// workaround old mencoder's bug:
|
|
|
|
if(sh->audio.dwSampleSize==1 && sh->audio.dwScale==1 &&
|
|
|
|
(sh->wf->nBlockAlign==1152 || sh->wf->nBlockAlign==576)){
|
2006-11-05 17:32:18 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_WARN,MSGTR_WorkAroundBlockAlignHeaderBug);
|
2002-11-02 21:52:48 +00:00
|
|
|
priv->audio_block_size=1;
|
|
|
|
}
|
2002-10-17 22:12:32 +00:00
|
|
|
}
|
2002-11-02 00:45:12 +00:00
|
|
|
} else {
|
|
|
|
priv->audio_block_size=sh->audio.dwSampleSize;
|
2002-10-17 22:12:32 +00:00
|
|
|
}
|
2001-04-15 03:40:37 +00:00
|
|
|
}
|
|
|
|
return demux->audio;
|
|
|
|
}
|
|
|
|
if(stream_id==demux->video->id){
|
|
|
|
if(!demux->video->sh){
|
2001-04-23 03:42:17 +00:00
|
|
|
demux->video->sh=demux->v_streams[stream_id];
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected AVI video ID = %d\n",demux->video->id);
|
2001-04-15 03:40:37 +00:00
|
|
|
}
|
|
|
|
return demux->video;
|
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
if(id!=mmioFOURCC('J','U','N','K')){
|
|
|
|
// unknown
|
2001-09-27 20:36:19 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_DBG2,"Unknown chunk: %.4s (%X)\n",(char *) &id,id);
|
2002-05-12 01:51:09 +00:00
|
|
|
//abort();
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-10-24 00:25:52 +00:00
|
|
|
static int valid_fourcc(unsigned int id){
|
2006-10-23 16:46:16 +00:00
|
|
|
static const char valid[] = "0123456789abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
|
2001-10-24 00:25:52 +00:00
|
|
|
unsigned char* fcc=(unsigned char*)(&id);
|
2006-10-23 16:46:16 +00:00
|
|
|
return strchr(valid, fcc[0]) && strchr(valid, fcc[1]) &&
|
|
|
|
strchr(valid, fcc[2]) && strchr(valid, fcc[3]);
|
2001-10-24 00:25:52 +00:00
|
|
|
}
|
|
|
|
|
2009-11-16 10:41:06 +00:00
|
|
|
static int valid_stream_id(unsigned int id) {
|
|
|
|
unsigned char* fcc=(unsigned char*)(&id);
|
|
|
|
return fcc[0] >= '0' && fcc[0] <= '9' && fcc[1] >= '0' && fcc[1] <= '9' &&
|
|
|
|
((fcc[2] == 'w' && fcc[3] == 'b') || (fcc[2] == 'd' && fcc[3] == 'c'));
|
|
|
|
}
|
|
|
|
|
2001-10-24 00:25:52 +00:00
|
|
|
static int choose_chunk_len(unsigned int len1,unsigned int len2){
|
|
|
|
// len1 has a bit more priority than len2. len1!=len2
|
|
|
|
// Note: this is a first-idea-logic, may be wrong. comments welcomed.
|
|
|
|
|
|
|
|
// prefer small frames rather than 0
|
|
|
|
if(!len1) return (len2>0x80000) ? len1 : len2;
|
|
|
|
if(!len2) return (len1>0x100000) ? len2 : len1;
|
|
|
|
|
|
|
|
// choose the smaller value:
|
|
|
|
return (len1<len2)? len1 : len2;
|
|
|
|
}
|
|
|
|
|
2002-08-04 01:48:35 +00:00
|
|
|
static int demux_avi_read_packet(demuxer_t *demux,demux_stream_t *ds,unsigned int id,unsigned int len,int idxpos,int flags){
|
2001-08-11 20:37:33 +00:00
|
|
|
avi_priv_t *priv=demux->priv;
|
2001-02-24 20:28:24 +00:00
|
|
|
int skip;
|
|
|
|
float pts=0;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_dbg(MSGT_DEMUX,MSGL_DBG3,"demux_avi.read_packet: %X\n",id);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
if(ds==demux->audio){
|
2001-08-11 20:37:33 +00:00
|
|
|
if(priv->pts_corrected==0){
|
|
|
|
if(priv->pts_has_video){
|
2001-08-06 23:59:22 +00:00
|
|
|
// we have video pts now
|
2001-11-11 13:25:06 +00:00
|
|
|
float delay=0;
|
|
|
|
if(((sh_audio_t*)(ds->sh))->wf->nAvgBytesPerSec)
|
|
|
|
delay=(float)priv->pts_corr_bytes/((sh_audio_t*)(ds->sh))->wf->nAvgBytesPerSec;
|
2001-08-17 00:40:25 +00:00
|
|
|
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);
|
2001-08-11 20:37:33 +00:00
|
|
|
//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;
|
2001-08-06 23:59:22 +00:00
|
|
|
} else
|
2002-11-02 00:45:12 +00:00
|
|
|
priv->pts_corr_bytes+=len;
|
2001-08-06 23:59:22 +00:00
|
|
|
}
|
2002-11-02 00:45:12 +00:00
|
|
|
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;
|
2009-05-13 02:58:57 +00:00
|
|
|
} else
|
2001-07-29 01:28:07 +00:00
|
|
|
if(ds==demux->video){
|
2001-02-24 20:28:24 +00:00
|
|
|
// video
|
2001-08-11 20:37:33 +00:00
|
|
|
if(priv->skip_video_frames>0){
|
2001-02-24 20:28:24 +00:00
|
|
|
// drop frame (seeking)
|
2001-08-11 20:37:33 +00:00
|
|
|
--priv->skip_video_frames;
|
2001-02-24 20:28:24 +00:00
|
|
|
ds=NULL;
|
|
|
|
}
|
2002-11-02 00:45:12 +00:00
|
|
|
|
|
|
|
pts = priv->avi_video_pts = priv->video_pack_no *
|
2001-07-21 00:10:09 +00:00
|
|
|
(float)((sh_video_t*)demux->video->sh)->video.dwScale /
|
|
|
|
(float)((sh_video_t*)demux->video->sh)->video.dwRate;
|
2002-11-02 00:45:12 +00:00
|
|
|
|
2001-08-11 20:37:33 +00:00
|
|
|
priv->avi_audio_pts=priv->avi_video_pts+priv->pts_correction;
|
|
|
|
priv->pts_has_video=1;
|
2001-08-06 23:59:22 +00:00
|
|
|
|
2002-11-02 00:45:12 +00:00
|
|
|
if(ds) ++priv->video_pack_no;
|
2001-08-12 20:52:35 +00:00
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
skip=(len+1)&(~1); // total bytes in this chunk
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
if(ds){
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_dbg(MSGT_DEMUX,MSGL_DBG2,"DEMUX_AVI: Read %d data bytes from packet %04X\n",len,id);
|
2001-06-03 23:37:20 +00:00
|
|
|
ds_read_packet(ds,demux->stream,len,pts,idxpos,flags);
|
2001-02-24 20:28:24 +00:00
|
|
|
skip-=len;
|
|
|
|
}
|
2009-01-01 11:39:47 +00:00
|
|
|
skip = FFMAX(skip, 0);
|
|
|
|
if (avi_stream_id(id) > 99 && id != mmioFOURCC('J','U','N','K'))
|
|
|
|
skip = FFMIN(skip, 65536);
|
2001-02-24 20:28:24 +00:00
|
|
|
if(skip){
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_dbg(MSGT_DEMUX,MSGL_DBG2,"DEMUX_AVI: Skipping %d bytes from packet %04X\n",skip,id);
|
2001-02-24 20:28:24 +00:00
|
|
|
stream_skip(demux->stream,skip);
|
|
|
|
}
|
|
|
|
return ds?1:0;
|
|
|
|
}
|
|
|
|
|
2009-01-01 11:39:47 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
// return value:
|
|
|
|
// 0 = EOF or no stream found
|
|
|
|
// 1 = successfully read a packet
|
2005-08-05 19:57:47 +00:00
|
|
|
static int demux_avi_fill_buffer(demuxer_t *demux, demux_stream_t *dsds){
|
2001-08-11 20:37:33 +00:00
|
|
|
avi_priv_t *priv=demux->priv;
|
2001-02-24 20:28:24 +00:00
|
|
|
unsigned int id=0;
|
|
|
|
unsigned int len;
|
|
|
|
int ret=0;
|
2002-08-04 01:48:35 +00:00
|
|
|
demux_stream_t *ds;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
do{
|
2003-02-02 21:32:05 +00:00
|
|
|
int flags=1;
|
2001-02-24 20:28:24 +00:00
|
|
|
AVIINDEXENTRY *idx=NULL;
|
2001-08-11 20:37:33 +00:00
|
|
|
if(priv->idx_size>0 && priv->idx_pos<priv->idx_size){
|
2002-05-12 01:51:09 +00:00
|
|
|
off_t pos;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-08-11 20:37:33 +00:00
|
|
|
idx=&((AVIINDEXENTRY *)priv->idx)[priv->idx_pos++];
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
if(idx->dwFlags&AVIIF_LIST){
|
2009-11-16 10:41:06 +00:00
|
|
|
if (!valid_stream_id(idx->ckid))
|
2001-02-24 20:28:24 +00:00
|
|
|
// LIST
|
|
|
|
continue;
|
2009-11-16 10:41:06 +00:00
|
|
|
if (!priv->warned_unaligned)
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_WARN, "Looks like unaligned chunk in index, broken AVI file!\n");
|
|
|
|
priv->warned_unaligned = 1;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
if(!demux_avi_select_stream(demux,idx->ckid)){
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_dbg(MSGT_DEMUX,MSGL_DBG3,"Skip chunk %.4s (0x%X) \n",(char *)&idx->ckid,(unsigned int)idx->ckid);
|
2001-02-24 20:28:24 +00:00
|
|
|
continue; // skip this chunk
|
|
|
|
}
|
|
|
|
|
2004-03-17 14:50:37 +00:00
|
|
|
pos = (off_t)priv->idx_offset+AVI_IDX_OFFSET(idx);
|
2004-02-17 12:30:45 +00:00
|
|
|
if((pos<demux->movi_start || pos>=demux->movi_end) && (demux->movi_end>demux->movi_start) && (demux->stream->flags & STREAM_SEEK)){
|
2006-01-12 20:04:36 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"ChunkOffset out of range! idx=0x%"PRIX64" \n",(int64_t)pos);
|
2001-02-24 20:28:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
stream_seek(demux->stream,pos);
|
2001-07-29 21:07:34 +00:00
|
|
|
demux->filepos=stream_tell(demux->stream);
|
2001-02-24 20:28:24 +00:00
|
|
|
id=stream_read_dword_le(demux->stream);
|
2001-07-29 21:07:34 +00:00
|
|
|
if(stream_eof(demux->stream)) return 0; // EOF!
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
if(id!=idx->ckid){
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id,(char *)&idx->ckid);
|
2001-10-24 00:25:52 +00:00
|
|
|
if(valid_fourcc(idx->ckid))
|
|
|
|
id=idx->ckid; // use index if valid
|
|
|
|
else
|
|
|
|
if(!valid_fourcc(id)) continue; // drop chunk if both id and idx bad
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
len=stream_read_dword_le(demux->stream);
|
|
|
|
if((len!=idx->dwChunkLength)&&((len+1)!=idx->dwChunkLength)){
|
2006-01-12 20:04:36 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"ChunkSize mismatch! raw=%d idx=%d \n",len,idx->dwChunkLength);
|
2001-10-24 00:25:52 +00:00
|
|
|
if(len>0x200000 && idx->dwChunkLength>0x200000) continue; // both values bad :(
|
|
|
|
len=choose_chunk_len(idx->dwChunkLength,len);
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2003-02-02 21:32:05 +00:00
|
|
|
if(!(idx->dwFlags&AVIIF_KEYFRAME)) flags=0;
|
2001-02-24 20:28:24 +00:00
|
|
|
} else {
|
2001-07-29 21:07:34 +00:00
|
|
|
demux->filepos=stream_tell(demux->stream);
|
2004-02-17 12:30:45 +00:00
|
|
|
if(demux->filepos>=demux->movi_end && demux->movi_end>demux->movi_start && (demux->stream->flags & STREAM_SEEK)){
|
2001-07-29 21:07:34 +00:00
|
|
|
demux->stream->eof=1;
|
|
|
|
return 0;
|
|
|
|
}
|
2009-01-01 11:39:47 +00:00
|
|
|
id=avi_find_id(demux->stream);
|
2001-02-24 20:28:24 +00:00
|
|
|
len=stream_read_dword_le(demux->stream);
|
2001-07-29 21:07:34 +00:00
|
|
|
if(stream_eof(demux->stream)) return 0; // EOF!
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2002-05-12 01:51:09 +00:00
|
|
|
if(id==mmioFOURCC('L','I','S','T') || id==mmioFOURCC('R', 'I', 'F', 'F')){
|
|
|
|
id=stream_read_dword_le(demux->stream); // list or RIFF type
|
2001-02-24 20:28:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2002-08-04 01:48:35 +00:00
|
|
|
|
|
|
|
ds=demux_avi_select_stream(demux,id);
|
|
|
|
if(ds)
|
|
|
|
if(ds->packs+1>=MAX_PACKS || ds->bytes+len>=MAX_PACK_BYTES){
|
|
|
|
// this packet will cause a buffer overflow, switch to -ni mode!!!
|
|
|
|
mp_msg(MSGT_DEMUX,MSGL_WARN,MSGTR_SwitchToNi);
|
|
|
|
if(priv->idx_size>0){
|
|
|
|
// has index
|
|
|
|
demux->type=DEMUXER_TYPE_AVI_NI;
|
2005-08-13 12:20:07 +00:00
|
|
|
demux->desc=&demuxer_desc_avi_ni;
|
2002-08-04 01:48:35 +00:00
|
|
|
--priv->idx_pos; // hack
|
|
|
|
} else {
|
|
|
|
// no index
|
|
|
|
demux->type=DEMUXER_TYPE_AVI_NINI;
|
2005-08-13 12:20:07 +00:00
|
|
|
demux->desc=&demuxer_desc_avi_nini;
|
2002-08-04 01:48:35 +00:00
|
|
|
priv->idx_pos=demux->filepos; // hack
|
|
|
|
}
|
|
|
|
priv->idx_pos_v=priv->idx_pos_a=priv->idx_pos;
|
|
|
|
// quit now, we can't even (no enough buffer memory) read this packet :(
|
|
|
|
return -1;
|
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2002-08-04 01:48:35 +00:00
|
|
|
ret=demux_avi_read_packet(demux,ds,id,len,priv->idx_pos-1,flags);
|
2001-02-24 20:28:24 +00:00
|
|
|
} while(ret!=1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// return value:
|
|
|
|
// 0 = EOF or no stream found
|
|
|
|
// 1 = successfully read a packet
|
|
|
|
int demux_avi_fill_buffer_ni(demuxer_t *demux,demux_stream_t* ds){
|
2001-08-11 20:37:33 +00:00
|
|
|
avi_priv_t *priv=demux->priv;
|
2001-02-24 20:28:24 +00:00
|
|
|
unsigned int id=0;
|
|
|
|
unsigned int len;
|
|
|
|
int ret=0;
|
|
|
|
|
|
|
|
do{
|
2003-02-02 21:32:05 +00:00
|
|
|
int flags=1;
|
2001-02-24 20:28:24 +00:00
|
|
|
AVIINDEXENTRY *idx=NULL;
|
|
|
|
int idx_pos=0;
|
|
|
|
demux->filepos=stream_tell(demux->stream);
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-08-11 20:37:33 +00:00
|
|
|
if(ds==demux->video) idx_pos=priv->idx_pos_v++; else
|
|
|
|
if(ds==demux->audio) idx_pos=priv->idx_pos_a++; else
|
|
|
|
idx_pos=priv->idx_pos++;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-08-11 20:37:33 +00:00
|
|
|
if(priv->idx_size>0 && idx_pos<priv->idx_size){
|
2002-05-12 01:51:09 +00:00
|
|
|
off_t pos;
|
2001-08-11 20:37:33 +00:00
|
|
|
idx=&((AVIINDEXENTRY *)priv->idx)[idx_pos];
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
if(idx->dwFlags&AVIIF_LIST){
|
2009-11-16 10:41:06 +00:00
|
|
|
if (!valid_stream_id(idx->ckid))
|
2001-02-24 20:28:24 +00:00
|
|
|
// LIST
|
|
|
|
continue;
|
2009-11-16 10:41:06 +00:00
|
|
|
if (!priv->warned_unaligned)
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_WARN, "Looks like unaligned chunk in index, broken AVI file!\n");
|
|
|
|
priv->warned_unaligned = 1;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
if(ds && demux_avi_select_stream(demux,idx->ckid)!=ds){
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_dbg(MSGT_DEMUX,MSGL_DBG3,"Skip chunk %.4s (0x%X) \n",(char *)&idx->ckid,(unsigned int)idx->ckid);
|
2001-02-24 20:28:24 +00:00
|
|
|
continue; // skip this chunk
|
|
|
|
}
|
|
|
|
|
2004-03-17 14:50:37 +00:00
|
|
|
pos = priv->idx_offset+AVI_IDX_OFFSET(idx);
|
2001-10-24 00:50:54 +00:00
|
|
|
if((pos<demux->movi_start || pos>=demux->movi_end) && (demux->movi_end>demux->movi_start)){
|
2006-01-12 20:04:36 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"ChunkOffset out of range! current=0x%"PRIX64" idx=0x%"PRIX64" \n",(int64_t)demux->filepos,(int64_t)pos);
|
2001-02-24 20:28:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
stream_seek(demux->stream,pos);
|
|
|
|
|
|
|
|
id=stream_read_dword_le(demux->stream);
|
|
|
|
|
|
|
|
if(stream_eof(demux->stream)) return 0;
|
|
|
|
|
|
|
|
if(id!=idx->ckid){
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id,(char *)&idx->ckid);
|
2001-10-24 00:25:52 +00:00
|
|
|
if(valid_fourcc(idx->ckid))
|
|
|
|
id=idx->ckid; // use index if valid
|
|
|
|
else
|
|
|
|
if(!valid_fourcc(id)) continue; // drop chunk if both id and idx bad
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
len=stream_read_dword_le(demux->stream);
|
|
|
|
if((len!=idx->dwChunkLength)&&((len+1)!=idx->dwChunkLength)){
|
2006-01-12 20:04:36 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"ChunkSize mismatch! raw=%d idx=%d \n",len,idx->dwChunkLength);
|
2001-10-24 00:25:52 +00:00
|
|
|
if(len>0x200000 && idx->dwChunkLength>0x200000) continue; // both values bad :(
|
|
|
|
len=choose_chunk_len(idx->dwChunkLength,len);
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2003-02-02 21:32:05 +00:00
|
|
|
if(!(idx->dwFlags&AVIIF_KEYFRAME)) flags=0;
|
2001-02-24 20:28:24 +00:00
|
|
|
} else return 0;
|
2002-08-04 01:48:35 +00:00
|
|
|
ret=demux_avi_read_packet(demux,demux_avi_select_stream(demux,id),id,len,idx_pos,flags);
|
2001-02-24 20:28:24 +00:00
|
|
|
} while(ret!=1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// return value:
|
|
|
|
// 0 = EOF or no stream found
|
|
|
|
// 1 = successfully read a packet
|
|
|
|
int demux_avi_fill_buffer_nini(demuxer_t *demux,demux_stream_t* ds){
|
2001-08-11 20:37:33 +00:00
|
|
|
avi_priv_t *priv=demux->priv;
|
2001-02-24 20:28:24 +00:00
|
|
|
unsigned int id=0;
|
|
|
|
unsigned int len;
|
|
|
|
int ret=0;
|
2002-05-12 01:51:09 +00:00
|
|
|
off_t *fpos=NULL;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-08-11 20:37:33 +00:00
|
|
|
if(ds==demux->video) fpos=&priv->idx_pos_v; else
|
|
|
|
if(ds==demux->audio) fpos=&priv->idx_pos_a; else
|
2001-02-24 20:28:24 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
stream_seek(demux->stream,fpos[0]);
|
|
|
|
|
|
|
|
do{
|
|
|
|
|
|
|
|
demux->filepos=stream_tell(demux->stream);
|
2001-10-24 00:50:54 +00:00
|
|
|
if(demux->filepos>=demux->movi_end && (demux->movi_end>demux->movi_start)){
|
2002-08-04 01:48:35 +00:00
|
|
|
ds->eof=1;
|
2001-02-24 20:28:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-01 11:39:47 +00:00
|
|
|
id=avi_find_id(demux->stream);
|
2001-02-24 20:28:24 +00:00
|
|
|
len=stream_read_dword_le(demux->stream);
|
2002-08-04 01:48:35 +00:00
|
|
|
|
|
|
|
if(stream_eof(demux->stream)) return 0;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
if(id==mmioFOURCC('L','I','S','T')){
|
|
|
|
id=stream_read_dword_le(demux->stream); // list type
|
|
|
|
continue;
|
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2002-05-12 01:51:09 +00:00
|
|
|
if(id==mmioFOURCC('R','I','F','F')){
|
2002-08-04 01:48:35 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"additional RIFF header...\n");
|
2002-05-12 01:51:09 +00:00
|
|
|
id=stream_read_dword_le(demux->stream); // "AVIX"
|
|
|
|
continue;
|
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
if(ds==demux_avi_select_stream(demux,id)){
|
|
|
|
// read it!
|
2002-08-04 01:48:35 +00:00
|
|
|
ret=demux_avi_read_packet(demux,ds,id,len,priv->idx_pos-1,0);
|
2001-02-24 20:28:24 +00:00
|
|
|
} else {
|
|
|
|
// skip it!
|
|
|
|
int skip=(len+1)&(~1); // total bytes in this chunk
|
|
|
|
stream_skip(demux->stream,skip);
|
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
} while(ret!=1);
|
|
|
|
fpos[0]=stream_tell(demux->stream);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-10-20 20:03:10 +00:00
|
|
|
// AVI demuxer parameters:
|
2008-02-09 13:13:02 +00:00
|
|
|
int index_mode=-1; // -1=untouched 0=don't use index 1=use (generate) index
|
2003-10-22 19:01:37 +00:00
|
|
|
char *index_file_save = NULL, *index_file_load = NULL;
|
2001-10-20 20:03:10 +00:00
|
|
|
int force_ni=0; // force non-interleaved AVI parsing
|
|
|
|
|
2001-10-02 21:45:41 +00:00
|
|
|
void read_avi_header(demuxer_t *demuxer,int index_mode);
|
|
|
|
|
2005-08-05 19:57:47 +00:00
|
|
|
static demuxer_t* demux_open_avi(demuxer_t* demuxer){
|
2001-08-11 20:37:33 +00:00
|
|
|
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;
|
2009-11-16 10:44:00 +00:00
|
|
|
avi_priv_t* priv=calloc(1, sizeof(avi_priv_t));
|
2004-03-17 14:50:37 +00:00
|
|
|
|
2001-08-11 20:37:33 +00:00
|
|
|
demuxer->priv=(void*)priv;
|
|
|
|
|
|
|
|
//---- AVI header:
|
2004-03-13 16:10:02 +00:00
|
|
|
read_avi_header(demuxer,(demuxer->stream->flags & STREAM_SEEK_BW)?index_mode:-2);
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2002-09-25 22:19:11 +00:00
|
|
|
if(demuxer->audio->id>=0 && !demuxer->a_streams[demuxer->audio->id]){
|
2006-11-05 16:36:02 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_WARN,MSGTR_InvalidAudioStreamNosound,demuxer->audio->id);
|
2002-09-25 22:19:11 +00:00
|
|
|
demuxer->audio->id=-2; // disabled
|
|
|
|
}
|
|
|
|
if(demuxer->video->id>=0 && !demuxer->v_streams[demuxer->video->id]){
|
2006-11-05 16:36:02 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_WARN,MSGTR_InvalidAudioStreamUsingDefault,demuxer->video->id);
|
2002-09-25 22:19:11 +00:00
|
|
|
demuxer->video->id=-1; // autodetect
|
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-08-11 20:37:33 +00:00
|
|
|
stream_reset(demuxer->stream);
|
|
|
|
stream_seek(demuxer->stream,demuxer->movi_start);
|
2002-02-12 08:27:49 +00:00
|
|
|
if(priv->idx_size>1){
|
2001-08-11 20:37:33 +00:00
|
|
|
// decide index format:
|
2002-02-12 08:27:49 +00:00
|
|
|
#if 1
|
2004-03-17 14:50:37 +00:00
|
|
|
if((AVI_IDX_OFFSET(&((AVIINDEXENTRY *)priv->idx)[0])<demuxer->movi_start ||
|
|
|
|
AVI_IDX_OFFSET(&((AVIINDEXENTRY *)priv->idx)[1])<demuxer->movi_start )&& !priv->isodml)
|
2002-02-12 08:27:49 +00:00
|
|
|
priv->idx_offset=demuxer->movi_start-4;
|
|
|
|
#else
|
2004-03-17 14:50:37 +00:00
|
|
|
if(AVI_IDX_OFFSET(&((AVIINDEXENTRY *)priv->idx)[0])<demuxer->movi_start)
|
2001-08-11 20:37:33 +00:00
|
|
|
priv->idx_offset=demuxer->movi_start-4;
|
2002-02-12 08:27:49 +00:00
|
|
|
#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);
|
2001-08-11 20:37:33 +00:00
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-08-11 20:37:33 +00:00
|
|
|
if(priv->idx_size>0){
|
|
|
|
// check that file is non-interleaved:
|
|
|
|
int i;
|
2002-05-12 01:51:09 +00:00
|
|
|
off_t a_pos=-1;
|
|
|
|
off_t v_pos=-1;
|
2001-08-11 20:37:33 +00:00
|
|
|
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);
|
2004-03-17 14:50:37 +00:00
|
|
|
off_t pos = priv->idx_offset + AVI_IDX_OFFSET(idx);
|
2001-08-11 20:37:33 +00:00
|
|
|
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){
|
2001-09-26 21:35:14 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_ERR,"AVI_NI: " MSGTR_MissingVideoStream);
|
2001-08-11 20:37:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if(a_pos==-1){
|
2006-10-21 20:48:41 +00:00
|
|
|
d_audio->sh=sh_audio=NULL;
|
2001-08-11 20:37:33 +00:00
|
|
|
} else {
|
|
|
|
if(force_ni || abs(a_pos-v_pos)>0x100000){ // distance > 1MB
|
2001-09-26 21:35:14 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_INFO,MSGTR_NI_Message,force_ni?MSGTR_NI_Forced:MSGTR_NI_Detected);
|
2001-08-11 20:37:33 +00:00
|
|
|
demuxer->type=DEMUXER_TYPE_AVI_NI; // HACK!!!!
|
2005-08-13 12:20:07 +00:00
|
|
|
demuxer->desc=&demuxer_desc_avi_ni; // HACK!!!!
|
2001-08-11 20:37:33 +00:00
|
|
|
pts_from_bps=1; // force BPS sync!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// no index
|
|
|
|
if(force_ni){
|
2001-09-26 21:35:14 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_INFO,MSGTR_UsingNINI);
|
2001-08-11 20:37:33 +00:00
|
|
|
demuxer->type=DEMUXER_TYPE_AVI_NINI; // HACK!!!!
|
2005-08-13 12:20:07 +00:00
|
|
|
demuxer->desc=&demuxer_desc_avi_nini; // HACK!!!!
|
2001-08-11 20:37:33 +00:00
|
|
|
priv->idx_pos_a=
|
|
|
|
priv->idx_pos_v=demuxer->movi_start;
|
|
|
|
pts_from_bps=1; // force BPS sync!
|
|
|
|
}
|
2001-08-22 16:42:37 +00:00
|
|
|
demuxer->seekable=0;
|
2001-08-11 20:37:33 +00:00
|
|
|
}
|
|
|
|
if(!ds_fill_buffer(d_video)){
|
2001-09-26 21:35:14 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_ERR,"AVI: " MSGTR_MissingVideoStreamBug);
|
2001-08-11 20:37:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
sh_video=d_video->sh;sh_video->ds=d_video;
|
2001-08-12 15:46:09 +00:00
|
|
|
if(d_audio->id!=-2){
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"AVI: Searching for audio stream (id:%d)\n",d_audio->id);
|
2001-10-21 12:18:45 +00:00
|
|
|
if(!priv->audio_streams || !ds_fill_buffer(d_audio)){
|
2001-09-26 21:35:14 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_INFO,"AVI: " MSGTR_MissingAudioStream);
|
2006-10-21 20:48:41 +00:00
|
|
|
d_audio->sh=sh_audio=NULL;
|
2001-08-11 20:37:33 +00:00
|
|
|
} else {
|
|
|
|
sh_audio=d_audio->sh;sh_audio->ds=d_audio;
|
|
|
|
}
|
|
|
|
}
|
2002-12-21 22:11:48 +00:00
|
|
|
|
|
|
|
// calculating audio/video bitrate:
|
|
|
|
if(priv->idx_size>0){
|
|
|
|
// we have index, let's count 'em!
|
2006-05-06 06:23:23 +00:00
|
|
|
int64_t vsize=0;
|
|
|
|
int64_t asize=0;
|
2002-12-21 22:11:48 +00:00
|
|
|
size_t vsamples=0;
|
|
|
|
size_t asamples=0;
|
|
|
|
int i;
|
2009-05-13 02:58:57 +00:00
|
|
|
for(i=0;i<priv->idx_size;i++){
|
2002-12-21 22:11:48 +00:00
|
|
|
int id=avi_stream_id(((AVIINDEXENTRY *)priv->idx)[i].ckid);
|
|
|
|
int len=((AVIINDEXENTRY *)priv->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;
|
|
|
|
}
|
|
|
|
}
|
2006-05-06 06:23:23 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"AVI video size=%"PRId64" (%u) audio size=%"PRId64" (%u)\n",vsize,vsamples,asize,asamples);
|
2002-12-21 22:11:48 +00:00
|
|
|
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:
|
2006-05-06 06:23:23 +00:00
|
|
|
int64_t vsize;
|
|
|
|
int64_t asize=0;
|
2002-12-21 22:11:48 +00:00
|
|
|
|
|
|
|
if((priv->numberofframes=sh_video->video.dwLength)<=1)
|
|
|
|
// bad video header, try to get number of frames from audio
|
2002-12-29 21:20:45 +00:00
|
|
|
if(sh_audio && sh_audio->wf->nAvgBytesPerSec) priv->numberofframes=sh_video->fps*sh_audio->audio.dwLength/sh_audio->audio.dwRate*sh_audio->audio.dwScale;
|
2002-12-21 22:11:48 +00:00
|
|
|
if(priv->numberofframes<=1){
|
|
|
|
mp_msg(MSGT_SEEK,MSGL_WARN,MSGTR_CouldntDetFNo);
|
|
|
|
priv->numberofframes=0;
|
2009-05-13 02:58:57 +00:00
|
|
|
}
|
2002-12-21 22:11:48 +00:00
|
|
|
|
|
|
|
if(sh_audio){
|
|
|
|
if(sh_audio->wf->nAvgBytesPerSec && sh_audio->audio.dwSampleSize!=1){
|
2002-12-29 21:20:45 +00:00
|
|
|
asize=(float)sh_audio->wf->nAvgBytesPerSec*sh_audio->audio.dwLength*sh_audio->audio.dwScale/sh_audio->audio.dwRate;
|
2002-12-21 22:11:48 +00:00
|
|
|
} 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;
|
2006-05-06 06:23:23 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"AVI video size=%"PRId64" (%u) audio size=%"PRId64"\n",vsize,priv->numberofframes,asize);
|
2002-12-21 22:11:48 +00:00
|
|
|
sh_video->i_bps=(float)vsize/(sh_video->frametime*priv->numberofframes);
|
|
|
|
}
|
2006-02-18 09:26:39 +00:00
|
|
|
|
2001-08-11 20:37:33 +00:00
|
|
|
return demuxer;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-08-11 20:37:33 +00:00
|
|
|
}
|
|
|
|
|
2001-08-08 19:37:45 +00:00
|
|
|
|
2006-02-17 01:57:41 +00:00
|
|
|
void demux_seek_avi(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags){
|
2001-08-11 20:37:33 +00:00
|
|
|
avi_priv_t *priv=demuxer->priv;
|
2001-08-08 19:37:45 +00:00
|
|
|
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;
|
2001-08-22 19:03:15 +00:00
|
|
|
|
2008-01-29 15:11:38 +00:00
|
|
|
if(flags&SEEK_ABSOLUTE){
|
2001-08-22 19:03:15 +00:00
|
|
|
// seek absolute
|
|
|
|
video_chunk_pos=0;
|
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2008-01-29 15:11:38 +00:00
|
|
|
if(flags&SEEK_FACTOR){
|
2002-11-16 03:42:14 +00:00
|
|
|
rel_seek_frames=rel_seek_secs*priv->numberofframes;
|
2001-08-22 19:03:15 +00:00
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-08-11 20:37:33 +00:00
|
|
|
priv->skip_video_frames=0;
|
|
|
|
priv->avi_audio_pts=0;
|
2001-08-08 19:37:45 +00:00
|
|
|
|
2001-08-11 00:53:45 +00:00
|
|
|
// ------------ STEP 1: find nearest video keyframe chunk ------------
|
2001-08-08 19:37:45 +00:00
|
|
|
// find nearest video keyframe chunk pos:
|
|
|
|
if(rel_seek_frames>0){
|
|
|
|
// seek forward
|
2002-08-14 21:25:01 +00:00
|
|
|
while(video_chunk_pos<priv->idx_size-1){
|
2001-08-11 20:37:33 +00:00
|
|
|
int id=((AVIINDEXENTRY *)priv->idx)[video_chunk_pos].ckid;
|
2001-08-08 19:37:45 +00:00
|
|
|
if(avi_stream_id(id)==d_video->id){ // video frame
|
2001-08-11 20:37:33 +00:00
|
|
|
if((--rel_seek_frames)<0 && ((AVIINDEXENTRY *)priv->idx)[video_chunk_pos].dwFlags&AVIIF_KEYFRAME) break;
|
2001-08-08 19:37:45 +00:00
|
|
|
}
|
|
|
|
++video_chunk_pos;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// seek backward
|
2001-08-12 20:52:35 +00:00
|
|
|
while(video_chunk_pos>0){
|
2001-08-11 20:37:33 +00:00
|
|
|
int id=((AVIINDEXENTRY *)priv->idx)[video_chunk_pos].ckid;
|
2001-08-08 19:37:45 +00:00
|
|
|
if(avi_stream_id(id)==d_video->id){ // video frame
|
2001-08-11 20:37:33 +00:00
|
|
|
if((++rel_seek_frames)>0 && ((AVIINDEXENTRY *)priv->idx)[video_chunk_pos].dwFlags&AVIIF_KEYFRAME) break;
|
2001-08-08 19:37:45 +00:00
|
|
|
}
|
|
|
|
--video_chunk_pos;
|
|
|
|
}
|
|
|
|
}
|
2001-08-11 20:37:33 +00:00
|
|
|
priv->idx_pos_a=priv->idx_pos_v=priv->idx_pos=video_chunk_pos;
|
2001-08-08 19:37:45 +00:00
|
|
|
|
|
|
|
// re-calc video pts:
|
|
|
|
d_video->pack_no=0;
|
|
|
|
for(i=0;i<video_chunk_pos;i++){
|
2001-08-11 20:37:33 +00:00
|
|
|
int id=((AVIINDEXENTRY *)priv->idx)[i].ckid;
|
2001-08-08 19:37:45 +00:00
|
|
|
if(avi_stream_id(id)==d_video->id) ++d_video->pack_no;
|
|
|
|
}
|
2002-11-02 00:45:12 +00:00
|
|
|
priv->video_pack_no=
|
2001-08-16 01:03:51 +00:00
|
|
|
sh_video->num_frames=sh_video->num_frames_decoded=d_video->pack_no;
|
2001-08-11 20:37:33 +00:00
|
|
|
priv->avi_video_pts=d_video->pack_no*(float)sh_video->video.dwScale/(float)sh_video->video.dwRate;
|
2001-08-11 00:53:45 +00:00
|
|
|
d_video->pos=video_chunk_pos;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-08-17 00:40:25 +00:00
|
|
|
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);
|
2001-08-11 00:53:45 +00:00
|
|
|
|
|
|
|
// ------------ STEP 2: seek audio, find the right chunk & pos ------------
|
|
|
|
|
|
|
|
d_audio->pack_no=0;
|
2002-11-02 00:45:12 +00:00
|
|
|
priv->audio_block_no=0;
|
2001-08-11 00:53:45 +00:00
|
|
|
d_audio->dpos=0;
|
2001-08-08 19:37:45 +00:00
|
|
|
|
|
|
|
if(sh_audio){
|
|
|
|
int i;
|
|
|
|
int len=0;
|
|
|
|
int skip_audio_bytes=0;
|
2001-08-11 00:53:45 +00:00
|
|
|
int curr_audio_pos=-1;
|
|
|
|
int audio_chunk_pos=-1;
|
2001-08-11 20:37:33 +00:00
|
|
|
int chunk_max=(demuxer->type==DEMUXER_TYPE_AVI)?video_chunk_pos:priv->idx_size;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-08-11 00:53:45 +00:00
|
|
|
if(sh_audio->audio.dwSampleSize){
|
|
|
|
// constant rate audio stream
|
2006-02-17 01:57:41 +00:00
|
|
|
/* 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;
|
2001-08-11 00:53:45 +00:00
|
|
|
curr_audio_pos*=sh_audio->audio.dwSampleSize;
|
2001-08-08 19:37:45 +00:00
|
|
|
|
|
|
|
// find audio chunk pos:
|
2001-08-11 00:53:45 +00:00
|
|
|
for(i=0;i<chunk_max;i++){
|
2001-08-11 20:37:33 +00:00
|
|
|
int id=((AVIINDEXENTRY *)priv->idx)[i].ckid;
|
2001-08-08 19:37:45 +00:00
|
|
|
if(avi_stream_id(id)==d_audio->id){
|
2001-08-11 20:37:33 +00:00
|
|
|
len=((AVIINDEXENTRY *)priv->idx)[i].dwChunkLength;
|
2001-08-11 00:53:45 +00:00
|
|
|
if(d_audio->dpos<=curr_audio_pos && curr_audio_pos<(d_audio->dpos+len)){
|
2001-08-08 19:37:45 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-11-02 00:45:12 +00:00
|
|
|
++d_audio->pack_no;
|
|
|
|
priv->audio_block_no+=priv->audio_block_size ?
|
|
|
|
((len+priv->audio_block_size-1)/priv->audio_block_size) : 1;
|
2001-08-11 00:53:45 +00:00
|
|
|
d_audio->dpos+=len;
|
2001-08-08 19:37:45 +00:00
|
|
|
}
|
|
|
|
}
|
2002-07-04 22:17:30 +00:00
|
|
|
audio_chunk_pos=i;
|
2001-08-11 00:53:45 +00:00
|
|
|
skip_audio_bytes=curr_audio_pos-d_audio->dpos;
|
2001-08-08 19:37:45 +00:00
|
|
|
|
2002-07-04 22:17:30 +00:00
|
|
|
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);
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-08-11 00:53:45 +00:00
|
|
|
} else {
|
|
|
|
// VBR audio
|
2006-02-17 01:57:41 +00:00
|
|
|
/* 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;
|
2001-08-11 00:53:45 +00:00
|
|
|
audio_chunk_pos=0;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-08-11 00:53:45 +00:00
|
|
|
// find audio chunk pos:
|
2001-08-11 20:37:33 +00:00
|
|
|
for(i=0;i<priv->idx_size && chunks>0;i++){
|
|
|
|
int id=((AVIINDEXENTRY *)priv->idx)[i].ckid;
|
2001-08-11 00:53:45 +00:00
|
|
|
if(avi_stream_id(id)==d_audio->id){
|
2001-08-11 20:37:33 +00:00
|
|
|
len=((AVIINDEXENTRY *)priv->idx)[i].dwChunkLength;
|
2001-08-11 00:53:45 +00:00
|
|
|
if(i>chunk_max){
|
|
|
|
skip_audio_bytes+=len;
|
|
|
|
} else {
|
|
|
|
++d_audio->pack_no;
|
2002-11-02 00:45:12 +00:00
|
|
|
priv->audio_block_no+=priv->audio_block_size ?
|
|
|
|
((len+priv->audio_block_size-1)/priv->audio_block_size) : 1;
|
2001-08-11 00:53:45 +00:00
|
|
|
d_audio->dpos+=len;
|
|
|
|
audio_chunk_pos=i;
|
|
|
|
}
|
2002-11-02 00:45:12 +00:00
|
|
|
if(priv->audio_block_size)
|
|
|
|
chunks-=(len+priv->audio_block_size-1)/priv->audio_block_size;
|
2001-08-11 00:53:45 +00:00
|
|
|
}
|
2001-08-08 19:37:45 +00:00
|
|
|
}
|
2001-08-11 00:53:45 +00:00
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-08-11 00:53:45 +00:00
|
|
|
// 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!
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-08-11 00:53:45 +00:00
|
|
|
// update stream position:
|
|
|
|
d_audio->pos=audio_chunk_pos;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-08-11 00:53:45 +00:00
|
|
|
if(demuxer->type==DEMUXER_TYPE_AVI){
|
|
|
|
// interleaved stream:
|
|
|
|
if(audio_chunk_pos<video_chunk_pos){
|
2001-08-11 20:37:33 +00:00
|
|
|
// calc priv->skip_video_frames & adjust video pts counter:
|
2001-08-11 00:53:45 +00:00
|
|
|
for(i=audio_chunk_pos;i<video_chunk_pos;i++){
|
2001-08-11 20:37:33 +00:00
|
|
|
int id=((AVIINDEXENTRY *)priv->idx)[i].ckid;
|
|
|
|
if(avi_stream_id(id)==d_video->id) ++priv->skip_video_frames;
|
2001-08-11 00:53:45 +00:00
|
|
|
}
|
|
|
|
// requires for correct audio pts calculation (demuxer):
|
2001-08-11 20:37:33 +00:00
|
|
|
priv->avi_video_pts-=priv->skip_video_frames*(float)sh_video->video.dwScale/(float)sh_video->video.dwRate;
|
2002-11-02 00:45:12 +00:00
|
|
|
priv->avi_audio_pts=priv->avi_video_pts;
|
|
|
|
// set index position:
|
2001-08-11 20:37:33 +00:00
|
|
|
priv->idx_pos_a=priv->idx_pos_v=priv->idx_pos=audio_chunk_pos;
|
2001-08-11 00:53:45 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// non-interleaved stream:
|
2001-08-11 20:37:33 +00:00
|
|
|
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;
|
2001-08-11 00:53:45 +00:00
|
|
|
}
|
2001-08-08 19:37:45 +00:00
|
|
|
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_SEEK,MSGL_V,"SEEK: idx=%d (a:%d v:%d) v.skip=%d a.skip=%d/%4.3f \n",
|
2002-07-04 22:17:30 +00:00
|
|
|
(int)priv->idx_pos,audio_chunk_pos,video_chunk_pos,
|
|
|
|
(int)priv->skip_video_frames,skip_audio_bytes,skip_audio_secs);
|
2001-08-08 19:37:45 +00:00
|
|
|
|
|
|
|
if(skip_audio_bytes){
|
|
|
|
demux_read_data(d_audio,NULL,skip_audio_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2001-08-14 07:16:12 +00:00
|
|
|
d_video->pts=priv->avi_video_pts; // OSD
|
2001-08-08 19:37:45 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-26 12:36:13 +00:00
|
|
|
void demux_close_avi(demuxer_t *demuxer) {
|
|
|
|
avi_priv_t* priv=demuxer->priv;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2002-07-26 12:36:13 +00:00
|
|
|
if(!priv)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(priv->idx_size > 0)
|
|
|
|
free(priv->idx);
|
|
|
|
free(priv);
|
|
|
|
}
|
2002-11-16 03:42:14 +00:00
|
|
|
|
|
|
|
|
2005-08-11 17:23:45 +00:00
|
|
|
static int demux_avi_control(demuxer_t *demuxer,int cmd, void *arg){
|
2002-11-16 03:42:14 +00:00
|
|
|
avi_priv_t *priv=demuxer->priv;
|
|
|
|
demux_stream_t *d_video=demuxer->video;
|
|
|
|
sh_video_t *sh_video=d_video->sh;
|
|
|
|
|
|
|
|
switch(cmd) {
|
|
|
|
case DEMUXER_CTRL_GET_TIME_LENGTH:
|
2006-01-08 13:35:54 +00:00
|
|
|
if (!priv->numberofframes || !sh_video) return DEMUXER_CTRL_DONTKNOW;
|
2005-09-02 08:32:32 +00:00
|
|
|
*((double *)arg)=(double)priv->numberofframes/sh_video->fps;
|
2002-11-16 03:42:14 +00:00
|
|
|
if (sh_video->video.dwLength<=1) return DEMUXER_CTRL_GUESS;
|
|
|
|
return DEMUXER_CTRL_OK;
|
|
|
|
|
|
|
|
case DEMUXER_CTRL_GET_PERCENT_POS:
|
2006-01-08 13:35:54 +00:00
|
|
|
if (!priv->numberofframes || !sh_video) {
|
2005-01-15 16:11:45 +00:00
|
|
|
return DEMUXER_CTRL_DONTKNOW;
|
2002-11-16 03:42:14 +00:00
|
|
|
}
|
|
|
|
*((int *)arg)=(int)(priv->video_pack_no*100/priv->numberofframes);
|
|
|
|
if (sh_video->video.dwLength<=1) return DEMUXER_CTRL_GUESS;
|
|
|
|
return DEMUXER_CTRL_OK;
|
|
|
|
|
2006-12-01 17:57:29 +00:00
|
|
|
case DEMUXER_CTRL_SWITCH_AUDIO:
|
|
|
|
case DEMUXER_CTRL_SWITCH_VIDEO: {
|
|
|
|
int audio = (cmd == DEMUXER_CTRL_SWITCH_AUDIO);
|
|
|
|
demux_stream_t *ds = audio ? demuxer->audio : demuxer->video;
|
|
|
|
void **streams = audio ? demuxer->a_streams : demuxer->v_streams;
|
2006-12-01 19:43:36 +00:00
|
|
|
int maxid = FFMIN(100, audio ? MAX_A_STREAMS : MAX_V_STREAMS);
|
2006-12-01 17:57:29 +00:00
|
|
|
int chunkid;
|
|
|
|
if (ds->id < -1)
|
|
|
|
return DEMUXER_CTRL_NOTIMPL;
|
|
|
|
|
|
|
|
if (*(int *)arg >= 0)
|
|
|
|
ds->id = *(int *)arg;
|
|
|
|
else {
|
2006-12-01 19:24:42 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < maxid; i++) {
|
|
|
|
if (++ds->id >= maxid) ds->id = 0;
|
|
|
|
if (streams[ds->id]) break;
|
|
|
|
}
|
2006-12-01 17:57:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
chunkid = (ds->id / 10 + '0') | (ds->id % 10 + '0') << 8;
|
|
|
|
ds->sh = NULL;
|
2006-12-01 19:24:42 +00:00
|
|
|
if (!streams[ds->id]) // stream not available
|
|
|
|
ds->id = -1;
|
|
|
|
else
|
|
|
|
demux_avi_select_stream(demuxer, chunkid);
|
2006-12-01 17:57:29 +00:00
|
|
|
*(int *)arg = ds->id;
|
|
|
|
return DEMUXER_CTRL_OK;
|
|
|
|
}
|
|
|
|
|
2002-11-16 03:42:14 +00:00
|
|
|
default:
|
|
|
|
return DEMUXER_CTRL_NOTIMPL;
|
|
|
|
}
|
|
|
|
}
|
2005-08-05 19:57:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
static int avi_check_file(demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
int id=stream_read_dword_le(demuxer->stream); // "RIFF"
|
|
|
|
|
|
|
|
if((id==mmioFOURCC('R','I','F','F')) || (id==mmioFOURCC('O','N','2',' '))) {
|
|
|
|
stream_read_dword_le(demuxer->stream); //filesize
|
|
|
|
id=stream_read_dword_le(demuxer->stream); // "AVI "
|
|
|
|
if(id==formtypeAVI)
|
|
|
|
return DEMUXER_TYPE_AVI;
|
2007-02-06 19:01:28 +00:00
|
|
|
// "Samsung Digimax i6 PMP" crap according to bug 742
|
|
|
|
if(id==mmioFOURCC('A','V','I',0x19))
|
|
|
|
return DEMUXER_TYPE_AVI;
|
2005-08-05 19:57:47 +00:00
|
|
|
if(id==mmioFOURCC('O','N','2','f')){
|
2006-11-05 16:36:02 +00:00
|
|
|
mp_msg(MSGT_DEMUXER,MSGL_INFO,MSGTR_ON2AviFormat);
|
2005-08-05 19:57:47 +00:00
|
|
|
return DEMUXER_TYPE_AVI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static demuxer_t* demux_open_hack_avi(demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
sh_audio_t* sh_a;
|
|
|
|
|
2007-12-01 14:22:27 +00:00
|
|
|
demuxer = demux_open_avi(demuxer);
|
2005-08-05 19:57:47 +00:00
|
|
|
if(!demuxer) return NULL; // failed to open
|
2007-12-01 14:22:27 +00:00
|
|
|
sh_a = demuxer->audio->sh;
|
2005-08-05 19:57:47 +00:00
|
|
|
if(demuxer->audio->id != -2 && sh_a) {
|
2008-08-02 16:30:32 +00:00
|
|
|
#ifdef CONFIG_OGGVORBIS
|
2005-08-05 19:57:47 +00:00
|
|
|
// support for Ogg-in-AVI:
|
|
|
|
if(sh_a->format == 0xFFFE)
|
|
|
|
demuxer = init_avi_with_ogg(demuxer);
|
|
|
|
else if(sh_a->format == 0x674F) {
|
|
|
|
stream_t* s;
|
|
|
|
demuxer_t *od;
|
|
|
|
s = new_ds_stream(demuxer->audio);
|
|
|
|
od = new_demuxer(s,DEMUXER_TYPE_OGG,-1,-2,-2,NULL);
|
|
|
|
if(!demux_ogg_open(od)) {
|
|
|
|
mp_msg( MSGT_DEMUXER,MSGL_ERR,MSGTR_ErrorOpeningOGGDemuxer);
|
|
|
|
free_stream(s);
|
|
|
|
demuxer->audio->id = -2;
|
|
|
|
} else
|
|
|
|
demuxer = new_demuxers_demuxer(demuxer,od,demuxer);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return demuxer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-13 16:00:39 +00:00
|
|
|
const demuxer_desc_t demuxer_desc_avi = {
|
2005-08-05 19:57:47 +00:00
|
|
|
"AVI demuxer",
|
|
|
|
"avi",
|
|
|
|
"AVI",
|
|
|
|
"Arpi?",
|
|
|
|
"AVI files, including non interleaved files",
|
|
|
|
DEMUXER_TYPE_AVI,
|
|
|
|
1, // safe autodetect
|
|
|
|
avi_check_file,
|
|
|
|
demux_avi_fill_buffer,
|
|
|
|
demux_open_hack_avi,
|
|
|
|
demux_close_avi,
|
|
|
|
demux_seek_avi,
|
2005-08-11 17:23:45 +00:00
|
|
|
demux_avi_control
|
2005-08-05 19:57:47 +00:00
|
|
|
};
|
2005-08-13 12:20:07 +00:00
|
|
|
|
2008-01-13 16:00:39 +00:00
|
|
|
const demuxer_desc_t demuxer_desc_avi_ni = {
|
2005-08-13 12:20:07 +00:00
|
|
|
"AVI demuxer, non-interleaved",
|
|
|
|
"avini",
|
|
|
|
"AVI",
|
|
|
|
"Arpi?",
|
|
|
|
"AVI files, including non interleaved files",
|
|
|
|
DEMUXER_TYPE_AVI,
|
|
|
|
1, // safe autodetect
|
|
|
|
avi_check_file,
|
|
|
|
demux_avi_fill_buffer_ni,
|
|
|
|
demux_open_hack_avi,
|
|
|
|
demux_close_avi,
|
|
|
|
demux_seek_avi,
|
|
|
|
demux_avi_control
|
|
|
|
};
|
|
|
|
|
2008-01-13 16:00:39 +00:00
|
|
|
const demuxer_desc_t demuxer_desc_avi_nini = {
|
2005-08-13 12:20:07 +00:00
|
|
|
"AVI demuxer, non-interleaved and no index",
|
|
|
|
"avinini",
|
|
|
|
"AVI",
|
|
|
|
"Arpi?",
|
|
|
|
"AVI files, including non interleaved files",
|
|
|
|
DEMUXER_TYPE_AVI,
|
|
|
|
1, // safe autodetect
|
|
|
|
avi_check_file,
|
|
|
|
demux_avi_fill_buffer_nini,
|
|
|
|
demux_open_hack_avi,
|
|
|
|
demux_close_avi,
|
|
|
|
demux_seek_avi,
|
|
|
|
demux_avi_control
|
|
|
|
};
|