Merge svn changes up to r29412
[mplayer/glamo.git] / libmpdemux / demuxer.c
blobc9142f1441e60b0a2a5dc29fcb86febae345d232
1 /*
2 * DEMUXER v2.5
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
29 #include "config.h"
30 #include "options.h"
31 #include "talloc.h"
32 #include "mp_msg.h"
33 #include "help_mp.h"
34 #include "m_config.h"
36 #include "libvo/fastmemcpy.h"
38 #include "stream/stream.h"
39 #include "demuxer.h"
40 #include "stheader.h"
41 #include "mf.h"
43 #include "libaf/af_format.h"
45 #ifdef CONFIG_ASS
46 #include "libass/ass.h"
47 #include "libass/ass_mp.h"
48 #endif
50 #ifdef CONFIG_LIBAVCODEC
51 #include "libavcodec/avcodec.h"
52 #if MP_INPUT_BUFFER_PADDING_SIZE < FF_INPUT_BUFFER_PADDING_SIZE
53 #error MP_INPUT_BUFFER_PADDING_SIZE is too small!
54 #endif
55 #endif
57 void resync_video_stream(sh_video_t *sh_video);
58 void resync_audio_stream(sh_audio_t *sh_audio);
60 // Demuxer list
61 extern const demuxer_desc_t demuxer_desc_rawaudio;
62 extern const demuxer_desc_t demuxer_desc_rawvideo;
63 extern const demuxer_desc_t demuxer_desc_tv;
64 extern const demuxer_desc_t demuxer_desc_mf;
65 extern const demuxer_desc_t demuxer_desc_avi;
66 extern const demuxer_desc_t demuxer_desc_y4m;
67 extern const demuxer_desc_t demuxer_desc_asf;
68 extern const demuxer_desc_t demuxer_desc_real;
69 extern const demuxer_desc_t demuxer_desc_smjpeg;
70 extern const demuxer_desc_t demuxer_desc_matroska;
71 extern const demuxer_desc_t demuxer_desc_realaudio;
72 extern const demuxer_desc_t demuxer_desc_vqf;
73 extern const demuxer_desc_t demuxer_desc_mov;
74 extern const demuxer_desc_t demuxer_desc_vivo;
75 extern const demuxer_desc_t demuxer_desc_fli;
76 extern const demuxer_desc_t demuxer_desc_film;
77 extern const demuxer_desc_t demuxer_desc_roq;
78 extern const demuxer_desc_t demuxer_desc_gif;
79 extern const demuxer_desc_t demuxer_desc_ogg;
80 extern const demuxer_desc_t demuxer_desc_avs;
81 extern const demuxer_desc_t demuxer_desc_pva;
82 extern const demuxer_desc_t demuxer_desc_nsv;
83 extern const demuxer_desc_t demuxer_desc_mpeg_ts;
84 extern const demuxer_desc_t demuxer_desc_lmlm4;
85 extern const demuxer_desc_t demuxer_desc_mpeg_ps;
86 extern const demuxer_desc_t demuxer_desc_mpeg_pes;
87 extern const demuxer_desc_t demuxer_desc_mpeg_es;
88 extern const demuxer_desc_t demuxer_desc_mpeg_gxf;
89 extern const demuxer_desc_t demuxer_desc_mpeg4_es;
90 extern const demuxer_desc_t demuxer_desc_h264_es;
91 extern const demuxer_desc_t demuxer_desc_rawdv;
92 extern const demuxer_desc_t demuxer_desc_mpc;
93 extern const demuxer_desc_t demuxer_desc_audio;
94 extern const demuxer_desc_t demuxer_desc_xmms;
95 extern const demuxer_desc_t demuxer_desc_mpeg_ty;
96 extern const demuxer_desc_t demuxer_desc_rtp;
97 extern const demuxer_desc_t demuxer_desc_rtp_nemesi;
98 extern const demuxer_desc_t demuxer_desc_lavf;
99 extern const demuxer_desc_t demuxer_desc_lavf_preferred;
100 extern const demuxer_desc_t demuxer_desc_aac;
101 extern const demuxer_desc_t demuxer_desc_nut;
102 extern const demuxer_desc_t demuxer_desc_mng;
104 /* Please do not add any new demuxers here. If you want to implement a new
105 * demuxer, add it to libavformat, except for wrappers around external
106 * libraries and demuxers requiring binary support. */
108 const demuxer_desc_t *const demuxer_list[] = {
109 &demuxer_desc_rawaudio,
110 &demuxer_desc_rawvideo,
111 #ifdef CONFIG_TV
112 &demuxer_desc_tv,
113 #endif
114 &demuxer_desc_mf,
115 #ifdef CONFIG_LIBAVFORMAT
116 &demuxer_desc_lavf_preferred,
117 #endif
118 &demuxer_desc_avi,
119 &demuxer_desc_y4m,
120 &demuxer_desc_asf,
121 &demuxer_desc_nsv,
122 &demuxer_desc_real,
123 &demuxer_desc_smjpeg,
124 &demuxer_desc_matroska,
125 &demuxer_desc_realaudio,
126 &demuxer_desc_vqf,
127 &demuxer_desc_mov,
128 &demuxer_desc_vivo,
129 &demuxer_desc_fli,
130 &demuxer_desc_film,
131 &demuxer_desc_roq,
132 #ifdef CONFIG_GIF
133 &demuxer_desc_gif,
134 #endif
135 #ifdef CONFIG_OGGVORBIS
136 &demuxer_desc_ogg,
137 #endif
138 #ifdef CONFIG_WIN32DLL
139 &demuxer_desc_avs,
140 #endif
141 &demuxer_desc_pva,
142 &demuxer_desc_mpeg_ts,
143 &demuxer_desc_lmlm4,
144 &demuxer_desc_mpeg_ps,
145 &demuxer_desc_mpeg_pes,
146 &demuxer_desc_mpeg_es,
147 &demuxer_desc_mpeg_gxf,
148 &demuxer_desc_mpeg4_es,
149 &demuxer_desc_h264_es,
150 &demuxer_desc_audio,
151 &demuxer_desc_mpeg_ty,
152 #ifdef CONFIG_LIVE555
153 &demuxer_desc_rtp,
154 #endif
155 #ifdef CONFIG_LIBNEMESI
156 &demuxer_desc_rtp_nemesi,
157 #endif
158 #ifdef CONFIG_LIBAVFORMAT
159 &demuxer_desc_lavf,
160 #endif
161 #ifdef CONFIG_MUSEPACK
162 &demuxer_desc_mpc,
163 #endif
164 #ifdef CONFIG_LIBDV095
165 &demuxer_desc_rawdv,
166 #endif
167 &demuxer_desc_aac,
168 #ifdef CONFIG_LIBNUT
169 &demuxer_desc_nut,
170 #endif
171 #ifdef CONFIG_XMMS
172 &demuxer_desc_xmms,
173 #endif
174 #ifdef CONFIG_MNG
175 &demuxer_desc_mng,
176 #endif
177 /* Please do not add any new demuxers here. If you want to implement a new
178 * demuxer, add it to libavformat, except for wrappers around external
179 * libraries and demuxers requiring binary support. */
180 NULL
183 void free_demuxer_stream(demux_stream_t *ds)
185 ds_free_packs(ds);
186 free(ds);
189 demux_stream_t *new_demuxer_stream(struct demuxer *demuxer, int id)
191 demux_stream_t *ds = malloc(sizeof(demux_stream_t));
192 ds->buffer_pos = ds->buffer_size = 0;
193 ds->buffer = NULL;
194 ds->pts = 0;
195 ds->pts_bytes = 0;
196 ds->eof = 0;
197 ds->pos = 0;
198 ds->dpos = 0;
199 ds->pack_no = 0;
201 ds->packs = 0;
202 ds->bytes = 0;
203 ds->first = ds->last = ds->current = NULL;
204 ds->id = id;
205 ds->demuxer = demuxer;
207 ds->asf_seq = -1;
208 ds->asf_packet = NULL;
210 ds->ss_mul = ds->ss_div = 0;
212 ds->sh = NULL;
213 return ds;
218 * Get demuxer description structure for a given demuxer type
220 * @param file_format type of the demuxer
221 * @return structure for the demuxer, NULL if not found
223 static const demuxer_desc_t *get_demuxer_desc_from_type(int file_format)
225 int i;
227 for (i = 0; demuxer_list[i]; i++)
228 if (file_format == demuxer_list[i]->type)
229 return demuxer_list[i];
231 return NULL;
235 demuxer_t *new_demuxer(struct MPOpts *opts, stream_t *stream, int type,
236 int a_id, int v_id, int s_id, char *filename)
238 struct demuxer *d = talloc_zero(NULL, struct demuxer);
239 d->stream = stream;
240 d->stream_pts = MP_NOPTS_VALUE;
241 d->reference_clock = MP_NOPTS_VALUE;
242 d->movi_start = stream->start_pos;
243 d->movi_end = stream->end_pos;
244 d->seekable = 1;
245 d->synced = 0;
246 d->filepos = 0;
247 d->audio = new_demuxer_stream(d, a_id);
248 d->video = new_demuxer_stream(d, v_id);
249 d->sub = new_demuxer_stream(d, s_id);
250 d->type = type;
251 d->opts = opts;
252 if (type)
253 if (!(d->desc = get_demuxer_desc_from_type(type)))
254 mp_msg(MSGT_DEMUXER, MSGL_ERR,
255 "BUG! Invalid demuxer type in new_demuxer(), "
256 "big troubles ahead.");
257 if (filename) // Filename hack for avs_check_file
258 d->filename = strdup(filename);
259 stream_reset(stream);
260 stream_seek(stream, stream->start_pos);
261 return d;
264 sh_sub_t *new_sh_sub_sid(demuxer_t *demuxer, int id, int sid)
266 if (id > MAX_S_STREAMS - 1 || id < 0) {
267 mp_msg(MSGT_DEMUXER, MSGL_WARN,
268 "Requested sub stream id overflow (%d > %d)\n", id,
269 MAX_S_STREAMS);
270 return NULL;
272 if (demuxer->s_streams[id])
273 mp_msg(MSGT_DEMUXER, MSGL_WARN, "Sub stream %i redefined\n", id);
274 else {
275 sh_sub_t *sh = calloc(1, sizeof(sh_sub_t));
276 demuxer->s_streams[id] = sh;
277 sh->sid = sid;
278 sh->opts = demuxer->opts;
279 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SUBTITLE_ID=%d\n", sid);
281 if (sid == demuxer->opts->sub_id) {
282 demuxer->sub->id = id;
283 demuxer->sub->sh = demuxer->s_streams[id];
285 return demuxer->s_streams[id];
288 static void free_sh_sub(sh_sub_t *sh)
290 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_sub at %p\n", sh);
291 free(sh->extradata);
292 #ifdef CONFIG_ASS
293 if (sh->ass_track)
294 ass_free_track(sh->ass_track);
295 #endif
296 free(sh->lang);
297 free(sh);
300 sh_audio_t *new_sh_audio_aid(demuxer_t *demuxer, int id, int aid)
302 if (id > MAX_A_STREAMS - 1 || id < 0) {
303 mp_msg(MSGT_DEMUXER, MSGL_WARN,
304 "Requested audio stream id overflow (%d > %d)\n", id,
305 MAX_A_STREAMS);
306 return NULL;
308 if (demuxer->a_streams[id]) {
309 mp_tmsg(MSGT_DEMUXER, MSGL_WARN, "WARNING: Audio stream header %d redefined.\n", id);
310 } else {
311 mp_tmsg(MSGT_DEMUXER, MSGL_V, "==> Found audio stream: %d\n", id);
312 sh_audio_t *sh = calloc(1, sizeof(sh_audio_t));
313 demuxer->a_streams[id] = sh;
314 sh->aid = aid;
315 sh->ds = demuxer->audio;
316 // set some defaults
317 sh->samplesize = 2;
318 sh->sample_format = AF_FORMAT_S16_NE;
319 sh->audio_out_minsize = 8192; /* default size, maybe not enough for Win32/ACM */
320 sh->pts = MP_NOPTS_VALUE;
321 sh->opts = demuxer->opts;
322 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_ID=%d\n", aid);
324 return demuxer->a_streams[id];
327 void free_sh_audio(demuxer_t *demuxer, int id)
329 sh_audio_t *sh = demuxer->a_streams[id];
330 demuxer->a_streams[id] = NULL;
331 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_audio at %p\n", sh);
332 free(sh->wf);
333 free(sh->codecdata);
334 free(sh->lang);
335 free(sh);
338 sh_video_t *new_sh_video_vid(demuxer_t *demuxer, int id, int vid)
340 if (id > MAX_V_STREAMS - 1 || id < 0) {
341 mp_msg(MSGT_DEMUXER, MSGL_WARN,
342 "Requested video stream id overflow (%d > %d)\n", id,
343 MAX_V_STREAMS);
344 return NULL;
346 if (demuxer->v_streams[id])
347 mp_tmsg(MSGT_DEMUXER, MSGL_WARN, "WARNING: Video stream header %d redefined.\n", id);
348 else {
349 mp_tmsg(MSGT_DEMUXER, MSGL_V, "==> Found video stream: %d\n", id);
350 sh_video_t *sh = calloc(1, sizeof *sh);
351 demuxer->v_streams[id] = sh;
352 sh->vid = vid;
353 sh->ds = demuxer->video;
354 sh->opts = demuxer->opts;
355 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_ID=%d\n", vid);
357 return demuxer->v_streams[id];
360 void free_sh_video(sh_video_t *sh)
362 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_video at %p\n", sh);
363 free(sh->bih);
364 free(sh);
367 void free_demuxer(demuxer_t *demuxer)
369 int i;
370 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing demuxer at %p\n",
371 demuxer);
372 if (demuxer->desc->close)
373 demuxer->desc->close(demuxer);
374 // Very ugly hack to make it behave like old implementation
375 if (demuxer->desc->type == DEMUXER_TYPE_DEMUXERS)
376 goto skip_streamfree;
377 // free streams:
378 for (i = 0; i < MAX_A_STREAMS; i++)
379 if (demuxer->a_streams[i])
380 free_sh_audio(demuxer, i);
381 for (i = 0; i < MAX_V_STREAMS; i++)
382 if (demuxer->v_streams[i])
383 free_sh_video(demuxer->v_streams[i]);
384 for (i = 0; i < MAX_S_STREAMS; i++)
385 if (demuxer->s_streams[i])
386 free_sh_sub(demuxer->s_streams[i]);
387 // free demuxers:
388 free_demuxer_stream(demuxer->audio);
389 free_demuxer_stream(demuxer->video);
390 free_demuxer_stream(demuxer->sub);
391 skip_streamfree:
392 if (demuxer->info) {
393 for (i = 0; demuxer->info[i] != NULL; i++)
394 free(demuxer->info[i]);
395 free(demuxer->info);
397 free(demuxer->filename);
398 if (demuxer->chapters) {
399 for (i = 0; i < demuxer->num_chapters; i++)
400 free(demuxer->chapters[i].name);
401 free(demuxer->chapters);
403 if (demuxer->attachments) {
404 for (i = 0; i < demuxer->num_attachments; i++) {
405 free(demuxer->attachments[i].name);
406 free(demuxer->attachments[i].type);
407 free(demuxer->attachments[i].data);
409 free(demuxer->attachments);
411 talloc_free(demuxer);
415 void ds_add_packet(demux_stream_t *ds, demux_packet_t *dp)
417 // append packet to DS stream:
418 ++ds->packs;
419 ds->bytes += dp->len;
420 if (ds->last) {
421 // next packet in stream
422 ds->last->next = dp;
423 ds->last = dp;
424 } else {
425 // first packet in stream
426 ds->first = ds->last = dp;
428 mp_dbg(MSGT_DEMUXER, MSGL_DBG2,
429 "DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
430 (ds == ds->demuxer->audio) ? "d_audio" : "d_video", dp->len,
431 dp->pts, (unsigned int) dp->pos, ds->demuxer->audio->packs,
432 ds->demuxer->video->packs);
435 void ds_read_packet(demux_stream_t *ds, stream_t *stream, int len,
436 double pts, off_t pos, int flags)
438 demux_packet_t *dp = new_demux_packet(len);
439 len = stream_read(stream, dp->buffer, len);
440 resize_demux_packet(dp, len);
441 dp->pts = pts;
442 dp->pos = pos;
443 dp->flags = flags;
444 // append packet to DS stream:
445 ds_add_packet(ds, dp);
448 // return value:
449 // 0 = EOF or no stream found or invalid type
450 // 1 = successfully read a packet
452 int demux_fill_buffer(demuxer_t *demux, demux_stream_t *ds)
454 // Note: parameter 'ds' can be NULL!
455 return demux->desc->fill_buffer(demux, ds);
458 // return value:
459 // 0 = EOF
460 // 1 = successful
461 #define MAX_ACUMULATED_PACKETS 64
462 int ds_fill_buffer(demux_stream_t *ds)
464 demuxer_t *demux = ds->demuxer;
465 if (ds->current)
466 free_demux_packet(ds->current);
467 ds->current = NULL;
468 if (mp_msg_test(MSGT_DEMUXER, MSGL_DBG3)) {
469 if (ds == demux->audio)
470 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
471 "ds_fill_buffer(d_audio) called\n");
472 else if (ds == demux->video)
473 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
474 "ds_fill_buffer(d_video) called\n");
475 else if (ds == demux->sub)
476 mp_dbg(MSGT_DEMUXER, MSGL_DBG3, "ds_fill_buffer(d_sub) called\n");
477 else
478 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
479 "ds_fill_buffer(unknown 0x%X) called\n", (unsigned int) ds);
481 while (1) {
482 if (ds->packs) {
483 demux_packet_t *p = ds->first;
484 #if 0
485 if (demux->reference_clock != MP_NOPTS_VALUE) {
486 if ( p->pts != MP_NOPTS_VALUE
487 && p->pts > demux->reference_clock
488 && ds->packs < MAX_ACUMULATED_PACKETS) {
489 if (demux_fill_buffer(demux, ds))
490 continue;
493 #endif
494 // copy useful data:
495 ds->buffer = p->buffer;
496 ds->buffer_pos = 0;
497 ds->buffer_size = p->len;
498 ds->pos = p->pos;
499 ds->dpos += p->len; // !!!
500 ++ds->pack_no;
501 if (p->pts != MP_NOPTS_VALUE) {
502 ds->pts = p->pts;
503 ds->pts_bytes = 0;
505 ds->pts_bytes += p->len; // !!!
506 if (p->stream_pts != MP_NOPTS_VALUE)
507 demux->stream_pts = p->stream_pts;
508 ds->flags = p->flags;
509 // unlink packet:
510 ds->bytes -= p->len;
511 ds->current = p;
512 ds->first = p->next;
513 if (!ds->first)
514 ds->last = NULL;
515 --ds->packs;
516 /* The code below can set ds->eof to 1 when another stream runs
517 * out of buffer space. That makes sense because in that situation
518 * the calling code should not count on being able to demux more
519 * packets from this stream.
520 * If however the situation improves and we're called again
521 * despite the eof flag then it's better to clear it to avoid
522 * weird behavior. */
523 ds->eof = 0;
524 return 1;
527 #define MaybeNI _("Maybe you are playing a non-interleaved stream/file or the codec failed?\n" \
528 "For AVI files, try to force non-interleaved mode with the -ni option.\n")
530 if (demux->audio->packs >= MAX_PACKS
531 || demux->audio->bytes >= MAX_PACK_BYTES) {
532 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
533 demux->audio->packs, demux->audio->bytes);
534 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
535 break;
537 if (demux->video->packs >= MAX_PACKS
538 || demux->video->bytes >= MAX_PACK_BYTES) {
539 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
540 demux->video->packs, demux->video->bytes);
541 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
542 break;
544 if (!demux_fill_buffer(demux, ds)) {
545 mp_dbg(MSGT_DEMUXER, MSGL_DBG2,
546 "ds_fill_buffer()->demux_fill_buffer() failed\n");
547 break; // EOF
550 ds->buffer_pos = ds->buffer_size = 0;
551 ds->buffer = NULL;
552 mp_msg(MSGT_DEMUXER, MSGL_V,
553 "ds_fill_buffer: EOF reached (stream: %s) \n",
554 ds == demux->audio ? "audio" : "video");
555 ds->eof = 1;
556 return 0;
559 int demux_read_data(demux_stream_t *ds, unsigned char *mem, int len)
561 int x;
562 int bytes = 0;
563 while (len > 0) {
564 x = ds->buffer_size - ds->buffer_pos;
565 if (x == 0) {
566 if (!ds_fill_buffer(ds))
567 return bytes;
568 } else {
569 if (x > len)
570 x = len;
571 if (mem)
572 fast_memcpy(mem + bytes, &ds->buffer[ds->buffer_pos], x);
573 bytes += x;
574 len -= x;
575 ds->buffer_pos += x;
578 return bytes;
582 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
583 * \param mem memory to read data into, may be NULL to discard data
584 * \param maxlen maximum number of bytes to read
585 * \param read number of bytes actually read
586 * \param pattern pattern to search for (lowest 8 bits are ignored)
587 * \return whether pattern was found
589 int demux_pattern_3(demux_stream_t *ds, unsigned char *mem, int maxlen,
590 int *read, uint32_t pattern)
592 register uint32_t head = 0xffffff00;
593 register uint32_t pat = pattern & 0xffffff00;
594 int total_len = 0;
595 do {
596 register unsigned char *ds_buf = &ds->buffer[ds->buffer_size];
597 int len = ds->buffer_size - ds->buffer_pos;
598 register long pos = -len;
599 if (unlikely(pos >= 0)) { // buffer is empty
600 ds_fill_buffer(ds);
601 continue;
603 do {
604 head |= ds_buf[pos];
605 head <<= 8;
606 } while (++pos && head != pat);
607 len += pos;
608 if (total_len + len > maxlen)
609 len = maxlen - total_len;
610 len = demux_read_data(ds, mem ? &mem[total_len] : NULL, len);
611 total_len += len;
612 } while ((head != pat || total_len < 3) && total_len < maxlen && !ds->eof);
613 if (read)
614 *read = total_len;
615 return total_len >= 3 && head == pat;
618 void ds_free_packs(demux_stream_t *ds)
620 demux_packet_t *dp = ds->first;
621 while (dp) {
622 demux_packet_t *dn = dp->next;
623 free_demux_packet(dp);
624 dp = dn;
626 if (ds->asf_packet) {
627 // free unfinished .asf fragments:
628 free(ds->asf_packet->buffer);
629 free(ds->asf_packet);
630 ds->asf_packet = NULL;
632 ds->first = ds->last = NULL;
633 ds->packs = 0; // !!!!!
634 ds->bytes = 0;
635 if (ds->current)
636 free_demux_packet(ds->current);
637 ds->current = NULL;
638 ds->buffer = NULL;
639 ds->buffer_pos = ds->buffer_size;
640 ds->pts = 0;
641 ds->pts_bytes = 0;
644 int ds_get_packet(demux_stream_t *ds, unsigned char **start)
646 int len;
647 if (ds->buffer_pos >= ds->buffer_size) {
648 if (!ds_fill_buffer(ds)) {
649 // EOF
650 *start = NULL;
651 return -1;
654 len = ds->buffer_size - ds->buffer_pos;
655 *start = &ds->buffer[ds->buffer_pos];
656 ds->buffer_pos += len;
657 return len;
660 int ds_get_packet_pts(demux_stream_t *ds, unsigned char **start, double *pts)
662 int len;
663 *pts = MP_NOPTS_VALUE;
664 if (ds->buffer_pos >= ds->buffer_size) {
665 if (!ds_fill_buffer(ds)) {
666 // EOF
667 *start = NULL;
668 return -1;
671 // Return pts unless this read starts from the middle of a packet
672 if (!ds->buffer_pos)
673 *pts = ds->current->pts;
674 len = ds->buffer_size - ds->buffer_pos;
675 *start = &ds->buffer[ds->buffer_pos];
676 ds->buffer_pos += len;
677 return len;
680 int ds_get_packet_sub(demux_stream_t *ds, unsigned char **start)
682 int len;
683 if (ds->buffer_pos >= ds->buffer_size) {
684 *start = NULL;
685 if (!ds->packs)
686 return -1; // no sub
687 if (!ds_fill_buffer(ds))
688 return -1; // EOF
690 len = ds->buffer_size - ds->buffer_pos;
691 *start = &ds->buffer[ds->buffer_pos];
692 ds->buffer_pos += len;
693 return len;
696 double ds_get_next_pts(demux_stream_t *ds)
698 demuxer_t *demux = ds->demuxer;
699 while (!ds->first) {
700 if (demux->audio->packs >= MAX_PACKS
701 || demux->audio->bytes >= MAX_PACK_BYTES) {
702 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many audio packets in the buffer: (%d in %d bytes).\n",
703 demux->audio->packs, demux->audio->bytes);
704 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
705 return MP_NOPTS_VALUE;
707 if (demux->video->packs >= MAX_PACKS
708 || demux->video->bytes >= MAX_PACK_BYTES) {
709 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "\nToo many video packets in the buffer: (%d in %d bytes).\n",
710 demux->video->packs, demux->video->bytes);
711 mp_tmsg(MSGT_DEMUXER, MSGL_HINT, MaybeNI);
712 return MP_NOPTS_VALUE;
714 if (!demux_fill_buffer(demux, ds))
715 return MP_NOPTS_VALUE;
717 return ds->first->pts;
720 // ====================================================================
722 void demuxer_help(void)
724 int i;
726 mp_msg(MSGT_DEMUXER, MSGL_INFO, "Available demuxers:\n");
727 mp_msg(MSGT_DEMUXER, MSGL_INFO, " demuxer: type info: (comment)\n");
728 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DEMUXERS\n");
729 for (i = 0; demuxer_list[i]; i++) {
730 if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Don't display special demuxers
731 continue;
732 if (demuxer_list[i]->comment && strlen(demuxer_list[i]->comment))
733 mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s (%s)\n",
734 demuxer_list[i]->name, demuxer_list[i]->type,
735 demuxer_list[i]->info, demuxer_list[i]->comment);
736 else
737 mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s\n",
738 demuxer_list[i]->name, demuxer_list[i]->type,
739 demuxer_list[i]->info);
745 * Get demuxer type for a given demuxer name
747 * @param demuxer_name string with demuxer name of demuxer number
748 * @param force will be set if demuxer should be forced.
749 * May be NULL.
750 * @return DEMUXER_TYPE_xxx, -1 if error or not found
752 int get_demuxer_type_from_name(char *demuxer_name, int *force)
754 int i;
755 long type_int;
756 char *endptr;
758 if (!demuxer_name || !demuxer_name[0])
759 return DEMUXER_TYPE_UNKNOWN;
760 if (force)
761 *force = demuxer_name[0] == '+';
762 if (demuxer_name[0] == '+')
763 demuxer_name = &demuxer_name[1];
764 for (i = 0; demuxer_list[i]; i++) {
765 if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Can't select special demuxers from commandline
766 continue;
767 if (strcmp(demuxer_name, demuxer_list[i]->name) == 0)
768 return demuxer_list[i]->type;
771 // No match found, try to parse name as an integer (demuxer number)
772 type_int = strtol(demuxer_name, &endptr, 0);
773 if (*endptr) // Conversion failed
774 return -1;
775 if ((type_int > 0) && (type_int <= DEMUXER_TYPE_MAX))
776 return (int) type_int;
778 return -1;
781 int extension_parsing = 1; // 0=off 1=mixed (used only for unstable formats)
784 NOTE : Several demuxers may be opened at the same time so
785 demuxers should NEVER rely on an external var to enable them
786 self. If a demuxer can't do any auto-detection it should only use
787 file_format. The user can explicitly set file_format with the -demuxer
788 option so there is really no need for another extra var.
789 For convenience an option can be added to set file_format directly
790 to the right type (ex: rawaudio,rawvideo).
791 Also the stream can override the file_format so a demuxer which rely
792 on a special stream type can set file_format at the stream level
793 (ex: tv,mf).
796 static demuxer_t *demux_open_stream(struct MPOpts *opts, stream_t *stream,
797 int file_format, int force, int audio_id,
798 int video_id, int dvdsub_id,
799 char *filename)
801 demuxer_t *demuxer = NULL;
803 sh_video_t *sh_video = NULL;
805 const demuxer_desc_t *demuxer_desc;
806 int fformat = 0;
807 int i;
809 // If somebody requested a demuxer check it
810 if (file_format) {
811 if ((demuxer_desc = get_demuxer_desc_from_type(file_format))) {
812 demuxer = new_demuxer(opts, stream, demuxer_desc->type, audio_id,
813 video_id, dvdsub_id, filename);
814 if (demuxer_desc->check_file)
815 fformat = demuxer_desc->check_file(demuxer);
816 if (force || !demuxer_desc->check_file)
817 fformat = demuxer_desc->type;
818 if (fformat != 0) {
819 if (fformat == demuxer_desc->type) {
820 demuxer_t *demux2 = demuxer;
821 // Move messages to demuxer detection code?
822 mp_tmsg(MSGT_DEMUXER, MSGL_INFO,
823 "%s file format detected.\n",
824 demuxer_desc->shortdesc);
825 file_format = fformat;
826 if (!demuxer->desc->open
827 || (demux2 = demuxer->desc->open(demuxer))) {
828 demuxer = demux2;
829 goto dmx_open;
831 } else {
832 // Format changed after check, recurse
833 free_demuxer(demuxer);
834 return demux_open_stream(opts, stream, fformat, force,
835 audio_id, video_id, dvdsub_id,
836 filename);
839 // Check failed for forced demuxer, quit
840 free_demuxer(demuxer);
841 return NULL;
844 // Test demuxers with safe file checks
845 for (i = 0; (demuxer_desc = demuxer_list[i]); i++) {
846 if (demuxer_desc->safe_check) {
847 demuxer = new_demuxer(opts, stream, demuxer_desc->type, audio_id,
848 video_id, dvdsub_id, filename);
849 if ((fformat = demuxer_desc->check_file(demuxer)) != 0) {
850 if (fformat == demuxer_desc->type) {
851 demuxer_t *demux2 = demuxer;
852 mp_tmsg(MSGT_DEMUXER, MSGL_INFO,
853 "%s file format detected.\n",
854 demuxer_desc->shortdesc);
855 file_format = fformat;
856 if (!demuxer->desc->open
857 || (demux2 = demuxer->desc->open(demuxer))) {
858 demuxer = demux2;
859 goto dmx_open;
861 } else {
862 if (fformat == DEMUXER_TYPE_PLAYLIST)
863 return demuxer; // handled in mplayer.c
864 // Format changed after check, recurse
865 free_demuxer(demuxer);
866 demuxer = demux_open_stream(opts, stream, fformat, force,
867 audio_id, video_id,
868 dvdsub_id, filename);
869 if (demuxer)
870 return demuxer; // done!
871 file_format = DEMUXER_TYPE_UNKNOWN;
874 free_demuxer(demuxer);
875 demuxer = NULL;
879 // If no forced demuxer perform file extension based detection
880 // Ok. We're over the stable detectable fileformats, the next ones are
881 // a bit fuzzy. So by default (extension_parsing==1) try extension-based
882 // detection first:
883 if (file_format == DEMUXER_TYPE_UNKNOWN && filename
884 && extension_parsing == 1) {
885 file_format = demuxer_type_by_filename(filename);
886 if (file_format != DEMUXER_TYPE_UNKNOWN) {
887 // we like recursion :)
888 demuxer = demux_open_stream(opts, stream, file_format, force,
889 audio_id, video_id, dvdsub_id,
890 filename);
891 if (demuxer)
892 return demuxer; // done!
893 file_format = DEMUXER_TYPE_UNKNOWN; // continue fuzzy guessing...
894 mp_msg(MSGT_DEMUXER, MSGL_V,
895 "demuxer: continue fuzzy content-based format guessing...\n");
898 // Try detection for all other demuxers
899 for (i = 0; (demuxer_desc = demuxer_list[i]); i++) {
900 if (!demuxer_desc->safe_check && demuxer_desc->check_file) {
901 demuxer = new_demuxer(opts, stream, demuxer_desc->type, audio_id,
902 video_id, dvdsub_id, filename);
903 if ((fformat = demuxer_desc->check_file(demuxer)) != 0) {
904 if (fformat == demuxer_desc->type) {
905 demuxer_t *demux2 = demuxer;
906 mp_tmsg(MSGT_DEMUXER, MSGL_INFO,
907 "%s file format detected.\n",
908 demuxer_desc->shortdesc);
909 file_format = fformat;
910 if (!demuxer->desc->open
911 || (demux2 = demuxer->desc->open(demuxer))) {
912 demuxer = demux2;
913 goto dmx_open;
915 } else {
916 if (fformat == DEMUXER_TYPE_PLAYLIST)
917 return demuxer; // handled in mplayer.c
918 // Format changed after check, recurse
919 free_demuxer(demuxer);
920 demuxer = demux_open_stream(opts, stream, fformat, force,
921 audio_id, video_id,
922 dvdsub_id, filename);
923 if (demuxer)
924 return demuxer; // done!
925 file_format = DEMUXER_TYPE_UNKNOWN;
928 free_demuxer(demuxer);
929 demuxer = NULL;
933 return NULL;
934 //====== File format recognized, set up these for compatibility: =========
935 dmx_open:
937 demuxer->file_format = file_format;
939 if ((sh_video = demuxer->video->sh) && sh_video->bih) {
940 int biComp = le2me_32(sh_video->bih->biCompression);
941 mp_msg(MSGT_DEMUX, MSGL_INFO,
942 "VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
943 (char *) &biComp, sh_video->bih->biWidth,
944 sh_video->bih->biHeight, sh_video->bih->biBitCount,
945 sh_video->fps, sh_video->i_bps * 0.008f,
946 sh_video->i_bps / 1024.0f);
948 #ifdef CONFIG_ASS
949 if (ass_enabled && ass_library) {
950 for (i = 0; i < MAX_S_STREAMS; ++i) {
951 sh_sub_t *sh = demuxer->s_streams[i];
952 if (sh && sh->type == 'a') {
953 sh->ass_track = ass_new_track(ass_library);
954 if (sh->ass_track && sh->extradata)
955 ass_process_codec_private(sh->ass_track, sh->extradata,
956 sh->extradata_len);
957 } else if (sh && sh->type != 'v')
958 sh->ass_track = ass_default_track(ass_library);
961 #endif
962 return demuxer;
965 char *audio_stream = NULL;
966 char *sub_stream = NULL;
967 int audio_stream_cache = 0;
969 char *demuxer_name = NULL; // parameter from -demuxer
970 char *audio_demuxer_name = NULL; // parameter from -audio-demuxer
971 char *sub_demuxer_name = NULL; // parameter from -sub-demuxer
973 extern int hr_mp3_seek;
975 extern float stream_cache_min_percent;
976 extern float stream_cache_seek_min_percent;
978 demuxer_t *demux_open(struct MPOpts *opts, stream_t *vs, int file_format,
979 int audio_id, int video_id, int dvdsub_id,
980 char *filename)
982 stream_t *as = NULL, *ss = NULL;
983 demuxer_t *vd, *ad = NULL, *sd = NULL;
984 demuxer_t *res;
985 int afmt = DEMUXER_TYPE_UNKNOWN, sfmt = DEMUXER_TYPE_UNKNOWN;
986 int demuxer_type;
987 int audio_demuxer_type = 0, sub_demuxer_type = 0;
988 int demuxer_force = 0, audio_demuxer_force = 0, sub_demuxer_force = 0;
990 if ((demuxer_type =
991 get_demuxer_type_from_name(demuxer_name, &demuxer_force)) < 0) {
992 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-demuxer %s does not exist.\n",
993 demuxer_name);
995 if ((audio_demuxer_type =
996 get_demuxer_type_from_name(audio_demuxer_name,
997 &audio_demuxer_force)) < 0) {
998 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-audio-demuxer %s does not exist.\n",
999 audio_demuxer_name);
1001 if ((sub_demuxer_type =
1002 get_demuxer_type_from_name(sub_demuxer_name,
1003 &sub_demuxer_force)) < 0) {
1004 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-sub-demuxer %s does not exist.\n",
1005 sub_demuxer_name);
1008 if (audio_stream) {
1009 as = open_stream(audio_stream, 0, &afmt);
1010 if (!as) {
1011 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "Cannot open audio stream: %s\n",
1012 audio_stream);
1013 return NULL;
1015 if (audio_stream_cache) {
1016 if (!stream_enable_cache
1017 (as, audio_stream_cache * 1024,
1018 audio_stream_cache * 1024 * (stream_cache_min_percent /
1019 100.0),
1020 audio_stream_cache * 1024 * (stream_cache_seek_min_percent /
1021 100.0))) {
1022 free_stream(as);
1023 mp_msg(MSGT_DEMUXER, MSGL_ERR,
1024 "Can't enable audio stream cache\n");
1025 return NULL;
1029 if (sub_stream) {
1030 ss = open_stream(sub_stream, 0, &sfmt);
1031 if (!ss) {
1032 mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "Cannot open subtitle stream: %s\n",
1033 sub_stream);
1034 return NULL;
1038 vd = demux_open_stream(opts, vs, demuxer_type ? demuxer_type : file_format,
1039 demuxer_force, audio_stream ? -2 : audio_id,
1040 video_id, sub_stream ? -2 : dvdsub_id, filename);
1041 if (!vd) {
1042 if (as)
1043 free_stream(as);
1044 if (ss)
1045 free_stream(ss);
1046 return NULL;
1048 if (as) {
1049 ad = demux_open_stream(opts, as,
1050 audio_demuxer_type ? audio_demuxer_type : afmt,
1051 audio_demuxer_force, audio_id, -2, -2,
1052 audio_stream);
1053 if (!ad) {
1054 mp_tmsg(MSGT_DEMUXER, MSGL_WARN, "Failed to open audio demuxer: %s\n",
1055 audio_stream);
1056 free_stream(as);
1057 } else if (ad->audio->sh
1058 && ((sh_audio_t *) ad->audio->sh)->format == 0x55) // MP3
1059 hr_mp3_seek = 1; // Enable high res seeking
1061 if (ss) {
1062 sd = demux_open_stream(opts, ss,
1063 sub_demuxer_type ? sub_demuxer_type : sfmt,
1064 sub_demuxer_force, -2, -2, dvdsub_id,
1065 sub_stream);
1066 if (!sd) {
1067 mp_tmsg(MSGT_DEMUXER, MSGL_WARN,
1068 "Failed to open subtitle demuxer: %s\n", sub_stream);
1069 free_stream(ss);
1073 if (ad && sd)
1074 res = new_demuxers_demuxer(vd, ad, sd);
1075 else if (ad)
1076 res = new_demuxers_demuxer(vd, ad, vd);
1077 else if (sd)
1078 res = new_demuxers_demuxer(vd, vd, sd);
1079 else
1080 res = vd;
1082 opts->correct_pts = opts->user_correct_pts;
1083 if (opts->correct_pts < 0)
1084 opts->correct_pts =
1085 demux_control(res, DEMUXER_CTRL_CORRECT_PTS,
1086 NULL) == DEMUXER_CTRL_OK;
1087 return res;
1091 void demux_flush(demuxer_t *demuxer)
1093 ds_free_packs(demuxer->video);
1094 ds_free_packs(demuxer->audio);
1095 ds_free_packs(demuxer->sub);
1098 int demux_seek(demuxer_t *demuxer, float rel_seek_secs, float audio_delay,
1099 int flags)
1101 demux_stream_t *d_audio = demuxer->audio;
1102 demux_stream_t *d_video = demuxer->video;
1103 sh_audio_t *sh_audio = d_audio->sh;
1104 sh_video_t *sh_video = d_video->sh;
1105 double tmp = 0;
1106 double pts;
1108 if (!demuxer->seekable) {
1109 if (demuxer->file_format == DEMUXER_TYPE_AVI)
1110 mp_tmsg(MSGT_SEEK, MSGL_WARN, "Cannot seek in raw AVI streams. (Index required, try with the -idx switch.)\n");
1111 #ifdef CONFIG_TV
1112 else if (demuxer->file_format == DEMUXER_TYPE_TV)
1113 mp_tmsg(MSGT_SEEK, MSGL_WARN, "TV input is not seekable! (Seeking will probably be for changing channels ;)\n");
1114 #endif
1115 else
1116 mp_tmsg(MSGT_SEEK, MSGL_WARN, "Cannot seek in this file.\n");
1117 return 0;
1119 // clear demux buffers:
1120 demux_flush(demuxer);
1121 if (sh_audio)
1122 sh_audio->a_buffer_len = 0;
1124 demuxer->stream->eof = 0;
1125 demuxer->video->eof = 0;
1126 demuxer->audio->eof = 0;
1128 if (sh_video)
1129 sh_video->timer = 0; // !!!!!!
1131 if (flags & SEEK_ABSOLUTE)
1132 pts = 0.0f;
1133 else {
1134 if (demuxer->stream_pts == MP_NOPTS_VALUE)
1135 goto dmx_seek;
1136 pts = demuxer->stream_pts;
1139 if (flags & SEEK_FACTOR) {
1140 if (stream_control(demuxer->stream, STREAM_CTRL_GET_TIME_LENGTH, &tmp)
1141 == STREAM_UNSUPPORTED)
1142 goto dmx_seek;
1143 pts += tmp * rel_seek_secs;
1144 } else
1145 pts += rel_seek_secs;
1147 if (stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_TIME, &pts) !=
1148 STREAM_UNSUPPORTED) {
1149 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1150 return 1;
1153 dmx_seek:
1154 if (demuxer->desc->seek)
1155 demuxer->desc->seek(demuxer, rel_seek_secs, audio_delay, flags);
1157 if (sh_audio)
1158 resync_audio_stream(sh_audio);
1160 return 1;
1163 int demux_info_add(demuxer_t *demuxer, const char *opt, const char *param)
1165 char **info = demuxer->info;
1166 int n = 0;
1169 for (n = 0; info && info[2 * n] != NULL; n++) {
1170 if (!strcasecmp(opt, info[2 * n])) {
1171 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "Demuxer info %s changed to %s\n", opt,
1172 param);
1173 free(info[2 * n + 1]);
1174 info[2 * n + 1] = strdup(param);
1175 return 0;
1179 info = demuxer->info =
1180 (char **) realloc(info, (2 * (n + 2)) * sizeof(char *));
1181 info[2 * n] = strdup(opt);
1182 info[2 * n + 1] = strdup(param);
1183 memset(&info[2 * (n + 1)], 0, 2 * sizeof(char *));
1185 return 1;
1188 int demux_info_print(demuxer_t *demuxer)
1190 char **info = demuxer->info;
1191 int n;
1193 if (!info)
1194 return 0;
1196 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "Clip info:\n");
1197 for (n = 0; info[2 * n] != NULL; n++) {
1198 mp_msg(MSGT_DEMUX, MSGL_INFO, " %s: %s\n", info[2 * n],
1199 info[2 * n + 1]);
1200 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_NAME%d=%s\n", n,
1201 info[2 * n]);
1202 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_VALUE%d=%s\n", n,
1203 info[2 * n + 1]);
1205 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_N=%d\n", n);
1207 return 0;
1210 char *demux_info_get(demuxer_t *demuxer, const char *opt)
1212 int i;
1213 char **info = demuxer->info;
1215 for (i = 0; info && info[2 * i] != NULL; i++) {
1216 if (!strcasecmp(opt, info[2 * i]))
1217 return info[2 * i + 1];
1220 return NULL;
1223 int demux_control(demuxer_t *demuxer, int cmd, void *arg)
1226 if (demuxer->desc->control)
1227 return demuxer->desc->control(demuxer, cmd, arg);
1229 return DEMUXER_CTRL_NOTIMPL;
1234 double demuxer_get_time_length(demuxer_t *demuxer)
1236 double get_time_ans;
1237 sh_video_t *sh_video = demuxer->video->sh;
1238 sh_audio_t *sh_audio = demuxer->audio->sh;
1239 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
1240 if (demux_control
1241 (demuxer, DEMUXER_CTRL_GET_TIME_LENGTH, (void *) &get_time_ans) <= 0) {
1242 if (sh_video && sh_video->i_bps && sh_audio && sh_audio->i_bps)
1243 get_time_ans = (double) (demuxer->movi_end -
1244 demuxer->movi_start) / (sh_video->i_bps +
1245 sh_audio->i_bps);
1246 else if (sh_video && sh_video->i_bps)
1247 get_time_ans = (double) (demuxer->movi_end -
1248 demuxer->movi_start) / sh_video->i_bps;
1249 else if (sh_audio && sh_audio->i_bps)
1250 get_time_ans = (double) (demuxer->movi_end -
1251 demuxer->movi_start) / sh_audio->i_bps;
1252 else
1253 get_time_ans = 0;
1255 return get_time_ans;
1259 * \brief demuxer_get_current_time() returns the time of the current play in three possible ways:
1260 * either when the stream reader satisfies STREAM_CTRL_GET_CURRENT_TIME (e.g. dvd)
1261 * or using sh_video->pts when the former method fails
1262 * 0 otherwise
1263 * \return the current play time
1265 int demuxer_get_current_time(demuxer_t *demuxer)
1267 double get_time_ans = 0;
1268 sh_video_t *sh_video = demuxer->video->sh;
1269 if (demuxer->stream_pts != MP_NOPTS_VALUE)
1270 get_time_ans = demuxer->stream_pts;
1271 else if (sh_video)
1272 get_time_ans = sh_video->pts;
1273 return (int) get_time_ans;
1276 int demuxer_get_percent_pos(demuxer_t *demuxer)
1278 int ans = 0;
1279 int res = demux_control(demuxer, DEMUXER_CTRL_GET_PERCENT_POS, &ans);
1280 int len = (demuxer->movi_end - demuxer->movi_start) / 100;
1281 if (res <= 0) {
1282 if (len > 0)
1283 ans = (demuxer->filepos - demuxer->movi_start) / len;
1284 else
1285 ans = 0;
1287 if (ans < 0)
1288 ans = 0;
1289 if (ans > 100)
1290 ans = 100;
1291 return ans;
1294 int demuxer_switch_audio(demuxer_t *demuxer, int index)
1296 int res = demux_control(demuxer, DEMUXER_CTRL_SWITCH_AUDIO, &index);
1297 if (res == DEMUXER_CTRL_NOTIMPL)
1298 index = demuxer->audio->id;
1299 if (demuxer->audio->id >= 0)
1300 demuxer->audio->sh = demuxer->a_streams[demuxer->audio->id];
1301 else
1302 demuxer->audio->sh = NULL;
1303 return index;
1306 int demuxer_switch_video(demuxer_t *demuxer, int index)
1308 int res = demux_control(demuxer, DEMUXER_CTRL_SWITCH_VIDEO, &index);
1309 if (res == DEMUXER_CTRL_NOTIMPL)
1310 index = demuxer->video->id;
1311 if (demuxer->video->id >= 0)
1312 demuxer->video->sh = demuxer->v_streams[demuxer->video->id];
1313 else
1314 demuxer->video->sh = NULL;
1315 return index;
1318 int demuxer_add_attachment(demuxer_t *demuxer, const char *name,
1319 const char *type, const void *data, size_t size)
1321 if (!(demuxer->num_attachments & 31))
1322 demuxer->attachments = realloc(demuxer->attachments,
1323 (demuxer->num_attachments + 32) * sizeof(demux_attachment_t));
1325 demuxer->attachments[demuxer->num_attachments].name = strdup(name);
1326 demuxer->attachments[demuxer->num_attachments].type = strdup(type);
1327 demuxer->attachments[demuxer->num_attachments].data = malloc(size);
1328 memcpy(demuxer->attachments[demuxer->num_attachments].data, data, size);
1329 demuxer->attachments[demuxer->num_attachments].data_size = size;
1331 return demuxer->num_attachments++;
1334 int demuxer_add_chapter(demuxer_t *demuxer, const char *name, uint64_t start,
1335 uint64_t end)
1337 if (demuxer->chapters == NULL)
1338 demuxer->chapters = malloc(32 * sizeof(*demuxer->chapters));
1339 else if (!(demuxer->num_chapters % 32))
1340 demuxer->chapters = realloc(demuxer->chapters,
1341 (demuxer->num_chapters + 32) *
1342 sizeof(*demuxer->chapters));
1344 demuxer->chapters[demuxer->num_chapters].start = start;
1345 demuxer->chapters[demuxer->num_chapters].end = end;
1346 demuxer->chapters[demuxer->num_chapters].name = strdup(name ? name : mp_gtext("unknown"));
1348 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_ID=%d\n", demuxer->num_chapters);
1349 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_START=%"PRIu64"\n", demuxer->num_chapters, start);
1350 if (end)
1351 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_END=%"PRIu64"\n", demuxer->num_chapters, end);
1352 if (name)
1353 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_NAME=%s\n", demuxer->num_chapters, name);
1355 return demuxer->num_chapters++;
1359 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1360 * either using the demuxer->chapters structure set by the demuxer
1361 * or asking help to the stream layer (e.g. dvd)
1362 * \param chapter - chapter number wished - 0-based
1363 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1364 * \param chapter_name name of chapter found (set by this function is param is not null)
1365 * \return -1 on error, current chapter if successful
1368 int demuxer_seek_chapter(demuxer_t *demuxer, int chapter, double *seek_pts,
1369 char **chapter_name)
1371 int ris;
1372 sh_video_t *sh_video = demuxer->video->sh;
1373 sh_audio_t *sh_audio = demuxer->audio->sh;
1375 if (!demuxer->num_chapters || !demuxer->chapters) {
1376 demux_flush(demuxer);
1378 ris = stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_CHAPTER,
1379 &chapter);
1380 if (ris != STREAM_UNSUPPORTED)
1381 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1382 if (sh_video) {
1383 ds_fill_buffer(demuxer->video);
1384 resync_video_stream(sh_video);
1387 if (sh_audio) {
1388 ds_fill_buffer(demuxer->audio);
1389 resync_audio_stream(sh_audio);
1391 // exit status may be ok, but main() doesn't have to seek itself
1392 // (because e.g. dvds depend on sectors, not on pts)
1393 *seek_pts = -1.0;
1395 if (chapter_name) {
1396 *chapter_name = NULL;
1397 int num_chapters;
1398 if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS,
1399 &num_chapters) == STREAM_UNSUPPORTED)
1400 num_chapters = 0;
1401 if (num_chapters) {
1402 *chapter_name = talloc_size(NULL, 16);
1403 sprintf(*chapter_name, " of %3d", num_chapters);
1407 return ris != STREAM_UNSUPPORTED ? chapter : -1;
1408 } else { // chapters structure is set in the demuxer
1409 if (chapter >= demuxer->num_chapters)
1410 return -1;
1411 if (chapter < 0)
1412 chapter = 0;
1414 *seek_pts = demuxer->chapters[chapter].start / 1000.0;
1416 if (chapter_name)
1417 *chapter_name = talloc_strdup(NULL, demuxer->chapters[chapter].name);
1419 return chapter;
1423 int demuxer_get_current_chapter(demuxer_t *demuxer)
1425 int chapter = -1;
1426 if (!demuxer->num_chapters || !demuxer->chapters) {
1427 if (stream_control(demuxer->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
1428 &chapter) == STREAM_UNSUPPORTED)
1429 chapter = -1;
1430 } else {
1431 sh_video_t *sh_video = demuxer->video->sh;
1432 sh_audio_t *sh_audio = demuxer->audio->sh;
1433 uint64_t now;
1434 now = (sh_video ? sh_video->pts : (sh_audio ? sh_audio->pts : 0))
1435 * 1000 + 0.5;
1436 for (chapter = demuxer->num_chapters - 1; chapter >= 0; --chapter) {
1437 if (demuxer->chapters[chapter].start <= now)
1438 break;
1441 return chapter;
1444 char *demuxer_chapter_name(demuxer_t *demuxer, int chapter)
1446 if (demuxer->num_chapters && demuxer->chapters) {
1447 if (chapter >= 0 && chapter < demuxer->num_chapters
1448 && demuxer->chapters[chapter].name)
1449 return strdup(demuxer->chapters[chapter].name);
1451 return NULL;
1454 char *demuxer_chapter_display_name(demuxer_t *demuxer, int chapter)
1456 char *chapter_name = demuxer_chapter_name(demuxer, chapter);
1457 if (chapter_name) {
1458 char *tmp = malloc(strlen(chapter_name) + 14);
1459 snprintf(tmp, 63, "(%d) %s", chapter + 1, chapter_name);
1460 free(chapter_name);
1461 return tmp;
1462 } else {
1463 int chapter_num = demuxer_chapter_count(demuxer);
1464 char tmp[30];
1465 if (chapter_num <= 0)
1466 sprintf(tmp, "(%d)", chapter + 1);
1467 else
1468 sprintf(tmp, "(%d) of %d", chapter + 1, chapter_num);
1469 return strdup(tmp);
1473 float demuxer_chapter_time(demuxer_t *demuxer, int chapter, float *end)
1475 if (demuxer->num_chapters && demuxer->chapters && chapter >= 0
1476 && chapter < demuxer->num_chapters) {
1477 if (end)
1478 *end = demuxer->chapters[chapter].end / 1000.0;
1479 return demuxer->chapters[chapter].start / 1000.0;
1481 return -1.0;
1484 int demuxer_chapter_count(demuxer_t *demuxer)
1486 if (!demuxer->num_chapters || !demuxer->chapters) {
1487 int num_chapters = 0;
1488 if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS,
1489 &num_chapters) == STREAM_UNSUPPORTED)
1490 num_chapters = 0;
1491 return num_chapters;
1492 } else
1493 return demuxer->num_chapters;
1496 int demuxer_angles_count(demuxer_t *demuxer)
1498 int ris, angles = -1;
1500 ris = stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_ANGLES, &angles);
1501 if (ris == STREAM_UNSUPPORTED)
1502 return -1;
1503 return angles;
1506 int demuxer_get_current_angle(demuxer_t *demuxer)
1508 int ris, curr_angle = -1;
1509 ris = stream_control(demuxer->stream, STREAM_CTRL_GET_ANGLE, &curr_angle);
1510 if (ris == STREAM_UNSUPPORTED)
1511 return -1;
1512 return curr_angle;
1516 int demuxer_set_angle(demuxer_t *demuxer, int angle)
1518 int ris, angles = -1;
1519 sh_video_t *sh_video = demuxer->video->sh;
1520 sh_audio_t *sh_audio = demuxer->audio->sh;
1522 angles = demuxer_angles_count(demuxer);
1523 if ((angles < 1) || (angle > angles))
1524 return -1;
1526 demux_flush(demuxer);
1528 ris = stream_control(demuxer->stream, STREAM_CTRL_SET_ANGLE, &angle);
1529 if (ris == STREAM_UNSUPPORTED)
1530 return -1;
1532 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1533 if (sh_video) {
1534 ds_fill_buffer(demuxer->video);
1535 resync_video_stream(sh_video);
1538 if (sh_audio) {
1539 ds_fill_buffer(demuxer->audio);
1540 resync_audio_stream(sh_audio);
1543 return angle;
1546 int demuxer_audio_track_by_lang(demuxer_t *d, char *lang)
1548 int i, len;
1549 lang += strspn(lang, ",");
1550 while ((len = strcspn(lang, ",")) > 0) {
1551 for (i = 0; i < MAX_A_STREAMS; ++i) {
1552 sh_audio_t *sh = d->a_streams[i];
1553 if (sh && sh->lang && strncmp(sh->lang, lang, len) == 0)
1554 return sh->aid;
1556 lang += len;
1557 lang += strspn(lang, ",");
1559 return -1;
1562 int demuxer_sub_track_by_lang(demuxer_t *d, char *lang)
1564 int i, len;
1565 lang += strspn(lang, ",");
1566 while ((len = strcspn(lang, ",")) > 0) {
1567 for (i = 0; i < MAX_S_STREAMS; ++i) {
1568 sh_sub_t *sh = d->s_streams[i];
1569 if (sh && sh->lang && strncmp(sh->lang, lang, len) == 0)
1570 return sh->sid;
1572 lang += len;
1573 lang += strspn(lang, ",");
1575 return -1;
1578 int demuxer_default_audio_track(demuxer_t *d)
1580 int i;
1581 for (i = 0; i < MAX_A_STREAMS; ++i) {
1582 sh_audio_t *sh = d->a_streams[i];
1583 if (sh && sh->default_track)
1584 return sh->aid;
1586 for (i = 0; i < MAX_A_STREAMS; ++i) {
1587 sh_audio_t *sh = d->a_streams[i];
1588 if (sh)
1589 return sh->aid;
1591 return -1;
1594 int demuxer_default_sub_track(demuxer_t *d)
1596 int i;
1597 for (i = 0; i < MAX_S_STREAMS; ++i) {
1598 sh_sub_t *sh = d->s_streams[i];
1599 if (sh && sh->default_track)
1600 return sh->sid;
1602 return -1;