Raise LIBASS_VERSION, forgotten in r31293.
[mplayer/glamo.git] / libmpdemux / demuxer.c
blob0553429790400384a8e4c46de526056ac0502302
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 "mp_msg.h"
31 #include "help_mp.h"
32 #include "m_config.h"
34 #include "libvo/fastmemcpy.h"
36 #include "stream/stream.h"
37 #include "demuxer.h"
38 #include "stheader.h"
39 #include "mf.h"
41 #include "libaf/af_format.h"
42 #include "libmpcodecs/dec_teletext.h"
44 #ifdef CONFIG_ASS
45 #include "libass/ass.h"
46 #include "libass/ass_mp.h"
47 #endif
49 #ifdef CONFIG_LIBAVCODEC
50 #include "libavcodec/avcodec.h"
51 #if MP_INPUT_BUFFER_PADDING_SIZE < FF_INPUT_BUFFER_PADDING_SIZE
52 #error MP_INPUT_BUFFER_PADDING_SIZE is too small!
53 #endif
54 #endif
56 // This is quite experimental, in particular it will mess up the pts values
57 // in the queue - on the other hand it might fix some issues like generating
58 // broken files with mencoder and stream copy.
59 // Better leave it disabled for now, if we find no use for it this code should
60 // just be removed again.
61 #define PARSE_ON_ADD 0
63 static void clear_parser(sh_common_t *sh);
64 void resync_video_stream(sh_video_t *sh_video);
65 void resync_audio_stream(sh_audio_t *sh_audio);
67 // Demuxer list
68 extern const demuxer_desc_t demuxer_desc_rawaudio;
69 extern const demuxer_desc_t demuxer_desc_rawvideo;
70 extern const demuxer_desc_t demuxer_desc_tv;
71 extern const demuxer_desc_t demuxer_desc_mf;
72 extern const demuxer_desc_t demuxer_desc_avi;
73 extern const demuxer_desc_t demuxer_desc_y4m;
74 extern const demuxer_desc_t demuxer_desc_asf;
75 extern const demuxer_desc_t demuxer_desc_real;
76 extern const demuxer_desc_t demuxer_desc_smjpeg;
77 extern const demuxer_desc_t demuxer_desc_matroska;
78 extern const demuxer_desc_t demuxer_desc_realaudio;
79 extern const demuxer_desc_t demuxer_desc_vqf;
80 extern const demuxer_desc_t demuxer_desc_mov;
81 extern const demuxer_desc_t demuxer_desc_vivo;
82 extern const demuxer_desc_t demuxer_desc_fli;
83 extern const demuxer_desc_t demuxer_desc_film;
84 extern const demuxer_desc_t demuxer_desc_roq;
85 extern const demuxer_desc_t demuxer_desc_gif;
86 extern const demuxer_desc_t demuxer_desc_ogg;
87 extern const demuxer_desc_t demuxer_desc_avs;
88 extern const demuxer_desc_t demuxer_desc_pva;
89 extern const demuxer_desc_t demuxer_desc_nsv;
90 extern const demuxer_desc_t demuxer_desc_mpeg_ts;
91 extern const demuxer_desc_t demuxer_desc_lmlm4;
92 extern const demuxer_desc_t demuxer_desc_mpeg_ps;
93 extern const demuxer_desc_t demuxer_desc_mpeg_pes;
94 extern const demuxer_desc_t demuxer_desc_mpeg_es;
95 extern const demuxer_desc_t demuxer_desc_mpeg_gxf;
96 extern const demuxer_desc_t demuxer_desc_mpeg4_es;
97 extern const demuxer_desc_t demuxer_desc_h264_es;
98 extern const demuxer_desc_t demuxer_desc_rawdv;
99 extern const demuxer_desc_t demuxer_desc_mpc;
100 extern const demuxer_desc_t demuxer_desc_audio;
101 extern const demuxer_desc_t demuxer_desc_xmms;
102 extern const demuxer_desc_t demuxer_desc_mpeg_ty;
103 extern const demuxer_desc_t demuxer_desc_rtp;
104 extern const demuxer_desc_t demuxer_desc_rtp_nemesi;
105 extern const demuxer_desc_t demuxer_desc_lavf;
106 extern const demuxer_desc_t demuxer_desc_lavf_preferred;
107 extern const demuxer_desc_t demuxer_desc_aac;
108 extern const demuxer_desc_t demuxer_desc_nut;
109 extern const demuxer_desc_t demuxer_desc_mng;
111 /* Please do not add any new demuxers here. If you want to implement a new
112 * demuxer, add it to libavformat, except for wrappers around external
113 * libraries and demuxers requiring binary support. */
115 const demuxer_desc_t *const demuxer_list[] = {
116 &demuxer_desc_rawaudio,
117 &demuxer_desc_rawvideo,
118 #ifdef CONFIG_TV
119 &demuxer_desc_tv,
120 #endif
121 &demuxer_desc_mf,
122 #ifdef CONFIG_LIBAVFORMAT
123 &demuxer_desc_lavf_preferred,
124 #endif
125 &demuxer_desc_avi,
126 &demuxer_desc_y4m,
127 &demuxer_desc_asf,
128 &demuxer_desc_nsv,
129 &demuxer_desc_real,
130 &demuxer_desc_smjpeg,
131 &demuxer_desc_matroska,
132 &demuxer_desc_realaudio,
133 &demuxer_desc_vqf,
134 &demuxer_desc_mov,
135 &demuxer_desc_vivo,
136 &demuxer_desc_fli,
137 &demuxer_desc_film,
138 &demuxer_desc_roq,
139 #ifdef CONFIG_GIF
140 &demuxer_desc_gif,
141 #endif
142 #ifdef CONFIG_OGGVORBIS
143 &demuxer_desc_ogg,
144 #endif
145 #ifdef CONFIG_WIN32DLL
146 &demuxer_desc_avs,
147 #endif
148 &demuxer_desc_pva,
149 &demuxer_desc_mpeg_ts,
150 &demuxer_desc_lmlm4,
151 &demuxer_desc_mpeg_ps,
152 &demuxer_desc_mpeg_pes,
153 &demuxer_desc_mpeg_es,
154 &demuxer_desc_mpeg_gxf,
155 &demuxer_desc_mpeg4_es,
156 &demuxer_desc_h264_es,
157 &demuxer_desc_audio,
158 &demuxer_desc_mpeg_ty,
159 #ifdef CONFIG_LIVE555
160 &demuxer_desc_rtp,
161 #endif
162 #ifdef CONFIG_LIBNEMESI
163 &demuxer_desc_rtp_nemesi,
164 #endif
165 #ifdef CONFIG_LIBAVFORMAT
166 &demuxer_desc_lavf,
167 #endif
168 #ifdef CONFIG_MUSEPACK
169 &demuxer_desc_mpc,
170 #endif
171 #ifdef CONFIG_LIBDV095
172 &demuxer_desc_rawdv,
173 #endif
174 &demuxer_desc_aac,
175 #ifdef CONFIG_LIBNUT
176 &demuxer_desc_nut,
177 #endif
178 #ifdef CONFIG_XMMS
179 &demuxer_desc_xmms,
180 #endif
181 #ifdef CONFIG_MNG
182 &demuxer_desc_mng,
183 #endif
184 /* Please do not add any new demuxers here. If you want to implement a new
185 * demuxer, add it to libavformat, except for wrappers around external
186 * libraries and demuxers requiring binary support. */
187 NULL
190 void free_demuxer_stream(demux_stream_t *ds)
192 ds_free_packs(ds);
193 free(ds);
196 demux_stream_t *new_demuxer_stream(struct demuxer *demuxer, int id)
198 demux_stream_t *ds = malloc(sizeof(demux_stream_t));
199 *ds = (demux_stream_t){
200 .id = id,
201 .demuxer = demuxer,
202 .asf_seq = -1,
204 return ds;
209 * Get demuxer description structure for a given demuxer type
211 * @param file_format type of the demuxer
212 * @return structure for the demuxer, NULL if not found
214 static const demuxer_desc_t *get_demuxer_desc_from_type(int file_format)
216 int i;
218 for (i = 0; demuxer_list[i]; i++)
219 if (file_format == demuxer_list[i]->type)
220 return demuxer_list[i];
222 return NULL;
226 demuxer_t *new_demuxer(stream_t *stream, int type, int a_id, int v_id,
227 int s_id, char *filename)
229 demuxer_t *d = malloc(sizeof(demuxer_t));
230 memset(d, 0, sizeof(demuxer_t));
231 d->stream = stream;
232 d->stream_pts = MP_NOPTS_VALUE;
233 d->reference_clock = MP_NOPTS_VALUE;
234 d->movi_start = stream->start_pos;
235 d->movi_end = stream->end_pos;
236 d->seekable = 1;
237 d->synced = 0;
238 d->filepos = -1;
239 d->audio = new_demuxer_stream(d, a_id);
240 d->video = new_demuxer_stream(d, v_id);
241 d->sub = new_demuxer_stream(d, s_id);
242 d->type = type;
243 if (type)
244 if (!(d->desc = get_demuxer_desc_from_type(type)))
245 mp_msg(MSGT_DEMUXER, MSGL_ERR,
246 "BUG! Invalid demuxer type in new_demuxer(), "
247 "big troubles ahead.");
248 if (filename) // Filename hack for avs_check_file
249 d->filename = strdup(filename);
250 stream->eof = 0;
251 stream_seek(stream, stream->start_pos);
252 return d;
255 extern int dvdsub_id;
257 sh_sub_t *new_sh_sub_sid(demuxer_t *demuxer, int id, int sid)
259 if (id > MAX_S_STREAMS - 1 || id < 0) {
260 mp_msg(MSGT_DEMUXER, MSGL_WARN,
261 "Requested sub stream id overflow (%d > %d)\n", id,
262 MAX_S_STREAMS);
263 return NULL;
265 if (demuxer->s_streams[id])
266 mp_msg(MSGT_DEMUXER, MSGL_WARN, "Sub stream %i redefined\n", id);
267 else {
268 sh_sub_t *sh = calloc(1, sizeof(sh_sub_t));
269 demuxer->s_streams[id] = sh;
270 sh->sid = sid;
271 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SUBTITLE_ID=%d\n", sid);
273 if (sid == dvdsub_id) {
274 demuxer->sub->id = id;
275 demuxer->sub->sh = demuxer->s_streams[id];
277 return demuxer->s_streams[id];
280 static void free_sh_sub(sh_sub_t *sh)
282 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_sub at %p\n", sh);
283 free(sh->extradata);
284 #ifdef CONFIG_ASS
285 if (sh->ass_track)
286 ass_free_track(sh->ass_track);
287 #endif
288 free(sh->lang);
289 #ifdef CONFIG_LIBAVCODEC
290 clear_parser((sh_common_t *)sh);
291 #endif
292 free(sh);
295 sh_audio_t *new_sh_audio_aid(demuxer_t *demuxer, int id, int aid)
297 if (id > MAX_A_STREAMS - 1 || id < 0) {
298 mp_msg(MSGT_DEMUXER, MSGL_WARN,
299 "Requested audio stream id overflow (%d > %d)\n", id,
300 MAX_A_STREAMS);
301 return NULL;
303 if (demuxer->a_streams[id])
304 mp_msg(MSGT_DEMUXER, MSGL_WARN, MSGTR_AudioStreamRedefined, id);
305 else {
306 sh_audio_t *sh = calloc(1, sizeof(sh_audio_t));
307 mp_msg(MSGT_DEMUXER, MSGL_V, MSGTR_FoundAudioStream, id);
308 demuxer->a_streams[id] = sh;
309 sh->aid = aid;
310 sh->ds = demuxer->audio;
311 // set some defaults
312 sh->samplesize = 2;
313 sh->sample_format = AF_FORMAT_S16_NE;
314 sh->audio_out_minsize = 8192; /* default size, maybe not enough for Win32/ACM */
315 sh->pts = MP_NOPTS_VALUE;
316 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_ID=%d\n", aid);
318 return demuxer->a_streams[id];
321 void free_sh_audio(demuxer_t *demuxer, int id)
323 sh_audio_t *sh = demuxer->a_streams[id];
324 demuxer->a_streams[id] = NULL;
325 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_audio at %p\n", sh);
326 free(sh->wf);
327 free(sh->codecdata);
328 free(sh->lang);
329 #ifdef CONFIG_LIBAVCODEC
330 clear_parser((sh_common_t *)sh);
331 #endif
332 free(sh);
335 sh_video_t *new_sh_video_vid(demuxer_t *demuxer, int id, int vid)
337 if (id > MAX_V_STREAMS - 1 || id < 0) {
338 mp_msg(MSGT_DEMUXER, MSGL_WARN,
339 "Requested video stream id overflow (%d > %d)\n", id,
340 MAX_V_STREAMS);
341 return NULL;
343 if (demuxer->v_streams[id])
344 mp_msg(MSGT_DEMUXER, MSGL_WARN, MSGTR_VideoStreamRedefined, id);
345 else {
346 sh_video_t *sh = calloc(1, sizeof(sh_video_t));
347 mp_msg(MSGT_DEMUXER, MSGL_V, MSGTR_FoundVideoStream, id);
348 demuxer->v_streams[id] = sh;
349 sh->vid = vid;
350 sh->ds = demuxer->video;
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->chapters) {
398 for (i = 0; i < demuxer->num_chapters; i++)
399 free(demuxer->chapters[i].name);
400 free(demuxer->chapters);
402 if (demuxer->attachments) {
403 for (i = 0; i < demuxer->num_attachments; i++) {
404 free(demuxer->attachments[i].name);
405 free(demuxer->attachments[i].type);
406 free(demuxer->attachments[i].data);
408 free(demuxer->attachments);
410 if (demuxer->teletext)
411 teletext_control(demuxer->teletext, TV_VBI_CONTROL_STOP, NULL);
412 free(demuxer);
416 static void ds_add_packet_internal(demux_stream_t *ds, demux_packet_t *dp)
418 // append packet to DS stream:
419 ++ds->packs;
420 ds->bytes += dp->len;
421 if (ds->last) {
422 // next packet in stream
423 ds->last->next = dp;
424 ds->last = dp;
425 } else {
426 // first packet in stream
427 ds->first = ds->last = dp;
429 mp_dbg(MSGT_DEMUXER, MSGL_DBG2,
430 "DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
431 (ds == ds->demuxer->audio) ? "d_audio" : "d_video", dp->len,
432 dp->pts, (unsigned int) dp->pos, ds->demuxer->audio->packs,
433 ds->demuxer->video->packs);
436 #ifdef CONFIG_LIBAVCODEC
437 static void allocate_parser(AVCodecContext **avctx, AVCodecParserContext **parser, unsigned format)
439 enum CodecID codec_id = CODEC_ID_NONE;
440 extern int avcodec_initialized;
441 if (!avcodec_initialized) {
442 avcodec_init();
443 avcodec_register_all();
444 avcodec_initialized = 1;
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_LIBAVCODEC)
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 return 1;
642 if (demux->audio->packs >= MAX_PACKS
643 || demux->audio->bytes >= MAX_PACK_BYTES) {
644 mp_msg(MSGT_DEMUXER, MSGL_ERR, MSGTR_TooManyAudioInBuffer,
645 demux->audio->packs, demux->audio->bytes);
646 mp_msg(MSGT_DEMUXER, MSGL_HINT, MSGTR_MaybeNI);
647 break;
649 if (demux->video->packs >= MAX_PACKS
650 || demux->video->bytes >= MAX_PACK_BYTES) {
651 mp_msg(MSGT_DEMUXER, MSGL_ERR, MSGTR_TooManyVideoInBuffer,
652 demux->video->packs, demux->video->bytes);
653 mp_msg(MSGT_DEMUXER, MSGL_HINT, MSGTR_MaybeNI);
654 break;
656 if (!demux_fill_buffer(demux, ds)) {
657 #if PARSE_ON_ADD && defined(CONFIG_LIBAVCODEC)
658 uint8_t *parsed_start = NULL;
659 int parsed_len = 0;
660 ds_parse(ds->sh, &parsed_start, &parsed_len, MP_NOPTS_VALUE, 0);
661 if (parsed_len) {
662 demux_packet_t *dp2 = new_demux_packet(parsed_len);
663 dp2->pts = MP_NOPTS_VALUE;
664 memcpy(dp2->buffer, parsed_start, parsed_len);
665 ds_add_packet_internal(ds, dp2);
666 continue;
668 #endif
669 mp_dbg(MSGT_DEMUXER, MSGL_DBG2,
670 "ds_fill_buffer()->demux_fill_buffer() failed\n");
671 break; // EOF
674 ds->buffer_pos = ds->buffer_size = 0;
675 ds->buffer = NULL;
676 mp_msg(MSGT_DEMUXER, MSGL_V,
677 "ds_fill_buffer: EOF reached (stream: %s) \n",
678 ds == demux->audio ? "audio" : "video");
679 ds->eof = 1;
680 return 0;
683 int demux_read_data(demux_stream_t *ds, unsigned char *mem, int len)
685 int x;
686 int bytes = 0;
687 while (len > 0) {
688 x = ds->buffer_size - ds->buffer_pos;
689 if (x == 0) {
690 if (!ds_fill_buffer(ds))
691 return bytes;
692 } else {
693 if (x > len)
694 x = len;
695 if (mem)
696 fast_memcpy(mem + bytes, &ds->buffer[ds->buffer_pos], x);
697 bytes += x;
698 len -= x;
699 ds->buffer_pos += x;
702 return bytes;
706 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
707 * \param mem memory to read data into, may be NULL to discard data
708 * \param maxlen maximum number of bytes to read
709 * \param read number of bytes actually read
710 * \param pattern pattern to search for (lowest 8 bits are ignored)
711 * \return whether pattern was found
713 int demux_pattern_3(demux_stream_t *ds, unsigned char *mem, int maxlen,
714 int *read, uint32_t pattern)
716 register uint32_t head = 0xffffff00;
717 register uint32_t pat = pattern & 0xffffff00;
718 int total_len = 0;
719 do {
720 register unsigned char *ds_buf = &ds->buffer[ds->buffer_size];
721 int len = ds->buffer_size - ds->buffer_pos;
722 register long pos = -len;
723 if (unlikely(pos >= 0)) { // buffer is empty
724 ds_fill_buffer(ds);
725 continue;
727 do {
728 head |= ds_buf[pos];
729 head <<= 8;
730 } while (++pos && head != pat);
731 len += pos;
732 if (total_len + len > maxlen)
733 len = maxlen - total_len;
734 len = demux_read_data(ds, mem ? &mem[total_len] : NULL, len);
735 total_len += len;
736 } while ((head != pat || total_len < 3) && total_len < maxlen && !ds->eof);
737 if (read)
738 *read = total_len;
739 return total_len >= 3 && head == pat;
742 void ds_free_packs(demux_stream_t *ds)
744 demux_packet_t *dp = ds->first;
745 while (dp) {
746 demux_packet_t *dn = dp->next;
747 free_demux_packet(dp);
748 dp = dn;
750 if (ds->asf_packet) {
751 // free unfinished .asf fragments:
752 free(ds->asf_packet->buffer);
753 free(ds->asf_packet);
754 ds->asf_packet = NULL;
756 ds->first = ds->last = NULL;
757 ds->packs = 0; // !!!!!
758 ds->bytes = 0;
759 if (ds->current)
760 free_demux_packet(ds->current);
761 ds->current = NULL;
762 ds->buffer = NULL;
763 ds->buffer_pos = ds->buffer_size;
764 ds->pts = 0;
765 ds->pts_bytes = 0;
768 int ds_get_packet(demux_stream_t *ds, unsigned char **start)
770 int len;
771 if (ds->buffer_pos >= ds->buffer_size) {
772 if (!ds_fill_buffer(ds)) {
773 // EOF
774 *start = NULL;
775 return -1;
778 len = ds->buffer_size - ds->buffer_pos;
779 *start = &ds->buffer[ds->buffer_pos];
780 ds->buffer_pos += len;
781 return len;
784 int ds_get_packet_pts(demux_stream_t *ds, unsigned char **start, double *pts)
786 int len;
787 *pts = MP_NOPTS_VALUE;
788 len = ds_get_packet(ds, start);
789 if (len < 0)
790 return len;
791 // Return pts unless this read starts from the middle of a packet
792 if (len == ds->buffer_pos)
793 *pts = ds->current->pts;
794 return len;
797 int ds_get_packet_sub(demux_stream_t *ds, unsigned char **start)
799 int len;
800 if (ds->buffer_pos >= ds->buffer_size) {
801 *start = NULL;
802 if (!ds->packs)
803 return -1; // no sub
804 if (!ds_fill_buffer(ds))
805 return -1; // EOF
807 len = ds->buffer_size - ds->buffer_pos;
808 *start = &ds->buffer[ds->buffer_pos];
809 ds->buffer_pos += len;
810 return len;
813 double ds_get_next_pts(demux_stream_t *ds)
815 demuxer_t *demux = ds->demuxer;
816 while (!ds->first) {
817 if (demux->audio->packs >= MAX_PACKS
818 || demux->audio->bytes >= MAX_PACK_BYTES) {
819 mp_msg(MSGT_DEMUXER, MSGL_ERR, MSGTR_TooManyAudioInBuffer,
820 demux->audio->packs, demux->audio->bytes);
821 mp_msg(MSGT_DEMUXER, MSGL_HINT, MSGTR_MaybeNI);
822 return MP_NOPTS_VALUE;
824 if (demux->video->packs >= MAX_PACKS
825 || demux->video->bytes >= MAX_PACK_BYTES) {
826 mp_msg(MSGT_DEMUXER, MSGL_ERR, MSGTR_TooManyVideoInBuffer,
827 demux->video->packs, demux->video->bytes);
828 mp_msg(MSGT_DEMUXER, MSGL_HINT, MSGTR_MaybeNI);
829 return MP_NOPTS_VALUE;
831 if (!demux_fill_buffer(demux, ds))
832 return MP_NOPTS_VALUE;
834 return ds->first->pts;
837 // ====================================================================
839 void demuxer_help(void)
841 int i;
843 mp_msg(MSGT_DEMUXER, MSGL_INFO, "Available demuxers:\n");
844 mp_msg(MSGT_DEMUXER, MSGL_INFO, " demuxer: type info: (comment)\n");
845 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DEMUXERS\n");
846 for (i = 0; demuxer_list[i]; i++) {
847 if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Don't display special demuxers
848 continue;
849 if (demuxer_list[i]->comment && strlen(demuxer_list[i]->comment))
850 mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s (%s)\n",
851 demuxer_list[i]->name, demuxer_list[i]->type,
852 demuxer_list[i]->info, demuxer_list[i]->comment);
853 else
854 mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s\n",
855 demuxer_list[i]->name, demuxer_list[i]->type,
856 demuxer_list[i]->info);
862 * Get demuxer type for a given demuxer name
864 * @param demuxer_name string with demuxer name of demuxer number
865 * @param force will be set if demuxer should be forced.
866 * May be NULL.
867 * @return DEMUXER_TYPE_xxx, -1 if error or not found
869 int get_demuxer_type_from_name(char *demuxer_name, int *force)
871 int i;
872 long type_int;
873 char *endptr;
875 if (!demuxer_name || !demuxer_name[0])
876 return DEMUXER_TYPE_UNKNOWN;
877 if (force)
878 *force = demuxer_name[0] == '+';
879 if (demuxer_name[0] == '+')
880 demuxer_name = &demuxer_name[1];
881 for (i = 0; demuxer_list[i]; i++) {
882 if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Can't select special demuxers from commandline
883 continue;
884 if (strcmp(demuxer_name, demuxer_list[i]->name) == 0)
885 return demuxer_list[i]->type;
888 // No match found, try to parse name as an integer (demuxer number)
889 type_int = strtol(demuxer_name, &endptr, 0);
890 if (*endptr) // Conversion failed
891 return -1;
892 if ((type_int > 0) && (type_int <= DEMUXER_TYPE_MAX))
893 return (int) type_int;
895 return -1;
898 int extension_parsing = 1; // 0=off 1=mixed (used only for unstable formats)
900 int correct_pts = 0;
901 int user_correct_pts = -1;
904 NOTE : Several demuxers may be opened at the same time so
905 demuxers should NEVER rely on an external var to enable them
906 self. If a demuxer can't do any auto-detection it should only use
907 file_format. The user can explicitly set file_format with the -demuxer
908 option so there is really no need for another extra var.
909 For convenience an option can be added to set file_format directly
910 to the right type (ex: rawaudio,rawvideo).
911 Also the stream can override the file_format so a demuxer which rely
912 on a special stream type can set file_format at the stream level
913 (ex: tv,mf).
916 static demuxer_t *demux_open_stream(stream_t *stream, int file_format,
917 int force, int audio_id, int video_id,
918 int dvdsub_id, char *filename)
920 demuxer_t *demuxer = NULL;
922 sh_video_t *sh_video = NULL;
924 const demuxer_desc_t *demuxer_desc;
925 int fformat = 0;
926 int i;
928 // If somebody requested a demuxer check it
929 if (file_format) {
930 if ((demuxer_desc = get_demuxer_desc_from_type(file_format))) {
931 demuxer = new_demuxer(stream, demuxer_desc->type, audio_id,
932 video_id, dvdsub_id, filename);
933 if (demuxer_desc->check_file)
934 fformat = demuxer_desc->check_file(demuxer);
935 if (force || !demuxer_desc->check_file)
936 fformat = demuxer_desc->type;
937 if (fformat != 0) {
938 if (fformat == demuxer_desc->type) {
939 demuxer_t *demux2 = demuxer;
940 // Move messages to demuxer detection code?
941 mp_msg(MSGT_DEMUXER, MSGL_INFO,
942 MSGTR_Detected_XXX_FileFormat,
943 demuxer_desc->shortdesc);
944 file_format = fformat;
945 if (!demuxer->desc->open
946 || (demux2 = demuxer->desc->open(demuxer))) {
947 demuxer = demux2;
948 goto dmx_open;
950 } else {
951 // Format changed after check, recurse
952 free_demuxer(demuxer);
953 return demux_open_stream(stream, fformat, force, audio_id,
954 video_id, dvdsub_id, filename);
957 // Check failed for forced demuxer, quit
958 free_demuxer(demuxer);
959 return NULL;
962 // Test demuxers with safe file checks
963 for (i = 0; (demuxer_desc = demuxer_list[i]); i++) {
964 if (demuxer_desc->safe_check) {
965 demuxer = new_demuxer(stream, demuxer_desc->type, audio_id,
966 video_id, dvdsub_id, filename);
967 if ((fformat = demuxer_desc->check_file(demuxer)) != 0) {
968 if (fformat == demuxer_desc->type) {
969 demuxer_t *demux2 = demuxer;
970 mp_msg(MSGT_DEMUXER, MSGL_INFO,
971 MSGTR_Detected_XXX_FileFormat,
972 demuxer_desc->shortdesc);
973 file_format = fformat;
974 if (!demuxer->desc->open
975 || (demux2 = demuxer->desc->open(demuxer))) {
976 demuxer = demux2;
977 goto dmx_open;
979 } else {
980 if (fformat == DEMUXER_TYPE_PLAYLIST)
981 return demuxer; // handled in mplayer.c
982 // Format changed after check, recurse
983 free_demuxer(demuxer);
984 demuxer = demux_open_stream(stream, fformat, force,
985 audio_id, video_id,
986 dvdsub_id, filename);
987 if (demuxer)
988 return demuxer; // done!
989 file_format = DEMUXER_TYPE_UNKNOWN;
992 free_demuxer(demuxer);
993 demuxer = NULL;
997 // If no forced demuxer perform file extension based detection
998 // Ok. We're over the stable detectable fileformats, the next ones are
999 // a bit fuzzy. So by default (extension_parsing==1) try extension-based
1000 // detection first:
1001 if (file_format == DEMUXER_TYPE_UNKNOWN && filename
1002 && extension_parsing == 1) {
1003 file_format = demuxer_type_by_filename(filename);
1004 if (file_format != DEMUXER_TYPE_UNKNOWN) {
1005 // we like recursion :)
1006 demuxer = demux_open_stream(stream, file_format, force, audio_id,
1007 video_id, dvdsub_id, filename);
1008 if (demuxer)
1009 return demuxer; // done!
1010 file_format = DEMUXER_TYPE_UNKNOWN; // continue fuzzy guessing...
1011 mp_msg(MSGT_DEMUXER, MSGL_V,
1012 "demuxer: continue fuzzy content-based format guessing...\n");
1015 // Try detection for all other demuxers
1016 for (i = 0; (demuxer_desc = demuxer_list[i]); i++) {
1017 if (!demuxer_desc->safe_check && demuxer_desc->check_file) {
1018 demuxer = new_demuxer(stream, demuxer_desc->type, audio_id,
1019 video_id, dvdsub_id, filename);
1020 if ((fformat = demuxer_desc->check_file(demuxer)) != 0) {
1021 if (fformat == demuxer_desc->type) {
1022 demuxer_t *demux2 = demuxer;
1023 mp_msg(MSGT_DEMUXER, MSGL_INFO,
1024 MSGTR_Detected_XXX_FileFormat,
1025 demuxer_desc->shortdesc);
1026 file_format = fformat;
1027 if (!demuxer->desc->open
1028 || (demux2 = demuxer->desc->open(demuxer))) {
1029 demuxer = demux2;
1030 goto dmx_open;
1032 } else {
1033 if (fformat == DEMUXER_TYPE_PLAYLIST)
1034 return demuxer; // handled in mplayer.c
1035 // Format changed after check, recurse
1036 free_demuxer(demuxer);
1037 demuxer = demux_open_stream(stream, fformat, force,
1038 audio_id, video_id,
1039 dvdsub_id, filename);
1040 if (demuxer)
1041 return demuxer; // done!
1042 file_format = DEMUXER_TYPE_UNKNOWN;
1045 free_demuxer(demuxer);
1046 demuxer = NULL;
1050 return NULL;
1051 //====== File format recognized, set up these for compatibility: =========
1052 dmx_open:
1054 demuxer->file_format = file_format;
1056 if ((sh_video = demuxer->video->sh) && sh_video->bih) {
1057 int biComp = le2me_32(sh_video->bih->biCompression);
1058 mp_msg(MSGT_DEMUX, MSGL_INFO,
1059 "VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
1060 (char *) &biComp, sh_video->bih->biWidth,
1061 sh_video->bih->biHeight, sh_video->bih->biBitCount,
1062 sh_video->fps, sh_video->i_bps * 0.008f,
1063 sh_video->i_bps / 1024.0f);
1065 #ifdef CONFIG_ASS
1066 if (ass_enabled && ass_library) {
1067 for (i = 0; i < MAX_S_STREAMS; ++i) {
1068 sh_sub_t *sh = demuxer->s_streams[i];
1069 if (sh && sh->type == 'a') {
1070 sh->ass_track = ass_new_track(ass_library);
1071 if (sh->ass_track && sh->extradata)
1072 ass_process_codec_private(sh->ass_track, sh->extradata,
1073 sh->extradata_len);
1074 } else if (sh && sh->type != 'v')
1075 sh->ass_track = ass_default_track(ass_library);
1078 #endif
1079 return demuxer;
1082 char *audio_stream = NULL;
1083 char *sub_stream = NULL;
1084 int audio_stream_cache = 0;
1086 char *demuxer_name = NULL; // parameter from -demuxer
1087 char *audio_demuxer_name = NULL; // parameter from -audio-demuxer
1088 char *sub_demuxer_name = NULL; // parameter from -sub-demuxer
1090 extern int hr_mp3_seek;
1092 extern float stream_cache_min_percent;
1093 extern float stream_cache_seek_min_percent;
1095 demuxer_t *demux_open(stream_t *vs, int file_format, int audio_id,
1096 int video_id, int dvdsub_id, 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_msg(MSGT_DEMUXER, MSGL_ERR, MSGTR_CannotOpenAudioStream,
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_msg(MSGT_DEMUXER, MSGL_ERR, MSGTR_CannotOpenSubtitlesStream,
1149 sub_stream);
1150 return NULL;
1154 vd = demux_open_stream(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(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_msg(MSGT_DEMUXER, MSGL_WARN, MSGTR_OpeningAudioDemuxerFailed,
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(ss, sub_demuxer_type ? sub_demuxer_type : sfmt,
1179 sub_demuxer_force, -2, -2, dvdsub_id,
1180 sub_stream);
1181 if (!sd) {
1182 mp_msg(MSGT_DEMUXER, MSGL_WARN,
1183 MSGTR_OpeningSubtitlesDemuxerFailed, sub_stream);
1184 free_stream(ss);
1188 if (ad && sd)
1189 res = new_demuxers_demuxer(vd, ad, sd);
1190 else if (ad)
1191 res = new_demuxers_demuxer(vd, ad, vd);
1192 else if (sd)
1193 res = new_demuxers_demuxer(vd, vd, sd);
1194 else
1195 res = vd;
1197 correct_pts = user_correct_pts;
1198 if (correct_pts < 0)
1199 correct_pts = demux_control(res, DEMUXER_CTRL_CORRECT_PTS, NULL)
1200 == DEMUXER_CTRL_OK;
1201 return res;
1205 * Do necessary reinitialization after e.g. a seek.
1206 * Do _not_ call ds_fill_buffer between the seek and this, it breaks at least
1207 * seeking with ASF demuxer.
1209 static void demux_resync(demuxer_t *demuxer)
1211 sh_video_t *sh_video = demuxer->video->sh;
1212 sh_audio_t *sh_audio = demuxer->audio->sh;
1213 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1214 if (sh_video) {
1215 resync_video_stream(sh_video);
1217 if (sh_audio) {
1218 resync_audio_stream(sh_audio);
1222 void demux_flush(demuxer_t *demuxer)
1224 #if PARSE_ON_ADD
1225 ds_clear_parser(demuxer->video);
1226 ds_clear_parser(demuxer->audio);
1227 ds_clear_parser(demuxer->sub);
1228 #endif
1229 ds_free_packs(demuxer->video);
1230 ds_free_packs(demuxer->audio);
1231 ds_free_packs(demuxer->sub);
1234 int demux_seek(demuxer_t *demuxer, float rel_seek_secs, float audio_delay,
1235 int flags)
1237 double tmp = 0;
1238 double pts;
1240 if (!demuxer->seekable) {
1241 if (demuxer->file_format == DEMUXER_TYPE_AVI)
1242 mp_msg(MSGT_SEEK, MSGL_WARN, MSGTR_CantSeekRawAVI);
1243 #ifdef CONFIG_TV
1244 else if (demuxer->file_format == DEMUXER_TYPE_TV)
1245 mp_msg(MSGT_SEEK, MSGL_WARN, MSGTR_TVInputNotSeekable);
1246 #endif
1247 else
1248 mp_msg(MSGT_SEEK, MSGL_WARN, MSGTR_CantSeekFile);
1249 return 0;
1252 demux_flush(demuxer);
1254 demuxer->stream->eof = 0;
1255 demuxer->video->eof = 0;
1256 demuxer->audio->eof = 0;
1258 if (flags & SEEK_ABSOLUTE)
1259 pts = 0.0f;
1260 else {
1261 if (demuxer->stream_pts == MP_NOPTS_VALUE)
1262 goto dmx_seek;
1263 pts = demuxer->stream_pts;
1266 if (flags & SEEK_FACTOR) {
1267 if (stream_control(demuxer->stream, STREAM_CTRL_GET_TIME_LENGTH, &tmp)
1268 == STREAM_UNSUPPORTED)
1269 goto dmx_seek;
1270 pts += tmp * rel_seek_secs;
1271 } else
1272 pts += rel_seek_secs;
1274 if (stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_TIME, &pts) !=
1275 STREAM_UNSUPPORTED) {
1276 demux_resync(demuxer);
1277 return 1;
1280 dmx_seek:
1281 if (demuxer->desc->seek)
1282 demuxer->desc->seek(demuxer, rel_seek_secs, audio_delay, flags);
1284 demux_resync(demuxer);
1286 return 1;
1289 int demux_info_add(demuxer_t *demuxer, const char *opt, const char *param)
1291 char **info = demuxer->info;
1292 int n = 0;
1295 for (n = 0; info && info[2 * n] != NULL; n++) {
1296 if (!strcasecmp(opt, info[2 * n])) {
1297 if (!strcmp(param, info[2 * n + 1])) {
1298 mp_msg(MSGT_DEMUX, MSGL_V, "Demuxer info %s set to unchanged value %s\n", opt, param);
1299 return 0;
1301 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_DemuxerInfoChanged, opt,
1302 param);
1303 free(info[2 * n + 1]);
1304 info[2 * n + 1] = strdup(param);
1305 return 0;
1309 info = demuxer->info = realloc(info, (2 * (n + 2)) * sizeof(char *));
1310 info[2 * n] = strdup(opt);
1311 info[2 * n + 1] = strdup(param);
1312 memset(&info[2 * (n + 1)], 0, 2 * sizeof(char *));
1314 return 1;
1317 int demux_info_print(demuxer_t *demuxer)
1319 char **info = demuxer->info;
1320 int n;
1322 if (!info)
1323 return 0;
1325 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_ClipInfo);
1326 for (n = 0; info[2 * n] != NULL; n++) {
1327 mp_msg(MSGT_DEMUX, MSGL_INFO, " %s: %s\n", info[2 * n],
1328 info[2 * n + 1]);
1329 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_NAME%d=%s\n", n,
1330 info[2 * n]);
1331 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_VALUE%d=%s\n", n,
1332 info[2 * n + 1]);
1334 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_N=%d\n", n);
1336 return 0;
1339 char *demux_info_get(demuxer_t *demuxer, const char *opt)
1341 int i;
1342 char **info = demuxer->info;
1344 for (i = 0; info && info[2 * i] != NULL; i++) {
1345 if (!strcasecmp(opt, info[2 * i]))
1346 return info[2 * i + 1];
1349 return NULL;
1352 int demux_control(demuxer_t *demuxer, int cmd, void *arg)
1355 if (demuxer->desc->control)
1356 return demuxer->desc->control(demuxer, cmd, arg);
1358 return DEMUXER_CTRL_NOTIMPL;
1363 double demuxer_get_time_length(demuxer_t *demuxer)
1365 double get_time_ans;
1366 sh_video_t *sh_video = demuxer->video->sh;
1367 sh_audio_t *sh_audio = demuxer->audio->sh;
1368 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
1369 if (demux_control
1370 (demuxer, DEMUXER_CTRL_GET_TIME_LENGTH, (void *) &get_time_ans) <= 0) {
1371 if (sh_video && sh_video->i_bps && sh_audio && sh_audio->i_bps)
1372 get_time_ans = (double) (demuxer->movi_end -
1373 demuxer->movi_start) / (sh_video->i_bps +
1374 sh_audio->i_bps);
1375 else if (sh_video && sh_video->i_bps)
1376 get_time_ans = (double) (demuxer->movi_end -
1377 demuxer->movi_start) / sh_video->i_bps;
1378 else if (sh_audio && sh_audio->i_bps)
1379 get_time_ans = (double) (demuxer->movi_end -
1380 demuxer->movi_start) / sh_audio->i_bps;
1381 else
1382 get_time_ans = 0;
1384 return get_time_ans;
1388 * \brief demuxer_get_current_time() returns the time of the current play in three possible ways:
1389 * either when the stream reader satisfies STREAM_CTRL_GET_CURRENT_TIME (e.g. dvd)
1390 * or using sh_video->pts when the former method fails
1391 * 0 otherwise
1392 * \return the current play time
1394 int demuxer_get_current_time(demuxer_t *demuxer)
1396 double get_time_ans = 0;
1397 sh_video_t *sh_video = demuxer->video->sh;
1398 if (demuxer->stream_pts != MP_NOPTS_VALUE)
1399 get_time_ans = demuxer->stream_pts;
1400 else if (sh_video)
1401 get_time_ans = sh_video->pts;
1402 return (int) get_time_ans;
1405 int demuxer_get_percent_pos(demuxer_t *demuxer)
1407 int ans = 0;
1408 int res = demux_control(demuxer, DEMUXER_CTRL_GET_PERCENT_POS, &ans);
1409 int len = (demuxer->movi_end - demuxer->movi_start) / 100;
1410 if (res <= 0) {
1411 off_t pos = demuxer->filepos > 0 ? demuxer->filepos : stream_tell(demuxer->stream);
1412 if (len > 0)
1413 ans = (pos - demuxer->movi_start) / len;
1414 else
1415 ans = 0;
1417 if (ans < 0)
1418 ans = 0;
1419 if (ans > 100)
1420 ans = 100;
1421 return ans;
1424 int demuxer_switch_audio(demuxer_t *demuxer, int index)
1426 int res = demux_control(demuxer, DEMUXER_CTRL_SWITCH_AUDIO, &index);
1427 if (res == DEMUXER_CTRL_NOTIMPL)
1428 index = demuxer->audio->id;
1429 if (demuxer->audio->id >= 0)
1430 demuxer->audio->sh = demuxer->a_streams[demuxer->audio->id];
1431 else
1432 demuxer->audio->sh = NULL;
1433 return index;
1436 int demuxer_switch_video(demuxer_t *demuxer, int index)
1438 int res = demux_control(demuxer, DEMUXER_CTRL_SWITCH_VIDEO, &index);
1439 if (res == DEMUXER_CTRL_NOTIMPL)
1440 index = demuxer->video->id;
1441 if (demuxer->video->id >= 0)
1442 demuxer->video->sh = demuxer->v_streams[demuxer->video->id];
1443 else
1444 demuxer->video->sh = NULL;
1445 return index;
1448 int demuxer_add_attachment(demuxer_t *demuxer, const char *name,
1449 const char *type, const void *data, size_t size)
1451 if (!(demuxer->num_attachments & 31))
1452 demuxer->attachments = realloc(demuxer->attachments,
1453 (demuxer->num_attachments + 32) * sizeof(demux_attachment_t));
1455 demuxer->attachments[demuxer->num_attachments].name = strdup(name);
1456 demuxer->attachments[demuxer->num_attachments].type = strdup(type);
1457 demuxer->attachments[demuxer->num_attachments].data = malloc(size);
1458 memcpy(demuxer->attachments[demuxer->num_attachments].data, data, size);
1459 demuxer->attachments[demuxer->num_attachments].data_size = size;
1461 return demuxer->num_attachments++;
1464 int demuxer_add_chapter(demuxer_t *demuxer, const char *name, uint64_t start,
1465 uint64_t end)
1467 if (demuxer->chapters == NULL)
1468 demuxer->chapters = malloc(32 * sizeof(*demuxer->chapters));
1469 else if (!(demuxer->num_chapters % 32))
1470 demuxer->chapters = realloc(demuxer->chapters,
1471 (demuxer->num_chapters + 32) *
1472 sizeof(*demuxer->chapters));
1474 demuxer->chapters[demuxer->num_chapters].start = start;
1475 demuxer->chapters[demuxer->num_chapters].end = end;
1476 demuxer->chapters[demuxer->num_chapters].name = strdup(name ? name : MSGTR_Unknown);
1478 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_ID=%d\n", demuxer->num_chapters);
1479 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_START=%"PRIu64"\n", demuxer->num_chapters, start);
1480 if (end)
1481 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_END=%"PRIu64"\n", demuxer->num_chapters, end);
1482 if (name)
1483 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_NAME=%s\n", demuxer->num_chapters, name);
1485 return demuxer->num_chapters++;
1489 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1490 * either using the demuxer->chapters structure set by the demuxer
1491 * or asking help to the stream layer (e.g. dvd)
1492 * \param chapter - chapter number wished - 0-based
1493 * \param mode 0: relative to current main pts, 1: absolute
1494 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1495 * \param num_chapters number of chapters present (set by this function is param is not null)
1496 * \param chapter_name name of chapter found (set by this function is param is not null)
1497 * \return -1 on error, current chapter if successful
1500 int demuxer_seek_chapter(demuxer_t *demuxer, int chapter, int mode,
1501 float *seek_pts, int *num_chapters,
1502 char **chapter_name)
1504 int ris;
1505 int current, total;
1507 if (!demuxer->num_chapters || !demuxer->chapters) {
1508 if (!mode) {
1509 ris = stream_control(demuxer->stream,
1510 STREAM_CTRL_GET_CURRENT_CHAPTER, &current);
1511 if (ris == STREAM_UNSUPPORTED)
1512 return -1;
1513 chapter += current;
1516 demux_flush(demuxer);
1518 ris = stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_CHAPTER,
1519 &chapter);
1521 demux_resync(demuxer);
1523 // exit status may be ok, but main() doesn't have to seek itself
1524 // (because e.g. dvds depend on sectors, not on pts)
1525 *seek_pts = -1.0;
1527 if (num_chapters) {
1528 if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS,
1529 num_chapters) == STREAM_UNSUPPORTED)
1530 *num_chapters = 0;
1533 if (chapter_name) {
1534 *chapter_name = NULL;
1535 if (num_chapters && *num_chapters) {
1536 char *tmp = malloc(16);
1537 if (tmp) {
1538 sprintf(tmp, " of %3d", *num_chapters);
1539 *chapter_name = tmp;
1544 return ris != STREAM_UNSUPPORTED ? chapter : -1;
1545 } else { // chapters structure is set in the demuxer
1546 sh_video_t *sh_video = demuxer->video->sh;
1547 sh_audio_t *sh_audio = demuxer->audio->sh;
1549 total = demuxer->num_chapters;
1551 if (mode == 1) //absolute seeking
1552 current = chapter;
1553 else { //relative seeking
1554 uint64_t now;
1555 now = (sh_video ? sh_video->pts : (sh_audio ? sh_audio->pts : 0.))
1556 * 1000 + .5;
1558 for (current = total - 1; current >= 0; --current) {
1559 demux_chapter_t *chapter = demuxer->chapters + current;
1560 if (chapter->start <= now)
1561 break;
1563 current += chapter;
1566 if (current >= total)
1567 return -1;
1568 if (current < 0)
1569 current = 0;
1571 *seek_pts = demuxer->chapters[current].start / 1000.0;
1573 if (num_chapters)
1574 *num_chapters = demuxer->num_chapters;
1576 if (chapter_name) {
1577 if (demuxer->chapters[current].name)
1578 *chapter_name = strdup(demuxer->chapters[current].name);
1579 else
1580 *chapter_name = NULL;
1583 return current;
1587 int demuxer_get_current_chapter(demuxer_t *demuxer)
1589 int chapter = -1;
1590 if (!demuxer->num_chapters || !demuxer->chapters) {
1591 if (stream_control(demuxer->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
1592 &chapter) == STREAM_UNSUPPORTED)
1593 chapter = -1;
1594 } else {
1595 sh_video_t *sh_video = demuxer->video->sh;
1596 sh_audio_t *sh_audio = demuxer->audio->sh;
1597 uint64_t now;
1598 now = (sh_video ? sh_video->pts : (sh_audio ? sh_audio->pts : 0))
1599 * 1000 + 0.5;
1600 for (chapter = demuxer->num_chapters - 1; chapter >= 0; --chapter) {
1601 if (demuxer->chapters[chapter].start <= now)
1602 break;
1605 return chapter;
1608 char *demuxer_chapter_name(demuxer_t *demuxer, int chapter)
1610 if (demuxer->num_chapters && demuxer->chapters) {
1611 if (chapter >= 0 && chapter < demuxer->num_chapters
1612 && demuxer->chapters[chapter].name)
1613 return strdup(demuxer->chapters[chapter].name);
1615 return NULL;
1618 char *demuxer_chapter_display_name(demuxer_t *demuxer, int chapter)
1620 char *chapter_name = demuxer_chapter_name(demuxer, chapter);
1621 if (chapter_name) {
1622 char *tmp = malloc(strlen(chapter_name) + 14);
1623 snprintf(tmp, 63, "(%d) %s", chapter + 1, chapter_name);
1624 free(chapter_name);
1625 return tmp;
1626 } else {
1627 int chapter_num = demuxer_chapter_count(demuxer);
1628 char tmp[30];
1629 if (chapter_num <= 0)
1630 sprintf(tmp, "(%d)", chapter + 1);
1631 else
1632 sprintf(tmp, "(%d) of %d", chapter + 1, chapter_num);
1633 return strdup(tmp);
1637 float demuxer_chapter_time(demuxer_t *demuxer, int chapter, float *end)
1639 if (demuxer->num_chapters && demuxer->chapters && chapter >= 0
1640 && chapter < demuxer->num_chapters) {
1641 if (end)
1642 *end = demuxer->chapters[chapter].end / 1000.0;
1643 return demuxer->chapters[chapter].start / 1000.0;
1645 return -1.0;
1648 int demuxer_chapter_count(demuxer_t *demuxer)
1650 if (!demuxer->num_chapters || !demuxer->chapters) {
1651 int num_chapters = 0;
1652 if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS,
1653 &num_chapters) == STREAM_UNSUPPORTED)
1654 num_chapters = 0;
1655 return num_chapters;
1656 } else
1657 return demuxer->num_chapters;
1660 int demuxer_angles_count(demuxer_t *demuxer)
1662 int ris, angles = -1;
1664 ris = stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_ANGLES, &angles);
1665 if (ris == STREAM_UNSUPPORTED)
1666 return -1;
1667 return angles;
1670 int demuxer_get_current_angle(demuxer_t *demuxer)
1672 int ris, curr_angle = -1;
1673 ris = stream_control(demuxer->stream, STREAM_CTRL_GET_ANGLE, &curr_angle);
1674 if (ris == STREAM_UNSUPPORTED)
1675 return -1;
1676 return curr_angle;
1680 int demuxer_set_angle(demuxer_t *demuxer, int angle)
1682 int ris, angles = -1;
1684 angles = demuxer_angles_count(demuxer);
1685 if ((angles < 1) || (angle > angles))
1686 return -1;
1688 demux_flush(demuxer);
1690 ris = stream_control(demuxer->stream, STREAM_CTRL_SET_ANGLE, &angle);
1691 if (ris == STREAM_UNSUPPORTED)
1692 return -1;
1694 demux_resync(demuxer);
1696 return angle;
1699 int demuxer_audio_track_by_lang(demuxer_t *d, char *lang)
1701 int i, len;
1702 lang += strspn(lang, ",");
1703 while ((len = strcspn(lang, ",")) > 0) {
1704 for (i = 0; i < MAX_A_STREAMS; ++i) {
1705 sh_audio_t *sh = d->a_streams[i];
1706 if (sh && sh->lang && strncmp(sh->lang, lang, len) == 0)
1707 return sh->aid;
1709 lang += len;
1710 lang += strspn(lang, ",");
1712 return -1;
1715 int demuxer_sub_track_by_lang(demuxer_t *d, char *lang)
1717 int i, len;
1718 lang += strspn(lang, ",");
1719 while ((len = strcspn(lang, ",")) > 0) {
1720 for (i = 0; i < MAX_S_STREAMS; ++i) {
1721 sh_sub_t *sh = d->s_streams[i];
1722 if (sh && sh->lang && strncmp(sh->lang, lang, len) == 0)
1723 return sh->sid;
1725 lang += len;
1726 lang += strspn(lang, ",");
1728 return -1;
1731 int demuxer_default_audio_track(demuxer_t *d)
1733 int i;
1734 for (i = 0; i < MAX_A_STREAMS; ++i) {
1735 sh_audio_t *sh = d->a_streams[i];
1736 if (sh && sh->default_track)
1737 return sh->aid;
1739 for (i = 0; i < MAX_A_STREAMS; ++i) {
1740 sh_audio_t *sh = d->a_streams[i];
1741 if (sh)
1742 return sh->aid;
1744 return -1;
1747 int demuxer_default_sub_track(demuxer_t *d)
1749 int i;
1750 for (i = 0; i < MAX_S_STREAMS; ++i) {
1751 sh_sub_t *sh = d->s_streams[i];
1752 if (sh && sh->default_track)
1753 return sh->sid;
1755 return -1;