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-04-23 03:42:17 +00:00
|
|
|
|
2001-08-11 20:37:33 +00:00
|
|
|
#include "config.h"
|
2001-08-17 00:40:25 +00:00
|
|
|
#include "mp_msg.h"
|
2001-04-23 03:42:17 +00:00
|
|
|
|
|
|
|
#include "stream.h"
|
|
|
|
#include "demuxer.h"
|
2001-10-21 15:47:31 +00:00
|
|
|
#include "stheader.h"
|
2001-04-23 03:42:17 +00:00
|
|
|
|
2001-07-12 15:35:52 +00:00
|
|
|
#include "bswap.h"
|
2001-07-19 15:15:21 +00:00
|
|
|
#include "aviheader.h"
|
2001-04-23 03:42:17 +00:00
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
#define MIN(a,b) (((a)<(b))?(a):(b))
|
|
|
|
|
2001-07-12 15:35:52 +00:00
|
|
|
|
2001-04-23 03:42:17 +00:00
|
|
|
static MainAVIHeader avih;
|
|
|
|
|
2001-04-23 22:51:13 +00:00
|
|
|
extern void print_avih(MainAVIHeader *h);
|
2001-08-06 23:59:50 +00:00
|
|
|
extern void print_avih_flags(MainAVIHeader *h);
|
2001-04-23 22:51:13 +00:00
|
|
|
extern void print_strh(AVIStreamHeader *h);
|
|
|
|
extern void print_wave_header(WAVEFORMATEX *h);
|
2001-08-12 15:46:09 +00:00
|
|
|
extern void print_video_header(BITMAPINFOHEADER *h);
|
2001-04-23 22:51:13 +00:00
|
|
|
extern void print_index(AVIINDEXENTRY *idx,int idx_size);
|
|
|
|
|
2001-04-23 03:42:17 +00:00
|
|
|
void read_avi_header(demuxer_t *demuxer,int index_mode){
|
2001-04-15 03:40:37 +00:00
|
|
|
sh_audio_t *sh_audio=NULL;
|
|
|
|
sh_video_t *sh_video=NULL;
|
2001-02-24 20:28:24 +00:00
|
|
|
int stream_id=-1;
|
2001-04-22 01:04:18 +00:00
|
|
|
int idxfix_videostream=0;
|
|
|
|
int idxfix_divx=0;
|
2001-08-11 20:37:33 +00:00
|
|
|
avi_priv_t* priv=demuxer->priv;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
//---- AVI header:
|
2001-08-11 20:37:33 +00:00
|
|
|
priv->idx_size=0;
|
2001-10-21 12:18:45 +00:00
|
|
|
priv->audio_streams=0;
|
2001-02-24 20:28:24 +00:00
|
|
|
while(1){
|
|
|
|
int id=stream_read_dword_le(demuxer->stream);
|
|
|
|
int chunksize,size2;
|
|
|
|
static int last_fccType=0;
|
2001-08-23 21:10:31 +00:00
|
|
|
char* hdr=NULL;
|
2001-02-24 20:28:24 +00:00
|
|
|
//
|
|
|
|
if(stream_eof(demuxer->stream)) break;
|
|
|
|
//
|
|
|
|
if(id==mmioFOURCC('L','I','S','T')){
|
|
|
|
int len=stream_read_dword_le(demuxer->stream)-4; // list size
|
|
|
|
id=stream_read_dword_le(demuxer->stream); // list type
|
2001-08-23 21:10:31 +00:00
|
|
|
mp_msg(MSGT_HEADER,MSGL_DBG2,"LIST %.4s len=%d\n",(char *) &id,len);
|
2001-02-24 20:28:24 +00:00
|
|
|
if(id==listtypeAVIMOVIE){
|
|
|
|
// found MOVI header
|
2001-04-23 03:42:17 +00:00
|
|
|
demuxer->movi_start=stream_tell(demuxer->stream);
|
|
|
|
demuxer->movi_end=demuxer->movi_start+len;
|
2001-08-29 17:39:10 +00:00
|
|
|
mp_msg(MSGT_HEADER,MSGL_V,"Found movie at 0x%X - 0x%X\n",(int)demuxer->movi_start,(int)demuxer->movi_end);
|
2001-05-03 23:32:56 +00:00
|
|
|
if(index_mode==-2) break; // reading from non-seekable source (stdin)
|
2001-02-24 20:28:24 +00:00
|
|
|
len=(len+1)&(~1);
|
|
|
|
stream_skip(demuxer->stream,len);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
size2=stream_read_dword_le(demuxer->stream);
|
2001-08-23 21:10:31 +00:00
|
|
|
mp_msg(MSGT_HEADER,MSGL_DBG2,"CHUNK %.4s len=%d\n",(char *) &id,size2);
|
2001-02-24 20:28:24 +00:00
|
|
|
chunksize=(size2+1)&(~1);
|
|
|
|
switch(id){
|
2001-08-23 21:10:31 +00:00
|
|
|
case mmioFOURCC('I','S','F','T'): hdr="Software";break;
|
|
|
|
case mmioFOURCC('I','N','A','M'): hdr="Name";break;
|
2001-08-23 22:01:13 +00:00
|
|
|
case mmioFOURCC('I','S','B','J'): hdr="Title";break;
|
|
|
|
case mmioFOURCC('I','A','R','T'): hdr="Author";break;
|
2001-08-23 21:10:31 +00:00
|
|
|
case mmioFOURCC('I','C','O','P'): hdr="Copyright";break;
|
|
|
|
case mmioFOURCC('I','C','M','T'): hdr="Comment";break;
|
2001-02-24 20:28:24 +00:00
|
|
|
case ckidAVIMAINHDR: // read 'avih'
|
2001-04-23 03:42:17 +00:00
|
|
|
stream_read(demuxer->stream,(char*) &avih,MIN(size2,sizeof(avih)));
|
2001-07-12 15:35:52 +00:00
|
|
|
le2me_MainAVIHeader(&avih); // swap to machine endian
|
2001-04-23 03:42:17 +00:00
|
|
|
chunksize-=MIN(size2,sizeof(avih));
|
2001-08-06 23:59:50 +00:00
|
|
|
if(verbose) print_avih(&avih); else print_avih_flags(&avih);
|
2001-02-24 20:28:24 +00:00
|
|
|
break;
|
|
|
|
case ckidSTREAMHEADER: { // read 'strh'
|
|
|
|
AVIStreamHeader h;
|
|
|
|
stream_read(demuxer->stream,(char*) &h,MIN(size2,sizeof(h)));
|
2001-07-12 15:35:52 +00:00
|
|
|
le2me_AVIStreamHeader(&h); // swap to machine endian
|
2001-02-24 20:28:24 +00:00
|
|
|
chunksize-=MIN(size2,sizeof(h));
|
2001-04-15 03:40:37 +00:00
|
|
|
++stream_id;
|
|
|
|
if(h.fccType==streamtypeVIDEO){
|
2001-07-07 18:46:15 +00:00
|
|
|
sh_video=new_sh_video(demuxer,stream_id);
|
2001-04-15 03:40:37 +00:00
|
|
|
memcpy(&sh_video->video,&h,sizeof(h));
|
|
|
|
} else
|
|
|
|
if(h.fccType==streamtypeAUDIO){
|
2001-07-07 18:46:15 +00:00
|
|
|
sh_audio=new_sh_audio(demuxer,stream_id);
|
2001-04-15 03:40:37 +00:00
|
|
|
memcpy(&sh_audio->audio,&h,sizeof(h));
|
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
last_fccType=h.fccType;
|
|
|
|
if(verbose>=1) print_strh(&h);
|
|
|
|
break; }
|
|
|
|
case ckidSTREAMFORMAT: { // read 'strf'
|
|
|
|
if(last_fccType==streamtypeVIDEO){
|
2001-04-15 14:45:15 +00:00
|
|
|
sh_video->bih=calloc((chunksize<sizeof(BITMAPINFOHEADER))?sizeof(BITMAPINFOHEADER):chunksize,1);
|
|
|
|
// sh_video->bih=malloc(chunksize); memset(sh_video->bih,0,chunksize);
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_HEADER,MSGL_V,"found 'bih', %d bytes of %d\n",chunksize,sizeof(BITMAPINFOHEADER));
|
2001-04-15 14:33:49 +00:00
|
|
|
stream_read(demuxer->stream,(char*) sh_video->bih,chunksize);
|
2001-07-12 15:35:52 +00:00
|
|
|
le2me_BITMAPINFOHEADER(sh_video->bih); // swap to machine endian
|
2001-08-12 02:00:14 +00:00
|
|
|
if(verbose>=1) print_video_header(sh_video->bih);
|
2001-04-15 14:33:49 +00:00
|
|
|
chunksize=0;
|
|
|
|
// sh_video->fps=(float)sh_video->video.dwRate/(float)sh_video->video.dwScale;
|
|
|
|
// sh_video->frametime=(float)sh_video->video.dwScale/(float)sh_video->video.dwRate;
|
2001-04-15 03:40:37 +00:00
|
|
|
// if(demuxer->video->id==-1) demuxer->video->id=stream_id;
|
2001-04-22 01:04:18 +00:00
|
|
|
// IdxFix:
|
|
|
|
idxfix_videostream=stream_id;
|
|
|
|
switch(sh_video->bih->biCompression){
|
|
|
|
case mmioFOURCC('D', 'I', 'V', '3'):
|
|
|
|
case mmioFOURCC('d', 'i', 'v', '3'):
|
|
|
|
case mmioFOURCC('D', 'I', 'V', '4'):
|
|
|
|
case mmioFOURCC('d', 'i', 'v', '4'):
|
|
|
|
case mmioFOURCC('D', 'I', 'V', '5'):
|
|
|
|
case mmioFOURCC('d', 'i', 'v', '5'):
|
|
|
|
case mmioFOURCC('D', 'I', 'V', '6'):
|
|
|
|
case mmioFOURCC('d', 'i', 'v', '6'):
|
|
|
|
case mmioFOURCC('M', 'P', '4', '3'):
|
|
|
|
case mmioFOURCC('m', 'p', '4', '3'):
|
2001-04-23 03:42:17 +00:00
|
|
|
case mmioFOURCC('M', 'P', '4', '2'):
|
|
|
|
case mmioFOURCC('m', 'p', '4', '2'):
|
2001-05-11 23:01:52 +00:00
|
|
|
case mmioFOURCC('D', 'I', 'V', '2'):
|
2001-04-22 01:04:18 +00:00
|
|
|
case mmioFOURCC('A', 'P', '4', '1'):
|
|
|
|
idxfix_divx=1; // we can fix keyframes only for divx coded files!
|
2001-10-31 20:43:13 +00:00
|
|
|
mp_msg(MSGT_HEADER,MSGL_V,"Regenerating keyframe table for DIVX 3 video\n");
|
|
|
|
break;
|
|
|
|
case mmioFOURCC('D', 'I', 'V', 'X'):
|
|
|
|
case mmioFOURCC('d', 'i', 'v', 'x'):
|
|
|
|
idxfix_divx=2; // we can fix keyframes only for divx coded files!
|
|
|
|
mp_msg(MSGT_HEADER,MSGL_V,"Regenerating keyframe table for DIVX 4 video\n");
|
|
|
|
break;
|
2001-04-22 01:04:18 +00:00
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
} else
|
|
|
|
if(last_fccType==streamtypeAUDIO){
|
2001-06-05 18:40:44 +00:00
|
|
|
int wf_size = chunksize<sizeof(WAVEFORMATEX)?sizeof(WAVEFORMATEX):chunksize;
|
|
|
|
sh_audio->wf=calloc(wf_size,1);
|
2001-04-15 14:45:15 +00:00
|
|
|
// sh_audio->wf=malloc(chunksize); memset(sh_audio->wf,0,chunksize);
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_HEADER,MSGL_V,"found 'wf', %d bytes of %d\n",chunksize,sizeof(WAVEFORMATEX));
|
2001-04-15 14:33:49 +00:00
|
|
|
stream_read(demuxer->stream,(char*) sh_audio->wf,chunksize);
|
2001-07-12 15:35:52 +00:00
|
|
|
le2me_WAVEFORMATEX(sh_audio->wf);
|
2001-06-05 18:40:44 +00:00
|
|
|
if (sh_audio->wf->cbSize != 0 &&
|
|
|
|
wf_size < sizeof(WAVEFORMATEX)+sh_audio->wf->cbSize) {
|
|
|
|
sh_audio->wf=realloc(sh_audio->wf, sizeof(WAVEFORMATEX)+sh_audio->wf->cbSize);
|
|
|
|
}
|
2001-04-15 14:33:49 +00:00
|
|
|
chunksize=0;
|
|
|
|
if(verbose>=1) print_wave_header(sh_audio->wf);
|
2001-10-21 12:18:45 +00:00
|
|
|
++priv->audio_streams;
|
2001-04-15 03:40:37 +00:00
|
|
|
// if(demuxer->audio->id==-1) demuxer->audio->id=stream_id;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2001-04-21 20:44:41 +00:00
|
|
|
case ckidAVINEWINDEX: if(index_mode){
|
2001-07-12 15:35:52 +00:00
|
|
|
int i;
|
2001-08-11 20:37:33 +00:00
|
|
|
priv->idx_size=size2>>4;
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_HEADER,MSGL_V,"Reading INDEX block, %d chunks for %ld frames\n",
|
2001-08-11 20:37:33 +00:00
|
|
|
priv->idx_size,avih.dwTotalFrames);
|
|
|
|
priv->idx=malloc(priv->idx_size<<4);
|
|
|
|
stream_read(demuxer->stream,(char*)priv->idx,priv->idx_size<<4);
|
|
|
|
for (i = 0; i < priv->idx_size; i++) // swap index to machine endian
|
|
|
|
le2me_AVIINDEXENTRY((AVIINDEXENTRY*)priv->idx + i);
|
|
|
|
chunksize-=priv->idx_size<<4;
|
|
|
|
if(verbose>=2) print_index(priv->idx,priv->idx_size);
|
2001-02-24 20:28:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-08-23 21:10:31 +00:00
|
|
|
if(hdr){
|
|
|
|
char buf[256];
|
|
|
|
int len=(size2<250)?size2:250;
|
|
|
|
stream_read(demuxer->stream,buf,len);
|
|
|
|
chunksize-=len;
|
|
|
|
buf[len]=0;
|
|
|
|
mp_msg(MSGT_HEADER,MSGL_V,"%-10s: %s\n",hdr,buf);
|
2001-11-22 15:43:24 +00:00
|
|
|
demux_info_add(demuxer, hdr, buf);
|
2001-08-23 21:10:31 +00:00
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
if(chunksize>0) stream_skip(demuxer->stream,chunksize); else
|
2001-08-17 00:40:25 +00:00
|
|
|
if(chunksize<0) mp_msg(MSGT_HEADER,MSGL_WARN,"chunksize=%d (id=%.4s)\n",chunksize,(char *) &id);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2001-08-11 20:37:33 +00:00
|
|
|
if(index_mode>=2 || (priv->idx_size==0 && index_mode==1)){
|
2001-04-21 20:44:41 +00:00
|
|
|
// build index for file:
|
|
|
|
stream_reset(demuxer->stream);
|
2001-04-23 03:42:17 +00:00
|
|
|
stream_seek(demuxer->stream,demuxer->movi_start);
|
2001-04-21 20:44:41 +00:00
|
|
|
|
2001-08-11 20:37:33 +00:00
|
|
|
priv->idx_pos=0;
|
|
|
|
priv->idx_size=0;
|
|
|
|
priv->idx=NULL;
|
2001-04-21 20:44:41 +00:00
|
|
|
|
|
|
|
while(1){
|
|
|
|
int id,len,skip;
|
|
|
|
AVIINDEXENTRY* idx;
|
2001-10-31 20:43:13 +00:00
|
|
|
unsigned int c;
|
2001-04-21 20:44:41 +00:00
|
|
|
demuxer->filepos=stream_tell(demuxer->stream);
|
2001-10-31 20:43:13 +00:00
|
|
|
if(demuxer->filepos>=demuxer->movi_end && demuxer->movi_start<demuxer->movi_end) break;
|
2001-04-21 20:44:41 +00:00
|
|
|
id=stream_read_dword_le(demuxer->stream);
|
|
|
|
len=stream_read_dword_le(demuxer->stream);
|
|
|
|
if(id==mmioFOURCC('L','I','S','T')){
|
|
|
|
id=stream_read_dword_le(demuxer->stream); // list type
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(stream_eof(demuxer->stream)) break;
|
2001-07-27 01:16:54 +00:00
|
|
|
if(!id || avi_stream_id(id)==100) goto skip_chunk; // bad ID (or padding?)
|
|
|
|
|
2001-08-12 22:36:05 +00:00
|
|
|
if(priv->idx_pos>=priv->idx_size){
|
2001-08-11 20:37:33 +00:00
|
|
|
// priv->idx_size+=32;
|
|
|
|
priv->idx_size+=1024; // +16kB
|
|
|
|
priv->idx=realloc(priv->idx,priv->idx_size*sizeof(AVIINDEXENTRY));
|
|
|
|
if(!priv->idx){priv->idx_pos=0; break;} // error!
|
2001-04-21 20:44:41 +00:00
|
|
|
}
|
2001-08-11 20:37:33 +00:00
|
|
|
idx=&((AVIINDEXENTRY *)priv->idx)[priv->idx_pos++];
|
2001-04-21 20:44:41 +00:00
|
|
|
idx->ckid=id;
|
|
|
|
idx->dwFlags=AVIIF_KEYFRAME; // FIXME
|
|
|
|
idx->dwChunkOffset=demuxer->filepos;
|
|
|
|
idx->dwChunkLength=len;
|
2001-04-22 01:29:59 +00:00
|
|
|
|
2001-10-31 20:43:13 +00:00
|
|
|
c=stream_read_dword(demuxer->stream);
|
2001-04-22 01:04:18 +00:00
|
|
|
|
|
|
|
// Fix keyframes for DivX files:
|
|
|
|
if(idxfix_divx)
|
|
|
|
if(avi_stream_id(id)==idxfix_videostream){
|
2001-10-31 20:43:13 +00:00
|
|
|
switch(idxfix_divx){
|
|
|
|
case 1: if(c&0x40000000) idx->dwFlags=0;break; // divx 3
|
|
|
|
case 2: if(c==0x1B6) idx->dwFlags=0;break; // divx 4
|
|
|
|
}
|
2001-04-22 01:04:18 +00:00
|
|
|
}
|
2001-12-27 00:37:03 +00:00
|
|
|
|
|
|
|
// update status line:
|
|
|
|
{ static int lastpos;
|
|
|
|
int pos;
|
|
|
|
off_t len=demuxer->movi_end-demuxer->movi_start;
|
|
|
|
if(len){
|
|
|
|
pos=100*(demuxer->filepos-demuxer->movi_start)/len; // %
|
|
|
|
} else {
|
|
|
|
pos=(demuxer->filepos-demuxer->movi_start)>>20; // MB
|
|
|
|
}
|
|
|
|
if(pos!=lastpos){
|
|
|
|
lastpos=pos;
|
|
|
|
mp_msg(MSGT_HEADER,MSGL_STATUS,"Generating Index: %3d %s \r",
|
|
|
|
pos, len?"%":"MB");
|
|
|
|
}
|
|
|
|
}
|
2001-10-31 20:43:13 +00:00
|
|
|
mp_dbg(MSGT_HEADER,MSGL_DBG2,"%08X %08X %.4s %08X %X\n",(int)demuxer->filepos,id,(char *) &id,(int)c,(unsigned int) idx->dwFlags);
|
2001-04-22 01:04:18 +00:00
|
|
|
#if 0
|
|
|
|
{ unsigned char tmp[64];
|
|
|
|
int i;
|
|
|
|
stream_read(demuxer->stream,tmp,64);
|
|
|
|
printf("%.4s",&id);
|
|
|
|
for(i=0;i<64;i++) printf(" %02X",tmp[i]);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
#endif
|
2001-07-27 01:16:54 +00:00
|
|
|
skip_chunk:
|
2001-04-21 20:44:41 +00:00
|
|
|
skip=(len+1)&(~1); // total bytes in this chunk
|
2001-04-22 01:04:18 +00:00
|
|
|
stream_seek(demuxer->stream,8+demuxer->filepos+skip);
|
2001-04-21 20:44:41 +00:00
|
|
|
}
|
2001-08-11 20:37:33 +00:00
|
|
|
priv->idx_size=priv->idx_pos;
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_HEADER,MSGL_INFO,"AVI: Generated index table for %d chunks!\n",priv->idx_size);
|
2001-04-21 20:44:41 +00:00
|
|
|
}
|
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef MIN
|
|
|
|
|
2001-08-11 20:37:33 +00:00
|
|
|
|