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"
44 #include "libass/ass.h"
45 #include "libass/ass_mp.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_st
*demuxer
, int id
)
189 demux_stream_t
*ds
= malloc(sizeof(demux_stream_t
));
190 ds
->buffer_pos
= ds
->buffer_size
= 0;
201 ds
->first
= ds
->last
= ds
->current
= NULL
;
203 ds
->demuxer
= demuxer
;
206 ds
->asf_packet
= NULL
;
208 ds
->ss_mul
= ds
->ss_div
= 0;
216 * Get demuxer description structure for a given demuxer type
218 * @param file_format type of the demuxer
219 * @return structure for the demuxer, NULL if not found
221 static const demuxer_desc_t
*get_demuxer_desc_from_type(int file_format
)
225 for (i
= 0; demuxer_list
[i
]; i
++)
226 if (file_format
== demuxer_list
[i
]->type
)
227 return demuxer_list
[i
];
233 demuxer_t
*new_demuxer(stream_t
*stream
, int type
, int a_id
, int v_id
,
234 int s_id
, char *filename
)
236 demuxer_t
*d
= malloc(sizeof(demuxer_t
));
237 memset(d
, 0, sizeof(demuxer_t
));
239 d
->stream_pts
= MP_NOPTS_VALUE
;
240 d
->reference_clock
= MP_NOPTS_VALUE
;
241 d
->movi_start
= stream
->start_pos
;
242 d
->movi_end
= stream
->end_pos
;
246 d
->audio
= new_demuxer_stream(d
, a_id
);
247 d
->video
= new_demuxer_stream(d
, v_id
);
248 d
->sub
= new_demuxer_stream(d
, s_id
);
251 if (!(d
->desc
= get_demuxer_desc_from_type(type
)))
252 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
253 "BUG! Invalid demuxer type in new_demuxer(), "
254 "big troubles ahead.");
255 if (filename
) // Filename hack for avs_check_file
256 d
->filename
= strdup(filename
);
257 stream_reset(stream
);
258 stream_seek(stream
, stream
->start_pos
);
262 extern int dvdsub_id
;
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 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SUBTITLE_ID=%d\n", sid
);
280 if (sid
== dvdsub_id
) {
281 demuxer
->sub
->id
= id
;
282 demuxer
->sub
->sh
= demuxer
->s_streams
[id
];
284 return demuxer
->s_streams
[id
];
287 void free_sh_sub(sh_sub_t
*sh
)
289 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_sub at %p\n", sh
);
293 ass_free_track(sh
->ass_track
);
299 sh_audio_t
*new_sh_audio_aid(demuxer_t
*demuxer
, int id
, int aid
)
301 if (id
> MAX_A_STREAMS
- 1 || id
< 0) {
302 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
303 "Requested audio stream id overflow (%d > %d)\n", id
,
307 if (demuxer
->a_streams
[id
])
308 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, MSGTR_AudioStreamRedefined
, id
);
310 sh_audio_t
*sh
= calloc(1, sizeof(sh_audio_t
));
311 mp_msg(MSGT_DEMUXER
, MSGL_V
, MSGTR_FoundAudioStream
, id
);
312 demuxer
->a_streams
[id
] = sh
;
314 sh
->ds
= demuxer
->audio
;
317 sh
->sample_format
= AF_FORMAT_S16_NE
;
318 sh
->audio_out_minsize
= 8192; /* default size, maybe not enough for Win32/ACM */
319 sh
->pts
= MP_NOPTS_VALUE
;
320 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_ID=%d\n", aid
);
322 return demuxer
->a_streams
[id
];
325 void free_sh_audio(demuxer_t
*demuxer
, int id
)
327 sh_audio_t
*sh
= demuxer
->a_streams
[id
];
328 demuxer
->a_streams
[id
] = NULL
;
329 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_audio at %p\n", sh
);
336 sh_video_t
*new_sh_video_vid(demuxer_t
*demuxer
, int id
, int vid
)
338 if (id
> MAX_V_STREAMS
- 1 || id
< 0) {
339 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
340 "Requested video stream id overflow (%d > %d)\n", id
,
344 if (demuxer
->v_streams
[id
])
345 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, MSGTR_VideoStreamRedefined
, id
);
347 sh_video_t
*sh
= calloc(1, sizeof(sh_video_t
));
348 mp_msg(MSGT_DEMUXER
, MSGL_V
, MSGTR_FoundVideoStream
, id
);
349 demuxer
->v_streams
[id
] = sh
;
351 sh
->ds
= demuxer
->video
;
352 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_ID=%d\n", vid
);
354 return demuxer
->v_streams
[id
];
357 void free_sh_video(sh_video_t
*sh
)
359 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_video at %p\n", sh
);
364 void free_demuxer(demuxer_t
*demuxer
)
367 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing demuxer at %p\n",
369 if (demuxer
->desc
->close
)
370 demuxer
->desc
->close(demuxer
);
371 // Very ugly hack to make it behave like old implementation
372 if (demuxer
->desc
->type
== DEMUXER_TYPE_DEMUXERS
)
373 goto skip_streamfree
;
375 for (i
= 0; i
< MAX_A_STREAMS
; i
++)
376 if (demuxer
->a_streams
[i
])
377 free_sh_audio(demuxer
, i
);
378 for (i
= 0; i
< MAX_V_STREAMS
; i
++)
379 if (demuxer
->v_streams
[i
])
380 free_sh_video(demuxer
->v_streams
[i
]);
381 for (i
= 0; i
< MAX_S_STREAMS
; i
++)
382 if (demuxer
->s_streams
[i
])
383 free_sh_sub(demuxer
->s_streams
[i
]);
385 free_demuxer_stream(demuxer
->audio
);
386 free_demuxer_stream(demuxer
->video
);
387 free_demuxer_stream(demuxer
->sub
);
390 for (i
= 0; demuxer
->info
[i
] != NULL
; i
++)
391 free(demuxer
->info
[i
]);
394 free(demuxer
->filename
);
395 if (demuxer
->chapters
) {
396 for (i
= 0; i
< demuxer
->num_chapters
; i
++)
397 free(demuxer
->chapters
[i
].name
);
398 free(demuxer
->chapters
);
400 if (demuxer
->attachments
) {
401 for (i
= 0; i
< demuxer
->num_attachments
; i
++) {
402 free(demuxer
->attachments
[i
].name
);
403 free(demuxer
->attachments
[i
].type
);
404 free(demuxer
->attachments
[i
].data
);
406 free(demuxer
->attachments
);
412 void ds_add_packet(demux_stream_t
*ds
, demux_packet_t
*dp
)
414 // append packet to DS stream:
416 ds
->bytes
+= dp
->len
;
418 // next packet in stream
422 // first packet in stream
423 ds
->first
= ds
->last
= dp
;
425 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
426 "DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
427 (ds
== ds
->demuxer
->audio
) ? "d_audio" : "d_video", dp
->len
,
428 dp
->pts
, (unsigned int) dp
->pos
, ds
->demuxer
->audio
->packs
,
429 ds
->demuxer
->video
->packs
);
432 void ds_read_packet(demux_stream_t
*ds
, stream_t
*stream
, int len
,
433 double pts
, off_t pos
, int flags
)
435 demux_packet_t
*dp
= new_demux_packet(len
);
436 len
= stream_read(stream
, dp
->buffer
, len
);
437 resize_demux_packet(dp
, len
);
441 // append packet to DS stream:
442 ds_add_packet(ds
, dp
);
446 // 0 = EOF or no stream found or invalid type
447 // 1 = successfully read a packet
449 int demux_fill_buffer(demuxer_t
*demux
, demux_stream_t
*ds
)
451 // Note: parameter 'ds' can be NULL!
452 return demux
->desc
->fill_buffer(demux
, ds
);
458 #define MAX_ACUMULATED_PACKETS 64
459 int ds_fill_buffer(demux_stream_t
*ds
)
461 demuxer_t
*demux
= ds
->demuxer
;
463 free_demux_packet(ds
->current
);
465 if (mp_msg_test(MSGT_DEMUXER
, MSGL_DBG3
)) {
466 if (ds
== demux
->audio
)
467 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
468 "ds_fill_buffer(d_audio) called\n");
469 else if (ds
== demux
->video
)
470 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
471 "ds_fill_buffer(d_video) called\n");
472 else if (ds
== demux
->sub
)
473 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
, "ds_fill_buffer(d_sub) called\n");
475 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
476 "ds_fill_buffer(unknown 0x%X) called\n", (unsigned int) ds
);
480 demux_packet_t
*p
= ds
->first
;
482 if (demux
->reference_clock
!= MP_NOPTS_VALUE
) {
483 if ( p
->pts
!= MP_NOPTS_VALUE
484 && p
->pts
> demux
->reference_clock
485 && ds
->packs
< MAX_ACUMULATED_PACKETS
) {
486 if (demux_fill_buffer(demux
, ds
))
492 ds
->buffer
= p
->buffer
;
494 ds
->buffer_size
= p
->len
;
496 ds
->dpos
+= p
->len
; // !!!
498 if (p
->pts
!= MP_NOPTS_VALUE
) {
502 ds
->pts_bytes
+= p
->len
; // !!!
503 if (p
->stream_pts
!= MP_NOPTS_VALUE
)
504 demux
->stream_pts
= p
->stream_pts
;
505 ds
->flags
= p
->flags
;
515 if (demux
->audio
->packs
>= MAX_PACKS
516 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
517 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_TooManyAudioInBuffer
,
518 demux
->audio
->packs
, demux
->audio
->bytes
);
519 mp_msg(MSGT_DEMUXER
, MSGL_HINT
, MSGTR_MaybeNI
);
522 if (demux
->video
->packs
>= MAX_PACKS
523 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
524 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_TooManyVideoInBuffer
,
525 demux
->video
->packs
, demux
->video
->bytes
);
526 mp_msg(MSGT_DEMUXER
, MSGL_HINT
, MSGTR_MaybeNI
);
529 if (!demux_fill_buffer(demux
, ds
)) {
530 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
531 "ds_fill_buffer()->demux_fill_buffer() failed\n");
535 ds
->buffer_pos
= ds
->buffer_size
= 0;
537 mp_msg(MSGT_DEMUXER
, MSGL_V
,
538 "ds_fill_buffer: EOF reached (stream: %s) \n",
539 ds
== demux
->audio
? "audio" : "video");
544 int demux_read_data(demux_stream_t
*ds
, unsigned char *mem
, int len
)
549 x
= ds
->buffer_size
- ds
->buffer_pos
;
551 if (!ds_fill_buffer(ds
))
557 fast_memcpy(mem
+ bytes
, &ds
->buffer
[ds
->buffer_pos
], x
);
567 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
568 * \param mem memory to read data into, may be NULL to discard data
569 * \param maxlen maximum number of bytes to read
570 * \param read number of bytes actually read
571 * \param pattern pattern to search for (lowest 8 bits are ignored)
572 * \return whether pattern was found
574 int demux_pattern_3(demux_stream_t
*ds
, unsigned char *mem
, int maxlen
,
575 int *read
, uint32_t pattern
)
577 register uint32_t head
= 0xffffff00;
578 register uint32_t pat
= pattern
& 0xffffff00;
581 register unsigned char *ds_buf
= &ds
->buffer
[ds
->buffer_size
];
582 int len
= ds
->buffer_size
- ds
->buffer_pos
;
583 register long pos
= -len
;
584 if (unlikely(pos
>= 0)) { // buffer is empty
591 } while (++pos
&& head
!= pat
);
593 if (total_len
+ len
> maxlen
)
594 len
= maxlen
- total_len
;
595 len
= demux_read_data(ds
, mem
? &mem
[total_len
] : NULL
, len
);
597 } while ((head
!= pat
|| total_len
< 3) && total_len
< maxlen
&& !ds
->eof
);
600 return total_len
>= 3 && head
== pat
;
603 void ds_free_packs(demux_stream_t
*ds
)
605 demux_packet_t
*dp
= ds
->first
;
607 demux_packet_t
*dn
= dp
->next
;
608 free_demux_packet(dp
);
611 if (ds
->asf_packet
) {
612 // free unfinished .asf fragments:
613 free(ds
->asf_packet
->buffer
);
614 free(ds
->asf_packet
);
615 ds
->asf_packet
= NULL
;
617 ds
->first
= ds
->last
= NULL
;
618 ds
->packs
= 0; // !!!!!
621 free_demux_packet(ds
->current
);
624 ds
->buffer_pos
= ds
->buffer_size
;
629 int ds_get_packet(demux_stream_t
*ds
, unsigned char **start
)
632 if (ds
->buffer_pos
>= ds
->buffer_size
) {
633 if (!ds_fill_buffer(ds
)) {
639 len
= ds
->buffer_size
- ds
->buffer_pos
;
640 *start
= &ds
->buffer
[ds
->buffer_pos
];
641 ds
->buffer_pos
+= len
;
645 int ds_get_packet_pts(demux_stream_t
*ds
, unsigned char **start
, double *pts
)
648 *pts
= MP_NOPTS_VALUE
;
649 if (ds
->buffer_pos
>= ds
->buffer_size
) {
650 if (!ds_fill_buffer(ds
)) {
656 // Return pts unless this read starts from the middle of a packet
658 *pts
= ds
->current
->pts
;
659 len
= ds
->buffer_size
- ds
->buffer_pos
;
660 *start
= &ds
->buffer
[ds
->buffer_pos
];
661 ds
->buffer_pos
+= len
;
665 int ds_get_packet_sub(demux_stream_t
*ds
, unsigned char **start
)
668 if (ds
->buffer_pos
>= ds
->buffer_size
) {
672 if (!ds_fill_buffer(ds
))
675 len
= ds
->buffer_size
- ds
->buffer_pos
;
676 *start
= &ds
->buffer
[ds
->buffer_pos
];
677 ds
->buffer_pos
+= len
;
681 double ds_get_next_pts(demux_stream_t
*ds
)
683 demuxer_t
*demux
= ds
->demuxer
;
685 if (demux
->audio
->packs
>= MAX_PACKS
686 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
687 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_TooManyAudioInBuffer
,
688 demux
->audio
->packs
, demux
->audio
->bytes
);
689 mp_msg(MSGT_DEMUXER
, MSGL_HINT
, MSGTR_MaybeNI
);
690 return MP_NOPTS_VALUE
;
692 if (demux
->video
->packs
>= MAX_PACKS
693 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
694 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_TooManyVideoInBuffer
,
695 demux
->video
->packs
, demux
->video
->bytes
);
696 mp_msg(MSGT_DEMUXER
, MSGL_HINT
, MSGTR_MaybeNI
);
697 return MP_NOPTS_VALUE
;
699 if (!demux_fill_buffer(demux
, ds
))
700 return MP_NOPTS_VALUE
;
702 return ds
->first
->pts
;
705 // ====================================================================
707 void demuxer_help(void)
711 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "Available demuxers:\n");
712 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, " demuxer: type info: (comment)\n");
713 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DEMUXERS\n");
714 for (i
= 0; demuxer_list
[i
]; i
++) {
715 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Don't display special demuxers
717 if (demuxer_list
[i
]->comment
&& strlen(demuxer_list
[i
]->comment
))
718 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s (%s)\n",
719 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
720 demuxer_list
[i
]->info
, demuxer_list
[i
]->comment
);
722 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s\n",
723 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
724 demuxer_list
[i
]->info
);
730 * Get demuxer type for a given demuxer name
732 * @param demuxer_name string with demuxer name of demuxer number
733 * @param force will be set if demuxer should be forced.
735 * @return DEMUXER_TYPE_xxx, -1 if error or not found
737 int get_demuxer_type_from_name(char *demuxer_name
, int *force
)
743 if (!demuxer_name
|| !demuxer_name
[0])
744 return DEMUXER_TYPE_UNKNOWN
;
746 *force
= demuxer_name
[0] == '+';
747 if (demuxer_name
[0] == '+')
748 demuxer_name
= &demuxer_name
[1];
749 for (i
= 0; demuxer_list
[i
]; i
++) {
750 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Can't select special demuxers from commandline
752 if (strcmp(demuxer_name
, demuxer_list
[i
]->name
) == 0)
753 return demuxer_list
[i
]->type
;
756 // No match found, try to parse name as an integer (demuxer number)
757 type_int
= strtol(demuxer_name
, &endptr
, 0);
758 if (*endptr
) // Conversion failed
760 if ((type_int
> 0) && (type_int
<= DEMUXER_TYPE_MAX
))
761 return (int) type_int
;
766 int extension_parsing
= 1; // 0=off 1=mixed (used only for unstable formats)
769 int user_correct_pts
= -1;
772 NOTE : Several demuxers may be opened at the same time so
773 demuxers should NEVER rely on an external var to enable them
774 self. If a demuxer can't do any auto-detection it should only use
775 file_format. The user can explicitly set file_format with the -demuxer
776 option so there is really no need for another extra var.
777 For convenience an option can be added to set file_format directly
778 to the right type (ex: rawaudio,rawvideo).
779 Also the stream can override the file_format so a demuxer which rely
780 on a special stream type can set file_format at the stream level
784 static demuxer_t
*demux_open_stream(stream_t
*stream
, int file_format
,
785 int force
, int audio_id
, int video_id
,
786 int dvdsub_id
, char *filename
)
788 demuxer_t
*demuxer
= NULL
;
790 sh_video_t
*sh_video
= NULL
;
792 const demuxer_desc_t
*demuxer_desc
;
796 // If somebody requested a demuxer check it
798 if ((demuxer_desc
= get_demuxer_desc_from_type(file_format
))) {
799 demuxer
= new_demuxer(stream
, demuxer_desc
->type
, audio_id
,
800 video_id
, dvdsub_id
, filename
);
801 if (demuxer_desc
->check_file
)
802 fformat
= demuxer_desc
->check_file(demuxer
);
803 if (force
|| !demuxer_desc
->check_file
)
804 fformat
= demuxer_desc
->type
;
806 if (fformat
== demuxer_desc
->type
) {
807 demuxer_t
*demux2
= demuxer
;
808 // Move messages to demuxer detection code?
809 mp_msg(MSGT_DEMUXER
, MSGL_INFO
,
810 MSGTR_Detected_XXX_FileFormat
,
811 demuxer_desc
->shortdesc
);
812 file_format
= fformat
;
813 if (!demuxer
->desc
->open
814 || (demux2
= demuxer
->desc
->open(demuxer
))) {
819 // Format changed after check, recurse
820 free_demuxer(demuxer
);
821 return demux_open_stream(stream
, fformat
, force
, audio_id
,
822 video_id
, dvdsub_id
, filename
);
825 // Check failed for forced demuxer, quit
826 free_demuxer(demuxer
);
830 // Test demuxers with safe file checks
831 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
832 if (demuxer_desc
->safe_check
) {
833 demuxer
= new_demuxer(stream
, demuxer_desc
->type
, audio_id
,
834 video_id
, dvdsub_id
, filename
);
835 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
836 if (fformat
== demuxer_desc
->type
) {
837 demuxer_t
*demux2
= demuxer
;
838 mp_msg(MSGT_DEMUXER
, MSGL_INFO
,
839 MSGTR_Detected_XXX_FileFormat
,
840 demuxer_desc
->shortdesc
);
841 file_format
= fformat
;
842 if (!demuxer
->desc
->open
843 || (demux2
= demuxer
->desc
->open(demuxer
))) {
848 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
849 return demuxer
; // handled in mplayer.c
850 // Format changed after check, recurse
851 free_demuxer(demuxer
);
852 demuxer
= demux_open_stream(stream
, fformat
, force
,
854 dvdsub_id
, filename
);
856 return demuxer
; // done!
857 file_format
= DEMUXER_TYPE_UNKNOWN
;
860 free_demuxer(demuxer
);
865 // If no forced demuxer perform file extension based detection
866 // Ok. We're over the stable detectable fileformats, the next ones are
867 // a bit fuzzy. So by default (extension_parsing==1) try extension-based
869 if (file_format
== DEMUXER_TYPE_UNKNOWN
&& filename
870 && extension_parsing
== 1) {
871 file_format
= demuxer_type_by_filename(filename
);
872 if (file_format
!= DEMUXER_TYPE_UNKNOWN
) {
873 // we like recursion :)
874 demuxer
= demux_open_stream(stream
, file_format
, force
, audio_id
,
875 video_id
, dvdsub_id
, filename
);
877 return demuxer
; // done!
878 file_format
= DEMUXER_TYPE_UNKNOWN
; // continue fuzzy guessing...
879 mp_msg(MSGT_DEMUXER
, MSGL_V
,
880 "demuxer: continue fuzzy content-based format guessing...\n");
883 // Try detection for all other demuxers
884 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
885 if (!demuxer_desc
->safe_check
&& demuxer_desc
->check_file
) {
886 demuxer
= new_demuxer(stream
, demuxer_desc
->type
, audio_id
,
887 video_id
, dvdsub_id
, filename
);
888 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
889 if (fformat
== demuxer_desc
->type
) {
890 demuxer_t
*demux2
= demuxer
;
891 mp_msg(MSGT_DEMUXER
, MSGL_INFO
,
892 MSGTR_Detected_XXX_FileFormat
,
893 demuxer_desc
->shortdesc
);
894 file_format
= fformat
;
895 if (!demuxer
->desc
->open
896 || (demux2
= demuxer
->desc
->open(demuxer
))) {
901 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
902 return demuxer
; // handled in mplayer.c
903 // Format changed after check, recurse
904 free_demuxer(demuxer
);
905 demuxer
= demux_open_stream(stream
, fformat
, force
,
907 dvdsub_id
, filename
);
909 return demuxer
; // done!
910 file_format
= DEMUXER_TYPE_UNKNOWN
;
913 free_demuxer(demuxer
);
919 //====== File format recognized, set up these for compatibility: =========
922 demuxer
->file_format
= file_format
;
924 if ((sh_video
= demuxer
->video
->sh
) && sh_video
->bih
) {
925 int biComp
= le2me_32(sh_video
->bih
->biCompression
);
926 mp_msg(MSGT_DEMUX
, MSGL_INFO
,
927 "VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
928 (char *) &biComp
, sh_video
->bih
->biWidth
,
929 sh_video
->bih
->biHeight
, sh_video
->bih
->biBitCount
,
930 sh_video
->fps
, sh_video
->i_bps
* 0.008f
,
931 sh_video
->i_bps
/ 1024.0f
);
934 if (ass_enabled
&& ass_library
) {
935 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
936 sh_sub_t
*sh
= demuxer
->s_streams
[i
];
937 if (sh
&& sh
->type
== 'a') {
938 sh
->ass_track
= ass_new_track(ass_library
);
939 if (sh
->ass_track
&& sh
->extradata
)
940 ass_process_codec_private(sh
->ass_track
, sh
->extradata
,
942 } else if (sh
&& sh
->type
!= 'v')
943 sh
->ass_track
= ass_default_track(ass_library
);
950 char *audio_stream
= NULL
;
951 char *sub_stream
= NULL
;
952 int audio_stream_cache
= 0;
954 char *demuxer_name
= NULL
; // parameter from -demuxer
955 char *audio_demuxer_name
= NULL
; // parameter from -audio-demuxer
956 char *sub_demuxer_name
= NULL
; // parameter from -sub-demuxer
958 extern int hr_mp3_seek
;
960 extern float stream_cache_min_percent
;
961 extern float stream_cache_seek_min_percent
;
963 demuxer_t
*demux_open(stream_t
*vs
, int file_format
, int audio_id
,
964 int video_id
, int dvdsub_id
, char *filename
)
966 stream_t
*as
= NULL
, *ss
= NULL
;
967 demuxer_t
*vd
, *ad
= NULL
, *sd
= NULL
;
969 int afmt
= DEMUXER_TYPE_UNKNOWN
, sfmt
= DEMUXER_TYPE_UNKNOWN
;
971 int audio_demuxer_type
= 0, sub_demuxer_type
= 0;
972 int demuxer_force
= 0, audio_demuxer_force
= 0, sub_demuxer_force
= 0;
975 get_demuxer_type_from_name(demuxer_name
, &demuxer_force
)) < 0) {
976 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-demuxer %s does not exist.\n",
979 if ((audio_demuxer_type
=
980 get_demuxer_type_from_name(audio_demuxer_name
,
981 &audio_demuxer_force
)) < 0) {
982 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-audio-demuxer %s does not exist.\n",
985 if ((sub_demuxer_type
=
986 get_demuxer_type_from_name(sub_demuxer_name
,
987 &sub_demuxer_force
)) < 0) {
988 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-sub-demuxer %s does not exist.\n",
993 as
= open_stream(audio_stream
, 0, &afmt
);
995 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_CannotOpenAudioStream
,
999 if (audio_stream_cache
) {
1000 if (!stream_enable_cache
1001 (as
, audio_stream_cache
* 1024,
1002 audio_stream_cache
* 1024 * (stream_cache_min_percent
/
1004 audio_stream_cache
* 1024 * (stream_cache_seek_min_percent
/
1007 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
1008 "Can't enable audio stream cache\n");
1014 ss
= open_stream(sub_stream
, 0, &sfmt
);
1016 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_CannotOpenSubtitlesStream
,
1022 vd
= demux_open_stream(vs
, demuxer_type
? demuxer_type
: file_format
,
1023 demuxer_force
, audio_stream
? -2 : audio_id
,
1024 video_id
, sub_stream
? -2 : dvdsub_id
, filename
);
1033 ad
= demux_open_stream(as
,
1034 audio_demuxer_type
? audio_demuxer_type
: afmt
,
1035 audio_demuxer_force
, audio_id
, -2, -2,
1038 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, MSGTR_OpeningAudioDemuxerFailed
,
1041 } else if (ad
->audio
->sh
1042 && ((sh_audio_t
*) ad
->audio
->sh
)->format
== 0x55) // MP3
1043 hr_mp3_seek
= 1; // Enable high res seeking
1046 sd
= demux_open_stream(ss
, sub_demuxer_type
? sub_demuxer_type
: sfmt
,
1047 sub_demuxer_force
, -2, -2, dvdsub_id
,
1050 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
1051 MSGTR_OpeningSubtitlesDemuxerFailed
, sub_stream
);
1057 res
= new_demuxers_demuxer(vd
, ad
, sd
);
1059 res
= new_demuxers_demuxer(vd
, ad
, vd
);
1061 res
= new_demuxers_demuxer(vd
, vd
, sd
);
1065 correct_pts
= user_correct_pts
;
1066 if (correct_pts
< 0)
1067 correct_pts
= demux_control(res
, DEMUXER_CTRL_CORRECT_PTS
, NULL
)
1073 void demux_flush(demuxer_t
*demuxer
)
1075 ds_free_packs(demuxer
->video
);
1076 ds_free_packs(demuxer
->audio
);
1077 ds_free_packs(demuxer
->sub
);
1080 int demux_seek(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
,
1083 demux_stream_t
*d_audio
= demuxer
->audio
;
1084 demux_stream_t
*d_video
= demuxer
->video
;
1085 sh_audio_t
*sh_audio
= d_audio
->sh
;
1086 sh_video_t
*sh_video
= d_video
->sh
;
1090 if (!demuxer
->seekable
) {
1091 if (demuxer
->file_format
== DEMUXER_TYPE_AVI
)
1092 mp_msg(MSGT_SEEK
, MSGL_WARN
, MSGTR_CantSeekRawAVI
);
1094 else if (demuxer
->file_format
== DEMUXER_TYPE_TV
)
1095 mp_msg(MSGT_SEEK
, MSGL_WARN
, MSGTR_TVInputNotSeekable
);
1098 mp_msg(MSGT_SEEK
, MSGL_WARN
, MSGTR_CantSeekFile
);
1102 demux_flush(demuxer
);
1103 // clear demux buffers:
1105 sh_audio
->a_buffer_len
= 0;
1107 demuxer
->stream
->eof
= 0;
1108 demuxer
->video
->eof
= 0;
1109 demuxer
->audio
->eof
= 0;
1112 sh_video
->timer
= 0; // !!!!!!
1114 if (flags
& SEEK_ABSOLUTE
)
1117 if (demuxer
->stream_pts
== MP_NOPTS_VALUE
)
1119 pts
= demuxer
->stream_pts
;
1122 if (flags
& SEEK_FACTOR
) {
1123 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_TIME_LENGTH
, &tmp
)
1124 == STREAM_UNSUPPORTED
)
1126 pts
+= tmp
* rel_seek_secs
;
1128 pts
+= rel_seek_secs
;
1130 if (stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_TIME
, &pts
) !=
1131 STREAM_UNSUPPORTED
) {
1132 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1137 if (demuxer
->desc
->seek
)
1138 demuxer
->desc
->seek(demuxer
, rel_seek_secs
, audio_delay
, flags
);
1141 resync_audio_stream(sh_audio
);
1146 int demux_info_add(demuxer_t
*demuxer
, const char *opt
, const char *param
)
1148 char **info
= demuxer
->info
;
1152 for (n
= 0; info
&& info
[2 * n
] != NULL
; n
++) {
1153 if (!strcasecmp(opt
, info
[2 * n
])) {
1154 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_DemuxerInfoChanged
, opt
,
1156 free(info
[2 * n
+ 1]);
1157 info
[2 * n
+ 1] = strdup(param
);
1162 info
= demuxer
->info
= (char **) realloc(info
,
1163 (2 * (n
+ 2)) * sizeof(char *));
1164 info
[2 * n
] = strdup(opt
);
1165 info
[2 * n
+ 1] = strdup(param
);
1166 memset(&info
[2 * (n
+ 1)], 0, 2 * sizeof(char *));
1171 int demux_info_print(demuxer_t
*demuxer
)
1173 char **info
= demuxer
->info
;
1179 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_ClipInfo
);
1180 for (n
= 0; info
[2 * n
] != NULL
; n
++) {
1181 mp_msg(MSGT_DEMUX
, MSGL_INFO
, " %s: %s\n", info
[2 * n
],
1183 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_NAME%d=%s\n", n
,
1185 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_VALUE%d=%s\n", n
,
1188 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_N=%d\n", n
);
1193 char *demux_info_get(demuxer_t
*demuxer
, const char *opt
)
1196 char **info
= demuxer
->info
;
1198 for (i
= 0; info
&& info
[2 * i
] != NULL
; i
++) {
1199 if (!strcasecmp(opt
, info
[2 * i
]))
1200 return info
[2 * i
+ 1];
1206 int demux_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
1209 if (demuxer
->desc
->control
)
1210 return demuxer
->desc
->control(demuxer
, cmd
, arg
);
1212 return DEMUXER_CTRL_NOTIMPL
;
1217 double demuxer_get_time_length(demuxer_t
*demuxer
)
1219 double get_time_ans
;
1220 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1221 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1222 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
1224 (demuxer
, DEMUXER_CTRL_GET_TIME_LENGTH
, (void *) &get_time_ans
) <= 0) {
1225 if (sh_video
&& sh_video
->i_bps
&& sh_audio
&& sh_audio
->i_bps
)
1226 get_time_ans
= (double) (demuxer
->movi_end
-
1227 demuxer
->movi_start
) / (sh_video
->i_bps
+
1229 else if (sh_video
&& sh_video
->i_bps
)
1230 get_time_ans
= (double) (demuxer
->movi_end
-
1231 demuxer
->movi_start
) / sh_video
->i_bps
;
1232 else if (sh_audio
&& sh_audio
->i_bps
)
1233 get_time_ans
= (double) (demuxer
->movi_end
-
1234 demuxer
->movi_start
) / sh_audio
->i_bps
;
1238 return get_time_ans
;
1242 * \brief demuxer_get_current_time() returns the time of the current play in three possible ways:
1243 * either when the stream reader satisfies STREAM_CTRL_GET_CURRENT_TIME (e.g. dvd)
1244 * or using sh_video->pts when the former method fails
1246 * \return the current play time
1248 int demuxer_get_current_time(demuxer_t
*demuxer
)
1250 double get_time_ans
= 0;
1251 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1252 if (demuxer
->stream_pts
!= MP_NOPTS_VALUE
)
1253 get_time_ans
= demuxer
->stream_pts
;
1255 get_time_ans
= sh_video
->pts
;
1256 return (int) get_time_ans
;
1259 int demuxer_get_percent_pos(demuxer_t
*demuxer
)
1262 int res
= demux_control(demuxer
, DEMUXER_CTRL_GET_PERCENT_POS
, &ans
);
1263 int len
= (demuxer
->movi_end
- demuxer
->movi_start
) / 100;
1266 ans
= (demuxer
->filepos
- demuxer
->movi_start
) / len
;
1277 int demuxer_switch_audio(demuxer_t
*demuxer
, int index
)
1279 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_AUDIO
, &index
);
1280 if (res
== DEMUXER_CTRL_NOTIMPL
)
1281 index
= demuxer
->audio
->id
;
1282 if (demuxer
->audio
->id
>= 0)
1283 demuxer
->audio
->sh
= demuxer
->a_streams
[demuxer
->audio
->id
];
1285 demuxer
->audio
->sh
= NULL
;
1289 int demuxer_switch_video(demuxer_t
*demuxer
, int index
)
1291 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_VIDEO
, &index
);
1292 if (res
== DEMUXER_CTRL_NOTIMPL
)
1293 index
= demuxer
->video
->id
;
1294 if (demuxer
->video
->id
>= 0)
1295 demuxer
->video
->sh
= demuxer
->v_streams
[demuxer
->video
->id
];
1297 demuxer
->video
->sh
= NULL
;
1301 int demuxer_add_attachment(demuxer_t
*demuxer
, const char *name
,
1302 const char *type
, const void *data
, size_t size
)
1304 if (!(demuxer
->num_attachments
& 31))
1305 demuxer
->attachments
= realloc(demuxer
->attachments
,
1306 (demuxer
->num_attachments
+ 32) * sizeof(demux_attachment_t
));
1308 demuxer
->attachments
[demuxer
->num_attachments
].name
= strdup(name
);
1309 demuxer
->attachments
[demuxer
->num_attachments
].type
= strdup(type
);
1310 demuxer
->attachments
[demuxer
->num_attachments
].data
= malloc(size
);
1311 memcpy(demuxer
->attachments
[demuxer
->num_attachments
].data
, data
, size
);
1312 demuxer
->attachments
[demuxer
->num_attachments
].data_size
= size
;
1314 return demuxer
->num_attachments
++;
1317 int demuxer_add_chapter(demuxer_t
*demuxer
, const char *name
, uint64_t start
,
1320 if (demuxer
->chapters
== NULL
)
1321 demuxer
->chapters
= malloc(32 * sizeof(*demuxer
->chapters
));
1322 else if (!(demuxer
->num_chapters
% 32))
1323 demuxer
->chapters
= realloc(demuxer
->chapters
,
1324 (demuxer
->num_chapters
+ 32) *
1325 sizeof(*demuxer
->chapters
));
1327 demuxer
->chapters
[demuxer
->num_chapters
].start
= start
;
1328 demuxer
->chapters
[demuxer
->num_chapters
].end
= end
;
1329 demuxer
->chapters
[demuxer
->num_chapters
].name
= strdup(name
? name
: MSGTR_Unknown
);
1331 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_ID=%d\n", demuxer
->num_chapters
);
1332 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_START=%"PRIu64
"\n", demuxer
->num_chapters
, start
);
1334 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_END=%"PRIu64
"\n", demuxer
->num_chapters
, end
);
1336 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CHAPTER_%d_NAME=%s\n", demuxer
->num_chapters
, name
);
1338 return demuxer
->num_chapters
++;
1342 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1343 * either using the demuxer->chapters structure set by the demuxer
1344 * or asking help to the stream layer (e.g. dvd)
1345 * \param chapter - chapter number wished - 0-based
1346 * \param mode 0: relative to current main pts, 1: absolute
1347 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1348 * \param num_chapters number of chapters present (set by this function is param is not null)
1349 * \param chapter_name name of chapter found (set by this function is param is not null)
1350 * \return -1 on error, current chapter if successful
1353 int demuxer_seek_chapter(demuxer_t
*demuxer
, int chapter
, int mode
,
1354 float *seek_pts
, int *num_chapters
,
1355 char **chapter_name
)
1359 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1360 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1362 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1364 ris
= stream_control(demuxer
->stream
,
1365 STREAM_CTRL_GET_CURRENT_CHAPTER
, ¤t
);
1366 if (ris
== STREAM_UNSUPPORTED
)
1371 demux_flush(demuxer
);
1373 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_CHAPTER
,
1375 if (ris
!= STREAM_UNSUPPORTED
)
1376 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1378 ds_fill_buffer(demuxer
->video
);
1379 resync_video_stream(sh_video
);
1383 ds_fill_buffer(demuxer
->audio
);
1384 resync_audio_stream(sh_audio
);
1386 // exit status may be ok, but main() doesn't have to seek itself
1387 // (because e.g. dvds depend on sectors, not on pts)
1391 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1392 num_chapters
) == STREAM_UNSUPPORTED
)
1397 *chapter_name
= NULL
;
1398 if (num_chapters
&& *num_chapters
) {
1399 char *tmp
= malloc(16);
1401 sprintf(tmp
, " of %3d", *num_chapters
);
1402 *chapter_name
= tmp
;
1407 return ris
!= STREAM_UNSUPPORTED
? chapter
: -1;
1408 } else { // chapters structure is set in the demuxer
1409 total
= demuxer
->num_chapters
;
1411 if (mode
== 1) //absolute seeking
1413 else { //relative seeking
1415 now
= (sh_video
? sh_video
->pts
: (sh_audio
? sh_audio
->pts
: 0.))
1418 for (current
= total
- 1; current
>= 0; --current
) {
1419 demux_chapter_t
*chapter
= demuxer
->chapters
+ current
;
1420 if (chapter
->start
<= now
)
1426 if (current
>= total
)
1431 *seek_pts
= demuxer
->chapters
[current
].start
/ 1000.0;
1434 *num_chapters
= demuxer
->num_chapters
;
1437 if (demuxer
->chapters
[current
].name
)
1438 *chapter_name
= strdup(demuxer
->chapters
[current
].name
);
1440 *chapter_name
= NULL
;
1447 int demuxer_get_current_chapter(demuxer_t
*demuxer
)
1450 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1451 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_CURRENT_CHAPTER
,
1452 &chapter
) == STREAM_UNSUPPORTED
)
1455 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1456 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1458 now
= (sh_video
? sh_video
->pts
: (sh_audio
? sh_audio
->pts
: 0))
1460 for (chapter
= demuxer
->num_chapters
- 1; chapter
>= 0; --chapter
) {
1461 if (demuxer
->chapters
[chapter
].start
<= now
)
1468 char *demuxer_chapter_name(demuxer_t
*demuxer
, int chapter
)
1470 if (demuxer
->num_chapters
&& demuxer
->chapters
) {
1471 if (chapter
>= 0 && chapter
< demuxer
->num_chapters
1472 && demuxer
->chapters
[chapter
].name
)
1473 return strdup(demuxer
->chapters
[chapter
].name
);
1478 char *demuxer_chapter_display_name(demuxer_t
*demuxer
, int chapter
)
1480 char *chapter_name
= demuxer_chapter_name(demuxer
, chapter
);
1482 char *tmp
= malloc(strlen(chapter_name
) + 14);
1483 snprintf(tmp
, 63, "(%d) %s", chapter
+ 1, chapter_name
);
1487 int chapter_num
= demuxer_chapter_count(demuxer
);
1489 if (chapter_num
<= 0)
1490 sprintf(tmp
, "(%d)", chapter
+ 1);
1492 sprintf(tmp
, "(%d) of %d", chapter
+ 1, chapter_num
);
1497 float demuxer_chapter_time(demuxer_t
*demuxer
, int chapter
, float *end
)
1499 if (demuxer
->num_chapters
&& demuxer
->chapters
&& chapter
>= 0
1500 && chapter
< demuxer
->num_chapters
) {
1502 *end
= demuxer
->chapters
[chapter
].end
/ 1000.0;
1503 return demuxer
->chapters
[chapter
].start
/ 1000.0;
1508 int demuxer_chapter_count(demuxer_t
*demuxer
)
1510 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1511 int num_chapters
= 0;
1512 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1513 &num_chapters
) == STREAM_UNSUPPORTED
)
1515 return num_chapters
;
1517 return demuxer
->num_chapters
;
1520 int demuxer_angles_count(demuxer_t
*demuxer
)
1522 int ris
, angles
= -1;
1524 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_ANGLES
, &angles
);
1525 if (ris
== STREAM_UNSUPPORTED
)
1530 int demuxer_get_current_angle(demuxer_t
*demuxer
)
1532 int ris
, curr_angle
= -1;
1533 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_ANGLE
, &curr_angle
);
1534 if (ris
== STREAM_UNSUPPORTED
)
1540 int demuxer_set_angle(demuxer_t
*demuxer
, int angle
)
1542 int ris
, angles
= -1;
1543 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1544 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1546 angles
= demuxer_angles_count(demuxer
);
1547 if ((angles
< 1) || (angle
> angles
))
1550 demux_flush(demuxer
);
1552 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SET_ANGLE
, &angle
);
1553 if (ris
== STREAM_UNSUPPORTED
)
1556 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1558 ds_fill_buffer(demuxer
->video
);
1559 resync_video_stream(sh_video
);
1563 ds_fill_buffer(demuxer
->audio
);
1564 resync_audio_stream(sh_audio
);
1570 int demuxer_audio_track_by_lang(demuxer_t
*d
, char *lang
)
1573 lang
+= strspn(lang
, ",");
1574 while ((len
= strcspn(lang
, ",")) > 0) {
1575 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1576 sh_audio_t
*sh
= d
->a_streams
[i
];
1577 if (sh
&& sh
->lang
&& strncmp(sh
->lang
, lang
, len
) == 0)
1581 lang
+= strspn(lang
, ",");
1586 int demuxer_sub_track_by_lang(demuxer_t
*d
, char *lang
)
1589 lang
+= strspn(lang
, ",");
1590 while ((len
= strcspn(lang
, ",")) > 0) {
1591 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1592 sh_sub_t
*sh
= d
->s_streams
[i
];
1593 if (sh
&& sh
->lang
&& strncmp(sh
->lang
, lang
, len
) == 0)
1597 lang
+= strspn(lang
, ",");
1602 int demuxer_default_audio_track(demuxer_t
*d
)
1605 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1606 sh_audio_t
*sh
= d
->a_streams
[i
];
1607 if (sh
&& sh
->default_track
)
1610 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1611 sh_audio_t
*sh
= d
->a_streams
[i
];
1618 int demuxer_default_sub_track(demuxer_t
*d
)
1621 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1622 sh_sub_t
*sh
= d
->s_streams
[i
];
1623 if (sh
&& sh
->default_track
)