whitespace cosmetics:
[mplayer/glamo.git] / libmpdemux / demuxer.c
blob74bbd8e5597bccd285eee5fe1de7051458169b0e
1 //=================== DEMUXER v2.5 =========================
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
11 #include "config.h"
12 #include "mp_msg.h"
13 #include "help_mp.h"
14 #include "m_config.h"
16 #include "libvo/fastmemcpy.h"
18 #include "stream/stream.h"
19 #include "demuxer.h"
20 #include "stheader.h"
21 #include "mf.h"
23 #include "libaf/af_format.h"
25 #ifdef CONFIG_ASS
26 #include "libass/ass.h"
27 #include "libass/ass_mp.h"
28 #endif
30 #ifdef CONFIG_LIBAVCODEC
31 #include "libavcodec/avcodec.h"
32 #if MP_INPUT_BUFFER_PADDING_SIZE < FF_INPUT_BUFFER_PADDING_SIZE
33 #error MP_INPUT_BUFFER_PADDING_SIZE is too small!
34 #endif
35 #endif
37 void resync_video_stream(sh_video_t *sh_video);
38 void resync_audio_stream(sh_audio_t *sh_audio);
40 // Demuxer list
41 extern const demuxer_desc_t demuxer_desc_rawaudio;
42 extern const demuxer_desc_t demuxer_desc_rawvideo;
43 extern const demuxer_desc_t demuxer_desc_tv;
44 extern const demuxer_desc_t demuxer_desc_mf;
45 extern const demuxer_desc_t demuxer_desc_avi;
46 extern const demuxer_desc_t demuxer_desc_y4m;
47 extern const demuxer_desc_t demuxer_desc_asf;
48 extern const demuxer_desc_t demuxer_desc_nuv;
49 extern const demuxer_desc_t demuxer_desc_real;
50 extern const demuxer_desc_t demuxer_desc_smjpeg;
51 extern const demuxer_desc_t demuxer_desc_matroska;
52 extern const demuxer_desc_t demuxer_desc_realaudio;
53 extern const demuxer_desc_t demuxer_desc_vqf;
54 extern const demuxer_desc_t demuxer_desc_mov;
55 extern const demuxer_desc_t demuxer_desc_vivo;
56 extern const demuxer_desc_t demuxer_desc_fli;
57 extern const demuxer_desc_t demuxer_desc_film;
58 extern const demuxer_desc_t demuxer_desc_roq;
59 extern const demuxer_desc_t demuxer_desc_gif;
60 extern const demuxer_desc_t demuxer_desc_ogg;
61 extern const demuxer_desc_t demuxer_desc_avs;
62 extern const demuxer_desc_t demuxer_desc_pva;
63 extern const demuxer_desc_t demuxer_desc_nsv;
64 extern const demuxer_desc_t demuxer_desc_mpeg_ts;
65 extern const demuxer_desc_t demuxer_desc_lmlm4;
66 extern const demuxer_desc_t demuxer_desc_mpeg_ps;
67 extern const demuxer_desc_t demuxer_desc_mpeg_pes;
68 extern const demuxer_desc_t demuxer_desc_mpeg_es;
69 extern const demuxer_desc_t demuxer_desc_mpeg_gxf;
70 extern const demuxer_desc_t demuxer_desc_mpeg4_es;
71 extern const demuxer_desc_t demuxer_desc_h264_es;
72 extern const demuxer_desc_t demuxer_desc_rawdv;
73 extern const demuxer_desc_t demuxer_desc_mpc;
74 extern const demuxer_desc_t demuxer_desc_audio;
75 extern const demuxer_desc_t demuxer_desc_xmms;
76 extern const demuxer_desc_t demuxer_desc_mpeg_ty;
77 extern const demuxer_desc_t demuxer_desc_rtp;
78 extern const demuxer_desc_t demuxer_desc_rtp_nemesi;
79 extern const demuxer_desc_t demuxer_desc_lavf;
80 extern const demuxer_desc_t demuxer_desc_lavf_preferred;
81 extern const demuxer_desc_t demuxer_desc_aac;
82 extern const demuxer_desc_t demuxer_desc_nut;
83 extern const demuxer_desc_t demuxer_desc_mng;
85 /* Please do not add any new demuxers here. If you want to implement a new
86 * demuxer, add it to libavformat, except for wrappers around external
87 * libraries and demuxers requiring binary support. */
89 const demuxer_desc_t *const demuxer_list[] = {
90 &demuxer_desc_rawaudio,
91 &demuxer_desc_rawvideo,
92 #ifdef CONFIG_TV
93 &demuxer_desc_tv,
94 #endif
95 &demuxer_desc_mf,
96 #ifdef CONFIG_LIBAVFORMAT
97 &demuxer_desc_lavf_preferred,
98 #endif
99 &demuxer_desc_avi,
100 &demuxer_desc_y4m,
101 &demuxer_desc_asf,
102 &demuxer_desc_nsv,
103 &demuxer_desc_nuv,
104 &demuxer_desc_real,
105 &demuxer_desc_smjpeg,
106 &demuxer_desc_matroska,
107 &demuxer_desc_realaudio,
108 &demuxer_desc_vqf,
109 &demuxer_desc_mov,
110 &demuxer_desc_vivo,
111 &demuxer_desc_fli,
112 &demuxer_desc_film,
113 &demuxer_desc_roq,
114 #ifdef CONFIG_GIF
115 &demuxer_desc_gif,
116 #endif
117 #ifdef CONFIG_OGGVORBIS
118 &demuxer_desc_ogg,
119 #endif
120 #ifdef CONFIG_WIN32DLL
121 &demuxer_desc_avs,
122 #endif
123 &demuxer_desc_pva,
124 &demuxer_desc_mpeg_ts,
125 &demuxer_desc_lmlm4,
126 &demuxer_desc_mpeg_ps,
127 &demuxer_desc_mpeg_pes,
128 &demuxer_desc_mpeg_es,
129 &demuxer_desc_mpeg_gxf,
130 &demuxer_desc_mpeg4_es,
131 &demuxer_desc_h264_es,
132 &demuxer_desc_audio,
133 &demuxer_desc_mpeg_ty,
134 #ifdef CONFIG_LIVE555
135 &demuxer_desc_rtp,
136 #endif
137 #ifdef CONFIG_LIBNEMESI
138 &demuxer_desc_rtp_nemesi,
139 #endif
140 #ifdef CONFIG_LIBAVFORMAT
141 &demuxer_desc_lavf,
142 #endif
143 #ifdef CONFIG_MUSEPACK
144 &demuxer_desc_mpc,
145 #endif
146 #ifdef CONFIG_LIBDV095
147 &demuxer_desc_rawdv,
148 #endif
149 &demuxer_desc_aac,
150 #ifdef CONFIG_LIBNUT
151 &demuxer_desc_nut,
152 #endif
153 #ifdef CONFIG_XMMS
154 &demuxer_desc_xmms,
155 #endif
156 #ifdef CONFIG_MNG
157 &demuxer_desc_mng,
158 #endif
159 /* Please do not add any new demuxers here. If you want to implement a new
160 * demuxer, add it to libavformat, except for wrappers around external
161 * libraries and demuxers requiring binary support. */
162 NULL
165 void free_demuxer_stream(demux_stream_t *ds)
167 ds_free_packs(ds);
168 free(ds);
171 demux_stream_t *new_demuxer_stream(struct demuxer_st *demuxer, int id)
173 demux_stream_t *ds = malloc(sizeof(demux_stream_t));
174 ds->buffer_pos = ds->buffer_size = 0;
175 ds->buffer = NULL;
176 ds->pts = 0;
177 ds->pts_bytes = 0;
178 ds->eof = 0;
179 ds->pos = 0;
180 ds->dpos = 0;
181 ds->pack_no = 0;
183 ds->packs = 0;
184 ds->bytes = 0;
185 ds->first = ds->last = ds->current = NULL;
186 ds->id = id;
187 ds->demuxer = demuxer;
189 ds->asf_seq = -1;
190 ds->asf_packet = NULL;
192 ds->ss_mul = ds->ss_div = 0;
194 ds->sh = NULL;
195 return ds;
200 * Get demuxer description structure for a given demuxer type
202 * @param file_format type of the demuxer
203 * @return structure for the demuxer, NULL if not found
205 static const demuxer_desc_t *get_demuxer_desc_from_type(int file_format)
207 int i;
209 for (i = 0; demuxer_list[i]; i++)
210 if (file_format == demuxer_list[i]->type)
211 return demuxer_list[i];
213 return NULL;
217 demuxer_t *new_demuxer(stream_t *stream, int type, int a_id, int v_id,
218 int s_id, char *filename)
220 demuxer_t *d = malloc(sizeof(demuxer_t));
221 memset(d, 0, sizeof(demuxer_t));
222 d->stream = stream;
223 d->stream_pts = MP_NOPTS_VALUE;
224 d->reference_clock = MP_NOPTS_VALUE;
225 d->movi_start = stream->start_pos;
226 d->movi_end = stream->end_pos;
227 d->seekable = 1;
228 d->synced = 0;
229 d->filepos = 0;
230 d->audio = new_demuxer_stream(d, a_id);
231 d->video = new_demuxer_stream(d, v_id);
232 d->sub = new_demuxer_stream(d, s_id);
233 d->type = type;
234 if (type)
235 if (!(d->desc = get_demuxer_desc_from_type(type)))
236 mp_msg(MSGT_DEMUXER, MSGL_ERR,
237 "BUG! Invalid demuxer type in new_demuxer(), "
238 "big troubles ahead.");
239 if (filename) // Filename hack for avs_check_file
240 d->filename = strdup(filename);
241 stream_reset(stream);
242 stream_seek(stream, stream->start_pos);
243 return d;
246 extern int dvdsub_id;
248 sh_sub_t *new_sh_sub_sid(demuxer_t *demuxer, int id, int sid)
250 if (id > MAX_S_STREAMS - 1 || id < 0) {
251 mp_msg(MSGT_DEMUXER, MSGL_WARN,
252 "Requested sub stream id overflow (%d > %d)\n", id,
253 MAX_S_STREAMS);
254 return NULL;
256 if (demuxer->s_streams[id])
257 mp_msg(MSGT_DEMUXER, MSGL_WARN, "Sub stream %i redefined\n", id);
258 else {
259 sh_sub_t *sh = calloc(1, sizeof(sh_sub_t));
260 demuxer->s_streams[id] = sh;
261 sh->sid = sid;
262 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SUBTITLE_ID=%d\n", sid);
264 return demuxer->s_streams[id];
267 void free_sh_sub(sh_sub_t *sh)
269 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_sub at %p\n", sh);
270 free(sh->extradata);
271 #ifdef CONFIG_ASS
272 if (sh->ass_track)
273 ass_free_track(sh->ass_track);
274 #endif
275 free(sh->lang);
276 free(sh);
279 sh_audio_t *new_sh_audio_aid(demuxer_t *demuxer, int id, int aid)
281 if (id > MAX_A_STREAMS - 1 || id < 0) {
282 mp_msg(MSGT_DEMUXER, MSGL_WARN,
283 "Requested audio stream id overflow (%d > %d)\n", id,
284 MAX_A_STREAMS);
285 return NULL;
287 if (demuxer->a_streams[id])
288 mp_msg(MSGT_DEMUXER, MSGL_WARN, MSGTR_AudioStreamRedefined, id);
289 else {
290 sh_audio_t *sh = calloc(1, sizeof(sh_audio_t));
291 mp_msg(MSGT_DEMUXER, MSGL_V, MSGTR_FoundAudioStream, id);
292 demuxer->a_streams[id] = sh;
293 sh->aid = aid;
294 sh->ds = demuxer->audio;
295 // set some defaults
296 sh->samplesize = 2;
297 sh->sample_format = AF_FORMAT_S16_NE;
298 sh->audio_out_minsize = 8192; /* default size, maybe not enough for Win32/ACM */
299 sh->pts = MP_NOPTS_VALUE;
300 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_ID=%d\n", aid);
302 return demuxer->a_streams[id];
305 void free_sh_audio(demuxer_t *demuxer, int id)
307 sh_audio_t *sh = demuxer->a_streams[id];
308 demuxer->a_streams[id] = NULL;
309 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_audio at %p\n", sh);
310 free(sh->wf);
311 free(sh->codecdata);
312 free(sh->lang);
313 free(sh);
316 sh_video_t *new_sh_video_vid(demuxer_t *demuxer, int id, int vid)
318 if (id > MAX_V_STREAMS - 1 || id < 0) {
319 mp_msg(MSGT_DEMUXER, MSGL_WARN,
320 "Requested video stream id overflow (%d > %d)\n", id,
321 MAX_V_STREAMS);
322 return NULL;
324 if (demuxer->v_streams[id])
325 mp_msg(MSGT_DEMUXER, MSGL_WARN, MSGTR_VideoStreamRedefined, id);
326 else {
327 sh_video_t *sh = calloc(1, sizeof(sh_video_t));
328 mp_msg(MSGT_DEMUXER, MSGL_V, MSGTR_FoundVideoStream, id);
329 demuxer->v_streams[id] = sh;
330 sh->vid = vid;
331 sh->ds = demuxer->video;
332 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_ID=%d\n", vid);
334 return demuxer->v_streams[id];
337 void free_sh_video(sh_video_t *sh)
339 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_video at %p\n", sh);
340 free(sh->bih);
341 free(sh);
344 void free_demuxer(demuxer_t *demuxer)
346 int i;
347 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing demuxer at %p\n",
348 demuxer);
349 if (demuxer->desc->close)
350 demuxer->desc->close(demuxer);
351 // Very ugly hack to make it behave like old implementation
352 if (demuxer->desc->type == DEMUXER_TYPE_DEMUXERS)
353 goto skip_streamfree;
354 // free streams:
355 for (i = 0; i < MAX_A_STREAMS; i++)
356 if (demuxer->a_streams[i])
357 free_sh_audio(demuxer, i);
358 for (i = 0; i < MAX_V_STREAMS; i++)
359 if (demuxer->v_streams[i])
360 free_sh_video(demuxer->v_streams[i]);
361 for (i = 0; i < MAX_S_STREAMS; i++)
362 if (demuxer->s_streams[i])
363 free_sh_sub(demuxer->s_streams[i]);
364 // free demuxers:
365 free_demuxer_stream(demuxer->audio);
366 free_demuxer_stream(demuxer->video);
367 free_demuxer_stream(demuxer->sub);
368 skip_streamfree:
369 if (demuxer->info) {
370 for (i = 0; demuxer->info[i] != NULL; i++)
371 free(demuxer->info[i]);
372 free(demuxer->info);
374 free(demuxer->filename);
375 if (demuxer->chapters) {
376 for (i = 0; i < demuxer->num_chapters; i++)
377 free(demuxer->chapters[i].name);
378 free(demuxer->chapters);
380 if (demuxer->attachments) {
381 for (i = 0; i < demuxer->num_attachments; i++) {
382 free(demuxer->attachments[i].name);
383 free(demuxer->attachments[i].type);
384 free(demuxer->attachments[i].data);
386 free(demuxer->attachments);
388 free(demuxer);
392 void ds_add_packet(demux_stream_t *ds, demux_packet_t *dp)
394 // append packet to DS stream:
395 ++ds->packs;
396 ds->bytes += dp->len;
397 if (ds->last) {
398 // next packet in stream
399 ds->last->next = dp;
400 ds->last = dp;
401 } else {
402 // first packet in stream
403 ds->first = ds->last = dp;
405 mp_dbg(MSGT_DEMUXER, MSGL_DBG2,
406 "DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
407 (ds == ds->demuxer->audio) ? "d_audio" : "d_video", dp->len,
408 dp->pts, (unsigned int) dp->pos, ds->demuxer->audio->packs,
409 ds->demuxer->video->packs);
412 void ds_read_packet(demux_stream_t *ds, stream_t *stream, int len,
413 double pts, off_t pos, int flags)
415 demux_packet_t *dp = new_demux_packet(len);
416 len = stream_read(stream, dp->buffer, len);
417 resize_demux_packet(dp, len);
418 dp->pts = pts;
419 dp->pos = pos;
420 dp->flags = flags;
421 // append packet to DS stream:
422 ds_add_packet(ds, dp);
425 // return value:
426 // 0 = EOF or no stream found or invalid type
427 // 1 = successfully read a packet
429 int demux_fill_buffer(demuxer_t *demux, demux_stream_t *ds)
431 // Note: parameter 'ds' can be NULL!
432 return demux->desc->fill_buffer(demux, ds);
435 // return value:
436 // 0 = EOF
437 // 1 = successful
438 #define MAX_ACUMULATED_PACKETS 64
439 int ds_fill_buffer(demux_stream_t *ds)
441 demuxer_t *demux = ds->demuxer;
442 if (ds->current)
443 free_demux_packet(ds->current);
444 ds->current = NULL;
445 if (mp_msg_test(MSGT_DEMUXER, MSGL_DBG3)) {
446 if (ds == demux->audio)
447 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
448 "ds_fill_buffer(d_audio) called\n");
449 else if (ds == demux->video)
450 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
451 "ds_fill_buffer(d_video) called\n");
452 else if (ds == demux->sub)
453 mp_dbg(MSGT_DEMUXER, MSGL_DBG3, "ds_fill_buffer(d_sub) called\n");
454 else
455 mp_dbg(MSGT_DEMUXER, MSGL_DBG3,
456 "ds_fill_buffer(unknown 0x%X) called\n", (unsigned int) ds);
458 while (1) {
459 if (ds->packs) {
460 demux_packet_t *p = ds->first;
461 #if 0
462 if (demux->reference_clock != MP_NOPTS_VALUE) {
463 if ( p->pts != MP_NOPTS_VALUE
464 && p->pts > demux->reference_clock
465 && ds->packs < MAX_ACUMULATED_PACKETS) {
466 if (demux_fill_buffer(demux, ds))
467 continue;
470 #endif
471 // copy useful data:
472 ds->buffer = p->buffer;
473 ds->buffer_pos = 0;
474 ds->buffer_size = p->len;
475 ds->pos = p->pos;
476 ds->dpos += p->len; // !!!
477 ++ds->pack_no;
478 if (p->pts != MP_NOPTS_VALUE) {
479 ds->pts = p->pts;
480 ds->pts_bytes = 0;
482 ds->pts_bytes += p->len; // !!!
483 if (p->stream_pts != MP_NOPTS_VALUE)
484 demux->stream_pts = p->stream_pts;
485 ds->flags = p->flags;
486 // unlink packet:
487 ds->bytes -= p->len;
488 ds->current = p;
489 ds->first = p->next;
490 if (!ds->first)
491 ds->last = NULL;
492 --ds->packs;
493 return 1;
495 if (demux->audio->packs >= MAX_PACKS
496 || demux->audio->bytes >= MAX_PACK_BYTES) {
497 mp_msg(MSGT_DEMUXER, MSGL_ERR, MSGTR_TooManyAudioInBuffer,
498 demux->audio->packs, demux->audio->bytes);
499 mp_msg(MSGT_DEMUXER, MSGL_HINT, MSGTR_MaybeNI);
500 break;
502 if (demux->video->packs >= MAX_PACKS
503 || demux->video->bytes >= MAX_PACK_BYTES) {
504 mp_msg(MSGT_DEMUXER, MSGL_ERR, MSGTR_TooManyVideoInBuffer,
505 demux->video->packs, demux->video->bytes);
506 mp_msg(MSGT_DEMUXER, MSGL_HINT, MSGTR_MaybeNI);
507 break;
509 if (!demux_fill_buffer(demux, ds)) {
510 mp_dbg(MSGT_DEMUXER, MSGL_DBG2,
511 "ds_fill_buffer()->demux_fill_buffer() failed\n");
512 break; // EOF
515 ds->buffer_pos = ds->buffer_size = 0;
516 ds->buffer = NULL;
517 mp_msg(MSGT_DEMUXER, MSGL_V,
518 "ds_fill_buffer: EOF reached (stream: %s) \n",
519 ds == demux->audio ? "audio" : "video");
520 ds->eof = 1;
521 return 0;
524 int demux_read_data(demux_stream_t *ds, unsigned char *mem, int len)
526 int x;
527 int bytes = 0;
528 while (len > 0) {
529 x = ds->buffer_size - ds->buffer_pos;
530 if (x == 0) {
531 if (!ds_fill_buffer(ds))
532 return bytes;
533 } else {
534 if (x > len)
535 x = len;
536 if (mem)
537 fast_memcpy(mem + bytes, &ds->buffer[ds->buffer_pos], x);
538 bytes += x;
539 len -= x;
540 ds->buffer_pos += x;
543 return bytes;
547 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
548 * \param mem memory to read data into, may be NULL to discard data
549 * \param maxlen maximum number of bytes to read
550 * \param read number of bytes actually read
551 * \param pattern pattern to search for (lowest 8 bits are ignored)
552 * \return whether pattern was found
554 int demux_pattern_3(demux_stream_t *ds, unsigned char *mem, int maxlen,
555 int *read, uint32_t pattern)
557 register uint32_t head = 0xffffff00;
558 register uint32_t pat = pattern & 0xffffff00;
559 int total_len = 0;
560 do {
561 register unsigned char *ds_buf = &ds->buffer[ds->buffer_size];
562 int len = ds->buffer_size - ds->buffer_pos;
563 register long pos = -len;
564 if (unlikely(pos >= 0)) { // buffer is empty
565 ds_fill_buffer(ds);
566 continue;
568 do {
569 head |= ds_buf[pos];
570 head <<= 8;
571 } while (++pos && head != pat);
572 len += pos;
573 if (total_len + len > maxlen)
574 len = maxlen - total_len;
575 len = demux_read_data(ds, mem ? &mem[total_len] : NULL, len);
576 total_len += len;
577 } while ((head != pat || total_len < 3) && total_len < maxlen && !ds->eof);
578 if (read)
579 *read = total_len;
580 return total_len >= 3 && head == pat;
583 void ds_free_packs(demux_stream_t *ds)
585 demux_packet_t *dp = ds->first;
586 while (dp) {
587 demux_packet_t *dn = dp->next;
588 free_demux_packet(dp);
589 dp = dn;
591 if (ds->asf_packet) {
592 // free unfinished .asf fragments:
593 free(ds->asf_packet->buffer);
594 free(ds->asf_packet);
595 ds->asf_packet = NULL;
597 ds->first = ds->last = NULL;
598 ds->packs = 0; // !!!!!
599 ds->bytes = 0;
600 if (ds->current)
601 free_demux_packet(ds->current);
602 ds->current = NULL;
603 ds->buffer = NULL;
604 ds->buffer_pos = ds->buffer_size;
605 ds->pts = 0;
606 ds->pts_bytes = 0;
609 int ds_get_packet(demux_stream_t *ds, unsigned char **start)
611 int len;
612 if (ds->buffer_pos >= ds->buffer_size) {
613 if (!ds_fill_buffer(ds)) {
614 // EOF
615 *start = NULL;
616 return -1;
619 len = ds->buffer_size - ds->buffer_pos;
620 *start = &ds->buffer[ds->buffer_pos];
621 ds->buffer_pos += len;
622 return len;
625 int ds_get_packet_pts(demux_stream_t *ds, unsigned char **start, double *pts)
627 int len;
628 *pts = MP_NOPTS_VALUE;
629 if (ds->buffer_pos >= ds->buffer_size) {
630 if (!ds_fill_buffer(ds)) {
631 // EOF
632 *start = NULL;
633 return -1;
636 // Return pts unless this read starts from the middle of a packet
637 if (!ds->buffer_pos)
638 *pts = ds->current->pts;
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_sub(demux_stream_t *ds, unsigned char **start)
647 int len;
648 if (ds->buffer_pos >= ds->buffer_size) {
649 *start = NULL;
650 if (!ds->packs)
651 return -1; // no sub
652 if (!ds_fill_buffer(ds))
653 return -1; // EOF
655 len = ds->buffer_size - ds->buffer_pos;
656 *start = &ds->buffer[ds->buffer_pos];
657 ds->buffer_pos += len;
658 return len;
661 double ds_get_next_pts(demux_stream_t *ds)
663 demuxer_t *demux = ds->demuxer;
664 while (!ds->first) {
665 if (demux->audio->packs >= MAX_PACKS
666 || demux->audio->bytes >= MAX_PACK_BYTES) {
667 mp_msg(MSGT_DEMUXER, MSGL_ERR, MSGTR_TooManyAudioInBuffer,
668 demux->audio->packs, demux->audio->bytes);
669 mp_msg(MSGT_DEMUXER, MSGL_HINT, MSGTR_MaybeNI);
670 return MP_NOPTS_VALUE;
672 if (demux->video->packs >= MAX_PACKS
673 || demux->video->bytes >= MAX_PACK_BYTES) {
674 mp_msg(MSGT_DEMUXER, MSGL_ERR, MSGTR_TooManyVideoInBuffer,
675 demux->video->packs, demux->video->bytes);
676 mp_msg(MSGT_DEMUXER, MSGL_HINT, MSGTR_MaybeNI);
677 return MP_NOPTS_VALUE;
679 if (!demux_fill_buffer(demux, ds))
680 return MP_NOPTS_VALUE;
682 return ds->first->pts;
685 // ====================================================================
687 void demuxer_help(void)
689 int i;
691 mp_msg(MSGT_DEMUXER, MSGL_INFO, "Available demuxers:\n");
692 mp_msg(MSGT_DEMUXER, MSGL_INFO, " demuxer: type info: (comment)\n");
693 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DEMUXERS\n");
694 for (i = 0; demuxer_list[i]; i++) {
695 if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Don't display special demuxers
696 continue;
697 if (demuxer_list[i]->comment && strlen(demuxer_list[i]->comment))
698 mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s (%s)\n",
699 demuxer_list[i]->name, demuxer_list[i]->type,
700 demuxer_list[i]->info, demuxer_list[i]->comment);
701 else
702 mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s\n",
703 demuxer_list[i]->name, demuxer_list[i]->type,
704 demuxer_list[i]->info);
710 * Get demuxer type for a given demuxer name
712 * @param demuxer_name string with demuxer name of demuxer number
713 * @param force will be set if demuxer should be forced.
714 * May be NULL.
715 * @return DEMUXER_TYPE_xxx, -1 if error or not found
717 int get_demuxer_type_from_name(char *demuxer_name, int *force)
719 int i;
720 long type_int;
721 char *endptr;
723 if (!demuxer_name || !demuxer_name[0])
724 return DEMUXER_TYPE_UNKNOWN;
725 if (force)
726 *force = demuxer_name[0] == '+';
727 if (demuxer_name[0] == '+')
728 demuxer_name = &demuxer_name[1];
729 for (i = 0; demuxer_list[i]; i++) {
730 if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Can't select special demuxers from commandline
731 continue;
732 if (strcmp(demuxer_name, demuxer_list[i]->name) == 0)
733 return demuxer_list[i]->type;
736 // No match found, try to parse name as an integer (demuxer number)
737 type_int = strtol(demuxer_name, &endptr, 0);
738 if (*endptr) // Conversion failed
739 return -1;
740 if ((type_int > 0) && (type_int <= DEMUXER_TYPE_MAX))
741 return (int) type_int;
743 return -1;
746 int extension_parsing = 1; // 0=off 1=mixed (used only for unstable formats)
748 int correct_pts = 0;
749 int user_correct_pts = -1;
752 NOTE : Several demuxers may be opened at the same time so
753 demuxers should NEVER rely on an external var to enable them
754 self. If a demuxer can't do any auto-detection it should only use
755 file_format. The user can explicitly set file_format with the -demuxer
756 option so there is really no need for another extra var.
757 For convenience an option can be added to set file_format directly
758 to the right type (ex: rawaudio,rawvideo).
759 Also the stream can override the file_format so a demuxer which rely
760 on a special stream type can set file_format at the stream level
761 (ex: tv,mf).
764 static demuxer_t *demux_open_stream(stream_t *stream, int file_format,
765 int force, int audio_id, int video_id,
766 int dvdsub_id, char *filename)
768 demuxer_t *demuxer = NULL;
770 sh_video_t *sh_video = NULL;
772 const demuxer_desc_t *demuxer_desc;
773 int fformat = 0;
774 int i;
776 // If somebody requested a demuxer check it
777 if (file_format) {
778 if ((demuxer_desc = get_demuxer_desc_from_type(file_format))) {
779 demuxer = new_demuxer(stream, demuxer_desc->type, audio_id,
780 video_id, dvdsub_id, filename);
781 if (demuxer_desc->check_file)
782 fformat = demuxer_desc->check_file(demuxer);
783 if (force || !demuxer_desc->check_file)
784 fformat = demuxer_desc->type;
785 if (fformat != 0) {
786 if (fformat == demuxer_desc->type) {
787 demuxer_t *demux2 = demuxer;
788 // Move messages to demuxer detection code?
789 mp_msg(MSGT_DEMUXER, MSGL_INFO,
790 MSGTR_Detected_XXX_FileFormat,
791 demuxer_desc->shortdesc);
792 file_format = fformat;
793 if (!demuxer->desc->open
794 || (demux2 = demuxer->desc->open(demuxer))) {
795 demuxer = demux2;
796 goto dmx_open;
798 } else {
799 // Format changed after check, recurse
800 free_demuxer(demuxer);
801 return demux_open_stream(stream, fformat, force, audio_id,
802 video_id, dvdsub_id, filename);
805 // Check failed for forced demuxer, quit
806 free_demuxer(demuxer);
807 return NULL;
810 // Test demuxers with safe file checks
811 for (i = 0; (demuxer_desc = demuxer_list[i]); i++) {
812 if (demuxer_desc->safe_check) {
813 demuxer = new_demuxer(stream, demuxer_desc->type, audio_id,
814 video_id, dvdsub_id, filename);
815 if ((fformat = demuxer_desc->check_file(demuxer)) != 0) {
816 if (fformat == demuxer_desc->type) {
817 demuxer_t *demux2 = demuxer;
818 mp_msg(MSGT_DEMUXER, MSGL_INFO,
819 MSGTR_Detected_XXX_FileFormat,
820 demuxer_desc->shortdesc);
821 file_format = fformat;
822 if (!demuxer->desc->open
823 || (demux2 = demuxer->desc->open(demuxer))) {
824 demuxer = demux2;
825 goto dmx_open;
827 } else {
828 if (fformat == DEMUXER_TYPE_PLAYLIST)
829 return demuxer; // handled in mplayer.c
830 // Format changed after check, recurse
831 free_demuxer(demuxer);
832 demuxer = demux_open_stream(stream, fformat, force,
833 audio_id, video_id,
834 dvdsub_id, filename);
835 if (demuxer)
836 return demuxer; // done!
837 file_format = DEMUXER_TYPE_UNKNOWN;
840 free_demuxer(demuxer);
841 demuxer = NULL;
845 // If no forced demuxer perform file extension based detection
846 // Ok. We're over the stable detectable fileformats, the next ones are
847 // a bit fuzzy. So by default (extension_parsing==1) try extension-based
848 // detection first:
849 if (file_format == DEMUXER_TYPE_UNKNOWN && filename
850 && extension_parsing == 1) {
851 file_format = demuxer_type_by_filename(filename);
852 if (file_format != DEMUXER_TYPE_UNKNOWN) {
853 // we like recursion :)
854 demuxer = demux_open_stream(stream, file_format, force, audio_id,
855 video_id, dvdsub_id, filename);
856 if (demuxer)
857 return demuxer; // done!
858 file_format = DEMUXER_TYPE_UNKNOWN; // continue fuzzy guessing...
859 mp_msg(MSGT_DEMUXER, MSGL_V,
860 "demuxer: continue fuzzy content-based format guessing...\n");
863 // Try detection for all other demuxers
864 for (i = 0; (demuxer_desc = demuxer_list[i]); i++) {
865 if (!demuxer_desc->safe_check && demuxer_desc->check_file) {
866 demuxer = new_demuxer(stream, demuxer_desc->type, audio_id,
867 video_id, dvdsub_id, filename);
868 if ((fformat = demuxer_desc->check_file(demuxer)) != 0) {
869 if (fformat == demuxer_desc->type) {
870 demuxer_t *demux2 = demuxer;
871 mp_msg(MSGT_DEMUXER, MSGL_INFO,
872 MSGTR_Detected_XXX_FileFormat,
873 demuxer_desc->shortdesc);
874 file_format = fformat;
875 if (!demuxer->desc->open
876 || (demux2 = demuxer->desc->open(demuxer))) {
877 demuxer = demux2;
878 goto dmx_open;
880 } else {
881 if (fformat == DEMUXER_TYPE_PLAYLIST)
882 return demuxer; // handled in mplayer.c
883 // Format changed after check, recurse
884 free_demuxer(demuxer);
885 demuxer = demux_open_stream(stream, fformat, force,
886 audio_id, video_id,
887 dvdsub_id, filename);
888 if (demuxer)
889 return demuxer; // done!
890 file_format = DEMUXER_TYPE_UNKNOWN;
893 free_demuxer(demuxer);
894 demuxer = NULL;
898 return NULL;
899 //====== File format recognized, set up these for compatibility: =========
900 dmx_open:
902 demuxer->file_format = file_format;
904 if ((sh_video = demuxer->video->sh) && sh_video->bih) {
905 int biComp = le2me_32(sh_video->bih->biCompression);
906 mp_msg(MSGT_DEMUX, MSGL_INFO,
907 "VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
908 (char *) &biComp, sh_video->bih->biWidth,
909 sh_video->bih->biHeight, sh_video->bih->biBitCount,
910 sh_video->fps, sh_video->i_bps * 0.008f,
911 sh_video->i_bps / 1024.0f);
913 #ifdef CONFIG_ASS
914 if (ass_enabled && ass_library) {
915 for (i = 0; i < MAX_S_STREAMS; ++i) {
916 sh_sub_t *sh = demuxer->s_streams[i];
917 if (sh && sh->type == 'a') {
918 sh->ass_track = ass_new_track(ass_library);
919 if (sh->ass_track && sh->extradata)
920 ass_process_codec_private(sh->ass_track, sh->extradata,
921 sh->extradata_len);
922 } else if (sh && sh->type != 'v')
923 sh->ass_track = ass_default_track(ass_library);
926 #endif
927 return demuxer;
930 char *audio_stream = NULL;
931 char *sub_stream = NULL;
932 int audio_stream_cache = 0;
934 char *demuxer_name = NULL; // parameter from -demuxer
935 char *audio_demuxer_name = NULL; // parameter from -audio-demuxer
936 char *sub_demuxer_name = NULL; // parameter from -sub-demuxer
938 extern int hr_mp3_seek;
940 extern float stream_cache_min_percent;
941 extern float stream_cache_seek_min_percent;
943 demuxer_t *demux_open(stream_t *vs, int file_format, int audio_id,
944 int video_id, int dvdsub_id, char *filename)
946 stream_t *as = NULL, *ss = NULL;
947 demuxer_t *vd, *ad = NULL, *sd = NULL;
948 demuxer_t *res;
949 int afmt = DEMUXER_TYPE_UNKNOWN, sfmt = DEMUXER_TYPE_UNKNOWN;
950 int demuxer_type;
951 int audio_demuxer_type = 0, sub_demuxer_type = 0;
952 int demuxer_force = 0, audio_demuxer_force = 0, sub_demuxer_force = 0;
954 if ((demuxer_type =
955 get_demuxer_type_from_name(demuxer_name, &demuxer_force)) < 0) {
956 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-demuxer %s does not exist.\n",
957 demuxer_name);
959 if ((audio_demuxer_type =
960 get_demuxer_type_from_name(audio_demuxer_name,
961 &audio_demuxer_force)) < 0) {
962 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-audio-demuxer %s does not exist.\n",
963 audio_demuxer_name);
965 if ((sub_demuxer_type =
966 get_demuxer_type_from_name(sub_demuxer_name,
967 &sub_demuxer_force)) < 0) {
968 mp_msg(MSGT_DEMUXER, MSGL_ERR, "-sub-demuxer %s does not exist.\n",
969 sub_demuxer_name);
972 if (audio_stream) {
973 as = open_stream(audio_stream, 0, &afmt);
974 if (!as) {
975 mp_msg(MSGT_DEMUXER, MSGL_ERR, MSGTR_CannotOpenAudioStream,
976 audio_stream);
977 return NULL;
979 if (audio_stream_cache) {
980 if (!stream_enable_cache
981 (as, audio_stream_cache * 1024,
982 audio_stream_cache * 1024 * (stream_cache_min_percent /
983 100.0),
984 audio_stream_cache * 1024 * (stream_cache_seek_min_percent /
985 100.0))) {
986 free_stream(as);
987 mp_msg(MSGT_DEMUXER, MSGL_ERR,
988 "Can't enable audio stream cache\n");
989 return NULL;
993 if (sub_stream) {
994 ss = open_stream(sub_stream, 0, &sfmt);
995 if (!ss) {
996 mp_msg(MSGT_DEMUXER, MSGL_ERR, MSGTR_CannotOpenSubtitlesStream,
997 sub_stream);
998 return NULL;
1002 vd = demux_open_stream(vs, demuxer_type ? demuxer_type : file_format,
1003 demuxer_force, audio_stream ? -2 : audio_id,
1004 video_id, sub_stream ? -2 : dvdsub_id, filename);
1005 if (!vd) {
1006 if (as)
1007 free_stream(as);
1008 if (ss)
1009 free_stream(ss);
1010 return NULL;
1012 if (as) {
1013 ad = demux_open_stream(as,
1014 audio_demuxer_type ? audio_demuxer_type : afmt,
1015 audio_demuxer_force, audio_id, -2, -2,
1016 audio_stream);
1017 if (!ad) {
1018 mp_msg(MSGT_DEMUXER, MSGL_WARN, MSGTR_OpeningAudioDemuxerFailed,
1019 audio_stream);
1020 free_stream(as);
1021 } else if (ad->audio->sh
1022 && ((sh_audio_t *) ad->audio->sh)->format == 0x55) // MP3
1023 hr_mp3_seek = 1; // Enable high res seeking
1025 if (ss) {
1026 sd = demux_open_stream(ss, sub_demuxer_type ? sub_demuxer_type : sfmt,
1027 sub_demuxer_force, -2, -2, dvdsub_id,
1028 sub_stream);
1029 if (!sd) {
1030 mp_msg(MSGT_DEMUXER, MSGL_WARN,
1031 MSGTR_OpeningSubtitlesDemuxerFailed, sub_stream);
1032 free_stream(ss);
1036 if (ad && sd)
1037 res = new_demuxers_demuxer(vd, ad, sd);
1038 else if (ad)
1039 res = new_demuxers_demuxer(vd, ad, vd);
1040 else if (sd)
1041 res = new_demuxers_demuxer(vd, vd, sd);
1042 else
1043 res = vd;
1045 correct_pts = user_correct_pts;
1046 if (correct_pts < 0)
1047 correct_pts = demux_control(res, DEMUXER_CTRL_CORRECT_PTS, NULL)
1048 == DEMUXER_CTRL_OK;
1049 return res;
1053 void demux_flush(demuxer_t *demuxer)
1055 ds_free_packs(demuxer->video);
1056 ds_free_packs(demuxer->audio);
1057 ds_free_packs(demuxer->sub);
1060 int demux_seek(demuxer_t *demuxer, float rel_seek_secs, float audio_delay,
1061 int flags)
1063 demux_stream_t *d_audio = demuxer->audio;
1064 demux_stream_t *d_video = demuxer->video;
1065 sh_audio_t *sh_audio = d_audio->sh;
1066 sh_video_t *sh_video = d_video->sh;
1067 double tmp = 0;
1068 double pts;
1070 if (!demuxer->seekable) {
1071 if (demuxer->file_format == DEMUXER_TYPE_AVI)
1072 mp_msg(MSGT_SEEK, MSGL_WARN, MSGTR_CantSeekRawAVI);
1073 #ifdef CONFIG_TV
1074 else if (demuxer->file_format == DEMUXER_TYPE_TV)
1075 mp_msg(MSGT_SEEK, MSGL_WARN, MSGTR_TVInputNotSeekable);
1076 #endif
1077 else
1078 mp_msg(MSGT_SEEK, MSGL_WARN, MSGTR_CantSeekFile);
1079 return 0;
1082 demux_flush(demuxer);
1083 // clear demux buffers:
1084 if (sh_audio)
1085 sh_audio->a_buffer_len = 0;
1087 demuxer->stream->eof = 0;
1088 demuxer->video->eof = 0;
1089 demuxer->audio->eof = 0;
1091 if (sh_video)
1092 sh_video->timer = 0; // !!!!!!
1094 if (flags & SEEK_ABSOLUTE)
1095 pts = 0.0f;
1096 else {
1097 if (demuxer->stream_pts == MP_NOPTS_VALUE)
1098 goto dmx_seek;
1099 pts = demuxer->stream_pts;
1102 if (flags & SEEK_FACTOR) {
1103 if (stream_control(demuxer->stream, STREAM_CTRL_GET_TIME_LENGTH, &tmp)
1104 == STREAM_UNSUPPORTED)
1105 goto dmx_seek;
1106 pts += tmp * rel_seek_secs;
1107 } else
1108 pts += rel_seek_secs;
1110 if (stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_TIME, &pts) !=
1111 STREAM_UNSUPPORTED) {
1112 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1113 return 1;
1116 dmx_seek:
1117 if (demuxer->desc->seek)
1118 demuxer->desc->seek(demuxer, rel_seek_secs, audio_delay, flags);
1120 if (sh_audio)
1121 resync_audio_stream(sh_audio);
1123 return 1;
1126 int demux_info_add(demuxer_t *demuxer, const char *opt, const char *param)
1128 char **info = demuxer->info;
1129 int n = 0;
1132 for (n = 0; info && info[2 * n] != NULL; n++) {
1133 if (!strcasecmp(opt, info[2 * n])) {
1134 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_DemuxerInfoChanged, opt,
1135 param);
1136 free(info[2 * n + 1]);
1137 info[2 * n + 1] = strdup(param);
1138 return 0;
1142 info = demuxer->info = (char **) realloc(info,
1143 (2 * (n + 2)) * sizeof(char *));
1144 info[2 * n] = strdup(opt);
1145 info[2 * n + 1] = strdup(param);
1146 memset(&info[2 * (n + 1)], 0, 2 * sizeof(char *));
1148 return 1;
1151 int demux_info_print(demuxer_t *demuxer)
1153 char **info = demuxer->info;
1154 int n;
1156 if (!info)
1157 return 0;
1159 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_ClipInfo);
1160 for (n = 0; info[2 * n] != NULL; n++) {
1161 mp_msg(MSGT_DEMUX, MSGL_INFO, " %s: %s\n", info[2 * n],
1162 info[2 * n + 1]);
1163 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_NAME%d=%s\n", n,
1164 info[2 * n]);
1165 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_VALUE%d=%s\n", n,
1166 info[2 * n + 1]);
1168 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_N=%d\n", n);
1170 return 0;
1173 char *demux_info_get(demuxer_t *demuxer, const char *opt)
1175 int i;
1176 char **info = demuxer->info;
1178 for (i = 0; info && info[2 * i] != NULL; i++) {
1179 if (!strcasecmp(opt, info[2 * i]))
1180 return info[2 * i + 1];
1183 return NULL;
1186 int demux_control(demuxer_t *demuxer, int cmd, void *arg)
1189 if (demuxer->desc->control)
1190 return demuxer->desc->control(demuxer, cmd, arg);
1192 return DEMUXER_CTRL_NOTIMPL;
1197 double demuxer_get_time_length(demuxer_t *demuxer)
1199 double get_time_ans;
1200 sh_video_t *sh_video = demuxer->video->sh;
1201 sh_audio_t *sh_audio = demuxer->audio->sh;
1202 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
1203 if (demux_control
1204 (demuxer, DEMUXER_CTRL_GET_TIME_LENGTH, (void *) &get_time_ans) <= 0) {
1205 if (sh_video && sh_video->i_bps && sh_audio && sh_audio->i_bps)
1206 get_time_ans = (double) (demuxer->movi_end -
1207 demuxer->movi_start) / (sh_video->i_bps +
1208 sh_audio->i_bps);
1209 else if (sh_video && sh_video->i_bps)
1210 get_time_ans = (double) (demuxer->movi_end -
1211 demuxer->movi_start) / sh_video->i_bps;
1212 else if (sh_audio && sh_audio->i_bps)
1213 get_time_ans = (double) (demuxer->movi_end -
1214 demuxer->movi_start) / sh_audio->i_bps;
1215 else
1216 get_time_ans = 0;
1218 return get_time_ans;
1222 * \brief demuxer_get_current_time() returns the time of the current play in three possible ways:
1223 * either when the stream reader satisfies STREAM_CTRL_GET_CURRENT_TIME (e.g. dvd)
1224 * or using sh_video->pts when the former method fails
1225 * 0 otherwise
1226 * \return the current play time
1228 int demuxer_get_current_time(demuxer_t *demuxer)
1230 double get_time_ans = 0;
1231 sh_video_t *sh_video = demuxer->video->sh;
1232 if (demuxer->stream_pts != MP_NOPTS_VALUE)
1233 get_time_ans = demuxer->stream_pts;
1234 else if (sh_video)
1235 get_time_ans = sh_video->pts;
1236 return (int) get_time_ans;
1239 int demuxer_get_percent_pos(demuxer_t *demuxer)
1241 int ans = 0;
1242 int res = demux_control(demuxer, DEMUXER_CTRL_GET_PERCENT_POS, &ans);
1243 int len = (demuxer->movi_end - demuxer->movi_start) / 100;
1244 if (res <= 0) {
1245 if (len > 0)
1246 ans = (demuxer->filepos - demuxer->movi_start) / len;
1247 else
1248 ans = 0;
1250 if (ans < 0)
1251 ans = 0;
1252 if (ans > 100)
1253 ans = 100;
1254 return ans;
1257 int demuxer_switch_audio(demuxer_t *demuxer, int index)
1259 int res = demux_control(demuxer, DEMUXER_CTRL_SWITCH_AUDIO, &index);
1260 if (res == DEMUXER_CTRL_NOTIMPL)
1261 index = demuxer->audio->id;
1262 if (demuxer->audio->id >= 0)
1263 demuxer->audio->sh = demuxer->a_streams[demuxer->audio->id];
1264 else
1265 demuxer->audio->sh = NULL;
1266 return index;
1269 int demuxer_switch_video(demuxer_t *demuxer, int index)
1271 int res = demux_control(demuxer, DEMUXER_CTRL_SWITCH_VIDEO, &index);
1272 if (res == DEMUXER_CTRL_NOTIMPL)
1273 index = demuxer->video->id;
1274 if (demuxer->video->id >= 0)
1275 demuxer->video->sh = demuxer->v_streams[demuxer->video->id];
1276 else
1277 demuxer->video->sh = NULL;
1278 return index;
1281 int demuxer_add_attachment(demuxer_t *demuxer, const char *name,
1282 const char *type, const void *data, size_t size)
1284 if (!(demuxer->num_attachments & 31))
1285 demuxer->attachments = realloc(demuxer->attachments,
1286 (demuxer->num_attachments + 32) * sizeof(demux_attachment_t));
1288 demuxer->attachments[demuxer->num_attachments].name = strdup(name);
1289 demuxer->attachments[demuxer->num_attachments].type = strdup(type);
1290 demuxer->attachments[demuxer->num_attachments].data = malloc(size);
1291 memcpy(demuxer->attachments[demuxer->num_attachments].data, data, size);
1292 demuxer->attachments[demuxer->num_attachments].data_size = size;
1294 return demuxer->num_attachments++;
1297 int demuxer_add_chapter(demuxer_t *demuxer, const char *name, uint64_t start,
1298 uint64_t end)
1300 if (demuxer->chapters == NULL)
1301 demuxer->chapters = malloc(32 * sizeof(*demuxer->chapters));
1302 else if (!(demuxer->num_chapters % 32))
1303 demuxer->chapters = realloc(demuxer->chapters,
1304 (demuxer->num_chapters + 32) *
1305 sizeof(*demuxer->chapters));
1307 demuxer->chapters[demuxer->num_chapters].start = start;
1308 demuxer->chapters[demuxer->num_chapters].end = end;
1309 demuxer->chapters[demuxer->num_chapters].name = strdup(name ? name : MSGTR_Unknown);
1311 return demuxer->num_chapters++;
1315 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1316 * either using the demuxer->chapters structure set by the demuxer
1317 * or asking help to the stream layer (e.g. dvd)
1318 * \param chapter - chapter number wished - 0-based
1319 * \param mode 0: relative to current main pts, 1: absolute
1320 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1321 * \param num_chapters number of chapters present (set by this function is param is not null)
1322 * \param chapter_name name of chapter found (set by this function is param is not null)
1323 * \return -1 on error, current chapter if successful
1326 int demuxer_seek_chapter(demuxer_t *demuxer, int chapter, int mode,
1327 float *seek_pts, int *num_chapters,
1328 char **chapter_name)
1330 int ris;
1331 int current, total;
1332 sh_video_t *sh_video = demuxer->video->sh;
1333 sh_audio_t *sh_audio = demuxer->audio->sh;
1335 if (!demuxer->num_chapters || !demuxer->chapters) {
1336 if (!mode) {
1337 ris = stream_control(demuxer->stream,
1338 STREAM_CTRL_GET_CURRENT_CHAPTER, &current);
1339 if (ris == STREAM_UNSUPPORTED)
1340 return -1;
1341 chapter += current;
1344 demux_flush(demuxer);
1346 ris = stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_CHAPTER,
1347 &chapter);
1348 if (ris != STREAM_UNSUPPORTED)
1349 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1350 if (sh_video) {
1351 ds_fill_buffer(demuxer->video);
1352 resync_video_stream(sh_video);
1355 if (sh_audio) {
1356 ds_fill_buffer(demuxer->audio);
1357 resync_audio_stream(sh_audio);
1359 // exit status may be ok, but main() doesn't have to seek itself
1360 // (because e.g. dvds depend on sectors, not on pts)
1361 *seek_pts = -1.0;
1363 if (num_chapters) {
1364 if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS,
1365 num_chapters) == STREAM_UNSUPPORTED)
1366 *num_chapters = 0;
1369 if (chapter_name) {
1370 *chapter_name = NULL;
1371 if (num_chapters && *num_chapters) {
1372 char *tmp = malloc(16);
1373 if (tmp) {
1374 sprintf(tmp, " of %3d", *num_chapters);
1375 *chapter_name = tmp;
1380 return ris != STREAM_UNSUPPORTED ? chapter : -1;
1381 } else { // chapters structure is set in the demuxer
1382 total = demuxer->num_chapters;
1384 if (mode == 1) //absolute seeking
1385 current = chapter;
1386 else { //relative seeking
1387 uint64_t now;
1388 now = (sh_video ? sh_video->pts : (sh_audio ? sh_audio->pts : 0.))
1389 * 1000 + .5;
1391 for (current = total - 1; current >= 0; --current) {
1392 demux_chapter_t *chapter = demuxer->chapters + current;
1393 if (chapter->start <= now)
1394 break;
1396 current += chapter;
1399 if (current >= total)
1400 return -1;
1401 if (current < 0)
1402 current = 0;
1404 *seek_pts = demuxer->chapters[current].start / 1000.0;
1406 if (num_chapters)
1407 *num_chapters = demuxer->num_chapters;
1409 if (chapter_name) {
1410 if (demuxer->chapters[current].name)
1411 *chapter_name = strdup(demuxer->chapters[current].name);
1412 else
1413 *chapter_name = NULL;
1416 return current;
1420 int demuxer_get_current_chapter(demuxer_t *demuxer)
1422 int chapter = -1;
1423 if (!demuxer->num_chapters || !demuxer->chapters) {
1424 if (stream_control(demuxer->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
1425 &chapter) == STREAM_UNSUPPORTED)
1426 chapter = -1;
1427 } else {
1428 sh_video_t *sh_video = demuxer->video->sh;
1429 sh_audio_t *sh_audio = demuxer->audio->sh;
1430 uint64_t now;
1431 now = (sh_video ? sh_video->pts : (sh_audio ? sh_audio->pts : 0))
1432 * 1000 + 0.5;
1433 for (chapter = demuxer->num_chapters - 1; chapter >= 0; --chapter) {
1434 if (demuxer->chapters[chapter].start <= now)
1435 break;
1438 return chapter;
1441 char *demuxer_chapter_name(demuxer_t *demuxer, int chapter)
1443 if (demuxer->num_chapters && demuxer->chapters) {
1444 if (chapter >= 0 && chapter < demuxer->num_chapters
1445 && demuxer->chapters[chapter].name)
1446 return strdup(demuxer->chapters[chapter].name);
1448 return NULL;
1451 char *demuxer_chapter_display_name(demuxer_t *demuxer, int chapter)
1453 char *chapter_name = demuxer_chapter_name(demuxer, chapter);
1454 if (chapter_name) {
1455 char *tmp = malloc(strlen(chapter_name) + 14);
1456 snprintf(tmp, 63, "(%d) %s", chapter + 1, chapter_name);
1457 free(chapter_name);
1458 return tmp;
1459 } else {
1460 int chapter_num = demuxer_chapter_count(demuxer);
1461 char tmp[30];
1462 if (chapter_num <= 0)
1463 sprintf(tmp, "(%d)", chapter + 1);
1464 else
1465 sprintf(tmp, "(%d) of %d", chapter + 1, chapter_num);
1466 return strdup(tmp);
1470 float demuxer_chapter_time(demuxer_t *demuxer, int chapter, float *end)
1472 if (demuxer->num_chapters && demuxer->chapters && chapter >= 0
1473 && chapter < demuxer->num_chapters) {
1474 if (end)
1475 *end = demuxer->chapters[chapter].end / 1000.0;
1476 return demuxer->chapters[chapter].start / 1000.0;
1478 return -1.0;
1481 int demuxer_chapter_count(demuxer_t *demuxer)
1483 if (!demuxer->num_chapters || !demuxer->chapters) {
1484 int num_chapters = 0;
1485 if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS,
1486 &num_chapters) == STREAM_UNSUPPORTED)
1487 num_chapters = 0;
1488 return num_chapters;
1489 } else
1490 return demuxer->num_chapters;
1493 int demuxer_angles_count(demuxer_t *demuxer)
1495 int ris, angles = -1;
1497 ris = stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_ANGLES, &angles);
1498 if (ris == STREAM_UNSUPPORTED)
1499 return -1;
1500 return angles;
1503 int demuxer_get_current_angle(demuxer_t *demuxer)
1505 int ris, curr_angle = -1;
1506 ris = stream_control(demuxer->stream, STREAM_CTRL_GET_ANGLE, &curr_angle);
1507 if (ris == STREAM_UNSUPPORTED)
1508 return -1;
1509 return curr_angle;
1513 int demuxer_set_angle(demuxer_t *demuxer, int angle)
1515 int ris, angles = -1;
1516 sh_video_t *sh_video = demuxer->video->sh;
1517 sh_audio_t *sh_audio = demuxer->audio->sh;
1519 angles = demuxer_angles_count(demuxer);
1520 if ((angles < 1) || (angle > angles))
1521 return -1;
1523 demux_flush(demuxer);
1525 ris = stream_control(demuxer->stream, STREAM_CTRL_SET_ANGLE, &angle);
1526 if (ris == STREAM_UNSUPPORTED)
1527 return -1;
1529 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1530 if (sh_video) {
1531 ds_fill_buffer(demuxer->video);
1532 resync_video_stream(sh_video);
1535 if (sh_audio) {
1536 ds_fill_buffer(demuxer->audio);
1537 resync_audio_stream(sh_audio);
1540 return angle;
1543 int demuxer_audio_track_by_lang(demuxer_t *d, char *lang)
1545 int i, len;
1546 lang += strspn(lang, ",");
1547 while ((len = strcspn(lang, ",")) > 0) {
1548 for (i = 0; i < MAX_A_STREAMS; ++i) {
1549 sh_audio_t *sh = d->a_streams[i];
1550 if (sh && sh->lang && strncmp(sh->lang, lang, len) == 0)
1551 return sh->aid;
1553 lang += len;
1554 lang += strspn(lang, ",");
1556 return -1;
1559 int demuxer_sub_track_by_lang(demuxer_t *d, char *lang)
1561 int i, len;
1562 lang += strspn(lang, ",");
1563 while ((len = strcspn(lang, ",")) > 0) {
1564 for (i = 0; i < MAX_S_STREAMS; ++i) {
1565 sh_sub_t *sh = d->s_streams[i];
1566 if (sh && sh->lang && strncmp(sh->lang, lang, len) == 0)
1567 return sh->sid;
1569 lang += len;
1570 lang += strspn(lang, ",");
1572 return -1;
1575 int demuxer_default_audio_track(demuxer_t *d)
1577 int i;
1578 for (i = 0; i < MAX_A_STREAMS; ++i) {
1579 sh_audio_t *sh = d->a_streams[i];
1580 if (sh && sh->default_track)
1581 return sh->aid;
1583 for (i = 0; i < MAX_A_STREAMS; ++i) {
1584 sh_audio_t *sh = d->a_streams[i];
1585 if (sh)
1586 return sh->aid;
1588 return -1;
1591 int demuxer_default_sub_track(demuxer_t *d)
1593 int i;
1594 for (i = 0; i < MAX_S_STREAMS; ++i) {
1595 sh_sub_t *sh = d->s_streams[i];
1596 if (sh && sh->default_track)
1597 return sh->sid;
1599 return -1;