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>
34 #include "libvo/fastmemcpy.h"
36 #include "stream/stream.h"
41 #include "libaf/af_format.h"
42 #include "libmpcodecs/dec_teletext.h"
45 #include "libass/ass.h"
46 #include "libass/ass_mp.h"
49 #ifdef CONFIG_LIBAVCODEC
50 #include "libavcodec/avcodec.h"
51 #if MP_INPUT_BUFFER_PADDING_SIZE < FF_INPUT_BUFFER_PADDING_SIZE
52 #error MP_INPUT_BUFFER_PADDING_SIZE is too small!
56 // This is quite experimental, in particular it will mess up the pts values
57 // in the queue - on the other hand it might fix some issues like generating
58 // broken files with mencoder and stream copy.
59 // Better leave it disabled for now, if we find no use for it this code should
60 // just be removed again.
61 #define PARSE_ON_ADD 0
63 static void clear_parser(sh_common_t
*sh
);
64 void resync_video_stream(sh_video_t
*sh_video
);
65 void resync_audio_stream(sh_audio_t
*sh_audio
);
68 extern const demuxer_desc_t demuxer_desc_rawaudio
;
69 extern const demuxer_desc_t demuxer_desc_rawvideo
;
70 extern const demuxer_desc_t demuxer_desc_tv
;
71 extern const demuxer_desc_t demuxer_desc_mf
;
72 extern const demuxer_desc_t demuxer_desc_avi
;
73 extern const demuxer_desc_t demuxer_desc_y4m
;
74 extern const demuxer_desc_t demuxer_desc_asf
;
75 extern const demuxer_desc_t demuxer_desc_real
;
76 extern const demuxer_desc_t demuxer_desc_smjpeg
;
77 extern const demuxer_desc_t demuxer_desc_matroska
;
78 extern const demuxer_desc_t demuxer_desc_realaudio
;
79 extern const demuxer_desc_t demuxer_desc_vqf
;
80 extern const demuxer_desc_t demuxer_desc_mov
;
81 extern const demuxer_desc_t demuxer_desc_vivo
;
82 extern const demuxer_desc_t demuxer_desc_fli
;
83 extern const demuxer_desc_t demuxer_desc_film
;
84 extern const demuxer_desc_t demuxer_desc_roq
;
85 extern const demuxer_desc_t demuxer_desc_gif
;
86 extern const demuxer_desc_t demuxer_desc_ogg
;
87 extern const demuxer_desc_t demuxer_desc_avs
;
88 extern const demuxer_desc_t demuxer_desc_pva
;
89 extern const demuxer_desc_t demuxer_desc_nsv
;
90 extern const demuxer_desc_t demuxer_desc_mpeg_ts
;
91 extern const demuxer_desc_t demuxer_desc_lmlm4
;
92 extern const demuxer_desc_t demuxer_desc_mpeg_ps
;
93 extern const demuxer_desc_t demuxer_desc_mpeg_pes
;
94 extern const demuxer_desc_t demuxer_desc_mpeg_es
;
95 extern const demuxer_desc_t demuxer_desc_mpeg_gxf
;
96 extern const demuxer_desc_t demuxer_desc_mpeg4_es
;
97 extern const demuxer_desc_t demuxer_desc_h264_es
;
98 extern const demuxer_desc_t demuxer_desc_rawdv
;
99 extern const demuxer_desc_t demuxer_desc_mpc
;
100 extern const demuxer_desc_t demuxer_desc_audio
;
101 extern const demuxer_desc_t demuxer_desc_xmms
;
102 extern const demuxer_desc_t demuxer_desc_mpeg_ty
;
103 extern const demuxer_desc_t demuxer_desc_rtp
;
104 extern const demuxer_desc_t demuxer_desc_rtp_nemesi
;
105 extern const demuxer_desc_t demuxer_desc_lavf
;
106 extern const demuxer_desc_t demuxer_desc_lavf_preferred
;
107 extern const demuxer_desc_t demuxer_desc_aac
;
108 extern const demuxer_desc_t demuxer_desc_nut
;
109 extern const demuxer_desc_t demuxer_desc_mng
;
111 /* Please do not add any new demuxers here. If you want to implement a new
112 * demuxer, add it to libavformat, except for wrappers around external
113 * libraries and demuxers requiring binary support. */
115 const demuxer_desc_t
*const demuxer_list
[] = {
116 &demuxer_desc_rawaudio
,
117 &demuxer_desc_rawvideo
,
122 #ifdef CONFIG_LIBAVFORMAT
123 &demuxer_desc_lavf_preferred
,
130 &demuxer_desc_smjpeg
,
131 &demuxer_desc_matroska
,
132 &demuxer_desc_realaudio
,
142 #ifdef CONFIG_OGGVORBIS
145 #ifdef CONFIG_WIN32DLL
149 &demuxer_desc_mpeg_ts
,
151 &demuxer_desc_mpeg_ps
,
152 &demuxer_desc_mpeg_pes
,
153 &demuxer_desc_mpeg_es
,
154 &demuxer_desc_mpeg_gxf
,
155 &demuxer_desc_mpeg4_es
,
156 &demuxer_desc_h264_es
,
158 &demuxer_desc_mpeg_ty
,
159 #ifdef CONFIG_LIVE555
162 #ifdef CONFIG_LIBNEMESI
163 &demuxer_desc_rtp_nemesi
,
165 #ifdef CONFIG_LIBAVFORMAT
168 #ifdef CONFIG_MUSEPACK
171 #ifdef CONFIG_LIBDV095
184 /* Please do not add any new demuxers here. If you want to implement a new
185 * demuxer, add it to libavformat, except for wrappers around external
186 * libraries and demuxers requiring binary support. */
190 void free_demuxer_stream(demux_stream_t
*ds
)
196 demux_stream_t
*new_demuxer_stream(struct demuxer
*demuxer
, int id
)
198 demux_stream_t
*ds
= malloc(sizeof(demux_stream_t
));
199 *ds
= (demux_stream_t
){
209 * Get demuxer description structure for a given demuxer type
211 * @param file_format type of the demuxer
212 * @return structure for the demuxer, NULL if not found
214 static const demuxer_desc_t
*get_demuxer_desc_from_type(int file_format
)
218 for (i
= 0; demuxer_list
[i
]; i
++)
219 if (file_format
== demuxer_list
[i
]->type
)
220 return demuxer_list
[i
];
226 demuxer_t
*new_demuxer(stream_t
*stream
, int type
, int a_id
, int v_id
,
227 int s_id
, char *filename
)
229 demuxer_t
*d
= malloc(sizeof(demuxer_t
));
230 memset(d
, 0, sizeof(demuxer_t
));
232 d
->stream_pts
= MP_NOPTS_VALUE
;
233 d
->reference_clock
= MP_NOPTS_VALUE
;
234 d
->movi_start
= stream
->start_pos
;
235 d
->movi_end
= stream
->end_pos
;
239 d
->audio
= new_demuxer_stream(d
, a_id
);
240 d
->video
= new_demuxer_stream(d
, v_id
);
241 d
->sub
= new_demuxer_stream(d
, s_id
);
244 if (!(d
->desc
= get_demuxer_desc_from_type(type
)))
245 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
246 "BUG! Invalid demuxer type in new_demuxer(), "
247 "big troubles ahead.");
248 if (filename
) // Filename hack for avs_check_file
249 d
->filename
= strdup(filename
);
251 stream_seek(stream
, stream
->start_pos
);
255 extern int dvdsub_id
;
257 sh_sub_t
*new_sh_sub_sid(demuxer_t
*demuxer
, int id
, int sid
)
259 if (id
> MAX_S_STREAMS
- 1 || id
< 0) {
260 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
261 "Requested sub stream id overflow (%d > %d)\n", id
,
265 if (demuxer
->s_streams
[id
])
266 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, "Sub stream %i redefined\n", id
);
268 sh_sub_t
*sh
= calloc(1, sizeof(sh_sub_t
));
269 demuxer
->s_streams
[id
] = sh
;
271 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SUBTITLE_ID=%d\n", sid
);
273 if (sid
== dvdsub_id
) {
274 demuxer
->sub
->id
= id
;
275 demuxer
->sub
->sh
= demuxer
->s_streams
[id
];
277 return demuxer
->s_streams
[id
];
280 static void free_sh_sub(sh_sub_t
*sh
)
282 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_sub at %p\n", sh
);
286 ass_free_track(sh
->ass_track
);
289 #ifdef CONFIG_LIBAVCODEC
290 clear_parser((sh_common_t
*)sh
);
295 sh_audio_t
*new_sh_audio_aid(demuxer_t
*demuxer
, int id
, int aid
)
297 if (id
> MAX_A_STREAMS
- 1 || id
< 0) {
298 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
299 "Requested audio stream id overflow (%d > %d)\n", id
,
303 if (demuxer
->a_streams
[id
])
304 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, MSGTR_AudioStreamRedefined
, id
);
306 sh_audio_t
*sh
= calloc(1, sizeof(sh_audio_t
));
307 mp_msg(MSGT_DEMUXER
, MSGL_V
, MSGTR_FoundAudioStream
, id
);
308 demuxer
->a_streams
[id
] = sh
;
310 sh
->ds
= demuxer
->audio
;
313 sh
->sample_format
= AF_FORMAT_S16_NE
;
314 sh
->audio_out_minsize
= 8192; /* default size, maybe not enough for Win32/ACM */
315 sh
->pts
= MP_NOPTS_VALUE
;
316 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_ID=%d\n", aid
);
318 return demuxer
->a_streams
[id
];
321 void free_sh_audio(demuxer_t
*demuxer
, int id
)
323 sh_audio_t
*sh
= demuxer
->a_streams
[id
];
324 demuxer
->a_streams
[id
] = NULL
;
325 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_audio at %p\n", sh
);
329 #ifdef CONFIG_LIBAVCODEC
330 clear_parser((sh_common_t
*)sh
);
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_msg(MSGT_DEMUXER
, MSGL_WARN
, MSGTR_VideoStreamRedefined
, id
);
346 sh_video_t
*sh
= calloc(1, sizeof(sh_video_t
));
347 mp_msg(MSGT_DEMUXER
, MSGL_V
, MSGTR_FoundVideoStream
, id
);
348 demuxer
->v_streams
[id
] = sh
;
350 sh
->ds
= demuxer
->video
;
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
->chapters
) {
398 for (i
= 0; i
< demuxer
->num_chapters
; i
++)
399 free(demuxer
->chapters
[i
].name
);
400 free(demuxer
->chapters
);
402 if (demuxer
->attachments
) {
403 for (i
= 0; i
< demuxer
->num_attachments
; i
++) {
404 free(demuxer
->attachments
[i
].name
);
405 free(demuxer
->attachments
[i
].type
);
406 free(demuxer
->attachments
[i
].data
);
408 free(demuxer
->attachments
);
410 if (demuxer
->teletext
)
411 teletext_control(demuxer
->teletext
, TV_VBI_CONTROL_STOP
, NULL
);
416 static void ds_add_packet_internal(demux_stream_t
*ds
, demux_packet_t
*dp
)
418 // append packet to DS stream:
420 ds
->bytes
+= dp
->len
;
422 // next packet in stream
426 // first packet in stream
427 ds
->first
= ds
->last
= dp
;
429 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
430 "DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
431 (ds
== ds
->demuxer
->audio
) ? "d_audio" : "d_video", dp
->len
,
432 dp
->pts
, (unsigned int) dp
->pos
, ds
->demuxer
->audio
->packs
,
433 ds
->demuxer
->video
->packs
);
436 #ifdef CONFIG_LIBAVCODEC
437 static void allocate_parser(AVCodecContext
**avctx
, AVCodecParserContext
**parser
, unsigned format
)
439 enum CodecID codec_id
= CODEC_ID_NONE
;
440 extern int avcodec_initialized
;
441 if (!avcodec_initialized
) {
443 avcodec_register_all();
444 avcodec_initialized
= 1;
450 case MKTAG('d', 'n', 'e', 't'):
451 case MKTAG('s', 'a', 'c', '3'):
452 codec_id
= CODEC_ID_AC3
;
454 case MKTAG('E', 'A', 'C', '3'):
455 codec_id
= CODEC_ID_EAC3
;
459 codec_id
= CODEC_ID_DTS
;
461 case MKTAG('M', 'L', 'P', ' '):
462 codec_id
= CODEC_ID_MLP
;
466 case MKTAG('.', 'm', 'p', '3'):
467 case MKTAG('M', 'P', 'E', ' '):
468 case MKTAG('L', 'A', 'M', 'E'):
469 codec_id
= CODEC_ID_MP3
;
472 case MKTAG('.', 'm', 'p', '2'):
473 case MKTAG('.', 'm', 'p', '1'):
474 codec_id
= CODEC_ID_MP2
;
476 case MKTAG('T', 'R', 'H', 'D'):
477 codec_id
= CODEC_ID_TRUEHD
;
480 if (codec_id
!= CODEC_ID_NONE
) {
481 *avctx
= avcodec_alloc_context();
484 *parser
= av_parser_init(codec_id
);
490 static void get_parser(sh_common_t
*sh
, AVCodecContext
**avctx
, AVCodecParserContext
**parser
)
495 if (!sh
|| !sh
->needs_parsing
)
499 *parser
= sh
->parser
;
503 allocate_parser(avctx
, parser
, sh
->format
);
505 sh
->parser
= *parser
;
508 int ds_parse(demux_stream_t
*ds
, uint8_t **buffer
, int *len
, double pts
, off_t pos
)
510 AVCodecContext
*avctx
;
511 AVCodecParserContext
*parser
;
512 get_parser(ds
->sh
, &avctx
, &parser
);
515 return av_parser_parse2(parser
, avctx
, buffer
, len
, *buffer
, *len
, pts
, pts
, pos
);
518 static void clear_parser(sh_common_t
*sh
)
520 av_parser_close(sh
->parser
);
522 av_freep(&sh
->avctx
);
525 void ds_clear_parser(demux_stream_t
*ds
)
529 clear_parser(ds
->sh
);
533 void ds_add_packet(demux_stream_t
*ds
, demux_packet_t
*dp
)
535 #if PARSE_ON_ADD && defined(CONFIG_LIBAVCODEC)
539 uint8_t *parsed_start
= dp
->buffer
+ pos
;
540 int parsed_len
= len
;
541 int consumed
= ds_parse(ds
->sh
, &parsed_start
, &parsed_len
, dp
->pts
, dp
->pos
);
544 if (parsed_start
== dp
->buffer
&& parsed_len
== dp
->len
) {
545 ds_add_packet_internal(ds
, dp
);
546 } else if (parsed_len
) {
547 demux_packet_t
*dp2
= new_demux_packet(parsed_len
);
549 dp2
->pts
= dp
->pts
; // should be parser->pts but that works badly
550 memcpy(dp2
->buffer
, parsed_start
, parsed_len
);
551 ds_add_packet_internal(ds
, dp2
);
555 ds_add_packet_internal(ds
, dp
);
559 void ds_read_packet(demux_stream_t
*ds
, stream_t
*stream
, int len
,
560 double pts
, off_t pos
, int flags
)
562 demux_packet_t
*dp
= new_demux_packet(len
);
563 len
= stream_read(stream
, dp
->buffer
, len
);
564 resize_demux_packet(dp
, len
);
568 // append packet to DS stream:
569 ds_add_packet(ds
, dp
);
573 // 0 = EOF or no stream found or invalid type
574 // 1 = successfully read a packet
576 int demux_fill_buffer(demuxer_t
*demux
, demux_stream_t
*ds
)
578 // Note: parameter 'ds' can be NULL!
579 return demux
->desc
->fill_buffer(demux
, ds
);
585 #define MAX_ACUMULATED_PACKETS 64
586 int ds_fill_buffer(demux_stream_t
*ds
)
588 demuxer_t
*demux
= ds
->demuxer
;
590 free_demux_packet(ds
->current
);
592 if (mp_msg_test(MSGT_DEMUXER
, MSGL_DBG3
)) {
593 if (ds
== demux
->audio
)
594 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
595 "ds_fill_buffer(d_audio) called\n");
596 else if (ds
== demux
->video
)
597 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
598 "ds_fill_buffer(d_video) called\n");
599 else if (ds
== demux
->sub
)
600 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
, "ds_fill_buffer(d_sub) called\n");
602 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
603 "ds_fill_buffer(unknown 0x%X) called\n", (unsigned int) ds
);
607 demux_packet_t
*p
= ds
->first
;
609 if (demux
->reference_clock
!= MP_NOPTS_VALUE
) {
610 if ( p
->pts
!= MP_NOPTS_VALUE
611 && p
->pts
> demux
->reference_clock
612 && ds
->packs
< MAX_ACUMULATED_PACKETS
) {
613 if (demux_fill_buffer(demux
, ds
))
619 ds
->buffer
= p
->buffer
;
621 ds
->buffer_size
= p
->len
;
623 ds
->dpos
+= p
->len
; // !!!
625 if (p
->pts
!= MP_NOPTS_VALUE
) {
629 ds
->pts_bytes
+= p
->len
; // !!!
630 if (p
->stream_pts
!= MP_NOPTS_VALUE
)
631 demux
->stream_pts
= p
->stream_pts
;
632 ds
->flags
= p
->flags
;
642 if (demux
->audio
->packs
>= MAX_PACKS
643 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
644 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_TooManyAudioInBuffer
,
645 demux
->audio
->packs
, demux
->audio
->bytes
);
646 mp_msg(MSGT_DEMUXER
, MSGL_HINT
, MSGTR_MaybeNI
);
649 if (demux
->video
->packs
>= MAX_PACKS
650 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
651 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_TooManyVideoInBuffer
,
652 demux
->video
->packs
, demux
->video
->bytes
);
653 mp_msg(MSGT_DEMUXER
, MSGL_HINT
, MSGTR_MaybeNI
);
656 if (!demux_fill_buffer(demux
, ds
)) {
657 #if PARSE_ON_ADD && defined(CONFIG_LIBAVCODEC)
658 uint8_t *parsed_start
= NULL
;
660 ds_parse(ds
->sh
, &parsed_start
, &parsed_len
, MP_NOPTS_VALUE
, 0);
662 demux_packet_t
*dp2
= new_demux_packet(parsed_len
);
663 dp2
->pts
= MP_NOPTS_VALUE
;
664 memcpy(dp2
->buffer
, parsed_start
, parsed_len
);
665 ds_add_packet_internal(ds
, dp2
);
669 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
670 "ds_fill_buffer()->demux_fill_buffer() failed\n");
674 ds
->buffer_pos
= ds
->buffer_size
= 0;
676 mp_msg(MSGT_DEMUXER
, MSGL_V
,
677 "ds_fill_buffer: EOF reached (stream: %s) \n",
678 ds
== demux
->audio
? "audio" : "video");
683 int demux_read_data(demux_stream_t
*ds
, unsigned char *mem
, int len
)
688 x
= ds
->buffer_size
- ds
->buffer_pos
;
690 if (!ds_fill_buffer(ds
))
696 fast_memcpy(mem
+ bytes
, &ds
->buffer
[ds
->buffer_pos
], x
);
706 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
707 * \param mem memory to read data into, may be NULL to discard data
708 * \param maxlen maximum number of bytes to read
709 * \param read number of bytes actually read
710 * \param pattern pattern to search for (lowest 8 bits are ignored)
711 * \return whether pattern was found
713 int demux_pattern_3(demux_stream_t
*ds
, unsigned char *mem
, int maxlen
,
714 int *read
, uint32_t pattern
)
716 register uint32_t head
= 0xffffff00;
717 register uint32_t pat
= pattern
& 0xffffff00;
720 register unsigned char *ds_buf
= &ds
->buffer
[ds
->buffer_size
];
721 int len
= ds
->buffer_size
- ds
->buffer_pos
;
722 register long pos
= -len
;
723 if (unlikely(pos
>= 0)) { // buffer is empty
730 } while (++pos
&& head
!= pat
);
732 if (total_len
+ len
> maxlen
)
733 len
= maxlen
- total_len
;
734 len
= demux_read_data(ds
, mem
? &mem
[total_len
] : NULL
, len
);
736 } while ((head
!= pat
|| total_len
< 3) && total_len
< maxlen
&& !ds
->eof
);
739 return total_len
>= 3 && head
== pat
;
742 void ds_free_packs(demux_stream_t
*ds
)
744 demux_packet_t
*dp
= ds
->first
;
746 demux_packet_t
*dn
= dp
->next
;
747 free_demux_packet(dp
);
750 if (ds
->asf_packet
) {
751 // free unfinished .asf fragments:
752 free(ds
->asf_packet
->buffer
);
753 free(ds
->asf_packet
);
754 ds
->asf_packet
= NULL
;
756 ds
->first
= ds
->last
= NULL
;
757 ds
->packs
= 0; // !!!!!
760 free_demux_packet(ds
->current
);
763 ds
->buffer_pos
= ds
->buffer_size
;
768 int ds_get_packet(demux_stream_t
*ds
, unsigned char **start
)
771 if (ds
->buffer_pos
>= ds
->buffer_size
) {
772 if (!ds_fill_buffer(ds
)) {
778 len
= ds
->buffer_size
- ds
->buffer_pos
;
779 *start
= &ds
->buffer
[ds
->buffer_pos
];
780 ds
->buffer_pos
+= len
;
784 int ds_get_packet_pts(demux_stream_t
*ds
, unsigned char **start
, double *pts
)
787 *pts
= MP_NOPTS_VALUE
;
788 len
= ds_get_packet(ds
, start
);
791 // Return pts unless this read starts from the middle of a packet
792 if (len
== ds
->buffer_pos
)
793 *pts
= ds
->current
->pts
;
797 int ds_get_packet_sub(demux_stream_t
*ds
, unsigned char **start
)
800 if (ds
->buffer_pos
>= ds
->buffer_size
) {
804 if (!ds_fill_buffer(ds
))
807 len
= ds
->buffer_size
- ds
->buffer_pos
;
808 *start
= &ds
->buffer
[ds
->buffer_pos
];
809 ds
->buffer_pos
+= len
;
813 double ds_get_next_pts(demux_stream_t
*ds
)
815 demuxer_t
*demux
= ds
->demuxer
;
817 if (demux
->audio
->packs
>= MAX_PACKS
818 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
819 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_TooManyAudioInBuffer
,
820 demux
->audio
->packs
, demux
->audio
->bytes
);
821 mp_msg(MSGT_DEMUXER
, MSGL_HINT
, MSGTR_MaybeNI
);
822 return MP_NOPTS_VALUE
;
824 if (demux
->video
->packs
>= MAX_PACKS
825 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
826 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_TooManyVideoInBuffer
,
827 demux
->video
->packs
, demux
->video
->bytes
);
828 mp_msg(MSGT_DEMUXER
, MSGL_HINT
, MSGTR_MaybeNI
);
829 return MP_NOPTS_VALUE
;
831 if (!demux_fill_buffer(demux
, ds
))
832 return MP_NOPTS_VALUE
;
834 return ds
->first
->pts
;
837 // ====================================================================
839 void demuxer_help(void)
843 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "Available demuxers:\n");
844 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, " demuxer: type info: (comment)\n");
845 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DEMUXERS\n");
846 for (i
= 0; demuxer_list
[i
]; i
++) {
847 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Don't display special demuxers
849 if (demuxer_list
[i
]->comment
&& strlen(demuxer_list
[i
]->comment
))
850 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s (%s)\n",
851 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
852 demuxer_list
[i
]->info
, demuxer_list
[i
]->comment
);
854 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s\n",
855 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
856 demuxer_list
[i
]->info
);
862 * Get demuxer type for a given demuxer name
864 * @param demuxer_name string with demuxer name of demuxer number
865 * @param force will be set if demuxer should be forced.
867 * @return DEMUXER_TYPE_xxx, -1 if error or not found
869 int get_demuxer_type_from_name(char *demuxer_name
, int *force
)
875 if (!demuxer_name
|| !demuxer_name
[0])
876 return DEMUXER_TYPE_UNKNOWN
;
878 *force
= demuxer_name
[0] == '+';
879 if (demuxer_name
[0] == '+')
880 demuxer_name
= &demuxer_name
[1];
881 for (i
= 0; demuxer_list
[i
]; i
++) {
882 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Can't select special demuxers from commandline
884 if (strcmp(demuxer_name
, demuxer_list
[i
]->name
) == 0)
885 return demuxer_list
[i
]->type
;
888 // No match found, try to parse name as an integer (demuxer number)
889 type_int
= strtol(demuxer_name
, &endptr
, 0);
890 if (*endptr
) // Conversion failed
892 if ((type_int
> 0) && (type_int
<= DEMUXER_TYPE_MAX
))
893 return (int) type_int
;
898 int extension_parsing
= 1; // 0=off 1=mixed (used only for unstable formats)
901 int user_correct_pts
= -1;
904 NOTE : Several demuxers may be opened at the same time so
905 demuxers should NEVER rely on an external var to enable them
906 self. If a demuxer can't do any auto-detection it should only use
907 file_format. The user can explicitly set file_format with the -demuxer
908 option so there is really no need for another extra var.
909 For convenience an option can be added to set file_format directly
910 to the right type (ex: rawaudio,rawvideo).
911 Also the stream can override the file_format so a demuxer which rely
912 on a special stream type can set file_format at the stream level
916 static demuxer_t
*demux_open_stream(stream_t
*stream
, int file_format
,
917 int force
, int audio_id
, int video_id
,
918 int dvdsub_id
, char *filename
)
920 demuxer_t
*demuxer
= NULL
;
922 sh_video_t
*sh_video
= NULL
;
924 const demuxer_desc_t
*demuxer_desc
;
928 // If somebody requested a demuxer check it
930 if ((demuxer_desc
= get_demuxer_desc_from_type(file_format
))) {
931 demuxer
= new_demuxer(stream
, demuxer_desc
->type
, audio_id
,
932 video_id
, dvdsub_id
, filename
);
933 if (demuxer_desc
->check_file
)
934 fformat
= demuxer_desc
->check_file(demuxer
);
935 if (force
|| !demuxer_desc
->check_file
)
936 fformat
= demuxer_desc
->type
;
938 if (fformat
== demuxer_desc
->type
) {
939 demuxer_t
*demux2
= demuxer
;
940 // Move messages to demuxer detection code?
941 mp_msg(MSGT_DEMUXER
, MSGL_INFO
,
942 MSGTR_Detected_XXX_FileFormat
,
943 demuxer_desc
->shortdesc
);
944 file_format
= fformat
;
945 if (!demuxer
->desc
->open
946 || (demux2
= demuxer
->desc
->open(demuxer
))) {
951 // Format changed after check, recurse
952 free_demuxer(demuxer
);
953 return demux_open_stream(stream
, fformat
, force
, audio_id
,
954 video_id
, dvdsub_id
, filename
);
957 // Check failed for forced demuxer, quit
958 free_demuxer(demuxer
);
962 // Test demuxers with safe file checks
963 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
964 if (demuxer_desc
->safe_check
) {
965 demuxer
= new_demuxer(stream
, demuxer_desc
->type
, audio_id
,
966 video_id
, dvdsub_id
, filename
);
967 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
968 if (fformat
== demuxer_desc
->type
) {
969 demuxer_t
*demux2
= demuxer
;
970 mp_msg(MSGT_DEMUXER
, MSGL_INFO
,
971 MSGTR_Detected_XXX_FileFormat
,
972 demuxer_desc
->shortdesc
);
973 file_format
= fformat
;
974 if (!demuxer
->desc
->open
975 || (demux2
= demuxer
->desc
->open(demuxer
))) {
980 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
981 return demuxer
; // handled in mplayer.c
982 // Format changed after check, recurse
983 free_demuxer(demuxer
);
984 demuxer
= demux_open_stream(stream
, fformat
, force
,
986 dvdsub_id
, filename
);
988 return demuxer
; // done!
989 file_format
= DEMUXER_TYPE_UNKNOWN
;
992 free_demuxer(demuxer
);
997 // If no forced demuxer perform file extension based detection
998 // Ok. We're over the stable detectable fileformats, the next ones are
999 // a bit fuzzy. So by default (extension_parsing==1) try extension-based
1001 if (file_format
== DEMUXER_TYPE_UNKNOWN
&& filename
1002 && extension_parsing
== 1) {
1003 file_format
= demuxer_type_by_filename(filename
);
1004 if (file_format
!= DEMUXER_TYPE_UNKNOWN
) {
1005 // we like recursion :)
1006 demuxer
= demux_open_stream(stream
, file_format
, force
, audio_id
,
1007 video_id
, dvdsub_id
, filename
);
1009 return demuxer
; // done!
1010 file_format
= DEMUXER_TYPE_UNKNOWN
; // continue fuzzy guessing...
1011 mp_msg(MSGT_DEMUXER
, MSGL_V
,
1012 "demuxer: continue fuzzy content-based format guessing...\n");
1015 // Try detection for all other demuxers
1016 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
1017 if (!demuxer_desc
->safe_check
&& demuxer_desc
->check_file
) {
1018 demuxer
= new_demuxer(stream
, demuxer_desc
->type
, audio_id
,
1019 video_id
, dvdsub_id
, filename
);
1020 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
1021 if (fformat
== demuxer_desc
->type
) {
1022 demuxer_t
*demux2
= demuxer
;
1023 mp_msg(MSGT_DEMUXER
, MSGL_INFO
,
1024 MSGTR_Detected_XXX_FileFormat
,
1025 demuxer_desc
->shortdesc
);
1026 file_format
= fformat
;
1027 if (!demuxer
->desc
->open
1028 || (demux2
= demuxer
->desc
->open(demuxer
))) {
1033 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
1034 return demuxer
; // handled in mplayer.c
1035 // Format changed after check, recurse
1036 free_demuxer(demuxer
);
1037 demuxer
= demux_open_stream(stream
, fformat
, force
,
1039 dvdsub_id
, filename
);
1041 return demuxer
; // done!
1042 file_format
= DEMUXER_TYPE_UNKNOWN
;
1045 free_demuxer(demuxer
);
1051 //====== File format recognized, set up these for compatibility: =========
1054 demuxer
->file_format
= file_format
;
1056 if ((sh_video
= demuxer
->video
->sh
) && sh_video
->bih
) {
1057 int biComp
= le2me_32(sh_video
->bih
->biCompression
);
1058 mp_msg(MSGT_DEMUX
, MSGL_INFO
,
1059 "VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
1060 (char *) &biComp
, sh_video
->bih
->biWidth
,
1061 sh_video
->bih
->biHeight
, sh_video
->bih
->biBitCount
,
1062 sh_video
->fps
, sh_video
->i_bps
* 0.008f
,
1063 sh_video
->i_bps
/ 1024.0f
);
1066 if (ass_enabled
&& ass_library
) {
1067 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1068 sh_sub_t
*sh
= demuxer
->s_streams
[i
];
1069 if (sh
&& sh
->type
== 'a') {
1070 sh
->ass_track
= ass_new_track(ass_library
);
1071 if (sh
->ass_track
&& sh
->extradata
)
1072 ass_process_codec_private(sh
->ass_track
, sh
->extradata
,
1074 } else if (sh
&& sh
->type
!= 'v')
1075 sh
->ass_track
= ass_default_track(ass_library
);
1082 char *audio_stream
= NULL
;
1083 char *sub_stream
= NULL
;
1084 int audio_stream_cache
= 0;
1086 char *demuxer_name
= NULL
; // parameter from -demuxer
1087 char *audio_demuxer_name
= NULL
; // parameter from -audio-demuxer
1088 char *sub_demuxer_name
= NULL
; // parameter from -sub-demuxer
1090 extern int hr_mp3_seek
;
1092 extern float stream_cache_min_percent
;
1093 extern float stream_cache_seek_min_percent
;
1095 demuxer_t
*demux_open(stream_t
*vs
, int file_format
, int audio_id
,
1096 int video_id
, int dvdsub_id
, char *filename
)
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_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_CannotOpenAudioStream
,
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_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_CannotOpenSubtitlesStream
,
1154 vd
= demux_open_stream(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(as
,
1166 audio_demuxer_type
? audio_demuxer_type
: afmt
,
1167 audio_demuxer_force
, audio_id
, -2, -2,
1170 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, MSGTR_OpeningAudioDemuxerFailed
,
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(ss
, sub_demuxer_type
? sub_demuxer_type
: sfmt
,
1179 sub_demuxer_force
, -2, -2, dvdsub_id
,
1182 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
1183 MSGTR_OpeningSubtitlesDemuxerFailed
, sub_stream
);
1189 res
= new_demuxers_demuxer(vd
, ad
, sd
);
1191 res
= new_demuxers_demuxer(vd
, ad
, vd
);
1193 res
= new_demuxers_demuxer(vd
, vd
, sd
);
1197 correct_pts
= user_correct_pts
;
1198 if (correct_pts
< 0)
1199 correct_pts
= demux_control(res
, DEMUXER_CTRL_CORRECT_PTS
, NULL
)
1205 * Do necessary reinitialization after e.g. a seek.
1206 * Do _not_ call ds_fill_buffer between the seek and this, it breaks at least
1207 * seeking with ASF demuxer.
1209 static void demux_resync(demuxer_t
*demuxer
)
1211 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1212 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1213 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1215 resync_video_stream(sh_video
);
1218 resync_audio_stream(sh_audio
);
1222 void demux_flush(demuxer_t
*demuxer
)
1225 ds_clear_parser(demuxer
->video
);
1226 ds_clear_parser(demuxer
->audio
);
1227 ds_clear_parser(demuxer
->sub
);
1229 ds_free_packs(demuxer
->video
);
1230 ds_free_packs(demuxer
->audio
);
1231 ds_free_packs(demuxer
->sub
);
1234 int demux_seek(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
,
1240 if (!demuxer
->seekable
) {
1241 if (demuxer
->file_format
== DEMUXER_TYPE_AVI
)
1242 mp_msg(MSGT_SEEK
, MSGL_WARN
, MSGTR_CantSeekRawAVI
);
1244 else if (demuxer
->file_format
== DEMUXER_TYPE_TV
)
1245 mp_msg(MSGT_SEEK
, MSGL_WARN
, MSGTR_TVInputNotSeekable
);
1248 mp_msg(MSGT_SEEK
, MSGL_WARN
, MSGTR_CantSeekFile
);
1252 demux_flush(demuxer
);
1254 demuxer
->stream
->eof
= 0;
1255 demuxer
->video
->eof
= 0;
1256 demuxer
->audio
->eof
= 0;
1258 if (flags
& SEEK_ABSOLUTE
)
1261 if (demuxer
->stream_pts
== MP_NOPTS_VALUE
)
1263 pts
= demuxer
->stream_pts
;
1266 if (flags
& SEEK_FACTOR
) {
1267 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_TIME_LENGTH
, &tmp
)
1268 == STREAM_UNSUPPORTED
)
1270 pts
+= tmp
* rel_seek_secs
;
1272 pts
+= rel_seek_secs
;
1274 if (stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_TIME
, &pts
) !=
1275 STREAM_UNSUPPORTED
) {
1276 demux_resync(demuxer
);
1281 if (demuxer
->desc
->seek
)
1282 demuxer
->desc
->seek(demuxer
, rel_seek_secs
, audio_delay
, flags
);
1284 demux_resync(demuxer
);
1289 int demux_info_add(demuxer_t
*demuxer
, const char *opt
, const char *param
)
1291 char **info
= demuxer
->info
;
1295 for (n
= 0; info
&& info
[2 * n
] != NULL
; n
++) {
1296 if (!strcasecmp(opt
, info
[2 * n
])) {
1297 if (!strcmp(param
, info
[2 * n
+ 1])) {
1298 mp_msg(MSGT_DEMUX
, MSGL_V
, "Demuxer info %s set to unchanged value %s\n", opt
, param
);
1301 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_DemuxerInfoChanged
, opt
,
1303 free(info
[2 * n
+ 1]);
1304 info
[2 * n
+ 1] = strdup(param
);
1309 info
= demuxer
->info
= realloc(info
, (2 * (n
+ 2)) * sizeof(char *));
1310 info
[2 * n
] = strdup(opt
);
1311 info
[2 * n
+ 1] = strdup(param
);
1312 memset(&info
[2 * (n
+ 1)], 0, 2 * sizeof(char *));
1317 int demux_info_print(demuxer_t
*demuxer
)
1319 char **info
= demuxer
->info
;
1325 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_ClipInfo
);
1326 for (n
= 0; info
[2 * n
] != NULL
; n
++) {
1327 mp_msg(MSGT_DEMUX
, MSGL_INFO
, " %s: %s\n", info
[2 * n
],
1329 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_NAME%d=%s\n", n
,
1331 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_VALUE%d=%s\n", n
,
1334 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_N=%d\n", n
);
1339 char *demux_info_get(demuxer_t
*demuxer
, const char *opt
)
1342 char **info
= demuxer
->info
;
1344 for (i
= 0; info
&& info
[2 * i
] != NULL
; i
++) {
1345 if (!strcasecmp(opt
, info
[2 * i
]))
1346 return info
[2 * i
+ 1];
1352 int demux_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
1355 if (demuxer
->desc
->control
)
1356 return demuxer
->desc
->control(demuxer
, cmd
, arg
);
1358 return DEMUXER_CTRL_NOTIMPL
;
1363 double demuxer_get_time_length(demuxer_t
*demuxer
)
1365 double get_time_ans
;
1366 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1367 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1368 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
1370 (demuxer
, DEMUXER_CTRL_GET_TIME_LENGTH
, (void *) &get_time_ans
) <= 0) {
1371 if (sh_video
&& sh_video
->i_bps
&& sh_audio
&& sh_audio
->i_bps
)
1372 get_time_ans
= (double) (demuxer
->movi_end
-
1373 demuxer
->movi_start
) / (sh_video
->i_bps
+
1375 else if (sh_video
&& sh_video
->i_bps
)
1376 get_time_ans
= (double) (demuxer
->movi_end
-
1377 demuxer
->movi_start
) / sh_video
->i_bps
;
1378 else if (sh_audio
&& sh_audio
->i_bps
)
1379 get_time_ans
= (double) (demuxer
->movi_end
-
1380 demuxer
->movi_start
) / sh_audio
->i_bps
;
1384 return get_time_ans
;
1388 * \brief demuxer_get_current_time() returns the time of the current play in three possible ways:
1389 * either when the stream reader satisfies STREAM_CTRL_GET_CURRENT_TIME (e.g. dvd)
1390 * or using sh_video->pts when the former method fails
1392 * \return the current play time
1394 int demuxer_get_current_time(demuxer_t
*demuxer
)
1396 double get_time_ans
= 0;
1397 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1398 if (demuxer
->stream_pts
!= MP_NOPTS_VALUE
)
1399 get_time_ans
= demuxer
->stream_pts
;
1401 get_time_ans
= sh_video
->pts
;
1402 return (int) get_time_ans
;
1405 int demuxer_get_percent_pos(demuxer_t
*demuxer
)
1408 int res
= demux_control(demuxer
, DEMUXER_CTRL_GET_PERCENT_POS
, &ans
);
1409 int len
= (demuxer
->movi_end
- demuxer
->movi_start
) / 100;
1411 off_t pos
= demuxer
->filepos
> 0 ? demuxer
->filepos
: stream_tell(demuxer
->stream
);
1413 ans
= (pos
- demuxer
->movi_start
) / len
;
1424 int demuxer_switch_audio(demuxer_t
*demuxer
, int index
)
1426 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_AUDIO
, &index
);
1427 if (res
== DEMUXER_CTRL_NOTIMPL
)
1428 index
= demuxer
->audio
->id
;
1429 if (demuxer
->audio
->id
>= 0)
1430 demuxer
->audio
->sh
= demuxer
->a_streams
[demuxer
->audio
->id
];
1432 demuxer
->audio
->sh
= NULL
;
1436 int demuxer_switch_video(demuxer_t
*demuxer
, int index
)
1438 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_VIDEO
, &index
);
1439 if (res
== DEMUXER_CTRL_NOTIMPL
)
1440 index
= demuxer
->video
->id
;
1441 if (demuxer
->video
->id
>= 0)
1442 demuxer
->video
->sh
= demuxer
->v_streams
[demuxer
->video
->id
];
1444 demuxer
->video
->sh
= NULL
;
1448 int demuxer_add_attachment(demuxer_t
*demuxer
, const char *name
,
1449 const char *type
, const void *data
, size_t size
)
1451 if (!(demuxer
->num_attachments
& 31))
1452 demuxer
->attachments
= realloc(demuxer
->attachments
,
1453 (demuxer
->num_attachments
+ 32) * sizeof(demux_attachment_t
));
1455 demuxer
->attachments
[demuxer
->num_attachments
].name
= strdup(name
);
1456 demuxer
->attachments
[demuxer
->num_attachments
].type
= strdup(type
);
1457 demuxer
->attachments
[demuxer
->num_attachments
].data
= malloc(size
);
1458 memcpy(demuxer
->attachments
[demuxer
->num_attachments
].data
, data
, size
);
1459 demuxer
->attachments
[demuxer
->num_attachments
].data_size
= size
;
1461 return demuxer
->num_attachments
++;
1464 int demuxer_add_chapter(demuxer_t
*demuxer
, const char *name
, uint64_t start
,
1467 if (demuxer
->chapters
== NULL
)
1468 demuxer
->chapters
= malloc(32 * sizeof(*demuxer
->chapters
));
1469 else if (!(demuxer
->num_chapters
% 32))
1470 demuxer
->chapters
= realloc(demuxer
->chapters
,
1471 (demuxer
->num_chapters
+ 32) *
1472 sizeof(*demuxer
->chapters
));
1474 demuxer
->chapters
[demuxer
->num_chapters
].start
= start
;
1475 demuxer
->chapters
[demuxer
->num_chapters
].end
= end
;
1476 demuxer
->chapters
[demuxer
->num_chapters
].name
= strdup(name
? name
: MSGTR_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
);
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 mode 0: relative to current main pts, 1: absolute
1494 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1495 * \param num_chapters number of chapters present (set by this function is param is not null)
1496 * \param chapter_name name of chapter found (set by this function is param is not null)
1497 * \return -1 on error, current chapter if successful
1500 int demuxer_seek_chapter(demuxer_t
*demuxer
, int chapter
, int mode
,
1501 float *seek_pts
, int *num_chapters
,
1502 char **chapter_name
)
1507 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1509 ris
= stream_control(demuxer
->stream
,
1510 STREAM_CTRL_GET_CURRENT_CHAPTER
, ¤t
);
1511 if (ris
== STREAM_UNSUPPORTED
)
1516 demux_flush(demuxer
);
1518 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_CHAPTER
,
1521 demux_resync(demuxer
);
1523 // exit status may be ok, but main() doesn't have to seek itself
1524 // (because e.g. dvds depend on sectors, not on pts)
1528 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1529 num_chapters
) == STREAM_UNSUPPORTED
)
1534 *chapter_name
= NULL
;
1535 if (num_chapters
&& *num_chapters
) {
1536 char *tmp
= malloc(16);
1538 sprintf(tmp
, " of %3d", *num_chapters
);
1539 *chapter_name
= tmp
;
1544 return ris
!= STREAM_UNSUPPORTED
? chapter
: -1;
1545 } else { // chapters structure is set in the demuxer
1546 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1547 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1549 total
= demuxer
->num_chapters
;
1551 if (mode
== 1) //absolute seeking
1553 else { //relative seeking
1555 now
= (sh_video
? sh_video
->pts
: (sh_audio
? sh_audio
->pts
: 0.))
1558 for (current
= total
- 1; current
>= 0; --current
) {
1559 demux_chapter_t
*chapter
= demuxer
->chapters
+ current
;
1560 if (chapter
->start
<= now
)
1566 if (current
>= total
)
1571 *seek_pts
= demuxer
->chapters
[current
].start
/ 1000.0;
1574 *num_chapters
= demuxer
->num_chapters
;
1577 if (demuxer
->chapters
[current
].name
)
1578 *chapter_name
= strdup(demuxer
->chapters
[current
].name
);
1580 *chapter_name
= NULL
;
1587 int demuxer_get_current_chapter(demuxer_t
*demuxer
)
1590 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1591 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_CURRENT_CHAPTER
,
1592 &chapter
) == STREAM_UNSUPPORTED
)
1595 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1596 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1598 now
= (sh_video
? sh_video
->pts
: (sh_audio
? sh_audio
->pts
: 0))
1600 for (chapter
= demuxer
->num_chapters
- 1; chapter
>= 0; --chapter
) {
1601 if (demuxer
->chapters
[chapter
].start
<= now
)
1608 char *demuxer_chapter_name(demuxer_t
*demuxer
, int chapter
)
1610 if (demuxer
->num_chapters
&& demuxer
->chapters
) {
1611 if (chapter
>= 0 && chapter
< demuxer
->num_chapters
1612 && demuxer
->chapters
[chapter
].name
)
1613 return strdup(demuxer
->chapters
[chapter
].name
);
1618 char *demuxer_chapter_display_name(demuxer_t
*demuxer
, int chapter
)
1620 char *chapter_name
= demuxer_chapter_name(demuxer
, chapter
);
1622 char *tmp
= malloc(strlen(chapter_name
) + 14);
1623 snprintf(tmp
, 63, "(%d) %s", chapter
+ 1, chapter_name
);
1627 int chapter_num
= demuxer_chapter_count(demuxer
);
1629 if (chapter_num
<= 0)
1630 sprintf(tmp
, "(%d)", chapter
+ 1);
1632 sprintf(tmp
, "(%d) of %d", chapter
+ 1, chapter_num
);
1637 float demuxer_chapter_time(demuxer_t
*demuxer
, int chapter
, float *end
)
1639 if (demuxer
->num_chapters
&& demuxer
->chapters
&& chapter
>= 0
1640 && chapter
< demuxer
->num_chapters
) {
1642 *end
= demuxer
->chapters
[chapter
].end
/ 1000.0;
1643 return demuxer
->chapters
[chapter
].start
/ 1000.0;
1648 int demuxer_chapter_count(demuxer_t
*demuxer
)
1650 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1651 int num_chapters
= 0;
1652 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1653 &num_chapters
) == STREAM_UNSUPPORTED
)
1655 return num_chapters
;
1657 return demuxer
->num_chapters
;
1660 int demuxer_angles_count(demuxer_t
*demuxer
)
1662 int ris
, angles
= -1;
1664 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_ANGLES
, &angles
);
1665 if (ris
== STREAM_UNSUPPORTED
)
1670 int demuxer_get_current_angle(demuxer_t
*demuxer
)
1672 int ris
, curr_angle
= -1;
1673 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_ANGLE
, &curr_angle
);
1674 if (ris
== STREAM_UNSUPPORTED
)
1680 int demuxer_set_angle(demuxer_t
*demuxer
, int angle
)
1682 int ris
, angles
= -1;
1684 angles
= demuxer_angles_count(demuxer
);
1685 if ((angles
< 1) || (angle
> angles
))
1688 demux_flush(demuxer
);
1690 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SET_ANGLE
, &angle
);
1691 if (ris
== STREAM_UNSUPPORTED
)
1694 demux_resync(demuxer
);
1699 int demuxer_audio_track_by_lang(demuxer_t
*d
, char *lang
)
1702 lang
+= strspn(lang
, ",");
1703 while ((len
= strcspn(lang
, ",")) > 0) {
1704 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1705 sh_audio_t
*sh
= d
->a_streams
[i
];
1706 if (sh
&& sh
->lang
&& strncmp(sh
->lang
, lang
, len
) == 0)
1710 lang
+= strspn(lang
, ",");
1715 int demuxer_sub_track_by_lang(demuxer_t
*d
, char *lang
)
1718 lang
+= strspn(lang
, ",");
1719 while ((len
= strcspn(lang
, ",")) > 0) {
1720 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1721 sh_sub_t
*sh
= d
->s_streams
[i
];
1722 if (sh
&& sh
->lang
&& strncmp(sh
->lang
, lang
, len
) == 0)
1726 lang
+= strspn(lang
, ",");
1731 int demuxer_default_audio_track(demuxer_t
*d
)
1734 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1735 sh_audio_t
*sh
= d
->a_streams
[i
];
1736 if (sh
&& sh
->default_track
)
1739 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1740 sh_audio_t
*sh
= d
->a_streams
[i
];
1747 int demuxer_default_sub_track(demuxer_t
*d
)
1750 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1751 sh_sub_t
*sh
= d
->s_streams
[i
];
1752 if (sh
&& sh
->default_track
)