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>
34 #include "stream/stream.h"
39 #include "libvo/sub.h"
41 #include "libavformat/avformat.h"
42 #include "libavformat/avio.h"
43 #include "libavutil/avutil.h"
44 #include "libavutil/avstring.h"
45 #include "libavcodec/opt.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
) {
85 struct demuxer
*demuxer
= opaque
;
86 struct stream
*stream
= demuxer
->stream
;
89 ret
=stream_read(stream
, buf
, size
);
91 mp_msg(MSGT_HEADER
,MSGL_DBG2
,"%d=mp_read(%p, %p, %d), pos: %"PRId64
", eof:%d\n",
92 ret
, stream
, buf
, size
, stream_tell(stream
), stream
->eof
);
96 static int64_t mp_seek(void *opaque
, int64_t pos
, int whence
) {
97 struct demuxer
*demuxer
= opaque
;
98 struct stream
*stream
= demuxer
->stream
;
100 mp_msg(MSGT_HEADER
,MSGL_DBG2
,"mp_seek(%p, %"PRId64
", %d)\n", stream
, pos
, whence
);
101 if(whence
== SEEK_CUR
)
102 pos
+=stream_tell(stream
);
103 else if(whence
== SEEK_END
&& stream
->end_pos
> 0)
104 pos
+= stream
->end_pos
;
105 else if(whence
== SEEK_SET
)
106 pos
+= stream
->start_pos
;
107 else if(whence
== AVSEEK_SIZE
&& stream
->end_pos
> 0)
108 return stream
->end_pos
- stream
->start_pos
;
114 current_pos
= stream_tell(stream
);
115 if(stream_seek(stream
, pos
)==0) {
116 stream_reset(stream
);
117 stream_seek(stream
, current_pos
);
121 return pos
- stream
->start_pos
;
124 static int64_t mp_read_seek(void *opaque
, int stream_idx
, int64_t ts
, int flags
)
126 struct demuxer
*demuxer
= opaque
;
127 struct stream
*stream
= demuxer
->stream
;
128 struct lavf_priv
*priv
= demuxer
->priv
;
130 AVStream
*st
= priv
->avfc
->streams
[stream_idx
];
131 double pts
= (double)ts
* st
->time_base
.num
/ st
->time_base
.den
;
132 int ret
= stream_control(stream
, STREAM_CTRL_SEEK_TO_TIME
, &pts
);
134 ret
= AVERROR(ENOSYS
);
138 static void list_formats(void) {
139 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "Available lavf input formats:\n");
140 AVInputFormat
*fmt
= NULL
;
141 while (fmt
= av_iformat_next(fmt
))
142 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "%15s : %s\n", fmt
->name
, fmt
->long_name
);
145 static int lavf_check_file(demuxer_t
*demuxer
){
146 struct MPOpts
*opts
= demuxer
->opts
;
147 struct lavfdopts
*lavfdopts
= &opts
->lavfdopts
;
150 int probe_data_size
= 0;
151 int read_size
= INITIAL_PROBE_SIZE
;
155 demuxer
->priv
=calloc(sizeof(lavf_priv_t
),1);
160 char *format
= lavfdopts
->format
;
162 format
= demuxer
->stream
->lavf_type
;
164 if (strcmp(format
, "help") == 0) {
168 priv
->avif
= av_find_input_format(format
);
170 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "Unknown lavf format %s\n", format
);
173 mp_msg(MSGT_DEMUX
,MSGL_INFO
,"Forced lavf %s demuxer\n", priv
->avif
->long_name
);
174 return DEMUXER_TYPE_LAVF
;
177 avpd
.buf
= av_mallocz(FFMAX(BIO_BUFFER_SIZE
, PROBE_BUF_SIZE
) +
178 FF_INPUT_BUFFER_PADDING_SIZE
);
180 read_size
= stream_read(demuxer
->stream
, avpd
.buf
+ probe_data_size
, read_size
);
185 probe_data_size
+= read_size
;
186 avpd
.filename
= demuxer
->stream
->url
;
187 if (!avpd
.filename
) {
188 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "Stream url is not set!\n");
191 if (!strncmp(avpd
.filename
, "ffmpeg://", 9))
193 avpd
.buf_size
= probe_data_size
;
196 priv
->avif
= av_probe_input_format2(&avpd
, probe_data_size
> 0, &score
);
197 read_size
= FFMIN(2*read_size
, PROBE_BUF_SIZE
- probe_data_size
);
198 } while ((demuxer
->desc
->type
!= DEMUXER_TYPE_LAVF_PREFERRED
||
199 probe_data_size
< SMALL_MAX_PROBE_SIZE
) &&
200 score
<= AVPROBE_SCORE_MAX
/ 4 &&
201 read_size
> 0 && probe_data_size
< PROBE_BUF_SIZE
);
205 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF_check: no clue about this gibberish!\n");
208 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF_check: %s\n", priv
->avif
->long_name
);
210 demuxer
->filetype
= priv
->avif
->long_name
;
211 if (!demuxer
->filetype
)
212 demuxer
->filetype
= priv
->avif
->name
;
214 return DEMUXER_TYPE_LAVF
;
217 static bool matches_avinputformat_name(struct lavf_priv
*priv
,
220 const char *avifname
= priv
->avif
->name
;
222 const char *next
= strchr(avifname
, ',');
224 return !strcmp(avifname
, name
);
225 int len
= next
- avifname
;
226 if (len
== strlen(name
) && !memcmp(avifname
, name
, len
))
232 /* formats for which an internal demuxer is preferred */
233 static const char * const preferred_internal
[] = {
234 /* lavf Matroska demuxer doesn't support ordered chapters and fails
237 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(52, 99, 0)
238 /* Seeking doesn't work with lavf FLAC demuxer in FFmpeg versions
239 * without a FLAC parser. In principle this could use a runtime check to
240 * switch if a shared library is updated. */
243 /* lavf gives neither pts nor dts for some video frames in .rm */
248 static int lavf_check_preferred_file(demuxer_t
*demuxer
){
249 if (lavf_check_file(demuxer
)) {
250 const char * const *p
;
251 lavf_priv_t
*priv
= demuxer
->priv
;
252 for (p
= preferred_internal
; *p
; p
++)
253 if (matches_avinputformat_name(priv
, *p
))
255 return DEMUXER_TYPE_LAVF_PREFERRED
;
260 static uint8_t char2int(char c
) {
261 if (c
>= '0' && c
<= '9') return c
- '0';
262 if (c
>= 'a' && c
<= 'f') return c
- 'a' + 10;
263 if (c
>= 'A' && c
<= 'F') return c
- 'A' + 10;
267 static void parse_cryptokey(AVFormatContext
*avfc
, const char *str
) {
268 int len
= strlen(str
) / 2;
269 uint8_t *key
= av_mallocz(len
);
273 for (i
= 0; i
< len
; i
++, str
+= 2)
274 *key
++ = (char2int(str
[0]) << 4) | char2int(str
[1]);
277 static void handle_stream(demuxer_t
*demuxer
, AVFormatContext
*avfc
, int i
) {
278 lavf_priv_t
*priv
= demuxer
->priv
;
279 AVStream
*st
= avfc
->streams
[i
];
280 AVCodecContext
*codec
= st
->codec
;
281 char *stream_type
= NULL
;
283 AVMetadataTag
*lang
= av_metadata_get(st
->metadata
, "language", NULL
, 0);
284 AVMetadataTag
*title
= av_metadata_get(st
->metadata
, "title", NULL
, 0);
285 int g
, override_tag
= mp_av_codec_get_tag(mp_codecid_override_taglists
,
287 // For some formats (like PCM) always trust CODEC_ID_* more than codec_tag
289 codec
->codec_tag
= override_tag
;
291 switch(codec
->codec_type
){
292 case CODEC_TYPE_AUDIO
:{
294 sh_audio_t
* sh_audio
;
295 sh_audio
= new_sh_audio_aid(demuxer
, i
, priv
->audio_streams
);
298 stream_type
= "audio";
299 priv
->astreams
[priv
->audio_streams
] = i
;
300 wf
= calloc(sizeof(*wf
) + codec
->extradata_size
, 1);
301 // mp4a tag is used for all mp4 files no matter what they actually contain
302 if(codec
->codec_tag
== MKTAG('m', 'p', '4', 'a'))
304 if(!codec
->codec_tag
)
305 codec
->codec_tag
= mp_av_codec_get_tag(mp_wav_taglists
, codec
->codec_id
);
306 wf
->wFormatTag
= codec
->codec_tag
;
307 wf
->nChannels
= codec
->channels
;
308 wf
->nSamplesPerSec
= codec
->sample_rate
;
309 wf
->nAvgBytesPerSec
= codec
->bit_rate
/8;
310 wf
->nBlockAlign
= codec
->block_align
? codec
->block_align
: 1;
311 wf
->wBitsPerSample
= codec
->bits_per_coded_sample
;
312 wf
->cbSize
= codec
->extradata_size
;
313 if(codec
->extradata_size
)
314 memcpy(wf
+ 1, codec
->extradata
, codec
->extradata_size
);
316 sh_audio
->audio
.dwSampleSize
= codec
->block_align
;
317 if(codec
->frame_size
&& codec
->sample_rate
){
318 sh_audio
->audio
.dwScale
=codec
->frame_size
;
319 sh_audio
->audio
.dwRate
= codec
->sample_rate
;
321 sh_audio
->audio
.dwScale
= codec
->block_align
? codec
->block_align
*8 : 8;
322 sh_audio
->audio
.dwRate
= codec
->bit_rate
;
324 g
= av_gcd(sh_audio
->audio
.dwScale
, sh_audio
->audio
.dwRate
);
325 sh_audio
->audio
.dwScale
/= g
;
326 sh_audio
->audio
.dwRate
/= g
;
327 // 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);
328 sh_audio
->ds
= demuxer
->audio
;
329 sh_audio
->format
= codec
->codec_tag
;
330 sh_audio
->channels
= codec
->channels
;
331 sh_audio
->samplerate
= codec
->sample_rate
;
332 sh_audio
->i_bps
= codec
->bit_rate
/8;
333 switch (codec
->codec_id
) {
334 case CODEC_ID_PCM_S8
:
335 case CODEC_ID_PCM_U8
:
336 sh_audio
->samplesize
= 1;
338 case CODEC_ID_PCM_S16LE
:
339 case CODEC_ID_PCM_S16BE
:
340 case CODEC_ID_PCM_U16LE
:
341 case CODEC_ID_PCM_U16BE
:
342 sh_audio
->samplesize
= 2;
344 case CODEC_ID_PCM_ALAW
:
345 sh_audio
->format
= 0x6;
347 case CODEC_ID_PCM_MULAW
:
348 sh_audio
->format
= 0x7;
351 if (title
&& title
->value
)
352 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AID_%d_NAME=%s\n", priv
->audio_streams
, title
->value
);
353 if (lang
&& lang
->value
) {
354 sh_audio
->lang
= strdup(lang
->value
);
355 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AID_%d_LANG=%s\n", priv
->audio_streams
, sh_audio
->lang
);
357 if (st
->disposition
& AV_DISPOSITION_DEFAULT
)
358 sh_audio
->default_track
= 1;
359 if(mp_msg_test(MSGT_HEADER
,MSGL_V
) ) print_wave_header(sh_audio
->wf
, MSGL_V
);
360 // select the first audio stream
361 if (!demuxer
->audio
->sh
) {
362 demuxer
->audio
->id
= i
;
363 demuxer
->audio
->sh
= demuxer
->a_streams
[i
];
365 st
->discard
= AVDISCARD_ALL
;
366 stream_id
= priv
->audio_streams
++;
369 case CODEC_TYPE_VIDEO
:{
370 sh_video_t
* sh_video
;
371 BITMAPINFOHEADER
*bih
;
372 sh_video
=new_sh_video_vid(demuxer
, i
, priv
->video_streams
);
374 stream_type
= "video";
375 priv
->vstreams
[priv
->video_streams
] = i
;
376 bih
=calloc(sizeof(*bih
) + codec
->extradata_size
,1);
378 if(codec
->codec_id
== CODEC_ID_RAWVIDEO
) {
379 switch (codec
->pix_fmt
) {
381 codec
->codec_tag
= MKTAG(24, 'B', 'G', 'R');
384 if(!codec
->codec_tag
)
385 codec
->codec_tag
= mp_av_codec_get_tag(mp_bmp_taglists
, codec
->codec_id
);
386 bih
->biSize
= sizeof(*bih
) + codec
->extradata_size
;
387 bih
->biWidth
= codec
->width
;
388 bih
->biHeight
= codec
->height
;
389 bih
->biBitCount
= codec
->bits_per_coded_sample
;
390 bih
->biSizeImage
= bih
->biWidth
* bih
->biHeight
* bih
->biBitCount
/8;
391 bih
->biCompression
= codec
->codec_tag
;
393 sh_video
->disp_w
= codec
->width
;
394 sh_video
->disp_h
= codec
->height
;
395 if (st
->time_base
.den
) { /* if container has time_base, use that */
396 sh_video
->video
.dwRate
= st
->time_base
.den
;
397 sh_video
->video
.dwScale
= st
->time_base
.num
;
399 sh_video
->video
.dwRate
= codec
->time_base
.den
;
400 sh_video
->video
.dwScale
= codec
->time_base
.num
;
402 sh_video
->fps
=av_q2d(st
->r_frame_rate
);
403 sh_video
->frametime
=1/av_q2d(st
->r_frame_rate
);
404 sh_video
->format
=bih
->biCompression
;
405 if(st
->sample_aspect_ratio
.num
)
406 sh_video
->aspect
= codec
->width
* st
->sample_aspect_ratio
.num
407 / (float)(codec
->height
* st
->sample_aspect_ratio
.den
);
409 sh_video
->aspect
=codec
->width
* codec
->sample_aspect_ratio
.num
410 / (float)(codec
->height
* codec
->sample_aspect_ratio
.den
);
411 sh_video
->i_bps
=codec
->bit_rate
/8;
412 if (title
&& title
->value
)
413 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VID_%d_NAME=%s\n", priv
->video_streams
, title
->value
);
414 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"aspect= %d*%d/(%d*%d)\n",
415 codec
->width
, codec
->sample_aspect_ratio
.num
,
416 codec
->height
, codec
->sample_aspect_ratio
.den
);
418 sh_video
->ds
= demuxer
->video
;
419 if(codec
->extradata_size
)
420 memcpy(sh_video
->bih
+ 1, codec
->extradata
, codec
->extradata_size
);
421 if( mp_msg_test(MSGT_HEADER
,MSGL_V
) ) print_video_header(sh_video
->bih
, MSGL_V
);
429 if(demuxer
->video
->id
!= i
&& demuxer
->video
->id
!= -1)
430 st
->discard
= AVDISCARD_ALL
;
432 demuxer
->video
->id
= i
;
433 demuxer
->video
->sh
= demuxer
->v_streams
[i
];
435 stream_id
= priv
->video_streams
++;
438 case CODEC_TYPE_SUBTITLE
:{
441 /* only support text subtitles for now */
442 if(codec
->codec_id
== CODEC_ID_TEXT
)
444 else if(codec
->codec_id
== CODEC_ID_MOV_TEXT
)
446 else if(codec
->codec_id
== CODEC_ID_SSA
)
448 else if(codec
->codec_id
== CODEC_ID_DVD_SUBTITLE
)
450 else if(codec
->codec_id
== CODEC_ID_XSUB
)
452 else if(codec
->codec_id
== CODEC_ID_DVB_SUBTITLE
)
454 else if(codec
->codec_id
== CODEC_ID_DVB_TELETEXT
)
456 else if(codec
->codec_id
== CODEC_ID_HDMV_PGS_SUBTITLE
)
460 sh_sub
= new_sh_sub_sid(demuxer
, i
, priv
->sub_streams
);
462 stream_type
= "subtitle";
463 priv
->sstreams
[priv
->sub_streams
] = i
;
465 if (codec
->extradata_size
) {
466 sh_sub
->extradata
= malloc(codec
->extradata_size
);
467 memcpy(sh_sub
->extradata
, codec
->extradata
, codec
->extradata_size
);
468 sh_sub
->extradata_len
= codec
->extradata_size
;
470 if (title
&& title
->value
)
471 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SID_%d_NAME=%s\n", priv
->sub_streams
, title
->value
);
472 if (lang
&& lang
->value
) {
473 sh_sub
->lang
= strdup(lang
->value
);
474 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SID_%d_LANG=%s\n", priv
->sub_streams
, sh_sub
->lang
);
476 if (st
->disposition
& AV_DISPOSITION_DEFAULT
)
477 sh_sub
->default_track
= 1;
478 stream_id
= priv
->sub_streams
++;
481 case CODEC_TYPE_ATTACHMENT
:{
482 if (st
->codec
->codec_id
== CODEC_ID_TTF
)
483 demuxer_add_attachment(demuxer
, BSTR(st
->filename
),
484 BSTR("application/x-truetype-font"),
485 (struct bstr
){codec
->extradata
,
486 codec
->extradata_size
});
490 st
->discard
= AVDISCARD_ALL
;
493 AVCodec
*avc
= avcodec_find_decoder(codec
->codec_id
);
494 const char *codec_name
= avc
? avc
->name
: "unknown";
495 if (!avc
&& *stream_type
== 's' && demuxer
->s_streams
[stream_id
])
496 codec_name
= sh_sub_type2str(((sh_sub_t
*)demuxer
->s_streams
[stream_id
])->type
);
497 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "[lavf] stream %d: %s (%s), -%cid %d", i
, stream_type
, codec_name
, *stream_type
, stream_id
);
498 if (lang
&& lang
->value
&& *stream_type
!= 'v')
499 mp_msg(MSGT_DEMUX
, MSGL_INFO
, ", -%clang %s", *stream_type
, lang
->value
);
500 if (title
&& title
->value
)
501 mp_msg(MSGT_DEMUX
, MSGL_INFO
, ", %s", title
->value
);
502 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "\n");
506 static demuxer_t
* demux_open_lavf(demuxer_t
*demuxer
){
507 struct MPOpts
*opts
= demuxer
->opts
;
508 struct lavfdopts
*lavfdopts
= &opts
->lavfdopts
;
509 AVFormatContext
*avfc
;
510 AVFormatParameters ap
;
512 AVMetadataTag
*t
= NULL
;
513 lavf_priv_t
*priv
= demuxer
->priv
;
515 char mp_filename
[256]="mp:";
517 memset(&ap
, 0, sizeof(AVFormatParameters
));
519 stream_seek(demuxer
->stream
, 0);
521 avfc
= avformat_alloc_context();
523 if (lavfdopts
->cryptokey
)
524 parse_cryptokey(avfc
, lavfdopts
->cryptokey
);
525 if (matches_avinputformat_name(priv
, "avi")) {
526 /* for avi libavformat returns the avi timestamps in .dts,
527 * some made-up stuff that's not really pts in .pts */
528 priv
->use_dts
= true;
529 demuxer
->timestamp_type
= TIMESTAMP_TYPE_SORT
;
531 if (opts
->user_correct_pts
!= 0)
532 avfc
->flags
|= AVFMT_FLAG_GENPTS
;
535 avfc
->flags
|= AVFMT_FLAG_IGNIDX
;
537 ap
.prealloced_context
= 1;
538 if (lavfdopts
->probesize
) {
539 opt
= av_set_int(avfc
, "probesize", lavfdopts
->probesize
);
540 if(!opt
) mp_msg(MSGT_HEADER
,MSGL_ERR
, "demux_lavf, couldn't set option probesize to %u\n", lavfdopts
->probesize
);
542 if (lavfdopts
->analyzeduration
) {
543 opt
= av_set_int(avfc
, "analyzeduration",
544 lavfdopts
->analyzeduration
* AV_TIME_BASE
);
546 mp_msg(MSGT_HEADER
, MSGL_ERR
, "demux_lavf, couldn't set option "
547 "analyzeduration to %u\n", lavfdopts
->analyzeduration
);
550 if (lavfdopts
->avopt
){
551 if(parse_avopts(avfc
, lavfdopts
->avopt
) < 0){
552 mp_msg(MSGT_HEADER
,MSGL_ERR
, "Your options /%s/ look like gibberish to me pal\n", lavfdopts
->avopt
);
557 if(demuxer
->stream
->url
) {
558 if (!strncmp(demuxer
->stream
->url
, "ffmpeg://rtsp:", 14))
559 av_strlcpy(mp_filename
, demuxer
->stream
->url
+ 9, sizeof(mp_filename
));
561 av_strlcat(mp_filename
, demuxer
->stream
->url
, sizeof(mp_filename
));
563 av_strlcat(mp_filename
, "foobar.dummy", sizeof(mp_filename
));
565 priv
->pb
= av_alloc_put_byte(priv
->buffer
, BIO_BUFFER_SIZE
, 0,
566 demuxer
, mp_read
, NULL
, mp_seek
);
567 priv
->pb
->read_seek
= mp_read_seek
;
568 priv
->pb
->is_streamed
= !demuxer
->stream
->end_pos
|| (demuxer
->stream
->flags
& MP_STREAM_SEEK
) != MP_STREAM_SEEK
;
570 if(av_open_input_stream(&avfc
, priv
->pb
, mp_filename
, priv
->avif
, &ap
)<0){
571 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF_header: av_open_input_stream() failed\n");
577 if(av_find_stream_info(avfc
) < 0){
578 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF_header: av_find_stream_info() failed\n");
583 av_metadata_conv(avfc
, NULL
, avfc
->iformat
->metadata_conv
);
584 while((t
= av_metadata_get(avfc
->metadata
, "", t
, AV_METADATA_IGNORE_SUFFIX
)))
585 demux_info_add(demuxer
, t
->key
, t
->value
);
587 for(i
=0; i
< avfc
->nb_chapters
; i
++) {
588 AVChapter
*c
= avfc
->chapters
[i
];
589 uint64_t start
= av_rescale_q(c
->start
, c
->time_base
, (AVRational
){1,1000});
590 uint64_t end
= av_rescale_q(c
->end
, c
->time_base
, (AVRational
){1,1000});
591 t
= av_metadata_get(c
->metadata
, "title", NULL
, 0);
592 demuxer_add_chapter(demuxer
, t
? BSTR(t
->value
) : BSTR(NULL
), start
, end
);
595 for(i
=0; i
<avfc
->nb_streams
; i
++)
596 handle_stream(demuxer
, avfc
, i
);
597 priv
->nb_streams_last
= avfc
->nb_streams
;
599 if(avfc
->nb_programs
) {
601 for (p
= 0; p
< avfc
->nb_programs
; p
++) {
602 AVProgram
*program
= avfc
->programs
[p
];
603 t
= av_metadata_get(program
->metadata
, "title", NULL
, 0);
604 mp_msg(MSGT_HEADER
,MSGL_INFO
,"LAVF: Program %d %s\n", program
->id
, t
? t
->value
: "");
605 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "PROGRAM_ID=%d\n", program
->id
);
609 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF: %d audio and %d video streams found\n",priv
->audio_streams
,priv
->video_streams
);
610 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF: build %d\n", LIBAVFORMAT_BUILD
);
611 if(!priv
->audio_streams
) demuxer
->audio
->id
=-2; // nosound
612 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio;
613 if(!priv
->video_streams
){
614 if(!priv
->audio_streams
){
615 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF: no audio or video headers found - broken file?\n");
618 demuxer
->video
->id
=-2; // audio-only
619 } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video;
621 /* libavformat sets bitrate for mpeg based on pts at start and end
622 * of file, which fails for files with pts resets. So calculate our
623 * own bitrate estimate. */
624 if (priv
->avif
->flags
& AVFMT_TS_DISCONT
) {
625 for (int i
= 0; i
< avfc
->nb_streams
; i
++)
626 priv
->bitrate
+= avfc
->streams
[i
]->codec
->bit_rate
;
627 /* pts-based is more accurate if there are no resets; try to make
628 * a somewhat reasonable guess */
629 if (!avfc
->duration
|| avfc
->duration
== AV_NOPTS_VALUE
630 || priv
->bitrate
&& (avfc
->bit_rate
< priv
->bitrate
/ 2
631 || avfc
->bit_rate
> priv
->bitrate
* 2))
632 priv
->seek_by_bytes
= true;
634 priv
->bitrate
= 1440000;
636 demuxer
->accurate_seek
= !priv
->seek_by_bytes
;
641 static void check_internet_radio_hack(struct demuxer
*demuxer
)
643 struct lavf_priv
*priv
= demuxer
->priv
;
644 struct AVFormatContext
*avfc
= priv
->avfc
;
646 if (!matches_avinputformat_name(priv
, "ogg"))
648 if (priv
->nb_streams_last
== avfc
->nb_streams
)
650 if (avfc
->nb_streams
- priv
->nb_streams_last
== 1
651 && priv
->video_streams
== 0 && priv
->sub_streams
== 0
652 && demuxer
->a_streams
[priv
->audio_streams
-1]->format
== 0x566f // vorbis
653 && (priv
->audio_streams
== 2 || priv
->internet_radio_hack
)
654 && demuxer
->a_streams
[0]->format
== 0x566f) {
655 // extradata match could be checked but would require parsing
656 // headers, as the comment section will vary
657 if (!priv
->internet_radio_hack
) {
658 mp_msg(MSGT_DEMUX
, MSGL_V
,
659 "[lavf] enabling internet ogg radio hack\n");
660 #if LIBAVFORMAT_VERSION_MAJOR < 53
661 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "[lavf] This looks like an "
662 "internet radio ogg stream with track changes.\n"
663 "Playback will likely fail after %d track changes "
664 "due to libavformat limitations.\n"
665 "You may be able to work around that limitation by "
666 "using -demuxer ogg.\n", MAX_STREAMS
);
669 #if LIBAVFORMAT_VERSION_MAJOR < 53
670 if (avfc
->nb_streams
== MAX_STREAMS
) {
671 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "[lavf] This is the %dth "
672 "track.\nPlayback will likely fail at the next change.\n"
673 "You may be able to work around this limitation by "
674 "using -demuxer ogg.\n", MAX_STREAMS
);
677 priv
->internet_radio_hack
= true;
678 // use new per-track metadata as global metadata
679 AVMetadataTag
*t
= NULL
;
680 AVStream
*stream
= avfc
->streams
[avfc
->nb_streams
- 1];
681 while ((t
= av_metadata_get(stream
->metadata
, "", t
,
682 AV_METADATA_IGNORE_SUFFIX
)))
683 demux_info_add(demuxer
, t
->key
, t
->value
);
685 if (priv
->internet_radio_hack
)
686 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "[lavf] Internet radio ogg hack "
687 "was enabled, but stream characteristics changed.\n"
688 "This may or may not work.\n");
689 priv
->internet_radio_hack
= false;
693 static int demux_lavf_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
){
694 lavf_priv_t
*priv
= demux
->priv
;
699 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"demux_lavf_fill_buffer()\n");
701 demux
->filepos
=stream_tell(demux
->stream
);
703 if(av_read_frame(priv
->avfc
, &pkt
) < 0)
706 // handle any new streams that might have been added
707 for (id
= priv
->nb_streams_last
; id
< priv
->avfc
->nb_streams
; id
++)
708 handle_stream(demux
, priv
->avfc
, id
);
709 check_internet_radio_hack(demux
);
711 priv
->nb_streams_last
= priv
->avfc
->nb_streams
;
713 id
= pkt
.stream_index
;
715 if (id
== demux
->audio
->id
|| priv
->internet_radio_hack
) {
719 ds
->sh
=demux
->a_streams
[id
];
720 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected LAVF audio ID = %d\n",ds
->id
);
722 } else if(id
==demux
->video
->id
){
726 ds
->sh
=demux
->v_streams
[id
];
727 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected LAVF video ID = %d\n",ds
->id
);
729 } else if(id
==demux
->sub
->id
){
734 av_free_packet(&pkt
);
738 if(0/*pkt.destruct == av_destruct_packet*/){
739 //ok kids, dont try this at home :)
740 dp
=malloc(sizeof(demux_packet_t
));
748 dp
=new_demux_packet(pkt
.size
);
749 memcpy(dp
->buffer
, pkt
.data
, pkt
.size
);
750 av_free_packet(&pkt
);
753 int64_t ts
= priv
->use_dts
? pkt
.dts
: pkt
.pts
;
754 if(ts
!= AV_NOPTS_VALUE
){
755 dp
->pts
= ts
* av_q2d(priv
->avfc
->streams
[id
]->time_base
);
756 priv
->last_pts
= dp
->pts
* AV_TIME_BASE
;
757 // always set endpts for subtitles, even if PKT_FLAG_KEY is not set,
758 // otherwise they will stay on screen to long if e.g. ASS is demuxed from mkv
759 if((ds
== demux
->sub
|| (pkt
.flags
& PKT_FLAG_KEY
)) &&
760 pkt
.convergence_duration
> 0)
761 dp
->endpts
= dp
->pts
+ pkt
.convergence_duration
* av_q2d(priv
->avfc
->streams
[id
]->time_base
);
763 dp
->pos
=demux
->filepos
;
764 dp
->flags
= !!(pkt
.flags
&PKT_FLAG_KEY
);
765 // append packet to DS stream:
766 ds_add_packet(ds
,dp
);
770 static void demux_seek_lavf(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
, int flags
){
771 lavf_priv_t
*priv
= demuxer
->priv
;
773 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"demux_seek_lavf(%p, %f, %f, %d)\n", demuxer
, rel_seek_secs
, audio_delay
, flags
);
775 if (priv
->seek_by_bytes
) {
776 int64_t pos
= demuxer
->filepos
;
777 rel_seek_secs
*= priv
->bitrate
/ 8;
778 pos
+= rel_seek_secs
;
779 av_seek_frame(priv
->avfc
, -1, pos
, AVSEEK_FLAG_BYTE
);
783 if (flags
& SEEK_ABSOLUTE
) {
786 if (rel_seek_secs
< 0) avsflags
= AVSEEK_FLAG_BACKWARD
;
788 if (flags
& SEEK_FORWARD
)
790 else if (flags
& SEEK_BACKWARD
)
791 avsflags
= AVSEEK_FLAG_BACKWARD
;
792 if (flags
& SEEK_FACTOR
) {
793 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
795 priv
->last_pts
+= rel_seek_secs
* priv
->avfc
->duration
;
797 priv
->last_pts
+= rel_seek_secs
* AV_TIME_BASE
;
799 if (av_seek_frame(priv
->avfc
, -1, priv
->last_pts
, avsflags
) < 0) {
800 avsflags
^= AVSEEK_FLAG_BACKWARD
;
801 av_seek_frame(priv
->avfc
, -1, priv
->last_pts
, avsflags
);
805 static int demux_lavf_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
807 lavf_priv_t
*priv
= demuxer
->priv
;
810 case DEMUXER_CTRL_CORRECT_PTS
:
811 return DEMUXER_CTRL_OK
;
812 case DEMUXER_CTRL_GET_TIME_LENGTH
:
813 if (priv
->seek_by_bytes
) {
814 /* Our bitrate estimate may be better than would be used in
815 * otherwise similar fallback code at higher level */
816 if (demuxer
->movi_end
<= 0)
817 return DEMUXER_CTRL_DONTKNOW
;
818 *(double *)arg
= (demuxer
->movi_end
- demuxer
->movi_start
) * 8 /
820 return DEMUXER_CTRL_GUESS
;
822 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
823 return DEMUXER_CTRL_DONTKNOW
;
825 *((double *)arg
) = (double)priv
->avfc
->duration
/ AV_TIME_BASE
;
826 return DEMUXER_CTRL_OK
;
828 case DEMUXER_CTRL_GET_PERCENT_POS
:
829 if (priv
->seek_by_bytes
)
830 return DEMUXER_CTRL_DONTKNOW
; // let it use the fallback code
831 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
832 return DEMUXER_CTRL_DONTKNOW
;
834 *((int *)arg
) = (int)((priv
->last_pts
- priv
->avfc
->start_time
)*100 / priv
->avfc
->duration
);
835 return DEMUXER_CTRL_OK
;
836 case DEMUXER_CTRL_SWITCH_AUDIO
:
837 case DEMUXER_CTRL_SWITCH_VIDEO
:
839 int id
= *((int*)arg
);
842 int nstreams
, *pstreams
;
845 if(cmd
== DEMUXER_CTRL_SWITCH_VIDEO
)
848 nstreams
= priv
->video_streams
;
849 pstreams
= priv
->vstreams
;
854 nstreams
= priv
->audio_streams
;
855 pstreams
= priv
->astreams
;
857 for(i
= 0; i
< nstreams
; i
++)
859 if(pstreams
[i
] == ds
->id
) //current stream id
866 if(id
== -2) { // no sound
868 } else if(id
== -1) { // next track
869 i
= (curridx
+ 2) % (nstreams
+ 1) - 1;
873 else // select track by id
875 if (id
>= 0 && id
< nstreams
) {
881 *(int *) arg
= curridx
;
882 return DEMUXER_CTRL_OK
;
886 priv
->avfc
->streams
[ds
->id
]->discard
= AVDISCARD_ALL
;
888 *(int *) arg
= i
< 0 ? -2 : i
;
890 priv
->avfc
->streams
[newid
]->discard
= AVDISCARD_NONE
;
891 return DEMUXER_CTRL_OK
;
894 case DEMUXER_CTRL_IDENTIFY_PROGRAM
:
896 demux_program_t
*prog
= arg
;
901 prog
->vid
= prog
->aid
= prog
->sid
= -2; //no audio and no video by default
902 if(priv
->avfc
->nb_programs
< 1)
903 return DEMUXER_CTRL_DONTKNOW
;
905 if(prog
->progid
== -1)
908 while(p
<priv
->avfc
->nb_programs
&& priv
->avfc
->programs
[p
]->id
!= priv
->cur_program
)
910 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
914 for(i
=0; i
<priv
->avfc
->nb_programs
; i
++)
915 if(priv
->avfc
->programs
[i
]->id
== prog
->progid
)
917 if(i
==priv
->avfc
->nb_programs
)
918 return DEMUXER_CTRL_DONTKNOW
;
923 program
= priv
->avfc
->programs
[p
];
924 for(i
=0; i
<program
->nb_stream_indexes
; i
++)
926 switch(priv
->avfc
->streams
[program
->stream_index
[i
]]->codec
->codec_type
)
928 case CODEC_TYPE_VIDEO
:
930 prog
->vid
= program
->stream_index
[i
];
932 case CODEC_TYPE_AUDIO
:
934 prog
->aid
= program
->stream_index
[i
];
936 case CODEC_TYPE_SUBTITLE
:
937 if(prog
->sid
== -2 && priv
->avfc
->streams
[program
->stream_index
[i
]]->codec
->codec_id
== CODEC_ID_TEXT
)
938 prog
->sid
= program
->stream_index
[i
];
942 if (prog
->aid
>= 0 && prog
->aid
< MAX_A_STREAMS
&&
943 demuxer
->a_streams
[prog
->aid
]) {
944 sh_audio_t
*sh
= demuxer
->a_streams
[prog
->aid
];
948 if (prog
->vid
>= 0 && prog
->vid
< MAX_V_STREAMS
&&
949 demuxer
->v_streams
[prog
->vid
]) {
950 sh_video_t
*sh
= demuxer
->v_streams
[prog
->vid
];
954 if(prog
->progid
== -1 && prog
->vid
== -2 && prog
->aid
== -2)
956 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
958 return DEMUXER_CTRL_DONTKNOW
;
961 priv
->cur_program
= prog
->progid
= program
->id
;
962 return DEMUXER_CTRL_OK
;
965 return DEMUXER_CTRL_NOTIMPL
;
969 static void demux_close_lavf(demuxer_t
*demuxer
)
971 lavf_priv_t
* priv
= demuxer
->priv
;
975 av_freep(&priv
->avfc
->key
);
976 av_close_input_stream(priv
->avfc
);
979 free(priv
); demuxer
->priv
= NULL
;
984 const demuxer_desc_t demuxer_desc_lavf
= {
985 "libavformat demuxer",
988 "Michael Niedermayer",
989 "supports many formats, requires libavformat",
991 0, // Check after other demuxer
993 demux_lavf_fill_buffer
,
1000 const demuxer_desc_t demuxer_desc_lavf_preferred
= {
1001 "libavformat preferred demuxer",
1004 "Michael Niedermayer",
1005 "supports many formats, requires libavformat",
1006 DEMUXER_TYPE_LAVF_PREFERRED
,
1008 lavf_check_preferred_file
,
1009 demux_lavf_fill_buffer
,