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
[] = {
216 "mov,mp4,m4a,3gp,3g2,mj2",
228 static int lavf_check_preferred_file(demuxer_t
*demuxer
){
229 if (lavf_check_file(demuxer
)) {
230 const char * const *p
= preferred_list
;
231 lavf_priv_t
*priv
= demuxer
->priv
;
233 if (strcmp(*p
, priv
->avif
->name
) == 0)
234 return DEMUXER_TYPE_LAVF_PREFERRED
;
241 static uint8_t char2int(char c
) {
242 if (c
>= '0' && c
<= '9') return c
- '0';
243 if (c
>= 'a' && c
<= 'f') return c
- 'a' + 10;
244 if (c
>= 'A' && c
<= 'F') return c
- 'A' + 10;
248 static void parse_cryptokey(AVFormatContext
*avfc
, const char *str
) {
249 int len
= strlen(str
) / 2;
250 uint8_t *key
= av_mallocz(len
);
254 for (i
= 0; i
< len
; i
++, str
+= 2)
255 *key
++ = (char2int(str
[0]) << 4) | char2int(str
[1]);
258 static void handle_stream(demuxer_t
*demuxer
, AVFormatContext
*avfc
, int i
) {
259 lavf_priv_t
*priv
= demuxer
->priv
;
260 AVStream
*st
= avfc
->streams
[i
];
261 AVCodecContext
*codec
= st
->codec
;
262 char *stream_type
= NULL
;
264 AVMetadataTag
*lang
= av_metadata_get(st
->metadata
, "language", NULL
, 0);
265 AVMetadataTag
*title
= av_metadata_get(st
->metadata
, "title", NULL
, 0);
266 int g
, override_tag
= av_codec_get_tag(mp_codecid_override_taglists
,
268 // For some formats (like PCM) always trust CODEC_ID_* more than codec_tag
270 codec
->codec_tag
= override_tag
;
272 switch(codec
->codec_type
){
273 case CODEC_TYPE_AUDIO
:{
275 sh_audio_t
* sh_audio
;
276 sh_audio
= new_sh_audio_aid(demuxer
, i
, priv
->audio_streams
);
279 stream_type
= "audio";
280 priv
->astreams
[priv
->audio_streams
] = i
;
281 wf
= calloc(sizeof(WAVEFORMATEX
) + codec
->extradata_size
, 1);
282 // mp4a tag is used for all mp4 files no matter what they actually contain
283 if(codec
->codec_tag
== MKTAG('m', 'p', '4', 'a'))
285 if(!codec
->codec_tag
)
286 codec
->codec_tag
= av_codec_get_tag(mp_wav_taglists
, codec
->codec_id
);
287 wf
->wFormatTag
= codec
->codec_tag
;
288 wf
->nChannels
= codec
->channels
;
289 wf
->nSamplesPerSec
= codec
->sample_rate
;
290 wf
->nAvgBytesPerSec
= codec
->bit_rate
/8;
291 wf
->nBlockAlign
= codec
->block_align
? codec
->block_align
: 1;
292 wf
->wBitsPerSample
= codec
->bits_per_coded_sample
;
293 wf
->cbSize
= codec
->extradata_size
;
294 if(codec
->extradata_size
)
295 memcpy(wf
+ 1, codec
->extradata
, codec
->extradata_size
);
297 sh_audio
->audio
.dwSampleSize
= codec
->block_align
;
298 if(codec
->frame_size
&& codec
->sample_rate
){
299 sh_audio
->audio
.dwScale
=codec
->frame_size
;
300 sh_audio
->audio
.dwRate
= codec
->sample_rate
;
302 sh_audio
->audio
.dwScale
= codec
->block_align
? codec
->block_align
*8 : 8;
303 sh_audio
->audio
.dwRate
= codec
->bit_rate
;
305 g
= av_gcd(sh_audio
->audio
.dwScale
, sh_audio
->audio
.dwRate
);
306 sh_audio
->audio
.dwScale
/= g
;
307 sh_audio
->audio
.dwRate
/= g
;
308 // 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);
309 sh_audio
->ds
= demuxer
->audio
;
310 sh_audio
->format
= codec
->codec_tag
;
311 sh_audio
->channels
= codec
->channels
;
312 sh_audio
->samplerate
= codec
->sample_rate
;
313 sh_audio
->i_bps
= codec
->bit_rate
/8;
314 switch (codec
->codec_id
) {
315 case CODEC_ID_PCM_S8
:
316 case CODEC_ID_PCM_U8
:
317 sh_audio
->samplesize
= 1;
319 case CODEC_ID_PCM_S16LE
:
320 case CODEC_ID_PCM_S16BE
:
321 case CODEC_ID_PCM_U16LE
:
322 case CODEC_ID_PCM_U16BE
:
323 sh_audio
->samplesize
= 2;
325 case CODEC_ID_PCM_ALAW
:
326 sh_audio
->format
= 0x6;
328 case CODEC_ID_PCM_MULAW
:
329 sh_audio
->format
= 0x7;
332 if (title
&& title
->value
)
333 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AID_%d_NAME=%s\n", priv
->audio_streams
, title
->value
);
334 if (lang
&& lang
->value
) {
335 sh_audio
->lang
= strdup(lang
->value
);
336 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AID_%d_LANG=%s\n", priv
->audio_streams
, sh_audio
->lang
);
338 if (st
->disposition
& AV_DISPOSITION_DEFAULT
)
339 sh_audio
->default_track
= 1;
340 if(mp_msg_test(MSGT_HEADER
,MSGL_V
) ) print_wave_header(sh_audio
->wf
, MSGL_V
);
341 // select the first audio stream
342 if (!demuxer
->audio
->sh
) {
343 demuxer
->audio
->id
= i
;
344 demuxer
->audio
->sh
= demuxer
->a_streams
[i
];
346 st
->discard
= AVDISCARD_ALL
;
347 stream_id
= priv
->audio_streams
++;
350 case CODEC_TYPE_VIDEO
:{
351 sh_video_t
* sh_video
;
352 BITMAPINFOHEADER
*bih
;
353 sh_video
=new_sh_video_vid(demuxer
, i
, priv
->video_streams
);
355 stream_type
= "video";
356 priv
->vstreams
[priv
->video_streams
] = i
;
357 bih
=calloc(sizeof(BITMAPINFOHEADER
) + codec
->extradata_size
,1);
359 if(codec
->codec_id
== CODEC_ID_RAWVIDEO
) {
360 switch (codec
->pix_fmt
) {
362 codec
->codec_tag
= MKTAG(24, 'B', 'G', 'R');
365 if(!codec
->codec_tag
)
366 codec
->codec_tag
= av_codec_get_tag(mp_bmp_taglists
, codec
->codec_id
);
367 bih
->biSize
= sizeof(BITMAPINFOHEADER
) + codec
->extradata_size
;
368 bih
->biWidth
= codec
->width
;
369 bih
->biHeight
= codec
->height
;
370 bih
->biBitCount
= codec
->bits_per_coded_sample
;
371 bih
->biSizeImage
= bih
->biWidth
* bih
->biHeight
* bih
->biBitCount
/8;
372 bih
->biCompression
= codec
->codec_tag
;
374 sh_video
->disp_w
= codec
->width
;
375 sh_video
->disp_h
= codec
->height
;
376 if (st
->time_base
.den
) { /* if container has time_base, use that */
377 sh_video
->video
.dwRate
= st
->time_base
.den
;
378 sh_video
->video
.dwScale
= st
->time_base
.num
;
380 sh_video
->video
.dwRate
= codec
->time_base
.den
;
381 sh_video
->video
.dwScale
= codec
->time_base
.num
;
383 sh_video
->fps
=av_q2d(st
->r_frame_rate
);
384 sh_video
->frametime
=1/av_q2d(st
->r_frame_rate
);
385 sh_video
->format
=bih
->biCompression
;
386 if(st
->sample_aspect_ratio
.num
)
387 sh_video
->aspect
= codec
->width
* st
->sample_aspect_ratio
.num
388 / (float)(codec
->height
* st
->sample_aspect_ratio
.den
);
390 sh_video
->aspect
=codec
->width
* codec
->sample_aspect_ratio
.num
391 / (float)(codec
->height
* codec
->sample_aspect_ratio
.den
);
392 sh_video
->i_bps
=codec
->bit_rate
/8;
393 if (title
&& title
->value
)
394 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VID_%d_NAME=%s\n", priv
->video_streams
, title
->value
);
395 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"aspect= %d*%d/(%d*%d)\n",
396 codec
->width
, codec
->sample_aspect_ratio
.num
,
397 codec
->height
, codec
->sample_aspect_ratio
.den
);
399 sh_video
->ds
= demuxer
->video
;
400 if(codec
->extradata_size
)
401 memcpy(sh_video
->bih
+ 1, codec
->extradata
, codec
->extradata_size
);
402 if( mp_msg_test(MSGT_HEADER
,MSGL_V
) ) print_video_header(sh_video
->bih
, MSGL_V
);
410 if(demuxer
->video
->id
!= i
&& demuxer
->video
->id
!= -1)
411 st
->discard
= AVDISCARD_ALL
;
413 demuxer
->video
->id
= i
;
414 demuxer
->video
->sh
= demuxer
->v_streams
[i
];
416 stream_id
= priv
->video_streams
++;
419 case CODEC_TYPE_SUBTITLE
:{
422 /* only support text subtitles for now */
423 if(codec
->codec_id
== CODEC_ID_TEXT
)
425 else if(codec
->codec_id
== CODEC_ID_MOV_TEXT
)
427 else if(codec
->codec_id
== CODEC_ID_SSA
)
429 else if(codec
->codec_id
== CODEC_ID_DVD_SUBTITLE
)
431 else if(codec
->codec_id
== CODEC_ID_DVB_TELETEXT
)
435 sh_sub
= new_sh_sub_sid(demuxer
, i
, priv
->sub_streams
);
437 stream_type
= "subtitle";
438 priv
->sstreams
[priv
->sub_streams
] = i
;
440 if (codec
->extradata_size
) {
441 sh_sub
->extradata
= malloc(codec
->extradata_size
);
442 memcpy(sh_sub
->extradata
, codec
->extradata
, codec
->extradata_size
);
443 sh_sub
->extradata_len
= codec
->extradata_size
;
445 if (title
&& title
->value
)
446 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SID_%d_NAME=%s\n", priv
->sub_streams
, title
->value
);
447 if (lang
&& lang
->value
) {
448 sh_sub
->lang
= strdup(lang
->value
);
449 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SID_%d_LANG=%s\n", priv
->sub_streams
, sh_sub
->lang
);
451 if (st
->disposition
& AV_DISPOSITION_DEFAULT
)
452 sh_sub
->default_track
= 1;
453 stream_id
= priv
->sub_streams
++;
456 case CODEC_TYPE_ATTACHMENT
:{
457 if (st
->codec
->codec_id
== CODEC_ID_TTF
)
458 demuxer_add_attachment(demuxer
, st
->filename
,
459 "application/x-truetype-font",
460 codec
->extradata
, codec
->extradata_size
);
464 st
->discard
= AVDISCARD_ALL
;
467 AVCodec
*avc
= avcodec_find_decoder(codec
->codec_id
);
468 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "[lavf] stream %d: %s (%s), -%cid %d", i
, stream_type
, avc
? avc
->name
: "unknown", *stream_type
, stream_id
);
469 if (lang
&& lang
->value
&& *stream_type
!= 'v')
470 mp_msg(MSGT_DEMUX
, MSGL_INFO
, ", -%clang %s", *stream_type
, lang
->value
);
471 if (title
&& title
->value
)
472 mp_msg(MSGT_DEMUX
, MSGL_INFO
, ", %s", title
->value
);
473 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "\n");
477 static demuxer_t
* demux_open_lavf(demuxer_t
*demuxer
){
478 AVFormatContext
*avfc
;
479 AVFormatParameters ap
;
481 AVMetadataTag
*t
= NULL
;
482 lavf_priv_t
*priv
= demuxer
->priv
;
484 char mp_filename
[256]="mp:";
486 memset(&ap
, 0, sizeof(AVFormatParameters
));
488 stream_seek(demuxer
->stream
, 0);
490 avfc
= avformat_alloc_context();
493 parse_cryptokey(avfc
, opt_cryptokey
);
494 if (user_correct_pts
!= 0)
495 avfc
->flags
|= AVFMT_FLAG_GENPTS
;
497 avfc
->flags
|= AVFMT_FLAG_IGNIDX
;
499 ap
.prealloced_context
= 1;
501 opt
= av_set_int(avfc
, "probesize", opt_probesize
);
502 if(!opt
) mp_msg(MSGT_HEADER
,MSGL_ERR
, "demux_lavf, couldn't set option probesize to %u\n", opt_probesize
);
504 if(opt_analyzeduration
) {
505 opt
= av_set_int(avfc
, "analyzeduration", opt_analyzeduration
* AV_TIME_BASE
);
506 if(!opt
) mp_msg(MSGT_HEADER
,MSGL_ERR
, "demux_lavf, couldn't set option analyzeduration to %u\n", opt_analyzeduration
);
510 if(parse_avopts(avfc
, opt_avopt
) < 0){
511 mp_msg(MSGT_HEADER
,MSGL_ERR
, "Your options /%s/ look like gibberish to me pal\n", opt_avopt
);
516 if(demuxer
->stream
->url
) {
517 if (!strncmp(demuxer
->stream
->url
, "ffmpeg://rtsp:", 14))
518 av_strlcpy(mp_filename
, demuxer
->stream
->url
+ 9, sizeof(mp_filename
));
520 av_strlcat(mp_filename
, demuxer
->stream
->url
, sizeof(mp_filename
));
522 av_strlcat(mp_filename
, "foobar.dummy", sizeof(mp_filename
));
524 priv
->pb
= av_alloc_put_byte(priv
->buffer
, BIO_BUFFER_SIZE
, 0,
525 demuxer
, mp_read
, NULL
, mp_seek
);
526 priv
->pb
->read_seek
= mp_read_seek
;
527 priv
->pb
->is_streamed
= !demuxer
->stream
->end_pos
|| (demuxer
->stream
->flags
& MP_STREAM_SEEK
) != MP_STREAM_SEEK
;
529 if(av_open_input_stream(&avfc
, priv
->pb
, mp_filename
, priv
->avif
, &ap
)<0){
530 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF_header: av_open_input_stream() failed\n");
536 if(av_find_stream_info(avfc
) < 0){
537 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF_header: av_find_stream_info() failed\n");
542 av_metadata_conv(avfc
, NULL
, avfc
->iformat
->metadata_conv
);
543 while((t
= av_metadata_get(avfc
->metadata
, "", t
, AV_METADATA_IGNORE_SUFFIX
)))
544 demux_info_add(demuxer
, t
->key
, t
->value
);
546 for(i
=0; i
< avfc
->nb_chapters
; i
++) {
547 AVChapter
*c
= avfc
->chapters
[i
];
548 uint64_t start
= av_rescale_q(c
->start
, c
->time_base
, (AVRational
){1,1000});
549 uint64_t end
= av_rescale_q(c
->end
, c
->time_base
, (AVRational
){1,1000});
550 t
= av_metadata_get(c
->metadata
, "title", NULL
, 0);
551 demuxer_add_chapter(demuxer
, t
? t
->value
: NULL
, start
, end
);
554 for(i
=0; i
<avfc
->nb_streams
; i
++)
555 handle_stream(demuxer
, avfc
, i
);
556 if(avfc
->nb_programs
) {
558 for (p
= 0; p
< avfc
->nb_programs
; p
++) {
559 AVProgram
*program
= avfc
->programs
[p
];
560 t
= av_metadata_get(program
->metadata
, "title", NULL
, 0);
561 mp_msg(MSGT_HEADER
,MSGL_INFO
,"LAVF: Program %d %s\n", program
->id
, t
? t
->value
: "");
565 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF: %d audio and %d video streams found\n",priv
->audio_streams
,priv
->video_streams
);
566 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF: build %d\n", LIBAVFORMAT_BUILD
);
567 if(!priv
->audio_streams
) demuxer
->audio
->id
=-2; // nosound
568 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio;
569 if(!priv
->video_streams
){
570 if(!priv
->audio_streams
){
571 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF: no audio or video headers found - broken file?\n");
574 demuxer
->video
->id
=-2; // audio-only
575 } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video;
580 static int demux_lavf_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
){
581 lavf_priv_t
*priv
= demux
->priv
;
586 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"demux_lavf_fill_buffer()\n");
588 demux
->filepos
=stream_tell(demux
->stream
);
590 if(av_read_frame(priv
->avfc
, &pkt
) < 0)
593 id
= pkt
.stream_index
;
595 if(id
==demux
->audio
->id
){
599 ds
->sh
=demux
->a_streams
[id
];
600 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected LAVF audio ID = %d\n",ds
->id
);
602 } else if(id
==demux
->video
->id
){
606 ds
->sh
=demux
->v_streams
[id
];
607 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected LAVF video ID = %d\n",ds
->id
);
609 } else if(id
==demux
->sub
->id
){
614 av_free_packet(&pkt
);
618 if(0/*pkt.destruct == av_destruct_packet*/){
619 //ok kids, dont try this at home :)
620 dp
=malloc(sizeof(demux_packet_t
));
628 dp
=new_demux_packet(pkt
.size
);
629 memcpy(dp
->buffer
, pkt
.data
, pkt
.size
);
630 av_free_packet(&pkt
);
633 if(pkt
.pts
!= AV_NOPTS_VALUE
){
634 dp
->pts
=pkt
.pts
* av_q2d(priv
->avfc
->streams
[id
]->time_base
);
635 priv
->last_pts
= dp
->pts
* AV_TIME_BASE
;
636 if(pkt
.convergence_duration
)
637 dp
->endpts
= dp
->pts
+ pkt
.convergence_duration
* av_q2d(priv
->avfc
->streams
[id
]->time_base
);
639 dp
->pos
=demux
->filepos
;
640 dp
->flags
= !!(pkt
.flags
&PKT_FLAG_KEY
);
641 // append packet to DS stream:
642 ds_add_packet(ds
,dp
);
646 static void demux_seek_lavf(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
, int flags
){
647 lavf_priv_t
*priv
= demuxer
->priv
;
649 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"demux_seek_lavf(%p, %f, %f, %d)\n", demuxer
, rel_seek_secs
, audio_delay
, flags
);
651 if (flags
& SEEK_ABSOLUTE
) {
652 priv
->last_pts
= priv
->avfc
->start_time
;
654 if (rel_seek_secs
< 0) avsflags
= AVSEEK_FLAG_BACKWARD
;
656 if (flags
& SEEK_FACTOR
) {
657 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
659 priv
->last_pts
+= rel_seek_secs
* priv
->avfc
->duration
;
661 priv
->last_pts
+= rel_seek_secs
* AV_TIME_BASE
;
663 if (av_seek_frame(priv
->avfc
, -1, priv
->last_pts
, avsflags
) < 0) {
664 avsflags
^= AVSEEK_FLAG_BACKWARD
;
665 av_seek_frame(priv
->avfc
, -1, priv
->last_pts
, avsflags
);
669 static int demux_lavf_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
671 lavf_priv_t
*priv
= demuxer
->priv
;
674 case DEMUXER_CTRL_CORRECT_PTS
:
675 return DEMUXER_CTRL_OK
;
676 case DEMUXER_CTRL_GET_TIME_LENGTH
:
677 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
678 return DEMUXER_CTRL_DONTKNOW
;
680 *((double *)arg
) = (double)priv
->avfc
->duration
/ AV_TIME_BASE
;
681 return DEMUXER_CTRL_OK
;
683 case DEMUXER_CTRL_GET_PERCENT_POS
:
684 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
685 return DEMUXER_CTRL_DONTKNOW
;
687 *((int *)arg
) = (int)((priv
->last_pts
- priv
->avfc
->start_time
)*100 / priv
->avfc
->duration
);
688 return DEMUXER_CTRL_OK
;
689 case DEMUXER_CTRL_SWITCH_AUDIO
:
690 case DEMUXER_CTRL_SWITCH_VIDEO
:
692 int id
= *((int*)arg
);
695 int nstreams
, *pstreams
;
698 if(cmd
== DEMUXER_CTRL_SWITCH_VIDEO
)
701 nstreams
= priv
->video_streams
;
702 pstreams
= priv
->vstreams
;
707 nstreams
= priv
->audio_streams
;
708 pstreams
= priv
->astreams
;
710 for(i
= 0; i
< nstreams
; i
++)
712 if(pstreams
[i
] == ds
->id
) //current stream id
719 if(id
== -2) { // no sound
721 } else if(id
== -1) { // next track
722 i
= (curridx
+ 2) % (nstreams
+ 1) - 1;
726 else // select track by id
728 if (id
>= 0 && id
< nstreams
) {
734 return DEMUXER_CTRL_NOTIMPL
;
739 priv
->avfc
->streams
[ds
->id
]->discard
= AVDISCARD_ALL
;
740 *((int*)arg
) = ds
->id
= newid
;
742 priv
->avfc
->streams
[newid
]->discard
= AVDISCARD_NONE
;
743 return DEMUXER_CTRL_OK
;
746 case DEMUXER_CTRL_IDENTIFY_PROGRAM
:
748 demux_program_t
*prog
= arg
;
753 prog
->vid
= prog
->aid
= prog
->sid
= -2; //no audio and no video by default
754 if(priv
->avfc
->nb_programs
< 1)
755 return DEMUXER_CTRL_DONTKNOW
;
757 if(prog
->progid
== -1)
760 while(p
<priv
->avfc
->nb_programs
&& priv
->avfc
->programs
[p
]->id
!= priv
->cur_program
)
762 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
766 for(i
=0; i
<priv
->avfc
->nb_programs
; i
++)
767 if(priv
->avfc
->programs
[i
]->id
== prog
->progid
)
769 if(i
==priv
->avfc
->nb_programs
)
770 return DEMUXER_CTRL_DONTKNOW
;
775 program
= priv
->avfc
->programs
[p
];
776 for(i
=0; i
<program
->nb_stream_indexes
; i
++)
778 switch(priv
->avfc
->streams
[program
->stream_index
[i
]]->codec
->codec_type
)
780 case CODEC_TYPE_VIDEO
:
782 prog
->vid
= program
->stream_index
[i
];
784 case CODEC_TYPE_AUDIO
:
786 prog
->aid
= program
->stream_index
[i
];
788 case CODEC_TYPE_SUBTITLE
:
789 if(prog
->sid
== -2 && priv
->avfc
->streams
[program
->stream_index
[i
]]->codec
->codec_id
== CODEC_ID_TEXT
)
790 prog
->sid
= program
->stream_index
[i
];
794 if(prog
->progid
== -1 && prog
->vid
== -2 && prog
->aid
== -2)
796 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
798 return DEMUXER_CTRL_DONTKNOW
;
801 priv
->cur_program
= prog
->progid
= program
->id
;
802 return DEMUXER_CTRL_OK
;
805 return DEMUXER_CTRL_NOTIMPL
;
809 static void demux_close_lavf(demuxer_t
*demuxer
)
811 lavf_priv_t
* priv
= demuxer
->priv
;
815 av_freep(&priv
->avfc
->key
);
816 av_close_input_stream(priv
->avfc
);
819 free(priv
); demuxer
->priv
= NULL
;
824 const demuxer_desc_t demuxer_desc_lavf
= {
825 "libavformat demuxer",
828 "Michael Niedermayer",
829 "supports many formats, requires libavformat",
831 0, // Check after other demuxer
833 demux_lavf_fill_buffer
,
840 const demuxer_desc_t demuxer_desc_lavf_preferred
= {
841 "libavformat preferred demuxer",
844 "Michael Niedermayer",
845 "supports many formats, requires libavformat",
846 DEMUXER_TYPE_LAVF_PREFERRED
,
848 lavf_check_preferred_file
,
849 demux_lavf_fill_buffer
,