Ignore svn changes up to r30324
[mplayer/glamo.git] / libmpdemux / demuxer.c
blob78c18e7640c346489e0175a902e6b942d78b747c
1 /*
2 * DEMUXER v2.5
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
29 #include "config.h"
30 #include "options.h"
31 #include "talloc.h"
32 #include "mp_msg.h"
33 #include "help_mp.h"
34 #include "m_config.h"
36 #include "libvo/fastmemcpy.h"
38 #include "stream/stream.h"
39 #include "demuxer.h"
40 #include "stheader.h"
41 #include "mf.h"
43 #include "libaf/af_format.h"
44 #include "libmpcodecs/dec_teletext.h"
46 #include "ass_mp.h"
48 #ifdef CONFIG_LIBAVCODEC
49 #include "libavcodec/avcodec.h"
50 #if MP_INPUT_BUFFER_PADDING_SIZE < FF_INPUT_BUFFER_PADDING_SIZE
51 #error MP_INPUT_BUFFER_PADDING_SIZE is too small!
52 #endif
53 #endif
55 // This is quite experimental, in particular it will mess up the pts values
56 // in the queue - on the other hand it might fix some issues like generating
57 // broken files with mencoder and stream copy.
58 // Better leave it disabled for now, if we find no use for it this code should
59 // just be removed again.
60 #define PARSE_ON_ADD 0
62 void resync_video_stream(sh_video_t *sh_video);
63 void resync_audio_stream(sh_audio_t *sh_audio);
65 // Demuxer list
66 extern const demuxer_desc_t demuxer_desc_rawaudio;
67 extern const demuxer_desc_t demuxer_desc_rawvideo;
68 extern const demuxer_desc_t demuxer_desc_tv;
69 extern const demuxer_desc_t demuxer_desc_mf;
70 extern const demuxer_desc_t demuxer_desc_avi;
71 extern const demuxer_desc_t demuxer_desc_y4m;
72 extern const demuxer_desc_t demuxer_desc_asf;
73 extern const demuxer_desc_t demuxer_desc_real;
74 extern const demuxer_desc_t demuxer_desc_smjpeg;
75 extern const demuxer_desc_t demuxer_desc_matroska;
76 extern const demuxer_desc_t demuxer_desc_realaudio;
77 extern const demuxer_desc_t demuxer_desc_vqf;
78 extern const demuxer_desc_t demuxer_desc_mov;
79 extern const demuxer_desc_t demuxer_desc_vivo;
80 extern const demuxer_desc_t demuxer_desc_fli;
81 extern const demuxer_desc_t demuxer_desc_film;
82 extern const demuxer_desc_t demuxer_desc_roq;
83 extern const demuxer_desc_t demuxer_desc_gif;
84 extern const demuxer_desc_t demuxer_desc_ogg;
85 extern const demuxer_desc_t demuxer_desc_avs;
86 extern const demuxer_desc_t demuxer_desc_pva;
87 extern const demuxer_desc_t demuxer_desc_nsv;
88 extern const demuxer_desc_t demuxer_desc_mpeg_ts;
89 extern const demuxer_desc_t demuxer_desc_lmlm4;
90 extern const demuxer_desc_t demuxer_desc_mpeg_ps;
91 extern const demuxer_desc_t demuxer_desc_mpeg_pes;
92 extern const demuxer_desc_t demuxer_desc_mpeg_es;
93 extern const demuxer_desc_t demuxer_desc_mpeg_gxf;
94 extern const demuxer_desc_t demuxer_desc_mpeg4_es;
95 extern const demuxer_desc_t demuxer_desc_h264_es;
96 extern const demuxer_desc_t demuxer_desc_rawdv;
97 extern const demuxer_desc_t demuxer_desc_mpc;
98 extern const demuxer_desc_t demuxer_desc_audio;
99 extern const demuxer_desc_t demuxer_desc_xmms;
100 extern const demuxer_desc_t demuxer_desc_mpeg_ty;
101 extern const demuxer_desc_t demuxer_desc_rtp;
102 extern const demuxer_desc_t demuxer_desc_rtp_nemesi;
103 extern const demuxer_desc_t demuxer_desc_lavf;
104 extern const demuxer_desc_t demuxer_desc_lavf_preferred;
105 extern const demuxer_desc_t demuxer_desc_aac;
106 extern const demuxer_desc_t demuxer_desc_nut;
107 extern const demuxer_desc_t demuxer_desc_mng;
109 /* Please do not add any new demuxers here. If you want to implement a new
110 * demuxer, add it to libavformat, except for wrappers around external
111 * libraries and demuxers requiring binary support. */
113 const demuxer_desc_t *const demuxer_list[] = {
114 &demuxer_desc_rawaudio,
115 &demuxer_desc_rawvideo,
116 #ifdef CONFIG_TV
117 &demuxer_desc_tv,
118 #endif
119 &demuxer_desc_mf,
120 #ifdef CONFIG_LIBAVFORMAT
121 &demuxer_desc_lavf_preferred,
122 #endif
123 &demuxer_desc_avi,
124 &demuxer_desc_y4m,
125 &demuxer_desc_asf,
126 &demuxer_desc_nsv,
127 &demuxer_desc_real,
128 &demuxer_desc_smjpeg,
129 &demuxer_desc_matroska,
130 &demuxer_desc_realaudio,
131 &demuxer_desc_vqf,
132 &demuxer_desc_mov,
133 &demuxer_desc_vivo,
134 &demuxer_desc_fli,
135 &demuxer_desc_film,
136 &demuxer_desc_roq,
137 #ifdef CONFIG_GIF
138 &demuxer_desc_gif,
139 #endif
140 #ifdef CONFIG_OGGVORBIS
141 &demuxer_desc_ogg,
142 #endif
143 #ifdef CONFIG_WIN32DLL
144 &demuxer_desc_avs,
145 #endif
146 &demuxer_desc_pva,
147 &demuxer_desc_mpeg_ts,
148 &demuxer_desc_lmlm4,
149 &demuxer_desc_mpeg_ps,
150 &demuxer_desc_mpeg_pes,
151 &demuxer_desc_mpeg_es,
152 &demuxer_desc_mpeg_gxf,
153 &demuxer_desc_mpeg4_es,
154 &demuxer_desc_h264_es,
155 &demuxer_desc_audio,
156 &demuxer_desc_mpeg_ty,
157 #ifdef CONFIG_LIVE555
158 &demuxer_desc_rtp,
159 #endif
160 #ifdef CONFIG_LIBNEMESI
161 &demuxer_desc_rtp_nemesi,
162 #endif
163 #ifdef CONFIG_LIBAVFORMAT
164 &demuxer_desc_lavf,
165 #endif
166 #ifdef CONFIG_MUSEPACK
167 &demuxer_desc_mpc,
168 #endif
169 #ifdef CONFIG_LIBDV095
170 &demuxer_desc_rawdv,
171 #endif
172 &demuxer_desc_aac,
173 #ifdef CONFIG_LIBNUT
174 &demuxer_desc_nut,
175 #endif
176 #ifdef CONFIG_XMMS
177 &demuxer_desc_xmms,
178 #endif
179 #ifdef CONFIG_MNG
180 &demuxer_desc_mng,
181 #endif
182 /* Please do not add any new demuxers here. If you want to implement a new
183 * demuxer, add it to libavformat, except for wrappers around external
184 * libraries and demuxers requiring binary support. */
185 NULL
188 void free_demuxer_stream(demux_stream_t *ds)
190 ds_free_packs(ds);
191 free(ds);
194 demux_stream_t *new_demuxer_stream(struct demuxer *demuxer, int id)
196 demux_stream_t *ds = malloc(sizeof(demux_stream_t));
197 *ds = (demux_stream_t){
198 .id = id,
199 .demuxer = demuxer,
200 .asf_seq = -1,
202 return ds;
207 * Get demuxer description structure for a given demuxer type
209 * @param file_format type of the demuxer
210 * @return structure for the demuxer, NULL if not found
212 static const demuxer_desc_t *get_demuxer_desc_from_type(int file_format)
214 int i;
216 for (i = 0; demuxer_list[i]; i++)
217 if (file_format == demuxer_list[i]->type)
218 return demuxer_list[i];
220 return NULL;
224 demuxer_t *new_demuxer(struct MPOpts *opts, stream_t *stream, int type,
225 int a_id, int v_id, int s_id, char *filename)
227 struct demuxer *d = talloc_zero(NULL, struct demuxer);
228 d->stream = stream;
229 d->stream_pts = MP_NOPTS_VALUE;
230 d->reference_clock = MP_NOPTS_VALUE;
231 d->movi_start = stream->start_pos;
232 d->movi_end = stream->end_pos;
233 d->seekable = 1;
234 d->synced = 0;
235 d->filepos = -1;
236 d->audio = new_demuxer_stream(d, a_id);
237 d->video = new_demuxer_stream(d, v_id);
238 d->sub = new_demuxer_stream(d, s_id);
239 d->type = type;
240 d->opts = opts;
241 if (type)
242 if (!(d->desc = get_demuxer_desc_from_type(type)))
243 mp_msg(MSGT_DEMUXER, MSGL_ERR,
244 "BUG! Invalid demuxer type in new_demuxer(), "
245 "big troubles ahead.");
246 if (filename) // Filename hack for avs_check_file
247 d->filename = strdup(filename);
248 stream_reset(stream);
249 stream_seek(stream, stream->start_pos);
250 return d;
253 sh_sub_t *new_sh_sub_sid(demuxer_t *demuxer, int id, int sid)
255 if (id > MAX_S_STREAMS - 1 || id < 0) {
256 mp_msg(MSGT_DEMUXER, MSGL_WARN,
257 "Requested sub stream id overflow (%d > %d)\n", id,
258 MAX_S_STREAMS);
259 return NULL;
261 if (demuxer->s_streams[id])
262 mp_msg(MSGT_DEMUXER, MSGL_WARN, "Sub stream %i redefined\n", id);
263 else {
264 sh_sub_t *sh = calloc(1, sizeof(sh_sub_t));
265 demuxer->s_streams[id] = sh;
266 sh->sid = sid;
267 sh->opts = demuxer->opts;
268 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SUBTITLE_ID=%d\n", sid);
270 if (sid == demuxer->opts->sub_id) {
271 demuxer->sub->id = id;
272 demuxer->sub->sh = demuxer->s_streams[id];
274 return demuxer->s_streams[id];
277 static void free_sh_sub(sh_sub_t *sh)
279 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_sub at %p\n", sh);
280 free(sh->extradata);
281 #ifdef CONFIG_ASS
282 if (sh->ass_track)
283 ass_free_track(sh->ass_track);
284 #endif
285 free(sh->lang);
286 #ifdef CONFIG_LIBAVCODEC
287 av_parser_close(sh->parser);
288 av_freep(&sh->avctx);
289 #endif
290 free(sh);
293 sh_audio_t *new_sh_audio_aid(demuxer_t *demuxer, int id, int aid)
295 if (id > MAX_A_STREAMS - 1 || id < 0) {
296 mp_msg(MSGT_DEMUXER, MSGL_WARN,
297 "Requested audio stream id overflow (%d > %d)\n", id,
298 MAX_A_STREAMS);
299 return NULL;
301 if (demuxer->a_streams[id]) {
302 mp_tmsg(MSGT_DEMUXER, MSGL_WARN, "WARNING: Audio stream header %d redefined.\n", id);
303 } else {
304 mp_tmsg(MSGT_DEMUXER, MSGL_V, "==> Found audio stream: %d\n", id);
305 sh_audio_t *sh = calloc(1, sizeof(sh_audio_t));
306 demuxer->a_streams[id] = sh;
307 sh->aid = aid;
308 sh->ds = demuxer->audio;
309 // set some defaults
310 sh->samplesize = 2;
311 sh->sample_format = AF_FORMAT_S16_NE;
312 sh->audio_out_minsize = 8192; /* default size, maybe not enough for Win32/ACM */
313 sh->pts = MP_NOPTS_VALUE;
314 sh->opts = demuxer->opts;
315 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_ID=%d\n", aid);
317 return demuxer->a_streams[id];
320 void free_sh_audio(demuxer_t *demuxer, int id)
322 sh_audio_t *sh = demuxer->a_streams[id];
323 demuxer->a_streams[id] = NULL;
324 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_audio at %p\n", sh);
325 free(sh->wf);
326 free(sh->codecdata);
327 free(sh->lang);
328 #ifdef CONFIG_LIBAVCODEC
329 av_parser_close(sh->parser);
330 av_freep(&sh->avctx);
331 #endif
332 free(sh);
335 sh_video_t *new_sh_video_vid(demuxer_t *demuxer, int id, int vid)
337 if (id > MAX_V_STREAMS - 1 || id < 0) {
338 mp_msg(MSGT_DEMUXER, MSGL_WARN,
339 "Requested video stream id overflow (%d > %d)\n", id,
340 MAX_V_STREAMS);
341 return NULL;
343 if (demuxer->v_streams[id])
344 mp_tmsg(MSGT_DEMUXER, MSGL_WARN, "WARNING: Video stream header %d redefined.\n", id);
345 else {
346 mp_tmsg(MSGT_DEMUXER, MSGL_V, "==> Found video stream: %d\n", id);
347 sh_video_t *sh = calloc(1, sizeof *sh);
348 demuxer->v_streams[id] = sh;
349 sh->vid = vid;
350 sh->ds = demuxer->video;
351 sh->opts = demuxer->opts;
352 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_ID=%d\n", vid);
354 return demuxer->v_streams[id];
357 void free_sh_video(sh_video_t *sh)
359 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_video at %p\n", sh);
360 free(sh->bih);
361 #ifdef CONFIG_LIBAVCODEC
362 av_parser_close(sh->parser);
363 av_freep(&sh->avctx);
364 #endif
365 free(sh);
368 void free_demuxer(demuxer_t *demuxer)
370 int i;
371 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing demuxer at %p\n",
372 demuxer);
373 if (demuxer->desc->close)
374 demuxer->desc->close(demuxer);
375 // Very ugly hack to make it behave like old implementation
376 if (demuxer->desc->type == DEMUXER_TYPE_DEMUXERS)
377 goto skip_streamfree;
378 // free streams:
379 for (i = 0; i < MAX_A_STREAMS; i++)
380 if (demuxer->a_streams[i])
381 free_sh_audio(demuxer, i);
382 for (i = 0; i < MAX_V_STREAMS; i++)
383 if (demuxer->v_streams[i])
384 free_sh_video(demuxer->v_streams[i]);
385 for (i = 0; i < MAX_S_STREAMS; i++)
386 if (demuxer->s_streams[i])
387 free_sh_sub(demuxer->s_streams[i]);
388 // free demuxers:
389 free_demuxer_stream(demuxer->audio);
390 free_demuxer_stream(demuxer->video);
391 free_demuxer_stream(demuxer->sub);
392 skip_streamfree:
393 if (demuxer->info) {
394 for (i = 0; demuxer->info[i] != NULL; i++)
395 free(demuxer->info[i]);
396 free(demuxer->info);
398 free(demuxer->filename);
399 if (demuxer->chapters) {
400 for (i = 0; i < demuxer->num_chapters; i++)
401 free(demuxer->chapters[i].name);
402 free(demuxer->chapters);
404 if (demuxer->attachments) {
405 for (i = 0; i < demuxer->num_attachments; i++) {
406 free(demuxer->attachments[i].name);
407 free(demuxer->attachments[i].type);
408 free(demuxer->attachments[i].data);
410 free(demuxer->attachments);
412 if (demuxer->teletext)
413 teletext_control(demuxer->teletext, TV_VBI_CONTROL_STOP, NULL);
414 talloc_free(demuxer);
418 static void ds_add_packet_internal(demux_stream_t *ds, demux_packet_t *dp)
420 // append packet to DS stream:
421 ++ds->packs;
422 ds->bytes += dp->len;
423 if (ds->last) {
424 // next packet in stream
425 ds->last->next = dp;
426 ds->last = dp;
427 } else {
428 // first packet in stream
429 ds->first = ds->last = dp;
431 mp_dbg(MSGT_DEMUXER, MSGL_DBG2,
432 "DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
433 (ds == ds->demuxer->audio) ? "d_audio" : "d_video", dp->len,
434 dp->pts, (unsigned int) dp->pos, ds->demuxer->audio->packs,
435 ds->demuxer->video->packs);
438 #ifdef CONFIG_LIBAVCODEC
439 static void allocate_parser(AVCodecContext **avctx, AVCodecParserContext **parser, unsigned format)
441 enum CodecID codec_id = CODEC_ID_NONE;
442 extern int avcodec_initialized;
443 if (!avcodec_initialized) {
444 avcodec_init();
445 avcodec_register_all();
446 avcodec_initialized = 1;
448 switch (format) {
449 case 0x2000:
450 case 0x332D6361:
451 case 0x332D4341:
452 case MKTAG('d', 'n', 'e', 't'):
453 case MKTAG('s', 'a', 'c', '3'):
454 codec_id = CODEC_ID_AC3;
455 break;
456 case MKTAG('E', 'A', 'C', '3'):
457 codec_id = CODEC_ID_EAC3;
458 break;
459 case 0x2001:
460 case 0x86:
461 codec_id = CODEC_ID_DTS;
462 break;
463 case 0x55:
464 case 0x5500736d:
465 case MKTAG('.', 'm', 'p', '3'):
466 case MKTAG('M', 'P', 'E', ' '):
467 case MKTAG('L', 'A', 'M', 'E'):
468 codec_id = CODEC_ID_MP3;
469 break;
470 case 0x50:
471 case MKTAG('.', 'm', 'p', '2'):
472 case MKTAG('.', 'm', 'p', '1'):
473 codec_id = CODEC_ID_MP2;
474 break;
476 if (codec_id != CODEC_ID_NONE) {
477 *avctx = avcodec_alloc_context();
478 if (!*avctx)
479 return;
480 *parser = av_parser_init(codec_id);
481 if (!*parser)
482 av_freep(avctx);
486 static void get_parser(sh_common_t *sh, AVCodecContext **avctx, AVCodecParserContext **parser)
488 *avctx = NULL;
489 *parser = NULL;
491 if (!sh || !sh->needs_parsing)
492 return;
494 *avctx = sh->avctx;
495 *parser = sh->parser;
496 if (*parser)
497 return;
499 allocate_parser(avctx, parser, sh->format);
500 sh->avctx = *avctx;
501 sh->parser = *parser;
504 int ds_parse(demux_stream_t *ds, uint8_t **buffer, int *len, double pts, off_t pos)
506 AVCodecContext *avctx;
507 AVCodecParserContext *parser;
508 get_parser(ds->sh, &avctx, &parser);
509 if (!parser)
510 return *len;
511 return av_parser_parse2(parser, avctx, buffer, len, *buffer, *len, pts, pts, pos);
513 #endif
515 void ds_add_packet(demux_stream_t *ds, demux_packet_t *dp)
517 #if PARSE_ON_ADD && defined(CONFIG_LIBAVCODEC)
518 int len = dp->len;
519 int pos = 0;
520 while (len > 0) {
521 uint8_t *parsed_start = dp->buffer + pos;
522 int parsed_len = len;
523 int consumed = ds_parse(ds->sh, &parsed_start, &parsed_len, dp->pts, dp->pos);
524 pos += consumed;
525 len -= consumed;
526 if (parsed_start == dp->buffer && parsed_len == dp->len) {
527 ds_add_packet_internal(ds, dp);
528 } else if (parsed_len) {
529 demux_packet_t *dp2 = new_demux_packet(parsed_len);
530 dp2->pos = dp->pos;
531 dp2->pts = dp->pts; // should be parser->pts but that works badly
532 memcpy(dp2->buffer, parsed_start, parsed_len);
533 ds_add_packet_internal(ds, dp2);
536 #else
537 ds_add_packet_internal(ds, dp);
538 #endif
541 void ds_read_packet(demux_stream_t *ds, stream_t *stream, int len,
542 double pts, off_t pos, int flags)
544 demux_packet_t *dp = new_demux_packet(len);
545 len = stream_read(stream, dp->buffer, len);
546 resize_demux_packet(dp, len);
547 dp->pts = pts;
548 dp->pos = pos;
549 dp->flags = flags;
550 // append packet to DS stream:
551 ds_add_packet(ds, dp);
554 // return value:
555 // 0 = EOF or no stream found or invalid type
556 // 1 = successfully read a packet
558 int demux_fill_buffer(demuxer_t *demux, demux_stream_t *ds)
560 // Note: parameter 'ds' can be NULL!
561 return demux->desc->fill_buffer(demux, ds);
564 // return value:
565 // 0 = EOF
566 // 1 = successful
567 #define MAX_ACUMULATED_PACKETS 64
568 int ds_fill_buffer(demux_stream_t *ds)
570 demuxer_t *demux = ds->demuxer;
571 if (ds->current)
572 free_demux_packet(ds->current);
573 ds->current = NULL;
574 if (mp_msg_test(MSGT_DEMUXER, MSGL_DBG3)) {
575 if (ds == demux->audio)
576 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
577 "ds_fill_buffer(d_audio) called\n");
578 else if (ds == demux->video)
579 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
580 "ds_fill_buffer(d_video) called\n");
581 else if (ds == demux->sub)
582 mp_dbg(MSGT_DEMUXER, MSGL_DBG3, "ds_fill_buffer(d_sub) called\n");
583 else
584 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
585 "ds_fill_buffer(unknown 0x%X) called\n", (unsigned int) ds);
587 while (1) {
588 if (ds->packs) {
589 demux_packet_t *p = ds->first;
590 #if 0
591 if (demux->reference_clock != MP_NOPTS_VALUE) {
592 if ( p->pts != MP_NOPTS_VALUE
593 && p->pts > demux->reference_clock
594 && ds->packs < MAX_ACUMULATED_PACKETS) {
595 if (demux_fill_buffer(demux, ds))
596 continue;
599 #endif
600 // copy useful data:
601 ds->buffer = p->buffer;
602 ds->buffer_pos = 0;
603 ds->buffer_size = p->len;
604 ds->pos = p->pos;
605 ds->dpos += p->len; // !!!
606 ++ds->pack_no;
607 if (p->pts != MP_NOPTS_VALUE) {
608 ds->pts = p->pts;
609 ds->pts_bytes = 0;
611 ds->pts_bytes += p->len; // !!!
612 if (p->stream_pts != MP_NOPTS_VALUE)
613 demux->stream_pts = p->stream_pts;
614 ds->flags = p->flags;
615 // unlink packet:
616 ds->bytes -= p->len;
617 ds->current = p;
618 ds->first = p->next;
619 if (!ds->first)
620 ds->last = NULL;
621 --ds->packs;
622 /* The code below can set ds->eof to 1 when another stream runs
623 * out of buffer space. That makes sense because in that situation
624 * the calling code should not count on being able to demux more
625 * packets from this stream.
626 * If however the situation improves and we're called again
627 * despite the eof flag then it's better to clear it to avoid
628 * weird behavior. */
629 ds->eof = 0;
630 return 1;
633 #define MaybeNI _("Maybe you are playing a non-interleaved stream/file or the codec failed?\n" \
634 "For AVI files, try to force non-interleaved mode with the -ni option.\n")
636 if (demux->audio->packs >= MAX_PACKS
637 || demux->audio->bytes >= MAX_PACK_BYTES) {
638 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
639 demux->audio->packs, demux->audio->bytes);
640 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
641 break;
643 if (demux->video->packs >= MAX_PACKS
644 || demux->video->bytes >= MAX_PACK_BYTES) {
645 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
646 demux->video->packs, demux->video->bytes);
647 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
648 break;
650 if (!demux_fill_buffer(demux, ds)) {
651 #if PARSE_ON_ADD && defined(CONFIG_LIBAVCODEC)
652 uint8_t *parsed_start = NULL;
653 int parsed_len = 0;
654 ds_parse(ds->sh, &parsed_start, &parsed_len, MP_NOPTS_VALUE, 0);
655 if (parsed_len) {
656 demux_packet_t *dp2 = new_demux_packet(parsed_len);
657 dp2->pts = MP_NOPTS_VALUE;
658 memcpy(dp2->buffer, parsed_start, parsed_len);
659 ds_add_packet_internal(ds, dp2);
660 continue;
662 #endif
663 mp_dbg(MSGT_DEMUXER, MSGL_DBG2,
664 "ds_fill_buffer()->demux_fill_buffer() failed\n");
665 break; // EOF
668 ds->buffer_pos = ds->buffer_size = 0;
669 ds->buffer = NULL;
670 mp_msg(MSGT_DEMUXER, MSGL_V,
671 "ds_fill_buffer: EOF reached (stream: %s) \n",
672 ds == demux->audio ? "audio" : "video");
673 ds->eof = 1;
674 return 0;
677 int demux_read_data(demux_stream_t *ds, unsigned char *mem, int len)
679 int x;
680 int bytes = 0;
681 while (len > 0) {
682 x = ds->buffer_size - ds->buffer_pos;
683 if (x == 0) {
684 if (!ds_fill_buffer(ds))
685 return bytes;
686 } else {
687 if (x > len)
688 x = len;
689 if (mem)
690 fast_memcpy(mem + bytes, &ds->buffer[ds->buffer_pos], x);
691 bytes += x;
692 len -= x;
693 ds->buffer_pos += x;
696 return bytes;
700 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
701 * \param mem memory to read data into, may be NULL to discard data
702 * \param maxlen maximum number of bytes to read
703 * \param read number of bytes actually read
704 * \param pattern pattern to search for (lowest 8 bits are ignored)
705 * \return whether pattern was found
707 int demux_pattern_3(demux_stream_t *ds, unsigned char *mem, int maxlen,
708 int *read, uint32_t pattern)
710 register uint32_t head = 0xffffff00;
711 register uint32_t pat = pattern & 0xffffff00;
712 int total_len = 0;
713 do {
714 register unsigned char *ds_buf = &ds->buffer[ds->buffer_size];
715 int len = ds->buffer_size - ds->buffer_pos;
716 register long pos = -len;
717 if (unlikely(pos >= 0)) { // buffer is empty
718 ds_fill_buffer(ds);
719 continue;
721 do {
722 head |= ds_buf[pos];
723 head <<= 8;
724 } while (++pos && head != pat);
725 len += pos;
726 if (total_len + len > maxlen)
727 len = maxlen - total_len;
728 len = demux_read_data(ds, mem ? &mem[total_len] : NULL, len);
729 total_len += len;
730 } while ((head != pat || total_len < 3) && total_len < maxlen && !ds->eof);
731 if (read)
732 *read = total_len;
733 return total_len >= 3 && head == pat;
736 void ds_free_packs(demux_stream_t *ds)
738 demux_packet_t *dp = ds->first;
739 while (dp) {
740 demux_packet_t *dn = dp->next;
741 free_demux_packet(dp);
742 dp = dn;
744 if (ds->asf_packet) {
745 // free unfinished .asf fragments:
746 free(ds->asf_packet->buffer);
747 free(ds->asf_packet);
748 ds->asf_packet = NULL;
750 ds->first = ds->last = NULL;
751 ds->packs = 0; // !!!!!
752 ds->bytes = 0;
753 if (ds->current)
754 free_demux_packet(ds->current);
755 ds->current = NULL;
756 ds->buffer = NULL;
757 ds->buffer_pos = ds->buffer_size;
758 ds->pts = 0;
759 ds->pts_bytes = 0;
762 int ds_get_packet(demux_stream_t *ds, unsigned char **start)
764 int len;
765 if (ds->buffer_pos >= ds->buffer_size) {
766 if (!ds_fill_buffer(ds)) {
767 // EOF
768 *start = NULL;
769 return -1;
772 len = ds->buffer_size - ds->buffer_pos;
773 *start = &ds->buffer[ds->buffer_pos];
774 ds->buffer_pos += len;
775 return len;
778 int ds_get_packet_pts(demux_stream_t *ds, unsigned char **start, double *pts)
780 int len;
781 *pts = MP_NOPTS_VALUE;
782 len = ds_get_packet(ds, start);
783 if (len < 0)
784 return len;
785 // Return pts unless this read starts from the middle of a packet
786 if (len == ds->buffer_pos)
787 *pts = ds->current->pts;
788 return len;
791 int ds_get_packet_sub(demux_stream_t *ds, unsigned char **start)
793 int len;
794 if (ds->buffer_pos >= ds->buffer_size) {
795 *start = NULL;
796 if (!ds->packs)
797 return -1; // no sub
798 if (!ds_fill_buffer(ds))
799 return -1; // EOF
801 len = ds->buffer_size - ds->buffer_pos;
802 *start = &ds->buffer[ds->buffer_pos];
803 ds->buffer_pos += len;
804 return len;
807 double ds_get_next_pts(demux_stream_t *ds)
809 demuxer_t *demux = ds->demuxer;
810 while (!ds->first) {
811 if (demux->audio->packs >= MAX_PACKS
812 || demux->audio->bytes >= MAX_PACK_BYTES) {
813 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
814 demux->audio->packs, demux->audio->bytes);
815 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
816 return MP_NOPTS_VALUE;
818 if (demux->video->packs >= MAX_PACKS
819 || demux->video->bytes >= MAX_PACK_BYTES) {
820 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
821 demux->video->packs, demux->video->bytes);
822 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
823 return MP_NOPTS_VALUE;
825 if (!demux_fill_buffer(demux, ds))
826 return MP_NOPTS_VALUE;
828 return ds->first->pts;
831 // ====================================================================
833 void demuxer_help(void)
835 int i;
837 mp_msg(MSGT_DEMUXER, MSGL_INFO, "Available demuxers:\n");
838 mp_msg(MSGT_DEMUXER, MSGL_INFO, " demuxer: type info: (comment)\n");
839 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DEMUXERS\n");
840 for (i = 0; demuxer_list[i]; i++) {
841 if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Don't display special demuxers
842 continue;
843 if (demuxer_list[i]->comment && strlen(demuxer_list[i]->comment))
844 mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s (%s)\n",
845 demuxer_list[i]->name, demuxer_list[i]->type,
846 demuxer_list[i]->info, demuxer_list[i]->comment);
847 else
848 mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s\n",
849 demuxer_list[i]->name, demuxer_list[i]->type,
850 demuxer_list[i]->info);
856 * Get demuxer type for a given demuxer name
858 * @param demuxer_name string with demuxer name of demuxer number
859 * @param force will be set if demuxer should be forced.
860 * May be NULL.
861 * @return DEMUXER_TYPE_xxx, -1 if error or not found
863 int get_demuxer_type_from_name(char *demuxer_name, int *force)
865 int i;
866 long type_int;
867 char *endptr;
869 if (!demuxer_name || !demuxer_name[0])
870 return DEMUXER_TYPE_UNKNOWN;
871 if (force)
872 *force = demuxer_name[0] == '+';
873 if (demuxer_name[0] == '+')
874 demuxer_name = &demuxer_name[1];
875 for (i = 0; demuxer_list[i]; i++) {
876 if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Can't select special demuxers from commandline
877 continue;
878 if (strcmp(demuxer_name, demuxer_list[i]->name) == 0)
879 return demuxer_list[i]->type;
882 // No match found, try to parse name as an integer (demuxer number)
883 type_int = strtol(demuxer_name, &endptr, 0);
884 if (*endptr) // Conversion failed
885 return -1;
886 if ((type_int > 0) && (type_int <= DEMUXER_TYPE_MAX))
887 return (int) type_int;
889 return -1;
892 int extension_parsing = 1; // 0=off 1=mixed (used only for unstable formats)
895 NOTE : Several demuxers may be opened at the same time so
896 demuxers should NEVER rely on an external var to enable them
897 self. If a demuxer can't do any auto-detection it should only use
898 file_format. The user can explicitly set file_format with the -demuxer
899 option so there is really no need for another extra var.
900 For convenience an option can be added to set file_format directly
901 to the right type (ex: rawaudio,rawvideo).
902 Also the stream can override the file_format so a demuxer which rely
903 on a special stream type can set file_format at the stream level
904 (ex: tv,mf).
907 static demuxer_t *demux_open_stream(struct MPOpts *opts, stream_t *stream,
908 int file_format, int force, int audio_id,
909 int video_id, int dvdsub_id,
910 char *filename)
912 demuxer_t *demuxer = NULL;
914 sh_video_t *sh_video = NULL;
916 const demuxer_desc_t *demuxer_desc;
917 int fformat = 0;
918 int i;
920 // If somebody requested a demuxer check it
921 if (file_format) {
922 if ((demuxer_desc = get_demuxer_desc_from_type(file_format))) {
923 demuxer = new_demuxer(opts, stream, demuxer_desc->type, audio_id,
924 video_id, dvdsub_id, filename);
925 if (demuxer_desc->check_file)
926 fformat = demuxer_desc->check_file(demuxer);
927 if (force || !demuxer_desc->check_file)
928 fformat = demuxer_desc->type;
929 if (fformat != 0) {
930 if (fformat == demuxer_desc->type) {
931 demuxer_t *demux2 = demuxer;
932 // Move messages to demuxer detection code?
933 mp_tmsg(MSGT_DEMUXER, MSGL_INFO,
934 "%s file format detected.\n",
935 demuxer_desc->shortdesc);
936 file_format = fformat;
937 if (!demuxer->desc->open
938 || (demux2 = demuxer->desc->open(demuxer))) {
939 demuxer = demux2;
940 goto dmx_open;
942 } else {
943 // Format changed after check, recurse
944 free_demuxer(demuxer);
945 return demux_open_stream(opts, stream, fformat, force,
946 audio_id, video_id, dvdsub_id,
947 filename);
950 // Check failed for forced demuxer, quit
951 free_demuxer(demuxer);
952 return NULL;
955 // Test demuxers with safe file checks
956 for (i = 0; (demuxer_desc = demuxer_list[i]); i++) {
957 if (demuxer_desc->safe_check) {
958 demuxer = new_demuxer(opts, stream, demuxer_desc->type, audio_id,
959 video_id, dvdsub_id, filename);
960 if ((fformat = demuxer_desc->check_file(demuxer)) != 0) {
961 if (fformat == demuxer_desc->type) {
962 demuxer_t *demux2 = demuxer;
963 mp_tmsg(MSGT_DEMUXER, MSGL_INFO,
964 "%s file format detected.\n",
965 demuxer_desc->shortdesc);
966 file_format = fformat;
967 if (!demuxer->desc->open
968 || (demux2 = demuxer->desc->open(demuxer))) {
969 demuxer = demux2;
970 goto dmx_open;
972 } else {
973 if (fformat == DEMUXER_TYPE_PLAYLIST)
974 return demuxer; // handled in mplayer.c
975 // Format changed after check, recurse
976 free_demuxer(demuxer);
977 demuxer = demux_open_stream(opts, stream, fformat, force,
978 audio_id, video_id,
979 dvdsub_id, filename);
980 if (demuxer)
981 return demuxer; // done!
982 file_format = DEMUXER_TYPE_UNKNOWN;
985 free_demuxer(demuxer);
986 demuxer = NULL;
990 // If no forced demuxer perform file extension based detection
991 // Ok. We're over the stable detectable fileformats, the next ones are
992 // a bit fuzzy. So by default (extension_parsing==1) try extension-based
993 // detection first:
994 if (file_format == DEMUXER_TYPE_UNKNOWN && filename
995 && extension_parsing == 1) {
996 file_format = demuxer_type_by_filename(filename);
997 if (file_format != DEMUXER_TYPE_UNKNOWN) {
998 // we like recursion :)
999 demuxer = demux_open_stream(opts, stream, file_format, force,
1000 audio_id, video_id, dvdsub_id,
1001 filename);
1002 if (demuxer)
1003 return demuxer; // done!
1004 file_format = DEMUXER_TYPE_UNKNOWN; // continue fuzzy guessing...
1005 mp_msg(MSGT_DEMUXER, MSGL_V,
1006 "demuxer: continue fuzzy content-based format guessing...\n");
1009 // Try detection for all other demuxers
1010 for (i = 0; (demuxer_desc = demuxer_list[i]); i++) {
1011 if (!demuxer_desc->safe_check && demuxer_desc->check_file) {
1012 demuxer = new_demuxer(opts, stream, demuxer_desc->type, audio_id,
1013 video_id, dvdsub_id, filename);
1014 if ((fformat = demuxer_desc->check_file(demuxer)) != 0) {
1015 if (fformat == demuxer_desc->type) {
1016 demuxer_t *demux2 = demuxer;
1017 mp_tmsg(MSGT_DEMUXER, MSGL_INFO,
1018 "%s file format detected.\n",
1019 demuxer_desc->shortdesc);
1020 file_format = fformat;
1021 if (!demuxer->desc->open
1022 || (demux2 = demuxer->desc->open(demuxer))) {
1023 demuxer = demux2;
1024 goto dmx_open;
1026 } else {
1027 if (fformat == DEMUXER_TYPE_PLAYLIST)
1028 return demuxer; // handled in mplayer.c
1029 // Format changed after check, recurse
1030 free_demuxer(demuxer);
1031 demuxer = demux_open_stream(opts, stream, fformat, force,
1032 audio_id, video_id,
1033 dvdsub_id, filename);
1034 if (demuxer)
1035 return demuxer; // done!
1036 file_format = DEMUXER_TYPE_UNKNOWN;
1039 free_demuxer(demuxer);
1040 demuxer = NULL;
1044 return NULL;
1045 //====== File format recognized, set up these for compatibility: =========
1046 dmx_open:
1048 demuxer->file_format = file_format;
1050 if ((sh_video = demuxer->video->sh) && sh_video->bih) {
1051 int biComp = le2me_32(sh_video->bih->biCompression);
1052 mp_msg(MSGT_DEMUX, MSGL_INFO,
1053 "VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
1054 (char *) &biComp, sh_video->bih->biWidth,
1055 sh_video->bih->biHeight, sh_video->bih->biBitCount,
1056 sh_video->fps, sh_video->i_bps * 0.008f,
1057 sh_video->i_bps / 1024.0f);
1059 #ifdef CONFIG_ASS
1060 if (opts->ass_enabled && ass_library) {
1061 for (i = 0; i < MAX_S_STREAMS; ++i) {
1062 sh_sub_t *sh = demuxer->s_streams[i];
1063 if (sh && sh->type == 'a') {
1064 sh->ass_track = ass_new_track(ass_library);
1065 if (sh->ass_track && sh->extradata)
1066 ass_process_codec_private(sh->ass_track, sh->extradata,
1067 sh->extradata_len);
1068 } else if (sh && sh->type != 'v')
1069 sh->ass_track = ass_default_track(ass_library);
1072 #endif
1073 return demuxer;
1076 char *audio_stream = NULL;
1077 char *sub_stream = NULL;
1078 int audio_stream_cache = 0;
1080 char *demuxer_name = NULL; // parameter from -demuxer
1081 char *audio_demuxer_name = NULL; // parameter from -audio-demuxer
1082 char *sub_demuxer_name = NULL; // parameter from -sub-demuxer
1084 extern int hr_mp3_seek;
1086 extern float stream_cache_min_percent;
1087 extern float stream_cache_seek_min_percent;
1089 demuxer_t *demux_open(struct MPOpts *opts, stream_t *vs, int file_format,
1090 int audio_id, int video_id, int dvdsub_id,
1091 char *filename)
1093 stream_t *as = NULL, *ss = NULL;
1094 demuxer_t *vd, *ad = NULL, *sd = NULL;
1095 demuxer_t *res;
1096 int afmt = DEMUXER_TYPE_UNKNOWN, sfmt = DEMUXER_TYPE_UNKNOWN;
1097 int demuxer_type;
1098 int audio_demuxer_type = 0, sub_demuxer_type = 0;
1099 int demuxer_force = 0, audio_demuxer_force = 0, sub_demuxer_force = 0;
1101 if ((demuxer_type =
1102 get_demuxer_type_from_name(demuxer_name, &demuxer_force)) < 0) {
1103 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-demuxer %s does not exist.\n",
1104 demuxer_name);
1106 if ((audio_demuxer_type =
1107 get_demuxer_type_from_name(audio_demuxer_name,
1108 &audio_demuxer_force)) < 0) {
1109 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-audio-demuxer %s does not exist.\n",
1110 audio_demuxer_name);
1112 if ((sub_demuxer_type =
1113 get_demuxer_type_from_name(sub_demuxer_name,
1114 &sub_demuxer_force)) < 0) {
1115 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-sub-demuxer %s does not exist.\n",
1116 sub_demuxer_name);
1119 if (audio_stream) {
1120 as = open_stream(audio_stream, 0, &afmt);
1121 if (!as) {
1122 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "Cannot open audio stream: %s\n",
1123 audio_stream);
1124 return NULL;
1126 if (audio_stream_cache) {
1127 if (!stream_enable_cache
1128 (as, audio_stream_cache * 1024,
1129 audio_stream_cache * 1024 * (stream_cache_min_percent /
1130 100.0),
1131 audio_stream_cache * 1024 * (stream_cache_seek_min_percent /
1132 100.0))) {
1133 free_stream(as);
1134 mp_msg(MSGT_DEMUXER, MSGL_ERR,
1135 "Can't enable audio stream cache\n");
1136 return NULL;
1140 if (sub_stream) {
1141 ss = open_stream(sub_stream, 0, &sfmt);
1142 if (!ss) {
1143 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "Cannot open subtitle stream: %s\n",
1144 sub_stream);
1145 return NULL;
1149 vd = demux_open_stream(opts, vs, demuxer_type ? demuxer_type : file_format,
1150 demuxer_force, audio_stream ? -2 : audio_id,
1151 video_id, sub_stream ? -2 : dvdsub_id, filename);
1152 if (!vd) {
1153 if (as)
1154 free_stream(as);
1155 if (ss)
1156 free_stream(ss);
1157 return NULL;
1159 if (as) {
1160 ad = demux_open_stream(opts, as,
1161 audio_demuxer_type ? audio_demuxer_type : afmt,
1162 audio_demuxer_force, audio_id, -2, -2,
1163 audio_stream);
1164 if (!ad) {
1165 mp_tmsg(MSGT_DEMUXER, MSGL_WARN, "Failed to open audio demuxer: %s\n",
1166 audio_stream);
1167 free_stream(as);
1168 } else if (ad->audio->sh
1169 && ((sh_audio_t *) ad->audio->sh)->format == 0x55) // MP3
1170 hr_mp3_seek = 1; // Enable high res seeking
1172 if (ss) {
1173 sd = demux_open_stream(opts, ss,
1174 sub_demuxer_type ? sub_demuxer_type : sfmt,
1175 sub_demuxer_force, -2, -2, dvdsub_id,
1176 sub_stream);
1177 if (!sd) {
1178 mp_tmsg(MSGT_DEMUXER, MSGL_WARN,
1179 "Failed to open subtitle demuxer: %s\n", sub_stream);
1180 free_stream(ss);
1184 if (ad && sd)
1185 res = new_demuxers_demuxer(vd, ad, sd);
1186 else if (ad)
1187 res = new_demuxers_demuxer(vd, ad, vd);
1188 else if (sd)
1189 res = new_demuxers_demuxer(vd, vd, sd);
1190 else
1191 res = vd;
1193 opts->correct_pts = opts->user_correct_pts;
1194 if (opts->correct_pts < 0)
1195 opts->correct_pts =
1196 demux_control(res, DEMUXER_CTRL_CORRECT_PTS,
1197 NULL) == DEMUXER_CTRL_OK;
1198 return res;
1202 void demux_flush(demuxer_t *demuxer)
1204 ds_free_packs(demuxer->video);
1205 ds_free_packs(demuxer->audio);
1206 ds_free_packs(demuxer->sub);
1209 int demux_seek(demuxer_t *demuxer, float rel_seek_secs, float audio_delay,
1210 int flags)
1212 demux_stream_t *d_audio = demuxer->audio;
1213 demux_stream_t *d_video = demuxer->video;
1214 sh_audio_t *sh_audio = d_audio->sh;
1215 sh_video_t *sh_video = d_video->sh;
1216 double tmp = 0;
1217 double pts;
1219 if (!demuxer->seekable) {
1220 if (demuxer->file_format == DEMUXER_TYPE_AVI)
1221 mp_tmsg(MSGT_SEEK, MSGL_WARN, "Cannot seek in raw AVI streams. (Index required, try with the -idx switch.)\n");
1222 #ifdef CONFIG_TV
1223 else if (demuxer->file_format == DEMUXER_TYPE_TV)
1224 mp_tmsg(MSGT_SEEK, MSGL_WARN, "TV input is not seekable! (Seeking will probably be for changing channels ;)\n");
1225 #endif
1226 else
1227 mp_tmsg(MSGT_SEEK, MSGL_WARN, "Cannot seek in this file.\n");
1228 return 0;
1230 // clear demux buffers:
1231 demux_flush(demuxer);
1232 if (sh_audio)
1233 sh_audio->a_buffer_len = 0;
1235 demuxer->stream->eof = 0;
1236 demuxer->video->eof = 0;
1237 demuxer->audio->eof = 0;
1239 if (sh_video)
1240 sh_video->timer = 0; // !!!!!!
1242 if (flags & SEEK_ABSOLUTE)
1243 pts = 0.0f;
1244 else {
1245 if (demuxer->stream_pts == MP_NOPTS_VALUE)
1246 goto dmx_seek;
1247 pts = demuxer->stream_pts;
1250 if (flags & SEEK_FACTOR) {
1251 if (stream_control(demuxer->stream, STREAM_CTRL_GET_TIME_LENGTH, &tmp)
1252 == STREAM_UNSUPPORTED)
1253 goto dmx_seek;
1254 pts += tmp * rel_seek_secs;
1255 } else
1256 pts += rel_seek_secs;
1258 if (stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_TIME, &pts) !=
1259 STREAM_UNSUPPORTED) {
1260 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1261 return 1;
1264 dmx_seek:
1265 if (demuxer->desc->seek)
1266 demuxer->desc->seek(demuxer, rel_seek_secs, audio_delay, flags);
1268 if (sh_audio)
1269 resync_audio_stream(sh_audio);
1271 return 1;
1274 int demux_info_add(demuxer_t *demuxer, const char *opt, const char *param)
1276 char **info = demuxer->info;
1277 int n = 0;
1280 for (n = 0; info && info[2 * n] != NULL; n++) {
1281 if (!strcasecmp(opt, info[2 * n])) {
1282 if (!strcmp(param, info[2 * n + 1])) {
1283 mp_msg(MSGT_DEMUX, MSGL_V, "Demuxer info %s set to unchanged value %s\n", opt, param);
1284 return 0;
1286 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "Demuxer info %s changed to %s\n", opt,
1287 param);
1288 free(info[2 * n + 1]);
1289 info[2 * n + 1] = strdup(param);
1290 return 0;
1294 info = demuxer->info =
1295 (char **) realloc(info, (2 * (n + 2)) * sizeof(char *));
1296 info[2 * n] = strdup(opt);
1297 info[2 * n + 1] = strdup(param);
1298 memset(&info[2 * (n + 1)], 0, 2 * sizeof(char *));
1300 return 1;
1303 int demux_info_print(demuxer_t *demuxer)
1305 char **info = demuxer->info;
1306 int n;
1308 if (!info)
1309 return 0;
1311 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "Clip info:\n");
1312 for (n = 0; info[2 * n] != NULL; n++) {
1313 mp_msg(MSGT_DEMUX, MSGL_INFO, " %s: %s\n", info[2 * n],
1314 info[2 * n + 1]);
1315 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_NAME%d=%s\n", n,
1316 info[2 * n]);
1317 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_VALUE%d=%s\n", n,
1318 info[2 * n + 1]);
1320 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_N=%d\n", n);
1322 return 0;
1325 char *demux_info_get(demuxer_t *demuxer, const char *opt)
1327 int i;
1328 char **info = demuxer->info;
1330 for (i = 0; info && info[2 * i] != NULL; i++) {
1331 if (!strcasecmp(opt, info[2 * i]))
1332 return info[2 * i + 1];
1335 return NULL;
1338 int demux_control(demuxer_t *demuxer, int cmd, void *arg)
1341 if (demuxer->desc->control)
1342 return demuxer->desc->control(demuxer, cmd, arg);
1344 return DEMUXER_CTRL_NOTIMPL;
1349 double demuxer_get_time_length(demuxer_t *demuxer)
1351 double get_time_ans;
1352 sh_video_t *sh_video = demuxer->video->sh;
1353 sh_audio_t *sh_audio = demuxer->audio->sh;
1354 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
1355 if (demux_control
1356 (demuxer, DEMUXER_CTRL_GET_TIME_LENGTH, (void *) &get_time_ans) <= 0) {
1357 if (sh_video && sh_video->i_bps && sh_audio && sh_audio->i_bps)
1358 get_time_ans = (double) (demuxer->movi_end -
1359 demuxer->movi_start) / (sh_video->i_bps +
1360 sh_audio->i_bps);
1361 else if (sh_video && sh_video->i_bps)
1362 get_time_ans = (double) (demuxer->movi_end -
1363 demuxer->movi_start) / sh_video->i_bps;
1364 else if (sh_audio && sh_audio->i_bps)
1365 get_time_ans = (double) (demuxer->movi_end -
1366 demuxer->movi_start) / sh_audio->i_bps;
1367 else
1368 get_time_ans = 0;
1370 return get_time_ans;
1374 * \brief demuxer_get_current_time() returns the time of the current play in three possible ways:
1375 * either when the stream reader satisfies STREAM_CTRL_GET_CURRENT_TIME (e.g. dvd)
1376 * or using sh_video->pts when the former method fails
1377 * 0 otherwise
1378 * \return the current play time
1380 int demuxer_get_current_time(demuxer_t *demuxer)
1382 double get_time_ans = 0;
1383 sh_video_t *sh_video = demuxer->video->sh;
1384 if (demuxer->stream_pts != MP_NOPTS_VALUE)
1385 get_time_ans = demuxer->stream_pts;
1386 else if (sh_video)
1387 get_time_ans = sh_video->pts;
1388 return (int) get_time_ans;
1391 int demuxer_get_percent_pos(demuxer_t *demuxer)
1393 int ans = 0;
1394 int res = demux_control(demuxer, DEMUXER_CTRL_GET_PERCENT_POS, &ans);
1395 int len = (demuxer->movi_end - demuxer->movi_start) / 100;
1396 if (res <= 0) {
1397 off_t pos = demuxer->filepos > 0 ? demuxer->filepos : stream_tell(demuxer->stream);
1398 if (len > 0)
1399 ans = (pos - demuxer->movi_start) / len;
1400 else
1401 ans = 0;
1403 if (ans < 0)
1404 ans = 0;
1405 if (ans > 100)
1406 ans = 100;
1407 return ans;
1410 int demuxer_switch_audio(demuxer_t *demuxer, int index)
1412 int res = demux_control(demuxer, DEMUXER_CTRL_SWITCH_AUDIO, &index);
1413 if (res == DEMUXER_CTRL_NOTIMPL)
1414 index = demuxer->audio->id;
1415 if (demuxer->audio->id >= 0)
1416 demuxer->audio->sh = demuxer->a_streams[demuxer->audio->id];
1417 else
1418 demuxer->audio->sh = NULL;
1419 return index;
1422 int demuxer_switch_video(demuxer_t *demuxer, int index)
1424 int res = demux_control(demuxer, DEMUXER_CTRL_SWITCH_VIDEO, &index);
1425 if (res == DEMUXER_CTRL_NOTIMPL)
1426 index = demuxer->video->id;
1427 if (demuxer->video->id >= 0)
1428 demuxer->video->sh = demuxer->v_streams[demuxer->video->id];
1429 else
1430 demuxer->video->sh = NULL;
1431 return index;
1434 int demuxer_add_attachment(demuxer_t *demuxer, const char *name,
1435 const char *type, const void *data, size_t size)
1437 if (!(demuxer->num_attachments & 31))
1438 demuxer->attachments = realloc(demuxer->attachments,
1439 (demuxer->num_attachments + 32) * sizeof(demux_attachment_t));
1441 demuxer->attachments[demuxer->num_attachments].name = strdup(name);
1442 demuxer->attachments[demuxer->num_attachments].type = strdup(type);
1443 demuxer->attachments[demuxer->num_attachments].data = malloc(size);
1444 memcpy(demuxer->attachments[demuxer->num_attachments].data, data, size);
1445 demuxer->attachments[demuxer->num_attachments].data_size = size;
1447 return demuxer->num_attachments++;
1450 int demuxer_add_chapter(demuxer_t *demuxer, const char *name, uint64_t start,
1451 uint64_t end)
1453 if (demuxer->chapters == NULL)
1454 demuxer->chapters = malloc(32 * sizeof(*demuxer->chapters));
1455 else if (!(demuxer->num_chapters % 32))
1456 demuxer->chapters = realloc(demuxer->chapters,
1457 (demuxer->num_chapters + 32) *
1458 sizeof(*demuxer->chapters));
1460 demuxer->chapters[demuxer->num_chapters].start = start;
1461 demuxer->chapters[demuxer->num_chapters].end = end;
1462 demuxer->chapters[demuxer->num_chapters].name = strdup(name ? name : mp_gtext("unknown"));
1464 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_ID=%d\n", demuxer->num_chapters);
1465 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_START=%"PRIu64"\n", demuxer->num_chapters, start);
1466 if (end)
1467 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_END=%"PRIu64"\n", demuxer->num_chapters, end);
1468 if (name)
1469 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_NAME=%s\n", demuxer->num_chapters, name);
1471 return demuxer->num_chapters++;
1475 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1476 * either using the demuxer->chapters structure set by the demuxer
1477 * or asking help to the stream layer (e.g. dvd)
1478 * \param chapter - chapter number wished - 0-based
1479 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1480 * \param chapter_name name of chapter found (set by this function is param is not null)
1481 * \return -1 on error, current chapter if successful
1484 int demuxer_seek_chapter(demuxer_t *demuxer, int chapter, double *seek_pts,
1485 char **chapter_name)
1487 int ris;
1488 sh_video_t *sh_video = demuxer->video->sh;
1489 sh_audio_t *sh_audio = demuxer->audio->sh;
1491 if (!demuxer->num_chapters || !demuxer->chapters) {
1492 demux_flush(demuxer);
1494 ris = stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_CHAPTER,
1495 &chapter);
1496 if (ris != STREAM_UNSUPPORTED)
1497 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1498 if (sh_video) {
1499 ds_fill_buffer(demuxer->video);
1500 resync_video_stream(sh_video);
1503 if (sh_audio) {
1504 ds_fill_buffer(demuxer->audio);
1505 resync_audio_stream(sh_audio);
1507 // exit status may be ok, but main() doesn't have to seek itself
1508 // (because e.g. dvds depend on sectors, not on pts)
1509 *seek_pts = -1.0;
1511 if (chapter_name) {
1512 *chapter_name = NULL;
1513 int num_chapters;
1514 if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS,
1515 &num_chapters) == STREAM_UNSUPPORTED)
1516 num_chapters = 0;
1517 if (num_chapters) {
1518 *chapter_name = talloc_size(NULL, 16);
1519 sprintf(*chapter_name, " of %3d", num_chapters);
1523 return ris != STREAM_UNSUPPORTED ? chapter : -1;
1524 } else { // chapters structure is set in the demuxer
1525 if (chapter >= demuxer->num_chapters)
1526 return -1;
1527 if (chapter < 0)
1528 chapter = 0;
1530 *seek_pts = demuxer->chapters[chapter].start / 1000.0;
1532 if (chapter_name)
1533 *chapter_name = talloc_strdup(NULL, demuxer->chapters[chapter].name);
1535 return chapter;
1539 int demuxer_get_current_chapter(demuxer_t *demuxer)
1541 int chapter = -1;
1542 if (!demuxer->num_chapters || !demuxer->chapters) {
1543 if (stream_control(demuxer->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
1544 &chapter) == STREAM_UNSUPPORTED)
1545 chapter = -1;
1546 } else {
1547 sh_video_t *sh_video = demuxer->video->sh;
1548 sh_audio_t *sh_audio = demuxer->audio->sh;
1549 uint64_t now;
1550 now = (sh_video ? sh_video->pts : (sh_audio ? sh_audio->pts : 0))
1551 * 1000 + 0.5;
1552 for (chapter = demuxer->num_chapters - 1; chapter >= 0; --chapter) {
1553 if (demuxer->chapters[chapter].start <= now)
1554 break;
1557 return chapter;
1560 char *demuxer_chapter_name(demuxer_t *demuxer, int chapter)
1562 if (demuxer->num_chapters && demuxer->chapters) {
1563 if (chapter >= 0 && chapter < demuxer->num_chapters
1564 && demuxer->chapters[chapter].name)
1565 return strdup(demuxer->chapters[chapter].name);
1567 return NULL;
1570 char *demuxer_chapter_display_name(demuxer_t *demuxer, int chapter)
1572 char *chapter_name = demuxer_chapter_name(demuxer, chapter);
1573 if (chapter_name) {
1574 char *tmp = malloc(strlen(chapter_name) + 14);
1575 snprintf(tmp, 63, "(%d) %s", chapter + 1, chapter_name);
1576 free(chapter_name);
1577 return tmp;
1578 } else {
1579 int chapter_num = demuxer_chapter_count(demuxer);
1580 char tmp[30];
1581 if (chapter_num <= 0)
1582 sprintf(tmp, "(%d)", chapter + 1);
1583 else
1584 sprintf(tmp, "(%d) of %d", chapter + 1, chapter_num);
1585 return strdup(tmp);
1589 float demuxer_chapter_time(demuxer_t *demuxer, int chapter, float *end)
1591 if (demuxer->num_chapters && demuxer->chapters && chapter >= 0
1592 && chapter < demuxer->num_chapters) {
1593 if (end)
1594 *end = demuxer->chapters[chapter].end / 1000.0;
1595 return demuxer->chapters[chapter].start / 1000.0;
1597 return -1.0;
1600 int demuxer_chapter_count(demuxer_t *demuxer)
1602 if (!demuxer->num_chapters || !demuxer->chapters) {
1603 int num_chapters = 0;
1604 if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS,
1605 &num_chapters) == STREAM_UNSUPPORTED)
1606 num_chapters = 0;
1607 return num_chapters;
1608 } else
1609 return demuxer->num_chapters;
1612 int demuxer_angles_count(demuxer_t *demuxer)
1614 int ris, angles = -1;
1616 ris = stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_ANGLES, &angles);
1617 if (ris == STREAM_UNSUPPORTED)
1618 return -1;
1619 return angles;
1622 int demuxer_get_current_angle(demuxer_t *demuxer)
1624 int ris, curr_angle = -1;
1625 ris = stream_control(demuxer->stream, STREAM_CTRL_GET_ANGLE, &curr_angle);
1626 if (ris == STREAM_UNSUPPORTED)
1627 return -1;
1628 return curr_angle;
1632 int demuxer_set_angle(demuxer_t *demuxer, int angle)
1634 int ris, angles = -1;
1635 sh_video_t *sh_video = demuxer->video->sh;
1636 sh_audio_t *sh_audio = demuxer->audio->sh;
1638 angles = demuxer_angles_count(demuxer);
1639 if ((angles < 1) || (angle > angles))
1640 return -1;
1642 demux_flush(demuxer);
1644 ris = stream_control(demuxer->stream, STREAM_CTRL_SET_ANGLE, &angle);
1645 if (ris == STREAM_UNSUPPORTED)
1646 return -1;
1648 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1649 if (sh_video) {
1650 ds_fill_buffer(demuxer->video);
1651 resync_video_stream(sh_video);
1654 if (sh_audio) {
1655 ds_fill_buffer(demuxer->audio);
1656 resync_audio_stream(sh_audio);
1659 return angle;
1662 int demuxer_audio_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_A_STREAMS; ++i) {
1668 sh_audio_t *sh = d->a_streams[i];
1669 if (sh && sh->lang && strncmp(sh->lang, lang, len) == 0)
1670 return sh->aid;
1672 lang += len;
1673 lang += strspn(lang, ",");
1675 return -1;
1678 int demuxer_sub_track_by_lang(demuxer_t *d, char *lang)
1680 int i, len;
1681 lang += strspn(lang, ",");
1682 while ((len = strcspn(lang, ",")) > 0) {
1683 for (i = 0; i < MAX_S_STREAMS; ++i) {
1684 sh_sub_t *sh = d->s_streams[i];
1685 if (sh && sh->lang && strncmp(sh->lang, lang, len) == 0)
1686 return sh->sid;
1688 lang += len;
1689 lang += strspn(lang, ",");
1691 return -1;
1694 int demuxer_default_audio_track(demuxer_t *d)
1696 int i;
1697 for (i = 0; i < MAX_A_STREAMS; ++i) {
1698 sh_audio_t *sh = d->a_streams[i];
1699 if (sh && sh->default_track)
1700 return sh->aid;
1702 for (i = 0; i < MAX_A_STREAMS; ++i) {
1703 sh_audio_t *sh = d->a_streams[i];
1704 if (sh)
1705 return sh->aid;
1707 return -1;
1710 int demuxer_default_sub_track(demuxer_t *d)
1712 int i;
1713 for (i = 0; i < MAX_S_STREAMS; ++i) {
1714 sh_sub_t *sh = d->s_streams[i];
1715 if (sh && sh->default_track)
1716 return sh->sid;
1718 return -1;