configure: fix disabling of (x)mga without swscale internals
[mplayer/glamo.git] / libmpdemux / demuxer.c
blob71e75e2efbbda3bcc108c6a104d7dfac9f6ae44e
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"
45 #include "ass_mp.h"
47 #ifdef CONFIG_LIBAVCODEC
48 #include "libavcodec/avcodec.h"
49 #if MP_INPUT_BUFFER_PADDING_SIZE < FF_INPUT_BUFFER_PADDING_SIZE
50 #error MP_INPUT_BUFFER_PADDING_SIZE is too small!
51 #endif
52 #endif
54 // This is quite experimental, in particular it will mess up the pts values
55 // in the queue - on the other hand it might fix some issues like generating
56 // broken files with mencoder and stream copy.
57 // Better leave it disabled for now, if we find no use for it this code should
58 // just be removed again.
59 #define PARSE_ON_ADD 0
61 static void clear_parser(sh_common_t *sh);
63 // Demuxer list
64 extern const demuxer_desc_t demuxer_desc_rawaudio;
65 extern const demuxer_desc_t demuxer_desc_rawvideo;
66 extern const demuxer_desc_t demuxer_desc_tv;
67 extern const demuxer_desc_t demuxer_desc_mf;
68 extern const demuxer_desc_t demuxer_desc_avi;
69 extern const demuxer_desc_t demuxer_desc_y4m;
70 extern const demuxer_desc_t demuxer_desc_asf;
71 extern const demuxer_desc_t demuxer_desc_real;
72 extern const demuxer_desc_t demuxer_desc_smjpeg;
73 extern const demuxer_desc_t demuxer_desc_matroska;
74 extern const demuxer_desc_t demuxer_desc_realaudio;
75 extern const demuxer_desc_t demuxer_desc_vqf;
76 extern const demuxer_desc_t demuxer_desc_mov;
77 extern const demuxer_desc_t demuxer_desc_vivo;
78 extern const demuxer_desc_t demuxer_desc_fli;
79 extern const demuxer_desc_t demuxer_desc_film;
80 extern const demuxer_desc_t demuxer_desc_roq;
81 extern const demuxer_desc_t demuxer_desc_gif;
82 extern const demuxer_desc_t demuxer_desc_ogg;
83 extern const demuxer_desc_t demuxer_desc_avs;
84 extern const demuxer_desc_t demuxer_desc_pva;
85 extern const demuxer_desc_t demuxer_desc_nsv;
86 extern const demuxer_desc_t demuxer_desc_mpeg_ts;
87 extern const demuxer_desc_t demuxer_desc_lmlm4;
88 extern const demuxer_desc_t demuxer_desc_mpeg_ps;
89 extern const demuxer_desc_t demuxer_desc_mpeg_pes;
90 extern const demuxer_desc_t demuxer_desc_mpeg_es;
91 extern const demuxer_desc_t demuxer_desc_mpeg_gxf;
92 extern const demuxer_desc_t demuxer_desc_mpeg4_es;
93 extern const demuxer_desc_t demuxer_desc_h264_es;
94 extern const demuxer_desc_t demuxer_desc_rawdv;
95 extern const demuxer_desc_t demuxer_desc_mpc;
96 extern const demuxer_desc_t demuxer_desc_audio;
97 extern const demuxer_desc_t demuxer_desc_xmms;
98 extern const demuxer_desc_t demuxer_desc_mpeg_ty;
99 extern const demuxer_desc_t demuxer_desc_rtp;
100 extern const demuxer_desc_t demuxer_desc_rtp_nemesi;
101 extern const demuxer_desc_t demuxer_desc_lavf;
102 extern const demuxer_desc_t demuxer_desc_lavf_preferred;
103 extern const demuxer_desc_t demuxer_desc_aac;
104 extern const demuxer_desc_t demuxer_desc_nut;
105 extern const demuxer_desc_t demuxer_desc_mng;
107 /* Please do not add any new demuxers here. If you want to implement a new
108 * demuxer, add it to libavformat, except for wrappers around external
109 * libraries and demuxers requiring binary support. */
111 const demuxer_desc_t *const demuxer_list[] = {
112 &demuxer_desc_rawaudio,
113 &demuxer_desc_rawvideo,
114 #ifdef CONFIG_TV
115 &demuxer_desc_tv,
116 #endif
117 &demuxer_desc_mf,
118 #ifdef CONFIG_LIBAVFORMAT
119 &demuxer_desc_lavf_preferred,
120 #endif
121 &demuxer_desc_avi,
122 &demuxer_desc_y4m,
123 &demuxer_desc_asf,
124 &demuxer_desc_nsv,
125 &demuxer_desc_real,
126 &demuxer_desc_smjpeg,
127 &demuxer_desc_matroska,
128 &demuxer_desc_realaudio,
129 &demuxer_desc_vqf,
130 &demuxer_desc_mov,
131 &demuxer_desc_vivo,
132 &demuxer_desc_fli,
133 &demuxer_desc_film,
134 &demuxer_desc_roq,
135 #ifdef CONFIG_GIF
136 &demuxer_desc_gif,
137 #endif
138 #ifdef CONFIG_OGGVORBIS
139 &demuxer_desc_ogg,
140 #endif
141 #ifdef CONFIG_WIN32DLL
142 &demuxer_desc_avs,
143 #endif
144 &demuxer_desc_pva,
145 &demuxer_desc_mpeg_ts,
146 &demuxer_desc_lmlm4,
147 &demuxer_desc_mpeg_ps,
148 &demuxer_desc_mpeg_pes,
149 &demuxer_desc_mpeg_es,
150 &demuxer_desc_mpeg_gxf,
151 &demuxer_desc_mpeg4_es,
152 &demuxer_desc_h264_es,
153 &demuxer_desc_audio,
154 &demuxer_desc_mpeg_ty,
155 #ifdef CONFIG_LIVE555
156 &demuxer_desc_rtp,
157 #endif
158 #ifdef CONFIG_LIBNEMESI
159 &demuxer_desc_rtp_nemesi,
160 #endif
161 #ifdef CONFIG_LIBAVFORMAT
162 &demuxer_desc_lavf,
163 #endif
164 #ifdef CONFIG_MUSEPACK
165 &demuxer_desc_mpc,
166 #endif
167 #ifdef CONFIG_LIBDV095
168 &demuxer_desc_rawdv,
169 #endif
170 &demuxer_desc_aac,
171 #ifdef CONFIG_LIBNUT
172 &demuxer_desc_nut,
173 #endif
174 #ifdef CONFIG_XMMS
175 &demuxer_desc_xmms,
176 #endif
177 #ifdef CONFIG_MNG
178 &demuxer_desc_mng,
179 #endif
180 /* Please do not add any new demuxers here. If you want to implement a new
181 * demuxer, add it to libavformat, except for wrappers around external
182 * libraries and demuxers requiring binary support. */
183 NULL
186 void free_demuxer_stream(demux_stream_t *ds)
188 ds_free_packs(ds);
189 free(ds);
192 demux_stream_t *new_demuxer_stream(struct demuxer *demuxer, int id)
194 demux_stream_t *ds = malloc(sizeof(demux_stream_t));
195 *ds = (demux_stream_t){
196 .id = id,
197 .demuxer = demuxer,
198 .asf_seq = -1,
200 return ds;
205 * Get demuxer description structure for a given demuxer type
207 * @param file_format type of the demuxer
208 * @return structure for the demuxer, NULL if not found
210 static const demuxer_desc_t *get_demuxer_desc_from_type(int file_format)
212 int i;
214 for (i = 0; demuxer_list[i]; i++)
215 if (file_format == demuxer_list[i]->type)
216 return demuxer_list[i];
218 return NULL;
222 demuxer_t *new_demuxer(struct MPOpts *opts, stream_t *stream, int type,
223 int a_id, int v_id, int s_id, char *filename)
225 struct demuxer *d = talloc_zero(NULL, struct demuxer);
226 d->stream = stream;
227 d->stream_pts = MP_NOPTS_VALUE;
228 d->reference_clock = MP_NOPTS_VALUE;
229 d->movi_start = stream->start_pos;
230 d->movi_end = stream->end_pos;
231 d->seekable = 1;
232 d->synced = 0;
233 d->filepos = -1;
234 d->audio = new_demuxer_stream(d, a_id);
235 d->video = new_demuxer_stream(d, v_id);
236 d->sub = new_demuxer_stream(d, s_id);
237 d->type = type;
238 d->opts = opts;
239 if (type)
240 if (!(d->desc = get_demuxer_desc_from_type(type)))
241 mp_msg(MSGT_DEMUXER, MSGL_ERR,
242 "BUG! Invalid demuxer type in new_demuxer(), "
243 "big troubles ahead.");
244 if (filename) // Filename hack for avs_check_file
245 d->filename = strdup(filename);
246 stream_seek(stream, stream->start_pos);
247 return d;
250 sh_sub_t *new_sh_sub_sid(demuxer_t *demuxer, int id, int sid)
252 if (id > MAX_S_STREAMS - 1 || id < 0) {
253 mp_msg(MSGT_DEMUXER, MSGL_WARN,
254 "Requested sub stream id overflow (%d > %d)\n", id,
255 MAX_S_STREAMS);
256 return NULL;
258 if (demuxer->s_streams[id])
259 mp_msg(MSGT_DEMUXER, MSGL_WARN, "Sub stream %i redefined\n", id);
260 else {
261 sh_sub_t *sh = calloc(1, sizeof(sh_sub_t));
262 demuxer->s_streams[id] = sh;
263 sh->sid = sid;
264 sh->opts = demuxer->opts;
265 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SUBTITLE_ID=%d\n", sid);
267 if (sid == demuxer->opts->sub_id) {
268 demuxer->sub->id = id;
269 demuxer->sub->sh = demuxer->s_streams[id];
271 return demuxer->s_streams[id];
274 static void free_sh_sub(sh_sub_t *sh)
276 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_sub at %p\n", sh);
277 free(sh->extradata);
278 #ifdef CONFIG_ASS
279 if (sh->ass_track)
280 ass_free_track(sh->ass_track);
281 #endif
282 free(sh->lang);
283 #ifdef CONFIG_LIBAVCODEC
284 clear_parser((sh_common_t *)sh);
285 #endif
286 free(sh);
289 sh_audio_t *new_sh_audio_aid(demuxer_t *demuxer, int id, int aid)
291 if (id > MAX_A_STREAMS - 1 || id < 0) {
292 mp_msg(MSGT_DEMUXER, MSGL_WARN,
293 "Requested audio stream id overflow (%d > %d)\n", id,
294 MAX_A_STREAMS);
295 return NULL;
297 if (demuxer->a_streams[id]) {
298 mp_tmsg(MSGT_DEMUXER, MSGL_WARN, "WARNING: Audio stream header %d redefined.\n", id);
299 } else {
300 mp_tmsg(MSGT_DEMUXER, MSGL_V, "==> Found audio stream: %d\n", id);
301 sh_audio_t *sh = calloc(1, sizeof(sh_audio_t));
302 demuxer->a_streams[id] = sh;
303 sh->aid = aid;
304 sh->ds = demuxer->audio;
305 // set some defaults
306 sh->samplesize = 2;
307 sh->sample_format = AF_FORMAT_S16_NE;
308 sh->opts = demuxer->opts;
309 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_ID=%d\n", aid);
311 return demuxer->a_streams[id];
314 void free_sh_audio(demuxer_t *demuxer, int id)
316 sh_audio_t *sh = demuxer->a_streams[id];
317 demuxer->a_streams[id] = NULL;
318 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_audio at %p\n", sh);
319 free(sh->wf);
320 free(sh->codecdata);
321 free(sh->lang);
322 #ifdef CONFIG_LIBAVCODEC
323 clear_parser((sh_common_t *)sh);
324 #endif
325 free(sh);
328 sh_video_t *new_sh_video_vid(demuxer_t *demuxer, int id, int vid)
330 if (id > MAX_V_STREAMS - 1 || id < 0) {
331 mp_msg(MSGT_DEMUXER, MSGL_WARN,
332 "Requested video stream id overflow (%d > %d)\n", id,
333 MAX_V_STREAMS);
334 return NULL;
336 if (demuxer->v_streams[id])
337 mp_tmsg(MSGT_DEMUXER, MSGL_WARN, "WARNING: Video stream header %d redefined.\n", id);
338 else {
339 mp_tmsg(MSGT_DEMUXER, MSGL_V, "==> Found video stream: %d\n", id);
340 sh_video_t *sh = calloc(1, sizeof *sh);
341 demuxer->v_streams[id] = sh;
342 sh->vid = vid;
343 sh->ds = demuxer->video;
344 sh->opts = demuxer->opts;
345 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_ID=%d\n", vid);
347 return demuxer->v_streams[id];
350 void free_sh_video(sh_video_t *sh)
352 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_video at %p\n", sh);
353 free(sh->bih);
354 #ifdef CONFIG_LIBAVCODEC
355 clear_parser((sh_common_t *)sh);
356 #endif
357 free(sh);
360 void free_demuxer(demuxer_t *demuxer)
362 int i;
363 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing %s demuxer at %p\n",
364 demuxer->desc->shortdesc, demuxer);
365 if (demuxer->desc->close)
366 demuxer->desc->close(demuxer);
367 // Very ugly hack to make it behave like old implementation
368 if (demuxer->desc->type == DEMUXER_TYPE_DEMUXERS)
369 goto skip_streamfree;
370 // free streams:
371 for (i = 0; i < MAX_A_STREAMS; i++)
372 if (demuxer->a_streams[i])
373 free_sh_audio(demuxer, i);
374 for (i = 0; i < MAX_V_STREAMS; i++)
375 if (demuxer->v_streams[i])
376 free_sh_video(demuxer->v_streams[i]);
377 for (i = 0; i < MAX_S_STREAMS; i++)
378 if (demuxer->s_streams[i])
379 free_sh_sub(demuxer->s_streams[i]);
380 // free demuxers:
381 free_demuxer_stream(demuxer->audio);
382 free_demuxer_stream(demuxer->video);
383 free_demuxer_stream(demuxer->sub);
384 skip_streamfree:
385 free(demuxer->filename);
386 if (demuxer->teletext)
387 teletext_control(demuxer->teletext, TV_VBI_CONTROL_STOP, NULL);
388 talloc_free(demuxer);
392 static void ds_add_packet_internal(demux_stream_t *ds, demux_packet_t *dp)
394 // append packet to DS stream:
395 ++ds->packs;
396 ds->bytes += dp->len;
397 if (ds->last) {
398 // next packet in stream
399 ds->last->next = dp;
400 ds->last = dp;
401 } else {
402 // first packet in stream
403 ds->first = ds->last = dp;
405 mp_dbg(MSGT_DEMUXER, MSGL_DBG2,
406 "DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
407 (ds == ds->demuxer->audio) ? "d_audio" : "d_video", dp->len,
408 dp->pts, (unsigned int) dp->pos, ds->demuxer->audio->packs,
409 ds->demuxer->video->packs);
412 #ifdef CONFIG_LIBAVCODEC
413 static void allocate_parser(AVCodecContext **avctx, AVCodecParserContext **parser, unsigned format)
415 enum CodecID codec_id = CODEC_ID_NONE;
416 extern int avcodec_initialized;
417 if (!avcodec_initialized) {
418 avcodec_init();
419 avcodec_register_all();
420 avcodec_initialized = 1;
422 switch (format) {
423 case 0x2000:
424 case 0x332D6361:
425 case 0x332D4341:
426 case MKTAG('d', 'n', 'e', 't'):
427 case MKTAG('s', 'a', 'c', '3'):
428 codec_id = CODEC_ID_AC3;
429 break;
430 case MKTAG('E', 'A', 'C', '3'):
431 codec_id = CODEC_ID_EAC3;
432 break;
433 case 0x2001:
434 case 0x86:
435 codec_id = CODEC_ID_DTS;
436 break;
437 case MKTAG('M', 'L', 'P', ' '):
438 codec_id = CODEC_ID_MLP;
439 break;
440 case 0x55:
441 case 0x5500736d:
442 case MKTAG('.', 'm', 'p', '3'):
443 case MKTAG('M', 'P', 'E', ' '):
444 case MKTAG('L', 'A', 'M', 'E'):
445 codec_id = CODEC_ID_MP3;
446 break;
447 case 0x50:
448 case MKTAG('.', 'm', 'p', '2'):
449 case MKTAG('.', 'm', 'p', '1'):
450 codec_id = CODEC_ID_MP2;
451 break;
452 case MKTAG('T', 'R', 'H', 'D'):
453 codec_id = CODEC_ID_TRUEHD;
454 break;
456 if (codec_id != CODEC_ID_NONE) {
457 *avctx = avcodec_alloc_context();
458 if (!*avctx)
459 return;
460 *parser = av_parser_init(codec_id);
461 if (!*parser)
462 av_freep(avctx);
466 static void get_parser(sh_common_t *sh, AVCodecContext **avctx, AVCodecParserContext **parser)
468 *avctx = NULL;
469 *parser = NULL;
471 if (!sh || !sh->needs_parsing)
472 return;
474 *avctx = sh->avctx;
475 *parser = sh->parser;
476 if (*parser)
477 return;
479 allocate_parser(avctx, parser, sh->format);
480 sh->avctx = *avctx;
481 sh->parser = *parser;
484 int ds_parse(demux_stream_t *ds, uint8_t **buffer, int *len, double pts, off_t pos)
486 AVCodecContext *avctx;
487 AVCodecParserContext *parser;
488 get_parser(ds->sh, &avctx, &parser);
489 if (!parser)
490 return *len;
491 return av_parser_parse2(parser, avctx, buffer, len, *buffer, *len, pts, pts, pos);
494 static void clear_parser(sh_common_t *sh)
496 av_parser_close(sh->parser);
497 sh->parser = NULL;
498 av_freep(&sh->avctx);
501 void ds_clear_parser(demux_stream_t *ds)
503 if (!ds->sh)
504 return;
505 clear_parser(ds->sh);
507 #endif
509 void ds_add_packet(demux_stream_t *ds, demux_packet_t *dp)
511 #if PARSE_ON_ADD && defined(CONFIG_LIBAVCODEC)
512 int len = dp->len;
513 int pos = 0;
514 while (len > 0) {
515 uint8_t *parsed_start = dp->buffer + pos;
516 int parsed_len = len;
517 int consumed = ds_parse(ds->sh, &parsed_start, &parsed_len, dp->pts, dp->pos);
518 pos += consumed;
519 len -= consumed;
520 if (parsed_start == dp->buffer && parsed_len == dp->len) {
521 ds_add_packet_internal(ds, dp);
522 } else if (parsed_len) {
523 demux_packet_t *dp2 = new_demux_packet(parsed_len);
524 dp2->pos = dp->pos;
525 dp2->pts = dp->pts; // should be parser->pts but that works badly
526 memcpy(dp2->buffer, parsed_start, parsed_len);
527 ds_add_packet_internal(ds, dp2);
530 #else
531 ds_add_packet_internal(ds, dp);
532 #endif
535 void ds_read_packet(demux_stream_t *ds, stream_t *stream, int len,
536 double pts, off_t pos, int flags)
538 demux_packet_t *dp = new_demux_packet(len);
539 len = stream_read(stream, dp->buffer, len);
540 resize_demux_packet(dp, len);
541 dp->pts = pts;
542 dp->pos = pos;
543 dp->flags = flags;
544 // append packet to DS stream:
545 ds_add_packet(ds, dp);
548 // return value:
549 // 0 = EOF or no stream found or invalid type
550 // 1 = successfully read a packet
552 int demux_fill_buffer(demuxer_t *demux, demux_stream_t *ds)
554 // Note: parameter 'ds' can be NULL!
555 return demux->desc->fill_buffer(demux, ds);
558 // return value:
559 // 0 = EOF
560 // 1 = successful
561 #define MAX_ACUMULATED_PACKETS 64
562 int ds_fill_buffer(demux_stream_t *ds)
564 demuxer_t *demux = ds->demuxer;
565 if (ds->current)
566 free_demux_packet(ds->current);
567 ds->current = NULL;
568 if (mp_msg_test(MSGT_DEMUXER, MSGL_DBG3)) {
569 if (ds == demux->audio)
570 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
571 "ds_fill_buffer(d_audio) called\n");
572 else if (ds == demux->video)
573 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
574 "ds_fill_buffer(d_video) called\n");
575 else if (ds == demux->sub)
576 mp_dbg(MSGT_DEMUXER, MSGL_DBG3, "ds_fill_buffer(d_sub) called\n");
577 else
578 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
579 "ds_fill_buffer(unknown 0x%X) called\n", (unsigned int) ds);
581 while (1) {
582 if (ds->packs) {
583 demux_packet_t *p = ds->first;
584 #if 0
585 if (demux->reference_clock != MP_NOPTS_VALUE) {
586 if ( p->pts != MP_NOPTS_VALUE
587 && p->pts > demux->reference_clock
588 && ds->packs < MAX_ACUMULATED_PACKETS) {
589 if (demux_fill_buffer(demux, ds))
590 continue;
593 #endif
594 // copy useful data:
595 ds->buffer = p->buffer;
596 ds->buffer_pos = 0;
597 ds->buffer_size = p->len;
598 ds->pos = p->pos;
599 ds->dpos += p->len; // !!!
600 ++ds->pack_no;
601 if (p->pts != MP_NOPTS_VALUE) {
602 ds->pts = p->pts;
603 ds->pts_bytes = 0;
605 ds->pts_bytes += p->len; // !!!
606 if (p->stream_pts != MP_NOPTS_VALUE)
607 demux->stream_pts = p->stream_pts;
608 ds->flags = p->flags;
609 // unlink packet:
610 ds->bytes -= p->len;
611 ds->current = p;
612 ds->first = p->next;
613 if (!ds->first)
614 ds->last = NULL;
615 --ds->packs;
616 /* The code below can set ds->eof to 1 when another stream runs
617 * out of buffer space. That makes sense because in that situation
618 * the calling code should not count on being able to demux more
619 * packets from this stream.
620 * If however the situation improves and we're called again
621 * despite the eof flag then it's better to clear it to avoid
622 * weird behavior. */
623 ds->eof = 0;
624 return 1;
627 #define MaybeNI _("Maybe you are playing a non-interleaved stream/file or the codec failed?\n" \
628 "For AVI files, try to force non-interleaved mode with the -ni option.\n")
630 if (demux->audio->packs >= MAX_PACKS
631 || demux->audio->bytes >= MAX_PACK_BYTES) {
632 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
633 demux->audio->packs, demux->audio->bytes);
634 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
635 break;
637 if (demux->video->packs >= MAX_PACKS
638 || demux->video->bytes >= MAX_PACK_BYTES) {
639 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
640 demux->video->packs, demux->video->bytes);
641 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
642 break;
644 if (!demux_fill_buffer(demux, ds)) {
645 #if PARSE_ON_ADD && defined(CONFIG_LIBAVCODEC)
646 uint8_t *parsed_start = NULL;
647 int parsed_len = 0;
648 ds_parse(ds->sh, &parsed_start, &parsed_len, MP_NOPTS_VALUE, 0);
649 if (parsed_len) {
650 demux_packet_t *dp2 = new_demux_packet(parsed_len);
651 dp2->pts = MP_NOPTS_VALUE;
652 memcpy(dp2->buffer, parsed_start, parsed_len);
653 ds_add_packet_internal(ds, dp2);
654 continue;
656 #endif
657 mp_dbg(MSGT_DEMUXER, MSGL_DBG2,
658 "ds_fill_buffer()->demux_fill_buffer() failed\n");
659 break; // EOF
662 ds->buffer_pos = ds->buffer_size = 0;
663 ds->buffer = NULL;
664 mp_msg(MSGT_DEMUXER, MSGL_V,
665 "ds_fill_buffer: EOF reached (stream: %s) \n",
666 ds == demux->audio ? "audio" : "video");
667 ds->eof = 1;
668 return 0;
671 int demux_read_data(demux_stream_t *ds, unsigned char *mem, int len)
673 int x;
674 int bytes = 0;
675 while (len > 0) {
676 x = ds->buffer_size - ds->buffer_pos;
677 if (x == 0) {
678 if (!ds_fill_buffer(ds))
679 return bytes;
680 } else {
681 if (x > len)
682 x = len;
683 if (mem)
684 fast_memcpy(mem + bytes, &ds->buffer[ds->buffer_pos], x);
685 bytes += x;
686 len -= x;
687 ds->buffer_pos += x;
690 return bytes;
694 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
695 * \param mem memory to read data into, may be NULL to discard data
696 * \param maxlen maximum number of bytes to read
697 * \param read number of bytes actually read
698 * \param pattern pattern to search for (lowest 8 bits are ignored)
699 * \return whether pattern was found
701 int demux_pattern_3(demux_stream_t *ds, unsigned char *mem, int maxlen,
702 int *read, uint32_t pattern)
704 register uint32_t head = 0xffffff00;
705 register uint32_t pat = pattern & 0xffffff00;
706 int total_len = 0;
707 do {
708 register unsigned char *ds_buf = &ds->buffer[ds->buffer_size];
709 int len = ds->buffer_size - ds->buffer_pos;
710 register long pos = -len;
711 if (unlikely(pos >= 0)) { // buffer is empty
712 ds_fill_buffer(ds);
713 continue;
715 do {
716 head |= ds_buf[pos];
717 head <<= 8;
718 } while (++pos && head != pat);
719 len += pos;
720 if (total_len + len > maxlen)
721 len = maxlen - total_len;
722 len = demux_read_data(ds, mem ? &mem[total_len] : NULL, len);
723 total_len += len;
724 } while ((head != pat || total_len < 3) && total_len < maxlen && !ds->eof);
725 if (read)
726 *read = total_len;
727 return total_len >= 3 && head == pat;
730 void ds_free_packs(demux_stream_t *ds)
732 demux_packet_t *dp = ds->first;
733 while (dp) {
734 demux_packet_t *dn = dp->next;
735 free_demux_packet(dp);
736 dp = dn;
738 if (ds->asf_packet) {
739 // free unfinished .asf fragments:
740 free(ds->asf_packet->buffer);
741 free(ds->asf_packet);
742 ds->asf_packet = NULL;
744 ds->first = ds->last = NULL;
745 ds->packs = 0; // !!!!!
746 ds->bytes = 0;
747 if (ds->current)
748 free_demux_packet(ds->current);
749 ds->current = NULL;
750 ds->buffer = NULL;
751 ds->buffer_pos = ds->buffer_size;
752 ds->pts = 0;
753 ds->pts_bytes = 0;
756 int ds_get_packet(demux_stream_t *ds, unsigned char **start)
758 int len;
759 if (ds->buffer_pos >= ds->buffer_size) {
760 if (!ds_fill_buffer(ds)) {
761 // EOF
762 *start = NULL;
763 return -1;
766 len = ds->buffer_size - ds->buffer_pos;
767 *start = &ds->buffer[ds->buffer_pos];
768 ds->buffer_pos += len;
769 return len;
772 int ds_get_packet_pts(demux_stream_t *ds, unsigned char **start, double *pts)
774 int len;
775 *pts = MP_NOPTS_VALUE;
776 len = ds_get_packet(ds, start);
777 if (len < 0)
778 return len;
779 // Return pts unless this read starts from the middle of a packet
780 if (len == ds->buffer_pos)
781 *pts = ds->current->pts;
782 return len;
785 int ds_get_packet_sub(demux_stream_t *ds, unsigned char **start)
787 int len;
788 if (ds->buffer_pos >= ds->buffer_size) {
789 *start = NULL;
790 if (!ds->packs)
791 return -1; // no sub
792 if (!ds_fill_buffer(ds))
793 return -1; // EOF
795 len = ds->buffer_size - ds->buffer_pos;
796 *start = &ds->buffer[ds->buffer_pos];
797 ds->buffer_pos += len;
798 return len;
801 double ds_get_next_pts(demux_stream_t *ds)
803 demuxer_t *demux = ds->demuxer;
804 while (!ds->first) {
805 if (demux->audio->packs >= MAX_PACKS
806 || demux->audio->bytes >= MAX_PACK_BYTES) {
807 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
808 demux->audio->packs, demux->audio->bytes);
809 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
810 return MP_NOPTS_VALUE;
812 if (demux->video->packs >= MAX_PACKS
813 || demux->video->bytes >= MAX_PACK_BYTES) {
814 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
815 demux->video->packs, demux->video->bytes);
816 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
817 return MP_NOPTS_VALUE;
819 if (!demux_fill_buffer(demux, ds))
820 return MP_NOPTS_VALUE;
822 return ds->first->pts;
825 // ====================================================================
827 void demuxer_help(void)
829 int i;
831 mp_msg(MSGT_DEMUXER, MSGL_INFO, "Available demuxers:\n");
832 mp_msg(MSGT_DEMUXER, MSGL_INFO, " demuxer: type info: (comment)\n");
833 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DEMUXERS\n");
834 for (i = 0; demuxer_list[i]; i++) {
835 if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Don't display special demuxers
836 continue;
837 if (demuxer_list[i]->comment && strlen(demuxer_list[i]->comment))
838 mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s (%s)\n",
839 demuxer_list[i]->name, demuxer_list[i]->type,
840 demuxer_list[i]->info, demuxer_list[i]->comment);
841 else
842 mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s\n",
843 demuxer_list[i]->name, demuxer_list[i]->type,
844 demuxer_list[i]->info);
850 * Get demuxer type for a given demuxer name
852 * @param demuxer_name string with demuxer name of demuxer number
853 * @param force will be set if demuxer should be forced.
854 * May be NULL.
855 * @return DEMUXER_TYPE_xxx, -1 if error or not found
857 int get_demuxer_type_from_name(char *demuxer_name, int *force)
859 int i;
860 long type_int;
861 char *endptr;
863 if (!demuxer_name || !demuxer_name[0])
864 return DEMUXER_TYPE_UNKNOWN;
865 if (force)
866 *force = demuxer_name[0] == '+';
867 if (demuxer_name[0] == '+')
868 demuxer_name = &demuxer_name[1];
869 for (i = 0; demuxer_list[i]; i++) {
870 if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Can't select special demuxers from commandline
871 continue;
872 if (strcmp(demuxer_name, demuxer_list[i]->name) == 0)
873 return demuxer_list[i]->type;
876 // No match found, try to parse name as an integer (demuxer number)
877 type_int = strtol(demuxer_name, &endptr, 0);
878 if (*endptr) // Conversion failed
879 return -1;
880 if ((type_int > 0) && (type_int <= DEMUXER_TYPE_MAX))
881 return (int) type_int;
883 return -1;
886 int extension_parsing = 1; // 0=off 1=mixed (used only for unstable formats)
889 NOTE : Several demuxers may be opened at the same time so
890 demuxers should NEVER rely on an external var to enable them
891 self. If a demuxer can't do any auto-detection it should only use
892 file_format. The user can explicitly set file_format with the -demuxer
893 option so there is really no need for another extra var.
894 For convenience an option can be added to set file_format directly
895 to the right type (ex: rawaudio,rawvideo).
896 Also the stream can override the file_format so a demuxer which rely
897 on a special stream type can set file_format at the stream level
898 (ex: tv,mf).
901 static demuxer_t *demux_open_stream(struct MPOpts *opts, stream_t *stream,
902 int file_format, int force, int audio_id,
903 int video_id, int dvdsub_id,
904 char *filename)
906 demuxer_t *demuxer = NULL;
908 sh_video_t *sh_video = NULL;
910 const demuxer_desc_t *demuxer_desc;
911 int fformat = 0;
912 int i;
914 // If somebody requested a demuxer check it
915 if (file_format) {
916 if ((demuxer_desc = get_demuxer_desc_from_type(file_format))) {
917 demuxer = new_demuxer(opts, stream, demuxer_desc->type, audio_id,
918 video_id, dvdsub_id, filename);
919 if (demuxer_desc->check_file)
920 fformat = demuxer_desc->check_file(demuxer);
921 if (force || !demuxer_desc->check_file)
922 fformat = demuxer_desc->type;
923 if (fformat != 0) {
924 if (fformat == demuxer_desc->type) {
925 demuxer_t *demux2 = demuxer;
926 // Move messages to demuxer detection code?
927 mp_tmsg(MSGT_DEMUXER, MSGL_INFO,
928 "%s file format detected.\n",
929 demuxer_desc->shortdesc);
930 file_format = fformat;
931 if (!demuxer->desc->open
932 || (demux2 = demuxer->desc->open(demuxer))) {
933 demuxer = demux2;
934 goto dmx_open;
936 } else {
937 // Format changed after check, recurse
938 free_demuxer(demuxer);
939 return demux_open_stream(opts, stream, fformat, force,
940 audio_id, video_id, dvdsub_id,
941 filename);
944 // Check failed for forced demuxer, quit
945 free_demuxer(demuxer);
946 return NULL;
949 // Test demuxers with safe file checks
950 for (i = 0; (demuxer_desc = demuxer_list[i]); i++) {
951 if (demuxer_desc->safe_check) {
952 demuxer = new_demuxer(opts, stream, demuxer_desc->type, audio_id,
953 video_id, dvdsub_id, filename);
954 if ((fformat = demuxer_desc->check_file(demuxer)) != 0) {
955 if (fformat == demuxer_desc->type) {
956 demuxer_t *demux2 = demuxer;
957 mp_tmsg(MSGT_DEMUXER, MSGL_INFO,
958 "%s file format detected.\n",
959 demuxer_desc->shortdesc);
960 file_format = fformat;
961 if (!demuxer->desc->open
962 || (demux2 = demuxer->desc->open(demuxer))) {
963 demuxer = demux2;
964 goto dmx_open;
966 } else {
967 if (fformat == DEMUXER_TYPE_PLAYLIST)
968 return demuxer; // handled in mplayer.c
969 // Format changed after check, recurse
970 free_demuxer(demuxer);
971 demuxer = demux_open_stream(opts, stream, fformat, force,
972 audio_id, video_id,
973 dvdsub_id, filename);
974 if (demuxer)
975 return demuxer; // done!
976 file_format = DEMUXER_TYPE_UNKNOWN;
979 free_demuxer(demuxer);
980 demuxer = NULL;
984 // If no forced demuxer perform file extension based detection
985 // Ok. We're over the stable detectable fileformats, the next ones are
986 // a bit fuzzy. So by default (extension_parsing==1) try extension-based
987 // detection first:
988 if (file_format == DEMUXER_TYPE_UNKNOWN && filename
989 && extension_parsing == 1) {
990 file_format = demuxer_type_by_filename(filename);
991 if (file_format != DEMUXER_TYPE_UNKNOWN) {
992 // we like recursion :)
993 demuxer = demux_open_stream(opts, stream, file_format, force,
994 audio_id, video_id, dvdsub_id,
995 filename);
996 if (demuxer)
997 return demuxer; // done!
998 file_format = DEMUXER_TYPE_UNKNOWN; // continue fuzzy guessing...
999 mp_msg(MSGT_DEMUXER, MSGL_V,
1000 "demuxer: continue fuzzy content-based format guessing...\n");
1003 // Try detection for all other demuxers
1004 for (i = 0; (demuxer_desc = demuxer_list[i]); i++) {
1005 if (!demuxer_desc->safe_check && demuxer_desc->check_file) {
1006 demuxer = new_demuxer(opts, stream, demuxer_desc->type, audio_id,
1007 video_id, dvdsub_id, filename);
1008 if ((fformat = demuxer_desc->check_file(demuxer)) != 0) {
1009 if (fformat == demuxer_desc->type) {
1010 demuxer_t *demux2 = demuxer;
1011 mp_tmsg(MSGT_DEMUXER, MSGL_INFO,
1012 "%s file format detected.\n",
1013 demuxer_desc->shortdesc);
1014 file_format = fformat;
1015 if (!demuxer->desc->open
1016 || (demux2 = demuxer->desc->open(demuxer))) {
1017 demuxer = demux2;
1018 goto dmx_open;
1020 } else {
1021 if (fformat == DEMUXER_TYPE_PLAYLIST)
1022 return demuxer; // handled in mplayer.c
1023 // Format changed after check, recurse
1024 free_demuxer(demuxer);
1025 demuxer = demux_open_stream(opts, stream, fformat, force,
1026 audio_id, video_id,
1027 dvdsub_id, filename);
1028 if (demuxer)
1029 return demuxer; // done!
1030 file_format = DEMUXER_TYPE_UNKNOWN;
1033 free_demuxer(demuxer);
1034 demuxer = NULL;
1038 return NULL;
1039 //====== File format recognized, set up these for compatibility: =========
1040 dmx_open:
1042 demuxer->file_format = file_format;
1044 if ((sh_video = demuxer->video->sh) && sh_video->bih) {
1045 int biComp = le2me_32(sh_video->bih->biCompression);
1046 mp_msg(MSGT_DEMUX, MSGL_INFO,
1047 "VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
1048 (char *) &biComp, sh_video->bih->biWidth,
1049 sh_video->bih->biHeight, sh_video->bih->biBitCount,
1050 sh_video->fps, sh_video->i_bps * 0.008f,
1051 sh_video->i_bps / 1024.0f);
1053 #ifdef CONFIG_ASS
1054 if (opts->ass_enabled && ass_library) {
1055 for (i = 0; i < MAX_S_STREAMS; ++i) {
1056 sh_sub_t *sh = demuxer->s_streams[i];
1057 if (sh && sh->type == 'a') {
1058 sh->ass_track = ass_new_track(ass_library);
1059 if (sh->ass_track && sh->extradata)
1060 ass_process_codec_private(sh->ass_track, sh->extradata,
1061 sh->extradata_len);
1062 } else if (sh && sh->type != 'v')
1063 sh->ass_track = ass_default_track(ass_library);
1066 #endif
1067 return demuxer;
1070 char *audio_stream = NULL;
1071 char *sub_stream = NULL;
1072 int audio_stream_cache = 0;
1074 char *demuxer_name = NULL; // parameter from -demuxer
1075 char *audio_demuxer_name = NULL; // parameter from -audio-demuxer
1076 char *sub_demuxer_name = NULL; // parameter from -sub-demuxer
1078 extern int hr_mp3_seek;
1080 extern float stream_cache_min_percent;
1081 extern float stream_cache_seek_min_percent;
1083 demuxer_t *demux_open(struct MPOpts *opts, stream_t *vs, int file_format,
1084 int audio_id, int video_id, int dvdsub_id,
1085 char *filename)
1087 stream_t *as = NULL, *ss = NULL;
1088 demuxer_t *vd, *ad = NULL, *sd = NULL;
1089 demuxer_t *res;
1090 int afmt = DEMUXER_TYPE_UNKNOWN, sfmt = DEMUXER_TYPE_UNKNOWN;
1091 int demuxer_type;
1092 int audio_demuxer_type = 0, sub_demuxer_type = 0;
1093 int demuxer_force = 0, audio_demuxer_force = 0, sub_demuxer_force = 0;
1095 if ((demuxer_type =
1096 get_demuxer_type_from_name(demuxer_name, &demuxer_force)) < 0) {
1097 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-demuxer %s does not exist.\n",
1098 demuxer_name);
1100 if ((audio_demuxer_type =
1101 get_demuxer_type_from_name(audio_demuxer_name,
1102 &audio_demuxer_force)) < 0) {
1103 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-audio-demuxer %s does not exist.\n",
1104 audio_demuxer_name);
1106 if ((sub_demuxer_type =
1107 get_demuxer_type_from_name(sub_demuxer_name,
1108 &sub_demuxer_force)) < 0) {
1109 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-sub-demuxer %s does not exist.\n",
1110 sub_demuxer_name);
1113 if (audio_stream) {
1114 as = open_stream(audio_stream, 0, &afmt);
1115 if (!as) {
1116 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "Cannot open audio stream: %s\n",
1117 audio_stream);
1118 return NULL;
1120 if (audio_stream_cache) {
1121 if (!stream_enable_cache
1122 (as, audio_stream_cache * 1024,
1123 audio_stream_cache * 1024 * (stream_cache_min_percent /
1124 100.0),
1125 audio_stream_cache * 1024 * (stream_cache_seek_min_percent /
1126 100.0))) {
1127 free_stream(as);
1128 mp_msg(MSGT_DEMUXER, MSGL_ERR,
1129 "Can't enable audio stream cache\n");
1130 return NULL;
1134 if (sub_stream) {
1135 ss = open_stream(sub_stream, 0, &sfmt);
1136 if (!ss) {
1137 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "Cannot open subtitle stream: %s\n",
1138 sub_stream);
1139 return NULL;
1143 vd = demux_open_stream(opts, vs, demuxer_type ? demuxer_type : file_format,
1144 demuxer_force, audio_stream ? -2 : audio_id,
1145 video_id, sub_stream ? -2 : dvdsub_id, filename);
1146 if (!vd) {
1147 if (as)
1148 free_stream(as);
1149 if (ss)
1150 free_stream(ss);
1151 return NULL;
1153 if (as) {
1154 ad = demux_open_stream(opts, as,
1155 audio_demuxer_type ? audio_demuxer_type : afmt,
1156 audio_demuxer_force, audio_id, -2, -2,
1157 audio_stream);
1158 if (!ad) {
1159 mp_tmsg(MSGT_DEMUXER, MSGL_WARN, "Failed to open audio demuxer: %s\n",
1160 audio_stream);
1161 free_stream(as);
1162 } else if (ad->audio->sh
1163 && ((sh_audio_t *) ad->audio->sh)->format == 0x55) // MP3
1164 hr_mp3_seek = 1; // Enable high res seeking
1166 if (ss) {
1167 sd = demux_open_stream(opts, ss,
1168 sub_demuxer_type ? sub_demuxer_type : sfmt,
1169 sub_demuxer_force, -2, -2, dvdsub_id,
1170 sub_stream);
1171 if (!sd) {
1172 mp_tmsg(MSGT_DEMUXER, MSGL_WARN,
1173 "Failed to open subtitle demuxer: %s\n", sub_stream);
1174 free_stream(ss);
1178 if (ad && sd)
1179 res = new_demuxers_demuxer(vd, ad, sd);
1180 else if (ad)
1181 res = new_demuxers_demuxer(vd, ad, vd);
1182 else if (sd)
1183 res = new_demuxers_demuxer(vd, vd, sd);
1184 else
1185 res = vd;
1187 opts->correct_pts = opts->user_correct_pts;
1188 if (opts->correct_pts < 0)
1189 opts->correct_pts =
1190 demux_control(res, DEMUXER_CTRL_CORRECT_PTS,
1191 NULL) == DEMUXER_CTRL_OK;
1192 return res;
1196 void demux_flush(demuxer_t *demuxer)
1198 #if PARSE_ON_ADD
1199 ds_clear_parser(demuxer->video);
1200 ds_clear_parser(demuxer->audio);
1201 ds_clear_parser(demuxer->sub);
1202 #endif
1203 ds_free_packs(demuxer->video);
1204 ds_free_packs(demuxer->audio);
1205 ds_free_packs(demuxer->sub);
1208 int demux_seek(demuxer_t *demuxer, float rel_seek_secs, float audio_delay,
1209 int flags)
1211 if (!demuxer->seekable) {
1212 if (demuxer->file_format == DEMUXER_TYPE_AVI)
1213 mp_tmsg(MSGT_SEEK, MSGL_WARN, "Cannot seek in raw AVI streams. (Index required, try with the -idx switch.)\n");
1214 #ifdef CONFIG_TV
1215 else if (demuxer->file_format == DEMUXER_TYPE_TV)
1216 mp_tmsg(MSGT_SEEK, MSGL_WARN, "TV input is not seekable! (Seeking will probably be for changing channels ;)\n");
1217 #endif
1218 else
1219 mp_tmsg(MSGT_SEEK, MSGL_WARN, "Cannot seek in this file.\n");
1220 return 0;
1222 // clear demux buffers:
1223 demux_flush(demuxer);
1224 demuxer->video->eof = 0;
1225 demuxer->audio->eof = 0;
1227 /* HACK: assume any demuxer used with these streams can cope with
1228 * the stream layer suddenly seeking to a different position under it
1229 * (nothing actually implements DEMUXER_CTRL_RESYNC now).
1231 struct stream *stream = demuxer->stream;
1232 if (stream->type == STREAMTYPE_DVD || stream->type == STREAMTYPE_DVDNAV) {
1233 double pts;
1235 if (flags & SEEK_ABSOLUTE)
1236 pts = 0.0f;
1237 else {
1238 if (demuxer->stream_pts == MP_NOPTS_VALUE)
1239 goto dmx_seek;
1240 pts = demuxer->stream_pts;
1243 if (flags & SEEK_FACTOR) {
1244 double tmp = 0;
1245 if (stream_control(demuxer->stream, STREAM_CTRL_GET_TIME_LENGTH,
1246 &tmp) == STREAM_UNSUPPORTED)
1247 goto dmx_seek;
1248 pts += tmp * rel_seek_secs;
1249 } else
1250 pts += rel_seek_secs;
1252 if (stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_TIME, &pts)
1253 != STREAM_UNSUPPORTED) {
1254 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1255 return 1;
1259 dmx_seek:
1260 if (demuxer->desc->seek)
1261 demuxer->desc->seek(demuxer, rel_seek_secs, audio_delay, flags);
1263 return 1;
1266 int demux_info_add(demuxer_t *demuxer, const char *opt, const char *param)
1268 return demux_info_add_bstr(demuxer, BSTR(opt), BSTR(param));
1271 int demux_info_add_bstr(demuxer_t *demuxer, struct bstr opt, struct bstr param)
1273 char **info = demuxer->info;
1274 int n = 0;
1277 for (n = 0; info && info[2 * n] != NULL; n++) {
1278 if (!bstrcasecmp(opt, BSTR(info[2*n]))) {
1279 if (!bstrcmp(param, BSTR(info[2*n + 1]))) {
1280 mp_msg(MSGT_DEMUX, MSGL_V, "Demuxer info %.*s set to unchanged value %.*s\n",
1281 BSTR_P(opt), BSTR_P(param));
1282 return 0;
1284 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "Demuxer info %.*s changed to %.*s\n",
1285 BSTR_P(opt), BSTR_P(param));
1286 talloc_free(info[2*n + 1]);
1287 info[2*n + 1] = talloc_strndup(demuxer->info, param.start, param.len);
1288 return 0;
1292 info = demuxer->info = talloc_realloc(demuxer, info, char *, 2 * (n + 2));
1293 info[2*n] = talloc_strndup(demuxer->info, opt.start, opt.len);
1294 info[2*n + 1] = talloc_strndup(demuxer->info, param.start, param.len);
1295 memset(&info[2 * (n + 1)], 0, 2 * sizeof(char *));
1297 return 1;
1300 int demux_info_print(demuxer_t *demuxer)
1302 char **info = demuxer->info;
1303 int n;
1305 if (!info)
1306 return 0;
1308 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "Clip info:\n");
1309 for (n = 0; info[2 * n] != NULL; n++) {
1310 mp_msg(MSGT_DEMUX, MSGL_INFO, " %s: %s\n", info[2 * n],
1311 info[2 * n + 1]);
1312 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_NAME%d=%s\n", n,
1313 info[2 * n]);
1314 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_VALUE%d=%s\n", n,
1315 info[2 * n + 1]);
1317 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_N=%d\n", n);
1319 return 0;
1322 char *demux_info_get(demuxer_t *demuxer, const char *opt)
1324 int i;
1325 char **info = demuxer->info;
1327 for (i = 0; info && info[2 * i] != NULL; i++) {
1328 if (!strcasecmp(opt, info[2 * i]))
1329 return info[2 * i + 1];
1332 return NULL;
1335 int demux_control(demuxer_t *demuxer, int cmd, void *arg)
1338 if (demuxer->desc->control)
1339 return demuxer->desc->control(demuxer, cmd, arg);
1341 return DEMUXER_CTRL_NOTIMPL;
1346 double demuxer_get_time_length(demuxer_t *demuxer)
1348 double get_time_ans;
1349 sh_video_t *sh_video = demuxer->video->sh;
1350 sh_audio_t *sh_audio = demuxer->audio->sh;
1351 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
1352 if (demux_control
1353 (demuxer, DEMUXER_CTRL_GET_TIME_LENGTH, (void *) &get_time_ans) <= 0) {
1354 if (sh_video && sh_video->i_bps && sh_audio && sh_audio->i_bps)
1355 get_time_ans = (double) (demuxer->movi_end -
1356 demuxer->movi_start) / (sh_video->i_bps +
1357 sh_audio->i_bps);
1358 else if (sh_video && sh_video->i_bps)
1359 get_time_ans = (double) (demuxer->movi_end -
1360 demuxer->movi_start) / sh_video->i_bps;
1361 else if (sh_audio && sh_audio->i_bps)
1362 get_time_ans = (double) (demuxer->movi_end -
1363 demuxer->movi_start) / sh_audio->i_bps;
1364 else
1365 get_time_ans = 0;
1367 return get_time_ans;
1371 * \brief demuxer_get_current_time() returns the time of the current play in three possible ways:
1372 * either when the stream reader satisfies STREAM_CTRL_GET_CURRENT_TIME (e.g. dvd)
1373 * or using sh_video->pts when the former method fails
1374 * 0 otherwise
1375 * \return the current play time
1377 int demuxer_get_current_time(demuxer_t *demuxer)
1379 double get_time_ans = 0;
1380 sh_video_t *sh_video = demuxer->video->sh;
1381 if (demuxer->stream_pts != MP_NOPTS_VALUE)
1382 get_time_ans = demuxer->stream_pts;
1383 else if (sh_video)
1384 get_time_ans = sh_video->pts;
1385 return (int) get_time_ans;
1388 int demuxer_get_percent_pos(demuxer_t *demuxer)
1390 int ans = 0;
1391 int res = demux_control(demuxer, DEMUXER_CTRL_GET_PERCENT_POS, &ans);
1392 int len = (demuxer->movi_end - demuxer->movi_start) / 100;
1393 if (res <= 0) {
1394 off_t pos = demuxer->filepos > 0 ? demuxer->filepos : stream_tell(demuxer->stream);
1395 if (len > 0)
1396 ans = (pos - demuxer->movi_start) / len;
1397 else
1398 ans = 0;
1400 if (ans < 0)
1401 ans = 0;
1402 if (ans > 100)
1403 ans = 100;
1404 return ans;
1407 int demuxer_switch_audio(demuxer_t *demuxer, int index)
1409 int res = demux_control(demuxer, DEMUXER_CTRL_SWITCH_AUDIO, &index);
1410 if (res == DEMUXER_CTRL_NOTIMPL) {
1411 struct sh_audio *sh_audio = demuxer->audio->sh;
1412 return sh_audio ? sh_audio->aid : -2;
1414 if (demuxer->audio->id >= 0) {
1415 struct sh_audio *sh_audio = demuxer->a_streams[demuxer->audio->id];
1416 demuxer->audio->sh = sh_audio;
1417 index = sh_audio->aid; // internal MPEG demuxers don't set it right
1419 else
1420 demuxer->audio->sh = NULL;
1421 return index;
1424 int demuxer_switch_video(demuxer_t *demuxer, int index)
1426 int res = demux_control(demuxer, DEMUXER_CTRL_SWITCH_VIDEO, &index);
1427 if (res == DEMUXER_CTRL_NOTIMPL)
1428 index = demuxer->video->id;
1429 if (demuxer->video->id >= 0)
1430 demuxer->video->sh = demuxer->v_streams[demuxer->video->id];
1431 else
1432 demuxer->video->sh = NULL;
1433 return index;
1436 int demuxer_add_attachment(demuxer_t *demuxer, struct bstr name,
1437 struct bstr type, struct bstr data)
1439 if (!(demuxer->num_attachments % 32))
1440 demuxer->attachments = talloc_realloc(demuxer, demuxer->attachments,
1441 struct demux_attachment,
1442 demuxer->num_attachments + 32);
1444 struct demux_attachment *att =
1445 demuxer->attachments + demuxer->num_attachments;
1446 att->name = talloc_strndup(demuxer->attachments, name.start, name.len);
1447 att->type = talloc_strndup(demuxer->attachments, type.start, type.len);
1448 att->data = talloc_size(demuxer->attachments, data.len);
1449 memcpy(att->data, data.start, data.len);
1450 att->data_size = data.len;
1452 return demuxer->num_attachments++;
1455 int demuxer_add_chapter(demuxer_t *demuxer, struct bstr name,
1456 uint64_t start, uint64_t end)
1458 if (!(demuxer->num_chapters % 32))
1459 demuxer->chapters = talloc_realloc(demuxer, demuxer->chapters,
1460 struct demux_chapter,
1461 demuxer->num_chapters + 32);
1463 demuxer->chapters[demuxer->num_chapters].start = start;
1464 demuxer->chapters[demuxer->num_chapters].end = end;
1465 demuxer->chapters[demuxer->num_chapters].name = name.len ?
1466 talloc_strndup(demuxer->chapters, name.start, name.len) :
1467 talloc_strdup(demuxer->chapters, mp_gtext("unknown"));
1469 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_ID=%d\n", demuxer->num_chapters);
1470 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_START=%"PRIu64"\n", demuxer->num_chapters, start);
1471 if (end)
1472 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_END=%"PRIu64"\n", demuxer->num_chapters, end);
1473 if (name.start)
1474 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_NAME=%.*s\n", demuxer->num_chapters, BSTR_P(name));
1476 return demuxer->num_chapters++;
1480 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1481 * either using the demuxer->chapters structure set by the demuxer
1482 * or asking help to the stream layer (e.g. dvd)
1483 * \param chapter - chapter number wished - 0-based
1484 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1485 * \param chapter_name name of chapter found (set by this function is param is not null)
1486 * \return -1 on error, current chapter if successful
1489 int demuxer_seek_chapter(demuxer_t *demuxer, int chapter, double *seek_pts,
1490 char **chapter_name)
1492 int ris;
1494 if (!demuxer->num_chapters || !demuxer->chapters) {
1495 demux_flush(demuxer);
1497 ris = stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_CHAPTER,
1498 &chapter);
1499 if (ris != STREAM_UNSUPPORTED)
1500 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1502 // exit status may be ok, but main() doesn't have to seek itself
1503 // (because e.g. dvds depend on sectors, not on pts)
1504 *seek_pts = -1.0;
1506 if (chapter_name) {
1507 *chapter_name = NULL;
1508 int num_chapters;
1509 if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS,
1510 &num_chapters) == STREAM_UNSUPPORTED)
1511 num_chapters = 0;
1512 if (num_chapters) {
1513 *chapter_name = talloc_size(NULL, 16);
1514 sprintf(*chapter_name, " of %3d", num_chapters);
1518 return ris != STREAM_UNSUPPORTED ? chapter : -1;
1519 } else { // chapters structure is set in the demuxer
1520 if (chapter >= demuxer->num_chapters)
1521 return -1;
1522 if (chapter < 0)
1523 chapter = 0;
1525 *seek_pts = demuxer->chapters[chapter].start / 1000.0;
1527 if (chapter_name)
1528 *chapter_name = talloc_strdup(NULL, demuxer->chapters[chapter].name);
1530 return chapter;
1534 int demuxer_get_current_chapter(demuxer_t *demuxer)
1536 int chapter = -2;
1537 if (!demuxer->num_chapters || !demuxer->chapters) {
1538 if (stream_control(demuxer->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
1539 &chapter) == STREAM_UNSUPPORTED)
1540 chapter = -2;
1541 } else {
1542 sh_video_t *sh_video = demuxer->video->sh;
1543 sh_audio_t *sh_audio = demuxer->audio->sh;
1544 uint64_t now;
1545 now = (sh_video ? sh_video->pts : (sh_audio ? sh_audio->pts : 0))
1546 * 1000 + 0.5;
1547 for (chapter = demuxer->num_chapters - 1; chapter >= 0; --chapter) {
1548 if (demuxer->chapters[chapter].start <= now)
1549 break;
1552 return chapter;
1555 char *demuxer_chapter_name(demuxer_t *demuxer, int chapter)
1557 if (demuxer->num_chapters && demuxer->chapters) {
1558 if (chapter >= 0 && chapter < demuxer->num_chapters
1559 && demuxer->chapters[chapter].name)
1560 return strdup(demuxer->chapters[chapter].name);
1562 return NULL;
1565 char *demuxer_chapter_display_name(demuxer_t *demuxer, int chapter)
1567 char *chapter_name = demuxer_chapter_name(demuxer, chapter);
1568 if (chapter_name) {
1569 char *tmp = malloc(strlen(chapter_name) + 14);
1570 snprintf(tmp, 63, "(%d) %s", chapter + 1, chapter_name);
1571 free(chapter_name);
1572 return tmp;
1573 } else {
1574 int chapter_num = demuxer_chapter_count(demuxer);
1575 char tmp[30];
1576 if (chapter_num <= 0)
1577 sprintf(tmp, "(%d)", chapter + 1);
1578 else
1579 sprintf(tmp, "(%d) of %d", chapter + 1, chapter_num);
1580 return strdup(tmp);
1584 float demuxer_chapter_time(demuxer_t *demuxer, int chapter, float *end)
1586 if (demuxer->num_chapters && demuxer->chapters && chapter >= 0
1587 && chapter < demuxer->num_chapters) {
1588 if (end)
1589 *end = demuxer->chapters[chapter].end / 1000.0;
1590 return demuxer->chapters[chapter].start / 1000.0;
1592 return -1.0;
1595 int demuxer_chapter_count(demuxer_t *demuxer)
1597 if (!demuxer->num_chapters || !demuxer->chapters) {
1598 int num_chapters = 0;
1599 if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS,
1600 &num_chapters) == STREAM_UNSUPPORTED)
1601 num_chapters = 0;
1602 return num_chapters;
1603 } else
1604 return demuxer->num_chapters;
1607 int demuxer_angles_count(demuxer_t *demuxer)
1609 int ris, angles = -1;
1611 ris = stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_ANGLES, &angles);
1612 if (ris == STREAM_UNSUPPORTED)
1613 return -1;
1614 return angles;
1617 int demuxer_get_current_angle(demuxer_t *demuxer)
1619 int ris, curr_angle = -1;
1620 ris = stream_control(demuxer->stream, STREAM_CTRL_GET_ANGLE, &curr_angle);
1621 if (ris == STREAM_UNSUPPORTED)
1622 return -1;
1623 return curr_angle;
1627 int demuxer_set_angle(demuxer_t *demuxer, int angle)
1629 int ris, angles = -1;
1631 angles = demuxer_angles_count(demuxer);
1632 if ((angles < 1) || (angle > angles))
1633 return -1;
1635 demux_flush(demuxer);
1637 ris = stream_control(demuxer->stream, STREAM_CTRL_SET_ANGLE, &angle);
1638 if (ris == STREAM_UNSUPPORTED)
1639 return -1;
1641 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1643 return angle;
1646 int demuxer_audio_track_by_lang(demuxer_t *d, char *lang)
1648 int i, len;
1649 lang += strspn(lang, ",");
1650 while ((len = strcspn(lang, ",")) > 0) {
1651 for (i = 0; i < MAX_A_STREAMS; ++i) {
1652 sh_audio_t *sh = d->a_streams[i];
1653 if (sh && sh->lang && strncmp(sh->lang, lang, len) == 0)
1654 return sh->aid;
1656 lang += len;
1657 lang += strspn(lang, ",");
1659 return -1;
1662 int demuxer_sub_track_by_lang(demuxer_t *d, char *lang)
1664 int i, len;
1665 lang += strspn(lang, ",");
1666 while ((len = strcspn(lang, ",")) > 0) {
1667 for (i = 0; i < MAX_S_STREAMS; ++i) {
1668 sh_sub_t *sh = d->s_streams[i];
1669 if (sh && sh->lang && strncmp(sh->lang, lang, len) == 0)
1670 return sh->sid;
1672 lang += len;
1673 lang += strspn(lang, ",");
1675 return -1;
1678 int demuxer_default_audio_track(demuxer_t *d)
1680 int i;
1681 for (i = 0; i < MAX_A_STREAMS; ++i) {
1682 sh_audio_t *sh = d->a_streams[i];
1683 if (sh && sh->default_track)
1684 return sh->aid;
1686 for (i = 0; i < MAX_A_STREAMS; ++i) {
1687 sh_audio_t *sh = d->a_streams[i];
1688 if (sh)
1689 return sh->aid;
1691 return -1;
1694 int demuxer_default_sub_track(demuxer_t *d)
1696 int i;
1697 for (i = 0; i < MAX_S_STREAMS; ++i) {
1698 sh_sub_t *sh = d->s_streams[i];
1699 if (sh && sh->default_track)
1700 return sh->sid;
1702 return -1;