Restore alphabetic order for HAVE_ defines.
[mplayer/glamo.git] / libmpdemux / demuxer.c
blobc46ba9d43d2bbe07923e24dd142018be50071858
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"
43 #ifdef CONFIG_ASS
44 #include "libass/ass.h"
45 #include "libass/ass_mp.h"
46 #endif
48 #ifdef CONFIG_LIBAVCODEC
49 #include "libavcodec/avcodec.h"
50 #if MP_INPUT_BUFFER_PADDING_SIZE < FF_INPUT_BUFFER_PADDING_SIZE
51 #error MP_INPUT_BUFFER_PADDING_SIZE is too small!
52 #endif
53 #endif
55 void resync_video_stream(sh_video_t *sh_video);
56 void resync_audio_stream(sh_audio_t *sh_audio);
58 // Demuxer list
59 extern const demuxer_desc_t demuxer_desc_rawaudio;
60 extern const demuxer_desc_t demuxer_desc_rawvideo;
61 extern const demuxer_desc_t demuxer_desc_tv;
62 extern const demuxer_desc_t demuxer_desc_mf;
63 extern const demuxer_desc_t demuxer_desc_avi;
64 extern const demuxer_desc_t demuxer_desc_y4m;
65 extern const demuxer_desc_t demuxer_desc_asf;
66 extern const demuxer_desc_t demuxer_desc_real;
67 extern const demuxer_desc_t demuxer_desc_smjpeg;
68 extern const demuxer_desc_t demuxer_desc_matroska;
69 extern const demuxer_desc_t demuxer_desc_realaudio;
70 extern const demuxer_desc_t demuxer_desc_vqf;
71 extern const demuxer_desc_t demuxer_desc_mov;
72 extern const demuxer_desc_t demuxer_desc_vivo;
73 extern const demuxer_desc_t demuxer_desc_fli;
74 extern const demuxer_desc_t demuxer_desc_film;
75 extern const demuxer_desc_t demuxer_desc_roq;
76 extern const demuxer_desc_t demuxer_desc_gif;
77 extern const demuxer_desc_t demuxer_desc_ogg;
78 extern const demuxer_desc_t demuxer_desc_avs;
79 extern const demuxer_desc_t demuxer_desc_pva;
80 extern const demuxer_desc_t demuxer_desc_nsv;
81 extern const demuxer_desc_t demuxer_desc_mpeg_ts;
82 extern const demuxer_desc_t demuxer_desc_lmlm4;
83 extern const demuxer_desc_t demuxer_desc_mpeg_ps;
84 extern const demuxer_desc_t demuxer_desc_mpeg_pes;
85 extern const demuxer_desc_t demuxer_desc_mpeg_es;
86 extern const demuxer_desc_t demuxer_desc_mpeg_gxf;
87 extern const demuxer_desc_t demuxer_desc_mpeg4_es;
88 extern const demuxer_desc_t demuxer_desc_h264_es;
89 extern const demuxer_desc_t demuxer_desc_rawdv;
90 extern const demuxer_desc_t demuxer_desc_mpc;
91 extern const demuxer_desc_t demuxer_desc_audio;
92 extern const demuxer_desc_t demuxer_desc_xmms;
93 extern const demuxer_desc_t demuxer_desc_mpeg_ty;
94 extern const demuxer_desc_t demuxer_desc_rtp;
95 extern const demuxer_desc_t demuxer_desc_rtp_nemesi;
96 extern const demuxer_desc_t demuxer_desc_lavf;
97 extern const demuxer_desc_t demuxer_desc_lavf_preferred;
98 extern const demuxer_desc_t demuxer_desc_aac;
99 extern const demuxer_desc_t demuxer_desc_nut;
100 extern const demuxer_desc_t demuxer_desc_mng;
102 /* Please do not add any new demuxers here. If you want to implement a new
103 * demuxer, add it to libavformat, except for wrappers around external
104 * libraries and demuxers requiring binary support. */
106 const demuxer_desc_t *const demuxer_list[] = {
107 &demuxer_desc_rawaudio,
108 &demuxer_desc_rawvideo,
109 #ifdef CONFIG_TV
110 &demuxer_desc_tv,
111 #endif
112 &demuxer_desc_mf,
113 #ifdef CONFIG_LIBAVFORMAT
114 &demuxer_desc_lavf_preferred,
115 #endif
116 &demuxer_desc_avi,
117 &demuxer_desc_y4m,
118 &demuxer_desc_asf,
119 &demuxer_desc_nsv,
120 &demuxer_desc_real,
121 &demuxer_desc_smjpeg,
122 &demuxer_desc_matroska,
123 &demuxer_desc_realaudio,
124 &demuxer_desc_vqf,
125 &demuxer_desc_mov,
126 &demuxer_desc_vivo,
127 &demuxer_desc_fli,
128 &demuxer_desc_film,
129 &demuxer_desc_roq,
130 #ifdef CONFIG_GIF
131 &demuxer_desc_gif,
132 #endif
133 #ifdef CONFIG_OGGVORBIS
134 &demuxer_desc_ogg,
135 #endif
136 #ifdef CONFIG_WIN32DLL
137 &demuxer_desc_avs,
138 #endif
139 &demuxer_desc_pva,
140 &demuxer_desc_mpeg_ts,
141 &demuxer_desc_lmlm4,
142 &demuxer_desc_mpeg_ps,
143 &demuxer_desc_mpeg_pes,
144 &demuxer_desc_mpeg_es,
145 &demuxer_desc_mpeg_gxf,
146 &demuxer_desc_mpeg4_es,
147 &demuxer_desc_h264_es,
148 &demuxer_desc_audio,
149 &demuxer_desc_mpeg_ty,
150 #ifdef CONFIG_LIVE555
151 &demuxer_desc_rtp,
152 #endif
153 #ifdef CONFIG_LIBNEMESI
154 &demuxer_desc_rtp_nemesi,
155 #endif
156 #ifdef CONFIG_LIBAVFORMAT
157 &demuxer_desc_lavf,
158 #endif
159 #ifdef CONFIG_MUSEPACK
160 &demuxer_desc_mpc,
161 #endif
162 #ifdef CONFIG_LIBDV095
163 &demuxer_desc_rawdv,
164 #endif
165 &demuxer_desc_aac,
166 #ifdef CONFIG_LIBNUT
167 &demuxer_desc_nut,
168 #endif
169 #ifdef CONFIG_XMMS
170 &demuxer_desc_xmms,
171 #endif
172 #ifdef CONFIG_MNG
173 &demuxer_desc_mng,
174 #endif
175 /* Please do not add any new demuxers here. If you want to implement a new
176 * demuxer, add it to libavformat, except for wrappers around external
177 * libraries and demuxers requiring binary support. */
178 NULL
181 void free_demuxer_stream(demux_stream_t *ds)
183 ds_free_packs(ds);
184 free(ds);
187 demux_stream_t *new_demuxer_stream(struct demuxer_st *demuxer, int id)
189 demux_stream_t *ds = malloc(sizeof(demux_stream_t));
190 ds->buffer_pos = ds->buffer_size = 0;
191 ds->buffer = NULL;
192 ds->pts = 0;
193 ds->pts_bytes = 0;
194 ds->eof = 0;
195 ds->pos = 0;
196 ds->dpos = 0;
197 ds->pack_no = 0;
199 ds->packs = 0;
200 ds->bytes = 0;
201 ds->first = ds->last = ds->current = NULL;
202 ds->id = id;
203 ds->demuxer = demuxer;
205 ds->asf_seq = -1;
206 ds->asf_packet = NULL;
208 ds->ss_mul = ds->ss_div = 0;
210 ds->sh = NULL;
211 return ds;
216 * Get demuxer description structure for a given demuxer type
218 * @param file_format type of the demuxer
219 * @return structure for the demuxer, NULL if not found
221 static const demuxer_desc_t *get_demuxer_desc_from_type(int file_format)
223 int i;
225 for (i = 0; demuxer_list[i]; i++)
226 if (file_format == demuxer_list[i]->type)
227 return demuxer_list[i];
229 return NULL;
233 demuxer_t *new_demuxer(stream_t *stream, int type, int a_id, int v_id,
234 int s_id, char *filename)
236 demuxer_t *d = malloc(sizeof(demuxer_t));
237 memset(d, 0, sizeof(demuxer_t));
238 d->stream = stream;
239 d->stream_pts = MP_NOPTS_VALUE;
240 d->reference_clock = MP_NOPTS_VALUE;
241 d->movi_start = stream->start_pos;
242 d->movi_end = stream->end_pos;
243 d->seekable = 1;
244 d->synced = 0;
245 d->filepos = 0;
246 d->audio = new_demuxer_stream(d, a_id);
247 d->video = new_demuxer_stream(d, v_id);
248 d->sub = new_demuxer_stream(d, s_id);
249 d->type = type;
250 if (type)
251 if (!(d->desc = get_demuxer_desc_from_type(type)))
252 mp_msg(MSGT_DEMUXER, MSGL_ERR,
253 "BUG! Invalid demuxer type in new_demuxer(), "
254 "big troubles ahead.");
255 if (filename) // Filename hack for avs_check_file
256 d->filename = strdup(filename);
257 stream_reset(stream);
258 stream_seek(stream, stream->start_pos);
259 return d;
262 extern int dvdsub_id;
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 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SUBTITLE_ID=%d\n", sid);
280 if (sid == dvdsub_id) {
281 demuxer->sub->id = id;
282 demuxer->sub->sh = demuxer->s_streams[id];
284 return demuxer->s_streams[id];
287 void free_sh_sub(sh_sub_t *sh)
289 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_sub at %p\n", sh);
290 free(sh->extradata);
291 #ifdef CONFIG_ASS
292 if (sh->ass_track)
293 ass_free_track(sh->ass_track);
294 #endif
295 free(sh->lang);
296 free(sh);
299 sh_audio_t *new_sh_audio_aid(demuxer_t *demuxer, int id, int aid)
301 if (id > MAX_A_STREAMS - 1 || id < 0) {
302 mp_msg(MSGT_DEMUXER, MSGL_WARN,
303 "Requested audio stream id overflow (%d > %d)\n", id,
304 MAX_A_STREAMS);
305 return NULL;
307 if (demuxer->a_streams[id])
308 mp_msg(MSGT_DEMUXER, MSGL_WARN, MSGTR_AudioStreamRedefined, id);
309 else {
310 sh_audio_t *sh = calloc(1, sizeof(sh_audio_t));
311 mp_msg(MSGT_DEMUXER, MSGL_V, MSGTR_FoundAudioStream, id);
312 demuxer->a_streams[id] = sh;
313 sh->aid = aid;
314 sh->ds = demuxer->audio;
315 // set some defaults
316 sh->samplesize = 2;
317 sh->sample_format = AF_FORMAT_S16_NE;
318 sh->audio_out_minsize = 8192; /* default size, maybe not enough for Win32/ACM */
319 sh->pts = MP_NOPTS_VALUE;
320 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_ID=%d\n", aid);
322 return demuxer->a_streams[id];
325 void free_sh_audio(demuxer_t *demuxer, int id)
327 sh_audio_t *sh = demuxer->a_streams[id];
328 demuxer->a_streams[id] = NULL;
329 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_audio at %p\n", sh);
330 free(sh->wf);
331 free(sh->codecdata);
332 free(sh->lang);
333 free(sh);
336 sh_video_t *new_sh_video_vid(demuxer_t *demuxer, int id, int vid)
338 if (id > MAX_V_STREAMS - 1 || id < 0) {
339 mp_msg(MSGT_DEMUXER, MSGL_WARN,
340 "Requested video stream id overflow (%d > %d)\n", id,
341 MAX_V_STREAMS);
342 return NULL;
344 if (demuxer->v_streams[id])
345 mp_msg(MSGT_DEMUXER, MSGL_WARN, MSGTR_VideoStreamRedefined, id);
346 else {
347 sh_video_t *sh = calloc(1, sizeof(sh_video_t));
348 mp_msg(MSGT_DEMUXER, MSGL_V, MSGTR_FoundVideoStream, id);
349 demuxer->v_streams[id] = sh;
350 sh->vid = vid;
351 sh->ds = demuxer->video;
352 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_ID=%d\n", vid);
354 return demuxer->v_streams[id];
357 void free_sh_video(sh_video_t *sh)
359 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_video at %p\n", sh);
360 free(sh->bih);
361 free(sh);
364 void free_demuxer(demuxer_t *demuxer)
366 int i;
367 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing demuxer at %p\n",
368 demuxer);
369 if (demuxer->desc->close)
370 demuxer->desc->close(demuxer);
371 // Very ugly hack to make it behave like old implementation
372 if (demuxer->desc->type == DEMUXER_TYPE_DEMUXERS)
373 goto skip_streamfree;
374 // free streams:
375 for (i = 0; i < MAX_A_STREAMS; i++)
376 if (demuxer->a_streams[i])
377 free_sh_audio(demuxer, i);
378 for (i = 0; i < MAX_V_STREAMS; i++)
379 if (demuxer->v_streams[i])
380 free_sh_video(demuxer->v_streams[i]);
381 for (i = 0; i < MAX_S_STREAMS; i++)
382 if (demuxer->s_streams[i])
383 free_sh_sub(demuxer->s_streams[i]);
384 // free demuxers:
385 free_demuxer_stream(demuxer->audio);
386 free_demuxer_stream(demuxer->video);
387 free_demuxer_stream(demuxer->sub);
388 skip_streamfree:
389 if (demuxer->info) {
390 for (i = 0; demuxer->info[i] != NULL; i++)
391 free(demuxer->info[i]);
392 free(demuxer->info);
394 free(demuxer->filename);
395 if (demuxer->chapters) {
396 for (i = 0; i < demuxer->num_chapters; i++)
397 free(demuxer->chapters[i].name);
398 free(demuxer->chapters);
400 if (demuxer->attachments) {
401 for (i = 0; i < demuxer->num_attachments; i++) {
402 free(demuxer->attachments[i].name);
403 free(demuxer->attachments[i].type);
404 free(demuxer->attachments[i].data);
406 free(demuxer->attachments);
408 free(demuxer);
412 void ds_add_packet(demux_stream_t *ds, demux_packet_t *dp)
414 // append packet to DS stream:
415 ++ds->packs;
416 ds->bytes += dp->len;
417 if (ds->last) {
418 // next packet in stream
419 ds->last->next = dp;
420 ds->last = dp;
421 } else {
422 // first packet in stream
423 ds->first = ds->last = dp;
425 mp_dbg(MSGT_DEMUXER, MSGL_DBG2,
426 "DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
427 (ds == ds->demuxer->audio) ? "d_audio" : "d_video", dp->len,
428 dp->pts, (unsigned int) dp->pos, ds->demuxer->audio->packs,
429 ds->demuxer->video->packs);
432 void ds_read_packet(demux_stream_t *ds, stream_t *stream, int len,
433 double pts, off_t pos, int flags)
435 demux_packet_t *dp = new_demux_packet(len);
436 len = stream_read(stream, dp->buffer, len);
437 resize_demux_packet(dp, len);
438 dp->pts = pts;
439 dp->pos = pos;
440 dp->flags = flags;
441 // append packet to DS stream:
442 ds_add_packet(ds, dp);
445 // return value:
446 // 0 = EOF or no stream found or invalid type
447 // 1 = successfully read a packet
449 int demux_fill_buffer(demuxer_t *demux, demux_stream_t *ds)
451 // Note: parameter 'ds' can be NULL!
452 return demux->desc->fill_buffer(demux, ds);
455 // return value:
456 // 0 = EOF
457 // 1 = successful
458 #define MAX_ACUMULATED_PACKETS 64
459 int ds_fill_buffer(demux_stream_t *ds)
461 demuxer_t *demux = ds->demuxer;
462 if (ds->current)
463 free_demux_packet(ds->current);
464 ds->current = NULL;
465 if (mp_msg_test(MSGT_DEMUXER, MSGL_DBG3)) {
466 if (ds == demux->audio)
467 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
468 "ds_fill_buffer(d_audio) called\n");
469 else if (ds == demux->video)
470 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
471 "ds_fill_buffer(d_video) called\n");
472 else if (ds == demux->sub)
473 mp_dbg(MSGT_DEMUXER, MSGL_DBG3, "ds_fill_buffer(d_sub) called\n");
474 else
475 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
476 "ds_fill_buffer(unknown 0x%X) called\n", (unsigned int) ds);
478 while (1) {
479 if (ds->packs) {
480 demux_packet_t *p = ds->first;
481 #if 0
482 if (demux->reference_clock != MP_NOPTS_VALUE) {
483 if ( p->pts != MP_NOPTS_VALUE
484 && p->pts > demux->reference_clock
485 && ds->packs < MAX_ACUMULATED_PACKETS) {
486 if (demux_fill_buffer(demux, ds))
487 continue;
490 #endif
491 // copy useful data:
492 ds->buffer = p->buffer;
493 ds->buffer_pos = 0;
494 ds->buffer_size = p->len;
495 ds->pos = p->pos;
496 ds->dpos += p->len; // !!!
497 ++ds->pack_no;
498 if (p->pts != MP_NOPTS_VALUE) {
499 ds->pts = p->pts;
500 ds->pts_bytes = 0;
502 ds->pts_bytes += p->len; // !!!
503 if (p->stream_pts != MP_NOPTS_VALUE)
504 demux->stream_pts = p->stream_pts;
505 ds->flags = p->flags;
506 // unlink packet:
507 ds->bytes -= p->len;
508 ds->current = p;
509 ds->first = p->next;
510 if (!ds->first)
511 ds->last = NULL;
512 --ds->packs;
513 return 1;
515 if (demux->audio->packs >= MAX_PACKS
516 || demux->audio->bytes >= MAX_PACK_BYTES) {
517 mp_msg(MSGT_DEMUXER, MSGL_ERR, MSGTR_TooManyAudioInBuffer,
518 demux->audio->packs, demux->audio->bytes);
519 mp_msg(MSGT_DEMUXER, MSGL_HINT, MSGTR_MaybeNI);
520 break;
522 if (demux->video->packs >= MAX_PACKS
523 || demux->video->bytes >= MAX_PACK_BYTES) {
524 mp_msg(MSGT_DEMUXER, MSGL_ERR, MSGTR_TooManyVideoInBuffer,
525 demux->video->packs, demux->video->bytes);
526 mp_msg(MSGT_DEMUXER, MSGL_HINT, MSGTR_MaybeNI);
527 break;
529 if (!demux_fill_buffer(demux, ds)) {
530 mp_dbg(MSGT_DEMUXER, MSGL_DBG2,
531 "ds_fill_buffer()->demux_fill_buffer() failed\n");
532 break; // EOF
535 ds->buffer_pos = ds->buffer_size = 0;
536 ds->buffer = NULL;
537 mp_msg(MSGT_DEMUXER, MSGL_V,
538 "ds_fill_buffer: EOF reached (stream: %s) \n",
539 ds == demux->audio ? "audio" : "video");
540 ds->eof = 1;
541 return 0;
544 int demux_read_data(demux_stream_t *ds, unsigned char *mem, int len)
546 int x;
547 int bytes = 0;
548 while (len > 0) {
549 x = ds->buffer_size - ds->buffer_pos;
550 if (x == 0) {
551 if (!ds_fill_buffer(ds))
552 return bytes;
553 } else {
554 if (x > len)
555 x = len;
556 if (mem)
557 fast_memcpy(mem + bytes, &ds->buffer[ds->buffer_pos], x);
558 bytes += x;
559 len -= x;
560 ds->buffer_pos += x;
563 return bytes;
567 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
568 * \param mem memory to read data into, may be NULL to discard data
569 * \param maxlen maximum number of bytes to read
570 * \param read number of bytes actually read
571 * \param pattern pattern to search for (lowest 8 bits are ignored)
572 * \return whether pattern was found
574 int demux_pattern_3(demux_stream_t *ds, unsigned char *mem, int maxlen,
575 int *read, uint32_t pattern)
577 register uint32_t head = 0xffffff00;
578 register uint32_t pat = pattern & 0xffffff00;
579 int total_len = 0;
580 do {
581 register unsigned char *ds_buf = &ds->buffer[ds->buffer_size];
582 int len = ds->buffer_size - ds->buffer_pos;
583 register long pos = -len;
584 if (unlikely(pos >= 0)) { // buffer is empty
585 ds_fill_buffer(ds);
586 continue;
588 do {
589 head |= ds_buf[pos];
590 head <<= 8;
591 } while (++pos && head != pat);
592 len += pos;
593 if (total_len + len > maxlen)
594 len = maxlen - total_len;
595 len = demux_read_data(ds, mem ? &mem[total_len] : NULL, len);
596 total_len += len;
597 } while ((head != pat || total_len < 3) && total_len < maxlen && !ds->eof);
598 if (read)
599 *read = total_len;
600 return total_len >= 3 && head == pat;
603 void ds_free_packs(demux_stream_t *ds)
605 demux_packet_t *dp = ds->first;
606 while (dp) {
607 demux_packet_t *dn = dp->next;
608 free_demux_packet(dp);
609 dp = dn;
611 if (ds->asf_packet) {
612 // free unfinished .asf fragments:
613 free(ds->asf_packet->buffer);
614 free(ds->asf_packet);
615 ds->asf_packet = NULL;
617 ds->first = ds->last = NULL;
618 ds->packs = 0; // !!!!!
619 ds->bytes = 0;
620 if (ds->current)
621 free_demux_packet(ds->current);
622 ds->current = NULL;
623 ds->buffer = NULL;
624 ds->buffer_pos = ds->buffer_size;
625 ds->pts = 0;
626 ds->pts_bytes = 0;
629 int ds_get_packet(demux_stream_t *ds, unsigned char **start)
631 int len;
632 if (ds->buffer_pos >= ds->buffer_size) {
633 if (!ds_fill_buffer(ds)) {
634 // EOF
635 *start = NULL;
636 return -1;
639 len = ds->buffer_size - ds->buffer_pos;
640 *start = &ds->buffer[ds->buffer_pos];
641 ds->buffer_pos += len;
642 return len;
645 int ds_get_packet_pts(demux_stream_t *ds, unsigned char **start, double *pts)
647 int len;
648 *pts = MP_NOPTS_VALUE;
649 if (ds->buffer_pos >= ds->buffer_size) {
650 if (!ds_fill_buffer(ds)) {
651 // EOF
652 *start = NULL;
653 return -1;
656 // Return pts unless this read starts from the middle of a packet
657 if (!ds->buffer_pos)
658 *pts = ds->current->pts;
659 len = ds->buffer_size - ds->buffer_pos;
660 *start = &ds->buffer[ds->buffer_pos];
661 ds->buffer_pos += len;
662 return len;
665 int ds_get_packet_sub(demux_stream_t *ds, unsigned char **start)
667 int len;
668 if (ds->buffer_pos >= ds->buffer_size) {
669 *start = NULL;
670 if (!ds->packs)
671 return -1; // no sub
672 if (!ds_fill_buffer(ds))
673 return -1; // EOF
675 len = ds->buffer_size - ds->buffer_pos;
676 *start = &ds->buffer[ds->buffer_pos];
677 ds->buffer_pos += len;
678 return len;
681 double ds_get_next_pts(demux_stream_t *ds)
683 demuxer_t *demux = ds->demuxer;
684 while (!ds->first) {
685 if (demux->audio->packs >= MAX_PACKS
686 || demux->audio->bytes >= MAX_PACK_BYTES) {
687 mp_msg(MSGT_DEMUXER, MSGL_ERR, MSGTR_TooManyAudioInBuffer,
688 demux->audio->packs, demux->audio->bytes);
689 mp_msg(MSGT_DEMUXER, MSGL_HINT, MSGTR_MaybeNI);
690 return MP_NOPTS_VALUE;
692 if (demux->video->packs >= MAX_PACKS
693 || demux->video->bytes >= MAX_PACK_BYTES) {
694 mp_msg(MSGT_DEMUXER, MSGL_ERR, MSGTR_TooManyVideoInBuffer,
695 demux->video->packs, demux->video->bytes);
696 mp_msg(MSGT_DEMUXER, MSGL_HINT, MSGTR_MaybeNI);
697 return MP_NOPTS_VALUE;
699 if (!demux_fill_buffer(demux, ds))
700 return MP_NOPTS_VALUE;
702 return ds->first->pts;
705 // ====================================================================
707 void demuxer_help(void)
709 int i;
711 mp_msg(MSGT_DEMUXER, MSGL_INFO, "Available demuxers:\n");
712 mp_msg(MSGT_DEMUXER, MSGL_INFO, " demuxer: type info: (comment)\n");
713 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DEMUXERS\n");
714 for (i = 0; demuxer_list[i]; i++) {
715 if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Don't display special demuxers
716 continue;
717 if (demuxer_list[i]->comment && strlen(demuxer_list[i]->comment))
718 mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s (%s)\n",
719 demuxer_list[i]->name, demuxer_list[i]->type,
720 demuxer_list[i]->info, demuxer_list[i]->comment);
721 else
722 mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s\n",
723 demuxer_list[i]->name, demuxer_list[i]->type,
724 demuxer_list[i]->info);
730 * Get demuxer type for a given demuxer name
732 * @param demuxer_name string with demuxer name of demuxer number
733 * @param force will be set if demuxer should be forced.
734 * May be NULL.
735 * @return DEMUXER_TYPE_xxx, -1 if error or not found
737 int get_demuxer_type_from_name(char *demuxer_name, int *force)
739 int i;
740 long type_int;
741 char *endptr;
743 if (!demuxer_name || !demuxer_name[0])
744 return DEMUXER_TYPE_UNKNOWN;
745 if (force)
746 *force = demuxer_name[0] == '+';
747 if (demuxer_name[0] == '+')
748 demuxer_name = &demuxer_name[1];
749 for (i = 0; demuxer_list[i]; i++) {
750 if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Can't select special demuxers from commandline
751 continue;
752 if (strcmp(demuxer_name, demuxer_list[i]->name) == 0)
753 return demuxer_list[i]->type;
756 // No match found, try to parse name as an integer (demuxer number)
757 type_int = strtol(demuxer_name, &endptr, 0);
758 if (*endptr) // Conversion failed
759 return -1;
760 if ((type_int > 0) && (type_int <= DEMUXER_TYPE_MAX))
761 return (int) type_int;
763 return -1;
766 int extension_parsing = 1; // 0=off 1=mixed (used only for unstable formats)
768 int correct_pts = 0;
769 int user_correct_pts = -1;
772 NOTE : Several demuxers may be opened at the same time so
773 demuxers should NEVER rely on an external var to enable them
774 self. If a demuxer can't do any auto-detection it should only use
775 file_format. The user can explicitly set file_format with the -demuxer
776 option so there is really no need for another extra var.
777 For convenience an option can be added to set file_format directly
778 to the right type (ex: rawaudio,rawvideo).
779 Also the stream can override the file_format so a demuxer which rely
780 on a special stream type can set file_format at the stream level
781 (ex: tv,mf).
784 static demuxer_t *demux_open_stream(stream_t *stream, int file_format,
785 int force, int audio_id, int video_id,
786 int dvdsub_id, char *filename)
788 demuxer_t *demuxer = NULL;
790 sh_video_t *sh_video = NULL;
792 const demuxer_desc_t *demuxer_desc;
793 int fformat = 0;
794 int i;
796 // If somebody requested a demuxer check it
797 if (file_format) {
798 if ((demuxer_desc = get_demuxer_desc_from_type(file_format))) {
799 demuxer = new_demuxer(stream, demuxer_desc->type, audio_id,
800 video_id, dvdsub_id, filename);
801 if (demuxer_desc->check_file)
802 fformat = demuxer_desc->check_file(demuxer);
803 if (force || !demuxer_desc->check_file)
804 fformat = demuxer_desc->type;
805 if (fformat != 0) {
806 if (fformat == demuxer_desc->type) {
807 demuxer_t *demux2 = demuxer;
808 // Move messages to demuxer detection code?
809 mp_msg(MSGT_DEMUXER, MSGL_INFO,
810 MSGTR_Detected_XXX_FileFormat,
811 demuxer_desc->shortdesc);
812 file_format = fformat;
813 if (!demuxer->desc->open
814 || (demux2 = demuxer->desc->open(demuxer))) {
815 demuxer = demux2;
816 goto dmx_open;
818 } else {
819 // Format changed after check, recurse
820 free_demuxer(demuxer);
821 return demux_open_stream(stream, fformat, force, audio_id,
822 video_id, dvdsub_id, filename);
825 // Check failed for forced demuxer, quit
826 free_demuxer(demuxer);
827 return NULL;
830 // Test demuxers with safe file checks
831 for (i = 0; (demuxer_desc = demuxer_list[i]); i++) {
832 if (demuxer_desc->safe_check) {
833 demuxer = new_demuxer(stream, demuxer_desc->type, audio_id,
834 video_id, dvdsub_id, filename);
835 if ((fformat = demuxer_desc->check_file(demuxer)) != 0) {
836 if (fformat == demuxer_desc->type) {
837 demuxer_t *demux2 = demuxer;
838 mp_msg(MSGT_DEMUXER, MSGL_INFO,
839 MSGTR_Detected_XXX_FileFormat,
840 demuxer_desc->shortdesc);
841 file_format = fformat;
842 if (!demuxer->desc->open
843 || (demux2 = demuxer->desc->open(demuxer))) {
844 demuxer = demux2;
845 goto dmx_open;
847 } else {
848 if (fformat == DEMUXER_TYPE_PLAYLIST)
849 return demuxer; // handled in mplayer.c
850 // Format changed after check, recurse
851 free_demuxer(demuxer);
852 demuxer = demux_open_stream(stream, fformat, force,
853 audio_id, video_id,
854 dvdsub_id, filename);
855 if (demuxer)
856 return demuxer; // done!
857 file_format = DEMUXER_TYPE_UNKNOWN;
860 free_demuxer(demuxer);
861 demuxer = NULL;
865 // If no forced demuxer perform file extension based detection
866 // Ok. We're over the stable detectable fileformats, the next ones are
867 // a bit fuzzy. So by default (extension_parsing==1) try extension-based
868 // detection first:
869 if (file_format == DEMUXER_TYPE_UNKNOWN && filename
870 && extension_parsing == 1) {
871 file_format = demuxer_type_by_filename(filename);
872 if (file_format != DEMUXER_TYPE_UNKNOWN) {
873 // we like recursion :)
874 demuxer = demux_open_stream(stream, file_format, force, audio_id,
875 video_id, dvdsub_id, filename);
876 if (demuxer)
877 return demuxer; // done!
878 file_format = DEMUXER_TYPE_UNKNOWN; // continue fuzzy guessing...
879 mp_msg(MSGT_DEMUXER, MSGL_V,
880 "demuxer: continue fuzzy content-based format guessing...\n");
883 // Try detection for all other demuxers
884 for (i = 0; (demuxer_desc = demuxer_list[i]); i++) {
885 if (!demuxer_desc->safe_check && demuxer_desc->check_file) {
886 demuxer = new_demuxer(stream, demuxer_desc->type, audio_id,
887 video_id, dvdsub_id, filename);
888 if ((fformat = demuxer_desc->check_file(demuxer)) != 0) {
889 if (fformat == demuxer_desc->type) {
890 demuxer_t *demux2 = demuxer;
891 mp_msg(MSGT_DEMUXER, MSGL_INFO,
892 MSGTR_Detected_XXX_FileFormat,
893 demuxer_desc->shortdesc);
894 file_format = fformat;
895 if (!demuxer->desc->open
896 || (demux2 = demuxer->desc->open(demuxer))) {
897 demuxer = demux2;
898 goto dmx_open;
900 } else {
901 if (fformat == DEMUXER_TYPE_PLAYLIST)
902 return demuxer; // handled in mplayer.c
903 // Format changed after check, recurse
904 free_demuxer(demuxer);
905 demuxer = demux_open_stream(stream, fformat, force,
906 audio_id, video_id,
907 dvdsub_id, filename);
908 if (demuxer)
909 return demuxer; // done!
910 file_format = DEMUXER_TYPE_UNKNOWN;
913 free_demuxer(demuxer);
914 demuxer = NULL;
918 return NULL;
919 //====== File format recognized, set up these for compatibility: =========
920 dmx_open:
922 demuxer->file_format = file_format;
924 if ((sh_video = demuxer->video->sh) && sh_video->bih) {
925 int biComp = le2me_32(sh_video->bih->biCompression);
926 mp_msg(MSGT_DEMUX, MSGL_INFO,
927 "VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
928 (char *) &biComp, sh_video->bih->biWidth,
929 sh_video->bih->biHeight, sh_video->bih->biBitCount,
930 sh_video->fps, sh_video->i_bps * 0.008f,
931 sh_video->i_bps / 1024.0f);
933 #ifdef CONFIG_ASS
934 if (ass_enabled && ass_library) {
935 for (i = 0; i < MAX_S_STREAMS; ++i) {
936 sh_sub_t *sh = demuxer->s_streams[i];
937 if (sh && sh->type == 'a') {
938 sh->ass_track = ass_new_track(ass_library);
939 if (sh->ass_track && sh->extradata)
940 ass_process_codec_private(sh->ass_track, sh->extradata,
941 sh->extradata_len);
942 } else if (sh && sh->type != 'v')
943 sh->ass_track = ass_default_track(ass_library);
946 #endif
947 return demuxer;
950 char *audio_stream = NULL;
951 char *sub_stream = NULL;
952 int audio_stream_cache = 0;
954 char *demuxer_name = NULL; // parameter from -demuxer
955 char *audio_demuxer_name = NULL; // parameter from -audio-demuxer
956 char *sub_demuxer_name = NULL; // parameter from -sub-demuxer
958 extern int hr_mp3_seek;
960 extern float stream_cache_min_percent;
961 extern float stream_cache_seek_min_percent;
963 demuxer_t *demux_open(stream_t *vs, int file_format, int audio_id,
964 int video_id, int dvdsub_id, char *filename)
966 stream_t *as = NULL, *ss = NULL;
967 demuxer_t *vd, *ad = NULL, *sd = NULL;
968 demuxer_t *res;
969 int afmt = DEMUXER_TYPE_UNKNOWN, sfmt = DEMUXER_TYPE_UNKNOWN;
970 int demuxer_type;
971 int audio_demuxer_type = 0, sub_demuxer_type = 0;
972 int demuxer_force = 0, audio_demuxer_force = 0, sub_demuxer_force = 0;
974 if ((demuxer_type =
975 get_demuxer_type_from_name(demuxer_name, &demuxer_force)) < 0) {
976 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-demuxer %s does not exist.\n",
977 demuxer_name);
979 if ((audio_demuxer_type =
980 get_demuxer_type_from_name(audio_demuxer_name,
981 &audio_demuxer_force)) < 0) {
982 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-audio-demuxer %s does not exist.\n",
983 audio_demuxer_name);
985 if ((sub_demuxer_type =
986 get_demuxer_type_from_name(sub_demuxer_name,
987 &sub_demuxer_force)) < 0) {
988 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-sub-demuxer %s does not exist.\n",
989 sub_demuxer_name);
992 if (audio_stream) {
993 as = open_stream(audio_stream, 0, &afmt);
994 if (!as) {
995 mp_msg(MSGT_DEMUXER, MSGL_ERR, MSGTR_CannotOpenAudioStream,
996 audio_stream);
997 return NULL;
999 if (audio_stream_cache) {
1000 if (!stream_enable_cache
1001 (as, audio_stream_cache * 1024,
1002 audio_stream_cache * 1024 * (stream_cache_min_percent /
1003 100.0),
1004 audio_stream_cache * 1024 * (stream_cache_seek_min_percent /
1005 100.0))) {
1006 free_stream(as);
1007 mp_msg(MSGT_DEMUXER, MSGL_ERR,
1008 "Can't enable audio stream cache\n");
1009 return NULL;
1013 if (sub_stream) {
1014 ss = open_stream(sub_stream, 0, &sfmt);
1015 if (!ss) {
1016 mp_msg(MSGT_DEMUXER, MSGL_ERR, MSGTR_CannotOpenSubtitlesStream,
1017 sub_stream);
1018 return NULL;
1022 vd = demux_open_stream(vs, demuxer_type ? demuxer_type : file_format,
1023 demuxer_force, audio_stream ? -2 : audio_id,
1024 video_id, sub_stream ? -2 : dvdsub_id, filename);
1025 if (!vd) {
1026 if (as)
1027 free_stream(as);
1028 if (ss)
1029 free_stream(ss);
1030 return NULL;
1032 if (as) {
1033 ad = demux_open_stream(as,
1034 audio_demuxer_type ? audio_demuxer_type : afmt,
1035 audio_demuxer_force, audio_id, -2, -2,
1036 audio_stream);
1037 if (!ad) {
1038 mp_msg(MSGT_DEMUXER, MSGL_WARN, MSGTR_OpeningAudioDemuxerFailed,
1039 audio_stream);
1040 free_stream(as);
1041 } else if (ad->audio->sh
1042 && ((sh_audio_t *) ad->audio->sh)->format == 0x55) // MP3
1043 hr_mp3_seek = 1; // Enable high res seeking
1045 if (ss) {
1046 sd = demux_open_stream(ss, sub_demuxer_type ? sub_demuxer_type : sfmt,
1047 sub_demuxer_force, -2, -2, dvdsub_id,
1048 sub_stream);
1049 if (!sd) {
1050 mp_msg(MSGT_DEMUXER, MSGL_WARN,
1051 MSGTR_OpeningSubtitlesDemuxerFailed, sub_stream);
1052 free_stream(ss);
1056 if (ad && sd)
1057 res = new_demuxers_demuxer(vd, ad, sd);
1058 else if (ad)
1059 res = new_demuxers_demuxer(vd, ad, vd);
1060 else if (sd)
1061 res = new_demuxers_demuxer(vd, vd, sd);
1062 else
1063 res = vd;
1065 correct_pts = user_correct_pts;
1066 if (correct_pts < 0)
1067 correct_pts = demux_control(res, DEMUXER_CTRL_CORRECT_PTS, NULL)
1068 == DEMUXER_CTRL_OK;
1069 return res;
1073 void demux_flush(demuxer_t *demuxer)
1075 ds_free_packs(demuxer->video);
1076 ds_free_packs(demuxer->audio);
1077 ds_free_packs(demuxer->sub);
1080 int demux_seek(demuxer_t *demuxer, float rel_seek_secs, float audio_delay,
1081 int flags)
1083 demux_stream_t *d_audio = demuxer->audio;
1084 demux_stream_t *d_video = demuxer->video;
1085 sh_audio_t *sh_audio = d_audio->sh;
1086 sh_video_t *sh_video = d_video->sh;
1087 double tmp = 0;
1088 double pts;
1090 if (!demuxer->seekable) {
1091 if (demuxer->file_format == DEMUXER_TYPE_AVI)
1092 mp_msg(MSGT_SEEK, MSGL_WARN, MSGTR_CantSeekRawAVI);
1093 #ifdef CONFIG_TV
1094 else if (demuxer->file_format == DEMUXER_TYPE_TV)
1095 mp_msg(MSGT_SEEK, MSGL_WARN, MSGTR_TVInputNotSeekable);
1096 #endif
1097 else
1098 mp_msg(MSGT_SEEK, MSGL_WARN, MSGTR_CantSeekFile);
1099 return 0;
1102 demux_flush(demuxer);
1103 // clear demux buffers:
1104 if (sh_audio)
1105 sh_audio->a_buffer_len = 0;
1107 demuxer->stream->eof = 0;
1108 demuxer->video->eof = 0;
1109 demuxer->audio->eof = 0;
1111 if (sh_video)
1112 sh_video->timer = 0; // !!!!!!
1114 if (flags & SEEK_ABSOLUTE)
1115 pts = 0.0f;
1116 else {
1117 if (demuxer->stream_pts == MP_NOPTS_VALUE)
1118 goto dmx_seek;
1119 pts = demuxer->stream_pts;
1122 if (flags & SEEK_FACTOR) {
1123 if (stream_control(demuxer->stream, STREAM_CTRL_GET_TIME_LENGTH, &tmp)
1124 == STREAM_UNSUPPORTED)
1125 goto dmx_seek;
1126 pts += tmp * rel_seek_secs;
1127 } else
1128 pts += rel_seek_secs;
1130 if (stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_TIME, &pts) !=
1131 STREAM_UNSUPPORTED) {
1132 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1133 return 1;
1136 dmx_seek:
1137 if (demuxer->desc->seek)
1138 demuxer->desc->seek(demuxer, rel_seek_secs, audio_delay, flags);
1140 if (sh_audio)
1141 resync_audio_stream(sh_audio);
1143 return 1;
1146 int demux_info_add(demuxer_t *demuxer, const char *opt, const char *param)
1148 char **info = demuxer->info;
1149 int n = 0;
1152 for (n = 0; info && info[2 * n] != NULL; n++) {
1153 if (!strcasecmp(opt, info[2 * n])) {
1154 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_DemuxerInfoChanged, opt,
1155 param);
1156 free(info[2 * n + 1]);
1157 info[2 * n + 1] = strdup(param);
1158 return 0;
1162 info = demuxer->info = (char **) realloc(info,
1163 (2 * (n + 2)) * sizeof(char *));
1164 info[2 * n] = strdup(opt);
1165 info[2 * n + 1] = strdup(param);
1166 memset(&info[2 * (n + 1)], 0, 2 * sizeof(char *));
1168 return 1;
1171 int demux_info_print(demuxer_t *demuxer)
1173 char **info = demuxer->info;
1174 int n;
1176 if (!info)
1177 return 0;
1179 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_ClipInfo);
1180 for (n = 0; info[2 * n] != NULL; n++) {
1181 mp_msg(MSGT_DEMUX, MSGL_INFO, " %s: %s\n", info[2 * n],
1182 info[2 * n + 1]);
1183 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_NAME%d=%s\n", n,
1184 info[2 * n]);
1185 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_VALUE%d=%s\n", n,
1186 info[2 * n + 1]);
1188 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_N=%d\n", n);
1190 return 0;
1193 char *demux_info_get(demuxer_t *demuxer, const char *opt)
1195 int i;
1196 char **info = demuxer->info;
1198 for (i = 0; info && info[2 * i] != NULL; i++) {
1199 if (!strcasecmp(opt, info[2 * i]))
1200 return info[2 * i + 1];
1203 return NULL;
1206 int demux_control(demuxer_t *demuxer, int cmd, void *arg)
1209 if (demuxer->desc->control)
1210 return demuxer->desc->control(demuxer, cmd, arg);
1212 return DEMUXER_CTRL_NOTIMPL;
1217 double demuxer_get_time_length(demuxer_t *demuxer)
1219 double get_time_ans;
1220 sh_video_t *sh_video = demuxer->video->sh;
1221 sh_audio_t *sh_audio = demuxer->audio->sh;
1222 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
1223 if (demux_control
1224 (demuxer, DEMUXER_CTRL_GET_TIME_LENGTH, (void *) &get_time_ans) <= 0) {
1225 if (sh_video && sh_video->i_bps && sh_audio && sh_audio->i_bps)
1226 get_time_ans = (double) (demuxer->movi_end -
1227 demuxer->movi_start) / (sh_video->i_bps +
1228 sh_audio->i_bps);
1229 else if (sh_video && sh_video->i_bps)
1230 get_time_ans = (double) (demuxer->movi_end -
1231 demuxer->movi_start) / sh_video->i_bps;
1232 else if (sh_audio && sh_audio->i_bps)
1233 get_time_ans = (double) (demuxer->movi_end -
1234 demuxer->movi_start) / sh_audio->i_bps;
1235 else
1236 get_time_ans = 0;
1238 return get_time_ans;
1242 * \brief demuxer_get_current_time() returns the time of the current play in three possible ways:
1243 * either when the stream reader satisfies STREAM_CTRL_GET_CURRENT_TIME (e.g. dvd)
1244 * or using sh_video->pts when the former method fails
1245 * 0 otherwise
1246 * \return the current play time
1248 int demuxer_get_current_time(demuxer_t *demuxer)
1250 double get_time_ans = 0;
1251 sh_video_t *sh_video = demuxer->video->sh;
1252 if (demuxer->stream_pts != MP_NOPTS_VALUE)
1253 get_time_ans = demuxer->stream_pts;
1254 else if (sh_video)
1255 get_time_ans = sh_video->pts;
1256 return (int) get_time_ans;
1259 int demuxer_get_percent_pos(demuxer_t *demuxer)
1261 int ans = 0;
1262 int res = demux_control(demuxer, DEMUXER_CTRL_GET_PERCENT_POS, &ans);
1263 int len = (demuxer->movi_end - demuxer->movi_start) / 100;
1264 if (res <= 0) {
1265 if (len > 0)
1266 ans = (demuxer->filepos - demuxer->movi_start) / len;
1267 else
1268 ans = 0;
1270 if (ans < 0)
1271 ans = 0;
1272 if (ans > 100)
1273 ans = 100;
1274 return ans;
1277 int demuxer_switch_audio(demuxer_t *demuxer, int index)
1279 int res = demux_control(demuxer, DEMUXER_CTRL_SWITCH_AUDIO, &index);
1280 if (res == DEMUXER_CTRL_NOTIMPL)
1281 index = demuxer->audio->id;
1282 if (demuxer->audio->id >= 0)
1283 demuxer->audio->sh = demuxer->a_streams[demuxer->audio->id];
1284 else
1285 demuxer->audio->sh = NULL;
1286 return index;
1289 int demuxer_switch_video(demuxer_t *demuxer, int index)
1291 int res = demux_control(demuxer, DEMUXER_CTRL_SWITCH_VIDEO, &index);
1292 if (res == DEMUXER_CTRL_NOTIMPL)
1293 index = demuxer->video->id;
1294 if (demuxer->video->id >= 0)
1295 demuxer->video->sh = demuxer->v_streams[demuxer->video->id];
1296 else
1297 demuxer->video->sh = NULL;
1298 return index;
1301 int demuxer_add_attachment(demuxer_t *demuxer, const char *name,
1302 const char *type, const void *data, size_t size)
1304 if (!(demuxer->num_attachments & 31))
1305 demuxer->attachments = realloc(demuxer->attachments,
1306 (demuxer->num_attachments + 32) * sizeof(demux_attachment_t));
1308 demuxer->attachments[demuxer->num_attachments].name = strdup(name);
1309 demuxer->attachments[demuxer->num_attachments].type = strdup(type);
1310 demuxer->attachments[demuxer->num_attachments].data = malloc(size);
1311 memcpy(demuxer->attachments[demuxer->num_attachments].data, data, size);
1312 demuxer->attachments[demuxer->num_attachments].data_size = size;
1314 return demuxer->num_attachments++;
1317 int demuxer_add_chapter(demuxer_t *demuxer, const char *name, uint64_t start,
1318 uint64_t end)
1320 if (demuxer->chapters == NULL)
1321 demuxer->chapters = malloc(32 * sizeof(*demuxer->chapters));
1322 else if (!(demuxer->num_chapters % 32))
1323 demuxer->chapters = realloc(demuxer->chapters,
1324 (demuxer->num_chapters + 32) *
1325 sizeof(*demuxer->chapters));
1327 demuxer->chapters[demuxer->num_chapters].start = start;
1328 demuxer->chapters[demuxer->num_chapters].end = end;
1329 demuxer->chapters[demuxer->num_chapters].name = strdup(name ? name : MSGTR_Unknown);
1331 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_ID=%d\n", demuxer->num_chapters);
1332 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_START=%"PRIu64"\n", demuxer->num_chapters, start);
1333 if (end)
1334 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_END=%"PRIu64"\n", demuxer->num_chapters, end);
1335 if (name)
1336 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CHAPTER_%d_NAME=%s\n", demuxer->num_chapters, name);
1338 return demuxer->num_chapters++;
1342 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1343 * either using the demuxer->chapters structure set by the demuxer
1344 * or asking help to the stream layer (e.g. dvd)
1345 * \param chapter - chapter number wished - 0-based
1346 * \param mode 0: relative to current main pts, 1: absolute
1347 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1348 * \param num_chapters number of chapters present (set by this function is param is not null)
1349 * \param chapter_name name of chapter found (set by this function is param is not null)
1350 * \return -1 on error, current chapter if successful
1353 int demuxer_seek_chapter(demuxer_t *demuxer, int chapter, int mode,
1354 float *seek_pts, int *num_chapters,
1355 char **chapter_name)
1357 int ris;
1358 int current, total;
1359 sh_video_t *sh_video = demuxer->video->sh;
1360 sh_audio_t *sh_audio = demuxer->audio->sh;
1362 if (!demuxer->num_chapters || !demuxer->chapters) {
1363 if (!mode) {
1364 ris = stream_control(demuxer->stream,
1365 STREAM_CTRL_GET_CURRENT_CHAPTER, &current);
1366 if (ris == STREAM_UNSUPPORTED)
1367 return -1;
1368 chapter += current;
1371 demux_flush(demuxer);
1373 ris = stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_CHAPTER,
1374 &chapter);
1375 if (ris != STREAM_UNSUPPORTED)
1376 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1377 if (sh_video) {
1378 ds_fill_buffer(demuxer->video);
1379 resync_video_stream(sh_video);
1382 if (sh_audio) {
1383 ds_fill_buffer(demuxer->audio);
1384 resync_audio_stream(sh_audio);
1386 // exit status may be ok, but main() doesn't have to seek itself
1387 // (because e.g. dvds depend on sectors, not on pts)
1388 *seek_pts = -1.0;
1390 if (num_chapters) {
1391 if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS,
1392 num_chapters) == STREAM_UNSUPPORTED)
1393 *num_chapters = 0;
1396 if (chapter_name) {
1397 *chapter_name = NULL;
1398 if (num_chapters && *num_chapters) {
1399 char *tmp = malloc(16);
1400 if (tmp) {
1401 sprintf(tmp, " of %3d", *num_chapters);
1402 *chapter_name = tmp;
1407 return ris != STREAM_UNSUPPORTED ? chapter : -1;
1408 } else { // chapters structure is set in the demuxer
1409 total = demuxer->num_chapters;
1411 if (mode == 1) //absolute seeking
1412 current = chapter;
1413 else { //relative seeking
1414 uint64_t now;
1415 now = (sh_video ? sh_video->pts : (sh_audio ? sh_audio->pts : 0.))
1416 * 1000 + .5;
1418 for (current = total - 1; current >= 0; --current) {
1419 demux_chapter_t *chapter = demuxer->chapters + current;
1420 if (chapter->start <= now)
1421 break;
1423 current += chapter;
1426 if (current >= total)
1427 return -1;
1428 if (current < 0)
1429 current = 0;
1431 *seek_pts = demuxer->chapters[current].start / 1000.0;
1433 if (num_chapters)
1434 *num_chapters = demuxer->num_chapters;
1436 if (chapter_name) {
1437 if (demuxer->chapters[current].name)
1438 *chapter_name = strdup(demuxer->chapters[current].name);
1439 else
1440 *chapter_name = NULL;
1443 return current;
1447 int demuxer_get_current_chapter(demuxer_t *demuxer)
1449 int chapter = -1;
1450 if (!demuxer->num_chapters || !demuxer->chapters) {
1451 if (stream_control(demuxer->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
1452 &chapter) == STREAM_UNSUPPORTED)
1453 chapter = -1;
1454 } else {
1455 sh_video_t *sh_video = demuxer->video->sh;
1456 sh_audio_t *sh_audio = demuxer->audio->sh;
1457 uint64_t now;
1458 now = (sh_video ? sh_video->pts : (sh_audio ? sh_audio->pts : 0))
1459 * 1000 + 0.5;
1460 for (chapter = demuxer->num_chapters - 1; chapter >= 0; --chapter) {
1461 if (demuxer->chapters[chapter].start <= now)
1462 break;
1465 return chapter;
1468 char *demuxer_chapter_name(demuxer_t *demuxer, int chapter)
1470 if (demuxer->num_chapters && demuxer->chapters) {
1471 if (chapter >= 0 && chapter < demuxer->num_chapters
1472 && demuxer->chapters[chapter].name)
1473 return strdup(demuxer->chapters[chapter].name);
1475 return NULL;
1478 char *demuxer_chapter_display_name(demuxer_t *demuxer, int chapter)
1480 char *chapter_name = demuxer_chapter_name(demuxer, chapter);
1481 if (chapter_name) {
1482 char *tmp = malloc(strlen(chapter_name) + 14);
1483 snprintf(tmp, 63, "(%d) %s", chapter + 1, chapter_name);
1484 free(chapter_name);
1485 return tmp;
1486 } else {
1487 int chapter_num = demuxer_chapter_count(demuxer);
1488 char tmp[30];
1489 if (chapter_num <= 0)
1490 sprintf(tmp, "(%d)", chapter + 1);
1491 else
1492 sprintf(tmp, "(%d) of %d", chapter + 1, chapter_num);
1493 return strdup(tmp);
1497 float demuxer_chapter_time(demuxer_t *demuxer, int chapter, float *end)
1499 if (demuxer->num_chapters && demuxer->chapters && chapter >= 0
1500 && chapter < demuxer->num_chapters) {
1501 if (end)
1502 *end = demuxer->chapters[chapter].end / 1000.0;
1503 return demuxer->chapters[chapter].start / 1000.0;
1505 return -1.0;
1508 int demuxer_chapter_count(demuxer_t *demuxer)
1510 if (!demuxer->num_chapters || !demuxer->chapters) {
1511 int num_chapters = 0;
1512 if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS,
1513 &num_chapters) == STREAM_UNSUPPORTED)
1514 num_chapters = 0;
1515 return num_chapters;
1516 } else
1517 return demuxer->num_chapters;
1520 int demuxer_angles_count(demuxer_t *demuxer)
1522 int ris, angles = -1;
1524 ris = stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_ANGLES, &angles);
1525 if (ris == STREAM_UNSUPPORTED)
1526 return -1;
1527 return angles;
1530 int demuxer_get_current_angle(demuxer_t *demuxer)
1532 int ris, curr_angle = -1;
1533 ris = stream_control(demuxer->stream, STREAM_CTRL_GET_ANGLE, &curr_angle);
1534 if (ris == STREAM_UNSUPPORTED)
1535 return -1;
1536 return curr_angle;
1540 int demuxer_set_angle(demuxer_t *demuxer, int angle)
1542 int ris, angles = -1;
1543 sh_video_t *sh_video = demuxer->video->sh;
1544 sh_audio_t *sh_audio = demuxer->audio->sh;
1546 angles = demuxer_angles_count(demuxer);
1547 if ((angles < 1) || (angle > angles))
1548 return -1;
1550 demux_flush(demuxer);
1552 ris = stream_control(demuxer->stream, STREAM_CTRL_SET_ANGLE, &angle);
1553 if (ris == STREAM_UNSUPPORTED)
1554 return -1;
1556 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1557 if (sh_video) {
1558 ds_fill_buffer(demuxer->video);
1559 resync_video_stream(sh_video);
1562 if (sh_audio) {
1563 ds_fill_buffer(demuxer->audio);
1564 resync_audio_stream(sh_audio);
1567 return angle;
1570 int demuxer_audio_track_by_lang(demuxer_t *d, char *lang)
1572 int i, len;
1573 lang += strspn(lang, ",");
1574 while ((len = strcspn(lang, ",")) > 0) {
1575 for (i = 0; i < MAX_A_STREAMS; ++i) {
1576 sh_audio_t *sh = d->a_streams[i];
1577 if (sh && sh->lang && strncmp(sh->lang, lang, len) == 0)
1578 return sh->aid;
1580 lang += len;
1581 lang += strspn(lang, ",");
1583 return -1;
1586 int demuxer_sub_track_by_lang(demuxer_t *d, char *lang)
1588 int i, len;
1589 lang += strspn(lang, ",");
1590 while ((len = strcspn(lang, ",")) > 0) {
1591 for (i = 0; i < MAX_S_STREAMS; ++i) {
1592 sh_sub_t *sh = d->s_streams[i];
1593 if (sh && sh->lang && strncmp(sh->lang, lang, len) == 0)
1594 return sh->sid;
1596 lang += len;
1597 lang += strspn(lang, ",");
1599 return -1;
1602 int demuxer_default_audio_track(demuxer_t *d)
1604 int i;
1605 for (i = 0; i < MAX_A_STREAMS; ++i) {
1606 sh_audio_t *sh = d->a_streams[i];
1607 if (sh && sh->default_track)
1608 return sh->aid;
1610 for (i = 0; i < MAX_A_STREAMS; ++i) {
1611 sh_audio_t *sh = d->a_streams[i];
1612 if (sh)
1613 return sh->aid;
1615 return -1;
1618 int demuxer_default_sub_track(demuxer_t *d)
1620 int i;
1621 for (i = 0; i < MAX_S_STREAMS; ++i) {
1622 sh_sub_t *sh = d->s_streams[i];
1623 if (sh && sh->default_track)
1624 return sh->sid;
1626 return -1;