4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/types.h>
36 #include "libvo/fastmemcpy.h"
38 #include "stream/stream.h"
43 #include "libaf/af_format.h"
44 #include "libmpcodecs/dec_teletext.h"
48 #ifdef CONFIG_LIBAVCODEC
49 #include "libavcodec/avcodec.h"
50 #if MP_INPUT_BUFFER_PADDING_SIZE < FF_INPUT_BUFFER_PADDING_SIZE
51 #error MP_INPUT_BUFFER_PADDING_SIZE is too small!
55 void resync_video_stream(sh_video_t
*sh_video
);
56 void resync_audio_stream(sh_audio_t
*sh_audio
);
59 extern const demuxer_desc_t demuxer_desc_rawaudio
;
60 extern const demuxer_desc_t demuxer_desc_rawvideo
;
61 extern const demuxer_desc_t demuxer_desc_tv
;
62 extern const demuxer_desc_t demuxer_desc_mf
;
63 extern const demuxer_desc_t demuxer_desc_avi
;
64 extern const demuxer_desc_t demuxer_desc_y4m
;
65 extern const demuxer_desc_t demuxer_desc_asf
;
66 extern const demuxer_desc_t demuxer_desc_real
;
67 extern const demuxer_desc_t demuxer_desc_smjpeg
;
68 extern const demuxer_desc_t demuxer_desc_matroska
;
69 extern const demuxer_desc_t demuxer_desc_realaudio
;
70 extern const demuxer_desc_t demuxer_desc_vqf
;
71 extern const demuxer_desc_t demuxer_desc_mov
;
72 extern const demuxer_desc_t demuxer_desc_vivo
;
73 extern const demuxer_desc_t demuxer_desc_fli
;
74 extern const demuxer_desc_t demuxer_desc_film
;
75 extern const demuxer_desc_t demuxer_desc_roq
;
76 extern const demuxer_desc_t demuxer_desc_gif
;
77 extern const demuxer_desc_t demuxer_desc_ogg
;
78 extern const demuxer_desc_t demuxer_desc_avs
;
79 extern const demuxer_desc_t demuxer_desc_pva
;
80 extern const demuxer_desc_t demuxer_desc_nsv
;
81 extern const demuxer_desc_t demuxer_desc_mpeg_ts
;
82 extern const demuxer_desc_t demuxer_desc_lmlm4
;
83 extern const demuxer_desc_t demuxer_desc_mpeg_ps
;
84 extern const demuxer_desc_t demuxer_desc_mpeg_pes
;
85 extern const demuxer_desc_t demuxer_desc_mpeg_es
;
86 extern const demuxer_desc_t demuxer_desc_mpeg_gxf
;
87 extern const demuxer_desc_t demuxer_desc_mpeg4_es
;
88 extern const demuxer_desc_t demuxer_desc_h264_es
;
89 extern const demuxer_desc_t demuxer_desc_rawdv
;
90 extern const demuxer_desc_t demuxer_desc_mpc
;
91 extern const demuxer_desc_t demuxer_desc_audio
;
92 extern const demuxer_desc_t demuxer_desc_xmms
;
93 extern const demuxer_desc_t demuxer_desc_mpeg_ty
;
94 extern const demuxer_desc_t demuxer_desc_rtp
;
95 extern const demuxer_desc_t demuxer_desc_rtp_nemesi
;
96 extern const demuxer_desc_t demuxer_desc_lavf
;
97 extern const demuxer_desc_t demuxer_desc_lavf_preferred
;
98 extern const demuxer_desc_t demuxer_desc_aac
;
99 extern const demuxer_desc_t demuxer_desc_nut
;
100 extern const demuxer_desc_t demuxer_desc_mng
;
102 /* Please do not add any new demuxers here. If you want to implement a new
103 * demuxer, add it to libavformat, except for wrappers around external
104 * libraries and demuxers requiring binary support. */
106 const demuxer_desc_t
*const demuxer_list
[] = {
107 &demuxer_desc_rawaudio
,
108 &demuxer_desc_rawvideo
,
113 #ifdef CONFIG_LIBAVFORMAT
114 &demuxer_desc_lavf_preferred
,
121 &demuxer_desc_smjpeg
,
122 &demuxer_desc_matroska
,
123 &demuxer_desc_realaudio
,
133 #ifdef CONFIG_OGGVORBIS
136 #ifdef CONFIG_WIN32DLL
140 &demuxer_desc_mpeg_ts
,
142 &demuxer_desc_mpeg_ps
,
143 &demuxer_desc_mpeg_pes
,
144 &demuxer_desc_mpeg_es
,
145 &demuxer_desc_mpeg_gxf
,
146 &demuxer_desc_mpeg4_es
,
147 &demuxer_desc_h264_es
,
149 &demuxer_desc_mpeg_ty
,
150 #ifdef CONFIG_LIVE555
153 #ifdef CONFIG_LIBNEMESI
154 &demuxer_desc_rtp_nemesi
,
156 #ifdef CONFIG_LIBAVFORMAT
159 #ifdef CONFIG_MUSEPACK
162 #ifdef CONFIG_LIBDV095
175 /* Please do not add any new demuxers here. If you want to implement a new
176 * demuxer, add it to libavformat, except for wrappers around external
177 * libraries and demuxers requiring binary support. */
181 void free_demuxer_stream(demux_stream_t
*ds
)
187 demux_stream_t
*new_demuxer_stream(struct demuxer
*demuxer
, int id
)
189 demux_stream_t
*ds
= malloc(sizeof(demux_stream_t
));
190 *ds
= (demux_stream_t
){
200 * Get demuxer description structure for a given demuxer type
202 * @param file_format type of the demuxer
203 * @return structure for the demuxer, NULL if not found
205 static const demuxer_desc_t
*get_demuxer_desc_from_type(int file_format
)
209 for (i
= 0; demuxer_list
[i
]; i
++)
210 if (file_format
== demuxer_list
[i
]->type
)
211 return demuxer_list
[i
];
217 demuxer_t
*new_demuxer(struct MPOpts
*opts
, stream_t
*stream
, int type
,
218 int a_id
, int v_id
, int s_id
, char *filename
)
220 struct demuxer
*d
= talloc_zero(NULL
, struct demuxer
);
222 d
->stream_pts
= MP_NOPTS_VALUE
;
223 d
->reference_clock
= MP_NOPTS_VALUE
;
224 d
->movi_start
= stream
->start_pos
;
225 d
->movi_end
= stream
->end_pos
;
229 d
->audio
= new_demuxer_stream(d
, a_id
);
230 d
->video
= new_demuxer_stream(d
, v_id
);
231 d
->sub
= new_demuxer_stream(d
, s_id
);
235 if (!(d
->desc
= get_demuxer_desc_from_type(type
)))
236 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
237 "BUG! Invalid demuxer type in new_demuxer(), "
238 "big troubles ahead.");
239 if (filename
) // Filename hack for avs_check_file
240 d
->filename
= strdup(filename
);
241 stream_reset(stream
);
242 stream_seek(stream
, stream
->start_pos
);
246 sh_sub_t
*new_sh_sub_sid(demuxer_t
*demuxer
, int id
, int sid
)
248 if (id
> MAX_S_STREAMS
- 1 || id
< 0) {
249 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
250 "Requested sub stream id overflow (%d > %d)\n", id
,
254 if (demuxer
->s_streams
[id
])
255 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, "Sub stream %i redefined\n", id
);
257 sh_sub_t
*sh
= calloc(1, sizeof(sh_sub_t
));
258 demuxer
->s_streams
[id
] = sh
;
260 sh
->opts
= demuxer
->opts
;
261 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SUBTITLE_ID=%d\n", sid
);
263 if (sid
== demuxer
->opts
->sub_id
) {
264 demuxer
->sub
->id
= id
;
265 demuxer
->sub
->sh
= demuxer
->s_streams
[id
];
267 return demuxer
->s_streams
[id
];
270 static void free_sh_sub(sh_sub_t
*sh
)
272 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_sub at %p\n", sh
);
276 ass_free_track(sh
->ass_track
);
282 sh_audio_t
*new_sh_audio_aid(demuxer_t
*demuxer
, int id
, int aid
)
284 if (id
> MAX_A_STREAMS
- 1 || id
< 0) {
285 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
286 "Requested audio stream id overflow (%d > %d)\n", id
,
290 if (demuxer
->a_streams
[id
]) {
291 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, "WARNING: Audio stream header %d redefined.\n", id
);
293 mp_tmsg(MSGT_DEMUXER
, MSGL_V
, "==> Found audio stream: %d\n", id
);
294 sh_audio_t
*sh
= calloc(1, sizeof(sh_audio_t
));
295 demuxer
->a_streams
[id
] = sh
;
297 sh
->ds
= demuxer
->audio
;
300 sh
->sample_format
= AF_FORMAT_S16_NE
;
301 sh
->audio_out_minsize
= 8192; /* default size, maybe not enough for Win32/ACM */
302 sh
->pts
= MP_NOPTS_VALUE
;
303 sh
->opts
= demuxer
->opts
;
304 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_ID=%d\n", aid
);
306 return demuxer
->a_streams
[id
];
309 void free_sh_audio(demuxer_t
*demuxer
, int id
)
311 sh_audio_t
*sh
= demuxer
->a_streams
[id
];
312 demuxer
->a_streams
[id
] = NULL
;
313 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_audio at %p\n", sh
);
320 sh_video_t
*new_sh_video_vid(demuxer_t
*demuxer
, int id
, int vid
)
322 if (id
> MAX_V_STREAMS
- 1 || id
< 0) {
323 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
324 "Requested video stream id overflow (%d > %d)\n", id
,
328 if (demuxer
->v_streams
[id
])
329 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, "WARNING: Video stream header %d redefined.\n", id
);
331 mp_tmsg(MSGT_DEMUXER
, MSGL_V
, "==> Found video stream: %d\n", id
);
332 sh_video_t
*sh
= calloc(1, sizeof *sh
);
333 demuxer
->v_streams
[id
] = sh
;
335 sh
->ds
= demuxer
->video
;
336 sh
->opts
= demuxer
->opts
;
337 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_ID=%d\n", vid
);
339 return demuxer
->v_streams
[id
];
342 void free_sh_video(sh_video_t
*sh
)
344 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_video at %p\n", sh
);
349 void free_demuxer(demuxer_t
*demuxer
)
352 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing demuxer at %p\n",
354 if (demuxer
->desc
->close
)
355 demuxer
->desc
->close(demuxer
);
356 // Very ugly hack to make it behave like old implementation
357 if (demuxer
->desc
->type
== DEMUXER_TYPE_DEMUXERS
)
358 goto skip_streamfree
;
360 for (i
= 0; i
< MAX_A_STREAMS
; i
++)
361 if (demuxer
->a_streams
[i
])
362 free_sh_audio(demuxer
, i
);
363 for (i
= 0; i
< MAX_V_STREAMS
; i
++)
364 if (demuxer
->v_streams
[i
])
365 free_sh_video(demuxer
->v_streams
[i
]);
366 for (i
= 0; i
< MAX_S_STREAMS
; i
++)
367 if (demuxer
->s_streams
[i
])
368 free_sh_sub(demuxer
->s_streams
[i
]);
370 free_demuxer_stream(demuxer
->audio
);
371 free_demuxer_stream(demuxer
->video
);
372 free_demuxer_stream(demuxer
->sub
);
375 for (i
= 0; demuxer
->info
[i
] != NULL
; i
++)
376 free(demuxer
->info
[i
]);
379 free(demuxer
->filename
);
380 if (demuxer
->chapters
) {
381 for (i
= 0; i
< demuxer
->num_chapters
; i
++)
382 free(demuxer
->chapters
[i
].name
);
383 free(demuxer
->chapters
);
385 if (demuxer
->attachments
) {
386 for (i
= 0; i
< demuxer
->num_attachments
; i
++) {
387 free(demuxer
->attachments
[i
].name
);
388 free(demuxer
->attachments
[i
].type
);
389 free(demuxer
->attachments
[i
].data
);
391 free(demuxer
->attachments
);
393 if (demuxer
->teletext
)
394 teletext_control(demuxer
->teletext
, TV_VBI_CONTROL_STOP
, NULL
);
395 talloc_free(demuxer
);
399 void ds_add_packet(demux_stream_t
*ds
, demux_packet_t
*dp
)
401 // append packet to DS stream:
403 ds
->bytes
+= dp
->len
;
405 // next packet in stream
409 // first packet in stream
410 ds
->first
= ds
->last
= dp
;
412 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
413 "DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
414 (ds
== ds
->demuxer
->audio
) ? "d_audio" : "d_video", dp
->len
,
415 dp
->pts
, (unsigned int) dp
->pos
, ds
->demuxer
->audio
->packs
,
416 ds
->demuxer
->video
->packs
);
419 void ds_read_packet(demux_stream_t
*ds
, stream_t
*stream
, int len
,
420 double pts
, off_t pos
, int flags
)
422 demux_packet_t
*dp
= new_demux_packet(len
);
423 len
= stream_read(stream
, dp
->buffer
, len
);
424 resize_demux_packet(dp
, len
);
428 // append packet to DS stream:
429 ds_add_packet(ds
, dp
);
433 // 0 = EOF or no stream found or invalid type
434 // 1 = successfully read a packet
436 int demux_fill_buffer(demuxer_t
*demux
, demux_stream_t
*ds
)
438 // Note: parameter 'ds' can be NULL!
439 return demux
->desc
->fill_buffer(demux
, ds
);
445 #define MAX_ACUMULATED_PACKETS 64
446 int ds_fill_buffer(demux_stream_t
*ds
)
448 demuxer_t
*demux
= ds
->demuxer
;
450 free_demux_packet(ds
->current
);
452 if (mp_msg_test(MSGT_DEMUXER
, MSGL_DBG3
)) {
453 if (ds
== demux
->audio
)
454 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
455 "ds_fill_buffer(d_audio) called\n");
456 else if (ds
== demux
->video
)
457 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
458 "ds_fill_buffer(d_video) called\n");
459 else if (ds
== demux
->sub
)
460 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
, "ds_fill_buffer(d_sub) called\n");
462 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
463 "ds_fill_buffer(unknown 0x%X) called\n", (unsigned int) ds
);
467 demux_packet_t
*p
= ds
->first
;
469 if (demux
->reference_clock
!= MP_NOPTS_VALUE
) {
470 if ( p
->pts
!= MP_NOPTS_VALUE
471 && p
->pts
> demux
->reference_clock
472 && ds
->packs
< MAX_ACUMULATED_PACKETS
) {
473 if (demux_fill_buffer(demux
, ds
))
479 ds
->buffer
= p
->buffer
;
481 ds
->buffer_size
= p
->len
;
483 ds
->dpos
+= p
->len
; // !!!
485 if (p
->pts
!= MP_NOPTS_VALUE
) {
489 ds
->pts_bytes
+= p
->len
; // !!!
490 if (p
->stream_pts
!= MP_NOPTS_VALUE
)
491 demux
->stream_pts
= p
->stream_pts
;
492 ds
->flags
= p
->flags
;
500 /* The code below can set ds->eof to 1 when another stream runs
501 * out of buffer space. That makes sense because in that situation
502 * the calling code should not count on being able to demux more
503 * packets from this stream.
504 * If however the situation improves and we're called again
505 * despite the eof flag then it's better to clear it to avoid
511 #define MaybeNI _("Maybe you are playing a non-interleaved stream/file or the codec failed?\n" \
512 "For AVI files, try to force non-interleaved mode with the -ni option.\n")
514 if (demux
->audio
->packs
>= MAX_PACKS
515 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
516 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
517 demux
->audio
->packs
, demux
->audio
->bytes
);
518 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
521 if (demux
->video
->packs
>= MAX_PACKS
522 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
523 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
524 demux
->video
->packs
, demux
->video
->bytes
);
525 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
528 if (!demux_fill_buffer(demux
, ds
)) {
529 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
530 "ds_fill_buffer()->demux_fill_buffer() failed\n");
534 ds
->buffer_pos
= ds
->buffer_size
= 0;
536 mp_msg(MSGT_DEMUXER
, MSGL_V
,
537 "ds_fill_buffer: EOF reached (stream: %s) \n",
538 ds
== demux
->audio
? "audio" : "video");
543 int demux_read_data(demux_stream_t
*ds
, unsigned char *mem
, int len
)
548 x
= ds
->buffer_size
- ds
->buffer_pos
;
550 if (!ds_fill_buffer(ds
))
556 fast_memcpy(mem
+ bytes
, &ds
->buffer
[ds
->buffer_pos
], x
);
566 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
567 * \param mem memory to read data into, may be NULL to discard data
568 * \param maxlen maximum number of bytes to read
569 * \param read number of bytes actually read
570 * \param pattern pattern to search for (lowest 8 bits are ignored)
571 * \return whether pattern was found
573 int demux_pattern_3(demux_stream_t
*ds
, unsigned char *mem
, int maxlen
,
574 int *read
, uint32_t pattern
)
576 register uint32_t head
= 0xffffff00;
577 register uint32_t pat
= pattern
& 0xffffff00;
580 register unsigned char *ds_buf
= &ds
->buffer
[ds
->buffer_size
];
581 int len
= ds
->buffer_size
- ds
->buffer_pos
;
582 register long pos
= -len
;
583 if (unlikely(pos
>= 0)) { // buffer is empty
590 } while (++pos
&& head
!= pat
);
592 if (total_len
+ len
> maxlen
)
593 len
= maxlen
- total_len
;
594 len
= demux_read_data(ds
, mem
? &mem
[total_len
] : NULL
, len
);
596 } while ((head
!= pat
|| total_len
< 3) && total_len
< maxlen
&& !ds
->eof
);
599 return total_len
>= 3 && head
== pat
;
602 void ds_free_packs(demux_stream_t
*ds
)
604 demux_packet_t
*dp
= ds
->first
;
606 demux_packet_t
*dn
= dp
->next
;
607 free_demux_packet(dp
);
610 if (ds
->asf_packet
) {
611 // free unfinished .asf fragments:
612 free(ds
->asf_packet
->buffer
);
613 free(ds
->asf_packet
);
614 ds
->asf_packet
= NULL
;
616 ds
->first
= ds
->last
= NULL
;
617 ds
->packs
= 0; // !!!!!
620 free_demux_packet(ds
->current
);
623 ds
->buffer_pos
= ds
->buffer_size
;
628 int ds_get_packet(demux_stream_t
*ds
, unsigned char **start
)
631 if (ds
->buffer_pos
>= ds
->buffer_size
) {
632 if (!ds_fill_buffer(ds
)) {
638 len
= ds
->buffer_size
- ds
->buffer_pos
;
639 *start
= &ds
->buffer
[ds
->buffer_pos
];
640 ds
->buffer_pos
+= len
;
644 int ds_get_packet_pts(demux_stream_t
*ds
, unsigned char **start
, double *pts
)
647 *pts
= MP_NOPTS_VALUE
;
648 len
= ds_get_packet(ds
, start
);
651 // Return pts unless this read starts from the middle of a packet
652 if (len
== ds
->buffer_pos
)
653 *pts
= ds
->current
->pts
;
657 int ds_get_packet_sub(demux_stream_t
*ds
, unsigned char **start
)
660 if (ds
->buffer_pos
>= ds
->buffer_size
) {
664 if (!ds_fill_buffer(ds
))
667 len
= ds
->buffer_size
- ds
->buffer_pos
;
668 *start
= &ds
->buffer
[ds
->buffer_pos
];
669 ds
->buffer_pos
+= len
;
673 double ds_get_next_pts(demux_stream_t
*ds
)
675 demuxer_t
*demux
= ds
->demuxer
;
677 if (demux
->audio
->packs
>= MAX_PACKS
678 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
679 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
680 demux
->audio
->packs
, demux
->audio
->bytes
);
681 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
682 return MP_NOPTS_VALUE
;
684 if (demux
->video
->packs
>= MAX_PACKS
685 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
686 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
687 demux
->video
->packs
, demux
->video
->bytes
);
688 mp_tmsg(MSGT_DEMUXER
, MSGL_HINT
, MaybeNI
);
689 return MP_NOPTS_VALUE
;
691 if (!demux_fill_buffer(demux
, ds
))
692 return MP_NOPTS_VALUE
;
694 return ds
->first
->pts
;
697 // ====================================================================
699 void demuxer_help(void)
703 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "Available demuxers:\n");
704 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, " demuxer: type info: (comment)\n");
705 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DEMUXERS\n");
706 for (i
= 0; demuxer_list
[i
]; i
++) {
707 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Don't display special demuxers
709 if (demuxer_list
[i
]->comment
&& strlen(demuxer_list
[i
]->comment
))
710 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s (%s)\n",
711 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
712 demuxer_list
[i
]->info
, demuxer_list
[i
]->comment
);
714 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s\n",
715 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
716 demuxer_list
[i
]->info
);
722 * Get demuxer type for a given demuxer name
724 * @param demuxer_name string with demuxer name of demuxer number
725 * @param force will be set if demuxer should be forced.
727 * @return DEMUXER_TYPE_xxx, -1 if error or not found
729 int get_demuxer_type_from_name(char *demuxer_name
, int *force
)
735 if (!demuxer_name
|| !demuxer_name
[0])
736 return DEMUXER_TYPE_UNKNOWN
;
738 *force
= demuxer_name
[0] == '+';
739 if (demuxer_name
[0] == '+')
740 demuxer_name
= &demuxer_name
[1];
741 for (i
= 0; demuxer_list
[i
]; i
++) {
742 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Can't select special demuxers from commandline
744 if (strcmp(demuxer_name
, demuxer_list
[i
]->name
) == 0)
745 return demuxer_list
[i
]->type
;
748 // No match found, try to parse name as an integer (demuxer number)
749 type_int
= strtol(demuxer_name
, &endptr
, 0);
750 if (*endptr
) // Conversion failed
752 if ((type_int
> 0) && (type_int
<= DEMUXER_TYPE_MAX
))
753 return (int) type_int
;
758 int extension_parsing
= 1; // 0=off 1=mixed (used only for unstable formats)
761 NOTE : Several demuxers may be opened at the same time so
762 demuxers should NEVER rely on an external var to enable them
763 self. If a demuxer can't do any auto-detection it should only use
764 file_format. The user can explicitly set file_format with the -demuxer
765 option so there is really no need for another extra var.
766 For convenience an option can be added to set file_format directly
767 to the right type (ex: rawaudio,rawvideo).
768 Also the stream can override the file_format so a demuxer which rely
769 on a special stream type can set file_format at the stream level
773 static demuxer_t
*demux_open_stream(struct MPOpts
*opts
, stream_t
*stream
,
774 int file_format
, int force
, int audio_id
,
775 int video_id
, int dvdsub_id
,
778 demuxer_t
*demuxer
= NULL
;
780 sh_video_t
*sh_video
= NULL
;
782 const demuxer_desc_t
*demuxer_desc
;
786 // If somebody requested a demuxer check it
788 if ((demuxer_desc
= get_demuxer_desc_from_type(file_format
))) {
789 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
790 video_id
, dvdsub_id
, filename
);
791 if (demuxer_desc
->check_file
)
792 fformat
= demuxer_desc
->check_file(demuxer
);
793 if (force
|| !demuxer_desc
->check_file
)
794 fformat
= demuxer_desc
->type
;
796 if (fformat
== demuxer_desc
->type
) {
797 demuxer_t
*demux2
= demuxer
;
798 // Move messages to demuxer detection code?
799 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
800 "%s file format detected.\n",
801 demuxer_desc
->shortdesc
);
802 file_format
= fformat
;
803 if (!demuxer
->desc
->open
804 || (demux2
= demuxer
->desc
->open(demuxer
))) {
809 // Format changed after check, recurse
810 free_demuxer(demuxer
);
811 return demux_open_stream(opts
, stream
, fformat
, force
,
812 audio_id
, video_id
, dvdsub_id
,
816 // Check failed for forced demuxer, quit
817 free_demuxer(demuxer
);
821 // Test demuxers with safe file checks
822 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
823 if (demuxer_desc
->safe_check
) {
824 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
825 video_id
, dvdsub_id
, filename
);
826 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
827 if (fformat
== demuxer_desc
->type
) {
828 demuxer_t
*demux2
= demuxer
;
829 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
830 "%s file format detected.\n",
831 demuxer_desc
->shortdesc
);
832 file_format
= fformat
;
833 if (!demuxer
->desc
->open
834 || (demux2
= demuxer
->desc
->open(demuxer
))) {
839 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
840 return demuxer
; // handled in mplayer.c
841 // Format changed after check, recurse
842 free_demuxer(demuxer
);
843 demuxer
= demux_open_stream(opts
, stream
, fformat
, force
,
845 dvdsub_id
, filename
);
847 return demuxer
; // done!
848 file_format
= DEMUXER_TYPE_UNKNOWN
;
851 free_demuxer(demuxer
);
856 // If no forced demuxer perform file extension based detection
857 // Ok. We're over the stable detectable fileformats, the next ones are
858 // a bit fuzzy. So by default (extension_parsing==1) try extension-based
860 if (file_format
== DEMUXER_TYPE_UNKNOWN
&& filename
861 && extension_parsing
== 1) {
862 file_format
= demuxer_type_by_filename(filename
);
863 if (file_format
!= DEMUXER_TYPE_UNKNOWN
) {
864 // we like recursion :)
865 demuxer
= demux_open_stream(opts
, stream
, file_format
, force
,
866 audio_id
, video_id
, dvdsub_id
,
869 return demuxer
; // done!
870 file_format
= DEMUXER_TYPE_UNKNOWN
; // continue fuzzy guessing...
871 mp_msg(MSGT_DEMUXER
, MSGL_V
,
872 "demuxer: continue fuzzy content-based format guessing...\n");
875 // Try detection for all other demuxers
876 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
877 if (!demuxer_desc
->safe_check
&& demuxer_desc
->check_file
) {
878 demuxer
= new_demuxer(opts
, stream
, demuxer_desc
->type
, audio_id
,
879 video_id
, dvdsub_id
, filename
);
880 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
881 if (fformat
== demuxer_desc
->type
) {
882 demuxer_t
*demux2
= demuxer
;
883 mp_tmsg(MSGT_DEMUXER
, MSGL_INFO
,
884 "%s file format detected.\n",
885 demuxer_desc
->shortdesc
);
886 file_format
= fformat
;
887 if (!demuxer
->desc
->open
888 || (demux2
= demuxer
->desc
->open(demuxer
))) {
893 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
894 return demuxer
; // handled in mplayer.c
895 // Format changed after check, recurse
896 free_demuxer(demuxer
);
897 demuxer
= demux_open_stream(opts
, stream
, fformat
, force
,
899 dvdsub_id
, filename
);
901 return demuxer
; // done!
902 file_format
= DEMUXER_TYPE_UNKNOWN
;
905 free_demuxer(demuxer
);
911 //====== File format recognized, set up these for compatibility: =========
914 demuxer
->file_format
= file_format
;
916 if ((sh_video
= demuxer
->video
->sh
) && sh_video
->bih
) {
917 int biComp
= le2me_32(sh_video
->bih
->biCompression
);
918 mp_msg(MSGT_DEMUX
, MSGL_INFO
,
919 "VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
920 (char *) &biComp
, sh_video
->bih
->biWidth
,
921 sh_video
->bih
->biHeight
, sh_video
->bih
->biBitCount
,
922 sh_video
->fps
, sh_video
->i_bps
* 0.008f
,
923 sh_video
->i_bps
/ 1024.0f
);
926 if (opts
->ass_enabled
&& ass_library
) {
927 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
928 sh_sub_t
*sh
= demuxer
->s_streams
[i
];
929 if (sh
&& sh
->type
== 'a') {
930 sh
->ass_track
= ass_new_track(ass_library
);
931 if (sh
->ass_track
&& sh
->extradata
)
932 ass_process_codec_private(sh
->ass_track
, sh
->extradata
,
934 } else if (sh
&& sh
->type
!= 'v')
935 sh
->ass_track
= ass_default_track(ass_library
);
942 char *audio_stream
= NULL
;
943 char *sub_stream
= NULL
;
944 int audio_stream_cache
= 0;
946 char *demuxer_name
= NULL
; // parameter from -demuxer
947 char *audio_demuxer_name
= NULL
; // parameter from -audio-demuxer
948 char *sub_demuxer_name
= NULL
; // parameter from -sub-demuxer
950 extern int hr_mp3_seek
;
952 extern float stream_cache_min_percent
;
953 extern float stream_cache_seek_min_percent
;
955 demuxer_t
*demux_open(struct MPOpts
*opts
, stream_t
*vs
, int file_format
,
956 int audio_id
, int video_id
, int dvdsub_id
,
959 stream_t
*as
= NULL
, *ss
= NULL
;
960 demuxer_t
*vd
, *ad
= NULL
, *sd
= NULL
;
962 int afmt
= DEMUXER_TYPE_UNKNOWN
, sfmt
= DEMUXER_TYPE_UNKNOWN
;
964 int audio_demuxer_type
= 0, sub_demuxer_type
= 0;
965 int demuxer_force
= 0, audio_demuxer_force
= 0, sub_demuxer_force
= 0;
968 get_demuxer_type_from_name(demuxer_name
, &demuxer_force
)) < 0) {
969 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-demuxer %s does not exist.\n",
972 if ((audio_demuxer_type
=
973 get_demuxer_type_from_name(audio_demuxer_name
,
974 &audio_demuxer_force
)) < 0) {
975 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-audio-demuxer %s does not exist.\n",
978 if ((sub_demuxer_type
=
979 get_demuxer_type_from_name(sub_demuxer_name
,
980 &sub_demuxer_force
)) < 0) {
981 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-sub-demuxer %s does not exist.\n",
986 as
= open_stream(audio_stream
, 0, &afmt
);
988 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "Cannot open audio stream: %s\n",
992 if (audio_stream_cache
) {
993 if (!stream_enable_cache
994 (as
, audio_stream_cache
* 1024,
995 audio_stream_cache
* 1024 * (stream_cache_min_percent
/
997 audio_stream_cache
* 1024 * (stream_cache_seek_min_percent
/
1000 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
1001 "Can't enable audio stream cache\n");
1007 ss
= open_stream(sub_stream
, 0, &sfmt
);
1009 mp_tmsg(MSGT_DEMUXER
, MSGL_ERR
, "Cannot open subtitle stream: %s\n",
1015 vd
= demux_open_stream(opts
, vs
, demuxer_type
? demuxer_type
: file_format
,
1016 demuxer_force
, audio_stream
? -2 : audio_id
,
1017 video_id
, sub_stream
? -2 : dvdsub_id
, filename
);
1026 ad
= demux_open_stream(opts
, as
,
1027 audio_demuxer_type
? audio_demuxer_type
: afmt
,
1028 audio_demuxer_force
, audio_id
, -2, -2,
1031 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
, "Failed to open audio demuxer: %s\n",
1034 } else if (ad
->audio
->sh
1035 && ((sh_audio_t
*) ad
->audio
->sh
)->format
== 0x55) // MP3
1036 hr_mp3_seek
= 1; // Enable high res seeking
1039 sd
= demux_open_stream(opts
, ss
,
1040 sub_demuxer_type
? sub_demuxer_type
: sfmt
,
1041 sub_demuxer_force
, -2, -2, dvdsub_id
,
1044 mp_tmsg(MSGT_DEMUXER
, MSGL_WARN
,
1045 "Failed to open subtitle demuxer: %s\n", sub_stream
);
1051 res
= new_demuxers_demuxer(vd
, ad
, sd
);
1053 res
= new_demuxers_demuxer(vd
, ad
, vd
);
1055 res
= new_demuxers_demuxer(vd
, vd
, sd
);
1059 opts
->correct_pts
= opts
->user_correct_pts
;
1060 if (opts
->correct_pts
< 0)
1062 demux_control(res
, DEMUXER_CTRL_CORRECT_PTS
,
1063 NULL
) == DEMUXER_CTRL_OK
;
1068 void demux_flush(demuxer_t
*demuxer
)
1070 ds_free_packs(demuxer
->video
);
1071 ds_free_packs(demuxer
->audio
);
1072 ds_free_packs(demuxer
->sub
);
1075 int demux_seek(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
,
1078 demux_stream_t
*d_audio
= demuxer
->audio
;
1079 demux_stream_t
*d_video
= demuxer
->video
;
1080 sh_audio_t
*sh_audio
= d_audio
->sh
;
1081 sh_video_t
*sh_video
= d_video
->sh
;
1085 if (!demuxer
->seekable
) {
1086 if (demuxer
->file_format
== DEMUXER_TYPE_AVI
)
1087 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "Cannot seek in raw AVI streams. (Index required, try with the -idx switch.)\n");
1089 else if (demuxer
->file_format
== DEMUXER_TYPE_TV
)
1090 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "TV input is not seekable! (Seeking will probably be for changing channels ;)\n");
1093 mp_tmsg(MSGT_SEEK
, MSGL_WARN
, "Cannot seek in this file.\n");
1096 // clear demux buffers:
1097 demux_flush(demuxer
);
1099 sh_audio
->a_buffer_len
= 0;
1101 demuxer
->stream
->eof
= 0;
1102 demuxer
->video
->eof
= 0;
1103 demuxer
->audio
->eof
= 0;
1106 sh_video
->timer
= 0; // !!!!!!
1108 if (flags
& SEEK_ABSOLUTE
)
1111 if (demuxer
->stream_pts
== MP_NOPTS_VALUE
)
1113 pts
= demuxer
->stream_pts
;
1116 if (flags
& SEEK_FACTOR
) {
1117 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_TIME_LENGTH
, &tmp
)
1118 == STREAM_UNSUPPORTED
)
1120 pts
+= tmp
* rel_seek_secs
;
1122 pts
+= rel_seek_secs
;
1124 if (stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_TIME
, &pts
) !=
1125 STREAM_UNSUPPORTED
) {
1126 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1131 if (demuxer
->desc
->seek
)
1132 demuxer
->desc
->seek(demuxer
, rel_seek_secs
, audio_delay
, flags
);
1135 resync_audio_stream(sh_audio
);
1140 int demux_info_add(demuxer_t
*demuxer
, const char *opt
, const char *param
)
1142 char **info
= demuxer
->info
;
1146 for (n
= 0; info
&& info
[2 * n
] != NULL
; n
++) {
1147 if (!strcasecmp(opt
, info
[2 * n
])) {
1148 if (!strcmp(param
, info
[2 * n
+ 1])) {
1149 mp_msg(MSGT_DEMUX
, MSGL_V
, "Demuxer info %s set to unchanged value %s\n", opt
, param
);
1152 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "Demuxer info %s changed to %s\n", opt
,
1154 free(info
[2 * n
+ 1]);
1155 info
[2 * n
+ 1] = strdup(param
);
1160 info
= demuxer
->info
=
1161 (char **) realloc(info
, (2 * (n
+ 2)) * sizeof(char *));
1162 info
[2 * n
] = strdup(opt
);
1163 info
[2 * n
+ 1] = strdup(param
);
1164 memset(&info
[2 * (n
+ 1)], 0, 2 * sizeof(char *));
1169 int demux_info_print(demuxer_t
*demuxer
)
1171 char **info
= demuxer
->info
;
1177 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "Clip info:\n");
1178 for (n
= 0; info
[2 * n
] != NULL
; n
++) {
1179 mp_msg(MSGT_DEMUX
, MSGL_INFO
, " %s: %s\n", info
[2 * n
],
1181 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_NAME%d=%s\n", n
,
1183 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_VALUE%d=%s\n", n
,
1186 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_N=%d\n", n
);
1191 char *demux_info_get(demuxer_t
*demuxer
, const char *opt
)
1194 char **info
= demuxer
->info
;
1196 for (i
= 0; info
&& info
[2 * i
] != NULL
; i
++) {
1197 if (!strcasecmp(opt
, info
[2 * i
]))
1198 return info
[2 * i
+ 1];
1204 int demux_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
1207 if (demuxer
->desc
->control
)
1208 return demuxer
->desc
->control(demuxer
, cmd
, arg
);
1210 return DEMUXER_CTRL_NOTIMPL
;
1215 double demuxer_get_time_length(demuxer_t
*demuxer
)
1217 double get_time_ans
;
1218 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1219 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1220 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
1222 (demuxer
, DEMUXER_CTRL_GET_TIME_LENGTH
, (void *) &get_time_ans
) <= 0) {
1223 if (sh_video
&& sh_video
->i_bps
&& sh_audio
&& sh_audio
->i_bps
)
1224 get_time_ans
= (double) (demuxer
->movi_end
-
1225 demuxer
->movi_start
) / (sh_video
->i_bps
+
1227 else if (sh_video
&& sh_video
->i_bps
)
1228 get_time_ans
= (double) (demuxer
->movi_end
-
1229 demuxer
->movi_start
) / sh_video
->i_bps
;
1230 else if (sh_audio
&& sh_audio
->i_bps
)
1231 get_time_ans
= (double) (demuxer
->movi_end
-
1232 demuxer
->movi_start
) / sh_audio
->i_bps
;
1236 return get_time_ans
;
1240 * \brief demuxer_get_current_time() returns the time of the current play in three possible ways:
1241 * either when the stream reader satisfies STREAM_CTRL_GET_CURRENT_TIME (e.g. dvd)
1242 * or using sh_video->pts when the former method fails
1244 * \return the current play time
1246 int demuxer_get_current_time(demuxer_t
*demuxer
)
1248 double get_time_ans
= 0;
1249 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1250 if (demuxer
->stream_pts
!= MP_NOPTS_VALUE
)
1251 get_time_ans
= demuxer
->stream_pts
;
1253 get_time_ans
= sh_video
->pts
;
1254 return (int) get_time_ans
;
1257 int demuxer_get_percent_pos(demuxer_t
*demuxer
)
1260 int res
= demux_control(demuxer
, DEMUXER_CTRL_GET_PERCENT_POS
, &ans
);
1261 int len
= (demuxer
->movi_end
- demuxer
->movi_start
) / 100;
1263 off_t pos
= demuxer
->filepos
> 0 ? demuxer
->filepos
: stream_tell(demuxer
->stream
);
1265 ans
= (pos
- demuxer
->movi_start
) / len
;
1276 int demuxer_switch_audio(demuxer_t
*demuxer
, int index
)
1278 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_AUDIO
, &index
);
1279 if (res
== DEMUXER_CTRL_NOTIMPL
)
1280 index
= demuxer
->audio
->id
;
1281 if (demuxer
->audio
->id
>= 0)
1282 demuxer
->audio
->sh
= demuxer
->a_streams
[demuxer
->audio
->id
];
1284 demuxer
->audio
->sh
= NULL
;
1288 int demuxer_switch_video(demuxer_t
*demuxer
, int index
)
1290 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_VIDEO
, &index
);
1291 if (res
== DEMUXER_CTRL_NOTIMPL
)
1292 index
= demuxer
->video
->id
;
1293 if (demuxer
->video
->id
>= 0)
1294 demuxer
->video
->sh
= demuxer
->v_streams
[demuxer
->video
->id
];
1296 demuxer
->video
->sh
= NULL
;
1300 int demuxer_add_attachment(demuxer_t
*demuxer
, const char *name
,
1301 const char *type
, const void *data
, size_t size
)
1303 if (!(demuxer
->num_attachments
& 31))
1304 demuxer
->attachments
= realloc(demuxer
->attachments
,
1305 (demuxer
->num_attachments
+ 32) * sizeof(demux_attachment_t
));
1307 demuxer
->attachments
[demuxer
->num_attachments
].name
= strdup(name
);
1308 demuxer
->attachments
[demuxer
->num_attachments
].type
= strdup(type
);
1309 demuxer
->attachments
[demuxer
->num_attachments
].data
= malloc(size
);
1310 memcpy(demuxer
->attachments
[demuxer
->num_attachments
].data
, data
, size
);
1311 demuxer
->attachments
[demuxer
->num_attachments
].data_size
= size
;
1313 return demuxer
->num_attachments
++;
1316 int demuxer_add_chapter(demuxer_t
*demuxer
, const char *name
, uint64_t start
,
1319 if (demuxer
->chapters
== NULL
)
1320 demuxer
->chapters
= malloc(32 * sizeof(*demuxer
->chapters
));
1321 else if (!(demuxer
->num_chapters
% 32))
1322 demuxer
->chapters
= realloc(demuxer
->chapters
,
1323 (demuxer
->num_chapters
+ 32) *
1324 sizeof(*demuxer
->chapters
));
1326 demuxer
->chapters
[demuxer
->num_chapters
].start
= start
;
1327 demuxer
->chapters
[demuxer
->num_chapters
].end
= end
;
1328 demuxer
->chapters
[demuxer
->num_chapters
].name
= strdup(name
? name
: mp_gtext("unknown"));
1330 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_ID=%d\n", demuxer
->num_chapters
);
1331 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_START=%"PRIu64
"\n", demuxer
->num_chapters
, start
);
1333 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_END=%"PRIu64
"\n", demuxer
->num_chapters
, end
);
1335 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_NAME=%s\n", demuxer
->num_chapters
, name
);
1337 return demuxer
->num_chapters
++;
1341 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1342 * either using the demuxer->chapters structure set by the demuxer
1343 * or asking help to the stream layer (e.g. dvd)
1344 * \param chapter - chapter number wished - 0-based
1345 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1346 * \param chapter_name name of chapter found (set by this function is param is not null)
1347 * \return -1 on error, current chapter if successful
1350 int demuxer_seek_chapter(demuxer_t
*demuxer
, int chapter
, double *seek_pts
,
1351 char **chapter_name
)
1354 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1355 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1357 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1358 demux_flush(demuxer
);
1360 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_CHAPTER
,
1362 if (ris
!= STREAM_UNSUPPORTED
)
1363 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1365 ds_fill_buffer(demuxer
->video
);
1366 resync_video_stream(sh_video
);
1370 ds_fill_buffer(demuxer
->audio
);
1371 resync_audio_stream(sh_audio
);
1373 // exit status may be ok, but main() doesn't have to seek itself
1374 // (because e.g. dvds depend on sectors, not on pts)
1378 *chapter_name
= NULL
;
1380 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1381 &num_chapters
) == STREAM_UNSUPPORTED
)
1384 *chapter_name
= talloc_size(NULL
, 16);
1385 sprintf(*chapter_name
, " of %3d", num_chapters
);
1389 return ris
!= STREAM_UNSUPPORTED
? chapter
: -1;
1390 } else { // chapters structure is set in the demuxer
1391 if (chapter
>= demuxer
->num_chapters
)
1396 *seek_pts
= demuxer
->chapters
[chapter
].start
/ 1000.0;
1399 *chapter_name
= talloc_strdup(NULL
, demuxer
->chapters
[chapter
].name
);
1405 int demuxer_get_current_chapter(demuxer_t
*demuxer
)
1408 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1409 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_CURRENT_CHAPTER
,
1410 &chapter
) == STREAM_UNSUPPORTED
)
1413 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1414 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1416 now
= (sh_video
? sh_video
->pts
: (sh_audio
? sh_audio
->pts
: 0))
1418 for (chapter
= demuxer
->num_chapters
- 1; chapter
>= 0; --chapter
) {
1419 if (demuxer
->chapters
[chapter
].start
<= now
)
1426 char *demuxer_chapter_name(demuxer_t
*demuxer
, int chapter
)
1428 if (demuxer
->num_chapters
&& demuxer
->chapters
) {
1429 if (chapter
>= 0 && chapter
< demuxer
->num_chapters
1430 && demuxer
->chapters
[chapter
].name
)
1431 return strdup(demuxer
->chapters
[chapter
].name
);
1436 char *demuxer_chapter_display_name(demuxer_t
*demuxer
, int chapter
)
1438 char *chapter_name
= demuxer_chapter_name(demuxer
, chapter
);
1440 char *tmp
= malloc(strlen(chapter_name
) + 14);
1441 snprintf(tmp
, 63, "(%d) %s", chapter
+ 1, chapter_name
);
1445 int chapter_num
= demuxer_chapter_count(demuxer
);
1447 if (chapter_num
<= 0)
1448 sprintf(tmp
, "(%d)", chapter
+ 1);
1450 sprintf(tmp
, "(%d) of %d", chapter
+ 1, chapter_num
);
1455 float demuxer_chapter_time(demuxer_t
*demuxer
, int chapter
, float *end
)
1457 if (demuxer
->num_chapters
&& demuxer
->chapters
&& chapter
>= 0
1458 && chapter
< demuxer
->num_chapters
) {
1460 *end
= demuxer
->chapters
[chapter
].end
/ 1000.0;
1461 return demuxer
->chapters
[chapter
].start
/ 1000.0;
1466 int demuxer_chapter_count(demuxer_t
*demuxer
)
1468 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1469 int num_chapters
= 0;
1470 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1471 &num_chapters
) == STREAM_UNSUPPORTED
)
1473 return num_chapters
;
1475 return demuxer
->num_chapters
;
1478 int demuxer_angles_count(demuxer_t
*demuxer
)
1480 int ris
, angles
= -1;
1482 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_ANGLES
, &angles
);
1483 if (ris
== STREAM_UNSUPPORTED
)
1488 int demuxer_get_current_angle(demuxer_t
*demuxer
)
1490 int ris
, curr_angle
= -1;
1491 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_ANGLE
, &curr_angle
);
1492 if (ris
== STREAM_UNSUPPORTED
)
1498 int demuxer_set_angle(demuxer_t
*demuxer
, int angle
)
1500 int ris
, angles
= -1;
1501 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1502 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1504 angles
= demuxer_angles_count(demuxer
);
1505 if ((angles
< 1) || (angle
> angles
))
1508 demux_flush(demuxer
);
1510 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SET_ANGLE
, &angle
);
1511 if (ris
== STREAM_UNSUPPORTED
)
1514 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1516 ds_fill_buffer(demuxer
->video
);
1517 resync_video_stream(sh_video
);
1521 ds_fill_buffer(demuxer
->audio
);
1522 resync_audio_stream(sh_audio
);
1528 int demuxer_audio_track_by_lang(demuxer_t
*d
, char *lang
)
1531 lang
+= strspn(lang
, ",");
1532 while ((len
= strcspn(lang
, ",")) > 0) {
1533 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1534 sh_audio_t
*sh
= d
->a_streams
[i
];
1535 if (sh
&& sh
->lang
&& strncmp(sh
->lang
, lang
, len
) == 0)
1539 lang
+= strspn(lang
, ",");
1544 int demuxer_sub_track_by_lang(demuxer_t
*d
, char *lang
)
1547 lang
+= strspn(lang
, ",");
1548 while ((len
= strcspn(lang
, ",")) > 0) {
1549 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1550 sh_sub_t
*sh
= d
->s_streams
[i
];
1551 if (sh
&& sh
->lang
&& strncmp(sh
->lang
, lang
, len
) == 0)
1555 lang
+= strspn(lang
, ",");
1560 int demuxer_default_audio_track(demuxer_t
*d
)
1563 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1564 sh_audio_t
*sh
= d
->a_streams
[i
];
1565 if (sh
&& sh
->default_track
)
1568 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1569 sh_audio_t
*sh
= d
->a_streams
[i
];
1576 int demuxer_default_sub_track(demuxer_t
*d
)
1579 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1580 sh_sub_t
*sh
= d
->s_streams
[i
];
1581 if (sh
&& sh
->default_track
)