1 //=================== DEMUXER v2.5 =========================
16 #include "libvo/fastmemcpy.h"
18 #include "stream/stream.h"
23 #include "libaf/af_format.h"
26 #include "libass/ass.h"
27 #include "libass/ass_mp.h"
30 #ifdef CONFIG_LIBAVCODEC
31 #include "libavcodec/avcodec.h"
32 #if MP_INPUT_BUFFER_PADDING_SIZE < FF_INPUT_BUFFER_PADDING_SIZE
33 #error MP_INPUT_BUFFER_PADDING_SIZE is too small!
37 void resync_video_stream(sh_video_t
*sh_video
);
38 void resync_audio_stream(sh_audio_t
*sh_audio
);
41 extern const demuxer_desc_t demuxer_desc_rawaudio
;
42 extern const demuxer_desc_t demuxer_desc_rawvideo
;
43 extern const demuxer_desc_t demuxer_desc_tv
;
44 extern const demuxer_desc_t demuxer_desc_mf
;
45 extern const demuxer_desc_t demuxer_desc_avi
;
46 extern const demuxer_desc_t demuxer_desc_y4m
;
47 extern const demuxer_desc_t demuxer_desc_asf
;
48 extern const demuxer_desc_t demuxer_desc_nuv
;
49 extern const demuxer_desc_t demuxer_desc_real
;
50 extern const demuxer_desc_t demuxer_desc_smjpeg
;
51 extern const demuxer_desc_t demuxer_desc_matroska
;
52 extern const demuxer_desc_t demuxer_desc_realaudio
;
53 extern const demuxer_desc_t demuxer_desc_vqf
;
54 extern const demuxer_desc_t demuxer_desc_mov
;
55 extern const demuxer_desc_t demuxer_desc_vivo
;
56 extern const demuxer_desc_t demuxer_desc_fli
;
57 extern const demuxer_desc_t demuxer_desc_film
;
58 extern const demuxer_desc_t demuxer_desc_roq
;
59 extern const demuxer_desc_t demuxer_desc_gif
;
60 extern const demuxer_desc_t demuxer_desc_ogg
;
61 extern const demuxer_desc_t demuxer_desc_avs
;
62 extern const demuxer_desc_t demuxer_desc_pva
;
63 extern const demuxer_desc_t demuxer_desc_nsv
;
64 extern const demuxer_desc_t demuxer_desc_mpeg_ts
;
65 extern const demuxer_desc_t demuxer_desc_lmlm4
;
66 extern const demuxer_desc_t demuxer_desc_mpeg_ps
;
67 extern const demuxer_desc_t demuxer_desc_mpeg_pes
;
68 extern const demuxer_desc_t demuxer_desc_mpeg_es
;
69 extern const demuxer_desc_t demuxer_desc_mpeg_gxf
;
70 extern const demuxer_desc_t demuxer_desc_mpeg4_es
;
71 extern const demuxer_desc_t demuxer_desc_h264_es
;
72 extern const demuxer_desc_t demuxer_desc_rawdv
;
73 extern const demuxer_desc_t demuxer_desc_mpc
;
74 extern const demuxer_desc_t demuxer_desc_audio
;
75 extern const demuxer_desc_t demuxer_desc_xmms
;
76 extern const demuxer_desc_t demuxer_desc_mpeg_ty
;
77 extern const demuxer_desc_t demuxer_desc_rtp
;
78 extern const demuxer_desc_t demuxer_desc_rtp_nemesi
;
79 extern const demuxer_desc_t demuxer_desc_lavf
;
80 extern const demuxer_desc_t demuxer_desc_lavf_preferred
;
81 extern const demuxer_desc_t demuxer_desc_aac
;
82 extern const demuxer_desc_t demuxer_desc_nut
;
83 extern const demuxer_desc_t demuxer_desc_mng
;
85 /* Please do not add any new demuxers here. If you want to implement a new
86 * demuxer, add it to libavformat, except for wrappers around external
87 * libraries and demuxers requiring binary support. */
89 const demuxer_desc_t
*const demuxer_list
[] = {
90 &demuxer_desc_rawaudio
,
91 &demuxer_desc_rawvideo
,
96 #ifdef CONFIG_LIBAVFORMAT
97 &demuxer_desc_lavf_preferred
,
105 &demuxer_desc_smjpeg
,
106 &demuxer_desc_matroska
,
107 &demuxer_desc_realaudio
,
117 #ifdef CONFIG_OGGVORBIS
120 #ifdef CONFIG_WIN32DLL
124 &demuxer_desc_mpeg_ts
,
126 &demuxer_desc_mpeg_ps
,
127 &demuxer_desc_mpeg_pes
,
128 &demuxer_desc_mpeg_es
,
129 &demuxer_desc_mpeg_gxf
,
130 &demuxer_desc_mpeg4_es
,
131 &demuxer_desc_h264_es
,
133 &demuxer_desc_mpeg_ty
,
134 #ifdef CONFIG_LIVE555
137 #ifdef CONFIG_LIBNEMESI
138 &demuxer_desc_rtp_nemesi
,
140 #ifdef CONFIG_LIBAVFORMAT
143 #ifdef CONFIG_MUSEPACK
146 #ifdef CONFIG_LIBDV095
159 /* Please do not add any new demuxers here. If you want to implement a new
160 * demuxer, add it to libavformat, except for wrappers around external
161 * libraries and demuxers requiring binary support. */
165 void free_demuxer_stream(demux_stream_t
*ds
)
171 demux_stream_t
*new_demuxer_stream(struct demuxer_st
*demuxer
, int id
)
173 demux_stream_t
*ds
= malloc(sizeof(demux_stream_t
));
174 ds
->buffer_pos
= ds
->buffer_size
= 0;
185 ds
->first
= ds
->last
= ds
->current
= NULL
;
187 ds
->demuxer
= demuxer
;
190 ds
->asf_packet
= NULL
;
192 ds
->ss_mul
= ds
->ss_div
= 0;
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(stream_t
*stream
, int type
, int a_id
, int v_id
,
218 int s_id
, char *filename
)
220 demuxer_t
*d
= malloc(sizeof(demuxer_t
));
221 memset(d
, 0, sizeof(demuxer_t
));
223 d
->stream_pts
= MP_NOPTS_VALUE
;
224 d
->reference_clock
= MP_NOPTS_VALUE
;
225 d
->movi_start
= stream
->start_pos
;
226 d
->movi_end
= stream
->end_pos
;
230 d
->audio
= new_demuxer_stream(d
, a_id
);
231 d
->video
= new_demuxer_stream(d
, v_id
);
232 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 extern int dvdsub_id
;
248 sh_sub_t
*new_sh_sub_sid(demuxer_t
*demuxer
, int id
, int sid
)
250 if (id
> MAX_S_STREAMS
- 1 || id
< 0) {
251 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
252 "Requested sub stream id overflow (%d > %d)\n", id
,
256 if (demuxer
->s_streams
[id
])
257 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, "Sub stream %i redefined\n", id
);
259 sh_sub_t
*sh
= calloc(1, sizeof(sh_sub_t
));
260 demuxer
->s_streams
[id
] = sh
;
262 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SUBTITLE_ID=%d\n", sid
);
264 return demuxer
->s_streams
[id
];
267 void free_sh_sub(sh_sub_t
*sh
)
269 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_sub at %p\n", sh
);
273 ass_free_track(sh
->ass_track
);
279 sh_audio_t
*new_sh_audio_aid(demuxer_t
*demuxer
, int id
, int aid
)
281 if (id
> MAX_A_STREAMS
- 1 || id
< 0) {
282 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
283 "Requested audio stream id overflow (%d > %d)\n", id
,
287 if (demuxer
->a_streams
[id
])
288 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, MSGTR_AudioStreamRedefined
, id
);
290 sh_audio_t
*sh
= calloc(1, sizeof(sh_audio_t
));
291 mp_msg(MSGT_DEMUXER
, MSGL_V
, MSGTR_FoundAudioStream
, id
);
292 demuxer
->a_streams
[id
] = sh
;
294 sh
->ds
= demuxer
->audio
;
297 sh
->sample_format
= AF_FORMAT_S16_NE
;
298 sh
->audio_out_minsize
= 8192; /* default size, maybe not enough for Win32/ACM */
299 sh
->pts
= MP_NOPTS_VALUE
;
300 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_ID=%d\n", aid
);
302 return demuxer
->a_streams
[id
];
305 void free_sh_audio(demuxer_t
*demuxer
, int id
)
307 sh_audio_t
*sh
= demuxer
->a_streams
[id
];
308 demuxer
->a_streams
[id
] = NULL
;
309 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_audio at %p\n", sh
);
316 sh_video_t
*new_sh_video_vid(demuxer_t
*demuxer
, int id
, int vid
)
318 if (id
> MAX_V_STREAMS
- 1 || id
< 0) {
319 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
320 "Requested video stream id overflow (%d > %d)\n", id
,
324 if (demuxer
->v_streams
[id
])
325 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, MSGTR_VideoStreamRedefined
, id
);
327 sh_video_t
*sh
= calloc(1, sizeof(sh_video_t
));
328 mp_msg(MSGT_DEMUXER
, MSGL_V
, MSGTR_FoundVideoStream
, id
);
329 demuxer
->v_streams
[id
] = sh
;
331 sh
->ds
= demuxer
->video
;
332 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_ID=%d\n", vid
);
334 return demuxer
->v_streams
[id
];
337 void free_sh_video(sh_video_t
*sh
)
339 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_video at %p\n", sh
);
344 void free_demuxer(demuxer_t
*demuxer
)
347 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing demuxer at %p\n",
349 if (demuxer
->desc
->close
)
350 demuxer
->desc
->close(demuxer
);
351 // Very ugly hack to make it behave like old implementation
352 if (demuxer
->desc
->type
== DEMUXER_TYPE_DEMUXERS
)
353 goto skip_streamfree
;
355 for (i
= 0; i
< MAX_A_STREAMS
; i
++)
356 if (demuxer
->a_streams
[i
])
357 free_sh_audio(demuxer
, i
);
358 for (i
= 0; i
< MAX_V_STREAMS
; i
++)
359 if (demuxer
->v_streams
[i
])
360 free_sh_video(demuxer
->v_streams
[i
]);
361 for (i
= 0; i
< MAX_S_STREAMS
; i
++)
362 if (demuxer
->s_streams
[i
])
363 free_sh_sub(demuxer
->s_streams
[i
]);
365 free_demuxer_stream(demuxer
->audio
);
366 free_demuxer_stream(demuxer
->video
);
367 free_demuxer_stream(demuxer
->sub
);
370 for (i
= 0; demuxer
->info
[i
] != NULL
; i
++)
371 free(demuxer
->info
[i
]);
374 free(demuxer
->filename
);
375 if (demuxer
->chapters
) {
376 for (i
= 0; i
< demuxer
->num_chapters
; i
++)
377 free(demuxer
->chapters
[i
].name
);
378 free(demuxer
->chapters
);
380 if (demuxer
->attachments
) {
381 for (i
= 0; i
< demuxer
->num_attachments
; i
++) {
382 free(demuxer
->attachments
[i
].name
);
383 free(demuxer
->attachments
[i
].type
);
384 free(demuxer
->attachments
[i
].data
);
386 free(demuxer
->attachments
);
392 void ds_add_packet(demux_stream_t
*ds
, demux_packet_t
*dp
)
394 // append packet to DS stream:
396 ds
->bytes
+= dp
->len
;
398 // next packet in stream
402 // first packet in stream
403 ds
->first
= ds
->last
= dp
;
405 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
406 "DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
407 (ds
== ds
->demuxer
->audio
) ? "d_audio" : "d_video", dp
->len
,
408 dp
->pts
, (unsigned int) dp
->pos
, ds
->demuxer
->audio
->packs
,
409 ds
->demuxer
->video
->packs
);
412 void ds_read_packet(demux_stream_t
*ds
, stream_t
*stream
, int len
,
413 double pts
, off_t pos
, int flags
)
415 demux_packet_t
*dp
= new_demux_packet(len
);
416 len
= stream_read(stream
, dp
->buffer
, len
);
417 resize_demux_packet(dp
, len
);
421 // append packet to DS stream:
422 ds_add_packet(ds
, dp
);
426 // 0 = EOF or no stream found or invalid type
427 // 1 = successfully read a packet
429 int demux_fill_buffer(demuxer_t
*demux
, demux_stream_t
*ds
)
431 // Note: parameter 'ds' can be NULL!
432 return demux
->desc
->fill_buffer(demux
, ds
);
438 #define MAX_ACUMULATED_PACKETS 64
439 int ds_fill_buffer(demux_stream_t
*ds
)
441 demuxer_t
*demux
= ds
->demuxer
;
443 free_demux_packet(ds
->current
);
445 if (mp_msg_test(MSGT_DEMUXER
, MSGL_DBG3
)) {
446 if (ds
== demux
->audio
)
447 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
448 "ds_fill_buffer(d_audio) called\n");
449 else if (ds
== demux
->video
)
450 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
451 "ds_fill_buffer(d_video) called\n");
452 else if (ds
== demux
->sub
)
453 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
, "ds_fill_buffer(d_sub) called\n");
455 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
456 "ds_fill_buffer(unknown 0x%X) called\n", (unsigned int) ds
);
460 demux_packet_t
*p
= ds
->first
;
462 if (demux
->reference_clock
!= MP_NOPTS_VALUE
) {
463 if ( p
->pts
!= MP_NOPTS_VALUE
464 && p
->pts
> demux
->reference_clock
465 && ds
->packs
< MAX_ACUMULATED_PACKETS
) {
466 if (demux_fill_buffer(demux
, ds
))
472 ds
->buffer
= p
->buffer
;
474 ds
->buffer_size
= p
->len
;
476 ds
->dpos
+= p
->len
; // !!!
478 if (p
->pts
!= MP_NOPTS_VALUE
) {
482 ds
->pts_bytes
+= p
->len
; // !!!
483 if (p
->stream_pts
!= MP_NOPTS_VALUE
)
484 demux
->stream_pts
= p
->stream_pts
;
485 ds
->flags
= p
->flags
;
495 if (demux
->audio
->packs
>= MAX_PACKS
496 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
497 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_TooManyAudioInBuffer
,
498 demux
->audio
->packs
, demux
->audio
->bytes
);
499 mp_msg(MSGT_DEMUXER
, MSGL_HINT
, MSGTR_MaybeNI
);
502 if (demux
->video
->packs
>= MAX_PACKS
503 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
504 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_TooManyVideoInBuffer
,
505 demux
->video
->packs
, demux
->video
->bytes
);
506 mp_msg(MSGT_DEMUXER
, MSGL_HINT
, MSGTR_MaybeNI
);
509 if (!demux_fill_buffer(demux
, ds
)) {
510 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
511 "ds_fill_buffer()->demux_fill_buffer() failed\n");
515 ds
->buffer_pos
= ds
->buffer_size
= 0;
517 mp_msg(MSGT_DEMUXER
, MSGL_V
,
518 "ds_fill_buffer: EOF reached (stream: %s) \n",
519 ds
== demux
->audio
? "audio" : "video");
524 int demux_read_data(demux_stream_t
*ds
, unsigned char *mem
, int len
)
529 x
= ds
->buffer_size
- ds
->buffer_pos
;
531 if (!ds_fill_buffer(ds
))
537 fast_memcpy(mem
+ bytes
, &ds
->buffer
[ds
->buffer_pos
], x
);
547 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
548 * \param mem memory to read data into, may be NULL to discard data
549 * \param maxlen maximum number of bytes to read
550 * \param read number of bytes actually read
551 * \param pattern pattern to search for (lowest 8 bits are ignored)
552 * \return whether pattern was found
554 int demux_pattern_3(demux_stream_t
*ds
, unsigned char *mem
, int maxlen
,
555 int *read
, uint32_t pattern
)
557 register uint32_t head
= 0xffffff00;
558 register uint32_t pat
= pattern
& 0xffffff00;
561 register unsigned char *ds_buf
= &ds
->buffer
[ds
->buffer_size
];
562 int len
= ds
->buffer_size
- ds
->buffer_pos
;
563 register long pos
= -len
;
564 if (unlikely(pos
>= 0)) { // buffer is empty
571 } while (++pos
&& head
!= pat
);
573 if (total_len
+ len
> maxlen
)
574 len
= maxlen
- total_len
;
575 len
= demux_read_data(ds
, mem
? &mem
[total_len
] : NULL
, len
);
577 } while ((head
!= pat
|| total_len
< 3) && total_len
< maxlen
&& !ds
->eof
);
580 return total_len
>= 3 && head
== pat
;
583 void ds_free_packs(demux_stream_t
*ds
)
585 demux_packet_t
*dp
= ds
->first
;
587 demux_packet_t
*dn
= dp
->next
;
588 free_demux_packet(dp
);
591 if (ds
->asf_packet
) {
592 // free unfinished .asf fragments:
593 free(ds
->asf_packet
->buffer
);
594 free(ds
->asf_packet
);
595 ds
->asf_packet
= NULL
;
597 ds
->first
= ds
->last
= NULL
;
598 ds
->packs
= 0; // !!!!!
601 free_demux_packet(ds
->current
);
604 ds
->buffer_pos
= ds
->buffer_size
;
609 int ds_get_packet(demux_stream_t
*ds
, unsigned char **start
)
612 if (ds
->buffer_pos
>= ds
->buffer_size
) {
613 if (!ds_fill_buffer(ds
)) {
619 len
= ds
->buffer_size
- ds
->buffer_pos
;
620 *start
= &ds
->buffer
[ds
->buffer_pos
];
621 ds
->buffer_pos
+= len
;
625 int ds_get_packet_pts(demux_stream_t
*ds
, unsigned char **start
, double *pts
)
628 *pts
= MP_NOPTS_VALUE
;
629 if (ds
->buffer_pos
>= ds
->buffer_size
) {
630 if (!ds_fill_buffer(ds
)) {
636 // Return pts unless this read starts from the middle of a packet
638 *pts
= ds
->current
->pts
;
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_sub(demux_stream_t
*ds
, unsigned char **start
)
648 if (ds
->buffer_pos
>= ds
->buffer_size
) {
652 if (!ds_fill_buffer(ds
))
655 len
= ds
->buffer_size
- ds
->buffer_pos
;
656 *start
= &ds
->buffer
[ds
->buffer_pos
];
657 ds
->buffer_pos
+= len
;
661 double ds_get_next_pts(demux_stream_t
*ds
)
663 demuxer_t
*demux
= ds
->demuxer
;
665 if (demux
->audio
->packs
>= MAX_PACKS
666 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
667 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_TooManyAudioInBuffer
,
668 demux
->audio
->packs
, demux
->audio
->bytes
);
669 mp_msg(MSGT_DEMUXER
, MSGL_HINT
, MSGTR_MaybeNI
);
670 return MP_NOPTS_VALUE
;
672 if (demux
->video
->packs
>= MAX_PACKS
673 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
674 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_TooManyVideoInBuffer
,
675 demux
->video
->packs
, demux
->video
->bytes
);
676 mp_msg(MSGT_DEMUXER
, MSGL_HINT
, MSGTR_MaybeNI
);
677 return MP_NOPTS_VALUE
;
679 if (!demux_fill_buffer(demux
, ds
))
680 return MP_NOPTS_VALUE
;
682 return ds
->first
->pts
;
685 // ====================================================================
687 void demuxer_help(void)
691 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "Available demuxers:\n");
692 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, " demuxer: type info: (comment)\n");
693 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DEMUXERS\n");
694 for (i
= 0; demuxer_list
[i
]; i
++) {
695 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Don't display special demuxers
697 if (demuxer_list
[i
]->comment
&& strlen(demuxer_list
[i
]->comment
))
698 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s (%s)\n",
699 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
700 demuxer_list
[i
]->info
, demuxer_list
[i
]->comment
);
702 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s\n",
703 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
704 demuxer_list
[i
]->info
);
710 * Get demuxer type for a given demuxer name
712 * @param demuxer_name string with demuxer name of demuxer number
713 * @param force will be set if demuxer should be forced.
715 * @return DEMUXER_TYPE_xxx, -1 if error or not found
717 int get_demuxer_type_from_name(char *demuxer_name
, int *force
)
723 if (!demuxer_name
|| !demuxer_name
[0])
724 return DEMUXER_TYPE_UNKNOWN
;
726 *force
= demuxer_name
[0] == '+';
727 if (demuxer_name
[0] == '+')
728 demuxer_name
= &demuxer_name
[1];
729 for (i
= 0; demuxer_list
[i
]; i
++) {
730 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Can't select special demuxers from commandline
732 if (strcmp(demuxer_name
, demuxer_list
[i
]->name
) == 0)
733 return demuxer_list
[i
]->type
;
736 // No match found, try to parse name as an integer (demuxer number)
737 type_int
= strtol(demuxer_name
, &endptr
, 0);
738 if (*endptr
) // Conversion failed
740 if ((type_int
> 0) && (type_int
<= DEMUXER_TYPE_MAX
))
741 return (int) type_int
;
746 int extension_parsing
= 1; // 0=off 1=mixed (used only for unstable formats)
749 int user_correct_pts
= -1;
752 NOTE : Several demuxers may be opened at the same time so
753 demuxers should NEVER rely on an external var to enable them
754 self. If a demuxer can't do any auto-detection it should only use
755 file_format. The user can explicitly set file_format with the -demuxer
756 option so there is really no need for another extra var.
757 For convenience an option can be added to set file_format directly
758 to the right type (ex: rawaudio,rawvideo).
759 Also the stream can override the file_format so a demuxer which rely
760 on a special stream type can set file_format at the stream level
764 static demuxer_t
*demux_open_stream(stream_t
*stream
, int file_format
,
765 int force
, int audio_id
, int video_id
,
766 int dvdsub_id
, char *filename
)
768 demuxer_t
*demuxer
= NULL
;
770 sh_video_t
*sh_video
= NULL
;
772 const demuxer_desc_t
*demuxer_desc
;
776 // If somebody requested a demuxer check it
778 if ((demuxer_desc
= get_demuxer_desc_from_type(file_format
))) {
779 demuxer
= new_demuxer(stream
, demuxer_desc
->type
, audio_id
,
780 video_id
, dvdsub_id
, filename
);
781 if (demuxer_desc
->check_file
)
782 fformat
= demuxer_desc
->check_file(demuxer
);
783 if (force
|| !demuxer_desc
->check_file
)
784 fformat
= demuxer_desc
->type
;
786 if (fformat
== demuxer_desc
->type
) {
787 demuxer_t
*demux2
= demuxer
;
788 // Move messages to demuxer detection code?
789 mp_msg(MSGT_DEMUXER
, MSGL_INFO
,
790 MSGTR_Detected_XXX_FileFormat
,
791 demuxer_desc
->shortdesc
);
792 file_format
= fformat
;
793 if (!demuxer
->desc
->open
794 || (demux2
= demuxer
->desc
->open(demuxer
))) {
799 // Format changed after check, recurse
800 free_demuxer(demuxer
);
801 return demux_open_stream(stream
, fformat
, force
, audio_id
,
802 video_id
, dvdsub_id
, filename
);
805 // Check failed for forced demuxer, quit
806 free_demuxer(demuxer
);
810 // Test demuxers with safe file checks
811 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
812 if (demuxer_desc
->safe_check
) {
813 demuxer
= new_demuxer(stream
, demuxer_desc
->type
, audio_id
,
814 video_id
, dvdsub_id
, filename
);
815 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
816 if (fformat
== demuxer_desc
->type
) {
817 demuxer_t
*demux2
= demuxer
;
818 mp_msg(MSGT_DEMUXER
, MSGL_INFO
,
819 MSGTR_Detected_XXX_FileFormat
,
820 demuxer_desc
->shortdesc
);
821 file_format
= fformat
;
822 if (!demuxer
->desc
->open
823 || (demux2
= demuxer
->desc
->open(demuxer
))) {
828 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
829 return demuxer
; // handled in mplayer.c
830 // Format changed after check, recurse
831 free_demuxer(demuxer
);
832 demuxer
= demux_open_stream(stream
, fformat
, force
,
834 dvdsub_id
, filename
);
836 return demuxer
; // done!
837 file_format
= DEMUXER_TYPE_UNKNOWN
;
840 free_demuxer(demuxer
);
845 // If no forced demuxer perform file extension based detection
846 // Ok. We're over the stable detectable fileformats, the next ones are
847 // a bit fuzzy. So by default (extension_parsing==1) try extension-based
849 if (file_format
== DEMUXER_TYPE_UNKNOWN
&& filename
850 && extension_parsing
== 1) {
851 file_format
= demuxer_type_by_filename(filename
);
852 if (file_format
!= DEMUXER_TYPE_UNKNOWN
) {
853 // we like recursion :)
854 demuxer
= demux_open_stream(stream
, file_format
, force
, audio_id
,
855 video_id
, dvdsub_id
, filename
);
857 return demuxer
; // done!
858 file_format
= DEMUXER_TYPE_UNKNOWN
; // continue fuzzy guessing...
859 mp_msg(MSGT_DEMUXER
, MSGL_V
,
860 "demuxer: continue fuzzy content-based format guessing...\n");
863 // Try detection for all other demuxers
864 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
865 if (!demuxer_desc
->safe_check
&& demuxer_desc
->check_file
) {
866 demuxer
= new_demuxer(stream
, demuxer_desc
->type
, audio_id
,
867 video_id
, dvdsub_id
, filename
);
868 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
869 if (fformat
== demuxer_desc
->type
) {
870 demuxer_t
*demux2
= demuxer
;
871 mp_msg(MSGT_DEMUXER
, MSGL_INFO
,
872 MSGTR_Detected_XXX_FileFormat
,
873 demuxer_desc
->shortdesc
);
874 file_format
= fformat
;
875 if (!demuxer
->desc
->open
876 || (demux2
= demuxer
->desc
->open(demuxer
))) {
881 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
882 return demuxer
; // handled in mplayer.c
883 // Format changed after check, recurse
884 free_demuxer(demuxer
);
885 demuxer
= demux_open_stream(stream
, fformat
, force
,
887 dvdsub_id
, filename
);
889 return demuxer
; // done!
890 file_format
= DEMUXER_TYPE_UNKNOWN
;
893 free_demuxer(demuxer
);
899 //====== File format recognized, set up these for compatibility: =========
902 demuxer
->file_format
= file_format
;
904 if ((sh_video
= demuxer
->video
->sh
) && sh_video
->bih
) {
905 int biComp
= le2me_32(sh_video
->bih
->biCompression
);
906 mp_msg(MSGT_DEMUX
, MSGL_INFO
,
907 "VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
908 (char *) &biComp
, sh_video
->bih
->biWidth
,
909 sh_video
->bih
->biHeight
, sh_video
->bih
->biBitCount
,
910 sh_video
->fps
, sh_video
->i_bps
* 0.008f
,
911 sh_video
->i_bps
/ 1024.0f
);
914 if (ass_enabled
&& ass_library
) {
915 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
916 sh_sub_t
*sh
= demuxer
->s_streams
[i
];
917 if (sh
&& sh
->type
== 'a') {
918 sh
->ass_track
= ass_new_track(ass_library
);
919 if (sh
->ass_track
&& sh
->extradata
)
920 ass_process_codec_private(sh
->ass_track
, sh
->extradata
,
922 } else if (sh
&& sh
->type
!= 'v')
923 sh
->ass_track
= ass_default_track(ass_library
);
930 char *audio_stream
= NULL
;
931 char *sub_stream
= NULL
;
932 int audio_stream_cache
= 0;
934 char *demuxer_name
= NULL
; // parameter from -demuxer
935 char *audio_demuxer_name
= NULL
; // parameter from -audio-demuxer
936 char *sub_demuxer_name
= NULL
; // parameter from -sub-demuxer
938 extern int hr_mp3_seek
;
940 extern float stream_cache_min_percent
;
941 extern float stream_cache_seek_min_percent
;
943 demuxer_t
*demux_open(stream_t
*vs
, int file_format
, int audio_id
,
944 int video_id
, int dvdsub_id
, char *filename
)
946 stream_t
*as
= NULL
, *ss
= NULL
;
947 demuxer_t
*vd
, *ad
= NULL
, *sd
= NULL
;
949 int afmt
= DEMUXER_TYPE_UNKNOWN
, sfmt
= DEMUXER_TYPE_UNKNOWN
;
951 int audio_demuxer_type
= 0, sub_demuxer_type
= 0;
952 int demuxer_force
= 0, audio_demuxer_force
= 0, sub_demuxer_force
= 0;
955 get_demuxer_type_from_name(demuxer_name
, &demuxer_force
)) < 0) {
956 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-demuxer %s does not exist.\n",
959 if ((audio_demuxer_type
=
960 get_demuxer_type_from_name(audio_demuxer_name
,
961 &audio_demuxer_force
)) < 0) {
962 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-audio-demuxer %s does not exist.\n",
965 if ((sub_demuxer_type
=
966 get_demuxer_type_from_name(sub_demuxer_name
,
967 &sub_demuxer_force
)) < 0) {
968 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-sub-demuxer %s does not exist.\n",
973 as
= open_stream(audio_stream
, 0, &afmt
);
975 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_CannotOpenAudioStream
,
979 if (audio_stream_cache
) {
980 if (!stream_enable_cache
981 (as
, audio_stream_cache
* 1024,
982 audio_stream_cache
* 1024 * (stream_cache_min_percent
/
984 audio_stream_cache
* 1024 * (stream_cache_seek_min_percent
/
987 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
988 "Can't enable audio stream cache\n");
994 ss
= open_stream(sub_stream
, 0, &sfmt
);
996 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_CannotOpenSubtitlesStream
,
1002 vd
= demux_open_stream(vs
, demuxer_type
? demuxer_type
: file_format
,
1003 demuxer_force
, audio_stream
? -2 : audio_id
,
1004 video_id
, sub_stream
? -2 : dvdsub_id
, filename
);
1013 ad
= demux_open_stream(as
,
1014 audio_demuxer_type
? audio_demuxer_type
: afmt
,
1015 audio_demuxer_force
, audio_id
, -2, -2,
1018 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, MSGTR_OpeningAudioDemuxerFailed
,
1021 } else if (ad
->audio
->sh
1022 && ((sh_audio_t
*) ad
->audio
->sh
)->format
== 0x55) // MP3
1023 hr_mp3_seek
= 1; // Enable high res seeking
1026 sd
= demux_open_stream(ss
, sub_demuxer_type
? sub_demuxer_type
: sfmt
,
1027 sub_demuxer_force
, -2, -2, dvdsub_id
,
1030 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
1031 MSGTR_OpeningSubtitlesDemuxerFailed
, sub_stream
);
1037 res
= new_demuxers_demuxer(vd
, ad
, sd
);
1039 res
= new_demuxers_demuxer(vd
, ad
, vd
);
1041 res
= new_demuxers_demuxer(vd
, vd
, sd
);
1045 correct_pts
= user_correct_pts
;
1046 if (correct_pts
< 0)
1047 correct_pts
= demux_control(res
, DEMUXER_CTRL_CORRECT_PTS
, NULL
)
1053 void demux_flush(demuxer_t
*demuxer
)
1055 ds_free_packs(demuxer
->video
);
1056 ds_free_packs(demuxer
->audio
);
1057 ds_free_packs(demuxer
->sub
);
1060 int demux_seek(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
,
1063 demux_stream_t
*d_audio
= demuxer
->audio
;
1064 demux_stream_t
*d_video
= demuxer
->video
;
1065 sh_audio_t
*sh_audio
= d_audio
->sh
;
1066 sh_video_t
*sh_video
= d_video
->sh
;
1070 if (!demuxer
->seekable
) {
1071 if (demuxer
->file_format
== DEMUXER_TYPE_AVI
)
1072 mp_msg(MSGT_SEEK
, MSGL_WARN
, MSGTR_CantSeekRawAVI
);
1074 else if (demuxer
->file_format
== DEMUXER_TYPE_TV
)
1075 mp_msg(MSGT_SEEK
, MSGL_WARN
, MSGTR_TVInputNotSeekable
);
1078 mp_msg(MSGT_SEEK
, MSGL_WARN
, MSGTR_CantSeekFile
);
1082 demux_flush(demuxer
);
1083 // clear demux buffers:
1085 sh_audio
->a_buffer_len
= 0;
1087 demuxer
->stream
->eof
= 0;
1088 demuxer
->video
->eof
= 0;
1089 demuxer
->audio
->eof
= 0;
1092 sh_video
->timer
= 0; // !!!!!!
1094 if (flags
& SEEK_ABSOLUTE
)
1097 if (demuxer
->stream_pts
== MP_NOPTS_VALUE
)
1099 pts
= demuxer
->stream_pts
;
1102 if (flags
& SEEK_FACTOR
) {
1103 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_TIME_LENGTH
, &tmp
)
1104 == STREAM_UNSUPPORTED
)
1106 pts
+= tmp
* rel_seek_secs
;
1108 pts
+= rel_seek_secs
;
1110 if (stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_TIME
, &pts
) !=
1111 STREAM_UNSUPPORTED
) {
1112 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1117 if (demuxer
->desc
->seek
)
1118 demuxer
->desc
->seek(demuxer
, rel_seek_secs
, audio_delay
, flags
);
1121 resync_audio_stream(sh_audio
);
1126 int demux_info_add(demuxer_t
*demuxer
, const char *opt
, const char *param
)
1128 char **info
= demuxer
->info
;
1132 for (n
= 0; info
&& info
[2 * n
] != NULL
; n
++) {
1133 if (!strcasecmp(opt
, info
[2 * n
])) {
1134 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_DemuxerInfoChanged
, opt
,
1136 free(info
[2 * n
+ 1]);
1137 info
[2 * n
+ 1] = strdup(param
);
1142 info
= demuxer
->info
= (char **) realloc(info
,
1143 (2 * (n
+ 2)) * sizeof(char *));
1144 info
[2 * n
] = strdup(opt
);
1145 info
[2 * n
+ 1] = strdup(param
);
1146 memset(&info
[2 * (n
+ 1)], 0, 2 * sizeof(char *));
1151 int demux_info_print(demuxer_t
*demuxer
)
1153 char **info
= demuxer
->info
;
1159 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_ClipInfo
);
1160 for (n
= 0; info
[2 * n
] != NULL
; n
++) {
1161 mp_msg(MSGT_DEMUX
, MSGL_INFO
, " %s: %s\n", info
[2 * n
],
1163 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_NAME%d=%s\n", n
,
1165 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_VALUE%d=%s\n", n
,
1168 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_N=%d\n", n
);
1173 char *demux_info_get(demuxer_t
*demuxer
, const char *opt
)
1176 char **info
= demuxer
->info
;
1178 for (i
= 0; info
&& info
[2 * i
] != NULL
; i
++) {
1179 if (!strcasecmp(opt
, info
[2 * i
]))
1180 return info
[2 * i
+ 1];
1186 int demux_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
1189 if (demuxer
->desc
->control
)
1190 return demuxer
->desc
->control(demuxer
, cmd
, arg
);
1192 return DEMUXER_CTRL_NOTIMPL
;
1197 double demuxer_get_time_length(demuxer_t
*demuxer
)
1199 double get_time_ans
;
1200 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1201 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1202 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
1204 (demuxer
, DEMUXER_CTRL_GET_TIME_LENGTH
, (void *) &get_time_ans
) <= 0) {
1205 if (sh_video
&& sh_video
->i_bps
&& sh_audio
&& sh_audio
->i_bps
)
1206 get_time_ans
= (double) (demuxer
->movi_end
-
1207 demuxer
->movi_start
) / (sh_video
->i_bps
+
1209 else if (sh_video
&& sh_video
->i_bps
)
1210 get_time_ans
= (double) (demuxer
->movi_end
-
1211 demuxer
->movi_start
) / sh_video
->i_bps
;
1212 else if (sh_audio
&& sh_audio
->i_bps
)
1213 get_time_ans
= (double) (demuxer
->movi_end
-
1214 demuxer
->movi_start
) / sh_audio
->i_bps
;
1218 return get_time_ans
;
1222 * \brief demuxer_get_current_time() returns the time of the current play in three possible ways:
1223 * either when the stream reader satisfies STREAM_CTRL_GET_CURRENT_TIME (e.g. dvd)
1224 * or using sh_video->pts when the former method fails
1226 * \return the current play time
1228 int demuxer_get_current_time(demuxer_t
*demuxer
)
1230 double get_time_ans
= 0;
1231 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1232 if (demuxer
->stream_pts
!= MP_NOPTS_VALUE
)
1233 get_time_ans
= demuxer
->stream_pts
;
1235 get_time_ans
= sh_video
->pts
;
1236 return (int) get_time_ans
;
1239 int demuxer_get_percent_pos(demuxer_t
*demuxer
)
1242 int res
= demux_control(demuxer
, DEMUXER_CTRL_GET_PERCENT_POS
, &ans
);
1243 int len
= (demuxer
->movi_end
- demuxer
->movi_start
) / 100;
1246 ans
= (demuxer
->filepos
- demuxer
->movi_start
) / len
;
1257 int demuxer_switch_audio(demuxer_t
*demuxer
, int index
)
1259 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_AUDIO
, &index
);
1260 if (res
== DEMUXER_CTRL_NOTIMPL
)
1261 index
= demuxer
->audio
->id
;
1262 if (demuxer
->audio
->id
>= 0)
1263 demuxer
->audio
->sh
= demuxer
->a_streams
[demuxer
->audio
->id
];
1265 demuxer
->audio
->sh
= NULL
;
1269 int demuxer_switch_video(demuxer_t
*demuxer
, int index
)
1271 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_VIDEO
, &index
);
1272 if (res
== DEMUXER_CTRL_NOTIMPL
)
1273 index
= demuxer
->video
->id
;
1274 if (demuxer
->video
->id
>= 0)
1275 demuxer
->video
->sh
= demuxer
->v_streams
[demuxer
->video
->id
];
1277 demuxer
->video
->sh
= NULL
;
1281 int demuxer_add_attachment(demuxer_t
*demuxer
, const char *name
,
1282 const char *type
, const void *data
, size_t size
)
1284 if (!(demuxer
->num_attachments
& 31))
1285 demuxer
->attachments
= realloc(demuxer
->attachments
,
1286 (demuxer
->num_attachments
+ 32) * sizeof(demux_attachment_t
));
1288 demuxer
->attachments
[demuxer
->num_attachments
].name
= strdup(name
);
1289 demuxer
->attachments
[demuxer
->num_attachments
].type
= strdup(type
);
1290 demuxer
->attachments
[demuxer
->num_attachments
].data
= malloc(size
);
1291 memcpy(demuxer
->attachments
[demuxer
->num_attachments
].data
, data
, size
);
1292 demuxer
->attachments
[demuxer
->num_attachments
].data_size
= size
;
1294 return demuxer
->num_attachments
++;
1297 int demuxer_add_chapter(demuxer_t
*demuxer
, const char *name
, uint64_t start
,
1300 if (demuxer
->chapters
== NULL
)
1301 demuxer
->chapters
= malloc(32 * sizeof(*demuxer
->chapters
));
1302 else if (!(demuxer
->num_chapters
% 32))
1303 demuxer
->chapters
= realloc(demuxer
->chapters
,
1304 (demuxer
->num_chapters
+ 32) *
1305 sizeof(*demuxer
->chapters
));
1307 demuxer
->chapters
[demuxer
->num_chapters
].start
= start
;
1308 demuxer
->chapters
[demuxer
->num_chapters
].end
= end
;
1309 demuxer
->chapters
[demuxer
->num_chapters
].name
= strdup(name
? name
: MSGTR_Unknown
);
1311 return demuxer
->num_chapters
++;
1315 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1316 * either using the demuxer->chapters structure set by the demuxer
1317 * or asking help to the stream layer (e.g. dvd)
1318 * \param chapter - chapter number wished - 0-based
1319 * \param mode 0: relative to current main pts, 1: absolute
1320 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1321 * \param num_chapters number of chapters present (set by this function is param is not null)
1322 * \param chapter_name name of chapter found (set by this function is param is not null)
1323 * \return -1 on error, current chapter if successful
1326 int demuxer_seek_chapter(demuxer_t
*demuxer
, int chapter
, int mode
,
1327 float *seek_pts
, int *num_chapters
,
1328 char **chapter_name
)
1332 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1333 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1335 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1337 ris
= stream_control(demuxer
->stream
,
1338 STREAM_CTRL_GET_CURRENT_CHAPTER
, ¤t
);
1339 if (ris
== STREAM_UNSUPPORTED
)
1344 demux_flush(demuxer
);
1346 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_CHAPTER
,
1348 if (ris
!= STREAM_UNSUPPORTED
)
1349 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1351 ds_fill_buffer(demuxer
->video
);
1352 resync_video_stream(sh_video
);
1356 ds_fill_buffer(demuxer
->audio
);
1357 resync_audio_stream(sh_audio
);
1359 // exit status may be ok, but main() doesn't have to seek itself
1360 // (because e.g. dvds depend on sectors, not on pts)
1364 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1365 num_chapters
) == STREAM_UNSUPPORTED
)
1370 *chapter_name
= NULL
;
1371 if (num_chapters
&& *num_chapters
) {
1372 char *tmp
= malloc(16);
1374 sprintf(tmp
, " of %3d", *num_chapters
);
1375 *chapter_name
= tmp
;
1380 return ris
!= STREAM_UNSUPPORTED
? chapter
: -1;
1381 } else { // chapters structure is set in the demuxer
1382 total
= demuxer
->num_chapters
;
1384 if (mode
== 1) //absolute seeking
1386 else { //relative seeking
1388 now
= (sh_video
? sh_video
->pts
: (sh_audio
? sh_audio
->pts
: 0.))
1391 for (current
= total
- 1; current
>= 0; --current
) {
1392 demux_chapter_t
*chapter
= demuxer
->chapters
+ current
;
1393 if (chapter
->start
<= now
)
1399 if (current
>= total
)
1404 *seek_pts
= demuxer
->chapters
[current
].start
/ 1000.0;
1407 *num_chapters
= demuxer
->num_chapters
;
1410 if (demuxer
->chapters
[current
].name
)
1411 *chapter_name
= strdup(demuxer
->chapters
[current
].name
);
1413 *chapter_name
= NULL
;
1420 int demuxer_get_current_chapter(demuxer_t
*demuxer
)
1423 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1424 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_CURRENT_CHAPTER
,
1425 &chapter
) == STREAM_UNSUPPORTED
)
1428 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1429 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1431 now
= (sh_video
? sh_video
->pts
: (sh_audio
? sh_audio
->pts
: 0))
1433 for (chapter
= demuxer
->num_chapters
- 1; chapter
>= 0; --chapter
) {
1434 if (demuxer
->chapters
[chapter
].start
<= now
)
1441 char *demuxer_chapter_name(demuxer_t
*demuxer
, int chapter
)
1443 if (demuxer
->num_chapters
&& demuxer
->chapters
) {
1444 if (chapter
>= 0 && chapter
< demuxer
->num_chapters
1445 && demuxer
->chapters
[chapter
].name
)
1446 return strdup(demuxer
->chapters
[chapter
].name
);
1451 char *demuxer_chapter_display_name(demuxer_t
*demuxer
, int chapter
)
1453 char *chapter_name
= demuxer_chapter_name(demuxer
, chapter
);
1455 char *tmp
= malloc(strlen(chapter_name
) + 14);
1456 snprintf(tmp
, 63, "(%d) %s", chapter
+ 1, chapter_name
);
1460 int chapter_num
= demuxer_chapter_count(demuxer
);
1462 if (chapter_num
<= 0)
1463 sprintf(tmp
, "(%d)", chapter
+ 1);
1465 sprintf(tmp
, "(%d) of %d", chapter
+ 1, chapter_num
);
1470 float demuxer_chapter_time(demuxer_t
*demuxer
, int chapter
, float *end
)
1472 if (demuxer
->num_chapters
&& demuxer
->chapters
&& chapter
>= 0
1473 && chapter
< demuxer
->num_chapters
) {
1475 *end
= demuxer
->chapters
[chapter
].end
/ 1000.0;
1476 return demuxer
->chapters
[chapter
].start
/ 1000.0;
1481 int demuxer_chapter_count(demuxer_t
*demuxer
)
1483 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1484 int num_chapters
= 0;
1485 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1486 &num_chapters
) == STREAM_UNSUPPORTED
)
1488 return num_chapters
;
1490 return demuxer
->num_chapters
;
1493 int demuxer_angles_count(demuxer_t
*demuxer
)
1495 int ris
, angles
= -1;
1497 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_ANGLES
, &angles
);
1498 if (ris
== STREAM_UNSUPPORTED
)
1503 int demuxer_get_current_angle(demuxer_t
*demuxer
)
1505 int ris
, curr_angle
= -1;
1506 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_ANGLE
, &curr_angle
);
1507 if (ris
== STREAM_UNSUPPORTED
)
1513 int demuxer_set_angle(demuxer_t
*demuxer
, int angle
)
1515 int ris
, angles
= -1;
1516 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1517 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1519 angles
= demuxer_angles_count(demuxer
);
1520 if ((angles
< 1) || (angle
> angles
))
1523 demux_flush(demuxer
);
1525 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SET_ANGLE
, &angle
);
1526 if (ris
== STREAM_UNSUPPORTED
)
1529 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1531 ds_fill_buffer(demuxer
->video
);
1532 resync_video_stream(sh_video
);
1536 ds_fill_buffer(demuxer
->audio
);
1537 resync_audio_stream(sh_audio
);
1543 int demuxer_audio_track_by_lang(demuxer_t
*d
, char *lang
)
1546 lang
+= strspn(lang
, ",");
1547 while ((len
= strcspn(lang
, ",")) > 0) {
1548 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1549 sh_audio_t
*sh
= d
->a_streams
[i
];
1550 if (sh
&& sh
->lang
&& strncmp(sh
->lang
, lang
, len
) == 0)
1554 lang
+= strspn(lang
, ",");
1559 int demuxer_sub_track_by_lang(demuxer_t
*d
, char *lang
)
1562 lang
+= strspn(lang
, ",");
1563 while ((len
= strcspn(lang
, ",")) > 0) {
1564 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1565 sh_sub_t
*sh
= d
->s_streams
[i
];
1566 if (sh
&& sh
->lang
&& strncmp(sh
->lang
, lang
, len
) == 0)
1570 lang
+= strspn(lang
, ",");
1575 int demuxer_default_audio_track(demuxer_t
*d
)
1578 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1579 sh_audio_t
*sh
= d
->a_streams
[i
];
1580 if (sh
&& sh
->default_track
)
1583 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1584 sh_audio_t
*sh
= d
->a_streams
[i
];
1591 int demuxer_default_sub_track(demuxer_t
*d
)
1594 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1595 sh_sub_t
*sh
= d
->s_streams
[i
];
1596 if (sh
&& sh
->default_track
)