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"
37 #include "libvo/sub.h"
39 #include "libavformat/avformat.h"
40 #include "libavformat/avio.h"
41 #include "libavutil/avutil.h"
42 #include "libavutil/avstring.h"
43 #include "libavcodec/opt.h"
45 #include "mp_taglists.h"
47 #define INITIAL_PROBE_SIZE (32*1024)
48 #define PROBE_BUF_SIZE (2*1024*1024)
50 const m_option_t lavfdopts_conf
[] = {
51 OPT_INTRANGE("probesize", lavfdopts
.probesize
, 0, 32, INT_MAX
),
52 OPT_STRING("format", lavfdopts
.format
, 0),
53 OPT_INTRANGE("analyzeduration", lavfdopts
.analyzeduration
, 0, 0, INT_MAX
),
54 OPT_STRING("cryptokey", lavfdopts
.cryptokey
, 0),
55 OPT_STRING("o", lavfdopts
.avopt
, 0),
56 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
59 #define BIO_BUFFER_SIZE 32768
61 typedef struct lavf_priv_t
{
63 AVFormatContext
*avfc
;
65 uint8_t buffer
[BIO_BUFFER_SIZE
];
70 int astreams
[MAX_A_STREAMS
];
71 int vstreams
[MAX_V_STREAMS
];
72 int sstreams
[MAX_S_STREAMS
];
76 static int mp_read(void *opaque
, uint8_t *buf
, int size
) {
77 stream_t
*stream
= opaque
;
80 if(stream_eof(stream
)) //needed?
82 ret
=stream_read(stream
, buf
, size
);
84 mp_msg(MSGT_HEADER
,MSGL_DBG2
,"%d=mp_read(%p, %p, %d), eof:%d\n", ret
, stream
, buf
, size
, stream
->eof
);
88 static int64_t mp_seek(void *opaque
, int64_t pos
, int whence
) {
89 stream_t
*stream
= opaque
;
91 mp_msg(MSGT_HEADER
,MSGL_DBG2
,"mp_seek(%p, %"PRId64
", %d)\n", stream
, pos
, whence
);
92 if(whence
== SEEK_CUR
)
93 pos
+=stream_tell(stream
);
94 else if(whence
== SEEK_END
&& stream
->end_pos
> 0)
95 pos
+= stream
->end_pos
;
96 else if(whence
== SEEK_SET
)
97 pos
+= stream
->start_pos
;
98 else if(whence
== AVSEEK_SIZE
&& stream
->end_pos
> 0)
99 return stream
->end_pos
- stream
->start_pos
;
105 if(pos
<stream
->end_pos
&& stream
->eof
)
106 stream_reset(stream
);
107 current_pos
= stream_tell(stream
);
108 if(stream_seek(stream
, pos
)==0) {
109 stream_reset(stream
);
110 stream_seek(stream
, current_pos
);
114 return pos
- stream
->start_pos
;
117 static void list_formats(void) {
119 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "Available lavf input formats:\n");
120 for (fmt
= first_iformat
; fmt
; fmt
= fmt
->next
)
121 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "%15s : %s\n", fmt
->name
, fmt
->long_name
);
124 static int lavf_check_file(demuxer_t
*demuxer
){
125 struct MPOpts
*opts
= demuxer
->opts
;
126 struct lavfdopts
*lavfdopts
= &opts
->lavfdopts
;
129 int probe_data_size
= 0;
130 int read_size
= INITIAL_PROBE_SIZE
;
133 demuxer
->priv
=calloc(sizeof(lavf_priv_t
),1);
138 char *format
= lavfdopts
->format
;
140 format
= demuxer
->stream
->lavf_type
;
142 if (strcmp(format
, "help") == 0) {
146 priv
->avif
= av_find_input_format(format
);
148 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "Unknown lavf format %s\n", format
);
151 mp_msg(MSGT_DEMUX
,MSGL_INFO
,"Forced lavf %s demuxer\n", priv
->avif
->long_name
);
152 return DEMUXER_TYPE_LAVF
;
155 avpd
.buf
= av_mallocz(FFMAX(BIO_BUFFER_SIZE
, PROBE_BUF_SIZE
) +
156 FF_INPUT_BUFFER_PADDING_SIZE
);
158 read_size
= stream_read(demuxer
->stream
, avpd
.buf
+ probe_data_size
, read_size
);
163 probe_data_size
+= read_size
;
164 avpd
.filename
= demuxer
->stream
->url
;
165 if (!strncmp(avpd
.filename
, "ffmpeg://", 9))
167 avpd
.buf_size
= probe_data_size
;
169 priv
->avif
= av_probe_input_format(&avpd
, probe_data_size
> 0);
170 read_size
= FFMIN(2*read_size
, PROBE_BUF_SIZE
- probe_data_size
);
171 } while (demuxer
->desc
->type
!= DEMUXER_TYPE_LAVF_PREFERRED
&&
172 !priv
->avif
&& read_size
> 0 && probe_data_size
< PROBE_BUF_SIZE
);
176 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF_check: no clue about this gibberish!\n");
179 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF_check: %s\n", priv
->avif
->long_name
);
181 return DEMUXER_TYPE_LAVF
;
184 static const char * const preferred_list
[] = {
190 "mov,mp4,m4a,3gp,3g2,mj2",
201 static int lavf_check_preferred_file(demuxer_t
*demuxer
){
202 if (lavf_check_file(demuxer
)) {
203 const char * const *p
= preferred_list
;
204 lavf_priv_t
*priv
= demuxer
->priv
;
206 if (strcmp(*p
, priv
->avif
->name
) == 0)
207 return DEMUXER_TYPE_LAVF_PREFERRED
;
214 static uint8_t char2int(char c
) {
215 if (c
>= '0' && c
<= '9') return c
- '0';
216 if (c
>= 'a' && c
<= 'f') return c
- 'a' + 10;
217 if (c
>= 'A' && c
<= 'F') return c
- 'A' + 10;
221 static void parse_cryptokey(AVFormatContext
*avfc
, const char *str
) {
222 int len
= strlen(str
) / 2;
223 uint8_t *key
= av_mallocz(len
);
227 for (i
= 0; i
< len
; i
++, str
+= 2)
228 *key
++ = (char2int(str
[0]) << 4) | char2int(str
[1]);
231 static void handle_stream(demuxer_t
*demuxer
, AVFormatContext
*avfc
, int i
) {
232 lavf_priv_t
*priv
= demuxer
->priv
;
233 AVStream
*st
= avfc
->streams
[i
];
234 AVCodecContext
*codec
= st
->codec
;
235 AVMetadataTag
*lang
= av_metadata_get(st
->metadata
, "language", NULL
, 0);
236 int g
, override_tag
= mp_av_codec_get_tag(mp_codecid_override_taglists
,
238 // For some formats (like PCM) always trust CODEC_ID_* more than codec_tag
240 codec
->codec_tag
= override_tag
;
242 switch(codec
->codec_type
){
243 case CODEC_TYPE_AUDIO
:{
245 sh_audio_t
* sh_audio
;
246 sh_audio
=new_sh_audio(demuxer
, i
);
247 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "[%s] Audio stream found, -aid %d\n", "lavf", i
);
250 priv
->astreams
[priv
->audio_streams
] = i
;
251 priv
->audio_streams
++;
252 wf
= calloc(sizeof(WAVEFORMATEX
) + codec
->extradata_size
, 1);
253 // mp4a tag is used for all mp4 files no matter what they actually contain
254 if(codec
->codec_tag
== MKTAG('m', 'p', '4', 'a'))
256 if(!codec
->codec_tag
)
257 codec
->codec_tag
= mp_av_codec_get_tag(mp_wav_taglists
, codec
->codec_id
);
258 wf
->wFormatTag
= codec
->codec_tag
;
259 wf
->nChannels
= codec
->channels
;
260 wf
->nSamplesPerSec
= codec
->sample_rate
;
261 wf
->nAvgBytesPerSec
= codec
->bit_rate
/8;
262 wf
->nBlockAlign
= codec
->block_align
? codec
->block_align
: 1;
263 wf
->wBitsPerSample
= codec
->bits_per_coded_sample
;
264 wf
->cbSize
= codec
->extradata_size
;
265 if(codec
->extradata_size
)
266 memcpy(wf
+ 1, codec
->extradata
, codec
->extradata_size
);
268 sh_audio
->audio
.dwSampleSize
= codec
->block_align
;
269 if(codec
->frame_size
&& codec
->sample_rate
){
270 sh_audio
->audio
.dwScale
=codec
->frame_size
;
271 sh_audio
->audio
.dwRate
= codec
->sample_rate
;
273 sh_audio
->audio
.dwScale
= codec
->block_align
? codec
->block_align
*8 : 8;
274 sh_audio
->audio
.dwRate
= codec
->bit_rate
;
276 g
= av_gcd(sh_audio
->audio
.dwScale
, sh_audio
->audio
.dwRate
);
277 sh_audio
->audio
.dwScale
/= g
;
278 sh_audio
->audio
.dwRate
/= g
;
279 // 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);
280 sh_audio
->ds
= demuxer
->audio
;
281 sh_audio
->format
= codec
->codec_tag
;
282 sh_audio
->channels
= codec
->channels
;
283 sh_audio
->samplerate
= codec
->sample_rate
;
284 sh_audio
->i_bps
= codec
->bit_rate
/8;
285 switch (codec
->codec_id
) {
286 case CODEC_ID_PCM_S8
:
287 case CODEC_ID_PCM_U8
:
288 sh_audio
->samplesize
= 1;
290 case CODEC_ID_PCM_S16LE
:
291 case CODEC_ID_PCM_S16BE
:
292 case CODEC_ID_PCM_U16LE
:
293 case CODEC_ID_PCM_U16BE
:
294 sh_audio
->samplesize
= 2;
296 case CODEC_ID_PCM_ALAW
:
297 sh_audio
->format
= 0x6;
299 case CODEC_ID_PCM_MULAW
:
300 sh_audio
->format
= 0x7;
303 if (lang
&& lang
->value
) {
304 sh_audio
->lang
= strdup(lang
->value
);
305 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AID_%d_LANG=%s\n", i
, sh_audio
->lang
);
307 if (st
->disposition
& AV_DISPOSITION_DEFAULT
)
308 sh_audio
->default_track
= 1;
309 if(mp_msg_test(MSGT_HEADER
,MSGL_V
) ) print_wave_header(sh_audio
->wf
, MSGL_V
);
310 // select the first audio stream
311 if (!demuxer
->audio
->sh
) {
312 demuxer
->audio
->id
= i
;
313 demuxer
->audio
->sh
= demuxer
->a_streams
[i
];
315 st
->discard
= AVDISCARD_ALL
;
318 case CODEC_TYPE_VIDEO
:{
319 sh_video_t
* sh_video
;
320 BITMAPINFOHEADER
*bih
;
321 sh_video
=new_sh_video(demuxer
, i
);
322 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "[%s] Video stream found, -vid %d\n", "lavf", i
);
324 priv
->vstreams
[priv
->video_streams
] = i
;
325 priv
->video_streams
++;
326 bih
=calloc(sizeof(BITMAPINFOHEADER
) + codec
->extradata_size
,1);
328 if(codec
->codec_id
== CODEC_ID_RAWVIDEO
) {
329 switch (codec
->pix_fmt
) {
331 codec
->codec_tag
= MKTAG(24, 'B', 'G', 'R');
334 if(!codec
->codec_tag
)
335 codec
->codec_tag
= mp_av_codec_get_tag(mp_bmp_taglists
, codec
->codec_id
);
336 bih
->biSize
= sizeof(BITMAPINFOHEADER
) + codec
->extradata_size
;
337 bih
->biWidth
= codec
->width
;
338 bih
->biHeight
= codec
->height
;
339 bih
->biBitCount
= codec
->bits_per_coded_sample
;
340 bih
->biSizeImage
= bih
->biWidth
* bih
->biHeight
* bih
->biBitCount
/8;
341 bih
->biCompression
= codec
->codec_tag
;
343 sh_video
->disp_w
= codec
->width
;
344 sh_video
->disp_h
= codec
->height
;
345 if (st
->time_base
.den
) { /* if container has time_base, use that */
346 sh_video
->video
.dwRate
= st
->time_base
.den
;
347 sh_video
->video
.dwScale
= st
->time_base
.num
;
349 sh_video
->video
.dwRate
= codec
->time_base
.den
;
350 sh_video
->video
.dwScale
= codec
->time_base
.num
;
352 sh_video
->fps
=av_q2d(st
->r_frame_rate
);
353 sh_video
->frametime
=1/av_q2d(st
->r_frame_rate
);
354 sh_video
->format
=bih
->biCompression
;
355 if(st
->sample_aspect_ratio
.num
)
356 sh_video
->aspect
= codec
->width
* st
->sample_aspect_ratio
.num
357 / (float)(codec
->height
* st
->sample_aspect_ratio
.den
);
359 sh_video
->aspect
=codec
->width
* codec
->sample_aspect_ratio
.num
360 / (float)(codec
->height
* codec
->sample_aspect_ratio
.den
);
361 sh_video
->i_bps
=codec
->bit_rate
/8;
362 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"aspect= %d*%d/(%d*%d)\n",
363 codec
->width
, codec
->sample_aspect_ratio
.num
,
364 codec
->height
, codec
->sample_aspect_ratio
.den
);
366 sh_video
->ds
= demuxer
->video
;
367 if(codec
->extradata_size
)
368 memcpy(sh_video
->bih
+ 1, codec
->extradata
, codec
->extradata_size
);
369 if( mp_msg_test(MSGT_HEADER
,MSGL_V
) ) print_video_header(sh_video
->bih
, MSGL_V
);
377 if(demuxer
->video
->id
!= i
&& demuxer
->video
->id
!= -1)
378 st
->discard
= AVDISCARD_ALL
;
380 demuxer
->video
->id
= i
;
381 demuxer
->video
->sh
= demuxer
->v_streams
[i
];
385 case CODEC_TYPE_SUBTITLE
:{
388 /* only support text subtitles for now */
389 if(codec
->codec_id
== CODEC_ID_TEXT
)
391 else if(codec
->codec_id
== CODEC_ID_MOV_TEXT
)
393 else if(codec
->codec_id
== CODEC_ID_SSA
)
395 else if(codec
->codec_id
== CODEC_ID_DVD_SUBTITLE
)
397 else if(codec
->codec_id
== CODEC_ID_DVB_TELETEXT
)
401 sh_sub
= new_sh_sub_sid(demuxer
, i
, priv
->sub_streams
);
402 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "[%s] Subtitle stream found, -sid %d\n", "lavf", priv
->sub_streams
);
404 priv
->sstreams
[priv
->sub_streams
] = i
;
406 if (codec
->extradata_size
) {
407 sh_sub
->extradata
= malloc(codec
->extradata_size
);
408 memcpy(sh_sub
->extradata
, codec
->extradata
, codec
->extradata_size
);
409 sh_sub
->extradata_len
= codec
->extradata_size
;
411 if (lang
&& lang
->value
) {
412 sh_sub
->lang
= strdup(lang
->value
);
413 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SID_%d_LANG=%s\n", priv
->sub_streams
, sh_sub
->lang
);
415 if (st
->disposition
& AV_DISPOSITION_DEFAULT
)
416 sh_sub
->default_track
= 1;
420 case CODEC_TYPE_ATTACHMENT
:{
421 if (st
->codec
->codec_id
== CODEC_ID_TTF
)
422 demuxer_add_attachment(demuxer
, st
->filename
, INT_MAX
,
423 "application/x-truetype-font", INT_MAX
,
424 codec
->extradata
, codec
->extradata_size
);
428 st
->discard
= AVDISCARD_ALL
;
432 static demuxer_t
* demux_open_lavf(demuxer_t
*demuxer
){
433 struct MPOpts
*opts
= demuxer
->opts
;
434 struct lavfdopts
*lavfdopts
= &opts
->lavfdopts
;
435 AVFormatContext
*avfc
;
436 AVFormatParameters ap
;
438 AVMetadataTag
*t
= NULL
;
439 lavf_priv_t
*priv
= demuxer
->priv
;
441 char mp_filename
[256]="mp:";
443 memset(&ap
, 0, sizeof(AVFormatParameters
));
445 stream_seek(demuxer
->stream
, 0);
447 avfc
= avformat_alloc_context();
449 if (lavfdopts
->cryptokey
)
450 parse_cryptokey(avfc
, lavfdopts
->cryptokey
);
451 if (opts
->user_correct_pts
!= 0)
452 avfc
->flags
|= AVFMT_FLAG_GENPTS
;
454 avfc
->flags
|= AVFMT_FLAG_IGNIDX
;
456 ap
.prealloced_context
= 1;
457 if (lavfdopts
->probesize
) {
458 opt
= av_set_int(avfc
, "probesize", lavfdopts
->probesize
);
459 if(!opt
) mp_msg(MSGT_HEADER
,MSGL_ERR
, "demux_lavf, couldn't set option probesize to %u\n", lavfdopts
->probesize
);
461 if (lavfdopts
->analyzeduration
) {
462 opt
= av_set_int(avfc
, "analyzeduration",
463 lavfdopts
->analyzeduration
* AV_TIME_BASE
);
465 mp_msg(MSGT_HEADER
, MSGL_ERR
, "demux_lavf, couldn't set option "
466 "analyzeduration to %u\n", lavfdopts
->analyzeduration
);
469 if (lavfdopts
->avopt
){
470 if(parse_avopts(avfc
, lavfdopts
->avopt
) < 0){
471 mp_msg(MSGT_HEADER
,MSGL_ERR
, "Your options /%s/ look like gibberish to me pal\n", lavfdopts
->avopt
);
476 if(demuxer
->stream
->url
) {
477 if (!strncmp(demuxer
->stream
->url
, "ffmpeg://rtsp:", 14))
478 av_strlcpy(mp_filename
, demuxer
->stream
->url
+ 9, sizeof(mp_filename
));
480 av_strlcat(mp_filename
, demuxer
->stream
->url
, sizeof(mp_filename
));
482 av_strlcat(mp_filename
, "foobar.dummy", sizeof(mp_filename
));
484 priv
->pb
= av_alloc_put_byte(priv
->buffer
, BIO_BUFFER_SIZE
, 0,
485 demuxer
->stream
, mp_read
, NULL
, mp_seek
);
486 priv
->pb
->is_streamed
= !demuxer
->stream
->end_pos
|| (demuxer
->stream
->flags
& MP_STREAM_SEEK
) != MP_STREAM_SEEK
;
488 if(av_open_input_stream(&avfc
, priv
->pb
, mp_filename
, priv
->avif
, &ap
)<0){
489 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF_header: av_open_input_stream() failed\n");
495 if(av_find_stream_info(avfc
) < 0){
496 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF_header: av_find_stream_info() failed\n");
501 av_metadata_conv(avfc
, NULL
, avfc
->iformat
->metadata_conv
);
502 while((t
= av_metadata_get(avfc
->metadata
, "", t
, AV_METADATA_IGNORE_SUFFIX
)))
503 demux_info_add(demuxer
, t
->key
, t
->value
);
505 for(i
=0; i
< avfc
->nb_chapters
; i
++) {
506 AVChapter
*c
= avfc
->chapters
[i
];
507 uint64_t start
= av_rescale_q(c
->start
, c
->time_base
, (AVRational
){1,1000});
508 uint64_t end
= av_rescale_q(c
->end
, c
->time_base
, (AVRational
){1,1000});
509 t
= av_metadata_get(c
->metadata
, "title", NULL
, 0);
510 demuxer_add_chapter(demuxer
, t
? t
->value
: NULL
, INT_MAX
, start
, end
);
513 for(i
=0; i
<avfc
->nb_streams
; i
++)
514 handle_stream(demuxer
, avfc
, i
);
515 if(avfc
->nb_programs
) {
517 for (p
= 0; p
< avfc
->nb_programs
; p
++) {
518 AVProgram
*program
= avfc
->programs
[p
];
519 t
= av_metadata_get(program
->metadata
, "title", NULL
, 0);
520 mp_msg(MSGT_HEADER
,MSGL_INFO
,"LAVF: Program %d %s\n", program
->id
, t
? t
->value
: "");
524 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF: %d audio and %d video streams found\n",priv
->audio_streams
,priv
->video_streams
);
525 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF: build %d\n", LIBAVFORMAT_BUILD
);
526 if(!priv
->audio_streams
) demuxer
->audio
->id
=-2; // nosound
527 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio;
528 if(!priv
->video_streams
){
529 if(!priv
->audio_streams
){
530 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF: no audio or video headers found - broken file?\n");
533 demuxer
->video
->id
=-2; // audio-only
534 } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video;
536 demuxer
->accurate_seek
= true;
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
) {
615 if (rel_seek_secs
< 0) avsflags
= AVSEEK_FLAG_BACKWARD
;
617 if (flags
& SEEK_FORWARD
)
619 else if (flags
& SEEK_BACKWARD
)
620 avsflags
= AVSEEK_FLAG_BACKWARD
;
621 if (flags
& SEEK_FACTOR
) {
622 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
624 priv
->last_pts
+= rel_seek_secs
* priv
->avfc
->duration
;
626 priv
->last_pts
+= rel_seek_secs
* AV_TIME_BASE
;
628 if (av_seek_frame(priv
->avfc
, -1, priv
->last_pts
, avsflags
) < 0) {
629 avsflags
^= AVSEEK_FLAG_BACKWARD
;
630 av_seek_frame(priv
->avfc
, -1, priv
->last_pts
, avsflags
);
634 static int demux_lavf_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
636 lavf_priv_t
*priv
= demuxer
->priv
;
639 case DEMUXER_CTRL_CORRECT_PTS
:
640 return DEMUXER_CTRL_OK
;
641 case DEMUXER_CTRL_GET_TIME_LENGTH
:
642 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
643 return DEMUXER_CTRL_DONTKNOW
;
645 *((double *)arg
) = (double)priv
->avfc
->duration
/ AV_TIME_BASE
;
646 return DEMUXER_CTRL_OK
;
648 case DEMUXER_CTRL_GET_PERCENT_POS
:
649 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
650 return DEMUXER_CTRL_DONTKNOW
;
652 *((int *)arg
) = (int)((priv
->last_pts
- priv
->avfc
->start_time
)*100 / priv
->avfc
->duration
);
653 return DEMUXER_CTRL_OK
;
654 case DEMUXER_CTRL_SWITCH_AUDIO
:
655 case DEMUXER_CTRL_SWITCH_VIDEO
:
657 int id
= *((int*)arg
);
660 int nstreams
, *pstreams
;
663 if(cmd
== DEMUXER_CTRL_SWITCH_VIDEO
)
666 nstreams
= priv
->video_streams
;
667 pstreams
= priv
->vstreams
;
672 nstreams
= priv
->audio_streams
;
673 pstreams
= priv
->astreams
;
675 for(i
= 0; i
< nstreams
; i
++)
677 if(pstreams
[i
] == ds
->id
) //current stream id
684 if(id
== -2) { // no sound
686 } else if(id
== -1) { // next track
687 i
= (curridx
+ 2) % (nstreams
+ 1) - 1;
691 else // select track by id
693 for(i
= 0; i
< nstreams
; i
++)
695 if(pstreams
[i
] == id
)
703 return DEMUXER_CTRL_NOTIMPL
;
708 priv
->avfc
->streams
[ds
->id
]->discard
= AVDISCARD_ALL
;
709 *((int*)arg
) = ds
->id
= newid
;
711 priv
->avfc
->streams
[newid
]->discard
= AVDISCARD_NONE
;
712 return DEMUXER_CTRL_OK
;
715 case DEMUXER_CTRL_IDENTIFY_PROGRAM
:
717 demux_program_t
*prog
= arg
;
722 prog
->vid
= prog
->aid
= prog
->sid
= -2; //no audio and no video by default
723 if(priv
->avfc
->nb_programs
< 1)
724 return DEMUXER_CTRL_DONTKNOW
;
726 if(prog
->progid
== -1)
729 while(p
<priv
->avfc
->nb_programs
&& priv
->avfc
->programs
[p
]->id
!= priv
->cur_program
)
731 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
735 for(i
=0; i
<priv
->avfc
->nb_programs
; i
++)
736 if(priv
->avfc
->programs
[i
]->id
== prog
->progid
)
738 if(i
==priv
->avfc
->nb_programs
)
739 return DEMUXER_CTRL_DONTKNOW
;
744 program
= priv
->avfc
->programs
[p
];
745 for(i
=0; i
<program
->nb_stream_indexes
; i
++)
747 switch(priv
->avfc
->streams
[program
->stream_index
[i
]]->codec
->codec_type
)
749 case CODEC_TYPE_VIDEO
:
751 prog
->vid
= program
->stream_index
[i
];
753 case CODEC_TYPE_AUDIO
:
755 prog
->aid
= program
->stream_index
[i
];
757 case CODEC_TYPE_SUBTITLE
:
758 if(prog
->sid
== -2 && priv
->avfc
->streams
[program
->stream_index
[i
]]->codec
->codec_id
== CODEC_ID_TEXT
)
759 prog
->sid
= program
->stream_index
[i
];
763 if(prog
->progid
== -1 && prog
->vid
== -2 && prog
->aid
== -2)
765 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
767 return DEMUXER_CTRL_DONTKNOW
;
770 priv
->cur_program
= prog
->progid
= program
->id
;
771 return DEMUXER_CTRL_OK
;
774 return DEMUXER_CTRL_NOTIMPL
;
778 static void demux_close_lavf(demuxer_t
*demuxer
)
780 lavf_priv_t
* priv
= demuxer
->priv
;
784 av_freep(&priv
->avfc
->key
);
785 av_close_input_stream(priv
->avfc
);
788 free(priv
); demuxer
->priv
= NULL
;
793 const demuxer_desc_t demuxer_desc_lavf
= {
794 "libavformat demuxer",
797 "Michael Niedermayer",
798 "supports many formats, requires libavformat",
800 0, // Check after other demuxer
802 demux_lavf_fill_buffer
,
809 const demuxer_desc_t demuxer_desc_lavf_preferred
= {
810 "libavformat preferred demuxer",
813 "Michael Niedermayer",
814 "supports many formats, requires libavformat",
815 DEMUXER_TYPE_LAVF_PREFERRED
,
817 lavf_check_preferred_file
,
818 demux_lavf_fill_buffer
,