2 Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 // #include <unistd.h>
28 #include "stream/stream.h"
32 #include "libvo/sub.h"
34 #ifdef USE_LIBAVFORMAT_SO
35 #include <ffmpeg/avformat.h>
36 #include <ffmpeg/avutil.h>
37 #include <ffmpeg/opt.h>
46 #define PROBE_BUF_SIZE 2048
48 extern char *audio_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
;
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 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
63 #define BIO_BUFFER_SIZE 32768
65 typedef struct lavf_priv_t
{
67 AVFormatContext
*avfc
;
69 uint8_t buffer
[BIO_BUFFER_SIZE
];
74 int astreams
[MAX_A_STREAMS
];
75 int vstreams
[MAX_V_STREAMS
];
76 int sstreams
[MAX_S_STREAMS
];
80 extern void print_wave_header(WAVEFORMATEX
*h
, int verbose_level
);
81 extern void print_video_header(BITMAPINFOHEADER
*h
, int verbose_level
);
83 static const AVCodecTag mp_wav_tags
[] = {
84 { CODEC_ID_ADPCM_4XM
, MKTAG('4', 'X', 'M', 'A')},
85 { CODEC_ID_ADPCM_EA
, MKTAG('A', 'D', 'E', 'A')},
86 { CODEC_ID_ADPCM_IMA_WS
, MKTAG('A', 'I', 'W', 'S')},
87 { CODEC_ID_ADPCM_THP
, MKTAG('T', 'H', 'P', 'A')},
88 { CODEC_ID_AMR_NB
, MKTAG('n', 'b', 0, 0)},
89 { CODEC_ID_COOK
, MKTAG('c', 'o', 'o', 'k')},
90 { CODEC_ID_DSICINAUDIO
, MKTAG('D', 'C', 'I', 'A')},
91 { CODEC_ID_INTERPLAY_DPCM
, MKTAG('I', 'N', 'P', 'A')},
92 { CODEC_ID_MUSEPACK7
, MKTAG('M', 'P', 'C', ' ')},
93 { CODEC_ID_MUSEPACK8
, MKTAG('M', 'P', 'C', '8')},
94 { CODEC_ID_NELLYMOSER
, MKTAG('N', 'E', 'L', 'L')},
95 { CODEC_ID_PCM_S24BE
, MKTAG('i', 'n', '2', '4')},
96 { CODEC_ID_PCM_S16BE
, MKTAG('t', 'w', 'o', 's')},
97 { CODEC_ID_PCM_S8
, MKTAG('t', 'w', 'o', 's')},
98 { CODEC_ID_ROQ_DPCM
, MKTAG('R', 'o', 'Q', 'A')},
99 { CODEC_ID_SHORTEN
, MKTAG('s', 'h', 'r', 'n')},
100 { CODEC_ID_TTA
, MKTAG('T', 'T', 'A', '1')},
101 { CODEC_ID_WAVPACK
, MKTAG('W', 'V', 'P', 'K')},
102 { CODEC_ID_WESTWOOD_SND1
, MKTAG('S', 'N', 'D', '1')},
103 { CODEC_ID_XAN_DPCM
, MKTAG('A', 'x', 'a', 'n')},
107 const struct AVCodecTag
*mp_wav_taglists
[] = {codec_wav_tags
, mp_wav_tags
, 0};
109 static const AVCodecTag mp_bmp_tags
[] = {
110 { CODEC_ID_AMV
, MKTAG('A', 'M', 'V', 'V')},
111 { CODEC_ID_BETHSOFTVID
, MKTAG('B', 'E', 'T', 'H')},
112 { CODEC_ID_C93
, MKTAG('C', '9', '3', 'V')},
113 { CODEC_ID_DSICINVIDEO
, MKTAG('D', 'C', 'I', 'V')},
114 { CODEC_ID_DXA
, MKTAG('D', 'X', 'A', '1')},
115 { CODEC_ID_FLIC
, MKTAG('F', 'L', 'I', 'C')},
116 { CODEC_ID_IDCIN
, MKTAG('I', 'D', 'C', 'I')},
117 { CODEC_ID_INTERPLAY_VIDEO
, MKTAG('I', 'N', 'P', 'V')},
118 { CODEC_ID_ROQ
, MKTAG('R', 'o', 'Q', 'V')},
119 { CODEC_ID_THP
, MKTAG('T', 'H', 'P', 'V')},
120 { CODEC_ID_TIERTEXSEQVIDEO
, MKTAG('T', 'S', 'E', 'Q')},
121 { CODEC_ID_TXD
, MKTAG('T', 'X', 'D', 'V')},
122 { CODEC_ID_VMDVIDEO
, MKTAG('V', 'M', 'D', 'V')},
123 { CODEC_ID_WS_VQA
, MKTAG('V', 'Q', 'A', 'V')},
124 { CODEC_ID_XAN_WC3
, MKTAG('W', 'C', '3', 'V')},
125 { CODEC_ID_NUV
, MKTAG('N', 'U', 'V', '1')},
129 const struct AVCodecTag
*mp_bmp_taglists
[] = {codec_bmp_tags
, mp_bmp_tags
, 0};
131 static int mp_read(void *opaque
, uint8_t *buf
, int size
) {
132 stream_t
*stream
= opaque
;
135 if(stream_eof(stream
)) //needed?
137 ret
=stream_read(stream
, buf
, size
);
139 mp_msg(MSGT_HEADER
,MSGL_DBG2
,"%d=mp_read(%p, %p, %d), eof:%d\n", ret
, stream
, buf
, size
, stream
->eof
);
143 static offset_t
mp_seek(void *opaque
, offset_t pos
, int whence
) {
144 stream_t
*stream
= opaque
;
145 mp_msg(MSGT_HEADER
,MSGL_DBG2
,"mp_seek(%p, %d, %d)\n", stream
, (int)pos
, whence
);
146 if(whence
== SEEK_CUR
)
147 pos
+=stream_tell(stream
);
148 else if(whence
== SEEK_END
&& stream
->end_pos
> 0)
149 pos
+= stream
->end_pos
;
150 else if(whence
== SEEK_SET
)
151 pos
+= stream
->start_pos
;
152 else if(whence
== AVSEEK_SIZE
&& stream
->end_pos
> 0)
153 return stream
->end_pos
- stream
->start_pos
;
159 if(pos
<stream
->end_pos
&& stream
->eof
)
160 stream_reset(stream
);
161 if(stream_seek(stream
, pos
)==0)
164 return pos
- stream
->start_pos
;
167 static void list_formats(void) {
169 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "Available lavf input formats:\n");
170 for (fmt
= first_iformat
; fmt
; fmt
= fmt
->next
)
171 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "%15s : %s\n", fmt
->name
, fmt
->long_name
);
174 static int lavf_check_file(demuxer_t
*demuxer
){
176 uint8_t buf
[PROBE_BUF_SIZE
];
180 demuxer
->priv
=calloc(sizeof(lavf_priv_t
),1);
185 if(stream_read(demuxer
->stream
, buf
, PROBE_BUF_SIZE
)!=PROBE_BUF_SIZE
)
187 avpd
.filename
= demuxer
->stream
->url
;
189 avpd
.buf_size
= PROBE_BUF_SIZE
;
192 if (strcmp(opt_format
, "help") == 0) {
196 priv
->avif
= av_find_input_format(opt_format
);
198 mp_msg(MSGT_DEMUX
,MSGL_FATAL
,"Unknown lavf format %s\n", opt_format
);
201 mp_msg(MSGT_DEMUX
,MSGL_INFO
,"Forced lavf %s demuxer\n", priv
->avif
->long_name
);
202 return DEMUXER_TYPE_LAVF
;
204 priv
->avif
= av_probe_input_format(&avpd
, 1);
206 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF_check: no clue about this gibberish!\n");
209 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF_check: %s\n", priv
->avif
->long_name
);
211 return DEMUXER_TYPE_LAVF
;
214 static const char *preferred_list
[] = {
226 static int lavf_check_preferred_file(demuxer_t
*demuxer
){
227 if (lavf_check_file(demuxer
)) {
228 char **p
= preferred_list
;
229 lavf_priv_t
*priv
= demuxer
->priv
;
231 if (strcmp(*p
, priv
->avif
->name
) == 0)
232 return DEMUXER_TYPE_LAVF_PREFERRED
;
239 static uint8_t char2int(char c
) {
240 if (c
>= '0' && c
<= '9') return c
- '0';
241 if (c
>= 'a' && c
<= 'f') return c
- 'a' + 10;
242 if (c
>= 'A' && c
<= 'F') return c
- 'A' + 10;
246 static void parse_cryptokey(AVFormatContext
*avfc
, const char *str
) {
247 int len
= strlen(str
) / 2;
248 uint8_t *key
= av_mallocz(len
);
252 for (i
= 0; i
< len
; i
++, str
+= 2)
253 *key
++ = (char2int(str
[0]) << 4) | char2int(str
[1]);
256 static void handle_stream(demuxer_t
*demuxer
, AVFormatContext
*avfc
, int i
) {
257 lavf_priv_t
*priv
= demuxer
->priv
;
258 AVStream
*st
= avfc
->streams
[i
];
259 AVCodecContext
*codec
= st
->codec
;
262 switch(codec
->codec_type
){
263 case CODEC_TYPE_AUDIO
:{
264 WAVEFORMATEX
*wf
= calloc(sizeof(WAVEFORMATEX
) + codec
->extradata_size
, 1);
265 sh_audio_t
* sh_audio
;
266 if(priv
->audio_streams
>= MAX_A_STREAMS
)
268 sh_audio
=new_sh_audio(demuxer
, i
);
269 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_AudioID
, "lavf", i
);
272 priv
->astreams
[priv
->audio_streams
] = i
;
273 priv
->audio_streams
++;
274 // mp4a tag is used for all mp4 files no matter what they actually contain
275 if(codec
->codec_tag
== MKTAG('m', 'p', '4', 'a'))
277 if(codec
->codec_id
== CODEC_ID_ADPCM_IMA_AMV
)
278 codec
->codec_tag
= MKTAG('A','M','V','A');
279 if(!codec
->codec_tag
)
280 codec
->codec_tag
= av_codec_get_tag(mp_wav_taglists
, codec
->codec_id
);
281 wf
->wFormatTag
= codec
->codec_tag
;
282 wf
->nChannels
= codec
->channels
;
283 wf
->nSamplesPerSec
= codec
->sample_rate
;
284 wf
->nAvgBytesPerSec
= codec
->bit_rate
/8;
285 wf
->nBlockAlign
= codec
->block_align
? codec
->block_align
: 1;
286 wf
->wBitsPerSample
= codec
->bits_per_sample
;
287 wf
->cbSize
= codec
->extradata_size
;
288 if(codec
->extradata_size
)
289 memcpy(wf
+ 1, codec
->extradata
, codec
->extradata_size
);
291 sh_audio
->audio
.dwSampleSize
= codec
->block_align
;
292 if(codec
->frame_size
&& codec
->sample_rate
){
293 sh_audio
->audio
.dwScale
=codec
->frame_size
;
294 sh_audio
->audio
.dwRate
= codec
->sample_rate
;
296 sh_audio
->audio
.dwScale
= codec
->block_align
? codec
->block_align
*8 : 8;
297 sh_audio
->audio
.dwRate
= codec
->bit_rate
;
299 g
= ff_gcd(sh_audio
->audio
.dwScale
, sh_audio
->audio
.dwRate
);
300 sh_audio
->audio
.dwScale
/= g
;
301 sh_audio
->audio
.dwRate
/= g
;
302 // 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);
303 sh_audio
->ds
= demuxer
->audio
;
304 sh_audio
->format
= codec
->codec_tag
;
305 sh_audio
->channels
= codec
->channels
;
306 sh_audio
->samplerate
= codec
->sample_rate
;
307 sh_audio
->i_bps
= codec
->bit_rate
/8;
308 switch (codec
->codec_id
) {
309 case CODEC_ID_PCM_S8
:
310 case CODEC_ID_PCM_U8
:
311 sh_audio
->samplesize
= 1;
313 case CODEC_ID_PCM_S16LE
:
314 case CODEC_ID_PCM_S16BE
:
315 case CODEC_ID_PCM_U16LE
:
316 case CODEC_ID_PCM_U16BE
:
317 sh_audio
->samplesize
= 2;
319 case CODEC_ID_PCM_ALAW
:
320 sh_audio
->format
= 0x6;
322 case CODEC_ID_PCM_MULAW
:
323 sh_audio
->format
= 0x7;
326 if(mp_msg_test(MSGT_HEADER
,MSGL_V
) ) print_wave_header(sh_audio
->wf
, MSGL_V
);
327 if((audio_lang
&& st
->language
[0] && !strncmp(audio_lang
, st
->language
, 3))
328 || (demuxer
->audio
->id
== i
|| demuxer
->audio
->id
== -1)) {
329 demuxer
->audio
->id
= i
;
330 demuxer
->audio
->sh
= demuxer
->a_streams
[i
];
332 st
->discard
= AVDISCARD_ALL
;
335 case CODEC_TYPE_VIDEO
:{
336 sh_video_t
* sh_video
;
337 BITMAPINFOHEADER
*bih
;
338 if(priv
->video_streams
>= MAX_V_STREAMS
)
340 sh_video
=new_sh_video(demuxer
, i
);
341 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_VideoID
, "lavf", i
);
343 priv
->vstreams
[priv
->video_streams
] = i
;
344 priv
->video_streams
++;
345 bih
=calloc(sizeof(BITMAPINFOHEADER
) + codec
->extradata_size
,1);
347 if(!codec
->codec_tag
)
348 codec
->codec_tag
= av_codec_get_tag(mp_bmp_taglists
, codec
->codec_id
);
349 bih
->biSize
= sizeof(BITMAPINFOHEADER
) + codec
->extradata_size
;
350 bih
->biWidth
= codec
->width
;
351 bih
->biHeight
= codec
->height
;
352 bih
->biBitCount
= codec
->bits_per_sample
;
353 bih
->biSizeImage
= bih
->biWidth
* bih
->biHeight
* bih
->biBitCount
/8;
354 bih
->biCompression
= codec
->codec_tag
;
356 sh_video
->disp_w
= codec
->width
;
357 sh_video
->disp_h
= codec
->height
;
358 if (st
->time_base
.den
) { /* if container has time_base, use that */
359 sh_video
->video
.dwRate
= st
->time_base
.den
;
360 sh_video
->video
.dwScale
= st
->time_base
.num
;
362 sh_video
->video
.dwRate
= codec
->time_base
.den
;
363 sh_video
->video
.dwScale
= codec
->time_base
.num
;
365 sh_video
->fps
=av_q2d(st
->r_frame_rate
);
366 sh_video
->frametime
=1/av_q2d(st
->r_frame_rate
);
367 sh_video
->format
=bih
->biCompression
;
368 sh_video
->aspect
=codec
->width
* codec
->sample_aspect_ratio
.num
369 / (float)(codec
->height
* codec
->sample_aspect_ratio
.den
);
370 sh_video
->i_bps
=codec
->bit_rate
/8;
371 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"aspect= %d*%d/(%d*%d)\n",
372 codec
->width
, codec
->sample_aspect_ratio
.num
,
373 codec
->height
, codec
->sample_aspect_ratio
.den
);
375 sh_video
->ds
= demuxer
->video
;
376 if(codec
->extradata_size
)
377 memcpy(sh_video
->bih
+ 1, codec
->extradata
, codec
->extradata_size
);
378 if( mp_msg_test(MSGT_HEADER
,MSGL_V
) ) print_video_header(sh_video
->bih
, MSGL_V
);
386 if(demuxer
->video
->id
!= i
&& demuxer
->video
->id
!= -1)
387 st
->discard
= AVDISCARD_ALL
;
389 demuxer
->video
->id
= i
;
390 demuxer
->video
->sh
= demuxer
->v_streams
[i
];
394 case CODEC_TYPE_SUBTITLE
:{
397 if(priv
->sub_streams
>= MAX_S_STREAMS
)
399 /* only support text subtitles for now */
400 if(codec
->codec_id
== CODEC_ID_TEXT
)
402 else if(codec
->codec_id
== CODEC_ID_SSA
)
406 sh_sub
= new_sh_sub_sid(demuxer
, i
, priv
->sub_streams
);
407 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_SubtitleID
, "lavf", priv
->sub_streams
);
409 priv
->sstreams
[priv
->sub_streams
] = i
;
411 if (codec
->extradata_size
) {
412 sh_sub
->extradata
= malloc(codec
->extradata_size
);
413 memcpy(sh_sub
->extradata
, codec
->extradata
, codec
->extradata_size
);
414 sh_sub
->extradata_len
= codec
->extradata_size
;
416 demuxer
->sub
->sh
= demuxer
->s_streams
[priv
->sub_streams
++];
420 st
->discard
= AVDISCARD_ALL
;
424 static demuxer_t
* demux_open_lavf(demuxer_t
*demuxer
){
425 AVFormatContext
*avfc
;
426 AVFormatParameters ap
;
428 lavf_priv_t
*priv
= demuxer
->priv
;
430 char mp_filename
[256]="mp:";
432 memset(&ap
, 0, sizeof(AVFormatParameters
));
434 stream_seek(demuxer
->stream
, 0);
436 avfc
= av_alloc_format_context();
439 parse_cryptokey(avfc
, opt_cryptokey
);
441 avfc
->flags
|= AVFMT_FLAG_GENPTS
;
443 avfc
->flags
|= AVFMT_FLAG_IGNIDX
;
445 ap
.prealloced_context
= 1;
447 opt
= av_set_int(avfc
, "probesize", opt_probesize
);
448 if(!opt
) mp_msg(MSGT_HEADER
,MSGL_ERR
, "demux_lavf, couldn't set option probesize to %u\n", opt_probesize
);
450 if(opt_analyzeduration
) {
451 opt
= av_set_int(avfc
, "analyzeduration", opt_analyzeduration
* AV_TIME_BASE
);
452 if(!opt
) mp_msg(MSGT_HEADER
,MSGL_ERR
, "demux_lavf, couldn't set option analyzeduration to %u\n", opt_analyzeduration
);
455 if(demuxer
->stream
->url
)
456 strncpy(mp_filename
+ 3, demuxer
->stream
->url
, sizeof(mp_filename
)-3);
458 strncpy(mp_filename
+ 3, "foobar.dummy", sizeof(mp_filename
)-3);
460 priv
->pb
= av_alloc_put_byte(priv
->buffer
, BIO_BUFFER_SIZE
, 0,
461 demuxer
->stream
, mp_read
, NULL
, mp_seek
);
462 priv
->pb
->is_streamed
= !demuxer
->stream
->end_pos
;
464 if(av_open_input_stream(&avfc
, priv
->pb
, mp_filename
, priv
->avif
, &ap
)<0){
465 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF_header: av_open_input_stream() failed\n");
471 if(av_find_stream_info(avfc
) < 0){
472 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF_header: av_find_stream_info() failed\n");
476 if(avfc
->title
[0]) demux_info_add(demuxer
, "name" , avfc
->title
);
477 if(avfc
->author
[0]) demux_info_add(demuxer
, "author" , avfc
->author
);
478 if(avfc
->copyright
[0]) demux_info_add(demuxer
, "copyright", avfc
->copyright
);
479 if(avfc
->comment
[0]) demux_info_add(demuxer
, "comments" , avfc
->comment
);
480 if(avfc
->album
[0]) demux_info_add(demuxer
, "album" , avfc
->album
);
481 // if(avfc->year ) demux_info_add(demuxer, "year" , avfc->year );
482 // if(avfc->track ) demux_info_add(demuxer, "track" , avfc->track );
483 if(avfc
->genre
[0]) demux_info_add(demuxer
, "genre" , avfc
->genre
);
485 if(avfc
->nb_programs
) {
486 int p
, start
=0, found
=0;
489 for(p
=0; p
<avfc
->nb_programs
; p
++) {
490 if(avfc
->programs
[p
]->id
== ts_prog
) {
497 mp_msg(MSGT_HEADER
,MSGL_ERR
,"DEMUX_LAVF: program %d doesn't seem to be present\n", ts_prog
);
503 AVProgram
*program
= avfc
->programs
[p
];
504 mp_msg(MSGT_HEADER
,MSGL_INFO
,"LAVF: Program %d %s\n", program
->id
, (program
->name
? program
->name
: ""));
505 for(i
=0; i
<program
->nb_stream_indexes
; i
++)
506 handle_stream(demuxer
, avfc
, program
->stream_index
[i
]);
507 if(!priv
->cur_program
&& (demuxer
->video
->sh
|| demuxer
->audio
->sh
))
508 priv
->cur_program
= program
->id
;
509 p
= (p
+ 1) % avfc
->nb_programs
;
512 for(i
=0; i
<avfc
->nb_streams
; i
++)
513 handle_stream(demuxer
, avfc
, i
);
515 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF: %d audio and %d video streams found\n",priv
->audio_streams
,priv
->video_streams
);
516 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF: build %d\n", LIBAVFORMAT_BUILD
);
517 if(!priv
->audio_streams
) demuxer
->audio
->id
=-2; // nosound
518 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio;
519 if(!priv
->video_streams
){
520 if(!priv
->audio_streams
){
521 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF: no audio or video headers found - broken file?\n");
524 demuxer
->video
->id
=-2; // audio-only
525 } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video;
530 static int demux_lavf_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
){
531 lavf_priv_t
*priv
= demux
->priv
;
536 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"demux_lavf_fill_buffer()\n");
538 demux
->filepos
=stream_tell(demux
->stream
);
540 if(av_read_frame(priv
->avfc
, &pkt
) < 0)
543 id
= pkt
.stream_index
;
545 if(id
==demux
->audio
->id
){
549 ds
->sh
=demux
->a_streams
[id
];
550 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected LAVF audio ID = %d\n",ds
->id
);
552 } else if(id
==demux
->video
->id
){
556 ds
->sh
=demux
->v_streams
[id
];
557 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected LAVF video ID = %d\n",ds
->id
);
559 } else if(id
==demux
->sub
->id
){
564 av_free_packet(&pkt
);
568 if(0/*pkt.destruct == av_destruct_packet*/){
569 //ok kids, dont try this at home :)
570 dp
=malloc(sizeof(demux_packet_t
));
578 dp
=new_demux_packet(pkt
.size
);
579 memcpy(dp
->buffer
, pkt
.data
, pkt
.size
);
580 av_free_packet(&pkt
);
583 if(pkt
.pts
!= AV_NOPTS_VALUE
){
584 dp
->pts
=pkt
.pts
* av_q2d(priv
->avfc
->streams
[id
]->time_base
);
585 priv
->last_pts
= dp
->pts
* AV_TIME_BASE
;
587 dp
->endpts
= dp
->pts
+ pkt
.duration
* av_q2d(priv
->avfc
->streams
[id
]->time_base
);
589 dp
->pos
=demux
->filepos
;
590 dp
->flags
= !!(pkt
.flags
&PKT_FLAG_KEY
);
591 // append packet to DS stream:
592 ds_add_packet(ds
,dp
);
596 static void demux_seek_lavf(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
, int flags
){
597 lavf_priv_t
*priv
= demuxer
->priv
;
599 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"demux_seek_lavf(%p, %f, %f, %d)\n", demuxer
, rel_seek_secs
, audio_delay
, flags
);
601 if (flags
& 1) // absolute seek
602 priv
->last_pts
= priv
->avfc
->start_time
;
603 if (flags
& 2) { // percent seek
604 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
606 priv
->last_pts
+= rel_seek_secs
* priv
->avfc
->duration
;
608 priv
->last_pts
+= rel_seek_secs
* AV_TIME_BASE
;
609 if (rel_seek_secs
< 0) avsflags
= AVSEEK_FLAG_BACKWARD
;
611 av_seek_frame(priv
->avfc
, -1, priv
->last_pts
, avsflags
);
614 static int demux_lavf_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
616 lavf_priv_t
*priv
= demuxer
->priv
;
619 case DEMUXER_CTRL_GET_TIME_LENGTH
:
620 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
621 return DEMUXER_CTRL_DONTKNOW
;
623 *((double *)arg
) = (double)priv
->avfc
->duration
/ AV_TIME_BASE
;
624 return DEMUXER_CTRL_OK
;
626 case DEMUXER_CTRL_GET_PERCENT_POS
:
627 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
628 return DEMUXER_CTRL_DONTKNOW
;
630 *((int *)arg
) = (int)((priv
->last_pts
- priv
->avfc
->start_time
)*100 / priv
->avfc
->duration
);
631 return DEMUXER_CTRL_OK
;
632 case DEMUXER_CTRL_SWITCH_AUDIO
:
633 case DEMUXER_CTRL_SWITCH_VIDEO
:
635 int id
= *((int*)arg
);
638 int nstreams
, *pstreams
;
641 if(cmd
== DEMUXER_CTRL_SWITCH_VIDEO
)
644 nstreams
= priv
->video_streams
;
645 pstreams
= priv
->vstreams
;
650 nstreams
= priv
->audio_streams
;
651 pstreams
= priv
->astreams
;
656 priv
->avfc
->streams
[ds
->id
]->discard
= AVDISCARD_ALL
;
658 *((int*)arg
) = ds
->id
= -2;
659 return DEMUXER_CTRL_OK
;
661 for(i
= 0; i
< nstreams
; i
++)
663 if(pstreams
[i
] == ds
->id
) //current stream id
672 i
= (curridx
+ 1) % nstreams
;
677 for(i
= 0; i
< nstreams
; i
++)
679 if(pstreams
[i
] == id
)
687 return DEMUXER_CTRL_NOTIMPL
;
692 priv
->avfc
->streams
[ds
->id
]->discard
= AVDISCARD_ALL
;
693 *((int*)arg
) = ds
->id
= newid
;
695 priv
->avfc
->streams
[newid
]->discard
= AVDISCARD_NONE
;
696 return DEMUXER_CTRL_OK
;
699 case DEMUXER_CTRL_IDENTIFY_PROGRAM
:
701 demux_program_t
*prog
= arg
;
705 if(priv
->avfc
->nb_programs
< 2)
706 return DEMUXER_CTRL_NOTIMPL
;
708 if(prog
->progid
== -1)
711 while(p
<priv
->avfc
->nb_programs
&& priv
->avfc
->programs
[p
]->id
!= priv
->cur_program
)
713 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
717 for(i
=0; i
<priv
->avfc
->nb_programs
; i
++)
718 if(priv
->avfc
->programs
[i
]->id
== prog
->progid
)
720 if(i
==priv
->avfc
->nb_programs
)
721 return DEMUXER_CTRL_NOTIMPL
;
724 prog
->vid
= prog
->aid
= prog
->sid
= -2; //no audio and no video by default
726 program
= priv
->avfc
->programs
[p
];
727 for(i
=0; i
<program
->nb_stream_indexes
; i
++)
729 switch(priv
->avfc
->streams
[program
->stream_index
[i
]]->codec
->codec_type
)
731 case CODEC_TYPE_VIDEO
:
733 prog
->vid
= program
->stream_index
[i
];
735 case CODEC_TYPE_AUDIO
:
737 prog
->aid
= program
->stream_index
[i
];
739 case CODEC_TYPE_SUBTITLE
:
740 if(prog
->sid
== -2 && priv
->avfc
->streams
[program
->stream_index
[i
]]->codec
->codec_id
== CODEC_ID_TEXT
)
741 prog
->sid
= program
->stream_index
[i
];
745 if(prog
->progid
== -1 && prog
->vid
== -2 && prog
->aid
== -2)
747 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
750 priv
->cur_program
= prog
->progid
= program
->id
;
751 return DEMUXER_CTRL_OK
;
754 return DEMUXER_CTRL_NOTIMPL
;
758 /** \brief Get the language code for a subtitle track.
760 Retrieves the language code for a subtitle track.
762 \param demuxer The demuxer to work on
763 \param track_num The subtitle track number to get the language from
765 char *demux_lavf_sub_lang(demuxer_t
*demuxer
, int track_num
)
767 lavf_priv_t
*priv
= demuxer
->priv
;
768 return priv
->avfc
->streams
[priv
->sstreams
[track_num
]]->language
;
771 static void demux_close_lavf(demuxer_t
*demuxer
)
773 lavf_priv_t
* priv
= demuxer
->priv
;
777 av_freep(&priv
->avfc
->key
);
778 av_close_input_stream(priv
->avfc
);
781 free(priv
); demuxer
->priv
= NULL
;
786 const demuxer_desc_t demuxer_desc_lavf
= {
787 "libavformat demuxer",
790 "Michael Niedermayer",
791 "supports many formats, requires libavformat",
793 0, // Check after other demuxer
795 demux_lavf_fill_buffer
,
802 const demuxer_desc_t demuxer_desc_lavf_preferred
= {
803 "libavformat preferred demuxer",
806 "Michael Niedermayer",
807 "supports many formats, requires libavformat",
808 DEMUXER_TYPE_LAVF_PREFERRED
,
810 lavf_check_preferred_file
,
811 demux_lavf_fill_buffer
,