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>
36 #include "libvo/fastmemcpy.h"
38 #include "stream/stream.h"
43 #include "libaf/af_format.h"
44 #include "libmpcodecs/dec_teletext.h"
48 #ifdef CONFIG_LIBAVCODEC
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
);
63 void resync_video_stream(sh_video_t
*sh_video
);
64 void resync_audio_stream(sh_audio_t
*sh_audio
);
67 extern const demuxer_desc_t demuxer_desc_rawaudio
;
68 extern const demuxer_desc_t demuxer_desc_rawvideo
;
69 extern const demuxer_desc_t demuxer_desc_tv
;
70 extern const demuxer_desc_t demuxer_desc_mf
;
71 extern const demuxer_desc_t demuxer_desc_avi
;
72 extern const demuxer_desc_t demuxer_desc_y4m
;
73 extern const demuxer_desc_t demuxer_desc_asf
;
74 extern const demuxer_desc_t demuxer_desc_real
;
75 extern const demuxer_desc_t demuxer_desc_smjpeg
;
76 extern const demuxer_desc_t demuxer_desc_matroska
;
77 extern const demuxer_desc_t demuxer_desc_realaudio
;
78 extern const demuxer_desc_t demuxer_desc_vqf
;
79 extern const demuxer_desc_t demuxer_desc_mov
;
80 extern const demuxer_desc_t demuxer_desc_vivo
;
81 extern const demuxer_desc_t demuxer_desc_fli
;
82 extern const demuxer_desc_t demuxer_desc_film
;
83 extern const demuxer_desc_t demuxer_desc_roq
;
84 extern const demuxer_desc_t demuxer_desc_gif
;
85 extern const demuxer_desc_t demuxer_desc_ogg
;
86 extern const demuxer_desc_t demuxer_desc_avs
;
87 extern const demuxer_desc_t demuxer_desc_pva
;
88 extern const demuxer_desc_t demuxer_desc_nsv
;
89 extern const demuxer_desc_t demuxer_desc_mpeg_ts
;
90 extern const demuxer_desc_t demuxer_desc_lmlm4
;
91 extern const demuxer_desc_t demuxer_desc_mpeg_ps
;
92 extern const demuxer_desc_t demuxer_desc_mpeg_pes
;
93 extern const demuxer_desc_t demuxer_desc_mpeg_es
;
94 extern const demuxer_desc_t demuxer_desc_mpeg_gxf
;
95 extern const demuxer_desc_t demuxer_desc_mpeg4_es
;
96 extern const demuxer_desc_t demuxer_desc_h264_es
;
97 extern const demuxer_desc_t demuxer_desc_rawdv
;
98 extern const demuxer_desc_t demuxer_desc_mpc
;
99 extern const demuxer_desc_t demuxer_desc_audio
;
100 extern const demuxer_desc_t demuxer_desc_xmms
;
101 extern const demuxer_desc_t demuxer_desc_mpeg_ty
;
102 extern const demuxer_desc_t demuxer_desc_rtp
;
103 extern const demuxer_desc_t demuxer_desc_rtp_nemesi
;
104 extern const demuxer_desc_t demuxer_desc_lavf
;
105 extern const demuxer_desc_t demuxer_desc_lavf_preferred
;
106 extern const demuxer_desc_t demuxer_desc_aac
;
107 extern const demuxer_desc_t demuxer_desc_nut
;
108 extern const demuxer_desc_t demuxer_desc_mng
;
110 /* Please do not add any new demuxers here. If you want to implement a new
111 * demuxer, add it to libavformat, except for wrappers around external
112 * libraries and demuxers requiring binary support. */
114 const demuxer_desc_t
*const demuxer_list
[] = {
115 &demuxer_desc_rawaudio
,
116 &demuxer_desc_rawvideo
,
121 #ifdef CONFIG_LIBAVFORMAT
122 &demuxer_desc_lavf_preferred
,
129 &demuxer_desc_smjpeg
,
130 &demuxer_desc_matroska
,
131 &demuxer_desc_realaudio
,
141 #ifdef CONFIG_OGGVORBIS
144 #ifdef CONFIG_WIN32DLL
148 &demuxer_desc_mpeg_ts
,
150 &demuxer_desc_mpeg_ps
,
151 &demuxer_desc_mpeg_pes
,
152 &demuxer_desc_mpeg_es
,
153 &demuxer_desc_mpeg_gxf
,
154 &demuxer_desc_mpeg4_es
,
155 &demuxer_desc_h264_es
,
157 &demuxer_desc_mpeg_ty
,
158 #ifdef CONFIG_LIVE555
161 #ifdef CONFIG_LIBNEMESI
162 &demuxer_desc_rtp_nemesi
,
164 #ifdef CONFIG_LIBAVFORMAT
167 #ifdef CONFIG_MUSEPACK
170 #ifdef CONFIG_LIBDV095
183 /* Please do not add any new demuxers here. If you want to implement a new
184 * demuxer, add it to libavformat, except for wrappers around external
185 * libraries and demuxers requiring binary support. */
189 void free_demuxer_stream(demux_stream_t
*ds
)
195 demux_stream_t
*new_demuxer_stream(struct demuxer
*demuxer
, int id
)
197 demux_stream_t
*ds
= malloc(sizeof(demux_stream_t
));
198 *ds
= (demux_stream_t
){
208 * Get demuxer description structure for a given demuxer type
210 * @param file_format type of the demuxer
211 * @return structure for the demuxer, NULL if not found
213 static const demuxer_desc_t
*get_demuxer_desc_from_type(int file_format
)
217 for (i
= 0; demuxer_list
[i
]; i
++)
218 if (file_format
== demuxer_list
[i
]->type
)
219 return demuxer_list
[i
];
225 demuxer_t
*new_demuxer(struct MPOpts
*opts
, stream_t
*stream
, int type
,
226 int a_id
, int v_id
, int s_id
, char *filename
)
228 struct demuxer
*d
= talloc_zero(NULL
, struct demuxer
);
230 d
->stream_pts
= MP_NOPTS_VALUE
;
231 d
->reference_clock
= MP_NOPTS_VALUE
;
232 d
->movi_start
= stream
->start_pos
;
233 d
->movi_end
= stream
->end_pos
;
237 d
->audio
= new_demuxer_stream(d
, a_id
);
238 d
->video
= new_demuxer_stream(d
, v_id
);
239 d
->sub
= new_demuxer_stream(d
, s_id
);
243 if (!(d
->desc
= get_demuxer_desc_from_type(type
)))
244 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
245 "BUG! Invalid demuxer type in new_demuxer(), "
246 "big troubles ahead.");
247 if (filename
) // Filename hack for avs_check_file
248 d
->filename
= strdup(filename
);
250 stream_seek(stream
, stream
->start_pos
);
254 sh_sub_t
*new_sh_sub_sid(demuxer_t
*demuxer
, int id
, int sid
)
256 if (id
> MAX_S_STREAMS
- 1 || id
< 0) {
257 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
258 "Requested sub stream id overflow (%d > %d)\n", id
,
262 if (demuxer
->s_streams
[id
])
263 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, "Sub stream %i redefined\n", id
);
265 sh_sub_t
*sh
= calloc(1, sizeof(sh_sub_t
));
266 demuxer
->s_streams
[id
] = sh
;
268 sh
->opts
= demuxer
->opts
;
269 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SUBTITLE_ID=%d\n", sid
);
271 if (sid
== demuxer
->opts
->sub_id
) {
272 demuxer
->sub
->id
= id
;
273 demuxer
->sub
->sh
= demuxer
->s_streams
[id
];
275 return demuxer
->s_streams
[id
];
278 static void free_sh_sub(sh_sub_t
*sh
)
280 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_sub at %p\n", sh
);
284 ass_free_track(sh
->ass_track
);
287 #ifdef CONFIG_LIBAVCODEC
288 clear_parser((sh_common_t
*)sh
);
293 sh_audio_t
*new_sh_audio_aid(demuxer_t
*demuxer
, int id
, int aid
)
295 if (id
> MAX_A_STREAMS
- 1 || id
< 0) {
296 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
297 "Requested audio stream id overflow (%d > %d)\n", id
,
301 if (demuxer
->a_streams
[id
]) {
302 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, "WARNING: Audio stream header %d redefined.\n", id
);
304 mp_tmsg(MSGT_DEMUXER
, MSGL_V
, "==> Found audio stream: %d\n", id
);
305 sh_audio_t
*sh
= calloc(1, sizeof(sh_audio_t
));
306 demuxer
->a_streams
[id
] = sh
;
308 sh
->ds
= demuxer
->audio
;
311 sh
->sample_format
= AF_FORMAT_S16_NE
;
312 sh
->audio_out_minsize
= 8192; /* default size, maybe not enough for Win32/ACM */
313 sh
->pts
= MP_NOPTS_VALUE
;
314 sh
->opts
= demuxer
->opts
;
315 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_ID=%d\n", aid
);
317 return demuxer
->a_streams
[id
];
320 void free_sh_audio(demuxer_t
*demuxer
, int id
)
322 sh_audio_t
*sh
= demuxer
->a_streams
[id
];
323 demuxer
->a_streams
[id
] = NULL
;
324 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_audio at %p\n", sh
);
328 #ifdef CONFIG_LIBAVCODEC
329 clear_parser((sh_common_t
*)sh
);
334 sh_video_t
*new_sh_video_vid(demuxer_t
*demuxer
, int id
, int vid
)
336 if (id
> MAX_V_STREAMS
- 1 || id
< 0) {
337 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
338 "Requested video stream id overflow (%d > %d)\n", id
,
342 if (demuxer
->v_streams
[id
])
343 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, "WARNING: Video stream header %d redefined.\n", id
);
345 mp_tmsg(MSGT_DEMUXER
, MSGL_V
, "==> Found video stream: %d\n", id
);
346 sh_video_t
*sh
= calloc(1, sizeof *sh
);
347 demuxer
->v_streams
[id
] = sh
;
349 sh
->ds
= demuxer
->video
;
350 sh
->opts
= demuxer
->opts
;
351 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_ID=%d\n", vid
);
353 return demuxer
->v_streams
[id
];
356 void free_sh_video(sh_video_t
*sh
)
358 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_video at %p\n", sh
);
360 #ifdef CONFIG_LIBAVCODEC
361 clear_parser((sh_common_t
*)sh
);
366 void free_demuxer(demuxer_t
*demuxer
)
369 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing %s demuxer at %p\n",
370 demuxer
->desc
->shortdesc
, demuxer
);
371 if (demuxer
->desc
->close
)
372 demuxer
->desc
->close(demuxer
);
373 // Very ugly hack to make it behave like old implementation
374 if (demuxer
->desc
->type
== DEMUXER_TYPE_DEMUXERS
)
375 goto skip_streamfree
;
377 for (i
= 0; i
< MAX_A_STREAMS
; i
++)
378 if (demuxer
->a_streams
[i
])
379 free_sh_audio(demuxer
, i
);
380 for (i
= 0; i
< MAX_V_STREAMS
; i
++)
381 if (demuxer
->v_streams
[i
])
382 free_sh_video(demuxer
->v_streams
[i
]);
383 for (i
= 0; i
< MAX_S_STREAMS
; i
++)
384 if (demuxer
->s_streams
[i
])
385 free_sh_sub(demuxer
->s_streams
[i
]);
387 free_demuxer_stream(demuxer
->audio
);
388 free_demuxer_stream(demuxer
->video
);
389 free_demuxer_stream(demuxer
->sub
);
392 for (i
= 0; demuxer
->info
[i
] != NULL
; i
++)
393 free(demuxer
->info
[i
]);
396 free(demuxer
->filename
);
397 if (demuxer
->teletext
)
398 teletext_control(demuxer
->teletext
, TV_VBI_CONTROL_STOP
, NULL
);
399 talloc_free(demuxer
);
403 static void ds_add_packet_internal(demux_stream_t
*ds
, demux_packet_t
*dp
)
405 // append packet to DS stream:
407 ds
->bytes
+= dp
->len
;
409 // next packet in stream
413 // first packet in stream
414 ds
->first
= ds
->last
= dp
;
416 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
417 "DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
418 (ds
== ds
->demuxer
->audio
) ? "d_audio" : "d_video", dp
->len
,
419 dp
->pts
, (unsigned int) dp
->pos
, ds
->demuxer
->audio
->packs
,
420 ds
->demuxer
->video
->packs
);
423 #ifdef CONFIG_LIBAVCODEC
424 static void allocate_parser(AVCodecContext
**avctx
, AVCodecParserContext
**parser
, unsigned format
)
426 enum CodecID codec_id
= CODEC_ID_NONE
;
427 extern int avcodec_initialized
;
428 if (!avcodec_initialized
) {
430 avcodec_register_all();
431 avcodec_initialized
= 1;
437 case MKTAG('d', 'n', 'e', 't'):
438 case MKTAG('s', 'a', 'c', '3'):
439 codec_id
= CODEC_ID_AC3
;
441 case MKTAG('E', 'A', 'C', '3'):
442 codec_id
= CODEC_ID_EAC3
;
446 codec_id
= CODEC_ID_DTS
;
448 case MKTAG('M', 'L', 'P', ' '):
449 codec_id
= CODEC_ID_MLP
;
453 case MKTAG('.', 'm', 'p', '3'):
454 case MKTAG('M', 'P', 'E', ' '):
455 case MKTAG('L', 'A', 'M', 'E'):
456 codec_id
= CODEC_ID_MP3
;
459 case MKTAG('.', 'm', 'p', '2'):
460 case MKTAG('.', 'm', 'p', '1'):
461 codec_id
= CODEC_ID_MP2
;
463 case MKTAG('T', 'R', 'H', 'D'):
464 codec_id
= CODEC_ID_TRUEHD
;
467 if (codec_id
!= CODEC_ID_NONE
) {
468 *avctx
= avcodec_alloc_context();
471 *parser
= av_parser_init(codec_id
);
477 static void get_parser(sh_common_t
*sh
, AVCodecContext
**avctx
, AVCodecParserContext
**parser
)
482 if (!sh
|| !sh
->needs_parsing
)
486 *parser
= sh
->parser
;
490 allocate_parser(avctx
, parser
, sh
->format
);
492 sh
->parser
= *parser
;
495 int ds_parse(demux_stream_t
*ds
, uint8_t **buffer
, int *len
, double pts
, off_t pos
)
497 AVCodecContext
*avctx
;
498 AVCodecParserContext
*parser
;
499 get_parser(ds
->sh
, &avctx
, &parser
);
502 return av_parser_parse2(parser
, avctx
, buffer
, len
, *buffer
, *len
, pts
, pts
, pos
);
505 static void clear_parser(sh_common_t
*sh
)
507 av_parser_close(sh
->parser
);
509 av_freep(&sh
->avctx
);
512 void ds_clear_parser(demux_stream_t
*ds
)
516 clear_parser(ds
->sh
);
520 void ds_add_packet(demux_stream_t
*ds
, demux_packet_t
*dp
)
522 #if PARSE_ON_ADD && defined(CONFIG_LIBAVCODEC)
526 uint8_t *parsed_start
= dp
->buffer
+ pos
;
527 int parsed_len
= len
;
528 int consumed
= ds_parse(ds
->sh
, &parsed_start
, &parsed_len
, dp
->pts
, dp
->pos
);
531 if (parsed_start
== dp
->buffer
&& parsed_len
== dp
->len
) {
532 ds_add_packet_internal(ds
, dp
);
533 } else if (parsed_len
) {
534 demux_packet_t
*dp2
= new_demux_packet(parsed_len
);
536 dp2
->pts
= dp
->pts
; // should be parser->pts but that works badly
537 memcpy(dp2
->buffer
, parsed_start
, parsed_len
);
538 ds_add_packet_internal(ds
, dp2
);
542 ds_add_packet_internal(ds
, dp
);
546 void ds_read_packet(demux_stream_t
*ds
, stream_t
*stream
, int len
,
547 double pts
, off_t pos
, int flags
)
549 demux_packet_t
*dp
= new_demux_packet(len
);
550 len
= stream_read(stream
, dp
->buffer
, len
);
551 resize_demux_packet(dp
, len
);
555 // append packet to DS stream:
556 ds_add_packet(ds
, dp
);
560 // 0 = EOF or no stream found or invalid type
561 // 1 = successfully read a packet
563 int demux_fill_buffer(demuxer_t
*demux
, demux_stream_t
*ds
)
565 // Note: parameter 'ds' can be NULL!
566 return demux
->desc
->fill_buffer(demux
, ds
);
572 #define MAX_ACUMULATED_PACKETS 64
573 int ds_fill_buffer(demux_stream_t
*ds
)
575 demuxer_t
*demux
= ds
->demuxer
;
577 free_demux_packet(ds
->current
);
579 if (mp_msg_test(MSGT_DEMUXER
, MSGL_DBG3
)) {
580 if (ds
== demux
->audio
)
581 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
582 "ds_fill_buffer(d_audio) called\n");
583 else if (ds
== demux
->video
)
584 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
585 "ds_fill_buffer(d_video) called\n");
586 else if (ds
== demux
->sub
)
587 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
, "ds_fill_buffer(d_sub) called\n");
589 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
590 "ds_fill_buffer(unknown 0x%X) called\n", (unsigned int) ds
);
594 demux_packet_t
*p
= ds
->first
;
596 if (demux
->reference_clock
!= MP_NOPTS_VALUE
) {
597 if ( p
->pts
!= MP_NOPTS_VALUE
598 && p
->pts
> demux
->reference_clock
599 && ds
->packs
< MAX_ACUMULATED_PACKETS
) {
600 if (demux_fill_buffer(demux
, ds
))
606 ds
->buffer
= p
->buffer
;
608 ds
->buffer_size
= p
->len
;
610 ds
->dpos
+= p
->len
; // !!!
612 if (p
->pts
!= MP_NOPTS_VALUE
) {
616 ds
->pts_bytes
+= p
->len
; // !!!
617 if (p
->stream_pts
!= MP_NOPTS_VALUE
)
618 demux
->stream_pts
= p
->stream_pts
;
619 ds
->flags
= p
->flags
;
627 /* The code below can set ds->eof to 1 when another stream runs
628 * out of buffer space. That makes sense because in that situation
629 * the calling code should not count on being able to demux more
630 * packets from this stream.
631 * If however the situation improves and we're called again
632 * despite the eof flag then it's better to clear it to avoid
638 #define MaybeNI _("Maybe you are playing a non-interleaved stream/file or the codec failed?\n" \
639 "For AVI files, try to force non-interleaved mode with the -ni option.\n")
641 if (demux
->audio
->packs
>= MAX_PACKS
642 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
643 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
644 demux
->audio
->packs
, demux
->audio
->bytes
);
645 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
648 if (demux
->video
->packs
>= MAX_PACKS
649 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
650 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
651 demux
->video
->packs
, demux
->video
->bytes
);
652 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
655 if (!demux_fill_buffer(demux
, ds
)) {
656 #if PARSE_ON_ADD && defined(CONFIG_LIBAVCODEC)
657 uint8_t *parsed_start
= NULL
;
659 ds_parse(ds
->sh
, &parsed_start
, &parsed_len
, MP_NOPTS_VALUE
, 0);
661 demux_packet_t
*dp2
= new_demux_packet(parsed_len
);
662 dp2
->pts
= MP_NOPTS_VALUE
;
663 memcpy(dp2
->buffer
, parsed_start
, parsed_len
);
664 ds_add_packet_internal(ds
, dp2
);
668 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
669 "ds_fill_buffer()->demux_fill_buffer() failed\n");
673 ds
->buffer_pos
= ds
->buffer_size
= 0;
675 mp_msg(MSGT_DEMUXER
, MSGL_V
,
676 "ds_fill_buffer: EOF reached (stream: %s) \n",
677 ds
== demux
->audio
? "audio" : "video");
682 int demux_read_data(demux_stream_t
*ds
, unsigned char *mem
, int len
)
687 x
= ds
->buffer_size
- ds
->buffer_pos
;
689 if (!ds_fill_buffer(ds
))
695 fast_memcpy(mem
+ bytes
, &ds
->buffer
[ds
->buffer_pos
], x
);
705 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
706 * \param mem memory to read data into, may be NULL to discard data
707 * \param maxlen maximum number of bytes to read
708 * \param read number of bytes actually read
709 * \param pattern pattern to search for (lowest 8 bits are ignored)
710 * \return whether pattern was found
712 int demux_pattern_3(demux_stream_t
*ds
, unsigned char *mem
, int maxlen
,
713 int *read
, uint32_t pattern
)
715 register uint32_t head
= 0xffffff00;
716 register uint32_t pat
= pattern
& 0xffffff00;
719 register unsigned char *ds_buf
= &ds
->buffer
[ds
->buffer_size
];
720 int len
= ds
->buffer_size
- ds
->buffer_pos
;
721 register long pos
= -len
;
722 if (unlikely(pos
>= 0)) { // buffer is empty
729 } while (++pos
&& head
!= pat
);
731 if (total_len
+ len
> maxlen
)
732 len
= maxlen
- total_len
;
733 len
= demux_read_data(ds
, mem
? &mem
[total_len
] : NULL
, len
);
735 } while ((head
!= pat
|| total_len
< 3) && total_len
< maxlen
&& !ds
->eof
);
738 return total_len
>= 3 && head
== pat
;
741 void ds_free_packs(demux_stream_t
*ds
)
743 demux_packet_t
*dp
= ds
->first
;
745 demux_packet_t
*dn
= dp
->next
;
746 free_demux_packet(dp
);
749 if (ds
->asf_packet
) {
750 // free unfinished .asf fragments:
751 free(ds
->asf_packet
->buffer
);
752 free(ds
->asf_packet
);
753 ds
->asf_packet
= NULL
;
755 ds
->first
= ds
->last
= NULL
;
756 ds
->packs
= 0; // !!!!!
759 free_demux_packet(ds
->current
);
762 ds
->buffer_pos
= ds
->buffer_size
;
767 int ds_get_packet(demux_stream_t
*ds
, unsigned char **start
)
770 if (ds
->buffer_pos
>= ds
->buffer_size
) {
771 if (!ds_fill_buffer(ds
)) {
777 len
= ds
->buffer_size
- ds
->buffer_pos
;
778 *start
= &ds
->buffer
[ds
->buffer_pos
];
779 ds
->buffer_pos
+= len
;
783 int ds_get_packet_pts(demux_stream_t
*ds
, unsigned char **start
, double *pts
)
786 *pts
= MP_NOPTS_VALUE
;
787 len
= ds_get_packet(ds
, start
);
790 // Return pts unless this read starts from the middle of a packet
791 if (len
== ds
->buffer_pos
)
792 *pts
= ds
->current
->pts
;
796 int ds_get_packet_sub(demux_stream_t
*ds
, unsigned char **start
)
799 if (ds
->buffer_pos
>= ds
->buffer_size
) {
803 if (!ds_fill_buffer(ds
))
806 len
= ds
->buffer_size
- ds
->buffer_pos
;
807 *start
= &ds
->buffer
[ds
->buffer_pos
];
808 ds
->buffer_pos
+= len
;
812 double ds_get_next_pts(demux_stream_t
*ds
)
814 demuxer_t
*demux
= ds
->demuxer
;
816 if (demux
->audio
->packs
>= MAX_PACKS
817 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
818 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
819 demux
->audio
->packs
, demux
->audio
->bytes
);
820 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
821 return MP_NOPTS_VALUE
;
823 if (demux
->video
->packs
>= MAX_PACKS
824 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
825 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
826 demux
->video
->packs
, demux
->video
->bytes
);
827 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
828 return MP_NOPTS_VALUE
;
830 if (!demux_fill_buffer(demux
, ds
))
831 return MP_NOPTS_VALUE
;
833 return ds
->first
->pts
;
836 // ====================================================================
838 void demuxer_help(void)
842 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "Available demuxers:\n");
843 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, " demuxer: type info: (comment)\n");
844 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DEMUXERS\n");
845 for (i
= 0; demuxer_list
[i
]; i
++) {
846 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Don't display special demuxers
848 if (demuxer_list
[i
]->comment
&& strlen(demuxer_list
[i
]->comment
))
849 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s (%s)\n",
850 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
851 demuxer_list
[i
]->info
, demuxer_list
[i
]->comment
);
853 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s\n",
854 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
855 demuxer_list
[i
]->info
);
861 * Get demuxer type for a given demuxer name
863 * @param demuxer_name string with demuxer name of demuxer number
864 * @param force will be set if demuxer should be forced.
866 * @return DEMUXER_TYPE_xxx, -1 if error or not found
868 int get_demuxer_type_from_name(char *demuxer_name
, int *force
)
874 if (!demuxer_name
|| !demuxer_name
[0])
875 return DEMUXER_TYPE_UNKNOWN
;
877 *force
= demuxer_name
[0] == '+';
878 if (demuxer_name
[0] == '+')
879 demuxer_name
= &demuxer_name
[1];
880 for (i
= 0; demuxer_list
[i
]; i
++) {
881 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Can't select special demuxers from commandline
883 if (strcmp(demuxer_name
, demuxer_list
[i
]->name
) == 0)
884 return demuxer_list
[i
]->type
;
887 // No match found, try to parse name as an integer (demuxer number)
888 type_int
= strtol(demuxer_name
, &endptr
, 0);
889 if (*endptr
) // Conversion failed
891 if ((type_int
> 0) && (type_int
<= DEMUXER_TYPE_MAX
))
892 return (int) type_int
;
897 int extension_parsing
= 1; // 0=off 1=mixed (used only for unstable formats)
900 NOTE : Several demuxers may be opened at the same time so
901 demuxers should NEVER rely on an external var to enable them
902 self. If a demuxer can't do any auto-detection it should only use
903 file_format. The user can explicitly set file_format with the -demuxer
904 option so there is really no need for another extra var.
905 For convenience an option can be added to set file_format directly
906 to the right type (ex: rawaudio,rawvideo).
907 Also the stream can override the file_format so a demuxer which rely
908 on a special stream type can set file_format at the stream level
912 static demuxer_t
*demux_open_stream(struct MPOpts
*opts
, stream_t
*stream
,
913 int file_format
, int force
, int audio_id
,
914 int video_id
, int dvdsub_id
,
917 demuxer_t
*demuxer
= NULL
;
919 sh_video_t
*sh_video
= NULL
;
921 const demuxer_desc_t
*demuxer_desc
;
925 // If somebody requested a demuxer check it
927 if ((demuxer_desc
= get_demuxer_desc_from_type(file_format
))) {
928 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
929 video_id
, dvdsub_id
, filename
);
930 if (demuxer_desc
->check_file
)
931 fformat
= demuxer_desc
->check_file(demuxer
);
932 if (force
|| !demuxer_desc
->check_file
)
933 fformat
= demuxer_desc
->type
;
935 if (fformat
== demuxer_desc
->type
) {
936 demuxer_t
*demux2
= demuxer
;
937 // Move messages to demuxer detection code?
938 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
939 "%s file format detected.\n",
940 demuxer_desc
->shortdesc
);
941 file_format
= fformat
;
942 if (!demuxer
->desc
->open
943 || (demux2
= demuxer
->desc
->open(demuxer
))) {
948 // Format changed after check, recurse
949 free_demuxer(demuxer
);
950 return demux_open_stream(opts
, stream
, fformat
, force
,
951 audio_id
, video_id
, dvdsub_id
,
955 // Check failed for forced demuxer, quit
956 free_demuxer(demuxer
);
960 // Test demuxers with safe file checks
961 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
962 if (demuxer_desc
->safe_check
) {
963 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
964 video_id
, dvdsub_id
, filename
);
965 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
966 if (fformat
== demuxer_desc
->type
) {
967 demuxer_t
*demux2
= demuxer
;
968 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
969 "%s file format detected.\n",
970 demuxer_desc
->shortdesc
);
971 file_format
= fformat
;
972 if (!demuxer
->desc
->open
973 || (demux2
= demuxer
->desc
->open(demuxer
))) {
978 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
979 return demuxer
; // handled in mplayer.c
980 // Format changed after check, recurse
981 free_demuxer(demuxer
);
982 demuxer
= demux_open_stream(opts
, stream
, fformat
, force
,
984 dvdsub_id
, filename
);
986 return demuxer
; // done!
987 file_format
= DEMUXER_TYPE_UNKNOWN
;
990 free_demuxer(demuxer
);
995 // If no forced demuxer perform file extension based detection
996 // Ok. We're over the stable detectable fileformats, the next ones are
997 // a bit fuzzy. So by default (extension_parsing==1) try extension-based
999 if (file_format
== DEMUXER_TYPE_UNKNOWN
&& filename
1000 && extension_parsing
== 1) {
1001 file_format
= demuxer_type_by_filename(filename
);
1002 if (file_format
!= DEMUXER_TYPE_UNKNOWN
) {
1003 // we like recursion :)
1004 demuxer
= demux_open_stream(opts
, stream
, file_format
, force
,
1005 audio_id
, video_id
, dvdsub_id
,
1008 return demuxer
; // done!
1009 file_format
= DEMUXER_TYPE_UNKNOWN
; // continue fuzzy guessing...
1010 mp_msg(MSGT_DEMUXER
, MSGL_V
,
1011 "demuxer: continue fuzzy content-based format guessing...\n");
1014 // Try detection for all other demuxers
1015 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
1016 if (!demuxer_desc
->safe_check
&& demuxer_desc
->check_file
) {
1017 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
1018 video_id
, dvdsub_id
, filename
);
1019 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
1020 if (fformat
== demuxer_desc
->type
) {
1021 demuxer_t
*demux2
= demuxer
;
1022 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
1023 "%s file format detected.\n",
1024 demuxer_desc
->shortdesc
);
1025 file_format
= fformat
;
1026 if (!demuxer
->desc
->open
1027 || (demux2
= demuxer
->desc
->open(demuxer
))) {
1032 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
1033 return demuxer
; // handled in mplayer.c
1034 // Format changed after check, recurse
1035 free_demuxer(demuxer
);
1036 demuxer
= demux_open_stream(opts
, stream
, fformat
, force
,
1038 dvdsub_id
, filename
);
1040 return demuxer
; // done!
1041 file_format
= DEMUXER_TYPE_UNKNOWN
;
1044 free_demuxer(demuxer
);
1050 //====== File format recognized, set up these for compatibility: =========
1053 demuxer
->file_format
= file_format
;
1055 if ((sh_video
= demuxer
->video
->sh
) && sh_video
->bih
) {
1056 int biComp
= le2me_32(sh_video
->bih
->biCompression
);
1057 mp_msg(MSGT_DEMUX
, MSGL_INFO
,
1058 "VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
1059 (char *) &biComp
, sh_video
->bih
->biWidth
,
1060 sh_video
->bih
->biHeight
, sh_video
->bih
->biBitCount
,
1061 sh_video
->fps
, sh_video
->i_bps
* 0.008f
,
1062 sh_video
->i_bps
/ 1024.0f
);
1065 if (opts
->ass_enabled
&& ass_library
) {
1066 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1067 sh_sub_t
*sh
= demuxer
->s_streams
[i
];
1068 if (sh
&& sh
->type
== 'a') {
1069 sh
->ass_track
= ass_new_track(ass_library
);
1070 if (sh
->ass_track
&& sh
->extradata
)
1071 ass_process_codec_private(sh
->ass_track
, sh
->extradata
,
1073 } else if (sh
&& sh
->type
!= 'v')
1074 sh
->ass_track
= ass_default_track(ass_library
);
1081 char *audio_stream
= NULL
;
1082 char *sub_stream
= NULL
;
1083 int audio_stream_cache
= 0;
1085 char *demuxer_name
= NULL
; // parameter from -demuxer
1086 char *audio_demuxer_name
= NULL
; // parameter from -audio-demuxer
1087 char *sub_demuxer_name
= NULL
; // parameter from -sub-demuxer
1089 extern int hr_mp3_seek
;
1091 extern float stream_cache_min_percent
;
1092 extern float stream_cache_seek_min_percent
;
1094 demuxer_t
*demux_open(struct MPOpts
*opts
, stream_t
*vs
, int file_format
,
1095 int audio_id
, int video_id
, int dvdsub_id
,
1098 stream_t
*as
= NULL
, *ss
= NULL
;
1099 demuxer_t
*vd
, *ad
= NULL
, *sd
= NULL
;
1101 int afmt
= DEMUXER_TYPE_UNKNOWN
, sfmt
= DEMUXER_TYPE_UNKNOWN
;
1103 int audio_demuxer_type
= 0, sub_demuxer_type
= 0;
1104 int demuxer_force
= 0, audio_demuxer_force
= 0, sub_demuxer_force
= 0;
1107 get_demuxer_type_from_name(demuxer_name
, &demuxer_force
)) < 0) {
1108 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-demuxer %s does not exist.\n",
1111 if ((audio_demuxer_type
=
1112 get_demuxer_type_from_name(audio_demuxer_name
,
1113 &audio_demuxer_force
)) < 0) {
1114 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-audio-demuxer %s does not exist.\n",
1115 audio_demuxer_name
);
1117 if ((sub_demuxer_type
=
1118 get_demuxer_type_from_name(sub_demuxer_name
,
1119 &sub_demuxer_force
)) < 0) {
1120 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-sub-demuxer %s does not exist.\n",
1125 as
= open_stream(audio_stream
, 0, &afmt
);
1127 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "Cannot open audio stream: %s\n",
1131 if (audio_stream_cache
) {
1132 if (!stream_enable_cache
1133 (as
, audio_stream_cache
* 1024,
1134 audio_stream_cache
* 1024 * (stream_cache_min_percent
/
1136 audio_stream_cache
* 1024 * (stream_cache_seek_min_percent
/
1139 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
1140 "Can't enable audio stream cache\n");
1146 ss
= open_stream(sub_stream
, 0, &sfmt
);
1148 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "Cannot open subtitle stream: %s\n",
1154 vd
= demux_open_stream(opts
, vs
, demuxer_type
? demuxer_type
: file_format
,
1155 demuxer_force
, audio_stream
? -2 : audio_id
,
1156 video_id
, sub_stream
? -2 : dvdsub_id
, filename
);
1165 ad
= demux_open_stream(opts
, as
,
1166 audio_demuxer_type
? audio_demuxer_type
: afmt
,
1167 audio_demuxer_force
, audio_id
, -2, -2,
1170 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, "Failed to open audio demuxer: %s\n",
1173 } else if (ad
->audio
->sh
1174 && ((sh_audio_t
*) ad
->audio
->sh
)->format
== 0x55) // MP3
1175 hr_mp3_seek
= 1; // Enable high res seeking
1178 sd
= demux_open_stream(opts
, ss
,
1179 sub_demuxer_type
? sub_demuxer_type
: sfmt
,
1180 sub_demuxer_force
, -2, -2, dvdsub_id
,
1183 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
,
1184 "Failed to open subtitle demuxer: %s\n", sub_stream
);
1190 res
= new_demuxers_demuxer(vd
, ad
, sd
);
1192 res
= new_demuxers_demuxer(vd
, ad
, vd
);
1194 res
= new_demuxers_demuxer(vd
, vd
, sd
);
1198 opts
->correct_pts
= opts
->user_correct_pts
;
1199 if (opts
->correct_pts
< 0)
1201 demux_control(res
, DEMUXER_CTRL_CORRECT_PTS
,
1202 NULL
) == DEMUXER_CTRL_OK
;
1207 void demux_flush(demuxer_t
*demuxer
)
1210 ds_clear_parser(demuxer
->video
);
1211 ds_clear_parser(demuxer
->audio
);
1212 ds_clear_parser(demuxer
->sub
);
1214 ds_free_packs(demuxer
->video
);
1215 ds_free_packs(demuxer
->audio
);
1216 ds_free_packs(demuxer
->sub
);
1219 int demux_seek(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
,
1222 demux_stream_t
*d_audio
= demuxer
->audio
;
1223 demux_stream_t
*d_video
= demuxer
->video
;
1224 sh_audio_t
*sh_audio
= d_audio
->sh
;
1225 sh_video_t
*sh_video
= d_video
->sh
;
1229 if (!demuxer
->seekable
) {
1230 if (demuxer
->file_format
== DEMUXER_TYPE_AVI
)
1231 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "Cannot seek in raw AVI streams. (Index required, try with the -idx switch.)\n");
1233 else if (demuxer
->file_format
== DEMUXER_TYPE_TV
)
1234 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "TV input is not seekable! (Seeking will probably be for changing channels ;)\n");
1237 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "Cannot seek in this file.\n");
1240 // clear demux buffers:
1241 demux_flush(demuxer
);
1243 sh_audio
->a_buffer_len
= 0;
1245 demuxer
->stream
->eof
= 0;
1246 demuxer
->video
->eof
= 0;
1247 demuxer
->audio
->eof
= 0;
1250 sh_video
->timer
= 0; // !!!!!!
1252 if (flags
& SEEK_ABSOLUTE
)
1255 if (demuxer
->stream_pts
== MP_NOPTS_VALUE
)
1257 pts
= demuxer
->stream_pts
;
1260 if (flags
& SEEK_FACTOR
) {
1261 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_TIME_LENGTH
, &tmp
)
1262 == STREAM_UNSUPPORTED
)
1264 pts
+= tmp
* rel_seek_secs
;
1266 pts
+= rel_seek_secs
;
1268 if (stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_TIME
, &pts
) !=
1269 STREAM_UNSUPPORTED
) {
1270 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1275 if (demuxer
->desc
->seek
)
1276 demuxer
->desc
->seek(demuxer
, rel_seek_secs
, audio_delay
, flags
);
1279 resync_audio_stream(sh_audio
);
1284 int demux_info_add(demuxer_t
*demuxer
, const char *opt
, const char *param
)
1286 char **info
= demuxer
->info
;
1290 for (n
= 0; info
&& info
[2 * n
] != NULL
; n
++) {
1291 if (!strcasecmp(opt
, info
[2 * n
])) {
1292 if (!strcmp(param
, info
[2 * n
+ 1])) {
1293 mp_msg(MSGT_DEMUX
, MSGL_V
, "Demuxer info %s set to unchanged value %s\n", opt
, param
);
1296 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "Demuxer info %s changed to %s\n", opt
,
1298 free(info
[2 * n
+ 1]);
1299 info
[2 * n
+ 1] = strdup(param
);
1304 info
= demuxer
->info
=
1305 (char **) realloc(info
, (2 * (n
+ 2)) * sizeof(char *));
1306 info
[2 * n
] = strdup(opt
);
1307 info
[2 * n
+ 1] = strdup(param
);
1308 memset(&info
[2 * (n
+ 1)], 0, 2 * sizeof(char *));
1313 int demux_info_print(demuxer_t
*demuxer
)
1315 char **info
= demuxer
->info
;
1321 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "Clip info:\n");
1322 for (n
= 0; info
[2 * n
] != NULL
; n
++) {
1323 mp_msg(MSGT_DEMUX
, MSGL_INFO
, " %s: %s\n", info
[2 * n
],
1325 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_NAME%d=%s\n", n
,
1327 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_VALUE%d=%s\n", n
,
1330 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_N=%d\n", n
);
1335 char *demux_info_get(demuxer_t
*demuxer
, const char *opt
)
1338 char **info
= demuxer
->info
;
1340 for (i
= 0; info
&& info
[2 * i
] != NULL
; i
++) {
1341 if (!strcasecmp(opt
, info
[2 * i
]))
1342 return info
[2 * i
+ 1];
1348 int demux_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
1351 if (demuxer
->desc
->control
)
1352 return demuxer
->desc
->control(demuxer
, cmd
, arg
);
1354 return DEMUXER_CTRL_NOTIMPL
;
1359 double demuxer_get_time_length(demuxer_t
*demuxer
)
1361 double get_time_ans
;
1362 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1363 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1364 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
1366 (demuxer
, DEMUXER_CTRL_GET_TIME_LENGTH
, (void *) &get_time_ans
) <= 0) {
1367 if (sh_video
&& sh_video
->i_bps
&& sh_audio
&& sh_audio
->i_bps
)
1368 get_time_ans
= (double) (demuxer
->movi_end
-
1369 demuxer
->movi_start
) / (sh_video
->i_bps
+
1371 else if (sh_video
&& sh_video
->i_bps
)
1372 get_time_ans
= (double) (demuxer
->movi_end
-
1373 demuxer
->movi_start
) / sh_video
->i_bps
;
1374 else if (sh_audio
&& sh_audio
->i_bps
)
1375 get_time_ans
= (double) (demuxer
->movi_end
-
1376 demuxer
->movi_start
) / sh_audio
->i_bps
;
1380 return get_time_ans
;
1384 * \brief demuxer_get_current_time() returns the time of the current play in three possible ways:
1385 * either when the stream reader satisfies STREAM_CTRL_GET_CURRENT_TIME (e.g. dvd)
1386 * or using sh_video->pts when the former method fails
1388 * \return the current play time
1390 int demuxer_get_current_time(demuxer_t
*demuxer
)
1392 double get_time_ans
= 0;
1393 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1394 if (demuxer
->stream_pts
!= MP_NOPTS_VALUE
)
1395 get_time_ans
= demuxer
->stream_pts
;
1397 get_time_ans
= sh_video
->pts
;
1398 return (int) get_time_ans
;
1401 int demuxer_get_percent_pos(demuxer_t
*demuxer
)
1404 int res
= demux_control(demuxer
, DEMUXER_CTRL_GET_PERCENT_POS
, &ans
);
1405 int len
= (demuxer
->movi_end
- demuxer
->movi_start
) / 100;
1407 off_t pos
= demuxer
->filepos
> 0 ? demuxer
->filepos
: stream_tell(demuxer
->stream
);
1409 ans
= (pos
- demuxer
->movi_start
) / len
;
1420 int demuxer_switch_audio(demuxer_t
*demuxer
, int index
)
1422 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_AUDIO
, &index
);
1423 if (res
== DEMUXER_CTRL_NOTIMPL
)
1424 index
= demuxer
->audio
->id
;
1425 if (demuxer
->audio
->id
>= 0)
1426 demuxer
->audio
->sh
= demuxer
->a_streams
[demuxer
->audio
->id
];
1428 demuxer
->audio
->sh
= NULL
;
1432 int demuxer_switch_video(demuxer_t
*demuxer
, int index
)
1434 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_VIDEO
, &index
);
1435 if (res
== DEMUXER_CTRL_NOTIMPL
)
1436 index
= demuxer
->video
->id
;
1437 if (demuxer
->video
->id
>= 0)
1438 demuxer
->video
->sh
= demuxer
->v_streams
[demuxer
->video
->id
];
1440 demuxer
->video
->sh
= NULL
;
1444 int demuxer_add_attachment(demuxer_t
*demuxer
, const char *name
,
1445 int name_maxlen
, const char *type
, int type_maxlen
,
1446 const void *data
, size_t size
)
1448 if (!(demuxer
->num_attachments
% 32))
1449 demuxer
->attachments
= talloc_realloc(demuxer
, demuxer
->attachments
,
1450 struct demux_attachment
,
1451 demuxer
->num_attachments
+ 32);
1453 struct demux_attachment
*att
=
1454 demuxer
->attachments
+ demuxer
->num_attachments
;
1455 att
->name
= talloc_strndup(demuxer
->attachments
, name
, name_maxlen
);
1456 att
->type
= talloc_strndup(demuxer
->attachments
, type
, type_maxlen
);
1457 att
->data
= talloc_size(demuxer
->attachments
, size
);
1458 memcpy(att
->data
, data
, size
);
1459 att
->data_size
= size
;
1461 return demuxer
->num_attachments
++;
1464 int demuxer_add_chapter(demuxer_t
*demuxer
, const char *name
, int name_maxlen
,
1465 uint64_t start
, uint64_t end
)
1467 if (!(demuxer
->num_chapters
% 32))
1468 demuxer
->chapters
= talloc_realloc(demuxer
, demuxer
->chapters
,
1469 struct demux_chapter
,
1470 demuxer
->num_chapters
+ 32);
1472 demuxer
->chapters
[demuxer
->num_chapters
].start
= start
;
1473 demuxer
->chapters
[demuxer
->num_chapters
].end
= end
;
1474 demuxer
->chapters
[demuxer
->num_chapters
].name
= name
?
1475 talloc_strndup(demuxer
->chapters
, name
, name_maxlen
) :
1476 talloc_strdup(demuxer
->chapters
, mp_gtext("unknown"));
1478 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_ID=%d\n", demuxer
->num_chapters
);
1479 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_START=%"PRIu64
"\n", demuxer
->num_chapters
, start
);
1481 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_END=%"PRIu64
"\n", demuxer
->num_chapters
, end
);
1483 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_NAME=%.*s\n", demuxer
->num_chapters
, name_maxlen
, name
);
1485 return demuxer
->num_chapters
++;
1489 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1490 * either using the demuxer->chapters structure set by the demuxer
1491 * or asking help to the stream layer (e.g. dvd)
1492 * \param chapter - chapter number wished - 0-based
1493 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1494 * \param chapter_name name of chapter found (set by this function is param is not null)
1495 * \return -1 on error, current chapter if successful
1498 int demuxer_seek_chapter(demuxer_t
*demuxer
, int chapter
, double *seek_pts
,
1499 char **chapter_name
)
1502 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1503 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1505 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1506 demux_flush(demuxer
);
1508 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_CHAPTER
,
1510 if (ris
!= STREAM_UNSUPPORTED
)
1511 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1513 ds_fill_buffer(demuxer
->video
);
1514 resync_video_stream(sh_video
);
1518 ds_fill_buffer(demuxer
->audio
);
1519 resync_audio_stream(sh_audio
);
1521 // exit status may be ok, but main() doesn't have to seek itself
1522 // (because e.g. dvds depend on sectors, not on pts)
1526 *chapter_name
= NULL
;
1528 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1529 &num_chapters
) == STREAM_UNSUPPORTED
)
1532 *chapter_name
= talloc_size(NULL
, 16);
1533 sprintf(*chapter_name
, " of %3d", num_chapters
);
1537 return ris
!= STREAM_UNSUPPORTED
? chapter
: -1;
1538 } else { // chapters structure is set in the demuxer
1539 if (chapter
>= demuxer
->num_chapters
)
1544 *seek_pts
= demuxer
->chapters
[chapter
].start
/ 1000.0;
1547 *chapter_name
= talloc_strdup(NULL
, demuxer
->chapters
[chapter
].name
);
1553 int demuxer_get_current_chapter(demuxer_t
*demuxer
)
1556 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1557 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_CURRENT_CHAPTER
,
1558 &chapter
) == STREAM_UNSUPPORTED
)
1561 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1562 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1564 now
= (sh_video
? sh_video
->pts
: (sh_audio
? sh_audio
->pts
: 0))
1566 for (chapter
= demuxer
->num_chapters
- 1; chapter
>= 0; --chapter
) {
1567 if (demuxer
->chapters
[chapter
].start
<= now
)
1574 char *demuxer_chapter_name(demuxer_t
*demuxer
, int chapter
)
1576 if (demuxer
->num_chapters
&& demuxer
->chapters
) {
1577 if (chapter
>= 0 && chapter
< demuxer
->num_chapters
1578 && demuxer
->chapters
[chapter
].name
)
1579 return strdup(demuxer
->chapters
[chapter
].name
);
1584 char *demuxer_chapter_display_name(demuxer_t
*demuxer
, int chapter
)
1586 char *chapter_name
= demuxer_chapter_name(demuxer
, chapter
);
1588 char *tmp
= malloc(strlen(chapter_name
) + 14);
1589 snprintf(tmp
, 63, "(%d) %s", chapter
+ 1, chapter_name
);
1593 int chapter_num
= demuxer_chapter_count(demuxer
);
1595 if (chapter_num
<= 0)
1596 sprintf(tmp
, "(%d)", chapter
+ 1);
1598 sprintf(tmp
, "(%d) of %d", chapter
+ 1, chapter_num
);
1603 float demuxer_chapter_time(demuxer_t
*demuxer
, int chapter
, float *end
)
1605 if (demuxer
->num_chapters
&& demuxer
->chapters
&& chapter
>= 0
1606 && chapter
< demuxer
->num_chapters
) {
1608 *end
= demuxer
->chapters
[chapter
].end
/ 1000.0;
1609 return demuxer
->chapters
[chapter
].start
/ 1000.0;
1614 int demuxer_chapter_count(demuxer_t
*demuxer
)
1616 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1617 int num_chapters
= 0;
1618 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1619 &num_chapters
) == STREAM_UNSUPPORTED
)
1621 return num_chapters
;
1623 return demuxer
->num_chapters
;
1626 int demuxer_angles_count(demuxer_t
*demuxer
)
1628 int ris
, angles
= -1;
1630 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_ANGLES
, &angles
);
1631 if (ris
== STREAM_UNSUPPORTED
)
1636 int demuxer_get_current_angle(demuxer_t
*demuxer
)
1638 int ris
, curr_angle
= -1;
1639 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_ANGLE
, &curr_angle
);
1640 if (ris
== STREAM_UNSUPPORTED
)
1646 int demuxer_set_angle(demuxer_t
*demuxer
, int angle
)
1648 int ris
, angles
= -1;
1649 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1650 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1652 angles
= demuxer_angles_count(demuxer
);
1653 if ((angles
< 1) || (angle
> angles
))
1656 demux_flush(demuxer
);
1658 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SET_ANGLE
, &angle
);
1659 if (ris
== STREAM_UNSUPPORTED
)
1662 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1664 ds_fill_buffer(demuxer
->video
);
1665 resync_video_stream(sh_video
);
1669 ds_fill_buffer(demuxer
->audio
);
1670 resync_audio_stream(sh_audio
);
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
)