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 (32*1024)
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);
145 if (strcmp(opt_format
, "help") == 0) {
149 priv
->avif
= av_find_input_format(opt_format
);
151 mp_msg(MSGT_DEMUX
,MSGL_FATAL
,"Unknown lavf format %s\n", opt_format
);
154 mp_msg(MSGT_DEMUX
,MSGL_INFO
,"Forced lavf %s demuxer\n", priv
->avif
->long_name
);
155 return DEMUXER_TYPE_LAVF
;
158 if(stream_read(demuxer
->stream
, buf
, PROBE_BUF_SIZE
)!=PROBE_BUF_SIZE
)
160 avpd
.filename
= demuxer
->stream
->url
;
162 avpd
.buf_size
= PROBE_BUF_SIZE
;
164 priv
->avif
= av_probe_input_format(&avpd
, 1);
166 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF_check: no clue about this gibberish!\n");
169 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF_check: %s\n", priv
->avif
->long_name
);
171 return DEMUXER_TYPE_LAVF
;
174 static const char *preferred_list
[] = {
183 "mov,mp4,m4a,3gp,3g2,mj2",
189 static int lavf_check_preferred_file(demuxer_t
*demuxer
){
190 if (lavf_check_file(demuxer
)) {
191 char **p
= preferred_list
;
192 lavf_priv_t
*priv
= demuxer
->priv
;
194 if (strcmp(*p
, priv
->avif
->name
) == 0)
195 return DEMUXER_TYPE_LAVF_PREFERRED
;
202 static uint8_t char2int(char c
) {
203 if (c
>= '0' && c
<= '9') return c
- '0';
204 if (c
>= 'a' && c
<= 'f') return c
- 'a' + 10;
205 if (c
>= 'A' && c
<= 'F') return c
- 'A' + 10;
209 static void parse_cryptokey(AVFormatContext
*avfc
, const char *str
) {
210 int len
= strlen(str
) / 2;
211 uint8_t *key
= av_mallocz(len
);
215 for (i
= 0; i
< len
; i
++, str
+= 2)
216 *key
++ = (char2int(str
[0]) << 4) | char2int(str
[1]);
219 static void handle_stream(demuxer_t
*demuxer
, AVFormatContext
*avfc
, int i
) {
220 lavf_priv_t
*priv
= demuxer
->priv
;
221 AVStream
*st
= avfc
->streams
[i
];
222 AVCodecContext
*codec
= st
->codec
;
225 switch(codec
->codec_type
){
226 case CODEC_TYPE_AUDIO
:{
228 WAVEFORMATEX
*wf
= calloc(sizeof(WAVEFORMATEX
) + codec
->extradata_size
, 1);
229 sh_audio_t
* sh_audio
;
230 if(priv
->audio_streams
>= MAX_A_STREAMS
)
232 sh_audio
=new_sh_audio(demuxer
, i
);
233 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_AudioID
, "lavf", i
);
236 priv
->astreams
[priv
->audio_streams
] = i
;
237 priv
->audio_streams
++;
238 // For some formats (like PCM) always trust CODEC_ID_* more than codec_tag
239 override_tag
= av_codec_get_tag(mp_wav_override_taglists
, codec
->codec_id
);
241 codec
->codec_tag
= override_tag
;
242 // mp4a tag is used for all mp4 files no matter what they actually contain
243 if(codec
->codec_tag
== MKTAG('m', 'p', '4', 'a'))
245 if(codec
->codec_id
== CODEC_ID_ADPCM_IMA_AMV
)
246 codec
->codec_tag
= MKTAG('A','M','V','A');
247 if(!codec
->codec_tag
)
248 codec
->codec_tag
= av_codec_get_tag(mp_wav_taglists
, codec
->codec_id
);
249 wf
->wFormatTag
= codec
->codec_tag
;
250 wf
->nChannels
= codec
->channels
;
251 wf
->nSamplesPerSec
= codec
->sample_rate
;
252 wf
->nAvgBytesPerSec
= codec
->bit_rate
/8;
253 wf
->nBlockAlign
= codec
->block_align
? codec
->block_align
: 1;
254 wf
->wBitsPerSample
= codec
->bits_per_coded_sample
;
255 wf
->cbSize
= codec
->extradata_size
;
256 if(codec
->extradata_size
)
257 memcpy(wf
+ 1, codec
->extradata
, codec
->extradata_size
);
259 sh_audio
->audio
.dwSampleSize
= codec
->block_align
;
260 if(codec
->frame_size
&& codec
->sample_rate
){
261 sh_audio
->audio
.dwScale
=codec
->frame_size
;
262 sh_audio
->audio
.dwRate
= codec
->sample_rate
;
264 sh_audio
->audio
.dwScale
= codec
->block_align
? codec
->block_align
*8 : 8;
265 sh_audio
->audio
.dwRate
= codec
->bit_rate
;
267 g
= av_gcd(sh_audio
->audio
.dwScale
, sh_audio
->audio
.dwRate
);
268 sh_audio
->audio
.dwScale
/= g
;
269 sh_audio
->audio
.dwRate
/= g
;
270 // 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);
271 sh_audio
->ds
= demuxer
->audio
;
272 sh_audio
->format
= codec
->codec_tag
;
273 sh_audio
->channels
= codec
->channels
;
274 sh_audio
->samplerate
= codec
->sample_rate
;
275 sh_audio
->i_bps
= codec
->bit_rate
/8;
276 switch (codec
->codec_id
) {
277 case CODEC_ID_PCM_S8
:
278 case CODEC_ID_PCM_U8
:
279 sh_audio
->samplesize
= 1;
281 case CODEC_ID_PCM_S16LE
:
282 case CODEC_ID_PCM_S16BE
:
283 case CODEC_ID_PCM_U16LE
:
284 case CODEC_ID_PCM_U16BE
:
285 sh_audio
->samplesize
= 2;
287 case CODEC_ID_PCM_ALAW
:
288 sh_audio
->format
= 0x6;
290 case CODEC_ID_PCM_MULAW
:
291 sh_audio
->format
= 0x7;
295 sh_audio
->lang
= strdup(st
->language
);
296 if (st
->disposition
& AV_DISPOSITION_DEFAULT
)
297 sh_audio
->default_track
= 1;
298 if(mp_msg_test(MSGT_HEADER
,MSGL_V
) ) print_wave_header(sh_audio
->wf
, MSGL_V
);
299 // select the first audio stream
300 if (!demuxer
->audio
->sh
) {
301 demuxer
->audio
->id
= i
;
302 demuxer
->audio
->sh
= demuxer
->a_streams
[i
];
304 st
->discard
= AVDISCARD_ALL
;
307 case CODEC_TYPE_VIDEO
:{
308 sh_video_t
* sh_video
;
309 BITMAPINFOHEADER
*bih
;
310 if(priv
->video_streams
>= MAX_V_STREAMS
)
312 sh_video
=new_sh_video(demuxer
, i
);
313 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_VideoID
, "lavf", i
);
315 priv
->vstreams
[priv
->video_streams
] = i
;
316 priv
->video_streams
++;
317 bih
=calloc(sizeof(BITMAPINFOHEADER
) + codec
->extradata_size
,1);
319 if(codec
->codec_id
== CODEC_ID_RAWVIDEO
) {
320 switch (codec
->pix_fmt
) {
322 codec
->codec_tag
= MKTAG(24, 'B', 'G', 'R');
325 if(!codec
->codec_tag
)
326 codec
->codec_tag
= av_codec_get_tag(mp_bmp_taglists
, codec
->codec_id
);
327 bih
->biSize
= sizeof(BITMAPINFOHEADER
) + codec
->extradata_size
;
328 bih
->biWidth
= codec
->width
;
329 bih
->biHeight
= codec
->height
;
330 bih
->biBitCount
= codec
->bits_per_coded_sample
;
331 bih
->biSizeImage
= bih
->biWidth
* bih
->biHeight
* bih
->biBitCount
/8;
332 bih
->biCompression
= codec
->codec_tag
;
334 sh_video
->disp_w
= codec
->width
;
335 sh_video
->disp_h
= codec
->height
;
336 if (st
->time_base
.den
) { /* if container has time_base, use that */
337 sh_video
->video
.dwRate
= st
->time_base
.den
;
338 sh_video
->video
.dwScale
= st
->time_base
.num
;
340 sh_video
->video
.dwRate
= codec
->time_base
.den
;
341 sh_video
->video
.dwScale
= codec
->time_base
.num
;
343 sh_video
->fps
=av_q2d(st
->r_frame_rate
);
344 sh_video
->frametime
=1/av_q2d(st
->r_frame_rate
);
345 sh_video
->format
=bih
->biCompression
;
346 if(st
->sample_aspect_ratio
.num
)
347 sh_video
->aspect
= codec
->width
* st
->sample_aspect_ratio
.num
348 / (float)(codec
->height
* st
->sample_aspect_ratio
.den
);
350 sh_video
->aspect
=codec
->width
* codec
->sample_aspect_ratio
.num
351 / (float)(codec
->height
* codec
->sample_aspect_ratio
.den
);
352 sh_video
->i_bps
=codec
->bit_rate
/8;
353 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"aspect= %d*%d/(%d*%d)\n",
354 codec
->width
, codec
->sample_aspect_ratio
.num
,
355 codec
->height
, codec
->sample_aspect_ratio
.den
);
357 sh_video
->ds
= demuxer
->video
;
358 if(codec
->extradata_size
)
359 memcpy(sh_video
->bih
+ 1, codec
->extradata
, codec
->extradata_size
);
360 if( mp_msg_test(MSGT_HEADER
,MSGL_V
) ) print_video_header(sh_video
->bih
, MSGL_V
);
368 if(demuxer
->video
->id
!= i
&& demuxer
->video
->id
!= -1)
369 st
->discard
= AVDISCARD_ALL
;
371 demuxer
->video
->id
= i
;
372 demuxer
->video
->sh
= demuxer
->v_streams
[i
];
376 case CODEC_TYPE_SUBTITLE
:{
379 if(priv
->sub_streams
>= MAX_S_STREAMS
)
381 /* only support text subtitles for now */
382 if(codec
->codec_id
== CODEC_ID_TEXT
)
384 else if(codec
->codec_id
== CODEC_ID_MOV_TEXT
)
386 else if(codec
->codec_id
== CODEC_ID_SSA
)
388 else if(codec
->codec_id
== CODEC_ID_DVD_SUBTITLE
)
392 sh_sub
= new_sh_sub_sid(demuxer
, i
, priv
->sub_streams
);
393 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_SubtitleID
, "lavf", priv
->sub_streams
);
395 priv
->sstreams
[priv
->sub_streams
] = i
;
397 if (codec
->extradata_size
) {
398 sh_sub
->extradata
= malloc(codec
->extradata_size
);
399 memcpy(sh_sub
->extradata
, codec
->extradata
, codec
->extradata_size
);
400 sh_sub
->extradata_len
= codec
->extradata_size
;
403 sh_sub
->lang
= strdup(st
->language
);
404 if (st
->disposition
& AV_DISPOSITION_DEFAULT
)
405 sh_sub
->default_track
= 1;
409 case CODEC_TYPE_ATTACHMENT
:{
410 if (st
->codec
->codec_id
== CODEC_ID_TTF
)
411 demuxer_add_attachment(demuxer
, st
->filename
,
412 "application/x-truetype-font",
413 codec
->extradata
, codec
->extradata_size
);
417 st
->discard
= AVDISCARD_ALL
;
421 static demuxer_t
* demux_open_lavf(demuxer_t
*demuxer
){
422 AVFormatContext
*avfc
;
423 AVFormatParameters ap
;
425 lavf_priv_t
*priv
= demuxer
->priv
;
427 char mp_filename
[256]="mp:";
429 memset(&ap
, 0, sizeof(AVFormatParameters
));
431 stream_seek(demuxer
->stream
, 0);
433 avfc
= av_alloc_format_context();
436 parse_cryptokey(avfc
, opt_cryptokey
);
437 if (user_correct_pts
!= 0)
438 avfc
->flags
|= AVFMT_FLAG_GENPTS
;
440 avfc
->flags
|= AVFMT_FLAG_IGNIDX
;
442 ap
.prealloced_context
= 1;
444 opt
= av_set_int(avfc
, "probesize", opt_probesize
);
445 if(!opt
) mp_msg(MSGT_HEADER
,MSGL_ERR
, "demux_lavf, couldn't set option probesize to %u\n", opt_probesize
);
447 if(opt_analyzeduration
) {
448 opt
= av_set_int(avfc
, "analyzeduration", opt_analyzeduration
* AV_TIME_BASE
);
449 if(!opt
) mp_msg(MSGT_HEADER
,MSGL_ERR
, "demux_lavf, couldn't set option analyzeduration to %u\n", opt_analyzeduration
);
453 if(parse_avopts(avfc
, opt_avopt
) < 0){
454 mp_msg(MSGT_HEADER
,MSGL_ERR
, "Your options /%s/ look like gibberish to me pal\n", opt_avopt
);
459 if(demuxer
->stream
->url
)
460 strncpy(mp_filename
+ 3, demuxer
->stream
->url
, sizeof(mp_filename
)-3);
462 strncpy(mp_filename
+ 3, "foobar.dummy", sizeof(mp_filename
)-3);
464 priv
->pb
= av_alloc_put_byte(priv
->buffer
, BIO_BUFFER_SIZE
, 0,
465 demuxer
->stream
, mp_read
, NULL
, mp_seek
);
466 priv
->pb
->is_streamed
= !demuxer
->stream
->end_pos
|| (demuxer
->stream
->flags
& STREAM_SEEK
) != STREAM_SEEK
;
468 if(av_open_input_stream(&avfc
, priv
->pb
, mp_filename
, priv
->avif
, &ap
)<0){
469 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF_header: av_open_input_stream() failed\n");
475 if(av_find_stream_info(avfc
) < 0){
476 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF_header: av_find_stream_info() failed\n");
480 if(avfc
->title
[0]) demux_info_add(demuxer
, "title" , avfc
->title
);
481 if(avfc
->author
[0]) demux_info_add(demuxer
, "author" , avfc
->author
);
482 if(avfc
->copyright
[0]) demux_info_add(demuxer
, "copyright", avfc
->copyright
);
483 if(avfc
->comment
[0]) demux_info_add(demuxer
, "comments" , avfc
->comment
);
484 if(avfc
->album
[0]) demux_info_add(demuxer
, "album" , avfc
->album
);
485 // if(avfc->year ) demux_info_add(demuxer, "year" , avfc->year );
486 // if(avfc->track ) demux_info_add(demuxer, "track" , avfc->track );
487 if(avfc
->genre
[0]) demux_info_add(demuxer
, "genre" , avfc
->genre
);
489 for(i
=0; i
< avfc
->nb_chapters
; i
++) {
490 AVChapter
*c
= avfc
->chapters
[i
];
491 uint64_t start
= av_rescale_q(c
->start
, c
->time_base
, (AVRational
){1,1000});
492 uint64_t end
= av_rescale_q(c
->end
, c
->time_base
, (AVRational
){1,1000});
493 demuxer_add_chapter(demuxer
, c
->title
, start
, end
);
496 if(avfc
->nb_programs
) {
497 int p
, start
=0, found
=0;
500 for(p
=0; p
<avfc
->nb_programs
; p
++) {
501 if(avfc
->programs
[p
]->id
== ts_prog
) {
508 mp_msg(MSGT_HEADER
,MSGL_ERR
,"DEMUX_LAVF: program %d doesn't seem to be present\n", ts_prog
);
514 AVProgram
*program
= avfc
->programs
[p
];
515 mp_msg(MSGT_HEADER
,MSGL_INFO
,"LAVF: Program %d %s\n", program
->id
, (program
->name
? program
->name
: ""));
516 for(i
=0; i
<program
->nb_stream_indexes
; i
++)
517 handle_stream(demuxer
, avfc
, program
->stream_index
[i
]);
518 if(!priv
->cur_program
&& (demuxer
->video
->sh
|| demuxer
->audio
->sh
))
519 priv
->cur_program
= program
->id
;
520 p
= (p
+ 1) % avfc
->nb_programs
;
523 for(i
=0; i
<avfc
->nb_streams
; i
++)
524 handle_stream(demuxer
, avfc
, i
);
526 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF: %d audio and %d video streams found\n",priv
->audio_streams
,priv
->video_streams
);
527 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF: build %d\n", LIBAVFORMAT_BUILD
);
528 if(!priv
->audio_streams
) demuxer
->audio
->id
=-2; // nosound
529 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio;
530 if(!priv
->video_streams
){
531 if(!priv
->audio_streams
){
532 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF: no audio or video headers found - broken file?\n");
535 demuxer
->video
->id
=-2; // audio-only
536 } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video;
541 static int demux_lavf_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
){
542 lavf_priv_t
*priv
= demux
->priv
;
547 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"demux_lavf_fill_buffer()\n");
549 demux
->filepos
=stream_tell(demux
->stream
);
551 if(av_read_frame(priv
->avfc
, &pkt
) < 0)
554 id
= pkt
.stream_index
;
556 if(id
==demux
->audio
->id
){
560 ds
->sh
=demux
->a_streams
[id
];
561 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected LAVF audio ID = %d\n",ds
->id
);
563 } else if(id
==demux
->video
->id
){
567 ds
->sh
=demux
->v_streams
[id
];
568 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected LAVF video ID = %d\n",ds
->id
);
570 } else if(id
==demux
->sub
->id
){
575 av_free_packet(&pkt
);
579 if(0/*pkt.destruct == av_destruct_packet*/){
580 //ok kids, dont try this at home :)
581 dp
=malloc(sizeof(demux_packet_t
));
589 dp
=new_demux_packet(pkt
.size
);
590 memcpy(dp
->buffer
, pkt
.data
, pkt
.size
);
591 av_free_packet(&pkt
);
594 if(pkt
.pts
!= AV_NOPTS_VALUE
){
595 dp
->pts
=pkt
.pts
* av_q2d(priv
->avfc
->streams
[id
]->time_base
);
596 priv
->last_pts
= dp
->pts
* AV_TIME_BASE
;
597 if(pkt
.convergence_duration
)
598 dp
->endpts
= dp
->pts
+ pkt
.convergence_duration
* av_q2d(priv
->avfc
->streams
[id
]->time_base
);
600 dp
->pos
=demux
->filepos
;
601 dp
->flags
= !!(pkt
.flags
&PKT_FLAG_KEY
);
602 // append packet to DS stream:
603 ds_add_packet(ds
,dp
);
607 static void demux_seek_lavf(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
, int flags
){
608 lavf_priv_t
*priv
= demuxer
->priv
;
610 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"demux_seek_lavf(%p, %f, %f, %d)\n", demuxer
, rel_seek_secs
, audio_delay
, flags
);
612 if (flags
& SEEK_ABSOLUTE
) {
613 priv
->last_pts
= priv
->avfc
->start_time
;
615 if (rel_seek_secs
< 0) avsflags
= AVSEEK_FLAG_BACKWARD
;
617 if (flags
& SEEK_FACTOR
) {
618 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
620 priv
->last_pts
+= rel_seek_secs
* priv
->avfc
->duration
;
622 priv
->last_pts
+= rel_seek_secs
* AV_TIME_BASE
;
624 av_seek_frame(priv
->avfc
, -1, priv
->last_pts
, avsflags
);
627 static int demux_lavf_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
629 lavf_priv_t
*priv
= demuxer
->priv
;
632 case DEMUXER_CTRL_CORRECT_PTS
:
633 return DEMUXER_CTRL_OK
;
634 case DEMUXER_CTRL_GET_TIME_LENGTH
:
635 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
636 return DEMUXER_CTRL_DONTKNOW
;
638 *((double *)arg
) = (double)priv
->avfc
->duration
/ AV_TIME_BASE
;
639 return DEMUXER_CTRL_OK
;
641 case DEMUXER_CTRL_GET_PERCENT_POS
:
642 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
643 return DEMUXER_CTRL_DONTKNOW
;
645 *((int *)arg
) = (int)((priv
->last_pts
- priv
->avfc
->start_time
)*100 / priv
->avfc
->duration
);
646 return DEMUXER_CTRL_OK
;
647 case DEMUXER_CTRL_SWITCH_AUDIO
:
648 case DEMUXER_CTRL_SWITCH_VIDEO
:
650 int id
= *((int*)arg
);
653 int nstreams
, *pstreams
;
656 if(cmd
== DEMUXER_CTRL_SWITCH_VIDEO
)
659 nstreams
= priv
->video_streams
;
660 pstreams
= priv
->vstreams
;
665 nstreams
= priv
->audio_streams
;
666 pstreams
= priv
->astreams
;
668 for(i
= 0; i
< nstreams
; i
++)
670 if(pstreams
[i
] == ds
->id
) //current stream id
677 if(id
== -2) { // no sound
679 } else if(id
== -1) { // next track
680 i
= (curridx
+ 2) % (nstreams
+ 1) - 1;
684 else // select track by id
686 for(i
= 0; i
< nstreams
; i
++)
688 if(pstreams
[i
] == id
)
696 return DEMUXER_CTRL_NOTIMPL
;
701 priv
->avfc
->streams
[ds
->id
]->discard
= AVDISCARD_ALL
;
702 *((int*)arg
) = ds
->id
= newid
;
704 priv
->avfc
->streams
[newid
]->discard
= AVDISCARD_NONE
;
705 return DEMUXER_CTRL_OK
;
708 case DEMUXER_CTRL_IDENTIFY_PROGRAM
:
710 demux_program_t
*prog
= arg
;
714 if(priv
->avfc
->nb_programs
< 2)
715 return DEMUXER_CTRL_NOTIMPL
;
717 if(prog
->progid
== -1)
720 while(p
<priv
->avfc
->nb_programs
&& priv
->avfc
->programs
[p
]->id
!= priv
->cur_program
)
722 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
726 for(i
=0; i
<priv
->avfc
->nb_programs
; i
++)
727 if(priv
->avfc
->programs
[i
]->id
== prog
->progid
)
729 if(i
==priv
->avfc
->nb_programs
)
730 return DEMUXER_CTRL_NOTIMPL
;
733 prog
->vid
= prog
->aid
= prog
->sid
= -2; //no audio and no video by default
735 program
= priv
->avfc
->programs
[p
];
736 for(i
=0; i
<program
->nb_stream_indexes
; i
++)
738 switch(priv
->avfc
->streams
[program
->stream_index
[i
]]->codec
->codec_type
)
740 case CODEC_TYPE_VIDEO
:
742 prog
->vid
= program
->stream_index
[i
];
744 case CODEC_TYPE_AUDIO
:
746 prog
->aid
= program
->stream_index
[i
];
748 case CODEC_TYPE_SUBTITLE
:
749 if(prog
->sid
== -2 && priv
->avfc
->streams
[program
->stream_index
[i
]]->codec
->codec_id
== CODEC_ID_TEXT
)
750 prog
->sid
= program
->stream_index
[i
];
754 if(prog
->progid
== -1 && prog
->vid
== -2 && prog
->aid
== -2)
756 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
759 priv
->cur_program
= prog
->progid
= program
->id
;
760 return DEMUXER_CTRL_OK
;
763 return DEMUXER_CTRL_NOTIMPL
;
767 static void demux_close_lavf(demuxer_t
*demuxer
)
769 lavf_priv_t
* priv
= demuxer
->priv
;
773 av_freep(&priv
->avfc
->key
);
774 av_close_input_stream(priv
->avfc
);
777 free(priv
); demuxer
->priv
= NULL
;
782 const demuxer_desc_t demuxer_desc_lavf
= {
783 "libavformat demuxer",
786 "Michael Niedermayer",
787 "supports many formats, requires libavformat",
789 0, // Check after other demuxer
791 demux_lavf_fill_buffer
,
798 const demuxer_desc_t demuxer_desc_lavf_preferred
= {
799 "libavformat preferred demuxer",
802 "Michael Niedermayer",
803 "supports many formats, requires libavformat",
804 DEMUXER_TYPE_LAVF_PREFERRED
,
806 lavf_check_preferred_file
,
807 demux_lavf_fill_buffer
,