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
;
450 case MKTAG('d', 'n', 'e', 't'):
451 case MKTAG('s', 'a', 'c', '3'):
452 codec_id
= CODEC_ID_AC3
;
454 case MKTAG('E', 'A', 'C', '3'):
455 codec_id
= CODEC_ID_EAC3
;
459 codec_id
= CODEC_ID_DTS
;
461 case MKTAG('M', 'L', 'P', ' '):
462 codec_id
= CODEC_ID_MLP
;
466 case MKTAG('.', 'm', 'p', '3'):
467 case MKTAG('M', 'P', 'E', ' '):
468 case MKTAG('L', 'A', 'M', 'E'):
469 codec_id
= CODEC_ID_MP3
;
472 case MKTAG('.', 'm', 'p', '2'):
473 case MKTAG('.', 'm', 'p', '1'):
474 codec_id
= CODEC_ID_MP2
;
476 case MKTAG('T', 'R', 'H', 'D'):
477 codec_id
= CODEC_ID_TRUEHD
;
480 if (codec_id
!= CODEC_ID_NONE
) {
481 *avctx
= avcodec_alloc_context();
484 *parser
= av_parser_init(codec_id
);
490 static void get_parser(sh_common_t
*sh
, AVCodecContext
**avctx
, AVCodecParserContext
**parser
)
495 if (!sh
|| !sh
->needs_parsing
)
499 *parser
= sh
->parser
;
503 allocate_parser(avctx
, parser
, sh
->format
);
505 sh
->parser
= *parser
;
508 int ds_parse(demux_stream_t
*ds
, uint8_t **buffer
, int *len
, double pts
, off_t pos
)
510 AVCodecContext
*avctx
;
511 AVCodecParserContext
*parser
;
512 get_parser(ds
->sh
, &avctx
, &parser
);
515 return av_parser_parse2(parser
, avctx
, buffer
, len
, *buffer
, *len
, pts
, pts
, pos
);
518 static void clear_parser(sh_common_t
*sh
)
520 av_parser_close(sh
->parser
);
522 av_freep(&sh
->avctx
);
525 void ds_clear_parser(demux_stream_t
*ds
)
529 clear_parser(ds
->sh
);
533 void ds_add_packet(demux_stream_t
*ds
, demux_packet_t
*dp
)
535 #if PARSE_ON_ADD && defined(CONFIG_FFMPEG)
539 uint8_t *parsed_start
= dp
->buffer
+ pos
;
540 int parsed_len
= len
;
541 int consumed
= ds_parse(ds
->sh
, &parsed_start
, &parsed_len
, dp
->pts
, dp
->pos
);
544 if (parsed_start
== dp
->buffer
&& parsed_len
== dp
->len
) {
545 ds_add_packet_internal(ds
, dp
);
546 } else if (parsed_len
) {
547 demux_packet_t
*dp2
= new_demux_packet(parsed_len
);
549 dp2
->pts
= dp
->pts
; // should be parser->pts but that works badly
550 memcpy(dp2
->buffer
, parsed_start
, parsed_len
);
551 ds_add_packet_internal(ds
, dp2
);
555 ds_add_packet_internal(ds
, dp
);
559 void ds_read_packet(demux_stream_t
*ds
, stream_t
*stream
, int len
,
560 double pts
, off_t pos
, int flags
)
562 demux_packet_t
*dp
= new_demux_packet(len
);
563 len
= stream_read(stream
, dp
->buffer
, len
);
564 resize_demux_packet(dp
, len
);
568 // append packet to DS stream:
569 ds_add_packet(ds
, dp
);
573 // 0 = EOF or no stream found or invalid type
574 // 1 = successfully read a packet
576 int demux_fill_buffer(demuxer_t
*demux
, demux_stream_t
*ds
)
578 // Note: parameter 'ds' can be NULL!
579 return demux
->desc
->fill_buffer(demux
, ds
);
585 #define MAX_ACUMULATED_PACKETS 64
586 int ds_fill_buffer(demux_stream_t
*ds
)
588 demuxer_t
*demux
= ds
->demuxer
;
590 free_demux_packet(ds
->current
);
592 if (mp_msg_test(MSGT_DEMUXER
, MSGL_DBG3
)) {
593 if (ds
== demux
->audio
)
594 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
595 "ds_fill_buffer(d_audio) called\n");
596 else if (ds
== demux
->video
)
597 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
598 "ds_fill_buffer(d_video) called\n");
599 else if (ds
== demux
->sub
)
600 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
, "ds_fill_buffer(d_sub) called\n");
602 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
603 "ds_fill_buffer(unknown 0x%X) called\n", (unsigned int) ds
);
607 demux_packet_t
*p
= ds
->first
;
609 if (demux
->reference_clock
!= MP_NOPTS_VALUE
) {
610 if ( p
->pts
!= MP_NOPTS_VALUE
611 && p
->pts
> demux
->reference_clock
612 && ds
->packs
< MAX_ACUMULATED_PACKETS
) {
613 if (demux_fill_buffer(demux
, ds
))
619 ds
->buffer
= p
->buffer
;
621 ds
->buffer_size
= p
->len
;
623 ds
->dpos
+= p
->len
; // !!!
625 if (p
->pts
!= MP_NOPTS_VALUE
) {
629 ds
->pts_bytes
+= p
->len
; // !!!
630 if (p
->stream_pts
!= MP_NOPTS_VALUE
)
631 demux
->stream_pts
= p
->stream_pts
;
632 ds
->flags
= p
->flags
;
640 /* The code below can set ds->eof to 1 when another stream runs
641 * out of buffer space. That makes sense because in that situation
642 * the calling code should not count on being able to demux more
643 * packets from this stream.
644 * If however the situation improves and we're called again
645 * despite the eof flag then it's better to clear it to avoid
651 #define MaybeNI _("Maybe you are playing a non-interleaved stream/file or the codec failed?\n" \
652 "For AVI files, try to force non-interleaved mode with the -ni option.\n")
654 if (demux
->audio
->packs
>= MAX_PACKS
655 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
656 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
657 demux
->audio
->packs
, demux
->audio
->bytes
);
658 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
661 if (demux
->video
->packs
>= MAX_PACKS
662 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
663 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
664 demux
->video
->packs
, demux
->video
->bytes
);
665 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
668 if (!demux_fill_buffer(demux
, ds
)) {
669 #if PARSE_ON_ADD && defined(CONFIG_FFMPEG)
670 uint8_t *parsed_start
= NULL
;
672 ds_parse(ds
->sh
, &parsed_start
, &parsed_len
, MP_NOPTS_VALUE
, 0);
674 demux_packet_t
*dp2
= new_demux_packet(parsed_len
);
675 dp2
->pts
= MP_NOPTS_VALUE
;
676 memcpy(dp2
->buffer
, parsed_start
, parsed_len
);
677 ds_add_packet_internal(ds
, dp2
);
681 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
682 "ds_fill_buffer()->demux_fill_buffer() failed\n");
686 ds
->buffer_pos
= ds
->buffer_size
= 0;
688 mp_msg(MSGT_DEMUXER
, MSGL_V
,
689 "ds_fill_buffer: EOF reached (stream: %s) \n",
690 ds
== demux
->audio
? "audio" : "video");
695 int demux_read_data(demux_stream_t
*ds
, unsigned char *mem
, int len
)
700 x
= ds
->buffer_size
- ds
->buffer_pos
;
702 if (!ds_fill_buffer(ds
))
708 fast_memcpy(mem
+ bytes
, &ds
->buffer
[ds
->buffer_pos
], x
);
718 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
719 * \param mem memory to read data into, may be NULL to discard data
720 * \param maxlen maximum number of bytes to read
721 * \param read number of bytes actually read
722 * \param pattern pattern to search for (lowest 8 bits are ignored)
723 * \return whether pattern was found
725 int demux_pattern_3(demux_stream_t
*ds
, unsigned char *mem
, int maxlen
,
726 int *read
, uint32_t pattern
)
728 register uint32_t head
= 0xffffff00;
729 register uint32_t pat
= pattern
& 0xffffff00;
732 register unsigned char *ds_buf
= &ds
->buffer
[ds
->buffer_size
];
733 int len
= ds
->buffer_size
- ds
->buffer_pos
;
734 register long pos
= -len
;
735 if (unlikely(pos
>= 0)) { // buffer is empty
742 } while (++pos
&& head
!= pat
);
744 if (total_len
+ len
> maxlen
)
745 len
= maxlen
- total_len
;
746 len
= demux_read_data(ds
, mem
? &mem
[total_len
] : NULL
, len
);
748 } while ((head
!= pat
|| total_len
< 3) && total_len
< maxlen
&& !ds
->eof
);
751 return total_len
>= 3 && head
== pat
;
754 void ds_free_packs(demux_stream_t
*ds
)
756 demux_packet_t
*dp
= ds
->first
;
758 demux_packet_t
*dn
= dp
->next
;
759 free_demux_packet(dp
);
762 if (ds
->asf_packet
) {
763 // free unfinished .asf fragments:
764 free(ds
->asf_packet
->buffer
);
765 free(ds
->asf_packet
);
766 ds
->asf_packet
= NULL
;
768 ds
->first
= ds
->last
= NULL
;
769 ds
->packs
= 0; // !!!!!
772 free_demux_packet(ds
->current
);
775 ds
->buffer_pos
= ds
->buffer_size
;
780 int ds_get_packet(demux_stream_t
*ds
, unsigned char **start
)
783 if (ds
->buffer_pos
>= ds
->buffer_size
) {
784 if (!ds_fill_buffer(ds
)) {
790 len
= ds
->buffer_size
- ds
->buffer_pos
;
791 *start
= &ds
->buffer
[ds
->buffer_pos
];
792 ds
->buffer_pos
+= len
;
796 int ds_get_packet_pts(demux_stream_t
*ds
, unsigned char **start
, double *pts
)
799 *pts
= MP_NOPTS_VALUE
;
800 len
= ds_get_packet(ds
, start
);
803 // Return pts unless this read starts from the middle of a packet
804 if (len
== ds
->buffer_pos
)
805 *pts
= ds
->current
->pts
;
809 int ds_get_packet_sub(demux_stream_t
*ds
, unsigned char **start
)
812 if (ds
->buffer_pos
>= ds
->buffer_size
) {
816 if (!ds_fill_buffer(ds
))
819 len
= ds
->buffer_size
- ds
->buffer_pos
;
820 *start
= &ds
->buffer
[ds
->buffer_pos
];
821 ds
->buffer_pos
+= len
;
825 double ds_get_next_pts(demux_stream_t
*ds
)
827 demuxer_t
*demux
= ds
->demuxer
;
828 // if we have not read from the "current" packet, consider it
829 // as the next, otherwise we never get the pts for the first packet.
830 while (!ds
->first
&& (!ds
->current
|| ds
->buffer_pos
)) {
831 if (demux
->audio
->packs
>= MAX_PACKS
832 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
833 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
834 demux
->audio
->packs
, demux
->audio
->bytes
);
835 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
836 return MP_NOPTS_VALUE
;
838 if (demux
->video
->packs
>= MAX_PACKS
839 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
840 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
841 demux
->video
->packs
, demux
->video
->bytes
);
842 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
843 return MP_NOPTS_VALUE
;
845 if (!demux_fill_buffer(demux
, ds
))
846 return MP_NOPTS_VALUE
;
848 // take pts from "current" if we never read from it.
849 if (ds
->current
&& !ds
->buffer_pos
)
850 return ds
->current
->pts
;
851 return ds
->first
->pts
;
854 // ====================================================================
856 void demuxer_help(void)
860 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "Available demuxers:\n");
861 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, " demuxer: type info: (comment)\n");
862 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DEMUXERS\n");
863 for (i
= 0; demuxer_list
[i
]; i
++) {
864 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Don't display special demuxers
866 if (demuxer_list
[i
]->comment
&& strlen(demuxer_list
[i
]->comment
))
867 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s (%s)\n",
868 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
869 demuxer_list
[i
]->info
, demuxer_list
[i
]->comment
);
871 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s\n",
872 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
873 demuxer_list
[i
]->info
);
879 * Get demuxer type for a given demuxer name
881 * @param demuxer_name string with demuxer name of demuxer number
882 * @param force will be set if demuxer should be forced.
884 * @return DEMUXER_TYPE_xxx, -1 if error or not found
886 int get_demuxer_type_from_name(char *demuxer_name
, int *force
)
892 if (!demuxer_name
|| !demuxer_name
[0])
893 return DEMUXER_TYPE_UNKNOWN
;
895 *force
= demuxer_name
[0] == '+';
896 if (demuxer_name
[0] == '+')
897 demuxer_name
= &demuxer_name
[1];
898 for (i
= 0; demuxer_list
[i
]; i
++) {
899 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Can't select special demuxers from commandline
901 if (strcmp(demuxer_name
, demuxer_list
[i
]->name
) == 0)
902 return demuxer_list
[i
]->type
;
905 // No match found, try to parse name as an integer (demuxer number)
906 type_int
= strtol(demuxer_name
, &endptr
, 0);
907 if (*endptr
) // Conversion failed
909 if ((type_int
> 0) && (type_int
<= DEMUXER_TYPE_MAX
))
910 return (int) type_int
;
915 int extension_parsing
= 1; // 0=off 1=mixed (used only for unstable formats)
918 NOTE : Several demuxers may be opened at the same time so
919 demuxers should NEVER rely on an external var to enable them
920 self. If a demuxer can't do any auto-detection it should only use
921 file_format. The user can explicitly set file_format with the -demuxer
922 option so there is really no need for another extra var.
923 For convenience an option can be added to set file_format directly
924 to the right type (ex: rawaudio,rawvideo).
925 Also the stream can override the file_format so a demuxer which rely
926 on a special stream type can set file_format at the stream level
930 static demuxer_t
*demux_open_stream(struct MPOpts
*opts
, stream_t
*stream
,
931 int file_format
, int force
, int audio_id
,
932 int video_id
, int dvdsub_id
,
935 demuxer_t
*demuxer
= NULL
;
937 sh_video_t
*sh_video
= NULL
;
939 const demuxer_desc_t
*demuxer_desc
;
943 // If somebody requested a demuxer check it
945 if ((demuxer_desc
= get_demuxer_desc_from_type(file_format
))) {
946 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
947 video_id
, dvdsub_id
, filename
);
948 if (demuxer_desc
->check_file
)
949 fformat
= demuxer_desc
->check_file(demuxer
);
950 if (force
|| !demuxer_desc
->check_file
)
951 fformat
= demuxer_desc
->type
;
953 if (fformat
== demuxer_desc
->type
) {
954 demuxer_t
*demux2
= demuxer
;
955 // Move messages to demuxer detection code?
956 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
957 "%s file format detected.\n",
958 demuxer_desc
->shortdesc
);
959 file_format
= fformat
;
960 if (!demuxer
->desc
->open
961 || (demux2
= demuxer
->desc
->open(demuxer
))) {
966 // Format changed after check, recurse
967 free_demuxer(demuxer
);
968 return demux_open_stream(opts
, stream
, fformat
, force
,
969 audio_id
, video_id
, dvdsub_id
,
973 // Check failed for forced demuxer, quit
974 free_demuxer(demuxer
);
978 // Test demuxers with safe file checks
979 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
980 if (demuxer_desc
->safe_check
) {
981 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
982 video_id
, dvdsub_id
, filename
);
983 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
984 if (fformat
== demuxer_desc
->type
) {
985 demuxer_t
*demux2
= demuxer
;
986 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
987 "%s file format detected.\n",
988 demuxer_desc
->shortdesc
);
989 file_format
= fformat
;
990 if (!demuxer
->desc
->open
991 || (demux2
= demuxer
->desc
->open(demuxer
))) {
996 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
997 return demuxer
; // handled in mplayer.c
998 // Format changed after check, recurse
999 free_demuxer(demuxer
);
1000 demuxer
= demux_open_stream(opts
, stream
, fformat
, force
,
1002 dvdsub_id
, filename
);
1004 return demuxer
; // done!
1005 file_format
= DEMUXER_TYPE_UNKNOWN
;
1008 free_demuxer(demuxer
);
1013 // If no forced demuxer perform file extension based detection
1014 // Ok. We're over the stable detectable fileformats, the next ones are
1015 // a bit fuzzy. So by default (extension_parsing==1) try extension-based
1017 if (file_format
== DEMUXER_TYPE_UNKNOWN
&& filename
1018 && extension_parsing
== 1) {
1019 file_format
= demuxer_type_by_filename(filename
);
1020 if (file_format
!= DEMUXER_TYPE_UNKNOWN
) {
1021 // we like recursion :)
1022 demuxer
= demux_open_stream(opts
, stream
, file_format
, force
,
1023 audio_id
, video_id
, dvdsub_id
,
1026 return demuxer
; // done!
1027 file_format
= DEMUXER_TYPE_UNKNOWN
; // continue fuzzy guessing...
1028 mp_msg(MSGT_DEMUXER
, MSGL_V
,
1029 "demuxer: continue fuzzy content-based format guessing...\n");
1032 // Try detection for all other demuxers
1033 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
1034 if (!demuxer_desc
->safe_check
&& demuxer_desc
->check_file
) {
1035 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
1036 video_id
, dvdsub_id
, filename
);
1037 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
1038 if (fformat
== demuxer_desc
->type
) {
1039 demuxer_t
*demux2
= demuxer
;
1040 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
1041 "%s file format detected.\n",
1042 demuxer_desc
->shortdesc
);
1043 file_format
= fformat
;
1044 if (!demuxer
->desc
->open
1045 || (demux2
= demuxer
->desc
->open(demuxer
))) {
1050 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
1051 return demuxer
; // handled in mplayer.c
1052 // Format changed after check, recurse
1053 free_demuxer(demuxer
);
1054 demuxer
= demux_open_stream(opts
, stream
, fformat
, force
,
1056 dvdsub_id
, filename
);
1058 return demuxer
; // done!
1059 file_format
= DEMUXER_TYPE_UNKNOWN
;
1062 free_demuxer(demuxer
);
1068 //====== File format recognized, set up these for compatibility: =========
1071 demuxer
->file_format
= file_format
;
1073 if ((sh_video
= demuxer
->video
->sh
) && sh_video
->bih
) {
1074 int biComp
= le2me_32(sh_video
->bih
->biCompression
);
1075 mp_msg(MSGT_DEMUX
, MSGL_INFO
,
1076 "VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
1077 (char *) &biComp
, sh_video
->bih
->biWidth
,
1078 sh_video
->bih
->biHeight
, sh_video
->bih
->biBitCount
,
1079 sh_video
->fps
, sh_video
->i_bps
* 0.008f
,
1080 sh_video
->i_bps
/ 1024.0f
);
1083 if (opts
->ass_enabled
&& ass_library
) {
1084 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1085 sh_sub_t
*sh
= demuxer
->s_streams
[i
];
1086 if (sh
&& sh
->type
== 'a') {
1087 sh
->ass_track
= ass_new_track(ass_library
);
1088 if (sh
->ass_track
&& sh
->extradata
)
1089 ass_process_codec_private(sh
->ass_track
, sh
->extradata
,
1091 } else if (sh
&& sh
->type
!= 'v')
1092 sh
->ass_track
= ass_default_track(ass_library
);
1099 char *audio_stream
= NULL
;
1100 char *sub_stream
= NULL
;
1101 int audio_stream_cache
= 0;
1103 char *demuxer_name
= NULL
; // parameter from -demuxer
1104 char *audio_demuxer_name
= NULL
; // parameter from -audio-demuxer
1105 char *sub_demuxer_name
= NULL
; // parameter from -sub-demuxer
1107 extern int hr_mp3_seek
;
1109 extern float stream_cache_min_percent
;
1110 extern float stream_cache_seek_min_percent
;
1112 demuxer_t
*demux_open(struct MPOpts
*opts
, stream_t
*vs
, int file_format
,
1113 int audio_id
, int video_id
, int dvdsub_id
,
1116 stream_t
*as
= NULL
, *ss
= NULL
;
1117 demuxer_t
*vd
, *ad
= NULL
, *sd
= NULL
;
1119 int afmt
= DEMUXER_TYPE_UNKNOWN
, sfmt
= DEMUXER_TYPE_UNKNOWN
;
1121 int audio_demuxer_type
= 0, sub_demuxer_type
= 0;
1122 int demuxer_force
= 0, audio_demuxer_force
= 0, sub_demuxer_force
= 0;
1125 get_demuxer_type_from_name(demuxer_name
, &demuxer_force
)) < 0) {
1126 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-demuxer %s does not exist.\n",
1129 if ((audio_demuxer_type
=
1130 get_demuxer_type_from_name(audio_demuxer_name
,
1131 &audio_demuxer_force
)) < 0) {
1132 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-audio-demuxer %s does not exist.\n",
1133 audio_demuxer_name
);
1135 if ((sub_demuxer_type
=
1136 get_demuxer_type_from_name(sub_demuxer_name
,
1137 &sub_demuxer_force
)) < 0) {
1138 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-sub-demuxer %s does not exist.\n",
1143 as
= open_stream(audio_stream
, 0, &afmt
);
1145 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "Cannot open audio stream: %s\n",
1149 if (audio_stream_cache
) {
1150 if (!stream_enable_cache
1151 (as
, audio_stream_cache
* 1024,
1152 audio_stream_cache
* 1024 * (stream_cache_min_percent
/
1154 audio_stream_cache
* 1024 * (stream_cache_seek_min_percent
/
1157 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
1158 "Can't enable audio stream cache\n");
1164 ss
= open_stream(sub_stream
, 0, &sfmt
);
1166 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "Cannot open subtitle stream: %s\n",
1172 vd
= demux_open_stream(opts
, vs
, demuxer_type
? demuxer_type
: file_format
,
1173 demuxer_force
, audio_stream
? -2 : audio_id
,
1174 video_id
, sub_stream
? -2 : dvdsub_id
, filename
);
1183 ad
= demux_open_stream(opts
, as
,
1184 audio_demuxer_type
? audio_demuxer_type
: afmt
,
1185 audio_demuxer_force
, audio_id
, -2, -2,
1188 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, "Failed to open audio demuxer: %s\n",
1191 } else if (ad
->audio
->sh
1192 && ((sh_audio_t
*) ad
->audio
->sh
)->format
== 0x55) // MP3
1193 hr_mp3_seek
= 1; // Enable high res seeking
1196 sd
= demux_open_stream(opts
, ss
,
1197 sub_demuxer_type
? sub_demuxer_type
: sfmt
,
1198 sub_demuxer_force
, -2, -2, dvdsub_id
,
1201 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
,
1202 "Failed to open subtitle demuxer: %s\n", sub_stream
);
1208 res
= new_demuxers_demuxer(vd
, ad
, sd
);
1210 res
= new_demuxers_demuxer(vd
, ad
, vd
);
1212 res
= new_demuxers_demuxer(vd
, vd
, sd
);
1216 opts
->correct_pts
= opts
->user_correct_pts
;
1217 if (opts
->correct_pts
< 0)
1219 demux_control(res
, DEMUXER_CTRL_CORRECT_PTS
,
1220 NULL
) == DEMUXER_CTRL_OK
;
1225 void demux_flush(demuxer_t
*demuxer
)
1228 ds_clear_parser(demuxer
->video
);
1229 ds_clear_parser(demuxer
->audio
);
1230 ds_clear_parser(demuxer
->sub
);
1232 ds_free_packs(demuxer
->video
);
1233 ds_free_packs(demuxer
->audio
);
1234 ds_free_packs(demuxer
->sub
);
1237 int demux_seek(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
,
1240 if (!demuxer
->seekable
) {
1241 if (demuxer
->file_format
== DEMUXER_TYPE_AVI
)
1242 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "Cannot seek in raw AVI streams. (Index required, try with the -idx switch.)\n");
1244 else if (demuxer
->file_format
== DEMUXER_TYPE_TV
)
1245 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "TV input is not seekable! (Seeking will probably be for changing channels ;)\n");
1248 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "Cannot seek in this file.\n");
1251 // clear demux buffers:
1252 demux_flush(demuxer
);
1253 demuxer
->video
->eof
= 0;
1254 demuxer
->audio
->eof
= 0;
1255 demuxer
->sub
->eof
= 0;
1257 /* HACK: assume any demuxer used with these streams can cope with
1258 * the stream layer suddenly seeking to a different position under it
1259 * (nothing actually implements DEMUXER_CTRL_RESYNC now).
1261 struct stream
*stream
= demuxer
->stream
;
1262 if (stream
->type
== STREAMTYPE_DVD
|| stream
->type
== STREAMTYPE_DVDNAV
) {
1265 if (flags
& SEEK_ABSOLUTE
)
1268 if (demuxer
->stream_pts
== MP_NOPTS_VALUE
)
1270 pts
= demuxer
->stream_pts
;
1273 if (flags
& SEEK_FACTOR
) {
1275 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_TIME_LENGTH
,
1276 &tmp
) == STREAM_UNSUPPORTED
)
1278 pts
+= tmp
* rel_seek_secs
;
1280 pts
+= rel_seek_secs
;
1282 if (stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_TIME
, &pts
)
1283 != STREAM_UNSUPPORTED
) {
1284 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1290 if (demuxer
->desc
->seek
)
1291 demuxer
->desc
->seek(demuxer
, rel_seek_secs
, audio_delay
, flags
);
1296 int demux_info_add(demuxer_t
*demuxer
, const char *opt
, const char *param
)
1298 return demux_info_add_bstr(demuxer
, BSTR(opt
), BSTR(param
));
1301 int demux_info_add_bstr(demuxer_t
*demuxer
, struct bstr opt
, struct bstr param
)
1303 char **info
= demuxer
->info
;
1307 for (n
= 0; info
&& info
[2 * n
] != NULL
; n
++) {
1308 if (!bstrcasecmp(opt
, BSTR(info
[2*n
]))) {
1309 if (!bstrcmp(param
, BSTR(info
[2*n
+ 1]))) {
1310 mp_msg(MSGT_DEMUX
, MSGL_V
, "Demuxer info %.*s set to unchanged value %.*s\n",
1311 BSTR_P(opt
), BSTR_P(param
));
1314 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "Demuxer info %.*s changed to %.*s\n",
1315 BSTR_P(opt
), BSTR_P(param
));
1316 talloc_free(info
[2*n
+ 1]);
1317 info
[2*n
+ 1] = talloc_strndup(demuxer
->info
, param
.start
, param
.len
);
1322 info
= demuxer
->info
= talloc_realloc(demuxer
, info
, char *, 2 * (n
+ 2));
1323 info
[2*n
] = talloc_strndup(demuxer
->info
, opt
.start
, opt
.len
);
1324 info
[2*n
+ 1] = talloc_strndup(demuxer
->info
, param
.start
, param
.len
);
1325 memset(&info
[2 * (n
+ 1)], 0, 2 * sizeof(char *));
1330 int demux_info_print(demuxer_t
*demuxer
)
1332 char **info
= demuxer
->info
;
1338 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "Clip info:\n");
1339 for (n
= 0; info
[2 * n
] != NULL
; n
++) {
1340 mp_msg(MSGT_DEMUX
, MSGL_INFO
, " %s: %s\n", info
[2 * n
],
1342 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_NAME%d=%s\n", n
,
1344 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_VALUE%d=%s\n", n
,
1347 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_N=%d\n", n
);
1352 char *demux_info_get(demuxer_t
*demuxer
, const char *opt
)
1355 char **info
= demuxer
->info
;
1357 for (i
= 0; info
&& info
[2 * i
] != NULL
; i
++) {
1358 if (!strcasecmp(opt
, info
[2 * i
]))
1359 return info
[2 * i
+ 1];
1365 int demux_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
1368 if (demuxer
->desc
->control
)
1369 return demuxer
->desc
->control(demuxer
, cmd
, arg
);
1371 return DEMUXER_CTRL_NOTIMPL
;
1376 double demuxer_get_time_length(demuxer_t
*demuxer
)
1378 double get_time_ans
;
1379 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1380 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1381 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
1383 (demuxer
, DEMUXER_CTRL_GET_TIME_LENGTH
, (void *) &get_time_ans
) <= 0) {
1384 if (sh_video
&& sh_video
->i_bps
&& sh_audio
&& sh_audio
->i_bps
)
1385 get_time_ans
= (double) (demuxer
->movi_end
-
1386 demuxer
->movi_start
) / (sh_video
->i_bps
+
1388 else if (sh_video
&& sh_video
->i_bps
)
1389 get_time_ans
= (double) (demuxer
->movi_end
-
1390 demuxer
->movi_start
) / sh_video
->i_bps
;
1391 else if (sh_audio
&& sh_audio
->i_bps
)
1392 get_time_ans
= (double) (demuxer
->movi_end
-
1393 demuxer
->movi_start
) / sh_audio
->i_bps
;
1397 return get_time_ans
;
1401 * \brief demuxer_get_current_time() returns the time of the current play in three possible ways:
1402 * either when the stream reader satisfies STREAM_CTRL_GET_CURRENT_TIME (e.g. dvd)
1403 * or using sh_video->pts when the former method fails
1405 * \return the current play time
1407 int demuxer_get_current_time(demuxer_t
*demuxer
)
1409 double get_time_ans
= 0;
1410 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1411 if (demuxer
->stream_pts
!= MP_NOPTS_VALUE
)
1412 get_time_ans
= demuxer
->stream_pts
;
1414 get_time_ans
= sh_video
->pts
;
1415 return (int) get_time_ans
;
1418 int demuxer_get_percent_pos(demuxer_t
*demuxer
)
1421 int res
= demux_control(demuxer
, DEMUXER_CTRL_GET_PERCENT_POS
, &ans
);
1422 int len
= (demuxer
->movi_end
- demuxer
->movi_start
) / 100;
1424 off_t pos
= demuxer
->filepos
> 0 ? demuxer
->filepos
: stream_tell(demuxer
->stream
);
1426 ans
= (pos
- demuxer
->movi_start
) / len
;
1437 int demuxer_switch_audio(demuxer_t
*demuxer
, int index
)
1439 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_AUDIO
, &index
);
1440 if (res
== DEMUXER_CTRL_NOTIMPL
) {
1441 struct sh_audio
*sh_audio
= demuxer
->audio
->sh
;
1442 return sh_audio
? sh_audio
->aid
: -2;
1444 if (demuxer
->audio
->id
>= 0) {
1445 struct sh_audio
*sh_audio
= demuxer
->a_streams
[demuxer
->audio
->id
];
1446 demuxer
->audio
->sh
= sh_audio
;
1447 index
= sh_audio
->aid
; // internal MPEG demuxers don't set it right
1450 demuxer
->audio
->sh
= NULL
;
1454 int demuxer_switch_video(demuxer_t
*demuxer
, int index
)
1456 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_VIDEO
, &index
);
1457 if (res
== DEMUXER_CTRL_NOTIMPL
)
1458 index
= demuxer
->video
->id
;
1459 if (demuxer
->video
->id
>= 0)
1460 demuxer
->video
->sh
= demuxer
->v_streams
[demuxer
->video
->id
];
1462 demuxer
->video
->sh
= NULL
;
1466 int demuxer_add_attachment(demuxer_t
*demuxer
, struct bstr name
,
1467 struct bstr type
, struct bstr data
)
1469 if (!(demuxer
->num_attachments
% 32))
1470 demuxer
->attachments
= talloc_realloc(demuxer
, demuxer
->attachments
,
1471 struct demux_attachment
,
1472 demuxer
->num_attachments
+ 32);
1474 struct demux_attachment
*att
=
1475 demuxer
->attachments
+ demuxer
->num_attachments
;
1476 att
->name
= talloc_strndup(demuxer
->attachments
, name
.start
, name
.len
);
1477 att
->type
= talloc_strndup(demuxer
->attachments
, type
.start
, type
.len
);
1478 att
->data
= talloc_size(demuxer
->attachments
, data
.len
);
1479 memcpy(att
->data
, data
.start
, data
.len
);
1480 att
->data_size
= data
.len
;
1482 return demuxer
->num_attachments
++;
1485 int demuxer_add_chapter(demuxer_t
*demuxer
, struct bstr name
,
1486 uint64_t start
, uint64_t end
)
1488 if (!(demuxer
->num_chapters
% 32))
1489 demuxer
->chapters
= talloc_realloc(demuxer
, demuxer
->chapters
,
1490 struct demux_chapter
,
1491 demuxer
->num_chapters
+ 32);
1493 demuxer
->chapters
[demuxer
->num_chapters
].start
= start
;
1494 demuxer
->chapters
[demuxer
->num_chapters
].end
= end
;
1495 demuxer
->chapters
[demuxer
->num_chapters
].name
= name
.len
?
1496 talloc_strndup(demuxer
->chapters
, name
.start
, name
.len
) :
1497 talloc_strdup(demuxer
->chapters
, mp_gtext("unknown"));
1499 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_ID=%d\n", demuxer
->num_chapters
);
1500 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_START=%"PRIu64
"\n", demuxer
->num_chapters
, start
);
1502 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_END=%"PRIu64
"\n", demuxer
->num_chapters
, end
);
1504 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_NAME=%.*s\n", demuxer
->num_chapters
, BSTR_P(name
));
1506 return demuxer
->num_chapters
++;
1510 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1511 * either using the demuxer->chapters structure set by the demuxer
1512 * or asking help to the stream layer (e.g. dvd)
1513 * \param chapter - chapter number wished - 0-based
1514 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1515 * \param chapter_name name of chapter found (set by this function is param is not null)
1516 * \return -1 on error, current chapter if successful
1519 int demuxer_seek_chapter(demuxer_t
*demuxer
, int chapter
, double *seek_pts
,
1520 char **chapter_name
)
1524 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1525 demux_flush(demuxer
);
1527 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_CHAPTER
,
1529 if (ris
!= STREAM_UNSUPPORTED
)
1530 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1532 // exit status may be ok, but main() doesn't have to seek itself
1533 // (because e.g. dvds depend on sectors, not on pts)
1537 *chapter_name
= NULL
;
1539 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1540 &num_chapters
) == STREAM_UNSUPPORTED
)
1543 *chapter_name
= talloc_size(NULL
, 16);
1544 sprintf(*chapter_name
, " of %3d", num_chapters
);
1548 return ris
!= STREAM_UNSUPPORTED
? chapter
: -1;
1549 } else { // chapters structure is set in the demuxer
1550 if (chapter
>= demuxer
->num_chapters
)
1555 *seek_pts
= demuxer
->chapters
[chapter
].start
/ 1000.0;
1558 *chapter_name
= talloc_strdup(NULL
, demuxer
->chapters
[chapter
].name
);
1564 int demuxer_get_current_chapter(demuxer_t
*demuxer
)
1567 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1568 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_CURRENT_CHAPTER
,
1569 &chapter
) == STREAM_UNSUPPORTED
)
1572 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1573 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1575 now
= (sh_video
? sh_video
->pts
: (sh_audio
? sh_audio
->pts
: 0))
1577 for (chapter
= demuxer
->num_chapters
- 1; chapter
>= 0; --chapter
) {
1578 if (demuxer
->chapters
[chapter
].start
<= now
)
1585 char *demuxer_chapter_name(demuxer_t
*demuxer
, int chapter
)
1587 if (demuxer
->num_chapters
&& demuxer
->chapters
) {
1588 if (chapter
>= 0 && chapter
< demuxer
->num_chapters
1589 && demuxer
->chapters
[chapter
].name
)
1590 return strdup(demuxer
->chapters
[chapter
].name
);
1595 char *demuxer_chapter_display_name(demuxer_t
*demuxer
, int chapter
)
1597 char *chapter_name
= demuxer_chapter_name(demuxer
, chapter
);
1599 char *tmp
= malloc(strlen(chapter_name
) + 14);
1600 snprintf(tmp
, 63, "(%d) %s", chapter
+ 1, chapter_name
);
1604 int chapter_num
= demuxer_chapter_count(demuxer
);
1606 if (chapter_num
<= 0)
1607 sprintf(tmp
, "(%d)", chapter
+ 1);
1609 sprintf(tmp
, "(%d) of %d", chapter
+ 1, chapter_num
);
1614 float demuxer_chapter_time(demuxer_t
*demuxer
, int chapter
, float *end
)
1616 if (demuxer
->num_chapters
&& demuxer
->chapters
&& chapter
>= 0
1617 && chapter
< demuxer
->num_chapters
) {
1619 *end
= demuxer
->chapters
[chapter
].end
/ 1000.0;
1620 return demuxer
->chapters
[chapter
].start
/ 1000.0;
1625 int demuxer_chapter_count(demuxer_t
*demuxer
)
1627 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1628 int num_chapters
= 0;
1629 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1630 &num_chapters
) == STREAM_UNSUPPORTED
)
1632 return num_chapters
;
1634 return demuxer
->num_chapters
;
1637 int demuxer_angles_count(demuxer_t
*demuxer
)
1639 int ris
, angles
= -1;
1641 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_ANGLES
, &angles
);
1642 if (ris
== STREAM_UNSUPPORTED
)
1647 int demuxer_get_current_angle(demuxer_t
*demuxer
)
1649 int ris
, curr_angle
= -1;
1650 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_ANGLE
, &curr_angle
);
1651 if (ris
== STREAM_UNSUPPORTED
)
1657 int demuxer_set_angle(demuxer_t
*demuxer
, int angle
)
1659 int ris
, angles
= -1;
1661 angles
= demuxer_angles_count(demuxer
);
1662 if ((angles
< 1) || (angle
> angles
))
1665 demux_flush(demuxer
);
1667 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SET_ANGLE
, &angle
);
1668 if (ris
== STREAM_UNSUPPORTED
)
1671 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1676 int demuxer_audio_track_by_lang(demuxer_t
*d
, char *lang
)
1679 lang
+= strspn(lang
, ",");
1680 while ((len
= strcspn(lang
, ",")) > 0) {
1681 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1682 sh_audio_t
*sh
= d
->a_streams
[i
];
1683 if (sh
&& sh
->lang
&& strncmp(sh
->lang
, lang
, len
) == 0)
1687 lang
+= strspn(lang
, ",");
1692 int demuxer_sub_track_by_lang(demuxer_t
*d
, char *lang
)
1695 lang
+= strspn(lang
, ",");
1696 while ((len
= strcspn(lang
, ",")) > 0) {
1697 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1698 sh_sub_t
*sh
= d
->s_streams
[i
];
1699 if (sh
&& sh
->lang
&& strncmp(sh
->lang
, lang
, len
) == 0)
1703 lang
+= strspn(lang
, ",");
1708 int demuxer_default_audio_track(demuxer_t
*d
)
1711 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1712 sh_audio_t
*sh
= d
->a_streams
[i
];
1713 if (sh
&& sh
->default_track
)
1716 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1717 sh_audio_t
*sh
= d
->a_streams
[i
];
1724 int demuxer_default_sub_track(demuxer_t
*d
)
1727 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1728 sh_sub_t
*sh
= d
->s_streams
[i
];
1729 if (sh
&& sh
->default_track
)