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.
30 #include "stream/stream.h"
34 #include "aviheader.h"
36 demuxer_t
* init_avi_with_ogg(demuxer_t
* demuxer
);
37 int demux_ogg_open(demuxer_t
* demuxer
);
39 extern const demuxer_desc_t demuxer_desc_avi_ni
;
40 extern const demuxer_desc_t demuxer_desc_avi_nini
;
42 // PTS: 0=interleaved 1=BPS-based
46 static demux_stream_t
* demux_avi_select_stream(demuxer_t
*demux
,
49 int stream_id
=avi_stream_id(id
);
52 if(demux
->video
->id
==-1)
53 if(demux
->v_streams
[stream_id
])
54 demux
->video
->id
=stream_id
;
56 if(demux
->audio
->id
==-1)
57 if(demux
->a_streams
[stream_id
])
58 demux
->audio
->id
=stream_id
;
60 if(stream_id
==demux
->audio
->id
){
61 if(!demux
->audio
->sh
){
63 avi_priv_t
*priv
=demux
->priv
;
64 sh
=demux
->audio
->sh
=demux
->a_streams
[stream_id
];
65 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected AVI audio ID = %d\n",demux
->audio
->id
);
67 priv
->audio_block_size
=sh
->wf
->nBlockAlign
;
68 if(!priv
->audio_block_size
){
69 // for PCM audio we can calculate the blocksize:
71 priv
->audio_block_size
=sh
->wf
->nChannels
*(sh
->wf
->wBitsPerSample
/8);
73 priv
->audio_block_size
=1; // hope the best...
75 // workaround old mencoder's bug:
76 if(sh
->audio
.dwSampleSize
==1 && sh
->audio
.dwScale
==1 &&
77 (sh
->wf
->nBlockAlign
==1152 || sh
->wf
->nBlockAlign
==576)){
78 mp_tmsg(MSGT_DEMUX
,MSGL_WARN
,"AVI: Working around CBR-MP3 nBlockAlign header bug!\n");
79 priv
->audio_block_size
=1;
83 priv
->audio_block_size
=sh
->audio
.dwSampleSize
;
88 if(stream_id
==demux
->video
->id
){
89 if(!demux
->video
->sh
){
90 demux
->video
->sh
=demux
->v_streams
[stream_id
];
91 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected AVI video ID = %d\n",demux
->video
->id
);
95 if(id
!=mmioFOURCC('J','U','N','K')){
97 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"Unknown chunk: %.4s (%X)\n",(char *) &id
,id
);
103 static int valid_fourcc(unsigned int id
){
104 static const char valid
[] = "0123456789abcdefghijklmnopqrstuvwxyz"
105 "ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
106 unsigned char* fcc
=(unsigned char*)(&id
);
107 return strchr(valid
, fcc
[0]) && strchr(valid
, fcc
[1]) &&
108 strchr(valid
, fcc
[2]) && strchr(valid
, fcc
[3]);
111 static int choose_chunk_len(unsigned int len1
,unsigned int len2
){
112 // len1 has a bit more priority than len2. len1!=len2
113 // Note: this is a first-idea-logic, may be wrong. comments welcomed.
115 // prefer small frames rather than 0
116 if(!len1
) return (len2
>0x80000) ? len1
: len2
;
117 if(!len2
) return (len1
>0x100000) ? len2
: len1
;
119 // choose the smaller value:
120 return (len1
<len2
)? len1
: len2
;
123 static int demux_avi_read_packet(demuxer_t
*demux
,demux_stream_t
*ds
,unsigned int id
,unsigned int len
,int idxpos
,int flags
){
124 avi_priv_t
*priv
=demux
->priv
;
128 mp_dbg(MSGT_DEMUX
,MSGL_DBG3
,"demux_avi.read_packet: %X\n",id
);
130 if(ds
==demux
->audio
){
131 if(priv
->pts_corrected
==0){
132 if(priv
->pts_has_video
){
133 // we have video pts now
135 if(((sh_audio_t
*)(ds
->sh
))->wf
->nAvgBytesPerSec
)
136 delay
=(float)priv
->pts_corr_bytes
/((sh_audio_t
*)(ds
->sh
))->wf
->nAvgBytesPerSec
;
137 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
);
138 //priv->pts_correction=-priv->avi_audio_pts+delay;
139 priv
->pts_correction
=delay
-priv
->avi_audio_pts
;
140 priv
->avi_audio_pts
+=priv
->pts_correction
;
141 priv
->pts_corrected
=1;
143 priv
->pts_corr_bytes
+=len
;
146 pts
= priv
->audio_block_no
*
147 (float)((sh_audio_t
*)demux
->audio
->sh
)->audio
.dwScale
/
148 (float)((sh_audio_t
*)demux
->audio
->sh
)->audio
.dwRate
;
150 pts
=priv
->avi_audio_pts
; //+priv->pts_correction;
151 priv
->avi_audio_pts
=0;
152 // update blockcount:
153 priv
->audio_block_no
+=priv
->audio_block_size
?
154 ((len
+priv
->audio_block_size
-1)/priv
->audio_block_size
) : 1;
156 if(ds
==demux
->video
){
158 if(priv
->skip_video_frames
>0){
159 // drop frame (seeking)
160 --priv
->skip_video_frames
;
164 pts
= priv
->avi_video_pts
= priv
->video_pack_no
*
165 (float)((sh_video_t
*)demux
->video
->sh
)->video
.dwScale
/
166 (float)((sh_video_t
*)demux
->video
->sh
)->video
.dwRate
;
168 priv
->avi_audio_pts
=priv
->avi_video_pts
+priv
->pts_correction
;
169 priv
->pts_has_video
=1;
171 if(ds
) ++priv
->video_pack_no
;
175 skip
=(len
+1)&(~1); // total bytes in this chunk
178 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
,"DEMUX_AVI: Read %d data bytes from packet %04X\n",len
,id
);
179 ds_read_packet(ds
,demux
->stream
,len
,pts
,idxpos
,flags
);
182 skip
= FFMAX(skip
, 0);
183 if (avi_stream_id(id
) > 99 && id
!= mmioFOURCC('J','U','N','K'))
184 skip
= FFMIN(skip
, 65536);
186 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
,"DEMUX_AVI: Skipping %d bytes from packet %04X\n",skip
,id
);
187 stream_skip(demux
->stream
,skip
);
192 static uint32_t avi_find_id(stream_t
*stream
) {
193 uint32_t id
= stream_read_dword_le(stream
);
195 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "Incomplete stream? Trying resync.\n");
197 id
= stream_read_dword_le(stream
);
198 if (stream_eof(stream
)) return 0;
199 } while (avi_stream_id(id
) > 99);
205 // 0 = EOF or no stream found
206 // 1 = successfully read a packet
207 static int demux_avi_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
){
208 avi_priv_t
*priv
=demux
->priv
;
216 AVIINDEXENTRY
*idx
=NULL
;
217 if(priv
->idx_size
>0 && priv
->idx_pos
<priv
->idx_size
){
220 idx
=&((AVIINDEXENTRY
*)priv
->idx
)[priv
->idx_pos
++];
222 if(idx
->dwFlags
&AVIIF_LIST
){
226 if(!demux_avi_select_stream(demux
,idx
->ckid
)){
227 mp_dbg(MSGT_DEMUX
,MSGL_DBG3
,"Skip chunk %.4s (0x%X) \n",(char *)&idx
->ckid
,(unsigned int)idx
->ckid
);
228 continue; // skip this chunk
231 pos
= (off_t
)priv
->idx_offset
+AVI_IDX_OFFSET(idx
);
232 if((pos
<demux
->movi_start
|| pos
>=demux
->movi_end
) && (demux
->movi_end
>demux
->movi_start
) && (demux
->stream
->flags
& STREAM_SEEK
)){
233 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkOffset out of range! idx=0x%"PRIX64
" \n",(int64_t)pos
);
236 stream_seek(demux
->stream
,pos
);
237 demux
->filepos
=stream_tell(demux
->stream
);
238 id
=stream_read_dword_le(demux
->stream
);
239 if(stream_eof(demux
->stream
)) return 0; // EOF!
242 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id
,(char *)&idx
->ckid
);
243 if(valid_fourcc(idx
->ckid
))
244 id
=idx
->ckid
; // use index if valid
246 if(!valid_fourcc(id
)) continue; // drop chunk if both id and idx bad
248 len
=stream_read_dword_le(demux
->stream
);
249 if((len
!=idx
->dwChunkLength
)&&((len
+1)!=idx
->dwChunkLength
)){
250 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkSize mismatch! raw=%d idx=%d \n",len
,idx
->dwChunkLength
);
251 if(len
>0x200000 && idx
->dwChunkLength
>0x200000) continue; // both values bad :(
252 len
=choose_chunk_len(idx
->dwChunkLength
,len
);
254 if(!(idx
->dwFlags
&AVIIF_KEYFRAME
)) flags
=0;
256 demux
->filepos
=stream_tell(demux
->stream
);
257 if(demux
->filepos
>=demux
->movi_end
&& demux
->movi_end
>demux
->movi_start
&& (demux
->stream
->flags
& STREAM_SEEK
)){
258 demux
->stream
->eof
=1;
261 id
=avi_find_id(demux
->stream
);
262 len
=stream_read_dword_le(demux
->stream
);
263 if(stream_eof(demux
->stream
)) return 0; // EOF!
265 if(id
==mmioFOURCC('L','I','S','T') || id
==mmioFOURCC('R', 'I', 'F', 'F')){
266 id
=stream_read_dword_le(demux
->stream
); // list or RIFF type
271 ds
=demux_avi_select_stream(demux
,id
);
273 if(ds
->packs
+1>=MAX_PACKS
|| ds
->bytes
+len
>=MAX_PACK_BYTES
){
274 // this packet will cause a buffer overflow, switch to -ni mode!!!
275 mp_tmsg(MSGT_DEMUX
,MSGL_WARN
,"\nBadly interleaved AVI file detected - switching to -ni mode...\n");
276 if(priv
->idx_size
>0){
278 demux
->type
=DEMUXER_TYPE_AVI_NI
;
279 demux
->desc
=&demuxer_desc_avi_ni
;
280 --priv
->idx_pos
; // hack
283 demux
->type
=DEMUXER_TYPE_AVI_NINI
;
284 demux
->desc
=&demuxer_desc_avi_nini
;
285 priv
->idx_pos
=demux
->filepos
; // hack
287 priv
->idx_pos_v
=priv
->idx_pos_a
=priv
->idx_pos
;
288 // quit now, we can't even (no enough buffer memory) read this packet :(
292 ret
=demux_avi_read_packet(demux
,ds
,id
,len
,priv
->idx_pos
-1,flags
);
299 // 0 = EOF or no stream found
300 // 1 = successfully read a packet
301 static int demux_avi_fill_buffer_ni(demuxer_t
*demux
,demux_stream_t
* ds
){
302 avi_priv_t
*priv
=demux
->priv
;
309 AVIINDEXENTRY
*idx
=NULL
;
311 demux
->filepos
=stream_tell(demux
->stream
);
313 if(ds
==demux
->video
) idx_pos
=priv
->idx_pos_v
++; else
314 if(ds
==demux
->audio
) idx_pos
=priv
->idx_pos_a
++; else
315 idx_pos
=priv
->idx_pos
++;
317 if(priv
->idx_size
>0 && idx_pos
<priv
->idx_size
){
319 idx
=&((AVIINDEXENTRY
*)priv
->idx
)[idx_pos
];
321 if(idx
->dwFlags
&AVIIF_LIST
){
325 if(ds
&& demux_avi_select_stream(demux
,idx
->ckid
)!=ds
){
326 mp_dbg(MSGT_DEMUX
,MSGL_DBG3
,"Skip chunk %.4s (0x%X) \n",(char *)&idx
->ckid
,(unsigned int)idx
->ckid
);
327 continue; // skip this chunk
330 pos
= priv
->idx_offset
+AVI_IDX_OFFSET(idx
);
331 if((pos
<demux
->movi_start
|| pos
>=demux
->movi_end
) && (demux
->movi_end
>demux
->movi_start
)){
332 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkOffset out of range! current=0x%"PRIX64
" idx=0x%"PRIX64
" \n",(int64_t)demux
->filepos
,(int64_t)pos
);
335 stream_seek(demux
->stream
,pos
);
337 id
=stream_read_dword_le(demux
->stream
);
339 if(stream_eof(demux
->stream
)) return 0;
342 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id
,(char *)&idx
->ckid
);
343 if(valid_fourcc(idx
->ckid
))
344 id
=idx
->ckid
; // use index if valid
346 if(!valid_fourcc(id
)) continue; // drop chunk if both id and idx bad
348 len
=stream_read_dword_le(demux
->stream
);
349 if((len
!=idx
->dwChunkLength
)&&((len
+1)!=idx
->dwChunkLength
)){
350 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkSize mismatch! raw=%d idx=%d \n",len
,idx
->dwChunkLength
);
351 if(len
>0x200000 && idx
->dwChunkLength
>0x200000) continue; // both values bad :(
352 len
=choose_chunk_len(idx
->dwChunkLength
,len
);
354 if(!(idx
->dwFlags
&AVIIF_KEYFRAME
)) flags
=0;
356 ret
=demux_avi_read_packet(demux
,demux_avi_select_stream(demux
,id
),id
,len
,idx_pos
,flags
);
363 // 0 = EOF or no stream found
364 // 1 = successfully read a packet
365 static int demux_avi_fill_buffer_nini(demuxer_t
*demux
,demux_stream_t
* ds
){
366 avi_priv_t
*priv
=demux
->priv
;
372 if(ds
==demux
->video
) fpos
=&priv
->idx_pos_v
; else
373 if(ds
==demux
->audio
) fpos
=&priv
->idx_pos_a
; else
376 stream_seek(demux
->stream
,fpos
[0]);
380 demux
->filepos
=stream_tell(demux
->stream
);
381 if(demux
->filepos
>=demux
->movi_end
&& (demux
->movi_end
>demux
->movi_start
)){
386 id
=avi_find_id(demux
->stream
);
387 len
=stream_read_dword_le(demux
->stream
);
389 if(stream_eof(demux
->stream
)) return 0;
391 if(id
==mmioFOURCC('L','I','S','T')){
392 id
=stream_read_dword_le(demux
->stream
); // list type
396 if(id
==mmioFOURCC('R','I','F','F')){
397 mp_msg(MSGT_DEMUX
,MSGL_V
,"additional RIFF header...\n");
398 id
=stream_read_dword_le(demux
->stream
); // "AVIX"
402 if(ds
==demux_avi_select_stream(demux
,id
)){
404 ret
=demux_avi_read_packet(demux
,ds
,id
,len
,priv
->idx_pos
-1,0);
407 int skip
=(len
+1)&(~1); // total bytes in this chunk
408 stream_skip(demux
->stream
,skip
);
412 fpos
[0]=stream_tell(demux
->stream
);
416 // AVI demuxer parameters:
417 int index_mode
=-1; // -1=untouched 0=don't use index 1=use (generate) index
418 char *index_file_save
= NULL
, *index_file_load
= NULL
;
419 int force_ni
=0; // force non-interleaved AVI parsing
421 void read_avi_header(demuxer_t
*demuxer
,int index_mode
);
423 static demuxer_t
* demux_open_avi(demuxer_t
* demuxer
){
424 demux_stream_t
*d_audio
=demuxer
->audio
;
425 demux_stream_t
*d_video
=demuxer
->video
;
426 sh_audio_t
*sh_audio
=NULL
;
427 sh_video_t
*sh_video
=NULL
;
428 avi_priv_t
* priv
=malloc(sizeof(avi_priv_t
));
431 priv
->avi_audio_pts
=priv
->avi_video_pts
=0.0f
;
432 priv
->pts_correction
=0.0f
;
433 priv
->skip_video_frames
=0;
434 priv
->pts_corr_bytes
=0;
435 priv
->pts_has_video
=priv
->pts_corrected
=0;
436 priv
->video_pack_no
=0;
437 priv
->audio_block_no
=0;
438 priv
->audio_block_size
=0;
440 priv
->suidx_size
= 0;
443 demuxer
->priv
=(void*)priv
;
446 read_avi_header(demuxer
,(demuxer
->stream
->flags
& STREAM_SEEK_BW
)?index_mode
:-2);
448 if(demuxer
->audio
->id
>=0 && !demuxer
->a_streams
[demuxer
->audio
->id
]){
449 mp_tmsg(MSGT_DEMUX
,MSGL_WARN
,"AVI: invalid audio stream ID: %d - ignoring (nosound)\n",demuxer
->audio
->id
);
450 demuxer
->audio
->id
=-2; // disabled
452 if(demuxer
->video
->id
>=0 && !demuxer
->v_streams
[demuxer
->video
->id
]){
453 mp_tmsg(MSGT_DEMUX
,MSGL_WARN
,"AVI: invalid video stream ID: %d - ignoring (using default)\n",demuxer
->video
->id
);
454 demuxer
->video
->id
=-1; // autodetect
457 stream_reset(demuxer
->stream
);
458 stream_seek(demuxer
->stream
,demuxer
->movi_start
);
462 if(priv
->idx_size
>1){
463 // decide index format:
465 if((AVI_IDX_OFFSET(&((AVIINDEXENTRY
*)priv
->idx
)[0])<demuxer
->movi_start
||
466 AVI_IDX_OFFSET(&((AVIINDEXENTRY
*)priv
->idx
)[1])<demuxer
->movi_start
)&& !priv
->isodml
)
467 priv
->idx_offset
=demuxer
->movi_start
-4;
471 if(AVI_IDX_OFFSET(&((AVIINDEXENTRY
*)priv
->idx
)[0])<demuxer
->movi_start
)
472 priv
->idx_offset
=demuxer
->movi_start
-4;
476 mp_msg(MSGT_DEMUX
,MSGL_V
,"AVI index offset: 0x%X (movi=0x%X idx0=0x%X idx1=0x%X)\n",
477 (int)priv
->idx_offset
,(int)demuxer
->movi_start
,
478 (int)((AVIINDEXENTRY
*)priv
->idx
)[0].dwChunkOffset
,
479 (int)((AVIINDEXENTRY
*)priv
->idx
)[1].dwChunkOffset
);
482 if(priv
->idx_size
>0){
483 // check that file is non-interleaved:
487 for(i
=0;i
<priv
->idx_size
;i
++){
488 AVIINDEXENTRY
* idx
=&((AVIINDEXENTRY
*)priv
->idx
)[i
];
489 demux_stream_t
* ds
=demux_avi_select_stream(demuxer
,idx
->ckid
);
490 off_t pos
= priv
->idx_offset
+ AVI_IDX_OFFSET(idx
);
491 if(a_pos
==-1 && ds
==demuxer
->audio
){
495 if(v_pos
==-1 && ds
==demuxer
->video
){
501 mp_tmsg(MSGT_DEMUX
,MSGL_ERR
,"AVI_NI: " "No video stream found.\n");
505 d_audio
->sh
=sh_audio
=NULL
;
507 if(force_ni
|| abs(a_pos
-v_pos
)>0x100000){ // distance > 1MB
508 mp_tmsg(MSGT_DEMUX
,MSGL_INFO
,"%s NON-INTERLEAVED AVI file format.\n",force_ni
?"Forced":"Detected");
509 demuxer
->type
=DEMUXER_TYPE_AVI_NI
; // HACK!!!!
510 demuxer
->desc
=&demuxer_desc_avi_ni
; // HACK!!!!
511 pts_from_bps
=1; // force BPS sync!
517 mp_tmsg(MSGT_DEMUX
,MSGL_INFO
,"Using NON-INTERLEAVED broken AVI file format.\n");
518 demuxer
->type
=DEMUXER_TYPE_AVI_NINI
; // HACK!!!!
519 demuxer
->desc
=&demuxer_desc_avi_nini
; // HACK!!!!
521 priv
->idx_pos_v
=demuxer
->movi_start
;
522 pts_from_bps
=1; // force BPS sync!
526 if(!ds_fill_buffer(d_video
)){
527 mp_tmsg(MSGT_DEMUX
,MSGL_ERR
,"AVI: " "Missing video stream!? Contact the author, it may be a bug :(\n");
530 sh_video
=d_video
->sh
;sh_video
->ds
=d_video
;
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_tmsg(MSGT_DEMUX
,MSGL_INFO
,"AVI: " "No audio stream found -> no sound.\n");
535 d_audio
->sh
=sh_audio
=NULL
;
537 sh_audio
=d_audio
->sh
;sh_audio
->ds
=d_audio
;
541 // calculating audio/video bitrate:
542 if(priv
->idx_size
>0){
543 // we have index, let's count 'em!
549 for(i
=0;i
<priv
->idx_size
;i
++){
550 int id
=avi_stream_id(((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
);
551 int len
=((AVIINDEXENTRY
*)priv
->idx
)[i
].dwChunkLength
;
552 if(sh_video
->ds
->id
== id
) {
556 else if(sh_audio
&& sh_audio
->ds
->id
== id
) {
558 asamples
+=(len
+priv
->audio_block_size
-1)/priv
->audio_block_size
;
561 mp_msg(MSGT_DEMUX
,MSGL_V
,"AVI video size=%"PRId64
" (%u) audio size=%"PRId64
" (%u)\n",vsize
,vsamples
,asize
,asamples
);
562 priv
->numberofframes
=vsamples
;
563 sh_video
->i_bps
=((float)vsize
/(float)vsamples
)*(float)sh_video
->video
.dwRate
/(float)sh_video
->video
.dwScale
;
564 if(sh_audio
) sh_audio
->i_bps
=((float)asize
/(float)asamples
)*(float)sh_audio
->audio
.dwRate
/(float)sh_audio
->audio
.dwScale
;
566 // guessing, results may be inaccurate:
570 if((priv
->numberofframes
=sh_video
->video
.dwLength
)<=1)
571 // bad video header, try to get number of frames from audio
572 if(sh_audio
&& sh_audio
->wf
->nAvgBytesPerSec
) priv
->numberofframes
=sh_video
->fps
*sh_audio
->audio
.dwLength
/sh_audio
->audio
.dwRate
*sh_audio
->audio
.dwScale
;
573 if(priv
->numberofframes
<=1){
574 mp_tmsg(MSGT_SEEK
,MSGL_WARN
,"Could not determine number of frames (for absolute seek).\n");
575 priv
->numberofframes
=0;
579 if(sh_audio
->wf
->nAvgBytesPerSec
&& sh_audio
->audio
.dwSampleSize
!=1){
580 asize
=(float)sh_audio
->wf
->nAvgBytesPerSec
*sh_audio
->audio
.dwLength
*sh_audio
->audio
.dwScale
/sh_audio
->audio
.dwRate
;
582 asize
=sh_audio
->audio
.dwLength
;
583 sh_audio
->i_bps
=(float)asize
/(sh_video
->frametime
*priv
->numberofframes
);
586 vsize
=demuxer
->movi_end
-demuxer
->movi_start
-asize
-8*priv
->numberofframes
;
587 mp_msg(MSGT_DEMUX
,MSGL_V
,"AVI video size=%"PRId64
" (%u) audio size=%"PRId64
"\n",vsize
,priv
->numberofframes
,asize
);
588 sh_video
->i_bps
=(float)vsize
/(sh_video
->frametime
*priv
->numberofframes
);
596 static void demux_seek_avi(demuxer_t
*demuxer
,float rel_seek_secs
,float audio_delay
,int flags
){
597 avi_priv_t
*priv
=demuxer
->priv
;
598 demux_stream_t
*d_audio
=demuxer
->audio
;
599 demux_stream_t
*d_video
=demuxer
->video
;
600 sh_audio_t
*sh_audio
=d_audio
->sh
;
601 sh_video_t
*sh_video
=d_video
->sh
;
602 float skip_audio_secs
=0;
604 //FIXME: OFF_T - Didn't check AVI case yet (avi files can't be >2G anyway?)
605 //================= seek in AVI ==========================
606 int rel_seek_frames
=rel_seek_secs
*sh_video
->fps
;
607 int video_chunk_pos
=d_video
->pos
;
610 if(flags
&SEEK_ABSOLUTE
){
615 if(flags
&SEEK_FACTOR
){
616 rel_seek_frames
=rel_seek_secs
*priv
->numberofframes
;
619 priv
->skip_video_frames
=0;
620 priv
->avi_audio_pts
=0;
622 // ------------ STEP 1: find nearest video keyframe chunk ------------
623 // find nearest video keyframe chunk pos:
624 if(rel_seek_frames
>0){
626 while(video_chunk_pos
<priv
->idx_size
-1){
627 int id
=((AVIINDEXENTRY
*)priv
->idx
)[video_chunk_pos
].ckid
;
628 if(avi_stream_id(id
)==d_video
->id
){ // video frame
629 if((--rel_seek_frames
)<0 && ((AVIINDEXENTRY
*)priv
->idx
)[video_chunk_pos
].dwFlags
&AVIIF_KEYFRAME
) break;
635 while(video_chunk_pos
>0){
636 int id
=((AVIINDEXENTRY
*)priv
->idx
)[video_chunk_pos
].ckid
;
637 if(avi_stream_id(id
)==d_video
->id
){ // video frame
638 if((++rel_seek_frames
)>0 && ((AVIINDEXENTRY
*)priv
->idx
)[video_chunk_pos
].dwFlags
&AVIIF_KEYFRAME
) break;
643 priv
->idx_pos_a
=priv
->idx_pos_v
=priv
->idx_pos
=video_chunk_pos
;
645 // re-calc video pts:
647 for(i
=0;i
<video_chunk_pos
;i
++){
648 int id
=((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
;
649 if(avi_stream_id(id
)==d_video
->id
) ++d_video
->pack_no
;
652 sh_video
->num_frames
=sh_video
->num_frames_decoded
=d_video
->pack_no
;
653 priv
->avi_video_pts
=d_video
->pack_no
*(float)sh_video
->video
.dwScale
/(float)sh_video
->video
.dwRate
;
654 d_video
->pos
=video_chunk_pos
;
656 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
);
658 // ------------ STEP 2: seek audio, find the right chunk & pos ------------
661 priv
->audio_block_no
=0;
667 int skip_audio_bytes
=0;
668 int curr_audio_pos
=-1;
669 int audio_chunk_pos
=-1;
670 int chunk_max
=(demuxer
->type
==DEMUXER_TYPE_AVI
)?video_chunk_pos
:priv
->idx_size
;
672 if(sh_audio
->audio
.dwSampleSize
){
673 // constant rate audio stream
674 /* immediate seeking to audio position, including when streams are delayed */
675 curr_audio_pos
=(priv
->avi_video_pts
+ audio_delay
)*(float)sh_audio
->audio
.dwRate
/(float)sh_audio
->audio
.dwScale
;
676 curr_audio_pos
*=sh_audio
->audio
.dwSampleSize
;
678 // find audio chunk pos:
679 for(i
=0;i
<chunk_max
;i
++){
680 int id
=((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
;
681 if(avi_stream_id(id
)==d_audio
->id
){
682 len
=((AVIINDEXENTRY
*)priv
->idx
)[i
].dwChunkLength
;
683 if(d_audio
->dpos
<=curr_audio_pos
&& curr_audio_pos
<(d_audio
->dpos
+len
)){
687 priv
->audio_block_no
+=priv
->audio_block_size
?
688 ((len
+priv
->audio_block_size
-1)/priv
->audio_block_size
) : 1;
693 skip_audio_bytes
=curr_audio_pos
-d_audio
->dpos
;
695 mp_msg(MSGT_SEEK
,MSGL_V
,"SEEK: i=%d (max:%d) dpos=%d (wanted:%d) \n",
696 i
,chunk_max
,(int)d_audio
->dpos
,curr_audio_pos
);
700 /* immediate seeking to audio position, including when streams are delayed */
701 int chunks
=(priv
->avi_video_pts
+ audio_delay
)*(float)sh_audio
->audio
.dwRate
/(float)sh_audio
->audio
.dwScale
;
704 // find audio chunk pos:
705 for(i
=0;i
<priv
->idx_size
&& chunks
>0;i
++){
706 int id
=((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
;
707 if(avi_stream_id(id
)==d_audio
->id
){
708 len
=((AVIINDEXENTRY
*)priv
->idx
)[i
].dwChunkLength
;
710 skip_audio_bytes
+=len
;
713 priv
->audio_block_no
+=priv
->audio_block_size
?
714 ((len
+priv
->audio_block_size
-1)/priv
->audio_block_size
) : 1;
718 if(priv
->audio_block_size
)
719 chunks
-=(len
+priv
->audio_block_size
-1)/priv
->audio_block_size
;
725 // audio_chunk_pos = chunk no in index table (it's <=chunk_max)
726 // skip_audio_bytes = bytes to be skipped after chunk seek
727 // d-audio->pack_no = chunk_no in stream at audio_chunk_pos
728 // d_audio->dpos = bytepos in stream at audio_chunk_pos
731 // update stream position:
732 d_audio
->pos
=audio_chunk_pos
;
734 if(demuxer
->type
==DEMUXER_TYPE_AVI
){
735 // interleaved stream:
736 if(audio_chunk_pos
<video_chunk_pos
){
737 // calc priv->skip_video_frames & adjust video pts counter:
738 for(i
=audio_chunk_pos
;i
<video_chunk_pos
;i
++){
739 int id
=((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
;
740 if(avi_stream_id(id
)==d_video
->id
) ++priv
->skip_video_frames
;
742 // requires for correct audio pts calculation (demuxer):
743 priv
->avi_video_pts
-=priv
->skip_video_frames
*(float)sh_video
->video
.dwScale
/(float)sh_video
->video
.dwRate
;
744 priv
->avi_audio_pts
=priv
->avi_video_pts
;
745 // set index position:
746 priv
->idx_pos_a
=priv
->idx_pos_v
=priv
->idx_pos
=audio_chunk_pos
;
749 // non-interleaved stream:
750 priv
->idx_pos_a
=audio_chunk_pos
;
751 priv
->idx_pos_v
=video_chunk_pos
;
752 priv
->idx_pos
=(audio_chunk_pos
<video_chunk_pos
)?audio_chunk_pos
:video_chunk_pos
;
755 mp_msg(MSGT_SEEK
,MSGL_V
,"SEEK: idx=%d (a:%d v:%d) v.skip=%d a.skip=%d/%4.3f \n",
756 (int)priv
->idx_pos
,audio_chunk_pos
,video_chunk_pos
,
757 (int)priv
->skip_video_frames
,skip_audio_bytes
,skip_audio_secs
);
759 if(skip_audio_bytes
){
760 demux_read_data(d_audio
,NULL
,skip_audio_bytes
);
764 d_video
->pts
=priv
->avi_video_pts
; // OSD
769 static void demux_close_avi(demuxer_t
*demuxer
) {
770 avi_priv_t
* priv
=demuxer
->priv
;
775 if(priv
->idx_size
> 0)
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
;
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
);
809 return DEMUXER_CTRL_NOTIMPL
;
811 if (*(int *)arg
>= 0)
812 ds
->id
= *(int *)arg
;
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;
823 if (!streams
[ds
->id
]) // stream not available
826 demux_avi_select_stream(demuxer
, chunkid
);
827 *(int *)arg
= ds
->id
;
828 return DEMUXER_CTRL_OK
;
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 "
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
;
859 static demuxer_t
* demux_open_hack_avi(demuxer_t
*demuxer
)
861 struct MPOpts
*opts
= demuxer
->opts
;
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) {
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");
880 demuxer
->audio
->id
= -2;
882 demuxer
= new_demuxers_demuxer(demuxer
,od
,demuxer
);
891 const demuxer_desc_t demuxer_desc_avi
= {
896 "AVI files, including non interleaved files",
898 1, // safe autodetect
900 demux_avi_fill_buffer
,
907 const demuxer_desc_t demuxer_desc_avi_ni
= {
908 "AVI demuxer, non-interleaved",
912 "AVI files, including non interleaved files",
914 1, // safe autodetect
916 demux_avi_fill_buffer_ni
,
923 const demuxer_desc_t demuxer_desc_avi_nini
= {
924 "AVI demuxer, non-interleaved and no index",
928 "AVI files, including non interleaved files",
930 1, // safe autodetect
932 demux_avi_fill_buffer_nini
,