Makefile: Merge dhahelperwinclean and dhahelperclean targets
[mplayer.git] / libmpdemux / demux_avi.c
blobfea37e50629ea8d5cf42e78a38bf06849e02f3cb
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 // Select ds from ID
42 static demux_stream_t *demux_avi_select_stream(demuxer_t *demux,
43 unsigned int id)
45 int stream_id=avi_stream_id(id);
48 if(demux->video->id==-1)
49 if(demux->v_streams[stream_id])
50 demux->video->id=stream_id;
52 if(demux->audio->id==-1)
53 if(demux->a_streams[stream_id])
54 demux->audio->id=stream_id;
56 if(stream_id==demux->audio->id){
57 if(!demux->audio->sh){
58 sh_audio_t* sh;
59 avi_priv_t *priv=demux->priv;
60 sh=demux->audio->sh=demux->a_streams[stream_id];
61 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected AVI audio ID = %d\n",demux->audio->id);
62 if(sh->wf){
63 priv->audio_block_size=sh->wf->nBlockAlign;
64 if(!priv->audio_block_size){
65 // for PCM audio we can calculate the blocksize:
66 if(sh->format==1)
67 priv->audio_block_size=sh->wf->nChannels*(sh->wf->wBitsPerSample/8);
68 else
69 priv->audio_block_size=1; // hope the best...
70 } else {
71 // workaround old mencoder's bug:
72 if(sh->audio.dwSampleSize==1 && sh->audio.dwScale==1 &&
73 (sh->wf->nBlockAlign==1152 || sh->wf->nBlockAlign==576)){
74 mp_tmsg(MSGT_DEMUX,MSGL_WARN,"AVI: Working around CBR-MP3 nBlockAlign header bug!\n");
75 priv->audio_block_size=1;
78 } else {
79 priv->audio_block_size=sh->audio.dwSampleSize;
82 return demux->audio;
84 if(stream_id==demux->video->id){
85 if(!demux->video->sh){
86 demux->video->sh=demux->v_streams[stream_id];
87 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected AVI video ID = %d\n",demux->video->id);
89 return demux->video;
91 if(id!=mmioFOURCC('J','U','N','K')){
92 // unknown
93 mp_msg(MSGT_DEMUX,MSGL_DBG2,"Unknown chunk: %.4s (%X)\n",(char *) &id,id);
94 //abort();
96 return NULL;
99 static int valid_fourcc(unsigned int id){
100 static const char valid[] = "0123456789abcdefghijklmnopqrstuvwxyz"
101 "ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
102 unsigned char* fcc=(unsigned char*)(&id);
103 return strchr(valid, fcc[0]) && strchr(valid, fcc[1]) &&
104 strchr(valid, fcc[2]) && strchr(valid, fcc[3]);
107 static int valid_stream_id(unsigned int id) {
108 unsigned char* fcc=(unsigned char*)(&id);
109 return fcc[0] >= '0' && fcc[0] <= '9' && fcc[1] >= '0' && fcc[1] <= '9' &&
110 ((fcc[2] == 'w' && fcc[3] == 'b') || (fcc[2] == 'd' && fcc[3] == 'c'));
113 static int choose_chunk_len(unsigned int len1,unsigned int len2){
114 // len1 has a bit more priority than len2. len1!=len2
115 // Note: this is a first-idea-logic, may be wrong. comments welcomed.
117 // prefer small frames rather than 0
118 if(!len1) return (len2>0x80000) ? len1 : len2;
119 if(!len2) return (len1>0x100000) ? len2 : len1;
121 // choose the smaller value:
122 return (len1<len2)? len1 : len2;
125 static int demux_avi_read_packet(demuxer_t *demux,demux_stream_t *ds,unsigned int id,unsigned int len,int idxpos,int flags){
126 avi_priv_t *priv=demux->priv;
127 int skip;
128 float pts=0;
130 mp_dbg(MSGT_DEMUX,MSGL_DBG3,"demux_avi.read_packet: %X\n",id);
132 if(ds==demux->audio){
133 if(priv->pts_corrected==0){
134 if(priv->pts_has_video){
135 // we have video pts now
136 float delay=0;
137 if(((sh_audio_t*)(ds->sh))->wf->nAvgBytesPerSec)
138 delay=(float)priv->pts_corr_bytes/((sh_audio_t*)(ds->sh))->wf->nAvgBytesPerSec;
139 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);
140 //priv->pts_correction=-priv->avi_audio_pts+delay;
141 priv->pts_correction=delay-priv->avi_audio_pts;
142 priv->avi_audio_pts+=priv->pts_correction;
143 priv->pts_corrected=1;
144 } else
145 priv->pts_corr_bytes+=len;
147 if(pts_from_bps){
148 pts = priv->audio_block_no *
149 (float)((sh_audio_t*)demux->audio->sh)->audio.dwScale /
150 (float)((sh_audio_t*)demux->audio->sh)->audio.dwRate;
151 } else
152 pts=priv->avi_audio_pts; //+priv->pts_correction;
153 priv->avi_audio_pts=0;
154 // update blockcount:
155 priv->audio_block_no+=priv->audio_block_size ?
156 ((len+priv->audio_block_size-1)/priv->audio_block_size) : 1;
157 } else
158 if(ds==demux->video){
159 // video
160 if(priv->skip_video_frames>0){
161 // drop frame (seeking)
162 --priv->skip_video_frames;
163 ds=NULL;
166 pts = priv->avi_video_pts = priv->video_pack_no *
167 (float)((sh_video_t*)demux->video->sh)->video.dwScale /
168 (float)((sh_video_t*)demux->video->sh)->video.dwRate;
170 priv->avi_audio_pts=priv->avi_video_pts+priv->pts_correction;
171 priv->pts_has_video=1;
173 if(ds) ++priv->video_pack_no;
177 skip=(len+1)&(~1); // total bytes in this chunk
179 if(ds){
180 mp_dbg(MSGT_DEMUX,MSGL_DBG2,"DEMUX_AVI: Read %d data bytes from packet %04X\n",len,id);
181 ds_read_packet(ds,demux->stream,len,pts,idxpos,flags);
182 skip-=len;
184 skip = FFMAX(skip, 0);
185 if (avi_stream_id(id) > 99 && id != mmioFOURCC('J','U','N','K'))
186 skip = FFMIN(skip, 65536);
187 if(skip){
188 mp_dbg(MSGT_DEMUX,MSGL_DBG2,"DEMUX_AVI: Skipping %d bytes from packet %04X\n",skip,id);
189 stream_skip(demux->stream,skip);
191 return ds?1:0;
194 static uint32_t avi_find_id(stream_t *stream) {
195 uint32_t id = stream_read_dword_le(stream);
196 if (!id) {
197 mp_msg(MSGT_DEMUX, MSGL_WARN, "Incomplete stream? Trying resync.\n");
198 do {
199 id = stream_read_dword_le(stream);
200 if (stream_eof(stream)) return 0;
201 } while (avi_stream_id(id) > 99);
203 return id;
206 // return value:
207 // 0 = EOF or no stream found
208 // 1 = successfully read a packet
209 static int demux_avi_fill_buffer(demuxer_t *demux, demux_stream_t *dsds){
210 avi_priv_t *priv=demux->priv;
211 unsigned int id=0;
212 unsigned int len;
213 int ret=0;
214 demux_stream_t *ds;
217 int flags=1;
218 AVIINDEXENTRY *idx=NULL;
219 if(priv->idx_size>0 && priv->idx_pos<priv->idx_size){
220 off_t pos;
222 idx=&((AVIINDEXENTRY *)priv->idx)[priv->idx_pos++];
224 if(idx->dwFlags&AVIIF_LIST){
225 if (!valid_stream_id(idx->ckid))
226 // LIST
227 continue;
228 if (!priv->warned_unaligned)
229 mp_msg(MSGT_DEMUX, MSGL_WARN, "Looks like unaligned chunk in index, broken AVI file!\n");
230 priv->warned_unaligned = 1;
232 if(!demux_avi_select_stream(demux,idx->ckid)){
233 mp_dbg(MSGT_DEMUX,MSGL_DBG3,"Skip chunk %.4s (0x%X) \n",(char *)&idx->ckid,(unsigned int)idx->ckid);
234 continue; // skip this chunk
237 pos = (off_t)priv->idx_offset+AVI_IDX_OFFSET(idx);
238 if((pos<demux->movi_start || pos>=demux->movi_end) && (demux->movi_end>demux->movi_start) && (demux->stream->flags & MP_STREAM_SEEK)){
239 mp_msg(MSGT_DEMUX,MSGL_V,"ChunkOffset out of range! idx=0x%"PRIX64" \n",(int64_t)pos);
240 continue;
242 stream_seek(demux->stream,pos);
243 demux->filepos=stream_tell(demux->stream);
244 id=stream_read_dword_le(demux->stream);
245 if(stream_eof(demux->stream)) return 0; // EOF!
247 if(id!=idx->ckid){
248 mp_msg(MSGT_DEMUX,MSGL_V,"ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id,(char *)&idx->ckid);
249 if(valid_fourcc(idx->ckid))
250 id=idx->ckid; // use index if valid
251 else
252 if(!valid_fourcc(id)) continue; // drop chunk if both id and idx bad
254 len=stream_read_dword_le(demux->stream);
255 if((len!=idx->dwChunkLength)&&((len+1)!=idx->dwChunkLength)){
256 mp_msg(MSGT_DEMUX,MSGL_V,"ChunkSize mismatch! raw=%d idx=%d \n",len,idx->dwChunkLength);
257 if(len>0x200000 && idx->dwChunkLength>0x200000) continue; // both values bad :(
258 len=choose_chunk_len(idx->dwChunkLength,len);
260 if(!(idx->dwFlags&AVIIF_KEYFRAME)) flags=0;
261 } else {
262 demux->filepos=stream_tell(demux->stream);
263 if(demux->filepos>=demux->movi_end && demux->movi_end>demux->movi_start && (demux->stream->flags & MP_STREAM_SEEK)){
264 demux->stream->eof=1;
265 return 0;
267 id=avi_find_id(demux->stream);
268 len=stream_read_dword_le(demux->stream);
269 if(stream_eof(demux->stream)) return 0; // EOF!
271 if(id==mmioFOURCC('L','I','S','T') || id==mmioFOURCC('R', 'I', 'F', 'F')){
272 id=stream_read_dword_le(demux->stream); // list or RIFF type
273 continue;
277 ds=demux_avi_select_stream(demux,id);
278 if(ds)
279 if(ds->packs+1>=MAX_PACKS || ds->bytes+len>=MAX_PACK_BYTES){
280 // this packet will cause a buffer overflow, switch to -ni mode!!!
281 mp_tmsg(MSGT_DEMUX,MSGL_WARN,"\nBadly interleaved AVI file detected - switching to -ni mode...\n");
282 if(priv->idx_size>0){
283 // has index
284 demux->type=DEMUXER_TYPE_AVI_NI;
285 demux->desc=&demuxer_desc_avi_ni;
286 --priv->idx_pos; // hack
287 } else {
288 // no index
289 demux->type=DEMUXER_TYPE_AVI_NINI;
290 demux->desc=&demuxer_desc_avi_nini;
291 priv->idx_pos=demux->filepos; // hack
293 priv->idx_pos_v=priv->idx_pos_a=priv->idx_pos;
294 // quit now, we can't even (no enough buffer memory) read this packet :(
295 return -1;
298 ret=demux_avi_read_packet(demux,ds,id,len,priv->idx_pos-1,flags);
299 } while(ret!=1);
300 return 1;
304 // return value:
305 // 0 = EOF or no stream found
306 // 1 = successfully read a packet
307 static int demux_avi_fill_buffer_ni(demuxer_t *demux, demux_stream_t *ds)
309 avi_priv_t *priv=demux->priv;
310 unsigned int id=0;
311 unsigned int len;
312 int ret=0;
315 int flags=1;
316 AVIINDEXENTRY *idx=NULL;
317 int idx_pos=0;
318 demux->filepos=stream_tell(demux->stream);
320 if(ds==demux->video) idx_pos=priv->idx_pos_v++; else
321 if(ds==demux->audio) idx_pos=priv->idx_pos_a++; else
322 idx_pos=priv->idx_pos++;
324 if(priv->idx_size>0 && idx_pos<priv->idx_size){
325 off_t pos;
326 idx=&((AVIINDEXENTRY *)priv->idx)[idx_pos];
328 if(idx->dwFlags&AVIIF_LIST){
329 if (!valid_stream_id(idx->ckid))
330 // LIST
331 continue;
332 if (!priv->warned_unaligned)
333 mp_msg(MSGT_DEMUX, MSGL_WARN, "Looks like unaligned chunk in index, broken AVI file!\n");
334 priv->warned_unaligned = 1;
336 if(ds && demux_avi_select_stream(demux,idx->ckid)!=ds){
337 mp_dbg(MSGT_DEMUX,MSGL_DBG3,"Skip chunk %.4s (0x%X) \n",(char *)&idx->ckid,(unsigned int)idx->ckid);
338 continue; // skip this chunk
341 pos = priv->idx_offset+AVI_IDX_OFFSET(idx);
342 if((pos<demux->movi_start || pos>=demux->movi_end) && (demux->movi_end>demux->movi_start)){
343 mp_msg(MSGT_DEMUX,MSGL_V,"ChunkOffset out of range! current=0x%"PRIX64" idx=0x%"PRIX64" \n",(int64_t)demux->filepos,(int64_t)pos);
344 continue;
346 stream_seek(demux->stream,pos);
348 id=stream_read_dword_le(demux->stream);
350 if(stream_eof(demux->stream)) return 0;
352 if(id!=idx->ckid){
353 mp_msg(MSGT_DEMUX,MSGL_V,"ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id,(char *)&idx->ckid);
354 if(valid_fourcc(idx->ckid))
355 id=idx->ckid; // use index if valid
356 else
357 if(!valid_fourcc(id)) continue; // drop chunk if both id and idx bad
359 len=stream_read_dword_le(demux->stream);
360 if((len!=idx->dwChunkLength)&&((len+1)!=idx->dwChunkLength)){
361 mp_msg(MSGT_DEMUX,MSGL_V,"ChunkSize mismatch! raw=%d idx=%d \n",len,idx->dwChunkLength);
362 if(len>0x200000 && idx->dwChunkLength>0x200000) continue; // both values bad :(
363 len=choose_chunk_len(idx->dwChunkLength,len);
365 if(!(idx->dwFlags&AVIIF_KEYFRAME)) flags=0;
366 } else return 0;
367 ret=demux_avi_read_packet(demux,demux_avi_select_stream(demux,id),id,len,idx_pos,flags);
368 } while(ret!=1);
369 return 1;
373 // return value:
374 // 0 = EOF or no stream found
375 // 1 = successfully read a packet
376 static int demux_avi_fill_buffer_nini(demuxer_t *demux, demux_stream_t *ds)
378 avi_priv_t *priv=demux->priv;
379 unsigned int id=0;
380 unsigned int len;
381 int ret=0;
382 off_t *fpos=NULL;
384 if(ds==demux->video) fpos=&priv->idx_pos_v; else
385 if(ds==demux->audio) fpos=&priv->idx_pos_a; else
386 return 0;
388 stream_seek(demux->stream,fpos[0]);
392 demux->filepos=stream_tell(demux->stream);
393 if(demux->filepos>=demux->movi_end && (demux->movi_end>demux->movi_start)){
394 ds->eof=1;
395 return 0;
398 id=avi_find_id(demux->stream);
399 len=stream_read_dword_le(demux->stream);
401 if(stream_eof(demux->stream)) return 0;
403 if(id==mmioFOURCC('L','I','S','T')){
404 id=stream_read_dword_le(demux->stream); // list type
405 continue;
408 if(id==mmioFOURCC('R','I','F','F')){
409 mp_msg(MSGT_DEMUX,MSGL_V,"additional RIFF header...\n");
410 id=stream_read_dword_le(demux->stream); // "AVIX"
411 continue;
414 if(ds==demux_avi_select_stream(demux,id)){
415 // read it!
416 ret=demux_avi_read_packet(demux,ds,id,len,priv->idx_pos-1,0);
417 } else {
418 // skip it!
419 int skip=(len+1)&(~1); // total bytes in this chunk
420 stream_skip(demux->stream,skip);
423 } while(ret!=1);
424 fpos[0]=stream_tell(demux->stream);
425 return 1;
428 // AVI demuxer parameters:
429 int index_mode=-1; // -1=untouched 0=don't use index 1=use (generate) index
430 char *index_file_save = NULL, *index_file_load = NULL;
431 int force_ni=0; // force non-interleaved AVI parsing
433 static demuxer_t* demux_open_avi(demuxer_t* demuxer){
434 demux_stream_t *d_audio=demuxer->audio;
435 demux_stream_t *d_video=demuxer->video;
436 sh_audio_t *sh_audio=NULL;
437 sh_video_t *sh_video=NULL;
438 avi_priv_t* priv=calloc(1, sizeof(avi_priv_t));
440 demuxer->priv=(void*)priv;
442 //---- AVI header:
443 read_avi_header(demuxer,(demuxer->stream->flags & MP_STREAM_SEEK_BW)?index_mode:-2);
445 if(demuxer->audio->id>=0 && !demuxer->a_streams[demuxer->audio->id]){
446 mp_tmsg(MSGT_DEMUX,MSGL_WARN,"AVI: invalid audio stream ID: %d - ignoring (nosound)\n",demuxer->audio->id);
447 demuxer->audio->id=-2; // disabled
449 if(demuxer->video->id>=0 && !demuxer->v_streams[demuxer->video->id]){
450 mp_tmsg(MSGT_DEMUX,MSGL_WARN,"AVI: invalid video stream ID: %d - ignoring (using default)\n",demuxer->video->id);
451 demuxer->video->id=-1; // autodetect
454 stream_reset(demuxer->stream);
455 stream_seek(demuxer->stream,demuxer->movi_start);
456 if(priv->idx_size>1){
457 // decide index format:
458 #if 1
459 if((AVI_IDX_OFFSET(&((AVIINDEXENTRY *)priv->idx)[0])<demuxer->movi_start ||
460 AVI_IDX_OFFSET(&((AVIINDEXENTRY *)priv->idx)[1])<demuxer->movi_start )&& !priv->isodml)
461 priv->idx_offset=demuxer->movi_start-4;
462 #else
463 if(AVI_IDX_OFFSET(&((AVIINDEXENTRY *)priv->idx)[0])<demuxer->movi_start)
464 priv->idx_offset=demuxer->movi_start-4;
465 #endif
466 mp_msg(MSGT_DEMUX,MSGL_V,"AVI index offset: 0x%X (movi=0x%X idx0=0x%X idx1=0x%X)\n",
467 (int)priv->idx_offset,(int)demuxer->movi_start,
468 (int)((AVIINDEXENTRY *)priv->idx)[0].dwChunkOffset,
469 (int)((AVIINDEXENTRY *)priv->idx)[1].dwChunkOffset);
472 if(priv->idx_size>0){
473 // check that file is non-interleaved:
474 int i;
475 off_t a_pos=-1;
476 off_t v_pos=-1;
477 for(i=0;i<priv->idx_size;i++){
478 AVIINDEXENTRY* idx=&((AVIINDEXENTRY *)priv->idx)[i];
479 demux_stream_t* ds=demux_avi_select_stream(demuxer,idx->ckid);
480 off_t pos = priv->idx_offset + AVI_IDX_OFFSET(idx);
481 if(a_pos==-1 && ds==demuxer->audio){
482 a_pos=pos;
483 if(v_pos!=-1) break;
485 if(v_pos==-1 && ds==demuxer->video){
486 v_pos=pos;
487 if(a_pos!=-1) break;
490 if(v_pos==-1){
491 mp_msg(MSGT_DEMUX, MSGL_ERR, "AVI_NI: %s",
492 mp_gtext("No video stream found.\n"));
493 return NULL;
495 if(a_pos==-1){
496 d_audio->sh=sh_audio=NULL;
497 } else {
498 if(force_ni || abs(a_pos-v_pos)>0x100000){ // distance > 1MB
499 mp_tmsg(MSGT_DEMUX,MSGL_INFO,"%s NON-INTERLEAVED AVI file format.\n",force_ni?"Forced":"Detected");
500 demuxer->type=DEMUXER_TYPE_AVI_NI; // HACK!!!!
501 demuxer->desc=&demuxer_desc_avi_ni; // HACK!!!!
502 pts_from_bps=1; // force BPS sync!
505 } else {
506 // no index
507 if(force_ni){
508 mp_tmsg(MSGT_DEMUX,MSGL_INFO,"Using NON-INTERLEAVED broken AVI file format.\n");
509 demuxer->type=DEMUXER_TYPE_AVI_NINI; // HACK!!!!
510 demuxer->desc=&demuxer_desc_avi_nini; // HACK!!!!
511 priv->idx_pos_a=
512 priv->idx_pos_v=demuxer->movi_start;
513 pts_from_bps=1; // force BPS sync!
515 demuxer->seekable=0;
517 if(!ds_fill_buffer(d_video)){
518 mp_msg(MSGT_DEMUX, MSGL_ERR, "AVI: %s",
519 mp_gtext("Missing video stream!? Contact the author, "
520 "it may be a bug :(\n"));
521 return NULL;
523 sh_video=d_video->sh;sh_video->ds=d_video;
524 if(d_audio->id!=-2){
525 mp_msg(MSGT_DEMUX,MSGL_V,"AVI: Searching for audio stream (id:%d)\n",d_audio->id);
526 if(!priv->audio_streams || !ds_fill_buffer(d_audio)){
527 mp_msg(MSGT_DEMUX, MSGL_INFO, "AVI: %s",
528 mp_gtext("No audio stream found -> no sound.\n"));
529 d_audio->sh=sh_audio=NULL;
530 } else {
531 sh_audio=d_audio->sh;sh_audio->ds=d_audio;
535 // calculating audio/video bitrate:
536 if(priv->idx_size>0){
537 // we have index, let's count 'em!
538 AVIINDEXENTRY *idx = priv->idx;
539 int64_t vsize=0;
540 int64_t asize=0;
541 size_t vsamples=0;
542 size_t asamples=0;
543 int i;
544 for(i=0;i<priv->idx_size;i++){
545 int id=avi_stream_id(idx[i].ckid);
546 unsigned len=idx[i].dwChunkLength;
547 if(sh_video->ds->id == id) {
548 vsize+=len;
549 ++vsamples;
551 else if(sh_audio && sh_audio->ds->id == id) {
552 asize+=len;
553 asamples+=(len+priv->audio_block_size-1)/priv->audio_block_size;
556 mp_msg(MSGT_DEMUX, MSGL_V,
557 "AVI video size=%"PRId64" (%zu) audio size=%"PRId64" (%zu)\n",
558 vsize, vsamples, asize, asamples);
559 priv->numberofframes=vsamples;
560 sh_video->i_bps=((float)vsize/(float)vsamples)*(float)sh_video->video.dwRate/(float)sh_video->video.dwScale;
561 if(sh_audio) sh_audio->i_bps=((float)asize/(float)asamples)*(float)sh_audio->audio.dwRate/(float)sh_audio->audio.dwScale;
562 } else {
563 // guessing, results may be inaccurate:
564 int64_t vsize;
565 int64_t asize=0;
567 if((priv->numberofframes=sh_video->video.dwLength)<=1)
568 // bad video header, try to get number of frames from audio
569 if(sh_audio && sh_audio->wf->nAvgBytesPerSec) priv->numberofframes=sh_video->fps*sh_audio->audio.dwLength/sh_audio->audio.dwRate*sh_audio->audio.dwScale;
570 if(priv->numberofframes<=1){
571 mp_tmsg(MSGT_SEEK,MSGL_WARN,"Could not determine number of frames (for absolute seek).\n");
572 priv->numberofframes=0;
575 if(sh_audio){
576 if(sh_audio->wf->nAvgBytesPerSec && sh_audio->audio.dwSampleSize!=1){
577 asize=(float)sh_audio->wf->nAvgBytesPerSec*sh_audio->audio.dwLength*sh_audio->audio.dwScale/sh_audio->audio.dwRate;
578 } else {
579 asize=sh_audio->audio.dwLength;
580 sh_audio->i_bps=(float)asize/(sh_video->frametime*priv->numberofframes);
583 vsize=demuxer->movi_end-demuxer->movi_start-asize-8*priv->numberofframes;
584 mp_msg(MSGT_DEMUX,MSGL_V,"AVI video size=%"PRId64" (%u) audio size=%"PRId64"\n",vsize,priv->numberofframes,asize);
585 sh_video->i_bps=(float)vsize/(sh_video->frametime*priv->numberofframes);
588 return demuxer;
593 static void demux_seek_avi(demuxer_t *demuxer, float rel_seek_secs,
594 float audio_delay, int flags)
596 avi_priv_t *priv=demuxer->priv;
597 demux_stream_t *d_audio=demuxer->audio;
598 demux_stream_t *d_video=demuxer->video;
599 sh_audio_t *sh_audio=d_audio->sh;
600 sh_video_t *sh_video=d_video->sh;
601 float skip_audio_secs=0;
603 //FIXME: OFF_T - Didn't check AVI case yet (avi files can't be >2G anyway?)
604 //================= seek in AVI ==========================
605 int rel_seek_frames=rel_seek_secs*sh_video->fps;
606 int video_chunk_pos=d_video->pos;
607 int i;
609 if(flags&SEEK_ABSOLUTE){
610 // seek absolute
611 video_chunk_pos=0;
614 if(flags&SEEK_FACTOR){
615 rel_seek_frames=rel_seek_secs*priv->numberofframes;
618 priv->skip_video_frames=0;
619 priv->avi_audio_pts=0;
621 // ------------ STEP 1: find nearest video keyframe chunk ------------
622 // find nearest video keyframe chunk pos:
623 if(rel_seek_frames>0){
624 // seek forward
625 while(video_chunk_pos<priv->idx_size-1){
626 int id=((AVIINDEXENTRY *)priv->idx)[video_chunk_pos].ckid;
627 if(avi_stream_id(id)==d_video->id){ // video frame
628 if((--rel_seek_frames)<0 && ((AVIINDEXENTRY *)priv->idx)[video_chunk_pos].dwFlags&AVIIF_KEYFRAME) break;
630 ++video_chunk_pos;
632 } else {
633 // seek backward
634 while(video_chunk_pos>0){
635 int id=((AVIINDEXENTRY *)priv->idx)[video_chunk_pos].ckid;
636 if(avi_stream_id(id)==d_video->id){ // video frame
637 if((++rel_seek_frames)>0 && ((AVIINDEXENTRY *)priv->idx)[video_chunk_pos].dwFlags&AVIIF_KEYFRAME) break;
639 --video_chunk_pos;
642 priv->idx_pos_a=priv->idx_pos_v=priv->idx_pos=video_chunk_pos;
644 // re-calc video pts:
645 d_video->pack_no=0;
646 for(i=0;i<video_chunk_pos;i++){
647 int id=((AVIINDEXENTRY *)priv->idx)[i].ckid;
648 if(avi_stream_id(id)==d_video->id) ++d_video->pack_no;
650 priv->video_pack_no=
651 sh_video->num_frames=sh_video->num_frames_decoded=d_video->pack_no;
652 priv->avi_video_pts=d_video->pack_no*(float)sh_video->video.dwScale/(float)sh_video->video.dwRate;
653 d_video->pos=video_chunk_pos;
655 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);
657 // ------------ STEP 2: seek audio, find the right chunk & pos ------------
659 d_audio->pack_no=0;
660 priv->audio_block_no=0;
661 d_audio->dpos=0;
663 if(sh_audio){
664 int i;
665 int len=0;
666 int skip_audio_bytes=0;
667 int curr_audio_pos=-1;
668 int audio_chunk_pos=-1;
669 int chunk_max=(demuxer->type==DEMUXER_TYPE_AVI)?video_chunk_pos:priv->idx_size;
671 if(sh_audio->audio.dwSampleSize){
672 // constant rate audio stream
673 /* immediate seeking to audio position, including when streams are delayed */
674 curr_audio_pos=(priv->avi_video_pts + audio_delay)*(float)sh_audio->audio.dwRate/(float)sh_audio->audio.dwScale;
675 curr_audio_pos*=sh_audio->audio.dwSampleSize;
677 // find audio chunk pos:
678 for(i=0;i<chunk_max;i++){
679 int id=((AVIINDEXENTRY *)priv->idx)[i].ckid;
680 if(avi_stream_id(id)==d_audio->id){
681 len=((AVIINDEXENTRY *)priv->idx)[i].dwChunkLength;
682 if(d_audio->dpos<=curr_audio_pos && curr_audio_pos<(d_audio->dpos+len)){
683 break;
685 ++d_audio->pack_no;
686 priv->audio_block_no+=priv->audio_block_size ?
687 ((len+priv->audio_block_size-1)/priv->audio_block_size) : 1;
688 d_audio->dpos+=len;
691 audio_chunk_pos=i;
692 skip_audio_bytes=curr_audio_pos-d_audio->dpos;
694 mp_msg(MSGT_SEEK,MSGL_V,"SEEK: i=%d (max:%d) dpos=%d (wanted:%d) \n",
695 i,chunk_max,(int)d_audio->dpos,curr_audio_pos);
697 } else {
698 // VBR audio
699 /* immediate seeking to audio position, including when streams are delayed */
700 int chunks=(priv->avi_video_pts + audio_delay)*(float)sh_audio->audio.dwRate/(float)sh_audio->audio.dwScale;
701 audio_chunk_pos=0;
703 // find audio chunk pos:
704 for(i=0;i<priv->idx_size && chunks>0;i++){
705 int id=((AVIINDEXENTRY *)priv->idx)[i].ckid;
706 if(avi_stream_id(id)==d_audio->id){
707 len=((AVIINDEXENTRY *)priv->idx)[i].dwChunkLength;
708 if(i>chunk_max){
709 skip_audio_bytes+=len;
710 } else {
711 ++d_audio->pack_no;
712 priv->audio_block_no+=priv->audio_block_size ?
713 ((len+priv->audio_block_size-1)/priv->audio_block_size) : 1;
714 d_audio->dpos+=len;
715 audio_chunk_pos=i;
717 if(priv->audio_block_size)
718 chunks-=(len+priv->audio_block_size-1)/priv->audio_block_size;
723 // Now we have:
724 // audio_chunk_pos = chunk no in index table (it's <=chunk_max)
725 // skip_audio_bytes = bytes to be skipped after chunk seek
726 // d-audio->pack_no = chunk_no in stream at audio_chunk_pos
727 // d_audio->dpos = bytepos in stream at audio_chunk_pos
728 // let's seek!
730 // update stream position:
731 d_audio->pos=audio_chunk_pos;
733 if(demuxer->type==DEMUXER_TYPE_AVI){
734 // interleaved stream:
735 if(audio_chunk_pos<video_chunk_pos){
736 // calc priv->skip_video_frames & adjust video pts counter:
737 for(i=audio_chunk_pos;i<video_chunk_pos;i++){
738 int id=((AVIINDEXENTRY *)priv->idx)[i].ckid;
739 if(avi_stream_id(id)==d_video->id) ++priv->skip_video_frames;
741 // requires for correct audio pts calculation (demuxer):
742 priv->avi_video_pts-=priv->skip_video_frames*(float)sh_video->video.dwScale/(float)sh_video->video.dwRate;
743 priv->avi_audio_pts=priv->avi_video_pts;
744 // set index position:
745 priv->idx_pos_a=priv->idx_pos_v=priv->idx_pos=audio_chunk_pos;
747 } else {
748 // non-interleaved stream:
749 priv->idx_pos_a=audio_chunk_pos;
750 priv->idx_pos_v=video_chunk_pos;
751 priv->idx_pos=(audio_chunk_pos<video_chunk_pos)?audio_chunk_pos:video_chunk_pos;
754 mp_msg(MSGT_SEEK,MSGL_V,"SEEK: idx=%d (a:%d v:%d) v.skip=%d a.skip=%d/%4.3f \n",
755 (int)priv->idx_pos,audio_chunk_pos,video_chunk_pos,
756 (int)priv->skip_video_frames,skip_audio_bytes,skip_audio_secs);
758 if(skip_audio_bytes){
759 demux_read_data(d_audio,NULL,skip_audio_bytes);
763 d_video->pts=priv->avi_video_pts; // OSD
768 static void demux_close_avi(demuxer_t *demuxer)
770 avi_priv_t* priv=demuxer->priv;
772 if(!priv)
773 return;
775 if(priv->idx_size > 0)
776 free(priv->idx);
777 free(priv);
781 static int demux_avi_control(demuxer_t *demuxer,int cmd, void *arg){
782 avi_priv_t *priv=demuxer->priv;
783 demux_stream_t *d_video=demuxer->video;
784 sh_video_t *sh_video=d_video->sh;
786 switch(cmd) {
787 case DEMUXER_CTRL_GET_TIME_LENGTH:
788 if (!priv->numberofframes || !sh_video) return DEMUXER_CTRL_DONTKNOW;
789 *((double *)arg)=(double)priv->numberofframes/sh_video->fps;
790 if (sh_video->video.dwLength<=1) return DEMUXER_CTRL_GUESS;
791 return DEMUXER_CTRL_OK;
793 case DEMUXER_CTRL_GET_PERCENT_POS:
794 if (!priv->numberofframes || !sh_video) {
795 return DEMUXER_CTRL_DONTKNOW;
797 *((int *)arg)=(int)(priv->video_pack_no*100/priv->numberofframes);
798 if (sh_video->video.dwLength<=1) return DEMUXER_CTRL_GUESS;
799 return DEMUXER_CTRL_OK;
801 case DEMUXER_CTRL_SWITCH_AUDIO:
802 case DEMUXER_CTRL_SWITCH_VIDEO: {
803 int audio = (cmd == DEMUXER_CTRL_SWITCH_AUDIO);
804 demux_stream_t *ds = audio ? demuxer->audio : demuxer->video;
805 void **streams = audio ? demuxer->a_streams : demuxer->v_streams;
806 int maxid = FFMIN(100, audio ? MAX_A_STREAMS : MAX_V_STREAMS);
807 int chunkid;
808 if (ds->id < -1)
809 ds->id = -1;
811 if (*(int *)arg >= 0)
812 ds->id = *(int *)arg;
813 else {
814 int i;
815 for (i = 0; i < maxid; i++) {
816 if (++ds->id >= maxid) ds->id = 0;
817 if (streams[ds->id]) break;
821 chunkid = (ds->id / 10 + '0') | (ds->id % 10 + '0') << 8;
822 ds->sh = NULL;
823 if (!streams[ds->id]) // stream not available
824 ds->id = -1;
825 else
826 demux_avi_select_stream(demuxer, chunkid);
827 *(int *)arg = ds->id;
828 return DEMUXER_CTRL_OK;
831 default:
832 return DEMUXER_CTRL_NOTIMPL;
837 static int avi_check_file(demuxer_t *demuxer)
839 int id=stream_read_dword_le(demuxer->stream); // "RIFF"
841 if((id==mmioFOURCC('R','I','F','F')) || (id==mmioFOURCC('O','N','2',' '))) {
842 stream_read_dword_le(demuxer->stream); //filesize
843 id=stream_read_dword_le(demuxer->stream); // "AVI "
844 if(id==formtypeAVI)
845 return DEMUXER_TYPE_AVI;
846 // "Samsung Digimax i6 PMP" crap according to bug 742
847 if(id==mmioFOURCC('A','V','I',0x19))
848 return DEMUXER_TYPE_AVI;
849 if(id==mmioFOURCC('O','N','2','f')){
850 mp_tmsg(MSGT_DEMUXER,MSGL_INFO,"ON2 AVI format");
851 return DEMUXER_TYPE_AVI;
855 return 0;
859 static demuxer_t* demux_open_hack_avi(demuxer_t *demuxer)
861 struct MPOpts *opts = demuxer->opts;
862 sh_audio_t* sh_a;
864 demuxer = demux_open_avi(demuxer);
865 if(!demuxer) return NULL; // failed to open
866 sh_a = demuxer->audio->sh;
867 if(demuxer->audio->id != -2 && sh_a) {
868 #ifdef CONFIG_OGGVORBIS
869 // support for Ogg-in-AVI:
870 if(sh_a->format == 0xFFFE)
871 demuxer = init_avi_with_ogg(demuxer);
872 else if(sh_a->format == 0x674F) {
873 stream_t* s;
874 demuxer_t *od;
875 s = new_ds_stream(demuxer->audio);
876 od = new_demuxer(opts, s,DEMUXER_TYPE_OGG,-1,-2,-2,NULL);
877 if(!demux_ogg_open(od)) {
878 mp_tmsg( MSGT_DEMUXER,MSGL_ERR,"Unable to open the Ogg demuxer.\n");
879 free_stream(s);
880 demuxer->audio->id = -2;
881 } else
882 demuxer = new_demuxers_demuxer(demuxer,od,demuxer);
884 #endif
887 return demuxer;
891 const demuxer_desc_t demuxer_desc_avi = {
892 "AVI demuxer",
893 "avi",
894 "AVI",
895 "Arpi?",
896 "AVI files, including non interleaved files",
897 DEMUXER_TYPE_AVI,
898 1, // safe autodetect
899 avi_check_file,
900 demux_avi_fill_buffer,
901 demux_open_hack_avi,
902 demux_close_avi,
903 demux_seek_avi,
904 demux_avi_control
907 const demuxer_desc_t demuxer_desc_avi_ni = {
908 "AVI demuxer, non-interleaved",
909 "avini",
910 "AVI",
911 "Arpi?",
912 "AVI files, including non interleaved files",
913 DEMUXER_TYPE_AVI,
914 1, // safe autodetect
915 avi_check_file,
916 demux_avi_fill_buffer_ni,
917 demux_open_hack_avi,
918 demux_close_avi,
919 demux_seek_avi,
920 demux_avi_control
923 const demuxer_desc_t demuxer_desc_avi_nini = {
924 "AVI demuxer, non-interleaved and no index",
925 "avinini",
926 "AVI",
927 "Arpi?",
928 "AVI files, including non interleaved files",
929 DEMUXER_TYPE_AVI,
930 1, // safe autodetect
931 avi_check_file,
932 demux_avi_fill_buffer_nini,
933 demux_open_hack_avi,
934 demux_close_avi,
935 demux_seek_avi,
936 demux_avi_control