demux_mkv: support E-AC-3 audio
[mplayer/greg.git] / libmpdemux / demuxer.c
blob9b38af94529c34721b8d6846c59aca1cc6003528
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 "m_config.h"
35 #include "libvo/fastmemcpy.h"
37 #include "stream/stream.h"
38 #include "demuxer.h"
39 #include "stheader.h"
40 #include "mf.h"
42 #include "libaf/af_format.h"
43 #include "libmpcodecs/dec_teletext.h"
44 #include "libmpcodecs/vd_ffmpeg.h"
46 #ifdef CONFIG_FFMPEG
47 #include "libavcodec/avcodec.h"
48 #if MP_INPUT_BUFFER_PADDING_SIZE < FF_INPUT_BUFFER_PADDING_SIZE
49 #error MP_INPUT_BUFFER_PADDING_SIZE is too small!
50 #endif
51 #endif
53 static void clear_parser(sh_common_t *sh);
55 // Demuxer list
56 extern const demuxer_desc_t demuxer_desc_rawaudio;
57 extern const demuxer_desc_t demuxer_desc_rawvideo;
58 extern const demuxer_desc_t demuxer_desc_tv;
59 extern const demuxer_desc_t demuxer_desc_mf;
60 extern const demuxer_desc_t demuxer_desc_avi;
61 extern const demuxer_desc_t demuxer_desc_y4m;
62 extern const demuxer_desc_t demuxer_desc_asf;
63 extern const demuxer_desc_t demuxer_desc_real;
64 extern const demuxer_desc_t demuxer_desc_smjpeg;
65 extern const demuxer_desc_t demuxer_desc_matroska;
66 extern const demuxer_desc_t demuxer_desc_realaudio;
67 extern const demuxer_desc_t demuxer_desc_vqf;
68 extern const demuxer_desc_t demuxer_desc_mov;
69 extern const demuxer_desc_t demuxer_desc_vivo;
70 extern const demuxer_desc_t demuxer_desc_fli;
71 extern const demuxer_desc_t demuxer_desc_film;
72 extern const demuxer_desc_t demuxer_desc_roq;
73 extern const demuxer_desc_t demuxer_desc_gif;
74 extern const demuxer_desc_t demuxer_desc_ogg;
75 extern const demuxer_desc_t demuxer_desc_avs;
76 extern const demuxer_desc_t demuxer_desc_pva;
77 extern const demuxer_desc_t demuxer_desc_nsv;
78 extern const demuxer_desc_t demuxer_desc_mpeg_ts;
79 extern const demuxer_desc_t demuxer_desc_lmlm4;
80 extern const demuxer_desc_t demuxer_desc_mpeg_ps;
81 extern const demuxer_desc_t demuxer_desc_mpeg_pes;
82 extern const demuxer_desc_t demuxer_desc_mpeg_es;
83 extern const demuxer_desc_t demuxer_desc_mpeg_gxf;
84 extern const demuxer_desc_t demuxer_desc_mpeg4_es;
85 extern const demuxer_desc_t demuxer_desc_h264_es;
86 extern const demuxer_desc_t demuxer_desc_rawdv;
87 extern const demuxer_desc_t demuxer_desc_mpc;
88 extern const demuxer_desc_t demuxer_desc_audio;
89 extern const demuxer_desc_t demuxer_desc_xmms;
90 extern const demuxer_desc_t demuxer_desc_mpeg_ty;
91 extern const demuxer_desc_t demuxer_desc_rtp;
92 extern const demuxer_desc_t demuxer_desc_rtp_nemesi;
93 extern const demuxer_desc_t demuxer_desc_lavf;
94 extern const demuxer_desc_t demuxer_desc_lavf_preferred;
95 extern const demuxer_desc_t demuxer_desc_aac;
96 extern const demuxer_desc_t demuxer_desc_nut;
97 extern const demuxer_desc_t demuxer_desc_mng;
99 /* Please do not add any new demuxers here. If you want to implement a new
100 * demuxer, add it to libavformat, except for wrappers around external
101 * libraries and demuxers requiring binary support. */
103 const demuxer_desc_t *const demuxer_list[] = {
104 &demuxer_desc_rawaudio,
105 &demuxer_desc_rawvideo,
106 #ifdef CONFIG_TV
107 &demuxer_desc_tv,
108 #endif
109 &demuxer_desc_mf,
110 #ifdef CONFIG_FFMPEG
111 &demuxer_desc_lavf_preferred,
112 #endif
113 &demuxer_desc_avi,
114 &demuxer_desc_y4m,
115 &demuxer_desc_asf,
116 &demuxer_desc_nsv,
117 &demuxer_desc_real,
118 &demuxer_desc_smjpeg,
119 &demuxer_desc_matroska,
120 &demuxer_desc_realaudio,
121 &demuxer_desc_vqf,
122 &demuxer_desc_mov,
123 &demuxer_desc_vivo,
124 &demuxer_desc_fli,
125 &demuxer_desc_film,
126 &demuxer_desc_roq,
127 #ifdef CONFIG_GIF
128 &demuxer_desc_gif,
129 #endif
130 #ifdef CONFIG_OGGVORBIS
131 &demuxer_desc_ogg,
132 #endif
133 #ifdef CONFIG_WIN32DLL
134 &demuxer_desc_avs,
135 #endif
136 &demuxer_desc_pva,
137 &demuxer_desc_mpeg_ts,
138 &demuxer_desc_lmlm4,
139 &demuxer_desc_mpeg_ps,
140 &demuxer_desc_mpeg_pes,
141 &demuxer_desc_mpeg_es,
142 &demuxer_desc_mpeg_gxf,
143 &demuxer_desc_mpeg4_es,
144 &demuxer_desc_h264_es,
145 &demuxer_desc_audio,
146 &demuxer_desc_mpeg_ty,
147 #ifdef CONFIG_LIVE555
148 &demuxer_desc_rtp,
149 #endif
150 #ifdef CONFIG_LIBNEMESI
151 &demuxer_desc_rtp_nemesi,
152 #endif
153 #ifdef CONFIG_FFMPEG
154 &demuxer_desc_lavf,
155 #endif
156 #ifdef CONFIG_MUSEPACK
157 &demuxer_desc_mpc,
158 #endif
159 #ifdef CONFIG_LIBDV095
160 &demuxer_desc_rawdv,
161 #endif
162 &demuxer_desc_aac,
163 #ifdef CONFIG_LIBNUT
164 &demuxer_desc_nut,
165 #endif
166 #ifdef CONFIG_XMMS
167 &demuxer_desc_xmms,
168 #endif
169 #ifdef CONFIG_MNG
170 &demuxer_desc_mng,
171 #endif
172 /* Please do not add any new demuxers here. If you want to implement a new
173 * demuxer, add it to libavformat, except for wrappers around external
174 * libraries and demuxers requiring binary support. */
175 NULL
178 struct demux_packet *new_demux_packet(size_t len)
180 if (len > 1000000000) {
181 mp_msg(MSGT_DEMUXER, MSGL_FATAL, "Attempt to allocate demux packet "
182 "over 1 GB!\n");
183 abort();
185 struct demux_packet *dp = malloc(sizeof(struct demux_packet));
186 dp->len = len;
187 dp->next = NULL;
188 dp->pts = MP_NOPTS_VALUE;
189 dp->duration = -1;
190 dp->stream_pts = MP_NOPTS_VALUE;
191 dp->pos = 0;
192 dp->flags = 0;
193 dp->refcount = 1;
194 dp->master = NULL;
195 dp->buffer = NULL;
196 if (len > 0) {
197 dp->buffer = malloc(len + MP_INPUT_BUFFER_PADDING_SIZE);
198 if (!dp->buffer) {
199 mp_msg(MSGT_DEMUXER, MSGL_FATAL, "Memory allocation failure!\n");
200 abort();
202 memset(dp->buffer + len, 0, 8);
204 return dp;
207 void resize_demux_packet(struct demux_packet *dp, size_t len)
209 if (len > 1000000000) {
210 mp_msg(MSGT_DEMUXER, MSGL_FATAL, "Attempt to realloc demux packet "
211 "over 1 GB!\n");
212 abort();
214 if (len > 0) {
215 dp->buffer = realloc(dp->buffer, len + 8);
216 if (!dp->buffer) {
217 mp_msg(MSGT_DEMUXER, MSGL_FATAL, "Memory allocation failure!\n");
218 abort();
220 memset(dp->buffer + len, 0, 8);
221 } else {
222 free(dp->buffer);
223 dp->buffer = NULL;
225 dp->len = len;
228 struct demux_packet *clone_demux_packet(struct demux_packet *pack)
230 struct demux_packet *dp = malloc(sizeof(struct demux_packet));
231 while (pack->master)
232 pack = pack->master; // find the master
233 memcpy(dp, pack, sizeof(struct demux_packet));
234 dp->next = NULL;
235 dp->refcount = 0;
236 dp->master = pack;
237 pack->refcount++;
238 return dp;
241 void free_demux_packet(struct demux_packet *dp)
243 if (dp->master == NULL) { //dp is a master packet
244 dp->refcount--;
245 if (dp->refcount == 0) {
246 free(dp->buffer);
247 free(dp);
249 return;
251 // dp is a clone:
252 free_demux_packet(dp->master);
253 free(dp);
256 void free_demuxer_stream(demux_stream_t *ds)
258 ds_free_packs(ds);
259 free(ds);
262 demux_stream_t *new_demuxer_stream(struct demuxer *demuxer, int id)
264 demux_stream_t *ds = malloc(sizeof(demux_stream_t));
265 *ds = (demux_stream_t){
266 .id = id,
267 .demuxer = demuxer,
268 .asf_seq = -1,
270 return ds;
275 * Get demuxer description structure for a given demuxer type
277 * @param file_format type of the demuxer
278 * @return structure for the demuxer, NULL if not found
280 static const demuxer_desc_t *get_demuxer_desc_from_type(int file_format)
282 int i;
284 for (i = 0; demuxer_list[i]; i++)
285 if (file_format == demuxer_list[i]->type)
286 return demuxer_list[i];
288 return NULL;
292 demuxer_t *new_demuxer(struct MPOpts *opts, stream_t *stream, int type,
293 int a_id, int v_id, int s_id, char *filename)
295 struct demuxer *d = talloc_zero(NULL, struct demuxer);
296 d->stream = stream;
297 d->stream_pts = MP_NOPTS_VALUE;
298 d->reference_clock = MP_NOPTS_VALUE;
299 d->movi_start = stream->start_pos;
300 d->movi_end = stream->end_pos;
301 d->seekable = 1;
302 d->synced = 0;
303 d->filepos = -1;
304 d->audio = new_demuxer_stream(d, a_id);
305 d->video = new_demuxer_stream(d, v_id);
306 d->sub = new_demuxer_stream(d, s_id);
307 d->type = type;
308 d->opts = opts;
309 if (type)
310 if (!(d->desc = get_demuxer_desc_from_type(type)))
311 mp_msg(MSGT_DEMUXER, MSGL_ERR,
312 "BUG! Invalid demuxer type in new_demuxer(), "
313 "big troubles ahead.");
314 if (filename) // Filename hack for avs_check_file
315 d->filename = strdup(filename);
316 stream_seek(stream, stream->start_pos);
317 return d;
320 const char *sh_sub_type2str(int type)
322 switch (type) {
323 case 't': return "text";
324 case 'm': return "movtext";
325 case 'a': return "ass";
326 case 'v': return "vobsub";
327 case 'x': return "xsub";
328 case 'b': return "dvb";
329 case 'd': return "dvb-teletext";
330 case 'p': return "hdmv pgs";
332 return "unknown";
335 sh_sub_t *new_sh_sub_sid(demuxer_t *demuxer, int id, int sid)
337 if (id > MAX_S_STREAMS - 1 || id < 0) {
338 mp_msg(MSGT_DEMUXER, MSGL_WARN,
339 "Requested sub stream id overflow (%d > %d)\n", id,
340 MAX_S_STREAMS);
341 return NULL;
343 if (demuxer->s_streams[id])
344 mp_msg(MSGT_DEMUXER, MSGL_WARN, "Sub stream %i redefined\n", id);
345 else {
346 sh_sub_t *sh = calloc(1, sizeof(sh_sub_t));
347 demuxer->s_streams[id] = sh;
348 sh->sid = sid;
349 sh->opts = demuxer->opts;
350 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SUBTITLE_ID=%d\n", sid);
352 return demuxer->s_streams[id];
355 struct sh_sub *new_sh_sub_sid_lang(struct demuxer *demuxer, int id, int sid,
356 const char *lang)
358 struct sh_sub *sh = new_sh_sub_sid(demuxer, id, sid);
359 if (lang && lang[0] && strcmp(lang, "und")) {
360 sh->lang = strdup(lang);
361 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_LANG=%s\n", sid, lang);
363 return sh;
366 static void free_sh_sub(sh_sub_t *sh)
368 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_sub at %p\n", sh);
369 free(sh->extradata);
370 free(sh->lang);
371 #ifdef CONFIG_FFMPEG
372 clear_parser((sh_common_t *)sh);
373 #endif
374 free(sh);
377 sh_audio_t *new_sh_audio_aid(demuxer_t *demuxer, int id, int aid)
379 if (id > MAX_A_STREAMS - 1 || id < 0) {
380 mp_msg(MSGT_DEMUXER, MSGL_WARN,
381 "Requested audio stream id overflow (%d > %d)\n", id,
382 MAX_A_STREAMS);
383 return NULL;
385 if (demuxer->a_streams[id]) {
386 mp_tmsg(MSGT_DEMUXER, MSGL_WARN, "WARNING: Audio stream header %d redefined.\n", id);
387 } else {
388 mp_tmsg(MSGT_DEMUXER, MSGL_V, "==> Found audio stream: %d\n", id);
389 sh_audio_t *sh = calloc(1, sizeof(sh_audio_t));
390 demuxer->a_streams[id] = sh;
391 sh->aid = aid;
392 sh->ds = demuxer->audio;
393 // set some defaults
394 sh->samplesize = 2;
395 sh->sample_format = AF_FORMAT_S16_NE;
396 sh->opts = demuxer->opts;
397 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_ID=%d\n", aid);
399 return demuxer->a_streams[id];
402 void free_sh_audio(demuxer_t *demuxer, int id)
404 sh_audio_t *sh = demuxer->a_streams[id];
405 demuxer->a_streams[id] = NULL;
406 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_audio at %p\n", sh);
407 free(sh->wf);
408 free(sh->codecdata);
409 free(sh->lang);
410 #ifdef CONFIG_FFMPEG
411 clear_parser((sh_common_t *)sh);
412 #endif
413 free(sh);
416 sh_video_t *new_sh_video_vid(demuxer_t *demuxer, int id, int vid)
418 if (id > MAX_V_STREAMS - 1 || id < 0) {
419 mp_msg(MSGT_DEMUXER, MSGL_WARN,
420 "Requested video stream id overflow (%d > %d)\n", id,
421 MAX_V_STREAMS);
422 return NULL;
424 if (demuxer->v_streams[id])
425 mp_tmsg(MSGT_DEMUXER, MSGL_WARN, "WARNING: Video stream header %d redefined.\n", id);
426 else {
427 mp_tmsg(MSGT_DEMUXER, MSGL_V, "==> Found video stream: %d\n", id);
428 sh_video_t *sh = calloc(1, sizeof *sh);
429 demuxer->v_streams[id] = sh;
430 sh->vid = vid;
431 sh->ds = demuxer->video;
432 sh->opts = demuxer->opts;
433 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_ID=%d\n", vid);
435 return demuxer->v_streams[id];
438 void free_sh_video(sh_video_t *sh)
440 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_video at %p\n", sh);
441 free(sh->bih);
442 #ifdef CONFIG_FFMPEG
443 clear_parser((sh_common_t *)sh);
444 #endif
445 free(sh);
448 void free_demuxer(demuxer_t *demuxer)
450 int i;
451 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing %s demuxer at %p\n",
452 demuxer->desc->shortdesc, demuxer);
453 if (demuxer->desc->close)
454 demuxer->desc->close(demuxer);
455 // Very ugly hack to make it behave like old implementation
456 if (demuxer->desc->type == DEMUXER_TYPE_DEMUXERS)
457 goto skip_streamfree;
458 // free streams:
459 for (i = 0; i < MAX_A_STREAMS; i++)
460 if (demuxer->a_streams[i])
461 free_sh_audio(demuxer, i);
462 for (i = 0; i < MAX_V_STREAMS; i++)
463 if (demuxer->v_streams[i])
464 free_sh_video(demuxer->v_streams[i]);
465 for (i = 0; i < MAX_S_STREAMS; i++)
466 if (demuxer->s_streams[i])
467 free_sh_sub(demuxer->s_streams[i]);
468 // free demuxers:
469 free_demuxer_stream(demuxer->audio);
470 free_demuxer_stream(demuxer->video);
471 free_demuxer_stream(demuxer->sub);
472 skip_streamfree:
473 free(demuxer->filename);
474 if (demuxer->teletext)
475 teletext_control(demuxer->teletext, TV_VBI_CONTROL_STOP, NULL);
476 talloc_free(demuxer);
480 void ds_add_packet(demux_stream_t *ds, demux_packet_t *dp)
482 // append packet to DS stream:
483 ++ds->packs;
484 ds->bytes += dp->len;
485 if (ds->last) {
486 // next packet in stream
487 ds->last->next = dp;
488 ds->last = dp;
489 } else {
490 // first packet in stream
491 ds->first = ds->last = dp;
493 mp_dbg(MSGT_DEMUXER, MSGL_DBG2,
494 "DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
495 (ds == ds->demuxer->audio) ? "d_audio" : "d_video", dp->len,
496 dp->pts, (unsigned int) dp->pos, ds->demuxer->audio->packs,
497 ds->demuxer->video->packs);
500 #ifdef CONFIG_FFMPEG
501 static void allocate_parser(AVCodecContext **avctx, AVCodecParserContext **parser, unsigned format)
503 enum CodecID codec_id = CODEC_ID_NONE;
505 init_avcodec();
507 switch (format) {
508 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52, 94, 0)
509 case MKTAG('M', 'P', '4', 'L'):
510 codec_id = CODEC_ID_AAC_LATM;
511 break;
512 #endif
513 case 0x2000:
514 case 0x332D6361:
515 case 0x332D4341:
516 case 0x20736D:
517 case MKTAG('d', 'n', 'e', 't'):
518 case MKTAG('s', 'a', 'c', '3'):
519 codec_id = CODEC_ID_AC3;
520 break;
521 case MKTAG('E', 'A', 'C', '3'):
522 codec_id = CODEC_ID_EAC3;
523 break;
524 case 0x2001:
525 case 0x86:
526 codec_id = CODEC_ID_DTS;
527 break;
528 case MKTAG('f', 'L', 'a', 'C'):
529 codec_id = CODEC_ID_FLAC;
530 break;
531 case MKTAG('M', 'L', 'P', ' '):
532 codec_id = CODEC_ID_MLP;
533 break;
534 case 0x55:
535 case 0x5500736d:
536 case 0x55005354:
537 case MKTAG('.', 'm', 'p', '3'):
538 case MKTAG('M', 'P', '3', ' '):
539 case MKTAG('L', 'A', 'M', 'E'):
540 codec_id = CODEC_ID_MP3;
541 break;
542 case 0x50:
543 case 0x5000736d:
544 case MKTAG('.', 'm', 'p', '2'):
545 case MKTAG('.', 'm', 'p', '1'):
546 codec_id = CODEC_ID_MP2;
547 break;
548 case MKTAG('T', 'R', 'H', 'D'):
549 codec_id = CODEC_ID_TRUEHD;
550 break;
552 if (codec_id != CODEC_ID_NONE) {
553 *avctx = avcodec_alloc_context();
554 if (!*avctx)
555 return;
556 *parser = av_parser_init(codec_id);
557 if (!*parser)
558 av_freep(avctx);
562 static void get_parser(sh_common_t *sh, AVCodecContext **avctx, AVCodecParserContext **parser)
564 *avctx = NULL;
565 *parser = NULL;
567 if (!sh || !sh->needs_parsing)
568 return;
570 *avctx = sh->avctx;
571 *parser = sh->parser;
572 if (*parser)
573 return;
575 allocate_parser(avctx, parser, sh->format);
576 sh->avctx = *avctx;
577 sh->parser = *parser;
580 int ds_parse(demux_stream_t *ds, uint8_t **buffer, int *len, double pts, off_t pos)
582 AVCodecContext *avctx;
583 AVCodecParserContext *parser;
584 get_parser(ds->sh, &avctx, &parser);
585 if (!parser)
586 return *len;
587 return av_parser_parse2(parser, avctx, buffer, len, *buffer, *len, pts, pts, pos);
590 static void clear_parser(sh_common_t *sh)
592 av_parser_close(sh->parser);
593 sh->parser = NULL;
594 av_freep(&sh->avctx);
597 void ds_clear_parser(demux_stream_t *ds)
599 if (!ds->sh)
600 return;
601 clear_parser(ds->sh);
603 #endif
605 void ds_read_packet(demux_stream_t *ds, stream_t *stream, int len,
606 double pts, off_t pos, int flags)
608 demux_packet_t *dp = new_demux_packet(len);
609 len = stream_read(stream, dp->buffer, len);
610 resize_demux_packet(dp, len);
611 dp->pts = pts;
612 dp->pos = pos;
613 dp->flags = flags;
614 // append packet to DS stream:
615 ds_add_packet(ds, dp);
618 // return value:
619 // 0 = EOF or no stream found or invalid type
620 // 1 = successfully read a packet
622 int demux_fill_buffer(demuxer_t *demux, demux_stream_t *ds)
624 // Note: parameter 'ds' can be NULL!
625 return demux->desc->fill_buffer(demux, ds);
628 // return value:
629 // 0 = EOF
630 // 1 = successful
631 int ds_fill_buffer(demux_stream_t *ds)
633 demuxer_t *demux = ds->demuxer;
634 if (ds->current)
635 free_demux_packet(ds->current);
636 ds->current = NULL;
637 if (mp_msg_test(MSGT_DEMUXER, MSGL_DBG3)) {
638 if (ds == demux->audio)
639 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
640 "ds_fill_buffer(d_audio) called\n");
641 else if (ds == demux->video)
642 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
643 "ds_fill_buffer(d_video) called\n");
644 else if (ds == demux->sub)
645 mp_dbg(MSGT_DEMUXER, MSGL_DBG3, "ds_fill_buffer(d_sub) called\n");
646 else
647 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
648 "ds_fill_buffer(unknown 0x%X) called\n", (unsigned int) ds);
650 while (1) {
651 if (ds->packs) {
652 demux_packet_t *p = ds->first;
653 // copy useful data:
654 ds->buffer = p->buffer;
655 ds->buffer_pos = 0;
656 ds->buffer_size = p->len;
657 ds->pos = p->pos;
658 ds->dpos += p->len; // !!!
659 ++ds->pack_no;
660 if (p->pts != MP_NOPTS_VALUE) {
661 ds->pts = p->pts;
662 ds->pts_bytes = 0;
664 ds->pts_bytes += p->len; // !!!
665 if (p->stream_pts != MP_NOPTS_VALUE)
666 demux->stream_pts = p->stream_pts;
667 ds->flags = p->flags;
668 // unlink packet:
669 ds->bytes -= p->len;
670 ds->current = p;
671 ds->first = p->next;
672 if (!ds->first)
673 ds->last = NULL;
674 --ds->packs;
675 /* The code below can set ds->eof to 1 when another stream runs
676 * out of buffer space. That makes sense because in that situation
677 * the calling code should not count on being able to demux more
678 * packets from this stream.
679 * If however the situation improves and we're called again
680 * despite the eof flag then it's better to clear it to avoid
681 * weird behavior. */
682 ds->eof = 0;
683 return 1;
686 #define MaybeNI _("Maybe you are playing a non-interleaved stream/file or the codec failed?\n" \
687 "For AVI files, try to force non-interleaved mode with the -ni option.\n")
689 if (demux->audio->packs >= MAX_PACKS
690 || demux->audio->bytes >= MAX_PACK_BYTES) {
691 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
692 demux->audio->packs, demux->audio->bytes);
693 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
694 break;
696 if (demux->video->packs >= MAX_PACKS
697 || demux->video->bytes >= MAX_PACK_BYTES) {
698 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
699 demux->video->packs, demux->video->bytes);
700 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
701 break;
703 if (!demux_fill_buffer(demux, ds)) {
704 mp_dbg(MSGT_DEMUXER, MSGL_DBG2,
705 "ds_fill_buffer()->demux_fill_buffer() failed\n");
706 break; // EOF
709 ds->buffer_pos = ds->buffer_size = 0;
710 ds->buffer = NULL;
711 mp_msg(MSGT_DEMUXER, MSGL_V,
712 "ds_fill_buffer: EOF reached (stream: %s) \n",
713 ds == demux->audio ? "audio" : "video");
714 ds->eof = 1;
715 return 0;
718 int demux_read_data(demux_stream_t *ds, unsigned char *mem, int len)
720 int x;
721 int bytes = 0;
722 while (len > 0) {
723 x = ds->buffer_size - ds->buffer_pos;
724 if (x == 0) {
725 if (!ds_fill_buffer(ds))
726 return bytes;
727 } else {
728 if (x > len)
729 x = len;
730 if (mem)
731 fast_memcpy(mem + bytes, &ds->buffer[ds->buffer_pos], x);
732 bytes += x;
733 len -= x;
734 ds->buffer_pos += x;
737 return bytes;
741 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
742 * \param mem memory to read data into, may be NULL to discard data
743 * \param maxlen maximum number of bytes to read
744 * \param read number of bytes actually read
745 * \param pattern pattern to search for (lowest 8 bits are ignored)
746 * \return whether pattern was found
748 int demux_pattern_3(demux_stream_t *ds, unsigned char *mem, int maxlen,
749 int *read, uint32_t pattern)
751 register uint32_t head = 0xffffff00;
752 register uint32_t pat = pattern & 0xffffff00;
753 int total_len = 0;
754 do {
755 register unsigned char *ds_buf = &ds->buffer[ds->buffer_size];
756 int len = ds->buffer_size - ds->buffer_pos;
757 register long pos = -len;
758 if (unlikely(pos >= 0)) { // buffer is empty
759 ds_fill_buffer(ds);
760 continue;
762 do {
763 head |= ds_buf[pos];
764 head <<= 8;
765 } while (++pos && head != pat);
766 len += pos;
767 if (total_len + len > maxlen)
768 len = maxlen - total_len;
769 len = demux_read_data(ds, mem ? &mem[total_len] : NULL, len);
770 total_len += len;
771 } while ((head != pat || total_len < 3) && total_len < maxlen && !ds->eof);
772 if (read)
773 *read = total_len;
774 return total_len >= 3 && head == pat;
777 void ds_free_packs(demux_stream_t *ds)
779 demux_packet_t *dp = ds->first;
780 while (dp) {
781 demux_packet_t *dn = dp->next;
782 free_demux_packet(dp);
783 dp = dn;
785 if (ds->asf_packet) {
786 // free unfinished .asf fragments:
787 free(ds->asf_packet->buffer);
788 free(ds->asf_packet);
789 ds->asf_packet = NULL;
791 ds->first = ds->last = NULL;
792 ds->packs = 0; // !!!!!
793 ds->bytes = 0;
794 if (ds->current)
795 free_demux_packet(ds->current);
796 ds->current = NULL;
797 ds->buffer = NULL;
798 ds->buffer_pos = ds->buffer_size;
799 ds->pts = 0;
800 ds->pts_bytes = 0;
803 int ds_get_packet(demux_stream_t *ds, unsigned char **start)
805 int len;
806 if (ds->buffer_pos >= ds->buffer_size) {
807 if (!ds_fill_buffer(ds)) {
808 // EOF
809 *start = NULL;
810 return -1;
813 len = ds->buffer_size - ds->buffer_pos;
814 *start = &ds->buffer[ds->buffer_pos];
815 ds->buffer_pos += len;
816 return len;
819 int ds_get_packet_pts(demux_stream_t *ds, unsigned char **start, double *pts)
821 int len;
822 *pts = MP_NOPTS_VALUE;
823 len = ds_get_packet(ds, start);
824 if (len < 0)
825 return len;
826 // Return pts unless this read starts from the middle of a packet
827 if (len == ds->buffer_pos)
828 *pts = ds->current->pts;
829 return len;
832 int ds_get_packet_sub(demux_stream_t *ds, unsigned char **start)
834 int len;
835 if (ds->buffer_pos >= ds->buffer_size) {
836 *start = NULL;
837 if (!ds->packs)
838 return -1; // no sub
839 if (!ds_fill_buffer(ds))
840 return -1; // EOF
842 len = ds->buffer_size - ds->buffer_pos;
843 *start = &ds->buffer[ds->buffer_pos];
844 ds->buffer_pos += len;
845 return len;
848 double ds_get_next_pts(demux_stream_t *ds)
850 demuxer_t *demux = ds->demuxer;
851 // if we have not read from the "current" packet, consider it
852 // as the next, otherwise we never get the pts for the first packet.
853 while (!ds->first && (!ds->current || ds->buffer_pos)) {
854 if (demux->audio->packs >= MAX_PACKS
855 || demux->audio->bytes >= MAX_PACK_BYTES) {
856 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
857 demux->audio->packs, demux->audio->bytes);
858 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
859 return MP_NOPTS_VALUE;
861 if (demux->video->packs >= MAX_PACKS
862 || demux->video->bytes >= MAX_PACK_BYTES) {
863 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
864 demux->video->packs, demux->video->bytes);
865 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
866 return MP_NOPTS_VALUE;
868 if (!demux_fill_buffer(demux, ds))
869 return MP_NOPTS_VALUE;
871 // take pts from "current" if we never read from it.
872 if (ds->current && !ds->buffer_pos)
873 return ds->current->pts;
874 return ds->first->pts;
877 // ====================================================================
879 void demuxer_help(void)
881 int i;
883 mp_msg(MSGT_DEMUXER, MSGL_INFO, "Available demuxers:\n");
884 mp_msg(MSGT_DEMUXER, MSGL_INFO, " demuxer: type info: (comment)\n");
885 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DEMUXERS\n");
886 for (i = 0; demuxer_list[i]; i++) {
887 if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Don't display special demuxers
888 continue;
889 if (demuxer_list[i]->comment && strlen(demuxer_list[i]->comment))
890 mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s (%s)\n",
891 demuxer_list[i]->name, demuxer_list[i]->type,
892 demuxer_list[i]->info, demuxer_list[i]->comment);
893 else
894 mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s\n",
895 demuxer_list[i]->name, demuxer_list[i]->type,
896 demuxer_list[i]->info);
902 * Get demuxer type for a given demuxer name
904 * @param demuxer_name string with demuxer name of demuxer number
905 * @param force will be set if demuxer should be forced.
906 * May be NULL.
907 * @return DEMUXER_TYPE_xxx, -1 if error or not found
909 int get_demuxer_type_from_name(char *demuxer_name, int *force)
911 int i;
912 long type_int;
913 char *endptr;
915 if (!demuxer_name || !demuxer_name[0])
916 return DEMUXER_TYPE_UNKNOWN;
917 if (force)
918 *force = demuxer_name[0] == '+';
919 if (demuxer_name[0] == '+')
920 demuxer_name = &demuxer_name[1];
921 for (i = 0; demuxer_list[i]; i++) {
922 if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Can't select special demuxers from commandline
923 continue;
924 if (strcmp(demuxer_name, demuxer_list[i]->name) == 0)
925 return demuxer_list[i]->type;
928 // No match found, try to parse name as an integer (demuxer number)
929 type_int = strtol(demuxer_name, &endptr, 0);
930 if (*endptr) // Conversion failed
931 return -1;
932 if ((type_int > 0) && (type_int <= DEMUXER_TYPE_MAX))
933 return (int) type_int;
935 return -1;
938 static struct demuxer *open_given_type(struct MPOpts *opts,
939 const struct demuxer_desc *desc,
940 struct stream *stream, bool force,
941 int audio_id, int video_id, int sub_id,
942 char *filename)
944 struct demuxer *demuxer;
945 int fformat;
946 demuxer = new_demuxer(opts, stream, desc->type, audio_id,
947 video_id, sub_id, filename);
948 if (desc->check_file)
949 fformat = desc->check_file(demuxer);
950 else
951 fformat = desc->type;
952 if (force)
953 fformat = desc->type;
954 if (fformat == 0)
955 goto fail;
956 if (fformat == desc->type) {
957 if (demuxer->filetype)
958 mp_tmsg(MSGT_DEMUXER, MSGL_INFO, "Detected file format: %s (%s)\n",
959 demuxer->filetype, desc->shortdesc);
960 else
961 mp_tmsg(MSGT_DEMUXER, MSGL_INFO, "Detected file format: %s\n",
962 desc->shortdesc);
963 if (demuxer->desc->open) {
964 struct demuxer *demux2 = demuxer->desc->open(demuxer);
965 if (!demux2) {
966 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "Opening as detected format "
967 "\"%s\" failed.\n", desc->shortdesc);
968 goto fail;
970 /* At least demux_mov can return a demux_demuxers instance
971 * from open() instead of the original fed in. */
972 demuxer = demux2;
974 demuxer->file_format = fformat;
975 return demuxer;
976 } else {
977 // demux_mov can return playlist instead of mov
978 if (fformat == DEMUXER_TYPE_PLAYLIST)
979 return demuxer; // handled in mplayer.c
980 /* Internal MPEG PS demuxer check can return other MPEG subtypes
981 * which don't have their own checks; recurse to try opening as
982 * the returned type instead. */
983 free_demuxer(demuxer);
984 desc = get_demuxer_desc_from_type(fformat);
985 if (!desc) {
986 mp_msg(MSGT_DEMUXER, MSGL_ERR,
987 "BUG: recursion to nonexistent file format\n");
988 return NULL;
990 return open_given_type(opts, desc, stream, false, audio_id,
991 video_id, sub_id, filename);
993 fail:
994 free_demuxer(demuxer);
995 return NULL;
998 static struct demuxer *demux_open_stream(struct MPOpts *opts,
999 struct stream *stream,
1000 int file_format, bool force,
1001 int audio_id, int video_id, int sub_id,
1002 char *filename)
1004 struct demuxer *demuxer = NULL;
1005 const struct demuxer_desc *desc;
1007 // If somebody requested a demuxer check it
1008 if (file_format) {
1009 desc = get_demuxer_desc_from_type(file_format);
1010 if (!desc)
1011 // should only happen with obsolete -demuxer 99 numeric format
1012 return NULL;
1013 demuxer = open_given_type(opts, desc, stream, force, audio_id,
1014 video_id, sub_id, filename);
1015 if (demuxer)
1016 goto dmx_open;
1017 return NULL;
1020 // Test demuxers with safe file checks
1021 for (int i = 0; (desc = demuxer_list[i]); i++) {
1022 if (desc->safe_check) {
1023 demuxer = open_given_type(opts, desc, stream, false, audio_id,
1024 video_id, sub_id, filename);
1025 if (demuxer)
1026 goto dmx_open;
1030 // Ok. We're over the stable detectable fileformats, the next ones are
1031 // a bit fuzzy. So by default (extension_parsing==1) try extension-based
1032 // detection first:
1033 if (filename && opts->extension_parsing == 1) {
1034 desc = get_demuxer_desc_from_type(demuxer_type_by_filename(filename));
1035 if (desc)
1036 demuxer = open_given_type(opts, desc, stream, false, audio_id,
1037 video_id, sub_id, filename);
1038 if (demuxer)
1039 goto dmx_open;
1042 // Finally try detection for demuxers with unsafe checks
1043 for (int i = 0; (desc = demuxer_list[i]); i++) {
1044 if (!desc->safe_check && desc->check_file) {
1045 demuxer = open_given_type(opts, desc, stream, false, audio_id,
1046 video_id, sub_id, filename);
1047 if (demuxer)
1048 goto dmx_open;
1052 return NULL;
1054 dmx_open:
1056 if (demuxer->type == DEMUXER_TYPE_PLAYLIST)
1057 return demuxer;
1059 struct sh_video *sh_video = demuxer->video->sh;
1060 if (sh_video && sh_video->bih) {
1061 int biComp = le2me_32(sh_video->bih->biCompression);
1062 mp_msg(MSGT_DEMUX, MSGL_INFO,
1063 "VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
1064 (char *) &biComp, sh_video->bih->biWidth,
1065 sh_video->bih->biHeight, sh_video->bih->biBitCount,
1066 sh_video->fps, sh_video->i_bps * 0.008f,
1067 sh_video->i_bps / 1024.0f);
1069 return demuxer;
1072 demuxer_t *demux_open(struct MPOpts *opts, stream_t *vs, int file_format,
1073 int audio_id, int video_id, int dvdsub_id,
1074 char *filename)
1076 stream_t *as = NULL, *ss = NULL;
1077 demuxer_t *vd, *ad = NULL, *sd = NULL;
1078 demuxer_t *res;
1079 int afmt = DEMUXER_TYPE_UNKNOWN, sfmt = DEMUXER_TYPE_UNKNOWN;
1080 int demuxer_type;
1081 int audio_demuxer_type = 0, sub_demuxer_type = 0;
1082 int demuxer_force = 0, audio_demuxer_force = 0, sub_demuxer_force = 0;
1084 if ((demuxer_type =
1085 get_demuxer_type_from_name(opts->demuxer_name, &demuxer_force)) < 0) {
1086 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-demuxer %s does not exist.\n",
1087 opts->demuxer_name);
1088 return NULL;
1090 if ((audio_demuxer_type =
1091 get_demuxer_type_from_name(opts->audio_demuxer_name,
1092 &audio_demuxer_force)) < 0) {
1093 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-audio-demuxer %s does not exist.\n",
1094 opts->audio_demuxer_name);
1095 if (opts->audio_stream)
1096 return NULL;
1098 if ((sub_demuxer_type =
1099 get_demuxer_type_from_name(opts->sub_demuxer_name,
1100 &sub_demuxer_force)) < 0) {
1101 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-sub-demuxer %s does not exist.\n",
1102 opts->sub_demuxer_name);
1103 if (opts->sub_stream)
1104 return NULL;
1107 if (opts->audio_stream) {
1108 as = open_stream(opts->audio_stream, 0, &afmt);
1109 if (!as) {
1110 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "Cannot open audio stream: %s\n",
1111 opts->audio_stream);
1112 return NULL;
1114 if (opts->audio_stream_cache) {
1115 if (!stream_enable_cache
1116 (as, opts->audio_stream_cache * 1024,
1117 opts->audio_stream_cache * 1024 *
1118 (opts->stream_cache_min_percent / 100.0),
1119 opts->audio_stream_cache * 1024 *
1120 (opts->stream_cache_seek_min_percent / 100.0))) {
1121 free_stream(as);
1122 mp_msg(MSGT_DEMUXER, MSGL_ERR,
1123 "Can't enable audio stream cache\n");
1124 return NULL;
1128 if (opts->sub_stream) {
1129 ss = open_stream(opts->sub_stream, 0, &sfmt);
1130 if (!ss) {
1131 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "Cannot open subtitle stream: %s\n",
1132 opts->sub_stream);
1133 return NULL;
1137 vd = demux_open_stream(opts, vs, demuxer_type ? demuxer_type : file_format,
1138 demuxer_force, opts->audio_stream ? -2 : audio_id,
1139 video_id, opts->sub_stream ? -2 : dvdsub_id, filename);
1140 if (!vd) {
1141 if (as)
1142 free_stream(as);
1143 if (ss)
1144 free_stream(ss);
1145 return NULL;
1147 if (as) {
1148 ad = demux_open_stream(opts, as,
1149 audio_demuxer_type ? audio_demuxer_type : afmt,
1150 audio_demuxer_force, audio_id, -2, -2,
1151 opts->audio_stream);
1152 if (!ad) {
1153 mp_tmsg(MSGT_DEMUXER, MSGL_WARN, "Failed to open audio demuxer: %s\n",
1154 opts->audio_stream);
1155 free_stream(as);
1156 } else if (ad->audio->sh
1157 && ((sh_audio_t *) ad->audio->sh)->format == 0x55) // MP3
1158 opts->hr_mp3_seek = 1; // Enable high res seeking
1160 if (ss) {
1161 sd = demux_open_stream(opts, ss,
1162 sub_demuxer_type ? sub_demuxer_type : sfmt,
1163 sub_demuxer_force, -2, -2, dvdsub_id,
1164 opts->sub_stream);
1165 if (!sd) {
1166 mp_tmsg(MSGT_DEMUXER, MSGL_WARN,
1167 "Failed to open subtitle demuxer: %s\n", opts->sub_stream);
1168 free_stream(ss);
1172 if (ad && sd)
1173 res = new_demuxers_demuxer(vd, ad, sd);
1174 else if (ad)
1175 res = new_demuxers_demuxer(vd, ad, vd);
1176 else if (sd)
1177 res = new_demuxers_demuxer(vd, vd, sd);
1178 else
1179 res = vd;
1181 opts->correct_pts = opts->user_correct_pts;
1182 if (opts->correct_pts < 0)
1183 opts->correct_pts =
1184 demux_control(vd ? vd : res, DEMUXER_CTRL_CORRECT_PTS,
1185 NULL) == DEMUXER_CTRL_OK;
1186 return res;
1190 void demux_flush(demuxer_t *demuxer)
1192 ds_free_packs(demuxer->video);
1193 ds_free_packs(demuxer->audio);
1194 ds_free_packs(demuxer->sub);
1197 int demux_seek(demuxer_t *demuxer, float rel_seek_secs, float audio_delay,
1198 int flags)
1200 if (!demuxer->seekable) {
1201 if (demuxer->file_format == DEMUXER_TYPE_AVI)
1202 mp_tmsg(MSGT_SEEK, MSGL_WARN, "Cannot seek in raw AVI streams. (Index required, try with the -idx switch.)\n");
1203 #ifdef CONFIG_TV
1204 else if (demuxer->file_format == DEMUXER_TYPE_TV)
1205 mp_tmsg(MSGT_SEEK, MSGL_WARN, "TV input is not seekable! (Seeking will probably be for changing channels ;)\n");
1206 #endif
1207 else
1208 mp_tmsg(MSGT_SEEK, MSGL_WARN, "Cannot seek in this file.\n");
1209 return 0;
1211 // clear demux buffers:
1212 demux_flush(demuxer);
1213 demuxer->video->eof = 0;
1214 demuxer->audio->eof = 0;
1215 demuxer->sub->eof = 0;
1217 /* HACK: assume any demuxer used with these streams can cope with
1218 * the stream layer suddenly seeking to a different position under it
1219 * (nothing actually implements DEMUXER_CTRL_RESYNC now).
1221 struct stream *stream = demuxer->stream;
1222 if (stream->type == STREAMTYPE_DVD || stream->type == STREAMTYPE_DVDNAV) {
1223 double pts;
1225 if (flags & SEEK_ABSOLUTE)
1226 pts = 0.0f;
1227 else {
1228 if (demuxer->stream_pts == MP_NOPTS_VALUE)
1229 goto dmx_seek;
1230 pts = demuxer->stream_pts;
1233 if (flags & SEEK_FACTOR) {
1234 double tmp = 0;
1235 if (stream_control(demuxer->stream, STREAM_CTRL_GET_TIME_LENGTH,
1236 &tmp) == STREAM_UNSUPPORTED)
1237 goto dmx_seek;
1238 pts += tmp * rel_seek_secs;
1239 } else
1240 pts += rel_seek_secs;
1242 if (stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_TIME, &pts)
1243 != STREAM_UNSUPPORTED) {
1244 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1245 return 1;
1249 dmx_seek:
1250 if (demuxer->desc->seek)
1251 demuxer->desc->seek(demuxer, rel_seek_secs, audio_delay, flags);
1253 return 1;
1256 int demux_info_add(demuxer_t *demuxer, const char *opt, const char *param)
1258 return demux_info_add_bstr(demuxer, BSTR(opt), BSTR(param));
1261 int demux_info_add_bstr(demuxer_t *demuxer, struct bstr opt, struct bstr param)
1263 char **info = demuxer->info;
1264 int n = 0;
1267 for (n = 0; info && info[2 * n] != NULL; n++) {
1268 if (!bstrcasecmp(opt, BSTR(info[2*n]))) {
1269 if (!bstrcmp(param, BSTR(info[2*n + 1]))) {
1270 mp_msg(MSGT_DEMUX, MSGL_V, "Demuxer info %.*s set to unchanged value %.*s\n",
1271 BSTR_P(opt), BSTR_P(param));
1272 return 0;
1274 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "Demuxer info %.*s changed to %.*s\n",
1275 BSTR_P(opt), BSTR_P(param));
1276 talloc_free(info[2*n + 1]);
1277 info[2*n + 1] = talloc_strndup(demuxer->info, param.start, param.len);
1278 return 0;
1282 info = demuxer->info = talloc_realloc(demuxer, info, char *, 2 * (n + 2));
1283 info[2*n] = talloc_strndup(demuxer->info, opt.start, opt.len);
1284 info[2*n + 1] = talloc_strndup(demuxer->info, param.start, param.len);
1285 memset(&info[2 * (n + 1)], 0, 2 * sizeof(char *));
1287 return 1;
1290 int demux_info_print(demuxer_t *demuxer)
1292 char **info = demuxer->info;
1293 int n;
1295 if (!info)
1296 return 0;
1298 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "Clip info:\n");
1299 for (n = 0; info[2 * n] != NULL; n++) {
1300 mp_msg(MSGT_DEMUX, MSGL_INFO, " %s: %s\n", info[2 * n],
1301 info[2 * n + 1]);
1302 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_NAME%d=%s\n", n,
1303 info[2 * n]);
1304 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_VALUE%d=%s\n", n,
1305 info[2 * n + 1]);
1307 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_N=%d\n", n);
1309 return 0;
1312 char *demux_info_get(demuxer_t *demuxer, const char *opt)
1314 int i;
1315 char **info = demuxer->info;
1317 for (i = 0; info && info[2 * i] != NULL; i++) {
1318 if (!strcasecmp(opt, info[2 * i]))
1319 return info[2 * i + 1];
1322 return NULL;
1325 int demux_control(demuxer_t *demuxer, int cmd, void *arg)
1328 if (demuxer->desc->control)
1329 return demuxer->desc->control(demuxer, cmd, arg);
1331 return DEMUXER_CTRL_NOTIMPL;
1334 int demuxer_switch_audio(demuxer_t *demuxer, int index)
1336 int res = demux_control(demuxer, DEMUXER_CTRL_SWITCH_AUDIO, &index);
1337 if (res == DEMUXER_CTRL_NOTIMPL) {
1338 struct sh_audio *sh_audio = demuxer->audio->sh;
1339 return sh_audio ? sh_audio->aid : -2;
1341 if (demuxer->audio->id >= 0) {
1342 struct sh_audio *sh_audio = demuxer->a_streams[demuxer->audio->id];
1343 demuxer->audio->sh = sh_audio;
1344 index = sh_audio->aid; // internal MPEG demuxers don't set it right
1346 else
1347 demuxer->audio->sh = NULL;
1348 return index;
1351 int demuxer_switch_video(demuxer_t *demuxer, int index)
1353 int res = demux_control(demuxer, DEMUXER_CTRL_SWITCH_VIDEO, &index);
1354 if (res == DEMUXER_CTRL_NOTIMPL) {
1355 struct sh_video *sh_video = demuxer->video->sh;
1356 return sh_video ? sh_video->vid : -2;
1358 if (demuxer->video->id >= 0) {
1359 struct sh_video *sh_video = demuxer->v_streams[demuxer->video->id];
1360 demuxer->video->sh = sh_video;
1361 index = sh_video->vid; // internal MPEG demuxers don't set it right
1362 } else
1363 demuxer->video->sh = NULL;
1364 return index;
1367 int demuxer_add_attachment(demuxer_t *demuxer, struct bstr name,
1368 struct bstr type, struct bstr data)
1370 if (!(demuxer->num_attachments % 32))
1371 demuxer->attachments = talloc_realloc(demuxer, demuxer->attachments,
1372 struct demux_attachment,
1373 demuxer->num_attachments + 32);
1375 struct demux_attachment *att =
1376 demuxer->attachments + demuxer->num_attachments;
1377 att->name = talloc_strndup(demuxer->attachments, name.start, name.len);
1378 att->type = talloc_strndup(demuxer->attachments, type.start, type.len);
1379 att->data = talloc_size(demuxer->attachments, data.len);
1380 memcpy(att->data, data.start, data.len);
1381 att->data_size = data.len;
1383 return demuxer->num_attachments++;
1386 int demuxer_add_chapter(demuxer_t *demuxer, struct bstr name,
1387 uint64_t start, uint64_t end)
1389 if (!(demuxer->num_chapters % 32))
1390 demuxer->chapters = talloc_realloc(demuxer, demuxer->chapters,
1391 struct demux_chapter,
1392 demuxer->num_chapters + 32);
1394 demuxer->chapters[demuxer->num_chapters].start = start;
1395 demuxer->chapters[demuxer->num_chapters].end = end;
1396 demuxer->chapters[demuxer->num_chapters].name = name.len ?
1397 talloc_strndup(demuxer->chapters, name.start, name.len) :
1398 talloc_strdup(demuxer->chapters, mp_gtext("unknown"));
1400 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_ID=%d\n", demuxer->num_chapters);
1401 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_START=%"PRIu64"\n", demuxer->num_chapters, start / 1000000);
1402 if (end)
1403 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_END=%"PRIu64"\n", demuxer->num_chapters, end / 1000000);
1404 if (name.start)
1405 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_NAME=%.*s\n", demuxer->num_chapters, BSTR_P(name));
1407 return demuxer->num_chapters++;
1411 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1412 * either using the demuxer->chapters structure set by the demuxer
1413 * or asking help to the stream layer (e.g. dvd)
1414 * \param chapter - chapter number wished - 0-based
1415 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1416 * \param chapter_name name of chapter found (set by this function is param is not null)
1417 * \return -1 on error, current chapter if successful
1420 int demuxer_seek_chapter(demuxer_t *demuxer, int chapter, double *seek_pts,
1421 char **chapter_name)
1423 int ris;
1425 if (!demuxer->num_chapters || !demuxer->chapters) {
1426 demux_flush(demuxer);
1428 ris = stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_CHAPTER,
1429 &chapter);
1430 if (ris != STREAM_UNSUPPORTED)
1431 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1433 // exit status may be ok, but main() doesn't have to seek itself
1434 // (because e.g. dvds depend on sectors, not on pts)
1435 *seek_pts = -1.0;
1437 if (chapter_name) {
1438 *chapter_name = NULL;
1439 int num_chapters;
1440 if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS,
1441 &num_chapters) == STREAM_UNSUPPORTED)
1442 num_chapters = 0;
1443 if (num_chapters) {
1444 *chapter_name = talloc_size(NULL, 16);
1445 sprintf(*chapter_name, " of %3d", num_chapters);
1449 return ris != STREAM_UNSUPPORTED ? chapter : -1;
1450 } else { // chapters structure is set in the demuxer
1451 if (chapter >= demuxer->num_chapters)
1452 return -1;
1453 if (chapter < 0)
1454 chapter = 0;
1456 *seek_pts = demuxer->chapters[chapter].start / 1e9;
1458 if (chapter_name)
1459 *chapter_name = talloc_strdup(NULL, demuxer->chapters[chapter].name);
1461 return chapter;
1465 int demuxer_get_current_chapter(demuxer_t *demuxer, double time_now)
1467 int chapter = -2;
1468 if (!demuxer->num_chapters || !demuxer->chapters) {
1469 if (stream_control(demuxer->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
1470 &chapter) == STREAM_UNSUPPORTED)
1471 chapter = -2;
1472 } else {
1473 uint64_t now = time_now * 1e9 + 0.5;
1474 for (chapter = demuxer->num_chapters - 1; chapter >= 0; --chapter) {
1475 if (demuxer->chapters[chapter].start <= now)
1476 break;
1479 return chapter;
1482 char *demuxer_chapter_name(demuxer_t *demuxer, int chapter)
1484 if (demuxer->num_chapters && demuxer->chapters) {
1485 if (chapter >= 0 && chapter < demuxer->num_chapters
1486 && demuxer->chapters[chapter].name)
1487 return strdup(demuxer->chapters[chapter].name);
1489 return NULL;
1492 char *demuxer_chapter_display_name(demuxer_t *demuxer, int chapter)
1494 char *chapter_name = demuxer_chapter_name(demuxer, chapter);
1495 if (chapter_name) {
1496 char *tmp = malloc(strlen(chapter_name) + 14);
1497 snprintf(tmp, 63, "(%d) %s", chapter + 1, chapter_name);
1498 free(chapter_name);
1499 return tmp;
1500 } else {
1501 int chapter_num = demuxer_chapter_count(demuxer);
1502 char tmp[30];
1503 if (chapter_num <= 0)
1504 sprintf(tmp, "(%d)", chapter + 1);
1505 else
1506 sprintf(tmp, "(%d) of %d", chapter + 1, chapter_num);
1507 return strdup(tmp);
1511 float demuxer_chapter_time(demuxer_t *demuxer, int chapter, float *end)
1513 if (demuxer->num_chapters && demuxer->chapters && chapter >= 0
1514 && chapter < demuxer->num_chapters) {
1515 if (end)
1516 *end = demuxer->chapters[chapter].end / 1e9;
1517 return demuxer->chapters[chapter].start / 1e9;
1519 return -1.0;
1522 int demuxer_chapter_count(demuxer_t *demuxer)
1524 if (!demuxer->num_chapters || !demuxer->chapters) {
1525 int num_chapters = 0;
1526 if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS,
1527 &num_chapters) == STREAM_UNSUPPORTED)
1528 num_chapters = 0;
1529 return num_chapters;
1530 } else
1531 return demuxer->num_chapters;
1534 int demuxer_angles_count(demuxer_t *demuxer)
1536 int ris, angles = -1;
1538 ris = stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_ANGLES, &angles);
1539 if (ris == STREAM_UNSUPPORTED)
1540 return -1;
1541 return angles;
1544 int demuxer_get_current_angle(demuxer_t *demuxer)
1546 int ris, curr_angle = -1;
1547 ris = stream_control(demuxer->stream, STREAM_CTRL_GET_ANGLE, &curr_angle);
1548 if (ris == STREAM_UNSUPPORTED)
1549 return -1;
1550 return curr_angle;
1554 int demuxer_set_angle(demuxer_t *demuxer, int angle)
1556 int ris, angles = -1;
1558 angles = demuxer_angles_count(demuxer);
1559 if ((angles < 1) || (angle > angles))
1560 return -1;
1562 demux_flush(demuxer);
1564 ris = stream_control(demuxer->stream, STREAM_CTRL_SET_ANGLE, &angle);
1565 if (ris == STREAM_UNSUPPORTED)
1566 return -1;
1568 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1570 return angle;
1573 int demuxer_audio_track_by_lang_and_default(struct demuxer *d, char *lang)
1575 if (!lang)
1576 lang = "";
1577 while (1) {
1578 lang += strspn(lang, ",");
1579 int len = strcspn(lang, ",");
1580 int id = -1;
1581 for (int i = 0; i < MAX_A_STREAMS; i++) {
1582 struct sh_audio *sh = d->a_streams[i];
1583 if (sh && (!len || sh->lang && strlen(sh->lang) == len &&
1584 !memcmp(lang, sh->lang, len))) {
1585 if (sh->default_track)
1586 return sh->aid;
1587 if (id < 0)
1588 id = sh->aid;
1591 if (id >= 0)
1592 return id;
1593 if (!len)
1594 return -1;
1595 lang += len;
1599 int demuxer_sub_track_by_lang_and_default(struct demuxer *d, char *lang)
1601 if (!lang)
1602 lang = "";
1603 while (1) {
1604 lang += strspn(lang, ",");
1605 int len = strcspn(lang, ",");
1606 int id = -1;
1607 for (int i = 0; i < MAX_S_STREAMS; i++) {
1608 struct sh_sub *sh = d->s_streams[i];
1609 if (sh && (!len || sh->lang && strlen(sh->lang) == len &&
1610 !memcmp(lang, sh->lang, len))) {
1611 if (sh->default_track)
1612 return sh->sid;
1613 if (id < 0)
1614 id = sh->sid;
1617 if (!len)
1618 return -1;
1619 if (id >= 0)
1620 return id;
1621 lang += len;