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.
29 #include "stream/stream.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
42 static demux_stream_t
*demux_avi_select_stream(demuxer_t
*demux
,
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
){
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
);
63 priv
->audio_block_size
=sh
->wf
->nBlockAlign
;
64 if(!priv
->audio_block_size
){
65 // for PCM audio we can calculate the blocksize:
67 priv
->audio_block_size
=sh
->wf
->nChannels
*(sh
->wf
->wBitsPerSample
/8);
69 priv
->audio_block_size
=1; // hope the best...
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;
79 priv
->audio_block_size
=sh
->audio
.dwSampleSize
;
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
);
91 if(id
!=mmioFOURCC('J','U','N','K')){
93 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"Unknown chunk: %.4s (%X)\n",(char *) &id
,id
);
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
;
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
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;
145 priv
->pts_corr_bytes
+=len
;
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
;
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;
158 if(ds
==demux
->video
){
160 if(priv
->skip_video_frames
>0){
161 // drop frame (seeking)
162 --priv
->skip_video_frames
;
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
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
);
184 skip
= FFMAX(skip
, 0);
185 if (avi_stream_id(id
) > 99 && id
!= mmioFOURCC('J','U','N','K'))
186 skip
= FFMIN(skip
, 65536);
188 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
,"DEMUX_AVI: Skipping %d bytes from packet %04X\n",skip
,id
);
189 stream_skip(demux
->stream
,skip
);
194 static uint32_t avi_find_id(stream_t
*stream
) {
195 uint32_t id
= stream_read_dword_le(stream
);
197 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "Incomplete stream? Trying resync.\n");
199 id
= stream_read_dword_le(stream
);
200 if (stream_eof(stream
)) return 0;
201 } while (avi_stream_id(id
) > 99);
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
;
218 AVIINDEXENTRY
*idx
=NULL
;
219 if(priv
->idx_size
>0 && priv
->idx_pos
<priv
->idx_size
){
222 idx
=&((AVIINDEXENTRY
*)priv
->idx
)[priv
->idx_pos
++];
224 if(idx
->dwFlags
&AVIIF_LIST
){
225 if (!valid_stream_id(idx
->ckid
))
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
);
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!
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
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;
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;
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
277 ds
=demux_avi_select_stream(demux
,id
);
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){
284 demux
->type
=DEMUXER_TYPE_AVI_NI
;
285 demux
->desc
=&demuxer_desc_avi_ni
;
286 --priv
->idx_pos
; // hack
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 :(
298 ret
=demux_avi_read_packet(demux
,ds
,id
,len
,priv
->idx_pos
-1,flags
);
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
;
316 AVIINDEXENTRY
*idx
=NULL
;
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
){
326 idx
=&((AVIINDEXENTRY
*)priv
->idx
)[idx_pos
];
328 if(idx
->dwFlags
&AVIIF_LIST
){
329 if (!valid_stream_id(idx
->ckid
))
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
);
346 stream_seek(demux
->stream
,pos
);
348 id
=stream_read_dword_le(demux
->stream
);
350 if(stream_eof(demux
->stream
)) return 0;
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
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;
367 ret
=demux_avi_read_packet(demux
,demux_avi_select_stream(demux
,id
),id
,len
,idx_pos
,flags
);
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
;
384 if(ds
==demux
->video
) fpos
=&priv
->idx_pos_v
; else
385 if(ds
==demux
->audio
) fpos
=&priv
->idx_pos_a
; else
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
)){
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
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"
414 if(ds
==demux_avi_select_stream(demux
,id
)){
416 ret
=demux_avi_read_packet(demux
,ds
,id
,len
,priv
->idx_pos
-1,0);
419 int skip
=(len
+1)&(~1); // total bytes in this chunk
420 stream_skip(demux
->stream
,skip
);
424 fpos
[0]=stream_tell(demux
->stream
);
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
;
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:
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;
463 if(AVI_IDX_OFFSET(&((AVIINDEXENTRY
*)priv
->idx
)[0])<demuxer
->movi_start
)
464 priv
->idx_offset
=demuxer
->movi_start
-4;
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:
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
){
485 if(v_pos
==-1 && ds
==demuxer
->video
){
491 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "AVI_NI: %s",
492 mp_gtext("No video stream found.\n"));
496 d_audio
->sh
=sh_audio
=NULL
;
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!
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!!!!
512 priv
->idx_pos_v
=demuxer
->movi_start
;
513 pts_from_bps
=1; // force BPS sync!
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"));
523 sh_video
=d_video
->sh
;sh_video
->ds
=d_video
;
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
;
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
;
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
) {
551 else if(sh_audio
&& sh_audio
->ds
->id
== id
) {
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
;
563 // guessing, results may be inaccurate:
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;
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
;
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
);
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
;
609 if(flags
&SEEK_ABSOLUTE
){
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){
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;
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;
642 priv
->idx_pos_a
=priv
->idx_pos_v
=priv
->idx_pos
=video_chunk_pos
;
644 // re-calc video pts:
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
;
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 ------------
660 priv
->audio_block_no
=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
)){
686 priv
->audio_block_no
+=priv
->audio_block_size
?
687 ((len
+priv
->audio_block_size
-1)/priv
->audio_block_size
) : 1;
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
);
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
;
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
;
709 skip_audio_bytes
+=len
;
712 priv
->audio_block_no
+=priv
->audio_block_size
?
713 ((len
+priv
->audio_block_size
-1)/priv
->audio_block_size
) : 1;
717 if(priv
->audio_block_size
)
718 chunks
-=(len
+priv
->audio_block_size
-1)/priv
->audio_block_size
;
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
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
;
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
;
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
);
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
,