configure: remove --datadir and other obsolete things
[mplayer.git] / libmpdemux / demux_lavf.c
blob7103a5e1661aea432d6ea5e6353823325f43d514
1 /*
2 * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
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 <unistd.h>
24 #include <limits.h>
25 #include <stdbool.h>
26 #include <string.h>
28 #include <libavformat/avformat.h>
29 #include <libavformat/avio.h>
30 #include <libavutil/avutil.h>
31 #include <libavutil/avstring.h>
32 #include <libavutil/mathematics.h>
33 #include <libavutil/opt.h>
35 #include "config.h"
36 #include "options.h"
37 #include "mp_msg.h"
38 #include "av_opts.h"
39 #include "bstr.h"
41 #include "stream/stream.h"
42 #include "aviprint.h"
43 #include "demuxer.h"
44 #include "stheader.h"
45 #include "m_option.h"
47 #include "mp_taglists.h"
49 #define INITIAL_PROBE_SIZE STREAM_BUFFER_SIZE
50 #define SMALL_MAX_PROBE_SIZE (32 * 1024)
51 #define PROBE_BUF_SIZE (2 * 1024 * 1024)
53 const m_option_t lavfdopts_conf[] = {
54 OPT_INTRANGE("probesize", lavfdopts.probesize, 0, 32, INT_MAX),
55 OPT_STRING("format", lavfdopts.format, 0),
56 OPT_INTRANGE("analyzeduration", lavfdopts.analyzeduration, 0, 0, INT_MAX),
57 OPT_STRING("cryptokey", lavfdopts.cryptokey, 0),
58 OPT_STRING("o", lavfdopts.avopt, 0),
59 {NULL, NULL, 0, 0, 0, 0, NULL}
62 #define BIO_BUFFER_SIZE 32768
64 typedef struct lavf_priv {
65 AVInputFormat *avif;
66 AVFormatContext *avfc;
67 AVIOContext *pb;
68 uint8_t buffer[BIO_BUFFER_SIZE];
69 int audio_streams;
70 int video_streams;
71 int sub_streams;
72 int64_t last_pts;
73 int astreams[MAX_A_STREAMS];
74 int vstreams[MAX_V_STREAMS];
75 int sstreams[MAX_S_STREAMS];
76 int cur_program;
77 int nb_streams_last;
78 bool internet_radio_hack;
79 bool use_dts;
80 bool seek_by_bytes;
81 int bitrate;
82 } lavf_priv_t;
84 static int mp_read(void *opaque, uint8_t *buf, int size)
86 struct demuxer *demuxer = opaque;
87 struct stream *stream = demuxer->stream;
88 int ret;
90 ret = stream_read(stream, buf, size);
92 mp_msg(MSGT_HEADER, MSGL_DBG2,
93 "%d=mp_read(%p, %p, %d), pos: %"PRId64", eof:%d\n",
94 ret, stream, buf, size, stream_tell(stream), stream->eof);
95 return ret;
98 static int64_t mp_seek(void *opaque, int64_t pos, int whence)
100 struct demuxer *demuxer = opaque;
101 struct stream *stream = demuxer->stream;
102 int64_t current_pos;
103 mp_msg(MSGT_HEADER, MSGL_DBG2, "mp_seek(%p, %"PRId64", %d)\n",
104 stream, pos, whence);
105 if (whence == SEEK_CUR)
106 pos += stream_tell(stream);
107 else if (whence == SEEK_END && stream->end_pos > 0)
108 pos += stream->end_pos;
109 else if (whence == SEEK_SET)
110 pos += stream->start_pos;
111 else if (whence == AVSEEK_SIZE && stream->end_pos > 0) {
112 off_t size;
113 if (stream_control(stream, STREAM_CTRL_GET_SIZE, &size) == STREAM_OK)
114 return size;
115 return stream->end_pos - stream->start_pos;
116 } else
117 return -1;
119 if (pos < 0)
120 return -1;
121 current_pos = stream_tell(stream);
122 if (stream_seek(stream, pos) == 0) {
123 stream_reset(stream);
124 stream_seek(stream, current_pos);
125 return -1;
128 return pos - stream->start_pos;
131 static int64_t mp_read_seek(void *opaque, int stream_idx, int64_t ts, int flags)
133 struct demuxer *demuxer = opaque;
134 struct stream *stream = demuxer->stream;
135 struct lavf_priv *priv = demuxer->priv;
137 AVStream *st = priv->avfc->streams[stream_idx];
138 double pts = (double)ts * st->time_base.num / st->time_base.den;
139 int ret = stream_control(stream, STREAM_CTRL_SEEK_TO_TIME, &pts);
140 if (ret < 0)
141 ret = AVERROR(ENOSYS);
142 return ret;
145 static void list_formats(void)
147 mp_msg(MSGT_DEMUX, MSGL_INFO, "Available lavf input formats:\n");
148 AVInputFormat *fmt = NULL;
149 while ((fmt = av_iformat_next(fmt)))
150 mp_msg(MSGT_DEMUX, MSGL_INFO, "%15s : %s\n", fmt->name, fmt->long_name);
153 static int lavf_check_file(demuxer_t *demuxer)
155 struct MPOpts *opts = demuxer->opts;
156 struct lavfdopts *lavfdopts = &opts->lavfdopts;
157 AVProbeData avpd;
158 lavf_priv_t *priv;
159 int probe_data_size = 0;
160 int read_size = INITIAL_PROBE_SIZE;
161 int score;
163 if (!demuxer->priv)
164 demuxer->priv = calloc(sizeof(lavf_priv_t), 1);
165 priv = demuxer->priv;
167 char *format = lavfdopts->format;
168 if (!format)
169 format = demuxer->stream->lavf_type;
170 if (format) {
171 if (strcmp(format, "help") == 0) {
172 list_formats();
173 return 0;
175 priv->avif = av_find_input_format(format);
176 if (!priv->avif) {
177 mp_msg(MSGT_DEMUX, MSGL_FATAL, "Unknown lavf format %s\n", format);
178 return 0;
180 mp_msg(MSGT_DEMUX, MSGL_INFO, "Forced lavf %s demuxer\n",
181 priv->avif->long_name);
182 return DEMUXER_TYPE_LAVF;
185 avpd.buf = av_mallocz(FFMAX(BIO_BUFFER_SIZE, PROBE_BUF_SIZE) +
186 FF_INPUT_BUFFER_PADDING_SIZE);
187 do {
188 read_size = stream_read(demuxer->stream, avpd.buf + probe_data_size,
189 read_size);
190 if (read_size < 0) {
191 av_free(avpd.buf);
192 return 0;
194 probe_data_size += read_size;
195 avpd.filename = demuxer->stream->url;
196 if (!avpd.filename) {
197 mp_msg(MSGT_DEMUX, MSGL_WARN, "Stream url is not set!\n");
198 avpd.filename = "";
200 if (!strncmp(avpd.filename, "ffmpeg://", 9) ||
201 !strncmp(avpd.filename, "lavf://", 7))
202 avpd.filename += 9;
203 avpd.buf_size = probe_data_size;
205 score = 0;
206 priv->avif = av_probe_input_format2(&avpd, probe_data_size > 0, &score);
207 read_size = FFMIN(2 * read_size, PROBE_BUF_SIZE - probe_data_size);
208 } while ((demuxer->desc->type != DEMUXER_TYPE_LAVF_PREFERRED ||
209 probe_data_size < SMALL_MAX_PROBE_SIZE) &&
210 score <= AVPROBE_SCORE_MAX / 4 &&
211 read_size > 0 && probe_data_size < PROBE_BUF_SIZE);
212 av_free(avpd.buf);
214 if (!priv->avif || score <= AVPROBE_SCORE_MAX / 4) {
215 mp_msg(MSGT_HEADER, MSGL_V,
216 "LAVF_check: no clue about this gibberish!\n");
217 return 0;
218 } else
219 mp_msg(MSGT_HEADER, MSGL_V, "LAVF_check: %s\n", priv->avif->long_name);
221 demuxer->filetype = priv->avif->long_name;
222 if (!demuxer->filetype)
223 demuxer->filetype = priv->avif->name;
225 return DEMUXER_TYPE_LAVF;
228 static bool matches_avinputformat_name(struct lavf_priv *priv,
229 const char *name)
231 const char *avifname = priv->avif->name;
232 while (1) {
233 const char *next = strchr(avifname, ',');
234 if (!next)
235 return !strcmp(avifname, name);
236 int len = next - avifname;
237 if (len == strlen(name) && !memcmp(avifname, name, len))
238 return true;
239 avifname = next + 1;
243 /* formats for which an internal demuxer is preferred */
244 static const char * const preferred_internal[] = {
245 /* lavf Matroska demuxer doesn't support ordered chapters and fails
246 * for more files */
247 "matroska",
248 NULL
251 static int lavf_check_preferred_file(demuxer_t *demuxer)
253 if (lavf_check_file(demuxer)) {
254 const char * const *p;
255 lavf_priv_t *priv = demuxer->priv;
256 for (p = preferred_internal; *p; p++)
257 if (matches_avinputformat_name(priv, *p))
258 return 0;
259 return DEMUXER_TYPE_LAVF_PREFERRED;
261 return 0;
264 static uint8_t char2int(char c)
266 if (c >= '0' && c <= '9') return c - '0';
267 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
268 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
269 return 0;
272 static void parse_cryptokey(AVFormatContext *avfc, const char *str)
274 int len = strlen(str) / 2;
275 uint8_t *key = av_mallocz(len);
276 int i;
277 avfc->keylen = len;
278 avfc->key = key;
279 for (i = 0; i < len; i++, str += 2)
280 *key++ = (char2int(str[0]) << 4) | char2int(str[1]);
283 static void handle_stream(demuxer_t *demuxer, AVFormatContext *avfc, int i)
285 lavf_priv_t *priv = demuxer->priv;
286 AVStream *st = avfc->streams[i];
287 AVCodecContext *codec = st->codec;
288 char *stream_type = NULL;
289 int stream_id;
290 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
291 AVDictionaryEntry *title = av_dict_get(st->metadata, "title", NULL, 0);
292 // Don't use native MPEG codec tag values with our generic tag tables.
293 // May contain for example value 3 for MP3, which we'd map to PCM audio.
294 if (matches_avinputformat_name(priv, "mpeg") ||
295 matches_avinputformat_name(priv, "mpegts"))
296 codec->codec_tag = 0;
297 int override_tag = mp_taglist_override(codec->codec_id);
298 // For some formats (like PCM) always trust CODEC_ID_* more than codec_tag
299 if (override_tag)
300 codec->codec_tag = override_tag;
302 switch (codec->codec_type) {
303 case AVMEDIA_TYPE_AUDIO: {
304 WAVEFORMATEX *wf;
305 sh_audio_t *sh_audio;
306 sh_audio = new_sh_audio_aid(demuxer, i, priv->audio_streams);
307 if (!sh_audio)
308 break;
309 stream_type = "audio";
310 priv->astreams[priv->audio_streams] = i;
311 sh_audio->libav_codec_id = codec->codec_id;
312 wf = calloc(sizeof(*wf) + codec->extradata_size, 1);
313 // mp4a tag is used for all mp4 files no matter what they actually contain
314 if (codec->codec_tag == MKTAG('m', 'p', '4', 'a'))
315 codec->codec_tag = 0;
316 if (!codec->codec_tag)
317 codec->codec_tag = mp_taglist_audio(codec->codec_id);
318 if (!codec->codec_tag)
319 codec->codec_tag = -1;
320 wf->wFormatTag = codec->codec_tag;
321 wf->nChannels = codec->channels;
322 wf->nSamplesPerSec = codec->sample_rate;
323 wf->nAvgBytesPerSec = codec->bit_rate / 8;
324 wf->nBlockAlign = codec->block_align ? codec->block_align : 1;
325 wf->wBitsPerSample = codec->bits_per_coded_sample;
326 wf->cbSize = codec->extradata_size;
327 if (codec->extradata_size)
328 memcpy(wf + 1, codec->extradata, codec->extradata_size);
329 sh_audio->wf = wf;
330 sh_audio->audio.dwSampleSize = codec->block_align;
331 if (codec->frame_size && codec->sample_rate) {
332 sh_audio->audio.dwScale = codec->frame_size;
333 sh_audio->audio.dwRate = codec->sample_rate;
334 } else {
335 sh_audio->audio.dwScale = codec->block_align ? codec->block_align * 8 : 8;
336 sh_audio->audio.dwRate = codec->bit_rate;
338 int g = av_gcd(sh_audio->audio.dwScale, sh_audio->audio.dwRate);
339 sh_audio->audio.dwScale /= g;
340 sh_audio->audio.dwRate /= g;
341 // printf("sca:%d rat:%d fs:%d sr:%d ba:%d\n", sh_audio->audio.dwScale, sh_audio->audio.dwRate, codec->frame_size, codec->sample_rate, codec->block_align);
342 sh_audio->ds = demuxer->audio;
343 sh_audio->format = codec->codec_tag;
344 sh_audio->channels = codec->channels;
345 sh_audio->samplerate = codec->sample_rate;
346 sh_audio->i_bps = codec->bit_rate / 8;
347 switch (codec->codec_id) {
348 case CODEC_ID_PCM_ALAW:
349 sh_audio->format = 0x6;
350 break;
351 case CODEC_ID_PCM_MULAW:
352 sh_audio->format = 0x7;
353 break;
355 if (title && title->value) {
356 sh_audio->title = talloc_strdup(sh_audio, title->value);
357 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_NAME=%s\n",
358 priv->audio_streams, title->value);
360 if (lang && lang->value) {
361 sh_audio->lang = talloc_strdup(sh_audio, lang->value);
362 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_LANG=%s\n",
363 priv->audio_streams, sh_audio->lang);
365 if (st->disposition & AV_DISPOSITION_DEFAULT)
366 sh_audio->default_track = 1;
367 if (mp_msg_test(MSGT_HEADER, MSGL_V))
368 print_wave_header(sh_audio->wf, MSGL_V);
369 st->discard = AVDISCARD_ALL;
370 stream_id = priv->audio_streams++;
371 break;
373 case AVMEDIA_TYPE_VIDEO: {
374 sh_video_t *sh_video;
375 BITMAPINFOHEADER *bih;
376 sh_video = new_sh_video_vid(demuxer, i, priv->video_streams);
377 if (!sh_video)
378 break;
379 stream_type = "video";
380 priv->vstreams[priv->video_streams] = i;
381 sh_video->libav_codec_id = codec->codec_id;
382 bih = calloc(sizeof(*bih) + codec->extradata_size, 1);
384 if (codec->codec_id == CODEC_ID_RAWVIDEO) {
385 switch (codec->pix_fmt) {
386 case PIX_FMT_RGB24:
387 codec->codec_tag = MKTAG(24, 'B', 'G', 'R');
388 case PIX_FMT_BGR24:
389 codec->codec_tag = MKTAG(24, 'R', 'G', 'B');
391 if (!codec->codec_tag)
392 codec->codec_tag = avcodec_pix_fmt_to_codec_tag(codec->pix_fmt);
393 } else if (!codec->codec_tag) {
394 codec->codec_tag = mp_taglist_video(codec->codec_id);
395 /* 0 might mean either unset or rawvideo; if codec_id
396 * was not RAWVIDEO assume it's unset
398 if (!codec->codec_tag)
399 codec->codec_tag = -1;
401 bih->biSize = sizeof(*bih) + codec->extradata_size;
402 bih->biWidth = codec->width;
403 bih->biHeight = codec->height;
404 bih->biBitCount = codec->bits_per_coded_sample;
405 bih->biSizeImage = bih->biWidth * bih->biHeight * bih->biBitCount / 8;
406 bih->biCompression = codec->codec_tag;
407 sh_video->bih = bih;
408 sh_video->disp_w = codec->width;
409 sh_video->disp_h = codec->height;
410 if (st->time_base.den) { /* if container has time_base, use that */
411 sh_video->video.dwRate = st->time_base.den;
412 sh_video->video.dwScale = st->time_base.num;
413 } else {
414 sh_video->video.dwRate = codec->time_base.den;
415 sh_video->video.dwScale = codec->time_base.num;
417 /* Try to make up some frame rate value, even if it's not reliable.
418 * FPS information is needed to support subtitle formats which base
419 * timing on frame numbers.
420 * Libavformat seems to report no "reliable" FPS value for AVI files,
421 * while they are typically constant enough FPS that the value this
422 * heuristic makes up works with subtitles in practice.
424 double fps;
425 if (st->r_frame_rate.num)
426 fps = av_q2d(st->r_frame_rate);
427 else
428 fps = 1.0 / FFMAX(av_q2d(st->time_base),
429 av_q2d(st->codec->time_base) *
430 st->codec->ticks_per_frame);
431 sh_video->fps = fps;
432 sh_video->frametime = 1 / fps;
433 sh_video->format = bih->biCompression;
434 if (st->sample_aspect_ratio.num)
435 sh_video->aspect = codec->width * st->sample_aspect_ratio.num
436 / (float)(codec->height * st->sample_aspect_ratio.den);
437 else
438 sh_video->aspect = codec->width * codec->sample_aspect_ratio.num
439 / (float)(codec->height * codec->sample_aspect_ratio.den);
440 sh_video->i_bps = codec->bit_rate / 8;
441 if (title && title->value) {
442 sh_video->title = talloc_strdup(sh_video, title->value);
443 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VID_%d_NAME=%s\n",
444 priv->video_streams, title->value);
446 mp_msg(MSGT_DEMUX, MSGL_DBG2, "aspect= %d*%d/(%d*%d)\n",
447 codec->width, codec->sample_aspect_ratio.num,
448 codec->height, codec->sample_aspect_ratio.den);
450 sh_video->ds = demuxer->video;
451 if (codec->extradata_size)
452 memcpy(sh_video->bih + 1, codec->extradata, codec->extradata_size);
453 if ( mp_msg_test(MSGT_HEADER, MSGL_V))
454 print_video_header(sh_video->bih, MSGL_V);
455 st->discard = AVDISCARD_ALL;
456 stream_id = priv->video_streams++;
457 break;
459 case AVMEDIA_TYPE_SUBTITLE: {
460 sh_sub_t *sh_sub;
461 char type;
462 /* only support text subtitles for now */
463 if (codec->codec_id == CODEC_ID_TEXT)
464 type = 't';
465 else if (codec->codec_id == CODEC_ID_MOV_TEXT)
466 type = 'm';
467 else if (codec->codec_id == CODEC_ID_SSA)
468 type = 'a';
469 else if (codec->codec_id == CODEC_ID_DVD_SUBTITLE)
470 type = 'v';
471 else if (codec->codec_id == CODEC_ID_XSUB)
472 type = 'x';
473 else if (codec->codec_id == CODEC_ID_DVB_SUBTITLE)
474 type = 'b';
475 else if (codec->codec_id == CODEC_ID_DVB_TELETEXT)
476 type = 'd';
477 else if (codec->codec_id == CODEC_ID_HDMV_PGS_SUBTITLE)
478 type = 'p';
479 else
480 break;
481 sh_sub = new_sh_sub_sid(demuxer, i, priv->sub_streams);
482 if (!sh_sub)
483 break;
484 stream_type = "subtitle";
485 priv->sstreams[priv->sub_streams] = i;
486 sh_sub->libav_codec_id = codec->codec_id;
487 sh_sub->type = type;
488 if (codec->extradata_size) {
489 sh_sub->extradata = malloc(codec->extradata_size);
490 memcpy(sh_sub->extradata, codec->extradata, codec->extradata_size);
491 sh_sub->extradata_len = codec->extradata_size;
493 if (title && title->value) {
494 sh_sub->title = talloc_strdup(sh_sub, title->value);
495 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_NAME=%s\n",
496 priv->sub_streams, title->value);
498 if (lang && lang->value) {
499 sh_sub->lang = talloc_strdup(sh_sub, lang->value);
500 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_LANG=%s\n",
501 priv->sub_streams, sh_sub->lang);
503 if (st->disposition & AV_DISPOSITION_DEFAULT)
504 sh_sub->default_track = 1;
505 stream_id = priv->sub_streams++;
506 break;
508 case AVMEDIA_TYPE_ATTACHMENT: {
509 AVDictionaryEntry *ftag = av_dict_get(st->metadata, "filename",
510 NULL, 0);
511 char *filename = ftag ? ftag->value : NULL;
512 if (st->codec->codec_id == CODEC_ID_TTF)
513 demuxer_add_attachment(demuxer, bstr(filename),
514 bstr("application/x-truetype-font"),
515 (struct bstr){codec->extradata,
516 codec->extradata_size});
517 break;
519 default:
520 st->discard = AVDISCARD_ALL;
522 if (stream_type) {
523 AVCodec *avc = avcodec_find_decoder(codec->codec_id);
524 const char *codec_name = avc ? avc->name : "unknown";
525 if (!avc && *stream_type == 's' && demuxer->s_streams[i])
526 codec_name = sh_sub_type2str((demuxer->s_streams[i])->type);
527 mp_msg(MSGT_DEMUX, MSGL_INFO, "[lavf] stream %d: %s (%s), -%cid %d",
528 i, stream_type, codec_name, *stream_type, stream_id);
529 if (lang && lang->value && *stream_type != 'v')
530 mp_msg(MSGT_DEMUX, MSGL_INFO, ", -%clang %s",
531 *stream_type, lang->value);
532 if (title && title->value)
533 mp_msg(MSGT_DEMUX, MSGL_INFO, ", %s", title->value);
534 mp_msg(MSGT_DEMUX, MSGL_INFO, "\n");
538 static demuxer_t *demux_open_lavf(demuxer_t *demuxer)
540 struct MPOpts *opts = demuxer->opts;
541 struct lavfdopts *lavfdopts = &opts->lavfdopts;
542 AVFormatContext *avfc;
543 AVDictionaryEntry *t = NULL;
544 lavf_priv_t *priv = demuxer->priv;
545 int i;
547 stream_seek(demuxer->stream, 0);
549 avfc = avformat_alloc_context();
551 if (lavfdopts->cryptokey)
552 parse_cryptokey(avfc, lavfdopts->cryptokey);
553 if (matches_avinputformat_name(priv, "avi")) {
554 /* for avi libavformat returns the avi timestamps in .dts,
555 * some made-up stuff that's not really pts in .pts */
556 priv->use_dts = true;
557 demuxer->timestamp_type = TIMESTAMP_TYPE_SORT;
558 } else {
559 if (opts->user_correct_pts != 0)
560 avfc->flags |= AVFMT_FLAG_GENPTS;
562 if (index_mode == 0)
563 avfc->flags |= AVFMT_FLAG_IGNIDX;
565 if (lavfdopts->probesize) {
566 if (av_opt_set_int(avfc, "probesize", lavfdopts->probesize, 0) < 0)
567 mp_msg(MSGT_HEADER, MSGL_ERR,
568 "demux_lavf, couldn't set option probesize to %u\n",
569 lavfdopts->probesize);
571 if (lavfdopts->analyzeduration) {
572 if (av_opt_set_int(avfc, "analyzeduration",
573 lavfdopts->analyzeduration * AV_TIME_BASE, 0) < 0)
574 mp_msg(MSGT_HEADER, MSGL_ERR, "demux_lavf, couldn't set option "
575 "analyzeduration to %u\n", lavfdopts->analyzeduration);
578 if (lavfdopts->avopt) {
579 if (parse_avopts(avfc, lavfdopts->avopt) < 0) {
580 mp_msg(MSGT_HEADER, MSGL_ERR,
581 "Your options /%s/ look like gibberish to me pal\n",
582 lavfdopts->avopt);
583 return NULL;
587 char *filename = demuxer->stream->url;
589 if (filename) {
590 if (demuxer->stream->lavf_type && !strcmp(demuxer->stream->lavf_type,
591 "rtsp")) {
592 // Remove possible leading ffmpeg:// or lavf://
593 char *name = strstr(filename, "rtsp:");
594 if (name)
595 filename = name;
597 } else
598 filename = "mp:unknown";
600 if (!(priv->avif->flags & AVFMT_NOFILE)) {
601 priv->pb = avio_alloc_context(priv->buffer, BIO_BUFFER_SIZE, 0,
602 demuxer, mp_read, NULL, mp_seek);
603 priv->pb->read_seek = mp_read_seek;
604 priv->pb->seekable = demuxer->stream->end_pos
605 && (demuxer->stream->flags & MP_STREAM_SEEK) == MP_STREAM_SEEK
606 ? AVIO_SEEKABLE_NORMAL : 0;
607 avfc->pb = priv->pb;
610 if (avformat_open_input(&avfc, filename, priv->avif, NULL) < 0) {
611 mp_msg(MSGT_HEADER, MSGL_ERR,
612 "LAVF_header: avformat_open_input() failed\n");
613 return NULL;
616 priv->avfc = avfc;
618 if (avformat_find_stream_info(avfc, NULL) < 0) {
619 mp_msg(MSGT_HEADER, MSGL_ERR,
620 "LAVF_header: av_find_stream_info() failed\n");
621 return NULL;
624 /* Add metadata. */
625 while ((t = av_dict_get(avfc->metadata, "", t,
626 AV_DICT_IGNORE_SUFFIX)))
627 demux_info_add(demuxer, t->key, t->value);
629 for (i = 0; i < avfc->nb_chapters; i++) {
630 AVChapter *c = avfc->chapters[i];
631 uint64_t start = av_rescale_q(c->start, c->time_base,
632 (AVRational){1, 1000000000});
633 uint64_t end = av_rescale_q(c->end, c->time_base,
634 (AVRational){1, 1000000000});
635 t = av_dict_get(c->metadata, "title", NULL, 0);
636 demuxer_add_chapter(demuxer, t ? bstr(t->value) : bstr(NULL),
637 start, end);
640 for (i = 0; i < avfc->nb_streams; i++)
641 handle_stream(demuxer, avfc, i);
642 priv->nb_streams_last = avfc->nb_streams;
644 if (avfc->nb_programs) {
645 int p;
646 for (p = 0; p < avfc->nb_programs; p++) {
647 AVProgram *program = avfc->programs[p];
648 t = av_dict_get(program->metadata, "title", NULL, 0);
649 mp_msg(MSGT_HEADER, MSGL_INFO, "LAVF: Program %d %s\n",
650 program->id, t ? t->value : "");
651 mp_msg(MSGT_IDENTIFY, MSGL_V, "PROGRAM_ID=%d\n", program->id);
655 mp_msg(MSGT_HEADER, MSGL_V, "LAVF: %d audio and %d video streams found\n",
656 priv->audio_streams, priv->video_streams);
657 demuxer->audio->id = -2; // wait for higher-level code to select track
658 /* There's still no global video track selection, so we have to pick
659 * the default video track in the demuxer.
660 * Note that the interface doesn't make sense: demuxer->video->id
661 * is initialized to --vid value, but after demuxer initialization
662 * it contains either -2 for disabled or v_streams[] index. */
663 int vid = demuxer->video->id;
664 demuxer->video->id = -2;
665 demuxer->desc->control(demuxer, DEMUXER_CTRL_SWITCH_VIDEO, &vid);
666 if (demuxer->video->id >= 0)
667 demuxer->video->sh = demuxer->v_streams[demuxer->video->id];
669 // disabled because unreliable per-stream bitrate values returned
670 // by libavformat trigger this heuristic incorrectly and break things
671 #if 0
672 /* libavformat sets bitrate for mpeg based on pts at start and end
673 * of file, which fails for files with pts resets. So calculate our
674 * own bitrate estimate. */
675 if (priv->avif->flags & AVFMT_TS_DISCONT) {
676 for (int i = 0; i < avfc->nb_streams; i++)
677 priv->bitrate += avfc->streams[i]->codec->bit_rate;
678 /* pts-based is more accurate if there are no resets; try to make
679 * a somewhat reasonable guess */
680 if (!avfc->duration || avfc->duration == AV_NOPTS_VALUE
681 || priv->bitrate && (avfc->bit_rate < priv->bitrate / 2
682 || avfc->bit_rate > priv->bitrate * 2))
683 priv->seek_by_bytes = true;
684 if (!priv->bitrate)
685 priv->bitrate = 1440000;
687 #endif
688 demuxer->accurate_seek = !priv->seek_by_bytes;
690 return demuxer;
693 static void check_internet_radio_hack(struct demuxer *demuxer)
695 struct lavf_priv *priv = demuxer->priv;
696 struct AVFormatContext *avfc = priv->avfc;
698 if (!matches_avinputformat_name(priv, "ogg"))
699 return;
700 if (priv->nb_streams_last == avfc->nb_streams)
701 return;
702 if (avfc->nb_streams - priv->nb_streams_last == 1
703 && priv->video_streams == 0 && priv->sub_streams == 0
704 && demuxer->a_streams[priv->audio_streams - 1]->format == 0x566f // vorbis
705 && (priv->audio_streams == 2 || priv->internet_radio_hack)
706 && demuxer->a_streams[0]->format == 0x566f) {
707 // extradata match could be checked but would require parsing
708 // headers, as the comment section will vary
709 if (!priv->internet_radio_hack) {
710 mp_msg(MSGT_DEMUX, MSGL_V,
711 "[lavf] enabling internet ogg radio hack\n");
713 priv->internet_radio_hack = true;
714 // use new per-track metadata as global metadata
715 AVDictionaryEntry *t = NULL;
716 AVStream *stream = avfc->streams[avfc->nb_streams - 1];
717 while ((t = av_dict_get(stream->metadata, "", t,
718 AV_DICT_IGNORE_SUFFIX)))
719 demux_info_add(demuxer, t->key, t->value);
720 } else {
721 if (priv->internet_radio_hack)
722 mp_tmsg(MSGT_DEMUX, MSGL_WARN, "[lavf] Internet radio ogg hack "
723 "was enabled, but stream characteristics changed.\n"
724 "This may or may not work.\n");
725 priv->internet_radio_hack = false;
729 static int destroy_avpacket(void *pkt)
731 av_free_packet(pkt);
732 return 0;
735 static int demux_lavf_fill_buffer(demuxer_t *demux, demux_stream_t *dsds)
737 lavf_priv_t *priv = demux->priv;
738 demux_packet_t *dp;
739 demux_stream_t *ds;
740 int id;
741 mp_msg(MSGT_DEMUX, MSGL_DBG2, "demux_lavf_fill_buffer()\n");
743 demux->filepos = stream_tell(demux->stream);
745 AVPacket *pkt = talloc(NULL, AVPacket);
746 if (av_read_frame(priv->avfc, pkt) < 0) {
747 talloc_free(pkt);
748 return 0;
750 talloc_set_destructor(pkt, destroy_avpacket);
752 // handle any new streams that might have been added
753 for (id = priv->nb_streams_last; id < priv->avfc->nb_streams; id++)
754 handle_stream(demux, priv->avfc, id);
755 check_internet_radio_hack(demux);
757 priv->nb_streams_last = priv->avfc->nb_streams;
759 id = pkt->stream_index;
761 if (id == demux->audio->id || priv->internet_radio_hack) {
762 // audio
763 ds = demux->audio;
764 } else if (id == demux->video->id) {
765 // video
766 ds = demux->video;
767 } else if (id == demux->sub->id) {
768 // subtitle
769 ds = demux->sub;
770 } else {
771 talloc_free(pkt);
772 return 1;
775 // If the packet has pointers to temporary fields that could be
776 // overwritten/freed by next av_read_frame(), copy them to persistent
777 // allocations so we can safely queue the packet for any length of time.
778 if (av_dup_packet(pkt) < 0)
779 abort();
780 dp = new_demux_packet_fromdata(pkt->data, pkt->size);
781 dp->avpacket = pkt;
783 int64_t ts = priv->use_dts ? pkt->dts : pkt->pts;
784 if (ts != AV_NOPTS_VALUE) {
785 dp->pts = ts * av_q2d(priv->avfc->streams[id]->time_base);
786 priv->last_pts = dp->pts * AV_TIME_BASE;
787 // always set duration for subtitles, even if AV_PKT_FLAG_KEY isn't set,
788 // otherwise they will stay on screen to long if e.g. ASS is demuxed
789 // from mkv
790 if ((ds == demux->sub || (pkt->flags & AV_PKT_FLAG_KEY)) &&
791 pkt->convergence_duration > 0)
792 dp->duration = pkt->convergence_duration *
793 av_q2d(priv->avfc->streams[id]->time_base);
795 dp->pos = demux->filepos;
796 dp->keyframe = pkt->flags & AV_PKT_FLAG_KEY;
797 // append packet to DS stream:
798 ds_add_packet(ds, dp);
799 return 1;
802 static void demux_seek_lavf(demuxer_t *demuxer, float rel_seek_secs,
803 float audio_delay, int flags)
805 lavf_priv_t *priv = demuxer->priv;
806 int avsflags = 0;
807 mp_msg(MSGT_DEMUX, MSGL_DBG2, "demux_seek_lavf(%p, %f, %f, %d)\n",
808 demuxer, rel_seek_secs, audio_delay, flags);
810 if (priv->seek_by_bytes) {
811 int64_t pos = demuxer->filepos;
812 rel_seek_secs *= priv->bitrate / 8;
813 pos += rel_seek_secs;
814 av_seek_frame(priv->avfc, -1, pos, AVSEEK_FLAG_BYTE);
815 return;
818 if (flags & SEEK_ABSOLUTE)
819 priv->last_pts = 0;
820 else if (rel_seek_secs < 0)
821 avsflags = AVSEEK_FLAG_BACKWARD;
822 if (flags & SEEK_FORWARD)
823 avsflags = 0;
824 else if (flags & SEEK_BACKWARD)
825 avsflags = AVSEEK_FLAG_BACKWARD;
826 if (flags & SEEK_FACTOR) {
827 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
828 return;
829 priv->last_pts += rel_seek_secs * priv->avfc->duration;
830 } else
831 priv->last_pts += rel_seek_secs * AV_TIME_BASE;
832 if (av_seek_frame(priv->avfc, -1, priv->last_pts, avsflags) < 0) {
833 avsflags ^= AVSEEK_FLAG_BACKWARD;
834 av_seek_frame(priv->avfc, -1, priv->last_pts, avsflags);
838 static int demux_lavf_control(demuxer_t *demuxer, int cmd, void *arg)
840 lavf_priv_t *priv = demuxer->priv;
842 switch (cmd) {
843 case DEMUXER_CTRL_CORRECT_PTS:
844 return DEMUXER_CTRL_OK;
845 case DEMUXER_CTRL_GET_TIME_LENGTH:
846 if (priv->seek_by_bytes) {
847 /* Our bitrate estimate may be better than would be used in
848 * otherwise similar fallback code at higher level */
849 if (demuxer->movi_end <= 0)
850 return DEMUXER_CTRL_DONTKNOW;
851 *(double *)arg = (demuxer->movi_end - demuxer->movi_start) * 8 /
852 priv->bitrate;
853 return DEMUXER_CTRL_GUESS;
855 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
856 return DEMUXER_CTRL_DONTKNOW;
858 *((double *)arg) = (double)priv->avfc->duration / AV_TIME_BASE;
859 return DEMUXER_CTRL_OK;
861 case DEMUXER_CTRL_GET_PERCENT_POS:
862 if (priv->seek_by_bytes)
863 return DEMUXER_CTRL_DONTKNOW; // let it use the fallback code
864 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
865 return DEMUXER_CTRL_DONTKNOW;
867 *((int *)arg) = (int)((priv->last_pts - priv->avfc->start_time) * 100 /
868 priv->avfc->duration);
869 return DEMUXER_CTRL_OK;
870 case DEMUXER_CTRL_SWITCH_AUDIO:
871 case DEMUXER_CTRL_SWITCH_VIDEO:
873 int id = *((int *)arg);
874 int newid = -2;
875 int i, curridx = -1;
876 int nstreams, *pstreams;
877 demux_stream_t *ds;
879 if (cmd == DEMUXER_CTRL_SWITCH_VIDEO) {
880 ds = demuxer->video;
881 nstreams = priv->video_streams;
882 pstreams = priv->vstreams;
883 } else {
884 ds = demuxer->audio;
885 nstreams = priv->audio_streams;
886 pstreams = priv->astreams;
888 for (i = 0; i < nstreams; i++) {
889 if (pstreams[i] == ds->id) { //current stream id
890 curridx = i;
891 break;
895 if (id == -1) { // next track
896 i = (curridx + 2) % (nstreams + 1) - 1;
897 if (i >= 0)
898 newid = pstreams[i];
899 } else if (id >= 0 && id < nstreams) { // select track by id
900 i = id;
901 newid = pstreams[i];
902 } else // no sound
903 i = -1;
905 if (i == curridx) {
906 *(int *) arg = curridx < 0 ? -2 : curridx;
907 return DEMUXER_CTRL_OK;
908 } else {
909 ds_free_packs(ds);
910 if (ds->id >= 0)
911 priv->avfc->streams[ds->id]->discard = AVDISCARD_ALL;
912 ds->id = newid;
913 *(int *) arg = i < 0 ? -2 : i;
914 if (newid >= 0)
915 priv->avfc->streams[newid]->discard = AVDISCARD_NONE;
916 return DEMUXER_CTRL_OK;
919 case DEMUXER_CTRL_IDENTIFY_PROGRAM:
921 demux_program_t *prog = arg;
922 AVProgram *program;
923 int p, i;
924 int start;
926 prog->vid = prog->aid = prog->sid = -2;
927 if (priv->avfc->nb_programs < 1)
928 return DEMUXER_CTRL_DONTKNOW;
930 if (prog->progid == -1) {
931 p = 0;
932 while (p < priv->avfc->nb_programs && priv->avfc->programs[p]->id != priv->cur_program)
933 p++;
934 p = (p + 1) % priv->avfc->nb_programs;
935 } else {
936 for (i = 0; i < priv->avfc->nb_programs; i++)
937 if (priv->avfc->programs[i]->id == prog->progid)
938 break;
939 if (i == priv->avfc->nb_programs)
940 return DEMUXER_CTRL_DONTKNOW;
941 p = i;
943 start = p;
944 redo:
945 program = priv->avfc->programs[p];
946 for (i = 0; i < program->nb_stream_indexes; i++) {
947 switch (priv->avfc->streams[program->stream_index[i]]->codec->codec_type) {
948 case AVMEDIA_TYPE_VIDEO:
949 if (prog->vid == -2)
950 prog->vid = program->stream_index[i];
951 break;
952 case AVMEDIA_TYPE_AUDIO:
953 if (prog->aid == -2)
954 prog->aid = program->stream_index[i];
955 break;
956 case AVMEDIA_TYPE_SUBTITLE:
957 if (prog->sid == -2 && priv->avfc->streams[program->stream_index[i]]->codec->codec_id == CODEC_ID_TEXT)
958 prog->sid = program->stream_index[i];
959 break;
962 if (prog->aid >= 0 && prog->aid < MAX_A_STREAMS &&
963 demuxer->a_streams[prog->aid]) {
964 sh_audio_t *sh = demuxer->a_streams[prog->aid];
965 prog->aid = sh->aid;
966 } else
967 prog->aid = -2;
968 if (prog->vid >= 0 && prog->vid < MAX_V_STREAMS &&
969 demuxer->v_streams[prog->vid]) {
970 sh_video_t *sh = demuxer->v_streams[prog->vid];
971 prog->vid = sh->vid;
972 } else
973 prog->vid = -2;
974 if (prog->progid == -1 && prog->vid == -2 && prog->aid == -2) {
975 p = (p + 1) % priv->avfc->nb_programs;
976 if (p == start)
977 return DEMUXER_CTRL_DONTKNOW;
978 goto redo;
980 priv->cur_program = prog->progid = program->id;
981 return DEMUXER_CTRL_OK;
983 default:
984 return DEMUXER_CTRL_NOTIMPL;
988 static void demux_close_lavf(demuxer_t *demuxer)
990 lavf_priv_t *priv = demuxer->priv;
991 if (priv) {
992 if (priv->avfc) {
993 av_freep(&priv->avfc->key);
994 avformat_close_input(&priv->avfc);
996 av_freep(&priv->pb);
997 free(priv);
998 demuxer->priv = NULL;
1003 const demuxer_desc_t demuxer_desc_lavf = {
1004 "libavformat demuxer",
1005 "lavf",
1006 "libavformat",
1007 "Michael Niedermayer",
1008 "supports many formats, requires libavformat",
1009 DEMUXER_TYPE_LAVF,
1010 0, // Check after other demuxer
1011 lavf_check_file,
1012 demux_lavf_fill_buffer,
1013 demux_open_lavf,
1014 demux_close_lavf,
1015 demux_seek_lavf,
1016 demux_lavf_control
1019 const demuxer_desc_t demuxer_desc_lavf_preferred = {
1020 "libavformat preferred demuxer",
1021 "lavfpref",
1022 "libavformat",
1023 "Michael Niedermayer",
1024 "supports many formats, requires libavformat",
1025 DEMUXER_TYPE_LAVF_PREFERRED,
1027 lavf_check_preferred_file,
1028 demux_lavf_fill_buffer,
1029 demux_open_lavf,
1030 demux_close_lavf,
1031 demux_seek_lavf,
1032 demux_lavf_control