libav: switch from CODEC_ID to AV_CODEC_ID
[mplayer.git] / libmpdemux / demux_lavf.c
blob5f13b3b4d294864844ecada5b32e3a1e84c68f1e
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 AV_CODEC_ID_PCM_ALAW:
349 sh_audio->format = 0x6;
350 break;
351 case AV_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 == AV_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 LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(54, 2, 0)
454 sh_video->is_attached_pic =
455 st->disposition & AV_DISPOSITION_ATTACHED_PIC;
456 #endif
457 if ( mp_msg_test(MSGT_HEADER, MSGL_V))
458 print_video_header(sh_video->bih, MSGL_V);
459 st->discard = AVDISCARD_ALL;
460 stream_id = priv->video_streams++;
461 break;
463 case AVMEDIA_TYPE_SUBTITLE: {
464 sh_sub_t *sh_sub;
465 char type;
466 /* only support text subtitles for now */
467 if (codec->codec_id == AV_CODEC_ID_TEXT)
468 type = 't';
469 else if (codec->codec_id == AV_CODEC_ID_MOV_TEXT)
470 type = 'm';
471 else if (codec->codec_id == AV_CODEC_ID_SSA)
472 type = 'a';
473 else if (codec->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
474 type = 'v';
475 else if (codec->codec_id == AV_CODEC_ID_XSUB)
476 type = 'x';
477 else if (codec->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
478 type = 'b';
479 else if (codec->codec_id == AV_CODEC_ID_DVB_TELETEXT)
480 type = 'd';
481 else if (codec->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE)
482 type = 'p';
483 else
484 break;
485 sh_sub = new_sh_sub_sid(demuxer, i, priv->sub_streams);
486 if (!sh_sub)
487 break;
488 stream_type = "subtitle";
489 priv->sstreams[priv->sub_streams] = i;
490 sh_sub->libav_codec_id = codec->codec_id;
491 sh_sub->type = type;
492 if (codec->extradata_size) {
493 sh_sub->extradata = malloc(codec->extradata_size);
494 memcpy(sh_sub->extradata, codec->extradata, codec->extradata_size);
495 sh_sub->extradata_len = codec->extradata_size;
497 if (title && title->value) {
498 sh_sub->title = talloc_strdup(sh_sub, title->value);
499 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_NAME=%s\n",
500 priv->sub_streams, title->value);
502 if (lang && lang->value) {
503 sh_sub->lang = talloc_strdup(sh_sub, lang->value);
504 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_LANG=%s\n",
505 priv->sub_streams, sh_sub->lang);
507 if (st->disposition & AV_DISPOSITION_DEFAULT)
508 sh_sub->default_track = 1;
509 stream_id = priv->sub_streams++;
510 break;
512 case AVMEDIA_TYPE_ATTACHMENT: {
513 AVDictionaryEntry *ftag = av_dict_get(st->metadata, "filename",
514 NULL, 0);
515 char *filename = ftag ? ftag->value : NULL;
516 if (st->codec->codec_id == AV_CODEC_ID_TTF)
517 demuxer_add_attachment(demuxer, bstr(filename),
518 bstr("application/x-truetype-font"),
519 (struct bstr){codec->extradata,
520 codec->extradata_size});
521 break;
523 default:
524 st->discard = AVDISCARD_ALL;
526 if (stream_type) {
527 AVCodec *avc = avcodec_find_decoder(codec->codec_id);
528 const char *codec_name = avc ? avc->name : "unknown";
529 if (!avc && *stream_type == 's' && demuxer->s_streams[i])
530 codec_name = sh_sub_type2str((demuxer->s_streams[i])->type);
531 mp_msg(MSGT_DEMUX, MSGL_INFO, "[lavf] stream %d: %s (%s), -%cid %d",
532 i, stream_type, codec_name, *stream_type, stream_id);
533 if (lang && lang->value && *stream_type != 'v')
534 mp_msg(MSGT_DEMUX, MSGL_INFO, ", -%clang %s",
535 *stream_type, lang->value);
536 if (title && title->value)
537 mp_msg(MSGT_DEMUX, MSGL_INFO, ", %s", title->value);
538 mp_msg(MSGT_DEMUX, MSGL_INFO, "\n");
542 static demuxer_t *demux_open_lavf(demuxer_t *demuxer)
544 struct MPOpts *opts = demuxer->opts;
545 struct lavfdopts *lavfdopts = &opts->lavfdopts;
546 AVFormatContext *avfc;
547 AVDictionaryEntry *t = NULL;
548 lavf_priv_t *priv = demuxer->priv;
549 int i;
551 stream_seek(demuxer->stream, 0);
553 avfc = avformat_alloc_context();
555 if (lavfdopts->cryptokey)
556 parse_cryptokey(avfc, lavfdopts->cryptokey);
557 if (matches_avinputformat_name(priv, "avi")) {
558 /* for avi libavformat returns the avi timestamps in .dts,
559 * some made-up stuff that's not really pts in .pts */
560 priv->use_dts = true;
561 demuxer->timestamp_type = TIMESTAMP_TYPE_SORT;
562 } else {
563 if (opts->user_correct_pts != 0)
564 avfc->flags |= AVFMT_FLAG_GENPTS;
566 if (index_mode == 0)
567 avfc->flags |= AVFMT_FLAG_IGNIDX;
569 if (lavfdopts->probesize) {
570 if (av_opt_set_int(avfc, "probesize", lavfdopts->probesize, 0) < 0)
571 mp_msg(MSGT_HEADER, MSGL_ERR,
572 "demux_lavf, couldn't set option probesize to %u\n",
573 lavfdopts->probesize);
575 if (lavfdopts->analyzeduration) {
576 if (av_opt_set_int(avfc, "analyzeduration",
577 lavfdopts->analyzeduration * AV_TIME_BASE, 0) < 0)
578 mp_msg(MSGT_HEADER, MSGL_ERR, "demux_lavf, couldn't set option "
579 "analyzeduration to %u\n", lavfdopts->analyzeduration);
582 if (lavfdopts->avopt) {
583 if (parse_avopts(avfc, lavfdopts->avopt) < 0) {
584 mp_msg(MSGT_HEADER, MSGL_ERR,
585 "Your options /%s/ look like gibberish to me pal\n",
586 lavfdopts->avopt);
587 return NULL;
591 char *filename = demuxer->stream->url;
593 if (filename) {
594 if (demuxer->stream->lavf_type && !strcmp(demuxer->stream->lavf_type,
595 "rtsp")) {
596 // Remove possible leading ffmpeg:// or lavf://
597 char *name = strstr(filename, "rtsp:");
598 if (name)
599 filename = name;
601 } else
602 filename = "mp:unknown";
604 if (!(priv->avif->flags & AVFMT_NOFILE)) {
605 priv->pb = avio_alloc_context(priv->buffer, BIO_BUFFER_SIZE, 0,
606 demuxer, mp_read, NULL, mp_seek);
607 priv->pb->read_seek = mp_read_seek;
608 priv->pb->seekable = demuxer->stream->end_pos
609 && (demuxer->stream->flags & MP_STREAM_SEEK) == MP_STREAM_SEEK
610 ? AVIO_SEEKABLE_NORMAL : 0;
611 avfc->pb = priv->pb;
614 if (avformat_open_input(&avfc, filename, priv->avif, NULL) < 0) {
615 mp_msg(MSGT_HEADER, MSGL_ERR,
616 "LAVF_header: avformat_open_input() failed\n");
617 return NULL;
620 priv->avfc = avfc;
622 if (avformat_find_stream_info(avfc, NULL) < 0) {
623 mp_msg(MSGT_HEADER, MSGL_ERR,
624 "LAVF_header: av_find_stream_info() failed\n");
625 return NULL;
628 /* Add metadata. */
629 while ((t = av_dict_get(avfc->metadata, "", t,
630 AV_DICT_IGNORE_SUFFIX)))
631 demux_info_add(demuxer, t->key, t->value);
633 for (i = 0; i < avfc->nb_chapters; i++) {
634 AVChapter *c = avfc->chapters[i];
635 uint64_t start = av_rescale_q(c->start, c->time_base,
636 (AVRational){1, 1000000000});
637 uint64_t end = av_rescale_q(c->end, c->time_base,
638 (AVRational){1, 1000000000});
639 t = av_dict_get(c->metadata, "title", NULL, 0);
640 demuxer_add_chapter(demuxer, t ? bstr(t->value) : bstr(NULL),
641 start, end);
644 for (i = 0; i < avfc->nb_streams; i++)
645 handle_stream(demuxer, avfc, i);
646 priv->nb_streams_last = avfc->nb_streams;
648 if (avfc->nb_programs) {
649 int p;
650 for (p = 0; p < avfc->nb_programs; p++) {
651 AVProgram *program = avfc->programs[p];
652 t = av_dict_get(program->metadata, "title", NULL, 0);
653 mp_msg(MSGT_HEADER, MSGL_INFO, "LAVF: Program %d %s\n",
654 program->id, t ? t->value : "");
655 mp_msg(MSGT_IDENTIFY, MSGL_V, "PROGRAM_ID=%d\n", program->id);
659 mp_msg(MSGT_HEADER, MSGL_V, "LAVF: %d audio and %d video streams found\n",
660 priv->audio_streams, priv->video_streams);
661 demuxer->audio->id = -2; // wait for higher-level code to select track
662 /* There's still no global video track selection, so we have to pick
663 * the default video track in the demuxer.
664 * Note that the interface doesn't make sense: demuxer->video->id
665 * is initialized to --vid value, but after demuxer initialization
666 * it contains either -2 for disabled or v_streams[] index. */
667 int vid = demuxer->video->id;
668 demuxer->video->id = -2;
669 demuxer->desc->control(demuxer, DEMUXER_CTRL_SWITCH_VIDEO, &vid);
670 while (demuxer->video->id >= 0) {
671 struct sh_video *v = demuxer->v_streams[demuxer->video->id];
672 if (v->is_attached_pic) {
673 demuxer->desc->control(demuxer, DEMUXER_CTRL_SWITCH_VIDEO,
674 &(int){-1});
675 } else {
676 demuxer->video->sh = v;
677 break;
681 // disabled because unreliable per-stream bitrate values returned
682 // by libavformat trigger this heuristic incorrectly and break things
683 #if 0
684 /* libavformat sets bitrate for mpeg based on pts at start and end
685 * of file, which fails for files with pts resets. So calculate our
686 * own bitrate estimate. */
687 if (priv->avif->flags & AVFMT_TS_DISCONT) {
688 for (int i = 0; i < avfc->nb_streams; i++)
689 priv->bitrate += avfc->streams[i]->codec->bit_rate;
690 /* pts-based is more accurate if there are no resets; try to make
691 * a somewhat reasonable guess */
692 if (!avfc->duration || avfc->duration == AV_NOPTS_VALUE
693 || priv->bitrate && (avfc->bit_rate < priv->bitrate / 2
694 || avfc->bit_rate > priv->bitrate * 2))
695 priv->seek_by_bytes = true;
696 if (!priv->bitrate)
697 priv->bitrate = 1440000;
699 #endif
700 demuxer->accurate_seek = !priv->seek_by_bytes;
702 return demuxer;
705 static void check_internet_radio_hack(struct demuxer *demuxer)
707 struct lavf_priv *priv = demuxer->priv;
708 struct AVFormatContext *avfc = priv->avfc;
710 if (!matches_avinputformat_name(priv, "ogg"))
711 return;
712 if (priv->nb_streams_last == avfc->nb_streams)
713 return;
714 if (avfc->nb_streams - priv->nb_streams_last == 1
715 && priv->video_streams == 0 && priv->sub_streams == 0
716 && demuxer->a_streams[priv->audio_streams - 1]->format == 0x566f // vorbis
717 && (priv->audio_streams == 2 || priv->internet_radio_hack)
718 && demuxer->a_streams[0]->format == 0x566f) {
719 // extradata match could be checked but would require parsing
720 // headers, as the comment section will vary
721 if (!priv->internet_radio_hack) {
722 mp_msg(MSGT_DEMUX, MSGL_V,
723 "[lavf] enabling internet ogg radio hack\n");
725 priv->internet_radio_hack = true;
726 // use new per-track metadata as global metadata
727 AVDictionaryEntry *t = NULL;
728 AVStream *stream = avfc->streams[avfc->nb_streams - 1];
729 while ((t = av_dict_get(stream->metadata, "", t,
730 AV_DICT_IGNORE_SUFFIX)))
731 demux_info_add(demuxer, t->key, t->value);
732 } else {
733 if (priv->internet_radio_hack)
734 mp_tmsg(MSGT_DEMUX, MSGL_WARN, "[lavf] Internet radio ogg hack "
735 "was enabled, but stream characteristics changed.\n"
736 "This may or may not work.\n");
737 priv->internet_radio_hack = false;
741 static int destroy_avpacket(void *pkt)
743 av_free_packet(pkt);
744 return 0;
747 static int demux_lavf_fill_buffer(demuxer_t *demux, demux_stream_t *dsds)
749 lavf_priv_t *priv = demux->priv;
750 demux_packet_t *dp;
751 demux_stream_t *ds;
752 int id;
753 mp_msg(MSGT_DEMUX, MSGL_DBG2, "demux_lavf_fill_buffer()\n");
755 demux->filepos = stream_tell(demux->stream);
757 AVPacket *pkt = talloc(NULL, AVPacket);
758 if (av_read_frame(priv->avfc, pkt) < 0) {
759 talloc_free(pkt);
760 return 0;
762 talloc_set_destructor(pkt, destroy_avpacket);
764 // handle any new streams that might have been added
765 for (id = priv->nb_streams_last; id < priv->avfc->nb_streams; id++)
766 handle_stream(demux, priv->avfc, id);
767 check_internet_radio_hack(demux);
769 priv->nb_streams_last = priv->avfc->nb_streams;
771 id = pkt->stream_index;
773 if (id == demux->audio->id || priv->internet_radio_hack) {
774 // audio
775 ds = demux->audio;
776 } else if (id == demux->video->id) {
777 // video
778 ds = demux->video;
779 } else if (id == demux->sub->id) {
780 // subtitle
781 ds = demux->sub;
782 } else {
783 talloc_free(pkt);
784 return 1;
787 // If the packet has pointers to temporary fields that could be
788 // overwritten/freed by next av_read_frame(), copy them to persistent
789 // allocations so we can safely queue the packet for any length of time.
790 if (av_dup_packet(pkt) < 0)
791 abort();
792 dp = new_demux_packet_fromdata(pkt->data, pkt->size);
793 dp->avpacket = pkt;
795 int64_t ts = priv->use_dts ? pkt->dts : pkt->pts;
796 if (ts != AV_NOPTS_VALUE) {
797 dp->pts = ts * av_q2d(priv->avfc->streams[id]->time_base);
798 priv->last_pts = dp->pts * AV_TIME_BASE;
799 // always set duration for subtitles, even if AV_PKT_FLAG_KEY isn't set,
800 // otherwise they will stay on screen to long if e.g. ASS is demuxed
801 // from mkv
802 if ((ds == demux->sub || (pkt->flags & AV_PKT_FLAG_KEY)) &&
803 pkt->convergence_duration > 0)
804 dp->duration = pkt->convergence_duration *
805 av_q2d(priv->avfc->streams[id]->time_base);
807 dp->pos = demux->filepos;
808 dp->keyframe = pkt->flags & AV_PKT_FLAG_KEY;
809 // append packet to DS stream:
810 ds_add_packet(ds, dp);
811 return 1;
814 static void demux_seek_lavf(demuxer_t *demuxer, float rel_seek_secs,
815 float audio_delay, int flags)
817 lavf_priv_t *priv = demuxer->priv;
818 int avsflags = 0;
819 mp_msg(MSGT_DEMUX, MSGL_DBG2, "demux_seek_lavf(%p, %f, %f, %d)\n",
820 demuxer, rel_seek_secs, audio_delay, flags);
822 if (priv->seek_by_bytes) {
823 int64_t pos = demuxer->filepos;
824 rel_seek_secs *= priv->bitrate / 8;
825 pos += rel_seek_secs;
826 av_seek_frame(priv->avfc, -1, pos, AVSEEK_FLAG_BYTE);
827 return;
830 if (flags & SEEK_ABSOLUTE)
831 priv->last_pts = 0;
832 else if (rel_seek_secs < 0)
833 avsflags = AVSEEK_FLAG_BACKWARD;
834 if (flags & SEEK_FORWARD)
835 avsflags = 0;
836 else if (flags & SEEK_BACKWARD)
837 avsflags = AVSEEK_FLAG_BACKWARD;
838 if (flags & SEEK_FACTOR) {
839 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
840 return;
841 priv->last_pts += rel_seek_secs * priv->avfc->duration;
842 } else
843 priv->last_pts += rel_seek_secs * AV_TIME_BASE;
844 if (av_seek_frame(priv->avfc, -1, priv->last_pts, avsflags) < 0) {
845 avsflags ^= AVSEEK_FLAG_BACKWARD;
846 av_seek_frame(priv->avfc, -1, priv->last_pts, avsflags);
850 static int demux_lavf_control(demuxer_t *demuxer, int cmd, void *arg)
852 lavf_priv_t *priv = demuxer->priv;
854 switch (cmd) {
855 case DEMUXER_CTRL_CORRECT_PTS:
856 return DEMUXER_CTRL_OK;
857 case DEMUXER_CTRL_GET_TIME_LENGTH:
858 if (priv->seek_by_bytes) {
859 /* Our bitrate estimate may be better than would be used in
860 * otherwise similar fallback code at higher level */
861 if (demuxer->movi_end <= 0)
862 return DEMUXER_CTRL_DONTKNOW;
863 *(double *)arg = (demuxer->movi_end - demuxer->movi_start) * 8 /
864 priv->bitrate;
865 return DEMUXER_CTRL_GUESS;
867 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
868 return DEMUXER_CTRL_DONTKNOW;
870 *((double *)arg) = (double)priv->avfc->duration / AV_TIME_BASE;
871 return DEMUXER_CTRL_OK;
873 case DEMUXER_CTRL_GET_PERCENT_POS:
874 if (priv->seek_by_bytes)
875 return DEMUXER_CTRL_DONTKNOW; // let it use the fallback code
876 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
877 return DEMUXER_CTRL_DONTKNOW;
879 *((int *)arg) = (int)((priv->last_pts - priv->avfc->start_time) * 100 /
880 priv->avfc->duration);
881 return DEMUXER_CTRL_OK;
882 case DEMUXER_CTRL_SWITCH_AUDIO:
883 case DEMUXER_CTRL_SWITCH_VIDEO:
885 int id = *((int *)arg);
886 int newid = -2;
887 int i, curridx = -1;
888 int nstreams, *pstreams;
889 demux_stream_t *ds;
891 if (cmd == DEMUXER_CTRL_SWITCH_VIDEO) {
892 ds = demuxer->video;
893 nstreams = priv->video_streams;
894 pstreams = priv->vstreams;
895 } else {
896 ds = demuxer->audio;
897 nstreams = priv->audio_streams;
898 pstreams = priv->astreams;
900 for (i = 0; i < nstreams; i++) {
901 if (pstreams[i] == ds->id) { //current stream id
902 curridx = i;
903 break;
907 if (id == -1) { // next track
908 i = (curridx + 2) % (nstreams + 1) - 1;
909 if (i >= 0)
910 newid = pstreams[i];
911 } else if (id >= 0 && id < nstreams) { // select track by id
912 i = id;
913 newid = pstreams[i];
914 } else // no sound
915 i = -1;
917 if (i == curridx) {
918 *(int *) arg = curridx < 0 ? -2 : curridx;
919 return DEMUXER_CTRL_OK;
920 } else {
921 ds_free_packs(ds);
922 if (ds->id >= 0)
923 priv->avfc->streams[ds->id]->discard = AVDISCARD_ALL;
924 ds->id = newid;
925 *(int *) arg = i < 0 ? -2 : i;
926 if (newid >= 0)
927 priv->avfc->streams[newid]->discard = AVDISCARD_NONE;
928 return DEMUXER_CTRL_OK;
931 case DEMUXER_CTRL_IDENTIFY_PROGRAM:
933 demux_program_t *prog = arg;
934 AVProgram *program;
935 int p, i;
936 int start;
938 prog->vid = prog->aid = prog->sid = -2;
939 if (priv->avfc->nb_programs < 1)
940 return DEMUXER_CTRL_DONTKNOW;
942 if (prog->progid == -1) {
943 p = 0;
944 while (p < priv->avfc->nb_programs && priv->avfc->programs[p]->id != priv->cur_program)
945 p++;
946 p = (p + 1) % priv->avfc->nb_programs;
947 } else {
948 for (i = 0; i < priv->avfc->nb_programs; i++)
949 if (priv->avfc->programs[i]->id == prog->progid)
950 break;
951 if (i == priv->avfc->nb_programs)
952 return DEMUXER_CTRL_DONTKNOW;
953 p = i;
955 start = p;
956 redo:
957 program = priv->avfc->programs[p];
958 for (i = 0; i < program->nb_stream_indexes; i++) {
959 switch (priv->avfc->streams[program->stream_index[i]]->codec->codec_type) {
960 case AVMEDIA_TYPE_VIDEO:
961 if (prog->vid == -2)
962 prog->vid = program->stream_index[i];
963 break;
964 case AVMEDIA_TYPE_AUDIO:
965 if (prog->aid == -2)
966 prog->aid = program->stream_index[i];
967 break;
968 case AVMEDIA_TYPE_SUBTITLE:
969 if (prog->sid == -2 && priv->avfc->streams[program->stream_index[i]]->codec->codec_id == AV_CODEC_ID_TEXT)
970 prog->sid = program->stream_index[i];
971 break;
974 if (prog->aid >= 0 && prog->aid < MAX_A_STREAMS &&
975 demuxer->a_streams[prog->aid]) {
976 sh_audio_t *sh = demuxer->a_streams[prog->aid];
977 prog->aid = sh->aid;
978 } else
979 prog->aid = -2;
980 if (prog->vid >= 0 && prog->vid < MAX_V_STREAMS &&
981 demuxer->v_streams[prog->vid]) {
982 sh_video_t *sh = demuxer->v_streams[prog->vid];
983 prog->vid = sh->vid;
984 } else
985 prog->vid = -2;
986 if (prog->progid == -1 && prog->vid == -2 && prog->aid == -2) {
987 p = (p + 1) % priv->avfc->nb_programs;
988 if (p == start)
989 return DEMUXER_CTRL_DONTKNOW;
990 goto redo;
992 priv->cur_program = prog->progid = program->id;
993 return DEMUXER_CTRL_OK;
995 default:
996 return DEMUXER_CTRL_NOTIMPL;
1000 static void demux_close_lavf(demuxer_t *demuxer)
1002 lavf_priv_t *priv = demuxer->priv;
1003 if (priv) {
1004 if (priv->avfc) {
1005 av_freep(&priv->avfc->key);
1006 avformat_close_input(&priv->avfc);
1008 av_freep(&priv->pb);
1009 free(priv);
1010 demuxer->priv = NULL;
1015 const demuxer_desc_t demuxer_desc_lavf = {
1016 "libavformat demuxer",
1017 "lavf",
1018 "libavformat",
1019 "Michael Niedermayer",
1020 "supports many formats, requires libavformat",
1021 DEMUXER_TYPE_LAVF,
1022 0, // Check after other demuxer
1023 lavf_check_file,
1024 demux_lavf_fill_buffer,
1025 demux_open_lavf,
1026 demux_close_lavf,
1027 demux_seek_lavf,
1028 demux_lavf_control
1031 const demuxer_desc_t demuxer_desc_lavf_preferred = {
1032 "libavformat preferred demuxer",
1033 "lavfpref",
1034 "libavformat",
1035 "Michael Niedermayer",
1036 "supports many formats, requires libavformat",
1037 DEMUXER_TYPE_LAVF_PREFERRED,
1039 lavf_check_preferred_file,
1040 demux_lavf_fill_buffer,
1041 demux_open_lavf,
1042 demux_close_lavf,
1043 demux_seek_lavf,
1044 demux_lavf_control