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.
23 // #include <unistd.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>
41 #include "stream/stream.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
{
66 AVFormatContext
*avfc
;
68 uint8_t buffer
[BIO_BUFFER_SIZE
];
73 int astreams
[MAX_A_STREAMS
];
74 int vstreams
[MAX_V_STREAMS
];
75 int sstreams
[MAX_S_STREAMS
];
78 bool internet_radio_hack
;
84 static int mp_read(void *opaque
, uint8_t *buf
, int size
)
86 struct demuxer
*demuxer
= opaque
;
87 struct stream
*stream
= demuxer
->stream
;
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
);
98 static int64_t mp_seek(void *opaque
, int64_t pos
, int whence
)
100 struct demuxer
*demuxer
= opaque
;
101 struct stream
*stream
= demuxer
->stream
;
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) {
113 if (stream_control(stream
, STREAM_CTRL_GET_SIZE
, &size
) == STREAM_OK
)
115 return stream
->end_pos
- stream
->start_pos
;
121 current_pos
= stream_tell(stream
);
122 if (stream_seek(stream
, pos
) == 0) {
123 stream_reset(stream
);
124 stream_seek(stream
, current_pos
);
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
);
141 ret
= AVERROR(ENOSYS
);
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
;
159 int probe_data_size
= 0;
160 int read_size
= INITIAL_PROBE_SIZE
;
164 demuxer
->priv
= calloc(sizeof(lavf_priv_t
), 1);
165 priv
= demuxer
->priv
;
167 char *format
= lavfdopts
->format
;
169 format
= demuxer
->stream
->lavf_type
;
171 if (strcmp(format
, "help") == 0) {
175 priv
->avif
= av_find_input_format(format
);
177 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "Unknown lavf format %s\n", format
);
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
);
188 read_size
= stream_read(demuxer
->stream
, avpd
.buf
+ probe_data_size
,
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");
200 if (!strncmp(avpd
.filename
, "ffmpeg://", 9) ||
201 !strncmp(avpd
.filename
, "lavf://", 7))
203 avpd
.buf_size
= probe_data_size
;
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
);
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");
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
,
231 const char *avifname
= priv
->avif
->name
;
233 const char *next
= strchr(avifname
, ',');
235 return !strcmp(avifname
, name
);
236 int len
= next
- avifname
;
237 if (len
== strlen(name
) && !memcmp(avifname
, name
, len
))
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
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
))
259 return DEMUXER_TYPE_LAVF_PREFERRED
;
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;
272 static void parse_cryptokey(AVFormatContext
*avfc
, const char *str
)
274 int len
= strlen(str
) / 2;
275 uint8_t *key
= av_mallocz(len
);
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
;
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
300 codec
->codec_tag
= override_tag
;
302 switch (codec
->codec_type
) {
303 case AVMEDIA_TYPE_AUDIO
: {
305 sh_audio_t
*sh_audio
;
306 sh_audio
= new_sh_audio_aid(demuxer
, i
, priv
->audio_streams
);
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
);
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
;
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;
351 case AV_CODEC_ID_PCM_MULAW
:
352 sh_audio
->format
= 0x7;
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
++;
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
);
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
) {
387 codec
->codec_tag
= MKTAG(24, 'B', 'G', 'R');
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
;
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
;
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.
425 if (st
->r_frame_rate
.num
)
426 fps
= av_q2d(st
->r_frame_rate
);
428 fps
= 1.0 / FFMAX(av_q2d(st
->time_base
),
429 av_q2d(st
->codec
->time_base
) *
430 st
->codec
->ticks_per_frame
);
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
);
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
;
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
++;
463 case AVMEDIA_TYPE_SUBTITLE
: {
466 /* only support text subtitles for now */
467 if (codec
->codec_id
== AV_CODEC_ID_TEXT
)
469 else if (codec
->codec_id
== AV_CODEC_ID_MOV_TEXT
)
471 else if (codec
->codec_id
== AV_CODEC_ID_SSA
)
473 else if (codec
->codec_id
== AV_CODEC_ID_DVD_SUBTITLE
)
475 else if (codec
->codec_id
== AV_CODEC_ID_XSUB
)
477 else if (codec
->codec_id
== AV_CODEC_ID_DVB_SUBTITLE
)
479 else if (codec
->codec_id
== AV_CODEC_ID_DVB_TELETEXT
)
481 else if (codec
->codec_id
== AV_CODEC_ID_HDMV_PGS_SUBTITLE
)
485 sh_sub
= new_sh_sub_sid(demuxer
, i
, priv
->sub_streams
);
488 stream_type
= "subtitle";
489 priv
->sstreams
[priv
->sub_streams
] = i
;
490 sh_sub
->libav_codec_id
= codec
->codec_id
;
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
++;
512 case AVMEDIA_TYPE_ATTACHMENT
: {
513 AVDictionaryEntry
*ftag
= av_dict_get(st
->metadata
, "filename",
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
});
524 st
->discard
= AVDISCARD_ALL
;
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
;
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
;
563 if (opts
->user_correct_pts
!= 0)
564 avfc
->flags
|= AVFMT_FLAG_GENPTS
;
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",
591 char *filename
= demuxer
->stream
->url
;
594 if (demuxer
->stream
->lavf_type
&& !strcmp(demuxer
->stream
->lavf_type
,
596 // Remove possible leading ffmpeg:// or lavf://
597 char *name
= strstr(filename
, "rtsp:");
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;
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");
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");
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
),
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
) {
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
,
676 demuxer
->video
->sh
= v
;
681 // disabled because unreliable per-stream bitrate values returned
682 // by libavformat trigger this heuristic incorrectly and break things
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;
697 priv
->bitrate
= 1440000;
700 demuxer
->accurate_seek
= !priv
->seek_by_bytes
;
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"))
712 if (priv
->nb_streams_last
== avfc
->nb_streams
)
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
);
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
)
747 static int demux_lavf_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
)
749 lavf_priv_t
*priv
= demux
->priv
;
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) {
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
) {
776 } else if (id
== demux
->video
->id
) {
779 } else if (id
== demux
->sub
->id
) {
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)
792 dp
= new_demux_packet_fromdata(pkt
->data
, pkt
->size
);
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
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
);
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
;
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
);
830 if (flags
& SEEK_ABSOLUTE
)
832 else if (rel_seek_secs
< 0)
833 avsflags
= AVSEEK_FLAG_BACKWARD
;
834 if (flags
& SEEK_FORWARD
)
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
)
841 priv
->last_pts
+= rel_seek_secs
* priv
->avfc
->duration
;
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
;
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 /
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
);
888 int nstreams
, *pstreams
;
891 if (cmd
== DEMUXER_CTRL_SWITCH_VIDEO
) {
893 nstreams
= priv
->video_streams
;
894 pstreams
= priv
->vstreams
;
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
907 if (id
== -1) { // next track
908 i
= (curridx
+ 2) % (nstreams
+ 1) - 1;
911 } else if (id
>= 0 && id
< nstreams
) { // select track by id
918 *(int *) arg
= curridx
< 0 ? -2 : curridx
;
919 return DEMUXER_CTRL_OK
;
923 priv
->avfc
->streams
[ds
->id
]->discard
= AVDISCARD_ALL
;
925 *(int *) arg
= i
< 0 ? -2 : i
;
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
;
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) {
944 while (p
< priv
->avfc
->nb_programs
&& priv
->avfc
->programs
[p
]->id
!= priv
->cur_program
)
946 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
948 for (i
= 0; i
< priv
->avfc
->nb_programs
; i
++)
949 if (priv
->avfc
->programs
[i
]->id
== prog
->progid
)
951 if (i
== priv
->avfc
->nb_programs
)
952 return DEMUXER_CTRL_DONTKNOW
;
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
:
962 prog
->vid
= program
->stream_index
[i
];
964 case AVMEDIA_TYPE_AUDIO
:
966 prog
->aid
= program
->stream_index
[i
];
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
];
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
];
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
];
986 if (prog
->progid
== -1 && prog
->vid
== -2 && prog
->aid
== -2) {
987 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
989 return DEMUXER_CTRL_DONTKNOW
;
992 priv
->cur_program
= prog
->progid
= program
->id
;
993 return DEMUXER_CTRL_OK
;
996 return DEMUXER_CTRL_NOTIMPL
;
1000 static void demux_close_lavf(demuxer_t
*demuxer
)
1002 lavf_priv_t
*priv
= demuxer
->priv
;
1005 av_freep(&priv
->avfc
->key
);
1006 avformat_close_input(&priv
->avfc
);
1008 av_freep(&priv
->pb
);
1010 demuxer
->priv
= NULL
;
1015 const demuxer_desc_t demuxer_desc_lavf
= {
1016 "libavformat demuxer",
1019 "Michael Niedermayer",
1020 "supports many formats, requires libavformat",
1022 0, // Check after other demuxer
1024 demux_lavf_fill_buffer
,
1031 const demuxer_desc_t demuxer_desc_lavf_preferred
= {
1032 "libavformat preferred demuxer",
1035 "Michael Niedermayer",
1036 "supports many formats, requires libavformat",
1037 DEMUXER_TYPE_LAVF_PREFERRED
,
1039 lavf_check_preferred_file
,
1040 demux_lavf_fill_buffer
,