Ignore svn change r30877
[mplayer/glamo.git] / libmpdemux / demuxer.c
blobe00a9cfb685fe5ce34fc7b92aae212f9a838976d
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 static void clear_parser(sh_common_t *sh);
63 void resync_video_stream(sh_video_t *sh_video);
64 void resync_audio_stream(sh_audio_t *sh_audio);
66 // Demuxer list
67 extern const demuxer_desc_t demuxer_desc_rawaudio;
68 extern const demuxer_desc_t demuxer_desc_rawvideo;
69 extern const demuxer_desc_t demuxer_desc_tv;
70 extern const demuxer_desc_t demuxer_desc_mf;
71 extern const demuxer_desc_t demuxer_desc_avi;
72 extern const demuxer_desc_t demuxer_desc_y4m;
73 extern const demuxer_desc_t demuxer_desc_asf;
74 extern const demuxer_desc_t demuxer_desc_real;
75 extern const demuxer_desc_t demuxer_desc_smjpeg;
76 extern const demuxer_desc_t demuxer_desc_matroska;
77 extern const demuxer_desc_t demuxer_desc_realaudio;
78 extern const demuxer_desc_t demuxer_desc_vqf;
79 extern const demuxer_desc_t demuxer_desc_mov;
80 extern const demuxer_desc_t demuxer_desc_vivo;
81 extern const demuxer_desc_t demuxer_desc_fli;
82 extern const demuxer_desc_t demuxer_desc_film;
83 extern const demuxer_desc_t demuxer_desc_roq;
84 extern const demuxer_desc_t demuxer_desc_gif;
85 extern const demuxer_desc_t demuxer_desc_ogg;
86 extern const demuxer_desc_t demuxer_desc_avs;
87 extern const demuxer_desc_t demuxer_desc_pva;
88 extern const demuxer_desc_t demuxer_desc_nsv;
89 extern const demuxer_desc_t demuxer_desc_mpeg_ts;
90 extern const demuxer_desc_t demuxer_desc_lmlm4;
91 extern const demuxer_desc_t demuxer_desc_mpeg_ps;
92 extern const demuxer_desc_t demuxer_desc_mpeg_pes;
93 extern const demuxer_desc_t demuxer_desc_mpeg_es;
94 extern const demuxer_desc_t demuxer_desc_mpeg_gxf;
95 extern const demuxer_desc_t demuxer_desc_mpeg4_es;
96 extern const demuxer_desc_t demuxer_desc_h264_es;
97 extern const demuxer_desc_t demuxer_desc_rawdv;
98 extern const demuxer_desc_t demuxer_desc_mpc;
99 extern const demuxer_desc_t demuxer_desc_audio;
100 extern const demuxer_desc_t demuxer_desc_xmms;
101 extern const demuxer_desc_t demuxer_desc_mpeg_ty;
102 extern const demuxer_desc_t demuxer_desc_rtp;
103 extern const demuxer_desc_t demuxer_desc_rtp_nemesi;
104 extern const demuxer_desc_t demuxer_desc_lavf;
105 extern const demuxer_desc_t demuxer_desc_lavf_preferred;
106 extern const demuxer_desc_t demuxer_desc_aac;
107 extern const demuxer_desc_t demuxer_desc_nut;
108 extern const demuxer_desc_t demuxer_desc_mng;
110 /* Please do not add any new demuxers here. If you want to implement a new
111 * demuxer, add it to libavformat, except for wrappers around external
112 * libraries and demuxers requiring binary support. */
114 const demuxer_desc_t *const demuxer_list[] = {
115 &demuxer_desc_rawaudio,
116 &demuxer_desc_rawvideo,
117 #ifdef CONFIG_TV
118 &demuxer_desc_tv,
119 #endif
120 &demuxer_desc_mf,
121 #ifdef CONFIG_LIBAVFORMAT
122 &demuxer_desc_lavf_preferred,
123 #endif
124 &demuxer_desc_avi,
125 &demuxer_desc_y4m,
126 &demuxer_desc_asf,
127 &demuxer_desc_nsv,
128 &demuxer_desc_real,
129 &demuxer_desc_smjpeg,
130 &demuxer_desc_matroska,
131 &demuxer_desc_realaudio,
132 &demuxer_desc_vqf,
133 &demuxer_desc_mov,
134 &demuxer_desc_vivo,
135 &demuxer_desc_fli,
136 &demuxer_desc_film,
137 &demuxer_desc_roq,
138 #ifdef CONFIG_GIF
139 &demuxer_desc_gif,
140 #endif
141 #ifdef CONFIG_OGGVORBIS
142 &demuxer_desc_ogg,
143 #endif
144 #ifdef CONFIG_WIN32DLL
145 &demuxer_desc_avs,
146 #endif
147 &demuxer_desc_pva,
148 &demuxer_desc_mpeg_ts,
149 &demuxer_desc_lmlm4,
150 &demuxer_desc_mpeg_ps,
151 &demuxer_desc_mpeg_pes,
152 &demuxer_desc_mpeg_es,
153 &demuxer_desc_mpeg_gxf,
154 &demuxer_desc_mpeg4_es,
155 &demuxer_desc_h264_es,
156 &demuxer_desc_audio,
157 &demuxer_desc_mpeg_ty,
158 #ifdef CONFIG_LIVE555
159 &demuxer_desc_rtp,
160 #endif
161 #ifdef CONFIG_LIBNEMESI
162 &demuxer_desc_rtp_nemesi,
163 #endif
164 #ifdef CONFIG_LIBAVFORMAT
165 &demuxer_desc_lavf,
166 #endif
167 #ifdef CONFIG_MUSEPACK
168 &demuxer_desc_mpc,
169 #endif
170 #ifdef CONFIG_LIBDV095
171 &demuxer_desc_rawdv,
172 #endif
173 &demuxer_desc_aac,
174 #ifdef CONFIG_LIBNUT
175 &demuxer_desc_nut,
176 #endif
177 #ifdef CONFIG_XMMS
178 &demuxer_desc_xmms,
179 #endif
180 #ifdef CONFIG_MNG
181 &demuxer_desc_mng,
182 #endif
183 /* Please do not add any new demuxers here. If you want to implement a new
184 * demuxer, add it to libavformat, except for wrappers around external
185 * libraries and demuxers requiring binary support. */
186 NULL
189 void free_demuxer_stream(demux_stream_t *ds)
191 ds_free_packs(ds);
192 free(ds);
195 demux_stream_t *new_demuxer_stream(struct demuxer *demuxer, int id)
197 demux_stream_t *ds = malloc(sizeof(demux_stream_t));
198 *ds = (demux_stream_t){
199 .id = id,
200 .demuxer = demuxer,
201 .asf_seq = -1,
203 return ds;
208 * Get demuxer description structure for a given demuxer type
210 * @param file_format type of the demuxer
211 * @return structure for the demuxer, NULL if not found
213 static const demuxer_desc_t *get_demuxer_desc_from_type(int file_format)
215 int i;
217 for (i = 0; demuxer_list[i]; i++)
218 if (file_format == demuxer_list[i]->type)
219 return demuxer_list[i];
221 return NULL;
225 demuxer_t *new_demuxer(struct MPOpts *opts, stream_t *stream, int type,
226 int a_id, int v_id, int s_id, char *filename)
228 struct demuxer *d = talloc_zero(NULL, struct demuxer);
229 d->stream = stream;
230 d->stream_pts = MP_NOPTS_VALUE;
231 d->reference_clock = MP_NOPTS_VALUE;
232 d->movi_start = stream->start_pos;
233 d->movi_end = stream->end_pos;
234 d->seekable = 1;
235 d->synced = 0;
236 d->filepos = -1;
237 d->audio = new_demuxer_stream(d, a_id);
238 d->video = new_demuxer_stream(d, v_id);
239 d->sub = new_demuxer_stream(d, s_id);
240 d->type = type;
241 d->opts = opts;
242 if (type)
243 if (!(d->desc = get_demuxer_desc_from_type(type)))
244 mp_msg(MSGT_DEMUXER, MSGL_ERR,
245 "BUG! Invalid demuxer type in new_demuxer(), "
246 "big troubles ahead.");
247 if (filename) // Filename hack for avs_check_file
248 d->filename = strdup(filename);
249 stream->eof = 0;
250 stream_seek(stream, stream->start_pos);
251 return d;
254 sh_sub_t *new_sh_sub_sid(demuxer_t *demuxer, int id, int sid)
256 if (id > MAX_S_STREAMS - 1 || id < 0) {
257 mp_msg(MSGT_DEMUXER, MSGL_WARN,
258 "Requested sub stream id overflow (%d > %d)\n", id,
259 MAX_S_STREAMS);
260 return NULL;
262 if (demuxer->s_streams[id])
263 mp_msg(MSGT_DEMUXER, MSGL_WARN, "Sub stream %i redefined\n", id);
264 else {
265 sh_sub_t *sh = calloc(1, sizeof(sh_sub_t));
266 demuxer->s_streams[id] = sh;
267 sh->sid = sid;
268 sh->opts = demuxer->opts;
269 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SUBTITLE_ID=%d\n", sid);
271 if (sid == demuxer->opts->sub_id) {
272 demuxer->sub->id = id;
273 demuxer->sub->sh = demuxer->s_streams[id];
275 return demuxer->s_streams[id];
278 static void free_sh_sub(sh_sub_t *sh)
280 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_sub at %p\n", sh);
281 free(sh->extradata);
282 #ifdef CONFIG_ASS
283 if (sh->ass_track)
284 ass_free_track(sh->ass_track);
285 #endif
286 free(sh->lang);
287 #ifdef CONFIG_LIBAVCODEC
288 clear_parser((sh_common_t *)sh);
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 clear_parser((sh_common_t *)sh);
330 #endif
331 free(sh);
334 sh_video_t *new_sh_video_vid(demuxer_t *demuxer, int id, int vid)
336 if (id > MAX_V_STREAMS - 1 || id < 0) {
337 mp_msg(MSGT_DEMUXER, MSGL_WARN,
338 "Requested video stream id overflow (%d > %d)\n", id,
339 MAX_V_STREAMS);
340 return NULL;
342 if (demuxer->v_streams[id])
343 mp_tmsg(MSGT_DEMUXER, MSGL_WARN, "WARNING: Video stream header %d redefined.\n", id);
344 else {
345 mp_tmsg(MSGT_DEMUXER, MSGL_V, "==> Found video stream: %d\n", id);
346 sh_video_t *sh = calloc(1, sizeof *sh);
347 demuxer->v_streams[id] = sh;
348 sh->vid = vid;
349 sh->ds = demuxer->video;
350 sh->opts = demuxer->opts;
351 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_ID=%d\n", vid);
353 return demuxer->v_streams[id];
356 void free_sh_video(sh_video_t *sh)
358 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_video at %p\n", sh);
359 free(sh->bih);
360 #ifdef CONFIG_LIBAVCODEC
361 clear_parser((sh_common_t *)sh);
362 #endif
363 free(sh);
366 void free_demuxer(demuxer_t *demuxer)
368 int i;
369 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing %s demuxer at %p\n",
370 demuxer->desc->shortdesc, demuxer);
371 if (demuxer->desc->close)
372 demuxer->desc->close(demuxer);
373 // Very ugly hack to make it behave like old implementation
374 if (demuxer->desc->type == DEMUXER_TYPE_DEMUXERS)
375 goto skip_streamfree;
376 // free streams:
377 for (i = 0; i < MAX_A_STREAMS; i++)
378 if (demuxer->a_streams[i])
379 free_sh_audio(demuxer, i);
380 for (i = 0; i < MAX_V_STREAMS; i++)
381 if (demuxer->v_streams[i])
382 free_sh_video(demuxer->v_streams[i]);
383 for (i = 0; i < MAX_S_STREAMS; i++)
384 if (demuxer->s_streams[i])
385 free_sh_sub(demuxer->s_streams[i]);
386 // free demuxers:
387 free_demuxer_stream(demuxer->audio);
388 free_demuxer_stream(demuxer->video);
389 free_demuxer_stream(demuxer->sub);
390 skip_streamfree:
391 if (demuxer->info) {
392 for (i = 0; demuxer->info[i] != NULL; i++)
393 free(demuxer->info[i]);
394 free(demuxer->info);
396 free(demuxer->filename);
397 if (demuxer->teletext)
398 teletext_control(demuxer->teletext, TV_VBI_CONTROL_STOP, NULL);
399 talloc_free(demuxer);
403 static void ds_add_packet_internal(demux_stream_t *ds, demux_packet_t *dp)
405 // append packet to DS stream:
406 ++ds->packs;
407 ds->bytes += dp->len;
408 if (ds->last) {
409 // next packet in stream
410 ds->last->next = dp;
411 ds->last = dp;
412 } else {
413 // first packet in stream
414 ds->first = ds->last = dp;
416 mp_dbg(MSGT_DEMUXER, MSGL_DBG2,
417 "DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
418 (ds == ds->demuxer->audio) ? "d_audio" : "d_video", dp->len,
419 dp->pts, (unsigned int) dp->pos, ds->demuxer->audio->packs,
420 ds->demuxer->video->packs);
423 #ifdef CONFIG_LIBAVCODEC
424 static void allocate_parser(AVCodecContext **avctx, AVCodecParserContext **parser, unsigned format)
426 enum CodecID codec_id = CODEC_ID_NONE;
427 extern int avcodec_initialized;
428 if (!avcodec_initialized) {
429 avcodec_init();
430 avcodec_register_all();
431 avcodec_initialized = 1;
433 switch (format) {
434 case 0x2000:
435 case 0x332D6361:
436 case 0x332D4341:
437 case MKTAG('d', 'n', 'e', 't'):
438 case MKTAG('s', 'a', 'c', '3'):
439 codec_id = CODEC_ID_AC3;
440 break;
441 case MKTAG('E', 'A', 'C', '3'):
442 codec_id = CODEC_ID_EAC3;
443 break;
444 case 0x2001:
445 case 0x86:
446 codec_id = CODEC_ID_DTS;
447 break;
448 case MKTAG('M', 'L', 'P', ' '):
449 codec_id = CODEC_ID_MLP;
450 break;
451 case 0x55:
452 case 0x5500736d:
453 case MKTAG('.', 'm', 'p', '3'):
454 case MKTAG('M', 'P', 'E', ' '):
455 case MKTAG('L', 'A', 'M', 'E'):
456 codec_id = CODEC_ID_MP3;
457 break;
458 case 0x50:
459 case MKTAG('.', 'm', 'p', '2'):
460 case MKTAG('.', 'm', 'p', '1'):
461 codec_id = CODEC_ID_MP2;
462 break;
463 case MKTAG('T', 'R', 'H', 'D'):
464 codec_id = CODEC_ID_TRUEHD;
465 break;
467 if (codec_id != CODEC_ID_NONE) {
468 *avctx = avcodec_alloc_context();
469 if (!*avctx)
470 return;
471 *parser = av_parser_init(codec_id);
472 if (!*parser)
473 av_freep(avctx);
477 static void get_parser(sh_common_t *sh, AVCodecContext **avctx, AVCodecParserContext **parser)
479 *avctx = NULL;
480 *parser = NULL;
482 if (!sh || !sh->needs_parsing)
483 return;
485 *avctx = sh->avctx;
486 *parser = sh->parser;
487 if (*parser)
488 return;
490 allocate_parser(avctx, parser, sh->format);
491 sh->avctx = *avctx;
492 sh->parser = *parser;
495 int ds_parse(demux_stream_t *ds, uint8_t **buffer, int *len, double pts, off_t pos)
497 AVCodecContext *avctx;
498 AVCodecParserContext *parser;
499 get_parser(ds->sh, &avctx, &parser);
500 if (!parser)
501 return *len;
502 return av_parser_parse2(parser, avctx, buffer, len, *buffer, *len, pts, pts, pos);
505 static void clear_parser(sh_common_t *sh)
507 av_parser_close(sh->parser);
508 sh->parser = NULL;
509 av_freep(&sh->avctx);
512 void ds_clear_parser(demux_stream_t *ds)
514 if (!ds->sh)
515 return;
516 clear_parser(ds->sh);
518 #endif
520 void ds_add_packet(demux_stream_t *ds, demux_packet_t *dp)
522 #if PARSE_ON_ADD && defined(CONFIG_LIBAVCODEC)
523 int len = dp->len;
524 int pos = 0;
525 while (len > 0) {
526 uint8_t *parsed_start = dp->buffer + pos;
527 int parsed_len = len;
528 int consumed = ds_parse(ds->sh, &parsed_start, &parsed_len, dp->pts, dp->pos);
529 pos += consumed;
530 len -= consumed;
531 if (parsed_start == dp->buffer && parsed_len == dp->len) {
532 ds_add_packet_internal(ds, dp);
533 } else if (parsed_len) {
534 demux_packet_t *dp2 = new_demux_packet(parsed_len);
535 dp2->pos = dp->pos;
536 dp2->pts = dp->pts; // should be parser->pts but that works badly
537 memcpy(dp2->buffer, parsed_start, parsed_len);
538 ds_add_packet_internal(ds, dp2);
541 #else
542 ds_add_packet_internal(ds, dp);
543 #endif
546 void ds_read_packet(demux_stream_t *ds, stream_t *stream, int len,
547 double pts, off_t pos, int flags)
549 demux_packet_t *dp = new_demux_packet(len);
550 len = stream_read(stream, dp->buffer, len);
551 resize_demux_packet(dp, len);
552 dp->pts = pts;
553 dp->pos = pos;
554 dp->flags = flags;
555 // append packet to DS stream:
556 ds_add_packet(ds, dp);
559 // return value:
560 // 0 = EOF or no stream found or invalid type
561 // 1 = successfully read a packet
563 int demux_fill_buffer(demuxer_t *demux, demux_stream_t *ds)
565 // Note: parameter 'ds' can be NULL!
566 return demux->desc->fill_buffer(demux, ds);
569 // return value:
570 // 0 = EOF
571 // 1 = successful
572 #define MAX_ACUMULATED_PACKETS 64
573 int ds_fill_buffer(demux_stream_t *ds)
575 demuxer_t *demux = ds->demuxer;
576 if (ds->current)
577 free_demux_packet(ds->current);
578 ds->current = NULL;
579 if (mp_msg_test(MSGT_DEMUXER, MSGL_DBG3)) {
580 if (ds == demux->audio)
581 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
582 "ds_fill_buffer(d_audio) called\n");
583 else if (ds == demux->video)
584 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
585 "ds_fill_buffer(d_video) called\n");
586 else if (ds == demux->sub)
587 mp_dbg(MSGT_DEMUXER, MSGL_DBG3, "ds_fill_buffer(d_sub) called\n");
588 else
589 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
590 "ds_fill_buffer(unknown 0x%X) called\n", (unsigned int) ds);
592 while (1) {
593 if (ds->packs) {
594 demux_packet_t *p = ds->first;
595 #if 0
596 if (demux->reference_clock != MP_NOPTS_VALUE) {
597 if ( p->pts != MP_NOPTS_VALUE
598 && p->pts > demux->reference_clock
599 && ds->packs < MAX_ACUMULATED_PACKETS) {
600 if (demux_fill_buffer(demux, ds))
601 continue;
604 #endif
605 // copy useful data:
606 ds->buffer = p->buffer;
607 ds->buffer_pos = 0;
608 ds->buffer_size = p->len;
609 ds->pos = p->pos;
610 ds->dpos += p->len; // !!!
611 ++ds->pack_no;
612 if (p->pts != MP_NOPTS_VALUE) {
613 ds->pts = p->pts;
614 ds->pts_bytes = 0;
616 ds->pts_bytes += p->len; // !!!
617 if (p->stream_pts != MP_NOPTS_VALUE)
618 demux->stream_pts = p->stream_pts;
619 ds->flags = p->flags;
620 // unlink packet:
621 ds->bytes -= p->len;
622 ds->current = p;
623 ds->first = p->next;
624 if (!ds->first)
625 ds->last = NULL;
626 --ds->packs;
627 /* The code below can set ds->eof to 1 when another stream runs
628 * out of buffer space. That makes sense because in that situation
629 * the calling code should not count on being able to demux more
630 * packets from this stream.
631 * If however the situation improves and we're called again
632 * despite the eof flag then it's better to clear it to avoid
633 * weird behavior. */
634 ds->eof = 0;
635 return 1;
638 #define MaybeNI _("Maybe you are playing a non-interleaved stream/file or the codec failed?\n" \
639 "For AVI files, try to force non-interleaved mode with the -ni option.\n")
641 if (demux->audio->packs >= MAX_PACKS
642 || demux->audio->bytes >= MAX_PACK_BYTES) {
643 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
644 demux->audio->packs, demux->audio->bytes);
645 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
646 break;
648 if (demux->video->packs >= MAX_PACKS
649 || demux->video->bytes >= MAX_PACK_BYTES) {
650 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
651 demux->video->packs, demux->video->bytes);
652 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
653 break;
655 if (!demux_fill_buffer(demux, ds)) {
656 #if PARSE_ON_ADD && defined(CONFIG_LIBAVCODEC)
657 uint8_t *parsed_start = NULL;
658 int parsed_len = 0;
659 ds_parse(ds->sh, &parsed_start, &parsed_len, MP_NOPTS_VALUE, 0);
660 if (parsed_len) {
661 demux_packet_t *dp2 = new_demux_packet(parsed_len);
662 dp2->pts = MP_NOPTS_VALUE;
663 memcpy(dp2->buffer, parsed_start, parsed_len);
664 ds_add_packet_internal(ds, dp2);
665 continue;
667 #endif
668 mp_dbg(MSGT_DEMUXER, MSGL_DBG2,
669 "ds_fill_buffer()->demux_fill_buffer() failed\n");
670 break; // EOF
673 ds->buffer_pos = ds->buffer_size = 0;
674 ds->buffer = NULL;
675 mp_msg(MSGT_DEMUXER, MSGL_V,
676 "ds_fill_buffer: EOF reached (stream: %s) \n",
677 ds == demux->audio ? "audio" : "video");
678 ds->eof = 1;
679 return 0;
682 int demux_read_data(demux_stream_t *ds, unsigned char *mem, int len)
684 int x;
685 int bytes = 0;
686 while (len > 0) {
687 x = ds->buffer_size - ds->buffer_pos;
688 if (x == 0) {
689 if (!ds_fill_buffer(ds))
690 return bytes;
691 } else {
692 if (x > len)
693 x = len;
694 if (mem)
695 fast_memcpy(mem + bytes, &ds->buffer[ds->buffer_pos], x);
696 bytes += x;
697 len -= x;
698 ds->buffer_pos += x;
701 return bytes;
705 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
706 * \param mem memory to read data into, may be NULL to discard data
707 * \param maxlen maximum number of bytes to read
708 * \param read number of bytes actually read
709 * \param pattern pattern to search for (lowest 8 bits are ignored)
710 * \return whether pattern was found
712 int demux_pattern_3(demux_stream_t *ds, unsigned char *mem, int maxlen,
713 int *read, uint32_t pattern)
715 register uint32_t head = 0xffffff00;
716 register uint32_t pat = pattern & 0xffffff00;
717 int total_len = 0;
718 do {
719 register unsigned char *ds_buf = &ds->buffer[ds->buffer_size];
720 int len = ds->buffer_size - ds->buffer_pos;
721 register long pos = -len;
722 if (unlikely(pos >= 0)) { // buffer is empty
723 ds_fill_buffer(ds);
724 continue;
726 do {
727 head |= ds_buf[pos];
728 head <<= 8;
729 } while (++pos && head != pat);
730 len += pos;
731 if (total_len + len > maxlen)
732 len = maxlen - total_len;
733 len = demux_read_data(ds, mem ? &mem[total_len] : NULL, len);
734 total_len += len;
735 } while ((head != pat || total_len < 3) && total_len < maxlen && !ds->eof);
736 if (read)
737 *read = total_len;
738 return total_len >= 3 && head == pat;
741 void ds_free_packs(demux_stream_t *ds)
743 demux_packet_t *dp = ds->first;
744 while (dp) {
745 demux_packet_t *dn = dp->next;
746 free_demux_packet(dp);
747 dp = dn;
749 if (ds->asf_packet) {
750 // free unfinished .asf fragments:
751 free(ds->asf_packet->buffer);
752 free(ds->asf_packet);
753 ds->asf_packet = NULL;
755 ds->first = ds->last = NULL;
756 ds->packs = 0; // !!!!!
757 ds->bytes = 0;
758 if (ds->current)
759 free_demux_packet(ds->current);
760 ds->current = NULL;
761 ds->buffer = NULL;
762 ds->buffer_pos = ds->buffer_size;
763 ds->pts = 0;
764 ds->pts_bytes = 0;
767 int ds_get_packet(demux_stream_t *ds, unsigned char **start)
769 int len;
770 if (ds->buffer_pos >= ds->buffer_size) {
771 if (!ds_fill_buffer(ds)) {
772 // EOF
773 *start = NULL;
774 return -1;
777 len = ds->buffer_size - ds->buffer_pos;
778 *start = &ds->buffer[ds->buffer_pos];
779 ds->buffer_pos += len;
780 return len;
783 int ds_get_packet_pts(demux_stream_t *ds, unsigned char **start, double *pts)
785 int len;
786 *pts = MP_NOPTS_VALUE;
787 len = ds_get_packet(ds, start);
788 if (len < 0)
789 return len;
790 // Return pts unless this read starts from the middle of a packet
791 if (len == ds->buffer_pos)
792 *pts = ds->current->pts;
793 return len;
796 int ds_get_packet_sub(demux_stream_t *ds, unsigned char **start)
798 int len;
799 if (ds->buffer_pos >= ds->buffer_size) {
800 *start = NULL;
801 if (!ds->packs)
802 return -1; // no sub
803 if (!ds_fill_buffer(ds))
804 return -1; // EOF
806 len = ds->buffer_size - ds->buffer_pos;
807 *start = &ds->buffer[ds->buffer_pos];
808 ds->buffer_pos += len;
809 return len;
812 double ds_get_next_pts(demux_stream_t *ds)
814 demuxer_t *demux = ds->demuxer;
815 while (!ds->first) {
816 if (demux->audio->packs >= MAX_PACKS
817 || demux->audio->bytes >= MAX_PACK_BYTES) {
818 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
819 demux->audio->packs, demux->audio->bytes);
820 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
821 return MP_NOPTS_VALUE;
823 if (demux->video->packs >= MAX_PACKS
824 || demux->video->bytes >= MAX_PACK_BYTES) {
825 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
826 demux->video->packs, demux->video->bytes);
827 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
828 return MP_NOPTS_VALUE;
830 if (!demux_fill_buffer(demux, ds))
831 return MP_NOPTS_VALUE;
833 return ds->first->pts;
836 // ====================================================================
838 void demuxer_help(void)
840 int i;
842 mp_msg(MSGT_DEMUXER, MSGL_INFO, "Available demuxers:\n");
843 mp_msg(MSGT_DEMUXER, MSGL_INFO, " demuxer: type info: (comment)\n");
844 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DEMUXERS\n");
845 for (i = 0; demuxer_list[i]; i++) {
846 if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Don't display special demuxers
847 continue;
848 if (demuxer_list[i]->comment && strlen(demuxer_list[i]->comment))
849 mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s (%s)\n",
850 demuxer_list[i]->name, demuxer_list[i]->type,
851 demuxer_list[i]->info, demuxer_list[i]->comment);
852 else
853 mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s\n",
854 demuxer_list[i]->name, demuxer_list[i]->type,
855 demuxer_list[i]->info);
861 * Get demuxer type for a given demuxer name
863 * @param demuxer_name string with demuxer name of demuxer number
864 * @param force will be set if demuxer should be forced.
865 * May be NULL.
866 * @return DEMUXER_TYPE_xxx, -1 if error or not found
868 int get_demuxer_type_from_name(char *demuxer_name, int *force)
870 int i;
871 long type_int;
872 char *endptr;
874 if (!demuxer_name || !demuxer_name[0])
875 return DEMUXER_TYPE_UNKNOWN;
876 if (force)
877 *force = demuxer_name[0] == '+';
878 if (demuxer_name[0] == '+')
879 demuxer_name = &demuxer_name[1];
880 for (i = 0; demuxer_list[i]; i++) {
881 if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Can't select special demuxers from commandline
882 continue;
883 if (strcmp(demuxer_name, demuxer_list[i]->name) == 0)
884 return demuxer_list[i]->type;
887 // No match found, try to parse name as an integer (demuxer number)
888 type_int = strtol(demuxer_name, &endptr, 0);
889 if (*endptr) // Conversion failed
890 return -1;
891 if ((type_int > 0) && (type_int <= DEMUXER_TYPE_MAX))
892 return (int) type_int;
894 return -1;
897 int extension_parsing = 1; // 0=off 1=mixed (used only for unstable formats)
900 NOTE : Several demuxers may be opened at the same time so
901 demuxers should NEVER rely on an external var to enable them
902 self. If a demuxer can't do any auto-detection it should only use
903 file_format. The user can explicitly set file_format with the -demuxer
904 option so there is really no need for another extra var.
905 For convenience an option can be added to set file_format directly
906 to the right type (ex: rawaudio,rawvideo).
907 Also the stream can override the file_format so a demuxer which rely
908 on a special stream type can set file_format at the stream level
909 (ex: tv,mf).
912 static demuxer_t *demux_open_stream(struct MPOpts *opts, stream_t *stream,
913 int file_format, int force, int audio_id,
914 int video_id, int dvdsub_id,
915 char *filename)
917 demuxer_t *demuxer = NULL;
919 sh_video_t *sh_video = NULL;
921 const demuxer_desc_t *demuxer_desc;
922 int fformat = 0;
923 int i;
925 // If somebody requested a demuxer check it
926 if (file_format) {
927 if ((demuxer_desc = get_demuxer_desc_from_type(file_format))) {
928 demuxer = new_demuxer(opts, stream, demuxer_desc->type, audio_id,
929 video_id, dvdsub_id, filename);
930 if (demuxer_desc->check_file)
931 fformat = demuxer_desc->check_file(demuxer);
932 if (force || !demuxer_desc->check_file)
933 fformat = demuxer_desc->type;
934 if (fformat != 0) {
935 if (fformat == demuxer_desc->type) {
936 demuxer_t *demux2 = demuxer;
937 // Move messages to demuxer detection code?
938 mp_tmsg(MSGT_DEMUXER, MSGL_INFO,
939 "%s file format detected.\n",
940 demuxer_desc->shortdesc);
941 file_format = fformat;
942 if (!demuxer->desc->open
943 || (demux2 = demuxer->desc->open(demuxer))) {
944 demuxer = demux2;
945 goto dmx_open;
947 } else {
948 // Format changed after check, recurse
949 free_demuxer(demuxer);
950 return demux_open_stream(opts, stream, fformat, force,
951 audio_id, video_id, dvdsub_id,
952 filename);
955 // Check failed for forced demuxer, quit
956 free_demuxer(demuxer);
957 return NULL;
960 // Test demuxers with safe file checks
961 for (i = 0; (demuxer_desc = demuxer_list[i]); i++) {
962 if (demuxer_desc->safe_check) {
963 demuxer = new_demuxer(opts, stream, demuxer_desc->type, audio_id,
964 video_id, dvdsub_id, filename);
965 if ((fformat = demuxer_desc->check_file(demuxer)) != 0) {
966 if (fformat == demuxer_desc->type) {
967 demuxer_t *demux2 = demuxer;
968 mp_tmsg(MSGT_DEMUXER, MSGL_INFO,
969 "%s file format detected.\n",
970 demuxer_desc->shortdesc);
971 file_format = fformat;
972 if (!demuxer->desc->open
973 || (demux2 = demuxer->desc->open(demuxer))) {
974 demuxer = demux2;
975 goto dmx_open;
977 } else {
978 if (fformat == DEMUXER_TYPE_PLAYLIST)
979 return demuxer; // handled in mplayer.c
980 // Format changed after check, recurse
981 free_demuxer(demuxer);
982 demuxer = demux_open_stream(opts, stream, fformat, force,
983 audio_id, video_id,
984 dvdsub_id, filename);
985 if (demuxer)
986 return demuxer; // done!
987 file_format = DEMUXER_TYPE_UNKNOWN;
990 free_demuxer(demuxer);
991 demuxer = NULL;
995 // If no forced demuxer perform file extension based detection
996 // Ok. We're over the stable detectable fileformats, the next ones are
997 // a bit fuzzy. So by default (extension_parsing==1) try extension-based
998 // detection first:
999 if (file_format == DEMUXER_TYPE_UNKNOWN && filename
1000 && extension_parsing == 1) {
1001 file_format = demuxer_type_by_filename(filename);
1002 if (file_format != DEMUXER_TYPE_UNKNOWN) {
1003 // we like recursion :)
1004 demuxer = demux_open_stream(opts, stream, file_format, force,
1005 audio_id, video_id, dvdsub_id,
1006 filename);
1007 if (demuxer)
1008 return demuxer; // done!
1009 file_format = DEMUXER_TYPE_UNKNOWN; // continue fuzzy guessing...
1010 mp_msg(MSGT_DEMUXER, MSGL_V,
1011 "demuxer: continue fuzzy content-based format guessing...\n");
1014 // Try detection for all other demuxers
1015 for (i = 0; (demuxer_desc = demuxer_list[i]); i++) {
1016 if (!demuxer_desc->safe_check && demuxer_desc->check_file) {
1017 demuxer = new_demuxer(opts, stream, demuxer_desc->type, audio_id,
1018 video_id, dvdsub_id, filename);
1019 if ((fformat = demuxer_desc->check_file(demuxer)) != 0) {
1020 if (fformat == demuxer_desc->type) {
1021 demuxer_t *demux2 = demuxer;
1022 mp_tmsg(MSGT_DEMUXER, MSGL_INFO,
1023 "%s file format detected.\n",
1024 demuxer_desc->shortdesc);
1025 file_format = fformat;
1026 if (!demuxer->desc->open
1027 || (demux2 = demuxer->desc->open(demuxer))) {
1028 demuxer = demux2;
1029 goto dmx_open;
1031 } else {
1032 if (fformat == DEMUXER_TYPE_PLAYLIST)
1033 return demuxer; // handled in mplayer.c
1034 // Format changed after check, recurse
1035 free_demuxer(demuxer);
1036 demuxer = demux_open_stream(opts, stream, fformat, force,
1037 audio_id, video_id,
1038 dvdsub_id, filename);
1039 if (demuxer)
1040 return demuxer; // done!
1041 file_format = DEMUXER_TYPE_UNKNOWN;
1044 free_demuxer(demuxer);
1045 demuxer = NULL;
1049 return NULL;
1050 //====== File format recognized, set up these for compatibility: =========
1051 dmx_open:
1053 demuxer->file_format = file_format;
1055 if ((sh_video = demuxer->video->sh) && sh_video->bih) {
1056 int biComp = le2me_32(sh_video->bih->biCompression);
1057 mp_msg(MSGT_DEMUX, MSGL_INFO,
1058 "VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
1059 (char *) &biComp, sh_video->bih->biWidth,
1060 sh_video->bih->biHeight, sh_video->bih->biBitCount,
1061 sh_video->fps, sh_video->i_bps * 0.008f,
1062 sh_video->i_bps / 1024.0f);
1064 #ifdef CONFIG_ASS
1065 if (opts->ass_enabled && ass_library) {
1066 for (i = 0; i < MAX_S_STREAMS; ++i) {
1067 sh_sub_t *sh = demuxer->s_streams[i];
1068 if (sh && sh->type == 'a') {
1069 sh->ass_track = ass_new_track(ass_library);
1070 if (sh->ass_track && sh->extradata)
1071 ass_process_codec_private(sh->ass_track, sh->extradata,
1072 sh->extradata_len);
1073 } else if (sh && sh->type != 'v')
1074 sh->ass_track = ass_default_track(ass_library);
1077 #endif
1078 return demuxer;
1081 char *audio_stream = NULL;
1082 char *sub_stream = NULL;
1083 int audio_stream_cache = 0;
1085 char *demuxer_name = NULL; // parameter from -demuxer
1086 char *audio_demuxer_name = NULL; // parameter from -audio-demuxer
1087 char *sub_demuxer_name = NULL; // parameter from -sub-demuxer
1089 extern int hr_mp3_seek;
1091 extern float stream_cache_min_percent;
1092 extern float stream_cache_seek_min_percent;
1094 demuxer_t *demux_open(struct MPOpts *opts, stream_t *vs, int file_format,
1095 int audio_id, int video_id, int dvdsub_id,
1096 char *filename)
1098 stream_t *as = NULL, *ss = NULL;
1099 demuxer_t *vd, *ad = NULL, *sd = NULL;
1100 demuxer_t *res;
1101 int afmt = DEMUXER_TYPE_UNKNOWN, sfmt = DEMUXER_TYPE_UNKNOWN;
1102 int demuxer_type;
1103 int audio_demuxer_type = 0, sub_demuxer_type = 0;
1104 int demuxer_force = 0, audio_demuxer_force = 0, sub_demuxer_force = 0;
1106 if ((demuxer_type =
1107 get_demuxer_type_from_name(demuxer_name, &demuxer_force)) < 0) {
1108 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-demuxer %s does not exist.\n",
1109 demuxer_name);
1111 if ((audio_demuxer_type =
1112 get_demuxer_type_from_name(audio_demuxer_name,
1113 &audio_demuxer_force)) < 0) {
1114 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-audio-demuxer %s does not exist.\n",
1115 audio_demuxer_name);
1117 if ((sub_demuxer_type =
1118 get_demuxer_type_from_name(sub_demuxer_name,
1119 &sub_demuxer_force)) < 0) {
1120 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-sub-demuxer %s does not exist.\n",
1121 sub_demuxer_name);
1124 if (audio_stream) {
1125 as = open_stream(audio_stream, 0, &afmt);
1126 if (!as) {
1127 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "Cannot open audio stream: %s\n",
1128 audio_stream);
1129 return NULL;
1131 if (audio_stream_cache) {
1132 if (!stream_enable_cache
1133 (as, audio_stream_cache * 1024,
1134 audio_stream_cache * 1024 * (stream_cache_min_percent /
1135 100.0),
1136 audio_stream_cache * 1024 * (stream_cache_seek_min_percent /
1137 100.0))) {
1138 free_stream(as);
1139 mp_msg(MSGT_DEMUXER, MSGL_ERR,
1140 "Can't enable audio stream cache\n");
1141 return NULL;
1145 if (sub_stream) {
1146 ss = open_stream(sub_stream, 0, &sfmt);
1147 if (!ss) {
1148 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "Cannot open subtitle stream: %s\n",
1149 sub_stream);
1150 return NULL;
1154 vd = demux_open_stream(opts, vs, demuxer_type ? demuxer_type : file_format,
1155 demuxer_force, audio_stream ? -2 : audio_id,
1156 video_id, sub_stream ? -2 : dvdsub_id, filename);
1157 if (!vd) {
1158 if (as)
1159 free_stream(as);
1160 if (ss)
1161 free_stream(ss);
1162 return NULL;
1164 if (as) {
1165 ad = demux_open_stream(opts, as,
1166 audio_demuxer_type ? audio_demuxer_type : afmt,
1167 audio_demuxer_force, audio_id, -2, -2,
1168 audio_stream);
1169 if (!ad) {
1170 mp_tmsg(MSGT_DEMUXER, MSGL_WARN, "Failed to open audio demuxer: %s\n",
1171 audio_stream);
1172 free_stream(as);
1173 } else if (ad->audio->sh
1174 && ((sh_audio_t *) ad->audio->sh)->format == 0x55) // MP3
1175 hr_mp3_seek = 1; // Enable high res seeking
1177 if (ss) {
1178 sd = demux_open_stream(opts, ss,
1179 sub_demuxer_type ? sub_demuxer_type : sfmt,
1180 sub_demuxer_force, -2, -2, dvdsub_id,
1181 sub_stream);
1182 if (!sd) {
1183 mp_tmsg(MSGT_DEMUXER, MSGL_WARN,
1184 "Failed to open subtitle demuxer: %s\n", sub_stream);
1185 free_stream(ss);
1189 if (ad && sd)
1190 res = new_demuxers_demuxer(vd, ad, sd);
1191 else if (ad)
1192 res = new_demuxers_demuxer(vd, ad, vd);
1193 else if (sd)
1194 res = new_demuxers_demuxer(vd, vd, sd);
1195 else
1196 res = vd;
1198 opts->correct_pts = opts->user_correct_pts;
1199 if (opts->correct_pts < 0)
1200 opts->correct_pts =
1201 demux_control(res, DEMUXER_CTRL_CORRECT_PTS,
1202 NULL) == DEMUXER_CTRL_OK;
1203 return res;
1207 void demux_flush(demuxer_t *demuxer)
1209 #if PARSE_ON_ADD
1210 ds_clear_parser(demuxer->video);
1211 ds_clear_parser(demuxer->audio);
1212 ds_clear_parser(demuxer->sub);
1213 #endif
1214 ds_free_packs(demuxer->video);
1215 ds_free_packs(demuxer->audio);
1216 ds_free_packs(demuxer->sub);
1219 int demux_seek(demuxer_t *demuxer, float rel_seek_secs, float audio_delay,
1220 int flags)
1222 demux_stream_t *d_audio = demuxer->audio;
1223 demux_stream_t *d_video = demuxer->video;
1224 sh_audio_t *sh_audio = d_audio->sh;
1225 sh_video_t *sh_video = d_video->sh;
1226 double tmp = 0;
1227 double pts;
1229 if (!demuxer->seekable) {
1230 if (demuxer->file_format == DEMUXER_TYPE_AVI)
1231 mp_tmsg(MSGT_SEEK, MSGL_WARN, "Cannot seek in raw AVI streams. (Index required, try with the -idx switch.)\n");
1232 #ifdef CONFIG_TV
1233 else if (demuxer->file_format == DEMUXER_TYPE_TV)
1234 mp_tmsg(MSGT_SEEK, MSGL_WARN, "TV input is not seekable! (Seeking will probably be for changing channels ;)\n");
1235 #endif
1236 else
1237 mp_tmsg(MSGT_SEEK, MSGL_WARN, "Cannot seek in this file.\n");
1238 return 0;
1240 // clear demux buffers:
1241 demux_flush(demuxer);
1242 if (sh_audio)
1243 sh_audio->a_buffer_len = 0;
1245 demuxer->stream->eof = 0;
1246 demuxer->video->eof = 0;
1247 demuxer->audio->eof = 0;
1249 if (sh_video)
1250 sh_video->timer = 0; // !!!!!!
1252 if (flags & SEEK_ABSOLUTE)
1253 pts = 0.0f;
1254 else {
1255 if (demuxer->stream_pts == MP_NOPTS_VALUE)
1256 goto dmx_seek;
1257 pts = demuxer->stream_pts;
1260 if (flags & SEEK_FACTOR) {
1261 if (stream_control(demuxer->stream, STREAM_CTRL_GET_TIME_LENGTH, &tmp)
1262 == STREAM_UNSUPPORTED)
1263 goto dmx_seek;
1264 pts += tmp * rel_seek_secs;
1265 } else
1266 pts += rel_seek_secs;
1268 if (stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_TIME, &pts) !=
1269 STREAM_UNSUPPORTED) {
1270 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1271 return 1;
1274 dmx_seek:
1275 if (demuxer->desc->seek)
1276 demuxer->desc->seek(demuxer, rel_seek_secs, audio_delay, flags);
1278 if (sh_audio)
1279 resync_audio_stream(sh_audio);
1281 return 1;
1284 int demux_info_add(demuxer_t *demuxer, const char *opt, const char *param)
1286 char **info = demuxer->info;
1287 int n = 0;
1290 for (n = 0; info && info[2 * n] != NULL; n++) {
1291 if (!strcasecmp(opt, info[2 * n])) {
1292 if (!strcmp(param, info[2 * n + 1])) {
1293 mp_msg(MSGT_DEMUX, MSGL_V, "Demuxer info %s set to unchanged value %s\n", opt, param);
1294 return 0;
1296 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "Demuxer info %s changed to %s\n", opt,
1297 param);
1298 free(info[2 * n + 1]);
1299 info[2 * n + 1] = strdup(param);
1300 return 0;
1304 info = demuxer->info = realloc(info, (2 * (n + 2)) * sizeof(char *));
1305 info[2 * n] = strdup(opt);
1306 info[2 * n + 1] = strdup(param);
1307 memset(&info[2 * (n + 1)], 0, 2 * sizeof(char *));
1309 return 1;
1312 int demux_info_print(demuxer_t *demuxer)
1314 char **info = demuxer->info;
1315 int n;
1317 if (!info)
1318 return 0;
1320 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "Clip info:\n");
1321 for (n = 0; info[2 * n] != NULL; n++) {
1322 mp_msg(MSGT_DEMUX, MSGL_INFO, " %s: %s\n", info[2 * n],
1323 info[2 * n + 1]);
1324 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_NAME%d=%s\n", n,
1325 info[2 * n]);
1326 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_VALUE%d=%s\n", n,
1327 info[2 * n + 1]);
1329 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_N=%d\n", n);
1331 return 0;
1334 char *demux_info_get(demuxer_t *demuxer, const char *opt)
1336 int i;
1337 char **info = demuxer->info;
1339 for (i = 0; info && info[2 * i] != NULL; i++) {
1340 if (!strcasecmp(opt, info[2 * i]))
1341 return info[2 * i + 1];
1344 return NULL;
1347 int demux_control(demuxer_t *demuxer, int cmd, void *arg)
1350 if (demuxer->desc->control)
1351 return demuxer->desc->control(demuxer, cmd, arg);
1353 return DEMUXER_CTRL_NOTIMPL;
1358 double demuxer_get_time_length(demuxer_t *demuxer)
1360 double get_time_ans;
1361 sh_video_t *sh_video = demuxer->video->sh;
1362 sh_audio_t *sh_audio = demuxer->audio->sh;
1363 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
1364 if (demux_control
1365 (demuxer, DEMUXER_CTRL_GET_TIME_LENGTH, (void *) &get_time_ans) <= 0) {
1366 if (sh_video && sh_video->i_bps && sh_audio && sh_audio->i_bps)
1367 get_time_ans = (double) (demuxer->movi_end -
1368 demuxer->movi_start) / (sh_video->i_bps +
1369 sh_audio->i_bps);
1370 else if (sh_video && sh_video->i_bps)
1371 get_time_ans = (double) (demuxer->movi_end -
1372 demuxer->movi_start) / sh_video->i_bps;
1373 else if (sh_audio && sh_audio->i_bps)
1374 get_time_ans = (double) (demuxer->movi_end -
1375 demuxer->movi_start) / sh_audio->i_bps;
1376 else
1377 get_time_ans = 0;
1379 return get_time_ans;
1383 * \brief demuxer_get_current_time() returns the time of the current play in three possible ways:
1384 * either when the stream reader satisfies STREAM_CTRL_GET_CURRENT_TIME (e.g. dvd)
1385 * or using sh_video->pts when the former method fails
1386 * 0 otherwise
1387 * \return the current play time
1389 int demuxer_get_current_time(demuxer_t *demuxer)
1391 double get_time_ans = 0;
1392 sh_video_t *sh_video = demuxer->video->sh;
1393 if (demuxer->stream_pts != MP_NOPTS_VALUE)
1394 get_time_ans = demuxer->stream_pts;
1395 else if (sh_video)
1396 get_time_ans = sh_video->pts;
1397 return (int) get_time_ans;
1400 int demuxer_get_percent_pos(demuxer_t *demuxer)
1402 int ans = 0;
1403 int res = demux_control(demuxer, DEMUXER_CTRL_GET_PERCENT_POS, &ans);
1404 int len = (demuxer->movi_end - demuxer->movi_start) / 100;
1405 if (res <= 0) {
1406 off_t pos = demuxer->filepos > 0 ? demuxer->filepos : stream_tell(demuxer->stream);
1407 if (len > 0)
1408 ans = (pos - demuxer->movi_start) / len;
1409 else
1410 ans = 0;
1412 if (ans < 0)
1413 ans = 0;
1414 if (ans > 100)
1415 ans = 100;
1416 return ans;
1419 int demuxer_switch_audio(demuxer_t *demuxer, int index)
1421 int res = demux_control(demuxer, DEMUXER_CTRL_SWITCH_AUDIO, &index);
1422 if (res == DEMUXER_CTRL_NOTIMPL)
1423 index = demuxer->audio->id;
1424 if (demuxer->audio->id >= 0)
1425 demuxer->audio->sh = demuxer->a_streams[demuxer->audio->id];
1426 else
1427 demuxer->audio->sh = NULL;
1428 return index;
1431 int demuxer_switch_video(demuxer_t *demuxer, int index)
1433 int res = demux_control(demuxer, DEMUXER_CTRL_SWITCH_VIDEO, &index);
1434 if (res == DEMUXER_CTRL_NOTIMPL)
1435 index = demuxer->video->id;
1436 if (demuxer->video->id >= 0)
1437 demuxer->video->sh = demuxer->v_streams[demuxer->video->id];
1438 else
1439 demuxer->video->sh = NULL;
1440 return index;
1443 int demuxer_add_attachment(demuxer_t *demuxer, const char *name,
1444 int name_maxlen, const char *type, int type_maxlen,
1445 const void *data, size_t size)
1447 if (!(demuxer->num_attachments % 32))
1448 demuxer->attachments = talloc_realloc(demuxer, demuxer->attachments,
1449 struct demux_attachment,
1450 demuxer->num_attachments + 32);
1452 struct demux_attachment *att =
1453 demuxer->attachments + demuxer->num_attachments;
1454 att->name = talloc_strndup(demuxer->attachments, name, name_maxlen);
1455 att->type = talloc_strndup(demuxer->attachments, type, type_maxlen);
1456 att->data = talloc_size(demuxer->attachments, size);
1457 memcpy(att->data, data, size);
1458 att->data_size = size;
1460 return demuxer->num_attachments++;
1463 int demuxer_add_chapter(demuxer_t *demuxer, const char *name, int name_maxlen,
1464 uint64_t start, uint64_t end)
1466 if (!(demuxer->num_chapters % 32))
1467 demuxer->chapters = talloc_realloc(demuxer, demuxer->chapters,
1468 struct demux_chapter,
1469 demuxer->num_chapters + 32);
1471 demuxer->chapters[demuxer->num_chapters].start = start;
1472 demuxer->chapters[demuxer->num_chapters].end = end;
1473 demuxer->chapters[demuxer->num_chapters].name = name ?
1474 talloc_strndup(demuxer->chapters, name, name_maxlen) :
1475 talloc_strdup(demuxer->chapters, mp_gtext("unknown"));
1477 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_ID=%d\n", demuxer->num_chapters);
1478 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_START=%"PRIu64"\n", demuxer->num_chapters, start);
1479 if (end)
1480 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_END=%"PRIu64"\n", demuxer->num_chapters, end);
1481 if (name)
1482 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_NAME=%.*s\n", demuxer->num_chapters, name_maxlen, name);
1484 return demuxer->num_chapters++;
1488 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1489 * either using the demuxer->chapters structure set by the demuxer
1490 * or asking help to the stream layer (e.g. dvd)
1491 * \param chapter - chapter number wished - 0-based
1492 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1493 * \param chapter_name name of chapter found (set by this function is param is not null)
1494 * \return -1 on error, current chapter if successful
1497 int demuxer_seek_chapter(demuxer_t *demuxer, int chapter, double *seek_pts,
1498 char **chapter_name)
1500 int ris;
1501 sh_video_t *sh_video = demuxer->video->sh;
1502 sh_audio_t *sh_audio = demuxer->audio->sh;
1504 if (!demuxer->num_chapters || !demuxer->chapters) {
1505 demux_flush(demuxer);
1507 ris = stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_CHAPTER,
1508 &chapter);
1509 if (ris != STREAM_UNSUPPORTED)
1510 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1511 if (sh_video) {
1512 ds_fill_buffer(demuxer->video);
1513 resync_video_stream(sh_video);
1516 if (sh_audio) {
1517 ds_fill_buffer(demuxer->audio);
1518 resync_audio_stream(sh_audio);
1520 // exit status may be ok, but main() doesn't have to seek itself
1521 // (because e.g. dvds depend on sectors, not on pts)
1522 *seek_pts = -1.0;
1524 if (chapter_name) {
1525 *chapter_name = NULL;
1526 int num_chapters;
1527 if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS,
1528 &num_chapters) == STREAM_UNSUPPORTED)
1529 num_chapters = 0;
1530 if (num_chapters) {
1531 *chapter_name = talloc_size(NULL, 16);
1532 sprintf(*chapter_name, " of %3d", num_chapters);
1536 return ris != STREAM_UNSUPPORTED ? chapter : -1;
1537 } else { // chapters structure is set in the demuxer
1538 if (chapter >= demuxer->num_chapters)
1539 return -1;
1540 if (chapter < 0)
1541 chapter = 0;
1543 *seek_pts = demuxer->chapters[chapter].start / 1000.0;
1545 if (chapter_name)
1546 *chapter_name = talloc_strdup(NULL, demuxer->chapters[chapter].name);
1548 return chapter;
1552 int demuxer_get_current_chapter(demuxer_t *demuxer)
1554 int chapter = -1;
1555 if (!demuxer->num_chapters || !demuxer->chapters) {
1556 if (stream_control(demuxer->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
1557 &chapter) == STREAM_UNSUPPORTED)
1558 chapter = -1;
1559 } else {
1560 sh_video_t *sh_video = demuxer->video->sh;
1561 sh_audio_t *sh_audio = demuxer->audio->sh;
1562 uint64_t now;
1563 now = (sh_video ? sh_video->pts : (sh_audio ? sh_audio->pts : 0))
1564 * 1000 + 0.5;
1565 for (chapter = demuxer->num_chapters - 1; chapter >= 0; --chapter) {
1566 if (demuxer->chapters[chapter].start <= now)
1567 break;
1570 return chapter;
1573 char *demuxer_chapter_name(demuxer_t *demuxer, int chapter)
1575 if (demuxer->num_chapters && demuxer->chapters) {
1576 if (chapter >= 0 && chapter < demuxer->num_chapters
1577 && demuxer->chapters[chapter].name)
1578 return strdup(demuxer->chapters[chapter].name);
1580 return NULL;
1583 char *demuxer_chapter_display_name(demuxer_t *demuxer, int chapter)
1585 char *chapter_name = demuxer_chapter_name(demuxer, chapter);
1586 if (chapter_name) {
1587 char *tmp = malloc(strlen(chapter_name) + 14);
1588 snprintf(tmp, 63, "(%d) %s", chapter + 1, chapter_name);
1589 free(chapter_name);
1590 return tmp;
1591 } else {
1592 int chapter_num = demuxer_chapter_count(demuxer);
1593 char tmp[30];
1594 if (chapter_num <= 0)
1595 sprintf(tmp, "(%d)", chapter + 1);
1596 else
1597 sprintf(tmp, "(%d) of %d", chapter + 1, chapter_num);
1598 return strdup(tmp);
1602 float demuxer_chapter_time(demuxer_t *demuxer, int chapter, float *end)
1604 if (demuxer->num_chapters && demuxer->chapters && chapter >= 0
1605 && chapter < demuxer->num_chapters) {
1606 if (end)
1607 *end = demuxer->chapters[chapter].end / 1000.0;
1608 return demuxer->chapters[chapter].start / 1000.0;
1610 return -1.0;
1613 int demuxer_chapter_count(demuxer_t *demuxer)
1615 if (!demuxer->num_chapters || !demuxer->chapters) {
1616 int num_chapters = 0;
1617 if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS,
1618 &num_chapters) == STREAM_UNSUPPORTED)
1619 num_chapters = 0;
1620 return num_chapters;
1621 } else
1622 return demuxer->num_chapters;
1625 int demuxer_angles_count(demuxer_t *demuxer)
1627 int ris, angles = -1;
1629 ris = stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_ANGLES, &angles);
1630 if (ris == STREAM_UNSUPPORTED)
1631 return -1;
1632 return angles;
1635 int demuxer_get_current_angle(demuxer_t *demuxer)
1637 int ris, curr_angle = -1;
1638 ris = stream_control(demuxer->stream, STREAM_CTRL_GET_ANGLE, &curr_angle);
1639 if (ris == STREAM_UNSUPPORTED)
1640 return -1;
1641 return curr_angle;
1645 int demuxer_set_angle(demuxer_t *demuxer, int angle)
1647 int ris, angles = -1;
1648 sh_video_t *sh_video = demuxer->video->sh;
1649 sh_audio_t *sh_audio = demuxer->audio->sh;
1651 angles = demuxer_angles_count(demuxer);
1652 if ((angles < 1) || (angle > angles))
1653 return -1;
1655 demux_flush(demuxer);
1657 ris = stream_control(demuxer->stream, STREAM_CTRL_SET_ANGLE, &angle);
1658 if (ris == STREAM_UNSUPPORTED)
1659 return -1;
1661 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1662 if (sh_video) {
1663 ds_fill_buffer(demuxer->video);
1664 resync_video_stream(sh_video);
1667 if (sh_audio) {
1668 ds_fill_buffer(demuxer->audio);
1669 resync_audio_stream(sh_audio);
1672 return angle;
1675 int demuxer_audio_track_by_lang(demuxer_t *d, char *lang)
1677 int i, len;
1678 lang += strspn(lang, ",");
1679 while ((len = strcspn(lang, ",")) > 0) {
1680 for (i = 0; i < MAX_A_STREAMS; ++i) {
1681 sh_audio_t *sh = d->a_streams[i];
1682 if (sh && sh->lang && strncmp(sh->lang, lang, len) == 0)
1683 return sh->aid;
1685 lang += len;
1686 lang += strspn(lang, ",");
1688 return -1;
1691 int demuxer_sub_track_by_lang(demuxer_t *d, char *lang)
1693 int i, len;
1694 lang += strspn(lang, ",");
1695 while ((len = strcspn(lang, ",")) > 0) {
1696 for (i = 0; i < MAX_S_STREAMS; ++i) {
1697 sh_sub_t *sh = d->s_streams[i];
1698 if (sh && sh->lang && strncmp(sh->lang, lang, len) == 0)
1699 return sh->sid;
1701 lang += len;
1702 lang += strspn(lang, ",");
1704 return -1;
1707 int demuxer_default_audio_track(demuxer_t *d)
1709 int i;
1710 for (i = 0; i < MAX_A_STREAMS; ++i) {
1711 sh_audio_t *sh = d->a_streams[i];
1712 if (sh && sh->default_track)
1713 return sh->aid;
1715 for (i = 0; i < MAX_A_STREAMS; ++i) {
1716 sh_audio_t *sh = d->a_streams[i];
1717 if (sh)
1718 return sh->aid;
1720 return -1;
1723 int demuxer_default_sub_track(demuxer_t *d)
1725 int i;
1726 for (i = 0; i < MAX_S_STREAMS; ++i) {
1727 sh_sub_t *sh = d->s_streams[i];
1728 if (sh && sh->default_track)
1729 return sh->sid;
1731 return -1;