1 //=================== DEMUXER v2.5 =========================
18 #include "libvo/fastmemcpy.h"
20 #include "stream/stream.h"
25 #include "libaf/af_format.h"
28 #include "libass/ass.h"
29 #include "libass/ass_mp.h"
32 #ifdef CONFIG_LIBAVCODEC
33 #include "libavcodec/avcodec.h"
34 #if MP_INPUT_BUFFER_PADDING_SIZE < FF_INPUT_BUFFER_PADDING_SIZE
35 #error MP_INPUT_BUFFER_PADDING_SIZE is too small!
39 void resync_video_stream(sh_video_t
*sh_video
);
40 void resync_audio_stream(sh_audio_t
*sh_audio
);
43 extern const demuxer_desc_t demuxer_desc_rawaudio
;
44 extern const demuxer_desc_t demuxer_desc_rawvideo
;
45 extern const demuxer_desc_t demuxer_desc_tv
;
46 extern const demuxer_desc_t demuxer_desc_mf
;
47 extern const demuxer_desc_t demuxer_desc_avi
;
48 extern const demuxer_desc_t demuxer_desc_y4m
;
49 extern const demuxer_desc_t demuxer_desc_asf
;
50 extern const demuxer_desc_t demuxer_desc_real
;
51 extern const demuxer_desc_t demuxer_desc_smjpeg
;
52 extern const demuxer_desc_t demuxer_desc_matroska
;
53 extern const demuxer_desc_t demuxer_desc_realaudio
;
54 extern const demuxer_desc_t demuxer_desc_vqf
;
55 extern const demuxer_desc_t demuxer_desc_mov
;
56 extern const demuxer_desc_t demuxer_desc_vivo
;
57 extern const demuxer_desc_t demuxer_desc_fli
;
58 extern const demuxer_desc_t demuxer_desc_film
;
59 extern const demuxer_desc_t demuxer_desc_roq
;
60 extern const demuxer_desc_t demuxer_desc_gif
;
61 extern const demuxer_desc_t demuxer_desc_ogg
;
62 extern const demuxer_desc_t demuxer_desc_avs
;
63 extern const demuxer_desc_t demuxer_desc_pva
;
64 extern const demuxer_desc_t demuxer_desc_nsv
;
65 extern const demuxer_desc_t demuxer_desc_mpeg_ts
;
66 extern const demuxer_desc_t demuxer_desc_lmlm4
;
67 extern const demuxer_desc_t demuxer_desc_mpeg_ps
;
68 extern const demuxer_desc_t demuxer_desc_mpeg_pes
;
69 extern const demuxer_desc_t demuxer_desc_mpeg_es
;
70 extern const demuxer_desc_t demuxer_desc_mpeg_gxf
;
71 extern const demuxer_desc_t demuxer_desc_mpeg4_es
;
72 extern const demuxer_desc_t demuxer_desc_h264_es
;
73 extern const demuxer_desc_t demuxer_desc_rawdv
;
74 extern const demuxer_desc_t demuxer_desc_mpc
;
75 extern const demuxer_desc_t demuxer_desc_audio
;
76 extern const demuxer_desc_t demuxer_desc_xmms
;
77 extern const demuxer_desc_t demuxer_desc_mpeg_ty
;
78 extern const demuxer_desc_t demuxer_desc_rtp
;
79 extern const demuxer_desc_t demuxer_desc_rtp_nemesi
;
80 extern const demuxer_desc_t demuxer_desc_lavf
;
81 extern const demuxer_desc_t demuxer_desc_lavf_preferred
;
82 extern const demuxer_desc_t demuxer_desc_aac
;
83 extern const demuxer_desc_t demuxer_desc_nut
;
84 extern const demuxer_desc_t demuxer_desc_mng
;
86 /* Please do not add any new demuxers here. If you want to implement a new
87 * demuxer, add it to libavformat, except for wrappers around external
88 * libraries and demuxers requiring binary support. */
90 const demuxer_desc_t
*const demuxer_list
[] = {
91 &demuxer_desc_rawaudio
,
92 &demuxer_desc_rawvideo
,
97 #ifdef CONFIG_LIBAVFORMAT
98 &demuxer_desc_lavf_preferred
,
105 &demuxer_desc_smjpeg
,
106 &demuxer_desc_matroska
,
107 &demuxer_desc_realaudio
,
117 #ifdef CONFIG_OGGVORBIS
120 #ifdef CONFIG_WIN32DLL
124 &demuxer_desc_mpeg_ts
,
126 &demuxer_desc_mpeg_ps
,
127 &demuxer_desc_mpeg_pes
,
128 &demuxer_desc_mpeg_es
,
129 &demuxer_desc_mpeg_gxf
,
130 &demuxer_desc_mpeg4_es
,
131 &demuxer_desc_h264_es
,
133 &demuxer_desc_mpeg_ty
,
134 #ifdef CONFIG_LIVE555
137 #ifdef CONFIG_LIBNEMESI
138 &demuxer_desc_rtp_nemesi
,
140 #ifdef CONFIG_LIBAVFORMAT
143 #ifdef CONFIG_MUSEPACK
146 #ifdef CONFIG_LIBDV095
159 /* Please do not add any new demuxers here. If you want to implement a new
160 * demuxer, add it to libavformat, except for wrappers around external
161 * libraries and demuxers requiring binary support. */
165 void free_demuxer_stream(demux_stream_t
*ds
)
171 demux_stream_t
*new_demuxer_stream(struct demuxer
*demuxer
, int id
)
173 demux_stream_t
*ds
= malloc(sizeof(demux_stream_t
));
174 ds
->buffer_pos
= ds
->buffer_size
= 0;
185 ds
->first
= ds
->last
= ds
->current
= NULL
;
187 ds
->demuxer
= demuxer
;
190 ds
->asf_packet
= NULL
;
192 ds
->ss_mul
= ds
->ss_div
= 0;
200 * Get demuxer description structure for a given demuxer type
202 * @param file_format type of the demuxer
203 * @return structure for the demuxer, NULL if not found
205 static const demuxer_desc_t
*get_demuxer_desc_from_type(int file_format
)
209 for (i
= 0; demuxer_list
[i
]; i
++)
210 if (file_format
== demuxer_list
[i
]->type
)
211 return demuxer_list
[i
];
217 demuxer_t
*new_demuxer(struct MPOpts
*opts
, stream_t
*stream
, int type
,
218 int a_id
, int v_id
, int s_id
, char *filename
)
220 struct demuxer
*d
= talloc_zero(NULL
, struct demuxer
);
222 d
->stream_pts
= MP_NOPTS_VALUE
;
223 d
->reference_clock
= MP_NOPTS_VALUE
;
224 d
->movi_start
= stream
->start_pos
;
225 d
->movi_end
= stream
->end_pos
;
229 d
->audio
= new_demuxer_stream(d
, a_id
);
230 d
->video
= new_demuxer_stream(d
, v_id
);
231 d
->sub
= new_demuxer_stream(d
, s_id
);
235 if (!(d
->desc
= get_demuxer_desc_from_type(type
)))
236 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
237 "BUG! Invalid demuxer type in new_demuxer(), "
238 "big troubles ahead.");
239 if (filename
) // Filename hack for avs_check_file
240 d
->filename
= strdup(filename
);
241 stream_reset(stream
);
242 stream_seek(stream
, stream
->start_pos
);
246 sh_sub_t
*new_sh_sub_sid(demuxer_t
*demuxer
, int id
, int sid
)
248 if (id
> MAX_S_STREAMS
- 1 || id
< 0) {
249 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
250 "Requested sub stream id overflow (%d > %d)\n", id
,
254 if (demuxer
->s_streams
[id
])
255 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, "Sub stream %i redefined\n", id
);
257 sh_sub_t
*sh
= calloc(1, sizeof(sh_sub_t
));
258 demuxer
->s_streams
[id
] = sh
;
260 sh
->opts
= demuxer
->opts
;
261 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SUBTITLE_ID=%d\n", sid
);
263 if (sid
== demuxer
->opts
->sub_id
) {
264 demuxer
->sub
->id
= id
;
265 demuxer
->sub
->sh
= demuxer
->s_streams
[id
];
267 return demuxer
->s_streams
[id
];
270 static void free_sh_sub(sh_sub_t
*sh
)
272 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_sub at %p\n", sh
);
276 ass_free_track(sh
->ass_track
);
282 sh_audio_t
*new_sh_audio_aid(demuxer_t
*demuxer
, int id
, int aid
)
284 if (id
> MAX_A_STREAMS
- 1 || id
< 0) {
285 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
286 "Requested audio stream id overflow (%d > %d)\n", id
,
290 if (demuxer
->a_streams
[id
]) {
291 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, MSGTR_AudioStreamRedefined
, id
);
293 mp_tmsg(MSGT_DEMUXER
, MSGL_V
, MSGTR_FoundAudioStream
, id
);
294 sh_audio_t
*sh
= calloc(1, sizeof(sh_audio_t
));
295 demuxer
->a_streams
[id
] = sh
;
297 sh
->ds
= demuxer
->audio
;
300 sh
->sample_format
= AF_FORMAT_S16_NE
;
301 sh
->audio_out_minsize
= 8192; /* default size, maybe not enough for Win32/ACM */
302 sh
->pts
= MP_NOPTS_VALUE
;
303 sh
->opts
= demuxer
->opts
;
304 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_ID=%d\n", aid
);
306 return demuxer
->a_streams
[id
];
309 void free_sh_audio(demuxer_t
*demuxer
, int id
)
311 sh_audio_t
*sh
= demuxer
->a_streams
[id
];
312 demuxer
->a_streams
[id
] = NULL
;
313 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_audio at %p\n", sh
);
320 sh_video_t
*new_sh_video_vid(demuxer_t
*demuxer
, int id
, int vid
)
322 if (id
> MAX_V_STREAMS
- 1 || id
< 0) {
323 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
324 "Requested video stream id overflow (%d > %d)\n", id
,
328 if (demuxer
->v_streams
[id
])
329 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, MSGTR_VideoStreamRedefined
, id
);
331 mp_tmsg(MSGT_DEMUXER
, MSGL_V
, MSGTR_FoundVideoStream
, id
);
332 sh_video_t
*sh
= calloc(1, sizeof *sh
);
333 demuxer
->v_streams
[id
] = sh
;
335 sh
->ds
= demuxer
->video
;
336 sh
->opts
= demuxer
->opts
;
337 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_ID=%d\n", vid
);
339 return demuxer
->v_streams
[id
];
342 void free_sh_video(sh_video_t
*sh
)
344 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_video at %p\n", sh
);
349 void free_demuxer(demuxer_t
*demuxer
)
352 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing demuxer at %p\n",
354 if (demuxer
->desc
->close
)
355 demuxer
->desc
->close(demuxer
);
356 // Very ugly hack to make it behave like old implementation
357 if (demuxer
->desc
->type
== DEMUXER_TYPE_DEMUXERS
)
358 goto skip_streamfree
;
360 for (i
= 0; i
< MAX_A_STREAMS
; i
++)
361 if (demuxer
->a_streams
[i
])
362 free_sh_audio(demuxer
, i
);
363 for (i
= 0; i
< MAX_V_STREAMS
; i
++)
364 if (demuxer
->v_streams
[i
])
365 free_sh_video(demuxer
->v_streams
[i
]);
366 for (i
= 0; i
< MAX_S_STREAMS
; i
++)
367 if (demuxer
->s_streams
[i
])
368 free_sh_sub(demuxer
->s_streams
[i
]);
370 free_demuxer_stream(demuxer
->audio
);
371 free_demuxer_stream(demuxer
->video
);
372 free_demuxer_stream(demuxer
->sub
);
375 for (i
= 0; demuxer
->info
[i
] != NULL
; i
++)
376 free(demuxer
->info
[i
]);
379 free(demuxer
->filename
);
380 if (demuxer
->chapters
) {
381 for (i
= 0; i
< demuxer
->num_chapters
; i
++)
382 free(demuxer
->chapters
[i
].name
);
383 free(demuxer
->chapters
);
385 if (demuxer
->attachments
) {
386 for (i
= 0; i
< demuxer
->num_attachments
; i
++) {
387 free(demuxer
->attachments
[i
].name
);
388 free(demuxer
->attachments
[i
].type
);
389 free(demuxer
->attachments
[i
].data
);
391 free(demuxer
->attachments
);
393 talloc_free(demuxer
);
397 void ds_add_packet(demux_stream_t
*ds
, demux_packet_t
*dp
)
399 // append packet to DS stream:
401 ds
->bytes
+= dp
->len
;
403 // next packet in stream
407 // first packet in stream
408 ds
->first
= ds
->last
= dp
;
410 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
411 "DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
412 (ds
== ds
->demuxer
->audio
) ? "d_audio" : "d_video", dp
->len
,
413 dp
->pts
, (unsigned int) dp
->pos
, ds
->demuxer
->audio
->packs
,
414 ds
->demuxer
->video
->packs
);
417 void ds_read_packet(demux_stream_t
*ds
, stream_t
*stream
, int len
,
418 double pts
, off_t pos
, int flags
)
420 demux_packet_t
*dp
= new_demux_packet(len
);
421 len
= stream_read(stream
, dp
->buffer
, len
);
422 resize_demux_packet(dp
, len
);
426 // append packet to DS stream:
427 ds_add_packet(ds
, dp
);
431 // 0 = EOF or no stream found or invalid type
432 // 1 = successfully read a packet
434 int demux_fill_buffer(demuxer_t
*demux
, demux_stream_t
*ds
)
436 // Note: parameter 'ds' can be NULL!
437 return demux
->desc
->fill_buffer(demux
, ds
);
443 #define MAX_ACUMULATED_PACKETS 64
444 int ds_fill_buffer(demux_stream_t
*ds
)
446 demuxer_t
*demux
= ds
->demuxer
;
448 free_demux_packet(ds
->current
);
450 if (mp_msg_test(MSGT_DEMUXER
, MSGL_DBG3
)) {
451 if (ds
== demux
->audio
)
452 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
453 "ds_fill_buffer(d_audio) called\n");
454 else if (ds
== demux
->video
)
455 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
456 "ds_fill_buffer(d_video) called\n");
457 else if (ds
== demux
->sub
)
458 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
, "ds_fill_buffer(d_sub) called\n");
460 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
461 "ds_fill_buffer(unknown 0x%X) called\n", (unsigned int) ds
);
465 demux_packet_t
*p
= ds
->first
;
467 if (demux
->reference_clock
!= MP_NOPTS_VALUE
) {
468 if ( p
->pts
!= MP_NOPTS_VALUE
469 && p
->pts
> demux
->reference_clock
470 && ds
->packs
< MAX_ACUMULATED_PACKETS
) {
471 if (demux_fill_buffer(demux
, ds
))
477 ds
->buffer
= p
->buffer
;
479 ds
->buffer_size
= p
->len
;
481 ds
->dpos
+= p
->len
; // !!!
483 if (p
->pts
!= MP_NOPTS_VALUE
) {
487 ds
->pts_bytes
+= p
->len
; // !!!
488 if (p
->stream_pts
!= MP_NOPTS_VALUE
)
489 demux
->stream_pts
= p
->stream_pts
;
490 ds
->flags
= p
->flags
;
498 /* The code below can set ds->eof to 1 when another stream runs
499 * out of buffer space. That makes sense because in that situation
500 * the calling code should not count on being able to demux more
501 * packets from this stream.
502 * If however the situation improves and we're called again
503 * despite the eof flag then it's better to clear it to avoid
508 if (demux
->audio
->packs
>= MAX_PACKS
509 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
510 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_TooManyAudioInBuffer
,
511 demux
->audio
->packs
, demux
->audio
->bytes
);
512 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MSGTR_MaybeNI
);
515 if (demux
->video
->packs
>= MAX_PACKS
516 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
517 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_TooManyVideoInBuffer
,
518 demux
->video
->packs
, demux
->video
->bytes
);
519 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MSGTR_MaybeNI
);
522 if (!demux_fill_buffer(demux
, ds
)) {
523 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
524 "ds_fill_buffer()->demux_fill_buffer() failed\n");
528 ds
->buffer_pos
= ds
->buffer_size
= 0;
530 mp_msg(MSGT_DEMUXER
, MSGL_V
,
531 "ds_fill_buffer: EOF reached (stream: %s) \n",
532 ds
== demux
->audio
? "audio" : "video");
537 int demux_read_data(demux_stream_t
*ds
, unsigned char *mem
, int len
)
542 x
= ds
->buffer_size
- ds
->buffer_pos
;
544 if (!ds_fill_buffer(ds
))
550 fast_memcpy(mem
+ bytes
, &ds
->buffer
[ds
->buffer_pos
], x
);
560 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
561 * \param mem memory to read data into, may be NULL to discard data
562 * \param maxlen maximum number of bytes to read
563 * \param read number of bytes actually read
564 * \param pattern pattern to search for (lowest 8 bits are ignored)
565 * \return whether pattern was found
567 int demux_pattern_3(demux_stream_t
*ds
, unsigned char *mem
, int maxlen
,
568 int *read
, uint32_t pattern
)
570 register uint32_t head
= 0xffffff00;
571 register uint32_t pat
= pattern
& 0xffffff00;
574 register unsigned char *ds_buf
= &ds
->buffer
[ds
->buffer_size
];
575 int len
= ds
->buffer_size
- ds
->buffer_pos
;
576 register long pos
= -len
;
577 if (unlikely(pos
>= 0)) { // buffer is empty
584 } while (++pos
&& head
!= pat
);
586 if (total_len
+ len
> maxlen
)
587 len
= maxlen
- total_len
;
588 len
= demux_read_data(ds
, mem
? &mem
[total_len
] : NULL
, len
);
590 } while ((head
!= pat
|| total_len
< 3) && total_len
< maxlen
&& !ds
->eof
);
593 return total_len
>= 3 && head
== pat
;
596 void ds_free_packs(demux_stream_t
*ds
)
598 demux_packet_t
*dp
= ds
->first
;
600 demux_packet_t
*dn
= dp
->next
;
601 free_demux_packet(dp
);
604 if (ds
->asf_packet
) {
605 // free unfinished .asf fragments:
606 free(ds
->asf_packet
->buffer
);
607 free(ds
->asf_packet
);
608 ds
->asf_packet
= NULL
;
610 ds
->first
= ds
->last
= NULL
;
611 ds
->packs
= 0; // !!!!!
614 free_demux_packet(ds
->current
);
617 ds
->buffer_pos
= ds
->buffer_size
;
622 int ds_get_packet(demux_stream_t
*ds
, unsigned char **start
)
625 if (ds
->buffer_pos
>= ds
->buffer_size
) {
626 if (!ds_fill_buffer(ds
)) {
632 len
= ds
->buffer_size
- ds
->buffer_pos
;
633 *start
= &ds
->buffer
[ds
->buffer_pos
];
634 ds
->buffer_pos
+= len
;
638 int ds_get_packet_pts(demux_stream_t
*ds
, unsigned char **start
, double *pts
)
641 *pts
= MP_NOPTS_VALUE
;
642 if (ds
->buffer_pos
>= ds
->buffer_size
) {
643 if (!ds_fill_buffer(ds
)) {
649 // Return pts unless this read starts from the middle of a packet
651 *pts
= ds
->current
->pts
;
652 len
= ds
->buffer_size
- ds
->buffer_pos
;
653 *start
= &ds
->buffer
[ds
->buffer_pos
];
654 ds
->buffer_pos
+= len
;
658 int ds_get_packet_sub(demux_stream_t
*ds
, unsigned char **start
)
661 if (ds
->buffer_pos
>= ds
->buffer_size
) {
665 if (!ds_fill_buffer(ds
))
668 len
= ds
->buffer_size
- ds
->buffer_pos
;
669 *start
= &ds
->buffer
[ds
->buffer_pos
];
670 ds
->buffer_pos
+= len
;
674 double ds_get_next_pts(demux_stream_t
*ds
)
676 demuxer_t
*demux
= ds
->demuxer
;
678 if (demux
->audio
->packs
>= MAX_PACKS
679 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
680 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_TooManyAudioInBuffer
,
681 demux
->audio
->packs
, demux
->audio
->bytes
);
682 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MSGTR_MaybeNI
);
683 return MP_NOPTS_VALUE
;
685 if (demux
->video
->packs
>= MAX_PACKS
686 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
687 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_TooManyVideoInBuffer
,
688 demux
->video
->packs
, demux
->video
->bytes
);
689 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MSGTR_MaybeNI
);
690 return MP_NOPTS_VALUE
;
692 if (!demux_fill_buffer(demux
, ds
))
693 return MP_NOPTS_VALUE
;
695 return ds
->first
->pts
;
698 // ====================================================================
700 void demuxer_help(void)
704 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "Available demuxers:\n");
705 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, " demuxer: type info: (comment)\n");
706 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DEMUXERS\n");
707 for (i
= 0; demuxer_list
[i
]; i
++) {
708 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Don't display special demuxers
710 if (demuxer_list
[i
]->comment
&& strlen(demuxer_list
[i
]->comment
))
711 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s (%s)\n",
712 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
713 demuxer_list
[i
]->info
, demuxer_list
[i
]->comment
);
715 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s\n",
716 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
717 demuxer_list
[i
]->info
);
723 * Get demuxer type for a given demuxer name
725 * @param demuxer_name string with demuxer name of demuxer number
726 * @param force will be set if demuxer should be forced.
728 * @return DEMUXER_TYPE_xxx, -1 if error or not found
730 int get_demuxer_type_from_name(char *demuxer_name
, int *force
)
736 if (!demuxer_name
|| !demuxer_name
[0])
737 return DEMUXER_TYPE_UNKNOWN
;
739 *force
= demuxer_name
[0] == '+';
740 if (demuxer_name
[0] == '+')
741 demuxer_name
= &demuxer_name
[1];
742 for (i
= 0; demuxer_list
[i
]; i
++) {
743 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Can't select special demuxers from commandline
745 if (strcmp(demuxer_name
, demuxer_list
[i
]->name
) == 0)
746 return demuxer_list
[i
]->type
;
749 // No match found, try to parse name as an integer (demuxer number)
750 type_int
= strtol(demuxer_name
, &endptr
, 0);
751 if (*endptr
) // Conversion failed
753 if ((type_int
> 0) && (type_int
<= DEMUXER_TYPE_MAX
))
754 return (int) type_int
;
759 int extension_parsing
= 1; // 0=off 1=mixed (used only for unstable formats)
762 NOTE : Several demuxers may be opened at the same time so
763 demuxers should NEVER rely on an external var to enable them
764 self. If a demuxer can't do any auto-detection it should only use
765 file_format. The user can explicitly set file_format with the -demuxer
766 option so there is really no need for another extra var.
767 For convenience an option can be added to set file_format directly
768 to the right type (ex: rawaudio,rawvideo).
769 Also the stream can override the file_format so a demuxer which rely
770 on a special stream type can set file_format at the stream level
774 static demuxer_t
*demux_open_stream(struct MPOpts
*opts
, stream_t
*stream
,
775 int file_format
, int force
, int audio_id
,
776 int video_id
, int dvdsub_id
,
779 demuxer_t
*demuxer
= NULL
;
781 sh_video_t
*sh_video
= NULL
;
783 const demuxer_desc_t
*demuxer_desc
;
787 // If somebody requested a demuxer check it
789 if ((demuxer_desc
= get_demuxer_desc_from_type(file_format
))) {
790 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
791 video_id
, dvdsub_id
, filename
);
792 if (demuxer_desc
->check_file
)
793 fformat
= demuxer_desc
->check_file(demuxer
);
794 if (force
|| !demuxer_desc
->check_file
)
795 fformat
= demuxer_desc
->type
;
797 if (fformat
== demuxer_desc
->type
) {
798 demuxer_t
*demux2
= demuxer
;
799 // Move messages to demuxer detection code?
800 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
801 MSGTR_Detected_XXX_FileFormat
,
802 demuxer_desc
->shortdesc
);
803 file_format
= fformat
;
804 if (!demuxer
->desc
->open
805 || (demux2
= demuxer
->desc
->open(demuxer
))) {
810 // Format changed after check, recurse
811 free_demuxer(demuxer
);
812 return demux_open_stream(opts
, stream
, fformat
, force
,
813 audio_id
, video_id
, dvdsub_id
,
817 // Check failed for forced demuxer, quit
818 free_demuxer(demuxer
);
822 // Test demuxers with safe file checks
823 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
824 if (demuxer_desc
->safe_check
) {
825 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
826 video_id
, dvdsub_id
, filename
);
827 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
828 if (fformat
== demuxer_desc
->type
) {
829 demuxer_t
*demux2
= demuxer
;
830 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
831 MSGTR_Detected_XXX_FileFormat
,
832 demuxer_desc
->shortdesc
);
833 file_format
= fformat
;
834 if (!demuxer
->desc
->open
835 || (demux2
= demuxer
->desc
->open(demuxer
))) {
840 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
841 return demuxer
; // handled in mplayer.c
842 // Format changed after check, recurse
843 free_demuxer(demuxer
);
844 demuxer
= demux_open_stream(opts
, stream
, fformat
, force
,
846 dvdsub_id
, filename
);
848 return demuxer
; // done!
849 file_format
= DEMUXER_TYPE_UNKNOWN
;
852 free_demuxer(demuxer
);
857 // If no forced demuxer perform file extension based detection
858 // Ok. We're over the stable detectable fileformats, the next ones are
859 // a bit fuzzy. So by default (extension_parsing==1) try extension-based
861 if (file_format
== DEMUXER_TYPE_UNKNOWN
&& filename
862 && extension_parsing
== 1) {
863 file_format
= demuxer_type_by_filename(filename
);
864 if (file_format
!= DEMUXER_TYPE_UNKNOWN
) {
865 // we like recursion :)
866 demuxer
= demux_open_stream(opts
, stream
, file_format
, force
,
867 audio_id
, video_id
, dvdsub_id
,
870 return demuxer
; // done!
871 file_format
= DEMUXER_TYPE_UNKNOWN
; // continue fuzzy guessing...
872 mp_msg(MSGT_DEMUXER
, MSGL_V
,
873 "demuxer: continue fuzzy content-based format guessing...\n");
876 // Try detection for all other demuxers
877 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
878 if (!demuxer_desc
->safe_check
&& demuxer_desc
->check_file
) {
879 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
880 video_id
, dvdsub_id
, filename
);
881 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
882 if (fformat
== demuxer_desc
->type
) {
883 demuxer_t
*demux2
= demuxer
;
884 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
885 MSGTR_Detected_XXX_FileFormat
,
886 demuxer_desc
->shortdesc
);
887 file_format
= fformat
;
888 if (!demuxer
->desc
->open
889 || (demux2
= demuxer
->desc
->open(demuxer
))) {
894 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
895 return demuxer
; // handled in mplayer.c
896 // Format changed after check, recurse
897 free_demuxer(demuxer
);
898 demuxer
= demux_open_stream(opts
, stream
, fformat
, force
,
900 dvdsub_id
, filename
);
902 return demuxer
; // done!
903 file_format
= DEMUXER_TYPE_UNKNOWN
;
906 free_demuxer(demuxer
);
912 //====== File format recognized, set up these for compatibility: =========
915 demuxer
->file_format
= file_format
;
917 if ((sh_video
= demuxer
->video
->sh
) && sh_video
->bih
) {
918 int biComp
= le2me_32(sh_video
->bih
->biCompression
);
919 mp_msg(MSGT_DEMUX
, MSGL_INFO
,
920 "VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
921 (char *) &biComp
, sh_video
->bih
->biWidth
,
922 sh_video
->bih
->biHeight
, sh_video
->bih
->biBitCount
,
923 sh_video
->fps
, sh_video
->i_bps
* 0.008f
,
924 sh_video
->i_bps
/ 1024.0f
);
927 if (ass_enabled
&& ass_library
) {
928 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
929 sh_sub_t
*sh
= demuxer
->s_streams
[i
];
930 if (sh
&& sh
->type
== 'a') {
931 sh
->ass_track
= ass_new_track(ass_library
);
932 if (sh
->ass_track
&& sh
->extradata
)
933 ass_process_codec_private(sh
->ass_track
, sh
->extradata
,
935 } else if (sh
&& sh
->type
!= 'v')
936 sh
->ass_track
= ass_default_track(ass_library
);
943 char *audio_stream
= NULL
;
944 char *sub_stream
= NULL
;
945 int audio_stream_cache
= 0;
947 char *demuxer_name
= NULL
; // parameter from -demuxer
948 char *audio_demuxer_name
= NULL
; // parameter from -audio-demuxer
949 char *sub_demuxer_name
= NULL
; // parameter from -sub-demuxer
951 extern int hr_mp3_seek
;
953 extern float stream_cache_min_percent
;
954 extern float stream_cache_seek_min_percent
;
956 demuxer_t
*demux_open(struct MPOpts
*opts
, stream_t
*vs
, int file_format
,
957 int audio_id
, int video_id
, int dvdsub_id
,
960 stream_t
*as
= NULL
, *ss
= NULL
;
961 demuxer_t
*vd
, *ad
= NULL
, *sd
= NULL
;
963 int afmt
= DEMUXER_TYPE_UNKNOWN
, sfmt
= DEMUXER_TYPE_UNKNOWN
;
965 int audio_demuxer_type
= 0, sub_demuxer_type
= 0;
966 int demuxer_force
= 0, audio_demuxer_force
= 0, sub_demuxer_force
= 0;
969 get_demuxer_type_from_name(demuxer_name
, &demuxer_force
)) < 0) {
970 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-demuxer %s does not exist.\n",
973 if ((audio_demuxer_type
=
974 get_demuxer_type_from_name(audio_demuxer_name
,
975 &audio_demuxer_force
)) < 0) {
976 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-audio-demuxer %s does not exist.\n",
979 if ((sub_demuxer_type
=
980 get_demuxer_type_from_name(sub_demuxer_name
,
981 &sub_demuxer_force
)) < 0) {
982 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-sub-demuxer %s does not exist.\n",
987 as
= open_stream(audio_stream
, 0, &afmt
);
989 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_CannotOpenAudioStream
,
993 if (audio_stream_cache
) {
994 if (!stream_enable_cache
995 (as
, audio_stream_cache
* 1024,
996 audio_stream_cache
* 1024 * (stream_cache_min_percent
/
998 audio_stream_cache
* 1024 * (stream_cache_seek_min_percent
/
1001 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
1002 "Can't enable audio stream cache\n");
1008 ss
= open_stream(sub_stream
, 0, &sfmt
);
1010 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_CannotOpenSubtitlesStream
,
1016 vd
= demux_open_stream(opts
, vs
, demuxer_type
? demuxer_type
: file_format
,
1017 demuxer_force
, audio_stream
? -2 : audio_id
,
1018 video_id
, sub_stream
? -2 : dvdsub_id
, filename
);
1027 ad
= demux_open_stream(opts
, as
,
1028 audio_demuxer_type
? audio_demuxer_type
: afmt
,
1029 audio_demuxer_force
, audio_id
, -2, -2,
1032 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, MSGTR_OpeningAudioDemuxerFailed
,
1035 } else if (ad
->audio
->sh
1036 && ((sh_audio_t
*) ad
->audio
->sh
)->format
== 0x55) // MP3
1037 hr_mp3_seek
= 1; // Enable high res seeking
1040 sd
= demux_open_stream(opts
, ss
,
1041 sub_demuxer_type
? sub_demuxer_type
: sfmt
,
1042 sub_demuxer_force
, -2, -2, dvdsub_id
,
1045 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
,
1046 MSGTR_OpeningSubtitlesDemuxerFailed
, sub_stream
);
1052 res
= new_demuxers_demuxer(vd
, ad
, sd
);
1054 res
= new_demuxers_demuxer(vd
, ad
, vd
);
1056 res
= new_demuxers_demuxer(vd
, vd
, sd
);
1060 opts
->correct_pts
= opts
->user_correct_pts
;
1061 if (opts
->correct_pts
< 0)
1063 demux_control(res
, DEMUXER_CTRL_CORRECT_PTS
,
1064 NULL
) == DEMUXER_CTRL_OK
;
1069 void demux_flush(demuxer_t
*demuxer
)
1071 ds_free_packs(demuxer
->video
);
1072 ds_free_packs(demuxer
->audio
);
1073 ds_free_packs(demuxer
->sub
);
1076 int demux_seek(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
,
1079 demux_stream_t
*d_audio
= demuxer
->audio
;
1080 demux_stream_t
*d_video
= demuxer
->video
;
1081 sh_audio_t
*sh_audio
= d_audio
->sh
;
1082 sh_video_t
*sh_video
= d_video
->sh
;
1086 if (!demuxer
->seekable
) {
1087 if (demuxer
->file_format
== DEMUXER_TYPE_AVI
)
1088 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, MSGTR_CantSeekRawAVI
);
1090 else if (demuxer
->file_format
== DEMUXER_TYPE_TV
)
1091 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, MSGTR_TVInputNotSeekable
);
1094 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, MSGTR_CantSeekFile
);
1097 // clear demux buffers:
1098 demux_flush(demuxer
);
1100 sh_audio
->a_buffer_len
= 0;
1102 demuxer
->stream
->eof
= 0;
1103 demuxer
->video
->eof
= 0;
1104 demuxer
->audio
->eof
= 0;
1107 sh_video
->timer
= 0; // !!!!!!
1109 if (flags
& SEEK_ABSOLUTE
)
1112 if (demuxer
->stream_pts
== MP_NOPTS_VALUE
)
1114 pts
= demuxer
->stream_pts
;
1117 if (flags
& SEEK_FACTOR
) {
1118 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_TIME_LENGTH
, &tmp
)
1119 == STREAM_UNSUPPORTED
)
1121 pts
+= tmp
* rel_seek_secs
;
1123 pts
+= rel_seek_secs
;
1125 if (stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_TIME
, &pts
) !=
1126 STREAM_UNSUPPORTED
) {
1127 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1132 if (demuxer
->desc
->seek
)
1133 demuxer
->desc
->seek(demuxer
, rel_seek_secs
, audio_delay
, flags
);
1136 resync_audio_stream(sh_audio
);
1141 int demux_info_add(demuxer_t
*demuxer
, const char *opt
, const char *param
)
1143 char **info
= demuxer
->info
;
1147 for (n
= 0; info
&& info
[2 * n
] != NULL
; n
++) {
1148 if (!strcasecmp(opt
, info
[2 * n
])) {
1149 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_DemuxerInfoChanged
, opt
,
1151 free(info
[2 * n
+ 1]);
1152 info
[2 * n
+ 1] = strdup(param
);
1157 info
= demuxer
->info
=
1158 (char **) realloc(info
, (2 * (n
+ 2)) * sizeof(char *));
1159 info
[2 * n
] = strdup(opt
);
1160 info
[2 * n
+ 1] = strdup(param
);
1161 memset(&info
[2 * (n
+ 1)], 0, 2 * sizeof(char *));
1166 int demux_info_print(demuxer_t
*demuxer
)
1168 char **info
= demuxer
->info
;
1174 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_ClipInfo
);
1175 for (n
= 0; info
[2 * n
] != NULL
; n
++) {
1176 mp_msg(MSGT_DEMUX
, MSGL_INFO
, " %s: %s\n", info
[2 * n
],
1178 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_NAME%d=%s\n", n
,
1180 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_VALUE%d=%s\n", n
,
1183 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_N=%d\n", n
);
1188 char *demux_info_get(demuxer_t
*demuxer
, const char *opt
)
1191 char **info
= demuxer
->info
;
1193 for (i
= 0; info
&& info
[2 * i
] != NULL
; i
++) {
1194 if (!strcasecmp(opt
, info
[2 * i
]))
1195 return info
[2 * i
+ 1];
1201 int demux_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
1204 if (demuxer
->desc
->control
)
1205 return demuxer
->desc
->control(demuxer
, cmd
, arg
);
1207 return DEMUXER_CTRL_NOTIMPL
;
1212 double demuxer_get_time_length(demuxer_t
*demuxer
)
1214 double get_time_ans
;
1215 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1216 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1217 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
1219 (demuxer
, DEMUXER_CTRL_GET_TIME_LENGTH
, (void *) &get_time_ans
) <= 0) {
1220 if (sh_video
&& sh_video
->i_bps
&& sh_audio
&& sh_audio
->i_bps
)
1221 get_time_ans
= (double) (demuxer
->movi_end
-
1222 demuxer
->movi_start
) / (sh_video
->i_bps
+
1224 else if (sh_video
&& sh_video
->i_bps
)
1225 get_time_ans
= (double) (demuxer
->movi_end
-
1226 demuxer
->movi_start
) / sh_video
->i_bps
;
1227 else if (sh_audio
&& sh_audio
->i_bps
)
1228 get_time_ans
= (double) (demuxer
->movi_end
-
1229 demuxer
->movi_start
) / sh_audio
->i_bps
;
1233 return get_time_ans
;
1237 * \brief demuxer_get_current_time() returns the time of the current play in three possible ways:
1238 * either when the stream reader satisfies STREAM_CTRL_GET_CURRENT_TIME (e.g. dvd)
1239 * or using sh_video->pts when the former method fails
1241 * \return the current play time
1243 int demuxer_get_current_time(demuxer_t
*demuxer
)
1245 double get_time_ans
= 0;
1246 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1247 if (demuxer
->stream_pts
!= MP_NOPTS_VALUE
)
1248 get_time_ans
= demuxer
->stream_pts
;
1250 get_time_ans
= sh_video
->pts
;
1251 return (int) get_time_ans
;
1254 int demuxer_get_percent_pos(demuxer_t
*demuxer
)
1257 int res
= demux_control(demuxer
, DEMUXER_CTRL_GET_PERCENT_POS
, &ans
);
1258 int len
= (demuxer
->movi_end
- demuxer
->movi_start
) / 100;
1261 ans
= (demuxer
->filepos
- demuxer
->movi_start
) / len
;
1272 int demuxer_switch_audio(demuxer_t
*demuxer
, int index
)
1274 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_AUDIO
, &index
);
1275 if (res
== DEMUXER_CTRL_NOTIMPL
)
1276 index
= demuxer
->audio
->id
;
1277 if (demuxer
->audio
->id
>= 0)
1278 demuxer
->audio
->sh
= demuxer
->a_streams
[demuxer
->audio
->id
];
1280 demuxer
->audio
->sh
= NULL
;
1284 int demuxer_switch_video(demuxer_t
*demuxer
, int index
)
1286 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_VIDEO
, &index
);
1287 if (res
== DEMUXER_CTRL_NOTIMPL
)
1288 index
= demuxer
->video
->id
;
1289 if (demuxer
->video
->id
>= 0)
1290 demuxer
->video
->sh
= demuxer
->v_streams
[demuxer
->video
->id
];
1292 demuxer
->video
->sh
= NULL
;
1296 int demuxer_add_attachment(demuxer_t
*demuxer
, const char *name
,
1297 const char *type
, const void *data
, size_t size
)
1299 if (!(demuxer
->num_attachments
& 31))
1300 demuxer
->attachments
= realloc(demuxer
->attachments
,
1301 (demuxer
->num_attachments
+ 32) * sizeof(demux_attachment_t
));
1303 demuxer
->attachments
[demuxer
->num_attachments
].name
= strdup(name
);
1304 demuxer
->attachments
[demuxer
->num_attachments
].type
= strdup(type
);
1305 demuxer
->attachments
[demuxer
->num_attachments
].data
= malloc(size
);
1306 memcpy(demuxer
->attachments
[demuxer
->num_attachments
].data
, data
, size
);
1307 demuxer
->attachments
[demuxer
->num_attachments
].data_size
= size
;
1309 return demuxer
->num_attachments
++;
1312 int demuxer_add_chapter(demuxer_t
*demuxer
, const char *name
, uint64_t start
,
1315 if (demuxer
->chapters
== NULL
)
1316 demuxer
->chapters
= malloc(32 * sizeof(*demuxer
->chapters
));
1317 else if (!(demuxer
->num_chapters
% 32))
1318 demuxer
->chapters
= realloc(demuxer
->chapters
,
1319 (demuxer
->num_chapters
+ 32) *
1320 sizeof(*demuxer
->chapters
));
1322 demuxer
->chapters
[demuxer
->num_chapters
].start
= start
;
1323 demuxer
->chapters
[demuxer
->num_chapters
].end
= end
;
1324 demuxer
->chapters
[demuxer
->num_chapters
].name
= strdup(name
? name
: mp_gtext(MSGTR_Unknown
));
1326 return demuxer
->num_chapters
++;
1330 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1331 * either using the demuxer->chapters structure set by the demuxer
1332 * or asking help to the stream layer (e.g. dvd)
1333 * \param chapter - chapter number wished - 0-based
1334 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1335 * \param chapter_name name of chapter found (set by this function is param is not null)
1336 * \return -1 on error, current chapter if successful
1339 int demuxer_seek_chapter(demuxer_t
*demuxer
, int chapter
, double *seek_pts
,
1340 char **chapter_name
)
1343 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1344 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1346 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1347 demux_flush(demuxer
);
1349 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_CHAPTER
,
1351 if (ris
!= STREAM_UNSUPPORTED
)
1352 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1354 ds_fill_buffer(demuxer
->video
);
1355 resync_video_stream(sh_video
);
1359 ds_fill_buffer(demuxer
->audio
);
1360 resync_audio_stream(sh_audio
);
1362 // exit status may be ok, but main() doesn't have to seek itself
1363 // (because e.g. dvds depend on sectors, not on pts)
1367 *chapter_name
= NULL
;
1369 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1370 &num_chapters
) == STREAM_UNSUPPORTED
)
1373 *chapter_name
= talloc_size(NULL
, 16);
1374 sprintf(*chapter_name
, " of %3d", num_chapters
);
1378 return ris
!= STREAM_UNSUPPORTED
? chapter
: -1;
1379 } else { // chapters structure is set in the demuxer
1380 if (chapter
>= demuxer
->num_chapters
)
1385 *seek_pts
= demuxer
->chapters
[chapter
].start
/ 1000.0;
1388 *chapter_name
= talloc_strdup(NULL
, demuxer
->chapters
[chapter
].name
);
1394 int demuxer_get_current_chapter(demuxer_t
*demuxer
)
1397 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1398 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_CURRENT_CHAPTER
,
1399 &chapter
) == STREAM_UNSUPPORTED
)
1402 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1403 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1405 now
= (sh_video
? sh_video
->pts
: (sh_audio
? sh_audio
->pts
: 0))
1407 for (chapter
= demuxer
->num_chapters
- 1; chapter
>= 0; --chapter
) {
1408 if (demuxer
->chapters
[chapter
].start
<= now
)
1415 char *demuxer_chapter_name(demuxer_t
*demuxer
, int chapter
)
1417 if (demuxer
->num_chapters
&& demuxer
->chapters
) {
1418 if (chapter
>= 0 && chapter
< demuxer
->num_chapters
1419 && demuxer
->chapters
[chapter
].name
)
1420 return strdup(demuxer
->chapters
[chapter
].name
);
1425 char *demuxer_chapter_display_name(demuxer_t
*demuxer
, int chapter
)
1427 char *chapter_name
= demuxer_chapter_name(demuxer
, chapter
);
1429 char *tmp
= malloc(strlen(chapter_name
) + 14);
1430 snprintf(tmp
, 63, "(%d) %s", chapter
+ 1, chapter_name
);
1434 int chapter_num
= demuxer_chapter_count(demuxer
);
1436 if (chapter_num
<= 0)
1437 sprintf(tmp
, "(%d)", chapter
+ 1);
1439 sprintf(tmp
, "(%d) of %d", chapter
+ 1, chapter_num
);
1444 float demuxer_chapter_time(demuxer_t
*demuxer
, int chapter
, float *end
)
1446 if (demuxer
->num_chapters
&& demuxer
->chapters
&& chapter
>= 0
1447 && chapter
< demuxer
->num_chapters
) {
1449 *end
= demuxer
->chapters
[chapter
].end
/ 1000.0;
1450 return demuxer
->chapters
[chapter
].start
/ 1000.0;
1455 int demuxer_chapter_count(demuxer_t
*demuxer
)
1457 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1458 int num_chapters
= 0;
1459 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1460 &num_chapters
) == STREAM_UNSUPPORTED
)
1462 return num_chapters
;
1464 return demuxer
->num_chapters
;
1467 int demuxer_angles_count(demuxer_t
*demuxer
)
1469 int ris
, angles
= -1;
1471 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_ANGLES
, &angles
);
1472 if (ris
== STREAM_UNSUPPORTED
)
1477 int demuxer_get_current_angle(demuxer_t
*demuxer
)
1479 int ris
, curr_angle
= -1;
1480 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_ANGLE
, &curr_angle
);
1481 if (ris
== STREAM_UNSUPPORTED
)
1487 int demuxer_set_angle(demuxer_t
*demuxer
, int angle
)
1489 int ris
, angles
= -1;
1490 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1491 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1493 angles
= demuxer_angles_count(demuxer
);
1494 if ((angles
< 1) || (angle
> angles
))
1497 demux_flush(demuxer
);
1499 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SET_ANGLE
, &angle
);
1500 if (ris
== STREAM_UNSUPPORTED
)
1503 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1505 ds_fill_buffer(demuxer
->video
);
1506 resync_video_stream(sh_video
);
1510 ds_fill_buffer(demuxer
->audio
);
1511 resync_audio_stream(sh_audio
);
1517 int demuxer_audio_track_by_lang(demuxer_t
*d
, char *lang
)
1520 lang
+= strspn(lang
, ",");
1521 while ((len
= strcspn(lang
, ",")) > 0) {
1522 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1523 sh_audio_t
*sh
= d
->a_streams
[i
];
1524 if (sh
&& sh
->lang
&& strncmp(sh
->lang
, lang
, len
) == 0)
1528 lang
+= strspn(lang
, ",");
1533 int demuxer_sub_track_by_lang(demuxer_t
*d
, char *lang
)
1536 lang
+= strspn(lang
, ",");
1537 while ((len
= strcspn(lang
, ",")) > 0) {
1538 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1539 sh_sub_t
*sh
= d
->s_streams
[i
];
1540 if (sh
&& sh
->lang
&& strncmp(sh
->lang
, lang
, len
) == 0)
1544 lang
+= strspn(lang
, ",");
1549 int demuxer_default_audio_track(demuxer_t
*d
)
1552 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1553 sh_audio_t
*sh
= d
->a_streams
[i
];
1554 if (sh
&& sh
->default_track
)
1557 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1558 sh_audio_t
*sh
= d
->a_streams
[i
];
1565 int demuxer_default_sub_track(demuxer_t
*d
)
1568 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1569 sh_sub_t
*sh
= d
->s_streams
[i
];
1570 if (sh
&& sh
->default_track
)