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_real
;
49 extern const demuxer_desc_t demuxer_desc_smjpeg
;
50 extern const demuxer_desc_t demuxer_desc_matroska
;
51 extern const demuxer_desc_t demuxer_desc_realaudio
;
52 extern const demuxer_desc_t demuxer_desc_vqf
;
53 extern const demuxer_desc_t demuxer_desc_mov
;
54 extern const demuxer_desc_t demuxer_desc_vivo
;
55 extern const demuxer_desc_t demuxer_desc_fli
;
56 extern const demuxer_desc_t demuxer_desc_film
;
57 extern const demuxer_desc_t demuxer_desc_roq
;
58 extern const demuxer_desc_t demuxer_desc_gif
;
59 extern const demuxer_desc_t demuxer_desc_ogg
;
60 extern const demuxer_desc_t demuxer_desc_avs
;
61 extern const demuxer_desc_t demuxer_desc_pva
;
62 extern const demuxer_desc_t demuxer_desc_nsv
;
63 extern const demuxer_desc_t demuxer_desc_mpeg_ts
;
64 extern const demuxer_desc_t demuxer_desc_lmlm4
;
65 extern const demuxer_desc_t demuxer_desc_mpeg_ps
;
66 extern const demuxer_desc_t demuxer_desc_mpeg_pes
;
67 extern const demuxer_desc_t demuxer_desc_mpeg_es
;
68 extern const demuxer_desc_t demuxer_desc_mpeg_gxf
;
69 extern const demuxer_desc_t demuxer_desc_mpeg4_es
;
70 extern const demuxer_desc_t demuxer_desc_h264_es
;
71 extern const demuxer_desc_t demuxer_desc_rawdv
;
72 extern const demuxer_desc_t demuxer_desc_mpc
;
73 extern const demuxer_desc_t demuxer_desc_audio
;
74 extern const demuxer_desc_t demuxer_desc_xmms
;
75 extern const demuxer_desc_t demuxer_desc_mpeg_ty
;
76 extern const demuxer_desc_t demuxer_desc_rtp
;
77 extern const demuxer_desc_t demuxer_desc_rtp_nemesi
;
78 extern const demuxer_desc_t demuxer_desc_lavf
;
79 extern const demuxer_desc_t demuxer_desc_lavf_preferred
;
80 extern const demuxer_desc_t demuxer_desc_aac
;
81 extern const demuxer_desc_t demuxer_desc_nut
;
82 extern const demuxer_desc_t demuxer_desc_mng
;
84 /* Please do not add any new demuxers here. If you want to implement a new
85 * demuxer, add it to libavformat, except for wrappers around external
86 * libraries and demuxers requiring binary support. */
88 const demuxer_desc_t
*const demuxer_list
[] = {
89 &demuxer_desc_rawaudio
,
90 &demuxer_desc_rawvideo
,
95 #ifdef CONFIG_LIBAVFORMAT
96 &demuxer_desc_lavf_preferred
,
103 &demuxer_desc_smjpeg
,
104 &demuxer_desc_matroska
,
105 &demuxer_desc_realaudio
,
115 #ifdef CONFIG_OGGVORBIS
118 #ifdef CONFIG_WIN32DLL
122 &demuxer_desc_mpeg_ts
,
124 &demuxer_desc_mpeg_ps
,
125 &demuxer_desc_mpeg_pes
,
126 &demuxer_desc_mpeg_es
,
127 &demuxer_desc_mpeg_gxf
,
128 &demuxer_desc_mpeg4_es
,
129 &demuxer_desc_h264_es
,
131 &demuxer_desc_mpeg_ty
,
132 #ifdef CONFIG_LIVE555
135 #ifdef CONFIG_LIBNEMESI
136 &demuxer_desc_rtp_nemesi
,
138 #ifdef CONFIG_LIBAVFORMAT
141 #ifdef CONFIG_MUSEPACK
144 #ifdef CONFIG_LIBDV095
157 /* Please do not add any new demuxers here. If you want to implement a new
158 * demuxer, add it to libavformat, except for wrappers around external
159 * libraries and demuxers requiring binary support. */
163 void free_demuxer_stream(demux_stream_t
*ds
)
169 demux_stream_t
*new_demuxer_stream(struct demuxer_st
*demuxer
, int id
)
171 demux_stream_t
*ds
= malloc(sizeof(demux_stream_t
));
172 ds
->buffer_pos
= ds
->buffer_size
= 0;
183 ds
->first
= ds
->last
= ds
->current
= NULL
;
185 ds
->demuxer
= demuxer
;
188 ds
->asf_packet
= NULL
;
190 ds
->ss_mul
= ds
->ss_div
= 0;
198 * Get demuxer description structure for a given demuxer type
200 * @param file_format type of the demuxer
201 * @return structure for the demuxer, NULL if not found
203 static const demuxer_desc_t
*get_demuxer_desc_from_type(int file_format
)
207 for (i
= 0; demuxer_list
[i
]; i
++)
208 if (file_format
== demuxer_list
[i
]->type
)
209 return demuxer_list
[i
];
215 demuxer_t
*new_demuxer(stream_t
*stream
, int type
, int a_id
, int v_id
,
216 int s_id
, char *filename
)
218 demuxer_t
*d
= malloc(sizeof(demuxer_t
));
219 memset(d
, 0, sizeof(demuxer_t
));
221 d
->stream_pts
= MP_NOPTS_VALUE
;
222 d
->reference_clock
= MP_NOPTS_VALUE
;
223 d
->movi_start
= stream
->start_pos
;
224 d
->movi_end
= stream
->end_pos
;
228 d
->audio
= new_demuxer_stream(d
, a_id
);
229 d
->video
= new_demuxer_stream(d
, v_id
);
230 d
->sub
= new_demuxer_stream(d
, s_id
);
233 if (!(d
->desc
= get_demuxer_desc_from_type(type
)))
234 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
235 "BUG! Invalid demuxer type in new_demuxer(), "
236 "big troubles ahead.");
237 if (filename
) // Filename hack for avs_check_file
238 d
->filename
= strdup(filename
);
239 stream_reset(stream
);
240 stream_seek(stream
, stream
->start_pos
);
244 extern int dvdsub_id
;
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 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SUBTITLE_ID=%d\n", sid
);
262 if (sid
== dvdsub_id
) {
263 demuxer
->sub
->id
= id
;
264 demuxer
->sub
->sh
= demuxer
->s_streams
[id
];
266 return demuxer
->s_streams
[id
];
269 void free_sh_sub(sh_sub_t
*sh
)
271 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_sub at %p\n", sh
);
275 ass_free_track(sh
->ass_track
);
281 sh_audio_t
*new_sh_audio_aid(demuxer_t
*demuxer
, int id
, int aid
)
283 if (id
> MAX_A_STREAMS
- 1 || id
< 0) {
284 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
285 "Requested audio stream id overflow (%d > %d)\n", id
,
289 if (demuxer
->a_streams
[id
])
290 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, MSGTR_AudioStreamRedefined
, id
);
292 sh_audio_t
*sh
= calloc(1, sizeof(sh_audio_t
));
293 mp_msg(MSGT_DEMUXER
, MSGL_V
, MSGTR_FoundAudioStream
, id
);
294 demuxer
->a_streams
[id
] = sh
;
296 sh
->ds
= demuxer
->audio
;
299 sh
->sample_format
= AF_FORMAT_S16_NE
;
300 sh
->audio_out_minsize
= 8192; /* default size, maybe not enough for Win32/ACM */
301 sh
->pts
= MP_NOPTS_VALUE
;
302 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_ID=%d\n", aid
);
304 return demuxer
->a_streams
[id
];
307 void free_sh_audio(demuxer_t
*demuxer
, int id
)
309 sh_audio_t
*sh
= demuxer
->a_streams
[id
];
310 demuxer
->a_streams
[id
] = NULL
;
311 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_audio at %p\n", sh
);
318 sh_video_t
*new_sh_video_vid(demuxer_t
*demuxer
, int id
, int vid
)
320 if (id
> MAX_V_STREAMS
- 1 || id
< 0) {
321 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
322 "Requested video stream id overflow (%d > %d)\n", id
,
326 if (demuxer
->v_streams
[id
])
327 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, MSGTR_VideoStreamRedefined
, id
);
329 sh_video_t
*sh
= calloc(1, sizeof(sh_video_t
));
330 mp_msg(MSGT_DEMUXER
, MSGL_V
, MSGTR_FoundVideoStream
, id
);
331 demuxer
->v_streams
[id
] = sh
;
333 sh
->ds
= demuxer
->video
;
334 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_ID=%d\n", vid
);
336 return demuxer
->v_streams
[id
];
339 void free_sh_video(sh_video_t
*sh
)
341 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing sh_video at %p\n", sh
);
346 void free_demuxer(demuxer_t
*demuxer
)
349 mp_msg(MSGT_DEMUXER
, MSGL_DBG2
, "DEMUXER: freeing demuxer at %p\n",
351 if (demuxer
->desc
->close
)
352 demuxer
->desc
->close(demuxer
);
353 // Very ugly hack to make it behave like old implementation
354 if (demuxer
->desc
->type
== DEMUXER_TYPE_DEMUXERS
)
355 goto skip_streamfree
;
357 for (i
= 0; i
< MAX_A_STREAMS
; i
++)
358 if (demuxer
->a_streams
[i
])
359 free_sh_audio(demuxer
, i
);
360 for (i
= 0; i
< MAX_V_STREAMS
; i
++)
361 if (demuxer
->v_streams
[i
])
362 free_sh_video(demuxer
->v_streams
[i
]);
363 for (i
= 0; i
< MAX_S_STREAMS
; i
++)
364 if (demuxer
->s_streams
[i
])
365 free_sh_sub(demuxer
->s_streams
[i
]);
367 free_demuxer_stream(demuxer
->audio
);
368 free_demuxer_stream(demuxer
->video
);
369 free_demuxer_stream(demuxer
->sub
);
372 for (i
= 0; demuxer
->info
[i
] != NULL
; i
++)
373 free(demuxer
->info
[i
]);
376 free(demuxer
->filename
);
377 if (demuxer
->chapters
) {
378 for (i
= 0; i
< demuxer
->num_chapters
; i
++)
379 free(demuxer
->chapters
[i
].name
);
380 free(demuxer
->chapters
);
382 if (demuxer
->attachments
) {
383 for (i
= 0; i
< demuxer
->num_attachments
; i
++) {
384 free(demuxer
->attachments
[i
].name
);
385 free(demuxer
->attachments
[i
].type
);
386 free(demuxer
->attachments
[i
].data
);
388 free(demuxer
->attachments
);
394 void ds_add_packet(demux_stream_t
*ds
, demux_packet_t
*dp
)
396 // append packet to DS stream:
398 ds
->bytes
+= dp
->len
;
400 // next packet in stream
404 // first packet in stream
405 ds
->first
= ds
->last
= dp
;
407 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
408 "DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
409 (ds
== ds
->demuxer
->audio
) ? "d_audio" : "d_video", dp
->len
,
410 dp
->pts
, (unsigned int) dp
->pos
, ds
->demuxer
->audio
->packs
,
411 ds
->demuxer
->video
->packs
);
414 void ds_read_packet(demux_stream_t
*ds
, stream_t
*stream
, int len
,
415 double pts
, off_t pos
, int flags
)
417 demux_packet_t
*dp
= new_demux_packet(len
);
418 len
= stream_read(stream
, dp
->buffer
, len
);
419 resize_demux_packet(dp
, len
);
423 // append packet to DS stream:
424 ds_add_packet(ds
, dp
);
428 // 0 = EOF or no stream found or invalid type
429 // 1 = successfully read a packet
431 int demux_fill_buffer(demuxer_t
*demux
, demux_stream_t
*ds
)
433 // Note: parameter 'ds' can be NULL!
434 return demux
->desc
->fill_buffer(demux
, ds
);
440 #define MAX_ACUMULATED_PACKETS 64
441 int ds_fill_buffer(demux_stream_t
*ds
)
443 demuxer_t
*demux
= ds
->demuxer
;
445 free_demux_packet(ds
->current
);
447 if (mp_msg_test(MSGT_DEMUXER
, MSGL_DBG3
)) {
448 if (ds
== demux
->audio
)
449 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
450 "ds_fill_buffer(d_audio) called\n");
451 else if (ds
== demux
->video
)
452 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
453 "ds_fill_buffer(d_video) called\n");
454 else if (ds
== demux
->sub
)
455 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
, "ds_fill_buffer(d_sub) called\n");
457 mp_dbg(MSGT_DEMUXER
, MSGL_DBG3
,
458 "ds_fill_buffer(unknown 0x%X) called\n", (unsigned int) ds
);
462 demux_packet_t
*p
= ds
->first
;
464 if (demux
->reference_clock
!= MP_NOPTS_VALUE
) {
465 if ( p
->pts
!= MP_NOPTS_VALUE
466 && p
->pts
> demux
->reference_clock
467 && ds
->packs
< MAX_ACUMULATED_PACKETS
) {
468 if (demux_fill_buffer(demux
, ds
))
474 ds
->buffer
= p
->buffer
;
476 ds
->buffer_size
= p
->len
;
478 ds
->dpos
+= p
->len
; // !!!
480 if (p
->pts
!= MP_NOPTS_VALUE
) {
484 ds
->pts_bytes
+= p
->len
; // !!!
485 if (p
->stream_pts
!= MP_NOPTS_VALUE
)
486 demux
->stream_pts
= p
->stream_pts
;
487 ds
->flags
= p
->flags
;
497 if (demux
->audio
->packs
>= MAX_PACKS
498 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
499 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_TooManyAudioInBuffer
,
500 demux
->audio
->packs
, demux
->audio
->bytes
);
501 mp_msg(MSGT_DEMUXER
, MSGL_HINT
, MSGTR_MaybeNI
);
504 if (demux
->video
->packs
>= MAX_PACKS
505 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
506 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_TooManyVideoInBuffer
,
507 demux
->video
->packs
, demux
->video
->bytes
);
508 mp_msg(MSGT_DEMUXER
, MSGL_HINT
, MSGTR_MaybeNI
);
511 if (!demux_fill_buffer(demux
, ds
)) {
512 mp_dbg(MSGT_DEMUXER
, MSGL_DBG2
,
513 "ds_fill_buffer()->demux_fill_buffer() failed\n");
517 ds
->buffer_pos
= ds
->buffer_size
= 0;
519 mp_msg(MSGT_DEMUXER
, MSGL_V
,
520 "ds_fill_buffer: EOF reached (stream: %s) \n",
521 ds
== demux
->audio
? "audio" : "video");
526 int demux_read_data(demux_stream_t
*ds
, unsigned char *mem
, int len
)
531 x
= ds
->buffer_size
- ds
->buffer_pos
;
533 if (!ds_fill_buffer(ds
))
539 fast_memcpy(mem
+ bytes
, &ds
->buffer
[ds
->buffer_pos
], x
);
549 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
550 * \param mem memory to read data into, may be NULL to discard data
551 * \param maxlen maximum number of bytes to read
552 * \param read number of bytes actually read
553 * \param pattern pattern to search for (lowest 8 bits are ignored)
554 * \return whether pattern was found
556 int demux_pattern_3(demux_stream_t
*ds
, unsigned char *mem
, int maxlen
,
557 int *read
, uint32_t pattern
)
559 register uint32_t head
= 0xffffff00;
560 register uint32_t pat
= pattern
& 0xffffff00;
563 register unsigned char *ds_buf
= &ds
->buffer
[ds
->buffer_size
];
564 int len
= ds
->buffer_size
- ds
->buffer_pos
;
565 register long pos
= -len
;
566 if (unlikely(pos
>= 0)) { // buffer is empty
573 } while (++pos
&& head
!= pat
);
575 if (total_len
+ len
> maxlen
)
576 len
= maxlen
- total_len
;
577 len
= demux_read_data(ds
, mem
? &mem
[total_len
] : NULL
, len
);
579 } while ((head
!= pat
|| total_len
< 3) && total_len
< maxlen
&& !ds
->eof
);
582 return total_len
>= 3 && head
== pat
;
585 void ds_free_packs(demux_stream_t
*ds
)
587 demux_packet_t
*dp
= ds
->first
;
589 demux_packet_t
*dn
= dp
->next
;
590 free_demux_packet(dp
);
593 if (ds
->asf_packet
) {
594 // free unfinished .asf fragments:
595 free(ds
->asf_packet
->buffer
);
596 free(ds
->asf_packet
);
597 ds
->asf_packet
= NULL
;
599 ds
->first
= ds
->last
= NULL
;
600 ds
->packs
= 0; // !!!!!
603 free_demux_packet(ds
->current
);
606 ds
->buffer_pos
= ds
->buffer_size
;
611 int ds_get_packet(demux_stream_t
*ds
, unsigned char **start
)
614 if (ds
->buffer_pos
>= ds
->buffer_size
) {
615 if (!ds_fill_buffer(ds
)) {
621 len
= ds
->buffer_size
- ds
->buffer_pos
;
622 *start
= &ds
->buffer
[ds
->buffer_pos
];
623 ds
->buffer_pos
+= len
;
627 int ds_get_packet_pts(demux_stream_t
*ds
, unsigned char **start
, double *pts
)
630 *pts
= MP_NOPTS_VALUE
;
631 if (ds
->buffer_pos
>= ds
->buffer_size
) {
632 if (!ds_fill_buffer(ds
)) {
638 // Return pts unless this read starts from the middle of a packet
640 *pts
= ds
->current
->pts
;
641 len
= ds
->buffer_size
- ds
->buffer_pos
;
642 *start
= &ds
->buffer
[ds
->buffer_pos
];
643 ds
->buffer_pos
+= len
;
647 int ds_get_packet_sub(demux_stream_t
*ds
, unsigned char **start
)
650 if (ds
->buffer_pos
>= ds
->buffer_size
) {
654 if (!ds_fill_buffer(ds
))
657 len
= ds
->buffer_size
- ds
->buffer_pos
;
658 *start
= &ds
->buffer
[ds
->buffer_pos
];
659 ds
->buffer_pos
+= len
;
663 double ds_get_next_pts(demux_stream_t
*ds
)
665 demuxer_t
*demux
= ds
->demuxer
;
667 if (demux
->audio
->packs
>= MAX_PACKS
668 || demux
->audio
->bytes
>= MAX_PACK_BYTES
) {
669 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_TooManyAudioInBuffer
,
670 demux
->audio
->packs
, demux
->audio
->bytes
);
671 mp_msg(MSGT_DEMUXER
, MSGL_HINT
, MSGTR_MaybeNI
);
672 return MP_NOPTS_VALUE
;
674 if (demux
->video
->packs
>= MAX_PACKS
675 || demux
->video
->bytes
>= MAX_PACK_BYTES
) {
676 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_TooManyVideoInBuffer
,
677 demux
->video
->packs
, demux
->video
->bytes
);
678 mp_msg(MSGT_DEMUXER
, MSGL_HINT
, MSGTR_MaybeNI
);
679 return MP_NOPTS_VALUE
;
681 if (!demux_fill_buffer(demux
, ds
))
682 return MP_NOPTS_VALUE
;
684 return ds
->first
->pts
;
687 // ====================================================================
689 void demuxer_help(void)
693 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "Available demuxers:\n");
694 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, " demuxer: type info: (comment)\n");
695 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DEMUXERS\n");
696 for (i
= 0; demuxer_list
[i
]; i
++) {
697 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Don't display special demuxers
699 if (demuxer_list
[i
]->comment
&& strlen(demuxer_list
[i
]->comment
))
700 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s (%s)\n",
701 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
702 demuxer_list
[i
]->info
, demuxer_list
[i
]->comment
);
704 mp_msg(MSGT_DEMUXER
, MSGL_INFO
, "%10s %2d %s\n",
705 demuxer_list
[i
]->name
, demuxer_list
[i
]->type
,
706 demuxer_list
[i
]->info
);
712 * Get demuxer type for a given demuxer name
714 * @param demuxer_name string with demuxer name of demuxer number
715 * @param force will be set if demuxer should be forced.
717 * @return DEMUXER_TYPE_xxx, -1 if error or not found
719 int get_demuxer_type_from_name(char *demuxer_name
, int *force
)
725 if (!demuxer_name
|| !demuxer_name
[0])
726 return DEMUXER_TYPE_UNKNOWN
;
728 *force
= demuxer_name
[0] == '+';
729 if (demuxer_name
[0] == '+')
730 demuxer_name
= &demuxer_name
[1];
731 for (i
= 0; demuxer_list
[i
]; i
++) {
732 if (demuxer_list
[i
]->type
> DEMUXER_TYPE_MAX
) // Can't select special demuxers from commandline
734 if (strcmp(demuxer_name
, demuxer_list
[i
]->name
) == 0)
735 return demuxer_list
[i
]->type
;
738 // No match found, try to parse name as an integer (demuxer number)
739 type_int
= strtol(demuxer_name
, &endptr
, 0);
740 if (*endptr
) // Conversion failed
742 if ((type_int
> 0) && (type_int
<= DEMUXER_TYPE_MAX
))
743 return (int) type_int
;
748 int extension_parsing
= 1; // 0=off 1=mixed (used only for unstable formats)
751 int user_correct_pts
= -1;
754 NOTE : Several demuxers may be opened at the same time so
755 demuxers should NEVER rely on an external var to enable them
756 self. If a demuxer can't do any auto-detection it should only use
757 file_format. The user can explicitly set file_format with the -demuxer
758 option so there is really no need for another extra var.
759 For convenience an option can be added to set file_format directly
760 to the right type (ex: rawaudio,rawvideo).
761 Also the stream can override the file_format so a demuxer which rely
762 on a special stream type can set file_format at the stream level
766 static demuxer_t
*demux_open_stream(stream_t
*stream
, int file_format
,
767 int force
, int audio_id
, int video_id
,
768 int dvdsub_id
, char *filename
)
770 demuxer_t
*demuxer
= NULL
;
772 sh_video_t
*sh_video
= NULL
;
774 const demuxer_desc_t
*demuxer_desc
;
778 // If somebody requested a demuxer check it
780 if ((demuxer_desc
= get_demuxer_desc_from_type(file_format
))) {
781 demuxer
= new_demuxer(stream
, demuxer_desc
->type
, audio_id
,
782 video_id
, dvdsub_id
, filename
);
783 if (demuxer_desc
->check_file
)
784 fformat
= demuxer_desc
->check_file(demuxer
);
785 if (force
|| !demuxer_desc
->check_file
)
786 fformat
= demuxer_desc
->type
;
788 if (fformat
== demuxer_desc
->type
) {
789 demuxer_t
*demux2
= demuxer
;
790 // Move messages to demuxer detection code?
791 mp_msg(MSGT_DEMUXER
, MSGL_INFO
,
792 MSGTR_Detected_XXX_FileFormat
,
793 demuxer_desc
->shortdesc
);
794 file_format
= fformat
;
795 if (!demuxer
->desc
->open
796 || (demux2
= demuxer
->desc
->open(demuxer
))) {
801 // Format changed after check, recurse
802 free_demuxer(demuxer
);
803 return demux_open_stream(stream
, fformat
, force
, audio_id
,
804 video_id
, dvdsub_id
, filename
);
807 // Check failed for forced demuxer, quit
808 free_demuxer(demuxer
);
812 // Test demuxers with safe file checks
813 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
814 if (demuxer_desc
->safe_check
) {
815 demuxer
= new_demuxer(stream
, demuxer_desc
->type
, audio_id
,
816 video_id
, dvdsub_id
, filename
);
817 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
818 if (fformat
== demuxer_desc
->type
) {
819 demuxer_t
*demux2
= demuxer
;
820 mp_msg(MSGT_DEMUXER
, MSGL_INFO
,
821 MSGTR_Detected_XXX_FileFormat
,
822 demuxer_desc
->shortdesc
);
823 file_format
= fformat
;
824 if (!demuxer
->desc
->open
825 || (demux2
= demuxer
->desc
->open(demuxer
))) {
830 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
831 return demuxer
; // handled in mplayer.c
832 // Format changed after check, recurse
833 free_demuxer(demuxer
);
834 demuxer
= demux_open_stream(stream
, fformat
, force
,
836 dvdsub_id
, filename
);
838 return demuxer
; // done!
839 file_format
= DEMUXER_TYPE_UNKNOWN
;
842 free_demuxer(demuxer
);
847 // If no forced demuxer perform file extension based detection
848 // Ok. We're over the stable detectable fileformats, the next ones are
849 // a bit fuzzy. So by default (extension_parsing==1) try extension-based
851 if (file_format
== DEMUXER_TYPE_UNKNOWN
&& filename
852 && extension_parsing
== 1) {
853 file_format
= demuxer_type_by_filename(filename
);
854 if (file_format
!= DEMUXER_TYPE_UNKNOWN
) {
855 // we like recursion :)
856 demuxer
= demux_open_stream(stream
, file_format
, force
, audio_id
,
857 video_id
, dvdsub_id
, filename
);
859 return demuxer
; // done!
860 file_format
= DEMUXER_TYPE_UNKNOWN
; // continue fuzzy guessing...
861 mp_msg(MSGT_DEMUXER
, MSGL_V
,
862 "demuxer: continue fuzzy content-based format guessing...\n");
865 // Try detection for all other demuxers
866 for (i
= 0; (demuxer_desc
= demuxer_list
[i
]); i
++) {
867 if (!demuxer_desc
->safe_check
&& demuxer_desc
->check_file
) {
868 demuxer
= new_demuxer(stream
, demuxer_desc
->type
, audio_id
,
869 video_id
, dvdsub_id
, filename
);
870 if ((fformat
= demuxer_desc
->check_file(demuxer
)) != 0) {
871 if (fformat
== demuxer_desc
->type
) {
872 demuxer_t
*demux2
= demuxer
;
873 mp_msg(MSGT_DEMUXER
, MSGL_INFO
,
874 MSGTR_Detected_XXX_FileFormat
,
875 demuxer_desc
->shortdesc
);
876 file_format
= fformat
;
877 if (!demuxer
->desc
->open
878 || (demux2
= demuxer
->desc
->open(demuxer
))) {
883 if (fformat
== DEMUXER_TYPE_PLAYLIST
)
884 return demuxer
; // handled in mplayer.c
885 // Format changed after check, recurse
886 free_demuxer(demuxer
);
887 demuxer
= demux_open_stream(stream
, fformat
, force
,
889 dvdsub_id
, filename
);
891 return demuxer
; // done!
892 file_format
= DEMUXER_TYPE_UNKNOWN
;
895 free_demuxer(demuxer
);
901 //====== File format recognized, set up these for compatibility: =========
904 demuxer
->file_format
= file_format
;
906 if ((sh_video
= demuxer
->video
->sh
) && sh_video
->bih
) {
907 int biComp
= le2me_32(sh_video
->bih
->biCompression
);
908 mp_msg(MSGT_DEMUX
, MSGL_INFO
,
909 "VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
910 (char *) &biComp
, sh_video
->bih
->biWidth
,
911 sh_video
->bih
->biHeight
, sh_video
->bih
->biBitCount
,
912 sh_video
->fps
, sh_video
->i_bps
* 0.008f
,
913 sh_video
->i_bps
/ 1024.0f
);
916 if (ass_enabled
&& ass_library
) {
917 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
918 sh_sub_t
*sh
= demuxer
->s_streams
[i
];
919 if (sh
&& sh
->type
== 'a') {
920 sh
->ass_track
= ass_new_track(ass_library
);
921 if (sh
->ass_track
&& sh
->extradata
)
922 ass_process_codec_private(sh
->ass_track
, sh
->extradata
,
924 } else if (sh
&& sh
->type
!= 'v')
925 sh
->ass_track
= ass_default_track(ass_library
);
932 char *audio_stream
= NULL
;
933 char *sub_stream
= NULL
;
934 int audio_stream_cache
= 0;
936 char *demuxer_name
= NULL
; // parameter from -demuxer
937 char *audio_demuxer_name
= NULL
; // parameter from -audio-demuxer
938 char *sub_demuxer_name
= NULL
; // parameter from -sub-demuxer
940 extern int hr_mp3_seek
;
942 extern float stream_cache_min_percent
;
943 extern float stream_cache_seek_min_percent
;
945 demuxer_t
*demux_open(stream_t
*vs
, int file_format
, int audio_id
,
946 int video_id
, int dvdsub_id
, char *filename
)
948 stream_t
*as
= NULL
, *ss
= NULL
;
949 demuxer_t
*vd
, *ad
= NULL
, *sd
= NULL
;
951 int afmt
= DEMUXER_TYPE_UNKNOWN
, sfmt
= DEMUXER_TYPE_UNKNOWN
;
953 int audio_demuxer_type
= 0, sub_demuxer_type
= 0;
954 int demuxer_force
= 0, audio_demuxer_force
= 0, sub_demuxer_force
= 0;
957 get_demuxer_type_from_name(demuxer_name
, &demuxer_force
)) < 0) {
958 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-demuxer %s does not exist.\n",
961 if ((audio_demuxer_type
=
962 get_demuxer_type_from_name(audio_demuxer_name
,
963 &audio_demuxer_force
)) < 0) {
964 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-audio-demuxer %s does not exist.\n",
967 if ((sub_demuxer_type
=
968 get_demuxer_type_from_name(sub_demuxer_name
,
969 &sub_demuxer_force
)) < 0) {
970 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "-sub-demuxer %s does not exist.\n",
975 as
= open_stream(audio_stream
, 0, &afmt
);
977 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_CannotOpenAudioStream
,
981 if (audio_stream_cache
) {
982 if (!stream_enable_cache
983 (as
, audio_stream_cache
* 1024,
984 audio_stream_cache
* 1024 * (stream_cache_min_percent
/
986 audio_stream_cache
* 1024 * (stream_cache_seek_min_percent
/
989 mp_msg(MSGT_DEMUXER
, MSGL_ERR
,
990 "Can't enable audio stream cache\n");
996 ss
= open_stream(sub_stream
, 0, &sfmt
);
998 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, MSGTR_CannotOpenSubtitlesStream
,
1004 vd
= demux_open_stream(vs
, demuxer_type
? demuxer_type
: file_format
,
1005 demuxer_force
, audio_stream
? -2 : audio_id
,
1006 video_id
, sub_stream
? -2 : dvdsub_id
, filename
);
1015 ad
= demux_open_stream(as
,
1016 audio_demuxer_type
? audio_demuxer_type
: afmt
,
1017 audio_demuxer_force
, audio_id
, -2, -2,
1020 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, MSGTR_OpeningAudioDemuxerFailed
,
1023 } else if (ad
->audio
->sh
1024 && ((sh_audio_t
*) ad
->audio
->sh
)->format
== 0x55) // MP3
1025 hr_mp3_seek
= 1; // Enable high res seeking
1028 sd
= demux_open_stream(ss
, sub_demuxer_type
? sub_demuxer_type
: sfmt
,
1029 sub_demuxer_force
, -2, -2, dvdsub_id
,
1032 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
1033 MSGTR_OpeningSubtitlesDemuxerFailed
, sub_stream
);
1039 res
= new_demuxers_demuxer(vd
, ad
, sd
);
1041 res
= new_demuxers_demuxer(vd
, ad
, vd
);
1043 res
= new_demuxers_demuxer(vd
, vd
, sd
);
1047 correct_pts
= user_correct_pts
;
1048 if (correct_pts
< 0)
1049 correct_pts
= demux_control(res
, DEMUXER_CTRL_CORRECT_PTS
, NULL
)
1055 void demux_flush(demuxer_t
*demuxer
)
1057 ds_free_packs(demuxer
->video
);
1058 ds_free_packs(demuxer
->audio
);
1059 ds_free_packs(demuxer
->sub
);
1062 int demux_seek(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
,
1065 demux_stream_t
*d_audio
= demuxer
->audio
;
1066 demux_stream_t
*d_video
= demuxer
->video
;
1067 sh_audio_t
*sh_audio
= d_audio
->sh
;
1068 sh_video_t
*sh_video
= d_video
->sh
;
1072 if (!demuxer
->seekable
) {
1073 if (demuxer
->file_format
== DEMUXER_TYPE_AVI
)
1074 mp_msg(MSGT_SEEK
, MSGL_WARN
, MSGTR_CantSeekRawAVI
);
1076 else if (demuxer
->file_format
== DEMUXER_TYPE_TV
)
1077 mp_msg(MSGT_SEEK
, MSGL_WARN
, MSGTR_TVInputNotSeekable
);
1080 mp_msg(MSGT_SEEK
, MSGL_WARN
, MSGTR_CantSeekFile
);
1084 demux_flush(demuxer
);
1085 // clear demux buffers:
1087 sh_audio
->a_buffer_len
= 0;
1089 demuxer
->stream
->eof
= 0;
1090 demuxer
->video
->eof
= 0;
1091 demuxer
->audio
->eof
= 0;
1094 sh_video
->timer
= 0; // !!!!!!
1096 if (flags
& SEEK_ABSOLUTE
)
1099 if (demuxer
->stream_pts
== MP_NOPTS_VALUE
)
1101 pts
= demuxer
->stream_pts
;
1104 if (flags
& SEEK_FACTOR
) {
1105 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_TIME_LENGTH
, &tmp
)
1106 == STREAM_UNSUPPORTED
)
1108 pts
+= tmp
* rel_seek_secs
;
1110 pts
+= rel_seek_secs
;
1112 if (stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_TIME
, &pts
) !=
1113 STREAM_UNSUPPORTED
) {
1114 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1119 if (demuxer
->desc
->seek
)
1120 demuxer
->desc
->seek(demuxer
, rel_seek_secs
, audio_delay
, flags
);
1123 resync_audio_stream(sh_audio
);
1128 int demux_info_add(demuxer_t
*demuxer
, const char *opt
, const char *param
)
1130 char **info
= demuxer
->info
;
1134 for (n
= 0; info
&& info
[2 * n
] != NULL
; n
++) {
1135 if (!strcasecmp(opt
, info
[2 * n
])) {
1136 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_DemuxerInfoChanged
, opt
,
1138 free(info
[2 * n
+ 1]);
1139 info
[2 * n
+ 1] = strdup(param
);
1144 info
= demuxer
->info
= (char **) realloc(info
,
1145 (2 * (n
+ 2)) * sizeof(char *));
1146 info
[2 * n
] = strdup(opt
);
1147 info
[2 * n
+ 1] = strdup(param
);
1148 memset(&info
[2 * (n
+ 1)], 0, 2 * sizeof(char *));
1153 int demux_info_print(demuxer_t
*demuxer
)
1155 char **info
= demuxer
->info
;
1161 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_ClipInfo
);
1162 for (n
= 0; info
[2 * n
] != NULL
; n
++) {
1163 mp_msg(MSGT_DEMUX
, MSGL_INFO
, " %s: %s\n", info
[2 * n
],
1165 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_NAME%d=%s\n", n
,
1167 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_VALUE%d=%s\n", n
,
1170 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CLIP_INFO_N=%d\n", n
);
1175 char *demux_info_get(demuxer_t
*demuxer
, const char *opt
)
1178 char **info
= demuxer
->info
;
1180 for (i
= 0; info
&& info
[2 * i
] != NULL
; i
++) {
1181 if (!strcasecmp(opt
, info
[2 * i
]))
1182 return info
[2 * i
+ 1];
1188 int demux_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
1191 if (demuxer
->desc
->control
)
1192 return demuxer
->desc
->control(demuxer
, cmd
, arg
);
1194 return DEMUXER_CTRL_NOTIMPL
;
1199 double demuxer_get_time_length(demuxer_t
*demuxer
)
1201 double get_time_ans
;
1202 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1203 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1204 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
1206 (demuxer
, DEMUXER_CTRL_GET_TIME_LENGTH
, (void *) &get_time_ans
) <= 0) {
1207 if (sh_video
&& sh_video
->i_bps
&& sh_audio
&& sh_audio
->i_bps
)
1208 get_time_ans
= (double) (demuxer
->movi_end
-
1209 demuxer
->movi_start
) / (sh_video
->i_bps
+
1211 else if (sh_video
&& sh_video
->i_bps
)
1212 get_time_ans
= (double) (demuxer
->movi_end
-
1213 demuxer
->movi_start
) / sh_video
->i_bps
;
1214 else if (sh_audio
&& sh_audio
->i_bps
)
1215 get_time_ans
= (double) (demuxer
->movi_end
-
1216 demuxer
->movi_start
) / sh_audio
->i_bps
;
1220 return get_time_ans
;
1224 * \brief demuxer_get_current_time() returns the time of the current play in three possible ways:
1225 * either when the stream reader satisfies STREAM_CTRL_GET_CURRENT_TIME (e.g. dvd)
1226 * or using sh_video->pts when the former method fails
1228 * \return the current play time
1230 int demuxer_get_current_time(demuxer_t
*demuxer
)
1232 double get_time_ans
= 0;
1233 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1234 if (demuxer
->stream_pts
!= MP_NOPTS_VALUE
)
1235 get_time_ans
= demuxer
->stream_pts
;
1237 get_time_ans
= sh_video
->pts
;
1238 return (int) get_time_ans
;
1241 int demuxer_get_percent_pos(demuxer_t
*demuxer
)
1244 int res
= demux_control(demuxer
, DEMUXER_CTRL_GET_PERCENT_POS
, &ans
);
1245 int len
= (demuxer
->movi_end
- demuxer
->movi_start
) / 100;
1248 ans
= (demuxer
->filepos
- demuxer
->movi_start
) / len
;
1259 int demuxer_switch_audio(demuxer_t
*demuxer
, int index
)
1261 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_AUDIO
, &index
);
1262 if (res
== DEMUXER_CTRL_NOTIMPL
)
1263 index
= demuxer
->audio
->id
;
1264 if (demuxer
->audio
->id
>= 0)
1265 demuxer
->audio
->sh
= demuxer
->a_streams
[demuxer
->audio
->id
];
1267 demuxer
->audio
->sh
= NULL
;
1271 int demuxer_switch_video(demuxer_t
*demuxer
, int index
)
1273 int res
= demux_control(demuxer
, DEMUXER_CTRL_SWITCH_VIDEO
, &index
);
1274 if (res
== DEMUXER_CTRL_NOTIMPL
)
1275 index
= demuxer
->video
->id
;
1276 if (demuxer
->video
->id
>= 0)
1277 demuxer
->video
->sh
= demuxer
->v_streams
[demuxer
->video
->id
];
1279 demuxer
->video
->sh
= NULL
;
1283 int demuxer_add_attachment(demuxer_t
*demuxer
, const char *name
,
1284 const char *type
, const void *data
, size_t size
)
1286 if (!(demuxer
->num_attachments
& 31))
1287 demuxer
->attachments
= realloc(demuxer
->attachments
,
1288 (demuxer
->num_attachments
+ 32) * sizeof(demux_attachment_t
));
1290 demuxer
->attachments
[demuxer
->num_attachments
].name
= strdup(name
);
1291 demuxer
->attachments
[demuxer
->num_attachments
].type
= strdup(type
);
1292 demuxer
->attachments
[demuxer
->num_attachments
].data
= malloc(size
);
1293 memcpy(demuxer
->attachments
[demuxer
->num_attachments
].data
, data
, size
);
1294 demuxer
->attachments
[demuxer
->num_attachments
].data_size
= size
;
1296 return demuxer
->num_attachments
++;
1299 int demuxer_add_chapter(demuxer_t
*demuxer
, const char *name
, uint64_t start
,
1302 if (demuxer
->chapters
== NULL
)
1303 demuxer
->chapters
= malloc(32 * sizeof(*demuxer
->chapters
));
1304 else if (!(demuxer
->num_chapters
% 32))
1305 demuxer
->chapters
= realloc(demuxer
->chapters
,
1306 (demuxer
->num_chapters
+ 32) *
1307 sizeof(*demuxer
->chapters
));
1309 demuxer
->chapters
[demuxer
->num_chapters
].start
= start
;
1310 demuxer
->chapters
[demuxer
->num_chapters
].end
= end
;
1311 demuxer
->chapters
[demuxer
->num_chapters
].name
= strdup(name
? name
: MSGTR_Unknown
);
1313 return demuxer
->num_chapters
++;
1317 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1318 * either using the demuxer->chapters structure set by the demuxer
1319 * or asking help to the stream layer (e.g. dvd)
1320 * \param chapter - chapter number wished - 0-based
1321 * \param mode 0: relative to current main pts, 1: absolute
1322 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1323 * \param num_chapters number of chapters present (set by this function is param is not null)
1324 * \param chapter_name name of chapter found (set by this function is param is not null)
1325 * \return -1 on error, current chapter if successful
1328 int demuxer_seek_chapter(demuxer_t
*demuxer
, int chapter
, int mode
,
1329 float *seek_pts
, int *num_chapters
,
1330 char **chapter_name
)
1334 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1335 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1337 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1339 ris
= stream_control(demuxer
->stream
,
1340 STREAM_CTRL_GET_CURRENT_CHAPTER
, ¤t
);
1341 if (ris
== STREAM_UNSUPPORTED
)
1346 demux_flush(demuxer
);
1348 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SEEK_TO_CHAPTER
,
1350 if (ris
!= STREAM_UNSUPPORTED
)
1351 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1353 ds_fill_buffer(demuxer
->video
);
1354 resync_video_stream(sh_video
);
1358 ds_fill_buffer(demuxer
->audio
);
1359 resync_audio_stream(sh_audio
);
1361 // exit status may be ok, but main() doesn't have to seek itself
1362 // (because e.g. dvds depend on sectors, not on pts)
1366 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1367 num_chapters
) == STREAM_UNSUPPORTED
)
1372 *chapter_name
= NULL
;
1373 if (num_chapters
&& *num_chapters
) {
1374 char *tmp
= malloc(16);
1376 sprintf(tmp
, " of %3d", *num_chapters
);
1377 *chapter_name
= tmp
;
1382 return ris
!= STREAM_UNSUPPORTED
? chapter
: -1;
1383 } else { // chapters structure is set in the demuxer
1384 total
= demuxer
->num_chapters
;
1386 if (mode
== 1) //absolute seeking
1388 else { //relative seeking
1390 now
= (sh_video
? sh_video
->pts
: (sh_audio
? sh_audio
->pts
: 0.))
1393 for (current
= total
- 1; current
>= 0; --current
) {
1394 demux_chapter_t
*chapter
= demuxer
->chapters
+ current
;
1395 if (chapter
->start
<= now
)
1401 if (current
>= total
)
1406 *seek_pts
= demuxer
->chapters
[current
].start
/ 1000.0;
1409 *num_chapters
= demuxer
->num_chapters
;
1412 if (demuxer
->chapters
[current
].name
)
1413 *chapter_name
= strdup(demuxer
->chapters
[current
].name
);
1415 *chapter_name
= NULL
;
1422 int demuxer_get_current_chapter(demuxer_t
*demuxer
)
1425 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1426 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_CURRENT_CHAPTER
,
1427 &chapter
) == STREAM_UNSUPPORTED
)
1430 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1431 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1433 now
= (sh_video
? sh_video
->pts
: (sh_audio
? sh_audio
->pts
: 0))
1435 for (chapter
= demuxer
->num_chapters
- 1; chapter
>= 0; --chapter
) {
1436 if (demuxer
->chapters
[chapter
].start
<= now
)
1443 char *demuxer_chapter_name(demuxer_t
*demuxer
, int chapter
)
1445 if (demuxer
->num_chapters
&& demuxer
->chapters
) {
1446 if (chapter
>= 0 && chapter
< demuxer
->num_chapters
1447 && demuxer
->chapters
[chapter
].name
)
1448 return strdup(demuxer
->chapters
[chapter
].name
);
1453 char *demuxer_chapter_display_name(demuxer_t
*demuxer
, int chapter
)
1455 char *chapter_name
= demuxer_chapter_name(demuxer
, chapter
);
1457 char *tmp
= malloc(strlen(chapter_name
) + 14);
1458 snprintf(tmp
, 63, "(%d) %s", chapter
+ 1, chapter_name
);
1462 int chapter_num
= demuxer_chapter_count(demuxer
);
1464 if (chapter_num
<= 0)
1465 sprintf(tmp
, "(%d)", chapter
+ 1);
1467 sprintf(tmp
, "(%d) of %d", chapter
+ 1, chapter_num
);
1472 float demuxer_chapter_time(demuxer_t
*demuxer
, int chapter
, float *end
)
1474 if (demuxer
->num_chapters
&& demuxer
->chapters
&& chapter
>= 0
1475 && chapter
< demuxer
->num_chapters
) {
1477 *end
= demuxer
->chapters
[chapter
].end
/ 1000.0;
1478 return demuxer
->chapters
[chapter
].start
/ 1000.0;
1483 int demuxer_chapter_count(demuxer_t
*demuxer
)
1485 if (!demuxer
->num_chapters
|| !demuxer
->chapters
) {
1486 int num_chapters
= 0;
1487 if (stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
,
1488 &num_chapters
) == STREAM_UNSUPPORTED
)
1490 return num_chapters
;
1492 return demuxer
->num_chapters
;
1495 int demuxer_angles_count(demuxer_t
*demuxer
)
1497 int ris
, angles
= -1;
1499 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_NUM_ANGLES
, &angles
);
1500 if (ris
== STREAM_UNSUPPORTED
)
1505 int demuxer_get_current_angle(demuxer_t
*demuxer
)
1507 int ris
, curr_angle
= -1;
1508 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_GET_ANGLE
, &curr_angle
);
1509 if (ris
== STREAM_UNSUPPORTED
)
1515 int demuxer_set_angle(demuxer_t
*demuxer
, int angle
)
1517 int ris
, angles
= -1;
1518 sh_video_t
*sh_video
= demuxer
->video
->sh
;
1519 sh_audio_t
*sh_audio
= demuxer
->audio
->sh
;
1521 angles
= demuxer_angles_count(demuxer
);
1522 if ((angles
< 1) || (angle
> angles
))
1525 demux_flush(demuxer
);
1527 ris
= stream_control(demuxer
->stream
, STREAM_CTRL_SET_ANGLE
, &angle
);
1528 if (ris
== STREAM_UNSUPPORTED
)
1531 demux_control(demuxer
, DEMUXER_CTRL_RESYNC
, NULL
);
1533 ds_fill_buffer(demuxer
->video
);
1534 resync_video_stream(sh_video
);
1538 ds_fill_buffer(demuxer
->audio
);
1539 resync_audio_stream(sh_audio
);
1545 int demuxer_audio_track_by_lang(demuxer_t
*d
, char *lang
)
1548 lang
+= strspn(lang
, ",");
1549 while ((len
= strcspn(lang
, ",")) > 0) {
1550 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1551 sh_audio_t
*sh
= d
->a_streams
[i
];
1552 if (sh
&& sh
->lang
&& strncmp(sh
->lang
, lang
, len
) == 0)
1556 lang
+= strspn(lang
, ",");
1561 int demuxer_sub_track_by_lang(demuxer_t
*d
, char *lang
)
1564 lang
+= strspn(lang
, ",");
1565 while ((len
= strcspn(lang
, ",")) > 0) {
1566 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1567 sh_sub_t
*sh
= d
->s_streams
[i
];
1568 if (sh
&& sh
->lang
&& strncmp(sh
->lang
, lang
, len
) == 0)
1572 lang
+= strspn(lang
, ",");
1577 int demuxer_default_audio_track(demuxer_t
*d
)
1580 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1581 sh_audio_t
*sh
= d
->a_streams
[i
];
1582 if (sh
&& sh
->default_track
)
1585 for (i
= 0; i
< MAX_A_STREAMS
; ++i
) {
1586 sh_audio_t
*sh
= d
->a_streams
[i
];
1593 int demuxer_default_sub_track(demuxer_t
*d
)
1596 for (i
= 0; i
< MAX_S_STREAMS
; ++i
) {
1597 sh_sub_t
*sh
= d
->s_streams
[i
];
1598 if (sh
&& sh
->default_track
)