stream.h: check against huge negative values in stream_seek()
[mplayer/glamo.git] / libmpdemux / demux_avi.c
blob904f6b86756375048d5d8e3ff0a8c898f65b5ff0
1 /*
2 * AVI file parser for DEMUXER v2.9
3 * Copyright (c) 2001 A'rpi/ESP-team
5 * This file is part of MPlayer.
7 * MPlayer is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * MPlayer is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
26 #include "config.h"
27 #include "mp_msg.h"
29 #include "stream/stream.h"
30 #include "demuxer.h"
31 #include "stheader.h"
32 #include "demux_ogg.h"
33 #include "aviheader.h"
35 extern const demuxer_desc_t demuxer_desc_avi_ni;
36 extern const demuxer_desc_t demuxer_desc_avi_nini;
38 // PTS: 0=interleaved 1=BPS-based
39 int pts_from_bps=1;
41 static void update_audio_block_size(demuxer_t *demux)
43 avi_priv_t *priv = demux->priv;
44 sh_audio_t *sh = demux->audio->sh;
45 if (!sh)
46 return;
47 priv->audio_block_size = sh->audio.dwSampleSize;
48 if (sh->wf) {
49 priv->audio_block_size = sh->wf->nBlockAlign;
50 if (!priv->audio_block_size) {
51 // for PCM audio we can calculate the blocksize:
52 if (sh->format == 1)
53 priv->audio_block_size = sh->wf->nChannels*(sh->wf->wBitsPerSample/8);
54 else
55 priv->audio_block_size = 1; // hope the best...
56 } else {
57 // workaround old mencoder bug:
58 if (sh->audio.dwSampleSize == 1 && sh->audio.dwScale == 1 &&
59 (sh->wf->nBlockAlign == 1152 || sh->wf->nBlockAlign == 576)) {
60 mp_tmsg(MSGT_DEMUX,MSGL_WARN,"AVI: Working around CBR-MP3 nBlockAlign header bug!\n");
61 priv->audio_block_size = 1;
67 // Select ds from ID
68 static demux_stream_t *demux_avi_select_stream(demuxer_t *demux,
69 unsigned int id)
71 int stream_id=avi_stream_id(id);
74 if(demux->video->id==-1)
75 if(demux->v_streams[stream_id])
76 demux->video->id=stream_id;
78 if(demux->audio->id==-1)
79 if(demux->a_streams[stream_id])
80 demux->audio->id=stream_id;
82 if(stream_id==demux->audio->id){
83 if(!demux->audio->sh){
84 demux->audio->sh=demux->a_streams[stream_id];
85 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected AVI audio ID = %d\n",demux->audio->id);
86 update_audio_block_size(demux);
88 return demux->audio;
90 if(stream_id==demux->video->id){
91 if(!demux->video->sh){
92 demux->video->sh=demux->v_streams[stream_id];
93 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected AVI video ID = %d\n",demux->video->id);
95 return demux->video;
97 if(id!=mmioFOURCC('J','U','N','K')){
98 // unknown
99 mp_msg(MSGT_DEMUX,MSGL_DBG2,"Unknown chunk: %.4s (%X)\n",(char *) &id,id);
100 //abort();
102 return NULL;
105 static int valid_fourcc(unsigned int id){
106 static const char valid[] = "0123456789abcdefghijklmnopqrstuvwxyz"
107 "ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
108 unsigned char* fcc=(unsigned char*)(&id);
109 return strchr(valid, fcc[0]) && strchr(valid, fcc[1]) &&
110 strchr(valid, fcc[2]) && strchr(valid, fcc[3]);
113 static int valid_stream_id(unsigned int id) {
114 unsigned char* fcc=(unsigned char*)(&id);
115 return fcc[0] >= '0' && fcc[0] <= '9' && fcc[1] >= '0' && fcc[1] <= '9' &&
116 ((fcc[2] == 'w' && fcc[3] == 'b') || (fcc[2] == 'd' && fcc[3] == 'c'));
119 static int choose_chunk_len(unsigned int len1,unsigned int len2){
120 // len1 has a bit more priority than len2. len1!=len2
121 // Note: this is a first-idea-logic, may be wrong. comments welcomed.
123 // prefer small frames rather than 0
124 if(!len1) return (len2>0x80000) ? len1 : len2;
125 if(!len2) return (len1>0x100000) ? len2 : len1;
127 // choose the smaller value:
128 return (len1<len2)? len1 : len2;
131 static int demux_avi_read_packet(demuxer_t *demux,demux_stream_t *ds,unsigned int id,unsigned int len,int idxpos,int flags){
132 avi_priv_t *priv=demux->priv;
133 int skip;
134 float pts=0;
136 mp_dbg(MSGT_DEMUX,MSGL_DBG3,"demux_avi.read_packet: %X\n",id);
138 if(ds==demux->audio){
139 if(priv->pts_corrected==0){
140 if(priv->pts_has_video){
141 // we have video pts now
142 float delay=0;
143 if(((sh_audio_t*)(ds->sh))->wf->nAvgBytesPerSec)
144 delay=(float)priv->pts_corr_bytes/((sh_audio_t*)(ds->sh))->wf->nAvgBytesPerSec;
145 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);
146 //priv->pts_correction=-priv->avi_audio_pts+delay;
147 priv->pts_correction=delay-priv->avi_audio_pts;
148 priv->avi_audio_pts+=priv->pts_correction;
149 priv->pts_corrected=1;
150 } else
151 priv->pts_corr_bytes+=len;
153 if(pts_from_bps){
154 pts = priv->audio_block_no *
155 (float)((sh_audio_t*)demux->audio->sh)->audio.dwScale /
156 (float)((sh_audio_t*)demux->audio->sh)->audio.dwRate;
157 } else
158 pts=priv->avi_audio_pts; //+priv->pts_correction;
159 priv->avi_audio_pts=0;
160 // update blockcount:
161 priv->audio_block_no+=
162 (len+priv->audio_block_size-1)/priv->audio_block_size;
163 } else
164 if(ds==demux->video){
165 // video
166 if(priv->skip_video_frames>0){
167 // drop frame (seeking)
168 --priv->skip_video_frames;
169 ds=NULL;
172 pts = priv->avi_video_pts = priv->video_pack_no *
173 (float)((sh_video_t*)demux->video->sh)->video.dwScale /
174 (float)((sh_video_t*)demux->video->sh)->video.dwRate;
176 priv->avi_audio_pts=priv->avi_video_pts+priv->pts_correction;
177 priv->pts_has_video=1;
179 if(ds) ++priv->video_pack_no;
183 skip=(len+1)&(~1); // total bytes in this chunk
185 if(ds){
186 mp_dbg(MSGT_DEMUX,MSGL_DBG2,"DEMUX_AVI: Read %d data bytes from packet %04X\n",len,id);
187 ds_read_packet(ds,demux->stream,len,pts,idxpos,flags);
188 skip-=len;
190 skip = FFMAX(skip, 0);
191 if (avi_stream_id(id) > 99 && id != mmioFOURCC('J','U','N','K'))
192 skip = FFMIN(skip, 65536);
193 if(skip){
194 mp_dbg(MSGT_DEMUX,MSGL_DBG2,"DEMUX_AVI: Skipping %d bytes from packet %04X\n",skip,id);
195 stream_skip(demux->stream,skip);
197 return ds?1:0;
200 static uint32_t avi_find_id(stream_t *stream) {
201 uint32_t id = stream_read_dword_le(stream);
202 if (!id) {
203 mp_msg(MSGT_DEMUX, MSGL_WARN, "Incomplete stream? Trying resync.\n");
204 do {
205 id = stream_read_dword_le(stream);
206 if (stream_eof(stream)) return 0;
207 } while (avi_stream_id(id) > 99);
209 return id;
212 // return value:
213 // 0 = EOF or no stream found
214 // 1 = successfully read a packet
215 static int demux_avi_fill_buffer(demuxer_t *demux, demux_stream_t *dsds){
216 avi_priv_t *priv=demux->priv;
217 unsigned int id=0;
218 unsigned int len;
219 int ret=0;
220 demux_stream_t *ds;
223 int flags=1;
224 AVIINDEXENTRY *idx=NULL;
225 if(priv->idx_size>0 && priv->idx_pos<priv->idx_size){
226 off_t pos;
228 idx=&((AVIINDEXENTRY *)priv->idx)[priv->idx_pos++];
230 if(idx->dwFlags&AVIIF_LIST){
231 if (!valid_stream_id(idx->ckid))
232 // LIST
233 continue;
234 if (!priv->warned_unaligned)
235 mp_msg(MSGT_DEMUX, MSGL_WARN, "Looks like unaligned chunk in index, broken AVI file!\n");
236 priv->warned_unaligned = 1;
238 if(!demux_avi_select_stream(demux,idx->ckid)){
239 mp_dbg(MSGT_DEMUX,MSGL_DBG3,"Skip chunk %.4s (0x%X) \n",(char *)&idx->ckid,(unsigned int)idx->ckid);
240 continue; // skip this chunk
243 pos = (off_t)priv->idx_offset+AVI_IDX_OFFSET(idx);
244 if((pos<demux->movi_start || pos>=demux->movi_end) && (demux->movi_end>demux->movi_start) && (demux->stream->flags & MP_STREAM_SEEK)){
245 mp_msg(MSGT_DEMUX,MSGL_V,"ChunkOffset out of range! idx=0x%"PRIX64" \n",(int64_t)pos);
246 continue;
248 stream_seek(demux->stream,pos);
249 demux->filepos=stream_tell(demux->stream);
250 id=stream_read_dword_le(demux->stream);
251 if(stream_eof(demux->stream)) return 0; // EOF!
253 if(id!=idx->ckid){
254 mp_msg(MSGT_DEMUX,MSGL_V,"ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id,(char *)&idx->ckid);
255 if(valid_fourcc(idx->ckid))
256 id=idx->ckid; // use index if valid
257 else
258 if(!valid_fourcc(id)) continue; // drop chunk if both id and idx bad
260 len=stream_read_dword_le(demux->stream);
261 if((len!=idx->dwChunkLength)&&((len+1)!=idx->dwChunkLength)){
262 mp_msg(MSGT_DEMUX,MSGL_V,"ChunkSize mismatch! raw=%d idx=%d \n",len,idx->dwChunkLength);
263 if(len>0x200000 && idx->dwChunkLength>0x200000) continue; // both values bad :(
264 len=choose_chunk_len(idx->dwChunkLength,len);
266 if(!(idx->dwFlags&AVIIF_KEYFRAME)) flags=0;
267 } else {
268 demux->filepos=stream_tell(demux->stream);
269 if(demux->filepos>=demux->movi_end && demux->movi_end>demux->movi_start && (demux->stream->flags & MP_STREAM_SEEK)){
270 demux->stream->eof=1;
271 return 0;
273 id=avi_find_id(demux->stream);
274 len=stream_read_dword_le(demux->stream);
275 if(stream_eof(demux->stream)) return 0; // EOF!
277 if(id==mmioFOURCC('L','I','S','T') || id==mmioFOURCC('R', 'I', 'F', 'F')){
278 id=stream_read_dword_le(demux->stream); // list or RIFF type
279 continue;
283 ds=demux_avi_select_stream(demux,id);
284 if(ds)
285 if(ds->packs+1>=MAX_PACKS || ds->bytes+len>=MAX_PACK_BYTES){
286 // this packet will cause a buffer overflow, switch to -ni mode!!!
287 mp_tmsg(MSGT_DEMUX,MSGL_WARN,"\nBadly interleaved AVI file detected - switching to -ni mode...\n");
288 if(priv->idx_size>0){
289 // has index
290 demux->type=DEMUXER_TYPE_AVI_NI;
291 demux->desc=&demuxer_desc_avi_ni;
292 --priv->idx_pos; // hack
293 } else {
294 // no index
295 demux->type=DEMUXER_TYPE_AVI_NINI;
296 demux->desc=&demuxer_desc_avi_nini;
297 priv->idx_pos=demux->filepos; // hack
299 priv->idx_pos_v=priv->idx_pos_a=priv->idx_pos;
300 // quit now, we can't even (no enough buffer memory) read this packet :(
301 return -1;
304 ret=demux_avi_read_packet(demux,ds,id,len,priv->idx_pos-1,flags);
305 } while(ret!=1);
306 return 1;
310 // return value:
311 // 0 = EOF or no stream found
312 // 1 = successfully read a packet
313 static int demux_avi_fill_buffer_ni(demuxer_t *demux, demux_stream_t *ds)
315 avi_priv_t *priv=demux->priv;
316 unsigned int id=0;
317 unsigned int len;
318 int ret=0;
321 int flags=1;
322 AVIINDEXENTRY *idx=NULL;
323 int idx_pos=0;
324 demux->filepos=stream_tell(demux->stream);
326 if(ds==demux->video) idx_pos=priv->idx_pos_v++; else
327 if(ds==demux->audio) idx_pos=priv->idx_pos_a++; else
328 idx_pos=priv->idx_pos++;
330 if(priv->idx_size>0 && idx_pos<priv->idx_size){
331 off_t pos;
332 idx=&((AVIINDEXENTRY *)priv->idx)[idx_pos];
334 if(idx->dwFlags&AVIIF_LIST){
335 if (!valid_stream_id(idx->ckid))
336 // LIST
337 continue;
338 if (!priv->warned_unaligned)
339 mp_msg(MSGT_DEMUX, MSGL_WARN, "Looks like unaligned chunk in index, broken AVI file!\n");
340 priv->warned_unaligned = 1;
342 if(ds && demux_avi_select_stream(demux,idx->ckid)!=ds){
343 mp_dbg(MSGT_DEMUX,MSGL_DBG3,"Skip chunk %.4s (0x%X) \n",(char *)&idx->ckid,(unsigned int)idx->ckid);
344 continue; // skip this chunk
347 pos = priv->idx_offset+AVI_IDX_OFFSET(idx);
348 if((pos<demux->movi_start || pos>=demux->movi_end) && (demux->movi_end>demux->movi_start)){
349 mp_msg(MSGT_DEMUX,MSGL_V,"ChunkOffset out of range! current=0x%"PRIX64" idx=0x%"PRIX64" \n",(int64_t)demux->filepos,(int64_t)pos);
350 continue;
352 stream_seek(demux->stream,pos);
354 id=stream_read_dword_le(demux->stream);
356 if(stream_eof(demux->stream)) return 0;
358 if(id!=idx->ckid){
359 mp_msg(MSGT_DEMUX,MSGL_V,"ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id,(char *)&idx->ckid);
360 if(valid_fourcc(idx->ckid))
361 id=idx->ckid; // use index if valid
362 else
363 if(!valid_fourcc(id)) continue; // drop chunk if both id and idx bad
365 len=stream_read_dword_le(demux->stream);
366 if((len!=idx->dwChunkLength)&&((len+1)!=idx->dwChunkLength)){
367 mp_msg(MSGT_DEMUX,MSGL_V,"ChunkSize mismatch! raw=%d idx=%d \n",len,idx->dwChunkLength);
368 if(len>0x200000 && idx->dwChunkLength>0x200000) continue; // both values bad :(
369 len=choose_chunk_len(idx->dwChunkLength,len);
371 if(!(idx->dwFlags&AVIIF_KEYFRAME)) flags=0;
372 } else return 0;
373 ret=demux_avi_read_packet(demux,demux_avi_select_stream(demux,id),id,len,idx_pos,flags);
374 } while(ret!=1);
375 return 1;
379 // return value:
380 // 0 = EOF or no stream found
381 // 1 = successfully read a packet
382 static int demux_avi_fill_buffer_nini(demuxer_t *demux, demux_stream_t *ds)
384 avi_priv_t *priv=demux->priv;
385 unsigned int id=0;
386 unsigned int len;
387 int ret=0;
388 off_t *fpos=NULL;
390 if(ds==demux->video) fpos=&priv->idx_pos_v; else
391 if(ds==demux->audio) fpos=&priv->idx_pos_a; else
392 return 0;
394 stream_seek(demux->stream,fpos[0]);
398 demux->filepos=stream_tell(demux->stream);
399 if(demux->filepos>=demux->movi_end && (demux->movi_end>demux->movi_start)){
400 ds->eof=1;
401 return 0;
404 id=avi_find_id(demux->stream);
405 len=stream_read_dword_le(demux->stream);
407 if(stream_eof(demux->stream)) return 0;
409 if(id==mmioFOURCC('L','I','S','T')){
410 id=stream_read_dword_le(demux->stream); // list type
411 continue;
414 if(id==mmioFOURCC('R','I','F','F')){
415 mp_msg(MSGT_DEMUX,MSGL_V,"additional RIFF header...\n");
416 id=stream_read_dword_le(demux->stream); // "AVIX"
417 continue;
420 if(ds==demux_avi_select_stream(demux,id)){
421 // read it!
422 ret=demux_avi_read_packet(demux,ds,id,len,priv->idx_pos-1,0);
423 } else {
424 // skip it!
425 int skip=(len+1)&(~1); // total bytes in this chunk
426 stream_skip(demux->stream,skip);
429 } while(ret!=1);
430 fpos[0]=stream_tell(demux->stream);
431 return 1;
434 // AVI demuxer parameters:
435 int index_mode=-1; // -1=untouched 0=don't use index 1=use (generate) index
436 char *index_file_save = NULL, *index_file_load = NULL;
437 int force_ni=0; // force non-interleaved AVI parsing
439 static demuxer_t* demux_open_avi(demuxer_t* demuxer){
440 demux_stream_t *d_audio=demuxer->audio;
441 demux_stream_t *d_video=demuxer->video;
442 sh_audio_t *sh_audio=NULL;
443 sh_video_t *sh_video=NULL;
444 avi_priv_t* priv=calloc(1, sizeof(avi_priv_t));
446 demuxer->priv=(void*)priv;
448 //---- AVI header:
449 read_avi_header(demuxer,(demuxer->stream->flags & MP_STREAM_SEEK_BW)?index_mode:-2);
450 update_audio_block_size(demuxer);
452 if(demuxer->audio->id>=0 && !demuxer->a_streams[demuxer->audio->id]){
453 mp_tmsg(MSGT_DEMUX,MSGL_WARN,"AVI: invalid audio stream ID: %d - ignoring (nosound)\n",demuxer->audio->id);
454 demuxer->audio->id=-2; // disabled
456 if(demuxer->video->id>=0 && !demuxer->v_streams[demuxer->video->id]){
457 mp_tmsg(MSGT_DEMUX,MSGL_WARN,"AVI: invalid video stream ID: %d - ignoring (using default)\n",demuxer->video->id);
458 demuxer->video->id=-1; // autodetect
461 stream_reset(demuxer->stream);
462 stream_seek(demuxer->stream,demuxer->movi_start);
463 if(priv->idx_size>1){
464 // decide index format:
465 #if 1
466 if((AVI_IDX_OFFSET(&((AVIINDEXENTRY *)priv->idx)[0])<demuxer->movi_start ||
467 AVI_IDX_OFFSET(&((AVIINDEXENTRY *)priv->idx)[1])<demuxer->movi_start )&& !priv->isodml)
468 priv->idx_offset=demuxer->movi_start-4;
469 #else
470 if(AVI_IDX_OFFSET(&((AVIINDEXENTRY *)priv->idx)[0])<demuxer->movi_start)
471 priv->idx_offset=demuxer->movi_start-4;
472 #endif
473 mp_msg(MSGT_DEMUX,MSGL_V,"AVI index offset: 0x%X (movi=0x%X idx0=0x%X idx1=0x%X)\n",
474 (int)priv->idx_offset,(int)demuxer->movi_start,
475 (int)((AVIINDEXENTRY *)priv->idx)[0].dwChunkOffset,
476 (int)((AVIINDEXENTRY *)priv->idx)[1].dwChunkOffset);
479 if(priv->idx_size>0){
480 // check that file is non-interleaved:
481 int i;
482 off_t a_pos=-1;
483 off_t v_pos=-1;
484 for(i=0;i<priv->idx_size;i++){
485 AVIINDEXENTRY* idx=&((AVIINDEXENTRY *)priv->idx)[i];
486 demux_stream_t* ds=demux_avi_select_stream(demuxer,idx->ckid);
487 off_t pos = priv->idx_offset + AVI_IDX_OFFSET(idx);
488 if(a_pos==-1 && ds==demuxer->audio){
489 a_pos=pos;
490 if(v_pos!=-1) break;
492 if(v_pos==-1 && ds==demuxer->video){
493 v_pos=pos;
494 if(a_pos!=-1) break;
497 if(v_pos==-1){
498 mp_msg(MSGT_DEMUX, MSGL_ERR, "AVI_NI: %s",
499 mp_gtext("No video stream found.\n"));
500 return NULL;
502 if(a_pos==-1){
503 d_audio->sh=sh_audio=NULL;
504 } else {
505 if(force_ni || abs(a_pos-v_pos)>0x100000){ // distance > 1MB
506 mp_tmsg(MSGT_DEMUX,MSGL_INFO,"%s NON-INTERLEAVED AVI file format.\n",force_ni?"Forced":"Detected");
507 demuxer->type=DEMUXER_TYPE_AVI_NI; // HACK!!!!
508 demuxer->desc=&demuxer_desc_avi_ni; // HACK!!!!
509 pts_from_bps=1; // force BPS sync!
512 } else {
513 // no index
514 if(force_ni){
515 mp_tmsg(MSGT_DEMUX,MSGL_INFO,"Using NON-INTERLEAVED broken AVI file format.\n");
516 demuxer->type=DEMUXER_TYPE_AVI_NINI; // HACK!!!!
517 demuxer->desc=&demuxer_desc_avi_nini; // HACK!!!!
518 priv->idx_pos_a=
519 priv->idx_pos_v=demuxer->movi_start;
520 pts_from_bps=1; // force BPS sync!
522 demuxer->seekable=0;
524 if(!ds_fill_buffer(d_video)){
525 mp_msg(MSGT_DEMUX, MSGL_ERR, "AVI: %s",
526 mp_gtext("Missing video stream!? Contact the author, "
527 "it may be a bug :(\n"));
528 return NULL;
530 sh_video=d_video->sh;sh_video->ds=d_video;
531 if(d_audio->id!=-2){
532 mp_msg(MSGT_DEMUX,MSGL_V,"AVI: Searching for audio stream (id:%d)\n",d_audio->id);
533 if(!priv->audio_streams || !ds_fill_buffer(d_audio)){
534 mp_msg(MSGT_DEMUX, MSGL_INFO, "AVI: %s",
535 mp_gtext("No audio stream found -> no sound.\n"));
536 d_audio->sh=sh_audio=NULL;
537 } else {
538 sh_audio=d_audio->sh;sh_audio->ds=d_audio;
542 // calculating audio/video bitrate:
543 if(priv->idx_size>0){
544 // we have index, let's count 'em!
545 AVIINDEXENTRY *idx = priv->idx;
546 int64_t vsize=0;
547 int64_t asize=0;
548 size_t vsamples=0;
549 size_t asamples=0;
550 int i;
551 for(i=0;i<priv->idx_size;i++){
552 int id=avi_stream_id(idx[i].ckid);
553 unsigned len=idx[i].dwChunkLength;
554 if(sh_video->ds->id == id) {
555 vsize+=len;
556 ++vsamples;
558 else if(sh_audio && sh_audio->ds->id == id) {
559 asize+=len;
560 asamples+=(len+priv->audio_block_size-1)/priv->audio_block_size;
563 mp_msg(MSGT_DEMUX, MSGL_V,
564 "AVI video size=%"PRId64" (%zu) audio size=%"PRId64" (%zu)\n",
565 vsize, vsamples, asize, asamples);
566 priv->numberofframes=vsamples;
567 sh_video->i_bps=((float)vsize/(float)vsamples)*(float)sh_video->video.dwRate/(float)sh_video->video.dwScale;
568 if(sh_audio) sh_audio->i_bps=((float)asize/(float)asamples)*(float)sh_audio->audio.dwRate/(float)sh_audio->audio.dwScale;
569 } else {
570 // guessing, results may be inaccurate:
571 int64_t vsize;
572 int64_t asize=0;
574 if((priv->numberofframes=sh_video->video.dwLength)<=1)
575 // bad video header, try to get number of frames from audio
576 if(sh_audio && sh_audio->wf->nAvgBytesPerSec) priv->numberofframes=sh_video->fps*sh_audio->audio.dwLength/sh_audio->audio.dwRate*sh_audio->audio.dwScale;
577 if(priv->numberofframes<=1){
578 mp_tmsg(MSGT_SEEK,MSGL_WARN,"Could not determine number of frames (for absolute seek).\n");
579 priv->numberofframes=0;
582 if(sh_audio){
583 if(sh_audio->wf->nAvgBytesPerSec && sh_audio->audio.dwSampleSize!=1){
584 asize=(float)sh_audio->wf->nAvgBytesPerSec*sh_audio->audio.dwLength*sh_audio->audio.dwScale/sh_audio->audio.dwRate;
585 } else {
586 asize=sh_audio->audio.dwLength;
587 sh_audio->i_bps=(float)asize/(sh_video->frametime*priv->numberofframes);
590 vsize=demuxer->movi_end-demuxer->movi_start-asize-8*priv->numberofframes;
591 mp_msg(MSGT_DEMUX,MSGL_V,"AVI video size=%"PRId64" (%u) audio size=%"PRId64"\n",vsize,priv->numberofframes,asize);
592 sh_video->i_bps=(float)vsize/(sh_video->frametime*priv->numberofframes);
595 return demuxer;
600 static void demux_seek_avi(demuxer_t *demuxer, float rel_seek_secs,
601 float audio_delay, int flags)
603 avi_priv_t *priv=demuxer->priv;
604 demux_stream_t *d_audio=demuxer->audio;
605 demux_stream_t *d_video=demuxer->video;
606 sh_audio_t *sh_audio=d_audio->sh;
607 sh_video_t *sh_video=d_video->sh;
608 float skip_audio_secs=0;
610 //FIXME: OFF_T - Didn't check AVI case yet (avi files can't be >2G anyway?)
611 //================= seek in AVI ==========================
612 int rel_seek_frames=rel_seek_secs*sh_video->fps;
613 int video_chunk_pos=d_video->pos;
614 int i;
616 if(flags&SEEK_ABSOLUTE){
617 // seek absolute
618 video_chunk_pos=0;
621 if(flags&SEEK_FACTOR){
622 rel_seek_frames=rel_seek_secs*priv->numberofframes;
625 priv->skip_video_frames=0;
626 priv->avi_audio_pts=0;
628 // ------------ STEP 1: find nearest video keyframe chunk ------------
629 // find nearest video keyframe chunk pos:
630 if(rel_seek_frames>0){
631 // seek forward
632 while(video_chunk_pos<priv->idx_size-1){
633 int id=((AVIINDEXENTRY *)priv->idx)[video_chunk_pos].ckid;
634 if(avi_stream_id(id)==d_video->id){ // video frame
635 if((--rel_seek_frames)<0 && ((AVIINDEXENTRY *)priv->idx)[video_chunk_pos].dwFlags&AVIIF_KEYFRAME) break;
637 ++video_chunk_pos;
639 } else {
640 // seek backward
641 while(video_chunk_pos>0){
642 int id=((AVIINDEXENTRY *)priv->idx)[video_chunk_pos].ckid;
643 if(avi_stream_id(id)==d_video->id){ // video frame
644 if((++rel_seek_frames)>0 && ((AVIINDEXENTRY *)priv->idx)[video_chunk_pos].dwFlags&AVIIF_KEYFRAME) break;
646 --video_chunk_pos;
649 priv->idx_pos_a=priv->idx_pos_v=priv->idx_pos=video_chunk_pos;
651 // re-calc video pts:
652 d_video->pack_no=0;
653 for(i=0;i<video_chunk_pos;i++){
654 int id=((AVIINDEXENTRY *)priv->idx)[i].ckid;
655 if(avi_stream_id(id)==d_video->id) ++d_video->pack_no;
657 priv->video_pack_no=
658 sh_video->num_frames=sh_video->num_frames_decoded=d_video->pack_no;
659 priv->avi_video_pts=d_video->pack_no*(float)sh_video->video.dwScale/(float)sh_video->video.dwRate;
660 d_video->pos=video_chunk_pos;
662 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);
664 // ------------ STEP 2: seek audio, find the right chunk & pos ------------
666 d_audio->pack_no=0;
667 priv->audio_block_no=0;
668 d_audio->dpos=0;
670 if(sh_audio){
671 int i;
672 int len=0;
673 int skip_audio_bytes=0;
674 int curr_audio_pos=-1;
675 int audio_chunk_pos=-1;
676 int chunk_max=(demuxer->type==DEMUXER_TYPE_AVI)?video_chunk_pos:priv->idx_size;
678 if(sh_audio->audio.dwSampleSize){
679 // constant rate audio stream
680 /* immediate seeking to audio position, including when streams are delayed */
681 curr_audio_pos=(priv->avi_video_pts + audio_delay)*(float)sh_audio->audio.dwRate/(float)sh_audio->audio.dwScale;
682 curr_audio_pos*=sh_audio->audio.dwSampleSize;
684 // find audio chunk pos:
685 for(i=0;i<chunk_max;i++){
686 int id=((AVIINDEXENTRY *)priv->idx)[i].ckid;
687 if(avi_stream_id(id)==d_audio->id){
688 len=((AVIINDEXENTRY *)priv->idx)[i].dwChunkLength;
689 if(d_audio->dpos<=curr_audio_pos && curr_audio_pos<(d_audio->dpos+len)){
690 break;
692 ++d_audio->pack_no;
693 priv->audio_block_no+=
694 (len+priv->audio_block_size-1)/priv->audio_block_size;
695 d_audio->dpos+=len;
698 audio_chunk_pos=i;
699 skip_audio_bytes=curr_audio_pos-d_audio->dpos;
701 mp_msg(MSGT_SEEK,MSGL_V,"SEEK: i=%d (max:%d) dpos=%d (wanted:%d) \n",
702 i,chunk_max,(int)d_audio->dpos,curr_audio_pos);
704 } else {
705 // VBR audio
706 /* immediate seeking to audio position, including when streams are delayed */
707 int chunks=(priv->avi_video_pts + audio_delay)*(float)sh_audio->audio.dwRate/(float)sh_audio->audio.dwScale;
708 audio_chunk_pos=0;
710 // find audio chunk pos:
711 for(i=0;i<priv->idx_size && chunks>0;i++){
712 int id=((AVIINDEXENTRY *)priv->idx)[i].ckid;
713 if(avi_stream_id(id)==d_audio->id){
714 len=((AVIINDEXENTRY *)priv->idx)[i].dwChunkLength;
715 if(i>chunk_max){
716 skip_audio_bytes+=len;
717 } else {
718 ++d_audio->pack_no;
719 priv->audio_block_no+=
720 (len+priv->audio_block_size-1)/priv->audio_block_size;
721 d_audio->dpos+=len;
722 audio_chunk_pos=i;
724 chunks-=(len+priv->audio_block_size-1)/priv->audio_block_size;
729 // Now we have:
730 // audio_chunk_pos = chunk no in index table (it's <=chunk_max)
731 // skip_audio_bytes = bytes to be skipped after chunk seek
732 // d-audio->pack_no = chunk_no in stream at audio_chunk_pos
733 // d_audio->dpos = bytepos in stream at audio_chunk_pos
734 // let's seek!
736 // update stream position:
737 d_audio->pos=audio_chunk_pos;
739 if(demuxer->type==DEMUXER_TYPE_AVI){
740 // interleaved stream:
741 if(audio_chunk_pos<video_chunk_pos){
742 // calc priv->skip_video_frames & adjust video pts counter:
743 for(i=audio_chunk_pos;i<video_chunk_pos;i++){
744 int id=((AVIINDEXENTRY *)priv->idx)[i].ckid;
745 if(avi_stream_id(id)==d_video->id) ++priv->skip_video_frames;
747 // requires for correct audio pts calculation (demuxer):
748 priv->avi_video_pts-=priv->skip_video_frames*(float)sh_video->video.dwScale/(float)sh_video->video.dwRate;
749 priv->avi_audio_pts=priv->avi_video_pts;
750 // set index position:
751 priv->idx_pos_a=priv->idx_pos_v=priv->idx_pos=audio_chunk_pos;
753 } else {
754 // non-interleaved stream:
755 priv->idx_pos_a=audio_chunk_pos;
756 priv->idx_pos_v=video_chunk_pos;
757 priv->idx_pos=(audio_chunk_pos<video_chunk_pos)?audio_chunk_pos:video_chunk_pos;
760 mp_msg(MSGT_SEEK,MSGL_V,"SEEK: idx=%d (a:%d v:%d) v.skip=%d a.skip=%d/%4.3f \n",
761 (int)priv->idx_pos,audio_chunk_pos,video_chunk_pos,
762 (int)priv->skip_video_frames,skip_audio_bytes,skip_audio_secs);
764 if(skip_audio_bytes){
765 demux_read_data(d_audio,NULL,skip_audio_bytes);
769 d_video->pts=priv->avi_video_pts; // OSD
774 static void demux_close_avi(demuxer_t *demuxer)
776 avi_priv_t* priv=demuxer->priv;
778 if(!priv)
779 return;
781 if(priv->idx_size > 0)
782 free(priv->idx);
783 free(priv);
787 static int demux_avi_control(demuxer_t *demuxer,int cmd, void *arg){
788 avi_priv_t *priv=demuxer->priv;
789 demux_stream_t *d_video=demuxer->video;
790 sh_video_t *sh_video=d_video->sh;
792 switch(cmd) {
793 case DEMUXER_CTRL_GET_TIME_LENGTH:
794 if (!priv->numberofframes || !sh_video) return DEMUXER_CTRL_DONTKNOW;
795 *((double *)arg)=(double)priv->numberofframes/sh_video->fps;
796 if (sh_video->video.dwLength<=1) return DEMUXER_CTRL_GUESS;
797 return DEMUXER_CTRL_OK;
799 case DEMUXER_CTRL_GET_PERCENT_POS:
800 if (!priv->numberofframes || !sh_video) {
801 return DEMUXER_CTRL_DONTKNOW;
803 *((int *)arg)=(int)(priv->video_pack_no*100/priv->numberofframes);
804 if (sh_video->video.dwLength<=1) return DEMUXER_CTRL_GUESS;
805 return DEMUXER_CTRL_OK;
807 case DEMUXER_CTRL_SWITCH_AUDIO:
808 case DEMUXER_CTRL_SWITCH_VIDEO: {
809 int audio = (cmd == DEMUXER_CTRL_SWITCH_AUDIO);
810 demux_stream_t *ds = audio ? demuxer->audio : demuxer->video;
811 void **streams = audio ? demuxer->a_streams : demuxer->v_streams;
812 int maxid = FFMIN(100, audio ? MAX_A_STREAMS : MAX_V_STREAMS);
813 int chunkid;
814 if (ds->id < -1)
815 ds->id = -1;
817 if (*(int *)arg >= 0)
818 ds->id = *(int *)arg;
819 else {
820 int i;
821 for (i = 0; i < maxid; i++) {
822 if (++ds->id >= maxid) ds->id = 0;
823 if (streams[ds->id]) break;
827 chunkid = (ds->id / 10 + '0') | (ds->id % 10 + '0') << 8;
828 ds->sh = NULL;
829 if (!streams[ds->id]) // stream not available
830 ds->id = -1;
831 else
832 demux_avi_select_stream(demuxer, chunkid);
833 *(int *)arg = ds->id;
834 return DEMUXER_CTRL_OK;
837 default:
838 return DEMUXER_CTRL_NOTIMPL;
843 static int avi_check_file(demuxer_t *demuxer)
845 int id=stream_read_dword_le(demuxer->stream); // "RIFF"
847 if((id==mmioFOURCC('R','I','F','F')) || (id==mmioFOURCC('O','N','2',' '))) {
848 stream_read_dword_le(demuxer->stream); //filesize
849 id=stream_read_dword_le(demuxer->stream); // "AVI "
850 if(id==formtypeAVI)
851 return DEMUXER_TYPE_AVI;
852 // "Samsung Digimax i6 PMP" crap according to bug 742
853 if(id==mmioFOURCC('A','V','I',0x19))
854 return DEMUXER_TYPE_AVI;
855 if(id==mmioFOURCC('O','N','2','f')){
856 mp_tmsg(MSGT_DEMUXER,MSGL_INFO,"ON2 AVI format");
857 return DEMUXER_TYPE_AVI;
861 return 0;
865 static demuxer_t* demux_open_hack_avi(demuxer_t *demuxer)
867 struct MPOpts *opts = demuxer->opts;
868 sh_audio_t* sh_a;
870 demuxer = demux_open_avi(demuxer);
871 if(!demuxer) return NULL; // failed to open
872 sh_a = demuxer->audio->sh;
873 if(demuxer->audio->id != -2 && sh_a) {
874 #ifdef CONFIG_OGGVORBIS
875 // support for Ogg-in-AVI:
876 if(sh_a->format == 0xFFFE)
877 demuxer = init_avi_with_ogg(demuxer);
878 else if(sh_a->format == 0x674F) {
879 stream_t* s;
880 demuxer_t *od;
881 s = new_ds_stream(demuxer->audio);
882 od = new_demuxer(opts, s,DEMUXER_TYPE_OGG,-1,-2,-2,NULL);
883 if(!demux_ogg_open(od)) {
884 mp_tmsg( MSGT_DEMUXER,MSGL_ERR,"Unable to open the Ogg demuxer.\n");
885 free_stream(s);
886 demuxer->audio->id = -2;
887 } else
888 demuxer = new_demuxers_demuxer(demuxer,od,demuxer);
890 #endif
893 return demuxer;
897 const demuxer_desc_t demuxer_desc_avi = {
898 "AVI demuxer",
899 "avi",
900 "AVI",
901 "Arpi?",
902 "AVI files, including non interleaved files",
903 DEMUXER_TYPE_AVI,
904 1, // safe autodetect
905 avi_check_file,
906 demux_avi_fill_buffer,
907 demux_open_hack_avi,
908 demux_close_avi,
909 demux_seek_avi,
910 demux_avi_control
913 const demuxer_desc_t demuxer_desc_avi_ni = {
914 "AVI demuxer, non-interleaved",
915 "avini",
916 "AVI",
917 "Arpi?",
918 "AVI files, including non interleaved files",
919 DEMUXER_TYPE_AVI,
920 1, // safe autodetect
921 avi_check_file,
922 demux_avi_fill_buffer_ni,
923 demux_open_hack_avi,
924 demux_close_avi,
925 demux_seek_avi,
926 demux_avi_control
929 const demuxer_desc_t demuxer_desc_avi_nini = {
930 "AVI demuxer, non-interleaved and no index",
931 "avinini",
932 "AVI",
933 "Arpi?",
934 "AVI files, including non interleaved files",
935 DEMUXER_TYPE_AVI,
936 1, // safe autodetect
937 avi_check_file,
938 demux_avi_fill_buffer_nini,
939 demux_open_hack_avi,
940 demux_close_avi,
941 demux_seek_avi,
942 demux_avi_control