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 valid_stream_id(unsigned int id
) {
112 unsigned char* fcc
=(unsigned char*)(&id
);
113 return fcc
[0] >= '0' && fcc
[0] <= '9' && fcc
[1] >= '0' && fcc
[1] <= '9' &&
114 ((fcc
[2] == 'w' && fcc
[3] == 'b') || (fcc
[2] == 'd' && fcc
[3] == 'c'));
117 static int choose_chunk_len(unsigned int len1
,unsigned int len2
){
118 // len1 has a bit more priority than len2. len1!=len2
119 // Note: this is a first-idea-logic, may be wrong. comments welcomed.
121 // prefer small frames rather than 0
122 if(!len1
) return (len2
>0x80000) ? len1
: len2
;
123 if(!len2
) return (len1
>0x100000) ? len2
: len1
;
125 // choose the smaller value:
126 return (len1
<len2
)? len1
: len2
;
129 static int demux_avi_read_packet(demuxer_t
*demux
,demux_stream_t
*ds
,unsigned int id
,unsigned int len
,int idxpos
,int flags
){
130 avi_priv_t
*priv
=demux
->priv
;
134 mp_dbg(MSGT_DEMUX
,MSGL_DBG3
,"demux_avi.read_packet: %X\n",id
);
136 if(ds
==demux
->audio
){
137 if(priv
->pts_corrected
==0){
138 if(priv
->pts_has_video
){
139 // we have video pts now
141 if(((sh_audio_t
*)(ds
->sh
))->wf
->nAvgBytesPerSec
)
142 delay
=(float)priv
->pts_corr_bytes
/((sh_audio_t
*)(ds
->sh
))->wf
->nAvgBytesPerSec
;
143 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
);
144 //priv->pts_correction=-priv->avi_audio_pts+delay;
145 priv
->pts_correction
=delay
-priv
->avi_audio_pts
;
146 priv
->avi_audio_pts
+=priv
->pts_correction
;
147 priv
->pts_corrected
=1;
149 priv
->pts_corr_bytes
+=len
;
152 pts
= priv
->audio_block_no
*
153 (float)((sh_audio_t
*)demux
->audio
->sh
)->audio
.dwScale
/
154 (float)((sh_audio_t
*)demux
->audio
->sh
)->audio
.dwRate
;
156 pts
=priv
->avi_audio_pts
; //+priv->pts_correction;
157 priv
->avi_audio_pts
=0;
158 // update blockcount:
159 priv
->audio_block_no
+=priv
->audio_block_size
?
160 ((len
+priv
->audio_block_size
-1)/priv
->audio_block_size
) : 1;
162 if(ds
==demux
->video
){
164 if(priv
->skip_video_frames
>0){
165 // drop frame (seeking)
166 --priv
->skip_video_frames
;
170 pts
= priv
->avi_video_pts
= priv
->video_pack_no
*
171 (float)((sh_video_t
*)demux
->video
->sh
)->video
.dwScale
/
172 (float)((sh_video_t
*)demux
->video
->sh
)->video
.dwRate
;
174 priv
->avi_audio_pts
=priv
->avi_video_pts
+priv
->pts_correction
;
175 priv
->pts_has_video
=1;
177 if(ds
) ++priv
->video_pack_no
;
181 skip
=(len
+1)&(~1); // total bytes in this chunk
184 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
,"DEMUX_AVI: Read %d data bytes from packet %04X\n",len
,id
);
185 ds_read_packet(ds
,demux
->stream
,len
,pts
,idxpos
,flags
);
188 skip
= FFMAX(skip
, 0);
189 if (avi_stream_id(id
) > 99 && id
!= mmioFOURCC('J','U','N','K'))
190 skip
= FFMIN(skip
, 65536);
192 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
,"DEMUX_AVI: Skipping %d bytes from packet %04X\n",skip
,id
);
193 stream_skip(demux
->stream
,skip
);
198 static uint32_t avi_find_id(stream_t
*stream
) {
199 uint32_t id
= stream_read_dword_le(stream
);
201 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "Incomplete stream? Trying resync.\n");
203 id
= stream_read_dword_le(stream
);
204 if (stream_eof(stream
)) return 0;
205 } while (avi_stream_id(id
) > 99);
211 // 0 = EOF or no stream found
212 // 1 = successfully read a packet
213 static int demux_avi_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
){
214 avi_priv_t
*priv
=demux
->priv
;
222 AVIINDEXENTRY
*idx
=NULL
;
223 if(priv
->idx_size
>0 && priv
->idx_pos
<priv
->idx_size
){
226 idx
=&((AVIINDEXENTRY
*)priv
->idx
)[priv
->idx_pos
++];
228 if(idx
->dwFlags
&AVIIF_LIST
){
229 if (!valid_stream_id(idx
->ckid
))
232 if (!priv
->warned_unaligned
)
233 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "Looks like unaligned chunk in index, broken AVI file!\n");
234 priv
->warned_unaligned
= 1;
236 if(!demux_avi_select_stream(demux
,idx
->ckid
)){
237 mp_dbg(MSGT_DEMUX
,MSGL_DBG3
,"Skip chunk %.4s (0x%X) \n",(char *)&idx
->ckid
,(unsigned int)idx
->ckid
);
238 continue; // skip this chunk
241 pos
= (off_t
)priv
->idx_offset
+AVI_IDX_OFFSET(idx
);
242 if((pos
<demux
->movi_start
|| pos
>=demux
->movi_end
) && (demux
->movi_end
>demux
->movi_start
) && (demux
->stream
->flags
& MP_STREAM_SEEK
)){
243 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkOffset out of range! idx=0x%"PRIX64
" \n",(int64_t)pos
);
246 stream_seek(demux
->stream
,pos
);
247 demux
->filepos
=stream_tell(demux
->stream
);
248 id
=stream_read_dword_le(demux
->stream
);
249 if(stream_eof(demux
->stream
)) return 0; // EOF!
252 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id
,(char *)&idx
->ckid
);
253 if(valid_fourcc(idx
->ckid
))
254 id
=idx
->ckid
; // use index if valid
256 if(!valid_fourcc(id
)) continue; // drop chunk if both id and idx bad
258 len
=stream_read_dword_le(demux
->stream
);
259 if((len
!=idx
->dwChunkLength
)&&((len
+1)!=idx
->dwChunkLength
)){
260 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkSize mismatch! raw=%d idx=%d \n",len
,idx
->dwChunkLength
);
261 if(len
>0x200000 && idx
->dwChunkLength
>0x200000) continue; // both values bad :(
262 len
=choose_chunk_len(idx
->dwChunkLength
,len
);
264 if(!(idx
->dwFlags
&AVIIF_KEYFRAME
)) flags
=0;
266 demux
->filepos
=stream_tell(demux
->stream
);
267 if(demux
->filepos
>=demux
->movi_end
&& demux
->movi_end
>demux
->movi_start
&& (demux
->stream
->flags
& MP_STREAM_SEEK
)){
268 demux
->stream
->eof
=1;
271 id
=avi_find_id(demux
->stream
);
272 len
=stream_read_dword_le(demux
->stream
);
273 if(stream_eof(demux
->stream
)) return 0; // EOF!
275 if(id
==mmioFOURCC('L','I','S','T') || id
==mmioFOURCC('R', 'I', 'F', 'F')){
276 id
=stream_read_dword_le(demux
->stream
); // list or RIFF type
281 ds
=demux_avi_select_stream(demux
,id
);
283 if(ds
->packs
+1>=MAX_PACKS
|| ds
->bytes
+len
>=MAX_PACK_BYTES
){
284 // this packet will cause a buffer overflow, switch to -ni mode!!!
285 mp_tmsg(MSGT_DEMUX
,MSGL_WARN
,"\nBadly interleaved AVI file detected - switching to -ni mode...\n");
286 if(priv
->idx_size
>0){
288 demux
->type
=DEMUXER_TYPE_AVI_NI
;
289 demux
->desc
=&demuxer_desc_avi_ni
;
290 --priv
->idx_pos
; // hack
293 demux
->type
=DEMUXER_TYPE_AVI_NINI
;
294 demux
->desc
=&demuxer_desc_avi_nini
;
295 priv
->idx_pos
=demux
->filepos
; // hack
297 priv
->idx_pos_v
=priv
->idx_pos_a
=priv
->idx_pos
;
298 // quit now, we can't even (no enough buffer memory) read this packet :(
302 ret
=demux_avi_read_packet(demux
,ds
,id
,len
,priv
->idx_pos
-1,flags
);
309 // 0 = EOF or no stream found
310 // 1 = successfully read a packet
311 static int demux_avi_fill_buffer_ni(demuxer_t
*demux
,demux_stream_t
* ds
){
312 avi_priv_t
*priv
=demux
->priv
;
319 AVIINDEXENTRY
*idx
=NULL
;
321 demux
->filepos
=stream_tell(demux
->stream
);
323 if(ds
==demux
->video
) idx_pos
=priv
->idx_pos_v
++; else
324 if(ds
==demux
->audio
) idx_pos
=priv
->idx_pos_a
++; else
325 idx_pos
=priv
->idx_pos
++;
327 if(priv
->idx_size
>0 && idx_pos
<priv
->idx_size
){
329 idx
=&((AVIINDEXENTRY
*)priv
->idx
)[idx_pos
];
331 if(idx
->dwFlags
&AVIIF_LIST
){
332 if (!valid_stream_id(idx
->ckid
))
335 if (!priv
->warned_unaligned
)
336 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "Looks like unaligned chunk in index, broken AVI file!\n");
337 priv
->warned_unaligned
= 1;
339 if(ds
&& demux_avi_select_stream(demux
,idx
->ckid
)!=ds
){
340 mp_dbg(MSGT_DEMUX
,MSGL_DBG3
,"Skip chunk %.4s (0x%X) \n",(char *)&idx
->ckid
,(unsigned int)idx
->ckid
);
341 continue; // skip this chunk
344 pos
= priv
->idx_offset
+AVI_IDX_OFFSET(idx
);
345 if((pos
<demux
->movi_start
|| pos
>=demux
->movi_end
) && (demux
->movi_end
>demux
->movi_start
)){
346 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkOffset out of range! current=0x%"PRIX64
" idx=0x%"PRIX64
" \n",(int64_t)demux
->filepos
,(int64_t)pos
);
349 stream_seek(demux
->stream
,pos
);
351 id
=stream_read_dword_le(demux
->stream
);
353 if(stream_eof(demux
->stream
)) return 0;
356 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id
,(char *)&idx
->ckid
);
357 if(valid_fourcc(idx
->ckid
))
358 id
=idx
->ckid
; // use index if valid
360 if(!valid_fourcc(id
)) continue; // drop chunk if both id and idx bad
362 len
=stream_read_dword_le(demux
->stream
);
363 if((len
!=idx
->dwChunkLength
)&&((len
+1)!=idx
->dwChunkLength
)){
364 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkSize mismatch! raw=%d idx=%d \n",len
,idx
->dwChunkLength
);
365 if(len
>0x200000 && idx
->dwChunkLength
>0x200000) continue; // both values bad :(
366 len
=choose_chunk_len(idx
->dwChunkLength
,len
);
368 if(!(idx
->dwFlags
&AVIIF_KEYFRAME
)) flags
=0;
370 ret
=demux_avi_read_packet(demux
,demux_avi_select_stream(demux
,id
),id
,len
,idx_pos
,flags
);
377 // 0 = EOF or no stream found
378 // 1 = successfully read a packet
379 static int demux_avi_fill_buffer_nini(demuxer_t
*demux
,demux_stream_t
* ds
){
380 avi_priv_t
*priv
=demux
->priv
;
386 if(ds
==demux
->video
) fpos
=&priv
->idx_pos_v
; else
387 if(ds
==demux
->audio
) fpos
=&priv
->idx_pos_a
; else
390 stream_seek(demux
->stream
,fpos
[0]);
394 demux
->filepos
=stream_tell(demux
->stream
);
395 if(demux
->filepos
>=demux
->movi_end
&& (demux
->movi_end
>demux
->movi_start
)){
400 id
=avi_find_id(demux
->stream
);
401 len
=stream_read_dword_le(demux
->stream
);
403 if(stream_eof(demux
->stream
)) return 0;
405 if(id
==mmioFOURCC('L','I','S','T')){
406 id
=stream_read_dword_le(demux
->stream
); // list type
410 if(id
==mmioFOURCC('R','I','F','F')){
411 mp_msg(MSGT_DEMUX
,MSGL_V
,"additional RIFF header...\n");
412 id
=stream_read_dword_le(demux
->stream
); // "AVIX"
416 if(ds
==demux_avi_select_stream(demux
,id
)){
418 ret
=demux_avi_read_packet(demux
,ds
,id
,len
,priv
->idx_pos
-1,0);
421 int skip
=(len
+1)&(~1); // total bytes in this chunk
422 stream_skip(demux
->stream
,skip
);
426 fpos
[0]=stream_tell(demux
->stream
);
430 // AVI demuxer parameters:
431 int index_mode
=-1; // -1=untouched 0=don't use index 1=use (generate) index
432 char *index_file_save
= NULL
, *index_file_load
= NULL
;
433 int force_ni
=0; // force non-interleaved AVI parsing
435 void read_avi_header(demuxer_t
*demuxer
,int index_mode
);
437 static demuxer_t
* demux_open_avi(demuxer_t
* demuxer
){
438 demux_stream_t
*d_audio
=demuxer
->audio
;
439 demux_stream_t
*d_video
=demuxer
->video
;
440 sh_audio_t
*sh_audio
=NULL
;
441 sh_video_t
*sh_video
=NULL
;
442 avi_priv_t
* priv
=calloc(1, sizeof(avi_priv_t
));
444 demuxer
->priv
=(void*)priv
;
447 read_avi_header(demuxer
,(demuxer
->stream
->flags
& MP_STREAM_SEEK_BW
)?index_mode
:-2);
449 if(demuxer
->audio
->id
>=0 && !demuxer
->a_streams
[demuxer
->audio
->id
]){
450 mp_tmsg(MSGT_DEMUX
,MSGL_WARN
,"AVI: invalid audio stream ID: %d - ignoring (nosound)\n",demuxer
->audio
->id
);
451 demuxer
->audio
->id
=-2; // disabled
453 if(demuxer
->video
->id
>=0 && !demuxer
->v_streams
[demuxer
->video
->id
]){
454 mp_tmsg(MSGT_DEMUX
,MSGL_WARN
,"AVI: invalid video stream ID: %d - ignoring (using default)\n",demuxer
->video
->id
);
455 demuxer
->video
->id
=-1; // autodetect
458 stream_reset(demuxer
->stream
);
459 stream_seek(demuxer
->stream
,demuxer
->movi_start
);
460 if(priv
->idx_size
>1){
461 // decide index format:
463 if((AVI_IDX_OFFSET(&((AVIINDEXENTRY
*)priv
->idx
)[0])<demuxer
->movi_start
||
464 AVI_IDX_OFFSET(&((AVIINDEXENTRY
*)priv
->idx
)[1])<demuxer
->movi_start
)&& !priv
->isodml
)
465 priv
->idx_offset
=demuxer
->movi_start
-4;
467 if(AVI_IDX_OFFSET(&((AVIINDEXENTRY
*)priv
->idx
)[0])<demuxer
->movi_start
)
468 priv
->idx_offset
=demuxer
->movi_start
-4;
470 mp_msg(MSGT_DEMUX
,MSGL_V
,"AVI index offset: 0x%X (movi=0x%X idx0=0x%X idx1=0x%X)\n",
471 (int)priv
->idx_offset
,(int)demuxer
->movi_start
,
472 (int)((AVIINDEXENTRY
*)priv
->idx
)[0].dwChunkOffset
,
473 (int)((AVIINDEXENTRY
*)priv
->idx
)[1].dwChunkOffset
);
476 if(priv
->idx_size
>0){
477 // check that file is non-interleaved:
481 for(i
=0;i
<priv
->idx_size
;i
++){
482 AVIINDEXENTRY
* idx
=&((AVIINDEXENTRY
*)priv
->idx
)[i
];
483 demux_stream_t
* ds
=demux_avi_select_stream(demuxer
,idx
->ckid
);
484 off_t pos
= priv
->idx_offset
+ AVI_IDX_OFFSET(idx
);
485 if(a_pos
==-1 && ds
==demuxer
->audio
){
489 if(v_pos
==-1 && ds
==demuxer
->video
){
495 mp_tmsg(MSGT_DEMUX
,MSGL_ERR
,"AVI_NI: " "No video stream found.\n");
499 d_audio
->sh
=sh_audio
=NULL
;
501 if(force_ni
|| abs(a_pos
-v_pos
)>0x100000){ // distance > 1MB
502 mp_tmsg(MSGT_DEMUX
,MSGL_INFO
,"%s NON-INTERLEAVED AVI file format.\n",force_ni
?"Forced":"Detected");
503 demuxer
->type
=DEMUXER_TYPE_AVI_NI
; // HACK!!!!
504 demuxer
->desc
=&demuxer_desc_avi_ni
; // HACK!!!!
505 pts_from_bps
=1; // force BPS sync!
511 mp_tmsg(MSGT_DEMUX
,MSGL_INFO
,"Using NON-INTERLEAVED broken AVI file format.\n");
512 demuxer
->type
=DEMUXER_TYPE_AVI_NINI
; // HACK!!!!
513 demuxer
->desc
=&demuxer_desc_avi_nini
; // HACK!!!!
515 priv
->idx_pos_v
=demuxer
->movi_start
;
516 pts_from_bps
=1; // force BPS sync!
520 if(!ds_fill_buffer(d_video
)){
521 mp_tmsg(MSGT_DEMUX
,MSGL_ERR
,"AVI: " "Missing video stream!? Contact the author, it may be a bug :(\n");
524 sh_video
=d_video
->sh
;sh_video
->ds
=d_video
;
526 mp_msg(MSGT_DEMUX
,MSGL_V
,"AVI: Searching for audio stream (id:%d)\n",d_audio
->id
);
527 if(!priv
->audio_streams
|| !ds_fill_buffer(d_audio
)){
528 mp_tmsg(MSGT_DEMUX
,MSGL_INFO
,"AVI: " "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!
543 for(i
=0;i
<priv
->idx_size
;i
++){
544 int id
=avi_stream_id(((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
);
545 int len
=((AVIINDEXENTRY
*)priv
->idx
)[i
].dwChunkLength
;
546 if(sh_video
->ds
->id
== id
) {
550 else if(sh_audio
&& sh_audio
->ds
->id
== id
) {
552 asamples
+=(len
+priv
->audio_block_size
-1)/priv
->audio_block_size
;
555 mp_msg(MSGT_DEMUX
,MSGL_V
,"AVI video size=%"PRId64
" (%u) audio size=%"PRId64
" (%u)\n",vsize
,vsamples
,asize
,asamples
);
556 priv
->numberofframes
=vsamples
;
557 sh_video
->i_bps
=((float)vsize
/(float)vsamples
)*(float)sh_video
->video
.dwRate
/(float)sh_video
->video
.dwScale
;
558 if(sh_audio
) sh_audio
->i_bps
=((float)asize
/(float)asamples
)*(float)sh_audio
->audio
.dwRate
/(float)sh_audio
->audio
.dwScale
;
560 // guessing, results may be inaccurate:
564 if((priv
->numberofframes
=sh_video
->video
.dwLength
)<=1)
565 // bad video header, try to get number of frames from audio
566 if(sh_audio
&& sh_audio
->wf
->nAvgBytesPerSec
) priv
->numberofframes
=sh_video
->fps
*sh_audio
->audio
.dwLength
/sh_audio
->audio
.dwRate
*sh_audio
->audio
.dwScale
;
567 if(priv
->numberofframes
<=1){
568 mp_tmsg(MSGT_SEEK
,MSGL_WARN
,"Could not determine number of frames (for absolute seek).\n");
569 priv
->numberofframes
=0;
573 if(sh_audio
->wf
->nAvgBytesPerSec
&& sh_audio
->audio
.dwSampleSize
!=1){
574 asize
=(float)sh_audio
->wf
->nAvgBytesPerSec
*sh_audio
->audio
.dwLength
*sh_audio
->audio
.dwScale
/sh_audio
->audio
.dwRate
;
576 asize
=sh_audio
->audio
.dwLength
;
577 sh_audio
->i_bps
=(float)asize
/(sh_video
->frametime
*priv
->numberofframes
);
580 vsize
=demuxer
->movi_end
-demuxer
->movi_start
-asize
-8*priv
->numberofframes
;
581 mp_msg(MSGT_DEMUX
,MSGL_V
,"AVI video size=%"PRId64
" (%u) audio size=%"PRId64
"\n",vsize
,priv
->numberofframes
,asize
);
582 sh_video
->i_bps
=(float)vsize
/(sh_video
->frametime
*priv
->numberofframes
);
590 static void demux_seek_avi(demuxer_t
*demuxer
,float rel_seek_secs
,float audio_delay
,int flags
){
591 avi_priv_t
*priv
=demuxer
->priv
;
592 demux_stream_t
*d_audio
=demuxer
->audio
;
593 demux_stream_t
*d_video
=demuxer
->video
;
594 sh_audio_t
*sh_audio
=d_audio
->sh
;
595 sh_video_t
*sh_video
=d_video
->sh
;
596 float skip_audio_secs
=0;
598 //FIXME: OFF_T - Didn't check AVI case yet (avi files can't be >2G anyway?)
599 //================= seek in AVI ==========================
600 int rel_seek_frames
=rel_seek_secs
*sh_video
->fps
;
601 int video_chunk_pos
=d_video
->pos
;
604 if(flags
&SEEK_ABSOLUTE
){
609 if(flags
&SEEK_FACTOR
){
610 rel_seek_frames
=rel_seek_secs
*priv
->numberofframes
;
613 priv
->skip_video_frames
=0;
614 priv
->avi_audio_pts
=0;
616 // ------------ STEP 1: find nearest video keyframe chunk ------------
617 // find nearest video keyframe chunk pos:
618 if(rel_seek_frames
>0){
620 while(video_chunk_pos
<priv
->idx_size
-1){
621 int id
=((AVIINDEXENTRY
*)priv
->idx
)[video_chunk_pos
].ckid
;
622 if(avi_stream_id(id
)==d_video
->id
){ // video frame
623 if((--rel_seek_frames
)<0 && ((AVIINDEXENTRY
*)priv
->idx
)[video_chunk_pos
].dwFlags
&AVIIF_KEYFRAME
) break;
629 while(video_chunk_pos
>0){
630 int id
=((AVIINDEXENTRY
*)priv
->idx
)[video_chunk_pos
].ckid
;
631 if(avi_stream_id(id
)==d_video
->id
){ // video frame
632 if((++rel_seek_frames
)>0 && ((AVIINDEXENTRY
*)priv
->idx
)[video_chunk_pos
].dwFlags
&AVIIF_KEYFRAME
) break;
637 priv
->idx_pos_a
=priv
->idx_pos_v
=priv
->idx_pos
=video_chunk_pos
;
639 // re-calc video pts:
641 for(i
=0;i
<video_chunk_pos
;i
++){
642 int id
=((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
;
643 if(avi_stream_id(id
)==d_video
->id
) ++d_video
->pack_no
;
646 sh_video
->num_frames
=sh_video
->num_frames_decoded
=d_video
->pack_no
;
647 priv
->avi_video_pts
=d_video
->pack_no
*(float)sh_video
->video
.dwScale
/(float)sh_video
->video
.dwRate
;
648 d_video
->pos
=video_chunk_pos
;
650 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
);
652 // ------------ STEP 2: seek audio, find the right chunk & pos ------------
655 priv
->audio_block_no
=0;
661 int skip_audio_bytes
=0;
662 int curr_audio_pos
=-1;
663 int audio_chunk_pos
=-1;
664 int chunk_max
=(demuxer
->type
==DEMUXER_TYPE_AVI
)?video_chunk_pos
:priv
->idx_size
;
666 if(sh_audio
->audio
.dwSampleSize
){
667 // constant rate audio stream
668 /* immediate seeking to audio position, including when streams are delayed */
669 curr_audio_pos
=(priv
->avi_video_pts
+ audio_delay
)*(float)sh_audio
->audio
.dwRate
/(float)sh_audio
->audio
.dwScale
;
670 curr_audio_pos
*=sh_audio
->audio
.dwSampleSize
;
672 // find audio chunk pos:
673 for(i
=0;i
<chunk_max
;i
++){
674 int id
=((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
;
675 if(avi_stream_id(id
)==d_audio
->id
){
676 len
=((AVIINDEXENTRY
*)priv
->idx
)[i
].dwChunkLength
;
677 if(d_audio
->dpos
<=curr_audio_pos
&& curr_audio_pos
<(d_audio
->dpos
+len
)){
681 priv
->audio_block_no
+=priv
->audio_block_size
?
682 ((len
+priv
->audio_block_size
-1)/priv
->audio_block_size
) : 1;
687 skip_audio_bytes
=curr_audio_pos
-d_audio
->dpos
;
689 mp_msg(MSGT_SEEK
,MSGL_V
,"SEEK: i=%d (max:%d) dpos=%d (wanted:%d) \n",
690 i
,chunk_max
,(int)d_audio
->dpos
,curr_audio_pos
);
694 /* immediate seeking to audio position, including when streams are delayed */
695 int chunks
=(priv
->avi_video_pts
+ audio_delay
)*(float)sh_audio
->audio
.dwRate
/(float)sh_audio
->audio
.dwScale
;
698 // find audio chunk pos:
699 for(i
=0;i
<priv
->idx_size
&& chunks
>0;i
++){
700 int id
=((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
;
701 if(avi_stream_id(id
)==d_audio
->id
){
702 len
=((AVIINDEXENTRY
*)priv
->idx
)[i
].dwChunkLength
;
704 skip_audio_bytes
+=len
;
707 priv
->audio_block_no
+=priv
->audio_block_size
?
708 ((len
+priv
->audio_block_size
-1)/priv
->audio_block_size
) : 1;
712 if(priv
->audio_block_size
)
713 chunks
-=(len
+priv
->audio_block_size
-1)/priv
->audio_block_size
;
719 // audio_chunk_pos = chunk no in index table (it's <=chunk_max)
720 // skip_audio_bytes = bytes to be skipped after chunk seek
721 // d-audio->pack_no = chunk_no in stream at audio_chunk_pos
722 // d_audio->dpos = bytepos in stream at audio_chunk_pos
725 // update stream position:
726 d_audio
->pos
=audio_chunk_pos
;
728 if(demuxer
->type
==DEMUXER_TYPE_AVI
){
729 // interleaved stream:
730 if(audio_chunk_pos
<video_chunk_pos
){
731 // calc priv->skip_video_frames & adjust video pts counter:
732 for(i
=audio_chunk_pos
;i
<video_chunk_pos
;i
++){
733 int id
=((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
;
734 if(avi_stream_id(id
)==d_video
->id
) ++priv
->skip_video_frames
;
736 // requires for correct audio pts calculation (demuxer):
737 priv
->avi_video_pts
-=priv
->skip_video_frames
*(float)sh_video
->video
.dwScale
/(float)sh_video
->video
.dwRate
;
738 priv
->avi_audio_pts
=priv
->avi_video_pts
;
739 // set index position:
740 priv
->idx_pos_a
=priv
->idx_pos_v
=priv
->idx_pos
=audio_chunk_pos
;
743 // non-interleaved stream:
744 priv
->idx_pos_a
=audio_chunk_pos
;
745 priv
->idx_pos_v
=video_chunk_pos
;
746 priv
->idx_pos
=(audio_chunk_pos
<video_chunk_pos
)?audio_chunk_pos
:video_chunk_pos
;
749 mp_msg(MSGT_SEEK
,MSGL_V
,"SEEK: idx=%d (a:%d v:%d) v.skip=%d a.skip=%d/%4.3f \n",
750 (int)priv
->idx_pos
,audio_chunk_pos
,video_chunk_pos
,
751 (int)priv
->skip_video_frames
,skip_audio_bytes
,skip_audio_secs
);
753 if(skip_audio_bytes
){
754 demux_read_data(d_audio
,NULL
,skip_audio_bytes
);
758 d_video
->pts
=priv
->avi_video_pts
; // OSD
763 static void demux_close_avi(demuxer_t
*demuxer
) {
764 avi_priv_t
* priv
=demuxer
->priv
;
769 if(priv
->idx_size
> 0)
775 static int demux_avi_control(demuxer_t
*demuxer
,int cmd
, void *arg
){
776 avi_priv_t
*priv
=demuxer
->priv
;
777 demux_stream_t
*d_video
=demuxer
->video
;
778 sh_video_t
*sh_video
=d_video
->sh
;
781 case DEMUXER_CTRL_GET_TIME_LENGTH
:
782 if (!priv
->numberofframes
|| !sh_video
) return DEMUXER_CTRL_DONTKNOW
;
783 *((double *)arg
)=(double)priv
->numberofframes
/sh_video
->fps
;
784 if (sh_video
->video
.dwLength
<=1) return DEMUXER_CTRL_GUESS
;
785 return DEMUXER_CTRL_OK
;
787 case DEMUXER_CTRL_GET_PERCENT_POS
:
788 if (!priv
->numberofframes
|| !sh_video
) {
789 return DEMUXER_CTRL_DONTKNOW
;
791 *((int *)arg
)=(int)(priv
->video_pack_no
*100/priv
->numberofframes
);
792 if (sh_video
->video
.dwLength
<=1) return DEMUXER_CTRL_GUESS
;
793 return DEMUXER_CTRL_OK
;
795 case DEMUXER_CTRL_SWITCH_AUDIO
:
796 case DEMUXER_CTRL_SWITCH_VIDEO
: {
797 int audio
= (cmd
== DEMUXER_CTRL_SWITCH_AUDIO
);
798 demux_stream_t
*ds
= audio
? demuxer
->audio
: demuxer
->video
;
799 void **streams
= audio
? demuxer
->a_streams
: demuxer
->v_streams
;
800 int maxid
= FFMIN(100, audio
? MAX_A_STREAMS
: MAX_V_STREAMS
);
803 return DEMUXER_CTRL_NOTIMPL
;
805 if (*(int *)arg
>= 0)
806 ds
->id
= *(int *)arg
;
809 for (i
= 0; i
< maxid
; i
++) {
810 if (++ds
->id
>= maxid
) ds
->id
= 0;
811 if (streams
[ds
->id
]) break;
815 chunkid
= (ds
->id
/ 10 + '0') | (ds
->id
% 10 + '0') << 8;
817 if (!streams
[ds
->id
]) // stream not available
820 demux_avi_select_stream(demuxer
, chunkid
);
821 *(int *)arg
= ds
->id
;
822 return DEMUXER_CTRL_OK
;
826 return DEMUXER_CTRL_NOTIMPL
;
831 static int avi_check_file(demuxer_t
*demuxer
)
833 int id
=stream_read_dword_le(demuxer
->stream
); // "RIFF"
835 if((id
==mmioFOURCC('R','I','F','F')) || (id
==mmioFOURCC('O','N','2',' '))) {
836 stream_read_dword_le(demuxer
->stream
); //filesize
837 id
=stream_read_dword_le(demuxer
->stream
); // "AVI "
839 return DEMUXER_TYPE_AVI
;
840 // "Samsung Digimax i6 PMP" crap according to bug 742
841 if(id
==mmioFOURCC('A','V','I',0x19))
842 return DEMUXER_TYPE_AVI
;
843 if(id
==mmioFOURCC('O','N','2','f')){
844 mp_tmsg(MSGT_DEMUXER
,MSGL_INFO
,"ON2 AVI format");
845 return DEMUXER_TYPE_AVI
;
853 static demuxer_t
* demux_open_hack_avi(demuxer_t
*demuxer
)
855 struct MPOpts
*opts
= demuxer
->opts
;
858 demuxer
= demux_open_avi(demuxer
);
859 if(!demuxer
) return NULL
; // failed to open
860 sh_a
= demuxer
->audio
->sh
;
861 if(demuxer
->audio
->id
!= -2 && sh_a
) {
862 #ifdef CONFIG_OGGVORBIS
863 // support for Ogg-in-AVI:
864 if(sh_a
->format
== 0xFFFE)
865 demuxer
= init_avi_with_ogg(demuxer
);
866 else if(sh_a
->format
== 0x674F) {
869 s
= new_ds_stream(demuxer
->audio
);
870 od
= new_demuxer(opts
, s
,DEMUXER_TYPE_OGG
,-1,-2,-2,NULL
);
871 if(!demux_ogg_open(od
)) {
872 mp_tmsg( MSGT_DEMUXER
,MSGL_ERR
,"Unable to open the Ogg demuxer.\n");
874 demuxer
->audio
->id
= -2;
876 demuxer
= new_demuxers_demuxer(demuxer
,od
,demuxer
);
885 const demuxer_desc_t demuxer_desc_avi
= {
890 "AVI files, including non interleaved files",
892 1, // safe autodetect
894 demux_avi_fill_buffer
,
901 const demuxer_desc_t demuxer_desc_avi_ni
= {
902 "AVI demuxer, non-interleaved",
906 "AVI files, including non interleaved files",
908 1, // safe autodetect
910 demux_avi_fill_buffer_ni
,
917 const demuxer_desc_t demuxer_desc_avi_nini
= {
918 "AVI demuxer, non-interleaved and no index",
922 "AVI files, including non interleaved files",
924 1, // safe autodetect
926 demux_avi_fill_buffer_nini
,