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"
47 #include "libavcodec/avcodec.h"
48 #if MP_INPUT_BUFFER_PADDING_SIZE < FF_INPUT_BUFFER_PADDING_SIZE
49 #error MP_INPUT_BUFFER_PADDING_SIZE is too small!
53 static void clear_parser(sh_common_t
*sh
);
56 extern const demuxer_desc_t demuxer_desc_rawaudio
;
57 extern const demuxer_desc_t demuxer_desc_rawvideo
;
58 extern const demuxer_desc_t demuxer_desc_tv
;
59 extern const demuxer_desc_t demuxer_desc_mf
;
60 extern const demuxer_desc_t demuxer_desc_avi
;
61 extern const demuxer_desc_t demuxer_desc_y4m
;
62 extern const demuxer_desc_t demuxer_desc_asf
;
63 extern const demuxer_desc_t demuxer_desc_real
;
64 extern const demuxer_desc_t demuxer_desc_smjpeg
;
65 extern const demuxer_desc_t demuxer_desc_matroska
;
66 extern const demuxer_desc_t demuxer_desc_realaudio
;
67 extern const demuxer_desc_t demuxer_desc_vqf
;
68 extern const demuxer_desc_t demuxer_desc_mov
;
69 extern const demuxer_desc_t demuxer_desc_vivo
;
70 extern const demuxer_desc_t demuxer_desc_fli
;
71 extern const demuxer_desc_t demuxer_desc_film
;
72 extern const demuxer_desc_t demuxer_desc_roq
;
73 extern const demuxer_desc_t demuxer_desc_gif
;
74 extern const demuxer_desc_t demuxer_desc_ogg
;
75 extern const demuxer_desc_t demuxer_desc_avs
;
76 extern const demuxer_desc_t demuxer_desc_pva
;
77 extern const demuxer_desc_t demuxer_desc_nsv
;
78 extern const demuxer_desc_t demuxer_desc_mpeg_ts
;
79 extern const demuxer_desc_t demuxer_desc_lmlm4
;
80 extern const demuxer_desc_t demuxer_desc_mpeg_ps
;
81 extern const demuxer_desc_t demuxer_desc_mpeg_pes
;
82 extern const demuxer_desc_t demuxer_desc_mpeg_es
;
83 extern const demuxer_desc_t demuxer_desc_mpeg_gxf
;
84 extern const demuxer_desc_t demuxer_desc_mpeg4_es
;
85 extern const demuxer_desc_t demuxer_desc_h264_es
;
86 extern const demuxer_desc_t demuxer_desc_rawdv
;
87 extern const demuxer_desc_t demuxer_desc_mpc
;
88 extern const demuxer_desc_t demuxer_desc_audio
;
89 extern const demuxer_desc_t demuxer_desc_xmms
;
90 extern const demuxer_desc_t demuxer_desc_mpeg_ty
;
91 extern const demuxer_desc_t demuxer_desc_rtp
;
92 extern const demuxer_desc_t demuxer_desc_rtp_nemesi
;
93 extern const demuxer_desc_t demuxer_desc_lavf
;
94 extern const demuxer_desc_t demuxer_desc_lavf_preferred
;
95 extern const demuxer_desc_t demuxer_desc_aac
;
96 extern const demuxer_desc_t demuxer_desc_nut
;
97 extern const demuxer_desc_t demuxer_desc_mng
;
99 /* Please do not add any new demuxers here. If you want to implement a new
100 * demuxer, add it to libavformat, except for wrappers around external
101 * libraries and demuxers requiring binary support. */
103 const demuxer_desc_t
*const demuxer_list
[] = {
104 &demuxer_desc_rawaudio
,
105 &demuxer_desc_rawvideo
,
111 &demuxer_desc_lavf_preferred
,
118 &demuxer_desc_smjpeg
,
119 &demuxer_desc_matroska
,
120 &demuxer_desc_realaudio
,
130 #ifdef CONFIG_OGGVORBIS
133 #ifdef CONFIG_WIN32DLL
137 &demuxer_desc_mpeg_ts
,
139 &demuxer_desc_mpeg_ps
,
140 &demuxer_desc_mpeg_pes
,
141 &demuxer_desc_mpeg_es
,
142 &demuxer_desc_mpeg_gxf
,
143 &demuxer_desc_mpeg4_es
,
144 &demuxer_desc_h264_es
,
146 &demuxer_desc_mpeg_ty
,
147 #ifdef CONFIG_LIVE555
150 #ifdef CONFIG_LIBNEMESI
151 &demuxer_desc_rtp_nemesi
,
156 #ifdef CONFIG_MUSEPACK
159 #ifdef CONFIG_LIBDV095
172 /* Please do not add any new demuxers here. If you want to implement a new
173 * demuxer, add it to libavformat, except for wrappers around external
174 * libraries and demuxers requiring binary support. */
178 struct demux_packet
*new_demux_packet(size_t len
)
180 if (len
> 1000000000) {
181 mp_msg(MSGT_DEMUXER
, MSGL_FATAL
, "Attempt to allocate demux packet "
185 struct demux_packet
*dp
= malloc(sizeof(struct demux_packet
));
188 dp
->pts
= MP_NOPTS_VALUE
;
190 dp
->stream_pts
= MP_NOPTS_VALUE
;
197 dp
->buffer
= malloc(len
+ MP_INPUT_BUFFER_PADDING_SIZE
);
199 mp_msg(MSGT_DEMUXER
, MSGL_FATAL
, "Memory allocation failure!\n");
202 memset(dp
->buffer
+ len
, 0, 8);
207 void resize_demux_packet(struct demux_packet
*dp
, size_t len
)
209 if (len
> 1000000000) {
210 mp_msg(MSGT_DEMUXER
, MSGL_FATAL
, "Attempt to realloc demux packet "
215 dp
->buffer
= realloc(dp
->buffer
, len
+ 8);
217 mp_msg(MSGT_DEMUXER
, MSGL_FATAL
, "Memory allocation failure!\n");
220 memset(dp
->buffer
+ len
, 0, 8);
228 struct demux_packet
*clone_demux_packet(struct demux_packet
*pack
)
230 struct demux_packet
*dp
= malloc(sizeof(struct demux_packet
));
232 pack
= pack
->master
; // find the master
233 memcpy(dp
, pack
, sizeof(struct demux_packet
));
241 void free_demux_packet(struct demux_packet
*dp
)
243 if (dp
->master
== NULL
) { //dp is a master packet
245 if (dp
->refcount
== 0) {
252 free_demux_packet(dp
->master
);
256 void free_demuxer_stream(demux_stream_t
*ds
)
262 demux_stream_t
*new_demuxer_stream(struct demuxer
*demuxer
, int id
)
264 demux_stream_t
*ds
= malloc(sizeof(demux_stream_t
));
265 *ds
= (demux_stream_t
){
275 * Get demuxer description structure for a given demuxer type
277 * @param file_format type of the demuxer
278 * @return structure for the demuxer, NULL if not found
280 static const demuxer_desc_t
*get_demuxer_desc_from_type(int file_format
)
284 for (i
= 0; demuxer_list
[i
]; i
++)
285 if (file_format
== demuxer_list
[i
]->type
)
286 return demuxer_list
[i
];
292 demuxer_t
*new_demuxer(struct MPOpts
*opts
, stream_t
*stream
, int type
,
293 int a_id
, int v_id
, int s_id
, char *filename
)
295 struct demuxer
*d
= talloc_zero(NULL
, struct demuxer
);
297 d
->stream_pts
= MP_NOPTS_VALUE
;
298 d
->reference_clock
= MP_NOPTS_VALUE
;
299 d
->movi_start
= stream
->start_pos
;
300 d
->movi_end
= stream
->end_pos
;
304 d
->audio
= new_demuxer_stream(d
, a_id
);
305 d
->video
= new_demuxer_stream(d
, v_id
);
306 d
->sub
= new_demuxer_stream(d
, s_id
);
310 if (!(d
->desc
= get_demuxer_desc_from_type(type
)))
311 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
312 "BUG! Invalid demuxer type in new_demuxer(), "
313 "big troubles ahead.");
314 if (filename
) // Filename hack for avs_check_file
315 d
->filename
= strdup(filename
);
316 stream_seek(stream
, stream
->start_pos
);
320 const char *sh_sub_type2str(int type
)
323 case 't': return "text";
324 case 'm': return "movtext";
325 case 'a': return "ass";
326 case 'v': return "vobsub";
327 case 'x': return "xsub";
328 case 'b': return "dvb";
329 case 'd': return "dvb-teletext";
330 case 'p': return "hdmv pgs";
335 sh_sub_t
*new_sh_sub_sid(demuxer_t
*demuxer
, int id
, int sid
)
337 if (id
> MAX_S_STREAMS
- 1 || id
< 0) {
338 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
339 "Requested sub stream id overflow (%d > %d)\n", id
,
343 if (demuxer
->s_streams
[id
])
344 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, "Sub stream %i redefined\n", id
);
346 sh_sub_t
*sh
= calloc(1, sizeof(sh_sub_t
));
347 demuxer
->s_streams
[id
] = sh
;
349 sh
->opts
= demuxer
->opts
;
350 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SUBTITLE_ID=%d\n", sid
);
352 return demuxer
->s_streams
[id
];
355 struct sh_sub
*new_sh_sub_sid_lang(struct demuxer
*demuxer
, int id
, int sid
,
358 struct sh_sub
*sh
= new_sh_sub_sid(demuxer
, id
, sid
);
359 if (lang
&& lang
[0] && strcmp(lang
, "und")) {
360 sh
->lang
= strdup(lang
);
361 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SID_%d_LANG=%s\n", sid
, lang
);
366 static void free_sh_sub(sh_sub_t
*sh
)
368 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_sub at %p\n", sh
);
372 clear_parser((sh_common_t
*)sh
);
377 sh_audio_t
*new_sh_audio_aid(demuxer_t
*demuxer
, int id
, int aid
)
379 if (id
> MAX_A_STREAMS
- 1 || id
< 0) {
380 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
381 "Requested audio stream id overflow (%d > %d)\n", id
,
385 if (demuxer
->a_streams
[id
]) {
386 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, "WARNING: Audio stream header %d redefined.\n", id
);
388 mp_tmsg(MSGT_DEMUXER
, MSGL_V
, "==> Found audio stream: %d\n", id
);
389 sh_audio_t
*sh
= calloc(1, sizeof(sh_audio_t
));
390 demuxer
->a_streams
[id
] = sh
;
392 sh
->ds
= demuxer
->audio
;
395 sh
->sample_format
= AF_FORMAT_S16_NE
;
396 sh
->opts
= demuxer
->opts
;
397 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_ID=%d\n", aid
);
399 return demuxer
->a_streams
[id
];
402 void free_sh_audio(demuxer_t
*demuxer
, int id
)
404 sh_audio_t
*sh
= demuxer
->a_streams
[id
];
405 demuxer
->a_streams
[id
] = NULL
;
406 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_audio at %p\n", sh
);
411 clear_parser((sh_common_t
*)sh
);
416 sh_video_t
*new_sh_video_vid(demuxer_t
*demuxer
, int id
, int vid
)
418 if (id
> MAX_V_STREAMS
- 1 || id
< 0) {
419 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
420 "Requested video stream id overflow (%d > %d)\n", id
,
424 if (demuxer
->v_streams
[id
])
425 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, "WARNING: Video stream header %d redefined.\n", id
);
427 mp_tmsg(MSGT_DEMUXER
, MSGL_V
, "==> Found video stream: %d\n", id
);
428 sh_video_t
*sh
= calloc(1, sizeof *sh
);
429 demuxer
->v_streams
[id
] = sh
;
431 sh
->ds
= demuxer
->video
;
432 sh
->opts
= demuxer
->opts
;
433 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_ID=%d\n", vid
);
435 return demuxer
->v_streams
[id
];
438 void free_sh_video(sh_video_t
*sh
)
440 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_video at %p\n", sh
);
443 clear_parser((sh_common_t
*)sh
);
448 void free_demuxer(demuxer_t
*demuxer
)
451 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing %s demuxer at %p\n",
452 demuxer
->desc
->shortdesc
, demuxer
);
453 if (demuxer
->desc
->close
)
454 demuxer
->desc
->close(demuxer
);
455 // Very ugly hack to make it behave like old implementation
456 if (demuxer
->desc
->type
== DEMUXER_TYPE_DEMUXERS
)
457 goto skip_streamfree
;
459 for (i
= 0; i
< MAX_A_STREAMS
; i
++)
460 if (demuxer
->a_streams
[i
])
461 free_sh_audio(demuxer
, i
);
462 for (i
= 0; i
< MAX_V_STREAMS
; i
++)
463 if (demuxer
->v_streams
[i
])
464 free_sh_video(demuxer
->v_streams
[i
]);
465 for (i
= 0; i
< MAX_S_STREAMS
; i
++)
466 if (demuxer
->s_streams
[i
])
467 free_sh_sub(demuxer
->s_streams
[i
]);
469 free_demuxer_stream(demuxer
->audio
);
470 free_demuxer_stream(demuxer
->video
);
471 free_demuxer_stream(demuxer
->sub
);
473 free(demuxer
->filename
);
474 if (demuxer
->teletext
)
475 teletext_control(demuxer
->teletext
, TV_VBI_CONTROL_STOP
, NULL
);
476 talloc_free(demuxer
);
480 void ds_add_packet(demux_stream_t
*ds
, demux_packet_t
*dp
)
482 // append packet to DS stream:
484 ds
->bytes
+= dp
->len
;
486 // next packet in stream
490 // first packet in stream
491 ds
->first
= ds
->last
= dp
;
493 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
494 "DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
495 (ds
== ds
->demuxer
->audio
) ? "d_audio" : "d_video", dp
->len
,
496 dp
->pts
, (unsigned int) dp
->pos
, ds
->demuxer
->audio
->packs
,
497 ds
->demuxer
->video
->packs
);
501 static void allocate_parser(AVCodecContext
**avctx
, AVCodecParserContext
**parser
, unsigned format
)
503 enum CodecID codec_id
= CODEC_ID_NONE
;
508 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52, 94, 0)
509 case MKTAG('M', 'P', '4', 'L'):
510 codec_id
= CODEC_ID_AAC_LATM
;
517 case MKTAG('d', 'n', 'e', 't'):
518 case MKTAG('s', 'a', 'c', '3'):
519 codec_id
= CODEC_ID_AC3
;
521 case MKTAG('E', 'A', 'C', '3'):
522 codec_id
= CODEC_ID_EAC3
;
526 codec_id
= CODEC_ID_DTS
;
528 case MKTAG('f', 'L', 'a', 'C'):
529 codec_id
= CODEC_ID_FLAC
;
531 case MKTAG('M', 'L', 'P', ' '):
532 codec_id
= CODEC_ID_MLP
;
537 case MKTAG('.', 'm', 'p', '3'):
538 case MKTAG('M', 'P', '3', ' '):
539 case MKTAG('L', 'A', 'M', 'E'):
540 codec_id
= CODEC_ID_MP3
;
544 case MKTAG('.', 'm', 'p', '2'):
545 case MKTAG('.', 'm', 'p', '1'):
546 codec_id
= CODEC_ID_MP2
;
548 case MKTAG('T', 'R', 'H', 'D'):
549 codec_id
= CODEC_ID_TRUEHD
;
552 if (codec_id
!= CODEC_ID_NONE
) {
553 *avctx
= avcodec_alloc_context();
556 *parser
= av_parser_init(codec_id
);
562 static void get_parser(sh_common_t
*sh
, AVCodecContext
**avctx
, AVCodecParserContext
**parser
)
567 if (!sh
|| !sh
->needs_parsing
)
571 *parser
= sh
->parser
;
575 allocate_parser(avctx
, parser
, sh
->format
);
577 sh
->parser
= *parser
;
580 int ds_parse(demux_stream_t
*ds
, uint8_t **buffer
, int *len
, double pts
, off_t pos
)
582 AVCodecContext
*avctx
;
583 AVCodecParserContext
*parser
;
584 get_parser(ds
->sh
, &avctx
, &parser
);
587 return av_parser_parse2(parser
, avctx
, buffer
, len
, *buffer
, *len
, pts
, pts
, pos
);
590 static void clear_parser(sh_common_t
*sh
)
592 av_parser_close(sh
->parser
);
594 av_freep(&sh
->avctx
);
597 void ds_clear_parser(demux_stream_t
*ds
)
601 clear_parser(ds
->sh
);
605 void ds_read_packet(demux_stream_t
*ds
, stream_t
*stream
, int len
,
606 double pts
, off_t pos
, int flags
)
608 demux_packet_t
*dp
= new_demux_packet(len
);
609 len
= stream_read(stream
, dp
->buffer
, len
);
610 resize_demux_packet(dp
, len
);
614 // append packet to DS stream:
615 ds_add_packet(ds
, dp
);
619 // 0 = EOF or no stream found or invalid type
620 // 1 = successfully read a packet
622 int demux_fill_buffer(demuxer_t
*demux
, demux_stream_t
*ds
)
624 // Note: parameter 'ds' can be NULL!
625 return demux
->desc
->fill_buffer(demux
, ds
);
631 int ds_fill_buffer(demux_stream_t
*ds
)
633 demuxer_t
*demux
= ds
->demuxer
;
635 free_demux_packet(ds
->current
);
637 if (mp_msg_test(MSGT_DEMUXER
, MSGL_DBG3
)) {
638 if (ds
== demux
->audio
)
639 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
640 "ds_fill_buffer(d_audio) called\n");
641 else if (ds
== demux
->video
)
642 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
643 "ds_fill_buffer(d_video) called\n");
644 else if (ds
== demux
->sub
)
645 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
, "ds_fill_buffer(d_sub) called\n");
647 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
648 "ds_fill_buffer(unknown 0x%X) called\n", (unsigned int) ds
);
652 demux_packet_t
*p
= ds
->first
;
654 ds
->buffer
= p
->buffer
;
656 ds
->buffer_size
= p
->len
;
658 ds
->dpos
+= p
->len
; // !!!
660 if (p
->pts
!= MP_NOPTS_VALUE
) {
664 ds
->pts_bytes
+= p
->len
; // !!!
665 if (p
->stream_pts
!= MP_NOPTS_VALUE
)
666 demux
->stream_pts
= p
->stream_pts
;
667 ds
->flags
= p
->flags
;
675 /* The code below can set ds->eof to 1 when another stream runs
676 * out of buffer space. That makes sense because in that situation
677 * the calling code should not count on being able to demux more
678 * packets from this stream.
679 * If however the situation improves and we're called again
680 * despite the eof flag then it's better to clear it to avoid
686 #define MaybeNI _("Maybe you are playing a non-interleaved stream/file or the codec failed?\n" \
687 "For AVI files, try to force non-interleaved mode with the -ni option.\n")
689 if (demux
->audio
->packs
>= MAX_PACKS
690 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
691 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
692 demux
->audio
->packs
, demux
->audio
->bytes
);
693 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
696 if (demux
->video
->packs
>= MAX_PACKS
697 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
698 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
699 demux
->video
->packs
, demux
->video
->bytes
);
700 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
703 if (!demux_fill_buffer(demux
, ds
)) {
704 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
705 "ds_fill_buffer()->demux_fill_buffer() failed\n");
709 ds
->buffer_pos
= ds
->buffer_size
= 0;
711 mp_msg(MSGT_DEMUXER
, MSGL_V
,
712 "ds_fill_buffer: EOF reached (stream: %s) \n",
713 ds
== demux
->audio
? "audio" : "video");
718 int demux_read_data(demux_stream_t
*ds
, unsigned char *mem
, int len
)
723 x
= ds
->buffer_size
- ds
->buffer_pos
;
725 if (!ds_fill_buffer(ds
))
731 fast_memcpy(mem
+ bytes
, &ds
->buffer
[ds
->buffer_pos
], x
);
741 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
742 * \param mem memory to read data into, may be NULL to discard data
743 * \param maxlen maximum number of bytes to read
744 * \param read number of bytes actually read
745 * \param pattern pattern to search for (lowest 8 bits are ignored)
746 * \return whether pattern was found
748 int demux_pattern_3(demux_stream_t
*ds
, unsigned char *mem
, int maxlen
,
749 int *read
, uint32_t pattern
)
751 register uint32_t head
= 0xffffff00;
752 register uint32_t pat
= pattern
& 0xffffff00;
755 register unsigned char *ds_buf
= &ds
->buffer
[ds
->buffer_size
];
756 int len
= ds
->buffer_size
- ds
->buffer_pos
;
757 register long pos
= -len
;
758 if (unlikely(pos
>= 0)) { // buffer is empty
765 } while (++pos
&& head
!= pat
);
767 if (total_len
+ len
> maxlen
)
768 len
= maxlen
- total_len
;
769 len
= demux_read_data(ds
, mem
? &mem
[total_len
] : NULL
, len
);
771 } while ((head
!= pat
|| total_len
< 3) && total_len
< maxlen
&& !ds
->eof
);
774 return total_len
>= 3 && head
== pat
;
777 void ds_free_packs(demux_stream_t
*ds
)
779 demux_packet_t
*dp
= ds
->first
;
781 demux_packet_t
*dn
= dp
->next
;
782 free_demux_packet(dp
);
785 if (ds
->asf_packet
) {
786 // free unfinished .asf fragments:
787 free(ds
->asf_packet
->buffer
);
788 free(ds
->asf_packet
);
789 ds
->asf_packet
= NULL
;
791 ds
->first
= ds
->last
= NULL
;
792 ds
->packs
= 0; // !!!!!
795 free_demux_packet(ds
->current
);
798 ds
->buffer_pos
= ds
->buffer_size
;
803 int ds_get_packet(demux_stream_t
*ds
, unsigned char **start
)
806 if (ds
->buffer_pos
>= ds
->buffer_size
) {
807 if (!ds_fill_buffer(ds
)) {
813 len
= ds
->buffer_size
- ds
->buffer_pos
;
814 *start
= &ds
->buffer
[ds
->buffer_pos
];
815 ds
->buffer_pos
+= len
;
819 int ds_get_packet_pts(demux_stream_t
*ds
, unsigned char **start
, double *pts
)
822 *pts
= MP_NOPTS_VALUE
;
823 len
= ds_get_packet(ds
, start
);
826 // Return pts unless this read starts from the middle of a packet
827 if (len
== ds
->buffer_pos
)
828 *pts
= ds
->current
->pts
;
832 int ds_get_packet_sub(demux_stream_t
*ds
, unsigned char **start
)
835 if (ds
->buffer_pos
>= ds
->buffer_size
) {
839 if (!ds_fill_buffer(ds
))
842 len
= ds
->buffer_size
- ds
->buffer_pos
;
843 *start
= &ds
->buffer
[ds
->buffer_pos
];
844 ds
->buffer_pos
+= len
;
848 double ds_get_next_pts(demux_stream_t
*ds
)
850 demuxer_t
*demux
= ds
->demuxer
;
851 // if we have not read from the "current" packet, consider it
852 // as the next, otherwise we never get the pts for the first packet.
853 while (!ds
->first
&& (!ds
->current
|| ds
->buffer_pos
)) {
854 if (demux
->audio
->packs
>= MAX_PACKS
855 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
856 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
857 demux
->audio
->packs
, demux
->audio
->bytes
);
858 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
859 return MP_NOPTS_VALUE
;
861 if (demux
->video
->packs
>= MAX_PACKS
862 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
863 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
864 demux
->video
->packs
, demux
->video
->bytes
);
865 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
866 return MP_NOPTS_VALUE
;
868 if (!demux_fill_buffer(demux
, ds
))
869 return MP_NOPTS_VALUE
;
871 // take pts from "current" if we never read from it.
872 if (ds
->current
&& !ds
->buffer_pos
)
873 return ds
->current
->pts
;
874 return ds
->first
->pts
;
877 // ====================================================================
879 void demuxer_help(void)
883 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "Available demuxers:\n");
884 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, " demuxer: type info: (comment)\n");
885 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DEMUXERS\n");
886 for (i
= 0; demuxer_list
[i
]; i
++) {
887 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Don't display special demuxers
889 if (demuxer_list
[i
]->comment
&& strlen(demuxer_list
[i
]->comment
))
890 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s (%s)\n",
891 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
892 demuxer_list
[i
]->info
, demuxer_list
[i
]->comment
);
894 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s\n",
895 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
896 demuxer_list
[i
]->info
);
902 * Get demuxer type for a given demuxer name
904 * @param demuxer_name string with demuxer name of demuxer number
905 * @param force will be set if demuxer should be forced.
907 * @return DEMUXER_TYPE_xxx, -1 if error or not found
909 int get_demuxer_type_from_name(char *demuxer_name
, int *force
)
915 if (!demuxer_name
|| !demuxer_name
[0])
916 return DEMUXER_TYPE_UNKNOWN
;
918 *force
= demuxer_name
[0] == '+';
919 if (demuxer_name
[0] == '+')
920 demuxer_name
= &demuxer_name
[1];
921 for (i
= 0; demuxer_list
[i
]; i
++) {
922 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Can't select special demuxers from commandline
924 if (strcmp(demuxer_name
, demuxer_list
[i
]->name
) == 0)
925 return demuxer_list
[i
]->type
;
928 // No match found, try to parse name as an integer (demuxer number)
929 type_int
= strtol(demuxer_name
, &endptr
, 0);
930 if (*endptr
) // Conversion failed
932 if ((type_int
> 0) && (type_int
<= DEMUXER_TYPE_MAX
))
933 return (int) type_int
;
938 static struct demuxer
*open_given_type(struct MPOpts
*opts
,
939 const struct demuxer_desc
*desc
,
940 struct stream
*stream
, bool force
,
941 int audio_id
, int video_id
, int sub_id
,
944 struct demuxer
*demuxer
;
946 demuxer
= new_demuxer(opts
, stream
, desc
->type
, audio_id
,
947 video_id
, sub_id
, filename
);
948 if (desc
->check_file
)
949 fformat
= desc
->check_file(demuxer
);
951 fformat
= desc
->type
;
953 fformat
= desc
->type
;
956 if (fformat
== desc
->type
) {
957 if (demuxer
->filetype
)
958 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
, "Detected file format: %s (%s)\n",
959 demuxer
->filetype
, desc
->shortdesc
);
961 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
, "Detected file format: %s\n",
963 if (demuxer
->desc
->open
) {
964 struct demuxer
*demux2
= demuxer
->desc
->open(demuxer
);
966 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "Opening as detected format "
967 "\"%s\" failed.\n", desc
->shortdesc
);
970 /* At least demux_mov can return a demux_demuxers instance
971 * from open() instead of the original fed in. */
974 demuxer
->file_format
= fformat
;
977 // demux_mov can return playlist instead of mov
978 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
979 return demuxer
; // handled in mplayer.c
980 /* Internal MPEG PS demuxer check can return other MPEG subtypes
981 * which don't have their own checks; recurse to try opening as
982 * the returned type instead. */
983 free_demuxer(demuxer
);
984 desc
= get_demuxer_desc_from_type(fformat
);
986 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
987 "BUG: recursion to nonexistent file format\n");
990 return open_given_type(opts
, desc
, stream
, false, audio_id
,
991 video_id
, sub_id
, filename
);
994 free_demuxer(demuxer
);
998 static struct demuxer
*demux_open_stream(struct MPOpts
*opts
,
999 struct stream
*stream
,
1000 int file_format
, bool force
,
1001 int audio_id
, int video_id
, int sub_id
,
1004 struct demuxer
*demuxer
= NULL
;
1005 const struct demuxer_desc
*desc
;
1007 // If somebody requested a demuxer check it
1009 desc
= get_demuxer_desc_from_type(file_format
);
1011 // should only happen with obsolete -demuxer 99 numeric format
1013 demuxer
= open_given_type(opts
, desc
, stream
, force
, audio_id
,
1014 video_id
, sub_id
, filename
);
1020 // Test demuxers with safe file checks
1021 for (int i
= 0; (desc
= demuxer_list
[i
]); i
++) {
1022 if (desc
->safe_check
) {
1023 demuxer
= open_given_type(opts
, desc
, stream
, false, audio_id
,
1024 video_id
, sub_id
, filename
);
1030 // Ok. We're over the stable detectable fileformats, the next ones are
1031 // a bit fuzzy. So by default (extension_parsing==1) try extension-based
1033 if (filename
&& opts
->extension_parsing
== 1) {
1034 desc
= get_demuxer_desc_from_type(demuxer_type_by_filename(filename
));
1036 demuxer
= open_given_type(opts
, desc
, stream
, false, audio_id
,
1037 video_id
, sub_id
, filename
);
1042 // Finally try detection for demuxers with unsafe checks
1043 for (int i
= 0; (desc
= demuxer_list
[i
]); i
++) {
1044 if (!desc
->safe_check
&& desc
->check_file
) {
1045 demuxer
= open_given_type(opts
, desc
, stream
, false, audio_id
,
1046 video_id
, sub_id
, filename
);
1056 if (demuxer
->type
== DEMUXER_TYPE_PLAYLIST
)
1059 struct sh_video
*sh_video
= demuxer
->video
->sh
;
1060 if (sh_video
&& sh_video
->bih
) {
1061 int biComp
= le2me_32(sh_video
->bih
->biCompression
);
1062 mp_msg(MSGT_DEMUX
, MSGL_INFO
,
1063 "VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
1064 (char *) &biComp
, sh_video
->bih
->biWidth
,
1065 sh_video
->bih
->biHeight
, sh_video
->bih
->biBitCount
,
1066 sh_video
->fps
, sh_video
->i_bps
* 0.008f
,
1067 sh_video
->i_bps
/ 1024.0f
);
1072 demuxer_t
*demux_open(struct MPOpts
*opts
, stream_t
*vs
, int file_format
,
1073 int audio_id
, int video_id
, int dvdsub_id
,
1076 stream_t
*as
= NULL
, *ss
= NULL
;
1077 demuxer_t
*vd
, *ad
= NULL
, *sd
= NULL
;
1079 int afmt
= DEMUXER_TYPE_UNKNOWN
, sfmt
= DEMUXER_TYPE_UNKNOWN
;
1081 int audio_demuxer_type
= 0, sub_demuxer_type
= 0;
1082 int demuxer_force
= 0, audio_demuxer_force
= 0, sub_demuxer_force
= 0;
1085 get_demuxer_type_from_name(opts
->demuxer_name
, &demuxer_force
)) < 0) {
1086 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-demuxer %s does not exist.\n",
1087 opts
->demuxer_name
);
1090 if ((audio_demuxer_type
=
1091 get_demuxer_type_from_name(opts
->audio_demuxer_name
,
1092 &audio_demuxer_force
)) < 0) {
1093 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-audio-demuxer %s does not exist.\n",
1094 opts
->audio_demuxer_name
);
1095 if (opts
->audio_stream
)
1098 if ((sub_demuxer_type
=
1099 get_demuxer_type_from_name(opts
->sub_demuxer_name
,
1100 &sub_demuxer_force
)) < 0) {
1101 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-sub-demuxer %s does not exist.\n",
1102 opts
->sub_demuxer_name
);
1103 if (opts
->sub_stream
)
1107 if (opts
->audio_stream
) {
1108 as
= open_stream(opts
->audio_stream
, 0, &afmt
);
1110 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "Cannot open audio stream: %s\n",
1111 opts
->audio_stream
);
1114 if (opts
->audio_stream_cache
) {
1115 if (!stream_enable_cache
1116 (as
, opts
->audio_stream_cache
* 1024,
1117 opts
->audio_stream_cache
* 1024 *
1118 (opts
->stream_cache_min_percent
/ 100.0),
1119 opts
->audio_stream_cache
* 1024 *
1120 (opts
->stream_cache_seek_min_percent
/ 100.0))) {
1122 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
1123 "Can't enable audio stream cache\n");
1128 if (opts
->sub_stream
) {
1129 ss
= open_stream(opts
->sub_stream
, 0, &sfmt
);
1131 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "Cannot open subtitle stream: %s\n",
1137 vd
= demux_open_stream(opts
, vs
, demuxer_type
? demuxer_type
: file_format
,
1138 demuxer_force
, opts
->audio_stream
? -2 : audio_id
,
1139 video_id
, opts
->sub_stream
? -2 : dvdsub_id
, filename
);
1148 ad
= demux_open_stream(opts
, as
,
1149 audio_demuxer_type
? audio_demuxer_type
: afmt
,
1150 audio_demuxer_force
, audio_id
, -2, -2,
1151 opts
->audio_stream
);
1153 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, "Failed to open audio demuxer: %s\n",
1154 opts
->audio_stream
);
1156 } else if (ad
->audio
->sh
1157 && ((sh_audio_t
*) ad
->audio
->sh
)->format
== 0x55) // MP3
1158 opts
->hr_mp3_seek
= 1; // Enable high res seeking
1161 sd
= demux_open_stream(opts
, ss
,
1162 sub_demuxer_type
? sub_demuxer_type
: sfmt
,
1163 sub_demuxer_force
, -2, -2, dvdsub_id
,
1166 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
,
1167 "Failed to open subtitle demuxer: %s\n", opts
->sub_stream
);
1173 res
= new_demuxers_demuxer(vd
, ad
, sd
);
1175 res
= new_demuxers_demuxer(vd
, ad
, vd
);
1177 res
= new_demuxers_demuxer(vd
, vd
, sd
);
1181 opts
->correct_pts
= opts
->user_correct_pts
;
1182 if (opts
->correct_pts
< 0)
1184 demux_control(vd
? vd
: res
, DEMUXER_CTRL_CORRECT_PTS
,
1185 NULL
) == DEMUXER_CTRL_OK
;
1190 void demux_flush(demuxer_t
*demuxer
)
1192 ds_free_packs(demuxer
->video
);
1193 ds_free_packs(demuxer
->audio
);
1194 ds_free_packs(demuxer
->sub
);
1197 int demux_seek(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
,
1200 if (!demuxer
->seekable
) {
1201 if (demuxer
->file_format
== DEMUXER_TYPE_AVI
)
1202 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "Cannot seek in raw AVI streams. (Index required, try with the -idx switch.)\n");
1204 else if (demuxer
->file_format
== DEMUXER_TYPE_TV
)
1205 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "TV input is not seekable! (Seeking will probably be for changing channels ;)\n");
1208 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "Cannot seek in this file.\n");
1211 // clear demux buffers:
1212 demux_flush(demuxer
);
1213 demuxer
->video
->eof
= 0;
1214 demuxer
->audio
->eof
= 0;
1215 demuxer
->sub
->eof
= 0;
1217 /* HACK: assume any demuxer used with these streams can cope with
1218 * the stream layer suddenly seeking to a different position under it
1219 * (nothing actually implements DEMUXER_CTRL_RESYNC now).
1221 struct stream
*stream
= demuxer
->stream
;
1222 if (stream
->type
== STREAMTYPE_DVD
|| stream
->type
== STREAMTYPE_DVDNAV
) {
1225 if (flags
& SEEK_ABSOLUTE
)
1228 if (demuxer
->stream_pts
== MP_NOPTS_VALUE
)
1230 pts
= demuxer
->stream_pts
;
1233 if (flags
& SEEK_FACTOR
) {
1235 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_TIME_LENGTH
,
1236 &tmp
) == STREAM_UNSUPPORTED
)
1238 pts
+= tmp
* rel_seek_secs
;
1240 pts
+= rel_seek_secs
;
1242 if (stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_TIME
, &pts
)
1243 != STREAM_UNSUPPORTED
) {
1244 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1250 if (demuxer
->desc
->seek
)
1251 demuxer
->desc
->seek(demuxer
, rel_seek_secs
, audio_delay
, flags
);
1256 int demux_info_add(demuxer_t
*demuxer
, const char *opt
, const char *param
)
1258 return demux_info_add_bstr(demuxer
, BSTR(opt
), BSTR(param
));
1261 int demux_info_add_bstr(demuxer_t
*demuxer
, struct bstr opt
, struct bstr param
)
1263 char **info
= demuxer
->info
;
1267 for (n
= 0; info
&& info
[2 * n
] != NULL
; n
++) {
1268 if (!bstrcasecmp(opt
, BSTR(info
[2*n
]))) {
1269 if (!bstrcmp(param
, BSTR(info
[2*n
+ 1]))) {
1270 mp_msg(MSGT_DEMUX
, MSGL_V
, "Demuxer info %.*s set to unchanged value %.*s\n",
1271 BSTR_P(opt
), BSTR_P(param
));
1274 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "Demuxer info %.*s changed to %.*s\n",
1275 BSTR_P(opt
), BSTR_P(param
));
1276 talloc_free(info
[2*n
+ 1]);
1277 info
[2*n
+ 1] = talloc_strndup(demuxer
->info
, param
.start
, param
.len
);
1282 info
= demuxer
->info
= talloc_realloc(demuxer
, info
, char *, 2 * (n
+ 2));
1283 info
[2*n
] = talloc_strndup(demuxer
->info
, opt
.start
, opt
.len
);
1284 info
[2*n
+ 1] = talloc_strndup(demuxer
->info
, param
.start
, param
.len
);
1285 memset(&info
[2 * (n
+ 1)], 0, 2 * sizeof(char *));
1290 int demux_info_print(demuxer_t
*demuxer
)
1292 char **info
= demuxer
->info
;
1298 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "Clip info:\n");
1299 for (n
= 0; info
[2 * n
] != NULL
; n
++) {
1300 mp_msg(MSGT_DEMUX
, MSGL_INFO
, " %s: %s\n", info
[2 * n
],
1302 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_NAME%d=%s\n", n
,
1304 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_VALUE%d=%s\n", n
,
1307 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_N=%d\n", n
);
1312 char *demux_info_get(demuxer_t
*demuxer
, const char *opt
)
1315 char **info
= demuxer
->info
;
1317 for (i
= 0; info
&& info
[2 * i
] != NULL
; i
++) {
1318 if (!strcasecmp(opt
, info
[2 * i
]))
1319 return info
[2 * i
+ 1];
1325 int demux_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
1328 if (demuxer
->desc
->control
)
1329 return demuxer
->desc
->control(demuxer
, cmd
, arg
);
1331 return DEMUXER_CTRL_NOTIMPL
;
1334 int demuxer_switch_audio(demuxer_t
*demuxer
, int index
)
1336 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_AUDIO
, &index
);
1337 if (res
== DEMUXER_CTRL_NOTIMPL
) {
1338 struct sh_audio
*sh_audio
= demuxer
->audio
->sh
;
1339 return sh_audio
? sh_audio
->aid
: -2;
1341 if (demuxer
->audio
->id
>= 0) {
1342 struct sh_audio
*sh_audio
= demuxer
->a_streams
[demuxer
->audio
->id
];
1343 demuxer
->audio
->sh
= sh_audio
;
1344 index
= sh_audio
->aid
; // internal MPEG demuxers don't set it right
1347 demuxer
->audio
->sh
= NULL
;
1351 int demuxer_switch_video(demuxer_t
*demuxer
, int index
)
1353 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_VIDEO
, &index
);
1354 if (res
== DEMUXER_CTRL_NOTIMPL
) {
1355 struct sh_video
*sh_video
= demuxer
->video
->sh
;
1356 return sh_video
? sh_video
->vid
: -2;
1358 if (demuxer
->video
->id
>= 0) {
1359 struct sh_video
*sh_video
= demuxer
->v_streams
[demuxer
->video
->id
];
1360 demuxer
->video
->sh
= sh_video
;
1361 index
= sh_video
->vid
; // internal MPEG demuxers don't set it right
1363 demuxer
->video
->sh
= NULL
;
1367 int demuxer_add_attachment(demuxer_t
*demuxer
, struct bstr name
,
1368 struct bstr type
, struct bstr data
)
1370 if (!(demuxer
->num_attachments
% 32))
1371 demuxer
->attachments
= talloc_realloc(demuxer
, demuxer
->attachments
,
1372 struct demux_attachment
,
1373 demuxer
->num_attachments
+ 32);
1375 struct demux_attachment
*att
=
1376 demuxer
->attachments
+ demuxer
->num_attachments
;
1377 att
->name
= talloc_strndup(demuxer
->attachments
, name
.start
, name
.len
);
1378 att
->type
= talloc_strndup(demuxer
->attachments
, type
.start
, type
.len
);
1379 att
->data
= talloc_size(demuxer
->attachments
, data
.len
);
1380 memcpy(att
->data
, data
.start
, data
.len
);
1381 att
->data_size
= data
.len
;
1383 return demuxer
->num_attachments
++;
1386 int demuxer_add_chapter(demuxer_t
*demuxer
, struct bstr name
,
1387 uint64_t start
, uint64_t end
)
1389 if (!(demuxer
->num_chapters
% 32))
1390 demuxer
->chapters
= talloc_realloc(demuxer
, demuxer
->chapters
,
1391 struct demux_chapter
,
1392 demuxer
->num_chapters
+ 32);
1394 demuxer
->chapters
[demuxer
->num_chapters
].start
= start
;
1395 demuxer
->chapters
[demuxer
->num_chapters
].end
= end
;
1396 demuxer
->chapters
[demuxer
->num_chapters
].name
= name
.len
?
1397 talloc_strndup(demuxer
->chapters
, name
.start
, name
.len
) :
1398 talloc_strdup(demuxer
->chapters
, mp_gtext("unknown"));
1400 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_ID=%d\n", demuxer
->num_chapters
);
1401 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_START=%"PRIu64
"\n", demuxer
->num_chapters
, start
/ 1000000);
1403 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_END=%"PRIu64
"\n", demuxer
->num_chapters
, end
/ 1000000);
1405 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_NAME=%.*s\n", demuxer
->num_chapters
, BSTR_P(name
));
1407 return demuxer
->num_chapters
++;
1411 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1412 * either using the demuxer->chapters structure set by the demuxer
1413 * or asking help to the stream layer (e.g. dvd)
1414 * \param chapter - chapter number wished - 0-based
1415 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1416 * \param chapter_name name of chapter found (set by this function is param is not null)
1417 * \return -1 on error, current chapter if successful
1420 int demuxer_seek_chapter(demuxer_t
*demuxer
, int chapter
, double *seek_pts
,
1421 char **chapter_name
)
1425 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1426 demux_flush(demuxer
);
1428 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_CHAPTER
,
1430 if (ris
!= STREAM_UNSUPPORTED
)
1431 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1433 // exit status may be ok, but main() doesn't have to seek itself
1434 // (because e.g. dvds depend on sectors, not on pts)
1438 *chapter_name
= NULL
;
1440 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1441 &num_chapters
) == STREAM_UNSUPPORTED
)
1444 *chapter_name
= talloc_size(NULL
, 16);
1445 sprintf(*chapter_name
, " of %3d", num_chapters
);
1449 return ris
!= STREAM_UNSUPPORTED
? chapter
: -1;
1450 } else { // chapters structure is set in the demuxer
1451 if (chapter
>= demuxer
->num_chapters
)
1456 *seek_pts
= demuxer
->chapters
[chapter
].start
/ 1e9
;
1459 *chapter_name
= talloc_strdup(NULL
, demuxer
->chapters
[chapter
].name
);
1465 int demuxer_get_current_chapter(demuxer_t
*demuxer
, double time_now
)
1468 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1469 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_CURRENT_CHAPTER
,
1470 &chapter
) == STREAM_UNSUPPORTED
)
1473 uint64_t now
= time_now
* 1e9
+ 0.5;
1474 for (chapter
= demuxer
->num_chapters
- 1; chapter
>= 0; --chapter
) {
1475 if (demuxer
->chapters
[chapter
].start
<= now
)
1482 char *demuxer_chapter_name(demuxer_t
*demuxer
, int chapter
)
1484 if (demuxer
->num_chapters
&& demuxer
->chapters
) {
1485 if (chapter
>= 0 && chapter
< demuxer
->num_chapters
1486 && demuxer
->chapters
[chapter
].name
)
1487 return strdup(demuxer
->chapters
[chapter
].name
);
1492 char *demuxer_chapter_display_name(demuxer_t
*demuxer
, int chapter
)
1494 char *chapter_name
= demuxer_chapter_name(demuxer
, chapter
);
1496 char *tmp
= malloc(strlen(chapter_name
) + 14);
1497 snprintf(tmp
, 63, "(%d) %s", chapter
+ 1, chapter_name
);
1501 int chapter_num
= demuxer_chapter_count(demuxer
);
1503 if (chapter_num
<= 0)
1504 sprintf(tmp
, "(%d)", chapter
+ 1);
1506 sprintf(tmp
, "(%d) of %d", chapter
+ 1, chapter_num
);
1511 float demuxer_chapter_time(demuxer_t
*demuxer
, int chapter
, float *end
)
1513 if (demuxer
->num_chapters
&& demuxer
->chapters
&& chapter
>= 0
1514 && chapter
< demuxer
->num_chapters
) {
1516 *end
= demuxer
->chapters
[chapter
].end
/ 1e9
;
1517 return demuxer
->chapters
[chapter
].start
/ 1e9
;
1522 int demuxer_chapter_count(demuxer_t
*demuxer
)
1524 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1525 int num_chapters
= 0;
1526 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1527 &num_chapters
) == STREAM_UNSUPPORTED
)
1529 return num_chapters
;
1531 return demuxer
->num_chapters
;
1534 int demuxer_angles_count(demuxer_t
*demuxer
)
1536 int ris
, angles
= -1;
1538 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_ANGLES
, &angles
);
1539 if (ris
== STREAM_UNSUPPORTED
)
1544 int demuxer_get_current_angle(demuxer_t
*demuxer
)
1546 int ris
, curr_angle
= -1;
1547 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_ANGLE
, &curr_angle
);
1548 if (ris
== STREAM_UNSUPPORTED
)
1554 int demuxer_set_angle(demuxer_t
*demuxer
, int angle
)
1556 int ris
, angles
= -1;
1558 angles
= demuxer_angles_count(demuxer
);
1559 if ((angles
< 1) || (angle
> angles
))
1562 demux_flush(demuxer
);
1564 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SET_ANGLE
, &angle
);
1565 if (ris
== STREAM_UNSUPPORTED
)
1568 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1573 int demuxer_audio_track_by_lang_and_default(struct demuxer
*d
, char *lang
)
1578 lang
+= strspn(lang
, ",");
1579 int len
= strcspn(lang
, ",");
1581 for (int i
= 0; i
< MAX_A_STREAMS
; i
++) {
1582 struct sh_audio
*sh
= d
->a_streams
[i
];
1583 if (sh
&& (!len
|| sh
->lang
&& strlen(sh
->lang
) == len
&&
1584 !memcmp(lang
, sh
->lang
, len
))) {
1585 if (sh
->default_track
)
1599 int demuxer_sub_track_by_lang_and_default(struct demuxer
*d
, char *lang
)
1604 lang
+= strspn(lang
, ",");
1605 int len
= strcspn(lang
, ",");
1607 for (int i
= 0; i
< MAX_S_STREAMS
; i
++) {
1608 struct sh_sub
*sh
= d
->s_streams
[i
];
1609 if (sh
&& (!len
|| sh
->lang
&& strlen(sh
->lang
) == len
&&
1610 !memcmp(lang
, sh
->lang
, len
))) {
1611 if (sh
->default_track
)