Add a comment that explains why this header has no multiple inclusion guards.
[mplayer/greg.git] / libmpdemux / demux_avi.c
blobdb2a31fe533d8ff1f2e100e7c1bea8f67e6f0a83
1 // AVI file parser for DEMUXER v2.9 by A'rpi/ESP-team
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
7 #include "config.h"
8 #include "mp_msg.h"
9 #include "help_mp.h"
11 #include "stream/stream.h"
12 #include "demuxer.h"
13 #include "stheader.h"
15 #include "aviheader.h"
17 extern demuxer_t* init_avi_with_ogg(demuxer_t* demuxer);
18 extern int demux_ogg_open(demuxer_t* demuxer);
20 extern demuxer_desc_t demuxer_desc_avi_ni;
21 extern demuxer_desc_t demuxer_desc_avi_nini;
23 // PTS: 0=interleaved 1=BPS-based
24 int pts_from_bps=1;
26 // Select ds from ID
27 demux_stream_t* demux_avi_select_stream(demuxer_t *demux,unsigned int id){
28 int stream_id=avi_stream_id(id);
31 if(demux->video->id==-1)
32 if(demux->v_streams[stream_id])
33 demux->video->id=stream_id;
35 if(demux->audio->id==-1)
36 if(demux->a_streams[stream_id])
37 demux->audio->id=stream_id;
39 if(stream_id==demux->audio->id){
40 if(!demux->audio->sh){
41 sh_audio_t* sh;
42 avi_priv_t *priv=demux->priv;
43 sh=demux->audio->sh=demux->a_streams[stream_id];
44 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected AVI audio ID = %d\n",demux->audio->id);
45 if(sh->wf){
46 priv->audio_block_size=sh->wf->nBlockAlign;
47 if(!priv->audio_block_size){
48 // for PCM audio we can calculate the blocksize:
49 if(sh->format==1)
50 priv->audio_block_size=sh->wf->nChannels*(sh->wf->wBitsPerSample/8);
51 else
52 priv->audio_block_size=1; // hope the best...
53 } else {
54 // workaround old mencoder's bug:
55 if(sh->audio.dwSampleSize==1 && sh->audio.dwScale==1 &&
56 (sh->wf->nBlockAlign==1152 || sh->wf->nBlockAlign==576)){
57 mp_msg(MSGT_DEMUX,MSGL_WARN,MSGTR_WorkAroundBlockAlignHeaderBug);
58 priv->audio_block_size=1;
61 } else {
62 priv->audio_block_size=sh->audio.dwSampleSize;
65 return demux->audio;
67 if(stream_id==demux->video->id){
68 if(!demux->video->sh){
69 demux->video->sh=demux->v_streams[stream_id];
70 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected AVI video ID = %d\n",demux->video->id);
72 return demux->video;
74 if(id!=mmioFOURCC('J','U','N','K')){
75 // unknown
76 mp_msg(MSGT_DEMUX,MSGL_DBG2,"Unknown chunk: %.4s (%X)\n",(char *) &id,id);
77 //abort();
79 return NULL;
82 static int valid_fourcc(unsigned int id){
83 static const char valid[] = "0123456789abcdefghijklmnopqrstuvwxyz"
84 "ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
85 unsigned char* fcc=(unsigned char*)(&id);
86 return strchr(valid, fcc[0]) && strchr(valid, fcc[1]) &&
87 strchr(valid, fcc[2]) && strchr(valid, fcc[3]);
90 static int choose_chunk_len(unsigned int len1,unsigned int len2){
91 // len1 has a bit more priority than len2. len1!=len2
92 // Note: this is a first-idea-logic, may be wrong. comments welcomed.
94 // prefer small frames rather than 0
95 if(!len1) return (len2>0x80000) ? len1 : len2;
96 if(!len2) return (len1>0x100000) ? len2 : len1;
98 // choose the smaller value:
99 return (len1<len2)? len1 : len2;
102 static int demux_avi_read_packet(demuxer_t *demux,demux_stream_t *ds,unsigned int id,unsigned int len,int idxpos,int flags){
103 avi_priv_t *priv=demux->priv;
104 int skip;
105 float pts=0;
107 mp_dbg(MSGT_DEMUX,MSGL_DBG3,"demux_avi.read_packet: %X\n",id);
109 if(ds==demux->audio){
110 if(priv->pts_corrected==0){
111 if(priv->pts_has_video){
112 // we have video pts now
113 float delay=0;
114 if(((sh_audio_t*)(ds->sh))->wf->nAvgBytesPerSec)
115 delay=(float)priv->pts_corr_bytes/((sh_audio_t*)(ds->sh))->wf->nAvgBytesPerSec;
116 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);
117 //priv->pts_correction=-priv->avi_audio_pts+delay;
118 priv->pts_correction=delay-priv->avi_audio_pts;
119 priv->avi_audio_pts+=priv->pts_correction;
120 priv->pts_corrected=1;
121 } else
122 priv->pts_corr_bytes+=len;
124 if(pts_from_bps){
125 pts = priv->audio_block_no *
126 (float)((sh_audio_t*)demux->audio->sh)->audio.dwScale /
127 (float)((sh_audio_t*)demux->audio->sh)->audio.dwRate;
128 } else
129 pts=priv->avi_audio_pts; //+priv->pts_correction;
130 priv->avi_audio_pts=0;
131 // update blockcount:
132 priv->audio_block_no+=priv->audio_block_size ?
133 ((len+priv->audio_block_size-1)/priv->audio_block_size) : 1;
134 } else
135 if(ds==demux->video){
136 // video
137 if(priv->skip_video_frames>0){
138 // drop frame (seeking)
139 --priv->skip_video_frames;
140 ds=NULL;
143 pts = priv->avi_video_pts = priv->video_pack_no *
144 (float)((sh_video_t*)demux->video->sh)->video.dwScale /
145 (float)((sh_video_t*)demux->video->sh)->video.dwRate;
147 priv->avi_audio_pts=priv->avi_video_pts+priv->pts_correction;
148 priv->pts_has_video=1;
150 if(ds) ++priv->video_pack_no;
154 skip=(len+1)&(~1); // total bytes in this chunk
156 if(ds){
157 mp_dbg(MSGT_DEMUX,MSGL_DBG2,"DEMUX_AVI: Read %d data bytes from packet %04X\n",len,id);
158 ds_read_packet(ds,demux->stream,len,pts,idxpos,flags);
159 skip-=len;
161 if(skip){
162 mp_dbg(MSGT_DEMUX,MSGL_DBG2,"DEMUX_AVI: Skipping %d bytes from packet %04X\n",skip,id);
163 stream_skip(demux->stream,skip);
165 return ds?1:0;
168 // return value:
169 // 0 = EOF or no stream found
170 // 1 = successfully read a packet
171 static int demux_avi_fill_buffer(demuxer_t *demux, demux_stream_t *dsds){
172 avi_priv_t *priv=demux->priv;
173 unsigned int id=0;
174 unsigned int len;
175 int ret=0;
176 demux_stream_t *ds;
179 int flags=1;
180 AVIINDEXENTRY *idx=NULL;
181 if(priv->idx_size>0 && priv->idx_pos<priv->idx_size){
182 off_t pos;
184 idx=&((AVIINDEXENTRY *)priv->idx)[priv->idx_pos++];
186 if(idx->dwFlags&AVIIF_LIST){
187 // LIST
188 continue;
190 if(!demux_avi_select_stream(demux,idx->ckid)){
191 mp_dbg(MSGT_DEMUX,MSGL_DBG3,"Skip chunk %.4s (0x%X) \n",(char *)&idx->ckid,(unsigned int)idx->ckid);
192 continue; // skip this chunk
195 pos = (off_t)priv->idx_offset+AVI_IDX_OFFSET(idx);
196 if((pos<demux->movi_start || pos>=demux->movi_end) && (demux->movi_end>demux->movi_start) && (demux->stream->flags & STREAM_SEEK)){
197 mp_msg(MSGT_DEMUX,MSGL_V,"ChunkOffset out of range! idx=0x%"PRIX64" \n",(int64_t)pos);
198 continue;
200 stream_seek(demux->stream,pos);
201 demux->filepos=stream_tell(demux->stream);
202 id=stream_read_dword_le(demux->stream);
203 if(stream_eof(demux->stream)) return 0; // EOF!
205 if(id!=idx->ckid){
206 mp_msg(MSGT_DEMUX,MSGL_V,"ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id,(char *)&idx->ckid);
207 if(valid_fourcc(idx->ckid))
208 id=idx->ckid; // use index if valid
209 else
210 if(!valid_fourcc(id)) continue; // drop chunk if both id and idx bad
212 len=stream_read_dword_le(demux->stream);
213 if((len!=idx->dwChunkLength)&&((len+1)!=idx->dwChunkLength)){
214 mp_msg(MSGT_DEMUX,MSGL_V,"ChunkSize mismatch! raw=%d idx=%d \n",len,idx->dwChunkLength);
215 if(len>0x200000 && idx->dwChunkLength>0x200000) continue; // both values bad :(
216 len=choose_chunk_len(idx->dwChunkLength,len);
218 if(!(idx->dwFlags&AVIIF_KEYFRAME)) flags=0;
219 } else {
220 demux->filepos=stream_tell(demux->stream);
221 if(demux->filepos>=demux->movi_end && demux->movi_end>demux->movi_start && (demux->stream->flags & STREAM_SEEK)){
222 demux->stream->eof=1;
223 return 0;
225 id=stream_read_dword_le(demux->stream);
226 len=stream_read_dword_le(demux->stream);
227 if(stream_eof(demux->stream)) return 0; // EOF!
229 if(id==mmioFOURCC('L','I','S','T') || id==mmioFOURCC('R', 'I', 'F', 'F')){
230 id=stream_read_dword_le(demux->stream); // list or RIFF type
231 continue;
235 ds=demux_avi_select_stream(demux,id);
236 if(ds)
237 if(ds->packs+1>=MAX_PACKS || ds->bytes+len>=MAX_PACK_BYTES){
238 // this packet will cause a buffer overflow, switch to -ni mode!!!
239 mp_msg(MSGT_DEMUX,MSGL_WARN,MSGTR_SwitchToNi);
240 if(priv->idx_size>0){
241 // has index
242 demux->type=DEMUXER_TYPE_AVI_NI;
243 demux->desc=&demuxer_desc_avi_ni;
244 --priv->idx_pos; // hack
245 } else {
246 // no index
247 demux->type=DEMUXER_TYPE_AVI_NINI;
248 demux->desc=&demuxer_desc_avi_nini;
249 priv->idx_pos=demux->filepos; // hack
251 priv->idx_pos_v=priv->idx_pos_a=priv->idx_pos;
252 // quit now, we can't even (no enough buffer memory) read this packet :(
253 return -1;
256 ret=demux_avi_read_packet(demux,ds,id,len,priv->idx_pos-1,flags);
257 } while(ret!=1);
258 return 1;
262 // return value:
263 // 0 = EOF or no stream found
264 // 1 = successfully read a packet
265 int demux_avi_fill_buffer_ni(demuxer_t *demux,demux_stream_t* ds){
266 avi_priv_t *priv=demux->priv;
267 unsigned int id=0;
268 unsigned int len;
269 int ret=0;
272 int flags=1;
273 AVIINDEXENTRY *idx=NULL;
274 int idx_pos=0;
275 demux->filepos=stream_tell(demux->stream);
277 if(ds==demux->video) idx_pos=priv->idx_pos_v++; else
278 if(ds==demux->audio) idx_pos=priv->idx_pos_a++; else
279 idx_pos=priv->idx_pos++;
281 if(priv->idx_size>0 && idx_pos<priv->idx_size){
282 off_t pos;
283 idx=&((AVIINDEXENTRY *)priv->idx)[idx_pos];
285 if(idx->dwFlags&AVIIF_LIST){
286 // LIST
287 continue;
289 if(ds && demux_avi_select_stream(demux,idx->ckid)!=ds){
290 mp_dbg(MSGT_DEMUX,MSGL_DBG3,"Skip chunk %.4s (0x%X) \n",(char *)&idx->ckid,(unsigned int)idx->ckid);
291 continue; // skip this chunk
294 pos = priv->idx_offset+AVI_IDX_OFFSET(idx);
295 if((pos<demux->movi_start || pos>=demux->movi_end) && (demux->movi_end>demux->movi_start)){
296 mp_msg(MSGT_DEMUX,MSGL_V,"ChunkOffset out of range! current=0x%"PRIX64" idx=0x%"PRIX64" \n",(int64_t)demux->filepos,(int64_t)pos);
297 continue;
299 stream_seek(demux->stream,pos);
301 id=stream_read_dword_le(demux->stream);
303 if(stream_eof(demux->stream)) return 0;
305 if(id!=idx->ckid){
306 mp_msg(MSGT_DEMUX,MSGL_V,"ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id,(char *)&idx->ckid);
307 if(valid_fourcc(idx->ckid))
308 id=idx->ckid; // use index if valid
309 else
310 if(!valid_fourcc(id)) continue; // drop chunk if both id and idx bad
312 len=stream_read_dword_le(demux->stream);
313 if((len!=idx->dwChunkLength)&&((len+1)!=idx->dwChunkLength)){
314 mp_msg(MSGT_DEMUX,MSGL_V,"ChunkSize mismatch! raw=%d idx=%d \n",len,idx->dwChunkLength);
315 if(len>0x200000 && idx->dwChunkLength>0x200000) continue; // both values bad :(
316 len=choose_chunk_len(idx->dwChunkLength,len);
318 if(!(idx->dwFlags&AVIIF_KEYFRAME)) flags=0;
319 } else return 0;
320 ret=demux_avi_read_packet(demux,demux_avi_select_stream(demux,id),id,len,idx_pos,flags);
321 } while(ret!=1);
322 return 1;
326 // return value:
327 // 0 = EOF or no stream found
328 // 1 = successfully read a packet
329 int demux_avi_fill_buffer_nini(demuxer_t *demux,demux_stream_t* ds){
330 avi_priv_t *priv=demux->priv;
331 unsigned int id=0;
332 unsigned int len;
333 int ret=0;
334 off_t *fpos=NULL;
336 if(ds==demux->video) fpos=&priv->idx_pos_v; else
337 if(ds==demux->audio) fpos=&priv->idx_pos_a; else
338 return 0;
340 stream_seek(demux->stream,fpos[0]);
344 demux->filepos=stream_tell(demux->stream);
345 if(demux->filepos>=demux->movi_end && (demux->movi_end>demux->movi_start)){
346 ds->eof=1;
347 return 0;
350 id=stream_read_dword_le(demux->stream);
351 len=stream_read_dword_le(demux->stream);
353 if(stream_eof(demux->stream)) return 0;
355 if(id==mmioFOURCC('L','I','S','T')){
356 id=stream_read_dword_le(demux->stream); // list type
357 continue;
360 if(id==mmioFOURCC('R','I','F','F')){
361 mp_msg(MSGT_DEMUX,MSGL_V,"additional RIFF header...\n");
362 id=stream_read_dword_le(demux->stream); // "AVIX"
363 continue;
366 if(ds==demux_avi_select_stream(demux,id)){
367 // read it!
368 ret=demux_avi_read_packet(demux,ds,id,len,priv->idx_pos-1,0);
369 } else {
370 // skip it!
371 int skip=(len+1)&(~1); // total bytes in this chunk
372 stream_skip(demux->stream,skip);
375 } while(ret!=1);
376 fpos[0]=stream_tell(demux->stream);
377 return 1;
380 // AVI demuxer parameters:
381 int index_mode=-1; // -1=untouched 0=don't use index 1=use (geneate) index
382 char *index_file_save = NULL, *index_file_load = NULL;
383 int force_ni=0; // force non-interleaved AVI parsing
385 void read_avi_header(demuxer_t *demuxer,int index_mode);
387 static demuxer_t* demux_open_avi(demuxer_t* demuxer){
388 demux_stream_t *d_audio=demuxer->audio;
389 demux_stream_t *d_video=demuxer->video;
390 sh_audio_t *sh_audio=NULL;
391 sh_video_t *sh_video=NULL;
392 avi_priv_t* priv=malloc(sizeof(avi_priv_t));
394 // priv struct:
395 priv->avi_audio_pts=priv->avi_video_pts=0.0f;
396 priv->pts_correction=0.0f;
397 priv->skip_video_frames=0;
398 priv->pts_corr_bytes=0;
399 priv->pts_has_video=priv->pts_corrected=0;
400 priv->video_pack_no=0;
401 priv->audio_block_no=0;
402 priv->audio_block_size=0;
403 priv->isodml = 0;
404 priv->suidx_size = 0;
405 priv->suidx = NULL;
407 demuxer->priv=(void*)priv;
409 //---- AVI header:
410 read_avi_header(demuxer,(demuxer->stream->flags & STREAM_SEEK_BW)?index_mode:-2);
412 if(demuxer->audio->id>=0 && !demuxer->a_streams[demuxer->audio->id]){
413 mp_msg(MSGT_DEMUX,MSGL_WARN,MSGTR_InvalidAudioStreamNosound,demuxer->audio->id);
414 demuxer->audio->id=-2; // disabled
416 if(demuxer->video->id>=0 && !demuxer->v_streams[demuxer->video->id]){
417 mp_msg(MSGT_DEMUX,MSGL_WARN,MSGTR_InvalidAudioStreamUsingDefault,demuxer->video->id);
418 demuxer->video->id=-1; // autodetect
421 stream_reset(demuxer->stream);
422 stream_seek(demuxer->stream,demuxer->movi_start);
423 priv->idx_pos=0;
424 priv->idx_pos_a=0;
425 priv->idx_pos_v=0;
426 if(priv->idx_size>1){
427 // decide index format:
428 #if 1
429 if((AVI_IDX_OFFSET(&((AVIINDEXENTRY *)priv->idx)[0])<demuxer->movi_start ||
430 AVI_IDX_OFFSET(&((AVIINDEXENTRY *)priv->idx)[1])<demuxer->movi_start )&& !priv->isodml)
431 priv->idx_offset=demuxer->movi_start-4;
432 else
433 priv->idx_offset=0;
434 #else
435 if(AVI_IDX_OFFSET(&((AVIINDEXENTRY *)priv->idx)[0])<demuxer->movi_start)
436 priv->idx_offset=demuxer->movi_start-4;
437 else
438 priv->idx_offset=0;
439 #endif
440 mp_msg(MSGT_DEMUX,MSGL_V,"AVI index offset: 0x%X (movi=0x%X idx0=0x%X idx1=0x%X)\n",
441 (int)priv->idx_offset,(int)demuxer->movi_start,
442 (int)((AVIINDEXENTRY *)priv->idx)[0].dwChunkOffset,
443 (int)((AVIINDEXENTRY *)priv->idx)[1].dwChunkOffset);
446 if(priv->idx_size>0){
447 // check that file is non-interleaved:
448 int i;
449 off_t a_pos=-1;
450 off_t v_pos=-1;
451 for(i=0;i<priv->idx_size;i++){
452 AVIINDEXENTRY* idx=&((AVIINDEXENTRY *)priv->idx)[i];
453 demux_stream_t* ds=demux_avi_select_stream(demuxer,idx->ckid);
454 off_t pos = priv->idx_offset + AVI_IDX_OFFSET(idx);
455 if(a_pos==-1 && ds==demuxer->audio){
456 a_pos=pos;
457 if(v_pos!=-1) break;
459 if(v_pos==-1 && ds==demuxer->video){
460 v_pos=pos;
461 if(a_pos!=-1) break;
464 if(v_pos==-1){
465 mp_msg(MSGT_DEMUX,MSGL_ERR,"AVI_NI: " MSGTR_MissingVideoStream);
466 return NULL;
468 if(a_pos==-1){
469 d_audio->sh=sh_audio=NULL;
470 } else {
471 if(force_ni || abs(a_pos-v_pos)>0x100000){ // distance > 1MB
472 mp_msg(MSGT_DEMUX,MSGL_INFO,MSGTR_NI_Message,force_ni?MSGTR_NI_Forced:MSGTR_NI_Detected);
473 demuxer->type=DEMUXER_TYPE_AVI_NI; // HACK!!!!
474 demuxer->desc=&demuxer_desc_avi_ni; // HACK!!!!
475 pts_from_bps=1; // force BPS sync!
478 } else {
479 // no index
480 if(force_ni){
481 mp_msg(MSGT_DEMUX,MSGL_INFO,MSGTR_UsingNINI);
482 demuxer->type=DEMUXER_TYPE_AVI_NINI; // HACK!!!!
483 demuxer->desc=&demuxer_desc_avi_nini; // HACK!!!!
484 priv->idx_pos_a=
485 priv->idx_pos_v=demuxer->movi_start;
486 pts_from_bps=1; // force BPS sync!
488 demuxer->seekable=0;
490 if(!ds_fill_buffer(d_video)){
491 mp_msg(MSGT_DEMUX,MSGL_ERR,"AVI: " MSGTR_MissingVideoStreamBug);
492 return NULL;
494 sh_video=d_video->sh;sh_video->ds=d_video;
495 if(d_audio->id!=-2){
496 mp_msg(MSGT_DEMUX,MSGL_V,"AVI: Searching for audio stream (id:%d)\n",d_audio->id);
497 if(!priv->audio_streams || !ds_fill_buffer(d_audio)){
498 mp_msg(MSGT_DEMUX,MSGL_INFO,"AVI: " MSGTR_MissingAudioStream);
499 d_audio->sh=sh_audio=NULL;
500 } else {
501 sh_audio=d_audio->sh;sh_audio->ds=d_audio;
505 // calculating audio/video bitrate:
506 if(priv->idx_size>0){
507 // we have index, let's count 'em!
508 int64_t vsize=0;
509 int64_t asize=0;
510 size_t vsamples=0;
511 size_t asamples=0;
512 int i;
513 for(i=0;i<priv->idx_size;i++){
514 int id=avi_stream_id(((AVIINDEXENTRY *)priv->idx)[i].ckid);
515 int len=((AVIINDEXENTRY *)priv->idx)[i].dwChunkLength;
516 if(sh_video->ds->id == id) {
517 vsize+=len;
518 ++vsamples;
520 else if(sh_audio && sh_audio->ds->id == id) {
521 asize+=len;
522 asamples+=(len+priv->audio_block_size-1)/priv->audio_block_size;
525 mp_msg(MSGT_DEMUX,MSGL_V,"AVI video size=%"PRId64" (%u) audio size=%"PRId64" (%u)\n",vsize,vsamples,asize,asamples);
526 priv->numberofframes=vsamples;
527 sh_video->i_bps=((float)vsize/(float)vsamples)*(float)sh_video->video.dwRate/(float)sh_video->video.dwScale;
528 if(sh_audio) sh_audio->i_bps=((float)asize/(float)asamples)*(float)sh_audio->audio.dwRate/(float)sh_audio->audio.dwScale;
529 } else {
530 // guessing, results may be inaccurate:
531 int64_t vsize;
532 int64_t asize=0;
534 if((priv->numberofframes=sh_video->video.dwLength)<=1)
535 // bad video header, try to get number of frames from audio
536 if(sh_audio && sh_audio->wf->nAvgBytesPerSec) priv->numberofframes=sh_video->fps*sh_audio->audio.dwLength/sh_audio->audio.dwRate*sh_audio->audio.dwScale;
537 if(priv->numberofframes<=1){
538 mp_msg(MSGT_SEEK,MSGL_WARN,MSGTR_CouldntDetFNo);
539 priv->numberofframes=0;
542 if(sh_audio){
543 if(sh_audio->wf->nAvgBytesPerSec && sh_audio->audio.dwSampleSize!=1){
544 asize=(float)sh_audio->wf->nAvgBytesPerSec*sh_audio->audio.dwLength*sh_audio->audio.dwScale/sh_audio->audio.dwRate;
545 } else {
546 asize=sh_audio->audio.dwLength;
547 sh_audio->i_bps=(float)asize/(sh_video->frametime*priv->numberofframes);
550 vsize=demuxer->movi_end-demuxer->movi_start-asize-8*priv->numberofframes;
551 mp_msg(MSGT_DEMUX,MSGL_V,"AVI video size=%"PRId64" (%u) audio size=%"PRId64"\n",vsize,priv->numberofframes,asize);
552 sh_video->i_bps=(float)vsize/(sh_video->frametime*priv->numberofframes);
555 return demuxer;
560 void demux_seek_avi(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags){
561 avi_priv_t *priv=demuxer->priv;
562 demux_stream_t *d_audio=demuxer->audio;
563 demux_stream_t *d_video=demuxer->video;
564 sh_audio_t *sh_audio=d_audio->sh;
565 sh_video_t *sh_video=d_video->sh;
566 float skip_audio_secs=0;
568 //FIXME: OFF_T - Didn't check AVI case yet (avi files can't be >2G anyway?)
569 //================= seek in AVI ==========================
570 int rel_seek_frames=rel_seek_secs*sh_video->fps;
571 int video_chunk_pos=d_video->pos;
572 int i;
574 if(flags&1){
575 // seek absolute
576 video_chunk_pos=0;
579 if(flags&2){
580 rel_seek_frames=rel_seek_secs*priv->numberofframes;
583 priv->skip_video_frames=0;
584 priv->avi_audio_pts=0;
586 // ------------ STEP 1: find nearest video keyframe chunk ------------
587 // find nearest video keyframe chunk pos:
588 if(rel_seek_frames>0){
589 // seek forward
590 while(video_chunk_pos<priv->idx_size-1){
591 int id=((AVIINDEXENTRY *)priv->idx)[video_chunk_pos].ckid;
592 if(avi_stream_id(id)==d_video->id){ // video frame
593 if((--rel_seek_frames)<0 && ((AVIINDEXENTRY *)priv->idx)[video_chunk_pos].dwFlags&AVIIF_KEYFRAME) break;
595 ++video_chunk_pos;
597 } else {
598 // seek backward
599 while(video_chunk_pos>0){
600 int id=((AVIINDEXENTRY *)priv->idx)[video_chunk_pos].ckid;
601 if(avi_stream_id(id)==d_video->id){ // video frame
602 if((++rel_seek_frames)>0 && ((AVIINDEXENTRY *)priv->idx)[video_chunk_pos].dwFlags&AVIIF_KEYFRAME) break;
604 --video_chunk_pos;
607 priv->idx_pos_a=priv->idx_pos_v=priv->idx_pos=video_chunk_pos;
609 // re-calc video pts:
610 d_video->pack_no=0;
611 for(i=0;i<video_chunk_pos;i++){
612 int id=((AVIINDEXENTRY *)priv->idx)[i].ckid;
613 if(avi_stream_id(id)==d_video->id) ++d_video->pack_no;
615 priv->video_pack_no=
616 sh_video->num_frames=sh_video->num_frames_decoded=d_video->pack_no;
617 priv->avi_video_pts=d_video->pack_no*(float)sh_video->video.dwScale/(float)sh_video->video.dwRate;
618 d_video->pos=video_chunk_pos;
620 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);
622 // ------------ STEP 2: seek audio, find the right chunk & pos ------------
624 d_audio->pack_no=0;
625 priv->audio_block_no=0;
626 d_audio->dpos=0;
628 if(sh_audio){
629 int i;
630 int len=0;
631 int skip_audio_bytes=0;
632 int curr_audio_pos=-1;
633 int audio_chunk_pos=-1;
634 int chunk_max=(demuxer->type==DEMUXER_TYPE_AVI)?video_chunk_pos:priv->idx_size;
636 if(sh_audio->audio.dwSampleSize){
637 // constant rate audio stream
638 /* immediate seeking to audio position, including when streams are delayed */
639 curr_audio_pos=(priv->avi_video_pts + audio_delay)*(float)sh_audio->audio.dwRate/(float)sh_audio->audio.dwScale;
640 curr_audio_pos*=sh_audio->audio.dwSampleSize;
642 // find audio chunk pos:
643 for(i=0;i<chunk_max;i++){
644 int id=((AVIINDEXENTRY *)priv->idx)[i].ckid;
645 if(avi_stream_id(id)==d_audio->id){
646 len=((AVIINDEXENTRY *)priv->idx)[i].dwChunkLength;
647 if(d_audio->dpos<=curr_audio_pos && curr_audio_pos<(d_audio->dpos+len)){
648 break;
650 ++d_audio->pack_no;
651 priv->audio_block_no+=priv->audio_block_size ?
652 ((len+priv->audio_block_size-1)/priv->audio_block_size) : 1;
653 d_audio->dpos+=len;
656 audio_chunk_pos=i;
657 skip_audio_bytes=curr_audio_pos-d_audio->dpos;
659 mp_msg(MSGT_SEEK,MSGL_V,"SEEK: i=%d (max:%d) dpos=%d (wanted:%d) \n",
660 i,chunk_max,(int)d_audio->dpos,curr_audio_pos);
662 } else {
663 // VBR audio
664 /* immediate seeking to audio position, including when streams are delayed */
665 int chunks=(priv->avi_video_pts + audio_delay)*(float)sh_audio->audio.dwRate/(float)sh_audio->audio.dwScale;
666 audio_chunk_pos=0;
668 // find audio chunk pos:
669 for(i=0;i<priv->idx_size && chunks>0;i++){
670 int id=((AVIINDEXENTRY *)priv->idx)[i].ckid;
671 if(avi_stream_id(id)==d_audio->id){
672 len=((AVIINDEXENTRY *)priv->idx)[i].dwChunkLength;
673 if(i>chunk_max){
674 skip_audio_bytes+=len;
675 } else {
676 ++d_audio->pack_no;
677 priv->audio_block_no+=priv->audio_block_size ?
678 ((len+priv->audio_block_size-1)/priv->audio_block_size) : 1;
679 d_audio->dpos+=len;
680 audio_chunk_pos=i;
682 if(priv->audio_block_size)
683 chunks-=(len+priv->audio_block_size-1)/priv->audio_block_size;
688 // Now we have:
689 // audio_chunk_pos = chunk no in index table (it's <=chunk_max)
690 // skip_audio_bytes = bytes to be skipped after chunk seek
691 // d-audio->pack_no = chunk_no in stream at audio_chunk_pos
692 // d_audio->dpos = bytepos in stream at audio_chunk_pos
693 // let's seek!
695 // update stream position:
696 d_audio->pos=audio_chunk_pos;
698 if(demuxer->type==DEMUXER_TYPE_AVI){
699 // interleaved stream:
700 if(audio_chunk_pos<video_chunk_pos){
701 // calc priv->skip_video_frames & adjust video pts counter:
702 for(i=audio_chunk_pos;i<video_chunk_pos;i++){
703 int id=((AVIINDEXENTRY *)priv->idx)[i].ckid;
704 if(avi_stream_id(id)==d_video->id) ++priv->skip_video_frames;
706 // requires for correct audio pts calculation (demuxer):
707 priv->avi_video_pts-=priv->skip_video_frames*(float)sh_video->video.dwScale/(float)sh_video->video.dwRate;
708 priv->avi_audio_pts=priv->avi_video_pts;
709 // set index position:
710 priv->idx_pos_a=priv->idx_pos_v=priv->idx_pos=audio_chunk_pos;
712 } else {
713 // non-interleaved stream:
714 priv->idx_pos_a=audio_chunk_pos;
715 priv->idx_pos_v=video_chunk_pos;
716 priv->idx_pos=(audio_chunk_pos<video_chunk_pos)?audio_chunk_pos:video_chunk_pos;
719 mp_msg(MSGT_SEEK,MSGL_V,"SEEK: idx=%d (a:%d v:%d) v.skip=%d a.skip=%d/%4.3f \n",
720 (int)priv->idx_pos,audio_chunk_pos,video_chunk_pos,
721 (int)priv->skip_video_frames,skip_audio_bytes,skip_audio_secs);
723 if(skip_audio_bytes){
724 demux_read_data(d_audio,NULL,skip_audio_bytes);
728 d_video->pts=priv->avi_video_pts; // OSD
733 void demux_close_avi(demuxer_t *demuxer) {
734 avi_priv_t* priv=demuxer->priv;
736 if(!priv)
737 return;
739 if(priv->idx_size > 0)
740 free(priv->idx);
741 free(priv);
745 static int demux_avi_control(demuxer_t *demuxer,int cmd, void *arg){
746 avi_priv_t *priv=demuxer->priv;
747 demux_stream_t *d_video=demuxer->video;
748 sh_video_t *sh_video=d_video->sh;
750 switch(cmd) {
751 case DEMUXER_CTRL_GET_TIME_LENGTH:
752 if (!priv->numberofframes || !sh_video) return DEMUXER_CTRL_DONTKNOW;
753 *((double *)arg)=(double)priv->numberofframes/sh_video->fps;
754 if (sh_video->video.dwLength<=1) return DEMUXER_CTRL_GUESS;
755 return DEMUXER_CTRL_OK;
757 case DEMUXER_CTRL_GET_PERCENT_POS:
758 if (!priv->numberofframes || !sh_video) {
759 return DEMUXER_CTRL_DONTKNOW;
761 *((int *)arg)=(int)(priv->video_pack_no*100/priv->numberofframes);
762 if (sh_video->video.dwLength<=1) return DEMUXER_CTRL_GUESS;
763 return DEMUXER_CTRL_OK;
765 case DEMUXER_CTRL_SWITCH_AUDIO:
766 case DEMUXER_CTRL_SWITCH_VIDEO: {
767 int audio = (cmd == DEMUXER_CTRL_SWITCH_AUDIO);
768 demux_stream_t *ds = audio ? demuxer->audio : demuxer->video;
769 void **streams = audio ? demuxer->a_streams : demuxer->v_streams;
770 int maxid = FFMIN(100, audio ? MAX_A_STREAMS : MAX_V_STREAMS);
771 int chunkid;
772 if (ds->id < -1)
773 return DEMUXER_CTRL_NOTIMPL;
775 if (*(int *)arg >= 0)
776 ds->id = *(int *)arg;
777 else {
778 int i;
779 for (i = 0; i < maxid; i++) {
780 if (++ds->id >= maxid) ds->id = 0;
781 if (streams[ds->id]) break;
785 chunkid = (ds->id / 10 + '0') | (ds->id % 10 + '0') << 8;
786 ds->sh = NULL;
787 if (!streams[ds->id]) // stream not available
788 ds->id = -1;
789 else
790 demux_avi_select_stream(demuxer, chunkid);
791 *(int *)arg = ds->id;
792 return DEMUXER_CTRL_OK;
795 default:
796 return DEMUXER_CTRL_NOTIMPL;
801 static int avi_check_file(demuxer_t *demuxer)
803 int id=stream_read_dword_le(demuxer->stream); // "RIFF"
805 if((id==mmioFOURCC('R','I','F','F')) || (id==mmioFOURCC('O','N','2',' '))) {
806 stream_read_dword_le(demuxer->stream); //filesize
807 id=stream_read_dword_le(demuxer->stream); // "AVI "
808 if(id==formtypeAVI)
809 return DEMUXER_TYPE_AVI;
810 // "Samsung Digimax i6 PMP" crap according to bug 742
811 if(id==mmioFOURCC('A','V','I',0x19))
812 return DEMUXER_TYPE_AVI;
813 if(id==mmioFOURCC('O','N','2','f')){
814 mp_msg(MSGT_DEMUXER,MSGL_INFO,MSGTR_ON2AviFormat);
815 return DEMUXER_TYPE_AVI;
819 return 0;
823 static demuxer_t* demux_open_hack_avi(demuxer_t *demuxer)
825 sh_audio_t* sh_a;
827 demuxer = demux_open_avi(demuxer);
828 if(!demuxer) return NULL; // failed to open
829 sh_a = demuxer->audio->sh;
830 if(demuxer->audio->id != -2 && sh_a) {
831 #ifdef HAVE_OGGVORBIS
832 // support for Ogg-in-AVI:
833 if(sh_a->format == 0xFFFE)
834 demuxer = init_avi_with_ogg(demuxer);
835 else if(sh_a->format == 0x674F) {
836 stream_t* s;
837 demuxer_t *od;
838 s = new_ds_stream(demuxer->audio);
839 od = new_demuxer(s,DEMUXER_TYPE_OGG,-1,-2,-2,NULL);
840 if(!demux_ogg_open(od)) {
841 mp_msg( MSGT_DEMUXER,MSGL_ERR,MSGTR_ErrorOpeningOGGDemuxer);
842 free_stream(s);
843 demuxer->audio->id = -2;
844 } else
845 demuxer = new_demuxers_demuxer(demuxer,od,demuxer);
847 #endif
850 return demuxer;
854 demuxer_desc_t demuxer_desc_avi = {
855 "AVI demuxer",
856 "avi",
857 "AVI",
858 "Arpi?",
859 "AVI files, including non interleaved files",
860 DEMUXER_TYPE_AVI,
861 1, // safe autodetect
862 avi_check_file,
863 demux_avi_fill_buffer,
864 demux_open_hack_avi,
865 demux_close_avi,
866 demux_seek_avi,
867 demux_avi_control
870 demuxer_desc_t demuxer_desc_avi_ni = {
871 "AVI demuxer, non-interleaved",
872 "avini",
873 "AVI",
874 "Arpi?",
875 "AVI files, including non interleaved files",
876 DEMUXER_TYPE_AVI,
877 1, // safe autodetect
878 avi_check_file,
879 demux_avi_fill_buffer_ni,
880 demux_open_hack_avi,
881 demux_close_avi,
882 demux_seek_avi,
883 demux_avi_control
886 demuxer_desc_t demuxer_desc_avi_nini = {
887 "AVI demuxer, non-interleaved and no index",
888 "avinini",
889 "AVI",
890 "Arpi?",
891 "AVI files, including non interleaved files",
892 DEMUXER_TYPE_AVI,
893 1, // safe autodetect
894 avi_check_file,
895 demux_avi_fill_buffer_nini,
896 demux_open_hack_avi,
897 demux_close_avi,
898 demux_seek_avi,
899 demux_avi_control