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>
31 #include "stream/stream.h"
36 #include "libvo/sub.h"
38 #include "libavformat/avformat.h"
39 #include "libavformat/avio.h"
40 #include "libavutil/avutil.h"
41 #include "libavutil/avstring.h"
42 #include "libavcodec/opt.h"
44 #include "mp_taglists.h"
46 #define INITIAL_PROBE_SIZE STREAM_BUFFER_SIZE
47 #define SMALL_MAX_PROBE_SIZE (32 * 1024)
48 #define PROBE_BUF_SIZE (2*1024*1024)
50 extern char *audio_lang
;
51 extern char *dvdsub_lang
;
53 static unsigned int opt_probesize
= 0;
54 static unsigned int opt_analyzeduration
= 0;
55 static char *opt_format
;
56 static char *opt_cryptokey
;
57 static char *opt_avopt
= NULL
;
59 const m_option_t lavfdopts_conf
[] = {
60 {"probesize", &(opt_probesize
), CONF_TYPE_INT
, CONF_RANGE
, 32, INT_MAX
, NULL
},
61 {"format", &(opt_format
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
62 {"analyzeduration", &(opt_analyzeduration
), CONF_TYPE_INT
, CONF_RANGE
, 0, INT_MAX
, NULL
},
63 {"cryptokey", &(opt_cryptokey
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
64 {"o", &opt_avopt
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
65 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
68 #define BIO_BUFFER_SIZE 32768
70 typedef struct lavf_priv
{
72 AVFormatContext
*avfc
;
74 uint8_t buffer
[BIO_BUFFER_SIZE
];
79 int astreams
[MAX_A_STREAMS
];
80 int vstreams
[MAX_V_STREAMS
];
81 int sstreams
[MAX_S_STREAMS
];
85 static int mp_read(void *opaque
, uint8_t *buf
, int size
) {
86 demuxer_t
*demuxer
= opaque
;
87 stream_t
*stream
= demuxer
->stream
;
90 if(stream_eof(stream
)) //needed?
92 ret
=stream_read(stream
, buf
, size
);
94 mp_msg(MSGT_HEADER
,MSGL_DBG2
,"%d=mp_read(%p, %p, %d), eof:%d\n", ret
, stream
, buf
, size
, stream
->eof
);
98 static int64_t mp_seek(void *opaque
, int64_t pos
, int whence
) {
99 demuxer_t
*demuxer
= opaque
;
100 stream_t
*stream
= demuxer
->stream
;
102 mp_msg(MSGT_HEADER
,MSGL_DBG2
,"mp_seek(%p, %"PRId64
", %d)\n", stream
, pos
, whence
);
103 if(whence
== SEEK_CUR
)
104 pos
+=stream_tell(stream
);
105 else if(whence
== SEEK_END
&& stream
->end_pos
> 0)
106 pos
+= stream
->end_pos
;
107 else if(whence
== SEEK_SET
)
108 pos
+= stream
->start_pos
;
109 else if(whence
== AVSEEK_SIZE
&& stream
->end_pos
> 0)
110 return stream
->end_pos
- stream
->start_pos
;
116 if(pos
<stream
->end_pos
&& stream
->eof
)
117 stream_reset(stream
);
118 current_pos
= stream_tell(stream
);
119 if(stream_seek(stream
, pos
)==0) {
120 stream_reset(stream
);
121 stream_seek(stream
, current_pos
);
125 return pos
- stream
->start_pos
;
128 static int64_t mp_read_seek(void *opaque
, int stream_idx
, int64_t ts
, int flags
) {
129 demuxer_t
*demuxer
= opaque
;
130 stream_t
*stream
= demuxer
->stream
;
131 lavf_priv_t
*priv
= demuxer
->priv
;
132 AVStream
*st
= priv
->avfc
->streams
[stream_idx
];
136 pts
= (double)ts
* st
->time_base
.num
/ st
->time_base
.den
;
137 ret
= stream_control(stream
, STREAM_CTRL_SEEK_TO_TIME
, &pts
);
139 ret
= AVERROR(ENOSYS
);
143 static void list_formats(void) {
145 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "Available lavf input formats:\n");
146 for (fmt
= first_iformat
; fmt
; fmt
= fmt
->next
)
147 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "%15s : %s\n", fmt
->name
, fmt
->long_name
);
150 static int lavf_check_file(demuxer_t
*demuxer
){
153 int probe_data_size
= 0;
154 int read_size
= INITIAL_PROBE_SIZE
;
158 demuxer
->priv
=calloc(sizeof(lavf_priv_t
),1);
164 if (strcmp(opt_format
, "help") == 0) {
168 priv
->avif
= av_find_input_format(opt_format
);
170 mp_msg(MSGT_DEMUX
,MSGL_FATAL
,"Unknown lavf format %s\n", opt_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 (!strncmp(avpd
.filename
, "ffmpeg://", 9))
189 avpd
.buf_size
= probe_data_size
;
192 priv
->avif
= av_probe_input_format2(&avpd
, probe_data_size
> 0, &score
);
193 read_size
= FFMIN(2*read_size
, PROBE_BUF_SIZE
- probe_data_size
);
194 } while ((demuxer
->desc
->type
!= DEMUXER_TYPE_LAVF_PREFERRED
||
195 probe_data_size
< SMALL_MAX_PROBE_SIZE
) &&
196 score
<= AVPROBE_SCORE_MAX
/ 4 &&
197 read_size
> 0 && probe_data_size
< PROBE_BUF_SIZE
);
201 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF_check: no clue about this gibberish!\n");
204 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF_check: %s\n", priv
->avif
->long_name
);
206 return DEMUXER_TYPE_LAVF
;
209 static const char * const preferred_list
[] = {
215 "mov,mp4,m4a,3gp,3g2,mj2",
227 static int lavf_check_preferred_file(demuxer_t
*demuxer
){
228 if (lavf_check_file(demuxer
)) {
229 const char * const *p
= preferred_list
;
230 lavf_priv_t
*priv
= demuxer
->priv
;
232 if (strcmp(*p
, priv
->avif
->name
) == 0)
233 return DEMUXER_TYPE_LAVF_PREFERRED
;
240 static uint8_t char2int(char c
) {
241 if (c
>= '0' && c
<= '9') return c
- '0';
242 if (c
>= 'a' && c
<= 'f') return c
- 'a' + 10;
243 if (c
>= 'A' && c
<= 'F') return c
- 'A' + 10;
247 static void parse_cryptokey(AVFormatContext
*avfc
, const char *str
) {
248 int len
= strlen(str
) / 2;
249 uint8_t *key
= av_mallocz(len
);
253 for (i
= 0; i
< len
; i
++, str
+= 2)
254 *key
++ = (char2int(str
[0]) << 4) | char2int(str
[1]);
257 static void handle_stream(demuxer_t
*demuxer
, AVFormatContext
*avfc
, int i
) {
258 lavf_priv_t
*priv
= demuxer
->priv
;
259 AVStream
*st
= avfc
->streams
[i
];
260 AVCodecContext
*codec
= st
->codec
;
261 char *stream_type
= NULL
;
263 AVMetadataTag
*lang
= av_metadata_get(st
->metadata
, "language", NULL
, 0);
264 AVMetadataTag
*title
= av_metadata_get(st
->metadata
, "title", NULL
, 0);
265 int g
, override_tag
= av_codec_get_tag(mp_codecid_override_taglists
,
267 // For some formats (like PCM) always trust CODEC_ID_* more than codec_tag
269 codec
->codec_tag
= override_tag
;
271 switch(codec
->codec_type
){
272 case CODEC_TYPE_AUDIO
:{
274 sh_audio_t
* sh_audio
;
275 sh_audio
= new_sh_audio_aid(demuxer
, i
, priv
->audio_streams
);
278 stream_type
= "audio";
279 priv
->astreams
[priv
->audio_streams
] = i
;
280 wf
= calloc(sizeof(WAVEFORMATEX
) + codec
->extradata_size
, 1);
281 // mp4a tag is used for all mp4 files no matter what they actually contain
282 if(codec
->codec_tag
== MKTAG('m', 'p', '4', 'a'))
284 if(!codec
->codec_tag
)
285 codec
->codec_tag
= av_codec_get_tag(mp_wav_taglists
, codec
->codec_id
);
286 wf
->wFormatTag
= codec
->codec_tag
;
287 wf
->nChannels
= codec
->channels
;
288 wf
->nSamplesPerSec
= codec
->sample_rate
;
289 wf
->nAvgBytesPerSec
= codec
->bit_rate
/8;
290 wf
->nBlockAlign
= codec
->block_align
? codec
->block_align
: 1;
291 wf
->wBitsPerSample
= codec
->bits_per_coded_sample
;
292 wf
->cbSize
= codec
->extradata_size
;
293 if(codec
->extradata_size
)
294 memcpy(wf
+ 1, codec
->extradata
, codec
->extradata_size
);
296 sh_audio
->audio
.dwSampleSize
= codec
->block_align
;
297 if(codec
->frame_size
&& codec
->sample_rate
){
298 sh_audio
->audio
.dwScale
=codec
->frame_size
;
299 sh_audio
->audio
.dwRate
= codec
->sample_rate
;
301 sh_audio
->audio
.dwScale
= codec
->block_align
? codec
->block_align
*8 : 8;
302 sh_audio
->audio
.dwRate
= codec
->bit_rate
;
304 g
= av_gcd(sh_audio
->audio
.dwScale
, sh_audio
->audio
.dwRate
);
305 sh_audio
->audio
.dwScale
/= g
;
306 sh_audio
->audio
.dwRate
/= g
;
307 // 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);
308 sh_audio
->ds
= demuxer
->audio
;
309 sh_audio
->format
= codec
->codec_tag
;
310 sh_audio
->channels
= codec
->channels
;
311 sh_audio
->samplerate
= codec
->sample_rate
;
312 sh_audio
->i_bps
= codec
->bit_rate
/8;
313 switch (codec
->codec_id
) {
314 case CODEC_ID_PCM_S8
:
315 case CODEC_ID_PCM_U8
:
316 sh_audio
->samplesize
= 1;
318 case CODEC_ID_PCM_S16LE
:
319 case CODEC_ID_PCM_S16BE
:
320 case CODEC_ID_PCM_U16LE
:
321 case CODEC_ID_PCM_U16BE
:
322 sh_audio
->samplesize
= 2;
324 case CODEC_ID_PCM_ALAW
:
325 sh_audio
->format
= 0x6;
327 case CODEC_ID_PCM_MULAW
:
328 sh_audio
->format
= 0x7;
331 if (title
&& title
->value
)
332 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AID_%d_NAME=%s\n", priv
->audio_streams
, title
->value
);
333 if (lang
&& lang
->value
) {
334 sh_audio
->lang
= strdup(lang
->value
);
335 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AID_%d_LANG=%s\n", priv
->audio_streams
, sh_audio
->lang
);
337 if (st
->disposition
& AV_DISPOSITION_DEFAULT
)
338 sh_audio
->default_track
= 1;
339 if(mp_msg_test(MSGT_HEADER
,MSGL_V
) ) print_wave_header(sh_audio
->wf
, MSGL_V
);
340 // select the first audio stream
341 if (!demuxer
->audio
->sh
) {
342 demuxer
->audio
->id
= i
;
343 demuxer
->audio
->sh
= demuxer
->a_streams
[i
];
345 st
->discard
= AVDISCARD_ALL
;
346 stream_id
= priv
->audio_streams
++;
349 case CODEC_TYPE_VIDEO
:{
350 sh_video_t
* sh_video
;
351 BITMAPINFOHEADER
*bih
;
352 sh_video
=new_sh_video_vid(demuxer
, i
, priv
->video_streams
);
354 stream_type
= "video";
355 priv
->vstreams
[priv
->video_streams
] = i
;
356 bih
=calloc(sizeof(BITMAPINFOHEADER
) + codec
->extradata_size
,1);
358 if(codec
->codec_id
== CODEC_ID_RAWVIDEO
) {
359 switch (codec
->pix_fmt
) {
361 codec
->codec_tag
= MKTAG(24, 'B', 'G', 'R');
364 if(!codec
->codec_tag
)
365 codec
->codec_tag
= av_codec_get_tag(mp_bmp_taglists
, codec
->codec_id
);
366 bih
->biSize
= sizeof(BITMAPINFOHEADER
) + codec
->extradata_size
;
367 bih
->biWidth
= codec
->width
;
368 bih
->biHeight
= codec
->height
;
369 bih
->biBitCount
= codec
->bits_per_coded_sample
;
370 bih
->biSizeImage
= bih
->biWidth
* bih
->biHeight
* bih
->biBitCount
/8;
371 bih
->biCompression
= codec
->codec_tag
;
373 sh_video
->disp_w
= codec
->width
;
374 sh_video
->disp_h
= codec
->height
;
375 if (st
->time_base
.den
) { /* if container has time_base, use that */
376 sh_video
->video
.dwRate
= st
->time_base
.den
;
377 sh_video
->video
.dwScale
= st
->time_base
.num
;
379 sh_video
->video
.dwRate
= codec
->time_base
.den
;
380 sh_video
->video
.dwScale
= codec
->time_base
.num
;
382 sh_video
->fps
=av_q2d(st
->r_frame_rate
);
383 sh_video
->frametime
=1/av_q2d(st
->r_frame_rate
);
384 sh_video
->format
=bih
->biCompression
;
385 if(st
->sample_aspect_ratio
.num
)
386 sh_video
->aspect
= codec
->width
* st
->sample_aspect_ratio
.num
387 / (float)(codec
->height
* st
->sample_aspect_ratio
.den
);
389 sh_video
->aspect
=codec
->width
* codec
->sample_aspect_ratio
.num
390 / (float)(codec
->height
* codec
->sample_aspect_ratio
.den
);
391 sh_video
->i_bps
=codec
->bit_rate
/8;
392 if (title
&& title
->value
)
393 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VID_%d_NAME=%s\n", priv
->video_streams
, title
->value
);
394 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"aspect= %d*%d/(%d*%d)\n",
395 codec
->width
, codec
->sample_aspect_ratio
.num
,
396 codec
->height
, codec
->sample_aspect_ratio
.den
);
398 sh_video
->ds
= demuxer
->video
;
399 if(codec
->extradata_size
)
400 memcpy(sh_video
->bih
+ 1, codec
->extradata
, codec
->extradata_size
);
401 if( mp_msg_test(MSGT_HEADER
,MSGL_V
) ) print_video_header(sh_video
->bih
, MSGL_V
);
409 if(demuxer
->video
->id
!= i
&& demuxer
->video
->id
!= -1)
410 st
->discard
= AVDISCARD_ALL
;
412 demuxer
->video
->id
= i
;
413 demuxer
->video
->sh
= demuxer
->v_streams
[i
];
415 stream_id
= priv
->video_streams
++;
418 case CODEC_TYPE_SUBTITLE
:{
421 /* only support text subtitles for now */
422 if(codec
->codec_id
== CODEC_ID_TEXT
)
424 else if(codec
->codec_id
== CODEC_ID_MOV_TEXT
)
426 else if(codec
->codec_id
== CODEC_ID_SSA
)
428 else if(codec
->codec_id
== CODEC_ID_DVD_SUBTITLE
)
430 else if(codec
->codec_id
== CODEC_ID_DVB_TELETEXT
)
434 sh_sub
= new_sh_sub_sid(demuxer
, i
, priv
->sub_streams
);
436 stream_type
= "subtitle";
437 priv
->sstreams
[priv
->sub_streams
] = i
;
439 if (codec
->extradata_size
) {
440 sh_sub
->extradata
= malloc(codec
->extradata_size
);
441 memcpy(sh_sub
->extradata
, codec
->extradata
, codec
->extradata_size
);
442 sh_sub
->extradata_len
= codec
->extradata_size
;
444 if (title
&& title
->value
)
445 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SID_%d_NAME=%s\n", priv
->sub_streams
, title
->value
);
446 if (lang
&& lang
->value
) {
447 sh_sub
->lang
= strdup(lang
->value
);
448 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SID_%d_LANG=%s\n", priv
->sub_streams
, sh_sub
->lang
);
450 if (st
->disposition
& AV_DISPOSITION_DEFAULT
)
451 sh_sub
->default_track
= 1;
452 stream_id
= priv
->sub_streams
++;
455 case CODEC_TYPE_ATTACHMENT
:{
456 if (st
->codec
->codec_id
== CODEC_ID_TTF
)
457 demuxer_add_attachment(demuxer
, st
->filename
,
458 "application/x-truetype-font",
459 codec
->extradata
, codec
->extradata_size
);
463 st
->discard
= AVDISCARD_ALL
;
466 AVCodec
*avc
= avcodec_find_decoder(codec
->codec_id
);
467 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "[lavf] stream %d: %s (%s), -%cid %d", i
, stream_type
, avc
? avc
->name
: "unknown", *stream_type
, stream_id
);
468 if (lang
&& lang
->value
&& *stream_type
!= 'v')
469 mp_msg(MSGT_DEMUX
, MSGL_INFO
, ", -%clang %s", *stream_type
, lang
->value
);
470 if (title
&& title
->value
)
471 mp_msg(MSGT_DEMUX
, MSGL_INFO
, ", %s", title
->value
);
472 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "\n");
476 static demuxer_t
* demux_open_lavf(demuxer_t
*demuxer
){
477 AVFormatContext
*avfc
;
478 AVFormatParameters ap
;
480 AVMetadataTag
*t
= NULL
;
481 lavf_priv_t
*priv
= demuxer
->priv
;
483 char mp_filename
[256]="mp:";
485 memset(&ap
, 0, sizeof(AVFormatParameters
));
487 stream_seek(demuxer
->stream
, 0);
489 avfc
= avformat_alloc_context();
492 parse_cryptokey(avfc
, opt_cryptokey
);
493 if (user_correct_pts
!= 0)
494 avfc
->flags
|= AVFMT_FLAG_GENPTS
;
496 avfc
->flags
|= AVFMT_FLAG_IGNIDX
;
498 ap
.prealloced_context
= 1;
500 opt
= av_set_int(avfc
, "probesize", opt_probesize
);
501 if(!opt
) mp_msg(MSGT_HEADER
,MSGL_ERR
, "demux_lavf, couldn't set option probesize to %u\n", opt_probesize
);
503 if(opt_analyzeduration
) {
504 opt
= av_set_int(avfc
, "analyzeduration", opt_analyzeduration
* AV_TIME_BASE
);
505 if(!opt
) mp_msg(MSGT_HEADER
,MSGL_ERR
, "demux_lavf, couldn't set option analyzeduration to %u\n", opt_analyzeduration
);
509 if(parse_avopts(avfc
, opt_avopt
) < 0){
510 mp_msg(MSGT_HEADER
,MSGL_ERR
, "Your options /%s/ look like gibberish to me pal\n", opt_avopt
);
515 if(demuxer
->stream
->url
) {
516 if (!strncmp(demuxer
->stream
->url
, "ffmpeg://rtsp:", 14))
517 av_strlcpy(mp_filename
, demuxer
->stream
->url
+ 9, sizeof(mp_filename
));
519 av_strlcat(mp_filename
, demuxer
->stream
->url
, sizeof(mp_filename
));
521 av_strlcat(mp_filename
, "foobar.dummy", sizeof(mp_filename
));
523 priv
->pb
= av_alloc_put_byte(priv
->buffer
, BIO_BUFFER_SIZE
, 0,
524 demuxer
, mp_read
, NULL
, mp_seek
);
525 priv
->pb
->read_seek
= mp_read_seek
;
526 priv
->pb
->is_streamed
= !demuxer
->stream
->end_pos
|| (demuxer
->stream
->flags
& MP_STREAM_SEEK
) != MP_STREAM_SEEK
;
528 if(av_open_input_stream(&avfc
, priv
->pb
, mp_filename
, priv
->avif
, &ap
)<0){
529 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF_header: av_open_input_stream() failed\n");
535 if(av_find_stream_info(avfc
) < 0){
536 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF_header: av_find_stream_info() failed\n");
541 av_metadata_conv(avfc
, NULL
, avfc
->iformat
->metadata_conv
);
542 while((t
= av_metadata_get(avfc
->metadata
, "", t
, AV_METADATA_IGNORE_SUFFIX
)))
543 demux_info_add(demuxer
, t
->key
, t
->value
);
545 for(i
=0; i
< avfc
->nb_chapters
; i
++) {
546 AVChapter
*c
= avfc
->chapters
[i
];
547 uint64_t start
= av_rescale_q(c
->start
, c
->time_base
, (AVRational
){1,1000});
548 uint64_t end
= av_rescale_q(c
->end
, c
->time_base
, (AVRational
){1,1000});
549 t
= av_metadata_get(c
->metadata
, "title", NULL
, 0);
550 demuxer_add_chapter(demuxer
, t
? t
->value
: NULL
, start
, end
);
553 for(i
=0; i
<avfc
->nb_streams
; i
++)
554 handle_stream(demuxer
, avfc
, i
);
555 if(avfc
->nb_programs
) {
557 for (p
= 0; p
< avfc
->nb_programs
; p
++) {
558 AVProgram
*program
= avfc
->programs
[p
];
559 t
= av_metadata_get(program
->metadata
, "title", NULL
, 0);
560 mp_msg(MSGT_HEADER
,MSGL_INFO
,"LAVF: Program %d %s\n", program
->id
, t
? t
->value
: "");
564 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF: %d audio and %d video streams found\n",priv
->audio_streams
,priv
->video_streams
);
565 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF: build %d\n", LIBAVFORMAT_BUILD
);
566 if(!priv
->audio_streams
) demuxer
->audio
->id
=-2; // nosound
567 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio;
568 if(!priv
->video_streams
){
569 if(!priv
->audio_streams
){
570 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF: no audio or video headers found - broken file?\n");
573 demuxer
->video
->id
=-2; // audio-only
574 } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video;
579 static int demux_lavf_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
){
580 lavf_priv_t
*priv
= demux
->priv
;
585 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"demux_lavf_fill_buffer()\n");
587 demux
->filepos
=stream_tell(demux
->stream
);
589 if(av_read_frame(priv
->avfc
, &pkt
) < 0)
592 id
= pkt
.stream_index
;
594 if(id
==demux
->audio
->id
){
598 ds
->sh
=demux
->a_streams
[id
];
599 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected LAVF audio ID = %d\n",ds
->id
);
601 } else if(id
==demux
->video
->id
){
605 ds
->sh
=demux
->v_streams
[id
];
606 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected LAVF video ID = %d\n",ds
->id
);
608 } else if(id
==demux
->sub
->id
){
613 av_free_packet(&pkt
);
617 if(0/*pkt.destruct == av_destruct_packet*/){
618 //ok kids, dont try this at home :)
619 dp
=malloc(sizeof(demux_packet_t
));
627 dp
=new_demux_packet(pkt
.size
);
628 memcpy(dp
->buffer
, pkt
.data
, pkt
.size
);
629 av_free_packet(&pkt
);
632 if(pkt
.pts
!= AV_NOPTS_VALUE
){
633 dp
->pts
=pkt
.pts
* av_q2d(priv
->avfc
->streams
[id
]->time_base
);
634 priv
->last_pts
= dp
->pts
* AV_TIME_BASE
;
635 if(pkt
.convergence_duration
)
636 dp
->endpts
= dp
->pts
+ pkt
.convergence_duration
* av_q2d(priv
->avfc
->streams
[id
]->time_base
);
638 dp
->pos
=demux
->filepos
;
639 dp
->flags
= !!(pkt
.flags
&PKT_FLAG_KEY
);
640 // append packet to DS stream:
641 ds_add_packet(ds
,dp
);
645 static void demux_seek_lavf(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
, int flags
){
646 lavf_priv_t
*priv
= demuxer
->priv
;
648 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"demux_seek_lavf(%p, %f, %f, %d)\n", demuxer
, rel_seek_secs
, audio_delay
, flags
);
650 if (flags
& SEEK_ABSOLUTE
) {
651 priv
->last_pts
= priv
->avfc
->start_time
;
653 if (rel_seek_secs
< 0) avsflags
= AVSEEK_FLAG_BACKWARD
;
655 if (flags
& SEEK_FACTOR
) {
656 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
658 priv
->last_pts
+= rel_seek_secs
* priv
->avfc
->duration
;
660 priv
->last_pts
+= rel_seek_secs
* AV_TIME_BASE
;
662 if (av_seek_frame(priv
->avfc
, -1, priv
->last_pts
, avsflags
) < 0) {
663 avsflags
^= AVSEEK_FLAG_BACKWARD
;
664 av_seek_frame(priv
->avfc
, -1, priv
->last_pts
, avsflags
);
668 static int demux_lavf_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
670 lavf_priv_t
*priv
= demuxer
->priv
;
673 case DEMUXER_CTRL_CORRECT_PTS
:
674 if (!strcmp("matroska", priv
->avif
->name
) ||
675 !strcmp("mpeg", priv
->avif
->name
) ||
676 !strcmp("mpegts", priv
->avif
->name
))
677 return DEMUXER_CTRL_NOTIMPL
;
678 return DEMUXER_CTRL_OK
;
679 case DEMUXER_CTRL_GET_TIME_LENGTH
:
680 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
681 return DEMUXER_CTRL_DONTKNOW
;
683 *((double *)arg
) = (double)priv
->avfc
->duration
/ AV_TIME_BASE
;
684 return DEMUXER_CTRL_OK
;
686 case DEMUXER_CTRL_GET_PERCENT_POS
:
687 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
688 return DEMUXER_CTRL_DONTKNOW
;
690 *((int *)arg
) = (int)((priv
->last_pts
- priv
->avfc
->start_time
)*100 / priv
->avfc
->duration
);
691 return DEMUXER_CTRL_OK
;
692 case DEMUXER_CTRL_SWITCH_AUDIO
:
693 case DEMUXER_CTRL_SWITCH_VIDEO
:
695 int id
= *((int*)arg
);
698 int nstreams
, *pstreams
;
701 if(cmd
== DEMUXER_CTRL_SWITCH_VIDEO
)
704 nstreams
= priv
->video_streams
;
705 pstreams
= priv
->vstreams
;
710 nstreams
= priv
->audio_streams
;
711 pstreams
= priv
->astreams
;
713 for(i
= 0; i
< nstreams
; i
++)
715 if(pstreams
[i
] == ds
->id
) //current stream id
722 if(id
== -2) { // no sound
724 } else if(id
== -1) { // next track
725 i
= (curridx
+ 2) % (nstreams
+ 1) - 1;
729 else // select track by id
731 if (id
>= 0 && id
< nstreams
) {
737 return DEMUXER_CTRL_NOTIMPL
;
742 priv
->avfc
->streams
[ds
->id
]->discard
= AVDISCARD_ALL
;
743 *((int*)arg
) = ds
->id
= newid
;
745 priv
->avfc
->streams
[newid
]->discard
= AVDISCARD_NONE
;
746 return DEMUXER_CTRL_OK
;
749 case DEMUXER_CTRL_IDENTIFY_PROGRAM
:
751 demux_program_t
*prog
= arg
;
756 prog
->vid
= prog
->aid
= prog
->sid
= -2; //no audio and no video by default
757 if(priv
->avfc
->nb_programs
< 1)
758 return DEMUXER_CTRL_DONTKNOW
;
760 if(prog
->progid
== -1)
763 while(p
<priv
->avfc
->nb_programs
&& priv
->avfc
->programs
[p
]->id
!= priv
->cur_program
)
765 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
769 for(i
=0; i
<priv
->avfc
->nb_programs
; i
++)
770 if(priv
->avfc
->programs
[i
]->id
== prog
->progid
)
772 if(i
==priv
->avfc
->nb_programs
)
773 return DEMUXER_CTRL_DONTKNOW
;
778 program
= priv
->avfc
->programs
[p
];
779 for(i
=0; i
<program
->nb_stream_indexes
; i
++)
781 switch(priv
->avfc
->streams
[program
->stream_index
[i
]]->codec
->codec_type
)
783 case CODEC_TYPE_VIDEO
:
785 prog
->vid
= program
->stream_index
[i
];
787 case CODEC_TYPE_AUDIO
:
789 prog
->aid
= program
->stream_index
[i
];
791 case CODEC_TYPE_SUBTITLE
:
792 if(prog
->sid
== -2 && priv
->avfc
->streams
[program
->stream_index
[i
]]->codec
->codec_id
== CODEC_ID_TEXT
)
793 prog
->sid
= program
->stream_index
[i
];
797 if(prog
->progid
== -1 && prog
->vid
== -2 && prog
->aid
== -2)
799 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
801 return DEMUXER_CTRL_DONTKNOW
;
804 priv
->cur_program
= prog
->progid
= program
->id
;
805 return DEMUXER_CTRL_OK
;
808 return DEMUXER_CTRL_NOTIMPL
;
812 static void demux_close_lavf(demuxer_t
*demuxer
)
814 lavf_priv_t
* priv
= demuxer
->priv
;
818 av_freep(&priv
->avfc
->key
);
819 av_close_input_stream(priv
->avfc
);
822 free(priv
); demuxer
->priv
= NULL
;
827 const demuxer_desc_t demuxer_desc_lavf
= {
828 "libavformat demuxer",
831 "Michael Niedermayer",
832 "supports many formats, requires libavformat",
834 0, // Check after other demuxer
836 demux_lavf_fill_buffer
,
843 const demuxer_desc_t demuxer_desc_lavf_preferred
= {
844 "libavformat preferred demuxer",
847 "Michael Niedermayer",
848 "supports many formats, requires libavformat",
849 DEMUXER_TYPE_LAVF_PREFERRED
,
851 lavf_check_preferred_file
,
852 demux_lavf_fill_buffer
,