1 // AVI file parser for DEMUXER v2.9 by A'rpi/ESP-team
11 #include "stream/stream.h"
15 #include "aviheader.h"
17 demuxer_t
* init_avi_with_ogg(demuxer_t
* demuxer
);
18 int demux_ogg_open(demuxer_t
* demuxer
);
20 extern const demuxer_desc_t demuxer_desc_avi_ni
;
21 extern const demuxer_desc_t demuxer_desc_avi_nini
;
23 // PTS: 0=interleaved 1=BPS-based
27 demux_stream_t
* demux_avi_select_stream(demuxer_t
*demux
,unsigned int id
){
28 int stream_id
=avi_stream_id(id
);
31 if(demux
->video
->id
==-1)
32 if(demux
->v_streams
[stream_id
])
33 demux
->video
->id
=stream_id
;
35 if(demux
->audio
->id
==-1)
36 if(demux
->a_streams
[stream_id
])
37 demux
->audio
->id
=stream_id
;
39 if(stream_id
==demux
->audio
->id
){
40 if(!demux
->audio
->sh
){
42 avi_priv_t
*priv
=demux
->priv
;
43 sh
=demux
->audio
->sh
=demux
->a_streams
[stream_id
];
44 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected AVI audio ID = %d\n",demux
->audio
->id
);
46 priv
->audio_block_size
=sh
->wf
->nBlockAlign
;
47 if(!priv
->audio_block_size
){
48 // for PCM audio we can calculate the blocksize:
50 priv
->audio_block_size
=sh
->wf
->nChannels
*(sh
->wf
->wBitsPerSample
/8);
52 priv
->audio_block_size
=1; // hope the best...
54 // workaround old mencoder's bug:
55 if(sh
->audio
.dwSampleSize
==1 && sh
->audio
.dwScale
==1 &&
56 (sh
->wf
->nBlockAlign
==1152 || sh
->wf
->nBlockAlign
==576)){
57 mp_msg(MSGT_DEMUX
,MSGL_WARN
,MSGTR_WorkAroundBlockAlignHeaderBug
);
58 priv
->audio_block_size
=1;
62 priv
->audio_block_size
=sh
->audio
.dwSampleSize
;
67 if(stream_id
==demux
->video
->id
){
68 if(!demux
->video
->sh
){
69 demux
->video
->sh
=demux
->v_streams
[stream_id
];
70 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected AVI video ID = %d\n",demux
->video
->id
);
74 if(id
!=mmioFOURCC('J','U','N','K')){
76 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"Unknown chunk: %.4s (%X)\n",(char *) &id
,id
);
82 static int valid_fourcc(unsigned int id
){
83 static const char valid
[] = "0123456789abcdefghijklmnopqrstuvwxyz"
84 "ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
85 unsigned char* fcc
=(unsigned char*)(&id
);
86 return strchr(valid
, fcc
[0]) && strchr(valid
, fcc
[1]) &&
87 strchr(valid
, fcc
[2]) && strchr(valid
, fcc
[3]);
90 static int choose_chunk_len(unsigned int len1
,unsigned int len2
){
91 // len1 has a bit more priority than len2. len1!=len2
92 // Note: this is a first-idea-logic, may be wrong. comments welcomed.
94 // prefer small frames rather than 0
95 if(!len1
) return (len2
>0x80000) ? len1
: len2
;
96 if(!len2
) return (len1
>0x100000) ? len2
: len1
;
98 // choose the smaller value:
99 return (len1
<len2
)? len1
: len2
;
102 static int demux_avi_read_packet(demuxer_t
*demux
,demux_stream_t
*ds
,unsigned int id
,unsigned int len
,int idxpos
,int flags
){
103 avi_priv_t
*priv
=demux
->priv
;
107 mp_dbg(MSGT_DEMUX
,MSGL_DBG3
,"demux_avi.read_packet: %X\n",id
);
109 if(ds
==demux
->audio
){
110 if(priv
->pts_corrected
==0){
111 if(priv
->pts_has_video
){
112 // we have video pts now
114 if(((sh_audio_t
*)(ds
->sh
))->wf
->nAvgBytesPerSec
)
115 delay
=(float)priv
->pts_corr_bytes
/((sh_audio_t
*)(ds
->sh
))->wf
->nAvgBytesPerSec
;
116 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
);
117 //priv->pts_correction=-priv->avi_audio_pts+delay;
118 priv
->pts_correction
=delay
-priv
->avi_audio_pts
;
119 priv
->avi_audio_pts
+=priv
->pts_correction
;
120 priv
->pts_corrected
=1;
122 priv
->pts_corr_bytes
+=len
;
125 pts
= priv
->audio_block_no
*
126 (float)((sh_audio_t
*)demux
->audio
->sh
)->audio
.dwScale
/
127 (float)((sh_audio_t
*)demux
->audio
->sh
)->audio
.dwRate
;
129 pts
=priv
->avi_audio_pts
; //+priv->pts_correction;
130 priv
->avi_audio_pts
=0;
131 // update blockcount:
132 priv
->audio_block_no
+=priv
->audio_block_size
?
133 ((len
+priv
->audio_block_size
-1)/priv
->audio_block_size
) : 1;
135 if(ds
==demux
->video
){
137 if(priv
->skip_video_frames
>0){
138 // drop frame (seeking)
139 --priv
->skip_video_frames
;
143 pts
= priv
->avi_video_pts
= priv
->video_pack_no
*
144 (float)((sh_video_t
*)demux
->video
->sh
)->video
.dwScale
/
145 (float)((sh_video_t
*)demux
->video
->sh
)->video
.dwRate
;
147 priv
->avi_audio_pts
=priv
->avi_video_pts
+priv
->pts_correction
;
148 priv
->pts_has_video
=1;
150 if(ds
) ++priv
->video_pack_no
;
154 skip
=(len
+1)&(~1); // total bytes in this chunk
157 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
,"DEMUX_AVI: Read %d data bytes from packet %04X\n",len
,id
);
158 ds_read_packet(ds
,demux
->stream
,len
,pts
,idxpos
,flags
);
161 skip
= FFMAX(skip
, 0);
162 if (avi_stream_id(id
) > 99 && id
!= mmioFOURCC('J','U','N','K'))
163 skip
= FFMIN(skip
, 65536);
165 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
,"DEMUX_AVI: Skipping %d bytes from packet %04X\n",skip
,id
);
166 stream_skip(demux
->stream
,skip
);
171 static uint32_t avi_find_id(stream_t
*stream
) {
172 uint32_t id
= stream_read_dword_le(stream
);
174 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "Incomplete stream? Trying resync.\n");
176 id
= stream_read_dword_le(stream
);
177 if (stream_eof(stream
)) return 0;
178 } while (avi_stream_id(id
) > 99);
184 // 0 = EOF or no stream found
185 // 1 = successfully read a packet
186 static int demux_avi_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
){
187 avi_priv_t
*priv
=demux
->priv
;
195 AVIINDEXENTRY
*idx
=NULL
;
196 if(priv
->idx_size
>0 && priv
->idx_pos
<priv
->idx_size
){
199 idx
=&((AVIINDEXENTRY
*)priv
->idx
)[priv
->idx_pos
++];
201 if(idx
->dwFlags
&AVIIF_LIST
){
205 if(!demux_avi_select_stream(demux
,idx
->ckid
)){
206 mp_dbg(MSGT_DEMUX
,MSGL_DBG3
,"Skip chunk %.4s (0x%X) \n",(char *)&idx
->ckid
,(unsigned int)idx
->ckid
);
207 continue; // skip this chunk
210 pos
= (off_t
)priv
->idx_offset
+AVI_IDX_OFFSET(idx
);
211 if((pos
<demux
->movi_start
|| pos
>=demux
->movi_end
) && (demux
->movi_end
>demux
->movi_start
) && (demux
->stream
->flags
& STREAM_SEEK
)){
212 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkOffset out of range! idx=0x%"PRIX64
" \n",(int64_t)pos
);
215 stream_seek(demux
->stream
,pos
);
216 demux
->filepos
=stream_tell(demux
->stream
);
217 id
=stream_read_dword_le(demux
->stream
);
218 if(stream_eof(demux
->stream
)) return 0; // EOF!
221 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id
,(char *)&idx
->ckid
);
222 if(valid_fourcc(idx
->ckid
))
223 id
=idx
->ckid
; // use index if valid
225 if(!valid_fourcc(id
)) continue; // drop chunk if both id and idx bad
227 len
=stream_read_dword_le(demux
->stream
);
228 if((len
!=idx
->dwChunkLength
)&&((len
+1)!=idx
->dwChunkLength
)){
229 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkSize mismatch! raw=%d idx=%d \n",len
,idx
->dwChunkLength
);
230 if(len
>0x200000 && idx
->dwChunkLength
>0x200000) continue; // both values bad :(
231 len
=choose_chunk_len(idx
->dwChunkLength
,len
);
233 if(!(idx
->dwFlags
&AVIIF_KEYFRAME
)) flags
=0;
235 demux
->filepos
=stream_tell(demux
->stream
);
236 if(demux
->filepos
>=demux
->movi_end
&& demux
->movi_end
>demux
->movi_start
&& (demux
->stream
->flags
& STREAM_SEEK
)){
237 demux
->stream
->eof
=1;
240 id
=avi_find_id(demux
->stream
);
241 len
=stream_read_dword_le(demux
->stream
);
242 if(stream_eof(demux
->stream
)) return 0; // EOF!
244 if(id
==mmioFOURCC('L','I','S','T') || id
==mmioFOURCC('R', 'I', 'F', 'F')){
245 id
=stream_read_dword_le(demux
->stream
); // list or RIFF type
250 ds
=demux_avi_select_stream(demux
,id
);
252 if(ds
->packs
+1>=MAX_PACKS
|| ds
->bytes
+len
>=MAX_PACK_BYTES
){
253 // this packet will cause a buffer overflow, switch to -ni mode!!!
254 mp_msg(MSGT_DEMUX
,MSGL_WARN
,MSGTR_SwitchToNi
);
255 if(priv
->idx_size
>0){
257 demux
->type
=DEMUXER_TYPE_AVI_NI
;
258 demux
->desc
=&demuxer_desc_avi_ni
;
259 --priv
->idx_pos
; // hack
262 demux
->type
=DEMUXER_TYPE_AVI_NINI
;
263 demux
->desc
=&demuxer_desc_avi_nini
;
264 priv
->idx_pos
=demux
->filepos
; // hack
266 priv
->idx_pos_v
=priv
->idx_pos_a
=priv
->idx_pos
;
267 // quit now, we can't even (no enough buffer memory) read this packet :(
271 ret
=demux_avi_read_packet(demux
,ds
,id
,len
,priv
->idx_pos
-1,flags
);
278 // 0 = EOF or no stream found
279 // 1 = successfully read a packet
280 int demux_avi_fill_buffer_ni(demuxer_t
*demux
,demux_stream_t
* ds
){
281 avi_priv_t
*priv
=demux
->priv
;
288 AVIINDEXENTRY
*idx
=NULL
;
290 demux
->filepos
=stream_tell(demux
->stream
);
292 if(ds
==demux
->video
) idx_pos
=priv
->idx_pos_v
++; else
293 if(ds
==demux
->audio
) idx_pos
=priv
->idx_pos_a
++; else
294 idx_pos
=priv
->idx_pos
++;
296 if(priv
->idx_size
>0 && idx_pos
<priv
->idx_size
){
298 idx
=&((AVIINDEXENTRY
*)priv
->idx
)[idx_pos
];
300 if(idx
->dwFlags
&AVIIF_LIST
){
304 if(ds
&& demux_avi_select_stream(demux
,idx
->ckid
)!=ds
){
305 mp_dbg(MSGT_DEMUX
,MSGL_DBG3
,"Skip chunk %.4s (0x%X) \n",(char *)&idx
->ckid
,(unsigned int)idx
->ckid
);
306 continue; // skip this chunk
309 pos
= priv
->idx_offset
+AVI_IDX_OFFSET(idx
);
310 if((pos
<demux
->movi_start
|| pos
>=demux
->movi_end
) && (demux
->movi_end
>demux
->movi_start
)){
311 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkOffset out of range! current=0x%"PRIX64
" idx=0x%"PRIX64
" \n",(int64_t)demux
->filepos
,(int64_t)pos
);
314 stream_seek(demux
->stream
,pos
);
316 id
=stream_read_dword_le(demux
->stream
);
318 if(stream_eof(demux
->stream
)) return 0;
321 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id
,(char *)&idx
->ckid
);
322 if(valid_fourcc(idx
->ckid
))
323 id
=idx
->ckid
; // use index if valid
325 if(!valid_fourcc(id
)) continue; // drop chunk if both id and idx bad
327 len
=stream_read_dword_le(demux
->stream
);
328 if((len
!=idx
->dwChunkLength
)&&((len
+1)!=idx
->dwChunkLength
)){
329 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkSize mismatch! raw=%d idx=%d \n",len
,idx
->dwChunkLength
);
330 if(len
>0x200000 && idx
->dwChunkLength
>0x200000) continue; // both values bad :(
331 len
=choose_chunk_len(idx
->dwChunkLength
,len
);
333 if(!(idx
->dwFlags
&AVIIF_KEYFRAME
)) flags
=0;
335 ret
=demux_avi_read_packet(demux
,demux_avi_select_stream(demux
,id
),id
,len
,idx_pos
,flags
);
342 // 0 = EOF or no stream found
343 // 1 = successfully read a packet
344 int demux_avi_fill_buffer_nini(demuxer_t
*demux
,demux_stream_t
* ds
){
345 avi_priv_t
*priv
=demux
->priv
;
351 if(ds
==demux
->video
) fpos
=&priv
->idx_pos_v
; else
352 if(ds
==demux
->audio
) fpos
=&priv
->idx_pos_a
; else
355 stream_seek(demux
->stream
,fpos
[0]);
359 demux
->filepos
=stream_tell(demux
->stream
);
360 if(demux
->filepos
>=demux
->movi_end
&& (demux
->movi_end
>demux
->movi_start
)){
365 id
=avi_find_id(demux
->stream
);
366 len
=stream_read_dword_le(demux
->stream
);
368 if(stream_eof(demux
->stream
)) return 0;
370 if(id
==mmioFOURCC('L','I','S','T')){
371 id
=stream_read_dword_le(demux
->stream
); // list type
375 if(id
==mmioFOURCC('R','I','F','F')){
376 mp_msg(MSGT_DEMUX
,MSGL_V
,"additional RIFF header...\n");
377 id
=stream_read_dword_le(demux
->stream
); // "AVIX"
381 if(ds
==demux_avi_select_stream(demux
,id
)){
383 ret
=demux_avi_read_packet(demux
,ds
,id
,len
,priv
->idx_pos
-1,0);
386 int skip
=(len
+1)&(~1); // total bytes in this chunk
387 stream_skip(demux
->stream
,skip
);
391 fpos
[0]=stream_tell(demux
->stream
);
395 // AVI demuxer parameters:
396 int index_mode
=-1; // -1=untouched 0=don't use index 1=use (generate) index
397 char *index_file_save
= NULL
, *index_file_load
= NULL
;
398 int force_ni
=0; // force non-interleaved AVI parsing
400 void read_avi_header(demuxer_t
*demuxer
,int index_mode
);
402 static demuxer_t
* demux_open_avi(demuxer_t
* demuxer
){
403 demux_stream_t
*d_audio
=demuxer
->audio
;
404 demux_stream_t
*d_video
=demuxer
->video
;
405 sh_audio_t
*sh_audio
=NULL
;
406 sh_video_t
*sh_video
=NULL
;
407 avi_priv_t
* priv
=malloc(sizeof(avi_priv_t
));
410 priv
->avi_audio_pts
=priv
->avi_video_pts
=0.0f
;
411 priv
->pts_correction
=0.0f
;
412 priv
->skip_video_frames
=0;
413 priv
->pts_corr_bytes
=0;
414 priv
->pts_has_video
=priv
->pts_corrected
=0;
415 priv
->video_pack_no
=0;
416 priv
->audio_block_no
=0;
417 priv
->audio_block_size
=0;
419 priv
->suidx_size
= 0;
422 demuxer
->priv
=(void*)priv
;
425 read_avi_header(demuxer
,(demuxer
->stream
->flags
& STREAM_SEEK_BW
)?index_mode
:-2);
427 if(demuxer
->audio
->id
>=0 && !demuxer
->a_streams
[demuxer
->audio
->id
]){
428 mp_msg(MSGT_DEMUX
,MSGL_WARN
,MSGTR_InvalidAudioStreamNosound
,demuxer
->audio
->id
);
429 demuxer
->audio
->id
=-2; // disabled
431 if(demuxer
->video
->id
>=0 && !demuxer
->v_streams
[demuxer
->video
->id
]){
432 mp_msg(MSGT_DEMUX
,MSGL_WARN
,MSGTR_InvalidAudioStreamUsingDefault
,demuxer
->video
->id
);
433 demuxer
->video
->id
=-1; // autodetect
436 stream_reset(demuxer
->stream
);
437 stream_seek(demuxer
->stream
,demuxer
->movi_start
);
441 if(priv
->idx_size
>1){
442 // decide index format:
444 if((AVI_IDX_OFFSET(&((AVIINDEXENTRY
*)priv
->idx
)[0])<demuxer
->movi_start
||
445 AVI_IDX_OFFSET(&((AVIINDEXENTRY
*)priv
->idx
)[1])<demuxer
->movi_start
)&& !priv
->isodml
)
446 priv
->idx_offset
=demuxer
->movi_start
-4;
450 if(AVI_IDX_OFFSET(&((AVIINDEXENTRY
*)priv
->idx
)[0])<demuxer
->movi_start
)
451 priv
->idx_offset
=demuxer
->movi_start
-4;
455 mp_msg(MSGT_DEMUX
,MSGL_V
,"AVI index offset: 0x%X (movi=0x%X idx0=0x%X idx1=0x%X)\n",
456 (int)priv
->idx_offset
,(int)demuxer
->movi_start
,
457 (int)((AVIINDEXENTRY
*)priv
->idx
)[0].dwChunkOffset
,
458 (int)((AVIINDEXENTRY
*)priv
->idx
)[1].dwChunkOffset
);
461 if(priv
->idx_size
>0){
462 // check that file is non-interleaved:
466 for(i
=0;i
<priv
->idx_size
;i
++){
467 AVIINDEXENTRY
* idx
=&((AVIINDEXENTRY
*)priv
->idx
)[i
];
468 demux_stream_t
* ds
=demux_avi_select_stream(demuxer
,idx
->ckid
);
469 off_t pos
= priv
->idx_offset
+ AVI_IDX_OFFSET(idx
);
470 if(a_pos
==-1 && ds
==demuxer
->audio
){
474 if(v_pos
==-1 && ds
==demuxer
->video
){
480 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"AVI_NI: " MSGTR_MissingVideoStream
);
484 d_audio
->sh
=sh_audio
=NULL
;
486 if(force_ni
|| abs(a_pos
-v_pos
)>0x100000){ // distance > 1MB
487 mp_msg(MSGT_DEMUX
,MSGL_INFO
,MSGTR_NI_Message
,force_ni
?MSGTR_NI_Forced
:MSGTR_NI_Detected
);
488 demuxer
->type
=DEMUXER_TYPE_AVI_NI
; // HACK!!!!
489 demuxer
->desc
=&demuxer_desc_avi_ni
; // HACK!!!!
490 pts_from_bps
=1; // force BPS sync!
496 mp_msg(MSGT_DEMUX
,MSGL_INFO
,MSGTR_UsingNINI
);
497 demuxer
->type
=DEMUXER_TYPE_AVI_NINI
; // HACK!!!!
498 demuxer
->desc
=&demuxer_desc_avi_nini
; // HACK!!!!
500 priv
->idx_pos_v
=demuxer
->movi_start
;
501 pts_from_bps
=1; // force BPS sync!
505 if(!ds_fill_buffer(d_video
)){
506 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"AVI: " MSGTR_MissingVideoStreamBug
);
509 sh_video
=d_video
->sh
;sh_video
->ds
=d_video
;
511 mp_msg(MSGT_DEMUX
,MSGL_V
,"AVI: Searching for audio stream (id:%d)\n",d_audio
->id
);
512 if(!priv
->audio_streams
|| !ds_fill_buffer(d_audio
)){
513 mp_msg(MSGT_DEMUX
,MSGL_INFO
,"AVI: " MSGTR_MissingAudioStream
);
514 d_audio
->sh
=sh_audio
=NULL
;
516 sh_audio
=d_audio
->sh
;sh_audio
->ds
=d_audio
;
520 // calculating audio/video bitrate:
521 if(priv
->idx_size
>0){
522 // we have index, let's count 'em!
528 for(i
=0;i
<priv
->idx_size
;i
++){
529 int id
=avi_stream_id(((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
);
530 int len
=((AVIINDEXENTRY
*)priv
->idx
)[i
].dwChunkLength
;
531 if(sh_video
->ds
->id
== id
) {
535 else if(sh_audio
&& sh_audio
->ds
->id
== id
) {
537 asamples
+=(len
+priv
->audio_block_size
-1)/priv
->audio_block_size
;
540 mp_msg(MSGT_DEMUX
,MSGL_V
,"AVI video size=%"PRId64
" (%u) audio size=%"PRId64
" (%u)\n",vsize
,vsamples
,asize
,asamples
);
541 priv
->numberofframes
=vsamples
;
542 sh_video
->i_bps
=((float)vsize
/(float)vsamples
)*(float)sh_video
->video
.dwRate
/(float)sh_video
->video
.dwScale
;
543 if(sh_audio
) sh_audio
->i_bps
=((float)asize
/(float)asamples
)*(float)sh_audio
->audio
.dwRate
/(float)sh_audio
->audio
.dwScale
;
545 // guessing, results may be inaccurate:
549 if((priv
->numberofframes
=sh_video
->video
.dwLength
)<=1)
550 // bad video header, try to get number of frames from audio
551 if(sh_audio
&& sh_audio
->wf
->nAvgBytesPerSec
) priv
->numberofframes
=sh_video
->fps
*sh_audio
->audio
.dwLength
/sh_audio
->audio
.dwRate
*sh_audio
->audio
.dwScale
;
552 if(priv
->numberofframes
<=1){
553 mp_msg(MSGT_SEEK
,MSGL_WARN
,MSGTR_CouldntDetFNo
);
554 priv
->numberofframes
=0;
558 if(sh_audio
->wf
->nAvgBytesPerSec
&& sh_audio
->audio
.dwSampleSize
!=1){
559 asize
=(float)sh_audio
->wf
->nAvgBytesPerSec
*sh_audio
->audio
.dwLength
*sh_audio
->audio
.dwScale
/sh_audio
->audio
.dwRate
;
561 asize
=sh_audio
->audio
.dwLength
;
562 sh_audio
->i_bps
=(float)asize
/(sh_video
->frametime
*priv
->numberofframes
);
565 vsize
=demuxer
->movi_end
-demuxer
->movi_start
-asize
-8*priv
->numberofframes
;
566 mp_msg(MSGT_DEMUX
,MSGL_V
,"AVI video size=%"PRId64
" (%u) audio size=%"PRId64
"\n",vsize
,priv
->numberofframes
,asize
);
567 sh_video
->i_bps
=(float)vsize
/(sh_video
->frametime
*priv
->numberofframes
);
575 void demux_seek_avi(demuxer_t
*demuxer
,float rel_seek_secs
,float audio_delay
,int flags
){
576 avi_priv_t
*priv
=demuxer
->priv
;
577 demux_stream_t
*d_audio
=demuxer
->audio
;
578 demux_stream_t
*d_video
=demuxer
->video
;
579 sh_audio_t
*sh_audio
=d_audio
->sh
;
580 sh_video_t
*sh_video
=d_video
->sh
;
581 float skip_audio_secs
=0;
583 //FIXME: OFF_T - Didn't check AVI case yet (avi files can't be >2G anyway?)
584 //================= seek in AVI ==========================
585 int rel_seek_frames
=rel_seek_secs
*sh_video
->fps
;
586 int video_chunk_pos
=d_video
->pos
;
589 if(flags
&SEEK_ABSOLUTE
){
594 if(flags
&SEEK_FACTOR
){
595 rel_seek_frames
=rel_seek_secs
*priv
->numberofframes
;
598 priv
->skip_video_frames
=0;
599 priv
->avi_audio_pts
=0;
601 // ------------ STEP 1: find nearest video keyframe chunk ------------
602 // find nearest video keyframe chunk pos:
603 if(rel_seek_frames
>0){
605 while(video_chunk_pos
<priv
->idx_size
-1){
606 int id
=((AVIINDEXENTRY
*)priv
->idx
)[video_chunk_pos
].ckid
;
607 if(avi_stream_id(id
)==d_video
->id
){ // video frame
608 if((--rel_seek_frames
)<0 && ((AVIINDEXENTRY
*)priv
->idx
)[video_chunk_pos
].dwFlags
&AVIIF_KEYFRAME
) break;
614 while(video_chunk_pos
>0){
615 int id
=((AVIINDEXENTRY
*)priv
->idx
)[video_chunk_pos
].ckid
;
616 if(avi_stream_id(id
)==d_video
->id
){ // video frame
617 if((++rel_seek_frames
)>0 && ((AVIINDEXENTRY
*)priv
->idx
)[video_chunk_pos
].dwFlags
&AVIIF_KEYFRAME
) break;
622 priv
->idx_pos_a
=priv
->idx_pos_v
=priv
->idx_pos
=video_chunk_pos
;
624 // re-calc video pts:
626 for(i
=0;i
<video_chunk_pos
;i
++){
627 int id
=((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
;
628 if(avi_stream_id(id
)==d_video
->id
) ++d_video
->pack_no
;
631 sh_video
->num_frames
=sh_video
->num_frames_decoded
=d_video
->pack_no
;
632 priv
->avi_video_pts
=d_video
->pack_no
*(float)sh_video
->video
.dwScale
/(float)sh_video
->video
.dwRate
;
633 d_video
->pos
=video_chunk_pos
;
635 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
);
637 // ------------ STEP 2: seek audio, find the right chunk & pos ------------
640 priv
->audio_block_no
=0;
646 int skip_audio_bytes
=0;
647 int curr_audio_pos
=-1;
648 int audio_chunk_pos
=-1;
649 int chunk_max
=(demuxer
->type
==DEMUXER_TYPE_AVI
)?video_chunk_pos
:priv
->idx_size
;
651 if(sh_audio
->audio
.dwSampleSize
){
652 // constant rate audio stream
653 /* immediate seeking to audio position, including when streams are delayed */
654 curr_audio_pos
=(priv
->avi_video_pts
+ audio_delay
)*(float)sh_audio
->audio
.dwRate
/(float)sh_audio
->audio
.dwScale
;
655 curr_audio_pos
*=sh_audio
->audio
.dwSampleSize
;
657 // find audio chunk pos:
658 for(i
=0;i
<chunk_max
;i
++){
659 int id
=((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
;
660 if(avi_stream_id(id
)==d_audio
->id
){
661 len
=((AVIINDEXENTRY
*)priv
->idx
)[i
].dwChunkLength
;
662 if(d_audio
->dpos
<=curr_audio_pos
&& curr_audio_pos
<(d_audio
->dpos
+len
)){
666 priv
->audio_block_no
+=priv
->audio_block_size
?
667 ((len
+priv
->audio_block_size
-1)/priv
->audio_block_size
) : 1;
672 skip_audio_bytes
=curr_audio_pos
-d_audio
->dpos
;
674 mp_msg(MSGT_SEEK
,MSGL_V
,"SEEK: i=%d (max:%d) dpos=%d (wanted:%d) \n",
675 i
,chunk_max
,(int)d_audio
->dpos
,curr_audio_pos
);
679 /* immediate seeking to audio position, including when streams are delayed */
680 int chunks
=(priv
->avi_video_pts
+ audio_delay
)*(float)sh_audio
->audio
.dwRate
/(float)sh_audio
->audio
.dwScale
;
683 // find audio chunk pos:
684 for(i
=0;i
<priv
->idx_size
&& chunks
>0;i
++){
685 int id
=((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
;
686 if(avi_stream_id(id
)==d_audio
->id
){
687 len
=((AVIINDEXENTRY
*)priv
->idx
)[i
].dwChunkLength
;
689 skip_audio_bytes
+=len
;
692 priv
->audio_block_no
+=priv
->audio_block_size
?
693 ((len
+priv
->audio_block_size
-1)/priv
->audio_block_size
) : 1;
697 if(priv
->audio_block_size
)
698 chunks
-=(len
+priv
->audio_block_size
-1)/priv
->audio_block_size
;
704 // audio_chunk_pos = chunk no in index table (it's <=chunk_max)
705 // skip_audio_bytes = bytes to be skipped after chunk seek
706 // d-audio->pack_no = chunk_no in stream at audio_chunk_pos
707 // d_audio->dpos = bytepos in stream at audio_chunk_pos
710 // update stream position:
711 d_audio
->pos
=audio_chunk_pos
;
713 if(demuxer
->type
==DEMUXER_TYPE_AVI
){
714 // interleaved stream:
715 if(audio_chunk_pos
<video_chunk_pos
){
716 // calc priv->skip_video_frames & adjust video pts counter:
717 for(i
=audio_chunk_pos
;i
<video_chunk_pos
;i
++){
718 int id
=((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
;
719 if(avi_stream_id(id
)==d_video
->id
) ++priv
->skip_video_frames
;
721 // requires for correct audio pts calculation (demuxer):
722 priv
->avi_video_pts
-=priv
->skip_video_frames
*(float)sh_video
->video
.dwScale
/(float)sh_video
->video
.dwRate
;
723 priv
->avi_audio_pts
=priv
->avi_video_pts
;
724 // set index position:
725 priv
->idx_pos_a
=priv
->idx_pos_v
=priv
->idx_pos
=audio_chunk_pos
;
728 // non-interleaved stream:
729 priv
->idx_pos_a
=audio_chunk_pos
;
730 priv
->idx_pos_v
=video_chunk_pos
;
731 priv
->idx_pos
=(audio_chunk_pos
<video_chunk_pos
)?audio_chunk_pos
:video_chunk_pos
;
734 mp_msg(MSGT_SEEK
,MSGL_V
,"SEEK: idx=%d (a:%d v:%d) v.skip=%d a.skip=%d/%4.3f \n",
735 (int)priv
->idx_pos
,audio_chunk_pos
,video_chunk_pos
,
736 (int)priv
->skip_video_frames
,skip_audio_bytes
,skip_audio_secs
);
738 if(skip_audio_bytes
){
739 demux_read_data(d_audio
,NULL
,skip_audio_bytes
);
743 d_video
->pts
=priv
->avi_video_pts
; // OSD
748 void demux_close_avi(demuxer_t
*demuxer
) {
749 avi_priv_t
* priv
=demuxer
->priv
;
754 if(priv
->idx_size
> 0)
760 static int demux_avi_control(demuxer_t
*demuxer
,int cmd
, void *arg
){
761 avi_priv_t
*priv
=demuxer
->priv
;
762 demux_stream_t
*d_video
=demuxer
->video
;
763 sh_video_t
*sh_video
=d_video
->sh
;
766 case DEMUXER_CTRL_GET_TIME_LENGTH
:
767 if (!priv
->numberofframes
|| !sh_video
) return DEMUXER_CTRL_DONTKNOW
;
768 *((double *)arg
)=(double)priv
->numberofframes
/sh_video
->fps
;
769 if (sh_video
->video
.dwLength
<=1) return DEMUXER_CTRL_GUESS
;
770 return DEMUXER_CTRL_OK
;
772 case DEMUXER_CTRL_GET_PERCENT_POS
:
773 if (!priv
->numberofframes
|| !sh_video
) {
774 return DEMUXER_CTRL_DONTKNOW
;
776 *((int *)arg
)=(int)(priv
->video_pack_no
*100/priv
->numberofframes
);
777 if (sh_video
->video
.dwLength
<=1) return DEMUXER_CTRL_GUESS
;
778 return DEMUXER_CTRL_OK
;
780 case DEMUXER_CTRL_SWITCH_AUDIO
:
781 case DEMUXER_CTRL_SWITCH_VIDEO
: {
782 int audio
= (cmd
== DEMUXER_CTRL_SWITCH_AUDIO
);
783 demux_stream_t
*ds
= audio
? demuxer
->audio
: demuxer
->video
;
784 void **streams
= audio
? demuxer
->a_streams
: demuxer
->v_streams
;
785 int maxid
= FFMIN(100, audio
? MAX_A_STREAMS
: MAX_V_STREAMS
);
788 return DEMUXER_CTRL_NOTIMPL
;
790 if (*(int *)arg
>= 0)
791 ds
->id
= *(int *)arg
;
794 for (i
= 0; i
< maxid
; i
++) {
795 if (++ds
->id
>= maxid
) ds
->id
= 0;
796 if (streams
[ds
->id
]) break;
800 chunkid
= (ds
->id
/ 10 + '0') | (ds
->id
% 10 + '0') << 8;
802 if (!streams
[ds
->id
]) // stream not available
805 demux_avi_select_stream(demuxer
, chunkid
);
806 *(int *)arg
= ds
->id
;
807 return DEMUXER_CTRL_OK
;
811 return DEMUXER_CTRL_NOTIMPL
;
816 static int avi_check_file(demuxer_t
*demuxer
)
818 int id
=stream_read_dword_le(demuxer
->stream
); // "RIFF"
820 if((id
==mmioFOURCC('R','I','F','F')) || (id
==mmioFOURCC('O','N','2',' '))) {
821 stream_read_dword_le(demuxer
->stream
); //filesize
822 id
=stream_read_dword_le(demuxer
->stream
); // "AVI "
824 return DEMUXER_TYPE_AVI
;
825 // "Samsung Digimax i6 PMP" crap according to bug 742
826 if(id
==mmioFOURCC('A','V','I',0x19))
827 return DEMUXER_TYPE_AVI
;
828 if(id
==mmioFOURCC('O','N','2','f')){
829 mp_msg(MSGT_DEMUXER
,MSGL_INFO
,MSGTR_ON2AviFormat
);
830 return DEMUXER_TYPE_AVI
;
838 static demuxer_t
* demux_open_hack_avi(demuxer_t
*demuxer
)
842 demuxer
= demux_open_avi(demuxer
);
843 if(!demuxer
) return NULL
; // failed to open
844 sh_a
= demuxer
->audio
->sh
;
845 if(demuxer
->audio
->id
!= -2 && sh_a
) {
846 #ifdef CONFIG_OGGVORBIS
847 // support for Ogg-in-AVI:
848 if(sh_a
->format
== 0xFFFE)
849 demuxer
= init_avi_with_ogg(demuxer
);
850 else if(sh_a
->format
== 0x674F) {
853 s
= new_ds_stream(demuxer
->audio
);
854 od
= new_demuxer(s
,DEMUXER_TYPE_OGG
,-1,-2,-2,NULL
);
855 if(!demux_ogg_open(od
)) {
856 mp_msg( MSGT_DEMUXER
,MSGL_ERR
,MSGTR_ErrorOpeningOGGDemuxer
);
858 demuxer
->audio
->id
= -2;
860 demuxer
= new_demuxers_demuxer(demuxer
,od
,demuxer
);
869 const demuxer_desc_t demuxer_desc_avi
= {
874 "AVI files, including non interleaved files",
876 1, // safe autodetect
878 demux_avi_fill_buffer
,
885 const demuxer_desc_t demuxer_desc_avi_ni
= {
886 "AVI demuxer, non-interleaved",
890 "AVI files, including non interleaved files",
892 1, // safe autodetect
894 demux_avi_fill_buffer_ni
,
901 const demuxer_desc_t demuxer_desc_avi_nini
= {
902 "AVI demuxer, non-interleaved and no index",
906 "AVI files, including non interleaved files",
908 1, // safe autodetect
910 demux_avi_fill_buffer_nini
,