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"
46 #include "libass/ass.h"
47 #include "libass/ass_mp.h"
50 #ifdef CONFIG_LIBAVCODEC
51 #include "libavcodec/avcodec.h"
52 #if MP_INPUT_BUFFER_PADDING_SIZE < FF_INPUT_BUFFER_PADDING_SIZE
53 #error MP_INPUT_BUFFER_PADDING_SIZE is too small!
57 void resync_video_stream(sh_video_t
*sh_video
);
58 void resync_audio_stream(sh_audio_t
*sh_audio
);
61 extern const demuxer_desc_t demuxer_desc_rawaudio
;
62 extern const demuxer_desc_t demuxer_desc_rawvideo
;
63 extern const demuxer_desc_t demuxer_desc_tv
;
64 extern const demuxer_desc_t demuxer_desc_mf
;
65 extern const demuxer_desc_t demuxer_desc_avi
;
66 extern const demuxer_desc_t demuxer_desc_y4m
;
67 extern const demuxer_desc_t demuxer_desc_asf
;
68 extern const demuxer_desc_t demuxer_desc_real
;
69 extern const demuxer_desc_t demuxer_desc_smjpeg
;
70 extern const demuxer_desc_t demuxer_desc_matroska
;
71 extern const demuxer_desc_t demuxer_desc_realaudio
;
72 extern const demuxer_desc_t demuxer_desc_vqf
;
73 extern const demuxer_desc_t demuxer_desc_mov
;
74 extern const demuxer_desc_t demuxer_desc_vivo
;
75 extern const demuxer_desc_t demuxer_desc_fli
;
76 extern const demuxer_desc_t demuxer_desc_film
;
77 extern const demuxer_desc_t demuxer_desc_roq
;
78 extern const demuxer_desc_t demuxer_desc_gif
;
79 extern const demuxer_desc_t demuxer_desc_ogg
;
80 extern const demuxer_desc_t demuxer_desc_avs
;
81 extern const demuxer_desc_t demuxer_desc_pva
;
82 extern const demuxer_desc_t demuxer_desc_nsv
;
83 extern const demuxer_desc_t demuxer_desc_mpeg_ts
;
84 extern const demuxer_desc_t demuxer_desc_lmlm4
;
85 extern const demuxer_desc_t demuxer_desc_mpeg_ps
;
86 extern const demuxer_desc_t demuxer_desc_mpeg_pes
;
87 extern const demuxer_desc_t demuxer_desc_mpeg_es
;
88 extern const demuxer_desc_t demuxer_desc_mpeg_gxf
;
89 extern const demuxer_desc_t demuxer_desc_mpeg4_es
;
90 extern const demuxer_desc_t demuxer_desc_h264_es
;
91 extern const demuxer_desc_t demuxer_desc_rawdv
;
92 extern const demuxer_desc_t demuxer_desc_mpc
;
93 extern const demuxer_desc_t demuxer_desc_audio
;
94 extern const demuxer_desc_t demuxer_desc_xmms
;
95 extern const demuxer_desc_t demuxer_desc_mpeg_ty
;
96 extern const demuxer_desc_t demuxer_desc_rtp
;
97 extern const demuxer_desc_t demuxer_desc_rtp_nemesi
;
98 extern const demuxer_desc_t demuxer_desc_lavf
;
99 extern const demuxer_desc_t demuxer_desc_lavf_preferred
;
100 extern const demuxer_desc_t demuxer_desc_aac
;
101 extern const demuxer_desc_t demuxer_desc_nut
;
102 extern const demuxer_desc_t demuxer_desc_mng
;
104 /* Please do not add any new demuxers here. If you want to implement a new
105 * demuxer, add it to libavformat, except for wrappers around external
106 * libraries and demuxers requiring binary support. */
108 const demuxer_desc_t
*const demuxer_list
[] = {
109 &demuxer_desc_rawaudio
,
110 &demuxer_desc_rawvideo
,
115 #ifdef CONFIG_LIBAVFORMAT
116 &demuxer_desc_lavf_preferred
,
123 &demuxer_desc_smjpeg
,
124 &demuxer_desc_matroska
,
125 &demuxer_desc_realaudio
,
135 #ifdef CONFIG_OGGVORBIS
138 #ifdef CONFIG_WIN32DLL
142 &demuxer_desc_mpeg_ts
,
144 &demuxer_desc_mpeg_ps
,
145 &demuxer_desc_mpeg_pes
,
146 &demuxer_desc_mpeg_es
,
147 &demuxer_desc_mpeg_gxf
,
148 &demuxer_desc_mpeg4_es
,
149 &demuxer_desc_h264_es
,
151 &demuxer_desc_mpeg_ty
,
152 #ifdef CONFIG_LIVE555
155 #ifdef CONFIG_LIBNEMESI
156 &demuxer_desc_rtp_nemesi
,
158 #ifdef CONFIG_LIBAVFORMAT
161 #ifdef CONFIG_MUSEPACK
164 #ifdef CONFIG_LIBDV095
177 /* Please do not add any new demuxers here. If you want to implement a new
178 * demuxer, add it to libavformat, except for wrappers around external
179 * libraries and demuxers requiring binary support. */
183 void free_demuxer_stream(demux_stream_t
*ds
)
189 demux_stream_t
*new_demuxer_stream(struct demuxer
*demuxer
, int id
)
191 demux_stream_t
*ds
= malloc(sizeof(demux_stream_t
));
192 ds
->buffer_pos
= ds
->buffer_size
= 0;
203 ds
->first
= ds
->last
= ds
->current
= NULL
;
205 ds
->demuxer
= demuxer
;
208 ds
->asf_packet
= NULL
;
210 ds
->ss_mul
= ds
->ss_div
= 0;
218 * Get demuxer description structure for a given demuxer type
220 * @param file_format type of the demuxer
221 * @return structure for the demuxer, NULL if not found
223 static const demuxer_desc_t
*get_demuxer_desc_from_type(int file_format
)
227 for (i
= 0; demuxer_list
[i
]; i
++)
228 if (file_format
== demuxer_list
[i
]->type
)
229 return demuxer_list
[i
];
235 demuxer_t
*new_demuxer(struct MPOpts
*opts
, stream_t
*stream
, int type
,
236 int a_id
, int v_id
, int s_id
, char *filename
)
238 struct demuxer
*d
= talloc_zero(NULL
, struct demuxer
);
240 d
->stream_pts
= MP_NOPTS_VALUE
;
241 d
->reference_clock
= MP_NOPTS_VALUE
;
242 d
->movi_start
= stream
->start_pos
;
243 d
->movi_end
= stream
->end_pos
;
247 d
->audio
= new_demuxer_stream(d
, a_id
);
248 d
->video
= new_demuxer_stream(d
, v_id
);
249 d
->sub
= new_demuxer_stream(d
, s_id
);
253 if (!(d
->desc
= get_demuxer_desc_from_type(type
)))
254 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
255 "BUG! Invalid demuxer type in new_demuxer(), "
256 "big troubles ahead.");
257 if (filename
) // Filename hack for avs_check_file
258 d
->filename
= strdup(filename
);
259 stream_reset(stream
);
260 stream_seek(stream
, stream
->start_pos
);
264 sh_sub_t
*new_sh_sub_sid(demuxer_t
*demuxer
, int id
, int sid
)
266 if (id
> MAX_S_STREAMS
- 1 || id
< 0) {
267 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
268 "Requested sub stream id overflow (%d > %d)\n", id
,
272 if (demuxer
->s_streams
[id
])
273 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, "Sub stream %i redefined\n", id
);
275 sh_sub_t
*sh
= calloc(1, sizeof(sh_sub_t
));
276 demuxer
->s_streams
[id
] = sh
;
278 sh
->opts
= demuxer
->opts
;
279 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SUBTITLE_ID=%d\n", sid
);
281 if (sid
== demuxer
->opts
->sub_id
) {
282 demuxer
->sub
->id
= id
;
283 demuxer
->sub
->sh
= demuxer
->s_streams
[id
];
285 return demuxer
->s_streams
[id
];
288 static void free_sh_sub(sh_sub_t
*sh
)
290 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_sub at %p\n", sh
);
294 ass_free_track(sh
->ass_track
);
300 sh_audio_t
*new_sh_audio_aid(demuxer_t
*demuxer
, int id
, int aid
)
302 if (id
> MAX_A_STREAMS
- 1 || id
< 0) {
303 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
304 "Requested audio stream id overflow (%d > %d)\n", id
,
308 if (demuxer
->a_streams
[id
]) {
309 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, "WARNING: Audio stream header %d redefined.\n", id
);
311 mp_tmsg(MSGT_DEMUXER
, MSGL_V
, "==> Found audio stream: %d\n", id
);
312 sh_audio_t
*sh
= calloc(1, sizeof(sh_audio_t
));
313 demuxer
->a_streams
[id
] = sh
;
315 sh
->ds
= demuxer
->audio
;
318 sh
->sample_format
= AF_FORMAT_S16_NE
;
319 sh
->audio_out_minsize
= 8192; /* default size, maybe not enough for Win32/ACM */
320 sh
->pts
= MP_NOPTS_VALUE
;
321 sh
->opts
= demuxer
->opts
;
322 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_ID=%d\n", aid
);
324 return demuxer
->a_streams
[id
];
327 void free_sh_audio(demuxer_t
*demuxer
, int id
)
329 sh_audio_t
*sh
= demuxer
->a_streams
[id
];
330 demuxer
->a_streams
[id
] = NULL
;
331 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_audio at %p\n", sh
);
338 sh_video_t
*new_sh_video_vid(demuxer_t
*demuxer
, int id
, int vid
)
340 if (id
> MAX_V_STREAMS
- 1 || id
< 0) {
341 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
342 "Requested video stream id overflow (%d > %d)\n", id
,
346 if (demuxer
->v_streams
[id
])
347 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, "WARNING: Video stream header %d redefined.\n", id
);
349 mp_tmsg(MSGT_DEMUXER
, MSGL_V
, "==> Found video stream: %d\n", id
);
350 sh_video_t
*sh
= calloc(1, sizeof *sh
);
351 demuxer
->v_streams
[id
] = sh
;
353 sh
->ds
= demuxer
->video
;
354 sh
->opts
= demuxer
->opts
;
355 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_ID=%d\n", vid
);
357 return demuxer
->v_streams
[id
];
360 void free_sh_video(sh_video_t
*sh
)
362 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_video at %p\n", sh
);
367 void free_demuxer(demuxer_t
*demuxer
)
370 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing demuxer at %p\n",
372 if (demuxer
->desc
->close
)
373 demuxer
->desc
->close(demuxer
);
374 // Very ugly hack to make it behave like old implementation
375 if (demuxer
->desc
->type
== DEMUXER_TYPE_DEMUXERS
)
376 goto skip_streamfree
;
378 for (i
= 0; i
< MAX_A_STREAMS
; i
++)
379 if (demuxer
->a_streams
[i
])
380 free_sh_audio(demuxer
, i
);
381 for (i
= 0; i
< MAX_V_STREAMS
; i
++)
382 if (demuxer
->v_streams
[i
])
383 free_sh_video(demuxer
->v_streams
[i
]);
384 for (i
= 0; i
< MAX_S_STREAMS
; i
++)
385 if (demuxer
->s_streams
[i
])
386 free_sh_sub(demuxer
->s_streams
[i
]);
388 free_demuxer_stream(demuxer
->audio
);
389 free_demuxer_stream(demuxer
->video
);
390 free_demuxer_stream(demuxer
->sub
);
393 for (i
= 0; demuxer
->info
[i
] != NULL
; i
++)
394 free(demuxer
->info
[i
]);
397 free(demuxer
->filename
);
398 if (demuxer
->chapters
) {
399 for (i
= 0; i
< demuxer
->num_chapters
; i
++)
400 free(demuxer
->chapters
[i
].name
);
401 free(demuxer
->chapters
);
403 if (demuxer
->attachments
) {
404 for (i
= 0; i
< demuxer
->num_attachments
; i
++) {
405 free(demuxer
->attachments
[i
].name
);
406 free(demuxer
->attachments
[i
].type
);
407 free(demuxer
->attachments
[i
].data
);
409 free(demuxer
->attachments
);
411 talloc_free(demuxer
);
415 void ds_add_packet(demux_stream_t
*ds
, demux_packet_t
*dp
)
417 // append packet to DS stream:
419 ds
->bytes
+= dp
->len
;
421 // next packet in stream
425 // first packet in stream
426 ds
->first
= ds
->last
= dp
;
428 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
429 "DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
430 (ds
== ds
->demuxer
->audio
) ? "d_audio" : "d_video", dp
->len
,
431 dp
->pts
, (unsigned int) dp
->pos
, ds
->demuxer
->audio
->packs
,
432 ds
->demuxer
->video
->packs
);
435 void ds_read_packet(demux_stream_t
*ds
, stream_t
*stream
, int len
,
436 double pts
, off_t pos
, int flags
)
438 demux_packet_t
*dp
= new_demux_packet(len
);
439 len
= stream_read(stream
, dp
->buffer
, len
);
440 resize_demux_packet(dp
, len
);
444 // append packet to DS stream:
445 ds_add_packet(ds
, dp
);
449 // 0 = EOF or no stream found or invalid type
450 // 1 = successfully read a packet
452 int demux_fill_buffer(demuxer_t
*demux
, demux_stream_t
*ds
)
454 // Note: parameter 'ds' can be NULL!
455 return demux
->desc
->fill_buffer(demux
, ds
);
461 #define MAX_ACUMULATED_PACKETS 64
462 int ds_fill_buffer(demux_stream_t
*ds
)
464 demuxer_t
*demux
= ds
->demuxer
;
466 free_demux_packet(ds
->current
);
468 if (mp_msg_test(MSGT_DEMUXER
, MSGL_DBG3
)) {
469 if (ds
== demux
->audio
)
470 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
471 "ds_fill_buffer(d_audio) called\n");
472 else if (ds
== demux
->video
)
473 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
474 "ds_fill_buffer(d_video) called\n");
475 else if (ds
== demux
->sub
)
476 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
, "ds_fill_buffer(d_sub) called\n");
478 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
479 "ds_fill_buffer(unknown 0x%X) called\n", (unsigned int) ds
);
483 demux_packet_t
*p
= ds
->first
;
485 if (demux
->reference_clock
!= MP_NOPTS_VALUE
) {
486 if ( p
->pts
!= MP_NOPTS_VALUE
487 && p
->pts
> demux
->reference_clock
488 && ds
->packs
< MAX_ACUMULATED_PACKETS
) {
489 if (demux_fill_buffer(demux
, ds
))
495 ds
->buffer
= p
->buffer
;
497 ds
->buffer_size
= p
->len
;
499 ds
->dpos
+= p
->len
; // !!!
501 if (p
->pts
!= MP_NOPTS_VALUE
) {
505 ds
->pts_bytes
+= p
->len
; // !!!
506 if (p
->stream_pts
!= MP_NOPTS_VALUE
)
507 demux
->stream_pts
= p
->stream_pts
;
508 ds
->flags
= p
->flags
;
516 /* The code below can set ds->eof to 1 when another stream runs
517 * out of buffer space. That makes sense because in that situation
518 * the calling code should not count on being able to demux more
519 * packets from this stream.
520 * If however the situation improves and we're called again
521 * despite the eof flag then it's better to clear it to avoid
527 #define MaybeNI _("Maybe you are playing a non-interleaved stream/file or the codec failed?\n" \
528 "For AVI files, try to force non-interleaved mode with the -ni option.\n")
530 if (demux
->audio
->packs
>= MAX_PACKS
531 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
532 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
533 demux
->audio
->packs
, demux
->audio
->bytes
);
534 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
537 if (demux
->video
->packs
>= MAX_PACKS
538 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
539 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
540 demux
->video
->packs
, demux
->video
->bytes
);
541 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
544 if (!demux_fill_buffer(demux
, ds
)) {
545 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
546 "ds_fill_buffer()->demux_fill_buffer() failed\n");
550 ds
->buffer_pos
= ds
->buffer_size
= 0;
552 mp_msg(MSGT_DEMUXER
, MSGL_V
,
553 "ds_fill_buffer: EOF reached (stream: %s) \n",
554 ds
== demux
->audio
? "audio" : "video");
559 int demux_read_data(demux_stream_t
*ds
, unsigned char *mem
, int len
)
564 x
= ds
->buffer_size
- ds
->buffer_pos
;
566 if (!ds_fill_buffer(ds
))
572 fast_memcpy(mem
+ bytes
, &ds
->buffer
[ds
->buffer_pos
], x
);
582 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
583 * \param mem memory to read data into, may be NULL to discard data
584 * \param maxlen maximum number of bytes to read
585 * \param read number of bytes actually read
586 * \param pattern pattern to search for (lowest 8 bits are ignored)
587 * \return whether pattern was found
589 int demux_pattern_3(demux_stream_t
*ds
, unsigned char *mem
, int maxlen
,
590 int *read
, uint32_t pattern
)
592 register uint32_t head
= 0xffffff00;
593 register uint32_t pat
= pattern
& 0xffffff00;
596 register unsigned char *ds_buf
= &ds
->buffer
[ds
->buffer_size
];
597 int len
= ds
->buffer_size
- ds
->buffer_pos
;
598 register long pos
= -len
;
599 if (unlikely(pos
>= 0)) { // buffer is empty
606 } while (++pos
&& head
!= pat
);
608 if (total_len
+ len
> maxlen
)
609 len
= maxlen
- total_len
;
610 len
= demux_read_data(ds
, mem
? &mem
[total_len
] : NULL
, len
);
612 } while ((head
!= pat
|| total_len
< 3) && total_len
< maxlen
&& !ds
->eof
);
615 return total_len
>= 3 && head
== pat
;
618 void ds_free_packs(demux_stream_t
*ds
)
620 demux_packet_t
*dp
= ds
->first
;
622 demux_packet_t
*dn
= dp
->next
;
623 free_demux_packet(dp
);
626 if (ds
->asf_packet
) {
627 // free unfinished .asf fragments:
628 free(ds
->asf_packet
->buffer
);
629 free(ds
->asf_packet
);
630 ds
->asf_packet
= NULL
;
632 ds
->first
= ds
->last
= NULL
;
633 ds
->packs
= 0; // !!!!!
636 free_demux_packet(ds
->current
);
639 ds
->buffer_pos
= ds
->buffer_size
;
644 int ds_get_packet(demux_stream_t
*ds
, unsigned char **start
)
647 if (ds
->buffer_pos
>= ds
->buffer_size
) {
648 if (!ds_fill_buffer(ds
)) {
654 len
= ds
->buffer_size
- ds
->buffer_pos
;
655 *start
= &ds
->buffer
[ds
->buffer_pos
];
656 ds
->buffer_pos
+= len
;
660 int ds_get_packet_pts(demux_stream_t
*ds
, unsigned char **start
, double *pts
)
663 *pts
= MP_NOPTS_VALUE
;
664 if (ds
->buffer_pos
>= ds
->buffer_size
) {
665 if (!ds_fill_buffer(ds
)) {
671 // Return pts unless this read starts from the middle of a packet
673 *pts
= ds
->current
->pts
;
674 len
= ds
->buffer_size
- ds
->buffer_pos
;
675 *start
= &ds
->buffer
[ds
->buffer_pos
];
676 ds
->buffer_pos
+= len
;
680 int ds_get_packet_sub(demux_stream_t
*ds
, unsigned char **start
)
683 if (ds
->buffer_pos
>= ds
->buffer_size
) {
687 if (!ds_fill_buffer(ds
))
690 len
= ds
->buffer_size
- ds
->buffer_pos
;
691 *start
= &ds
->buffer
[ds
->buffer_pos
];
692 ds
->buffer_pos
+= len
;
696 double ds_get_next_pts(demux_stream_t
*ds
)
698 demuxer_t
*demux
= ds
->demuxer
;
700 if (demux
->audio
->packs
>= MAX_PACKS
701 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
702 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
703 demux
->audio
->packs
, demux
->audio
->bytes
);
704 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
705 return MP_NOPTS_VALUE
;
707 if (demux
->video
->packs
>= MAX_PACKS
708 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
709 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
710 demux
->video
->packs
, demux
->video
->bytes
);
711 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
712 return MP_NOPTS_VALUE
;
714 if (!demux_fill_buffer(demux
, ds
))
715 return MP_NOPTS_VALUE
;
717 return ds
->first
->pts
;
720 // ====================================================================
722 void demuxer_help(void)
726 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "Available demuxers:\n");
727 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, " demuxer: type info: (comment)\n");
728 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DEMUXERS\n");
729 for (i
= 0; demuxer_list
[i
]; i
++) {
730 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Don't display special demuxers
732 if (demuxer_list
[i
]->comment
&& strlen(demuxer_list
[i
]->comment
))
733 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s (%s)\n",
734 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
735 demuxer_list
[i
]->info
, demuxer_list
[i
]->comment
);
737 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s\n",
738 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
739 demuxer_list
[i
]->info
);
745 * Get demuxer type for a given demuxer name
747 * @param demuxer_name string with demuxer name of demuxer number
748 * @param force will be set if demuxer should be forced.
750 * @return DEMUXER_TYPE_xxx, -1 if error or not found
752 int get_demuxer_type_from_name(char *demuxer_name
, int *force
)
758 if (!demuxer_name
|| !demuxer_name
[0])
759 return DEMUXER_TYPE_UNKNOWN
;
761 *force
= demuxer_name
[0] == '+';
762 if (demuxer_name
[0] == '+')
763 demuxer_name
= &demuxer_name
[1];
764 for (i
= 0; demuxer_list
[i
]; i
++) {
765 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Can't select special demuxers from commandline
767 if (strcmp(demuxer_name
, demuxer_list
[i
]->name
) == 0)
768 return demuxer_list
[i
]->type
;
771 // No match found, try to parse name as an integer (demuxer number)
772 type_int
= strtol(demuxer_name
, &endptr
, 0);
773 if (*endptr
) // Conversion failed
775 if ((type_int
> 0) && (type_int
<= DEMUXER_TYPE_MAX
))
776 return (int) type_int
;
781 int extension_parsing
= 1; // 0=off 1=mixed (used only for unstable formats)
784 NOTE : Several demuxers may be opened at the same time so
785 demuxers should NEVER rely on an external var to enable them
786 self. If a demuxer can't do any auto-detection it should only use
787 file_format. The user can explicitly set file_format with the -demuxer
788 option so there is really no need for another extra var.
789 For convenience an option can be added to set file_format directly
790 to the right type (ex: rawaudio,rawvideo).
791 Also the stream can override the file_format so a demuxer which rely
792 on a special stream type can set file_format at the stream level
796 static demuxer_t
*demux_open_stream(struct MPOpts
*opts
, stream_t
*stream
,
797 int file_format
, int force
, int audio_id
,
798 int video_id
, int dvdsub_id
,
801 demuxer_t
*demuxer
= NULL
;
803 sh_video_t
*sh_video
= NULL
;
805 const demuxer_desc_t
*demuxer_desc
;
809 // If somebody requested a demuxer check it
811 if ((demuxer_desc
= get_demuxer_desc_from_type(file_format
))) {
812 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
813 video_id
, dvdsub_id
, filename
);
814 if (demuxer_desc
->check_file
)
815 fformat
= demuxer_desc
->check_file(demuxer
);
816 if (force
|| !demuxer_desc
->check_file
)
817 fformat
= demuxer_desc
->type
;
819 if (fformat
== demuxer_desc
->type
) {
820 demuxer_t
*demux2
= demuxer
;
821 // Move messages to demuxer detection code?
822 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
823 "%s file format detected.\n",
824 demuxer_desc
->shortdesc
);
825 file_format
= fformat
;
826 if (!demuxer
->desc
->open
827 || (demux2
= demuxer
->desc
->open(demuxer
))) {
832 // Format changed after check, recurse
833 free_demuxer(demuxer
);
834 return demux_open_stream(opts
, stream
, fformat
, force
,
835 audio_id
, video_id
, dvdsub_id
,
839 // Check failed for forced demuxer, quit
840 free_demuxer(demuxer
);
844 // Test demuxers with safe file checks
845 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
846 if (demuxer_desc
->safe_check
) {
847 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
848 video_id
, dvdsub_id
, filename
);
849 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
850 if (fformat
== demuxer_desc
->type
) {
851 demuxer_t
*demux2
= demuxer
;
852 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
853 "%s file format detected.\n",
854 demuxer_desc
->shortdesc
);
855 file_format
= fformat
;
856 if (!demuxer
->desc
->open
857 || (demux2
= demuxer
->desc
->open(demuxer
))) {
862 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
863 return demuxer
; // handled in mplayer.c
864 // Format changed after check, recurse
865 free_demuxer(demuxer
);
866 demuxer
= demux_open_stream(opts
, stream
, fformat
, force
,
868 dvdsub_id
, filename
);
870 return demuxer
; // done!
871 file_format
= DEMUXER_TYPE_UNKNOWN
;
874 free_demuxer(demuxer
);
879 // If no forced demuxer perform file extension based detection
880 // Ok. We're over the stable detectable fileformats, the next ones are
881 // a bit fuzzy. So by default (extension_parsing==1) try extension-based
883 if (file_format
== DEMUXER_TYPE_UNKNOWN
&& filename
884 && extension_parsing
== 1) {
885 file_format
= demuxer_type_by_filename(filename
);
886 if (file_format
!= DEMUXER_TYPE_UNKNOWN
) {
887 // we like recursion :)
888 demuxer
= demux_open_stream(opts
, stream
, file_format
, force
,
889 audio_id
, video_id
, dvdsub_id
,
892 return demuxer
; // done!
893 file_format
= DEMUXER_TYPE_UNKNOWN
; // continue fuzzy guessing...
894 mp_msg(MSGT_DEMUXER
, MSGL_V
,
895 "demuxer: continue fuzzy content-based format guessing...\n");
898 // Try detection for all other demuxers
899 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
900 if (!demuxer_desc
->safe_check
&& demuxer_desc
->check_file
) {
901 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
902 video_id
, dvdsub_id
, filename
);
903 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
904 if (fformat
== demuxer_desc
->type
) {
905 demuxer_t
*demux2
= demuxer
;
906 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
907 "%s file format detected.\n",
908 demuxer_desc
->shortdesc
);
909 file_format
= fformat
;
910 if (!demuxer
->desc
->open
911 || (demux2
= demuxer
->desc
->open(demuxer
))) {
916 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
917 return demuxer
; // handled in mplayer.c
918 // Format changed after check, recurse
919 free_demuxer(demuxer
);
920 demuxer
= demux_open_stream(opts
, stream
, fformat
, force
,
922 dvdsub_id
, filename
);
924 return demuxer
; // done!
925 file_format
= DEMUXER_TYPE_UNKNOWN
;
928 free_demuxer(demuxer
);
934 //====== File format recognized, set up these for compatibility: =========
937 demuxer
->file_format
= file_format
;
939 if ((sh_video
= demuxer
->video
->sh
) && sh_video
->bih
) {
940 int biComp
= le2me_32(sh_video
->bih
->biCompression
);
941 mp_msg(MSGT_DEMUX
, MSGL_INFO
,
942 "VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
943 (char *) &biComp
, sh_video
->bih
->biWidth
,
944 sh_video
->bih
->biHeight
, sh_video
->bih
->biBitCount
,
945 sh_video
->fps
, sh_video
->i_bps
* 0.008f
,
946 sh_video
->i_bps
/ 1024.0f
);
949 if (ass_enabled
&& ass_library
) {
950 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
951 sh_sub_t
*sh
= demuxer
->s_streams
[i
];
952 if (sh
&& sh
->type
== 'a') {
953 sh
->ass_track
= ass_new_track(ass_library
);
954 if (sh
->ass_track
&& sh
->extradata
)
955 ass_process_codec_private(sh
->ass_track
, sh
->extradata
,
957 } else if (sh
&& sh
->type
!= 'v')
958 sh
->ass_track
= ass_default_track(ass_library
);
965 char *audio_stream
= NULL
;
966 char *sub_stream
= NULL
;
967 int audio_stream_cache
= 0;
969 char *demuxer_name
= NULL
; // parameter from -demuxer
970 char *audio_demuxer_name
= NULL
; // parameter from -audio-demuxer
971 char *sub_demuxer_name
= NULL
; // parameter from -sub-demuxer
973 extern int hr_mp3_seek
;
975 extern float stream_cache_min_percent
;
976 extern float stream_cache_seek_min_percent
;
978 demuxer_t
*demux_open(struct MPOpts
*opts
, stream_t
*vs
, int file_format
,
979 int audio_id
, int video_id
, int dvdsub_id
,
982 stream_t
*as
= NULL
, *ss
= NULL
;
983 demuxer_t
*vd
, *ad
= NULL
, *sd
= NULL
;
985 int afmt
= DEMUXER_TYPE_UNKNOWN
, sfmt
= DEMUXER_TYPE_UNKNOWN
;
987 int audio_demuxer_type
= 0, sub_demuxer_type
= 0;
988 int demuxer_force
= 0, audio_demuxer_force
= 0, sub_demuxer_force
= 0;
991 get_demuxer_type_from_name(demuxer_name
, &demuxer_force
)) < 0) {
992 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-demuxer %s does not exist.\n",
995 if ((audio_demuxer_type
=
996 get_demuxer_type_from_name(audio_demuxer_name
,
997 &audio_demuxer_force
)) < 0) {
998 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-audio-demuxer %s does not exist.\n",
1001 if ((sub_demuxer_type
=
1002 get_demuxer_type_from_name(sub_demuxer_name
,
1003 &sub_demuxer_force
)) < 0) {
1004 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-sub-demuxer %s does not exist.\n",
1009 as
= open_stream(audio_stream
, 0, &afmt
);
1011 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "Cannot open audio stream: %s\n",
1015 if (audio_stream_cache
) {
1016 if (!stream_enable_cache
1017 (as
, audio_stream_cache
* 1024,
1018 audio_stream_cache
* 1024 * (stream_cache_min_percent
/
1020 audio_stream_cache
* 1024 * (stream_cache_seek_min_percent
/
1023 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
1024 "Can't enable audio stream cache\n");
1030 ss
= open_stream(sub_stream
, 0, &sfmt
);
1032 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "Cannot open subtitle stream: %s\n",
1038 vd
= demux_open_stream(opts
, vs
, demuxer_type
? demuxer_type
: file_format
,
1039 demuxer_force
, audio_stream
? -2 : audio_id
,
1040 video_id
, sub_stream
? -2 : dvdsub_id
, filename
);
1049 ad
= demux_open_stream(opts
, as
,
1050 audio_demuxer_type
? audio_demuxer_type
: afmt
,
1051 audio_demuxer_force
, audio_id
, -2, -2,
1054 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, "Failed to open audio demuxer: %s\n",
1057 } else if (ad
->audio
->sh
1058 && ((sh_audio_t
*) ad
->audio
->sh
)->format
== 0x55) // MP3
1059 hr_mp3_seek
= 1; // Enable high res seeking
1062 sd
= demux_open_stream(opts
, ss
,
1063 sub_demuxer_type
? sub_demuxer_type
: sfmt
,
1064 sub_demuxer_force
, -2, -2, dvdsub_id
,
1067 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
,
1068 "Failed to open subtitle demuxer: %s\n", sub_stream
);
1074 res
= new_demuxers_demuxer(vd
, ad
, sd
);
1076 res
= new_demuxers_demuxer(vd
, ad
, vd
);
1078 res
= new_demuxers_demuxer(vd
, vd
, sd
);
1082 opts
->correct_pts
= opts
->user_correct_pts
;
1083 if (opts
->correct_pts
< 0)
1085 demux_control(res
, DEMUXER_CTRL_CORRECT_PTS
,
1086 NULL
) == DEMUXER_CTRL_OK
;
1091 void demux_flush(demuxer_t
*demuxer
)
1093 ds_free_packs(demuxer
->video
);
1094 ds_free_packs(demuxer
->audio
);
1095 ds_free_packs(demuxer
->sub
);
1098 int demux_seek(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
,
1101 demux_stream_t
*d_audio
= demuxer
->audio
;
1102 demux_stream_t
*d_video
= demuxer
->video
;
1103 sh_audio_t
*sh_audio
= d_audio
->sh
;
1104 sh_video_t
*sh_video
= d_video
->sh
;
1108 if (!demuxer
->seekable
) {
1109 if (demuxer
->file_format
== DEMUXER_TYPE_AVI
)
1110 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "Cannot seek in raw AVI streams. (Index required, try with the -idx switch.)\n");
1112 else if (demuxer
->file_format
== DEMUXER_TYPE_TV
)
1113 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "TV input is not seekable! (Seeking will probably be for changing channels ;)\n");
1116 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "Cannot seek in this file.\n");
1119 // clear demux buffers:
1120 demux_flush(demuxer
);
1122 sh_audio
->a_buffer_len
= 0;
1124 demuxer
->stream
->eof
= 0;
1125 demuxer
->video
->eof
= 0;
1126 demuxer
->audio
->eof
= 0;
1129 sh_video
->timer
= 0; // !!!!!!
1131 if (flags
& SEEK_ABSOLUTE
)
1134 if (demuxer
->stream_pts
== MP_NOPTS_VALUE
)
1136 pts
= demuxer
->stream_pts
;
1139 if (flags
& SEEK_FACTOR
) {
1140 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_TIME_LENGTH
, &tmp
)
1141 == STREAM_UNSUPPORTED
)
1143 pts
+= tmp
* rel_seek_secs
;
1145 pts
+= rel_seek_secs
;
1147 if (stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_TIME
, &pts
) !=
1148 STREAM_UNSUPPORTED
) {
1149 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1154 if (demuxer
->desc
->seek
)
1155 demuxer
->desc
->seek(demuxer
, rel_seek_secs
, audio_delay
, flags
);
1158 resync_audio_stream(sh_audio
);
1163 int demux_info_add(demuxer_t
*demuxer
, const char *opt
, const char *param
)
1165 char **info
= demuxer
->info
;
1169 for (n
= 0; info
&& info
[2 * n
] != NULL
; n
++) {
1170 if (!strcasecmp(opt
, info
[2 * n
])) {
1171 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "Demuxer info %s changed to %s\n", opt
,
1173 free(info
[2 * n
+ 1]);
1174 info
[2 * n
+ 1] = strdup(param
);
1179 info
= demuxer
->info
=
1180 (char **) realloc(info
, (2 * (n
+ 2)) * sizeof(char *));
1181 info
[2 * n
] = strdup(opt
);
1182 info
[2 * n
+ 1] = strdup(param
);
1183 memset(&info
[2 * (n
+ 1)], 0, 2 * sizeof(char *));
1188 int demux_info_print(demuxer_t
*demuxer
)
1190 char **info
= demuxer
->info
;
1196 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "Clip info:\n");
1197 for (n
= 0; info
[2 * n
] != NULL
; n
++) {
1198 mp_msg(MSGT_DEMUX
, MSGL_INFO
, " %s: %s\n", info
[2 * n
],
1200 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_NAME%d=%s\n", n
,
1202 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_VALUE%d=%s\n", n
,
1205 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_N=%d\n", n
);
1210 char *demux_info_get(demuxer_t
*demuxer
, const char *opt
)
1213 char **info
= demuxer
->info
;
1215 for (i
= 0; info
&& info
[2 * i
] != NULL
; i
++) {
1216 if (!strcasecmp(opt
, info
[2 * i
]))
1217 return info
[2 * i
+ 1];
1223 int demux_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
1226 if (demuxer
->desc
->control
)
1227 return demuxer
->desc
->control(demuxer
, cmd
, arg
);
1229 return DEMUXER_CTRL_NOTIMPL
;
1234 double demuxer_get_time_length(demuxer_t
*demuxer
)
1236 double get_time_ans
;
1237 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1238 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1239 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
1241 (demuxer
, DEMUXER_CTRL_GET_TIME_LENGTH
, (void *) &get_time_ans
) <= 0) {
1242 if (sh_video
&& sh_video
->i_bps
&& sh_audio
&& sh_audio
->i_bps
)
1243 get_time_ans
= (double) (demuxer
->movi_end
-
1244 demuxer
->movi_start
) / (sh_video
->i_bps
+
1246 else if (sh_video
&& sh_video
->i_bps
)
1247 get_time_ans
= (double) (demuxer
->movi_end
-
1248 demuxer
->movi_start
) / sh_video
->i_bps
;
1249 else if (sh_audio
&& sh_audio
->i_bps
)
1250 get_time_ans
= (double) (demuxer
->movi_end
-
1251 demuxer
->movi_start
) / sh_audio
->i_bps
;
1255 return get_time_ans
;
1259 * \brief demuxer_get_current_time() returns the time of the current play in three possible ways:
1260 * either when the stream reader satisfies STREAM_CTRL_GET_CURRENT_TIME (e.g. dvd)
1261 * or using sh_video->pts when the former method fails
1263 * \return the current play time
1265 int demuxer_get_current_time(demuxer_t
*demuxer
)
1267 double get_time_ans
= 0;
1268 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1269 if (demuxer
->stream_pts
!= MP_NOPTS_VALUE
)
1270 get_time_ans
= demuxer
->stream_pts
;
1272 get_time_ans
= sh_video
->pts
;
1273 return (int) get_time_ans
;
1276 int demuxer_get_percent_pos(demuxer_t
*demuxer
)
1279 int res
= demux_control(demuxer
, DEMUXER_CTRL_GET_PERCENT_POS
, &ans
);
1280 int len
= (demuxer
->movi_end
- demuxer
->movi_start
) / 100;
1283 ans
= (demuxer
->filepos
- demuxer
->movi_start
) / len
;
1294 int demuxer_switch_audio(demuxer_t
*demuxer
, int index
)
1296 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_AUDIO
, &index
);
1297 if (res
== DEMUXER_CTRL_NOTIMPL
)
1298 index
= demuxer
->audio
->id
;
1299 if (demuxer
->audio
->id
>= 0)
1300 demuxer
->audio
->sh
= demuxer
->a_streams
[demuxer
->audio
->id
];
1302 demuxer
->audio
->sh
= NULL
;
1306 int demuxer_switch_video(demuxer_t
*demuxer
, int index
)
1308 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_VIDEO
, &index
);
1309 if (res
== DEMUXER_CTRL_NOTIMPL
)
1310 index
= demuxer
->video
->id
;
1311 if (demuxer
->video
->id
>= 0)
1312 demuxer
->video
->sh
= demuxer
->v_streams
[demuxer
->video
->id
];
1314 demuxer
->video
->sh
= NULL
;
1318 int demuxer_add_attachment(demuxer_t
*demuxer
, const char *name
,
1319 const char *type
, const void *data
, size_t size
)
1321 if (!(demuxer
->num_attachments
& 31))
1322 demuxer
->attachments
= realloc(demuxer
->attachments
,
1323 (demuxer
->num_attachments
+ 32) * sizeof(demux_attachment_t
));
1325 demuxer
->attachments
[demuxer
->num_attachments
].name
= strdup(name
);
1326 demuxer
->attachments
[demuxer
->num_attachments
].type
= strdup(type
);
1327 demuxer
->attachments
[demuxer
->num_attachments
].data
= malloc(size
);
1328 memcpy(demuxer
->attachments
[demuxer
->num_attachments
].data
, data
, size
);
1329 demuxer
->attachments
[demuxer
->num_attachments
].data_size
= size
;
1331 return demuxer
->num_attachments
++;
1334 int demuxer_add_chapter(demuxer_t
*demuxer
, const char *name
, uint64_t start
,
1337 if (demuxer
->chapters
== NULL
)
1338 demuxer
->chapters
= malloc(32 * sizeof(*demuxer
->chapters
));
1339 else if (!(demuxer
->num_chapters
% 32))
1340 demuxer
->chapters
= realloc(demuxer
->chapters
,
1341 (demuxer
->num_chapters
+ 32) *
1342 sizeof(*demuxer
->chapters
));
1344 demuxer
->chapters
[demuxer
->num_chapters
].start
= start
;
1345 demuxer
->chapters
[demuxer
->num_chapters
].end
= end
;
1346 demuxer
->chapters
[demuxer
->num_chapters
].name
= strdup(name
? name
: mp_gtext("unknown"));
1348 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_ID=%d\n", demuxer
->num_chapters
);
1349 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_START=%"PRIu64
"\n", demuxer
->num_chapters
, start
);
1351 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_END=%"PRIu64
"\n", demuxer
->num_chapters
, end
);
1353 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_NAME=%s\n", demuxer
->num_chapters
, name
);
1355 return demuxer
->num_chapters
++;
1359 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1360 * either using the demuxer->chapters structure set by the demuxer
1361 * or asking help to the stream layer (e.g. dvd)
1362 * \param chapter - chapter number wished - 0-based
1363 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1364 * \param chapter_name name of chapter found (set by this function is param is not null)
1365 * \return -1 on error, current chapter if successful
1368 int demuxer_seek_chapter(demuxer_t
*demuxer
, int chapter
, double *seek_pts
,
1369 char **chapter_name
)
1372 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1373 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1375 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1376 demux_flush(demuxer
);
1378 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_CHAPTER
,
1380 if (ris
!= STREAM_UNSUPPORTED
)
1381 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1383 ds_fill_buffer(demuxer
->video
);
1384 resync_video_stream(sh_video
);
1388 ds_fill_buffer(demuxer
->audio
);
1389 resync_audio_stream(sh_audio
);
1391 // exit status may be ok, but main() doesn't have to seek itself
1392 // (because e.g. dvds depend on sectors, not on pts)
1396 *chapter_name
= NULL
;
1398 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1399 &num_chapters
) == STREAM_UNSUPPORTED
)
1402 *chapter_name
= talloc_size(NULL
, 16);
1403 sprintf(*chapter_name
, " of %3d", num_chapters
);
1407 return ris
!= STREAM_UNSUPPORTED
? chapter
: -1;
1408 } else { // chapters structure is set in the demuxer
1409 if (chapter
>= demuxer
->num_chapters
)
1414 *seek_pts
= demuxer
->chapters
[chapter
].start
/ 1000.0;
1417 *chapter_name
= talloc_strdup(NULL
, demuxer
->chapters
[chapter
].name
);
1423 int demuxer_get_current_chapter(demuxer_t
*demuxer
)
1426 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1427 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_CURRENT_CHAPTER
,
1428 &chapter
) == STREAM_UNSUPPORTED
)
1431 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1432 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1434 now
= (sh_video
? sh_video
->pts
: (sh_audio
? sh_audio
->pts
: 0))
1436 for (chapter
= demuxer
->num_chapters
- 1; chapter
>= 0; --chapter
) {
1437 if (demuxer
->chapters
[chapter
].start
<= now
)
1444 char *demuxer_chapter_name(demuxer_t
*demuxer
, int chapter
)
1446 if (demuxer
->num_chapters
&& demuxer
->chapters
) {
1447 if (chapter
>= 0 && chapter
< demuxer
->num_chapters
1448 && demuxer
->chapters
[chapter
].name
)
1449 return strdup(demuxer
->chapters
[chapter
].name
);
1454 char *demuxer_chapter_display_name(demuxer_t
*demuxer
, int chapter
)
1456 char *chapter_name
= demuxer_chapter_name(demuxer
, chapter
);
1458 char *tmp
= malloc(strlen(chapter_name
) + 14);
1459 snprintf(tmp
, 63, "(%d) %s", chapter
+ 1, chapter_name
);
1463 int chapter_num
= demuxer_chapter_count(demuxer
);
1465 if (chapter_num
<= 0)
1466 sprintf(tmp
, "(%d)", chapter
+ 1);
1468 sprintf(tmp
, "(%d) of %d", chapter
+ 1, chapter_num
);
1473 float demuxer_chapter_time(demuxer_t
*demuxer
, int chapter
, float *end
)
1475 if (demuxer
->num_chapters
&& demuxer
->chapters
&& chapter
>= 0
1476 && chapter
< demuxer
->num_chapters
) {
1478 *end
= demuxer
->chapters
[chapter
].end
/ 1000.0;
1479 return demuxer
->chapters
[chapter
].start
/ 1000.0;
1484 int demuxer_chapter_count(demuxer_t
*demuxer
)
1486 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1487 int num_chapters
= 0;
1488 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1489 &num_chapters
) == STREAM_UNSUPPORTED
)
1491 return num_chapters
;
1493 return demuxer
->num_chapters
;
1496 int demuxer_angles_count(demuxer_t
*demuxer
)
1498 int ris
, angles
= -1;
1500 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_ANGLES
, &angles
);
1501 if (ris
== STREAM_UNSUPPORTED
)
1506 int demuxer_get_current_angle(demuxer_t
*demuxer
)
1508 int ris
, curr_angle
= -1;
1509 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_ANGLE
, &curr_angle
);
1510 if (ris
== STREAM_UNSUPPORTED
)
1516 int demuxer_set_angle(demuxer_t
*demuxer
, int angle
)
1518 int ris
, angles
= -1;
1519 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1520 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1522 angles
= demuxer_angles_count(demuxer
);
1523 if ((angles
< 1) || (angle
> angles
))
1526 demux_flush(demuxer
);
1528 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SET_ANGLE
, &angle
);
1529 if (ris
== STREAM_UNSUPPORTED
)
1532 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1534 ds_fill_buffer(demuxer
->video
);
1535 resync_video_stream(sh_video
);
1539 ds_fill_buffer(demuxer
->audio
);
1540 resync_audio_stream(sh_audio
);
1546 int demuxer_audio_track_by_lang(demuxer_t
*d
, char *lang
)
1549 lang
+= strspn(lang
, ",");
1550 while ((len
= strcspn(lang
, ",")) > 0) {
1551 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1552 sh_audio_t
*sh
= d
->a_streams
[i
];
1553 if (sh
&& sh
->lang
&& strncmp(sh
->lang
, lang
, len
) == 0)
1557 lang
+= strspn(lang
, ",");
1562 int demuxer_sub_track_by_lang(demuxer_t
*d
, char *lang
)
1565 lang
+= strspn(lang
, ",");
1566 while ((len
= strcspn(lang
, ",")) > 0) {
1567 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1568 sh_sub_t
*sh
= d
->s_streams
[i
];
1569 if (sh
&& sh
->lang
&& strncmp(sh
->lang
, lang
, len
) == 0)
1573 lang
+= strspn(lang
, ",");
1578 int demuxer_default_audio_track(demuxer_t
*d
)
1581 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1582 sh_audio_t
*sh
= d
->a_streams
[i
];
1583 if (sh
&& sh
->default_track
)
1586 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1587 sh_audio_t
*sh
= d
->a_streams
[i
];
1594 int demuxer_default_sub_track(demuxer_t
*d
)
1597 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1598 sh_sub_t
*sh
= d
->s_streams
[i
];
1599 if (sh
&& sh
->default_track
)