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
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
;
47 priv
->audio_block_size
= sh
->audio
.dwSampleSize
;
49 priv
->audio_block_size
= sh
->wf
->nBlockAlign
;
50 if (!priv
->audio_block_size
) {
51 // for PCM audio we can calculate the blocksize:
53 priv
->audio_block_size
= sh
->wf
->nChannels
*(sh
->wf
->wBitsPerSample
/8);
55 priv
->audio_block_size
= 1; // hope the best...
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;
68 static demux_stream_t
*demux_avi_select_stream(demuxer_t
*demux
,
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
);
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
);
97 if(id
!=mmioFOURCC('J','U','N','K')){
99 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"Unknown chunk: %.4s (%X)\n",(char *) &id
,id
);
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
;
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
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;
151 priv
->pts_corr_bytes
+=len
;
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
;
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
;
164 if(ds
==demux
->video
){
166 if(priv
->skip_video_frames
>0){
167 // drop frame (seeking)
168 --priv
->skip_video_frames
;
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
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
);
190 skip
= FFMAX(skip
, 0);
191 if (avi_stream_id(id
) > 99 && id
!= mmioFOURCC('J','U','N','K'))
192 skip
= FFMIN(skip
, 65536);
194 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
,"DEMUX_AVI: Skipping %d bytes from packet %04X\n",skip
,id
);
195 stream_skip(demux
->stream
,skip
);
200 static uint32_t avi_find_id(stream_t
*stream
) {
201 uint32_t id
= stream_read_dword_le(stream
);
203 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "Incomplete stream? Trying resync.\n");
205 id
= stream_read_dword_le(stream
);
206 if (stream_eof(stream
)) return 0;
207 } while (avi_stream_id(id
) > 99);
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
;
224 AVIINDEXENTRY
*idx
=NULL
;
225 if(priv
->idx_size
>0 && priv
->idx_pos
<priv
->idx_size
){
228 idx
=&((AVIINDEXENTRY
*)priv
->idx
)[priv
->idx_pos
++];
230 if(idx
->dwFlags
&AVIIF_LIST
){
231 if (!valid_stream_id(idx
->ckid
))
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
);
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!
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
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;
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;
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
283 ds
=demux_avi_select_stream(demux
,id
);
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){
290 demux
->type
=DEMUXER_TYPE_AVI_NI
;
291 demux
->desc
=&demuxer_desc_avi_ni
;
292 --priv
->idx_pos
; // hack
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 :(
304 ret
=demux_avi_read_packet(demux
,ds
,id
,len
,priv
->idx_pos
-1,flags
);
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
;
322 AVIINDEXENTRY
*idx
=NULL
;
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
){
332 idx
=&((AVIINDEXENTRY
*)priv
->idx
)[idx_pos
];
334 if(idx
->dwFlags
&AVIIF_LIST
){
335 if (!valid_stream_id(idx
->ckid
))
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
);
352 stream_seek(demux
->stream
,pos
);
354 id
=stream_read_dword_le(demux
->stream
);
356 if(stream_eof(demux
->stream
)) return 0;
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
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;
373 ret
=demux_avi_read_packet(demux
,demux_avi_select_stream(demux
,id
),id
,len
,idx_pos
,flags
);
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
;
390 if(ds
==demux
->video
) fpos
=&priv
->idx_pos_v
; else
391 if(ds
==demux
->audio
) fpos
=&priv
->idx_pos_a
; else
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
)){
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
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"
420 if(ds
==demux_avi_select_stream(demux
,id
)){
422 ret
=demux_avi_read_packet(demux
,ds
,id
,len
,priv
->idx_pos
-1,0);
425 int skip
=(len
+1)&(~1); // total bytes in this chunk
426 stream_skip(demux
->stream
,skip
);
430 fpos
[0]=stream_tell(demux
->stream
);
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
;
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:
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;
470 if(AVI_IDX_OFFSET(&((AVIINDEXENTRY
*)priv
->idx
)[0])<demuxer
->movi_start
)
471 priv
->idx_offset
=demuxer
->movi_start
-4;
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:
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
){
492 if(v_pos
==-1 && ds
==demuxer
->video
){
498 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "AVI_NI: %s",
499 mp_gtext("No video stream found.\n"));
503 d_audio
->sh
=sh_audio
=NULL
;
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!
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!!!!
519 priv
->idx_pos_v
=demuxer
->movi_start
;
520 pts_from_bps
=1; // force BPS sync!
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"));
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_msg(MSGT_DEMUX
, MSGL_INFO
, "AVI: %s",
535 mp_gtext("No audio stream found -> no sound.\n"));
536 d_audio
->sh
=sh_audio
=NULL
;
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
;
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
) {
558 else if(sh_audio
&& sh_audio
->ds
->id
== id
) {
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
;
570 // guessing, results may be inaccurate:
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;
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
;
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
);
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
;
616 if(flags
&SEEK_ABSOLUTE
){
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){
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;
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;
649 priv
->idx_pos_a
=priv
->idx_pos_v
=priv
->idx_pos
=video_chunk_pos
;
651 // re-calc video pts:
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
;
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 ------------
667 priv
->audio_block_no
=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
)){
693 priv
->audio_block_no
+=
694 (len
+priv
->audio_block_size
-1)/priv
->audio_block_size
;
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
);
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
;
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
;
716 skip_audio_bytes
+=len
;
719 priv
->audio_block_no
+=
720 (len
+priv
->audio_block_size
-1)/priv
->audio_block_size
;
724 chunks
-=(len
+priv
->audio_block_size
-1)/priv
->audio_block_size
;
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
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
;
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
;
781 if(priv
->idx_size
> 0)
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
;
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
);
817 if (*(int *)arg
>= 0)
818 ds
->id
= *(int *)arg
;
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;
829 if (!streams
[ds
->id
]) // stream not available
832 demux_avi_select_stream(demuxer
, chunkid
);
833 *(int *)arg
= ds
->id
;
834 return DEMUXER_CTRL_OK
;
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 "
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
;
865 static demuxer_t
* demux_open_hack_avi(demuxer_t
*demuxer
)
867 struct MPOpts
*opts
= demuxer
->opts
;
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) {
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");
886 demuxer
->audio
->id
= -2;
888 demuxer
= new_demuxers_demuxer(demuxer
,od
,demuxer
);
897 const demuxer_desc_t demuxer_desc_avi
= {
902 "AVI files, including non interleaved files",
904 1, // safe autodetect
906 demux_avi_fill_buffer
,
913 const demuxer_desc_t demuxer_desc_avi_ni
= {
914 "AVI demuxer, non-interleaved",
918 "AVI files, including non interleaved files",
920 1, // safe autodetect
922 demux_avi_fill_buffer_ni
,
929 const demuxer_desc_t demuxer_desc_avi_nini
= {
930 "AVI demuxer, non-interleaved and no index",
934 "AVI files, including non interleaved files",
936 1, // safe autodetect
938 demux_avi_fill_buffer_nini
,