options: Move ass_enabled to options struct
[mplayer/glamo.git] / libmpdemux / demuxer.c
blob564655609f6cae97e76eb54a779b2e8d3dbcb5d2
1 /*
2 * DEMUXER v2.5
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.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
29 #include "config.h"
30 #include "options.h"
31 #include "talloc.h"
32 #include "mp_msg.h"
33 #include "help_mp.h"
34 #include "m_config.h"
36 #include "libvo/fastmemcpy.h"
38 #include "stream/stream.h"
39 #include "demuxer.h"
40 #include "stheader.h"
41 #include "mf.h"
43 #include "libaf/af_format.h"
44 #include "libmpcodecs/dec_teletext.h"
46 #include "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!
52 #endif
53 #endif
55 void resync_video_stream(sh_video_t *sh_video);
56 void resync_audio_stream(sh_audio_t *sh_audio);
58 // Demuxer list
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,
109 #ifdef CONFIG_TV
110 &demuxer_desc_tv,
111 #endif
112 &demuxer_desc_mf,
113 #ifdef CONFIG_LIBAVFORMAT
114 &demuxer_desc_lavf_preferred,
115 #endif
116 &demuxer_desc_avi,
117 &demuxer_desc_y4m,
118 &demuxer_desc_asf,
119 &demuxer_desc_nsv,
120 &demuxer_desc_real,
121 &demuxer_desc_smjpeg,
122 &demuxer_desc_matroska,
123 &demuxer_desc_realaudio,
124 &demuxer_desc_vqf,
125 &demuxer_desc_mov,
126 &demuxer_desc_vivo,
127 &demuxer_desc_fli,
128 &demuxer_desc_film,
129 &demuxer_desc_roq,
130 #ifdef CONFIG_GIF
131 &demuxer_desc_gif,
132 #endif
133 #ifdef CONFIG_OGGVORBIS
134 &demuxer_desc_ogg,
135 #endif
136 #ifdef CONFIG_WIN32DLL
137 &demuxer_desc_avs,
138 #endif
139 &demuxer_desc_pva,
140 &demuxer_desc_mpeg_ts,
141 &demuxer_desc_lmlm4,
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,
148 &demuxer_desc_audio,
149 &demuxer_desc_mpeg_ty,
150 #ifdef CONFIG_LIVE555
151 &demuxer_desc_rtp,
152 #endif
153 #ifdef CONFIG_LIBNEMESI
154 &demuxer_desc_rtp_nemesi,
155 #endif
156 #ifdef CONFIG_LIBAVFORMAT
157 &demuxer_desc_lavf,
158 #endif
159 #ifdef CONFIG_MUSEPACK
160 &demuxer_desc_mpc,
161 #endif
162 #ifdef CONFIG_LIBDV095
163 &demuxer_desc_rawdv,
164 #endif
165 &demuxer_desc_aac,
166 #ifdef CONFIG_LIBNUT
167 &demuxer_desc_nut,
168 #endif
169 #ifdef CONFIG_XMMS
170 &demuxer_desc_xmms,
171 #endif
172 #ifdef CONFIG_MNG
173 &demuxer_desc_mng,
174 #endif
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. */
178 NULL
181 void free_demuxer_stream(demux_stream_t *ds)
183 ds_free_packs(ds);
184 free(ds);
187 demux_stream_t *new_demuxer_stream(struct demuxer *demuxer, int id)
189 demux_stream_t *ds = malloc(sizeof(demux_stream_t));
190 *ds = (demux_stream_t){
191 .id = id,
192 .demuxer = demuxer,
193 .asf_seq = -1,
195 return ds;
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)
207 int i;
209 for (i = 0; demuxer_list[i]; i++)
210 if (file_format == demuxer_list[i]->type)
211 return demuxer_list[i];
213 return NULL;
217 demuxer_t *new_demuxer(struct MPOpts *opts, stream_t *stream, int type,
218 int a_id, int v_id, int s_id, char *filename)
220 struct demuxer *d = talloc_zero(NULL, struct demuxer);
221 d->stream = stream;
222 d->stream_pts = MP_NOPTS_VALUE;
223 d->reference_clock = MP_NOPTS_VALUE;
224 d->movi_start = stream->start_pos;
225 d->movi_end = stream->end_pos;
226 d->seekable = 1;
227 d->synced = 0;
228 d->filepos = -1;
229 d->audio = new_demuxer_stream(d, a_id);
230 d->video = new_demuxer_stream(d, v_id);
231 d->sub = new_demuxer_stream(d, s_id);
232 d->type = type;
233 d->opts = opts;
234 if (type)
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);
243 return d;
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,
251 MAX_S_STREAMS);
252 return NULL;
254 if (demuxer->s_streams[id])
255 mp_msg(MSGT_DEMUXER, MSGL_WARN, "Sub stream %i redefined\n", id);
256 else {
257 sh_sub_t *sh = calloc(1, sizeof(sh_sub_t));
258 demuxer->s_streams[id] = sh;
259 sh->sid = sid;
260 sh->opts = demuxer->opts;
261 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SUBTITLE_ID=%d\n", sid);
263 if (sid == demuxer->opts->sub_id) {
264 demuxer->sub->id = id;
265 demuxer->sub->sh = demuxer->s_streams[id];
267 return demuxer->s_streams[id];
270 static void free_sh_sub(sh_sub_t *sh)
272 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_sub at %p\n", sh);
273 free(sh->extradata);
274 #ifdef CONFIG_ASS
275 if (sh->ass_track)
276 ass_free_track(sh->ass_track);
277 #endif
278 free(sh->lang);
279 free(sh);
282 sh_audio_t *new_sh_audio_aid(demuxer_t *demuxer, int id, int aid)
284 if (id > MAX_A_STREAMS - 1 || id < 0) {
285 mp_msg(MSGT_DEMUXER, MSGL_WARN,
286 "Requested audio stream id overflow (%d > %d)\n", id,
287 MAX_A_STREAMS);
288 return NULL;
290 if (demuxer->a_streams[id]) {
291 mp_tmsg(MSGT_DEMUXER, MSGL_WARN, "WARNING: Audio stream header %d redefined.\n", id);
292 } else {
293 mp_tmsg(MSGT_DEMUXER, MSGL_V, "==> Found audio stream: %d\n", id);
294 sh_audio_t *sh = calloc(1, sizeof(sh_audio_t));
295 demuxer->a_streams[id] = sh;
296 sh->aid = aid;
297 sh->ds = demuxer->audio;
298 // set some defaults
299 sh->samplesize = 2;
300 sh->sample_format = AF_FORMAT_S16_NE;
301 sh->audio_out_minsize = 8192; /* default size, maybe not enough for Win32/ACM */
302 sh->pts = MP_NOPTS_VALUE;
303 sh->opts = demuxer->opts;
304 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_ID=%d\n", aid);
306 return demuxer->a_streams[id];
309 void free_sh_audio(demuxer_t *demuxer, int id)
311 sh_audio_t *sh = demuxer->a_streams[id];
312 demuxer->a_streams[id] = NULL;
313 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_audio at %p\n", sh);
314 free(sh->wf);
315 free(sh->codecdata);
316 free(sh->lang);
317 free(sh);
320 sh_video_t *new_sh_video_vid(demuxer_t *demuxer, int id, int vid)
322 if (id > MAX_V_STREAMS - 1 || id < 0) {
323 mp_msg(MSGT_DEMUXER, MSGL_WARN,
324 "Requested video stream id overflow (%d > %d)\n", id,
325 MAX_V_STREAMS);
326 return NULL;
328 if (demuxer->v_streams[id])
329 mp_tmsg(MSGT_DEMUXER, MSGL_WARN, "WARNING: Video stream header %d redefined.\n", id);
330 else {
331 mp_tmsg(MSGT_DEMUXER, MSGL_V, "==> Found video stream: %d\n", id);
332 sh_video_t *sh = calloc(1, sizeof *sh);
333 demuxer->v_streams[id] = sh;
334 sh->vid = vid;
335 sh->ds = demuxer->video;
336 sh->opts = demuxer->opts;
337 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_ID=%d\n", vid);
339 return demuxer->v_streams[id];
342 void free_sh_video(sh_video_t *sh)
344 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_video at %p\n", sh);
345 free(sh->bih);
346 free(sh);
349 void free_demuxer(demuxer_t *demuxer)
351 int i;
352 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing demuxer at %p\n",
353 demuxer);
354 if (demuxer->desc->close)
355 demuxer->desc->close(demuxer);
356 // Very ugly hack to make it behave like old implementation
357 if (demuxer->desc->type == DEMUXER_TYPE_DEMUXERS)
358 goto skip_streamfree;
359 // free streams:
360 for (i = 0; i < MAX_A_STREAMS; i++)
361 if (demuxer->a_streams[i])
362 free_sh_audio(demuxer, i);
363 for (i = 0; i < MAX_V_STREAMS; i++)
364 if (demuxer->v_streams[i])
365 free_sh_video(demuxer->v_streams[i]);
366 for (i = 0; i < MAX_S_STREAMS; i++)
367 if (demuxer->s_streams[i])
368 free_sh_sub(demuxer->s_streams[i]);
369 // free demuxers:
370 free_demuxer_stream(demuxer->audio);
371 free_demuxer_stream(demuxer->video);
372 free_demuxer_stream(demuxer->sub);
373 skip_streamfree:
374 if (demuxer->info) {
375 for (i = 0; demuxer->info[i] != NULL; i++)
376 free(demuxer->info[i]);
377 free(demuxer->info);
379 free(demuxer->filename);
380 if (demuxer->chapters) {
381 for (i = 0; i < demuxer->num_chapters; i++)
382 free(demuxer->chapters[i].name);
383 free(demuxer->chapters);
385 if (demuxer->attachments) {
386 for (i = 0; i < demuxer->num_attachments; i++) {
387 free(demuxer->attachments[i].name);
388 free(demuxer->attachments[i].type);
389 free(demuxer->attachments[i].data);
391 free(demuxer->attachments);
393 if (demuxer->teletext)
394 teletext_control(demuxer->teletext, TV_VBI_CONTROL_STOP, NULL);
395 talloc_free(demuxer);
399 void ds_add_packet(demux_stream_t *ds, demux_packet_t *dp)
401 // append packet to DS stream:
402 ++ds->packs;
403 ds->bytes += dp->len;
404 if (ds->last) {
405 // next packet in stream
406 ds->last->next = dp;
407 ds->last = dp;
408 } else {
409 // first packet in stream
410 ds->first = ds->last = dp;
412 mp_dbg(MSGT_DEMUXER, MSGL_DBG2,
413 "DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
414 (ds == ds->demuxer->audio) ? "d_audio" : "d_video", dp->len,
415 dp->pts, (unsigned int) dp->pos, ds->demuxer->audio->packs,
416 ds->demuxer->video->packs);
419 void ds_read_packet(demux_stream_t *ds, stream_t *stream, int len,
420 double pts, off_t pos, int flags)
422 demux_packet_t *dp = new_demux_packet(len);
423 len = stream_read(stream, dp->buffer, len);
424 resize_demux_packet(dp, len);
425 dp->pts = pts;
426 dp->pos = pos;
427 dp->flags = flags;
428 // append packet to DS stream:
429 ds_add_packet(ds, dp);
432 // return value:
433 // 0 = EOF or no stream found or invalid type
434 // 1 = successfully read a packet
436 int demux_fill_buffer(demuxer_t *demux, demux_stream_t *ds)
438 // Note: parameter 'ds' can be NULL!
439 return demux->desc->fill_buffer(demux, ds);
442 // return value:
443 // 0 = EOF
444 // 1 = successful
445 #define MAX_ACUMULATED_PACKETS 64
446 int ds_fill_buffer(demux_stream_t *ds)
448 demuxer_t *demux = ds->demuxer;
449 if (ds->current)
450 free_demux_packet(ds->current);
451 ds->current = NULL;
452 if (mp_msg_test(MSGT_DEMUXER, MSGL_DBG3)) {
453 if (ds == demux->audio)
454 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
455 "ds_fill_buffer(d_audio) called\n");
456 else if (ds == demux->video)
457 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
458 "ds_fill_buffer(d_video) called\n");
459 else if (ds == demux->sub)
460 mp_dbg(MSGT_DEMUXER, MSGL_DBG3, "ds_fill_buffer(d_sub) called\n");
461 else
462 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
463 "ds_fill_buffer(unknown 0x%X) called\n", (unsigned int) ds);
465 while (1) {
466 if (ds->packs) {
467 demux_packet_t *p = ds->first;
468 #if 0
469 if (demux->reference_clock != MP_NOPTS_VALUE) {
470 if ( p->pts != MP_NOPTS_VALUE
471 && p->pts > demux->reference_clock
472 && ds->packs < MAX_ACUMULATED_PACKETS) {
473 if (demux_fill_buffer(demux, ds))
474 continue;
477 #endif
478 // copy useful data:
479 ds->buffer = p->buffer;
480 ds->buffer_pos = 0;
481 ds->buffer_size = p->len;
482 ds->pos = p->pos;
483 ds->dpos += p->len; // !!!
484 ++ds->pack_no;
485 if (p->pts != MP_NOPTS_VALUE) {
486 ds->pts = p->pts;
487 ds->pts_bytes = 0;
489 ds->pts_bytes += p->len; // !!!
490 if (p->stream_pts != MP_NOPTS_VALUE)
491 demux->stream_pts = p->stream_pts;
492 ds->flags = p->flags;
493 // unlink packet:
494 ds->bytes -= p->len;
495 ds->current = p;
496 ds->first = p->next;
497 if (!ds->first)
498 ds->last = NULL;
499 --ds->packs;
500 /* The code below can set ds->eof to 1 when another stream runs
501 * out of buffer space. That makes sense because in that situation
502 * the calling code should not count on being able to demux more
503 * packets from this stream.
504 * If however the situation improves and we're called again
505 * despite the eof flag then it's better to clear it to avoid
506 * weird behavior. */
507 ds->eof = 0;
508 return 1;
511 #define MaybeNI _("Maybe you are playing a non-interleaved stream/file or the codec failed?\n" \
512 "For AVI files, try to force non-interleaved mode with the -ni option.\n")
514 if (demux->audio->packs >= MAX_PACKS
515 || demux->audio->bytes >= MAX_PACK_BYTES) {
516 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
517 demux->audio->packs, demux->audio->bytes);
518 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
519 break;
521 if (demux->video->packs >= MAX_PACKS
522 || demux->video->bytes >= MAX_PACK_BYTES) {
523 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
524 demux->video->packs, demux->video->bytes);
525 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
526 break;
528 if (!demux_fill_buffer(demux, ds)) {
529 mp_dbg(MSGT_DEMUXER, MSGL_DBG2,
530 "ds_fill_buffer()->demux_fill_buffer() failed\n");
531 break; // EOF
534 ds->buffer_pos = ds->buffer_size = 0;
535 ds->buffer = NULL;
536 mp_msg(MSGT_DEMUXER, MSGL_V,
537 "ds_fill_buffer: EOF reached (stream: %s) \n",
538 ds == demux->audio ? "audio" : "video");
539 ds->eof = 1;
540 return 0;
543 int demux_read_data(demux_stream_t *ds, unsigned char *mem, int len)
545 int x;
546 int bytes = 0;
547 while (len > 0) {
548 x = ds->buffer_size - ds->buffer_pos;
549 if (x == 0) {
550 if (!ds_fill_buffer(ds))
551 return bytes;
552 } else {
553 if (x > len)
554 x = len;
555 if (mem)
556 fast_memcpy(mem + bytes, &ds->buffer[ds->buffer_pos], x);
557 bytes += x;
558 len -= x;
559 ds->buffer_pos += x;
562 return bytes;
566 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
567 * \param mem memory to read data into, may be NULL to discard data
568 * \param maxlen maximum number of bytes to read
569 * \param read number of bytes actually read
570 * \param pattern pattern to search for (lowest 8 bits are ignored)
571 * \return whether pattern was found
573 int demux_pattern_3(demux_stream_t *ds, unsigned char *mem, int maxlen,
574 int *read, uint32_t pattern)
576 register uint32_t head = 0xffffff00;
577 register uint32_t pat = pattern & 0xffffff00;
578 int total_len = 0;
579 do {
580 register unsigned char *ds_buf = &ds->buffer[ds->buffer_size];
581 int len = ds->buffer_size - ds->buffer_pos;
582 register long pos = -len;
583 if (unlikely(pos >= 0)) { // buffer is empty
584 ds_fill_buffer(ds);
585 continue;
587 do {
588 head |= ds_buf[pos];
589 head <<= 8;
590 } while (++pos && head != pat);
591 len += pos;
592 if (total_len + len > maxlen)
593 len = maxlen - total_len;
594 len = demux_read_data(ds, mem ? &mem[total_len] : NULL, len);
595 total_len += len;
596 } while ((head != pat || total_len < 3) && total_len < maxlen && !ds->eof);
597 if (read)
598 *read = total_len;
599 return total_len >= 3 && head == pat;
602 void ds_free_packs(demux_stream_t *ds)
604 demux_packet_t *dp = ds->first;
605 while (dp) {
606 demux_packet_t *dn = dp->next;
607 free_demux_packet(dp);
608 dp = dn;
610 if (ds->asf_packet) {
611 // free unfinished .asf fragments:
612 free(ds->asf_packet->buffer);
613 free(ds->asf_packet);
614 ds->asf_packet = NULL;
616 ds->first = ds->last = NULL;
617 ds->packs = 0; // !!!!!
618 ds->bytes = 0;
619 if (ds->current)
620 free_demux_packet(ds->current);
621 ds->current = NULL;
622 ds->buffer = NULL;
623 ds->buffer_pos = ds->buffer_size;
624 ds->pts = 0;
625 ds->pts_bytes = 0;
628 int ds_get_packet(demux_stream_t *ds, unsigned char **start)
630 int len;
631 if (ds->buffer_pos >= ds->buffer_size) {
632 if (!ds_fill_buffer(ds)) {
633 // EOF
634 *start = NULL;
635 return -1;
638 len = ds->buffer_size - ds->buffer_pos;
639 *start = &ds->buffer[ds->buffer_pos];
640 ds->buffer_pos += len;
641 return len;
644 int ds_get_packet_pts(demux_stream_t *ds, unsigned char **start, double *pts)
646 int len;
647 *pts = MP_NOPTS_VALUE;
648 len = ds_get_packet(ds, start);
649 if (len < 0)
650 return len;
651 // Return pts unless this read starts from the middle of a packet
652 if (len == ds->buffer_pos)
653 *pts = ds->current->pts;
654 return len;
657 int ds_get_packet_sub(demux_stream_t *ds, unsigned char **start)
659 int len;
660 if (ds->buffer_pos >= ds->buffer_size) {
661 *start = NULL;
662 if (!ds->packs)
663 return -1; // no sub
664 if (!ds_fill_buffer(ds))
665 return -1; // EOF
667 len = ds->buffer_size - ds->buffer_pos;
668 *start = &ds->buffer[ds->buffer_pos];
669 ds->buffer_pos += len;
670 return len;
673 double ds_get_next_pts(demux_stream_t *ds)
675 demuxer_t *demux = ds->demuxer;
676 while (!ds->first) {
677 if (demux->audio->packs >= MAX_PACKS
678 || demux->audio->bytes >= MAX_PACK_BYTES) {
679 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
680 demux->audio->packs, demux->audio->bytes);
681 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
682 return MP_NOPTS_VALUE;
684 if (demux->video->packs >= MAX_PACKS
685 || demux->video->bytes >= MAX_PACK_BYTES) {
686 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
687 demux->video->packs, demux->video->bytes);
688 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
689 return MP_NOPTS_VALUE;
691 if (!demux_fill_buffer(demux, ds))
692 return MP_NOPTS_VALUE;
694 return ds->first->pts;
697 // ====================================================================
699 void demuxer_help(void)
701 int i;
703 mp_msg(MSGT_DEMUXER, MSGL_INFO, "Available demuxers:\n");
704 mp_msg(MSGT_DEMUXER, MSGL_INFO, " demuxer: type info: (comment)\n");
705 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DEMUXERS\n");
706 for (i = 0; demuxer_list[i]; i++) {
707 if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Don't display special demuxers
708 continue;
709 if (demuxer_list[i]->comment && strlen(demuxer_list[i]->comment))
710 mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s (%s)\n",
711 demuxer_list[i]->name, demuxer_list[i]->type,
712 demuxer_list[i]->info, demuxer_list[i]->comment);
713 else
714 mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s\n",
715 demuxer_list[i]->name, demuxer_list[i]->type,
716 demuxer_list[i]->info);
722 * Get demuxer type for a given demuxer name
724 * @param demuxer_name string with demuxer name of demuxer number
725 * @param force will be set if demuxer should be forced.
726 * May be NULL.
727 * @return DEMUXER_TYPE_xxx, -1 if error or not found
729 int get_demuxer_type_from_name(char *demuxer_name, int *force)
731 int i;
732 long type_int;
733 char *endptr;
735 if (!demuxer_name || !demuxer_name[0])
736 return DEMUXER_TYPE_UNKNOWN;
737 if (force)
738 *force = demuxer_name[0] == '+';
739 if (demuxer_name[0] == '+')
740 demuxer_name = &demuxer_name[1];
741 for (i = 0; demuxer_list[i]; i++) {
742 if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Can't select special demuxers from commandline
743 continue;
744 if (strcmp(demuxer_name, demuxer_list[i]->name) == 0)
745 return demuxer_list[i]->type;
748 // No match found, try to parse name as an integer (demuxer number)
749 type_int = strtol(demuxer_name, &endptr, 0);
750 if (*endptr) // Conversion failed
751 return -1;
752 if ((type_int > 0) && (type_int <= DEMUXER_TYPE_MAX))
753 return (int) type_int;
755 return -1;
758 int extension_parsing = 1; // 0=off 1=mixed (used only for unstable formats)
761 NOTE : Several demuxers may be opened at the same time so
762 demuxers should NEVER rely on an external var to enable them
763 self. If a demuxer can't do any auto-detection it should only use
764 file_format. The user can explicitly set file_format with the -demuxer
765 option so there is really no need for another extra var.
766 For convenience an option can be added to set file_format directly
767 to the right type (ex: rawaudio,rawvideo).
768 Also the stream can override the file_format so a demuxer which rely
769 on a special stream type can set file_format at the stream level
770 (ex: tv,mf).
773 static demuxer_t *demux_open_stream(struct MPOpts *opts, stream_t *stream,
774 int file_format, int force, int audio_id,
775 int video_id, int dvdsub_id,
776 char *filename)
778 demuxer_t *demuxer = NULL;
780 sh_video_t *sh_video = NULL;
782 const demuxer_desc_t *demuxer_desc;
783 int fformat = 0;
784 int i;
786 // If somebody requested a demuxer check it
787 if (file_format) {
788 if ((demuxer_desc = get_demuxer_desc_from_type(file_format))) {
789 demuxer = new_demuxer(opts, stream, demuxer_desc->type, audio_id,
790 video_id, dvdsub_id, filename);
791 if (demuxer_desc->check_file)
792 fformat = demuxer_desc->check_file(demuxer);
793 if (force || !demuxer_desc->check_file)
794 fformat = demuxer_desc->type;
795 if (fformat != 0) {
796 if (fformat == demuxer_desc->type) {
797 demuxer_t *demux2 = demuxer;
798 // Move messages to demuxer detection code?
799 mp_tmsg(MSGT_DEMUXER, MSGL_INFO,
800 "%s file format detected.\n",
801 demuxer_desc->shortdesc);
802 file_format = fformat;
803 if (!demuxer->desc->open
804 || (demux2 = demuxer->desc->open(demuxer))) {
805 demuxer = demux2;
806 goto dmx_open;
808 } else {
809 // Format changed after check, recurse
810 free_demuxer(demuxer);
811 return demux_open_stream(opts, stream, fformat, force,
812 audio_id, video_id, dvdsub_id,
813 filename);
816 // Check failed for forced demuxer, quit
817 free_demuxer(demuxer);
818 return NULL;
821 // Test demuxers with safe file checks
822 for (i = 0; (demuxer_desc = demuxer_list[i]); i++) {
823 if (demuxer_desc->safe_check) {
824 demuxer = new_demuxer(opts, stream, demuxer_desc->type, audio_id,
825 video_id, dvdsub_id, filename);
826 if ((fformat = demuxer_desc->check_file(demuxer)) != 0) {
827 if (fformat == demuxer_desc->type) {
828 demuxer_t *demux2 = demuxer;
829 mp_tmsg(MSGT_DEMUXER, MSGL_INFO,
830 "%s file format detected.\n",
831 demuxer_desc->shortdesc);
832 file_format = fformat;
833 if (!demuxer->desc->open
834 || (demux2 = demuxer->desc->open(demuxer))) {
835 demuxer = demux2;
836 goto dmx_open;
838 } else {
839 if (fformat == DEMUXER_TYPE_PLAYLIST)
840 return demuxer; // handled in mplayer.c
841 // Format changed after check, recurse
842 free_demuxer(demuxer);
843 demuxer = demux_open_stream(opts, stream, fformat, force,
844 audio_id, video_id,
845 dvdsub_id, filename);
846 if (demuxer)
847 return demuxer; // done!
848 file_format = DEMUXER_TYPE_UNKNOWN;
851 free_demuxer(demuxer);
852 demuxer = NULL;
856 // If no forced demuxer perform file extension based detection
857 // Ok. We're over the stable detectable fileformats, the next ones are
858 // a bit fuzzy. So by default (extension_parsing==1) try extension-based
859 // detection first:
860 if (file_format == DEMUXER_TYPE_UNKNOWN && filename
861 && extension_parsing == 1) {
862 file_format = demuxer_type_by_filename(filename);
863 if (file_format != DEMUXER_TYPE_UNKNOWN) {
864 // we like recursion :)
865 demuxer = demux_open_stream(opts, stream, file_format, force,
866 audio_id, video_id, dvdsub_id,
867 filename);
868 if (demuxer)
869 return demuxer; // done!
870 file_format = DEMUXER_TYPE_UNKNOWN; // continue fuzzy guessing...
871 mp_msg(MSGT_DEMUXER, MSGL_V,
872 "demuxer: continue fuzzy content-based format guessing...\n");
875 // Try detection for all other demuxers
876 for (i = 0; (demuxer_desc = demuxer_list[i]); i++) {
877 if (!demuxer_desc->safe_check && demuxer_desc->check_file) {
878 demuxer = new_demuxer(opts, stream, demuxer_desc->type, audio_id,
879 video_id, dvdsub_id, filename);
880 if ((fformat = demuxer_desc->check_file(demuxer)) != 0) {
881 if (fformat == demuxer_desc->type) {
882 demuxer_t *demux2 = demuxer;
883 mp_tmsg(MSGT_DEMUXER, MSGL_INFO,
884 "%s file format detected.\n",
885 demuxer_desc->shortdesc);
886 file_format = fformat;
887 if (!demuxer->desc->open
888 || (demux2 = demuxer->desc->open(demuxer))) {
889 demuxer = demux2;
890 goto dmx_open;
892 } else {
893 if (fformat == DEMUXER_TYPE_PLAYLIST)
894 return demuxer; // handled in mplayer.c
895 // Format changed after check, recurse
896 free_demuxer(demuxer);
897 demuxer = demux_open_stream(opts, stream, fformat, force,
898 audio_id, video_id,
899 dvdsub_id, filename);
900 if (demuxer)
901 return demuxer; // done!
902 file_format = DEMUXER_TYPE_UNKNOWN;
905 free_demuxer(demuxer);
906 demuxer = NULL;
910 return NULL;
911 //====== File format recognized, set up these for compatibility: =========
912 dmx_open:
914 demuxer->file_format = file_format;
916 if ((sh_video = demuxer->video->sh) && sh_video->bih) {
917 int biComp = le2me_32(sh_video->bih->biCompression);
918 mp_msg(MSGT_DEMUX, MSGL_INFO,
919 "VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
920 (char *) &biComp, sh_video->bih->biWidth,
921 sh_video->bih->biHeight, sh_video->bih->biBitCount,
922 sh_video->fps, sh_video->i_bps * 0.008f,
923 sh_video->i_bps / 1024.0f);
925 #ifdef CONFIG_ASS
926 if (opts->ass_enabled && ass_library) {
927 for (i = 0; i < MAX_S_STREAMS; ++i) {
928 sh_sub_t *sh = demuxer->s_streams[i];
929 if (sh && sh->type == 'a') {
930 sh->ass_track = ass_new_track(ass_library);
931 if (sh->ass_track && sh->extradata)
932 ass_process_codec_private(sh->ass_track, sh->extradata,
933 sh->extradata_len);
934 } else if (sh && sh->type != 'v')
935 sh->ass_track = ass_default_track(ass_library);
938 #endif
939 return demuxer;
942 char *audio_stream = NULL;
943 char *sub_stream = NULL;
944 int audio_stream_cache = 0;
946 char *demuxer_name = NULL; // parameter from -demuxer
947 char *audio_demuxer_name = NULL; // parameter from -audio-demuxer
948 char *sub_demuxer_name = NULL; // parameter from -sub-demuxer
950 extern int hr_mp3_seek;
952 extern float stream_cache_min_percent;
953 extern float stream_cache_seek_min_percent;
955 demuxer_t *demux_open(struct MPOpts *opts, stream_t *vs, int file_format,
956 int audio_id, int video_id, int dvdsub_id,
957 char *filename)
959 stream_t *as = NULL, *ss = NULL;
960 demuxer_t *vd, *ad = NULL, *sd = NULL;
961 demuxer_t *res;
962 int afmt = DEMUXER_TYPE_UNKNOWN, sfmt = DEMUXER_TYPE_UNKNOWN;
963 int demuxer_type;
964 int audio_demuxer_type = 0, sub_demuxer_type = 0;
965 int demuxer_force = 0, audio_demuxer_force = 0, sub_demuxer_force = 0;
967 if ((demuxer_type =
968 get_demuxer_type_from_name(demuxer_name, &demuxer_force)) < 0) {
969 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-demuxer %s does not exist.\n",
970 demuxer_name);
972 if ((audio_demuxer_type =
973 get_demuxer_type_from_name(audio_demuxer_name,
974 &audio_demuxer_force)) < 0) {
975 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-audio-demuxer %s does not exist.\n",
976 audio_demuxer_name);
978 if ((sub_demuxer_type =
979 get_demuxer_type_from_name(sub_demuxer_name,
980 &sub_demuxer_force)) < 0) {
981 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-sub-demuxer %s does not exist.\n",
982 sub_demuxer_name);
985 if (audio_stream) {
986 as = open_stream(audio_stream, 0, &afmt);
987 if (!as) {
988 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "Cannot open audio stream: %s\n",
989 audio_stream);
990 return NULL;
992 if (audio_stream_cache) {
993 if (!stream_enable_cache
994 (as, audio_stream_cache * 1024,
995 audio_stream_cache * 1024 * (stream_cache_min_percent /
996 100.0),
997 audio_stream_cache * 1024 * (stream_cache_seek_min_percent /
998 100.0))) {
999 free_stream(as);
1000 mp_msg(MSGT_DEMUXER, MSGL_ERR,
1001 "Can't enable audio stream cache\n");
1002 return NULL;
1006 if (sub_stream) {
1007 ss = open_stream(sub_stream, 0, &sfmt);
1008 if (!ss) {
1009 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "Cannot open subtitle stream: %s\n",
1010 sub_stream);
1011 return NULL;
1015 vd = demux_open_stream(opts, vs, demuxer_type ? demuxer_type : file_format,
1016 demuxer_force, audio_stream ? -2 : audio_id,
1017 video_id, sub_stream ? -2 : dvdsub_id, filename);
1018 if (!vd) {
1019 if (as)
1020 free_stream(as);
1021 if (ss)
1022 free_stream(ss);
1023 return NULL;
1025 if (as) {
1026 ad = demux_open_stream(opts, as,
1027 audio_demuxer_type ? audio_demuxer_type : afmt,
1028 audio_demuxer_force, audio_id, -2, -2,
1029 audio_stream);
1030 if (!ad) {
1031 mp_tmsg(MSGT_DEMUXER, MSGL_WARN, "Failed to open audio demuxer: %s\n",
1032 audio_stream);
1033 free_stream(as);
1034 } else if (ad->audio->sh
1035 && ((sh_audio_t *) ad->audio->sh)->format == 0x55) // MP3
1036 hr_mp3_seek = 1; // Enable high res seeking
1038 if (ss) {
1039 sd = demux_open_stream(opts, ss,
1040 sub_demuxer_type ? sub_demuxer_type : sfmt,
1041 sub_demuxer_force, -2, -2, dvdsub_id,
1042 sub_stream);
1043 if (!sd) {
1044 mp_tmsg(MSGT_DEMUXER, MSGL_WARN,
1045 "Failed to open subtitle demuxer: %s\n", sub_stream);
1046 free_stream(ss);
1050 if (ad && sd)
1051 res = new_demuxers_demuxer(vd, ad, sd);
1052 else if (ad)
1053 res = new_demuxers_demuxer(vd, ad, vd);
1054 else if (sd)
1055 res = new_demuxers_demuxer(vd, vd, sd);
1056 else
1057 res = vd;
1059 opts->correct_pts = opts->user_correct_pts;
1060 if (opts->correct_pts < 0)
1061 opts->correct_pts =
1062 demux_control(res, DEMUXER_CTRL_CORRECT_PTS,
1063 NULL) == DEMUXER_CTRL_OK;
1064 return res;
1068 void demux_flush(demuxer_t *demuxer)
1070 ds_free_packs(demuxer->video);
1071 ds_free_packs(demuxer->audio);
1072 ds_free_packs(demuxer->sub);
1075 int demux_seek(demuxer_t *demuxer, float rel_seek_secs, float audio_delay,
1076 int flags)
1078 demux_stream_t *d_audio = demuxer->audio;
1079 demux_stream_t *d_video = demuxer->video;
1080 sh_audio_t *sh_audio = d_audio->sh;
1081 sh_video_t *sh_video = d_video->sh;
1082 double tmp = 0;
1083 double pts;
1085 if (!demuxer->seekable) {
1086 if (demuxer->file_format == DEMUXER_TYPE_AVI)
1087 mp_tmsg(MSGT_SEEK, MSGL_WARN, "Cannot seek in raw AVI streams. (Index required, try with the -idx switch.)\n");
1088 #ifdef CONFIG_TV
1089 else if (demuxer->file_format == DEMUXER_TYPE_TV)
1090 mp_tmsg(MSGT_SEEK, MSGL_WARN, "TV input is not seekable! (Seeking will probably be for changing channels ;)\n");
1091 #endif
1092 else
1093 mp_tmsg(MSGT_SEEK, MSGL_WARN, "Cannot seek in this file.\n");
1094 return 0;
1096 // clear demux buffers:
1097 demux_flush(demuxer);
1098 if (sh_audio)
1099 sh_audio->a_buffer_len = 0;
1101 demuxer->stream->eof = 0;
1102 demuxer->video->eof = 0;
1103 demuxer->audio->eof = 0;
1105 if (sh_video)
1106 sh_video->timer = 0; // !!!!!!
1108 if (flags & SEEK_ABSOLUTE)
1109 pts = 0.0f;
1110 else {
1111 if (demuxer->stream_pts == MP_NOPTS_VALUE)
1112 goto dmx_seek;
1113 pts = demuxer->stream_pts;
1116 if (flags & SEEK_FACTOR) {
1117 if (stream_control(demuxer->stream, STREAM_CTRL_GET_TIME_LENGTH, &tmp)
1118 == STREAM_UNSUPPORTED)
1119 goto dmx_seek;
1120 pts += tmp * rel_seek_secs;
1121 } else
1122 pts += rel_seek_secs;
1124 if (stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_TIME, &pts) !=
1125 STREAM_UNSUPPORTED) {
1126 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1127 return 1;
1130 dmx_seek:
1131 if (demuxer->desc->seek)
1132 demuxer->desc->seek(demuxer, rel_seek_secs, audio_delay, flags);
1134 if (sh_audio)
1135 resync_audio_stream(sh_audio);
1137 return 1;
1140 int demux_info_add(demuxer_t *demuxer, const char *opt, const char *param)
1142 char **info = demuxer->info;
1143 int n = 0;
1146 for (n = 0; info && info[2 * n] != NULL; n++) {
1147 if (!strcasecmp(opt, info[2 * n])) {
1148 if (!strcmp(param, info[2 * n + 1])) {
1149 mp_msg(MSGT_DEMUX, MSGL_V, "Demuxer info %s set to unchanged value %s\n", opt, param);
1150 return 0;
1152 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "Demuxer info %s changed to %s\n", opt,
1153 param);
1154 free(info[2 * n + 1]);
1155 info[2 * n + 1] = strdup(param);
1156 return 0;
1160 info = demuxer->info =
1161 (char **) realloc(info, (2 * (n + 2)) * sizeof(char *));
1162 info[2 * n] = strdup(opt);
1163 info[2 * n + 1] = strdup(param);
1164 memset(&info[2 * (n + 1)], 0, 2 * sizeof(char *));
1166 return 1;
1169 int demux_info_print(demuxer_t *demuxer)
1171 char **info = demuxer->info;
1172 int n;
1174 if (!info)
1175 return 0;
1177 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "Clip info:\n");
1178 for (n = 0; info[2 * n] != NULL; n++) {
1179 mp_msg(MSGT_DEMUX, MSGL_INFO, " %s: %s\n", info[2 * n],
1180 info[2 * n + 1]);
1181 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_NAME%d=%s\n", n,
1182 info[2 * n]);
1183 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_VALUE%d=%s\n", n,
1184 info[2 * n + 1]);
1186 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_N=%d\n", n);
1188 return 0;
1191 char *demux_info_get(demuxer_t *demuxer, const char *opt)
1193 int i;
1194 char **info = demuxer->info;
1196 for (i = 0; info && info[2 * i] != NULL; i++) {
1197 if (!strcasecmp(opt, info[2 * i]))
1198 return info[2 * i + 1];
1201 return NULL;
1204 int demux_control(demuxer_t *demuxer, int cmd, void *arg)
1207 if (demuxer->desc->control)
1208 return demuxer->desc->control(demuxer, cmd, arg);
1210 return DEMUXER_CTRL_NOTIMPL;
1215 double demuxer_get_time_length(demuxer_t *demuxer)
1217 double get_time_ans;
1218 sh_video_t *sh_video = demuxer->video->sh;
1219 sh_audio_t *sh_audio = demuxer->audio->sh;
1220 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
1221 if (demux_control
1222 (demuxer, DEMUXER_CTRL_GET_TIME_LENGTH, (void *) &get_time_ans) <= 0) {
1223 if (sh_video && sh_video->i_bps && sh_audio && sh_audio->i_bps)
1224 get_time_ans = (double) (demuxer->movi_end -
1225 demuxer->movi_start) / (sh_video->i_bps +
1226 sh_audio->i_bps);
1227 else if (sh_video && sh_video->i_bps)
1228 get_time_ans = (double) (demuxer->movi_end -
1229 demuxer->movi_start) / sh_video->i_bps;
1230 else if (sh_audio && sh_audio->i_bps)
1231 get_time_ans = (double) (demuxer->movi_end -
1232 demuxer->movi_start) / sh_audio->i_bps;
1233 else
1234 get_time_ans = 0;
1236 return get_time_ans;
1240 * \brief demuxer_get_current_time() returns the time of the current play in three possible ways:
1241 * either when the stream reader satisfies STREAM_CTRL_GET_CURRENT_TIME (e.g. dvd)
1242 * or using sh_video->pts when the former method fails
1243 * 0 otherwise
1244 * \return the current play time
1246 int demuxer_get_current_time(demuxer_t *demuxer)
1248 double get_time_ans = 0;
1249 sh_video_t *sh_video = demuxer->video->sh;
1250 if (demuxer->stream_pts != MP_NOPTS_VALUE)
1251 get_time_ans = demuxer->stream_pts;
1252 else if (sh_video)
1253 get_time_ans = sh_video->pts;
1254 return (int) get_time_ans;
1257 int demuxer_get_percent_pos(demuxer_t *demuxer)
1259 int ans = 0;
1260 int res = demux_control(demuxer, DEMUXER_CTRL_GET_PERCENT_POS, &ans);
1261 int len = (demuxer->movi_end - demuxer->movi_start) / 100;
1262 if (res <= 0) {
1263 off_t pos = demuxer->filepos > 0 ? demuxer->filepos : stream_tell(demuxer->stream);
1264 if (len > 0)
1265 ans = (pos - demuxer->movi_start) / len;
1266 else
1267 ans = 0;
1269 if (ans < 0)
1270 ans = 0;
1271 if (ans > 100)
1272 ans = 100;
1273 return ans;
1276 int demuxer_switch_audio(demuxer_t *demuxer, int index)
1278 int res = demux_control(demuxer, DEMUXER_CTRL_SWITCH_AUDIO, &index);
1279 if (res == DEMUXER_CTRL_NOTIMPL)
1280 index = demuxer->audio->id;
1281 if (demuxer->audio->id >= 0)
1282 demuxer->audio->sh = demuxer->a_streams[demuxer->audio->id];
1283 else
1284 demuxer->audio->sh = NULL;
1285 return index;
1288 int demuxer_switch_video(demuxer_t *demuxer, int index)
1290 int res = demux_control(demuxer, DEMUXER_CTRL_SWITCH_VIDEO, &index);
1291 if (res == DEMUXER_CTRL_NOTIMPL)
1292 index = demuxer->video->id;
1293 if (demuxer->video->id >= 0)
1294 demuxer->video->sh = demuxer->v_streams[demuxer->video->id];
1295 else
1296 demuxer->video->sh = NULL;
1297 return index;
1300 int demuxer_add_attachment(demuxer_t *demuxer, const char *name,
1301 const char *type, const void *data, size_t size)
1303 if (!(demuxer->num_attachments & 31))
1304 demuxer->attachments = realloc(demuxer->attachments,
1305 (demuxer->num_attachments + 32) * sizeof(demux_attachment_t));
1307 demuxer->attachments[demuxer->num_attachments].name = strdup(name);
1308 demuxer->attachments[demuxer->num_attachments].type = strdup(type);
1309 demuxer->attachments[demuxer->num_attachments].data = malloc(size);
1310 memcpy(demuxer->attachments[demuxer->num_attachments].data, data, size);
1311 demuxer->attachments[demuxer->num_attachments].data_size = size;
1313 return demuxer->num_attachments++;
1316 int demuxer_add_chapter(demuxer_t *demuxer, const char *name, uint64_t start,
1317 uint64_t end)
1319 if (demuxer->chapters == NULL)
1320 demuxer->chapters = malloc(32 * sizeof(*demuxer->chapters));
1321 else if (!(demuxer->num_chapters % 32))
1322 demuxer->chapters = realloc(demuxer->chapters,
1323 (demuxer->num_chapters + 32) *
1324 sizeof(*demuxer->chapters));
1326 demuxer->chapters[demuxer->num_chapters].start = start;
1327 demuxer->chapters[demuxer->num_chapters].end = end;
1328 demuxer->chapters[demuxer->num_chapters].name = strdup(name ? name : mp_gtext("unknown"));
1330 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_ID=%d\n", demuxer->num_chapters);
1331 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_START=%"PRIu64"\n", demuxer->num_chapters, start);
1332 if (end)
1333 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_END=%"PRIu64"\n", demuxer->num_chapters, end);
1334 if (name)
1335 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_NAME=%s\n", demuxer->num_chapters, name);
1337 return demuxer->num_chapters++;
1341 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1342 * either using the demuxer->chapters structure set by the demuxer
1343 * or asking help to the stream layer (e.g. dvd)
1344 * \param chapter - chapter number wished - 0-based
1345 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1346 * \param chapter_name name of chapter found (set by this function is param is not null)
1347 * \return -1 on error, current chapter if successful
1350 int demuxer_seek_chapter(demuxer_t *demuxer, int chapter, double *seek_pts,
1351 char **chapter_name)
1353 int ris;
1354 sh_video_t *sh_video = demuxer->video->sh;
1355 sh_audio_t *sh_audio = demuxer->audio->sh;
1357 if (!demuxer->num_chapters || !demuxer->chapters) {
1358 demux_flush(demuxer);
1360 ris = stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_CHAPTER,
1361 &chapter);
1362 if (ris != STREAM_UNSUPPORTED)
1363 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1364 if (sh_video) {
1365 ds_fill_buffer(demuxer->video);
1366 resync_video_stream(sh_video);
1369 if (sh_audio) {
1370 ds_fill_buffer(demuxer->audio);
1371 resync_audio_stream(sh_audio);
1373 // exit status may be ok, but main() doesn't have to seek itself
1374 // (because e.g. dvds depend on sectors, not on pts)
1375 *seek_pts = -1.0;
1377 if (chapter_name) {
1378 *chapter_name = NULL;
1379 int num_chapters;
1380 if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS,
1381 &num_chapters) == STREAM_UNSUPPORTED)
1382 num_chapters = 0;
1383 if (num_chapters) {
1384 *chapter_name = talloc_size(NULL, 16);
1385 sprintf(*chapter_name, " of %3d", num_chapters);
1389 return ris != STREAM_UNSUPPORTED ? chapter : -1;
1390 } else { // chapters structure is set in the demuxer
1391 if (chapter >= demuxer->num_chapters)
1392 return -1;
1393 if (chapter < 0)
1394 chapter = 0;
1396 *seek_pts = demuxer->chapters[chapter].start / 1000.0;
1398 if (chapter_name)
1399 *chapter_name = talloc_strdup(NULL, demuxer->chapters[chapter].name);
1401 return chapter;
1405 int demuxer_get_current_chapter(demuxer_t *demuxer)
1407 int chapter = -1;
1408 if (!demuxer->num_chapters || !demuxer->chapters) {
1409 if (stream_control(demuxer->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
1410 &chapter) == STREAM_UNSUPPORTED)
1411 chapter = -1;
1412 } else {
1413 sh_video_t *sh_video = demuxer->video->sh;
1414 sh_audio_t *sh_audio = demuxer->audio->sh;
1415 uint64_t now;
1416 now = (sh_video ? sh_video->pts : (sh_audio ? sh_audio->pts : 0))
1417 * 1000 + 0.5;
1418 for (chapter = demuxer->num_chapters - 1; chapter >= 0; --chapter) {
1419 if (demuxer->chapters[chapter].start <= now)
1420 break;
1423 return chapter;
1426 char *demuxer_chapter_name(demuxer_t *demuxer, int chapter)
1428 if (demuxer->num_chapters && demuxer->chapters) {
1429 if (chapter >= 0 && chapter < demuxer->num_chapters
1430 && demuxer->chapters[chapter].name)
1431 return strdup(demuxer->chapters[chapter].name);
1433 return NULL;
1436 char *demuxer_chapter_display_name(demuxer_t *demuxer, int chapter)
1438 char *chapter_name = demuxer_chapter_name(demuxer, chapter);
1439 if (chapter_name) {
1440 char *tmp = malloc(strlen(chapter_name) + 14);
1441 snprintf(tmp, 63, "(%d) %s", chapter + 1, chapter_name);
1442 free(chapter_name);
1443 return tmp;
1444 } else {
1445 int chapter_num = demuxer_chapter_count(demuxer);
1446 char tmp[30];
1447 if (chapter_num <= 0)
1448 sprintf(tmp, "(%d)", chapter + 1);
1449 else
1450 sprintf(tmp, "(%d) of %d", chapter + 1, chapter_num);
1451 return strdup(tmp);
1455 float demuxer_chapter_time(demuxer_t *demuxer, int chapter, float *end)
1457 if (demuxer->num_chapters && demuxer->chapters && chapter >= 0
1458 && chapter < demuxer->num_chapters) {
1459 if (end)
1460 *end = demuxer->chapters[chapter].end / 1000.0;
1461 return demuxer->chapters[chapter].start / 1000.0;
1463 return -1.0;
1466 int demuxer_chapter_count(demuxer_t *demuxer)
1468 if (!demuxer->num_chapters || !demuxer->chapters) {
1469 int num_chapters = 0;
1470 if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS,
1471 &num_chapters) == STREAM_UNSUPPORTED)
1472 num_chapters = 0;
1473 return num_chapters;
1474 } else
1475 return demuxer->num_chapters;
1478 int demuxer_angles_count(demuxer_t *demuxer)
1480 int ris, angles = -1;
1482 ris = stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_ANGLES, &angles);
1483 if (ris == STREAM_UNSUPPORTED)
1484 return -1;
1485 return angles;
1488 int demuxer_get_current_angle(demuxer_t *demuxer)
1490 int ris, curr_angle = -1;
1491 ris = stream_control(demuxer->stream, STREAM_CTRL_GET_ANGLE, &curr_angle);
1492 if (ris == STREAM_UNSUPPORTED)
1493 return -1;
1494 return curr_angle;
1498 int demuxer_set_angle(demuxer_t *demuxer, int angle)
1500 int ris, angles = -1;
1501 sh_video_t *sh_video = demuxer->video->sh;
1502 sh_audio_t *sh_audio = demuxer->audio->sh;
1504 angles = demuxer_angles_count(demuxer);
1505 if ((angles < 1) || (angle > angles))
1506 return -1;
1508 demux_flush(demuxer);
1510 ris = stream_control(demuxer->stream, STREAM_CTRL_SET_ANGLE, &angle);
1511 if (ris == STREAM_UNSUPPORTED)
1512 return -1;
1514 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1515 if (sh_video) {
1516 ds_fill_buffer(demuxer->video);
1517 resync_video_stream(sh_video);
1520 if (sh_audio) {
1521 ds_fill_buffer(demuxer->audio);
1522 resync_audio_stream(sh_audio);
1525 return angle;
1528 int demuxer_audio_track_by_lang(demuxer_t *d, char *lang)
1530 int i, len;
1531 lang += strspn(lang, ",");
1532 while ((len = strcspn(lang, ",")) > 0) {
1533 for (i = 0; i < MAX_A_STREAMS; ++i) {
1534 sh_audio_t *sh = d->a_streams[i];
1535 if (sh && sh->lang && strncmp(sh->lang, lang, len) == 0)
1536 return sh->aid;
1538 lang += len;
1539 lang += strspn(lang, ",");
1541 return -1;
1544 int demuxer_sub_track_by_lang(demuxer_t *d, char *lang)
1546 int i, len;
1547 lang += strspn(lang, ",");
1548 while ((len = strcspn(lang, ",")) > 0) {
1549 for (i = 0; i < MAX_S_STREAMS; ++i) {
1550 sh_sub_t *sh = d->s_streams[i];
1551 if (sh && sh->lang && strncmp(sh->lang, lang, len) == 0)
1552 return sh->sid;
1554 lang += len;
1555 lang += strspn(lang, ",");
1557 return -1;
1560 int demuxer_default_audio_track(demuxer_t *d)
1562 int i;
1563 for (i = 0; i < MAX_A_STREAMS; ++i) {
1564 sh_audio_t *sh = d->a_streams[i];
1565 if (sh && sh->default_track)
1566 return sh->aid;
1568 for (i = 0; i < MAX_A_STREAMS; ++i) {
1569 sh_audio_t *sh = d->a_streams[i];
1570 if (sh)
1571 return sh->aid;
1573 return -1;
1576 int demuxer_default_sub_track(demuxer_t *d)
1578 int i;
1579 for (i = 0; i < MAX_S_STREAMS; ++i) {
1580 sh_sub_t *sh = d->s_streams[i];
1581 if (sh && sh->default_track)
1582 return sh->sid;
1584 return -1;