4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/types.h>
35 #include "libvo/fastmemcpy.h"
37 #include "stream/stream.h"
42 #include "libaf/af_format.h"
43 #include "libmpcodecs/dec_teletext.h"
47 #ifdef CONFIG_LIBAVCODEC
48 #include "libavcodec/avcodec.h"
49 #if MP_INPUT_BUFFER_PADDING_SIZE < FF_INPUT_BUFFER_PADDING_SIZE
50 #error MP_INPUT_BUFFER_PADDING_SIZE is too small!
54 // This is quite experimental, in particular it will mess up the pts values
55 // in the queue - on the other hand it might fix some issues like generating
56 // broken files with mencoder and stream copy.
57 // Better leave it disabled for now, if we find no use for it this code should
58 // just be removed again.
59 #define PARSE_ON_ADD 0
61 static void clear_parser(sh_common_t
*sh
);
64 extern const demuxer_desc_t demuxer_desc_rawaudio
;
65 extern const demuxer_desc_t demuxer_desc_rawvideo
;
66 extern const demuxer_desc_t demuxer_desc_tv
;
67 extern const demuxer_desc_t demuxer_desc_mf
;
68 extern const demuxer_desc_t demuxer_desc_avi
;
69 extern const demuxer_desc_t demuxer_desc_y4m
;
70 extern const demuxer_desc_t demuxer_desc_asf
;
71 extern const demuxer_desc_t demuxer_desc_real
;
72 extern const demuxer_desc_t demuxer_desc_smjpeg
;
73 extern const demuxer_desc_t demuxer_desc_matroska
;
74 extern const demuxer_desc_t demuxer_desc_realaudio
;
75 extern const demuxer_desc_t demuxer_desc_vqf
;
76 extern const demuxer_desc_t demuxer_desc_mov
;
77 extern const demuxer_desc_t demuxer_desc_vivo
;
78 extern const demuxer_desc_t demuxer_desc_fli
;
79 extern const demuxer_desc_t demuxer_desc_film
;
80 extern const demuxer_desc_t demuxer_desc_roq
;
81 extern const demuxer_desc_t demuxer_desc_gif
;
82 extern const demuxer_desc_t demuxer_desc_ogg
;
83 extern const demuxer_desc_t demuxer_desc_avs
;
84 extern const demuxer_desc_t demuxer_desc_pva
;
85 extern const demuxer_desc_t demuxer_desc_nsv
;
86 extern const demuxer_desc_t demuxer_desc_mpeg_ts
;
87 extern const demuxer_desc_t demuxer_desc_lmlm4
;
88 extern const demuxer_desc_t demuxer_desc_mpeg_ps
;
89 extern const demuxer_desc_t demuxer_desc_mpeg_pes
;
90 extern const demuxer_desc_t demuxer_desc_mpeg_es
;
91 extern const demuxer_desc_t demuxer_desc_mpeg_gxf
;
92 extern const demuxer_desc_t demuxer_desc_mpeg4_es
;
93 extern const demuxer_desc_t demuxer_desc_h264_es
;
94 extern const demuxer_desc_t demuxer_desc_rawdv
;
95 extern const demuxer_desc_t demuxer_desc_mpc
;
96 extern const demuxer_desc_t demuxer_desc_audio
;
97 extern const demuxer_desc_t demuxer_desc_xmms
;
98 extern const demuxer_desc_t demuxer_desc_mpeg_ty
;
99 extern const demuxer_desc_t demuxer_desc_rtp
;
100 extern const demuxer_desc_t demuxer_desc_rtp_nemesi
;
101 extern const demuxer_desc_t demuxer_desc_lavf
;
102 extern const demuxer_desc_t demuxer_desc_lavf_preferred
;
103 extern const demuxer_desc_t demuxer_desc_aac
;
104 extern const demuxer_desc_t demuxer_desc_nut
;
105 extern const demuxer_desc_t demuxer_desc_mng
;
107 /* Please do not add any new demuxers here. If you want to implement a new
108 * demuxer, add it to libavformat, except for wrappers around external
109 * libraries and demuxers requiring binary support. */
111 const demuxer_desc_t
*const demuxer_list
[] = {
112 &demuxer_desc_rawaudio
,
113 &demuxer_desc_rawvideo
,
118 #ifdef CONFIG_LIBAVFORMAT
119 &demuxer_desc_lavf_preferred
,
126 &demuxer_desc_smjpeg
,
127 &demuxer_desc_matroska
,
128 &demuxer_desc_realaudio
,
138 #ifdef CONFIG_OGGVORBIS
141 #ifdef CONFIG_WIN32DLL
145 &demuxer_desc_mpeg_ts
,
147 &demuxer_desc_mpeg_ps
,
148 &demuxer_desc_mpeg_pes
,
149 &demuxer_desc_mpeg_es
,
150 &demuxer_desc_mpeg_gxf
,
151 &demuxer_desc_mpeg4_es
,
152 &demuxer_desc_h264_es
,
154 &demuxer_desc_mpeg_ty
,
155 #ifdef CONFIG_LIVE555
158 #ifdef CONFIG_LIBNEMESI
159 &demuxer_desc_rtp_nemesi
,
161 #ifdef CONFIG_LIBAVFORMAT
164 #ifdef CONFIG_MUSEPACK
167 #ifdef CONFIG_LIBDV095
180 /* Please do not add any new demuxers here. If you want to implement a new
181 * demuxer, add it to libavformat, except for wrappers around external
182 * libraries and demuxers requiring binary support. */
186 void free_demuxer_stream(demux_stream_t
*ds
)
192 demux_stream_t
*new_demuxer_stream(struct demuxer
*demuxer
, int id
)
194 demux_stream_t
*ds
= malloc(sizeof(demux_stream_t
));
195 *ds
= (demux_stream_t
){
205 * Get demuxer description structure for a given demuxer type
207 * @param file_format type of the demuxer
208 * @return structure for the demuxer, NULL if not found
210 static const demuxer_desc_t
*get_demuxer_desc_from_type(int file_format
)
214 for (i
= 0; demuxer_list
[i
]; i
++)
215 if (file_format
== demuxer_list
[i
]->type
)
216 return demuxer_list
[i
];
222 demuxer_t
*new_demuxer(struct MPOpts
*opts
, stream_t
*stream
, int type
,
223 int a_id
, int v_id
, int s_id
, char *filename
)
225 struct demuxer
*d
= talloc_zero(NULL
, struct demuxer
);
227 d
->stream_pts
= MP_NOPTS_VALUE
;
228 d
->reference_clock
= MP_NOPTS_VALUE
;
229 d
->movi_start
= stream
->start_pos
;
230 d
->movi_end
= stream
->end_pos
;
234 d
->audio
= new_demuxer_stream(d
, a_id
);
235 d
->video
= new_demuxer_stream(d
, v_id
);
236 d
->sub
= new_demuxer_stream(d
, s_id
);
240 if (!(d
->desc
= get_demuxer_desc_from_type(type
)))
241 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
242 "BUG! Invalid demuxer type in new_demuxer(), "
243 "big troubles ahead.");
244 if (filename
) // Filename hack for avs_check_file
245 d
->filename
= strdup(filename
);
246 stream_seek(stream
, stream
->start_pos
);
250 sh_sub_t
*new_sh_sub_sid(demuxer_t
*demuxer
, int id
, int sid
)
252 if (id
> MAX_S_STREAMS
- 1 || id
< 0) {
253 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
254 "Requested sub stream id overflow (%d > %d)\n", id
,
258 if (demuxer
->s_streams
[id
])
259 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, "Sub stream %i redefined\n", id
);
261 sh_sub_t
*sh
= calloc(1, sizeof(sh_sub_t
));
262 demuxer
->s_streams
[id
] = sh
;
264 sh
->opts
= demuxer
->opts
;
265 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SUBTITLE_ID=%d\n", sid
);
267 if (sid
== demuxer
->opts
->sub_id
) {
268 demuxer
->sub
->id
= id
;
269 demuxer
->sub
->sh
= demuxer
->s_streams
[id
];
271 return demuxer
->s_streams
[id
];
274 static void free_sh_sub(sh_sub_t
*sh
)
276 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_sub at %p\n", sh
);
280 ass_free_track(sh
->ass_track
);
283 #ifdef CONFIG_LIBAVCODEC
284 clear_parser((sh_common_t
*)sh
);
289 sh_audio_t
*new_sh_audio_aid(demuxer_t
*demuxer
, int id
, int aid
)
291 if (id
> MAX_A_STREAMS
- 1 || id
< 0) {
292 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
293 "Requested audio stream id overflow (%d > %d)\n", id
,
297 if (demuxer
->a_streams
[id
]) {
298 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, "WARNING: Audio stream header %d redefined.\n", id
);
300 mp_tmsg(MSGT_DEMUXER
, MSGL_V
, "==> Found audio stream: %d\n", id
);
301 sh_audio_t
*sh
= calloc(1, sizeof(sh_audio_t
));
302 demuxer
->a_streams
[id
] = sh
;
304 sh
->ds
= demuxer
->audio
;
307 sh
->sample_format
= AF_FORMAT_S16_NE
;
308 sh
->opts
= demuxer
->opts
;
309 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_ID=%d\n", aid
);
311 return demuxer
->a_streams
[id
];
314 void free_sh_audio(demuxer_t
*demuxer
, int id
)
316 sh_audio_t
*sh
= demuxer
->a_streams
[id
];
317 demuxer
->a_streams
[id
] = NULL
;
318 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_audio at %p\n", sh
);
322 #ifdef CONFIG_LIBAVCODEC
323 clear_parser((sh_common_t
*)sh
);
328 sh_video_t
*new_sh_video_vid(demuxer_t
*demuxer
, int id
, int vid
)
330 if (id
> MAX_V_STREAMS
- 1 || id
< 0) {
331 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
332 "Requested video stream id overflow (%d > %d)\n", id
,
336 if (demuxer
->v_streams
[id
])
337 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, "WARNING: Video stream header %d redefined.\n", id
);
339 mp_tmsg(MSGT_DEMUXER
, MSGL_V
, "==> Found video stream: %d\n", id
);
340 sh_video_t
*sh
= calloc(1, sizeof *sh
);
341 demuxer
->v_streams
[id
] = sh
;
343 sh
->ds
= demuxer
->video
;
344 sh
->opts
= demuxer
->opts
;
345 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_ID=%d\n", vid
);
347 return demuxer
->v_streams
[id
];
350 void free_sh_video(sh_video_t
*sh
)
352 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_video at %p\n", sh
);
354 #ifdef CONFIG_LIBAVCODEC
355 clear_parser((sh_common_t
*)sh
);
360 void free_demuxer(demuxer_t
*demuxer
)
363 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing %s demuxer at %p\n",
364 demuxer
->desc
->shortdesc
, demuxer
);
365 if (demuxer
->desc
->close
)
366 demuxer
->desc
->close(demuxer
);
367 // Very ugly hack to make it behave like old implementation
368 if (demuxer
->desc
->type
== DEMUXER_TYPE_DEMUXERS
)
369 goto skip_streamfree
;
371 for (i
= 0; i
< MAX_A_STREAMS
; i
++)
372 if (demuxer
->a_streams
[i
])
373 free_sh_audio(demuxer
, i
);
374 for (i
= 0; i
< MAX_V_STREAMS
; i
++)
375 if (demuxer
->v_streams
[i
])
376 free_sh_video(demuxer
->v_streams
[i
]);
377 for (i
= 0; i
< MAX_S_STREAMS
; i
++)
378 if (demuxer
->s_streams
[i
])
379 free_sh_sub(demuxer
->s_streams
[i
]);
381 free_demuxer_stream(demuxer
->audio
);
382 free_demuxer_stream(demuxer
->video
);
383 free_demuxer_stream(demuxer
->sub
);
385 free(demuxer
->filename
);
386 if (demuxer
->teletext
)
387 teletext_control(demuxer
->teletext
, TV_VBI_CONTROL_STOP
, NULL
);
388 talloc_free(demuxer
);
392 static void ds_add_packet_internal(demux_stream_t
*ds
, demux_packet_t
*dp
)
394 // append packet to DS stream:
396 ds
->bytes
+= dp
->len
;
398 // next packet in stream
402 // first packet in stream
403 ds
->first
= ds
->last
= dp
;
405 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
406 "DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
407 (ds
== ds
->demuxer
->audio
) ? "d_audio" : "d_video", dp
->len
,
408 dp
->pts
, (unsigned int) dp
->pos
, ds
->demuxer
->audio
->packs
,
409 ds
->demuxer
->video
->packs
);
412 #ifdef CONFIG_LIBAVCODEC
413 static void allocate_parser(AVCodecContext
**avctx
, AVCodecParserContext
**parser
, unsigned format
)
415 enum CodecID codec_id
= CODEC_ID_NONE
;
416 extern int avcodec_initialized
;
417 if (!avcodec_initialized
) {
419 avcodec_register_all();
420 avcodec_initialized
= 1;
426 case MKTAG('d', 'n', 'e', 't'):
427 case MKTAG('s', 'a', 'c', '3'):
428 codec_id
= CODEC_ID_AC3
;
430 case MKTAG('E', 'A', 'C', '3'):
431 codec_id
= CODEC_ID_EAC3
;
435 codec_id
= CODEC_ID_DTS
;
437 case MKTAG('M', 'L', 'P', ' '):
438 codec_id
= CODEC_ID_MLP
;
442 case MKTAG('.', 'm', 'p', '3'):
443 case MKTAG('M', 'P', 'E', ' '):
444 case MKTAG('L', 'A', 'M', 'E'):
445 codec_id
= CODEC_ID_MP3
;
448 case MKTAG('.', 'm', 'p', '2'):
449 case MKTAG('.', 'm', 'p', '1'):
450 codec_id
= CODEC_ID_MP2
;
452 case MKTAG('T', 'R', 'H', 'D'):
453 codec_id
= CODEC_ID_TRUEHD
;
456 if (codec_id
!= CODEC_ID_NONE
) {
457 *avctx
= avcodec_alloc_context();
460 *parser
= av_parser_init(codec_id
);
466 static void get_parser(sh_common_t
*sh
, AVCodecContext
**avctx
, AVCodecParserContext
**parser
)
471 if (!sh
|| !sh
->needs_parsing
)
475 *parser
= sh
->parser
;
479 allocate_parser(avctx
, parser
, sh
->format
);
481 sh
->parser
= *parser
;
484 int ds_parse(demux_stream_t
*ds
, uint8_t **buffer
, int *len
, double pts
, off_t pos
)
486 AVCodecContext
*avctx
;
487 AVCodecParserContext
*parser
;
488 get_parser(ds
->sh
, &avctx
, &parser
);
491 return av_parser_parse2(parser
, avctx
, buffer
, len
, *buffer
, *len
, pts
, pts
, pos
);
494 static void clear_parser(sh_common_t
*sh
)
496 av_parser_close(sh
->parser
);
498 av_freep(&sh
->avctx
);
501 void ds_clear_parser(demux_stream_t
*ds
)
505 clear_parser(ds
->sh
);
509 void ds_add_packet(demux_stream_t
*ds
, demux_packet_t
*dp
)
511 #if PARSE_ON_ADD && defined(CONFIG_LIBAVCODEC)
515 uint8_t *parsed_start
= dp
->buffer
+ pos
;
516 int parsed_len
= len
;
517 int consumed
= ds_parse(ds
->sh
, &parsed_start
, &parsed_len
, dp
->pts
, dp
->pos
);
520 if (parsed_start
== dp
->buffer
&& parsed_len
== dp
->len
) {
521 ds_add_packet_internal(ds
, dp
);
522 } else if (parsed_len
) {
523 demux_packet_t
*dp2
= new_demux_packet(parsed_len
);
525 dp2
->pts
= dp
->pts
; // should be parser->pts but that works badly
526 memcpy(dp2
->buffer
, parsed_start
, parsed_len
);
527 ds_add_packet_internal(ds
, dp2
);
531 ds_add_packet_internal(ds
, dp
);
535 void ds_read_packet(demux_stream_t
*ds
, stream_t
*stream
, int len
,
536 double pts
, off_t pos
, int flags
)
538 demux_packet_t
*dp
= new_demux_packet(len
);
539 len
= stream_read(stream
, dp
->buffer
, len
);
540 resize_demux_packet(dp
, len
);
544 // append packet to DS stream:
545 ds_add_packet(ds
, dp
);
549 // 0 = EOF or no stream found or invalid type
550 // 1 = successfully read a packet
552 int demux_fill_buffer(demuxer_t
*demux
, demux_stream_t
*ds
)
554 // Note: parameter 'ds' can be NULL!
555 return demux
->desc
->fill_buffer(demux
, ds
);
561 #define MAX_ACUMULATED_PACKETS 64
562 int ds_fill_buffer(demux_stream_t
*ds
)
564 demuxer_t
*demux
= ds
->demuxer
;
566 free_demux_packet(ds
->current
);
568 if (mp_msg_test(MSGT_DEMUXER
, MSGL_DBG3
)) {
569 if (ds
== demux
->audio
)
570 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
571 "ds_fill_buffer(d_audio) called\n");
572 else if (ds
== demux
->video
)
573 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
574 "ds_fill_buffer(d_video) called\n");
575 else if (ds
== demux
->sub
)
576 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
, "ds_fill_buffer(d_sub) called\n");
578 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
579 "ds_fill_buffer(unknown 0x%X) called\n", (unsigned int) ds
);
583 demux_packet_t
*p
= ds
->first
;
585 if (demux
->reference_clock
!= MP_NOPTS_VALUE
) {
586 if ( p
->pts
!= MP_NOPTS_VALUE
587 && p
->pts
> demux
->reference_clock
588 && ds
->packs
< MAX_ACUMULATED_PACKETS
) {
589 if (demux_fill_buffer(demux
, ds
))
595 ds
->buffer
= p
->buffer
;
597 ds
->buffer_size
= p
->len
;
599 ds
->dpos
+= p
->len
; // !!!
601 if (p
->pts
!= MP_NOPTS_VALUE
) {
605 ds
->pts_bytes
+= p
->len
; // !!!
606 if (p
->stream_pts
!= MP_NOPTS_VALUE
)
607 demux
->stream_pts
= p
->stream_pts
;
608 ds
->flags
= p
->flags
;
616 /* The code below can set ds->eof to 1 when another stream runs
617 * out of buffer space. That makes sense because in that situation
618 * the calling code should not count on being able to demux more
619 * packets from this stream.
620 * If however the situation improves and we're called again
621 * despite the eof flag then it's better to clear it to avoid
627 #define MaybeNI _("Maybe you are playing a non-interleaved stream/file or the codec failed?\n" \
628 "For AVI files, try to force non-interleaved mode with the -ni option.\n")
630 if (demux
->audio
->packs
>= MAX_PACKS
631 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
632 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
633 demux
->audio
->packs
, demux
->audio
->bytes
);
634 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
637 if (demux
->video
->packs
>= MAX_PACKS
638 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
639 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
640 demux
->video
->packs
, demux
->video
->bytes
);
641 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
644 if (!demux_fill_buffer(demux
, ds
)) {
645 #if PARSE_ON_ADD && defined(CONFIG_LIBAVCODEC)
646 uint8_t *parsed_start
= NULL
;
648 ds_parse(ds
->sh
, &parsed_start
, &parsed_len
, MP_NOPTS_VALUE
, 0);
650 demux_packet_t
*dp2
= new_demux_packet(parsed_len
);
651 dp2
->pts
= MP_NOPTS_VALUE
;
652 memcpy(dp2
->buffer
, parsed_start
, parsed_len
);
653 ds_add_packet_internal(ds
, dp2
);
657 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
658 "ds_fill_buffer()->demux_fill_buffer() failed\n");
662 ds
->buffer_pos
= ds
->buffer_size
= 0;
664 mp_msg(MSGT_DEMUXER
, MSGL_V
,
665 "ds_fill_buffer: EOF reached (stream: %s) \n",
666 ds
== demux
->audio
? "audio" : "video");
671 int demux_read_data(demux_stream_t
*ds
, unsigned char *mem
, int len
)
676 x
= ds
->buffer_size
- ds
->buffer_pos
;
678 if (!ds_fill_buffer(ds
))
684 fast_memcpy(mem
+ bytes
, &ds
->buffer
[ds
->buffer_pos
], x
);
694 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
695 * \param mem memory to read data into, may be NULL to discard data
696 * \param maxlen maximum number of bytes to read
697 * \param read number of bytes actually read
698 * \param pattern pattern to search for (lowest 8 bits are ignored)
699 * \return whether pattern was found
701 int demux_pattern_3(demux_stream_t
*ds
, unsigned char *mem
, int maxlen
,
702 int *read
, uint32_t pattern
)
704 register uint32_t head
= 0xffffff00;
705 register uint32_t pat
= pattern
& 0xffffff00;
708 register unsigned char *ds_buf
= &ds
->buffer
[ds
->buffer_size
];
709 int len
= ds
->buffer_size
- ds
->buffer_pos
;
710 register long pos
= -len
;
711 if (unlikely(pos
>= 0)) { // buffer is empty
718 } while (++pos
&& head
!= pat
);
720 if (total_len
+ len
> maxlen
)
721 len
= maxlen
- total_len
;
722 len
= demux_read_data(ds
, mem
? &mem
[total_len
] : NULL
, len
);
724 } while ((head
!= pat
|| total_len
< 3) && total_len
< maxlen
&& !ds
->eof
);
727 return total_len
>= 3 && head
== pat
;
730 void ds_free_packs(demux_stream_t
*ds
)
732 demux_packet_t
*dp
= ds
->first
;
734 demux_packet_t
*dn
= dp
->next
;
735 free_demux_packet(dp
);
738 if (ds
->asf_packet
) {
739 // free unfinished .asf fragments:
740 free(ds
->asf_packet
->buffer
);
741 free(ds
->asf_packet
);
742 ds
->asf_packet
= NULL
;
744 ds
->first
= ds
->last
= NULL
;
745 ds
->packs
= 0; // !!!!!
748 free_demux_packet(ds
->current
);
751 ds
->buffer_pos
= ds
->buffer_size
;
756 int ds_get_packet(demux_stream_t
*ds
, unsigned char **start
)
759 if (ds
->buffer_pos
>= ds
->buffer_size
) {
760 if (!ds_fill_buffer(ds
)) {
766 len
= ds
->buffer_size
- ds
->buffer_pos
;
767 *start
= &ds
->buffer
[ds
->buffer_pos
];
768 ds
->buffer_pos
+= len
;
772 int ds_get_packet_pts(demux_stream_t
*ds
, unsigned char **start
, double *pts
)
775 *pts
= MP_NOPTS_VALUE
;
776 len
= ds_get_packet(ds
, start
);
779 // Return pts unless this read starts from the middle of a packet
780 if (len
== ds
->buffer_pos
)
781 *pts
= ds
->current
->pts
;
785 int ds_get_packet_sub(demux_stream_t
*ds
, unsigned char **start
)
788 if (ds
->buffer_pos
>= ds
->buffer_size
) {
792 if (!ds_fill_buffer(ds
))
795 len
= ds
->buffer_size
- ds
->buffer_pos
;
796 *start
= &ds
->buffer
[ds
->buffer_pos
];
797 ds
->buffer_pos
+= len
;
801 double ds_get_next_pts(demux_stream_t
*ds
)
803 demuxer_t
*demux
= ds
->demuxer
;
805 if (demux
->audio
->packs
>= MAX_PACKS
806 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
807 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
808 demux
->audio
->packs
, demux
->audio
->bytes
);
809 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
810 return MP_NOPTS_VALUE
;
812 if (demux
->video
->packs
>= MAX_PACKS
813 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
814 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
815 demux
->video
->packs
, demux
->video
->bytes
);
816 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
817 return MP_NOPTS_VALUE
;
819 if (!demux_fill_buffer(demux
, ds
))
820 return MP_NOPTS_VALUE
;
822 return ds
->first
->pts
;
825 // ====================================================================
827 void demuxer_help(void)
831 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "Available demuxers:\n");
832 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, " demuxer: type info: (comment)\n");
833 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DEMUXERS\n");
834 for (i
= 0; demuxer_list
[i
]; i
++) {
835 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Don't display special demuxers
837 if (demuxer_list
[i
]->comment
&& strlen(demuxer_list
[i
]->comment
))
838 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s (%s)\n",
839 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
840 demuxer_list
[i
]->info
, demuxer_list
[i
]->comment
);
842 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s\n",
843 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
844 demuxer_list
[i
]->info
);
850 * Get demuxer type for a given demuxer name
852 * @param demuxer_name string with demuxer name of demuxer number
853 * @param force will be set if demuxer should be forced.
855 * @return DEMUXER_TYPE_xxx, -1 if error or not found
857 int get_demuxer_type_from_name(char *demuxer_name
, int *force
)
863 if (!demuxer_name
|| !demuxer_name
[0])
864 return DEMUXER_TYPE_UNKNOWN
;
866 *force
= demuxer_name
[0] == '+';
867 if (demuxer_name
[0] == '+')
868 demuxer_name
= &demuxer_name
[1];
869 for (i
= 0; demuxer_list
[i
]; i
++) {
870 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Can't select special demuxers from commandline
872 if (strcmp(demuxer_name
, demuxer_list
[i
]->name
) == 0)
873 return demuxer_list
[i
]->type
;
876 // No match found, try to parse name as an integer (demuxer number)
877 type_int
= strtol(demuxer_name
, &endptr
, 0);
878 if (*endptr
) // Conversion failed
880 if ((type_int
> 0) && (type_int
<= DEMUXER_TYPE_MAX
))
881 return (int) type_int
;
886 int extension_parsing
= 1; // 0=off 1=mixed (used only for unstable formats)
889 NOTE : Several demuxers may be opened at the same time so
890 demuxers should NEVER rely on an external var to enable them
891 self. If a demuxer can't do any auto-detection it should only use
892 file_format. The user can explicitly set file_format with the -demuxer
893 option so there is really no need for another extra var.
894 For convenience an option can be added to set file_format directly
895 to the right type (ex: rawaudio,rawvideo).
896 Also the stream can override the file_format so a demuxer which rely
897 on a special stream type can set file_format at the stream level
901 static demuxer_t
*demux_open_stream(struct MPOpts
*opts
, stream_t
*stream
,
902 int file_format
, int force
, int audio_id
,
903 int video_id
, int dvdsub_id
,
906 demuxer_t
*demuxer
= NULL
;
908 sh_video_t
*sh_video
= NULL
;
910 const demuxer_desc_t
*demuxer_desc
;
914 // If somebody requested a demuxer check it
916 if ((demuxer_desc
= get_demuxer_desc_from_type(file_format
))) {
917 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
918 video_id
, dvdsub_id
, filename
);
919 if (demuxer_desc
->check_file
)
920 fformat
= demuxer_desc
->check_file(demuxer
);
921 if (force
|| !demuxer_desc
->check_file
)
922 fformat
= demuxer_desc
->type
;
924 if (fformat
== demuxer_desc
->type
) {
925 demuxer_t
*demux2
= demuxer
;
926 // Move messages to demuxer detection code?
927 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
928 "%s file format detected.\n",
929 demuxer_desc
->shortdesc
);
930 file_format
= fformat
;
931 if (!demuxer
->desc
->open
932 || (demux2
= demuxer
->desc
->open(demuxer
))) {
937 // Format changed after check, recurse
938 free_demuxer(demuxer
);
939 return demux_open_stream(opts
, stream
, fformat
, force
,
940 audio_id
, video_id
, dvdsub_id
,
944 // Check failed for forced demuxer, quit
945 free_demuxer(demuxer
);
949 // Test demuxers with safe file checks
950 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
951 if (demuxer_desc
->safe_check
) {
952 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
953 video_id
, dvdsub_id
, filename
);
954 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
955 if (fformat
== demuxer_desc
->type
) {
956 demuxer_t
*demux2
= demuxer
;
957 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
958 "%s file format detected.\n",
959 demuxer_desc
->shortdesc
);
960 file_format
= fformat
;
961 if (!demuxer
->desc
->open
962 || (demux2
= demuxer
->desc
->open(demuxer
))) {
967 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
968 return demuxer
; // handled in mplayer.c
969 // Format changed after check, recurse
970 free_demuxer(demuxer
);
971 demuxer
= demux_open_stream(opts
, stream
, fformat
, force
,
973 dvdsub_id
, filename
);
975 return demuxer
; // done!
976 file_format
= DEMUXER_TYPE_UNKNOWN
;
979 free_demuxer(demuxer
);
984 // If no forced demuxer perform file extension based detection
985 // Ok. We're over the stable detectable fileformats, the next ones are
986 // a bit fuzzy. So by default (extension_parsing==1) try extension-based
988 if (file_format
== DEMUXER_TYPE_UNKNOWN
&& filename
989 && extension_parsing
== 1) {
990 file_format
= demuxer_type_by_filename(filename
);
991 if (file_format
!= DEMUXER_TYPE_UNKNOWN
) {
992 // we like recursion :)
993 demuxer
= demux_open_stream(opts
, stream
, file_format
, force
,
994 audio_id
, video_id
, dvdsub_id
,
997 return demuxer
; // done!
998 file_format
= DEMUXER_TYPE_UNKNOWN
; // continue fuzzy guessing...
999 mp_msg(MSGT_DEMUXER
, MSGL_V
,
1000 "demuxer: continue fuzzy content-based format guessing...\n");
1003 // Try detection for all other demuxers
1004 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
1005 if (!demuxer_desc
->safe_check
&& demuxer_desc
->check_file
) {
1006 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
1007 video_id
, dvdsub_id
, filename
);
1008 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
1009 if (fformat
== demuxer_desc
->type
) {
1010 demuxer_t
*demux2
= demuxer
;
1011 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
1012 "%s file format detected.\n",
1013 demuxer_desc
->shortdesc
);
1014 file_format
= fformat
;
1015 if (!demuxer
->desc
->open
1016 || (demux2
= demuxer
->desc
->open(demuxer
))) {
1021 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
1022 return demuxer
; // handled in mplayer.c
1023 // Format changed after check, recurse
1024 free_demuxer(demuxer
);
1025 demuxer
= demux_open_stream(opts
, stream
, fformat
, force
,
1027 dvdsub_id
, filename
);
1029 return demuxer
; // done!
1030 file_format
= DEMUXER_TYPE_UNKNOWN
;
1033 free_demuxer(demuxer
);
1039 //====== File format recognized, set up these for compatibility: =========
1042 demuxer
->file_format
= file_format
;
1044 if ((sh_video
= demuxer
->video
->sh
) && sh_video
->bih
) {
1045 int biComp
= le2me_32(sh_video
->bih
->biCompression
);
1046 mp_msg(MSGT_DEMUX
, MSGL_INFO
,
1047 "VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
1048 (char *) &biComp
, sh_video
->bih
->biWidth
,
1049 sh_video
->bih
->biHeight
, sh_video
->bih
->biBitCount
,
1050 sh_video
->fps
, sh_video
->i_bps
* 0.008f
,
1051 sh_video
->i_bps
/ 1024.0f
);
1054 if (opts
->ass_enabled
&& ass_library
) {
1055 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1056 sh_sub_t
*sh
= demuxer
->s_streams
[i
];
1057 if (sh
&& sh
->type
== 'a') {
1058 sh
->ass_track
= ass_new_track(ass_library
);
1059 if (sh
->ass_track
&& sh
->extradata
)
1060 ass_process_codec_private(sh
->ass_track
, sh
->extradata
,
1062 } else if (sh
&& sh
->type
!= 'v')
1063 sh
->ass_track
= ass_default_track(ass_library
);
1070 char *audio_stream
= NULL
;
1071 char *sub_stream
= NULL
;
1072 int audio_stream_cache
= 0;
1074 char *demuxer_name
= NULL
; // parameter from -demuxer
1075 char *audio_demuxer_name
= NULL
; // parameter from -audio-demuxer
1076 char *sub_demuxer_name
= NULL
; // parameter from -sub-demuxer
1078 extern int hr_mp3_seek
;
1080 extern float stream_cache_min_percent
;
1081 extern float stream_cache_seek_min_percent
;
1083 demuxer_t
*demux_open(struct MPOpts
*opts
, stream_t
*vs
, int file_format
,
1084 int audio_id
, int video_id
, int dvdsub_id
,
1087 stream_t
*as
= NULL
, *ss
= NULL
;
1088 demuxer_t
*vd
, *ad
= NULL
, *sd
= NULL
;
1090 int afmt
= DEMUXER_TYPE_UNKNOWN
, sfmt
= DEMUXER_TYPE_UNKNOWN
;
1092 int audio_demuxer_type
= 0, sub_demuxer_type
= 0;
1093 int demuxer_force
= 0, audio_demuxer_force
= 0, sub_demuxer_force
= 0;
1096 get_demuxer_type_from_name(demuxer_name
, &demuxer_force
)) < 0) {
1097 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-demuxer %s does not exist.\n",
1100 if ((audio_demuxer_type
=
1101 get_demuxer_type_from_name(audio_demuxer_name
,
1102 &audio_demuxer_force
)) < 0) {
1103 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-audio-demuxer %s does not exist.\n",
1104 audio_demuxer_name
);
1106 if ((sub_demuxer_type
=
1107 get_demuxer_type_from_name(sub_demuxer_name
,
1108 &sub_demuxer_force
)) < 0) {
1109 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-sub-demuxer %s does not exist.\n",
1114 as
= open_stream(audio_stream
, 0, &afmt
);
1116 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "Cannot open audio stream: %s\n",
1120 if (audio_stream_cache
) {
1121 if (!stream_enable_cache
1122 (as
, audio_stream_cache
* 1024,
1123 audio_stream_cache
* 1024 * (stream_cache_min_percent
/
1125 audio_stream_cache
* 1024 * (stream_cache_seek_min_percent
/
1128 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
1129 "Can't enable audio stream cache\n");
1135 ss
= open_stream(sub_stream
, 0, &sfmt
);
1137 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "Cannot open subtitle stream: %s\n",
1143 vd
= demux_open_stream(opts
, vs
, demuxer_type
? demuxer_type
: file_format
,
1144 demuxer_force
, audio_stream
? -2 : audio_id
,
1145 video_id
, sub_stream
? -2 : dvdsub_id
, filename
);
1154 ad
= demux_open_stream(opts
, as
,
1155 audio_demuxer_type
? audio_demuxer_type
: afmt
,
1156 audio_demuxer_force
, audio_id
, -2, -2,
1159 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, "Failed to open audio demuxer: %s\n",
1162 } else if (ad
->audio
->sh
1163 && ((sh_audio_t
*) ad
->audio
->sh
)->format
== 0x55) // MP3
1164 hr_mp3_seek
= 1; // Enable high res seeking
1167 sd
= demux_open_stream(opts
, ss
,
1168 sub_demuxer_type
? sub_demuxer_type
: sfmt
,
1169 sub_demuxer_force
, -2, -2, dvdsub_id
,
1172 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
,
1173 "Failed to open subtitle demuxer: %s\n", sub_stream
);
1179 res
= new_demuxers_demuxer(vd
, ad
, sd
);
1181 res
= new_demuxers_demuxer(vd
, ad
, vd
);
1183 res
= new_demuxers_demuxer(vd
, vd
, sd
);
1187 opts
->correct_pts
= opts
->user_correct_pts
;
1188 if (opts
->correct_pts
< 0)
1190 demux_control(res
, DEMUXER_CTRL_CORRECT_PTS
,
1191 NULL
) == DEMUXER_CTRL_OK
;
1196 void demux_flush(demuxer_t
*demuxer
)
1199 ds_clear_parser(demuxer
->video
);
1200 ds_clear_parser(demuxer
->audio
);
1201 ds_clear_parser(demuxer
->sub
);
1203 ds_free_packs(demuxer
->video
);
1204 ds_free_packs(demuxer
->audio
);
1205 ds_free_packs(demuxer
->sub
);
1208 int demux_seek(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
,
1211 if (!demuxer
->seekable
) {
1212 if (demuxer
->file_format
== DEMUXER_TYPE_AVI
)
1213 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "Cannot seek in raw AVI streams. (Index required, try with the -idx switch.)\n");
1215 else if (demuxer
->file_format
== DEMUXER_TYPE_TV
)
1216 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "TV input is not seekable! (Seeking will probably be for changing channels ;)\n");
1219 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "Cannot seek in this file.\n");
1222 // clear demux buffers:
1223 demux_flush(demuxer
);
1224 demuxer
->video
->eof
= 0;
1225 demuxer
->audio
->eof
= 0;
1227 /* HACK: assume any demuxer used with these streams can cope with
1228 * the stream layer suddenly seeking to a different position under it
1229 * (nothing actually implements DEMUXER_CTRL_RESYNC now).
1231 struct stream
*stream
= demuxer
->stream
;
1232 if (stream
->type
== STREAMTYPE_DVD
|| stream
->type
== STREAMTYPE_DVDNAV
) {
1235 if (flags
& SEEK_ABSOLUTE
)
1238 if (demuxer
->stream_pts
== MP_NOPTS_VALUE
)
1240 pts
= demuxer
->stream_pts
;
1243 if (flags
& SEEK_FACTOR
) {
1245 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_TIME_LENGTH
,
1246 &tmp
) == STREAM_UNSUPPORTED
)
1248 pts
+= tmp
* rel_seek_secs
;
1250 pts
+= rel_seek_secs
;
1252 if (stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_TIME
, &pts
)
1253 != STREAM_UNSUPPORTED
) {
1254 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1260 if (demuxer
->desc
->seek
)
1261 demuxer
->desc
->seek(demuxer
, rel_seek_secs
, audio_delay
, flags
);
1266 int demux_info_add(demuxer_t
*demuxer
, const char *opt
, const char *param
)
1268 return demux_info_add_bstr(demuxer
, BSTR(opt
), BSTR(param
));
1271 int demux_info_add_bstr(demuxer_t
*demuxer
, struct bstr opt
, struct bstr param
)
1273 char **info
= demuxer
->info
;
1277 for (n
= 0; info
&& info
[2 * n
] != NULL
; n
++) {
1278 if (!bstrcasecmp(opt
, BSTR(info
[2*n
]))) {
1279 if (!bstrcmp(param
, BSTR(info
[2*n
+ 1]))) {
1280 mp_msg(MSGT_DEMUX
, MSGL_V
, "Demuxer info %.*s set to unchanged value %.*s\n",
1281 BSTR_P(opt
), BSTR_P(param
));
1284 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "Demuxer info %.*s changed to %.*s\n",
1285 BSTR_P(opt
), BSTR_P(param
));
1286 talloc_free(info
[2*n
+ 1]);
1287 info
[2*n
+ 1] = talloc_strndup(demuxer
->info
, param
.start
, param
.len
);
1292 info
= demuxer
->info
= talloc_realloc(demuxer
, info
, char *, 2 * (n
+ 2));
1293 info
[2*n
] = talloc_strndup(demuxer
->info
, opt
.start
, opt
.len
);
1294 info
[2*n
+ 1] = talloc_strndup(demuxer
->info
, param
.start
, param
.len
);
1295 memset(&info
[2 * (n
+ 1)], 0, 2 * sizeof(char *));
1300 int demux_info_print(demuxer_t
*demuxer
)
1302 char **info
= demuxer
->info
;
1308 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "Clip info:\n");
1309 for (n
= 0; info
[2 * n
] != NULL
; n
++) {
1310 mp_msg(MSGT_DEMUX
, MSGL_INFO
, " %s: %s\n", info
[2 * n
],
1312 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_NAME%d=%s\n", n
,
1314 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_VALUE%d=%s\n", n
,
1317 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_N=%d\n", n
);
1322 char *demux_info_get(demuxer_t
*demuxer
, const char *opt
)
1325 char **info
= demuxer
->info
;
1327 for (i
= 0; info
&& info
[2 * i
] != NULL
; i
++) {
1328 if (!strcasecmp(opt
, info
[2 * i
]))
1329 return info
[2 * i
+ 1];
1335 int demux_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
1338 if (demuxer
->desc
->control
)
1339 return demuxer
->desc
->control(demuxer
, cmd
, arg
);
1341 return DEMUXER_CTRL_NOTIMPL
;
1346 double demuxer_get_time_length(demuxer_t
*demuxer
)
1348 double get_time_ans
;
1349 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1350 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1351 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
1353 (demuxer
, DEMUXER_CTRL_GET_TIME_LENGTH
, (void *) &get_time_ans
) <= 0) {
1354 if (sh_video
&& sh_video
->i_bps
&& sh_audio
&& sh_audio
->i_bps
)
1355 get_time_ans
= (double) (demuxer
->movi_end
-
1356 demuxer
->movi_start
) / (sh_video
->i_bps
+
1358 else if (sh_video
&& sh_video
->i_bps
)
1359 get_time_ans
= (double) (demuxer
->movi_end
-
1360 demuxer
->movi_start
) / sh_video
->i_bps
;
1361 else if (sh_audio
&& sh_audio
->i_bps
)
1362 get_time_ans
= (double) (demuxer
->movi_end
-
1363 demuxer
->movi_start
) / sh_audio
->i_bps
;
1367 return get_time_ans
;
1371 * \brief demuxer_get_current_time() returns the time of the current play in three possible ways:
1372 * either when the stream reader satisfies STREAM_CTRL_GET_CURRENT_TIME (e.g. dvd)
1373 * or using sh_video->pts when the former method fails
1375 * \return the current play time
1377 int demuxer_get_current_time(demuxer_t
*demuxer
)
1379 double get_time_ans
= 0;
1380 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1381 if (demuxer
->stream_pts
!= MP_NOPTS_VALUE
)
1382 get_time_ans
= demuxer
->stream_pts
;
1384 get_time_ans
= sh_video
->pts
;
1385 return (int) get_time_ans
;
1388 int demuxer_get_percent_pos(demuxer_t
*demuxer
)
1391 int res
= demux_control(demuxer
, DEMUXER_CTRL_GET_PERCENT_POS
, &ans
);
1392 int len
= (demuxer
->movi_end
- demuxer
->movi_start
) / 100;
1394 off_t pos
= demuxer
->filepos
> 0 ? demuxer
->filepos
: stream_tell(demuxer
->stream
);
1396 ans
= (pos
- demuxer
->movi_start
) / len
;
1407 int demuxer_switch_audio(demuxer_t
*demuxer
, int index
)
1409 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_AUDIO
, &index
);
1410 if (res
== DEMUXER_CTRL_NOTIMPL
) {
1411 struct sh_audio
*sh_audio
= demuxer
->audio
->sh
;
1412 return sh_audio
? sh_audio
->aid
: -2;
1414 if (demuxer
->audio
->id
>= 0) {
1415 struct sh_audio
*sh_audio
= demuxer
->a_streams
[demuxer
->audio
->id
];
1416 demuxer
->audio
->sh
= sh_audio
;
1417 index
= sh_audio
->aid
; // internal MPEG demuxers don't set it right
1420 demuxer
->audio
->sh
= NULL
;
1424 int demuxer_switch_video(demuxer_t
*demuxer
, int index
)
1426 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_VIDEO
, &index
);
1427 if (res
== DEMUXER_CTRL_NOTIMPL
)
1428 index
= demuxer
->video
->id
;
1429 if (demuxer
->video
->id
>= 0)
1430 demuxer
->video
->sh
= demuxer
->v_streams
[demuxer
->video
->id
];
1432 demuxer
->video
->sh
= NULL
;
1436 int demuxer_add_attachment(demuxer_t
*demuxer
, struct bstr name
,
1437 struct bstr type
, struct bstr data
)
1439 if (!(demuxer
->num_attachments
% 32))
1440 demuxer
->attachments
= talloc_realloc(demuxer
, demuxer
->attachments
,
1441 struct demux_attachment
,
1442 demuxer
->num_attachments
+ 32);
1444 struct demux_attachment
*att
=
1445 demuxer
->attachments
+ demuxer
->num_attachments
;
1446 att
->name
= talloc_strndup(demuxer
->attachments
, name
.start
, name
.len
);
1447 att
->type
= talloc_strndup(demuxer
->attachments
, type
.start
, type
.len
);
1448 att
->data
= talloc_size(demuxer
->attachments
, data
.len
);
1449 memcpy(att
->data
, data
.start
, data
.len
);
1450 att
->data_size
= data
.len
;
1452 return demuxer
->num_attachments
++;
1455 int demuxer_add_chapter(demuxer_t
*demuxer
, struct bstr name
,
1456 uint64_t start
, uint64_t end
)
1458 if (!(demuxer
->num_chapters
% 32))
1459 demuxer
->chapters
= talloc_realloc(demuxer
, demuxer
->chapters
,
1460 struct demux_chapter
,
1461 demuxer
->num_chapters
+ 32);
1463 demuxer
->chapters
[demuxer
->num_chapters
].start
= start
;
1464 demuxer
->chapters
[demuxer
->num_chapters
].end
= end
;
1465 demuxer
->chapters
[demuxer
->num_chapters
].name
= name
.len
?
1466 talloc_strndup(demuxer
->chapters
, name
.start
, name
.len
) :
1467 talloc_strdup(demuxer
->chapters
, mp_gtext("unknown"));
1469 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_ID=%d\n", demuxer
->num_chapters
);
1470 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_START=%"PRIu64
"\n", demuxer
->num_chapters
, start
);
1472 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_END=%"PRIu64
"\n", demuxer
->num_chapters
, end
);
1474 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_NAME=%.*s\n", demuxer
->num_chapters
, BSTR_P(name
));
1476 return demuxer
->num_chapters
++;
1480 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1481 * either using the demuxer->chapters structure set by the demuxer
1482 * or asking help to the stream layer (e.g. dvd)
1483 * \param chapter - chapter number wished - 0-based
1484 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1485 * \param chapter_name name of chapter found (set by this function is param is not null)
1486 * \return -1 on error, current chapter if successful
1489 int demuxer_seek_chapter(demuxer_t
*demuxer
, int chapter
, double *seek_pts
,
1490 char **chapter_name
)
1494 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1495 demux_flush(demuxer
);
1497 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_CHAPTER
,
1499 if (ris
!= STREAM_UNSUPPORTED
)
1500 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1502 // exit status may be ok, but main() doesn't have to seek itself
1503 // (because e.g. dvds depend on sectors, not on pts)
1507 *chapter_name
= NULL
;
1509 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1510 &num_chapters
) == STREAM_UNSUPPORTED
)
1513 *chapter_name
= talloc_size(NULL
, 16);
1514 sprintf(*chapter_name
, " of %3d", num_chapters
);
1518 return ris
!= STREAM_UNSUPPORTED
? chapter
: -1;
1519 } else { // chapters structure is set in the demuxer
1520 if (chapter
>= demuxer
->num_chapters
)
1525 *seek_pts
= demuxer
->chapters
[chapter
].start
/ 1000.0;
1528 *chapter_name
= talloc_strdup(NULL
, demuxer
->chapters
[chapter
].name
);
1534 int demuxer_get_current_chapter(demuxer_t
*demuxer
)
1537 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1538 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_CURRENT_CHAPTER
,
1539 &chapter
) == STREAM_UNSUPPORTED
)
1542 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1543 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1545 now
= (sh_video
? sh_video
->pts
: (sh_audio
? sh_audio
->pts
: 0))
1547 for (chapter
= demuxer
->num_chapters
- 1; chapter
>= 0; --chapter
) {
1548 if (demuxer
->chapters
[chapter
].start
<= now
)
1555 char *demuxer_chapter_name(demuxer_t
*demuxer
, int chapter
)
1557 if (demuxer
->num_chapters
&& demuxer
->chapters
) {
1558 if (chapter
>= 0 && chapter
< demuxer
->num_chapters
1559 && demuxer
->chapters
[chapter
].name
)
1560 return strdup(demuxer
->chapters
[chapter
].name
);
1565 char *demuxer_chapter_display_name(demuxer_t
*demuxer
, int chapter
)
1567 char *chapter_name
= demuxer_chapter_name(demuxer
, chapter
);
1569 char *tmp
= malloc(strlen(chapter_name
) + 14);
1570 snprintf(tmp
, 63, "(%d) %s", chapter
+ 1, chapter_name
);
1574 int chapter_num
= demuxer_chapter_count(demuxer
);
1576 if (chapter_num
<= 0)
1577 sprintf(tmp
, "(%d)", chapter
+ 1);
1579 sprintf(tmp
, "(%d) of %d", chapter
+ 1, chapter_num
);
1584 float demuxer_chapter_time(demuxer_t
*demuxer
, int chapter
, float *end
)
1586 if (demuxer
->num_chapters
&& demuxer
->chapters
&& chapter
>= 0
1587 && chapter
< demuxer
->num_chapters
) {
1589 *end
= demuxer
->chapters
[chapter
].end
/ 1000.0;
1590 return demuxer
->chapters
[chapter
].start
/ 1000.0;
1595 int demuxer_chapter_count(demuxer_t
*demuxer
)
1597 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1598 int num_chapters
= 0;
1599 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1600 &num_chapters
) == STREAM_UNSUPPORTED
)
1602 return num_chapters
;
1604 return demuxer
->num_chapters
;
1607 int demuxer_angles_count(demuxer_t
*demuxer
)
1609 int ris
, angles
= -1;
1611 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_ANGLES
, &angles
);
1612 if (ris
== STREAM_UNSUPPORTED
)
1617 int demuxer_get_current_angle(demuxer_t
*demuxer
)
1619 int ris
, curr_angle
= -1;
1620 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_ANGLE
, &curr_angle
);
1621 if (ris
== STREAM_UNSUPPORTED
)
1627 int demuxer_set_angle(demuxer_t
*demuxer
, int angle
)
1629 int ris
, angles
= -1;
1631 angles
= demuxer_angles_count(demuxer
);
1632 if ((angles
< 1) || (angle
> angles
))
1635 demux_flush(demuxer
);
1637 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SET_ANGLE
, &angle
);
1638 if (ris
== STREAM_UNSUPPORTED
)
1641 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1646 int demuxer_audio_track_by_lang(demuxer_t
*d
, char *lang
)
1649 lang
+= strspn(lang
, ",");
1650 while ((len
= strcspn(lang
, ",")) > 0) {
1651 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1652 sh_audio_t
*sh
= d
->a_streams
[i
];
1653 if (sh
&& sh
->lang
&& strncmp(sh
->lang
, lang
, len
) == 0)
1657 lang
+= strspn(lang
, ",");
1662 int demuxer_sub_track_by_lang(demuxer_t
*d
, char *lang
)
1665 lang
+= strspn(lang
, ",");
1666 while ((len
= strcspn(lang
, ",")) > 0) {
1667 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1668 sh_sub_t
*sh
= d
->s_streams
[i
];
1669 if (sh
&& sh
->lang
&& strncmp(sh
->lang
, lang
, len
) == 0)
1673 lang
+= strspn(lang
, ",");
1678 int demuxer_default_audio_track(demuxer_t
*d
)
1681 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1682 sh_audio_t
*sh
= d
->a_streams
[i
];
1683 if (sh
&& sh
->default_track
)
1686 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1687 sh_audio_t
*sh
= d
->a_streams
[i
];
1694 int demuxer_default_sub_track(demuxer_t
*d
)
1697 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1698 sh_sub_t
*sh
= d
->s_streams
[i
];
1699 if (sh
&& sh
->default_track
)