1 //=================== DEMUXER v2.5 =========================
16 #include "libvo/fastmemcpy.h"
18 #include "stream/stream.h"
23 #include "libaf/af_format.h"
25 extern void resync_video_stream(sh_video_t
*sh_video
);
26 extern void resync_audio_stream(sh_audio_t
*sh_audio
);
29 extern demuxer_desc_t demuxer_desc_rawaudio
;
30 extern demuxer_desc_t demuxer_desc_rawvideo
;
31 extern demuxer_desc_t demuxer_desc_tv
;
32 extern demuxer_desc_t demuxer_desc_mf
;
33 extern demuxer_desc_t demuxer_desc_avi
;
34 extern demuxer_desc_t demuxer_desc_y4m
;
35 extern demuxer_desc_t demuxer_desc_asf
;
36 extern demuxer_desc_t demuxer_desc_nuv
;
37 extern demuxer_desc_t demuxer_desc_real
;
38 extern demuxer_desc_t demuxer_desc_smjpeg
;
39 extern demuxer_desc_t demuxer_desc_matroska
;
40 extern demuxer_desc_t demuxer_desc_realaudio
;
41 extern demuxer_desc_t demuxer_desc_vqf
;
42 extern demuxer_desc_t demuxer_desc_mov
;
43 extern demuxer_desc_t demuxer_desc_vivo
;
44 extern demuxer_desc_t demuxer_desc_fli
;
45 extern demuxer_desc_t demuxer_desc_film
;
46 extern demuxer_desc_t demuxer_desc_roq
;
47 extern demuxer_desc_t demuxer_desc_gif
;
48 extern demuxer_desc_t demuxer_desc_ogg
;
49 extern demuxer_desc_t demuxer_desc_avs
;
50 extern demuxer_desc_t demuxer_desc_pva
;
51 extern demuxer_desc_t demuxer_desc_nsv
;
52 extern demuxer_desc_t demuxer_desc_mpeg_ts
;
53 extern demuxer_desc_t demuxer_desc_lmlm4
;
54 extern demuxer_desc_t demuxer_desc_mpeg_ps
;
55 extern demuxer_desc_t demuxer_desc_mpeg_pes
;
56 extern demuxer_desc_t demuxer_desc_mpeg_es
;
57 extern demuxer_desc_t demuxer_desc_mpeg_gxf
;
58 extern demuxer_desc_t demuxer_desc_mpeg4_es
;
59 extern demuxer_desc_t demuxer_desc_h264_es
;
60 extern demuxer_desc_t demuxer_desc_rawdv
;
61 extern demuxer_desc_t demuxer_desc_mpc
;
62 extern demuxer_desc_t demuxer_desc_audio
;
63 extern demuxer_desc_t demuxer_desc_xmms
;
64 extern demuxer_desc_t demuxer_desc_mpeg_ty
;
65 extern demuxer_desc_t demuxer_desc_rtp
;
66 extern demuxer_desc_t demuxer_desc_lavf
;
67 extern demuxer_desc_t demuxer_desc_lavf_preferred
;
68 extern demuxer_desc_t demuxer_desc_aac
;
69 extern demuxer_desc_t demuxer_desc_nut
;
71 demuxer_desc_t
* demuxer_list
[] = {
72 &demuxer_desc_rawaudio
,
73 &demuxer_desc_rawvideo
,
78 #ifdef USE_LIBAVFORMAT
79 &demuxer_desc_lavf_preferred
,
88 &demuxer_desc_matroska
,
89 &demuxer_desc_realaudio
,
106 &demuxer_desc_mpeg_ts
,
108 &demuxer_desc_mpeg_ps
,
109 &demuxer_desc_mpeg_pes
,
110 &demuxer_desc_mpeg_es
,
111 &demuxer_desc_mpeg_gxf
,
112 &demuxer_desc_mpeg4_es
,
113 &demuxer_desc_h264_es
,
118 &demuxer_desc_mpeg_ty
,
119 #ifdef STREAMING_LIVE555
122 #ifdef USE_LIBAVFORMAT
138 void free_demuxer_stream(demux_stream_t
*ds
){
143 demux_stream_t
* new_demuxer_stream(struct demuxer_st
*demuxer
,int id
){
144 demux_stream_t
* ds
=malloc(sizeof(demux_stream_t
));
145 ds
->buffer_pos
=ds
->buffer_size
=0;
156 ds
->first
=ds
->last
=ds
->current
=NULL
;
163 ds
->ss_mul
=ds
->ss_div
=0;
171 * Get demuxer description structure for a given demuxer type
173 * @param file_format type of the demuxer
174 * @return structure for the demuxer, NULL if not found
176 static demuxer_desc_t
* get_demuxer_desc_from_type(int file_format
)
180 for (i
= 0; demuxer_list
[i
]; i
++)
181 if (file_format
== demuxer_list
[i
]->type
)
182 return demuxer_list
[i
];
188 demuxer_t
* new_demuxer(stream_t
*stream
,int type
,int a_id
,int v_id
,int s_id
,char *filename
){
189 demuxer_t
*d
=malloc(sizeof(demuxer_t
));
190 memset(d
,0,sizeof(demuxer_t
));
192 d
->stream_pts
= MP_NOPTS_VALUE
;
193 d
->movi_start
=stream
->start_pos
;
194 d
->movi_end
=stream
->end_pos
;
198 d
->audio
=new_demuxer_stream(d
,a_id
);
199 d
->video
=new_demuxer_stream(d
,v_id
);
200 d
->sub
=new_demuxer_stream(d
,s_id
);
203 if (!(d
->desc
= get_demuxer_desc_from_type(type
)))
204 mp_msg(MSGT_DEMUXER
,MSGL_ERR
,"BUG! Invalid demuxer type in new_demuxer(), big troubles ahead.");
205 if(filename
) // Filename hack for avs_check_file
206 d
->filename
=strdup(filename
);
207 stream_reset(stream
);
208 stream_seek(stream
,stream
->start_pos
);
212 sh_sub_t
*new_sh_sub_sid(demuxer_t
*demuxer
, int id
, int sid
) {
213 if (id
> MAX_S_STREAMS
- 1 || id
< 0) {
214 mp_msg(MSGT_DEMUXER
,MSGL_WARN
,"Requested sub stream id overflow (%d > %d)\n",
218 if (demuxer
->s_streams
[id
])
219 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, "Sub stream %i redefined\n", id
);
221 sh_sub_t
*sh
= calloc(1, sizeof(sh_sub_t
));
222 demuxer
->s_streams
[id
] = sh
;
224 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SUBTITLE_ID=%d\n", sid
);
226 return demuxer
->s_streams
[id
];
229 void free_sh_sub(sh_sub_t
*sh
) {
230 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_sub at %p\n", sh
);
234 sh_audio_t
* new_sh_audio_aid(demuxer_t
*demuxer
,int id
,int aid
){
235 if(id
> MAX_A_STREAMS
-1 || id
< 0)
237 mp_msg(MSGT_DEMUXER
,MSGL_WARN
,"Requested audio stream id overflow (%d > %d)\n",
241 if(demuxer
->a_streams
[id
]){
242 mp_msg(MSGT_DEMUXER
,MSGL_WARN
,MSGTR_AudioStreamRedefined
,id
);
245 mp_msg(MSGT_DEMUXER
,MSGL_V
,MSGTR_FoundAudioStream
,id
);
246 demuxer
->a_streams
[id
]=calloc(1, sizeof(sh_audio_t
));
247 sh
= demuxer
->a_streams
[id
];
250 sh
->sample_format
=AF_FORMAT_S16_NE
;
251 sh
->audio_out_minsize
=8192;/* default size, maybe not enough for Win32/ACM*/
252 sh
->pts
=MP_NOPTS_VALUE
;
253 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_ID=%d\n", aid
);
255 ((sh_audio_t
*)demuxer
->a_streams
[id
])->aid
= aid
;
256 return demuxer
->a_streams
[id
];
259 void free_sh_audio(demuxer_t
*demuxer
, int id
) {
260 sh_audio_t
*sh
= demuxer
->a_streams
[id
];
261 demuxer
->a_streams
[id
] = NULL
;
262 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
,"DEMUXER: freeing sh_audio at %p\n",sh
);
263 if(sh
->wf
) free(sh
->wf
);
267 sh_video_t
* new_sh_video_vid(demuxer_t
*demuxer
,int id
,int vid
){
268 if(id
> MAX_V_STREAMS
-1 || id
< 0)
270 mp_msg(MSGT_DEMUXER
,MSGL_WARN
,"Requested video stream id overflow (%d > %d)\n",
274 if(demuxer
->v_streams
[id
]){
275 mp_msg(MSGT_DEMUXER
,MSGL_WARN
,MSGTR_VideoStreamRedefined
,id
);
277 mp_msg(MSGT_DEMUXER
,MSGL_V
,MSGTR_FoundVideoStream
,id
);
278 demuxer
->v_streams
[id
]=calloc(1, sizeof(sh_video_t
));
279 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_ID=%d\n", vid
);
281 ((sh_video_t
*)demuxer
->v_streams
[id
])->vid
= vid
;
282 return demuxer
->v_streams
[id
];
285 void free_sh_video(sh_video_t
* sh
){
286 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
,"DEMUXER: freeing sh_video at %p\n",sh
);
287 if(sh
->bih
) free(sh
->bih
);
291 void free_demuxer(demuxer_t
*demuxer
){
293 mp_msg(MSGT_DEMUXER
,MSGL_DBG2
,"DEMUXER: freeing demuxer at %p\n",demuxer
);
294 if(demuxer
->desc
->close
)
295 demuxer
->desc
->close(demuxer
);
296 // Very ugly hack to make it behave like old implementation
297 if (demuxer
->desc
->type
== DEMUXER_TYPE_DEMUXERS
)
298 goto skip_streamfree
;
300 for(i
= 0; i
< MAX_A_STREAMS
; i
++)
301 if(demuxer
->a_streams
[i
]) free_sh_audio(demuxer
, i
);
302 for(i
= 0; i
< MAX_V_STREAMS
; i
++)
303 if(demuxer
->v_streams
[i
]) free_sh_video(demuxer
->v_streams
[i
]);
304 for(i
= 0; i
< MAX_S_STREAMS
; i
++)
305 if(demuxer
->s_streams
[i
]) free_sh_sub(demuxer
->s_streams
[i
]);
307 free_demuxer_stream(demuxer
->audio
);
308 free_demuxer_stream(demuxer
->video
);
309 free_demuxer_stream(demuxer
->sub
);
312 for(i
=0;demuxer
->info
[i
] != NULL
; i
++)
313 free(demuxer
->info
[i
]);
316 if(demuxer
->filename
)
317 free(demuxer
->filename
);
318 if (demuxer
->chapters
) {
319 for (i
=0; i
<demuxer
->num_chapters
; i
++)
320 if (demuxer
->chapters
[i
].name
)
321 free(demuxer
->chapters
[i
].name
);
322 free(demuxer
->chapters
);
328 void ds_add_packet(demux_stream_t
*ds
,demux_packet_t
* dp
){
329 // demux_packet_t* dp=new_demux_packet(len);
330 // stream_read(stream,dp->buffer,len);
331 // dp->pts=pts; //(float)pts/90000.0f;
333 // append packet to DS stream:
337 // next packet in stream
341 // first packet in stream
342 ds
->first
=ds
->last
=dp
;
344 mp_dbg(MSGT_DEMUXER
,MSGL_DBG2
,"DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
345 (ds
==ds
->demuxer
->audio
)?"d_audio":"d_video",
346 dp
->len
,dp
->pts
,(unsigned int)dp
->pos
,ds
->demuxer
->audio
->packs
,ds
->demuxer
->video
->packs
);
349 void ds_read_packet(demux_stream_t
*ds
, stream_t
*stream
, int len
, double pts
, off_t pos
, int flags
) {
350 demux_packet_t
* dp
=new_demux_packet(len
);
351 len
= stream_read(stream
,dp
->buffer
,len
);
352 resize_demux_packet(dp
, len
);
353 dp
->pts
=pts
; //(float)pts/90000.0f;
356 // append packet to DS stream:
357 ds_add_packet(ds
,dp
);
361 // 0 = EOF or no stream found or invalid type
362 // 1 = successfully read a packet
364 int demux_fill_buffer(demuxer_t
*demux
,demux_stream_t
*ds
){
365 // Note: parameter 'ds' can be NULL!
366 // printf("demux->type=%d\n",demux->type);
367 return demux
->desc
->fill_buffer(demux
, ds
);
373 int ds_fill_buffer(demux_stream_t
*ds
){
374 demuxer_t
*demux
=ds
->demuxer
;
375 if(ds
->current
) free_demux_packet(ds
->current
);
376 if( mp_msg_test(MSGT_DEMUXER
,MSGL_DBG3
) ){
377 if(ds
==demux
->audio
) mp_dbg(MSGT_DEMUXER
,MSGL_DBG3
,"ds_fill_buffer(d_audio) called\n");else
378 if(ds
==demux
->video
) mp_dbg(MSGT_DEMUXER
,MSGL_DBG3
,"ds_fill_buffer(d_video) called\n");else
379 if(ds
==demux
->sub
) mp_dbg(MSGT_DEMUXER
,MSGL_DBG3
,"ds_fill_buffer(d_sub) called\n");else
380 mp_dbg(MSGT_DEMUXER
,MSGL_DBG3
,"ds_fill_buffer(unknown 0x%X) called\n",(unsigned int)ds
);
384 demux_packet_t
*p
=ds
->first
;
386 ds
->buffer
=p
->buffer
;
388 ds
->buffer_size
=p
->len
;
390 ds
->dpos
+=p
->len
; // !!!
392 if (p
->pts
!= (correct_pts
? MP_NOPTS_VALUE
: 0)) {
396 ds
->pts_bytes
+=p
->len
; // !!!
397 if(p
->stream_pts
!= MP_NOPTS_VALUE
) demux
->stream_pts
=p
->stream_pts
;
403 if(!ds
->first
) ds
->last
=NULL
;
405 return 1; //ds->buffer_size;
407 if(demux
->audio
->packs
>=MAX_PACKS
|| demux
->audio
->bytes
>=MAX_PACK_BYTES
){
408 mp_msg(MSGT_DEMUXER
,MSGL_ERR
,MSGTR_TooManyAudioInBuffer
,demux
->audio
->packs
,demux
->audio
->bytes
);
409 mp_msg(MSGT_DEMUXER
,MSGL_HINT
,MSGTR_MaybeNI
);
412 if(demux
->video
->packs
>=MAX_PACKS
|| demux
->video
->bytes
>=MAX_PACK_BYTES
){
413 mp_msg(MSGT_DEMUXER
,MSGL_ERR
,MSGTR_TooManyVideoInBuffer
,demux
->video
->packs
,demux
->video
->bytes
);
414 mp_msg(MSGT_DEMUXER
,MSGL_HINT
,MSGTR_MaybeNI
);
417 if(!demux_fill_buffer(demux
,ds
)){
418 mp_dbg(MSGT_DEMUXER
,MSGL_DBG2
,"ds_fill_buffer()->demux_fill_buffer() failed\n");
422 ds
->buffer_pos
=ds
->buffer_size
=0;
425 mp_msg(MSGT_DEMUXER
,MSGL_V
,"ds_fill_buffer: EOF reached (stream: %s) \n",ds
==demux
->audio
?"audio":"video");
430 int demux_read_data(demux_stream_t
*ds
,unsigned char* mem
,int len
){
434 x
=ds
->buffer_size
-ds
->buffer_pos
;
436 if(!ds_fill_buffer(ds
)) return bytes
;
439 if(mem
) fast_memcpy(mem
+bytes
,&ds
->buffer
[ds
->buffer_pos
],x
);
440 bytes
+=x
;len
-=x
;ds
->buffer_pos
+=x
;
446 int demux_read_data_pack(demux_stream_t
*ds
,unsigned char* mem
,int len
){
450 x
=ds
->buffer_size
-ds
->buffer_pos
;
452 if(!ds_fill_buffer(ds
)) return bytes
;
455 if(mem
) fast_memcpy(mem
+bytes
,&ds
->buffer
[ds
->buffer_pos
],x
);
456 bytes
+=x
;len
-=x
;ds
->buffer_pos
+=x
;
457 return bytes
; // stop at end of package! (for correct timestamping)
464 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
465 * \param mem memory to read data into, may be NULL to discard data
466 * \param maxlen maximum number of bytes to read
467 * \param read number of bytes actually read
468 * \param pattern pattern to search for (lowest 8 bits are ignored)
469 * \return whether pattern was found
471 int demux_pattern_3(demux_stream_t
*ds
, unsigned char *mem
, int maxlen
,
472 int *read
, uint32_t pattern
) {
473 register uint32_t head
= 0xffffff00;
474 register uint32_t pat
= pattern
& 0xffffff00;
477 register unsigned char *ds_buf
= &ds
->buffer
[ds
->buffer_size
];
478 int len
= ds
->buffer_size
- ds
->buffer_pos
;
479 register long pos
= -len
;
480 if (unlikely(pos
>= 0)) { // buffer is empty
487 } while (++pos
&& head
!= pat
);
489 if (total_len
+ len
> maxlen
)
490 len
= maxlen
- total_len
;
491 len
= demux_read_data(ds
, mem
? &mem
[total_len
] : NULL
, len
);
493 } while ((head
!= pat
|| total_len
< 3) && total_len
< maxlen
&& !ds
->eof
);
496 return total_len
>= 3 && head
== pat
;
499 void ds_free_packs(demux_stream_t
*ds
){
500 demux_packet_t
*dp
=ds
->first
;
502 demux_packet_t
*dn
=dp
->next
;
503 free_demux_packet(dp
);
507 // free unfinished .asf fragments:
508 free(ds
->asf_packet
->buffer
);
509 free(ds
->asf_packet
);
512 ds
->first
=ds
->last
=NULL
;
513 ds
->packs
=0; // !!!!!
515 if(ds
->current
) free_demux_packet(ds
->current
);
518 ds
->buffer_pos
=ds
->buffer_size
;
519 ds
->pts
=0; ds
->pts_bytes
=0;
522 int ds_get_packet(demux_stream_t
*ds
,unsigned char **start
){
525 if(ds
->buffer_pos
>=ds
->buffer_size
){
526 if(!ds_fill_buffer(ds
)){
532 len
=ds
->buffer_size
-ds
->buffer_pos
;
533 *start
= &ds
->buffer
[ds
->buffer_pos
];
539 int ds_get_packet_pts(demux_stream_t
*ds
,unsigned char **start
, double *pts
)
542 *pts
= MP_NOPTS_VALUE
;
543 if(ds
->buffer_pos
>=ds
->buffer_size
){
544 if (!ds_fill_buffer(ds
)) {
550 // Should use MP_NOPTS_VALUE for "unknown pts" in the packets too
551 // Return pts unless this read starts from the middle of a packet
552 if (!ds
->buffer_pos
&& (correct_pts
|| ds
->current
->pts
))
553 *pts
= ds
->current
->pts
;
554 len
=ds
->buffer_size
-ds
->buffer_pos
;
555 *start
= &ds
->buffer
[ds
->buffer_pos
];
560 int ds_get_packet_sub(demux_stream_t
*ds
,unsigned char **start
){
563 if(ds
->buffer_pos
>=ds
->buffer_size
){
565 if(!ds
->packs
) return -1; // no sub
566 if(!ds_fill_buffer(ds
)) return -1; // EOF
568 len
=ds
->buffer_size
-ds
->buffer_pos
;
569 *start
= &ds
->buffer
[ds
->buffer_pos
];
575 double ds_get_next_pts(demux_stream_t
*ds
)
577 demuxer_t
* demux
= ds
->demuxer
;
579 if(demux
->audio
->packs
>=MAX_PACKS
|| demux
->audio
->bytes
>=MAX_PACK_BYTES
){
580 mp_msg(MSGT_DEMUXER
,MSGL_ERR
,MSGTR_TooManyAudioInBuffer
,demux
->audio
->packs
,demux
->audio
->bytes
);
581 mp_msg(MSGT_DEMUXER
,MSGL_HINT
,MSGTR_MaybeNI
);
582 return MP_NOPTS_VALUE
;
584 if(demux
->video
->packs
>=MAX_PACKS
|| demux
->video
->bytes
>=MAX_PACK_BYTES
){
585 mp_msg(MSGT_DEMUXER
,MSGL_ERR
,MSGTR_TooManyVideoInBuffer
,demux
->video
->packs
,demux
->video
->bytes
);
586 mp_msg(MSGT_DEMUXER
,MSGL_HINT
,MSGTR_MaybeNI
);
587 return MP_NOPTS_VALUE
;
589 if(!demux_fill_buffer(demux
,ds
))
590 return MP_NOPTS_VALUE
;
592 return ds
->first
->pts
;
595 // ====================================================================
597 // commandline options, flags:
599 extern int pts_from_bps
;
601 //extern int audio_id;
602 //extern int video_id;
603 //extern int dvdsub_id;
605 void demuxer_help(void)
609 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "Available demuxers:\n");
610 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, " demuxer: type info: (comment)\n");
611 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DEMUXERS\n");
612 for (i
= 0; demuxer_list
[i
]; i
++) {
613 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Don't display special demuxers
615 if (demuxer_list
[i
]->comment
&& strlen(demuxer_list
[i
]->comment
))
616 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s (%s)\n",
617 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
, demuxer_list
[i
]->info
, demuxer_list
[i
]->comment
);
619 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s\n",
620 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
, demuxer_list
[i
]->info
);
626 * Get demuxer type for a given demuxer name
628 * @param demuxer_name string with demuxer name of demuxer number
629 * @param force will be set if demuxer should be forced.
631 * @return DEMUXER_TYPE_xxx, -1 if error or not found
633 int get_demuxer_type_from_name(char *demuxer_name
, int *force
)
639 if (!demuxer_name
|| !demuxer_name
[0])
640 return DEMUXER_TYPE_UNKNOWN
;
641 if (force
) *force
= demuxer_name
[0] == '+';
642 if (demuxer_name
[0] == '+')
643 demuxer_name
= &demuxer_name
[1];
644 for (i
= 0; demuxer_list
[i
]; i
++) {
645 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Can't select special demuxers from commandline
647 if (strcmp(demuxer_name
, demuxer_list
[i
]->name
) == 0)
648 return demuxer_list
[i
]->type
;
651 // No match found, try to parse name as an integer (demuxer number)
652 type_int
= strtol(demuxer_name
, &endptr
, 0);
653 if (*endptr
) // Conversion failed
655 if ((type_int
> 0) && (type_int
<= DEMUXER_TYPE_MAX
))
656 return (int)type_int
;
661 int extension_parsing
=1; // 0=off 1=mixed (used only for unstable formats)
666 NOTE : Several demuxers may be opened at the same time so
667 demuxers should NEVER rely on an external var to enable them
668 self. If a demuxer can't do any autodection it should only use
669 file_format. The user can explictly set file_format with the -demuxer
670 option so there is really no need for another extra var.
671 For conivence an option can be added to set file_format directly
672 to the right type (ex: rawaudio,rawvideo).
673 Also the stream can override the file_format so a demuxer wich rely
674 on a special stream type can set file_format at the stream level
678 static demuxer_t
* demux_open_stream(stream_t
*stream
, int file_format
,
679 int force
, int audio_id
, int video_id
, int dvdsub_id
,
682 //int file_format=(*file_format_ptr);
684 demuxer_t
*demuxer
=NULL
;
686 sh_video_t
*sh_video
=NULL
;
688 demuxer_desc_t
*demuxer_desc
;
692 //printf("demux_open(%p,%d,%d,%d,%d) \n",stream,file_format,audio_id,video_id,dvdsub_id);
694 // If somebody requested a demuxer check it
696 if ((demuxer_desc
= get_demuxer_desc_from_type(file_format
))) {
697 demuxer
= new_demuxer(stream
,demuxer_desc
->type
,audio_id
,video_id
,dvdsub_id
,filename
);
698 if (demuxer_desc
->check_file
)
699 fformat
= demuxer_desc
->check_file(demuxer
);
700 if (force
|| !demuxer_desc
->check_file
)
701 fformat
= demuxer_desc
->type
;
703 if (fformat
== demuxer_desc
->type
) {
704 demuxer_t
*demux2
= demuxer
;
705 // Move messages to demuxer detection code?
706 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, MSGTR_Detected_XXX_FileFormat
, demuxer_desc
->shortdesc
);
707 file_format
= demuxer_desc
->type
= fformat
;
708 if (!demuxer
->desc
->open
|| (demux2
= demuxer
->desc
->open(demuxer
))) {
713 // Format changed after check, recurse
714 free_demuxer(demuxer
);
715 return demux_open_stream(stream
, fformat
, force
,
716 audio_id
, video_id
, dvdsub_id
, filename
);
719 // Check failed for forced demuxer, quit
720 free_demuxer(demuxer
);
725 // Test demuxers with safe file checks
726 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
727 if (demuxer_desc
->safe_check
) {
728 demuxer
= new_demuxer(stream
,demuxer_desc
->type
,audio_id
,video_id
,dvdsub_id
,filename
);
729 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
730 if (fformat
== demuxer_desc
->type
) {
731 demuxer_t
*demux2
= demuxer
;
732 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, MSGTR_Detected_XXX_FileFormat
, demuxer_desc
->shortdesc
);
733 file_format
= fformat
;
734 if (!demuxer
->desc
->open
|| (demux2
= demuxer
->desc
->open(demuxer
))) {
739 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
740 return demuxer
; // handled in mplayer.c
741 // Format changed after check, recurse
742 free_demuxer(demuxer
);
743 demuxer
=demux_open_stream(stream
, fformat
, force
,
744 audio_id
, video_id
, dvdsub_id
, filename
);
745 if(demuxer
) return demuxer
; // done!
746 file_format
= DEMUXER_TYPE_UNKNOWN
;
749 free_demuxer(demuxer
);
754 // If no forced demuxer perform file extension based detection
755 // Ok. We're over the stable detectable fileformats, the next ones are a bit
756 // fuzzy. So by default (extension_parsing==1) try extension-based detection
758 if(file_format
==DEMUXER_TYPE_UNKNOWN
&& filename
&& extension_parsing
==1){
759 file_format
=demuxer_type_by_filename(filename
);
760 if(file_format
!=DEMUXER_TYPE_UNKNOWN
){
761 // we like recursion :)
762 demuxer
=demux_open_stream(stream
, file_format
, force
,
763 audio_id
, video_id
, dvdsub_id
, filename
);
764 if(demuxer
) return demuxer
; // done!
765 file_format
=DEMUXER_TYPE_UNKNOWN
; // continue fuzzy guessing...
766 mp_msg(MSGT_DEMUXER
,MSGL_V
,"demuxer: continue fuzzy content-based format guessing...\n");
770 // Try detection for all other demuxers
771 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
772 if (!demuxer_desc
->safe_check
&& demuxer_desc
->check_file
) {
773 demuxer
= new_demuxer(stream
,demuxer_desc
->type
,audio_id
,video_id
,dvdsub_id
,filename
);
774 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
775 if (fformat
== demuxer_desc
->type
) {
776 demuxer_t
*demux2
= demuxer
;
777 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, MSGTR_Detected_XXX_FileFormat
, demuxer_desc
->shortdesc
);
778 file_format
= fformat
;
779 if (!demuxer
->desc
->open
|| (demux2
= demuxer
->desc
->open(demuxer
))) {
784 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
785 return demuxer
; // handled in mplayer.c
786 // Format changed after check, recurse
787 free_demuxer(demuxer
);
788 demuxer
=demux_open_stream(stream
, fformat
, force
,
789 audio_id
, video_id
, dvdsub_id
, filename
);
790 if(demuxer
) return demuxer
; // done!
791 file_format
= DEMUXER_TYPE_UNKNOWN
;
794 free_demuxer(demuxer
);
800 //====== File format recognized, set up these for compatibility: =========
803 demuxer
->file_format
=file_format
;
805 if ((sh_video
=demuxer
->video
->sh
) && sh_video
->bih
){
806 int biComp
=le2me_32(sh_video
->bih
->biCompression
);
807 mp_msg(MSGT_DEMUX
,MSGL_INFO
,"VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
809 sh_video
->bih
->biWidth
,
810 sh_video
->bih
->biHeight
,
811 sh_video
->bih
->biBitCount
,
813 sh_video
->i_bps
*0.008f
,
814 sh_video
->i_bps
/1024.0f
);
819 char* audio_stream
= NULL
;
820 char* sub_stream
= NULL
;
821 int demuxer_type
= 0; // used by rawaudio and rawvideo
822 int audio_stream_cache
= 0;
824 char *demuxer_name
= NULL
; // parameter from -demuxer
825 char *audio_demuxer_name
= NULL
; // parameter from -audio-demuxer
826 char *sub_demuxer_name
= NULL
; // parameter from -sub-demuxer
828 extern int hr_mp3_seek
;
830 extern float stream_cache_min_percent
;
831 extern float stream_cache_seek_min_percent
;
833 demuxer_t
* demux_open(stream_t
*vs
,int file_format
,int audio_id
,int video_id
,int dvdsub_id
,char* filename
){
834 stream_t
*as
= NULL
,*ss
= NULL
;
835 demuxer_t
*vd
,*ad
= NULL
,*sd
= NULL
;
836 int afmt
=DEMUXER_TYPE_UNKNOWN
,sfmt
= DEMUXER_TYPE_UNKNOWN
;
837 int audio_demuxer_type
= 0, sub_demuxer_type
= 0;
838 int demuxer_force
= 0, audio_demuxer_force
= 0,
839 sub_demuxer_force
= 0;
841 if ((demuxer_type
= get_demuxer_type_from_name(demuxer_name
, &demuxer_force
)) < 0) {
842 mp_msg(MSGT_DEMUXER
,MSGL_ERR
,"-demuxer %s does not exist.\n",demuxer_name
);
844 if ((audio_demuxer_type
= get_demuxer_type_from_name(audio_demuxer_name
, &audio_demuxer_force
)) < 0) {
845 mp_msg(MSGT_DEMUXER
,MSGL_ERR
,"-audio-demuxer %s does not exist.\n",audio_demuxer_name
);
847 if ((sub_demuxer_type
= get_demuxer_type_from_name(sub_demuxer_name
, &sub_demuxer_force
)) < 0) {
848 mp_msg(MSGT_DEMUXER
,MSGL_ERR
,"-sub-demuxer %s does not exist.\n",sub_demuxer_name
);
852 as
= open_stream(audio_stream
,0,&afmt
);
854 mp_msg(MSGT_DEMUXER
,MSGL_ERR
,MSGTR_CannotOpenAudioStream
,audio_stream
);
857 if(audio_stream_cache
) {
858 if(!stream_enable_cache(as
,audio_stream_cache
*1024,audio_stream_cache
*1024*(stream_cache_min_percent
/ 100.0),
859 audio_stream_cache
*1024*(stream_cache_seek_min_percent
/ 100.0))) {
861 mp_msg(MSGT_DEMUXER
,MSGL_ERR
,"Can't enable audio stream cache\n");
867 ss
= open_stream(sub_stream
,0,&sfmt
);
869 mp_msg(MSGT_DEMUXER
,MSGL_ERR
,MSGTR_CannotOpenSubtitlesStream
,sub_stream
);
874 vd
= demux_open_stream(vs
, demuxer_type
? demuxer_type
: file_format
,
875 demuxer_force
, audio_stream
? -2 : audio_id
, video_id
,
876 sub_stream
? -2 : dvdsub_id
, filename
);
878 if(as
) free_stream(as
);
879 if(ss
) free_stream(ss
);
883 ad
= demux_open_stream(as
, audio_demuxer_type
? audio_demuxer_type
: afmt
,
884 audio_demuxer_force
, audio_id
, -2, -2, audio_stream
);
886 mp_msg(MSGT_DEMUXER
,MSGL_WARN
,MSGTR_OpeningAudioDemuxerFailed
,audio_stream
);
889 else if(ad
->audio
->sh
&& ((sh_audio_t
*)ad
->audio
->sh
)->format
== 0x55) // MP3
890 hr_mp3_seek
=1; // Enable high res seeking
893 sd
= demux_open_stream(ss
, sub_demuxer_type
? sub_demuxer_type
: sfmt
,
894 sub_demuxer_force
, -2, -2, dvdsub_id
, sub_stream
);
896 mp_msg(MSGT_DEMUXER
,MSGL_WARN
,MSGTR_OpeningSubtitlesDemuxerFailed
,sub_stream
);
902 return new_demuxers_demuxer(vd
,ad
,sd
);
904 return new_demuxers_demuxer(vd
,ad
,vd
);
906 return new_demuxers_demuxer(vd
,vd
,sd
);
912 int demux_seek(demuxer_t
*demuxer
,float rel_seek_secs
,float audio_delay
,int flags
){
913 demux_stream_t
*d_audio
=demuxer
->audio
;
914 demux_stream_t
*d_video
=demuxer
->video
;
915 sh_audio_t
*sh_audio
=d_audio
->sh
;
916 sh_video_t
*sh_video
=d_video
->sh
;
920 if(!demuxer
->seekable
){
921 if(demuxer
->file_format
==DEMUXER_TYPE_AVI
)
922 mp_msg(MSGT_SEEK
,MSGL_WARN
,MSGTR_CantSeekRawAVI
);
924 else if (demuxer
->file_format
==DEMUXER_TYPE_TV
)
925 mp_msg(MSGT_SEEK
,MSGL_WARN
,MSGTR_TVInputNotSeekable
);
928 mp_msg(MSGT_SEEK
,MSGL_WARN
,MSGTR_CantSeekFile
);
932 // clear demux buffers:
933 if(sh_audio
){ ds_free_packs(d_audio
);sh_audio
->a_buffer_len
=0;}
934 ds_free_packs(d_video
);
936 demuxer
->stream
->eof
=0; // clear eof flag
937 demuxer
->video
->eof
=0;
938 demuxer
->audio
->eof
=0;
941 if(sh_audio
) sh_audio
->timer
=sh_video
->timer
;
943 if(sh_video
) sh_video
->timer
=0; // !!!!!!
946 if(flags
& 1) // absolute seek
949 if(demuxer
->stream_pts
== MP_NOPTS_VALUE
)
951 pts
= demuxer
->stream_pts
;
954 if(flags
& 2) { // percent seek
955 if(stream_control(demuxer
->stream
, STREAM_CTRL_GET_TIME_LENGTH
, &tmp
) == STREAM_UNSUPORTED
)
957 pts
+= tmp
* rel_seek_secs
;
959 pts
+= rel_seek_secs
;
961 if(stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_TIME
, &pts
) != STREAM_UNSUPORTED
) {
962 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
967 if (demuxer
->desc
->seek
)
968 demuxer
->desc
->seek(demuxer
,rel_seek_secs
,audio_delay
,flags
);
970 if (sh_audio
) resync_audio_stream(sh_audio
);
975 int demux_info_add(demuxer_t
*demuxer
, const char *opt
, const char *param
)
977 char **info
= demuxer
->info
;
981 for(n
= 0; info
&& info
[2*n
] != NULL
; n
++)
983 if(!strcasecmp(opt
,info
[2*n
]))
985 mp_msg(MSGT_DEMUX
, MSGL_INFO
,MSGTR_DemuxerInfoChanged
,opt
,param
);
987 info
[2*n
+1] = strdup(param
);
992 info
= demuxer
->info
= (char**)realloc(info
,(2*(n
+2))*sizeof(char*));
993 info
[2*n
] = strdup(opt
);
994 info
[2*n
+1] = strdup(param
);
995 memset(&info
[2*(n
+1)],0,2*sizeof(char*));
1000 int demux_info_print(demuxer_t
*demuxer
)
1002 char **info
= demuxer
->info
;
1008 mp_msg(MSGT_DEMUX
, MSGL_INFO
,MSGTR_ClipInfo
);
1009 for(n
= 0; info
[2*n
] != NULL
; n
++)
1011 mp_msg(MSGT_DEMUX
, MSGL_INFO
, " %s: %s\n",info
[2*n
],info
[2*n
+1]);
1012 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_NAME%d=%s\n", n
, info
[2*n
]);
1013 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_VALUE%d=%s\n", n
, info
[2*n
+1]);
1015 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_N=%d\n", n
);
1020 char* demux_info_get(demuxer_t
*demuxer
, const char *opt
) {
1022 char **info
= demuxer
->info
;
1024 for(i
= 0; info
&& info
[2*i
] != NULL
; i
++) {
1025 if(!strcasecmp(opt
,info
[2*i
]))
1032 int demux_control(demuxer_t
*demuxer
, int cmd
, void *arg
) {
1034 if (demuxer
->desc
->control
)
1035 return demuxer
->desc
->control(demuxer
,cmd
,arg
);
1037 return DEMUXER_CTRL_NOTIMPL
;
1042 double demuxer_get_time_length(demuxer_t
*demuxer
){
1043 double get_time_ans
;
1044 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1045 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1046 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
1047 if (demux_control(demuxer
, DEMUXER_CTRL_GET_TIME_LENGTH
,(void *)&get_time_ans
)<=0) {
1048 if (sh_video
&& sh_video
->i_bps
&& sh_audio
&& sh_audio
->i_bps
)
1049 get_time_ans
= (double)(demuxer
->movi_end
-demuxer
->movi_start
)/(sh_video
->i_bps
+sh_audio
->i_bps
);
1050 else if (sh_video
&& sh_video
->i_bps
)
1051 get_time_ans
= (double)(demuxer
->movi_end
-demuxer
->movi_start
)/sh_video
->i_bps
;
1052 else if (sh_audio
&& sh_audio
->i_bps
)
1053 get_time_ans
= (double)(demuxer
->movi_end
-demuxer
->movi_start
)/sh_audio
->i_bps
;
1057 return get_time_ans
;
1061 * \brief demuxer_get_current_time() returns the time of the current play in three possible ways:
1062 * either when the stream reader satisfies STREAM_CTRL_GET_CURRENT_TIME (e.g. dvd)
1063 * or using sh_video->pts when the former method fails
1065 * \return the current play time
1067 int demuxer_get_current_time(demuxer_t
*demuxer
){
1068 double get_time_ans
= 0;
1069 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1070 if(demuxer
->stream_pts
!= MP_NOPTS_VALUE
)
1071 get_time_ans
= demuxer
->stream_pts
;
1072 else if(sh_video
) get_time_ans
= sh_video
->pts
;
1073 return (int) get_time_ans
;
1076 int demuxer_get_percent_pos(demuxer_t
*demuxer
){
1078 int res
= demux_control(demuxer
, DEMUXER_CTRL_GET_PERCENT_POS
, &ans
);
1079 int len
= (demuxer
->movi_end
- demuxer
->movi_start
) / 100;
1082 ans
= (demuxer
->filepos
- demuxer
->movi_start
) / len
;
1086 if (ans
< 0) ans
= 0;
1087 if (ans
> 100) ans
= 100;
1091 int demuxer_switch_audio(demuxer_t
*demuxer
, int index
){
1092 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_AUDIO
, &index
);
1093 if (res
== DEMUXER_CTRL_NOTIMPL
)
1094 index
= demuxer
->audio
->id
;
1098 int demuxer_switch_video(demuxer_t
*demuxer
, int index
){
1099 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_VIDEO
, &index
);
1100 if (res
== DEMUXER_CTRL_NOTIMPL
)
1101 index
= demuxer
->video
->id
;
1105 int demuxer_add_chapter(demuxer_t
* demuxer
, const char* name
, uint64_t start
, uint64_t end
){
1106 if (demuxer
->chapters
== NULL
)
1107 demuxer
->chapters
= malloc (32*sizeof(*demuxer
->chapters
));
1108 else if (!(demuxer
->num_chapters
% 32))
1109 demuxer
->chapters
= realloc (demuxer
->chapters
, (demuxer
->num_chapters
+ 32) * sizeof(*demuxer
->chapters
));
1111 demuxer
->chapters
[demuxer
->num_chapters
].start
= start
;
1112 demuxer
->chapters
[demuxer
->num_chapters
].end
= end
;
1113 demuxer
->chapters
[demuxer
->num_chapters
].name
= strdup(name
);
1115 return demuxer
->num_chapters
++;
1119 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1120 * either using the demuxer->chapters structure set by the demuxer
1121 * or asking help to the stream layer (e.g. dvd)
1122 * \param chapter - chapter number wished - 0-based
1123 * \param mode 0: relative to current main pts, 1: absolute
1124 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1125 * \param num_chapters number of chapters present (set by this function is param is not null)
1126 * \param chapter_name name of chapter found (set by this function is param is not null)
1127 * \return -1 on error, current chapter if successful
1130 int demuxer_seek_chapter(demuxer_t
*demuxer
, int chapter
, int mode
, float *seek_pts
, int *num_chapters
, char **chapter_name
) {
1133 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1134 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1136 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1138 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_CURRENT_CHAPTER
, ¤t
);
1139 if(ris
== STREAM_UNSUPORTED
) return -1;
1143 if(demuxer
->video
->sh
)
1144 ds_free_packs(demuxer
->video
);
1146 if(demuxer
->audio
->sh
)
1147 ds_free_packs(demuxer
->audio
);
1149 if(demuxer
->sub
->id
>= 0)
1150 ds_free_packs(demuxer
->sub
);
1152 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_CHAPTER
, &chapter
);
1153 if(ris
!= STREAM_UNSUPORTED
)
1154 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1156 ds_fill_buffer(demuxer
->video
);
1157 resync_video_stream(sh_video
);
1161 ds_fill_buffer(demuxer
->audio
);
1162 resync_audio_stream(sh_audio
);
1165 //exit status may be ok, but main() doesn't have to seek itself (because e.g. dvds depend on sectors, not on pts)
1169 if(stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
, num_chapters
) == STREAM_UNSUPORTED
)
1174 char *tmp
= malloc(16);
1175 *chapter_name
= NULL
;
1176 if(num_chapters
&& *num_chapters
&& tmp
) {
1177 sprintf(tmp
, " of %3d", *num_chapters
);
1178 *chapter_name
= tmp
;
1182 return (ris
!= STREAM_UNSUPORTED
? chapter
: -1);
1183 } else { //chapters structure is set in the demuxer
1184 total
= demuxer
->num_chapters
;
1186 if (mode
==1) { //absolute seeking
1188 } else { //relative seeking
1190 now
= (sh_video
? sh_video
->pts
: (sh_audio
? sh_audio
->pts
: 0.)) * 1000 + .5;
1192 for (current
= total
- 1; current
>= 0; --current
) {
1193 demux_chapter_t
* chapter
= demuxer
->chapters
+ current
;
1194 if (chapter
->start
<= now
)
1200 if (current
>= total
)
1202 if (current
< 0) current
= 0;
1204 *seek_pts
= demuxer
->chapters
[current
].start
/ 1000.0;
1207 *num_chapters
= demuxer
->num_chapters
;
1210 if(demuxer
->chapters
[current
].name
)
1211 *chapter_name
= strdup(demuxer
->chapters
[current
].name
);
1212 else *chapter_name
= NULL
;