1 // AVI file parser for DEMUXER v2.9 by A'rpi/ESP-team
15 #include "aviheader.h"
17 extern demuxer_t
* init_avi_with_ogg(demuxer_t
* demuxer
);
18 extern int demux_ogg_open(demuxer_t
* demuxer
);
20 extern demuxer_desc_t demuxer_desc_avi_ni
;
21 extern 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
);
162 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
,"DEMUX_AVI: Skipping %d bytes from packet %04X\n",skip
,id
);
163 stream_skip(demux
->stream
,skip
);
169 // 0 = EOF or no stream found
170 // 1 = successfully read a packet
171 static int demux_avi_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
){
172 avi_priv_t
*priv
=demux
->priv
;
180 AVIINDEXENTRY
*idx
=NULL
;
181 if(priv
->idx_size
>0 && priv
->idx_pos
<priv
->idx_size
){
184 idx
=&((AVIINDEXENTRY
*)priv
->idx
)[priv
->idx_pos
++];
186 if(idx
->dwFlags
&AVIIF_LIST
){
190 if(!demux_avi_select_stream(demux
,idx
->ckid
)){
191 mp_dbg(MSGT_DEMUX
,MSGL_DBG3
,"Skip chunk %.4s (0x%X) \n",(char *)&idx
->ckid
,(unsigned int)idx
->ckid
);
192 continue; // skip this chunk
195 pos
= (off_t
)priv
->idx_offset
+AVI_IDX_OFFSET(idx
);
196 if((pos
<demux
->movi_start
|| pos
>=demux
->movi_end
) && (demux
->movi_end
>demux
->movi_start
) && (demux
->stream
->flags
& STREAM_SEEK
)){
197 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkOffset out of range! idx=0x%"PRIX64
" \n",(int64_t)pos
);
200 stream_seek(demux
->stream
,pos
);
201 demux
->filepos
=stream_tell(demux
->stream
);
202 id
=stream_read_dword_le(demux
->stream
);
203 if(stream_eof(demux
->stream
)) return 0; // EOF!
206 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id
,(char *)&idx
->ckid
);
207 if(valid_fourcc(idx
->ckid
))
208 id
=idx
->ckid
; // use index if valid
210 if(!valid_fourcc(id
)) continue; // drop chunk if both id and idx bad
212 len
=stream_read_dword_le(demux
->stream
);
213 if((len
!=idx
->dwChunkLength
)&&((len
+1)!=idx
->dwChunkLength
)){
214 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkSize mismatch! raw=%d idx=%d \n",len
,idx
->dwChunkLength
);
215 if(len
>0x200000 && idx
->dwChunkLength
>0x200000) continue; // both values bad :(
216 len
=choose_chunk_len(idx
->dwChunkLength
,len
);
218 if(!(idx
->dwFlags
&AVIIF_KEYFRAME
)) flags
=0;
220 demux
->filepos
=stream_tell(demux
->stream
);
221 if(demux
->filepos
>=demux
->movi_end
&& demux
->movi_end
>demux
->movi_start
&& (demux
->stream
->flags
& STREAM_SEEK
)){
222 demux
->stream
->eof
=1;
225 id
=stream_read_dword_le(demux
->stream
);
226 len
=stream_read_dword_le(demux
->stream
);
227 if(stream_eof(demux
->stream
)) return 0; // EOF!
229 if(id
==mmioFOURCC('L','I','S','T') || id
==mmioFOURCC('R', 'I', 'F', 'F')){
230 id
=stream_read_dword_le(demux
->stream
); // list or RIFF type
235 ds
=demux_avi_select_stream(demux
,id
);
237 if(ds
->packs
+1>=MAX_PACKS
|| ds
->bytes
+len
>=MAX_PACK_BYTES
){
238 // this packet will cause a buffer overflow, switch to -ni mode!!!
239 mp_msg(MSGT_DEMUX
,MSGL_WARN
,MSGTR_SwitchToNi
);
240 if(priv
->idx_size
>0){
242 demux
->type
=DEMUXER_TYPE_AVI_NI
;
243 demux
->desc
=&demuxer_desc_avi_ni
;
244 --priv
->idx_pos
; // hack
247 demux
->type
=DEMUXER_TYPE_AVI_NINI
;
248 demux
->desc
=&demuxer_desc_avi_nini
;
249 priv
->idx_pos
=demux
->filepos
; // hack
251 priv
->idx_pos_v
=priv
->idx_pos_a
=priv
->idx_pos
;
252 // quit now, we can't even (no enough buffer memory) read this packet :(
256 ret
=demux_avi_read_packet(demux
,ds
,id
,len
,priv
->idx_pos
-1,flags
);
263 // 0 = EOF or no stream found
264 // 1 = successfully read a packet
265 int demux_avi_fill_buffer_ni(demuxer_t
*demux
,demux_stream_t
* ds
){
266 avi_priv_t
*priv
=demux
->priv
;
273 AVIINDEXENTRY
*idx
=NULL
;
275 demux
->filepos
=stream_tell(demux
->stream
);
277 if(ds
==demux
->video
) idx_pos
=priv
->idx_pos_v
++; else
278 if(ds
==demux
->audio
) idx_pos
=priv
->idx_pos_a
++; else
279 idx_pos
=priv
->idx_pos
++;
281 if(priv
->idx_size
>0 && idx_pos
<priv
->idx_size
){
283 idx
=&((AVIINDEXENTRY
*)priv
->idx
)[idx_pos
];
285 if(idx
->dwFlags
&AVIIF_LIST
){
289 if(ds
&& demux_avi_select_stream(demux
,idx
->ckid
)!=ds
){
290 mp_dbg(MSGT_DEMUX
,MSGL_DBG3
,"Skip chunk %.4s (0x%X) \n",(char *)&idx
->ckid
,(unsigned int)idx
->ckid
);
291 continue; // skip this chunk
294 pos
= priv
->idx_offset
+AVI_IDX_OFFSET(idx
);
295 if((pos
<demux
->movi_start
|| pos
>=demux
->movi_end
) && (demux
->movi_end
>demux
->movi_start
)){
296 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkOffset out of range! current=0x%"PRIX64
" idx=0x%"PRIX64
" \n",(int64_t)demux
->filepos
,(int64_t)pos
);
299 stream_seek(demux
->stream
,pos
);
301 id
=stream_read_dword_le(demux
->stream
);
303 if(stream_eof(demux
->stream
)) return 0;
306 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id
,(char *)&idx
->ckid
);
307 if(valid_fourcc(idx
->ckid
))
308 id
=idx
->ckid
; // use index if valid
310 if(!valid_fourcc(id
)) continue; // drop chunk if both id and idx bad
312 len
=stream_read_dword_le(demux
->stream
);
313 if((len
!=idx
->dwChunkLength
)&&((len
+1)!=idx
->dwChunkLength
)){
314 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkSize mismatch! raw=%d idx=%d \n",len
,idx
->dwChunkLength
);
315 if(len
>0x200000 && idx
->dwChunkLength
>0x200000) continue; // both values bad :(
316 len
=choose_chunk_len(idx
->dwChunkLength
,len
);
318 if(!(idx
->dwFlags
&AVIIF_KEYFRAME
)) flags
=0;
320 ret
=demux_avi_read_packet(demux
,demux_avi_select_stream(demux
,id
),id
,len
,idx_pos
,flags
);
327 // 0 = EOF or no stream found
328 // 1 = successfully read a packet
329 int demux_avi_fill_buffer_nini(demuxer_t
*demux
,demux_stream_t
* ds
){
330 avi_priv_t
*priv
=demux
->priv
;
336 if(ds
==demux
->video
) fpos
=&priv
->idx_pos_v
; else
337 if(ds
==demux
->audio
) fpos
=&priv
->idx_pos_a
; else
340 stream_seek(demux
->stream
,fpos
[0]);
344 demux
->filepos
=stream_tell(demux
->stream
);
345 if(demux
->filepos
>=demux
->movi_end
&& (demux
->movi_end
>demux
->movi_start
)){
350 id
=stream_read_dword_le(demux
->stream
);
351 len
=stream_read_dword_le(demux
->stream
);
353 if(stream_eof(demux
->stream
)) return 0;
355 if(id
==mmioFOURCC('L','I','S','T')){
356 id
=stream_read_dword_le(demux
->stream
); // list type
360 if(id
==mmioFOURCC('R','I','F','F')){
361 mp_msg(MSGT_DEMUX
,MSGL_V
,"additional RIFF header...\n");
362 id
=stream_read_dword_le(demux
->stream
); // "AVIX"
366 if(ds
==demux_avi_select_stream(demux
,id
)){
368 ret
=demux_avi_read_packet(demux
,ds
,id
,len
,priv
->idx_pos
-1,0);
371 int skip
=(len
+1)&(~1); // total bytes in this chunk
372 stream_skip(demux
->stream
,skip
);
376 fpos
[0]=stream_tell(demux
->stream
);
380 // AVI demuxer parameters:
381 int index_mode
=-1; // -1=untouched 0=don't use index 1=use (geneate) index
382 char *index_file_save
= NULL
, *index_file_load
= NULL
;
383 int force_ni
=0; // force non-interleaved AVI parsing
385 void read_avi_header(demuxer_t
*demuxer
,int index_mode
);
387 static demuxer_t
* demux_open_avi(demuxer_t
* demuxer
){
388 demux_stream_t
*d_audio
=demuxer
->audio
;
389 demux_stream_t
*d_video
=demuxer
->video
;
390 sh_audio_t
*sh_audio
=NULL
;
391 sh_video_t
*sh_video
=NULL
;
392 avi_priv_t
* priv
=malloc(sizeof(avi_priv_t
));
395 priv
->avi_audio_pts
=priv
->avi_video_pts
=0.0f
;
396 priv
->pts_correction
=0.0f
;
397 priv
->skip_video_frames
=0;
398 priv
->pts_corr_bytes
=0;
399 priv
->pts_has_video
=priv
->pts_corrected
=0;
400 priv
->video_pack_no
=0;
401 priv
->audio_block_no
=0;
402 priv
->audio_block_size
=0;
404 priv
->suidx_size
= 0;
407 demuxer
->priv
=(void*)priv
;
410 read_avi_header(demuxer
,(demuxer
->stream
->flags
& STREAM_SEEK_BW
)?index_mode
:-2);
412 if(demuxer
->audio
->id
>=0 && !demuxer
->a_streams
[demuxer
->audio
->id
]){
413 mp_msg(MSGT_DEMUX
,MSGL_WARN
,MSGTR_InvalidAudioStreamNosound
,demuxer
->audio
->id
);
414 demuxer
->audio
->id
=-2; // disabled
416 if(demuxer
->video
->id
>=0 && !demuxer
->v_streams
[demuxer
->video
->id
]){
417 mp_msg(MSGT_DEMUX
,MSGL_WARN
,MSGTR_InvalidAudioStreamUsingDefault
,demuxer
->video
->id
);
418 demuxer
->video
->id
=-1; // autodetect
421 stream_reset(demuxer
->stream
);
422 stream_seek(demuxer
->stream
,demuxer
->movi_start
);
426 if(priv
->idx_size
>1){
427 // decide index format:
429 if((AVI_IDX_OFFSET(&((AVIINDEXENTRY
*)priv
->idx
)[0])<demuxer
->movi_start
||
430 AVI_IDX_OFFSET(&((AVIINDEXENTRY
*)priv
->idx
)[1])<demuxer
->movi_start
)&& !priv
->isodml
)
431 priv
->idx_offset
=demuxer
->movi_start
-4;
435 if(AVI_IDX_OFFSET(&((AVIINDEXENTRY
*)priv
->idx
)[0])<demuxer
->movi_start
)
436 priv
->idx_offset
=demuxer
->movi_start
-4;
440 mp_msg(MSGT_DEMUX
,MSGL_V
,"AVI index offset: 0x%X (movi=0x%X idx0=0x%X idx1=0x%X)\n",
441 (int)priv
->idx_offset
,(int)demuxer
->movi_start
,
442 (int)((AVIINDEXENTRY
*)priv
->idx
)[0].dwChunkOffset
,
443 (int)((AVIINDEXENTRY
*)priv
->idx
)[1].dwChunkOffset
);
446 if(priv
->idx_size
>0){
447 // check that file is non-interleaved:
451 for(i
=0;i
<priv
->idx_size
;i
++){
452 AVIINDEXENTRY
* idx
=&((AVIINDEXENTRY
*)priv
->idx
)[i
];
453 demux_stream_t
* ds
=demux_avi_select_stream(demuxer
,idx
->ckid
);
454 off_t pos
= priv
->idx_offset
+ AVI_IDX_OFFSET(idx
);
455 if(a_pos
==-1 && ds
==demuxer
->audio
){
459 if(v_pos
==-1 && ds
==demuxer
->video
){
465 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"AVI_NI: " MSGTR_MissingVideoStream
);
469 d_audio
->sh
=sh_audio
=NULL
;
471 if(force_ni
|| abs(a_pos
-v_pos
)>0x100000){ // distance > 1MB
472 mp_msg(MSGT_DEMUX
,MSGL_INFO
,MSGTR_NI_Message
,force_ni
?MSGTR_NI_Forced
:MSGTR_NI_Detected
);
473 demuxer
->type
=DEMUXER_TYPE_AVI_NI
; // HACK!!!!
474 demuxer
->desc
=&demuxer_desc_avi_ni
; // HACK!!!!
475 pts_from_bps
=1; // force BPS sync!
481 mp_msg(MSGT_DEMUX
,MSGL_INFO
,MSGTR_UsingNINI
);
482 demuxer
->type
=DEMUXER_TYPE_AVI_NINI
; // HACK!!!!
483 demuxer
->desc
=&demuxer_desc_avi_nini
; // HACK!!!!
485 priv
->idx_pos_v
=demuxer
->movi_start
;
486 pts_from_bps
=1; // force BPS sync!
490 if(!ds_fill_buffer(d_video
)){
491 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"AVI: " MSGTR_MissingVideoStreamBug
);
494 sh_video
=d_video
->sh
;sh_video
->ds
=d_video
;
496 mp_msg(MSGT_DEMUX
,MSGL_V
,"AVI: Searching for audio stream (id:%d)\n",d_audio
->id
);
497 if(!priv
->audio_streams
|| !ds_fill_buffer(d_audio
)){
498 mp_msg(MSGT_DEMUX
,MSGL_INFO
,"AVI: " MSGTR_MissingAudioStream
);
499 d_audio
->sh
=sh_audio
=NULL
;
501 sh_audio
=d_audio
->sh
;sh_audio
->ds
=d_audio
;
505 // calculating audio/video bitrate:
506 if(priv
->idx_size
>0){
507 // we have index, let's count 'em!
513 for(i
=0;i
<priv
->idx_size
;i
++){
514 int id
=avi_stream_id(((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
);
515 int len
=((AVIINDEXENTRY
*)priv
->idx
)[i
].dwChunkLength
;
516 if(sh_video
->ds
->id
== id
) {
520 else if(sh_audio
&& sh_audio
->ds
->id
== id
) {
522 asamples
+=(len
+priv
->audio_block_size
-1)/priv
->audio_block_size
;
525 mp_msg(MSGT_DEMUX
,MSGL_V
,"AVI video size=%"PRId64
" (%u) audio size=%"PRId64
" (%u)\n",vsize
,vsamples
,asize
,asamples
);
526 priv
->numberofframes
=vsamples
;
527 sh_video
->i_bps
=((float)vsize
/(float)vsamples
)*(float)sh_video
->video
.dwRate
/(float)sh_video
->video
.dwScale
;
528 if(sh_audio
) sh_audio
->i_bps
=((float)asize
/(float)asamples
)*(float)sh_audio
->audio
.dwRate
/(float)sh_audio
->audio
.dwScale
;
530 // guessing, results may be inaccurate:
534 if((priv
->numberofframes
=sh_video
->video
.dwLength
)<=1)
535 // bad video header, try to get number of frames from audio
536 if(sh_audio
&& sh_audio
->wf
->nAvgBytesPerSec
) priv
->numberofframes
=sh_video
->fps
*sh_audio
->audio
.dwLength
/sh_audio
->audio
.dwRate
*sh_audio
->audio
.dwScale
;
537 if(priv
->numberofframes
<=1){
538 mp_msg(MSGT_SEEK
,MSGL_WARN
,MSGTR_CouldntDetFNo
);
539 priv
->numberofframes
=0;
543 if(sh_audio
->wf
->nAvgBytesPerSec
&& sh_audio
->audio
.dwSampleSize
!=1){
544 asize
=(float)sh_audio
->wf
->nAvgBytesPerSec
*sh_audio
->audio
.dwLength
*sh_audio
->audio
.dwScale
/sh_audio
->audio
.dwRate
;
546 asize
=sh_audio
->audio
.dwLength
;
547 sh_audio
->i_bps
=(float)asize
/(sh_video
->frametime
*priv
->numberofframes
);
550 vsize
=demuxer
->movi_end
-demuxer
->movi_start
-asize
-8*priv
->numberofframes
;
551 mp_msg(MSGT_DEMUX
,MSGL_V
,"AVI video size=%"PRId64
" (%u) audio size=%"PRId64
"\n",vsize
,priv
->numberofframes
,asize
);
552 sh_video
->i_bps
=(float)vsize
/(sh_video
->frametime
*priv
->numberofframes
);
560 void demux_seek_avi(demuxer_t
*demuxer
,float rel_seek_secs
,float audio_delay
,int flags
){
561 avi_priv_t
*priv
=demuxer
->priv
;
562 demux_stream_t
*d_audio
=demuxer
->audio
;
563 demux_stream_t
*d_video
=demuxer
->video
;
564 sh_audio_t
*sh_audio
=d_audio
->sh
;
565 sh_video_t
*sh_video
=d_video
->sh
;
566 float skip_audio_secs
=0;
568 //FIXME: OFF_T - Didn't check AVI case yet (avi files can't be >2G anyway?)
569 //================= seek in AVI ==========================
570 int rel_seek_frames
=rel_seek_secs
*sh_video
->fps
;
571 int video_chunk_pos
=d_video
->pos
;
580 rel_seek_frames
=rel_seek_secs
*priv
->numberofframes
;
583 priv
->skip_video_frames
=0;
584 priv
->avi_audio_pts
=0;
586 // ------------ STEP 1: find nearest video keyframe chunk ------------
587 // find nearest video keyframe chunk pos:
588 if(rel_seek_frames
>0){
590 while(video_chunk_pos
<priv
->idx_size
-1){
591 int id
=((AVIINDEXENTRY
*)priv
->idx
)[video_chunk_pos
].ckid
;
592 if(avi_stream_id(id
)==d_video
->id
){ // video frame
593 if((--rel_seek_frames
)<0 && ((AVIINDEXENTRY
*)priv
->idx
)[video_chunk_pos
].dwFlags
&AVIIF_KEYFRAME
) break;
599 while(video_chunk_pos
>0){
600 int id
=((AVIINDEXENTRY
*)priv
->idx
)[video_chunk_pos
].ckid
;
601 if(avi_stream_id(id
)==d_video
->id
){ // video frame
602 if((++rel_seek_frames
)>0 && ((AVIINDEXENTRY
*)priv
->idx
)[video_chunk_pos
].dwFlags
&AVIIF_KEYFRAME
) break;
607 priv
->idx_pos_a
=priv
->idx_pos_v
=priv
->idx_pos
=video_chunk_pos
;
609 // re-calc video pts:
611 for(i
=0;i
<video_chunk_pos
;i
++){
612 int id
=((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
;
613 if(avi_stream_id(id
)==d_video
->id
) ++d_video
->pack_no
;
616 sh_video
->num_frames
=sh_video
->num_frames_decoded
=d_video
->pack_no
;
617 priv
->avi_video_pts
=d_video
->pack_no
*(float)sh_video
->video
.dwScale
/(float)sh_video
->video
.dwRate
;
618 d_video
->pos
=video_chunk_pos
;
620 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
);
622 // ------------ STEP 2: seek audio, find the right chunk & pos ------------
625 priv
->audio_block_no
=0;
631 int skip_audio_bytes
=0;
632 int curr_audio_pos
=-1;
633 int audio_chunk_pos
=-1;
634 int chunk_max
=(demuxer
->type
==DEMUXER_TYPE_AVI
)?video_chunk_pos
:priv
->idx_size
;
636 if(sh_audio
->audio
.dwSampleSize
){
637 // constant rate audio stream
638 /* immediate seeking to audio position, including when streams are delayed */
639 curr_audio_pos
=(priv
->avi_video_pts
+ audio_delay
)*(float)sh_audio
->audio
.dwRate
/(float)sh_audio
->audio
.dwScale
;
640 curr_audio_pos
*=sh_audio
->audio
.dwSampleSize
;
642 // find audio chunk pos:
643 for(i
=0;i
<chunk_max
;i
++){
644 int id
=((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
;
645 if(avi_stream_id(id
)==d_audio
->id
){
646 len
=((AVIINDEXENTRY
*)priv
->idx
)[i
].dwChunkLength
;
647 if(d_audio
->dpos
<=curr_audio_pos
&& curr_audio_pos
<(d_audio
->dpos
+len
)){
651 priv
->audio_block_no
+=priv
->audio_block_size
?
652 ((len
+priv
->audio_block_size
-1)/priv
->audio_block_size
) : 1;
657 skip_audio_bytes
=curr_audio_pos
-d_audio
->dpos
;
659 mp_msg(MSGT_SEEK
,MSGL_V
,"SEEK: i=%d (max:%d) dpos=%d (wanted:%d) \n",
660 i
,chunk_max
,(int)d_audio
->dpos
,curr_audio_pos
);
664 /* immediate seeking to audio position, including when streams are delayed */
665 int chunks
=(priv
->avi_video_pts
+ audio_delay
)*(float)sh_audio
->audio
.dwRate
/(float)sh_audio
->audio
.dwScale
;
668 // find audio chunk pos:
669 for(i
=0;i
<priv
->idx_size
&& chunks
>0;i
++){
670 int id
=((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
;
671 if(avi_stream_id(id
)==d_audio
->id
){
672 len
=((AVIINDEXENTRY
*)priv
->idx
)[i
].dwChunkLength
;
674 skip_audio_bytes
+=len
;
677 priv
->audio_block_no
+=priv
->audio_block_size
?
678 ((len
+priv
->audio_block_size
-1)/priv
->audio_block_size
) : 1;
682 if(priv
->audio_block_size
)
683 chunks
-=(len
+priv
->audio_block_size
-1)/priv
->audio_block_size
;
689 // audio_chunk_pos = chunk no in index table (it's <=chunk_max)
690 // skip_audio_bytes = bytes to be skipped after chunk seek
691 // d-audio->pack_no = chunk_no in stream at audio_chunk_pos
692 // d_audio->dpos = bytepos in stream at audio_chunk_pos
695 // update stream position:
696 d_audio
->pos
=audio_chunk_pos
;
698 if(demuxer
->type
==DEMUXER_TYPE_AVI
){
699 // interleaved stream:
700 if(audio_chunk_pos
<video_chunk_pos
){
701 // calc priv->skip_video_frames & adjust video pts counter:
702 for(i
=audio_chunk_pos
;i
<video_chunk_pos
;i
++){
703 int id
=((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
;
704 if(avi_stream_id(id
)==d_video
->id
) ++priv
->skip_video_frames
;
706 // requires for correct audio pts calculation (demuxer):
707 priv
->avi_video_pts
-=priv
->skip_video_frames
*(float)sh_video
->video
.dwScale
/(float)sh_video
->video
.dwRate
;
708 priv
->avi_audio_pts
=priv
->avi_video_pts
;
709 // set index position:
710 priv
->idx_pos_a
=priv
->idx_pos_v
=priv
->idx_pos
=audio_chunk_pos
;
713 // non-interleaved stream:
714 priv
->idx_pos_a
=audio_chunk_pos
;
715 priv
->idx_pos_v
=video_chunk_pos
;
716 priv
->idx_pos
=(audio_chunk_pos
<video_chunk_pos
)?audio_chunk_pos
:video_chunk_pos
;
719 mp_msg(MSGT_SEEK
,MSGL_V
,"SEEK: idx=%d (a:%d v:%d) v.skip=%d a.skip=%d/%4.3f \n",
720 (int)priv
->idx_pos
,audio_chunk_pos
,video_chunk_pos
,
721 (int)priv
->skip_video_frames
,skip_audio_bytes
,skip_audio_secs
);
723 if(skip_audio_bytes
){
724 demux_read_data(d_audio
,NULL
,skip_audio_bytes
);
728 d_video
->pts
=priv
->avi_video_pts
; // OSD
733 void demux_close_avi(demuxer_t
*demuxer
) {
734 avi_priv_t
* priv
=demuxer
->priv
;
739 if(priv
->idx_size
> 0)
745 static int demux_avi_control(demuxer_t
*demuxer
,int cmd
, void *arg
){
746 avi_priv_t
*priv
=demuxer
->priv
;
747 demux_stream_t
*d_video
=demuxer
->video
;
748 sh_video_t
*sh_video
=d_video
->sh
;
751 case DEMUXER_CTRL_GET_TIME_LENGTH
:
752 if (!priv
->numberofframes
|| !sh_video
) return DEMUXER_CTRL_DONTKNOW
;
753 *((double *)arg
)=(double)priv
->numberofframes
/sh_video
->fps
;
754 if (sh_video
->video
.dwLength
<=1) return DEMUXER_CTRL_GUESS
;
755 return DEMUXER_CTRL_OK
;
757 case DEMUXER_CTRL_GET_PERCENT_POS
:
758 if (!priv
->numberofframes
|| !sh_video
) {
759 return DEMUXER_CTRL_DONTKNOW
;
761 *((int *)arg
)=(int)(priv
->video_pack_no
*100/priv
->numberofframes
);
762 if (sh_video
->video
.dwLength
<=1) return DEMUXER_CTRL_GUESS
;
763 return DEMUXER_CTRL_OK
;
765 case DEMUXER_CTRL_SWITCH_AUDIO
:
766 case DEMUXER_CTRL_SWITCH_VIDEO
: {
767 int audio
= (cmd
== DEMUXER_CTRL_SWITCH_AUDIO
);
768 demux_stream_t
*ds
= audio
? demuxer
->audio
: demuxer
->video
;
769 void **streams
= audio
? demuxer
->a_streams
: demuxer
->v_streams
;
770 int maxid
= FFMIN(100, audio
? MAX_A_STREAMS
: MAX_V_STREAMS
);
773 return DEMUXER_CTRL_NOTIMPL
;
775 if (*(int *)arg
>= 0)
776 ds
->id
= *(int *)arg
;
779 for (i
= 0; i
< maxid
; i
++) {
780 if (++ds
->id
>= maxid
) ds
->id
= 0;
781 if (streams
[ds
->id
]) break;
785 chunkid
= (ds
->id
/ 10 + '0') | (ds
->id
% 10 + '0') << 8;
787 if (!streams
[ds
->id
]) // stream not available
790 demux_avi_select_stream(demuxer
, chunkid
);
791 *(int *)arg
= ds
->id
;
792 return DEMUXER_CTRL_OK
;
796 return DEMUXER_CTRL_NOTIMPL
;
801 static int avi_check_file(demuxer_t
*demuxer
)
803 int id
=stream_read_dword_le(demuxer
->stream
); // "RIFF"
805 if((id
==mmioFOURCC('R','I','F','F')) || (id
==mmioFOURCC('O','N','2',' '))) {
806 stream_read_dword_le(demuxer
->stream
); //filesize
807 id
=stream_read_dword_le(demuxer
->stream
); // "AVI "
809 return DEMUXER_TYPE_AVI
;
810 if(id
==mmioFOURCC('O','N','2','f')){
811 mp_msg(MSGT_DEMUXER
,MSGL_INFO
,MSGTR_ON2AviFormat
);
812 return DEMUXER_TYPE_AVI
;
820 static demuxer_t
* demux_open_hack_avi(demuxer_t
*demuxer
)
824 demuxer
= (demuxer_t
*) demux_open_avi(demuxer
);
825 if(!demuxer
) return NULL
; // failed to open
826 sh_a
= (sh_audio_t
*)demuxer
->audio
->sh
;
827 if(demuxer
->audio
->id
!= -2 && sh_a
) {
828 #ifdef HAVE_OGGVORBIS
829 // support for Ogg-in-AVI:
830 if(sh_a
->format
== 0xFFFE)
831 demuxer
= init_avi_with_ogg(demuxer
);
832 else if(sh_a
->format
== 0x674F) {
835 s
= new_ds_stream(demuxer
->audio
);
836 od
= new_demuxer(s
,DEMUXER_TYPE_OGG
,-1,-2,-2,NULL
);
837 if(!demux_ogg_open(od
)) {
838 mp_msg( MSGT_DEMUXER
,MSGL_ERR
,MSGTR_ErrorOpeningOGGDemuxer
);
840 demuxer
->audio
->id
= -2;
842 demuxer
= new_demuxers_demuxer(demuxer
,od
,demuxer
);
851 demuxer_desc_t demuxer_desc_avi
= {
856 "AVI files, including non interleaved files",
858 1, // safe autodetect
860 demux_avi_fill_buffer
,
867 demuxer_desc_t demuxer_desc_avi_ni
= {
868 "AVI demuxer, non-interleaved",
872 "AVI files, including non interleaved files",
874 1, // safe autodetect
876 demux_avi_fill_buffer_ni
,
883 demuxer_desc_t demuxer_desc_avi_nini
= {
884 "AVI demuxer, non-interleaved and no index",
888 "AVI files, including non interleaved files",
890 1, // safe autodetect
892 demux_avi_fill_buffer_nini
,