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"
35 #include "libvo/sub.h"
37 #include "libavformat/avformat.h"
38 #include "libavformat/avio.h"
39 #include "libavutil/avutil.h"
40 #include "libavcodec/opt.h"
42 #include "mp_taglists.h"
44 #define PROBE_BUF_SIZE 2048
46 extern char *audio_lang
;
47 extern char *dvdsub_lang
;
49 static unsigned int opt_probesize
= 0;
50 static unsigned int opt_analyzeduration
= 0;
51 static char *opt_format
;
52 static char *opt_cryptokey
;
54 static char *opt_avopt
= NULL
;
56 const m_option_t lavfdopts_conf
[] = {
57 {"probesize", &(opt_probesize
), CONF_TYPE_INT
, CONF_RANGE
, 32, INT_MAX
, NULL
},
58 {"format", &(opt_format
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
59 {"analyzeduration", &(opt_analyzeduration
), CONF_TYPE_INT
, CONF_RANGE
, 0, INT_MAX
, NULL
},
60 {"cryptokey", &(opt_cryptokey
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
61 {"o", &opt_avopt
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
62 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
65 #define BIO_BUFFER_SIZE 32768
67 typedef struct lavf_priv_t
{
69 AVFormatContext
*avfc
;
71 uint8_t buffer
[BIO_BUFFER_SIZE
];
76 int astreams
[MAX_A_STREAMS
];
77 int vstreams
[MAX_V_STREAMS
];
78 int sstreams
[MAX_S_STREAMS
];
82 void print_wave_header(WAVEFORMATEX
*h
, int verbose_level
);
83 void print_video_header(BITMAPINFOHEADER
*h
, int verbose_level
);
85 static int mp_read(void *opaque
, uint8_t *buf
, int size
) {
86 stream_t
*stream
= opaque
;
89 if(stream_eof(stream
)) //needed?
91 ret
=stream_read(stream
, buf
, size
);
93 mp_msg(MSGT_HEADER
,MSGL_DBG2
,"%d=mp_read(%p, %p, %d), eof:%d\n", ret
, stream
, buf
, size
, stream
->eof
);
97 static int64_t mp_seek(void *opaque
, int64_t pos
, int whence
) {
98 stream_t
*stream
= opaque
;
100 mp_msg(MSGT_HEADER
,MSGL_DBG2
,"mp_seek(%p, %d, %d)\n", stream
, (int)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 if(pos
<stream
->end_pos
&& stream
->eof
)
115 stream_reset(stream
);
116 current_pos
= stream_tell(stream
);
117 if(stream_seek(stream
, pos
)==0) {
118 stream_reset(stream
);
119 stream_seek(stream
, current_pos
);
123 return pos
- stream
->start_pos
;
126 static void list_formats(void) {
128 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "Available lavf input formats:\n");
129 for (fmt
= first_iformat
; fmt
; fmt
= fmt
->next
)
130 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "%15s : %s\n", fmt
->name
, fmt
->long_name
);
133 static int lavf_check_file(demuxer_t
*demuxer
){
135 uint8_t buf
[PROBE_BUF_SIZE
];
139 demuxer
->priv
=calloc(sizeof(lavf_priv_t
),1);
144 if(stream_read(demuxer
->stream
, buf
, PROBE_BUF_SIZE
)!=PROBE_BUF_SIZE
)
146 avpd
.filename
= demuxer
->stream
->url
;
148 avpd
.buf_size
= PROBE_BUF_SIZE
;
151 if (strcmp(opt_format
, "help") == 0) {
155 priv
->avif
= av_find_input_format(opt_format
);
157 mp_msg(MSGT_DEMUX
,MSGL_FATAL
,"Unknown lavf format %s\n", opt_format
);
160 mp_msg(MSGT_DEMUX
,MSGL_INFO
,"Forced lavf %s demuxer\n", priv
->avif
->long_name
);
161 return DEMUXER_TYPE_LAVF
;
163 priv
->avif
= av_probe_input_format(&avpd
, 1);
165 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF_check: no clue about this gibberish!\n");
168 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF_check: %s\n", priv
->avif
->long_name
);
170 return DEMUXER_TYPE_LAVF
;
173 static const char *preferred_list
[] = {
182 "mov,mp4,m4a,3gp,3g2,mj2",
188 static int lavf_check_preferred_file(demuxer_t
*demuxer
){
189 if (lavf_check_file(demuxer
)) {
190 char **p
= preferred_list
;
191 lavf_priv_t
*priv
= demuxer
->priv
;
193 if (strcmp(*p
, priv
->avif
->name
) == 0)
194 return DEMUXER_TYPE_LAVF_PREFERRED
;
201 static uint8_t char2int(char c
) {
202 if (c
>= '0' && c
<= '9') return c
- '0';
203 if (c
>= 'a' && c
<= 'f') return c
- 'a' + 10;
204 if (c
>= 'A' && c
<= 'F') return c
- 'A' + 10;
208 static void parse_cryptokey(AVFormatContext
*avfc
, const char *str
) {
209 int len
= strlen(str
) / 2;
210 uint8_t *key
= av_mallocz(len
);
214 for (i
= 0; i
< len
; i
++, str
+= 2)
215 *key
++ = (char2int(str
[0]) << 4) | char2int(str
[1]);
218 static void handle_stream(demuxer_t
*demuxer
, AVFormatContext
*avfc
, int i
) {
219 lavf_priv_t
*priv
= demuxer
->priv
;
220 AVStream
*st
= avfc
->streams
[i
];
221 AVCodecContext
*codec
= st
->codec
;
224 switch(codec
->codec_type
){
225 case CODEC_TYPE_AUDIO
:{
227 WAVEFORMATEX
*wf
= calloc(sizeof(WAVEFORMATEX
) + codec
->extradata_size
, 1);
228 sh_audio_t
* sh_audio
;
229 if(priv
->audio_streams
>= MAX_A_STREAMS
)
231 sh_audio
=new_sh_audio(demuxer
, i
);
232 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_AudioID
, "lavf", i
);
235 priv
->astreams
[priv
->audio_streams
] = i
;
236 priv
->audio_streams
++;
237 // For some formats (like PCM) always trust CODEC_ID_* more than codec_tag
238 override_tag
= av_codec_get_tag(mp_wav_override_taglists
, codec
->codec_id
);
240 codec
->codec_tag
= override_tag
;
241 // mp4a tag is used for all mp4 files no matter what they actually contain
242 if(codec
->codec_tag
== MKTAG('m', 'p', '4', 'a'))
244 if(codec
->codec_id
== CODEC_ID_ADPCM_IMA_AMV
)
245 codec
->codec_tag
= MKTAG('A','M','V','A');
246 if(!codec
->codec_tag
)
247 codec
->codec_tag
= av_codec_get_tag(mp_wav_taglists
, codec
->codec_id
);
248 wf
->wFormatTag
= codec
->codec_tag
;
249 wf
->nChannels
= codec
->channels
;
250 wf
->nSamplesPerSec
= codec
->sample_rate
;
251 wf
->nAvgBytesPerSec
= codec
->bit_rate
/8;
252 wf
->nBlockAlign
= codec
->block_align
? codec
->block_align
: 1;
253 wf
->wBitsPerSample
= codec
->bits_per_coded_sample
;
254 wf
->cbSize
= codec
->extradata_size
;
255 if(codec
->extradata_size
)
256 memcpy(wf
+ 1, codec
->extradata
, codec
->extradata_size
);
258 sh_audio
->audio
.dwSampleSize
= codec
->block_align
;
259 if(codec
->frame_size
&& codec
->sample_rate
){
260 sh_audio
->audio
.dwScale
=codec
->frame_size
;
261 sh_audio
->audio
.dwRate
= codec
->sample_rate
;
263 sh_audio
->audio
.dwScale
= codec
->block_align
? codec
->block_align
*8 : 8;
264 sh_audio
->audio
.dwRate
= codec
->bit_rate
;
266 g
= av_gcd(sh_audio
->audio
.dwScale
, sh_audio
->audio
.dwRate
);
267 sh_audio
->audio
.dwScale
/= g
;
268 sh_audio
->audio
.dwRate
/= g
;
269 // 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);
270 sh_audio
->ds
= demuxer
->audio
;
271 sh_audio
->format
= codec
->codec_tag
;
272 sh_audio
->channels
= codec
->channels
;
273 sh_audio
->samplerate
= codec
->sample_rate
;
274 sh_audio
->i_bps
= codec
->bit_rate
/8;
275 switch (codec
->codec_id
) {
276 case CODEC_ID_PCM_S8
:
277 case CODEC_ID_PCM_U8
:
278 sh_audio
->samplesize
= 1;
280 case CODEC_ID_PCM_S16LE
:
281 case CODEC_ID_PCM_S16BE
:
282 case CODEC_ID_PCM_U16LE
:
283 case CODEC_ID_PCM_U16BE
:
284 sh_audio
->samplesize
= 2;
286 case CODEC_ID_PCM_ALAW
:
287 sh_audio
->format
= 0x6;
289 case CODEC_ID_PCM_MULAW
:
290 sh_audio
->format
= 0x7;
294 sh_audio
->lang
= strdup(st
->language
);
295 if (st
->disposition
& AV_DISPOSITION_DEFAULT
)
296 sh_audio
->default_track
= 1;
297 if(mp_msg_test(MSGT_HEADER
,MSGL_V
) ) print_wave_header(sh_audio
->wf
, MSGL_V
);
298 // select the first audio stream
299 if (!demuxer
->audio
->sh
) {
300 demuxer
->audio
->id
= i
;
301 demuxer
->audio
->sh
= demuxer
->a_streams
[i
];
303 st
->discard
= AVDISCARD_ALL
;
306 case CODEC_TYPE_VIDEO
:{
307 sh_video_t
* sh_video
;
308 BITMAPINFOHEADER
*bih
;
309 if(priv
->video_streams
>= MAX_V_STREAMS
)
311 sh_video
=new_sh_video(demuxer
, i
);
312 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_VideoID
, "lavf", i
);
314 priv
->vstreams
[priv
->video_streams
] = i
;
315 priv
->video_streams
++;
316 bih
=calloc(sizeof(BITMAPINFOHEADER
) + codec
->extradata_size
,1);
318 if(codec
->codec_id
== CODEC_ID_RAWVIDEO
) {
319 switch (codec
->pix_fmt
) {
321 codec
->codec_tag
= MKTAG(24, 'B', 'G', 'R');
324 if(!codec
->codec_tag
)
325 codec
->codec_tag
= av_codec_get_tag(mp_bmp_taglists
, codec
->codec_id
);
326 bih
->biSize
= sizeof(BITMAPINFOHEADER
) + codec
->extradata_size
;
327 bih
->biWidth
= codec
->width
;
328 bih
->biHeight
= codec
->height
;
329 bih
->biBitCount
= codec
->bits_per_coded_sample
;
330 bih
->biSizeImage
= bih
->biWidth
* bih
->biHeight
* bih
->biBitCount
/8;
331 bih
->biCompression
= codec
->codec_tag
;
333 sh_video
->disp_w
= codec
->width
;
334 sh_video
->disp_h
= codec
->height
;
335 if (st
->time_base
.den
) { /* if container has time_base, use that */
336 sh_video
->video
.dwRate
= st
->time_base
.den
;
337 sh_video
->video
.dwScale
= st
->time_base
.num
;
339 sh_video
->video
.dwRate
= codec
->time_base
.den
;
340 sh_video
->video
.dwScale
= codec
->time_base
.num
;
342 sh_video
->fps
=av_q2d(st
->r_frame_rate
);
343 sh_video
->frametime
=1/av_q2d(st
->r_frame_rate
);
344 sh_video
->format
=bih
->biCompression
;
345 if(st
->sample_aspect_ratio
.num
)
346 sh_video
->aspect
= codec
->width
* st
->sample_aspect_ratio
.num
347 / (float)(codec
->height
* st
->sample_aspect_ratio
.den
);
349 sh_video
->aspect
=codec
->width
* codec
->sample_aspect_ratio
.num
350 / (float)(codec
->height
* codec
->sample_aspect_ratio
.den
);
351 sh_video
->i_bps
=codec
->bit_rate
/8;
352 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"aspect= %d*%d/(%d*%d)\n",
353 codec
->width
, codec
->sample_aspect_ratio
.num
,
354 codec
->height
, codec
->sample_aspect_ratio
.den
);
356 sh_video
->ds
= demuxer
->video
;
357 if(codec
->extradata_size
)
358 memcpy(sh_video
->bih
+ 1, codec
->extradata
, codec
->extradata_size
);
359 if( mp_msg_test(MSGT_HEADER
,MSGL_V
) ) print_video_header(sh_video
->bih
, MSGL_V
);
367 if(demuxer
->video
->id
!= i
&& demuxer
->video
->id
!= -1)
368 st
->discard
= AVDISCARD_ALL
;
370 demuxer
->video
->id
= i
;
371 demuxer
->video
->sh
= demuxer
->v_streams
[i
];
375 case CODEC_TYPE_SUBTITLE
:{
378 if(priv
->sub_streams
>= MAX_S_STREAMS
)
380 /* only support text subtitles for now */
381 if(codec
->codec_id
== CODEC_ID_TEXT
)
383 else if(codec
->codec_id
== CODEC_ID_MOV_TEXT
)
385 else if(codec
->codec_id
== CODEC_ID_SSA
)
387 else if(codec
->codec_id
== CODEC_ID_DVD_SUBTITLE
)
391 sh_sub
= new_sh_sub_sid(demuxer
, i
, priv
->sub_streams
);
392 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_SubtitleID
, "lavf", priv
->sub_streams
);
394 priv
->sstreams
[priv
->sub_streams
] = i
;
396 if (codec
->extradata_size
) {
397 sh_sub
->extradata
= malloc(codec
->extradata_size
);
398 memcpy(sh_sub
->extradata
, codec
->extradata
, codec
->extradata_size
);
399 sh_sub
->extradata_len
= codec
->extradata_size
;
402 sh_sub
->lang
= strdup(st
->language
);
403 if (st
->disposition
& AV_DISPOSITION_DEFAULT
)
404 sh_sub
->default_track
= 1;
408 case CODEC_TYPE_ATTACHMENT
:{
409 if (st
->codec
->codec_id
== CODEC_ID_TTF
)
410 demuxer_add_attachment(demuxer
, st
->filename
,
411 "application/x-truetype-font",
412 codec
->extradata
, codec
->extradata_size
);
416 st
->discard
= AVDISCARD_ALL
;
420 static demuxer_t
* demux_open_lavf(demuxer_t
*demuxer
){
421 AVFormatContext
*avfc
;
422 AVFormatParameters ap
;
424 lavf_priv_t
*priv
= demuxer
->priv
;
426 char mp_filename
[256]="mp:";
428 memset(&ap
, 0, sizeof(AVFormatParameters
));
430 stream_seek(demuxer
->stream
, 0);
432 avfc
= av_alloc_format_context();
435 parse_cryptokey(avfc
, opt_cryptokey
);
436 if (user_correct_pts
!= 0)
437 avfc
->flags
|= AVFMT_FLAG_GENPTS
;
439 avfc
->flags
|= AVFMT_FLAG_IGNIDX
;
441 ap
.prealloced_context
= 1;
443 opt
= av_set_int(avfc
, "probesize", opt_probesize
);
444 if(!opt
) mp_msg(MSGT_HEADER
,MSGL_ERR
, "demux_lavf, couldn't set option probesize to %u\n", opt_probesize
);
446 if(opt_analyzeduration
) {
447 opt
= av_set_int(avfc
, "analyzeduration", opt_analyzeduration
* AV_TIME_BASE
);
448 if(!opt
) mp_msg(MSGT_HEADER
,MSGL_ERR
, "demux_lavf, couldn't set option analyzeduration to %u\n", opt_analyzeduration
);
452 if(parse_avopts(avfc
, opt_avopt
) < 0){
453 mp_msg(MSGT_HEADER
,MSGL_ERR
, "Your options /%s/ look like gibberish to me pal\n", opt_avopt
);
458 if(demuxer
->stream
->url
)
459 strncpy(mp_filename
+ 3, demuxer
->stream
->url
, sizeof(mp_filename
)-3);
461 strncpy(mp_filename
+ 3, "foobar.dummy", sizeof(mp_filename
)-3);
463 priv
->pb
= av_alloc_put_byte(priv
->buffer
, BIO_BUFFER_SIZE
, 0,
464 demuxer
->stream
, mp_read
, NULL
, mp_seek
);
465 priv
->pb
->is_streamed
= !demuxer
->stream
->end_pos
|| (demuxer
->stream
->flags
& STREAM_SEEK
) != STREAM_SEEK
;
467 if(av_open_input_stream(&avfc
, priv
->pb
, mp_filename
, priv
->avif
, &ap
)<0){
468 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF_header: av_open_input_stream() failed\n");
474 if(av_find_stream_info(avfc
) < 0){
475 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF_header: av_find_stream_info() failed\n");
479 if(avfc
->title
[0]) demux_info_add(demuxer
, "name" , avfc
->title
);
480 if(avfc
->author
[0]) demux_info_add(demuxer
, "author" , avfc
->author
);
481 if(avfc
->copyright
[0]) demux_info_add(demuxer
, "copyright", avfc
->copyright
);
482 if(avfc
->comment
[0]) demux_info_add(demuxer
, "comments" , avfc
->comment
);
483 if(avfc
->album
[0]) demux_info_add(demuxer
, "album" , avfc
->album
);
484 // if(avfc->year ) demux_info_add(demuxer, "year" , avfc->year );
485 // if(avfc->track ) demux_info_add(demuxer, "track" , avfc->track );
486 if(avfc
->genre
[0]) demux_info_add(demuxer
, "genre" , avfc
->genre
);
488 for(i
=0; i
< avfc
->nb_chapters
; i
++) {
489 AVChapter
*c
= avfc
->chapters
[i
];
490 uint64_t start
= av_rescale_q(c
->start
, c
->time_base
, (AVRational
){1,1000});
491 uint64_t end
= av_rescale_q(c
->end
, c
->time_base
, (AVRational
){1,1000});
492 demuxer_add_chapter(demuxer
, c
->title
, start
, end
);
495 if(avfc
->nb_programs
) {
496 int p
, start
=0, found
=0;
499 for(p
=0; p
<avfc
->nb_programs
; p
++) {
500 if(avfc
->programs
[p
]->id
== ts_prog
) {
507 mp_msg(MSGT_HEADER
,MSGL_ERR
,"DEMUX_LAVF: program %d doesn't seem to be present\n", ts_prog
);
513 AVProgram
*program
= avfc
->programs
[p
];
514 mp_msg(MSGT_HEADER
,MSGL_INFO
,"LAVF: Program %d %s\n", program
->id
, (program
->name
? program
->name
: ""));
515 for(i
=0; i
<program
->nb_stream_indexes
; i
++)
516 handle_stream(demuxer
, avfc
, program
->stream_index
[i
]);
517 if(!priv
->cur_program
&& (demuxer
->video
->sh
|| demuxer
->audio
->sh
))
518 priv
->cur_program
= program
->id
;
519 p
= (p
+ 1) % avfc
->nb_programs
;
522 for(i
=0; i
<avfc
->nb_streams
; i
++)
523 handle_stream(demuxer
, avfc
, i
);
525 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF: %d audio and %d video streams found\n",priv
->audio_streams
,priv
->video_streams
);
526 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF: build %d\n", LIBAVFORMAT_BUILD
);
527 if(!priv
->audio_streams
) demuxer
->audio
->id
=-2; // nosound
528 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio;
529 if(!priv
->video_streams
){
530 if(!priv
->audio_streams
){
531 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF: no audio or video headers found - broken file?\n");
534 demuxer
->video
->id
=-2; // audio-only
535 } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video;
540 static int demux_lavf_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
){
541 lavf_priv_t
*priv
= demux
->priv
;
546 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"demux_lavf_fill_buffer()\n");
548 demux
->filepos
=stream_tell(demux
->stream
);
550 if(av_read_frame(priv
->avfc
, &pkt
) < 0)
553 id
= pkt
.stream_index
;
555 if(id
==demux
->audio
->id
){
559 ds
->sh
=demux
->a_streams
[id
];
560 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected LAVF audio ID = %d\n",ds
->id
);
562 } else if(id
==demux
->video
->id
){
566 ds
->sh
=demux
->v_streams
[id
];
567 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected LAVF video ID = %d\n",ds
->id
);
569 } else if(id
==demux
->sub
->id
){
574 av_free_packet(&pkt
);
578 if(0/*pkt.destruct == av_destruct_packet*/){
579 //ok kids, dont try this at home :)
580 dp
=malloc(sizeof(demux_packet_t
));
588 dp
=new_demux_packet(pkt
.size
);
589 memcpy(dp
->buffer
, pkt
.data
, pkt
.size
);
590 av_free_packet(&pkt
);
593 if(pkt
.pts
!= AV_NOPTS_VALUE
){
594 dp
->pts
=pkt
.pts
* av_q2d(priv
->avfc
->streams
[id
]->time_base
);
595 priv
->last_pts
= dp
->pts
* AV_TIME_BASE
;
596 if(pkt
.convergence_duration
)
597 dp
->endpts
= dp
->pts
+ pkt
.convergence_duration
* av_q2d(priv
->avfc
->streams
[id
]->time_base
);
599 dp
->pos
=demux
->filepos
;
600 dp
->flags
= !!(pkt
.flags
&PKT_FLAG_KEY
);
601 // append packet to DS stream:
602 ds_add_packet(ds
,dp
);
606 static void demux_seek_lavf(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
, int flags
){
607 lavf_priv_t
*priv
= demuxer
->priv
;
609 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"demux_seek_lavf(%p, %f, %f, %d)\n", demuxer
, rel_seek_secs
, audio_delay
, flags
);
611 if (flags
& SEEK_ABSOLUTE
) {
612 priv
->last_pts
= priv
->avfc
->start_time
;
614 if (rel_seek_secs
< 0) avsflags
= AVSEEK_FLAG_BACKWARD
;
616 if (flags
& SEEK_FACTOR
) {
617 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
619 priv
->last_pts
+= rel_seek_secs
* priv
->avfc
->duration
;
621 priv
->last_pts
+= rel_seek_secs
* AV_TIME_BASE
;
623 av_seek_frame(priv
->avfc
, -1, priv
->last_pts
, avsflags
);
626 static int demux_lavf_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
628 lavf_priv_t
*priv
= demuxer
->priv
;
631 case DEMUXER_CTRL_CORRECT_PTS
:
632 return DEMUXER_CTRL_OK
;
633 case DEMUXER_CTRL_GET_TIME_LENGTH
:
634 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
635 return DEMUXER_CTRL_DONTKNOW
;
637 *((double *)arg
) = (double)priv
->avfc
->duration
/ AV_TIME_BASE
;
638 return DEMUXER_CTRL_OK
;
640 case DEMUXER_CTRL_GET_PERCENT_POS
:
641 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
642 return DEMUXER_CTRL_DONTKNOW
;
644 *((int *)arg
) = (int)((priv
->last_pts
- priv
->avfc
->start_time
)*100 / priv
->avfc
->duration
);
645 return DEMUXER_CTRL_OK
;
646 case DEMUXER_CTRL_SWITCH_AUDIO
:
647 case DEMUXER_CTRL_SWITCH_VIDEO
:
649 int id
= *((int*)arg
);
652 int nstreams
, *pstreams
;
655 if(cmd
== DEMUXER_CTRL_SWITCH_VIDEO
)
658 nstreams
= priv
->video_streams
;
659 pstreams
= priv
->vstreams
;
664 nstreams
= priv
->audio_streams
;
665 pstreams
= priv
->astreams
;
667 for(i
= 0; i
< nstreams
; i
++)
669 if(pstreams
[i
] == ds
->id
) //current stream id
676 if(id
== -2) { // no sound
678 } else if(id
== -1) { // next track
679 i
= (curridx
+ 2) % (nstreams
+ 1) - 1;
683 else // select track by id
685 for(i
= 0; i
< nstreams
; i
++)
687 if(pstreams
[i
] == id
)
695 return DEMUXER_CTRL_NOTIMPL
;
700 priv
->avfc
->streams
[ds
->id
]->discard
= AVDISCARD_ALL
;
701 *((int*)arg
) = ds
->id
= newid
;
703 priv
->avfc
->streams
[newid
]->discard
= AVDISCARD_NONE
;
704 return DEMUXER_CTRL_OK
;
707 case DEMUXER_CTRL_IDENTIFY_PROGRAM
:
709 demux_program_t
*prog
= arg
;
713 if(priv
->avfc
->nb_programs
< 2)
714 return DEMUXER_CTRL_NOTIMPL
;
716 if(prog
->progid
== -1)
719 while(p
<priv
->avfc
->nb_programs
&& priv
->avfc
->programs
[p
]->id
!= priv
->cur_program
)
721 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
725 for(i
=0; i
<priv
->avfc
->nb_programs
; i
++)
726 if(priv
->avfc
->programs
[i
]->id
== prog
->progid
)
728 if(i
==priv
->avfc
->nb_programs
)
729 return DEMUXER_CTRL_NOTIMPL
;
732 prog
->vid
= prog
->aid
= prog
->sid
= -2; //no audio and no video by default
734 program
= priv
->avfc
->programs
[p
];
735 for(i
=0; i
<program
->nb_stream_indexes
; i
++)
737 switch(priv
->avfc
->streams
[program
->stream_index
[i
]]->codec
->codec_type
)
739 case CODEC_TYPE_VIDEO
:
741 prog
->vid
= program
->stream_index
[i
];
743 case CODEC_TYPE_AUDIO
:
745 prog
->aid
= program
->stream_index
[i
];
747 case CODEC_TYPE_SUBTITLE
:
748 if(prog
->sid
== -2 && priv
->avfc
->streams
[program
->stream_index
[i
]]->codec
->codec_id
== CODEC_ID_TEXT
)
749 prog
->sid
= program
->stream_index
[i
];
753 if(prog
->progid
== -1 && prog
->vid
== -2 && prog
->aid
== -2)
755 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
758 priv
->cur_program
= prog
->progid
= program
->id
;
759 return DEMUXER_CTRL_OK
;
762 return DEMUXER_CTRL_NOTIMPL
;
766 static void demux_close_lavf(demuxer_t
*demuxer
)
768 lavf_priv_t
* priv
= demuxer
->priv
;
772 av_freep(&priv
->avfc
->key
);
773 av_close_input_stream(priv
->avfc
);
776 free(priv
); demuxer
->priv
= NULL
;
781 const demuxer_desc_t demuxer_desc_lavf
= {
782 "libavformat demuxer",
785 "Michael Niedermayer",
786 "supports many formats, requires libavformat",
788 0, // Check after other demuxer
790 demux_lavf_fill_buffer
,
797 const demuxer_desc_t demuxer_desc_lavf_preferred
= {
798 "libavformat preferred demuxer",
801 "Michael Niedermayer",
802 "supports many formats, requires libavformat",
803 DEMUXER_TYPE_LAVF_PREFERRED
,
805 lavf_check_preferred_file
,
806 demux_lavf_fill_buffer
,