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"
44 #include "libmpcodecs/vd_ffmpeg.h"
49 #include "libavcodec/avcodec.h"
50 #if MP_INPUT_BUFFER_PADDING_SIZE < FF_INPUT_BUFFER_PADDING_SIZE
51 #error MP_INPUT_BUFFER_PADDING_SIZE is too small!
55 // This is quite experimental, in particular it will mess up the pts values
56 // in the queue - on the other hand it might fix some issues like generating
57 // broken files with mencoder and stream copy.
58 // Better leave it disabled for now, if we find no use for it this code should
59 // just be removed again.
60 #define PARSE_ON_ADD 0
62 static void clear_parser(sh_common_t
*sh
);
65 extern const demuxer_desc_t demuxer_desc_rawaudio
;
66 extern const demuxer_desc_t demuxer_desc_rawvideo
;
67 extern const demuxer_desc_t demuxer_desc_tv
;
68 extern const demuxer_desc_t demuxer_desc_mf
;
69 extern const demuxer_desc_t demuxer_desc_avi
;
70 extern const demuxer_desc_t demuxer_desc_y4m
;
71 extern const demuxer_desc_t demuxer_desc_asf
;
72 extern const demuxer_desc_t demuxer_desc_real
;
73 extern const demuxer_desc_t demuxer_desc_smjpeg
;
74 extern const demuxer_desc_t demuxer_desc_matroska
;
75 extern const demuxer_desc_t demuxer_desc_realaudio
;
76 extern const demuxer_desc_t demuxer_desc_vqf
;
77 extern const demuxer_desc_t demuxer_desc_mov
;
78 extern const demuxer_desc_t demuxer_desc_vivo
;
79 extern const demuxer_desc_t demuxer_desc_fli
;
80 extern const demuxer_desc_t demuxer_desc_film
;
81 extern const demuxer_desc_t demuxer_desc_roq
;
82 extern const demuxer_desc_t demuxer_desc_gif
;
83 extern const demuxer_desc_t demuxer_desc_ogg
;
84 extern const demuxer_desc_t demuxer_desc_avs
;
85 extern const demuxer_desc_t demuxer_desc_pva
;
86 extern const demuxer_desc_t demuxer_desc_nsv
;
87 extern const demuxer_desc_t demuxer_desc_mpeg_ts
;
88 extern const demuxer_desc_t demuxer_desc_lmlm4
;
89 extern const demuxer_desc_t demuxer_desc_mpeg_ps
;
90 extern const demuxer_desc_t demuxer_desc_mpeg_pes
;
91 extern const demuxer_desc_t demuxer_desc_mpeg_es
;
92 extern const demuxer_desc_t demuxer_desc_mpeg_gxf
;
93 extern const demuxer_desc_t demuxer_desc_mpeg4_es
;
94 extern const demuxer_desc_t demuxer_desc_h264_es
;
95 extern const demuxer_desc_t demuxer_desc_rawdv
;
96 extern const demuxer_desc_t demuxer_desc_mpc
;
97 extern const demuxer_desc_t demuxer_desc_audio
;
98 extern const demuxer_desc_t demuxer_desc_xmms
;
99 extern const demuxer_desc_t demuxer_desc_mpeg_ty
;
100 extern const demuxer_desc_t demuxer_desc_rtp
;
101 extern const demuxer_desc_t demuxer_desc_rtp_nemesi
;
102 extern const demuxer_desc_t demuxer_desc_lavf
;
103 extern const demuxer_desc_t demuxer_desc_lavf_preferred
;
104 extern const demuxer_desc_t demuxer_desc_aac
;
105 extern const demuxer_desc_t demuxer_desc_nut
;
106 extern const demuxer_desc_t demuxer_desc_mng
;
108 /* Please do not add any new demuxers here. If you want to implement a new
109 * demuxer, add it to libavformat, except for wrappers around external
110 * libraries and demuxers requiring binary support. */
112 const demuxer_desc_t
*const demuxer_list
[] = {
113 &demuxer_desc_rawaudio
,
114 &demuxer_desc_rawvideo
,
120 &demuxer_desc_lavf_preferred
,
127 &demuxer_desc_smjpeg
,
128 &demuxer_desc_matroska
,
129 &demuxer_desc_realaudio
,
139 #ifdef CONFIG_OGGVORBIS
142 #ifdef CONFIG_WIN32DLL
146 &demuxer_desc_mpeg_ts
,
148 &demuxer_desc_mpeg_ps
,
149 &demuxer_desc_mpeg_pes
,
150 &demuxer_desc_mpeg_es
,
151 &demuxer_desc_mpeg_gxf
,
152 &demuxer_desc_mpeg4_es
,
153 &demuxer_desc_h264_es
,
155 &demuxer_desc_mpeg_ty
,
156 #ifdef CONFIG_LIVE555
159 #ifdef CONFIG_LIBNEMESI
160 &demuxer_desc_rtp_nemesi
,
165 #ifdef CONFIG_MUSEPACK
168 #ifdef CONFIG_LIBDV095
181 /* Please do not add any new demuxers here. If you want to implement a new
182 * demuxer, add it to libavformat, except for wrappers around external
183 * libraries and demuxers requiring binary support. */
187 void free_demuxer_stream(demux_stream_t
*ds
)
193 demux_stream_t
*new_demuxer_stream(struct demuxer
*demuxer
, int id
)
195 demux_stream_t
*ds
= malloc(sizeof(demux_stream_t
));
196 *ds
= (demux_stream_t
){
206 * Get demuxer description structure for a given demuxer type
208 * @param file_format type of the demuxer
209 * @return structure for the demuxer, NULL if not found
211 static const demuxer_desc_t
*get_demuxer_desc_from_type(int file_format
)
215 for (i
= 0; demuxer_list
[i
]; i
++)
216 if (file_format
== demuxer_list
[i
]->type
)
217 return demuxer_list
[i
];
223 demuxer_t
*new_demuxer(struct MPOpts
*opts
, stream_t
*stream
, int type
,
224 int a_id
, int v_id
, int s_id
, char *filename
)
226 struct demuxer
*d
= talloc_zero(NULL
, struct demuxer
);
228 d
->stream_pts
= MP_NOPTS_VALUE
;
229 d
->reference_clock
= MP_NOPTS_VALUE
;
230 d
->movi_start
= stream
->start_pos
;
231 d
->movi_end
= stream
->end_pos
;
235 d
->audio
= new_demuxer_stream(d
, a_id
);
236 d
->video
= new_demuxer_stream(d
, v_id
);
237 d
->sub
= new_demuxer_stream(d
, s_id
);
241 if (!(d
->desc
= get_demuxer_desc_from_type(type
)))
242 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
243 "BUG! Invalid demuxer type in new_demuxer(), "
244 "big troubles ahead.");
245 if (filename
) // Filename hack for avs_check_file
246 d
->filename
= strdup(filename
);
247 stream_seek(stream
, stream
->start_pos
);
251 const char *sh_sub_type2str(int type
)
254 case 't': return "text";
255 case 'm': return "movtext";
256 case 'a': return "ass";
257 case 'v': return "vobsub";
258 case 'x': return "xsub";
259 case 'b': return "dvb";
260 case 'd': return "dvb-teletext";
261 case 'p': return "hdmv pgs";
266 sh_sub_t
*new_sh_sub_sid(demuxer_t
*demuxer
, int id
, int sid
)
268 if (id
> MAX_S_STREAMS
- 1 || id
< 0) {
269 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
270 "Requested sub stream id overflow (%d > %d)\n", id
,
274 if (demuxer
->s_streams
[id
])
275 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, "Sub stream %i redefined\n", id
);
277 sh_sub_t
*sh
= calloc(1, sizeof(sh_sub_t
));
278 demuxer
->s_streams
[id
] = sh
;
280 sh
->opts
= demuxer
->opts
;
281 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SUBTITLE_ID=%d\n", sid
);
283 if (sid
== demuxer
->opts
->sub_id
) {
284 demuxer
->sub
->id
= id
;
285 demuxer
->sub
->sh
= demuxer
->s_streams
[id
];
287 return demuxer
->s_streams
[id
];
290 struct sh_sub
*new_sh_sub_sid_lang(struct demuxer
*demuxer
, int id
, int sid
,
293 struct sh_sub
*sh
= new_sh_sub_sid(demuxer
, id
, sid
);
294 if (lang
&& lang
[0] && strcmp(lang
, "und")) {
295 sh
->lang
= strdup(lang
);
296 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SID_%d_LANG=%s\n", sid
, lang
);
301 static void free_sh_sub(sh_sub_t
*sh
)
303 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_sub at %p\n", sh
);
307 ass_free_track(sh
->ass_track
);
311 clear_parser((sh_common_t
*)sh
);
316 sh_audio_t
*new_sh_audio_aid(demuxer_t
*demuxer
, int id
, int aid
)
318 if (id
> MAX_A_STREAMS
- 1 || id
< 0) {
319 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
320 "Requested audio stream id overflow (%d > %d)\n", id
,
324 if (demuxer
->a_streams
[id
]) {
325 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, "WARNING: Audio stream header %d redefined.\n", id
);
327 mp_tmsg(MSGT_DEMUXER
, MSGL_V
, "==> Found audio stream: %d\n", id
);
328 sh_audio_t
*sh
= calloc(1, sizeof(sh_audio_t
));
329 demuxer
->a_streams
[id
] = sh
;
331 sh
->ds
= demuxer
->audio
;
334 sh
->sample_format
= AF_FORMAT_S16_NE
;
335 sh
->opts
= demuxer
->opts
;
336 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_ID=%d\n", aid
);
338 return demuxer
->a_streams
[id
];
341 void free_sh_audio(demuxer_t
*demuxer
, int id
)
343 sh_audio_t
*sh
= demuxer
->a_streams
[id
];
344 demuxer
->a_streams
[id
] = NULL
;
345 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_audio at %p\n", sh
);
350 clear_parser((sh_common_t
*)sh
);
355 sh_video_t
*new_sh_video_vid(demuxer_t
*demuxer
, int id
, int vid
)
357 if (id
> MAX_V_STREAMS
- 1 || id
< 0) {
358 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
359 "Requested video stream id overflow (%d > %d)\n", id
,
363 if (demuxer
->v_streams
[id
])
364 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, "WARNING: Video stream header %d redefined.\n", id
);
366 mp_tmsg(MSGT_DEMUXER
, MSGL_V
, "==> Found video stream: %d\n", id
);
367 sh_video_t
*sh
= calloc(1, sizeof *sh
);
368 demuxer
->v_streams
[id
] = sh
;
370 sh
->ds
= demuxer
->video
;
371 sh
->opts
= demuxer
->opts
;
372 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_ID=%d\n", vid
);
374 return demuxer
->v_streams
[id
];
377 void free_sh_video(sh_video_t
*sh
)
379 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_video at %p\n", sh
);
382 clear_parser((sh_common_t
*)sh
);
387 void free_demuxer(demuxer_t
*demuxer
)
390 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing %s demuxer at %p\n",
391 demuxer
->desc
->shortdesc
, demuxer
);
392 if (demuxer
->desc
->close
)
393 demuxer
->desc
->close(demuxer
);
394 // Very ugly hack to make it behave like old implementation
395 if (demuxer
->desc
->type
== DEMUXER_TYPE_DEMUXERS
)
396 goto skip_streamfree
;
398 for (i
= 0; i
< MAX_A_STREAMS
; i
++)
399 if (demuxer
->a_streams
[i
])
400 free_sh_audio(demuxer
, i
);
401 for (i
= 0; i
< MAX_V_STREAMS
; i
++)
402 if (demuxer
->v_streams
[i
])
403 free_sh_video(demuxer
->v_streams
[i
]);
404 for (i
= 0; i
< MAX_S_STREAMS
; i
++)
405 if (demuxer
->s_streams
[i
])
406 free_sh_sub(demuxer
->s_streams
[i
]);
408 free_demuxer_stream(demuxer
->audio
);
409 free_demuxer_stream(demuxer
->video
);
410 free_demuxer_stream(demuxer
->sub
);
412 free(demuxer
->filename
);
413 if (demuxer
->teletext
)
414 teletext_control(demuxer
->teletext
, TV_VBI_CONTROL_STOP
, NULL
);
415 talloc_free(demuxer
);
419 static void ds_add_packet_internal(demux_stream_t
*ds
, demux_packet_t
*dp
)
421 // append packet to DS stream:
423 ds
->bytes
+= dp
->len
;
425 // next packet in stream
429 // first packet in stream
430 ds
->first
= ds
->last
= dp
;
432 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
433 "DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
434 (ds
== ds
->demuxer
->audio
) ? "d_audio" : "d_video", dp
->len
,
435 dp
->pts
, (unsigned int) dp
->pos
, ds
->demuxer
->audio
->packs
,
436 ds
->demuxer
->video
->packs
);
440 static void allocate_parser(AVCodecContext
**avctx
, AVCodecParserContext
**parser
, unsigned format
)
442 enum CodecID codec_id
= CODEC_ID_NONE
;
447 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52, 94, 0)
448 case MKTAG('M', 'P', '4', 'L'):
449 codec_id
= CODEC_ID_AAC_LATM
;
456 case MKTAG('d', 'n', 'e', 't'):
457 case MKTAG('s', 'a', 'c', '3'):
458 codec_id
= CODEC_ID_AC3
;
460 case MKTAG('E', 'A', 'C', '3'):
461 codec_id
= CODEC_ID_EAC3
;
465 codec_id
= CODEC_ID_DTS
;
467 case MKTAG('M', 'L', 'P', ' '):
468 codec_id
= CODEC_ID_MLP
;
473 case MKTAG('.', 'm', 'p', '3'):
474 case MKTAG('M', 'P', '3', ' '):
475 case MKTAG('L', 'A', 'M', 'E'):
476 codec_id
= CODEC_ID_MP3
;
480 case MKTAG('.', 'm', 'p', '2'):
481 case MKTAG('.', 'm', 'p', '1'):
482 codec_id
= CODEC_ID_MP2
;
484 case MKTAG('T', 'R', 'H', 'D'):
485 codec_id
= CODEC_ID_TRUEHD
;
488 if (codec_id
!= CODEC_ID_NONE
) {
489 *avctx
= avcodec_alloc_context();
492 *parser
= av_parser_init(codec_id
);
498 static void get_parser(sh_common_t
*sh
, AVCodecContext
**avctx
, AVCodecParserContext
**parser
)
503 if (!sh
|| !sh
->needs_parsing
)
507 *parser
= sh
->parser
;
511 allocate_parser(avctx
, parser
, sh
->format
);
513 sh
->parser
= *parser
;
516 int ds_parse(demux_stream_t
*ds
, uint8_t **buffer
, int *len
, double pts
, off_t pos
)
518 AVCodecContext
*avctx
;
519 AVCodecParserContext
*parser
;
520 get_parser(ds
->sh
, &avctx
, &parser
);
523 return av_parser_parse2(parser
, avctx
, buffer
, len
, *buffer
, *len
, pts
, pts
, pos
);
526 static void clear_parser(sh_common_t
*sh
)
528 av_parser_close(sh
->parser
);
530 av_freep(&sh
->avctx
);
533 void ds_clear_parser(demux_stream_t
*ds
)
537 clear_parser(ds
->sh
);
541 void ds_add_packet(demux_stream_t
*ds
, demux_packet_t
*dp
)
543 #if PARSE_ON_ADD && defined(CONFIG_FFMPEG)
547 uint8_t *parsed_start
= dp
->buffer
+ pos
;
548 int parsed_len
= len
;
549 int consumed
= ds_parse(ds
->sh
, &parsed_start
, &parsed_len
, dp
->pts
, dp
->pos
);
552 if (parsed_start
== dp
->buffer
&& parsed_len
== dp
->len
) {
553 ds_add_packet_internal(ds
, dp
);
554 } else if (parsed_len
) {
555 demux_packet_t
*dp2
= new_demux_packet(parsed_len
);
557 dp2
->pts
= dp
->pts
; // should be parser->pts but that works badly
558 memcpy(dp2
->buffer
, parsed_start
, parsed_len
);
559 ds_add_packet_internal(ds
, dp2
);
563 ds_add_packet_internal(ds
, dp
);
567 void ds_read_packet(demux_stream_t
*ds
, stream_t
*stream
, int len
,
568 double pts
, off_t pos
, int flags
)
570 demux_packet_t
*dp
= new_demux_packet(len
);
571 len
= stream_read(stream
, dp
->buffer
, len
);
572 resize_demux_packet(dp
, len
);
576 // append packet to DS stream:
577 ds_add_packet(ds
, dp
);
581 // 0 = EOF or no stream found or invalid type
582 // 1 = successfully read a packet
584 int demux_fill_buffer(demuxer_t
*demux
, demux_stream_t
*ds
)
586 // Note: parameter 'ds' can be NULL!
587 return demux
->desc
->fill_buffer(demux
, ds
);
593 #define MAX_ACUMULATED_PACKETS 64
594 int ds_fill_buffer(demux_stream_t
*ds
)
596 demuxer_t
*demux
= ds
->demuxer
;
598 free_demux_packet(ds
->current
);
600 if (mp_msg_test(MSGT_DEMUXER
, MSGL_DBG3
)) {
601 if (ds
== demux
->audio
)
602 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
603 "ds_fill_buffer(d_audio) called\n");
604 else if (ds
== demux
->video
)
605 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
606 "ds_fill_buffer(d_video) called\n");
607 else if (ds
== demux
->sub
)
608 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
, "ds_fill_buffer(d_sub) called\n");
610 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
611 "ds_fill_buffer(unknown 0x%X) called\n", (unsigned int) ds
);
615 demux_packet_t
*p
= ds
->first
;
617 if (demux
->reference_clock
!= MP_NOPTS_VALUE
) {
618 if ( p
->pts
!= MP_NOPTS_VALUE
619 && p
->pts
> demux
->reference_clock
620 && ds
->packs
< MAX_ACUMULATED_PACKETS
) {
621 if (demux_fill_buffer(demux
, ds
))
627 ds
->buffer
= p
->buffer
;
629 ds
->buffer_size
= p
->len
;
631 ds
->dpos
+= p
->len
; // !!!
633 if (p
->pts
!= MP_NOPTS_VALUE
) {
637 ds
->pts_bytes
+= p
->len
; // !!!
638 if (p
->stream_pts
!= MP_NOPTS_VALUE
)
639 demux
->stream_pts
= p
->stream_pts
;
640 ds
->flags
= p
->flags
;
648 /* The code below can set ds->eof to 1 when another stream runs
649 * out of buffer space. That makes sense because in that situation
650 * the calling code should not count on being able to demux more
651 * packets from this stream.
652 * If however the situation improves and we're called again
653 * despite the eof flag then it's better to clear it to avoid
659 #define MaybeNI _("Maybe you are playing a non-interleaved stream/file or the codec failed?\n" \
660 "For AVI files, try to force non-interleaved mode with the -ni option.\n")
662 if (demux
->audio
->packs
>= MAX_PACKS
663 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
664 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
665 demux
->audio
->packs
, demux
->audio
->bytes
);
666 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
669 if (demux
->video
->packs
>= MAX_PACKS
670 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
671 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
672 demux
->video
->packs
, demux
->video
->bytes
);
673 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
676 if (!demux_fill_buffer(demux
, ds
)) {
677 #if PARSE_ON_ADD && defined(CONFIG_FFMPEG)
678 uint8_t *parsed_start
= NULL
;
680 ds_parse(ds
->sh
, &parsed_start
, &parsed_len
, MP_NOPTS_VALUE
, 0);
682 demux_packet_t
*dp2
= new_demux_packet(parsed_len
);
683 dp2
->pts
= MP_NOPTS_VALUE
;
684 memcpy(dp2
->buffer
, parsed_start
, parsed_len
);
685 ds_add_packet_internal(ds
, dp2
);
689 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
690 "ds_fill_buffer()->demux_fill_buffer() failed\n");
694 ds
->buffer_pos
= ds
->buffer_size
= 0;
696 mp_msg(MSGT_DEMUXER
, MSGL_V
,
697 "ds_fill_buffer: EOF reached (stream: %s) \n",
698 ds
== demux
->audio
? "audio" : "video");
703 int demux_read_data(demux_stream_t
*ds
, unsigned char *mem
, int len
)
708 x
= ds
->buffer_size
- ds
->buffer_pos
;
710 if (!ds_fill_buffer(ds
))
716 fast_memcpy(mem
+ bytes
, &ds
->buffer
[ds
->buffer_pos
], x
);
726 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
727 * \param mem memory to read data into, may be NULL to discard data
728 * \param maxlen maximum number of bytes to read
729 * \param read number of bytes actually read
730 * \param pattern pattern to search for (lowest 8 bits are ignored)
731 * \return whether pattern was found
733 int demux_pattern_3(demux_stream_t
*ds
, unsigned char *mem
, int maxlen
,
734 int *read
, uint32_t pattern
)
736 register uint32_t head
= 0xffffff00;
737 register uint32_t pat
= pattern
& 0xffffff00;
740 register unsigned char *ds_buf
= &ds
->buffer
[ds
->buffer_size
];
741 int len
= ds
->buffer_size
- ds
->buffer_pos
;
742 register long pos
= -len
;
743 if (unlikely(pos
>= 0)) { // buffer is empty
750 } while (++pos
&& head
!= pat
);
752 if (total_len
+ len
> maxlen
)
753 len
= maxlen
- total_len
;
754 len
= demux_read_data(ds
, mem
? &mem
[total_len
] : NULL
, len
);
756 } while ((head
!= pat
|| total_len
< 3) && total_len
< maxlen
&& !ds
->eof
);
759 return total_len
>= 3 && head
== pat
;
762 void ds_free_packs(demux_stream_t
*ds
)
764 demux_packet_t
*dp
= ds
->first
;
766 demux_packet_t
*dn
= dp
->next
;
767 free_demux_packet(dp
);
770 if (ds
->asf_packet
) {
771 // free unfinished .asf fragments:
772 free(ds
->asf_packet
->buffer
);
773 free(ds
->asf_packet
);
774 ds
->asf_packet
= NULL
;
776 ds
->first
= ds
->last
= NULL
;
777 ds
->packs
= 0; // !!!!!
780 free_demux_packet(ds
->current
);
783 ds
->buffer_pos
= ds
->buffer_size
;
788 int ds_get_packet(demux_stream_t
*ds
, unsigned char **start
)
791 if (ds
->buffer_pos
>= ds
->buffer_size
) {
792 if (!ds_fill_buffer(ds
)) {
798 len
= ds
->buffer_size
- ds
->buffer_pos
;
799 *start
= &ds
->buffer
[ds
->buffer_pos
];
800 ds
->buffer_pos
+= len
;
804 int ds_get_packet_pts(demux_stream_t
*ds
, unsigned char **start
, double *pts
)
807 *pts
= MP_NOPTS_VALUE
;
808 len
= ds_get_packet(ds
, start
);
811 // Return pts unless this read starts from the middle of a packet
812 if (len
== ds
->buffer_pos
)
813 *pts
= ds
->current
->pts
;
817 int ds_get_packet_sub(demux_stream_t
*ds
, unsigned char **start
)
820 if (ds
->buffer_pos
>= ds
->buffer_size
) {
824 if (!ds_fill_buffer(ds
))
827 len
= ds
->buffer_size
- ds
->buffer_pos
;
828 *start
= &ds
->buffer
[ds
->buffer_pos
];
829 ds
->buffer_pos
+= len
;
833 double ds_get_next_pts(demux_stream_t
*ds
)
835 demuxer_t
*demux
= ds
->demuxer
;
836 // if we have not read from the "current" packet, consider it
837 // as the next, otherwise we never get the pts for the first packet.
838 while (!ds
->first
&& (!ds
->current
|| ds
->buffer_pos
)) {
839 if (demux
->audio
->packs
>= MAX_PACKS
840 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
841 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
842 demux
->audio
->packs
, demux
->audio
->bytes
);
843 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
844 return MP_NOPTS_VALUE
;
846 if (demux
->video
->packs
>= MAX_PACKS
847 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
848 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
849 demux
->video
->packs
, demux
->video
->bytes
);
850 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
851 return MP_NOPTS_VALUE
;
853 if (!demux_fill_buffer(demux
, ds
))
854 return MP_NOPTS_VALUE
;
856 // take pts from "current" if we never read from it.
857 if (ds
->current
&& !ds
->buffer_pos
)
858 return ds
->current
->pts
;
859 return ds
->first
->pts
;
862 // ====================================================================
864 void demuxer_help(void)
868 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "Available demuxers:\n");
869 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, " demuxer: type info: (comment)\n");
870 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DEMUXERS\n");
871 for (i
= 0; demuxer_list
[i
]; i
++) {
872 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Don't display special demuxers
874 if (demuxer_list
[i
]->comment
&& strlen(demuxer_list
[i
]->comment
))
875 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s (%s)\n",
876 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
877 demuxer_list
[i
]->info
, demuxer_list
[i
]->comment
);
879 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s\n",
880 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
881 demuxer_list
[i
]->info
);
887 * Get demuxer type for a given demuxer name
889 * @param demuxer_name string with demuxer name of demuxer number
890 * @param force will be set if demuxer should be forced.
892 * @return DEMUXER_TYPE_xxx, -1 if error or not found
894 int get_demuxer_type_from_name(char *demuxer_name
, int *force
)
900 if (!demuxer_name
|| !demuxer_name
[0])
901 return DEMUXER_TYPE_UNKNOWN
;
903 *force
= demuxer_name
[0] == '+';
904 if (demuxer_name
[0] == '+')
905 demuxer_name
= &demuxer_name
[1];
906 for (i
= 0; demuxer_list
[i
]; i
++) {
907 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Can't select special demuxers from commandline
909 if (strcmp(demuxer_name
, demuxer_list
[i
]->name
) == 0)
910 return demuxer_list
[i
]->type
;
913 // No match found, try to parse name as an integer (demuxer number)
914 type_int
= strtol(demuxer_name
, &endptr
, 0);
915 if (*endptr
) // Conversion failed
917 if ((type_int
> 0) && (type_int
<= DEMUXER_TYPE_MAX
))
918 return (int) type_int
;
923 int extension_parsing
= 1; // 0=off 1=mixed (used only for unstable formats)
926 NOTE : Several demuxers may be opened at the same time so
927 demuxers should NEVER rely on an external var to enable them
928 self. If a demuxer can't do any auto-detection it should only use
929 file_format. The user can explicitly set file_format with the -demuxer
930 option so there is really no need for another extra var.
931 For convenience an option can be added to set file_format directly
932 to the right type (ex: rawaudio,rawvideo).
933 Also the stream can override the file_format so a demuxer which rely
934 on a special stream type can set file_format at the stream level
938 static demuxer_t
*demux_open_stream(struct MPOpts
*opts
, stream_t
*stream
,
939 int file_format
, int force
, int audio_id
,
940 int video_id
, int dvdsub_id
,
943 demuxer_t
*demuxer
= NULL
;
945 sh_video_t
*sh_video
= NULL
;
947 const demuxer_desc_t
*demuxer_desc
;
951 // If somebody requested a demuxer check it
953 if ((demuxer_desc
= get_demuxer_desc_from_type(file_format
))) {
954 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
955 video_id
, dvdsub_id
, filename
);
956 if (demuxer_desc
->check_file
)
957 fformat
= demuxer_desc
->check_file(demuxer
);
958 if (force
|| !demuxer_desc
->check_file
)
959 fformat
= demuxer_desc
->type
;
961 if (fformat
== demuxer_desc
->type
) {
962 demuxer_t
*demux2
= demuxer
;
963 // Move messages to demuxer detection code?
964 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
965 "%s file format detected.\n",
966 demuxer_desc
->shortdesc
);
967 file_format
= fformat
;
968 if (!demuxer
->desc
->open
969 || (demux2
= demuxer
->desc
->open(demuxer
))) {
974 // Format changed after check, recurse
975 free_demuxer(demuxer
);
976 return demux_open_stream(opts
, stream
, fformat
, force
,
977 audio_id
, video_id
, dvdsub_id
,
981 // Check failed for forced demuxer, quit
982 free_demuxer(demuxer
);
986 // Test demuxers with safe file checks
987 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
988 if (demuxer_desc
->safe_check
) {
989 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
990 video_id
, dvdsub_id
, filename
);
991 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
992 if (fformat
== demuxer_desc
->type
) {
993 demuxer_t
*demux2
= demuxer
;
994 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
995 "%s file format detected.\n",
996 demuxer_desc
->shortdesc
);
997 file_format
= fformat
;
998 if (!demuxer
->desc
->open
999 || (demux2
= demuxer
->desc
->open(demuxer
))) {
1004 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
1005 return demuxer
; // handled in mplayer.c
1006 // Format changed after check, recurse
1007 free_demuxer(demuxer
);
1008 demuxer
= demux_open_stream(opts
, stream
, fformat
, force
,
1010 dvdsub_id
, filename
);
1012 return demuxer
; // done!
1013 file_format
= DEMUXER_TYPE_UNKNOWN
;
1016 free_demuxer(demuxer
);
1021 // If no forced demuxer perform file extension based detection
1022 // Ok. We're over the stable detectable fileformats, the next ones are
1023 // a bit fuzzy. So by default (extension_parsing==1) try extension-based
1025 if (file_format
== DEMUXER_TYPE_UNKNOWN
&& filename
1026 && extension_parsing
== 1) {
1027 file_format
= demuxer_type_by_filename(filename
);
1028 if (file_format
!= DEMUXER_TYPE_UNKNOWN
) {
1029 // we like recursion :)
1030 demuxer
= demux_open_stream(opts
, stream
, file_format
, force
,
1031 audio_id
, video_id
, dvdsub_id
,
1034 return demuxer
; // done!
1035 file_format
= DEMUXER_TYPE_UNKNOWN
; // continue fuzzy guessing...
1036 mp_msg(MSGT_DEMUXER
, MSGL_V
,
1037 "demuxer: continue fuzzy content-based format guessing...\n");
1040 // Try detection for all other demuxers
1041 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
1042 if (!demuxer_desc
->safe_check
&& demuxer_desc
->check_file
) {
1043 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
1044 video_id
, dvdsub_id
, filename
);
1045 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
1046 if (fformat
== demuxer_desc
->type
) {
1047 demuxer_t
*demux2
= demuxer
;
1048 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
1049 "%s file format detected.\n",
1050 demuxer_desc
->shortdesc
);
1051 file_format
= fformat
;
1052 if (!demuxer
->desc
->open
1053 || (demux2
= demuxer
->desc
->open(demuxer
))) {
1058 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
1059 return demuxer
; // handled in mplayer.c
1060 // Format changed after check, recurse
1061 free_demuxer(demuxer
);
1062 demuxer
= demux_open_stream(opts
, stream
, fformat
, force
,
1064 dvdsub_id
, filename
);
1066 return demuxer
; // done!
1067 file_format
= DEMUXER_TYPE_UNKNOWN
;
1070 free_demuxer(demuxer
);
1076 //====== File format recognized, set up these for compatibility: =========
1079 demuxer
->file_format
= file_format
;
1081 if ((sh_video
= demuxer
->video
->sh
) && sh_video
->bih
) {
1082 int biComp
= le2me_32(sh_video
->bih
->biCompression
);
1083 mp_msg(MSGT_DEMUX
, MSGL_INFO
,
1084 "VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
1085 (char *) &biComp
, sh_video
->bih
->biWidth
,
1086 sh_video
->bih
->biHeight
, sh_video
->bih
->biBitCount
,
1087 sh_video
->fps
, sh_video
->i_bps
* 0.008f
,
1088 sh_video
->i_bps
/ 1024.0f
);
1091 if (opts
->ass_enabled
&& ass_library
) {
1092 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1093 sh_sub_t
*sh
= demuxer
->s_streams
[i
];
1094 if (sh
&& sh
->type
== 'a') {
1095 sh
->ass_track
= ass_new_track(ass_library
);
1096 if (sh
->ass_track
&& sh
->extradata
)
1097 ass_process_codec_private(sh
->ass_track
, sh
->extradata
,
1099 } else if (sh
&& sh
->type
!= 'v')
1100 sh
->ass_track
= ass_default_track(ass_library
);
1107 char *audio_stream
= NULL
;
1108 char *sub_stream
= NULL
;
1109 int audio_stream_cache
= 0;
1111 char *demuxer_name
= NULL
; // parameter from -demuxer
1112 char *audio_demuxer_name
= NULL
; // parameter from -audio-demuxer
1113 char *sub_demuxer_name
= NULL
; // parameter from -sub-demuxer
1115 extern int hr_mp3_seek
;
1117 extern float stream_cache_min_percent
;
1118 extern float stream_cache_seek_min_percent
;
1120 demuxer_t
*demux_open(struct MPOpts
*opts
, stream_t
*vs
, int file_format
,
1121 int audio_id
, int video_id
, int dvdsub_id
,
1124 stream_t
*as
= NULL
, *ss
= NULL
;
1125 demuxer_t
*vd
, *ad
= NULL
, *sd
= NULL
;
1127 int afmt
= DEMUXER_TYPE_UNKNOWN
, sfmt
= DEMUXER_TYPE_UNKNOWN
;
1129 int audio_demuxer_type
= 0, sub_demuxer_type
= 0;
1130 int demuxer_force
= 0, audio_demuxer_force
= 0, sub_demuxer_force
= 0;
1133 get_demuxer_type_from_name(demuxer_name
, &demuxer_force
)) < 0) {
1134 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-demuxer %s does not exist.\n",
1137 if ((audio_demuxer_type
=
1138 get_demuxer_type_from_name(audio_demuxer_name
,
1139 &audio_demuxer_force
)) < 0) {
1140 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-audio-demuxer %s does not exist.\n",
1141 audio_demuxer_name
);
1143 if ((sub_demuxer_type
=
1144 get_demuxer_type_from_name(sub_demuxer_name
,
1145 &sub_demuxer_force
)) < 0) {
1146 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-sub-demuxer %s does not exist.\n",
1151 as
= open_stream(audio_stream
, 0, &afmt
);
1153 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "Cannot open audio stream: %s\n",
1157 if (audio_stream_cache
) {
1158 if (!stream_enable_cache
1159 (as
, audio_stream_cache
* 1024,
1160 audio_stream_cache
* 1024 * (stream_cache_min_percent
/
1162 audio_stream_cache
* 1024 * (stream_cache_seek_min_percent
/
1165 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
1166 "Can't enable audio stream cache\n");
1172 ss
= open_stream(sub_stream
, 0, &sfmt
);
1174 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "Cannot open subtitle stream: %s\n",
1180 vd
= demux_open_stream(opts
, vs
, demuxer_type
? demuxer_type
: file_format
,
1181 demuxer_force
, audio_stream
? -2 : audio_id
,
1182 video_id
, sub_stream
? -2 : dvdsub_id
, filename
);
1191 ad
= demux_open_stream(opts
, as
,
1192 audio_demuxer_type
? audio_demuxer_type
: afmt
,
1193 audio_demuxer_force
, audio_id
, -2, -2,
1196 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, "Failed to open audio demuxer: %s\n",
1199 } else if (ad
->audio
->sh
1200 && ((sh_audio_t
*) ad
->audio
->sh
)->format
== 0x55) // MP3
1201 hr_mp3_seek
= 1; // Enable high res seeking
1204 sd
= demux_open_stream(opts
, ss
,
1205 sub_demuxer_type
? sub_demuxer_type
: sfmt
,
1206 sub_demuxer_force
, -2, -2, dvdsub_id
,
1209 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
,
1210 "Failed to open subtitle demuxer: %s\n", sub_stream
);
1216 res
= new_demuxers_demuxer(vd
, ad
, sd
);
1218 res
= new_demuxers_demuxer(vd
, ad
, vd
);
1220 res
= new_demuxers_demuxer(vd
, vd
, sd
);
1224 opts
->correct_pts
= opts
->user_correct_pts
;
1225 if (opts
->correct_pts
< 0)
1227 demux_control(vd
? vd
: res
, DEMUXER_CTRL_CORRECT_PTS
,
1228 NULL
) == DEMUXER_CTRL_OK
;
1233 void demux_flush(demuxer_t
*demuxer
)
1236 ds_clear_parser(demuxer
->video
);
1237 ds_clear_parser(demuxer
->audio
);
1238 ds_clear_parser(demuxer
->sub
);
1240 ds_free_packs(demuxer
->video
);
1241 ds_free_packs(demuxer
->audio
);
1242 ds_free_packs(demuxer
->sub
);
1245 int demux_seek(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
,
1248 if (!demuxer
->seekable
) {
1249 if (demuxer
->file_format
== DEMUXER_TYPE_AVI
)
1250 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "Cannot seek in raw AVI streams. (Index required, try with the -idx switch.)\n");
1252 else if (demuxer
->file_format
== DEMUXER_TYPE_TV
)
1253 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "TV input is not seekable! (Seeking will probably be for changing channels ;)\n");
1256 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "Cannot seek in this file.\n");
1259 // clear demux buffers:
1260 demux_flush(demuxer
);
1261 demuxer
->video
->eof
= 0;
1262 demuxer
->audio
->eof
= 0;
1263 demuxer
->sub
->eof
= 0;
1265 /* HACK: assume any demuxer used with these streams can cope with
1266 * the stream layer suddenly seeking to a different position under it
1267 * (nothing actually implements DEMUXER_CTRL_RESYNC now).
1269 struct stream
*stream
= demuxer
->stream
;
1270 if (stream
->type
== STREAMTYPE_DVD
|| stream
->type
== STREAMTYPE_DVDNAV
) {
1273 if (flags
& SEEK_ABSOLUTE
)
1276 if (demuxer
->stream_pts
== MP_NOPTS_VALUE
)
1278 pts
= demuxer
->stream_pts
;
1281 if (flags
& SEEK_FACTOR
) {
1283 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_TIME_LENGTH
,
1284 &tmp
) == STREAM_UNSUPPORTED
)
1286 pts
+= tmp
* rel_seek_secs
;
1288 pts
+= rel_seek_secs
;
1290 if (stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_TIME
, &pts
)
1291 != STREAM_UNSUPPORTED
) {
1292 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1298 if (demuxer
->desc
->seek
)
1299 demuxer
->desc
->seek(demuxer
, rel_seek_secs
, audio_delay
, flags
);
1304 int demux_info_add(demuxer_t
*demuxer
, const char *opt
, const char *param
)
1306 return demux_info_add_bstr(demuxer
, BSTR(opt
), BSTR(param
));
1309 int demux_info_add_bstr(demuxer_t
*demuxer
, struct bstr opt
, struct bstr param
)
1311 char **info
= demuxer
->info
;
1315 for (n
= 0; info
&& info
[2 * n
] != NULL
; n
++) {
1316 if (!bstrcasecmp(opt
, BSTR(info
[2*n
]))) {
1317 if (!bstrcmp(param
, BSTR(info
[2*n
+ 1]))) {
1318 mp_msg(MSGT_DEMUX
, MSGL_V
, "Demuxer info %.*s set to unchanged value %.*s\n",
1319 BSTR_P(opt
), BSTR_P(param
));
1322 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "Demuxer info %.*s changed to %.*s\n",
1323 BSTR_P(opt
), BSTR_P(param
));
1324 talloc_free(info
[2*n
+ 1]);
1325 info
[2*n
+ 1] = talloc_strndup(demuxer
->info
, param
.start
, param
.len
);
1330 info
= demuxer
->info
= talloc_realloc(demuxer
, info
, char *, 2 * (n
+ 2));
1331 info
[2*n
] = talloc_strndup(demuxer
->info
, opt
.start
, opt
.len
);
1332 info
[2*n
+ 1] = talloc_strndup(demuxer
->info
, param
.start
, param
.len
);
1333 memset(&info
[2 * (n
+ 1)], 0, 2 * sizeof(char *));
1338 int demux_info_print(demuxer_t
*demuxer
)
1340 char **info
= demuxer
->info
;
1346 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "Clip info:\n");
1347 for (n
= 0; info
[2 * n
] != NULL
; n
++) {
1348 mp_msg(MSGT_DEMUX
, MSGL_INFO
, " %s: %s\n", info
[2 * n
],
1350 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_NAME%d=%s\n", n
,
1352 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_VALUE%d=%s\n", n
,
1355 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_N=%d\n", n
);
1360 char *demux_info_get(demuxer_t
*demuxer
, const char *opt
)
1363 char **info
= demuxer
->info
;
1365 for (i
= 0; info
&& info
[2 * i
] != NULL
; i
++) {
1366 if (!strcasecmp(opt
, info
[2 * i
]))
1367 return info
[2 * i
+ 1];
1373 int demux_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
1376 if (demuxer
->desc
->control
)
1377 return demuxer
->desc
->control(demuxer
, cmd
, arg
);
1379 return DEMUXER_CTRL_NOTIMPL
;
1384 double demuxer_get_time_length(demuxer_t
*demuxer
)
1386 double get_time_ans
;
1387 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1388 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1389 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
1391 (demuxer
, DEMUXER_CTRL_GET_TIME_LENGTH
, (void *) &get_time_ans
) <= 0) {
1392 if (sh_video
&& sh_video
->i_bps
&& sh_audio
&& sh_audio
->i_bps
)
1393 get_time_ans
= (double) (demuxer
->movi_end
-
1394 demuxer
->movi_start
) / (sh_video
->i_bps
+
1396 else if (sh_video
&& sh_video
->i_bps
)
1397 get_time_ans
= (double) (demuxer
->movi_end
-
1398 demuxer
->movi_start
) / sh_video
->i_bps
;
1399 else if (sh_audio
&& sh_audio
->i_bps
)
1400 get_time_ans
= (double) (demuxer
->movi_end
-
1401 demuxer
->movi_start
) / sh_audio
->i_bps
;
1405 return get_time_ans
;
1409 * \brief demuxer_get_current_time() returns the time of the current play in three possible ways:
1410 * either when the stream reader satisfies STREAM_CTRL_GET_CURRENT_TIME (e.g. dvd)
1411 * or using sh_video->pts when the former method fails
1413 * \return the current play time
1415 int demuxer_get_current_time(demuxer_t
*demuxer
)
1417 double get_time_ans
= 0;
1418 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1419 if (demuxer
->stream_pts
!= MP_NOPTS_VALUE
)
1420 get_time_ans
= demuxer
->stream_pts
;
1422 get_time_ans
= sh_video
->pts
;
1423 return (int) get_time_ans
;
1426 int demuxer_get_percent_pos(demuxer_t
*demuxer
)
1429 int res
= demux_control(demuxer
, DEMUXER_CTRL_GET_PERCENT_POS
, &ans
);
1430 int len
= (demuxer
->movi_end
- demuxer
->movi_start
) / 100;
1432 off_t pos
= demuxer
->filepos
> 0 ? demuxer
->filepos
: stream_tell(demuxer
->stream
);
1434 ans
= (pos
- demuxer
->movi_start
) / len
;
1445 int demuxer_switch_audio(demuxer_t
*demuxer
, int index
)
1447 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_AUDIO
, &index
);
1448 if (res
== DEMUXER_CTRL_NOTIMPL
) {
1449 struct sh_audio
*sh_audio
= demuxer
->audio
->sh
;
1450 return sh_audio
? sh_audio
->aid
: -2;
1452 if (demuxer
->audio
->id
>= 0) {
1453 struct sh_audio
*sh_audio
= demuxer
->a_streams
[demuxer
->audio
->id
];
1454 demuxer
->audio
->sh
= sh_audio
;
1455 index
= sh_audio
->aid
; // internal MPEG demuxers don't set it right
1458 demuxer
->audio
->sh
= NULL
;
1462 int demuxer_switch_video(demuxer_t
*demuxer
, int index
)
1464 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_VIDEO
, &index
);
1465 if (res
== DEMUXER_CTRL_NOTIMPL
)
1466 index
= demuxer
->video
->id
;
1467 if (demuxer
->video
->id
>= 0)
1468 demuxer
->video
->sh
= demuxer
->v_streams
[demuxer
->video
->id
];
1470 demuxer
->video
->sh
= NULL
;
1474 int demuxer_add_attachment(demuxer_t
*demuxer
, struct bstr name
,
1475 struct bstr type
, struct bstr data
)
1477 if (!(demuxer
->num_attachments
% 32))
1478 demuxer
->attachments
= talloc_realloc(demuxer
, demuxer
->attachments
,
1479 struct demux_attachment
,
1480 demuxer
->num_attachments
+ 32);
1482 struct demux_attachment
*att
=
1483 demuxer
->attachments
+ demuxer
->num_attachments
;
1484 att
->name
= talloc_strndup(demuxer
->attachments
, name
.start
, name
.len
);
1485 att
->type
= talloc_strndup(demuxer
->attachments
, type
.start
, type
.len
);
1486 att
->data
= talloc_size(demuxer
->attachments
, data
.len
);
1487 memcpy(att
->data
, data
.start
, data
.len
);
1488 att
->data_size
= data
.len
;
1490 return demuxer
->num_attachments
++;
1493 int demuxer_add_chapter(demuxer_t
*demuxer
, struct bstr name
,
1494 uint64_t start
, uint64_t end
)
1496 if (!(demuxer
->num_chapters
% 32))
1497 demuxer
->chapters
= talloc_realloc(demuxer
, demuxer
->chapters
,
1498 struct demux_chapter
,
1499 demuxer
->num_chapters
+ 32);
1501 demuxer
->chapters
[demuxer
->num_chapters
].start
= start
;
1502 demuxer
->chapters
[demuxer
->num_chapters
].end
= end
;
1503 demuxer
->chapters
[demuxer
->num_chapters
].name
= name
.len
?
1504 talloc_strndup(demuxer
->chapters
, name
.start
, name
.len
) :
1505 talloc_strdup(demuxer
->chapters
, mp_gtext("unknown"));
1507 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_ID=%d\n", demuxer
->num_chapters
);
1508 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_START=%"PRIu64
"\n", demuxer
->num_chapters
, start
);
1510 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_END=%"PRIu64
"\n", demuxer
->num_chapters
, end
);
1512 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_NAME=%.*s\n", demuxer
->num_chapters
, BSTR_P(name
));
1514 return demuxer
->num_chapters
++;
1518 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1519 * either using the demuxer->chapters structure set by the demuxer
1520 * or asking help to the stream layer (e.g. dvd)
1521 * \param chapter - chapter number wished - 0-based
1522 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1523 * \param chapter_name name of chapter found (set by this function is param is not null)
1524 * \return -1 on error, current chapter if successful
1527 int demuxer_seek_chapter(demuxer_t
*demuxer
, int chapter
, double *seek_pts
,
1528 char **chapter_name
)
1532 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1533 demux_flush(demuxer
);
1535 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_CHAPTER
,
1537 if (ris
!= STREAM_UNSUPPORTED
)
1538 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1540 // exit status may be ok, but main() doesn't have to seek itself
1541 // (because e.g. dvds depend on sectors, not on pts)
1545 *chapter_name
= NULL
;
1547 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1548 &num_chapters
) == STREAM_UNSUPPORTED
)
1551 *chapter_name
= talloc_size(NULL
, 16);
1552 sprintf(*chapter_name
, " of %3d", num_chapters
);
1556 return ris
!= STREAM_UNSUPPORTED
? chapter
: -1;
1557 } else { // chapters structure is set in the demuxer
1558 if (chapter
>= demuxer
->num_chapters
)
1563 *seek_pts
= demuxer
->chapters
[chapter
].start
/ 1000.0;
1566 *chapter_name
= talloc_strdup(NULL
, demuxer
->chapters
[chapter
].name
);
1572 int demuxer_get_current_chapter(demuxer_t
*demuxer
)
1575 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1576 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_CURRENT_CHAPTER
,
1577 &chapter
) == STREAM_UNSUPPORTED
)
1580 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1581 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1583 now
= (sh_video
? sh_video
->pts
: (sh_audio
? sh_audio
->pts
: 0))
1585 for (chapter
= demuxer
->num_chapters
- 1; chapter
>= 0; --chapter
) {
1586 if (demuxer
->chapters
[chapter
].start
<= now
)
1593 char *demuxer_chapter_name(demuxer_t
*demuxer
, int chapter
)
1595 if (demuxer
->num_chapters
&& demuxer
->chapters
) {
1596 if (chapter
>= 0 && chapter
< demuxer
->num_chapters
1597 && demuxer
->chapters
[chapter
].name
)
1598 return strdup(demuxer
->chapters
[chapter
].name
);
1603 char *demuxer_chapter_display_name(demuxer_t
*demuxer
, int chapter
)
1605 char *chapter_name
= demuxer_chapter_name(demuxer
, chapter
);
1607 char *tmp
= malloc(strlen(chapter_name
) + 14);
1608 snprintf(tmp
, 63, "(%d) %s", chapter
+ 1, chapter_name
);
1612 int chapter_num
= demuxer_chapter_count(demuxer
);
1614 if (chapter_num
<= 0)
1615 sprintf(tmp
, "(%d)", chapter
+ 1);
1617 sprintf(tmp
, "(%d) of %d", chapter
+ 1, chapter_num
);
1622 float demuxer_chapter_time(demuxer_t
*demuxer
, int chapter
, float *end
)
1624 if (demuxer
->num_chapters
&& demuxer
->chapters
&& chapter
>= 0
1625 && chapter
< demuxer
->num_chapters
) {
1627 *end
= demuxer
->chapters
[chapter
].end
/ 1000.0;
1628 return demuxer
->chapters
[chapter
].start
/ 1000.0;
1633 int demuxer_chapter_count(demuxer_t
*demuxer
)
1635 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1636 int num_chapters
= 0;
1637 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1638 &num_chapters
) == STREAM_UNSUPPORTED
)
1640 return num_chapters
;
1642 return demuxer
->num_chapters
;
1645 int demuxer_angles_count(demuxer_t
*demuxer
)
1647 int ris
, angles
= -1;
1649 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_ANGLES
, &angles
);
1650 if (ris
== STREAM_UNSUPPORTED
)
1655 int demuxer_get_current_angle(demuxer_t
*demuxer
)
1657 int ris
, curr_angle
= -1;
1658 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_ANGLE
, &curr_angle
);
1659 if (ris
== STREAM_UNSUPPORTED
)
1665 int demuxer_set_angle(demuxer_t
*demuxer
, int angle
)
1667 int ris
, angles
= -1;
1669 angles
= demuxer_angles_count(demuxer
);
1670 if ((angles
< 1) || (angle
> angles
))
1673 demux_flush(demuxer
);
1675 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SET_ANGLE
, &angle
);
1676 if (ris
== STREAM_UNSUPPORTED
)
1679 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1684 int demuxer_audio_track_by_lang(demuxer_t
*d
, char *lang
)
1687 lang
+= strspn(lang
, ",");
1688 while ((len
= strcspn(lang
, ",")) > 0) {
1689 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1690 sh_audio_t
*sh
= d
->a_streams
[i
];
1691 if (sh
&& sh
->lang
&& strncmp(sh
->lang
, lang
, len
) == 0)
1695 lang
+= strspn(lang
, ",");
1700 int demuxer_sub_track_by_lang(demuxer_t
*d
, char *lang
)
1703 lang
+= strspn(lang
, ",");
1704 while ((len
= strcspn(lang
, ",")) > 0) {
1705 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1706 sh_sub_t
*sh
= d
->s_streams
[i
];
1707 if (sh
&& sh
->lang
&& strncmp(sh
->lang
, lang
, len
) == 0)
1711 lang
+= strspn(lang
, ",");
1716 int demuxer_default_audio_track(demuxer_t
*d
)
1719 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1720 sh_audio_t
*sh
= d
->a_streams
[i
];
1721 if (sh
&& sh
->default_track
)
1724 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1725 sh_audio_t
*sh
= d
->a_streams
[i
];
1732 int demuxer_default_sub_track(demuxer_t
*d
)
1735 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1736 sh_sub_t
*sh
= d
->s_streams
[i
];
1737 if (sh
&& sh
->default_track
)