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 void resync_video_stream(sh_video_t
*sh_video
);
63 void resync_audio_stream(sh_audio_t
*sh_audio
);
66 extern const demuxer_desc_t demuxer_desc_rawaudio
;
67 extern const demuxer_desc_t demuxer_desc_rawvideo
;
68 extern const demuxer_desc_t demuxer_desc_tv
;
69 extern const demuxer_desc_t demuxer_desc_mf
;
70 extern const demuxer_desc_t demuxer_desc_avi
;
71 extern const demuxer_desc_t demuxer_desc_y4m
;
72 extern const demuxer_desc_t demuxer_desc_asf
;
73 extern const demuxer_desc_t demuxer_desc_real
;
74 extern const demuxer_desc_t demuxer_desc_smjpeg
;
75 extern const demuxer_desc_t demuxer_desc_matroska
;
76 extern const demuxer_desc_t demuxer_desc_realaudio
;
77 extern const demuxer_desc_t demuxer_desc_vqf
;
78 extern const demuxer_desc_t demuxer_desc_mov
;
79 extern const demuxer_desc_t demuxer_desc_vivo
;
80 extern const demuxer_desc_t demuxer_desc_fli
;
81 extern const demuxer_desc_t demuxer_desc_film
;
82 extern const demuxer_desc_t demuxer_desc_roq
;
83 extern const demuxer_desc_t demuxer_desc_gif
;
84 extern const demuxer_desc_t demuxer_desc_ogg
;
85 extern const demuxer_desc_t demuxer_desc_avs
;
86 extern const demuxer_desc_t demuxer_desc_pva
;
87 extern const demuxer_desc_t demuxer_desc_nsv
;
88 extern const demuxer_desc_t demuxer_desc_mpeg_ts
;
89 extern const demuxer_desc_t demuxer_desc_lmlm4
;
90 extern const demuxer_desc_t demuxer_desc_mpeg_ps
;
91 extern const demuxer_desc_t demuxer_desc_mpeg_pes
;
92 extern const demuxer_desc_t demuxer_desc_mpeg_es
;
93 extern const demuxer_desc_t demuxer_desc_mpeg_gxf
;
94 extern const demuxer_desc_t demuxer_desc_mpeg4_es
;
95 extern const demuxer_desc_t demuxer_desc_h264_es
;
96 extern const demuxer_desc_t demuxer_desc_rawdv
;
97 extern const demuxer_desc_t demuxer_desc_mpc
;
98 extern const demuxer_desc_t demuxer_desc_audio
;
99 extern const demuxer_desc_t demuxer_desc_xmms
;
100 extern const demuxer_desc_t demuxer_desc_mpeg_ty
;
101 extern const demuxer_desc_t demuxer_desc_rtp
;
102 extern const demuxer_desc_t demuxer_desc_rtp_nemesi
;
103 extern const demuxer_desc_t demuxer_desc_lavf
;
104 extern const demuxer_desc_t demuxer_desc_lavf_preferred
;
105 extern const demuxer_desc_t demuxer_desc_aac
;
106 extern const demuxer_desc_t demuxer_desc_nut
;
107 extern const demuxer_desc_t demuxer_desc_mng
;
109 /* Please do not add any new demuxers here. If you want to implement a new
110 * demuxer, add it to libavformat, except for wrappers around external
111 * libraries and demuxers requiring binary support. */
113 const demuxer_desc_t
*const demuxer_list
[] = {
114 &demuxer_desc_rawaudio
,
115 &demuxer_desc_rawvideo
,
120 #ifdef CONFIG_LIBAVFORMAT
121 &demuxer_desc_lavf_preferred
,
128 &demuxer_desc_smjpeg
,
129 &demuxer_desc_matroska
,
130 &demuxer_desc_realaudio
,
140 #ifdef CONFIG_OGGVORBIS
143 #ifdef CONFIG_WIN32DLL
147 &demuxer_desc_mpeg_ts
,
149 &demuxer_desc_mpeg_ps
,
150 &demuxer_desc_mpeg_pes
,
151 &demuxer_desc_mpeg_es
,
152 &demuxer_desc_mpeg_gxf
,
153 &demuxer_desc_mpeg4_es
,
154 &demuxer_desc_h264_es
,
156 &demuxer_desc_mpeg_ty
,
157 #ifdef CONFIG_LIVE555
160 #ifdef CONFIG_LIBNEMESI
161 &demuxer_desc_rtp_nemesi
,
163 #ifdef CONFIG_LIBAVFORMAT
166 #ifdef CONFIG_MUSEPACK
169 #ifdef CONFIG_LIBDV095
182 /* Please do not add any new demuxers here. If you want to implement a new
183 * demuxer, add it to libavformat, except for wrappers around external
184 * libraries and demuxers requiring binary support. */
188 void free_demuxer_stream(demux_stream_t
*ds
)
194 demux_stream_t
*new_demuxer_stream(struct demuxer
*demuxer
, int id
)
196 demux_stream_t
*ds
= malloc(sizeof(demux_stream_t
));
197 *ds
= (demux_stream_t
){
207 * Get demuxer description structure for a given demuxer type
209 * @param file_format type of the demuxer
210 * @return structure for the demuxer, NULL if not found
212 static const demuxer_desc_t
*get_demuxer_desc_from_type(int file_format
)
216 for (i
= 0; demuxer_list
[i
]; i
++)
217 if (file_format
== demuxer_list
[i
]->type
)
218 return demuxer_list
[i
];
224 demuxer_t
*new_demuxer(struct MPOpts
*opts
, stream_t
*stream
, int type
,
225 int a_id
, int v_id
, int s_id
, char *filename
)
227 struct demuxer
*d
= talloc_zero(NULL
, struct demuxer
);
229 d
->stream_pts
= MP_NOPTS_VALUE
;
230 d
->reference_clock
= MP_NOPTS_VALUE
;
231 d
->movi_start
= stream
->start_pos
;
232 d
->movi_end
= stream
->end_pos
;
236 d
->audio
= new_demuxer_stream(d
, a_id
);
237 d
->video
= new_demuxer_stream(d
, v_id
);
238 d
->sub
= new_demuxer_stream(d
, s_id
);
242 if (!(d
->desc
= get_demuxer_desc_from_type(type
)))
243 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
244 "BUG! Invalid demuxer type in new_demuxer(), "
245 "big troubles ahead.");
246 if (filename
) // Filename hack for avs_check_file
247 d
->filename
= strdup(filename
);
248 stream_reset(stream
);
249 stream_seek(stream
, stream
->start_pos
);
253 sh_sub_t
*new_sh_sub_sid(demuxer_t
*demuxer
, int id
, int sid
)
255 if (id
> MAX_S_STREAMS
- 1 || id
< 0) {
256 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
257 "Requested sub stream id overflow (%d > %d)\n", id
,
261 if (demuxer
->s_streams
[id
])
262 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, "Sub stream %i redefined\n", id
);
264 sh_sub_t
*sh
= calloc(1, sizeof(sh_sub_t
));
265 demuxer
->s_streams
[id
] = sh
;
267 sh
->opts
= demuxer
->opts
;
268 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SUBTITLE_ID=%d\n", sid
);
270 if (sid
== demuxer
->opts
->sub_id
) {
271 demuxer
->sub
->id
= id
;
272 demuxer
->sub
->sh
= demuxer
->s_streams
[id
];
274 return demuxer
->s_streams
[id
];
277 static void free_sh_sub(sh_sub_t
*sh
)
279 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_sub at %p\n", sh
);
283 ass_free_track(sh
->ass_track
);
286 #ifdef CONFIG_LIBAVCODEC
287 av_parser_close(sh
->parser
);
288 av_freep(&sh
->avctx
);
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 av_parser_close(sh
->parser
);
330 av_freep(&sh
->avctx
);
335 sh_video_t
*new_sh_video_vid(demuxer_t
*demuxer
, int id
, int vid
)
337 if (id
> MAX_V_STREAMS
- 1 || id
< 0) {
338 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
339 "Requested video stream id overflow (%d > %d)\n", id
,
343 if (demuxer
->v_streams
[id
])
344 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, "WARNING: Video stream header %d redefined.\n", id
);
346 mp_tmsg(MSGT_DEMUXER
, MSGL_V
, "==> Found video stream: %d\n", id
);
347 sh_video_t
*sh
= calloc(1, sizeof *sh
);
348 demuxer
->v_streams
[id
] = sh
;
350 sh
->ds
= demuxer
->video
;
351 sh
->opts
= demuxer
->opts
;
352 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_ID=%d\n", vid
);
354 return demuxer
->v_streams
[id
];
357 void free_sh_video(sh_video_t
*sh
)
359 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_video at %p\n", sh
);
361 #ifdef CONFIG_LIBAVCODEC
362 av_parser_close(sh
->parser
);
363 av_freep(&sh
->avctx
);
368 void free_demuxer(demuxer_t
*demuxer
)
371 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing demuxer at %p\n",
373 if (demuxer
->desc
->close
)
374 demuxer
->desc
->close(demuxer
);
375 // Very ugly hack to make it behave like old implementation
376 if (demuxer
->desc
->type
== DEMUXER_TYPE_DEMUXERS
)
377 goto skip_streamfree
;
379 for (i
= 0; i
< MAX_A_STREAMS
; i
++)
380 if (demuxer
->a_streams
[i
])
381 free_sh_audio(demuxer
, i
);
382 for (i
= 0; i
< MAX_V_STREAMS
; i
++)
383 if (demuxer
->v_streams
[i
])
384 free_sh_video(demuxer
->v_streams
[i
]);
385 for (i
= 0; i
< MAX_S_STREAMS
; i
++)
386 if (demuxer
->s_streams
[i
])
387 free_sh_sub(demuxer
->s_streams
[i
]);
389 free_demuxer_stream(demuxer
->audio
);
390 free_demuxer_stream(demuxer
->video
);
391 free_demuxer_stream(demuxer
->sub
);
394 for (i
= 0; demuxer
->info
[i
] != NULL
; i
++)
395 free(demuxer
->info
[i
]);
398 free(demuxer
->filename
);
399 if (demuxer
->chapters
) {
400 for (i
= 0; i
< demuxer
->num_chapters
; i
++)
401 free(demuxer
->chapters
[i
].name
);
402 free(demuxer
->chapters
);
404 if (demuxer
->attachments
) {
405 for (i
= 0; i
< demuxer
->num_attachments
; i
++) {
406 free(demuxer
->attachments
[i
].name
);
407 free(demuxer
->attachments
[i
].type
);
408 free(demuxer
->attachments
[i
].data
);
410 free(demuxer
->attachments
);
412 if (demuxer
->teletext
)
413 teletext_control(demuxer
->teletext
, TV_VBI_CONTROL_STOP
, NULL
);
414 talloc_free(demuxer
);
418 static void ds_add_packet_internal(demux_stream_t
*ds
, demux_packet_t
*dp
)
420 // append packet to DS stream:
422 ds
->bytes
+= dp
->len
;
424 // next packet in stream
428 // first packet in stream
429 ds
->first
= ds
->last
= dp
;
431 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
432 "DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
433 (ds
== ds
->demuxer
->audio
) ? "d_audio" : "d_video", dp
->len
,
434 dp
->pts
, (unsigned int) dp
->pos
, ds
->demuxer
->audio
->packs
,
435 ds
->demuxer
->video
->packs
);
438 #ifdef CONFIG_LIBAVCODEC
439 static void allocate_parser(AVCodecContext
**avctx
, AVCodecParserContext
**parser
, unsigned format
)
441 enum CodecID codec_id
= CODEC_ID_NONE
;
442 extern int avcodec_initialized
;
443 if (!avcodec_initialized
) {
445 avcodec_register_all();
446 avcodec_initialized
= 1;
452 case MKTAG('d', 'n', 'e', 't'):
453 case MKTAG('s', 'a', 'c', '3'):
454 codec_id
= CODEC_ID_AC3
;
456 case MKTAG('E', 'A', 'C', '3'):
457 codec_id
= CODEC_ID_EAC3
;
461 codec_id
= CODEC_ID_DTS
;
465 case MKTAG('.', 'm', 'p', '3'):
466 case MKTAG('M', 'P', 'E', ' '):
467 case MKTAG('L', 'A', 'M', 'E'):
468 codec_id
= CODEC_ID_MP3
;
471 case MKTAG('.', 'm', 'p', '2'):
472 case MKTAG('.', 'm', 'p', '1'):
473 codec_id
= CODEC_ID_MP2
;
476 if (codec_id
!= CODEC_ID_NONE
) {
477 *avctx
= avcodec_alloc_context();
480 *parser
= av_parser_init(codec_id
);
486 static void get_parser(sh_common_t
*sh
, AVCodecContext
**avctx
, AVCodecParserContext
**parser
)
491 if (!sh
|| !sh
->needs_parsing
)
495 *parser
= sh
->parser
;
499 allocate_parser(avctx
, parser
, sh
->format
);
501 sh
->parser
= *parser
;
504 int ds_parse(demux_stream_t
*ds
, uint8_t **buffer
, int *len
, double pts
, off_t pos
)
506 AVCodecContext
*avctx
;
507 AVCodecParserContext
*parser
;
508 get_parser(ds
->sh
, &avctx
, &parser
);
511 return av_parser_parse2(parser
, avctx
, buffer
, len
, *buffer
, *len
, pts
, pts
, pos
);
515 void ds_add_packet(demux_stream_t
*ds
, demux_packet_t
*dp
)
517 #if PARSE_ON_ADD && defined(CONFIG_LIBAVCODEC)
521 uint8_t *parsed_start
= dp
->buffer
+ pos
;
522 int parsed_len
= len
;
523 int consumed
= ds_parse(ds
->sh
, &parsed_start
, &parsed_len
, dp
->pts
, dp
->pos
);
526 if (parsed_start
== dp
->buffer
&& parsed_len
== dp
->len
) {
527 ds_add_packet_internal(ds
, dp
);
528 } else if (parsed_len
) {
529 demux_packet_t
*dp2
= new_demux_packet(parsed_len
);
531 dp2
->pts
= dp
->pts
; // should be parser->pts but that works badly
532 memcpy(dp2
->buffer
, parsed_start
, parsed_len
);
533 ds_add_packet_internal(ds
, dp2
);
537 ds_add_packet_internal(ds
, dp
);
541 void ds_read_packet(demux_stream_t
*ds
, stream_t
*stream
, int len
,
542 double pts
, off_t pos
, int flags
)
544 demux_packet_t
*dp
= new_demux_packet(len
);
545 len
= stream_read(stream
, dp
->buffer
, len
);
546 resize_demux_packet(dp
, len
);
550 // append packet to DS stream:
551 ds_add_packet(ds
, dp
);
555 // 0 = EOF or no stream found or invalid type
556 // 1 = successfully read a packet
558 int demux_fill_buffer(demuxer_t
*demux
, demux_stream_t
*ds
)
560 // Note: parameter 'ds' can be NULL!
561 return demux
->desc
->fill_buffer(demux
, ds
);
567 #define MAX_ACUMULATED_PACKETS 64
568 int ds_fill_buffer(demux_stream_t
*ds
)
570 demuxer_t
*demux
= ds
->demuxer
;
572 free_demux_packet(ds
->current
);
574 if (mp_msg_test(MSGT_DEMUXER
, MSGL_DBG3
)) {
575 if (ds
== demux
->audio
)
576 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
577 "ds_fill_buffer(d_audio) called\n");
578 else if (ds
== demux
->video
)
579 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
580 "ds_fill_buffer(d_video) called\n");
581 else if (ds
== demux
->sub
)
582 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
, "ds_fill_buffer(d_sub) called\n");
584 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
585 "ds_fill_buffer(unknown 0x%X) called\n", (unsigned int) ds
);
589 demux_packet_t
*p
= ds
->first
;
591 if (demux
->reference_clock
!= MP_NOPTS_VALUE
) {
592 if ( p
->pts
!= MP_NOPTS_VALUE
593 && p
->pts
> demux
->reference_clock
594 && ds
->packs
< MAX_ACUMULATED_PACKETS
) {
595 if (demux_fill_buffer(demux
, ds
))
601 ds
->buffer
= p
->buffer
;
603 ds
->buffer_size
= p
->len
;
605 ds
->dpos
+= p
->len
; // !!!
607 if (p
->pts
!= MP_NOPTS_VALUE
) {
611 ds
->pts_bytes
+= p
->len
; // !!!
612 if (p
->stream_pts
!= MP_NOPTS_VALUE
)
613 demux
->stream_pts
= p
->stream_pts
;
614 ds
->flags
= p
->flags
;
622 /* The code below can set ds->eof to 1 when another stream runs
623 * out of buffer space. That makes sense because in that situation
624 * the calling code should not count on being able to demux more
625 * packets from this stream.
626 * If however the situation improves and we're called again
627 * despite the eof flag then it's better to clear it to avoid
633 #define MaybeNI _("Maybe you are playing a non-interleaved stream/file or the codec failed?\n" \
634 "For AVI files, try to force non-interleaved mode with the -ni option.\n")
636 if (demux
->audio
->packs
>= MAX_PACKS
637 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
638 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
639 demux
->audio
->packs
, demux
->audio
->bytes
);
640 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
643 if (demux
->video
->packs
>= MAX_PACKS
644 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
645 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
646 demux
->video
->packs
, demux
->video
->bytes
);
647 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
650 if (!demux_fill_buffer(demux
, ds
)) {
651 #if PARSE_ON_ADD && defined(CONFIG_LIBAVCODEC)
652 uint8_t *parsed_start
= NULL
;
654 ds_parse(ds
->sh
, &parsed_start
, &parsed_len
, MP_NOPTS_VALUE
, 0);
656 demux_packet_t
*dp2
= new_demux_packet(parsed_len
);
657 dp2
->pts
= MP_NOPTS_VALUE
;
658 memcpy(dp2
->buffer
, parsed_start
, parsed_len
);
659 ds_add_packet_internal(ds
, dp2
);
663 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
664 "ds_fill_buffer()->demux_fill_buffer() failed\n");
668 ds
->buffer_pos
= ds
->buffer_size
= 0;
670 mp_msg(MSGT_DEMUXER
, MSGL_V
,
671 "ds_fill_buffer: EOF reached (stream: %s) \n",
672 ds
== demux
->audio
? "audio" : "video");
677 int demux_read_data(demux_stream_t
*ds
, unsigned char *mem
, int len
)
682 x
= ds
->buffer_size
- ds
->buffer_pos
;
684 if (!ds_fill_buffer(ds
))
690 fast_memcpy(mem
+ bytes
, &ds
->buffer
[ds
->buffer_pos
], x
);
700 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
701 * \param mem memory to read data into, may be NULL to discard data
702 * \param maxlen maximum number of bytes to read
703 * \param read number of bytes actually read
704 * \param pattern pattern to search for (lowest 8 bits are ignored)
705 * \return whether pattern was found
707 int demux_pattern_3(demux_stream_t
*ds
, unsigned char *mem
, int maxlen
,
708 int *read
, uint32_t pattern
)
710 register uint32_t head
= 0xffffff00;
711 register uint32_t pat
= pattern
& 0xffffff00;
714 register unsigned char *ds_buf
= &ds
->buffer
[ds
->buffer_size
];
715 int len
= ds
->buffer_size
- ds
->buffer_pos
;
716 register long pos
= -len
;
717 if (unlikely(pos
>= 0)) { // buffer is empty
724 } while (++pos
&& head
!= pat
);
726 if (total_len
+ len
> maxlen
)
727 len
= maxlen
- total_len
;
728 len
= demux_read_data(ds
, mem
? &mem
[total_len
] : NULL
, len
);
730 } while ((head
!= pat
|| total_len
< 3) && total_len
< maxlen
&& !ds
->eof
);
733 return total_len
>= 3 && head
== pat
;
736 void ds_free_packs(demux_stream_t
*ds
)
738 demux_packet_t
*dp
= ds
->first
;
740 demux_packet_t
*dn
= dp
->next
;
741 free_demux_packet(dp
);
744 if (ds
->asf_packet
) {
745 // free unfinished .asf fragments:
746 free(ds
->asf_packet
->buffer
);
747 free(ds
->asf_packet
);
748 ds
->asf_packet
= NULL
;
750 ds
->first
= ds
->last
= NULL
;
751 ds
->packs
= 0; // !!!!!
754 free_demux_packet(ds
->current
);
757 ds
->buffer_pos
= ds
->buffer_size
;
762 int ds_get_packet(demux_stream_t
*ds
, unsigned char **start
)
765 if (ds
->buffer_pos
>= ds
->buffer_size
) {
766 if (!ds_fill_buffer(ds
)) {
772 len
= ds
->buffer_size
- ds
->buffer_pos
;
773 *start
= &ds
->buffer
[ds
->buffer_pos
];
774 ds
->buffer_pos
+= len
;
778 int ds_get_packet_pts(demux_stream_t
*ds
, unsigned char **start
, double *pts
)
781 *pts
= MP_NOPTS_VALUE
;
782 len
= ds_get_packet(ds
, start
);
785 // Return pts unless this read starts from the middle of a packet
786 if (len
== ds
->buffer_pos
)
787 *pts
= ds
->current
->pts
;
791 int ds_get_packet_sub(demux_stream_t
*ds
, unsigned char **start
)
794 if (ds
->buffer_pos
>= ds
->buffer_size
) {
798 if (!ds_fill_buffer(ds
))
801 len
= ds
->buffer_size
- ds
->buffer_pos
;
802 *start
= &ds
->buffer
[ds
->buffer_pos
];
803 ds
->buffer_pos
+= len
;
807 double ds_get_next_pts(demux_stream_t
*ds
)
809 demuxer_t
*demux
= ds
->demuxer
;
811 if (demux
->audio
->packs
>= MAX_PACKS
812 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
813 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
814 demux
->audio
->packs
, demux
->audio
->bytes
);
815 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
816 return MP_NOPTS_VALUE
;
818 if (demux
->video
->packs
>= MAX_PACKS
819 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
820 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
821 demux
->video
->packs
, demux
->video
->bytes
);
822 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
823 return MP_NOPTS_VALUE
;
825 if (!demux_fill_buffer(demux
, ds
))
826 return MP_NOPTS_VALUE
;
828 return ds
->first
->pts
;
831 // ====================================================================
833 void demuxer_help(void)
837 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "Available demuxers:\n");
838 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, " demuxer: type info: (comment)\n");
839 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DEMUXERS\n");
840 for (i
= 0; demuxer_list
[i
]; i
++) {
841 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Don't display special demuxers
843 if (demuxer_list
[i
]->comment
&& strlen(demuxer_list
[i
]->comment
))
844 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s (%s)\n",
845 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
846 demuxer_list
[i
]->info
, demuxer_list
[i
]->comment
);
848 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s\n",
849 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
850 demuxer_list
[i
]->info
);
856 * Get demuxer type for a given demuxer name
858 * @param demuxer_name string with demuxer name of demuxer number
859 * @param force will be set if demuxer should be forced.
861 * @return DEMUXER_TYPE_xxx, -1 if error or not found
863 int get_demuxer_type_from_name(char *demuxer_name
, int *force
)
869 if (!demuxer_name
|| !demuxer_name
[0])
870 return DEMUXER_TYPE_UNKNOWN
;
872 *force
= demuxer_name
[0] == '+';
873 if (demuxer_name
[0] == '+')
874 demuxer_name
= &demuxer_name
[1];
875 for (i
= 0; demuxer_list
[i
]; i
++) {
876 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Can't select special demuxers from commandline
878 if (strcmp(demuxer_name
, demuxer_list
[i
]->name
) == 0)
879 return demuxer_list
[i
]->type
;
882 // No match found, try to parse name as an integer (demuxer number)
883 type_int
= strtol(demuxer_name
, &endptr
, 0);
884 if (*endptr
) // Conversion failed
886 if ((type_int
> 0) && (type_int
<= DEMUXER_TYPE_MAX
))
887 return (int) type_int
;
892 int extension_parsing
= 1; // 0=off 1=mixed (used only for unstable formats)
895 NOTE : Several demuxers may be opened at the same time so
896 demuxers should NEVER rely on an external var to enable them
897 self. If a demuxer can't do any auto-detection it should only use
898 file_format. The user can explicitly set file_format with the -demuxer
899 option so there is really no need for another extra var.
900 For convenience an option can be added to set file_format directly
901 to the right type (ex: rawaudio,rawvideo).
902 Also the stream can override the file_format so a demuxer which rely
903 on a special stream type can set file_format at the stream level
907 static demuxer_t
*demux_open_stream(struct MPOpts
*opts
, stream_t
*stream
,
908 int file_format
, int force
, int audio_id
,
909 int video_id
, int dvdsub_id
,
912 demuxer_t
*demuxer
= NULL
;
914 sh_video_t
*sh_video
= NULL
;
916 const demuxer_desc_t
*demuxer_desc
;
920 // If somebody requested a demuxer check it
922 if ((demuxer_desc
= get_demuxer_desc_from_type(file_format
))) {
923 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
924 video_id
, dvdsub_id
, filename
);
925 if (demuxer_desc
->check_file
)
926 fformat
= demuxer_desc
->check_file(demuxer
);
927 if (force
|| !demuxer_desc
->check_file
)
928 fformat
= demuxer_desc
->type
;
930 if (fformat
== demuxer_desc
->type
) {
931 demuxer_t
*demux2
= demuxer
;
932 // Move messages to demuxer detection code?
933 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
934 "%s file format detected.\n",
935 demuxer_desc
->shortdesc
);
936 file_format
= fformat
;
937 if (!demuxer
->desc
->open
938 || (demux2
= demuxer
->desc
->open(demuxer
))) {
943 // Format changed after check, recurse
944 free_demuxer(demuxer
);
945 return demux_open_stream(opts
, stream
, fformat
, force
,
946 audio_id
, video_id
, dvdsub_id
,
950 // Check failed for forced demuxer, quit
951 free_demuxer(demuxer
);
955 // Test demuxers with safe file checks
956 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
957 if (demuxer_desc
->safe_check
) {
958 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
959 video_id
, dvdsub_id
, filename
);
960 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
961 if (fformat
== demuxer_desc
->type
) {
962 demuxer_t
*demux2
= demuxer
;
963 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
964 "%s file format detected.\n",
965 demuxer_desc
->shortdesc
);
966 file_format
= fformat
;
967 if (!demuxer
->desc
->open
968 || (demux2
= demuxer
->desc
->open(demuxer
))) {
973 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
974 return demuxer
; // handled in mplayer.c
975 // Format changed after check, recurse
976 free_demuxer(demuxer
);
977 demuxer
= demux_open_stream(opts
, stream
, fformat
, force
,
979 dvdsub_id
, filename
);
981 return demuxer
; // done!
982 file_format
= DEMUXER_TYPE_UNKNOWN
;
985 free_demuxer(demuxer
);
990 // If no forced demuxer perform file extension based detection
991 // Ok. We're over the stable detectable fileformats, the next ones are
992 // a bit fuzzy. So by default (extension_parsing==1) try extension-based
994 if (file_format
== DEMUXER_TYPE_UNKNOWN
&& filename
995 && extension_parsing
== 1) {
996 file_format
= demuxer_type_by_filename(filename
);
997 if (file_format
!= DEMUXER_TYPE_UNKNOWN
) {
998 // we like recursion :)
999 demuxer
= demux_open_stream(opts
, stream
, file_format
, force
,
1000 audio_id
, video_id
, dvdsub_id
,
1003 return demuxer
; // done!
1004 file_format
= DEMUXER_TYPE_UNKNOWN
; // continue fuzzy guessing...
1005 mp_msg(MSGT_DEMUXER
, MSGL_V
,
1006 "demuxer: continue fuzzy content-based format guessing...\n");
1009 // Try detection for all other demuxers
1010 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
1011 if (!demuxer_desc
->safe_check
&& demuxer_desc
->check_file
) {
1012 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
1013 video_id
, dvdsub_id
, filename
);
1014 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
1015 if (fformat
== demuxer_desc
->type
) {
1016 demuxer_t
*demux2
= demuxer
;
1017 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
1018 "%s file format detected.\n",
1019 demuxer_desc
->shortdesc
);
1020 file_format
= fformat
;
1021 if (!demuxer
->desc
->open
1022 || (demux2
= demuxer
->desc
->open(demuxer
))) {
1027 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
1028 return demuxer
; // handled in mplayer.c
1029 // Format changed after check, recurse
1030 free_demuxer(demuxer
);
1031 demuxer
= demux_open_stream(opts
, stream
, fformat
, force
,
1033 dvdsub_id
, filename
);
1035 return demuxer
; // done!
1036 file_format
= DEMUXER_TYPE_UNKNOWN
;
1039 free_demuxer(demuxer
);
1045 //====== File format recognized, set up these for compatibility: =========
1048 demuxer
->file_format
= file_format
;
1050 if ((sh_video
= demuxer
->video
->sh
) && sh_video
->bih
) {
1051 int biComp
= le2me_32(sh_video
->bih
->biCompression
);
1052 mp_msg(MSGT_DEMUX
, MSGL_INFO
,
1053 "VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
1054 (char *) &biComp
, sh_video
->bih
->biWidth
,
1055 sh_video
->bih
->biHeight
, sh_video
->bih
->biBitCount
,
1056 sh_video
->fps
, sh_video
->i_bps
* 0.008f
,
1057 sh_video
->i_bps
/ 1024.0f
);
1060 if (opts
->ass_enabled
&& ass_library
) {
1061 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1062 sh_sub_t
*sh
= demuxer
->s_streams
[i
];
1063 if (sh
&& sh
->type
== 'a') {
1064 sh
->ass_track
= ass_new_track(ass_library
);
1065 if (sh
->ass_track
&& sh
->extradata
)
1066 ass_process_codec_private(sh
->ass_track
, sh
->extradata
,
1068 } else if (sh
&& sh
->type
!= 'v')
1069 sh
->ass_track
= ass_default_track(ass_library
);
1076 char *audio_stream
= NULL
;
1077 char *sub_stream
= NULL
;
1078 int audio_stream_cache
= 0;
1080 char *demuxer_name
= NULL
; // parameter from -demuxer
1081 char *audio_demuxer_name
= NULL
; // parameter from -audio-demuxer
1082 char *sub_demuxer_name
= NULL
; // parameter from -sub-demuxer
1084 extern int hr_mp3_seek
;
1086 extern float stream_cache_min_percent
;
1087 extern float stream_cache_seek_min_percent
;
1089 demuxer_t
*demux_open(struct MPOpts
*opts
, stream_t
*vs
, int file_format
,
1090 int audio_id
, int video_id
, int dvdsub_id
,
1093 stream_t
*as
= NULL
, *ss
= NULL
;
1094 demuxer_t
*vd
, *ad
= NULL
, *sd
= NULL
;
1096 int afmt
= DEMUXER_TYPE_UNKNOWN
, sfmt
= DEMUXER_TYPE_UNKNOWN
;
1098 int audio_demuxer_type
= 0, sub_demuxer_type
= 0;
1099 int demuxer_force
= 0, audio_demuxer_force
= 0, sub_demuxer_force
= 0;
1102 get_demuxer_type_from_name(demuxer_name
, &demuxer_force
)) < 0) {
1103 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-demuxer %s does not exist.\n",
1106 if ((audio_demuxer_type
=
1107 get_demuxer_type_from_name(audio_demuxer_name
,
1108 &audio_demuxer_force
)) < 0) {
1109 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-audio-demuxer %s does not exist.\n",
1110 audio_demuxer_name
);
1112 if ((sub_demuxer_type
=
1113 get_demuxer_type_from_name(sub_demuxer_name
,
1114 &sub_demuxer_force
)) < 0) {
1115 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-sub-demuxer %s does not exist.\n",
1120 as
= open_stream(audio_stream
, 0, &afmt
);
1122 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "Cannot open audio stream: %s\n",
1126 if (audio_stream_cache
) {
1127 if (!stream_enable_cache
1128 (as
, audio_stream_cache
* 1024,
1129 audio_stream_cache
* 1024 * (stream_cache_min_percent
/
1131 audio_stream_cache
* 1024 * (stream_cache_seek_min_percent
/
1134 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
1135 "Can't enable audio stream cache\n");
1141 ss
= open_stream(sub_stream
, 0, &sfmt
);
1143 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "Cannot open subtitle stream: %s\n",
1149 vd
= demux_open_stream(opts
, vs
, demuxer_type
? demuxer_type
: file_format
,
1150 demuxer_force
, audio_stream
? -2 : audio_id
,
1151 video_id
, sub_stream
? -2 : dvdsub_id
, filename
);
1160 ad
= demux_open_stream(opts
, as
,
1161 audio_demuxer_type
? audio_demuxer_type
: afmt
,
1162 audio_demuxer_force
, audio_id
, -2, -2,
1165 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, "Failed to open audio demuxer: %s\n",
1168 } else if (ad
->audio
->sh
1169 && ((sh_audio_t
*) ad
->audio
->sh
)->format
== 0x55) // MP3
1170 hr_mp3_seek
= 1; // Enable high res seeking
1173 sd
= demux_open_stream(opts
, ss
,
1174 sub_demuxer_type
? sub_demuxer_type
: sfmt
,
1175 sub_demuxer_force
, -2, -2, dvdsub_id
,
1178 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
,
1179 "Failed to open subtitle demuxer: %s\n", sub_stream
);
1185 res
= new_demuxers_demuxer(vd
, ad
, sd
);
1187 res
= new_demuxers_demuxer(vd
, ad
, vd
);
1189 res
= new_demuxers_demuxer(vd
, vd
, sd
);
1193 opts
->correct_pts
= opts
->user_correct_pts
;
1194 if (opts
->correct_pts
< 0)
1196 demux_control(res
, DEMUXER_CTRL_CORRECT_PTS
,
1197 NULL
) == DEMUXER_CTRL_OK
;
1202 void demux_flush(demuxer_t
*demuxer
)
1204 ds_free_packs(demuxer
->video
);
1205 ds_free_packs(demuxer
->audio
);
1206 ds_free_packs(demuxer
->sub
);
1209 int demux_seek(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
,
1212 demux_stream_t
*d_audio
= demuxer
->audio
;
1213 demux_stream_t
*d_video
= demuxer
->video
;
1214 sh_audio_t
*sh_audio
= d_audio
->sh
;
1215 sh_video_t
*sh_video
= d_video
->sh
;
1219 if (!demuxer
->seekable
) {
1220 if (demuxer
->file_format
== DEMUXER_TYPE_AVI
)
1221 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "Cannot seek in raw AVI streams. (Index required, try with the -idx switch.)\n");
1223 else if (demuxer
->file_format
== DEMUXER_TYPE_TV
)
1224 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "TV input is not seekable! (Seeking will probably be for changing channels ;)\n");
1227 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "Cannot seek in this file.\n");
1230 // clear demux buffers:
1231 demux_flush(demuxer
);
1233 sh_audio
->a_buffer_len
= 0;
1235 demuxer
->stream
->eof
= 0;
1236 demuxer
->video
->eof
= 0;
1237 demuxer
->audio
->eof
= 0;
1240 sh_video
->timer
= 0; // !!!!!!
1242 if (flags
& SEEK_ABSOLUTE
)
1245 if (demuxer
->stream_pts
== MP_NOPTS_VALUE
)
1247 pts
= demuxer
->stream_pts
;
1250 if (flags
& SEEK_FACTOR
) {
1251 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_TIME_LENGTH
, &tmp
)
1252 == STREAM_UNSUPPORTED
)
1254 pts
+= tmp
* rel_seek_secs
;
1256 pts
+= rel_seek_secs
;
1258 if (stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_TIME
, &pts
) !=
1259 STREAM_UNSUPPORTED
) {
1260 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1265 if (demuxer
->desc
->seek
)
1266 demuxer
->desc
->seek(demuxer
, rel_seek_secs
, audio_delay
, flags
);
1269 resync_audio_stream(sh_audio
);
1274 int demux_info_add(demuxer_t
*demuxer
, const char *opt
, const char *param
)
1276 char **info
= demuxer
->info
;
1280 for (n
= 0; info
&& info
[2 * n
] != NULL
; n
++) {
1281 if (!strcasecmp(opt
, info
[2 * n
])) {
1282 if (!strcmp(param
, info
[2 * n
+ 1])) {
1283 mp_msg(MSGT_DEMUX
, MSGL_V
, "Demuxer info %s set to unchanged value %s\n", opt
, param
);
1286 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "Demuxer info %s changed to %s\n", opt
,
1288 free(info
[2 * n
+ 1]);
1289 info
[2 * n
+ 1] = strdup(param
);
1294 info
= demuxer
->info
=
1295 (char **) realloc(info
, (2 * (n
+ 2)) * sizeof(char *));
1296 info
[2 * n
] = strdup(opt
);
1297 info
[2 * n
+ 1] = strdup(param
);
1298 memset(&info
[2 * (n
+ 1)], 0, 2 * sizeof(char *));
1303 int demux_info_print(demuxer_t
*demuxer
)
1305 char **info
= demuxer
->info
;
1311 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "Clip info:\n");
1312 for (n
= 0; info
[2 * n
] != NULL
; n
++) {
1313 mp_msg(MSGT_DEMUX
, MSGL_INFO
, " %s: %s\n", info
[2 * n
],
1315 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_NAME%d=%s\n", n
,
1317 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_VALUE%d=%s\n", n
,
1320 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_N=%d\n", n
);
1325 char *demux_info_get(demuxer_t
*demuxer
, const char *opt
)
1328 char **info
= demuxer
->info
;
1330 for (i
= 0; info
&& info
[2 * i
] != NULL
; i
++) {
1331 if (!strcasecmp(opt
, info
[2 * i
]))
1332 return info
[2 * i
+ 1];
1338 int demux_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
1341 if (demuxer
->desc
->control
)
1342 return demuxer
->desc
->control(demuxer
, cmd
, arg
);
1344 return DEMUXER_CTRL_NOTIMPL
;
1349 double demuxer_get_time_length(demuxer_t
*demuxer
)
1351 double get_time_ans
;
1352 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1353 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1354 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
1356 (demuxer
, DEMUXER_CTRL_GET_TIME_LENGTH
, (void *) &get_time_ans
) <= 0) {
1357 if (sh_video
&& sh_video
->i_bps
&& sh_audio
&& sh_audio
->i_bps
)
1358 get_time_ans
= (double) (demuxer
->movi_end
-
1359 demuxer
->movi_start
) / (sh_video
->i_bps
+
1361 else if (sh_video
&& sh_video
->i_bps
)
1362 get_time_ans
= (double) (demuxer
->movi_end
-
1363 demuxer
->movi_start
) / sh_video
->i_bps
;
1364 else if (sh_audio
&& sh_audio
->i_bps
)
1365 get_time_ans
= (double) (demuxer
->movi_end
-
1366 demuxer
->movi_start
) / sh_audio
->i_bps
;
1370 return get_time_ans
;
1374 * \brief demuxer_get_current_time() returns the time of the current play in three possible ways:
1375 * either when the stream reader satisfies STREAM_CTRL_GET_CURRENT_TIME (e.g. dvd)
1376 * or using sh_video->pts when the former method fails
1378 * \return the current play time
1380 int demuxer_get_current_time(demuxer_t
*demuxer
)
1382 double get_time_ans
= 0;
1383 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1384 if (demuxer
->stream_pts
!= MP_NOPTS_VALUE
)
1385 get_time_ans
= demuxer
->stream_pts
;
1387 get_time_ans
= sh_video
->pts
;
1388 return (int) get_time_ans
;
1391 int demuxer_get_percent_pos(demuxer_t
*demuxer
)
1394 int res
= demux_control(demuxer
, DEMUXER_CTRL_GET_PERCENT_POS
, &ans
);
1395 int len
= (demuxer
->movi_end
- demuxer
->movi_start
) / 100;
1397 off_t pos
= demuxer
->filepos
> 0 ? demuxer
->filepos
: stream_tell(demuxer
->stream
);
1399 ans
= (pos
- demuxer
->movi_start
) / len
;
1410 int demuxer_switch_audio(demuxer_t
*demuxer
, int index
)
1412 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_AUDIO
, &index
);
1413 if (res
== DEMUXER_CTRL_NOTIMPL
)
1414 index
= demuxer
->audio
->id
;
1415 if (demuxer
->audio
->id
>= 0)
1416 demuxer
->audio
->sh
= demuxer
->a_streams
[demuxer
->audio
->id
];
1418 demuxer
->audio
->sh
= NULL
;
1422 int demuxer_switch_video(demuxer_t
*demuxer
, int index
)
1424 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_VIDEO
, &index
);
1425 if (res
== DEMUXER_CTRL_NOTIMPL
)
1426 index
= demuxer
->video
->id
;
1427 if (demuxer
->video
->id
>= 0)
1428 demuxer
->video
->sh
= demuxer
->v_streams
[demuxer
->video
->id
];
1430 demuxer
->video
->sh
= NULL
;
1434 int demuxer_add_attachment(demuxer_t
*demuxer
, const char *name
,
1435 const char *type
, const void *data
, size_t size
)
1437 if (!(demuxer
->num_attachments
& 31))
1438 demuxer
->attachments
= realloc(demuxer
->attachments
,
1439 (demuxer
->num_attachments
+ 32) * sizeof(demux_attachment_t
));
1441 demuxer
->attachments
[demuxer
->num_attachments
].name
= strdup(name
);
1442 demuxer
->attachments
[demuxer
->num_attachments
].type
= strdup(type
);
1443 demuxer
->attachments
[demuxer
->num_attachments
].data
= malloc(size
);
1444 memcpy(demuxer
->attachments
[demuxer
->num_attachments
].data
, data
, size
);
1445 demuxer
->attachments
[demuxer
->num_attachments
].data_size
= size
;
1447 return demuxer
->num_attachments
++;
1450 int demuxer_add_chapter(demuxer_t
*demuxer
, const char *name
, uint64_t start
,
1453 if (demuxer
->chapters
== NULL
)
1454 demuxer
->chapters
= malloc(32 * sizeof(*demuxer
->chapters
));
1455 else if (!(demuxer
->num_chapters
% 32))
1456 demuxer
->chapters
= realloc(demuxer
->chapters
,
1457 (demuxer
->num_chapters
+ 32) *
1458 sizeof(*demuxer
->chapters
));
1460 demuxer
->chapters
[demuxer
->num_chapters
].start
= start
;
1461 demuxer
->chapters
[demuxer
->num_chapters
].end
= end
;
1462 demuxer
->chapters
[demuxer
->num_chapters
].name
= strdup(name
? name
: mp_gtext("unknown"));
1464 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_ID=%d\n", demuxer
->num_chapters
);
1465 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_START=%"PRIu64
"\n", demuxer
->num_chapters
, start
);
1467 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_END=%"PRIu64
"\n", demuxer
->num_chapters
, end
);
1469 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_NAME=%s\n", demuxer
->num_chapters
, name
);
1471 return demuxer
->num_chapters
++;
1475 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1476 * either using the demuxer->chapters structure set by the demuxer
1477 * or asking help to the stream layer (e.g. dvd)
1478 * \param chapter - chapter number wished - 0-based
1479 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1480 * \param chapter_name name of chapter found (set by this function is param is not null)
1481 * \return -1 on error, current chapter if successful
1484 int demuxer_seek_chapter(demuxer_t
*demuxer
, int chapter
, double *seek_pts
,
1485 char **chapter_name
)
1488 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1489 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1491 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1492 demux_flush(demuxer
);
1494 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_CHAPTER
,
1496 if (ris
!= STREAM_UNSUPPORTED
)
1497 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1499 ds_fill_buffer(demuxer
->video
);
1500 resync_video_stream(sh_video
);
1504 ds_fill_buffer(demuxer
->audio
);
1505 resync_audio_stream(sh_audio
);
1507 // exit status may be ok, but main() doesn't have to seek itself
1508 // (because e.g. dvds depend on sectors, not on pts)
1512 *chapter_name
= NULL
;
1514 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1515 &num_chapters
) == STREAM_UNSUPPORTED
)
1518 *chapter_name
= talloc_size(NULL
, 16);
1519 sprintf(*chapter_name
, " of %3d", num_chapters
);
1523 return ris
!= STREAM_UNSUPPORTED
? chapter
: -1;
1524 } else { // chapters structure is set in the demuxer
1525 if (chapter
>= demuxer
->num_chapters
)
1530 *seek_pts
= demuxer
->chapters
[chapter
].start
/ 1000.0;
1533 *chapter_name
= talloc_strdup(NULL
, demuxer
->chapters
[chapter
].name
);
1539 int demuxer_get_current_chapter(demuxer_t
*demuxer
)
1542 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1543 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_CURRENT_CHAPTER
,
1544 &chapter
) == STREAM_UNSUPPORTED
)
1547 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1548 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1550 now
= (sh_video
? sh_video
->pts
: (sh_audio
? sh_audio
->pts
: 0))
1552 for (chapter
= demuxer
->num_chapters
- 1; chapter
>= 0; --chapter
) {
1553 if (demuxer
->chapters
[chapter
].start
<= now
)
1560 char *demuxer_chapter_name(demuxer_t
*demuxer
, int chapter
)
1562 if (demuxer
->num_chapters
&& demuxer
->chapters
) {
1563 if (chapter
>= 0 && chapter
< demuxer
->num_chapters
1564 && demuxer
->chapters
[chapter
].name
)
1565 return strdup(demuxer
->chapters
[chapter
].name
);
1570 char *demuxer_chapter_display_name(demuxer_t
*demuxer
, int chapter
)
1572 char *chapter_name
= demuxer_chapter_name(demuxer
, chapter
);
1574 char *tmp
= malloc(strlen(chapter_name
) + 14);
1575 snprintf(tmp
, 63, "(%d) %s", chapter
+ 1, chapter_name
);
1579 int chapter_num
= demuxer_chapter_count(demuxer
);
1581 if (chapter_num
<= 0)
1582 sprintf(tmp
, "(%d)", chapter
+ 1);
1584 sprintf(tmp
, "(%d) of %d", chapter
+ 1, chapter_num
);
1589 float demuxer_chapter_time(demuxer_t
*demuxer
, int chapter
, float *end
)
1591 if (demuxer
->num_chapters
&& demuxer
->chapters
&& chapter
>= 0
1592 && chapter
< demuxer
->num_chapters
) {
1594 *end
= demuxer
->chapters
[chapter
].end
/ 1000.0;
1595 return demuxer
->chapters
[chapter
].start
/ 1000.0;
1600 int demuxer_chapter_count(demuxer_t
*demuxer
)
1602 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1603 int num_chapters
= 0;
1604 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1605 &num_chapters
) == STREAM_UNSUPPORTED
)
1607 return num_chapters
;
1609 return demuxer
->num_chapters
;
1612 int demuxer_angles_count(demuxer_t
*demuxer
)
1614 int ris
, angles
= -1;
1616 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_ANGLES
, &angles
);
1617 if (ris
== STREAM_UNSUPPORTED
)
1622 int demuxer_get_current_angle(demuxer_t
*demuxer
)
1624 int ris
, curr_angle
= -1;
1625 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_ANGLE
, &curr_angle
);
1626 if (ris
== STREAM_UNSUPPORTED
)
1632 int demuxer_set_angle(demuxer_t
*demuxer
, int angle
)
1634 int ris
, angles
= -1;
1635 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1636 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1638 angles
= demuxer_angles_count(demuxer
);
1639 if ((angles
< 1) || (angle
> angles
))
1642 demux_flush(demuxer
);
1644 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SET_ANGLE
, &angle
);
1645 if (ris
== STREAM_UNSUPPORTED
)
1648 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1650 ds_fill_buffer(demuxer
->video
);
1651 resync_video_stream(sh_video
);
1655 ds_fill_buffer(demuxer
->audio
);
1656 resync_audio_stream(sh_audio
);
1662 int demuxer_audio_track_by_lang(demuxer_t
*d
, char *lang
)
1665 lang
+= strspn(lang
, ",");
1666 while ((len
= strcspn(lang
, ",")) > 0) {
1667 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1668 sh_audio_t
*sh
= d
->a_streams
[i
];
1669 if (sh
&& sh
->lang
&& strncmp(sh
->lang
, lang
, len
) == 0)
1673 lang
+= strspn(lang
, ",");
1678 int demuxer_sub_track_by_lang(demuxer_t
*d
, char *lang
)
1681 lang
+= strspn(lang
, ",");
1682 while ((len
= strcspn(lang
, ",")) > 0) {
1683 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1684 sh_sub_t
*sh
= d
->s_streams
[i
];
1685 if (sh
&& sh
->lang
&& strncmp(sh
->lang
, lang
, len
) == 0)
1689 lang
+= strspn(lang
, ",");
1694 int demuxer_default_audio_track(demuxer_t
*d
)
1697 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1698 sh_audio_t
*sh
= d
->a_streams
[i
];
1699 if (sh
&& sh
->default_track
)
1702 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1703 sh_audio_t
*sh
= d
->a_streams
[i
];
1710 int demuxer_default_sub_track(demuxer_t
*d
)
1713 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1714 sh_sub_t
*sh
= d
->s_streams
[i
];
1715 if (sh
&& sh
->default_track
)