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>
32 #include "stream/stream.h"
36 #include "libvo/sub.h"
38 #include "libavformat/avformat.h"
39 #include "libavutil/avutil.h"
40 #include "libavformat/avi.h"
41 #include "libavcodec/opt.h"
42 #include "libavformat/riff.h"
44 #include "mp_taglists.h"
46 #define PROBE_BUF_SIZE 2048
48 static unsigned int opt_probesize
= 0;
49 static unsigned int opt_analyzeduration
= 0;
50 static char *opt_format
;
51 static char *opt_cryptokey
;
53 static char *opt_avopt
= NULL
;
55 const m_option_t lavfdopts_conf
[] = {
56 {"probesize", &(opt_probesize
), CONF_TYPE_INT
, CONF_RANGE
, 32, INT_MAX
, NULL
},
57 {"format", &(opt_format
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
58 {"analyzeduration", &(opt_analyzeduration
), CONF_TYPE_INT
, CONF_RANGE
, 0, INT_MAX
, NULL
},
59 {"cryptokey", &(opt_cryptokey
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
60 {"o", &opt_avopt
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
61 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
64 #define BIO_BUFFER_SIZE 32768
66 typedef struct lavf_priv_t
{
68 AVFormatContext
*avfc
;
70 uint8_t buffer
[BIO_BUFFER_SIZE
];
75 int astreams
[MAX_A_STREAMS
];
76 int vstreams
[MAX_V_STREAMS
];
77 int sstreams
[MAX_S_STREAMS
];
81 extern void print_wave_header(WAVEFORMATEX
*h
, int verbose_level
);
82 extern void print_video_header(BITMAPINFOHEADER
*h
, int verbose_level
);
84 static int mp_read(void *opaque
, uint8_t *buf
, int size
) {
85 stream_t
*stream
= opaque
;
88 if(stream_eof(stream
)) //needed?
90 ret
=stream_read(stream
, buf
, size
);
92 mp_msg(MSGT_HEADER
,MSGL_DBG2
,"%d=mp_read(%p, %p, %d), eof:%d\n", ret
, stream
, buf
, size
, stream
->eof
);
96 static offset_t
mp_seek(void *opaque
, offset_t pos
, int whence
) {
97 stream_t
*stream
= opaque
;
98 mp_msg(MSGT_HEADER
,MSGL_DBG2
,"mp_seek(%p, %d, %d)\n", stream
, (int)pos
, whence
);
99 if(whence
== SEEK_CUR
)
100 pos
+=stream_tell(stream
);
101 else if(whence
== SEEK_END
&& stream
->end_pos
> 0)
102 pos
+= stream
->end_pos
;
103 else if(whence
== SEEK_SET
)
104 pos
+= stream
->start_pos
;
105 else if(whence
== AVSEEK_SIZE
&& stream
->end_pos
> 0)
106 return stream
->end_pos
- stream
->start_pos
;
112 if(pos
<stream
->end_pos
&& stream
->eof
)
113 stream_reset(stream
);
114 if(stream_seek(stream
, pos
)==0)
117 return pos
- stream
->start_pos
;
120 static void list_formats(void) {
122 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "Available lavf input formats:\n");
123 for (fmt
= first_iformat
; fmt
; fmt
= fmt
->next
)
124 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "%15s : %s\n", fmt
->name
, fmt
->long_name
);
127 static int lavf_check_file(demuxer_t
*demuxer
){
129 uint8_t buf
[PROBE_BUF_SIZE
];
133 demuxer
->priv
=calloc(sizeof(lavf_priv_t
),1);
138 if(stream_read(demuxer
->stream
, buf
, PROBE_BUF_SIZE
)!=PROBE_BUF_SIZE
)
140 avpd
.filename
= demuxer
->stream
->url
;
142 avpd
.buf_size
= PROBE_BUF_SIZE
;
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
;
157 priv
->avif
= av_probe_input_format(&avpd
, 1);
159 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF_check: no clue about this gibberish!\n");
162 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF_check: %s\n", priv
->avif
->long_name
);
164 return DEMUXER_TYPE_LAVF
;
167 static const char *preferred_list
[] = {
176 "mov,mp4,m4a,3gp,3g2,mj2",
182 static int lavf_check_preferred_file(demuxer_t
*demuxer
){
183 if (lavf_check_file(demuxer
)) {
184 char **p
= preferred_list
;
185 lavf_priv_t
*priv
= demuxer
->priv
;
187 if (strcmp(*p
, priv
->avif
->name
) == 0)
188 return DEMUXER_TYPE_LAVF_PREFERRED
;
195 static uint8_t char2int(char c
) {
196 if (c
>= '0' && c
<= '9') return c
- '0';
197 if (c
>= 'a' && c
<= 'f') return c
- 'a' + 10;
198 if (c
>= 'A' && c
<= 'F') return c
- 'A' + 10;
202 static void parse_cryptokey(AVFormatContext
*avfc
, const char *str
) {
203 int len
= strlen(str
) / 2;
204 uint8_t *key
= av_mallocz(len
);
208 for (i
= 0; i
< len
; i
++, str
+= 2)
209 *key
++ = (char2int(str
[0]) << 4) | char2int(str
[1]);
212 static void handle_stream(demuxer_t
*demuxer
, AVFormatContext
*avfc
, int i
) {
213 lavf_priv_t
*priv
= demuxer
->priv
;
214 AVStream
*st
= avfc
->streams
[i
];
215 AVCodecContext
*codec
= st
->codec
;
218 switch(codec
->codec_type
){
219 case CODEC_TYPE_AUDIO
:{
221 WAVEFORMATEX
*wf
= calloc(sizeof(WAVEFORMATEX
) + codec
->extradata_size
, 1);
222 sh_audio_t
* sh_audio
;
223 if(priv
->audio_streams
>= MAX_A_STREAMS
)
225 sh_audio
=new_sh_audio(demuxer
, i
);
226 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_AudioID
, "lavf", i
);
229 priv
->astreams
[priv
->audio_streams
] = i
;
230 priv
->audio_streams
++;
231 // For some formats (like PCM) always trust CODEC_ID_* more than codec_tag
232 override_tag
= av_codec_get_tag(mp_wav_override_taglists
, codec
->codec_id
);
234 codec
->codec_tag
= override_tag
;
235 // mp4a tag is used for all mp4 files no matter what they actually contain
236 if(codec
->codec_tag
== MKTAG('m', 'p', '4', 'a'))
238 if(codec
->codec_id
== CODEC_ID_ADPCM_IMA_AMV
)
239 codec
->codec_tag
= MKTAG('A','M','V','A');
240 if(!codec
->codec_tag
)
241 codec
->codec_tag
= av_codec_get_tag(mp_wav_taglists
, codec
->codec_id
);
242 wf
->wFormatTag
= codec
->codec_tag
;
243 wf
->nChannels
= codec
->channels
;
244 wf
->nSamplesPerSec
= codec
->sample_rate
;
245 wf
->nAvgBytesPerSec
= codec
->bit_rate
/8;
246 wf
->nBlockAlign
= codec
->block_align
? codec
->block_align
: 1;
247 wf
->wBitsPerSample
= codec
->bits_per_sample
;
248 wf
->cbSize
= codec
->extradata_size
;
249 if(codec
->extradata_size
)
250 memcpy(wf
+ 1, codec
->extradata
, codec
->extradata_size
);
252 sh_audio
->audio
.dwSampleSize
= codec
->block_align
;
253 if(codec
->frame_size
&& codec
->sample_rate
){
254 sh_audio
->audio
.dwScale
=codec
->frame_size
;
255 sh_audio
->audio
.dwRate
= codec
->sample_rate
;
257 sh_audio
->audio
.dwScale
= codec
->block_align
? codec
->block_align
*8 : 8;
258 sh_audio
->audio
.dwRate
= codec
->bit_rate
;
260 g
= ff_gcd(sh_audio
->audio
.dwScale
, sh_audio
->audio
.dwRate
);
261 sh_audio
->audio
.dwScale
/= g
;
262 sh_audio
->audio
.dwRate
/= g
;
263 // 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);
264 sh_audio
->ds
= demuxer
->audio
;
265 sh_audio
->format
= codec
->codec_tag
;
266 sh_audio
->channels
= codec
->channels
;
267 sh_audio
->samplerate
= codec
->sample_rate
;
268 sh_audio
->i_bps
= codec
->bit_rate
/8;
269 switch (codec
->codec_id
) {
270 case CODEC_ID_PCM_S8
:
271 case CODEC_ID_PCM_U8
:
272 sh_audio
->samplesize
= 1;
274 case CODEC_ID_PCM_S16LE
:
275 case CODEC_ID_PCM_S16BE
:
276 case CODEC_ID_PCM_U16LE
:
277 case CODEC_ID_PCM_U16BE
:
278 sh_audio
->samplesize
= 2;
280 case CODEC_ID_PCM_ALAW
:
281 sh_audio
->format
= 0x6;
283 case CODEC_ID_PCM_MULAW
:
284 sh_audio
->format
= 0x7;
288 sh_audio
->lang
= strdup(st
->language
);
289 if (st
->disposition
& AV_DISPOSITION_DEFAULT
)
290 sh_audio
->default_track
= 1;
291 if(mp_msg_test(MSGT_HEADER
,MSGL_V
) ) print_wave_header(sh_audio
->wf
, MSGL_V
);
292 // select the first audio stream
293 if (!demuxer
->audio
->sh
) {
294 demuxer
->audio
->id
= i
;
295 demuxer
->audio
->sh
= demuxer
->a_streams
[i
];
297 st
->discard
= AVDISCARD_ALL
;
300 case CODEC_TYPE_VIDEO
:{
301 sh_video_t
* sh_video
;
302 BITMAPINFOHEADER
*bih
;
303 if(priv
->video_streams
>= MAX_V_STREAMS
)
305 sh_video
=new_sh_video(demuxer
, i
);
306 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_VideoID
, "lavf", i
);
308 priv
->vstreams
[priv
->video_streams
] = i
;
309 priv
->video_streams
++;
310 bih
=calloc(sizeof(BITMAPINFOHEADER
) + codec
->extradata_size
,1);
312 if(codec
->codec_id
== CODEC_ID_RAWVIDEO
) {
313 switch (codec
->pix_fmt
) {
315 codec
->codec_tag
= MKTAG(24, 'B', 'G', 'R');
318 if(!codec
->codec_tag
)
319 codec
->codec_tag
= av_codec_get_tag(mp_bmp_taglists
, codec
->codec_id
);
320 bih
->biSize
= sizeof(BITMAPINFOHEADER
) + codec
->extradata_size
;
321 bih
->biWidth
= codec
->width
;
322 bih
->biHeight
= codec
->height
;
323 bih
->biBitCount
= codec
->bits_per_sample
;
324 bih
->biSizeImage
= bih
->biWidth
* bih
->biHeight
* bih
->biBitCount
/8;
325 bih
->biCompression
= codec
->codec_tag
;
327 sh_video
->disp_w
= codec
->width
;
328 sh_video
->disp_h
= codec
->height
;
329 if (st
->time_base
.den
) { /* if container has time_base, use that */
330 sh_video
->video
.dwRate
= st
->time_base
.den
;
331 sh_video
->video
.dwScale
= st
->time_base
.num
;
333 sh_video
->video
.dwRate
= codec
->time_base
.den
;
334 sh_video
->video
.dwScale
= codec
->time_base
.num
;
336 sh_video
->fps
=av_q2d(st
->r_frame_rate
);
337 sh_video
->frametime
=1/av_q2d(st
->r_frame_rate
);
338 sh_video
->format
=bih
->biCompression
;
339 sh_video
->aspect
=codec
->width
* codec
->sample_aspect_ratio
.num
340 / (float)(codec
->height
* codec
->sample_aspect_ratio
.den
);
341 sh_video
->i_bps
=codec
->bit_rate
/8;
342 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"aspect= %d*%d/(%d*%d)\n",
343 codec
->width
, codec
->sample_aspect_ratio
.num
,
344 codec
->height
, codec
->sample_aspect_ratio
.den
);
346 sh_video
->ds
= demuxer
->video
;
347 if(codec
->extradata_size
)
348 memcpy(sh_video
->bih
+ 1, codec
->extradata
, codec
->extradata_size
);
349 if( mp_msg_test(MSGT_HEADER
,MSGL_V
) ) print_video_header(sh_video
->bih
, MSGL_V
);
357 if(demuxer
->video
->id
!= i
&& demuxer
->video
->id
!= -1)
358 st
->discard
= AVDISCARD_ALL
;
360 demuxer
->video
->id
= i
;
361 demuxer
->video
->sh
= demuxer
->v_streams
[i
];
365 case CODEC_TYPE_SUBTITLE
:{
368 if(priv
->sub_streams
>= MAX_S_STREAMS
)
370 /* only support text subtitles for now */
371 if(codec
->codec_id
== CODEC_ID_TEXT
)
373 else if(codec
->codec_id
== CODEC_ID_MOV_TEXT
)
375 else if(codec
->codec_id
== CODEC_ID_SSA
)
377 else if(codec
->codec_id
== CODEC_ID_DVD_SUBTITLE
)
381 sh_sub
= new_sh_sub_sid(demuxer
, i
, priv
->sub_streams
);
382 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_SubtitleID
, "lavf", priv
->sub_streams
);
384 priv
->sstreams
[priv
->sub_streams
] = i
;
386 if (codec
->extradata_size
) {
387 sh_sub
->extradata
= malloc(codec
->extradata_size
);
388 memcpy(sh_sub
->extradata
, codec
->extradata
, codec
->extradata_size
);
389 sh_sub
->extradata_len
= codec
->extradata_size
;
392 sh_sub
->lang
= strdup(st
->language
);
393 if (st
->disposition
& AV_DISPOSITION_DEFAULT
)
394 sh_sub
->default_track
= 1;
398 case CODEC_TYPE_ATTACHMENT
:{
399 if (st
->codec
->codec_id
== CODEC_ID_TTF
)
400 demuxer_add_attachment(demuxer
, st
->filename
,
401 "application/x-truetype-font",
402 codec
->extradata
, codec
->extradata_size
);
406 st
->discard
= AVDISCARD_ALL
;
410 static demuxer_t
* demux_open_lavf(demuxer_t
*demuxer
){
411 struct MPOpts
*opts
= demuxer
->opts
;
412 AVFormatContext
*avfc
;
413 AVFormatParameters ap
;
415 lavf_priv_t
*priv
= demuxer
->priv
;
417 char mp_filename
[256]="mp:";
419 memset(&ap
, 0, sizeof(AVFormatParameters
));
421 stream_seek(demuxer
->stream
, 0);
423 avfc
= av_alloc_format_context();
426 parse_cryptokey(avfc
, opt_cryptokey
);
427 if (opts
->user_correct_pts
!= 0)
428 avfc
->flags
|= AVFMT_FLAG_GENPTS
;
430 avfc
->flags
|= AVFMT_FLAG_IGNIDX
;
432 ap
.prealloced_context
= 1;
434 opt
= av_set_int(avfc
, "probesize", opt_probesize
);
435 if(!opt
) mp_msg(MSGT_HEADER
,MSGL_ERR
, "demux_lavf, couldn't set option probesize to %u\n", opt_probesize
);
437 if(opt_analyzeduration
) {
438 opt
= av_set_int(avfc
, "analyzeduration", opt_analyzeduration
* AV_TIME_BASE
);
439 if(!opt
) mp_msg(MSGT_HEADER
,MSGL_ERR
, "demux_lavf, couldn't set option analyzeduration to %u\n", opt_analyzeduration
);
443 if(parse_avopts(avfc
, opt_avopt
) < 0){
444 mp_msg(MSGT_HEADER
,MSGL_ERR
, "Your options /%s/ look like gibberish to me pal\n", opt_avopt
);
449 if(demuxer
->stream
->url
)
450 strncpy(mp_filename
+ 3, demuxer
->stream
->url
, sizeof(mp_filename
)-3);
452 strncpy(mp_filename
+ 3, "foobar.dummy", sizeof(mp_filename
)-3);
454 priv
->pb
= av_alloc_put_byte(priv
->buffer
, BIO_BUFFER_SIZE
, 0,
455 demuxer
->stream
, mp_read
, NULL
, mp_seek
);
456 priv
->pb
->is_streamed
= !demuxer
->stream
->end_pos
;
458 if(av_open_input_stream(&avfc
, priv
->pb
, mp_filename
, priv
->avif
, &ap
)<0){
459 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF_header: av_open_input_stream() failed\n");
465 if(av_find_stream_info(avfc
) < 0){
466 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF_header: av_find_stream_info() failed\n");
470 if(avfc
->title
[0]) demux_info_add(demuxer
, "name" , avfc
->title
);
471 if(avfc
->author
[0]) demux_info_add(demuxer
, "author" , avfc
->author
);
472 if(avfc
->copyright
[0]) demux_info_add(demuxer
, "copyright", avfc
->copyright
);
473 if(avfc
->comment
[0]) demux_info_add(demuxer
, "comments" , avfc
->comment
);
474 if(avfc
->album
[0]) demux_info_add(demuxer
, "album" , avfc
->album
);
475 // if(avfc->year ) demux_info_add(demuxer, "year" , avfc->year );
476 // if(avfc->track ) demux_info_add(demuxer, "track" , avfc->track );
477 if(avfc
->genre
[0]) demux_info_add(demuxer
, "genre" , avfc
->genre
);
479 for(i
=0; i
< avfc
->nb_chapters
; i
++) {
480 AVChapter
*c
= avfc
->chapters
[i
];
481 uint64_t start
= av_rescale_q(c
->start
, c
->time_base
, (AVRational
){1,1000});
482 uint64_t end
= av_rescale_q(c
->end
, c
->time_base
, (AVRational
){1,1000});
483 demuxer_add_chapter(demuxer
, c
->title
, start
, end
);
486 if(avfc
->nb_programs
) {
487 int p
, start
=0, found
=0;
490 for(p
=0; p
<avfc
->nb_programs
; p
++) {
491 if(avfc
->programs
[p
]->id
== ts_prog
) {
498 mp_msg(MSGT_HEADER
,MSGL_ERR
,"DEMUX_LAVF: program %d doesn't seem to be present\n", ts_prog
);
504 AVProgram
*program
= avfc
->programs
[p
];
505 mp_msg(MSGT_HEADER
,MSGL_INFO
,"LAVF: Program %d %s\n", program
->id
, (program
->name
? program
->name
: ""));
506 for(i
=0; i
<program
->nb_stream_indexes
; i
++)
507 handle_stream(demuxer
, avfc
, program
->stream_index
[i
]);
508 if(!priv
->cur_program
&& (demuxer
->video
->sh
|| demuxer
->audio
->sh
))
509 priv
->cur_program
= program
->id
;
510 p
= (p
+ 1) % avfc
->nb_programs
;
513 for(i
=0; i
<avfc
->nb_streams
; i
++)
514 handle_stream(demuxer
, avfc
, i
);
516 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF: %d audio and %d video streams found\n",priv
->audio_streams
,priv
->video_streams
);
517 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF: build %d\n", LIBAVFORMAT_BUILD
);
518 if(!priv
->audio_streams
) demuxer
->audio
->id
=-2; // nosound
519 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio;
520 if(!priv
->video_streams
){
521 if(!priv
->audio_streams
){
522 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF: no audio or video headers found - broken file?\n");
525 demuxer
->video
->id
=-2; // audio-only
526 } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video;
531 static int demux_lavf_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
){
532 lavf_priv_t
*priv
= demux
->priv
;
537 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"demux_lavf_fill_buffer()\n");
539 demux
->filepos
=stream_tell(demux
->stream
);
541 if(av_read_frame(priv
->avfc
, &pkt
) < 0)
544 id
= pkt
.stream_index
;
546 if(id
==demux
->audio
->id
){
550 ds
->sh
=demux
->a_streams
[id
];
551 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected LAVF audio ID = %d\n",ds
->id
);
553 } else if(id
==demux
->video
->id
){
557 ds
->sh
=demux
->v_streams
[id
];
558 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected LAVF video ID = %d\n",ds
->id
);
560 } else if(id
==demux
->sub
->id
){
565 av_free_packet(&pkt
);
569 if(0/*pkt.destruct == av_destruct_packet*/){
570 //ok kids, dont try this at home :)
571 dp
=malloc(sizeof(demux_packet_t
));
579 dp
=new_demux_packet(pkt
.size
);
580 memcpy(dp
->buffer
, pkt
.data
, pkt
.size
);
581 av_free_packet(&pkt
);
584 if(pkt
.pts
!= AV_NOPTS_VALUE
){
585 dp
->pts
=pkt
.pts
* av_q2d(priv
->avfc
->streams
[id
]->time_base
);
586 priv
->last_pts
= dp
->pts
* AV_TIME_BASE
;
588 dp
->endpts
= dp
->pts
+ pkt
.duration
* av_q2d(priv
->avfc
->streams
[id
]->time_base
);
590 dp
->pos
=demux
->filepos
;
591 dp
->flags
= !!(pkt
.flags
&PKT_FLAG_KEY
);
592 // append packet to DS stream:
593 ds_add_packet(ds
,dp
);
597 static void demux_seek_lavf(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
, int flags
){
598 lavf_priv_t
*priv
= demuxer
->priv
;
600 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"demux_seek_lavf(%p, %f, %f, %d)\n", demuxer
, rel_seek_secs
, audio_delay
, flags
);
602 if (flags
& SEEK_ABSOLUTE
) {
603 priv
->last_pts
= priv
->avfc
->start_time
;
605 if (rel_seek_secs
< 0) avsflags
= AVSEEK_FLAG_BACKWARD
;
607 if (flags
& SEEK_FACTOR
) {
608 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
610 priv
->last_pts
+= rel_seek_secs
* priv
->avfc
->duration
;
612 priv
->last_pts
+= rel_seek_secs
* AV_TIME_BASE
;
614 av_seek_frame(priv
->avfc
, -1, priv
->last_pts
, avsflags
);
617 static int demux_lavf_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
619 lavf_priv_t
*priv
= demuxer
->priv
;
622 case DEMUXER_CTRL_CORRECT_PTS
:
623 return DEMUXER_CTRL_OK
;
624 case DEMUXER_CTRL_GET_TIME_LENGTH
:
625 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
626 return DEMUXER_CTRL_DONTKNOW
;
628 *((double *)arg
) = (double)priv
->avfc
->duration
/ AV_TIME_BASE
;
629 return DEMUXER_CTRL_OK
;
631 case DEMUXER_CTRL_GET_PERCENT_POS
:
632 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
633 return DEMUXER_CTRL_DONTKNOW
;
635 *((int *)arg
) = (int)((priv
->last_pts
- priv
->avfc
->start_time
)*100 / priv
->avfc
->duration
);
636 return DEMUXER_CTRL_OK
;
637 case DEMUXER_CTRL_SWITCH_AUDIO
:
638 case DEMUXER_CTRL_SWITCH_VIDEO
:
640 int id
= *((int*)arg
);
643 int nstreams
, *pstreams
;
646 if(cmd
== DEMUXER_CTRL_SWITCH_VIDEO
)
649 nstreams
= priv
->video_streams
;
650 pstreams
= priv
->vstreams
;
655 nstreams
= priv
->audio_streams
;
656 pstreams
= priv
->astreams
;
658 for(i
= 0; i
< nstreams
; i
++)
660 if(pstreams
[i
] == ds
->id
) //current stream id
667 if(id
== -2) { // no sound
669 } else if(id
== -1) { // next track
670 i
= (curridx
+ 2) % (nstreams
+ 1) - 1;
674 else // select track by id
676 for(i
= 0; i
< nstreams
; i
++)
678 if(pstreams
[i
] == id
)
686 return DEMUXER_CTRL_NOTIMPL
;
691 priv
->avfc
->streams
[ds
->id
]->discard
= AVDISCARD_ALL
;
692 *((int*)arg
) = ds
->id
= newid
;
694 priv
->avfc
->streams
[newid
]->discard
= AVDISCARD_NONE
;
695 return DEMUXER_CTRL_OK
;
698 case DEMUXER_CTRL_IDENTIFY_PROGRAM
:
700 demux_program_t
*prog
= arg
;
704 if(priv
->avfc
->nb_programs
< 2)
705 return DEMUXER_CTRL_NOTIMPL
;
707 if(prog
->progid
== -1)
710 while(p
<priv
->avfc
->nb_programs
&& priv
->avfc
->programs
[p
]->id
!= priv
->cur_program
)
712 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
716 for(i
=0; i
<priv
->avfc
->nb_programs
; i
++)
717 if(priv
->avfc
->programs
[i
]->id
== prog
->progid
)
719 if(i
==priv
->avfc
->nb_programs
)
720 return DEMUXER_CTRL_NOTIMPL
;
723 prog
->vid
= prog
->aid
= prog
->sid
= -2; //no audio and no video by default
725 program
= priv
->avfc
->programs
[p
];
726 for(i
=0; i
<program
->nb_stream_indexes
; i
++)
728 switch(priv
->avfc
->streams
[program
->stream_index
[i
]]->codec
->codec_type
)
730 case CODEC_TYPE_VIDEO
:
732 prog
->vid
= program
->stream_index
[i
];
734 case CODEC_TYPE_AUDIO
:
736 prog
->aid
= program
->stream_index
[i
];
738 case CODEC_TYPE_SUBTITLE
:
739 if(prog
->sid
== -2 && priv
->avfc
->streams
[program
->stream_index
[i
]]->codec
->codec_id
== CODEC_ID_TEXT
)
740 prog
->sid
= program
->stream_index
[i
];
744 if(prog
->progid
== -1 && prog
->vid
== -2 && prog
->aid
== -2)
746 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
749 priv
->cur_program
= prog
->progid
= program
->id
;
750 return DEMUXER_CTRL_OK
;
753 return DEMUXER_CTRL_NOTIMPL
;
757 static void demux_close_lavf(demuxer_t
*demuxer
)
759 lavf_priv_t
* priv
= demuxer
->priv
;
763 av_freep(&priv
->avfc
->key
);
764 av_close_input_stream(priv
->avfc
);
767 free(priv
); demuxer
->priv
= NULL
;
772 const demuxer_desc_t demuxer_desc_lavf
= {
773 "libavformat demuxer",
776 "Michael Niedermayer",
777 "supports many formats, requires libavformat",
779 0, // Check after other demuxer
781 demux_lavf_fill_buffer
,
788 const demuxer_desc_t demuxer_desc_lavf_preferred
= {
789 "libavformat preferred demuxer",
792 "Michael Niedermayer",
793 "supports many formats, requires libavformat",
794 DEMUXER_TYPE_LAVF_PREFERRED
,
796 lavf_check_preferred_file
,
797 demux_lavf_fill_buffer
,