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
;
81 static int mp_read(void *opaque
, uint8_t *buf
, int size
) {
82 struct demuxer
*demuxer
= opaque
;
83 struct stream
*stream
= demuxer
->stream
;
86 ret
=stream_read(stream
, buf
, size
);
88 mp_msg(MSGT_HEADER
,MSGL_DBG2
,"%d=mp_read(%p, %p, %d), pos: %"PRId64
", eof:%d\n",
89 ret
, stream
, buf
, size
, stream_tell(stream
), stream
->eof
);
93 static int64_t mp_seek(void *opaque
, int64_t pos
, int whence
) {
94 struct demuxer
*demuxer
= opaque
;
95 struct stream
*stream
= demuxer
->stream
;
97 mp_msg(MSGT_HEADER
,MSGL_DBG2
,"mp_seek(%p, %"PRId64
", %d)\n", stream
, pos
, whence
);
98 if(whence
== SEEK_CUR
)
99 pos
+=stream_tell(stream
);
100 else if(whence
== SEEK_END
&& stream
->end_pos
> 0)
101 pos
+= stream
->end_pos
;
102 else if(whence
== SEEK_SET
)
103 pos
+= stream
->start_pos
;
104 else if(whence
== AVSEEK_SIZE
&& stream
->end_pos
> 0)
105 return stream
->end_pos
- stream
->start_pos
;
111 current_pos
= stream_tell(stream
);
112 if(stream_seek(stream
, pos
)==0) {
113 stream_reset(stream
);
114 stream_seek(stream
, current_pos
);
118 return pos
- stream
->start_pos
;
121 static int64_t mp_read_seek(void *opaque
, int stream_idx
, int64_t ts
, int flags
)
123 struct demuxer
*demuxer
= opaque
;
124 struct stream
*stream
= demuxer
->stream
;
125 struct lavf_priv
*priv
= demuxer
->priv
;
127 AVStream
*st
= priv
->avfc
->streams
[stream_idx
];
128 double pts
= (double)ts
* st
->time_base
.num
/ st
->time_base
.den
;
129 int ret
= stream_control(stream
, STREAM_CTRL_SEEK_TO_TIME
, &pts
);
131 ret
= AVERROR(ENOSYS
);
135 static void list_formats(void) {
137 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "Available lavf input formats:\n");
138 for (fmt
= first_iformat
; fmt
; fmt
= fmt
->next
)
139 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "%15s : %s\n", fmt
->name
, fmt
->long_name
);
142 static int lavf_check_file(demuxer_t
*demuxer
){
143 struct MPOpts
*opts
= demuxer
->opts
;
144 struct lavfdopts
*lavfdopts
= &opts
->lavfdopts
;
147 int probe_data_size
= 0;
148 int read_size
= INITIAL_PROBE_SIZE
;
152 demuxer
->priv
=calloc(sizeof(lavf_priv_t
),1);
157 char *format
= lavfdopts
->format
;
159 format
= demuxer
->stream
->lavf_type
;
161 if (strcmp(format
, "help") == 0) {
165 priv
->avif
= av_find_input_format(format
);
167 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "Unknown lavf format %s\n", format
);
170 mp_msg(MSGT_DEMUX
,MSGL_INFO
,"Forced lavf %s demuxer\n", priv
->avif
->long_name
);
171 return DEMUXER_TYPE_LAVF
;
174 avpd
.buf
= av_mallocz(FFMAX(BIO_BUFFER_SIZE
, PROBE_BUF_SIZE
) +
175 FF_INPUT_BUFFER_PADDING_SIZE
);
177 read_size
= stream_read(demuxer
->stream
, avpd
.buf
+ probe_data_size
, read_size
);
182 probe_data_size
+= read_size
;
183 avpd
.filename
= demuxer
->stream
->url
;
184 if (!avpd
.filename
) {
185 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "Stream url is not set!\n");
188 if (!strncmp(avpd
.filename
, "ffmpeg://", 9))
190 avpd
.buf_size
= probe_data_size
;
193 priv
->avif
= av_probe_input_format2(&avpd
, probe_data_size
> 0, &score
);
194 read_size
= FFMIN(2*read_size
, PROBE_BUF_SIZE
- probe_data_size
);
195 } while ((demuxer
->desc
->type
!= DEMUXER_TYPE_LAVF_PREFERRED
||
196 probe_data_size
< SMALL_MAX_PROBE_SIZE
) &&
197 score
<= AVPROBE_SCORE_MAX
/ 4 &&
198 read_size
> 0 && probe_data_size
< PROBE_BUF_SIZE
);
202 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF_check: no clue about this gibberish!\n");
205 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF_check: %s\n", priv
->avif
->long_name
);
207 return DEMUXER_TYPE_LAVF
;
210 static bool matches_avinputformat_name(struct lavf_priv
*priv
,
213 const char *avifname
= priv
->avif
->name
;
215 const char *next
= strchr(avifname
, ',');
217 return !strcmp(avifname
, name
);
218 int len
= next
- avifname
;
219 if (len
== strlen(name
) && !memcmp(avifname
, name
, len
))
225 static const char * const preferred_list
[] = {
231 "mov", "mp4", // "mov,mp4,m4a,3gp,3g2,mj2" is one AVInputFormat
243 static int lavf_check_preferred_file(demuxer_t
*demuxer
){
244 if (lavf_check_file(demuxer
)) {
245 const char * const *p
;
246 lavf_priv_t
*priv
= demuxer
->priv
;
247 for (p
= preferred_list
; *p
; p
++)
248 if (matches_avinputformat_name(priv
, *p
))
249 return DEMUXER_TYPE_LAVF_PREFERRED
;
254 static uint8_t char2int(char c
) {
255 if (c
>= '0' && c
<= '9') return c
- '0';
256 if (c
>= 'a' && c
<= 'f') return c
- 'a' + 10;
257 if (c
>= 'A' && c
<= 'F') return c
- 'A' + 10;
261 static void parse_cryptokey(AVFormatContext
*avfc
, const char *str
) {
262 int len
= strlen(str
) / 2;
263 uint8_t *key
= av_mallocz(len
);
267 for (i
= 0; i
< len
; i
++, str
+= 2)
268 *key
++ = (char2int(str
[0]) << 4) | char2int(str
[1]);
271 static void handle_stream(demuxer_t
*demuxer
, AVFormatContext
*avfc
, int i
) {
272 lavf_priv_t
*priv
= demuxer
->priv
;
273 AVStream
*st
= avfc
->streams
[i
];
274 AVCodecContext
*codec
= st
->codec
;
275 char *stream_type
= NULL
;
277 AVMetadataTag
*lang
= av_metadata_get(st
->metadata
, "language", NULL
, 0);
278 AVMetadataTag
*title
= av_metadata_get(st
->metadata
, "title", NULL
, 0);
279 int g
, override_tag
= mp_av_codec_get_tag(mp_codecid_override_taglists
,
281 // For some formats (like PCM) always trust CODEC_ID_* more than codec_tag
283 codec
->codec_tag
= override_tag
;
285 switch(codec
->codec_type
){
286 case CODEC_TYPE_AUDIO
:{
288 sh_audio_t
* sh_audio
;
289 sh_audio
= new_sh_audio_aid(demuxer
, i
, priv
->audio_streams
);
292 stream_type
= "audio";
293 priv
->astreams
[priv
->audio_streams
] = i
;
294 wf
= calloc(sizeof(*wf
) + codec
->extradata_size
, 1);
295 // mp4a tag is used for all mp4 files no matter what they actually contain
296 if(codec
->codec_tag
== MKTAG('m', 'p', '4', 'a'))
298 if(!codec
->codec_tag
)
299 codec
->codec_tag
= mp_av_codec_get_tag(mp_wav_taglists
, codec
->codec_id
);
300 wf
->wFormatTag
= codec
->codec_tag
;
301 wf
->nChannels
= codec
->channels
;
302 wf
->nSamplesPerSec
= codec
->sample_rate
;
303 wf
->nAvgBytesPerSec
= codec
->bit_rate
/8;
304 wf
->nBlockAlign
= codec
->block_align
? codec
->block_align
: 1;
305 wf
->wBitsPerSample
= codec
->bits_per_coded_sample
;
306 wf
->cbSize
= codec
->extradata_size
;
307 if(codec
->extradata_size
)
308 memcpy(wf
+ 1, codec
->extradata
, codec
->extradata_size
);
310 sh_audio
->audio
.dwSampleSize
= codec
->block_align
;
311 if(codec
->frame_size
&& codec
->sample_rate
){
312 sh_audio
->audio
.dwScale
=codec
->frame_size
;
313 sh_audio
->audio
.dwRate
= codec
->sample_rate
;
315 sh_audio
->audio
.dwScale
= codec
->block_align
? codec
->block_align
*8 : 8;
316 sh_audio
->audio
.dwRate
= codec
->bit_rate
;
318 g
= av_gcd(sh_audio
->audio
.dwScale
, sh_audio
->audio
.dwRate
);
319 sh_audio
->audio
.dwScale
/= g
;
320 sh_audio
->audio
.dwRate
/= g
;
321 // 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);
322 sh_audio
->ds
= demuxer
->audio
;
323 sh_audio
->format
= codec
->codec_tag
;
324 sh_audio
->channels
= codec
->channels
;
325 sh_audio
->samplerate
= codec
->sample_rate
;
326 sh_audio
->i_bps
= codec
->bit_rate
/8;
327 switch (codec
->codec_id
) {
328 case CODEC_ID_PCM_S8
:
329 case CODEC_ID_PCM_U8
:
330 sh_audio
->samplesize
= 1;
332 case CODEC_ID_PCM_S16LE
:
333 case CODEC_ID_PCM_S16BE
:
334 case CODEC_ID_PCM_U16LE
:
335 case CODEC_ID_PCM_U16BE
:
336 sh_audio
->samplesize
= 2;
338 case CODEC_ID_PCM_ALAW
:
339 sh_audio
->format
= 0x6;
341 case CODEC_ID_PCM_MULAW
:
342 sh_audio
->format
= 0x7;
345 if (title
&& title
->value
)
346 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AID_%d_NAME=%s\n", priv
->audio_streams
, title
->value
);
347 if (lang
&& lang
->value
) {
348 sh_audio
->lang
= strdup(lang
->value
);
349 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AID_%d_LANG=%s\n", priv
->audio_streams
, sh_audio
->lang
);
351 if (st
->disposition
& AV_DISPOSITION_DEFAULT
)
352 sh_audio
->default_track
= 1;
353 if(mp_msg_test(MSGT_HEADER
,MSGL_V
) ) print_wave_header(sh_audio
->wf
, MSGL_V
);
354 // select the first audio stream
355 if (!demuxer
->audio
->sh
) {
356 demuxer
->audio
->id
= i
;
357 demuxer
->audio
->sh
= demuxer
->a_streams
[i
];
359 st
->discard
= AVDISCARD_ALL
;
360 stream_id
= priv
->audio_streams
++;
363 case CODEC_TYPE_VIDEO
:{
364 sh_video_t
* sh_video
;
365 BITMAPINFOHEADER
*bih
;
366 sh_video
=new_sh_video_vid(demuxer
, i
, priv
->video_streams
);
368 stream_type
= "video";
369 priv
->vstreams
[priv
->video_streams
] = i
;
370 bih
=calloc(sizeof(*bih
) + codec
->extradata_size
,1);
372 if(codec
->codec_id
== CODEC_ID_RAWVIDEO
) {
373 switch (codec
->pix_fmt
) {
375 codec
->codec_tag
= MKTAG(24, 'B', 'G', 'R');
378 if(!codec
->codec_tag
)
379 codec
->codec_tag
= mp_av_codec_get_tag(mp_bmp_taglists
, codec
->codec_id
);
380 bih
->biSize
= sizeof(*bih
) + codec
->extradata_size
;
381 bih
->biWidth
= codec
->width
;
382 bih
->biHeight
= codec
->height
;
383 bih
->biBitCount
= codec
->bits_per_coded_sample
;
384 bih
->biSizeImage
= bih
->biWidth
* bih
->biHeight
* bih
->biBitCount
/8;
385 bih
->biCompression
= codec
->codec_tag
;
387 sh_video
->disp_w
= codec
->width
;
388 sh_video
->disp_h
= codec
->height
;
389 if (st
->time_base
.den
) { /* if container has time_base, use that */
390 sh_video
->video
.dwRate
= st
->time_base
.den
;
391 sh_video
->video
.dwScale
= st
->time_base
.num
;
393 sh_video
->video
.dwRate
= codec
->time_base
.den
;
394 sh_video
->video
.dwScale
= codec
->time_base
.num
;
396 sh_video
->fps
=av_q2d(st
->r_frame_rate
);
397 sh_video
->frametime
=1/av_q2d(st
->r_frame_rate
);
398 sh_video
->format
=bih
->biCompression
;
399 if(st
->sample_aspect_ratio
.num
)
400 sh_video
->aspect
= codec
->width
* st
->sample_aspect_ratio
.num
401 / (float)(codec
->height
* st
->sample_aspect_ratio
.den
);
403 sh_video
->aspect
=codec
->width
* codec
->sample_aspect_ratio
.num
404 / (float)(codec
->height
* codec
->sample_aspect_ratio
.den
);
405 sh_video
->i_bps
=codec
->bit_rate
/8;
406 if (title
&& title
->value
)
407 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VID_%d_NAME=%s\n", priv
->video_streams
, title
->value
);
408 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"aspect= %d*%d/(%d*%d)\n",
409 codec
->width
, codec
->sample_aspect_ratio
.num
,
410 codec
->height
, codec
->sample_aspect_ratio
.den
);
412 sh_video
->ds
= demuxer
->video
;
413 if(codec
->extradata_size
)
414 memcpy(sh_video
->bih
+ 1, codec
->extradata
, codec
->extradata_size
);
415 if( mp_msg_test(MSGT_HEADER
,MSGL_V
) ) print_video_header(sh_video
->bih
, MSGL_V
);
423 if(demuxer
->video
->id
!= i
&& demuxer
->video
->id
!= -1)
424 st
->discard
= AVDISCARD_ALL
;
426 demuxer
->video
->id
= i
;
427 demuxer
->video
->sh
= demuxer
->v_streams
[i
];
429 stream_id
= priv
->video_streams
++;
432 case CODEC_TYPE_SUBTITLE
:{
435 /* only support text subtitles for now */
436 if(codec
->codec_id
== CODEC_ID_TEXT
)
438 else if(codec
->codec_id
== CODEC_ID_MOV_TEXT
)
440 else if(codec
->codec_id
== CODEC_ID_SSA
)
442 else if(codec
->codec_id
== CODEC_ID_DVD_SUBTITLE
)
444 else if(codec
->codec_id
== CODEC_ID_XSUB
)
446 else if(codec
->codec_id
== CODEC_ID_DVB_SUBTITLE
)
448 else if(codec
->codec_id
== CODEC_ID_DVB_TELETEXT
)
450 else if(codec
->codec_id
== CODEC_ID_HDMV_PGS_SUBTITLE
)
454 sh_sub
= new_sh_sub_sid(demuxer
, i
, priv
->sub_streams
);
456 stream_type
= "subtitle";
457 priv
->sstreams
[priv
->sub_streams
] = i
;
459 if (codec
->extradata_size
) {
460 sh_sub
->extradata
= malloc(codec
->extradata_size
);
461 memcpy(sh_sub
->extradata
, codec
->extradata
, codec
->extradata_size
);
462 sh_sub
->extradata_len
= codec
->extradata_size
;
464 if (title
&& title
->value
)
465 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SID_%d_NAME=%s\n", priv
->sub_streams
, title
->value
);
466 if (lang
&& lang
->value
) {
467 sh_sub
->lang
= strdup(lang
->value
);
468 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SID_%d_LANG=%s\n", priv
->sub_streams
, sh_sub
->lang
);
470 if (st
->disposition
& AV_DISPOSITION_DEFAULT
)
471 sh_sub
->default_track
= 1;
472 stream_id
= priv
->sub_streams
++;
475 case CODEC_TYPE_ATTACHMENT
:{
476 if (st
->codec
->codec_id
== CODEC_ID_TTF
)
477 demuxer_add_attachment(demuxer
, BSTR(st
->filename
),
478 BSTR("application/x-truetype-font"),
479 (struct bstr
){codec
->extradata
,
480 codec
->extradata_size
});
484 st
->discard
= AVDISCARD_ALL
;
487 AVCodec
*avc
= avcodec_find_decoder(codec
->codec_id
);
488 const char *codec_name
= avc
? avc
->name
: "unknown";
489 if (!avc
&& *stream_type
== 's' && demuxer
->s_streams
[stream_id
])
490 codec_name
= sh_sub_type2str(((sh_sub_t
*)demuxer
->s_streams
[stream_id
])->type
);
491 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "[lavf] stream %d: %s (%s), -%cid %d", i
, stream_type
, codec_name
, *stream_type
, stream_id
);
492 if (lang
&& lang
->value
&& *stream_type
!= 'v')
493 mp_msg(MSGT_DEMUX
, MSGL_INFO
, ", -%clang %s", *stream_type
, lang
->value
);
494 if (title
&& title
->value
)
495 mp_msg(MSGT_DEMUX
, MSGL_INFO
, ", %s", title
->value
);
496 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "\n");
500 static demuxer_t
* demux_open_lavf(demuxer_t
*demuxer
){
501 struct MPOpts
*opts
= demuxer
->opts
;
502 struct lavfdopts
*lavfdopts
= &opts
->lavfdopts
;
503 AVFormatContext
*avfc
;
504 AVFormatParameters ap
;
506 AVMetadataTag
*t
= NULL
;
507 lavf_priv_t
*priv
= demuxer
->priv
;
509 char mp_filename
[256]="mp:";
511 memset(&ap
, 0, sizeof(AVFormatParameters
));
513 stream_seek(demuxer
->stream
, 0);
515 avfc
= avformat_alloc_context();
517 if (lavfdopts
->cryptokey
)
518 parse_cryptokey(avfc
, lavfdopts
->cryptokey
);
519 if (opts
->user_correct_pts
!= 0)
520 avfc
->flags
|= AVFMT_FLAG_GENPTS
;
522 avfc
->flags
|= AVFMT_FLAG_IGNIDX
;
524 ap
.prealloced_context
= 1;
525 if (lavfdopts
->probesize
) {
526 opt
= av_set_int(avfc
, "probesize", lavfdopts
->probesize
);
527 if(!opt
) mp_msg(MSGT_HEADER
,MSGL_ERR
, "demux_lavf, couldn't set option probesize to %u\n", lavfdopts
->probesize
);
529 if (lavfdopts
->analyzeduration
) {
530 opt
= av_set_int(avfc
, "analyzeduration",
531 lavfdopts
->analyzeduration
* AV_TIME_BASE
);
533 mp_msg(MSGT_HEADER
, MSGL_ERR
, "demux_lavf, couldn't set option "
534 "analyzeduration to %u\n", lavfdopts
->analyzeduration
);
537 if (lavfdopts
->avopt
){
538 if(parse_avopts(avfc
, lavfdopts
->avopt
) < 0){
539 mp_msg(MSGT_HEADER
,MSGL_ERR
, "Your options /%s/ look like gibberish to me pal\n", lavfdopts
->avopt
);
544 if(demuxer
->stream
->url
) {
545 if (!strncmp(demuxer
->stream
->url
, "ffmpeg://rtsp:", 14))
546 av_strlcpy(mp_filename
, demuxer
->stream
->url
+ 9, sizeof(mp_filename
));
548 av_strlcat(mp_filename
, demuxer
->stream
->url
, sizeof(mp_filename
));
550 av_strlcat(mp_filename
, "foobar.dummy", sizeof(mp_filename
));
552 priv
->pb
= av_alloc_put_byte(priv
->buffer
, BIO_BUFFER_SIZE
, 0,
553 demuxer
, mp_read
, NULL
, mp_seek
);
554 priv
->pb
->read_seek
= mp_read_seek
;
555 priv
->pb
->is_streamed
= !demuxer
->stream
->end_pos
|| (demuxer
->stream
->flags
& MP_STREAM_SEEK
) != MP_STREAM_SEEK
;
557 if(av_open_input_stream(&avfc
, priv
->pb
, mp_filename
, priv
->avif
, &ap
)<0){
558 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF_header: av_open_input_stream() failed\n");
564 if(av_find_stream_info(avfc
) < 0){
565 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF_header: av_find_stream_info() failed\n");
570 av_metadata_conv(avfc
, NULL
, avfc
->iformat
->metadata_conv
);
571 while((t
= av_metadata_get(avfc
->metadata
, "", t
, AV_METADATA_IGNORE_SUFFIX
)))
572 demux_info_add(demuxer
, t
->key
, t
->value
);
574 for(i
=0; i
< avfc
->nb_chapters
; i
++) {
575 AVChapter
*c
= avfc
->chapters
[i
];
576 uint64_t start
= av_rescale_q(c
->start
, c
->time_base
, (AVRational
){1,1000});
577 uint64_t end
= av_rescale_q(c
->end
, c
->time_base
, (AVRational
){1,1000});
578 t
= av_metadata_get(c
->metadata
, "title", NULL
, 0);
579 demuxer_add_chapter(demuxer
, t
? BSTR(t
->value
) : BSTR(NULL
), start
, end
);
582 for(i
=0; i
<avfc
->nb_streams
; i
++)
583 handle_stream(demuxer
, avfc
, i
);
584 priv
->nb_streams_last
= avfc
->nb_streams
;
586 if(avfc
->nb_programs
) {
588 for (p
= 0; p
< avfc
->nb_programs
; p
++) {
589 AVProgram
*program
= avfc
->programs
[p
];
590 t
= av_metadata_get(program
->metadata
, "title", NULL
, 0);
591 mp_msg(MSGT_HEADER
,MSGL_INFO
,"LAVF: Program %d %s\n", program
->id
, t
? t
->value
: "");
595 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF: %d audio and %d video streams found\n",priv
->audio_streams
,priv
->video_streams
);
596 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF: build %d\n", LIBAVFORMAT_BUILD
);
597 if(!priv
->audio_streams
) demuxer
->audio
->id
=-2; // nosound
598 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio;
599 if(!priv
->video_streams
){
600 if(!priv
->audio_streams
){
601 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF: no audio or video headers found - broken file?\n");
604 demuxer
->video
->id
=-2; // audio-only
605 } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video;
607 demuxer
->accurate_seek
= true;
612 static void check_internet_radio_hack(struct demuxer
*demuxer
)
614 struct lavf_priv
*priv
= demuxer
->priv
;
615 struct AVFormatContext
*avfc
= priv
->avfc
;
617 if (!matches_avinputformat_name(priv
, "ogg"))
619 if (priv
->nb_streams_last
== avfc
->nb_streams
)
621 if (avfc
->nb_streams
- priv
->nb_streams_last
== 1
622 && priv
->video_streams
== 0 && priv
->sub_streams
== 0
623 && demuxer
->a_streams
[priv
->audio_streams
-1]->format
== 0x566f // vorbis
624 && (priv
->audio_streams
== 2 || priv
->internet_radio_hack
)
625 && demuxer
->a_streams
[0]->format
== 0x566f) {
626 // extradata match could be checked but would require parsing
627 // headers, as the comment section will vary
628 if (!priv
->internet_radio_hack
) {
629 mp_msg(MSGT_DEMUX
, MSGL_V
,
630 "[lavf] enabling internet ogg radio hack\n");
631 #if LIBAVFORMAT_VERSION_MAJOR < 53
632 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "[lavf] This looks like an "
633 "internet radio ogg stream with track changes.\n"
634 "Playback will likely fail after %d track changes "
635 "due to libavformat limitations.\n"
636 "You may be able to work around that limitation by "
637 "using -demuxer ogg.\n", MAX_STREAMS
);
640 #if LIBAVFORMAT_VERSION_MAJOR < 53
641 if (avfc
->nb_streams
== MAX_STREAMS
) {
642 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "[lavf] This is the %dth "
643 "track.\nPlayback will likely fail at the next change.\n"
644 "You may be able to work around this limitation by "
645 "using -demuxer ogg.\n", MAX_STREAMS
);
648 priv
->internet_radio_hack
= true;
649 // use new per-track metadata as global metadata
650 AVMetadataTag
*t
= NULL
;
651 AVStream
*stream
= avfc
->streams
[avfc
->nb_streams
- 1];
652 while ((t
= av_metadata_get(stream
->metadata
, "", t
,
653 AV_METADATA_IGNORE_SUFFIX
)))
654 demux_info_add(demuxer
, t
->key
, t
->value
);
656 if (priv
->internet_radio_hack
)
657 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "[lavf] Internet radio ogg hack "
658 "was enabled, but stream characteristics changed.\n"
659 "This may or may not work.\n");
660 priv
->internet_radio_hack
= false;
664 static int demux_lavf_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
){
665 lavf_priv_t
*priv
= demux
->priv
;
670 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"demux_lavf_fill_buffer()\n");
672 demux
->filepos
=stream_tell(demux
->stream
);
674 if(av_read_frame(priv
->avfc
, &pkt
) < 0)
677 // handle any new streams that might have been added
678 for (id
= priv
->nb_streams_last
; id
< priv
->avfc
->nb_streams
; id
++)
679 handle_stream(demux
, priv
->avfc
, id
);
680 check_internet_radio_hack(demux
);
682 priv
->nb_streams_last
= priv
->avfc
->nb_streams
;
684 id
= pkt
.stream_index
;
686 if (id
== demux
->audio
->id
|| priv
->internet_radio_hack
) {
690 ds
->sh
=demux
->a_streams
[id
];
691 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected LAVF audio ID = %d\n",ds
->id
);
693 } else if(id
==demux
->video
->id
){
697 ds
->sh
=demux
->v_streams
[id
];
698 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected LAVF video ID = %d\n",ds
->id
);
700 } else if(id
==demux
->sub
->id
){
705 av_free_packet(&pkt
);
709 if(0/*pkt.destruct == av_destruct_packet*/){
710 //ok kids, dont try this at home :)
711 dp
=malloc(sizeof(demux_packet_t
));
719 dp
=new_demux_packet(pkt
.size
);
720 memcpy(dp
->buffer
, pkt
.data
, pkt
.size
);
721 av_free_packet(&pkt
);
724 if(pkt
.pts
!= AV_NOPTS_VALUE
){
725 dp
->pts
=pkt
.pts
* av_q2d(priv
->avfc
->streams
[id
]->time_base
);
726 priv
->last_pts
= dp
->pts
* AV_TIME_BASE
;
727 // always set endpts for subtitles, even if PKT_FLAG_KEY is not set,
728 // otherwise they will stay on screen to long if e.g. ASS is demuxed from mkv
729 if((ds
== demux
->sub
|| (pkt
.flags
& PKT_FLAG_KEY
)) &&
730 pkt
.convergence_duration
> 0)
731 dp
->endpts
= dp
->pts
+ pkt
.convergence_duration
* av_q2d(priv
->avfc
->streams
[id
]->time_base
);
733 dp
->pos
=demux
->filepos
;
734 dp
->flags
= !!(pkt
.flags
&PKT_FLAG_KEY
);
735 // append packet to DS stream:
736 ds_add_packet(ds
,dp
);
740 static void demux_seek_lavf(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
, int flags
){
741 lavf_priv_t
*priv
= demuxer
->priv
;
743 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"demux_seek_lavf(%p, %f, %f, %d)\n", demuxer
, rel_seek_secs
, audio_delay
, flags
);
745 if (flags
& SEEK_ABSOLUTE
) {
748 if (rel_seek_secs
< 0) avsflags
= AVSEEK_FLAG_BACKWARD
;
750 if (flags
& SEEK_FORWARD
)
752 else if (flags
& SEEK_BACKWARD
)
753 avsflags
= AVSEEK_FLAG_BACKWARD
;
754 if (flags
& SEEK_FACTOR
) {
755 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
757 priv
->last_pts
+= rel_seek_secs
* priv
->avfc
->duration
;
759 priv
->last_pts
+= rel_seek_secs
* AV_TIME_BASE
;
761 if (av_seek_frame(priv
->avfc
, -1, priv
->last_pts
, avsflags
) < 0) {
762 avsflags
^= AVSEEK_FLAG_BACKWARD
;
763 av_seek_frame(priv
->avfc
, -1, priv
->last_pts
, avsflags
);
767 static int demux_lavf_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
769 lavf_priv_t
*priv
= demuxer
->priv
;
772 case DEMUXER_CTRL_CORRECT_PTS
:
773 return DEMUXER_CTRL_OK
;
774 case DEMUXER_CTRL_GET_TIME_LENGTH
:
775 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
776 return DEMUXER_CTRL_DONTKNOW
;
778 *((double *)arg
) = (double)priv
->avfc
->duration
/ AV_TIME_BASE
;
779 return DEMUXER_CTRL_OK
;
781 case DEMUXER_CTRL_GET_PERCENT_POS
:
782 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
783 return DEMUXER_CTRL_DONTKNOW
;
785 *((int *)arg
) = (int)((priv
->last_pts
- priv
->avfc
->start_time
)*100 / priv
->avfc
->duration
);
786 return DEMUXER_CTRL_OK
;
787 case DEMUXER_CTRL_SWITCH_AUDIO
:
788 case DEMUXER_CTRL_SWITCH_VIDEO
:
790 int id
= *((int*)arg
);
793 int nstreams
, *pstreams
;
796 if(cmd
== DEMUXER_CTRL_SWITCH_VIDEO
)
799 nstreams
= priv
->video_streams
;
800 pstreams
= priv
->vstreams
;
805 nstreams
= priv
->audio_streams
;
806 pstreams
= priv
->astreams
;
808 for(i
= 0; i
< nstreams
; i
++)
810 if(pstreams
[i
] == ds
->id
) //current stream id
817 if(id
== -2) { // no sound
819 } else if(id
== -1) { // next track
820 i
= (curridx
+ 2) % (nstreams
+ 1) - 1;
824 else // select track by id
826 if (id
>= 0 && id
< nstreams
) {
832 *(int *) arg
= curridx
;
833 return DEMUXER_CTRL_OK
;
837 priv
->avfc
->streams
[ds
->id
]->discard
= AVDISCARD_ALL
;
839 *(int *) arg
= i
< 0 ? -2 : i
;
841 priv
->avfc
->streams
[newid
]->discard
= AVDISCARD_NONE
;
842 return DEMUXER_CTRL_OK
;
845 case DEMUXER_CTRL_IDENTIFY_PROGRAM
:
847 demux_program_t
*prog
= arg
;
852 prog
->vid
= prog
->aid
= prog
->sid
= -2; //no audio and no video by default
853 if(priv
->avfc
->nb_programs
< 1)
854 return DEMUXER_CTRL_DONTKNOW
;
856 if(prog
->progid
== -1)
859 while(p
<priv
->avfc
->nb_programs
&& priv
->avfc
->programs
[p
]->id
!= priv
->cur_program
)
861 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
865 for(i
=0; i
<priv
->avfc
->nb_programs
; i
++)
866 if(priv
->avfc
->programs
[i
]->id
== prog
->progid
)
868 if(i
==priv
->avfc
->nb_programs
)
869 return DEMUXER_CTRL_DONTKNOW
;
874 program
= priv
->avfc
->programs
[p
];
875 for(i
=0; i
<program
->nb_stream_indexes
; i
++)
877 switch(priv
->avfc
->streams
[program
->stream_index
[i
]]->codec
->codec_type
)
879 case CODEC_TYPE_VIDEO
:
881 prog
->vid
= program
->stream_index
[i
];
883 case CODEC_TYPE_AUDIO
:
885 prog
->aid
= program
->stream_index
[i
];
887 case CODEC_TYPE_SUBTITLE
:
888 if(prog
->sid
== -2 && priv
->avfc
->streams
[program
->stream_index
[i
]]->codec
->codec_id
== CODEC_ID_TEXT
)
889 prog
->sid
= program
->stream_index
[i
];
893 if(prog
->progid
== -1 && prog
->vid
== -2 && prog
->aid
== -2)
895 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
897 return DEMUXER_CTRL_DONTKNOW
;
900 priv
->cur_program
= prog
->progid
= program
->id
;
901 return DEMUXER_CTRL_OK
;
904 return DEMUXER_CTRL_NOTIMPL
;
908 static void demux_close_lavf(demuxer_t
*demuxer
)
910 lavf_priv_t
* priv
= demuxer
->priv
;
914 av_freep(&priv
->avfc
->key
);
915 av_close_input_stream(priv
->avfc
);
918 free(priv
); demuxer
->priv
= NULL
;
923 const demuxer_desc_t demuxer_desc_lavf
= {
924 "libavformat demuxer",
927 "Michael Niedermayer",
928 "supports many formats, requires libavformat",
930 0, // Check after other demuxer
932 demux_lavf_fill_buffer
,
939 const demuxer_desc_t demuxer_desc_lavf_preferred
= {
940 "libavformat preferred demuxer",
943 "Michael Niedermayer",
944 "supports many formats, requires libavformat",
945 DEMUXER_TYPE_LAVF_PREFERRED
,
947 lavf_check_preferred_file
,
948 demux_lavf_fill_buffer
,