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"
48 #include "mp_taglists.h"
50 #define INITIAL_PROBE_SIZE STREAM_BUFFER_SIZE
51 #define SMALL_MAX_PROBE_SIZE (32 * 1024)
52 #define PROBE_BUF_SIZE (2 * 1024 * 1024)
54 const m_option_t lavfdopts_conf
[] = {
55 OPT_INTRANGE("probesize", lavfdopts
.probesize
, 0, 32, INT_MAX
),
56 OPT_STRING("format", lavfdopts
.format
, 0),
57 OPT_INTRANGE("analyzeduration", lavfdopts
.analyzeduration
, 0, 0, INT_MAX
),
58 OPT_STRING("cryptokey", lavfdopts
.cryptokey
, 0),
59 OPT_STRING("o", lavfdopts
.avopt
, 0),
60 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
63 #define BIO_BUFFER_SIZE 32768
65 typedef struct lavf_priv
{
67 AVFormatContext
*avfc
;
69 uint8_t buffer
[BIO_BUFFER_SIZE
];
74 int astreams
[MAX_A_STREAMS
];
75 int vstreams
[MAX_V_STREAMS
];
76 int sstreams
[MAX_S_STREAMS
];
79 bool internet_radio_hack
;
85 static int mp_read(void *opaque
, uint8_t *buf
, int size
)
87 struct demuxer
*demuxer
= opaque
;
88 struct stream
*stream
= demuxer
->stream
;
91 ret
= stream_read(stream
, buf
, size
);
93 mp_msg(MSGT_HEADER
, MSGL_DBG2
,
94 "%d=mp_read(%p, %p, %d), pos: %"PRId64
", eof:%d\n",
95 ret
, stream
, buf
, size
, stream_tell(stream
), stream
->eof
);
99 static int64_t mp_seek(void *opaque
, int64_t pos
, int whence
)
101 struct demuxer
*demuxer
= opaque
;
102 struct stream
*stream
= demuxer
->stream
;
104 mp_msg(MSGT_HEADER
, MSGL_DBG2
, "mp_seek(%p, %"PRId64
", %d)\n",
105 stream
, pos
, whence
);
106 if (whence
== SEEK_CUR
)
107 pos
+= stream_tell(stream
);
108 else if (whence
== SEEK_END
&& stream
->end_pos
> 0)
109 pos
+= stream
->end_pos
;
110 else if (whence
== SEEK_SET
)
111 pos
+= stream
->start_pos
;
112 else if (whence
== AVSEEK_SIZE
&& stream
->end_pos
> 0) {
114 if (stream_control(stream
, STREAM_CTRL_GET_SIZE
, &size
) == STREAM_OK
)
116 return stream
->end_pos
- stream
->start_pos
;
122 current_pos
= stream_tell(stream
);
123 if (stream_seek(stream
, pos
) == 0) {
124 stream_reset(stream
);
125 stream_seek(stream
, current_pos
);
129 return pos
- stream
->start_pos
;
132 static int64_t mp_read_seek(void *opaque
, int stream_idx
, int64_t ts
, int flags
)
134 struct demuxer
*demuxer
= opaque
;
135 struct stream
*stream
= demuxer
->stream
;
136 struct lavf_priv
*priv
= demuxer
->priv
;
138 AVStream
*st
= priv
->avfc
->streams
[stream_idx
];
139 double pts
= (double)ts
* st
->time_base
.num
/ st
->time_base
.den
;
140 int ret
= stream_control(stream
, STREAM_CTRL_SEEK_TO_TIME
, &pts
);
142 ret
= AVERROR(ENOSYS
);
146 static void list_formats(void)
148 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "Available lavf input formats:\n");
149 AVInputFormat
*fmt
= NULL
;
150 while ((fmt
= av_iformat_next(fmt
)))
151 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "%15s : %s\n", fmt
->name
, fmt
->long_name
);
154 static int lavf_check_file(demuxer_t
*demuxer
)
156 struct MPOpts
*opts
= demuxer
->opts
;
157 struct lavfdopts
*lavfdopts
= &opts
->lavfdopts
;
160 int probe_data_size
= 0;
161 int read_size
= INITIAL_PROBE_SIZE
;
165 demuxer
->priv
= calloc(sizeof(lavf_priv_t
), 1);
166 priv
= demuxer
->priv
;
168 char *format
= lavfdopts
->format
;
170 format
= demuxer
->stream
->lavf_type
;
172 if (strcmp(format
, "help") == 0) {
176 priv
->avif
= av_find_input_format(format
);
178 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "Unknown lavf format %s\n", format
);
181 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "Forced lavf %s demuxer\n",
182 priv
->avif
->long_name
);
183 return DEMUXER_TYPE_LAVF
;
186 avpd
.buf
= av_mallocz(FFMAX(BIO_BUFFER_SIZE
, PROBE_BUF_SIZE
) +
187 FF_INPUT_BUFFER_PADDING_SIZE
);
189 read_size
= stream_read(demuxer
->stream
, avpd
.buf
+ probe_data_size
,
195 probe_data_size
+= read_size
;
196 avpd
.filename
= demuxer
->stream
->url
;
197 if (!avpd
.filename
) {
198 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "Stream url is not set!\n");
201 if (!strncmp(avpd
.filename
, "ffmpeg://", 9))
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 CODEC_ID_PCM_S8
:
349 case CODEC_ID_PCM_U8
:
350 sh_audio
->samplesize
= 1;
352 case CODEC_ID_PCM_S16LE
:
353 case CODEC_ID_PCM_S16BE
:
354 case CODEC_ID_PCM_U16LE
:
355 case CODEC_ID_PCM_U16BE
:
356 sh_audio
->samplesize
= 2;
358 case CODEC_ID_PCM_ALAW
:
359 sh_audio
->format
= 0x6;
361 case CODEC_ID_PCM_MULAW
:
362 sh_audio
->format
= 0x7;
365 if (title
&& title
->value
) {
366 sh_audio
->title
= talloc_strdup(sh_audio
, title
->value
);
367 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AID_%d_NAME=%s\n",
368 priv
->audio_streams
, title
->value
);
370 if (lang
&& lang
->value
) {
371 sh_audio
->lang
= talloc_strdup(sh_audio
, lang
->value
);
372 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AID_%d_LANG=%s\n",
373 priv
->audio_streams
, sh_audio
->lang
);
375 if (st
->disposition
& AV_DISPOSITION_DEFAULT
)
376 sh_audio
->default_track
= 1;
377 if (mp_msg_test(MSGT_HEADER
, MSGL_V
))
378 print_wave_header(sh_audio
->wf
, MSGL_V
);
379 st
->discard
= AVDISCARD_ALL
;
380 stream_id
= priv
->audio_streams
++;
383 case AVMEDIA_TYPE_VIDEO
: {
384 sh_video_t
*sh_video
;
385 BITMAPINFOHEADER
*bih
;
386 sh_video
= new_sh_video_vid(demuxer
, i
, priv
->video_streams
);
389 stream_type
= "video";
390 priv
->vstreams
[priv
->video_streams
] = i
;
391 sh_video
->libav_codec_id
= codec
->codec_id
;
392 bih
= calloc(sizeof(*bih
) + codec
->extradata_size
, 1);
394 if (codec
->codec_id
== CODEC_ID_RAWVIDEO
) {
395 switch (codec
->pix_fmt
) {
397 codec
->codec_tag
= MKTAG(24, 'B', 'G', 'R');
399 codec
->codec_tag
= MKTAG(24, 'R', 'G', 'B');
401 if (!codec
->codec_tag
)
402 codec
->codec_tag
= avcodec_pix_fmt_to_codec_tag(codec
->pix_fmt
);
403 } else if (!codec
->codec_tag
) {
404 codec
->codec_tag
= mp_taglist_video(codec
->codec_id
);
405 /* 0 might mean either unset or rawvideo; if codec_id
406 * was not RAWVIDEO assume it's unset
408 if (!codec
->codec_tag
)
409 codec
->codec_tag
= -1;
411 bih
->biSize
= sizeof(*bih
) + codec
->extradata_size
;
412 bih
->biWidth
= codec
->width
;
413 bih
->biHeight
= codec
->height
;
414 bih
->biBitCount
= codec
->bits_per_coded_sample
;
415 bih
->biSizeImage
= bih
->biWidth
* bih
->biHeight
* bih
->biBitCount
/ 8;
416 bih
->biCompression
= codec
->codec_tag
;
418 sh_video
->disp_w
= codec
->width
;
419 sh_video
->disp_h
= codec
->height
;
420 if (st
->time_base
.den
) { /* if container has time_base, use that */
421 sh_video
->video
.dwRate
= st
->time_base
.den
;
422 sh_video
->video
.dwScale
= st
->time_base
.num
;
424 sh_video
->video
.dwRate
= codec
->time_base
.den
;
425 sh_video
->video
.dwScale
= codec
->time_base
.num
;
427 /* Try to make up some frame rate value, even if it's not reliable.
428 * FPS information is needed to support subtitle formats which base
429 * timing on frame numbers.
430 * Libavformat seems to report no "reliable" FPS value for AVI files,
431 * while they are typically constant enough FPS that the value this
432 * heuristic makes up works with subtitles in practice.
435 if (st
->r_frame_rate
.num
)
436 fps
= av_q2d(st
->r_frame_rate
);
438 fps
= 1.0 / FFMAX(av_q2d(st
->time_base
),
439 av_q2d(st
->codec
->time_base
) *
440 st
->codec
->ticks_per_frame
);
442 sh_video
->frametime
= 1 / fps
;
443 sh_video
->format
= bih
->biCompression
;
444 if (st
->sample_aspect_ratio
.num
)
445 sh_video
->aspect
= codec
->width
* st
->sample_aspect_ratio
.num
446 / (float)(codec
->height
* st
->sample_aspect_ratio
.den
);
448 sh_video
->aspect
= codec
->width
* codec
->sample_aspect_ratio
.num
449 / (float)(codec
->height
* codec
->sample_aspect_ratio
.den
);
450 sh_video
->i_bps
= codec
->bit_rate
/ 8;
451 if (title
&& title
->value
) {
452 sh_video
->title
= talloc_strdup(sh_video
, title
->value
);
453 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VID_%d_NAME=%s\n",
454 priv
->video_streams
, title
->value
);
456 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "aspect= %d*%d/(%d*%d)\n",
457 codec
->width
, codec
->sample_aspect_ratio
.num
,
458 codec
->height
, codec
->sample_aspect_ratio
.den
);
460 sh_video
->ds
= demuxer
->video
;
461 if (codec
->extradata_size
)
462 memcpy(sh_video
->bih
+ 1, codec
->extradata
, codec
->extradata_size
);
463 if ( mp_msg_test(MSGT_HEADER
, MSGL_V
))
464 print_video_header(sh_video
->bih
, MSGL_V
);
465 st
->discard
= AVDISCARD_ALL
;
466 stream_id
= priv
->video_streams
++;
469 case AVMEDIA_TYPE_SUBTITLE
: {
472 /* only support text subtitles for now */
473 if (codec
->codec_id
== CODEC_ID_TEXT
)
475 else if (codec
->codec_id
== CODEC_ID_MOV_TEXT
)
477 else if (codec
->codec_id
== CODEC_ID_SSA
)
479 else if (codec
->codec_id
== CODEC_ID_DVD_SUBTITLE
)
481 else if (codec
->codec_id
== CODEC_ID_XSUB
)
483 else if (codec
->codec_id
== CODEC_ID_DVB_SUBTITLE
)
485 else if (codec
->codec_id
== CODEC_ID_DVB_TELETEXT
)
487 else if (codec
->codec_id
== CODEC_ID_HDMV_PGS_SUBTITLE
)
491 sh_sub
= new_sh_sub_sid(demuxer
, i
, priv
->sub_streams
);
494 stream_type
= "subtitle";
495 priv
->sstreams
[priv
->sub_streams
] = i
;
496 sh_sub
->libav_codec_id
= codec
->codec_id
;
498 if (codec
->extradata_size
) {
499 sh_sub
->extradata
= malloc(codec
->extradata_size
);
500 memcpy(sh_sub
->extradata
, codec
->extradata
, codec
->extradata_size
);
501 sh_sub
->extradata_len
= codec
->extradata_size
;
503 if (title
&& title
->value
) {
504 sh_sub
->title
= talloc_strdup(sh_sub
, title
->value
);
505 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SID_%d_NAME=%s\n",
506 priv
->sub_streams
, title
->value
);
508 if (lang
&& lang
->value
) {
509 sh_sub
->lang
= talloc_strdup(sh_sub
, lang
->value
);
510 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SID_%d_LANG=%s\n",
511 priv
->sub_streams
, sh_sub
->lang
);
513 if (st
->disposition
& AV_DISPOSITION_DEFAULT
)
514 sh_sub
->default_track
= 1;
515 stream_id
= priv
->sub_streams
++;
518 case AVMEDIA_TYPE_ATTACHMENT
: {
519 AVDictionaryEntry
*ftag
= av_dict_get(st
->metadata
, "filename",
521 char *filename
= ftag
? ftag
->value
: NULL
;
522 if (st
->codec
->codec_id
== CODEC_ID_TTF
)
523 demuxer_add_attachment(demuxer
, bstr(filename
),
524 bstr("application/x-truetype-font"),
525 (struct bstr
){codec
->extradata
,
526 codec
->extradata_size
});
530 st
->discard
= AVDISCARD_ALL
;
533 AVCodec
*avc
= avcodec_find_decoder(codec
->codec_id
);
534 const char *codec_name
= avc
? avc
->name
: "unknown";
535 if (!avc
&& *stream_type
== 's' && demuxer
->s_streams
[i
])
536 codec_name
= sh_sub_type2str((demuxer
->s_streams
[i
])->type
);
537 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "[lavf] stream %d: %s (%s), -%cid %d",
538 i
, stream_type
, codec_name
, *stream_type
, stream_id
);
539 if (lang
&& lang
->value
&& *stream_type
!= 'v')
540 mp_msg(MSGT_DEMUX
, MSGL_INFO
, ", -%clang %s",
541 *stream_type
, lang
->value
);
542 if (title
&& title
->value
)
543 mp_msg(MSGT_DEMUX
, MSGL_INFO
, ", %s", title
->value
);
544 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "\n");
548 static demuxer_t
*demux_open_lavf(demuxer_t
*demuxer
)
550 struct MPOpts
*opts
= demuxer
->opts
;
551 struct lavfdopts
*lavfdopts
= &opts
->lavfdopts
;
552 AVFormatContext
*avfc
;
553 AVDictionaryEntry
*t
= NULL
;
554 lavf_priv_t
*priv
= demuxer
->priv
;
556 char mp_filename
[256] = "mp:";
558 stream_seek(demuxer
->stream
, 0);
560 avfc
= avformat_alloc_context();
562 if (lavfdopts
->cryptokey
)
563 parse_cryptokey(avfc
, lavfdopts
->cryptokey
);
564 if (matches_avinputformat_name(priv
, "avi")) {
565 /* for avi libavformat returns the avi timestamps in .dts,
566 * some made-up stuff that's not really pts in .pts */
567 priv
->use_dts
= true;
568 demuxer
->timestamp_type
= TIMESTAMP_TYPE_SORT
;
570 if (opts
->user_correct_pts
!= 0)
571 avfc
->flags
|= AVFMT_FLAG_GENPTS
;
574 avfc
->flags
|= AVFMT_FLAG_IGNIDX
;
576 if (lavfdopts
->probesize
) {
577 if (av_opt_set_int(avfc
, "probesize", lavfdopts
->probesize
, 0) < 0)
578 mp_msg(MSGT_HEADER
, MSGL_ERR
,
579 "demux_lavf, couldn't set option probesize to %u\n",
580 lavfdopts
->probesize
);
582 if (lavfdopts
->analyzeduration
) {
583 if (av_opt_set_int(avfc
, "analyzeduration",
584 lavfdopts
->analyzeduration
* AV_TIME_BASE
, 0) < 0)
585 mp_msg(MSGT_HEADER
, MSGL_ERR
, "demux_lavf, couldn't set option "
586 "analyzeduration to %u\n", lavfdopts
->analyzeduration
);
589 if (lavfdopts
->avopt
) {
590 if (parse_avopts(avfc
, lavfdopts
->avopt
) < 0) {
591 mp_msg(MSGT_HEADER
, MSGL_ERR
,
592 "Your options /%s/ look like gibberish to me pal\n",
598 if (demuxer
->stream
->url
) {
599 if (!strncmp(demuxer
->stream
->url
, "ffmpeg://rtsp:", 14))
600 av_strlcpy(mp_filename
, demuxer
->stream
->url
+ 9,
601 sizeof(mp_filename
));
603 av_strlcat(mp_filename
, demuxer
->stream
->url
, sizeof(mp_filename
));
605 av_strlcat(mp_filename
, "foobar.dummy", sizeof(mp_filename
));
607 if (!(priv
->avif
->flags
& AVFMT_NOFILE
)) {
608 priv
->pb
= avio_alloc_context(priv
->buffer
, BIO_BUFFER_SIZE
, 0,
609 demuxer
, mp_read
, NULL
, mp_seek
);
610 priv
->pb
->read_seek
= mp_read_seek
;
611 priv
->pb
->seekable
= demuxer
->stream
->end_pos
612 && (demuxer
->stream
->flags
& MP_STREAM_SEEK
) == MP_STREAM_SEEK
613 ? AVIO_SEEKABLE_NORMAL
: 0;
617 if (avformat_open_input(&avfc
, mp_filename
, priv
->avif
, NULL
) < 0) {
618 mp_msg(MSGT_HEADER
, MSGL_ERR
,
619 "LAVF_header: avformat_open_input() failed\n");
625 if (avformat_find_stream_info(avfc
, NULL
) < 0) {
626 mp_msg(MSGT_HEADER
, MSGL_ERR
,
627 "LAVF_header: av_find_stream_info() failed\n");
632 while ((t
= av_dict_get(avfc
->metadata
, "", t
,
633 AV_DICT_IGNORE_SUFFIX
)))
634 demux_info_add(demuxer
, t
->key
, t
->value
);
636 for (i
= 0; i
< avfc
->nb_chapters
; i
++) {
637 AVChapter
*c
= avfc
->chapters
[i
];
638 uint64_t start
= av_rescale_q(c
->start
, c
->time_base
,
639 (AVRational
){1, 1000000000});
640 uint64_t end
= av_rescale_q(c
->end
, c
->time_base
,
641 (AVRational
){1, 1000000000});
642 t
= av_dict_get(c
->metadata
, "title", NULL
, 0);
643 demuxer_add_chapter(demuxer
, t
? bstr(t
->value
) : bstr(NULL
),
647 for (i
= 0; i
< avfc
->nb_streams
; i
++)
648 handle_stream(demuxer
, avfc
, i
);
649 priv
->nb_streams_last
= avfc
->nb_streams
;
651 if (avfc
->nb_programs
) {
653 for (p
= 0; p
< avfc
->nb_programs
; p
++) {
654 AVProgram
*program
= avfc
->programs
[p
];
655 t
= av_dict_get(program
->metadata
, "title", NULL
, 0);
656 mp_msg(MSGT_HEADER
, MSGL_INFO
, "LAVF: Program %d %s\n",
657 program
->id
, t
? t
->value
: "");
658 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "PROGRAM_ID=%d\n", program
->id
);
662 mp_msg(MSGT_HEADER
, MSGL_V
, "LAVF: %d audio and %d video streams found\n",
663 priv
->audio_streams
, priv
->video_streams
);
664 demuxer
->audio
->id
= -2; // wait for higher-level code to select track
665 /* There's still no global video track selection, so we have to pick
666 * the default video track in the demuxer.
667 * Note that the interface doesn't make sense: demuxer->video->id
668 * is initialized to --vid value, but after demuxer initialization
669 * it contains either -2 for disabled or v_streams[] index. */
670 int vid
= demuxer
->video
->id
;
671 demuxer
->video
->id
= -2;
672 demuxer
->desc
->control(demuxer
, DEMUXER_CTRL_SWITCH_VIDEO
, &vid
);
673 if (demuxer
->video
->id
>= 0)
674 demuxer
->video
->sh
= demuxer
->v_streams
[demuxer
->video
->id
];
676 // disabled because unreliable per-stream bitrate values returned
677 // by libavformat trigger this heuristic incorrectly and break things
679 /* libavformat sets bitrate for mpeg based on pts at start and end
680 * of file, which fails for files with pts resets. So calculate our
681 * own bitrate estimate. */
682 if (priv
->avif
->flags
& AVFMT_TS_DISCONT
) {
683 for (int i
= 0; i
< avfc
->nb_streams
; i
++)
684 priv
->bitrate
+= avfc
->streams
[i
]->codec
->bit_rate
;
685 /* pts-based is more accurate if there are no resets; try to make
686 * a somewhat reasonable guess */
687 if (!avfc
->duration
|| avfc
->duration
== AV_NOPTS_VALUE
688 || priv
->bitrate
&& (avfc
->bit_rate
< priv
->bitrate
/ 2
689 || avfc
->bit_rate
> priv
->bitrate
* 2))
690 priv
->seek_by_bytes
= true;
692 priv
->bitrate
= 1440000;
695 demuxer
->accurate_seek
= !priv
->seek_by_bytes
;
700 static void check_internet_radio_hack(struct demuxer
*demuxer
)
702 struct lavf_priv
*priv
= demuxer
->priv
;
703 struct AVFormatContext
*avfc
= priv
->avfc
;
705 if (!matches_avinputformat_name(priv
, "ogg"))
707 if (priv
->nb_streams_last
== avfc
->nb_streams
)
709 if (avfc
->nb_streams
- priv
->nb_streams_last
== 1
710 && priv
->video_streams
== 0 && priv
->sub_streams
== 0
711 && demuxer
->a_streams
[priv
->audio_streams
- 1]->format
== 0x566f // vorbis
712 && (priv
->audio_streams
== 2 || priv
->internet_radio_hack
)
713 && demuxer
->a_streams
[0]->format
== 0x566f) {
714 // extradata match could be checked but would require parsing
715 // headers, as the comment section will vary
716 if (!priv
->internet_radio_hack
) {
717 mp_msg(MSGT_DEMUX
, MSGL_V
,
718 "[lavf] enabling internet ogg radio hack\n");
720 priv
->internet_radio_hack
= true;
721 // use new per-track metadata as global metadata
722 AVDictionaryEntry
*t
= NULL
;
723 AVStream
*stream
= avfc
->streams
[avfc
->nb_streams
- 1];
724 while ((t
= av_dict_get(stream
->metadata
, "", t
,
725 AV_DICT_IGNORE_SUFFIX
)))
726 demux_info_add(demuxer
, t
->key
, t
->value
);
728 if (priv
->internet_radio_hack
)
729 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "[lavf] Internet radio ogg hack "
730 "was enabled, but stream characteristics changed.\n"
731 "This may or may not work.\n");
732 priv
->internet_radio_hack
= false;
736 static int destroy_avpacket(void *pkt
)
742 static int demux_lavf_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
)
744 lavf_priv_t
*priv
= demux
->priv
;
748 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "demux_lavf_fill_buffer()\n");
750 demux
->filepos
= stream_tell(demux
->stream
);
752 AVPacket
*pkt
= talloc(NULL
, AVPacket
);
753 if (av_read_frame(priv
->avfc
, pkt
) < 0) {
757 talloc_set_destructor(pkt
, destroy_avpacket
);
759 // handle any new streams that might have been added
760 for (id
= priv
->nb_streams_last
; id
< priv
->avfc
->nb_streams
; id
++)
761 handle_stream(demux
, priv
->avfc
, id
);
762 check_internet_radio_hack(demux
);
764 priv
->nb_streams_last
= priv
->avfc
->nb_streams
;
766 id
= pkt
->stream_index
;
768 if (id
== demux
->audio
->id
|| priv
->internet_radio_hack
) {
771 } else if (id
== demux
->video
->id
) {
774 } else if (id
== demux
->sub
->id
) {
783 // If the packet has pointers to temporary fields that could be
784 // overwritten/freed by next av_read_frame(), copy them to persistent
785 // allocations so we can safely queue the packet for any length of time.
786 if (av_dup_packet(pkt
) < 0)
788 dp
= new_demux_packet_fromdata(pkt
->data
, pkt
->size
);
791 int64_t ts
= priv
->use_dts
? pkt
->dts
: pkt
->pts
;
792 if (ts
!= AV_NOPTS_VALUE
) {
793 dp
->pts
= ts
* av_q2d(priv
->avfc
->streams
[id
]->time_base
);
794 priv
->last_pts
= dp
->pts
* AV_TIME_BASE
;
795 // always set duration for subtitles, even if AV_PKT_FLAG_KEY isn't set,
796 // otherwise they will stay on screen to long if e.g. ASS is demuxed
798 if ((ds
== demux
->sub
|| (pkt
->flags
& AV_PKT_FLAG_KEY
)) &&
799 pkt
->convergence_duration
> 0)
800 dp
->duration
= pkt
->convergence_duration
*
801 av_q2d(priv
->avfc
->streams
[id
]->time_base
);
803 dp
->pos
= demux
->filepos
;
804 dp
->keyframe
= pkt
->flags
& AV_PKT_FLAG_KEY
;
805 // append packet to DS stream:
806 ds_add_packet(ds
, dp
);
810 static void demux_seek_lavf(demuxer_t
*demuxer
, float rel_seek_secs
,
811 float audio_delay
, int flags
)
813 lavf_priv_t
*priv
= demuxer
->priv
;
815 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "demux_seek_lavf(%p, %f, %f, %d)\n",
816 demuxer
, rel_seek_secs
, audio_delay
, flags
);
818 if (priv
->seek_by_bytes
) {
819 int64_t pos
= demuxer
->filepos
;
820 rel_seek_secs
*= priv
->bitrate
/ 8;
821 pos
+= rel_seek_secs
;
822 av_seek_frame(priv
->avfc
, -1, pos
, AVSEEK_FLAG_BYTE
);
826 if (flags
& SEEK_ABSOLUTE
)
828 else if (rel_seek_secs
< 0)
829 avsflags
= AVSEEK_FLAG_BACKWARD
;
830 if (flags
& SEEK_FORWARD
)
832 else if (flags
& SEEK_BACKWARD
)
833 avsflags
= AVSEEK_FLAG_BACKWARD
;
834 if (flags
& SEEK_FACTOR
) {
835 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
837 priv
->last_pts
+= rel_seek_secs
* priv
->avfc
->duration
;
839 priv
->last_pts
+= rel_seek_secs
* AV_TIME_BASE
;
840 if (av_seek_frame(priv
->avfc
, -1, priv
->last_pts
, avsflags
) < 0) {
841 avsflags
^= AVSEEK_FLAG_BACKWARD
;
842 av_seek_frame(priv
->avfc
, -1, priv
->last_pts
, avsflags
);
846 static int demux_lavf_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
848 lavf_priv_t
*priv
= demuxer
->priv
;
851 case DEMUXER_CTRL_CORRECT_PTS
:
852 return DEMUXER_CTRL_OK
;
853 case DEMUXER_CTRL_GET_TIME_LENGTH
:
854 if (priv
->seek_by_bytes
) {
855 /* Our bitrate estimate may be better than would be used in
856 * otherwise similar fallback code at higher level */
857 if (demuxer
->movi_end
<= 0)
858 return DEMUXER_CTRL_DONTKNOW
;
859 *(double *)arg
= (demuxer
->movi_end
- demuxer
->movi_start
) * 8 /
861 return DEMUXER_CTRL_GUESS
;
863 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
864 return DEMUXER_CTRL_DONTKNOW
;
866 *((double *)arg
) = (double)priv
->avfc
->duration
/ AV_TIME_BASE
;
867 return DEMUXER_CTRL_OK
;
869 case DEMUXER_CTRL_GET_PERCENT_POS
:
870 if (priv
->seek_by_bytes
)
871 return DEMUXER_CTRL_DONTKNOW
; // let it use the fallback code
872 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
873 return DEMUXER_CTRL_DONTKNOW
;
875 *((int *)arg
) = (int)((priv
->last_pts
- priv
->avfc
->start_time
) * 100 /
876 priv
->avfc
->duration
);
877 return DEMUXER_CTRL_OK
;
878 case DEMUXER_CTRL_SWITCH_AUDIO
:
879 case DEMUXER_CTRL_SWITCH_VIDEO
:
881 int id
= *((int *)arg
);
884 int nstreams
, *pstreams
;
887 if (cmd
== DEMUXER_CTRL_SWITCH_VIDEO
) {
889 nstreams
= priv
->video_streams
;
890 pstreams
= priv
->vstreams
;
893 nstreams
= priv
->audio_streams
;
894 pstreams
= priv
->astreams
;
896 for (i
= 0; i
< nstreams
; i
++) {
897 if (pstreams
[i
] == ds
->id
) { //current stream id
903 if (id
== -1) { // next track
904 i
= (curridx
+ 2) % (nstreams
+ 1) - 1;
907 } else if (id
>= 0 && id
< nstreams
) { // select track by id
914 *(int *) arg
= curridx
< 0 ? -2 : curridx
;
915 return DEMUXER_CTRL_OK
;
919 priv
->avfc
->streams
[ds
->id
]->discard
= AVDISCARD_ALL
;
921 *(int *) arg
= i
< 0 ? -2 : i
;
923 priv
->avfc
->streams
[newid
]->discard
= AVDISCARD_NONE
;
924 return DEMUXER_CTRL_OK
;
927 case DEMUXER_CTRL_IDENTIFY_PROGRAM
:
929 demux_program_t
*prog
= arg
;
934 prog
->vid
= prog
->aid
= prog
->sid
= -2;
935 if (priv
->avfc
->nb_programs
< 1)
936 return DEMUXER_CTRL_DONTKNOW
;
938 if (prog
->progid
== -1) {
940 while (p
< priv
->avfc
->nb_programs
&& priv
->avfc
->programs
[p
]->id
!= priv
->cur_program
)
942 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
944 for (i
= 0; i
< priv
->avfc
->nb_programs
; i
++)
945 if (priv
->avfc
->programs
[i
]->id
== prog
->progid
)
947 if (i
== priv
->avfc
->nb_programs
)
948 return DEMUXER_CTRL_DONTKNOW
;
953 program
= priv
->avfc
->programs
[p
];
954 for (i
= 0; i
< program
->nb_stream_indexes
; i
++) {
955 switch (priv
->avfc
->streams
[program
->stream_index
[i
]]->codec
->codec_type
) {
956 case AVMEDIA_TYPE_VIDEO
:
958 prog
->vid
= program
->stream_index
[i
];
960 case AVMEDIA_TYPE_AUDIO
:
962 prog
->aid
= program
->stream_index
[i
];
964 case AVMEDIA_TYPE_SUBTITLE
:
965 if (prog
->sid
== -2 && priv
->avfc
->streams
[program
->stream_index
[i
]]->codec
->codec_id
== CODEC_ID_TEXT
)
966 prog
->sid
= program
->stream_index
[i
];
970 if (prog
->aid
>= 0 && prog
->aid
< MAX_A_STREAMS
&&
971 demuxer
->a_streams
[prog
->aid
]) {
972 sh_audio_t
*sh
= demuxer
->a_streams
[prog
->aid
];
976 if (prog
->vid
>= 0 && prog
->vid
< MAX_V_STREAMS
&&
977 demuxer
->v_streams
[prog
->vid
]) {
978 sh_video_t
*sh
= demuxer
->v_streams
[prog
->vid
];
982 if (prog
->progid
== -1 && prog
->vid
== -2 && prog
->aid
== -2) {
983 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
985 return DEMUXER_CTRL_DONTKNOW
;
988 priv
->cur_program
= prog
->progid
= program
->id
;
989 return DEMUXER_CTRL_OK
;
992 return DEMUXER_CTRL_NOTIMPL
;
996 static void demux_close_lavf(demuxer_t
*demuxer
)
998 lavf_priv_t
*priv
= demuxer
->priv
;
1001 av_freep(&priv
->avfc
->key
);
1002 avformat_close_input(&priv
->avfc
);
1004 av_freep(&priv
->pb
);
1006 demuxer
->priv
= NULL
;
1011 const demuxer_desc_t demuxer_desc_lavf
= {
1012 "libavformat demuxer",
1015 "Michael Niedermayer",
1016 "supports many formats, requires libavformat",
1018 0, // Check after other demuxer
1020 demux_lavf_fill_buffer
,
1027 const demuxer_desc_t demuxer_desc_lavf_preferred
= {
1028 "libavformat preferred demuxer",
1031 "Michael Niedermayer",
1032 "supports many formats, requires libavformat",
1033 DEMUXER_TYPE_LAVF_PREFERRED
,
1035 lavf_check_preferred_file
,
1036 demux_lavf_fill_buffer
,