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 static demux_stream_t
* demux_avi_select_stream(demuxer_t
*demux
,
30 int stream_id
=avi_stream_id(id
);
33 if(demux
->video
->id
==-1)
34 if(demux
->v_streams
[stream_id
])
35 demux
->video
->id
=stream_id
;
37 if(demux
->audio
->id
==-1)
38 if(demux
->a_streams
[stream_id
])
39 demux
->audio
->id
=stream_id
;
41 if(stream_id
==demux
->audio
->id
){
42 if(!demux
->audio
->sh
){
44 avi_priv_t
*priv
=demux
->priv
;
45 sh
=demux
->audio
->sh
=demux
->a_streams
[stream_id
];
46 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected AVI audio ID = %d\n",demux
->audio
->id
);
48 priv
->audio_block_size
=sh
->wf
->nBlockAlign
;
49 if(!priv
->audio_block_size
){
50 // for PCM audio we can calculate the blocksize:
52 priv
->audio_block_size
=sh
->wf
->nChannels
*(sh
->wf
->wBitsPerSample
/8);
54 priv
->audio_block_size
=1; // hope the best...
56 // workaround old mencoder's bug:
57 if(sh
->audio
.dwSampleSize
==1 && sh
->audio
.dwScale
==1 &&
58 (sh
->wf
->nBlockAlign
==1152 || sh
->wf
->nBlockAlign
==576)){
59 mp_tmsg(MSGT_DEMUX
,MSGL_WARN
,"AVI: Working around CBR-MP3 nBlockAlign header bug!\n");
60 priv
->audio_block_size
=1;
64 priv
->audio_block_size
=sh
->audio
.dwSampleSize
;
69 if(stream_id
==demux
->video
->id
){
70 if(!demux
->video
->sh
){
71 demux
->video
->sh
=demux
->v_streams
[stream_id
];
72 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected AVI video ID = %d\n",demux
->video
->id
);
76 if(id
!=mmioFOURCC('J','U','N','K')){
78 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"Unknown chunk: %.4s (%X)\n",(char *) &id
,id
);
84 static int valid_fourcc(unsigned int id
){
85 static const char valid
[] = "0123456789abcdefghijklmnopqrstuvwxyz"
86 "ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
87 unsigned char* fcc
=(unsigned char*)(&id
);
88 return strchr(valid
, fcc
[0]) && strchr(valid
, fcc
[1]) &&
89 strchr(valid
, fcc
[2]) && strchr(valid
, fcc
[3]);
92 static int choose_chunk_len(unsigned int len1
,unsigned int len2
){
93 // len1 has a bit more priority than len2. len1!=len2
94 // Note: this is a first-idea-logic, may be wrong. comments welcomed.
96 // prefer small frames rather than 0
97 if(!len1
) return (len2
>0x80000) ? len1
: len2
;
98 if(!len2
) return (len1
>0x100000) ? len2
: len1
;
100 // choose the smaller value:
101 return (len1
<len2
)? len1
: len2
;
104 static int demux_avi_read_packet(demuxer_t
*demux
,demux_stream_t
*ds
,unsigned int id
,unsigned int len
,int idxpos
,int flags
){
105 avi_priv_t
*priv
=demux
->priv
;
109 mp_dbg(MSGT_DEMUX
,MSGL_DBG3
,"demux_avi.read_packet: %X\n",id
);
111 if(ds
==demux
->audio
){
112 if(priv
->pts_corrected
==0){
113 if(priv
->pts_has_video
){
114 // we have video pts now
116 if(((sh_audio_t
*)(ds
->sh
))->wf
->nAvgBytesPerSec
)
117 delay
=(float)priv
->pts_corr_bytes
/((sh_audio_t
*)(ds
->sh
))->wf
->nAvgBytesPerSec
;
118 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
);
119 //priv->pts_correction=-priv->avi_audio_pts+delay;
120 priv
->pts_correction
=delay
-priv
->avi_audio_pts
;
121 priv
->avi_audio_pts
+=priv
->pts_correction
;
122 priv
->pts_corrected
=1;
124 priv
->pts_corr_bytes
+=len
;
127 pts
= priv
->audio_block_no
*
128 (float)((sh_audio_t
*)demux
->audio
->sh
)->audio
.dwScale
/
129 (float)((sh_audio_t
*)demux
->audio
->sh
)->audio
.dwRate
;
131 pts
=priv
->avi_audio_pts
; //+priv->pts_correction;
132 priv
->avi_audio_pts
=0;
133 // update blockcount:
134 priv
->audio_block_no
+=priv
->audio_block_size
?
135 ((len
+priv
->audio_block_size
-1)/priv
->audio_block_size
) : 1;
137 if(ds
==demux
->video
){
139 if(priv
->skip_video_frames
>0){
140 // drop frame (seeking)
141 --priv
->skip_video_frames
;
145 pts
= priv
->avi_video_pts
= priv
->video_pack_no
*
146 (float)((sh_video_t
*)demux
->video
->sh
)->video
.dwScale
/
147 (float)((sh_video_t
*)demux
->video
->sh
)->video
.dwRate
;
149 priv
->avi_audio_pts
=priv
->avi_video_pts
+priv
->pts_correction
;
150 priv
->pts_has_video
=1;
152 if(ds
) ++priv
->video_pack_no
;
156 skip
=(len
+1)&(~1); // total bytes in this chunk
159 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
,"DEMUX_AVI: Read %d data bytes from packet %04X\n",len
,id
);
160 ds_read_packet(ds
,demux
->stream
,len
,pts
,idxpos
,flags
);
163 skip
= FFMAX(skip
, 0);
164 if (avi_stream_id(id
) > 99 && id
!= mmioFOURCC('J','U','N','K'))
165 skip
= FFMIN(skip
, 65536);
167 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
,"DEMUX_AVI: Skipping %d bytes from packet %04X\n",skip
,id
);
168 stream_skip(demux
->stream
,skip
);
173 static uint32_t avi_find_id(stream_t
*stream
) {
174 uint32_t id
= stream_read_dword_le(stream
);
176 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "Incomplete stream? Trying resync.\n");
178 id
= stream_read_dword_le(stream
);
179 if (stream_eof(stream
)) return 0;
180 } while (avi_stream_id(id
) > 99);
186 // 0 = EOF or no stream found
187 // 1 = successfully read a packet
188 static int demux_avi_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
){
189 avi_priv_t
*priv
=demux
->priv
;
197 AVIINDEXENTRY
*idx
=NULL
;
198 if(priv
->idx_size
>0 && priv
->idx_pos
<priv
->idx_size
){
201 idx
=&((AVIINDEXENTRY
*)priv
->idx
)[priv
->idx_pos
++];
203 if(idx
->dwFlags
&AVIIF_LIST
){
207 if(!demux_avi_select_stream(demux
,idx
->ckid
)){
208 mp_dbg(MSGT_DEMUX
,MSGL_DBG3
,"Skip chunk %.4s (0x%X) \n",(char *)&idx
->ckid
,(unsigned int)idx
->ckid
);
209 continue; // skip this chunk
212 pos
= (off_t
)priv
->idx_offset
+AVI_IDX_OFFSET(idx
);
213 if((pos
<demux
->movi_start
|| pos
>=demux
->movi_end
) && (demux
->movi_end
>demux
->movi_start
) && (demux
->stream
->flags
& STREAM_SEEK
)){
214 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkOffset out of range! idx=0x%"PRIX64
" \n",(int64_t)pos
);
217 stream_seek(demux
->stream
,pos
);
218 demux
->filepos
=stream_tell(demux
->stream
);
219 id
=stream_read_dword_le(demux
->stream
);
220 if(stream_eof(demux
->stream
)) return 0; // EOF!
223 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id
,(char *)&idx
->ckid
);
224 if(valid_fourcc(idx
->ckid
))
225 id
=idx
->ckid
; // use index if valid
227 if(!valid_fourcc(id
)) continue; // drop chunk if both id and idx bad
229 len
=stream_read_dword_le(demux
->stream
);
230 if((len
!=idx
->dwChunkLength
)&&((len
+1)!=idx
->dwChunkLength
)){
231 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkSize mismatch! raw=%d idx=%d \n",len
,idx
->dwChunkLength
);
232 if(len
>0x200000 && idx
->dwChunkLength
>0x200000) continue; // both values bad :(
233 len
=choose_chunk_len(idx
->dwChunkLength
,len
);
235 if(!(idx
->dwFlags
&AVIIF_KEYFRAME
)) flags
=0;
237 demux
->filepos
=stream_tell(demux
->stream
);
238 if(demux
->filepos
>=demux
->movi_end
&& demux
->movi_end
>demux
->movi_start
&& (demux
->stream
->flags
& STREAM_SEEK
)){
239 demux
->stream
->eof
=1;
242 id
=avi_find_id(demux
->stream
);
243 len
=stream_read_dword_le(demux
->stream
);
244 if(stream_eof(demux
->stream
)) return 0; // EOF!
246 if(id
==mmioFOURCC('L','I','S','T') || id
==mmioFOURCC('R', 'I', 'F', 'F')){
247 id
=stream_read_dword_le(demux
->stream
); // list or RIFF type
252 ds
=demux_avi_select_stream(demux
,id
);
254 if(ds
->packs
+1>=MAX_PACKS
|| ds
->bytes
+len
>=MAX_PACK_BYTES
){
255 // this packet will cause a buffer overflow, switch to -ni mode!!!
256 mp_tmsg(MSGT_DEMUX
,MSGL_WARN
,"\nBadly interleaved AVI file detected - switching to -ni mode...\n");
257 if(priv
->idx_size
>0){
259 demux
->type
=DEMUXER_TYPE_AVI_NI
;
260 demux
->desc
=&demuxer_desc_avi_ni
;
261 --priv
->idx_pos
; // hack
264 demux
->type
=DEMUXER_TYPE_AVI_NINI
;
265 demux
->desc
=&demuxer_desc_avi_nini
;
266 priv
->idx_pos
=demux
->filepos
; // hack
268 priv
->idx_pos_v
=priv
->idx_pos_a
=priv
->idx_pos
;
269 // quit now, we can't even (no enough buffer memory) read this packet :(
273 ret
=demux_avi_read_packet(demux
,ds
,id
,len
,priv
->idx_pos
-1,flags
);
280 // 0 = EOF or no stream found
281 // 1 = successfully read a packet
282 static int demux_avi_fill_buffer_ni(demuxer_t
*demux
,demux_stream_t
* ds
){
283 avi_priv_t
*priv
=demux
->priv
;
290 AVIINDEXENTRY
*idx
=NULL
;
292 demux
->filepos
=stream_tell(demux
->stream
);
294 if(ds
==demux
->video
) idx_pos
=priv
->idx_pos_v
++; else
295 if(ds
==demux
->audio
) idx_pos
=priv
->idx_pos_a
++; else
296 idx_pos
=priv
->idx_pos
++;
298 if(priv
->idx_size
>0 && idx_pos
<priv
->idx_size
){
300 idx
=&((AVIINDEXENTRY
*)priv
->idx
)[idx_pos
];
302 if(idx
->dwFlags
&AVIIF_LIST
){
306 if(ds
&& demux_avi_select_stream(demux
,idx
->ckid
)!=ds
){
307 mp_dbg(MSGT_DEMUX
,MSGL_DBG3
,"Skip chunk %.4s (0x%X) \n",(char *)&idx
->ckid
,(unsigned int)idx
->ckid
);
308 continue; // skip this chunk
311 pos
= priv
->idx_offset
+AVI_IDX_OFFSET(idx
);
312 if((pos
<demux
->movi_start
|| pos
>=demux
->movi_end
) && (demux
->movi_end
>demux
->movi_start
)){
313 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkOffset out of range! current=0x%"PRIX64
" idx=0x%"PRIX64
" \n",(int64_t)demux
->filepos
,(int64_t)pos
);
316 stream_seek(demux
->stream
,pos
);
318 id
=stream_read_dword_le(demux
->stream
);
320 if(stream_eof(demux
->stream
)) return 0;
323 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id
,(char *)&idx
->ckid
);
324 if(valid_fourcc(idx
->ckid
))
325 id
=idx
->ckid
; // use index if valid
327 if(!valid_fourcc(id
)) continue; // drop chunk if both id and idx bad
329 len
=stream_read_dword_le(demux
->stream
);
330 if((len
!=idx
->dwChunkLength
)&&((len
+1)!=idx
->dwChunkLength
)){
331 mp_msg(MSGT_DEMUX
,MSGL_V
,"ChunkSize mismatch! raw=%d idx=%d \n",len
,idx
->dwChunkLength
);
332 if(len
>0x200000 && idx
->dwChunkLength
>0x200000) continue; // both values bad :(
333 len
=choose_chunk_len(idx
->dwChunkLength
,len
);
335 if(!(idx
->dwFlags
&AVIIF_KEYFRAME
)) flags
=0;
337 ret
=demux_avi_read_packet(demux
,demux_avi_select_stream(demux
,id
),id
,len
,idx_pos
,flags
);
344 // 0 = EOF or no stream found
345 // 1 = successfully read a packet
346 static int demux_avi_fill_buffer_nini(demuxer_t
*demux
,demux_stream_t
* ds
){
347 avi_priv_t
*priv
=demux
->priv
;
353 if(ds
==demux
->video
) fpos
=&priv
->idx_pos_v
; else
354 if(ds
==demux
->audio
) fpos
=&priv
->idx_pos_a
; else
357 stream_seek(demux
->stream
,fpos
[0]);
361 demux
->filepos
=stream_tell(demux
->stream
);
362 if(demux
->filepos
>=demux
->movi_end
&& (demux
->movi_end
>demux
->movi_start
)){
367 id
=avi_find_id(demux
->stream
);
368 len
=stream_read_dword_le(demux
->stream
);
370 if(stream_eof(demux
->stream
)) return 0;
372 if(id
==mmioFOURCC('L','I','S','T')){
373 id
=stream_read_dword_le(demux
->stream
); // list type
377 if(id
==mmioFOURCC('R','I','F','F')){
378 mp_msg(MSGT_DEMUX
,MSGL_V
,"additional RIFF header...\n");
379 id
=stream_read_dword_le(demux
->stream
); // "AVIX"
383 if(ds
==demux_avi_select_stream(demux
,id
)){
385 ret
=demux_avi_read_packet(demux
,ds
,id
,len
,priv
->idx_pos
-1,0);
388 int skip
=(len
+1)&(~1); // total bytes in this chunk
389 stream_skip(demux
->stream
,skip
);
393 fpos
[0]=stream_tell(demux
->stream
);
397 // AVI demuxer parameters:
398 int index_mode
=-1; // -1=untouched 0=don't use index 1=use (generate) index
399 char *index_file_save
= NULL
, *index_file_load
= NULL
;
400 int force_ni
=0; // force non-interleaved AVI parsing
402 void read_avi_header(demuxer_t
*demuxer
,int index_mode
);
404 static demuxer_t
* demux_open_avi(demuxer_t
* demuxer
){
405 demux_stream_t
*d_audio
=demuxer
->audio
;
406 demux_stream_t
*d_video
=demuxer
->video
;
407 sh_audio_t
*sh_audio
=NULL
;
408 sh_video_t
*sh_video
=NULL
;
409 avi_priv_t
* priv
=malloc(sizeof(avi_priv_t
));
412 priv
->avi_audio_pts
=priv
->avi_video_pts
=0.0f
;
413 priv
->pts_correction
=0.0f
;
414 priv
->skip_video_frames
=0;
415 priv
->pts_corr_bytes
=0;
416 priv
->pts_has_video
=priv
->pts_corrected
=0;
417 priv
->video_pack_no
=0;
418 priv
->audio_block_no
=0;
419 priv
->audio_block_size
=0;
421 priv
->suidx_size
= 0;
424 demuxer
->priv
=(void*)priv
;
427 read_avi_header(demuxer
,(demuxer
->stream
->flags
& STREAM_SEEK_BW
)?index_mode
:-2);
429 if(demuxer
->audio
->id
>=0 && !demuxer
->a_streams
[demuxer
->audio
->id
]){
430 mp_tmsg(MSGT_DEMUX
,MSGL_WARN
,"AVI: invalid audio stream ID: %d - ignoring (nosound)\n",demuxer
->audio
->id
);
431 demuxer
->audio
->id
=-2; // disabled
433 if(demuxer
->video
->id
>=0 && !demuxer
->v_streams
[demuxer
->video
->id
]){
434 mp_tmsg(MSGT_DEMUX
,MSGL_WARN
,"AVI: invalid video stream ID: %d - ignoring (using default)\n",demuxer
->video
->id
);
435 demuxer
->video
->id
=-1; // autodetect
438 stream_reset(demuxer
->stream
);
439 stream_seek(demuxer
->stream
,demuxer
->movi_start
);
443 if(priv
->idx_size
>1){
444 // decide index format:
446 if((AVI_IDX_OFFSET(&((AVIINDEXENTRY
*)priv
->idx
)[0])<demuxer
->movi_start
||
447 AVI_IDX_OFFSET(&((AVIINDEXENTRY
*)priv
->idx
)[1])<demuxer
->movi_start
)&& !priv
->isodml
)
448 priv
->idx_offset
=demuxer
->movi_start
-4;
452 if(AVI_IDX_OFFSET(&((AVIINDEXENTRY
*)priv
->idx
)[0])<demuxer
->movi_start
)
453 priv
->idx_offset
=demuxer
->movi_start
-4;
457 mp_msg(MSGT_DEMUX
,MSGL_V
,"AVI index offset: 0x%X (movi=0x%X idx0=0x%X idx1=0x%X)\n",
458 (int)priv
->idx_offset
,(int)demuxer
->movi_start
,
459 (int)((AVIINDEXENTRY
*)priv
->idx
)[0].dwChunkOffset
,
460 (int)((AVIINDEXENTRY
*)priv
->idx
)[1].dwChunkOffset
);
463 if(priv
->idx_size
>0){
464 // check that file is non-interleaved:
468 for(i
=0;i
<priv
->idx_size
;i
++){
469 AVIINDEXENTRY
* idx
=&((AVIINDEXENTRY
*)priv
->idx
)[i
];
470 demux_stream_t
* ds
=demux_avi_select_stream(demuxer
,idx
->ckid
);
471 off_t pos
= priv
->idx_offset
+ AVI_IDX_OFFSET(idx
);
472 if(a_pos
==-1 && ds
==demuxer
->audio
){
476 if(v_pos
==-1 && ds
==demuxer
->video
){
482 mp_tmsg(MSGT_DEMUX
,MSGL_ERR
,"AVI_NI: " "No video stream found.\n");
486 d_audio
->sh
=sh_audio
=NULL
;
488 if(force_ni
|| abs(a_pos
-v_pos
)>0x100000){ // distance > 1MB
489 mp_tmsg(MSGT_DEMUX
,MSGL_INFO
,"%s NON-INTERLEAVED AVI file format.\n",force_ni
?"Forced":"Detected");
490 demuxer
->type
=DEMUXER_TYPE_AVI_NI
; // HACK!!!!
491 demuxer
->desc
=&demuxer_desc_avi_ni
; // HACK!!!!
492 pts_from_bps
=1; // force BPS sync!
498 mp_tmsg(MSGT_DEMUX
,MSGL_INFO
,"Using NON-INTERLEAVED broken AVI file format.\n");
499 demuxer
->type
=DEMUXER_TYPE_AVI_NINI
; // HACK!!!!
500 demuxer
->desc
=&demuxer_desc_avi_nini
; // HACK!!!!
502 priv
->idx_pos_v
=demuxer
->movi_start
;
503 pts_from_bps
=1; // force BPS sync!
507 if(!ds_fill_buffer(d_video
)){
508 mp_tmsg(MSGT_DEMUX
,MSGL_ERR
,"AVI: " "Missing video stream!? Contact the author, it may be a bug :(\n");
511 sh_video
=d_video
->sh
;sh_video
->ds
=d_video
;
513 mp_msg(MSGT_DEMUX
,MSGL_V
,"AVI: Searching for audio stream (id:%d)\n",d_audio
->id
);
514 if(!priv
->audio_streams
|| !ds_fill_buffer(d_audio
)){
515 mp_tmsg(MSGT_DEMUX
,MSGL_INFO
,"AVI: " "No audio stream found -> no sound.\n");
516 d_audio
->sh
=sh_audio
=NULL
;
518 sh_audio
=d_audio
->sh
;sh_audio
->ds
=d_audio
;
522 // calculating audio/video bitrate:
523 if(priv
->idx_size
>0){
524 // we have index, let's count 'em!
530 for(i
=0;i
<priv
->idx_size
;i
++){
531 int id
=avi_stream_id(((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
);
532 int len
=((AVIINDEXENTRY
*)priv
->idx
)[i
].dwChunkLength
;
533 if(sh_video
->ds
->id
== id
) {
537 else if(sh_audio
&& sh_audio
->ds
->id
== id
) {
539 asamples
+=(len
+priv
->audio_block_size
-1)/priv
->audio_block_size
;
542 mp_msg(MSGT_DEMUX
,MSGL_V
,"AVI video size=%"PRId64
" (%u) audio size=%"PRId64
" (%u)\n",vsize
,vsamples
,asize
,asamples
);
543 priv
->numberofframes
=vsamples
;
544 sh_video
->i_bps
=((float)vsize
/(float)vsamples
)*(float)sh_video
->video
.dwRate
/(float)sh_video
->video
.dwScale
;
545 if(sh_audio
) sh_audio
->i_bps
=((float)asize
/(float)asamples
)*(float)sh_audio
->audio
.dwRate
/(float)sh_audio
->audio
.dwScale
;
547 // guessing, results may be inaccurate:
551 if((priv
->numberofframes
=sh_video
->video
.dwLength
)<=1)
552 // bad video header, try to get number of frames from audio
553 if(sh_audio
&& sh_audio
->wf
->nAvgBytesPerSec
) priv
->numberofframes
=sh_video
->fps
*sh_audio
->audio
.dwLength
/sh_audio
->audio
.dwRate
*sh_audio
->audio
.dwScale
;
554 if(priv
->numberofframes
<=1){
555 mp_tmsg(MSGT_SEEK
,MSGL_WARN
,"Could not determine number of frames (for absolute seek).\n");
556 priv
->numberofframes
=0;
560 if(sh_audio
->wf
->nAvgBytesPerSec
&& sh_audio
->audio
.dwSampleSize
!=1){
561 asize
=(float)sh_audio
->wf
->nAvgBytesPerSec
*sh_audio
->audio
.dwLength
*sh_audio
->audio
.dwScale
/sh_audio
->audio
.dwRate
;
563 asize
=sh_audio
->audio
.dwLength
;
564 sh_audio
->i_bps
=(float)asize
/(sh_video
->frametime
*priv
->numberofframes
);
567 vsize
=demuxer
->movi_end
-demuxer
->movi_start
-asize
-8*priv
->numberofframes
;
568 mp_msg(MSGT_DEMUX
,MSGL_V
,"AVI video size=%"PRId64
" (%u) audio size=%"PRId64
"\n",vsize
,priv
->numberofframes
,asize
);
569 sh_video
->i_bps
=(float)vsize
/(sh_video
->frametime
*priv
->numberofframes
);
577 static void demux_seek_avi(demuxer_t
*demuxer
,float rel_seek_secs
,float audio_delay
,int flags
){
578 avi_priv_t
*priv
=demuxer
->priv
;
579 demux_stream_t
*d_audio
=demuxer
->audio
;
580 demux_stream_t
*d_video
=demuxer
->video
;
581 sh_audio_t
*sh_audio
=d_audio
->sh
;
582 sh_video_t
*sh_video
=d_video
->sh
;
583 float skip_audio_secs
=0;
585 //FIXME: OFF_T - Didn't check AVI case yet (avi files can't be >2G anyway?)
586 //================= seek in AVI ==========================
587 int rel_seek_frames
=rel_seek_secs
*sh_video
->fps
;
588 int video_chunk_pos
=d_video
->pos
;
591 if(flags
&SEEK_ABSOLUTE
){
596 if(flags
&SEEK_FACTOR
){
597 rel_seek_frames
=rel_seek_secs
*priv
->numberofframes
;
600 priv
->skip_video_frames
=0;
601 priv
->avi_audio_pts
=0;
603 // ------------ STEP 1: find nearest video keyframe chunk ------------
604 // find nearest video keyframe chunk pos:
605 if(rel_seek_frames
>0){
607 while(video_chunk_pos
<priv
->idx_size
-1){
608 int id
=((AVIINDEXENTRY
*)priv
->idx
)[video_chunk_pos
].ckid
;
609 if(avi_stream_id(id
)==d_video
->id
){ // video frame
610 if((--rel_seek_frames
)<0 && ((AVIINDEXENTRY
*)priv
->idx
)[video_chunk_pos
].dwFlags
&AVIIF_KEYFRAME
) break;
616 while(video_chunk_pos
>0){
617 int id
=((AVIINDEXENTRY
*)priv
->idx
)[video_chunk_pos
].ckid
;
618 if(avi_stream_id(id
)==d_video
->id
){ // video frame
619 if((++rel_seek_frames
)>0 && ((AVIINDEXENTRY
*)priv
->idx
)[video_chunk_pos
].dwFlags
&AVIIF_KEYFRAME
) break;
624 priv
->idx_pos_a
=priv
->idx_pos_v
=priv
->idx_pos
=video_chunk_pos
;
626 // re-calc video pts:
628 for(i
=0;i
<video_chunk_pos
;i
++){
629 int id
=((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
;
630 if(avi_stream_id(id
)==d_video
->id
) ++d_video
->pack_no
;
633 sh_video
->num_frames
=sh_video
->num_frames_decoded
=d_video
->pack_no
;
634 priv
->avi_video_pts
=d_video
->pack_no
*(float)sh_video
->video
.dwScale
/(float)sh_video
->video
.dwRate
;
635 d_video
->pos
=video_chunk_pos
;
637 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
);
639 // ------------ STEP 2: seek audio, find the right chunk & pos ------------
642 priv
->audio_block_no
=0;
648 int skip_audio_bytes
=0;
649 int curr_audio_pos
=-1;
650 int audio_chunk_pos
=-1;
651 int chunk_max
=(demuxer
->type
==DEMUXER_TYPE_AVI
)?video_chunk_pos
:priv
->idx_size
;
653 if(sh_audio
->audio
.dwSampleSize
){
654 // constant rate audio stream
655 /* immediate seeking to audio position, including when streams are delayed */
656 curr_audio_pos
=(priv
->avi_video_pts
+ audio_delay
)*(float)sh_audio
->audio
.dwRate
/(float)sh_audio
->audio
.dwScale
;
657 curr_audio_pos
*=sh_audio
->audio
.dwSampleSize
;
659 // find audio chunk pos:
660 for(i
=0;i
<chunk_max
;i
++){
661 int id
=((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
;
662 if(avi_stream_id(id
)==d_audio
->id
){
663 len
=((AVIINDEXENTRY
*)priv
->idx
)[i
].dwChunkLength
;
664 if(d_audio
->dpos
<=curr_audio_pos
&& curr_audio_pos
<(d_audio
->dpos
+len
)){
668 priv
->audio_block_no
+=priv
->audio_block_size
?
669 ((len
+priv
->audio_block_size
-1)/priv
->audio_block_size
) : 1;
674 skip_audio_bytes
=curr_audio_pos
-d_audio
->dpos
;
676 mp_msg(MSGT_SEEK
,MSGL_V
,"SEEK: i=%d (max:%d) dpos=%d (wanted:%d) \n",
677 i
,chunk_max
,(int)d_audio
->dpos
,curr_audio_pos
);
681 /* immediate seeking to audio position, including when streams are delayed */
682 int chunks
=(priv
->avi_video_pts
+ audio_delay
)*(float)sh_audio
->audio
.dwRate
/(float)sh_audio
->audio
.dwScale
;
685 // find audio chunk pos:
686 for(i
=0;i
<priv
->idx_size
&& chunks
>0;i
++){
687 int id
=((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
;
688 if(avi_stream_id(id
)==d_audio
->id
){
689 len
=((AVIINDEXENTRY
*)priv
->idx
)[i
].dwChunkLength
;
691 skip_audio_bytes
+=len
;
694 priv
->audio_block_no
+=priv
->audio_block_size
?
695 ((len
+priv
->audio_block_size
-1)/priv
->audio_block_size
) : 1;
699 if(priv
->audio_block_size
)
700 chunks
-=(len
+priv
->audio_block_size
-1)/priv
->audio_block_size
;
706 // audio_chunk_pos = chunk no in index table (it's <=chunk_max)
707 // skip_audio_bytes = bytes to be skipped after chunk seek
708 // d-audio->pack_no = chunk_no in stream at audio_chunk_pos
709 // d_audio->dpos = bytepos in stream at audio_chunk_pos
712 // update stream position:
713 d_audio
->pos
=audio_chunk_pos
;
715 if(demuxer
->type
==DEMUXER_TYPE_AVI
){
716 // interleaved stream:
717 if(audio_chunk_pos
<video_chunk_pos
){
718 // calc priv->skip_video_frames & adjust video pts counter:
719 for(i
=audio_chunk_pos
;i
<video_chunk_pos
;i
++){
720 int id
=((AVIINDEXENTRY
*)priv
->idx
)[i
].ckid
;
721 if(avi_stream_id(id
)==d_video
->id
) ++priv
->skip_video_frames
;
723 // requires for correct audio pts calculation (demuxer):
724 priv
->avi_video_pts
-=priv
->skip_video_frames
*(float)sh_video
->video
.dwScale
/(float)sh_video
->video
.dwRate
;
725 priv
->avi_audio_pts
=priv
->avi_video_pts
;
726 // set index position:
727 priv
->idx_pos_a
=priv
->idx_pos_v
=priv
->idx_pos
=audio_chunk_pos
;
730 // non-interleaved stream:
731 priv
->idx_pos_a
=audio_chunk_pos
;
732 priv
->idx_pos_v
=video_chunk_pos
;
733 priv
->idx_pos
=(audio_chunk_pos
<video_chunk_pos
)?audio_chunk_pos
:video_chunk_pos
;
736 mp_msg(MSGT_SEEK
,MSGL_V
,"SEEK: idx=%d (a:%d v:%d) v.skip=%d a.skip=%d/%4.3f \n",
737 (int)priv
->idx_pos
,audio_chunk_pos
,video_chunk_pos
,
738 (int)priv
->skip_video_frames
,skip_audio_bytes
,skip_audio_secs
);
740 if(skip_audio_bytes
){
741 demux_read_data(d_audio
,NULL
,skip_audio_bytes
);
745 d_video
->pts
=priv
->avi_video_pts
; // OSD
750 static void demux_close_avi(demuxer_t
*demuxer
) {
751 avi_priv_t
* priv
=demuxer
->priv
;
756 if(priv
->idx_size
> 0)
762 static int demux_avi_control(demuxer_t
*demuxer
,int cmd
, void *arg
){
763 avi_priv_t
*priv
=demuxer
->priv
;
764 demux_stream_t
*d_video
=demuxer
->video
;
765 sh_video_t
*sh_video
=d_video
->sh
;
768 case DEMUXER_CTRL_GET_TIME_LENGTH
:
769 if (!priv
->numberofframes
|| !sh_video
) return DEMUXER_CTRL_DONTKNOW
;
770 *((double *)arg
)=(double)priv
->numberofframes
/sh_video
->fps
;
771 if (sh_video
->video
.dwLength
<=1) return DEMUXER_CTRL_GUESS
;
772 return DEMUXER_CTRL_OK
;
774 case DEMUXER_CTRL_GET_PERCENT_POS
:
775 if (!priv
->numberofframes
|| !sh_video
) {
776 return DEMUXER_CTRL_DONTKNOW
;
778 *((int *)arg
)=(int)(priv
->video_pack_no
*100/priv
->numberofframes
);
779 if (sh_video
->video
.dwLength
<=1) return DEMUXER_CTRL_GUESS
;
780 return DEMUXER_CTRL_OK
;
782 case DEMUXER_CTRL_SWITCH_AUDIO
:
783 case DEMUXER_CTRL_SWITCH_VIDEO
: {
784 int audio
= (cmd
== DEMUXER_CTRL_SWITCH_AUDIO
);
785 demux_stream_t
*ds
= audio
? demuxer
->audio
: demuxer
->video
;
786 void **streams
= audio
? demuxer
->a_streams
: demuxer
->v_streams
;
787 int maxid
= FFMIN(100, audio
? MAX_A_STREAMS
: MAX_V_STREAMS
);
790 return DEMUXER_CTRL_NOTIMPL
;
792 if (*(int *)arg
>= 0)
793 ds
->id
= *(int *)arg
;
796 for (i
= 0; i
< maxid
; i
++) {
797 if (++ds
->id
>= maxid
) ds
->id
= 0;
798 if (streams
[ds
->id
]) break;
802 chunkid
= (ds
->id
/ 10 + '0') | (ds
->id
% 10 + '0') << 8;
804 if (!streams
[ds
->id
]) // stream not available
807 demux_avi_select_stream(demuxer
, chunkid
);
808 *(int *)arg
= ds
->id
;
809 return DEMUXER_CTRL_OK
;
813 return DEMUXER_CTRL_NOTIMPL
;
818 static int avi_check_file(demuxer_t
*demuxer
)
820 int id
=stream_read_dword_le(demuxer
->stream
); // "RIFF"
822 if((id
==mmioFOURCC('R','I','F','F')) || (id
==mmioFOURCC('O','N','2',' '))) {
823 stream_read_dword_le(demuxer
->stream
); //filesize
824 id
=stream_read_dword_le(demuxer
->stream
); // "AVI "
826 return DEMUXER_TYPE_AVI
;
827 // "Samsung Digimax i6 PMP" crap according to bug 742
828 if(id
==mmioFOURCC('A','V','I',0x19))
829 return DEMUXER_TYPE_AVI
;
830 if(id
==mmioFOURCC('O','N','2','f')){
831 mp_tmsg(MSGT_DEMUXER
,MSGL_INFO
,"ON2 AVI format");
832 return DEMUXER_TYPE_AVI
;
840 static demuxer_t
* demux_open_hack_avi(demuxer_t
*demuxer
)
842 struct MPOpts
*opts
= demuxer
->opts
;
845 demuxer
= demux_open_avi(demuxer
);
846 if(!demuxer
) return NULL
; // failed to open
847 sh_a
= demuxer
->audio
->sh
;
848 if(demuxer
->audio
->id
!= -2 && sh_a
) {
849 #ifdef CONFIG_OGGVORBIS
850 // support for Ogg-in-AVI:
851 if(sh_a
->format
== 0xFFFE)
852 demuxer
= init_avi_with_ogg(demuxer
);
853 else if(sh_a
->format
== 0x674F) {
856 s
= new_ds_stream(demuxer
->audio
);
857 od
= new_demuxer(opts
, s
,DEMUXER_TYPE_OGG
,-1,-2,-2,NULL
);
858 if(!demux_ogg_open(od
)) {
859 mp_tmsg( MSGT_DEMUXER
,MSGL_ERR
,"Unable to open the Ogg demuxer.\n");
861 demuxer
->audio
->id
= -2;
863 demuxer
= new_demuxers_demuxer(demuxer
,od
,demuxer
);
872 const demuxer_desc_t demuxer_desc_avi
= {
877 "AVI files, including non interleaved files",
879 1, // safe autodetect
881 demux_avi_fill_buffer
,
888 const demuxer_desc_t demuxer_desc_avi_ni
= {
889 "AVI demuxer, non-interleaved",
893 "AVI files, including non interleaved files",
895 1, // safe autodetect
897 demux_avi_fill_buffer_ni
,
904 const demuxer_desc_t demuxer_desc_avi_nini
= {
905 "AVI demuxer, non-interleaved and no index",
909 "AVI files, including non interleaved files",
911 1, // safe autodetect
913 demux_avi_fill_buffer_nini
,