2001-02-24 21:28:24 +01:00
|
|
|
// AVI file parser for DEMUXER v2.6 by A'rpi/ESP-team
|
|
|
|
|
2001-04-23 05:42:17 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2001-08-01 11:14:02 +02:00
|
|
|
#include <unistd.h>
|
2001-02-24 21:28:24 +01:00
|
|
|
|
2001-04-23 05:42:17 +02:00
|
|
|
extern int verbose; // defined in mplayer.c
|
2001-02-24 21:28:24 +01:00
|
|
|
|
2001-04-23 05:42:17 +02:00
|
|
|
#include "stream.h"
|
|
|
|
#include "demuxer.h"
|
|
|
|
|
|
|
|
#include "wine/mmreg.h"
|
|
|
|
#include "wine/avifmt.h"
|
|
|
|
#include "wine/vfw.h"
|
|
|
|
|
|
|
|
#include "codec-cfg.h"
|
|
|
|
#include "stheader.h"
|
|
|
|
|
|
|
|
//static float avi_pts_frametime=1.0f/25.0f;
|
|
|
|
float avi_audio_pts=0;
|
|
|
|
float avi_video_pts=0;
|
|
|
|
//float avi_video_ftime=0.04;
|
|
|
|
int skip_video_frames=0;
|
2001-02-24 21:28:24 +01:00
|
|
|
|
|
|
|
// Select ds from ID
|
2001-04-23 05:42:17 +02:00
|
|
|
demux_stream_t* demux_avi_select_stream(demuxer_t *demux,unsigned int id){
|
2001-02-24 21:28:24 +01:00
|
|
|
int stream_id=avi_stream_id(id);
|
2001-04-15 05:40:37 +02:00
|
|
|
|
|
|
|
// printf("demux_avi_select_stream(%d) {a:%d/v:%d}\n",stream_id,
|
|
|
|
// demux->audio->id,demux->video->id);
|
|
|
|
|
|
|
|
if(demux->video->id==-1)
|
2001-04-23 05:42:17 +02:00
|
|
|
if(demux->v_streams[stream_id])
|
2001-04-15 05:40:37 +02:00
|
|
|
demux->video->id=stream_id;
|
|
|
|
|
|
|
|
if(demux->audio->id==-1)
|
2001-04-23 05:42:17 +02:00
|
|
|
if(demux->a_streams[stream_id])
|
2001-04-15 05:40:37 +02:00
|
|
|
demux->audio->id=stream_id;
|
|
|
|
|
|
|
|
if(stream_id==demux->audio->id){
|
|
|
|
if(!demux->audio->sh){
|
2001-04-23 05:42:17 +02:00
|
|
|
demux->audio->sh=demux->a_streams[stream_id];
|
2001-04-15 05:40:37 +02:00
|
|
|
if(verbose) printf("Auto-selected AVI audio ID = %d\n",demux->audio->id);
|
|
|
|
}
|
|
|
|
return demux->audio;
|
|
|
|
}
|
|
|
|
if(stream_id==demux->video->id){
|
|
|
|
if(!demux->video->sh){
|
2001-04-23 05:42:17 +02:00
|
|
|
demux->video->sh=demux->v_streams[stream_id];
|
2001-04-15 05:40:37 +02:00
|
|
|
if(verbose) printf("Auto-selected AVI video ID = %d\n",demux->video->id);
|
|
|
|
}
|
|
|
|
return demux->video;
|
|
|
|
}
|
2001-02-24 21:28:24 +01:00
|
|
|
if(id!=mmioFOURCC('J','U','N','K')){
|
|
|
|
// unknown
|
2001-04-24 00:33:06 +02:00
|
|
|
if(verbose>=2) printf("Unknown chunk: %.4s (%X)\n",(char *) &id,id);
|
2001-02-24 21:28:24 +01:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-06-04 01:37:20 +02:00
|
|
|
static int demux_avi_read_packet(demuxer_t *demux,unsigned int id,unsigned int len,int idxpos,int flags){
|
2001-02-24 21:28:24 +01:00
|
|
|
int skip;
|
|
|
|
float pts=0;
|
|
|
|
demux_stream_t *ds=demux_avi_select_stream(demux,id);
|
|
|
|
|
|
|
|
if(verbose>=3) printf("demux_avi.read_packet: %X\n",id);
|
|
|
|
|
|
|
|
if(ds==demux->audio){
|
|
|
|
pts=avi_audio_pts;
|
|
|
|
avi_audio_pts=0;
|
|
|
|
} else
|
2001-07-29 03:28:07 +02:00
|
|
|
if(ds==demux->video){
|
2001-02-24 21:28:24 +01:00
|
|
|
// video
|
|
|
|
if(skip_video_frames>0){
|
|
|
|
// drop frame (seeking)
|
|
|
|
--skip_video_frames;
|
|
|
|
ds=NULL;
|
|
|
|
} else {
|
|
|
|
pts=avi_video_pts;
|
|
|
|
}
|
|
|
|
// ezt a 2 sort lehet hogy fell kell majd cserelni:
|
|
|
|
//avi_video_pts+=avi_pts_frametime;
|
2001-04-06 03:18:59 +02:00
|
|
|
//avi_video_pts+=(float)avi_header.video.dwScale/(float)avi_header.video.dwRate;
|
|
|
|
//avi_video_pts+=((sh_video_t*)ds->sh)->frametime;
|
2001-04-23 05:42:17 +02:00
|
|
|
// FIXME!!!
|
2001-07-21 02:10:09 +02:00
|
|
|
#if 1
|
|
|
|
// printf("ds=0x%X\n",ds);
|
|
|
|
// printf("packno=%d\n",ds->pack_no);
|
2001-07-29 03:28:07 +02:00
|
|
|
// printf("### pack_no=%d\n",demux->video->pack_no+demux->video->packs);
|
|
|
|
avi_video_pts = (demux->video->pack_no+demux->video->packs) *
|
2001-07-21 02:10:09 +02:00
|
|
|
(float)((sh_video_t*)demux->video->sh)->video.dwScale /
|
|
|
|
(float)((sh_video_t*)demux->video->sh)->video.dwRate;
|
|
|
|
#else
|
2001-04-06 03:18:59 +02:00
|
|
|
avi_video_pts+=(float)((sh_video_t*)(demux->video->sh))->video.dwScale/(float)((sh_video_t*)(demux->video->sh))->video.dwRate;
|
2001-04-23 05:42:17 +02:00
|
|
|
// avi_video_pts+=avi_video_ftime;
|
2001-07-21 02:10:09 +02:00
|
|
|
#endif
|
2001-02-24 21:28:24 +01:00
|
|
|
avi_audio_pts=avi_video_pts;
|
|
|
|
}
|
|
|
|
|
|
|
|
// len=stream_read_dword_le(demux->stream);
|
|
|
|
skip=(len+1)&(~1); // total bytes in this chunk
|
|
|
|
|
|
|
|
if(ds){
|
|
|
|
if(verbose>=2) printf("DEMUX_AVI: Read %d data bytes from packet %04X\n",len,id);
|
2001-06-04 01:37:20 +02:00
|
|
|
ds_read_packet(ds,demux->stream,len,pts,idxpos,flags);
|
2001-02-24 21:28:24 +01:00
|
|
|
skip-=len;
|
|
|
|
}
|
|
|
|
if(skip){
|
|
|
|
if(verbose>=2) printf("DEMUX_AVI: Skipping %d bytes from packet %04X\n",skip,id);
|
|
|
|
stream_skip(demux->stream,skip);
|
|
|
|
}
|
|
|
|
return ds?1:0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//static int num_elementary_packets100=0;
|
|
|
|
//static int num_elementary_packets101=0;
|
|
|
|
|
|
|
|
// return value:
|
|
|
|
// 0 = EOF or no stream found
|
|
|
|
// 1 = successfully read a packet
|
|
|
|
int demux_avi_fill_buffer(demuxer_t *demux){
|
|
|
|
unsigned int id=0;
|
|
|
|
unsigned int len;
|
|
|
|
int max_packs=128;
|
|
|
|
int ret=0;
|
|
|
|
|
|
|
|
do{
|
2001-06-04 01:37:20 +02:00
|
|
|
int flags=0;
|
2001-02-24 21:28:24 +01:00
|
|
|
AVIINDEXENTRY *idx=NULL;
|
2001-07-29 23:07:34 +02:00
|
|
|
#if 0
|
2001-02-24 21:28:24 +01:00
|
|
|
demux->filepos=stream_tell(demux->stream);
|
2001-04-23 05:42:17 +02:00
|
|
|
if(demux->filepos>=demux->movi_end){
|
2001-02-24 21:28:24 +01:00
|
|
|
demux->stream->eof=1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(stream_eof(demux->stream)) return 0;
|
2001-07-29 23:07:34 +02:00
|
|
|
#endif
|
2001-04-23 05:42:17 +02:00
|
|
|
if(demux->idx_size>0 && demux->idx_pos<demux->idx_size){
|
2001-02-24 21:28:24 +01:00
|
|
|
unsigned int pos;
|
|
|
|
|
2001-04-23 05:42:17 +02:00
|
|
|
//if(demux->idx_pos<0) printf("Fatal! idx_pos=%d\n",demux->idx_pos);
|
2001-02-24 21:28:24 +01:00
|
|
|
|
2001-04-23 05:42:17 +02:00
|
|
|
idx=&((AVIINDEXENTRY *)demux->idx)[demux->idx_pos++];
|
2001-02-24 21:28:24 +01:00
|
|
|
|
2001-04-23 05:42:17 +02:00
|
|
|
//printf("[%d]",demux->idx_pos);fflush(stdout);
|
2001-02-24 21:28:24 +01:00
|
|
|
|
|
|
|
//stream_seek(demux->stream,idx.dwChunkOffset);
|
|
|
|
//printf("IDX pos=%X idx.pos=%X idx.size=%X idx.flags=%X\n",demux->filepos,
|
|
|
|
// pos-4,idx->dwChunkLength,idx->dwFlags);
|
|
|
|
if(idx->dwFlags&AVIIF_LIST){
|
|
|
|
// LIST
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(!demux_avi_select_stream(demux,idx->ckid)){
|
2001-04-24 00:33:06 +02:00
|
|
|
if(verbose>2) printf("Skip chunk %.4s (0x%X) \n",(char *)&idx->ckid,(unsigned int)idx->ckid);
|
2001-02-24 21:28:24 +01:00
|
|
|
continue; // skip this chunk
|
|
|
|
}
|
|
|
|
|
2001-04-23 05:42:17 +02:00
|
|
|
pos=idx->dwChunkOffset+demux->idx_offset;
|
|
|
|
if(pos<demux->movi_start || pos>=demux->movi_end){
|
2001-07-29 23:07:34 +02:00
|
|
|
printf("ChunkOffset out of range! idx=0x%X \n",pos);
|
2001-02-24 21:28:24 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
if(pos!=demux->filepos){
|
|
|
|
printf("Warning! pos=0x%X idx.pos=0x%X diff=%d \n",demux->filepos,pos,pos-demux->filepos);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
stream_seek(demux->stream,pos);
|
2001-07-29 23:07:34 +02:00
|
|
|
demux->filepos=stream_tell(demux->stream);
|
2001-02-24 21:28:24 +01:00
|
|
|
id=stream_read_dword_le(demux->stream);
|
2001-07-29 23:07:34 +02:00
|
|
|
if(stream_eof(demux->stream)) return 0; // EOF!
|
|
|
|
|
2001-02-24 21:28:24 +01:00
|
|
|
if(id!=idx->ckid){
|
2001-04-24 00:33:06 +02:00
|
|
|
printf("ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id,(char *)&idx->ckid);
|
2001-02-24 21:28:24 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
len=stream_read_dword_le(demux->stream);
|
|
|
|
// if((len&(~1))!=(idx->dwChunkLength&(~1))){
|
|
|
|
// if((len)!=(idx->dwChunkLength)){
|
|
|
|
if((len!=idx->dwChunkLength)&&((len+1)!=idx->dwChunkLength)){
|
2001-04-24 00:33:06 +02:00
|
|
|
printf("ChunkSize mismatch! raw=%d idx=%ld \n",len,idx->dwChunkLength);
|
2001-02-24 21:28:24 +01:00
|
|
|
continue;
|
|
|
|
}
|
2001-06-04 01:37:20 +02:00
|
|
|
if(idx->dwFlags&AVIIF_KEYFRAME) flags=1;
|
2001-02-24 21:28:24 +01:00
|
|
|
} else {
|
2001-07-29 23:07:34 +02:00
|
|
|
demux->filepos=stream_tell(demux->stream);
|
|
|
|
if(demux->filepos>=demux->movi_end){
|
|
|
|
demux->stream->eof=1;
|
|
|
|
return 0;
|
|
|
|
}
|
2001-02-24 21:28:24 +01:00
|
|
|
id=stream_read_dword_le(demux->stream);
|
|
|
|
len=stream_read_dword_le(demux->stream);
|
2001-07-29 23:07:34 +02:00
|
|
|
if(stream_eof(demux->stream)) return 0; // EOF!
|
|
|
|
|
2001-02-24 21:28:24 +01:00
|
|
|
if(id==mmioFOURCC('L','I','S','T')){
|
|
|
|
id=stream_read_dword_le(demux->stream); // list type
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2001-06-04 01:37:20 +02:00
|
|
|
ret=demux_avi_read_packet(demux,id,len,demux->idx_pos-1,flags);
|
2001-02-24 21:28:24 +01:00
|
|
|
if(!ret && skip_video_frames<=0)
|
|
|
|
if(--max_packs==0){
|
|
|
|
demux->stream->eof=1;
|
|
|
|
printf("demux: file doesn't contain the selected audio or video stream\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} 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){
|
|
|
|
unsigned int id=0;
|
|
|
|
unsigned int len;
|
|
|
|
int max_packs=128;
|
|
|
|
int ret=0;
|
|
|
|
|
|
|
|
do{
|
2001-06-04 01:37:20 +02:00
|
|
|
int flags=0;
|
2001-02-24 21:28:24 +01:00
|
|
|
AVIINDEXENTRY *idx=NULL;
|
|
|
|
int idx_pos=0;
|
|
|
|
demux->filepos=stream_tell(demux->stream);
|
|
|
|
|
2001-04-23 05:42:17 +02:00
|
|
|
if(ds==demux->video) idx_pos=demux->idx_pos_a++; else
|
|
|
|
if(ds==demux->audio) idx_pos=demux->idx_pos_v++; else
|
|
|
|
idx_pos=demux->idx_pos++;
|
2001-02-24 21:28:24 +01:00
|
|
|
|
2001-04-23 05:42:17 +02:00
|
|
|
if(demux->idx_size>0 && idx_pos<demux->idx_size){
|
2001-02-24 21:28:24 +01:00
|
|
|
unsigned int pos;
|
2001-04-23 05:42:17 +02:00
|
|
|
idx=&((AVIINDEXENTRY *)demux->idx)[idx_pos];
|
|
|
|
// idx=&demux->idx[idx_pos];
|
2001-02-24 21:28:24 +01:00
|
|
|
|
|
|
|
if(idx->dwFlags&AVIIF_LIST){
|
|
|
|
// LIST
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(ds && demux_avi_select_stream(demux,idx->ckid)!=ds){
|
2001-04-24 00:33:06 +02:00
|
|
|
if(verbose>2) printf("Skip chunk %.4s (0x%X) \n",(char *)&idx->ckid,(unsigned int)idx->ckid);
|
2001-02-24 21:28:24 +01:00
|
|
|
continue; // skip this chunk
|
|
|
|
}
|
|
|
|
|
2001-04-23 05:42:17 +02:00
|
|
|
pos=idx->dwChunkOffset+demux->idx_offset;
|
|
|
|
if(pos<demux->movi_start || pos>=demux->movi_end){
|
2001-02-24 21:28:24 +01:00
|
|
|
printf("ChunkOffset out of range! current=0x%X idx=0x%X \n",demux->filepos,pos);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
if(pos!=demux->filepos){
|
|
|
|
printf("Warning! pos=0x%X idx.pos=0x%X diff=%d \n",demux->filepos,pos,pos-demux->filepos);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
stream_seek(demux->stream,pos);
|
|
|
|
|
|
|
|
id=stream_read_dword_le(demux->stream);
|
|
|
|
|
|
|
|
if(stream_eof(demux->stream)) return 0;
|
|
|
|
|
|
|
|
if(id!=idx->ckid){
|
2001-04-24 00:33:06 +02:00
|
|
|
printf("ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id,(char *)&idx->ckid);
|
2001-02-24 21:28:24 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
len=stream_read_dword_le(demux->stream);
|
|
|
|
// if((len&(~1))!=(idx->dwChunkLength&(~1))){
|
|
|
|
// if((len)!=(idx->dwChunkLength)){
|
|
|
|
if((len!=idx->dwChunkLength)&&((len+1)!=idx->dwChunkLength)){
|
2001-04-24 00:33:06 +02:00
|
|
|
printf("ChunkSize mismatch! raw=%d idx=%ld \n",len,idx->dwChunkLength);
|
2001-02-24 21:28:24 +01:00
|
|
|
continue;
|
|
|
|
}
|
2001-06-04 01:37:20 +02:00
|
|
|
if(idx->dwFlags&AVIIF_KEYFRAME) flags=1;
|
2001-02-24 21:28:24 +01:00
|
|
|
} else return 0;
|
2001-06-04 01:37:20 +02:00
|
|
|
ret=demux_avi_read_packet(demux,id,len,idx_pos,flags);
|
2001-02-24 21:28:24 +01:00
|
|
|
if(!ret && skip_video_frames<=0)
|
|
|
|
if(--max_packs==0){
|
|
|
|
demux->stream->eof=1;
|
|
|
|
printf("demux: file doesn't contain the selected audio or video stream\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} 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){
|
|
|
|
unsigned int id=0;
|
|
|
|
unsigned int len;
|
|
|
|
int ret=0;
|
|
|
|
int *fpos=NULL;
|
|
|
|
|
2001-04-23 05:42:17 +02:00
|
|
|
if(ds==demux->video) fpos=&demux->idx_pos_a; else
|
|
|
|
if(ds==demux->audio) fpos=&demux->idx_pos_v; else
|
2001-02-24 21:28:24 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
stream_seek(demux->stream,fpos[0]);
|
|
|
|
|
|
|
|
do{
|
|
|
|
|
|
|
|
demux->filepos=stream_tell(demux->stream);
|
2001-04-23 05:42:17 +02:00
|
|
|
if(demux->filepos>=demux->movi_end){
|
2001-02-24 21:28:24 +01:00
|
|
|
demux->stream->eof=1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(stream_eof(demux->stream)) return 0;
|
|
|
|
|
|
|
|
id=stream_read_dword_le(demux->stream);
|
|
|
|
len=stream_read_dword_le(demux->stream);
|
|
|
|
if(id==mmioFOURCC('L','I','S','T')){
|
|
|
|
id=stream_read_dword_le(demux->stream); // list type
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ds==demux_avi_select_stream(demux,id)){
|
|
|
|
// read it!
|
2001-06-04 01:37:20 +02:00
|
|
|
ret=demux_avi_read_packet(demux,id,len,demux->idx_pos-1,0);
|
2001-02-24 21:28:24 +01:00
|
|
|
} else {
|
|
|
|
// skip it!
|
|
|
|
int skip=(len+1)&(~1); // total bytes in this chunk
|
|
|
|
stream_skip(demux->stream,skip);
|
|
|
|
}
|
|
|
|
|
|
|
|
} while(ret!=1);
|
|
|
|
fpos[0]=stream_tell(demux->stream);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|