demux_real.c: Remove unused hexdump() forward declaration
[mplayer/greg.git] / libmpdemux / demuxer.c
blob378100b1803c00489e28175b5a5d0b32a3b35a18
1 /*
2 * DEMUXER v2.5
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
29 #include "config.h"
30 #include "options.h"
31 #include "talloc.h"
32 #include "mp_msg.h"
33 #include "m_config.h"
35 #include "libvo/fastmemcpy.h"
37 #include "stream/stream.h"
38 #include "demuxer.h"
39 #include "stheader.h"
40 #include "mf.h"
42 #include "libaf/af_format.h"
43 #include "libmpcodecs/dec_teletext.h"
44 #include "libmpcodecs/vd_ffmpeg.h"
46 #include "ass_mp.h"
48 #ifdef CONFIG_FFMPEG
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);
64 // Demuxer list
65 extern const demuxer_desc_t demuxer_desc_rawaudio;
66 extern const demuxer_desc_t demuxer_desc_rawvideo;
67 extern const demuxer_desc_t demuxer_desc_tv;
68 extern const demuxer_desc_t demuxer_desc_mf;
69 extern const demuxer_desc_t demuxer_desc_avi;
70 extern const demuxer_desc_t demuxer_desc_y4m;
71 extern const demuxer_desc_t demuxer_desc_asf;
72 extern const demuxer_desc_t demuxer_desc_real;
73 extern const demuxer_desc_t demuxer_desc_smjpeg;
74 extern const demuxer_desc_t demuxer_desc_matroska;
75 extern const demuxer_desc_t demuxer_desc_realaudio;
76 extern const demuxer_desc_t demuxer_desc_vqf;
77 extern const demuxer_desc_t demuxer_desc_mov;
78 extern const demuxer_desc_t demuxer_desc_vivo;
79 extern const demuxer_desc_t demuxer_desc_fli;
80 extern const demuxer_desc_t demuxer_desc_film;
81 extern const demuxer_desc_t demuxer_desc_roq;
82 extern const demuxer_desc_t demuxer_desc_gif;
83 extern const demuxer_desc_t demuxer_desc_ogg;
84 extern const demuxer_desc_t demuxer_desc_avs;
85 extern const demuxer_desc_t demuxer_desc_pva;
86 extern const demuxer_desc_t demuxer_desc_nsv;
87 extern const demuxer_desc_t demuxer_desc_mpeg_ts;
88 extern const demuxer_desc_t demuxer_desc_lmlm4;
89 extern const demuxer_desc_t demuxer_desc_mpeg_ps;
90 extern const demuxer_desc_t demuxer_desc_mpeg_pes;
91 extern const demuxer_desc_t demuxer_desc_mpeg_es;
92 extern const demuxer_desc_t demuxer_desc_mpeg_gxf;
93 extern const demuxer_desc_t demuxer_desc_mpeg4_es;
94 extern const demuxer_desc_t demuxer_desc_h264_es;
95 extern const demuxer_desc_t demuxer_desc_rawdv;
96 extern const demuxer_desc_t demuxer_desc_mpc;
97 extern const demuxer_desc_t demuxer_desc_audio;
98 extern const demuxer_desc_t demuxer_desc_xmms;
99 extern const demuxer_desc_t demuxer_desc_mpeg_ty;
100 extern const demuxer_desc_t demuxer_desc_rtp;
101 extern const demuxer_desc_t demuxer_desc_rtp_nemesi;
102 extern const demuxer_desc_t demuxer_desc_lavf;
103 extern const demuxer_desc_t demuxer_desc_lavf_preferred;
104 extern const demuxer_desc_t demuxer_desc_aac;
105 extern const demuxer_desc_t demuxer_desc_nut;
106 extern const demuxer_desc_t demuxer_desc_mng;
108 /* Please do not add any new demuxers here. If you want to implement a new
109 * demuxer, add it to libavformat, except for wrappers around external
110 * libraries and demuxers requiring binary support. */
112 const demuxer_desc_t *const demuxer_list[] = {
113 &demuxer_desc_rawaudio,
114 &demuxer_desc_rawvideo,
115 #ifdef CONFIG_TV
116 &demuxer_desc_tv,
117 #endif
118 &demuxer_desc_mf,
119 #ifdef CONFIG_FFMPEG
120 &demuxer_desc_lavf_preferred,
121 #endif
122 &demuxer_desc_avi,
123 &demuxer_desc_y4m,
124 &demuxer_desc_asf,
125 &demuxer_desc_nsv,
126 &demuxer_desc_real,
127 &demuxer_desc_smjpeg,
128 &demuxer_desc_matroska,
129 &demuxer_desc_realaudio,
130 &demuxer_desc_vqf,
131 &demuxer_desc_mov,
132 &demuxer_desc_vivo,
133 &demuxer_desc_fli,
134 &demuxer_desc_film,
135 &demuxer_desc_roq,
136 #ifdef CONFIG_GIF
137 &demuxer_desc_gif,
138 #endif
139 #ifdef CONFIG_OGGVORBIS
140 &demuxer_desc_ogg,
141 #endif
142 #ifdef CONFIG_WIN32DLL
143 &demuxer_desc_avs,
144 #endif
145 &demuxer_desc_pva,
146 &demuxer_desc_mpeg_ts,
147 &demuxer_desc_lmlm4,
148 &demuxer_desc_mpeg_ps,
149 &demuxer_desc_mpeg_pes,
150 &demuxer_desc_mpeg_es,
151 &demuxer_desc_mpeg_gxf,
152 &demuxer_desc_mpeg4_es,
153 &demuxer_desc_h264_es,
154 &demuxer_desc_audio,
155 &demuxer_desc_mpeg_ty,
156 #ifdef CONFIG_LIVE555
157 &demuxer_desc_rtp,
158 #endif
159 #ifdef CONFIG_LIBNEMESI
160 &demuxer_desc_rtp_nemesi,
161 #endif
162 #ifdef CONFIG_FFMPEG
163 &demuxer_desc_lavf,
164 #endif
165 #ifdef CONFIG_MUSEPACK
166 &demuxer_desc_mpc,
167 #endif
168 #ifdef CONFIG_LIBDV095
169 &demuxer_desc_rawdv,
170 #endif
171 &demuxer_desc_aac,
172 #ifdef CONFIG_LIBNUT
173 &demuxer_desc_nut,
174 #endif
175 #ifdef CONFIG_XMMS
176 &demuxer_desc_xmms,
177 #endif
178 #ifdef CONFIG_MNG
179 &demuxer_desc_mng,
180 #endif
181 /* Please do not add any new demuxers here. If you want to implement a new
182 * demuxer, add it to libavformat, except for wrappers around external
183 * libraries and demuxers requiring binary support. */
184 NULL
187 void free_demuxer_stream(demux_stream_t *ds)
189 ds_free_packs(ds);
190 free(ds);
193 demux_stream_t *new_demuxer_stream(struct demuxer *demuxer, int id)
195 demux_stream_t *ds = malloc(sizeof(demux_stream_t));
196 *ds = (demux_stream_t){
197 .id = id,
198 .demuxer = demuxer,
199 .asf_seq = -1,
201 return ds;
206 * Get demuxer description structure for a given demuxer type
208 * @param file_format type of the demuxer
209 * @return structure for the demuxer, NULL if not found
211 static const demuxer_desc_t *get_demuxer_desc_from_type(int file_format)
213 int i;
215 for (i = 0; demuxer_list[i]; i++)
216 if (file_format == demuxer_list[i]->type)
217 return demuxer_list[i];
219 return NULL;
223 demuxer_t *new_demuxer(struct MPOpts *opts, stream_t *stream, int type,
224 int a_id, int v_id, int s_id, char *filename)
226 struct demuxer *d = talloc_zero(NULL, struct demuxer);
227 d->stream = stream;
228 d->stream_pts = MP_NOPTS_VALUE;
229 d->reference_clock = MP_NOPTS_VALUE;
230 d->movi_start = stream->start_pos;
231 d->movi_end = stream->end_pos;
232 d->seekable = 1;
233 d->synced = 0;
234 d->filepos = -1;
235 d->audio = new_demuxer_stream(d, a_id);
236 d->video = new_demuxer_stream(d, v_id);
237 d->sub = new_demuxer_stream(d, s_id);
238 d->type = type;
239 d->opts = opts;
240 if (type)
241 if (!(d->desc = get_demuxer_desc_from_type(type)))
242 mp_msg(MSGT_DEMUXER, MSGL_ERR,
243 "BUG! Invalid demuxer type in new_demuxer(), "
244 "big troubles ahead.");
245 if (filename) // Filename hack for avs_check_file
246 d->filename = strdup(filename);
247 stream_seek(stream, stream->start_pos);
248 return d;
251 const char *sh_sub_type2str(int type)
253 switch (type) {
254 case 't': return "text";
255 case 'm': return "movtext";
256 case 'a': return "ass";
257 case 'v': return "vobsub";
258 case 'x': return "xsub";
259 case 'b': return "dvb";
260 case 'd': return "dvb-teletext";
261 case 'p': return "hdmv pgs";
263 return "unknown";
266 sh_sub_t *new_sh_sub_sid(demuxer_t *demuxer, int id, int sid)
268 if (id > MAX_S_STREAMS - 1 || id < 0) {
269 mp_msg(MSGT_DEMUXER, MSGL_WARN,
270 "Requested sub stream id overflow (%d > %d)\n", id,
271 MAX_S_STREAMS);
272 return NULL;
274 if (demuxer->s_streams[id])
275 mp_msg(MSGT_DEMUXER, MSGL_WARN, "Sub stream %i redefined\n", id);
276 else {
277 sh_sub_t *sh = calloc(1, sizeof(sh_sub_t));
278 demuxer->s_streams[id] = sh;
279 sh->sid = sid;
280 sh->opts = demuxer->opts;
281 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SUBTITLE_ID=%d\n", sid);
283 if (sid == demuxer->opts->sub_id) {
284 demuxer->sub->id = id;
285 demuxer->sub->sh = demuxer->s_streams[id];
287 return demuxer->s_streams[id];
290 struct sh_sub *new_sh_sub_sid_lang(struct demuxer *demuxer, int id, int sid,
291 const char *lang)
293 struct sh_sub *sh = new_sh_sub_sid(demuxer, id, sid);
294 if (lang && lang[0] && strcmp(lang, "und")) {
295 sh->lang = strdup(lang);
296 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_LANG=%s\n", sid, lang);
298 return sh;
301 static void free_sh_sub(sh_sub_t *sh)
303 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_sub at %p\n", sh);
304 free(sh->extradata);
305 #ifdef CONFIG_ASS
306 if (sh->ass_track)
307 ass_free_track(sh->ass_track);
308 #endif
309 free(sh->lang);
310 #ifdef CONFIG_FFMPEG
311 clear_parser((sh_common_t *)sh);
312 #endif
313 free(sh);
316 sh_audio_t *new_sh_audio_aid(demuxer_t *demuxer, int id, int aid)
318 if (id > MAX_A_STREAMS - 1 || id < 0) {
319 mp_msg(MSGT_DEMUXER, MSGL_WARN,
320 "Requested audio stream id overflow (%d > %d)\n", id,
321 MAX_A_STREAMS);
322 return NULL;
324 if (demuxer->a_streams[id]) {
325 mp_tmsg(MSGT_DEMUXER, MSGL_WARN, "WARNING: Audio stream header %d redefined.\n", id);
326 } else {
327 mp_tmsg(MSGT_DEMUXER, MSGL_V, "==> Found audio stream: %d\n", id);
328 sh_audio_t *sh = calloc(1, sizeof(sh_audio_t));
329 demuxer->a_streams[id] = sh;
330 sh->aid = aid;
331 sh->ds = demuxer->audio;
332 // set some defaults
333 sh->samplesize = 2;
334 sh->sample_format = AF_FORMAT_S16_NE;
335 sh->opts = demuxer->opts;
336 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_ID=%d\n", aid);
338 return demuxer->a_streams[id];
341 void free_sh_audio(demuxer_t *demuxer, int id)
343 sh_audio_t *sh = demuxer->a_streams[id];
344 demuxer->a_streams[id] = NULL;
345 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_audio at %p\n", sh);
346 free(sh->wf);
347 free(sh->codecdata);
348 free(sh->lang);
349 #ifdef CONFIG_FFMPEG
350 clear_parser((sh_common_t *)sh);
351 #endif
352 free(sh);
355 sh_video_t *new_sh_video_vid(demuxer_t *demuxer, int id, int vid)
357 if (id > MAX_V_STREAMS - 1 || id < 0) {
358 mp_msg(MSGT_DEMUXER, MSGL_WARN,
359 "Requested video stream id overflow (%d > %d)\n", id,
360 MAX_V_STREAMS);
361 return NULL;
363 if (demuxer->v_streams[id])
364 mp_tmsg(MSGT_DEMUXER, MSGL_WARN, "WARNING: Video stream header %d redefined.\n", id);
365 else {
366 mp_tmsg(MSGT_DEMUXER, MSGL_V, "==> Found video stream: %d\n", id);
367 sh_video_t *sh = calloc(1, sizeof *sh);
368 demuxer->v_streams[id] = sh;
369 sh->vid = vid;
370 sh->ds = demuxer->video;
371 sh->opts = demuxer->opts;
372 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_ID=%d\n", vid);
374 return demuxer->v_streams[id];
377 void free_sh_video(sh_video_t *sh)
379 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_video at %p\n", sh);
380 free(sh->bih);
381 #ifdef CONFIG_FFMPEG
382 clear_parser((sh_common_t *)sh);
383 #endif
384 free(sh);
387 void free_demuxer(demuxer_t *demuxer)
389 int i;
390 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing %s demuxer at %p\n",
391 demuxer->desc->shortdesc, demuxer);
392 if (demuxer->desc->close)
393 demuxer->desc->close(demuxer);
394 // Very ugly hack to make it behave like old implementation
395 if (demuxer->desc->type == DEMUXER_TYPE_DEMUXERS)
396 goto skip_streamfree;
397 // free streams:
398 for (i = 0; i < MAX_A_STREAMS; i++)
399 if (demuxer->a_streams[i])
400 free_sh_audio(demuxer, i);
401 for (i = 0; i < MAX_V_STREAMS; i++)
402 if (demuxer->v_streams[i])
403 free_sh_video(demuxer->v_streams[i]);
404 for (i = 0; i < MAX_S_STREAMS; i++)
405 if (demuxer->s_streams[i])
406 free_sh_sub(demuxer->s_streams[i]);
407 // free demuxers:
408 free_demuxer_stream(demuxer->audio);
409 free_demuxer_stream(demuxer->video);
410 free_demuxer_stream(demuxer->sub);
411 skip_streamfree:
412 free(demuxer->filename);
413 if (demuxer->teletext)
414 teletext_control(demuxer->teletext, TV_VBI_CONTROL_STOP, NULL);
415 talloc_free(demuxer);
419 static void ds_add_packet_internal(demux_stream_t *ds, demux_packet_t *dp)
421 // append packet to DS stream:
422 ++ds->packs;
423 ds->bytes += dp->len;
424 if (ds->last) {
425 // next packet in stream
426 ds->last->next = dp;
427 ds->last = dp;
428 } else {
429 // first packet in stream
430 ds->first = ds->last = dp;
432 mp_dbg(MSGT_DEMUXER, MSGL_DBG2,
433 "DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
434 (ds == ds->demuxer->audio) ? "d_audio" : "d_video", dp->len,
435 dp->pts, (unsigned int) dp->pos, ds->demuxer->audio->packs,
436 ds->demuxer->video->packs);
439 #ifdef CONFIG_FFMPEG
440 static void allocate_parser(AVCodecContext **avctx, AVCodecParserContext **parser, unsigned format)
442 enum CodecID codec_id = CODEC_ID_NONE;
444 init_avcodec();
446 switch (format) {
447 case 0x2000:
448 case 0x332D6361:
449 case 0x332D4341:
450 case MKTAG('d', 'n', 'e', 't'):
451 case MKTAG('s', 'a', 'c', '3'):
452 codec_id = CODEC_ID_AC3;
453 break;
454 case MKTAG('E', 'A', 'C', '3'):
455 codec_id = CODEC_ID_EAC3;
456 break;
457 case 0x2001:
458 case 0x86:
459 codec_id = CODEC_ID_DTS;
460 break;
461 case MKTAG('M', 'L', 'P', ' '):
462 codec_id = CODEC_ID_MLP;
463 break;
464 case 0x55:
465 case 0x5500736d:
466 case MKTAG('.', 'm', 'p', '3'):
467 case MKTAG('M', 'P', 'E', ' '):
468 case MKTAG('L', 'A', 'M', 'E'):
469 codec_id = CODEC_ID_MP3;
470 break;
471 case 0x50:
472 case MKTAG('.', 'm', 'p', '2'):
473 case MKTAG('.', 'm', 'p', '1'):
474 codec_id = CODEC_ID_MP2;
475 break;
476 case MKTAG('T', 'R', 'H', 'D'):
477 codec_id = CODEC_ID_TRUEHD;
478 break;
480 if (codec_id != CODEC_ID_NONE) {
481 *avctx = avcodec_alloc_context();
482 if (!*avctx)
483 return;
484 *parser = av_parser_init(codec_id);
485 if (!*parser)
486 av_freep(avctx);
490 static void get_parser(sh_common_t *sh, AVCodecContext **avctx, AVCodecParserContext **parser)
492 *avctx = NULL;
493 *parser = NULL;
495 if (!sh || !sh->needs_parsing)
496 return;
498 *avctx = sh->avctx;
499 *parser = sh->parser;
500 if (*parser)
501 return;
503 allocate_parser(avctx, parser, sh->format);
504 sh->avctx = *avctx;
505 sh->parser = *parser;
508 int ds_parse(demux_stream_t *ds, uint8_t **buffer, int *len, double pts, off_t pos)
510 AVCodecContext *avctx;
511 AVCodecParserContext *parser;
512 get_parser(ds->sh, &avctx, &parser);
513 if (!parser)
514 return *len;
515 return av_parser_parse2(parser, avctx, buffer, len, *buffer, *len, pts, pts, pos);
518 static void clear_parser(sh_common_t *sh)
520 av_parser_close(sh->parser);
521 sh->parser = NULL;
522 av_freep(&sh->avctx);
525 void ds_clear_parser(demux_stream_t *ds)
527 if (!ds->sh)
528 return;
529 clear_parser(ds->sh);
531 #endif
533 void ds_add_packet(demux_stream_t *ds, demux_packet_t *dp)
535 #if PARSE_ON_ADD && defined(CONFIG_FFMPEG)
536 int len = dp->len;
537 int pos = 0;
538 while (len > 0) {
539 uint8_t *parsed_start = dp->buffer + pos;
540 int parsed_len = len;
541 int consumed = ds_parse(ds->sh, &parsed_start, &parsed_len, dp->pts, dp->pos);
542 pos += consumed;
543 len -= consumed;
544 if (parsed_start == dp->buffer && parsed_len == dp->len) {
545 ds_add_packet_internal(ds, dp);
546 } else if (parsed_len) {
547 demux_packet_t *dp2 = new_demux_packet(parsed_len);
548 dp2->pos = dp->pos;
549 dp2->pts = dp->pts; // should be parser->pts but that works badly
550 memcpy(dp2->buffer, parsed_start, parsed_len);
551 ds_add_packet_internal(ds, dp2);
554 #else
555 ds_add_packet_internal(ds, dp);
556 #endif
559 void ds_read_packet(demux_stream_t *ds, stream_t *stream, int len,
560 double pts, off_t pos, int flags)
562 demux_packet_t *dp = new_demux_packet(len);
563 len = stream_read(stream, dp->buffer, len);
564 resize_demux_packet(dp, len);
565 dp->pts = pts;
566 dp->pos = pos;
567 dp->flags = flags;
568 // append packet to DS stream:
569 ds_add_packet(ds, dp);
572 // return value:
573 // 0 = EOF or no stream found or invalid type
574 // 1 = successfully read a packet
576 int demux_fill_buffer(demuxer_t *demux, demux_stream_t *ds)
578 // Note: parameter 'ds' can be NULL!
579 return demux->desc->fill_buffer(demux, ds);
582 // return value:
583 // 0 = EOF
584 // 1 = successful
585 #define MAX_ACUMULATED_PACKETS 64
586 int ds_fill_buffer(demux_stream_t *ds)
588 demuxer_t *demux = ds->demuxer;
589 if (ds->current)
590 free_demux_packet(ds->current);
591 ds->current = NULL;
592 if (mp_msg_test(MSGT_DEMUXER, MSGL_DBG3)) {
593 if (ds == demux->audio)
594 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
595 "ds_fill_buffer(d_audio) called\n");
596 else if (ds == demux->video)
597 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
598 "ds_fill_buffer(d_video) called\n");
599 else if (ds == demux->sub)
600 mp_dbg(MSGT_DEMUXER, MSGL_DBG3, "ds_fill_buffer(d_sub) called\n");
601 else
602 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
603 "ds_fill_buffer(unknown 0x%X) called\n", (unsigned int) ds);
605 while (1) {
606 if (ds->packs) {
607 demux_packet_t *p = ds->first;
608 #if 0
609 if (demux->reference_clock != MP_NOPTS_VALUE) {
610 if ( p->pts != MP_NOPTS_VALUE
611 && p->pts > demux->reference_clock
612 && ds->packs < MAX_ACUMULATED_PACKETS) {
613 if (demux_fill_buffer(demux, ds))
614 continue;
617 #endif
618 // copy useful data:
619 ds->buffer = p->buffer;
620 ds->buffer_pos = 0;
621 ds->buffer_size = p->len;
622 ds->pos = p->pos;
623 ds->dpos += p->len; // !!!
624 ++ds->pack_no;
625 if (p->pts != MP_NOPTS_VALUE) {
626 ds->pts = p->pts;
627 ds->pts_bytes = 0;
629 ds->pts_bytes += p->len; // !!!
630 if (p->stream_pts != MP_NOPTS_VALUE)
631 demux->stream_pts = p->stream_pts;
632 ds->flags = p->flags;
633 // unlink packet:
634 ds->bytes -= p->len;
635 ds->current = p;
636 ds->first = p->next;
637 if (!ds->first)
638 ds->last = NULL;
639 --ds->packs;
640 /* The code below can set ds->eof to 1 when another stream runs
641 * out of buffer space. That makes sense because in that situation
642 * the calling code should not count on being able to demux more
643 * packets from this stream.
644 * If however the situation improves and we're called again
645 * despite the eof flag then it's better to clear it to avoid
646 * weird behavior. */
647 ds->eof = 0;
648 return 1;
651 #define MaybeNI _("Maybe you are playing a non-interleaved stream/file or the codec failed?\n" \
652 "For AVI files, try to force non-interleaved mode with the -ni option.\n")
654 if (demux->audio->packs >= MAX_PACKS
655 || demux->audio->bytes >= MAX_PACK_BYTES) {
656 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
657 demux->audio->packs, demux->audio->bytes);
658 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
659 break;
661 if (demux->video->packs >= MAX_PACKS
662 || demux->video->bytes >= MAX_PACK_BYTES) {
663 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
664 demux->video->packs, demux->video->bytes);
665 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
666 break;
668 if (!demux_fill_buffer(demux, ds)) {
669 #if PARSE_ON_ADD && defined(CONFIG_FFMPEG)
670 uint8_t *parsed_start = NULL;
671 int parsed_len = 0;
672 ds_parse(ds->sh, &parsed_start, &parsed_len, MP_NOPTS_VALUE, 0);
673 if (parsed_len) {
674 demux_packet_t *dp2 = new_demux_packet(parsed_len);
675 dp2->pts = MP_NOPTS_VALUE;
676 memcpy(dp2->buffer, parsed_start, parsed_len);
677 ds_add_packet_internal(ds, dp2);
678 continue;
680 #endif
681 mp_dbg(MSGT_DEMUXER, MSGL_DBG2,
682 "ds_fill_buffer()->demux_fill_buffer() failed\n");
683 break; // EOF
686 ds->buffer_pos = ds->buffer_size = 0;
687 ds->buffer = NULL;
688 mp_msg(MSGT_DEMUXER, MSGL_V,
689 "ds_fill_buffer: EOF reached (stream: %s) \n",
690 ds == demux->audio ? "audio" : "video");
691 ds->eof = 1;
692 return 0;
695 int demux_read_data(demux_stream_t *ds, unsigned char *mem, int len)
697 int x;
698 int bytes = 0;
699 while (len > 0) {
700 x = ds->buffer_size - ds->buffer_pos;
701 if (x == 0) {
702 if (!ds_fill_buffer(ds))
703 return bytes;
704 } else {
705 if (x > len)
706 x = len;
707 if (mem)
708 fast_memcpy(mem + bytes, &ds->buffer[ds->buffer_pos], x);
709 bytes += x;
710 len -= x;
711 ds->buffer_pos += x;
714 return bytes;
718 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
719 * \param mem memory to read data into, may be NULL to discard data
720 * \param maxlen maximum number of bytes to read
721 * \param read number of bytes actually read
722 * \param pattern pattern to search for (lowest 8 bits are ignored)
723 * \return whether pattern was found
725 int demux_pattern_3(demux_stream_t *ds, unsigned char *mem, int maxlen,
726 int *read, uint32_t pattern)
728 register uint32_t head = 0xffffff00;
729 register uint32_t pat = pattern & 0xffffff00;
730 int total_len = 0;
731 do {
732 register unsigned char *ds_buf = &ds->buffer[ds->buffer_size];
733 int len = ds->buffer_size - ds->buffer_pos;
734 register long pos = -len;
735 if (unlikely(pos >= 0)) { // buffer is empty
736 ds_fill_buffer(ds);
737 continue;
739 do {
740 head |= ds_buf[pos];
741 head <<= 8;
742 } while (++pos && head != pat);
743 len += pos;
744 if (total_len + len > maxlen)
745 len = maxlen - total_len;
746 len = demux_read_data(ds, mem ? &mem[total_len] : NULL, len);
747 total_len += len;
748 } while ((head != pat || total_len < 3) && total_len < maxlen && !ds->eof);
749 if (read)
750 *read = total_len;
751 return total_len >= 3 && head == pat;
754 void ds_free_packs(demux_stream_t *ds)
756 demux_packet_t *dp = ds->first;
757 while (dp) {
758 demux_packet_t *dn = dp->next;
759 free_demux_packet(dp);
760 dp = dn;
762 if (ds->asf_packet) {
763 // free unfinished .asf fragments:
764 free(ds->asf_packet->buffer);
765 free(ds->asf_packet);
766 ds->asf_packet = NULL;
768 ds->first = ds->last = NULL;
769 ds->packs = 0; // !!!!!
770 ds->bytes = 0;
771 if (ds->current)
772 free_demux_packet(ds->current);
773 ds->current = NULL;
774 ds->buffer = NULL;
775 ds->buffer_pos = ds->buffer_size;
776 ds->pts = 0;
777 ds->pts_bytes = 0;
780 int ds_get_packet(demux_stream_t *ds, unsigned char **start)
782 int len;
783 if (ds->buffer_pos >= ds->buffer_size) {
784 if (!ds_fill_buffer(ds)) {
785 // EOF
786 *start = NULL;
787 return -1;
790 len = ds->buffer_size - ds->buffer_pos;
791 *start = &ds->buffer[ds->buffer_pos];
792 ds->buffer_pos += len;
793 return len;
796 int ds_get_packet_pts(demux_stream_t *ds, unsigned char **start, double *pts)
798 int len;
799 *pts = MP_NOPTS_VALUE;
800 len = ds_get_packet(ds, start);
801 if (len < 0)
802 return len;
803 // Return pts unless this read starts from the middle of a packet
804 if (len == ds->buffer_pos)
805 *pts = ds->current->pts;
806 return len;
809 int ds_get_packet_sub(demux_stream_t *ds, unsigned char **start)
811 int len;
812 if (ds->buffer_pos >= ds->buffer_size) {
813 *start = NULL;
814 if (!ds->packs)
815 return -1; // no sub
816 if (!ds_fill_buffer(ds))
817 return -1; // EOF
819 len = ds->buffer_size - ds->buffer_pos;
820 *start = &ds->buffer[ds->buffer_pos];
821 ds->buffer_pos += len;
822 return len;
825 double ds_get_next_pts(demux_stream_t *ds)
827 demuxer_t *demux = ds->demuxer;
828 // if we have not read from the "current" packet, consider it
829 // as the next, otherwise we never get the pts for the first packet.
830 while (!ds->first && (!ds->current || ds->buffer_pos)) {
831 if (demux->audio->packs >= MAX_PACKS
832 || demux->audio->bytes >= MAX_PACK_BYTES) {
833 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
834 demux->audio->packs, demux->audio->bytes);
835 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
836 return MP_NOPTS_VALUE;
838 if (demux->video->packs >= MAX_PACKS
839 || demux->video->bytes >= MAX_PACK_BYTES) {
840 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
841 demux->video->packs, demux->video->bytes);
842 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
843 return MP_NOPTS_VALUE;
845 if (!demux_fill_buffer(demux, ds))
846 return MP_NOPTS_VALUE;
848 // take pts from "current" if we never read from it.
849 if (ds->current && !ds->buffer_pos)
850 return ds->current->pts;
851 return ds->first->pts;
854 // ====================================================================
856 void demuxer_help(void)
858 int i;
860 mp_msg(MSGT_DEMUXER, MSGL_INFO, "Available demuxers:\n");
861 mp_msg(MSGT_DEMUXER, MSGL_INFO, " demuxer: type info: (comment)\n");
862 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DEMUXERS\n");
863 for (i = 0; demuxer_list[i]; i++) {
864 if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Don't display special demuxers
865 continue;
866 if (demuxer_list[i]->comment && strlen(demuxer_list[i]->comment))
867 mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s (%s)\n",
868 demuxer_list[i]->name, demuxer_list[i]->type,
869 demuxer_list[i]->info, demuxer_list[i]->comment);
870 else
871 mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s\n",
872 demuxer_list[i]->name, demuxer_list[i]->type,
873 demuxer_list[i]->info);
879 * Get demuxer type for a given demuxer name
881 * @param demuxer_name string with demuxer name of demuxer number
882 * @param force will be set if demuxer should be forced.
883 * May be NULL.
884 * @return DEMUXER_TYPE_xxx, -1 if error or not found
886 int get_demuxer_type_from_name(char *demuxer_name, int *force)
888 int i;
889 long type_int;
890 char *endptr;
892 if (!demuxer_name || !demuxer_name[0])
893 return DEMUXER_TYPE_UNKNOWN;
894 if (force)
895 *force = demuxer_name[0] == '+';
896 if (demuxer_name[0] == '+')
897 demuxer_name = &demuxer_name[1];
898 for (i = 0; demuxer_list[i]; i++) {
899 if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Can't select special demuxers from commandline
900 continue;
901 if (strcmp(demuxer_name, demuxer_list[i]->name) == 0)
902 return demuxer_list[i]->type;
905 // No match found, try to parse name as an integer (demuxer number)
906 type_int = strtol(demuxer_name, &endptr, 0);
907 if (*endptr) // Conversion failed
908 return -1;
909 if ((type_int > 0) && (type_int <= DEMUXER_TYPE_MAX))
910 return (int) type_int;
912 return -1;
915 int extension_parsing = 1; // 0=off 1=mixed (used only for unstable formats)
918 NOTE : Several demuxers may be opened at the same time so
919 demuxers should NEVER rely on an external var to enable them
920 self. If a demuxer can't do any auto-detection it should only use
921 file_format. The user can explicitly set file_format with the -demuxer
922 option so there is really no need for another extra var.
923 For convenience an option can be added to set file_format directly
924 to the right type (ex: rawaudio,rawvideo).
925 Also the stream can override the file_format so a demuxer which rely
926 on a special stream type can set file_format at the stream level
927 (ex: tv,mf).
930 static demuxer_t *demux_open_stream(struct MPOpts *opts, stream_t *stream,
931 int file_format, int force, int audio_id,
932 int video_id, int dvdsub_id,
933 char *filename)
935 demuxer_t *demuxer = NULL;
937 sh_video_t *sh_video = NULL;
939 const demuxer_desc_t *demuxer_desc;
940 int fformat = 0;
941 int i;
943 // If somebody requested a demuxer check it
944 if (file_format) {
945 if ((demuxer_desc = get_demuxer_desc_from_type(file_format))) {
946 demuxer = new_demuxer(opts, stream, demuxer_desc->type, audio_id,
947 video_id, dvdsub_id, filename);
948 if (demuxer_desc->check_file)
949 fformat = demuxer_desc->check_file(demuxer);
950 if (force || !demuxer_desc->check_file)
951 fformat = demuxer_desc->type;
952 if (fformat != 0) {
953 if (fformat == demuxer_desc->type) {
954 demuxer_t *demux2 = demuxer;
955 // Move messages to demuxer detection code?
956 mp_tmsg(MSGT_DEMUXER, MSGL_INFO,
957 "%s file format detected.\n",
958 demuxer_desc->shortdesc);
959 file_format = fformat;
960 if (!demuxer->desc->open
961 || (demux2 = demuxer->desc->open(demuxer))) {
962 demuxer = demux2;
963 goto dmx_open;
965 } else {
966 // Format changed after check, recurse
967 free_demuxer(demuxer);
968 return demux_open_stream(opts, stream, fformat, force,
969 audio_id, video_id, dvdsub_id,
970 filename);
973 // Check failed for forced demuxer, quit
974 free_demuxer(demuxer);
975 return NULL;
978 // Test demuxers with safe file checks
979 for (i = 0; (demuxer_desc = demuxer_list[i]); i++) {
980 if (demuxer_desc->safe_check) {
981 demuxer = new_demuxer(opts, stream, demuxer_desc->type, audio_id,
982 video_id, dvdsub_id, filename);
983 if ((fformat = demuxer_desc->check_file(demuxer)) != 0) {
984 if (fformat == demuxer_desc->type) {
985 demuxer_t *demux2 = demuxer;
986 mp_tmsg(MSGT_DEMUXER, MSGL_INFO,
987 "%s file format detected.\n",
988 demuxer_desc->shortdesc);
989 file_format = fformat;
990 if (!demuxer->desc->open
991 || (demux2 = demuxer->desc->open(demuxer))) {
992 demuxer = demux2;
993 goto dmx_open;
995 } else {
996 if (fformat == DEMUXER_TYPE_PLAYLIST)
997 return demuxer; // handled in mplayer.c
998 // Format changed after check, recurse
999 free_demuxer(demuxer);
1000 demuxer = demux_open_stream(opts, stream, fformat, force,
1001 audio_id, video_id,
1002 dvdsub_id, filename);
1003 if (demuxer)
1004 return demuxer; // done!
1005 file_format = DEMUXER_TYPE_UNKNOWN;
1008 free_demuxer(demuxer);
1009 demuxer = NULL;
1013 // If no forced demuxer perform file extension based detection
1014 // Ok. We're over the stable detectable fileformats, the next ones are
1015 // a bit fuzzy. So by default (extension_parsing==1) try extension-based
1016 // detection first:
1017 if (file_format == DEMUXER_TYPE_UNKNOWN && filename
1018 && extension_parsing == 1) {
1019 file_format = demuxer_type_by_filename(filename);
1020 if (file_format != DEMUXER_TYPE_UNKNOWN) {
1021 // we like recursion :)
1022 demuxer = demux_open_stream(opts, stream, file_format, force,
1023 audio_id, video_id, dvdsub_id,
1024 filename);
1025 if (demuxer)
1026 return demuxer; // done!
1027 file_format = DEMUXER_TYPE_UNKNOWN; // continue fuzzy guessing...
1028 mp_msg(MSGT_DEMUXER, MSGL_V,
1029 "demuxer: continue fuzzy content-based format guessing...\n");
1032 // Try detection for all other demuxers
1033 for (i = 0; (demuxer_desc = demuxer_list[i]); i++) {
1034 if (!demuxer_desc->safe_check && demuxer_desc->check_file) {
1035 demuxer = new_demuxer(opts, stream, demuxer_desc->type, audio_id,
1036 video_id, dvdsub_id, filename);
1037 if ((fformat = demuxer_desc->check_file(demuxer)) != 0) {
1038 if (fformat == demuxer_desc->type) {
1039 demuxer_t *demux2 = demuxer;
1040 mp_tmsg(MSGT_DEMUXER, MSGL_INFO,
1041 "%s file format detected.\n",
1042 demuxer_desc->shortdesc);
1043 file_format = fformat;
1044 if (!demuxer->desc->open
1045 || (demux2 = demuxer->desc->open(demuxer))) {
1046 demuxer = demux2;
1047 goto dmx_open;
1049 } else {
1050 if (fformat == DEMUXER_TYPE_PLAYLIST)
1051 return demuxer; // handled in mplayer.c
1052 // Format changed after check, recurse
1053 free_demuxer(demuxer);
1054 demuxer = demux_open_stream(opts, stream, fformat, force,
1055 audio_id, video_id,
1056 dvdsub_id, filename);
1057 if (demuxer)
1058 return demuxer; // done!
1059 file_format = DEMUXER_TYPE_UNKNOWN;
1062 free_demuxer(demuxer);
1063 demuxer = NULL;
1067 return NULL;
1068 //====== File format recognized, set up these for compatibility: =========
1069 dmx_open:
1071 demuxer->file_format = file_format;
1073 if ((sh_video = demuxer->video->sh) && sh_video->bih) {
1074 int biComp = le2me_32(sh_video->bih->biCompression);
1075 mp_msg(MSGT_DEMUX, MSGL_INFO,
1076 "VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
1077 (char *) &biComp, sh_video->bih->biWidth,
1078 sh_video->bih->biHeight, sh_video->bih->biBitCount,
1079 sh_video->fps, sh_video->i_bps * 0.008f,
1080 sh_video->i_bps / 1024.0f);
1082 #ifdef CONFIG_ASS
1083 if (opts->ass_enabled && ass_library) {
1084 for (i = 0; i < MAX_S_STREAMS; ++i) {
1085 sh_sub_t *sh = demuxer->s_streams[i];
1086 if (sh && sh->type == 'a') {
1087 sh->ass_track = ass_new_track(ass_library);
1088 if (sh->ass_track && sh->extradata)
1089 ass_process_codec_private(sh->ass_track, sh->extradata,
1090 sh->extradata_len);
1091 } else if (sh && sh->type != 'v')
1092 sh->ass_track = ass_default_track(ass_library);
1095 #endif
1096 return demuxer;
1099 char *audio_stream = NULL;
1100 char *sub_stream = NULL;
1101 int audio_stream_cache = 0;
1103 char *demuxer_name = NULL; // parameter from -demuxer
1104 char *audio_demuxer_name = NULL; // parameter from -audio-demuxer
1105 char *sub_demuxer_name = NULL; // parameter from -sub-demuxer
1107 extern int hr_mp3_seek;
1109 extern float stream_cache_min_percent;
1110 extern float stream_cache_seek_min_percent;
1112 demuxer_t *demux_open(struct MPOpts *opts, stream_t *vs, int file_format,
1113 int audio_id, int video_id, int dvdsub_id,
1114 char *filename)
1116 stream_t *as = NULL, *ss = NULL;
1117 demuxer_t *vd, *ad = NULL, *sd = NULL;
1118 demuxer_t *res;
1119 int afmt = DEMUXER_TYPE_UNKNOWN, sfmt = DEMUXER_TYPE_UNKNOWN;
1120 int demuxer_type;
1121 int audio_demuxer_type = 0, sub_demuxer_type = 0;
1122 int demuxer_force = 0, audio_demuxer_force = 0, sub_demuxer_force = 0;
1124 if ((demuxer_type =
1125 get_demuxer_type_from_name(demuxer_name, &demuxer_force)) < 0) {
1126 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-demuxer %s does not exist.\n",
1127 demuxer_name);
1129 if ((audio_demuxer_type =
1130 get_demuxer_type_from_name(audio_demuxer_name,
1131 &audio_demuxer_force)) < 0) {
1132 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-audio-demuxer %s does not exist.\n",
1133 audio_demuxer_name);
1135 if ((sub_demuxer_type =
1136 get_demuxer_type_from_name(sub_demuxer_name,
1137 &sub_demuxer_force)) < 0) {
1138 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-sub-demuxer %s does not exist.\n",
1139 sub_demuxer_name);
1142 if (audio_stream) {
1143 as = open_stream(audio_stream, 0, &afmt);
1144 if (!as) {
1145 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "Cannot open audio stream: %s\n",
1146 audio_stream);
1147 return NULL;
1149 if (audio_stream_cache) {
1150 if (!stream_enable_cache
1151 (as, audio_stream_cache * 1024,
1152 audio_stream_cache * 1024 * (stream_cache_min_percent /
1153 100.0),
1154 audio_stream_cache * 1024 * (stream_cache_seek_min_percent /
1155 100.0))) {
1156 free_stream(as);
1157 mp_msg(MSGT_DEMUXER, MSGL_ERR,
1158 "Can't enable audio stream cache\n");
1159 return NULL;
1163 if (sub_stream) {
1164 ss = open_stream(sub_stream, 0, &sfmt);
1165 if (!ss) {
1166 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "Cannot open subtitle stream: %s\n",
1167 sub_stream);
1168 return NULL;
1172 vd = demux_open_stream(opts, vs, demuxer_type ? demuxer_type : file_format,
1173 demuxer_force, audio_stream ? -2 : audio_id,
1174 video_id, sub_stream ? -2 : dvdsub_id, filename);
1175 if (!vd) {
1176 if (as)
1177 free_stream(as);
1178 if (ss)
1179 free_stream(ss);
1180 return NULL;
1182 if (as) {
1183 ad = demux_open_stream(opts, as,
1184 audio_demuxer_type ? audio_demuxer_type : afmt,
1185 audio_demuxer_force, audio_id, -2, -2,
1186 audio_stream);
1187 if (!ad) {
1188 mp_tmsg(MSGT_DEMUXER, MSGL_WARN, "Failed to open audio demuxer: %s\n",
1189 audio_stream);
1190 free_stream(as);
1191 } else if (ad->audio->sh
1192 && ((sh_audio_t *) ad->audio->sh)->format == 0x55) // MP3
1193 hr_mp3_seek = 1; // Enable high res seeking
1195 if (ss) {
1196 sd = demux_open_stream(opts, ss,
1197 sub_demuxer_type ? sub_demuxer_type : sfmt,
1198 sub_demuxer_force, -2, -2, dvdsub_id,
1199 sub_stream);
1200 if (!sd) {
1201 mp_tmsg(MSGT_DEMUXER, MSGL_WARN,
1202 "Failed to open subtitle demuxer: %s\n", sub_stream);
1203 free_stream(ss);
1207 if (ad && sd)
1208 res = new_demuxers_demuxer(vd, ad, sd);
1209 else if (ad)
1210 res = new_demuxers_demuxer(vd, ad, vd);
1211 else if (sd)
1212 res = new_demuxers_demuxer(vd, vd, sd);
1213 else
1214 res = vd;
1216 opts->correct_pts = opts->user_correct_pts;
1217 if (opts->correct_pts < 0)
1218 opts->correct_pts =
1219 demux_control(res, DEMUXER_CTRL_CORRECT_PTS,
1220 NULL) == DEMUXER_CTRL_OK;
1221 return res;
1225 void demux_flush(demuxer_t *demuxer)
1227 #if PARSE_ON_ADD
1228 ds_clear_parser(demuxer->video);
1229 ds_clear_parser(demuxer->audio);
1230 ds_clear_parser(demuxer->sub);
1231 #endif
1232 ds_free_packs(demuxer->video);
1233 ds_free_packs(demuxer->audio);
1234 ds_free_packs(demuxer->sub);
1237 int demux_seek(demuxer_t *demuxer, float rel_seek_secs, float audio_delay,
1238 int flags)
1240 if (!demuxer->seekable) {
1241 if (demuxer->file_format == DEMUXER_TYPE_AVI)
1242 mp_tmsg(MSGT_SEEK, MSGL_WARN, "Cannot seek in raw AVI streams. (Index required, try with the -idx switch.)\n");
1243 #ifdef CONFIG_TV
1244 else if (demuxer->file_format == DEMUXER_TYPE_TV)
1245 mp_tmsg(MSGT_SEEK, MSGL_WARN, "TV input is not seekable! (Seeking will probably be for changing channels ;)\n");
1246 #endif
1247 else
1248 mp_tmsg(MSGT_SEEK, MSGL_WARN, "Cannot seek in this file.\n");
1249 return 0;
1251 // clear demux buffers:
1252 demux_flush(demuxer);
1253 demuxer->video->eof = 0;
1254 demuxer->audio->eof = 0;
1255 demuxer->sub->eof = 0;
1257 /* HACK: assume any demuxer used with these streams can cope with
1258 * the stream layer suddenly seeking to a different position under it
1259 * (nothing actually implements DEMUXER_CTRL_RESYNC now).
1261 struct stream *stream = demuxer->stream;
1262 if (stream->type == STREAMTYPE_DVD || stream->type == STREAMTYPE_DVDNAV) {
1263 double pts;
1265 if (flags & SEEK_ABSOLUTE)
1266 pts = 0.0f;
1267 else {
1268 if (demuxer->stream_pts == MP_NOPTS_VALUE)
1269 goto dmx_seek;
1270 pts = demuxer->stream_pts;
1273 if (flags & SEEK_FACTOR) {
1274 double tmp = 0;
1275 if (stream_control(demuxer->stream, STREAM_CTRL_GET_TIME_LENGTH,
1276 &tmp) == STREAM_UNSUPPORTED)
1277 goto dmx_seek;
1278 pts += tmp * rel_seek_secs;
1279 } else
1280 pts += rel_seek_secs;
1282 if (stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_TIME, &pts)
1283 != STREAM_UNSUPPORTED) {
1284 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1285 return 1;
1289 dmx_seek:
1290 if (demuxer->desc->seek)
1291 demuxer->desc->seek(demuxer, rel_seek_secs, audio_delay, flags);
1293 return 1;
1296 int demux_info_add(demuxer_t *demuxer, const char *opt, const char *param)
1298 return demux_info_add_bstr(demuxer, BSTR(opt), BSTR(param));
1301 int demux_info_add_bstr(demuxer_t *demuxer, struct bstr opt, struct bstr param)
1303 char **info = demuxer->info;
1304 int n = 0;
1307 for (n = 0; info && info[2 * n] != NULL; n++) {
1308 if (!bstrcasecmp(opt, BSTR(info[2*n]))) {
1309 if (!bstrcmp(param, BSTR(info[2*n + 1]))) {
1310 mp_msg(MSGT_DEMUX, MSGL_V, "Demuxer info %.*s set to unchanged value %.*s\n",
1311 BSTR_P(opt), BSTR_P(param));
1312 return 0;
1314 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "Demuxer info %.*s changed to %.*s\n",
1315 BSTR_P(opt), BSTR_P(param));
1316 talloc_free(info[2*n + 1]);
1317 info[2*n + 1] = talloc_strndup(demuxer->info, param.start, param.len);
1318 return 0;
1322 info = demuxer->info = talloc_realloc(demuxer, info, char *, 2 * (n + 2));
1323 info[2*n] = talloc_strndup(demuxer->info, opt.start, opt.len);
1324 info[2*n + 1] = talloc_strndup(demuxer->info, param.start, param.len);
1325 memset(&info[2 * (n + 1)], 0, 2 * sizeof(char *));
1327 return 1;
1330 int demux_info_print(demuxer_t *demuxer)
1332 char **info = demuxer->info;
1333 int n;
1335 if (!info)
1336 return 0;
1338 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "Clip info:\n");
1339 for (n = 0; info[2 * n] != NULL; n++) {
1340 mp_msg(MSGT_DEMUX, MSGL_INFO, " %s: %s\n", info[2 * n],
1341 info[2 * n + 1]);
1342 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_NAME%d=%s\n", n,
1343 info[2 * n]);
1344 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_VALUE%d=%s\n", n,
1345 info[2 * n + 1]);
1347 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_N=%d\n", n);
1349 return 0;
1352 char *demux_info_get(demuxer_t *demuxer, const char *opt)
1354 int i;
1355 char **info = demuxer->info;
1357 for (i = 0; info && info[2 * i] != NULL; i++) {
1358 if (!strcasecmp(opt, info[2 * i]))
1359 return info[2 * i + 1];
1362 return NULL;
1365 int demux_control(demuxer_t *demuxer, int cmd, void *arg)
1368 if (demuxer->desc->control)
1369 return demuxer->desc->control(demuxer, cmd, arg);
1371 return DEMUXER_CTRL_NOTIMPL;
1376 double demuxer_get_time_length(demuxer_t *demuxer)
1378 double get_time_ans;
1379 sh_video_t *sh_video = demuxer->video->sh;
1380 sh_audio_t *sh_audio = demuxer->audio->sh;
1381 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
1382 if (demux_control
1383 (demuxer, DEMUXER_CTRL_GET_TIME_LENGTH, (void *) &get_time_ans) <= 0) {
1384 if (sh_video && sh_video->i_bps && sh_audio && sh_audio->i_bps)
1385 get_time_ans = (double) (demuxer->movi_end -
1386 demuxer->movi_start) / (sh_video->i_bps +
1387 sh_audio->i_bps);
1388 else if (sh_video && sh_video->i_bps)
1389 get_time_ans = (double) (demuxer->movi_end -
1390 demuxer->movi_start) / sh_video->i_bps;
1391 else if (sh_audio && sh_audio->i_bps)
1392 get_time_ans = (double) (demuxer->movi_end -
1393 demuxer->movi_start) / sh_audio->i_bps;
1394 else
1395 get_time_ans = 0;
1397 return get_time_ans;
1401 * \brief demuxer_get_current_time() returns the time of the current play in three possible ways:
1402 * either when the stream reader satisfies STREAM_CTRL_GET_CURRENT_TIME (e.g. dvd)
1403 * or using sh_video->pts when the former method fails
1404 * 0 otherwise
1405 * \return the current play time
1407 int demuxer_get_current_time(demuxer_t *demuxer)
1409 double get_time_ans = 0;
1410 sh_video_t *sh_video = demuxer->video->sh;
1411 if (demuxer->stream_pts != MP_NOPTS_VALUE)
1412 get_time_ans = demuxer->stream_pts;
1413 else if (sh_video)
1414 get_time_ans = sh_video->pts;
1415 return (int) get_time_ans;
1418 int demuxer_get_percent_pos(demuxer_t *demuxer)
1420 int ans = 0;
1421 int res = demux_control(demuxer, DEMUXER_CTRL_GET_PERCENT_POS, &ans);
1422 int len = (demuxer->movi_end - demuxer->movi_start) / 100;
1423 if (res <= 0) {
1424 off_t pos = demuxer->filepos > 0 ? demuxer->filepos : stream_tell(demuxer->stream);
1425 if (len > 0)
1426 ans = (pos - demuxer->movi_start) / len;
1427 else
1428 ans = 0;
1430 if (ans < 0)
1431 ans = 0;
1432 if (ans > 100)
1433 ans = 100;
1434 return ans;
1437 int demuxer_switch_audio(demuxer_t *demuxer, int index)
1439 int res = demux_control(demuxer, DEMUXER_CTRL_SWITCH_AUDIO, &index);
1440 if (res == DEMUXER_CTRL_NOTIMPL) {
1441 struct sh_audio *sh_audio = demuxer->audio->sh;
1442 return sh_audio ? sh_audio->aid : -2;
1444 if (demuxer->audio->id >= 0) {
1445 struct sh_audio *sh_audio = demuxer->a_streams[demuxer->audio->id];
1446 demuxer->audio->sh = sh_audio;
1447 index = sh_audio->aid; // internal MPEG demuxers don't set it right
1449 else
1450 demuxer->audio->sh = NULL;
1451 return index;
1454 int demuxer_switch_video(demuxer_t *demuxer, int index)
1456 int res = demux_control(demuxer, DEMUXER_CTRL_SWITCH_VIDEO, &index);
1457 if (res == DEMUXER_CTRL_NOTIMPL)
1458 index = demuxer->video->id;
1459 if (demuxer->video->id >= 0)
1460 demuxer->video->sh = demuxer->v_streams[demuxer->video->id];
1461 else
1462 demuxer->video->sh = NULL;
1463 return index;
1466 int demuxer_add_attachment(demuxer_t *demuxer, struct bstr name,
1467 struct bstr type, struct bstr data)
1469 if (!(demuxer->num_attachments % 32))
1470 demuxer->attachments = talloc_realloc(demuxer, demuxer->attachments,
1471 struct demux_attachment,
1472 demuxer->num_attachments + 32);
1474 struct demux_attachment *att =
1475 demuxer->attachments + demuxer->num_attachments;
1476 att->name = talloc_strndup(demuxer->attachments, name.start, name.len);
1477 att->type = talloc_strndup(demuxer->attachments, type.start, type.len);
1478 att->data = talloc_size(demuxer->attachments, data.len);
1479 memcpy(att->data, data.start, data.len);
1480 att->data_size = data.len;
1482 return demuxer->num_attachments++;
1485 int demuxer_add_chapter(demuxer_t *demuxer, struct bstr name,
1486 uint64_t start, uint64_t end)
1488 if (!(demuxer->num_chapters % 32))
1489 demuxer->chapters = talloc_realloc(demuxer, demuxer->chapters,
1490 struct demux_chapter,
1491 demuxer->num_chapters + 32);
1493 demuxer->chapters[demuxer->num_chapters].start = start;
1494 demuxer->chapters[demuxer->num_chapters].end = end;
1495 demuxer->chapters[demuxer->num_chapters].name = name.len ?
1496 talloc_strndup(demuxer->chapters, name.start, name.len) :
1497 talloc_strdup(demuxer->chapters, mp_gtext("unknown"));
1499 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_ID=%d\n", demuxer->num_chapters);
1500 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_START=%"PRIu64"\n", demuxer->num_chapters, start);
1501 if (end)
1502 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_END=%"PRIu64"\n", demuxer->num_chapters, end);
1503 if (name.start)
1504 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_NAME=%.*s\n", demuxer->num_chapters, BSTR_P(name));
1506 return demuxer->num_chapters++;
1510 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1511 * either using the demuxer->chapters structure set by the demuxer
1512 * or asking help to the stream layer (e.g. dvd)
1513 * \param chapter - chapter number wished - 0-based
1514 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1515 * \param chapter_name name of chapter found (set by this function is param is not null)
1516 * \return -1 on error, current chapter if successful
1519 int demuxer_seek_chapter(demuxer_t *demuxer, int chapter, double *seek_pts,
1520 char **chapter_name)
1522 int ris;
1524 if (!demuxer->num_chapters || !demuxer->chapters) {
1525 demux_flush(demuxer);
1527 ris = stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_CHAPTER,
1528 &chapter);
1529 if (ris != STREAM_UNSUPPORTED)
1530 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1532 // exit status may be ok, but main() doesn't have to seek itself
1533 // (because e.g. dvds depend on sectors, not on pts)
1534 *seek_pts = -1.0;
1536 if (chapter_name) {
1537 *chapter_name = NULL;
1538 int num_chapters;
1539 if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS,
1540 &num_chapters) == STREAM_UNSUPPORTED)
1541 num_chapters = 0;
1542 if (num_chapters) {
1543 *chapter_name = talloc_size(NULL, 16);
1544 sprintf(*chapter_name, " of %3d", num_chapters);
1548 return ris != STREAM_UNSUPPORTED ? chapter : -1;
1549 } else { // chapters structure is set in the demuxer
1550 if (chapter >= demuxer->num_chapters)
1551 return -1;
1552 if (chapter < 0)
1553 chapter = 0;
1555 *seek_pts = demuxer->chapters[chapter].start / 1000.0;
1557 if (chapter_name)
1558 *chapter_name = talloc_strdup(NULL, demuxer->chapters[chapter].name);
1560 return chapter;
1564 int demuxer_get_current_chapter(demuxer_t *demuxer)
1566 int chapter = -2;
1567 if (!demuxer->num_chapters || !demuxer->chapters) {
1568 if (stream_control(demuxer->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
1569 &chapter) == STREAM_UNSUPPORTED)
1570 chapter = -2;
1571 } else {
1572 sh_video_t *sh_video = demuxer->video->sh;
1573 sh_audio_t *sh_audio = demuxer->audio->sh;
1574 uint64_t now;
1575 now = (sh_video ? sh_video->pts : (sh_audio ? sh_audio->pts : 0))
1576 * 1000 + 0.5;
1577 for (chapter = demuxer->num_chapters - 1; chapter >= 0; --chapter) {
1578 if (demuxer->chapters[chapter].start <= now)
1579 break;
1582 return chapter;
1585 char *demuxer_chapter_name(demuxer_t *demuxer, int chapter)
1587 if (demuxer->num_chapters && demuxer->chapters) {
1588 if (chapter >= 0 && chapter < demuxer->num_chapters
1589 && demuxer->chapters[chapter].name)
1590 return strdup(demuxer->chapters[chapter].name);
1592 return NULL;
1595 char *demuxer_chapter_display_name(demuxer_t *demuxer, int chapter)
1597 char *chapter_name = demuxer_chapter_name(demuxer, chapter);
1598 if (chapter_name) {
1599 char *tmp = malloc(strlen(chapter_name) + 14);
1600 snprintf(tmp, 63, "(%d) %s", chapter + 1, chapter_name);
1601 free(chapter_name);
1602 return tmp;
1603 } else {
1604 int chapter_num = demuxer_chapter_count(demuxer);
1605 char tmp[30];
1606 if (chapter_num <= 0)
1607 sprintf(tmp, "(%d)", chapter + 1);
1608 else
1609 sprintf(tmp, "(%d) of %d", chapter + 1, chapter_num);
1610 return strdup(tmp);
1614 float demuxer_chapter_time(demuxer_t *demuxer, int chapter, float *end)
1616 if (demuxer->num_chapters && demuxer->chapters && chapter >= 0
1617 && chapter < demuxer->num_chapters) {
1618 if (end)
1619 *end = demuxer->chapters[chapter].end / 1000.0;
1620 return demuxer->chapters[chapter].start / 1000.0;
1622 return -1.0;
1625 int demuxer_chapter_count(demuxer_t *demuxer)
1627 if (!demuxer->num_chapters || !demuxer->chapters) {
1628 int num_chapters = 0;
1629 if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS,
1630 &num_chapters) == STREAM_UNSUPPORTED)
1631 num_chapters = 0;
1632 return num_chapters;
1633 } else
1634 return demuxer->num_chapters;
1637 int demuxer_angles_count(demuxer_t *demuxer)
1639 int ris, angles = -1;
1641 ris = stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_ANGLES, &angles);
1642 if (ris == STREAM_UNSUPPORTED)
1643 return -1;
1644 return angles;
1647 int demuxer_get_current_angle(demuxer_t *demuxer)
1649 int ris, curr_angle = -1;
1650 ris = stream_control(demuxer->stream, STREAM_CTRL_GET_ANGLE, &curr_angle);
1651 if (ris == STREAM_UNSUPPORTED)
1652 return -1;
1653 return curr_angle;
1657 int demuxer_set_angle(demuxer_t *demuxer, int angle)
1659 int ris, angles = -1;
1661 angles = demuxer_angles_count(demuxer);
1662 if ((angles < 1) || (angle > angles))
1663 return -1;
1665 demux_flush(demuxer);
1667 ris = stream_control(demuxer->stream, STREAM_CTRL_SET_ANGLE, &angle);
1668 if (ris == STREAM_UNSUPPORTED)
1669 return -1;
1671 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1673 return angle;
1676 int demuxer_audio_track_by_lang(demuxer_t *d, char *lang)
1678 int i, len;
1679 lang += strspn(lang, ",");
1680 while ((len = strcspn(lang, ",")) > 0) {
1681 for (i = 0; i < MAX_A_STREAMS; ++i) {
1682 sh_audio_t *sh = d->a_streams[i];
1683 if (sh && sh->lang && strncmp(sh->lang, lang, len) == 0)
1684 return sh->aid;
1686 lang += len;
1687 lang += strspn(lang, ",");
1689 return -1;
1692 int demuxer_sub_track_by_lang(demuxer_t *d, char *lang)
1694 int i, len;
1695 lang += strspn(lang, ",");
1696 while ((len = strcspn(lang, ",")) > 0) {
1697 for (i = 0; i < MAX_S_STREAMS; ++i) {
1698 sh_sub_t *sh = d->s_streams[i];
1699 if (sh && sh->lang && strncmp(sh->lang, lang, len) == 0)
1700 return sh->sid;
1702 lang += len;
1703 lang += strspn(lang, ",");
1705 return -1;
1708 int demuxer_default_audio_track(demuxer_t *d)
1710 int i;
1711 for (i = 0; i < MAX_A_STREAMS; ++i) {
1712 sh_audio_t *sh = d->a_streams[i];
1713 if (sh && sh->default_track)
1714 return sh->aid;
1716 for (i = 0; i < MAX_A_STREAMS; ++i) {
1717 sh_audio_t *sh = d->a_streams[i];
1718 if (sh)
1719 return sh->aid;
1721 return -1;
1724 int demuxer_default_sub_track(demuxer_t *d)
1726 int i;
1727 for (i = 0; i < MAX_S_STREAMS; ++i) {
1728 sh_sub_t *sh = d->s_streams[i];
1729 if (sh && sh->default_track)
1730 return sh->sid;
1732 return -1;