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
:{
396 if(priv
->sub_streams
>= MAX_S_STREAMS
)
398 /* only support text subtitles for now */
399 if(codec
->codec_id
!= CODEC_ID_TEXT
)
401 sh_sub
= new_sh_sub_sid(demuxer
, i
, priv
->sub_streams
);
402 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_SubtitleID
, "lavf", priv
->sub_streams
);
404 priv
->sstreams
[priv
->sub_streams
] = i
;
406 demuxer
->sub
->sh
= demuxer
->s_streams
[priv
->sub_streams
++];
410 st
->discard
= AVDISCARD_ALL
;
414 static demuxer_t
* demux_open_lavf(demuxer_t
*demuxer
){
415 AVFormatContext
*avfc
;
416 AVFormatParameters ap
;
418 lavf_priv_t
*priv
= demuxer
->priv
;
420 char mp_filename
[256]="mp:";
422 memset(&ap
, 0, sizeof(AVFormatParameters
));
424 stream_seek(demuxer
->stream
, 0);
426 avfc
= av_alloc_format_context();
429 parse_cryptokey(avfc
, opt_cryptokey
);
431 avfc
->flags
|= AVFMT_FLAG_GENPTS
;
433 avfc
->flags
|= AVFMT_FLAG_IGNIDX
;
435 ap
.prealloced_context
= 1;
437 opt
= av_set_int(avfc
, "probesize", opt_probesize
);
438 if(!opt
) mp_msg(MSGT_HEADER
,MSGL_ERR
, "demux_lavf, couldn't set option probesize to %u\n", opt_probesize
);
440 if(opt_analyzeduration
) {
441 opt
= av_set_int(avfc
, "analyzeduration", opt_analyzeduration
* AV_TIME_BASE
);
442 if(!opt
) mp_msg(MSGT_HEADER
,MSGL_ERR
, "demux_lavf, couldn't set option analyzeduration to %u\n", opt_analyzeduration
);
445 if(demuxer
->stream
->url
)
446 strncpy(mp_filename
+ 3, demuxer
->stream
->url
, sizeof(mp_filename
)-3);
448 strncpy(mp_filename
+ 3, "foobar.dummy", sizeof(mp_filename
)-3);
450 priv
->pb
= av_alloc_put_byte(priv
->buffer
, BIO_BUFFER_SIZE
, 0,
451 demuxer
->stream
, mp_read
, NULL
, mp_seek
);
452 priv
->pb
->is_streamed
= !demuxer
->stream
->end_pos
;
454 if(av_open_input_stream(&avfc
, priv
->pb
, mp_filename
, priv
->avif
, &ap
)<0){
455 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF_header: av_open_input_stream() failed\n");
461 if(av_find_stream_info(avfc
) < 0){
462 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF_header: av_find_stream_info() failed\n");
466 if(avfc
->title
[0]) demux_info_add(demuxer
, "name" , avfc
->title
);
467 if(avfc
->author
[0]) demux_info_add(demuxer
, "author" , avfc
->author
);
468 if(avfc
->copyright
[0]) demux_info_add(demuxer
, "copyright", avfc
->copyright
);
469 if(avfc
->comment
[0]) demux_info_add(demuxer
, "comments" , avfc
->comment
);
470 if(avfc
->album
[0]) demux_info_add(demuxer
, "album" , avfc
->album
);
471 // if(avfc->year ) demux_info_add(demuxer, "year" , avfc->year );
472 // if(avfc->track ) demux_info_add(demuxer, "track" , avfc->track );
473 if(avfc
->genre
[0]) demux_info_add(demuxer
, "genre" , avfc
->genre
);
475 if(avfc
->nb_programs
) {
476 int p
, start
=0, found
=0;
479 for(p
=0; p
<avfc
->nb_programs
; p
++) {
480 if(avfc
->programs
[p
]->id
== ts_prog
) {
487 mp_msg(MSGT_HEADER
,MSGL_ERR
,"DEMUX_LAVF: program %d doesn't seem to be present\n", ts_prog
);
493 AVProgram
*program
= avfc
->programs
[p
];
494 mp_msg(MSGT_HEADER
,MSGL_INFO
,"LAVF: Program %d %s\n", program
->id
, (program
->name
? program
->name
: ""));
495 for(i
=0; i
<program
->nb_stream_indexes
; i
++)
496 handle_stream(demuxer
, avfc
, program
->stream_index
[i
]);
497 if(!priv
->cur_program
&& (demuxer
->video
->sh
|| demuxer
->audio
->sh
))
498 priv
->cur_program
= program
->id
;
499 p
= (p
+ 1) % avfc
->nb_programs
;
502 for(i
=0; i
<avfc
->nb_streams
; i
++)
503 handle_stream(demuxer
, avfc
, i
);
505 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF: %d audio and %d video streams found\n",priv
->audio_streams
,priv
->video_streams
);
506 mp_msg(MSGT_HEADER
,MSGL_V
,"LAVF: build %d\n", LIBAVFORMAT_BUILD
);
507 if(!priv
->audio_streams
) demuxer
->audio
->id
=-2; // nosound
508 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio;
509 if(!priv
->video_streams
){
510 if(!priv
->audio_streams
){
511 mp_msg(MSGT_HEADER
,MSGL_ERR
,"LAVF: no audio or video headers found - broken file?\n");
514 demuxer
->video
->id
=-2; // audio-only
515 } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video;
520 static int demux_lavf_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
){
521 lavf_priv_t
*priv
= demux
->priv
;
526 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"demux_lavf_fill_buffer()\n");
528 demux
->filepos
=stream_tell(demux
->stream
);
530 if(av_read_frame(priv
->avfc
, &pkt
) < 0)
533 id
= pkt
.stream_index
;
535 if(id
==demux
->audio
->id
){
539 ds
->sh
=demux
->a_streams
[id
];
540 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected LAVF audio ID = %d\n",ds
->id
);
542 } else if(id
==demux
->video
->id
){
546 ds
->sh
=demux
->v_streams
[id
];
547 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected LAVF video ID = %d\n",ds
->id
);
549 } else if(id
==demux
->sub
->id
){
554 av_free_packet(&pkt
);
558 if(0/*pkt.destruct == av_destruct_packet*/){
559 //ok kids, dont try this at home :)
560 dp
=malloc(sizeof(demux_packet_t
));
568 dp
=new_demux_packet(pkt
.size
);
569 memcpy(dp
->buffer
, pkt
.data
, pkt
.size
);
570 av_free_packet(&pkt
);
573 if(pkt
.pts
!= AV_NOPTS_VALUE
){
574 dp
->pts
=pkt
.pts
* av_q2d(priv
->avfc
->streams
[id
]->time_base
);
575 priv
->last_pts
= dp
->pts
* AV_TIME_BASE
;
577 dp
->endpts
= dp
->pts
+ pkt
.duration
* av_q2d(priv
->avfc
->streams
[id
]->time_base
);
579 dp
->pos
=demux
->filepos
;
580 dp
->flags
= !!(pkt
.flags
&PKT_FLAG_KEY
);
581 // append packet to DS stream:
582 ds_add_packet(ds
,dp
);
586 static void demux_seek_lavf(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
, int flags
){
587 lavf_priv_t
*priv
= demuxer
->priv
;
589 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"demux_seek_lavf(%p, %f, %f, %d)\n", demuxer
, rel_seek_secs
, audio_delay
, flags
);
591 if (flags
& 1) // absolute seek
592 priv
->last_pts
= priv
->avfc
->start_time
;
593 if (flags
& 2) { // percent seek
594 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
596 priv
->last_pts
+= rel_seek_secs
* priv
->avfc
->duration
;
598 priv
->last_pts
+= rel_seek_secs
* AV_TIME_BASE
;
599 if (rel_seek_secs
< 0) avsflags
= AVSEEK_FLAG_BACKWARD
;
601 av_seek_frame(priv
->avfc
, -1, priv
->last_pts
, avsflags
);
604 static int demux_lavf_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
606 lavf_priv_t
*priv
= demuxer
->priv
;
609 case DEMUXER_CTRL_GET_TIME_LENGTH
:
610 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
611 return DEMUXER_CTRL_DONTKNOW
;
613 *((double *)arg
) = (double)priv
->avfc
->duration
/ AV_TIME_BASE
;
614 return DEMUXER_CTRL_OK
;
616 case DEMUXER_CTRL_GET_PERCENT_POS
:
617 if (priv
->avfc
->duration
== 0 || priv
->avfc
->duration
== AV_NOPTS_VALUE
)
618 return DEMUXER_CTRL_DONTKNOW
;
620 *((int *)arg
) = (int)((priv
->last_pts
- priv
->avfc
->start_time
)*100 / priv
->avfc
->duration
);
621 return DEMUXER_CTRL_OK
;
622 case DEMUXER_CTRL_SWITCH_AUDIO
:
623 case DEMUXER_CTRL_SWITCH_VIDEO
:
625 int id
= *((int*)arg
);
628 int nstreams
, *pstreams
;
631 if(cmd
== DEMUXER_CTRL_SWITCH_VIDEO
)
634 nstreams
= priv
->video_streams
;
635 pstreams
= priv
->vstreams
;
640 nstreams
= priv
->audio_streams
;
641 pstreams
= priv
->astreams
;
646 priv
->avfc
->streams
[ds
->id
]->discard
= AVDISCARD_ALL
;
648 *((int*)arg
) = ds
->id
= -2;
649 return DEMUXER_CTRL_OK
;
651 for(i
= 0; i
< nstreams
; i
++)
653 if(pstreams
[i
] == ds
->id
) //current stream id
662 i
= (curridx
+ 1) % nstreams
;
667 for(i
= 0; i
< nstreams
; i
++)
669 if(pstreams
[i
] == id
)
677 return DEMUXER_CTRL_NOTIMPL
;
682 priv
->avfc
->streams
[ds
->id
]->discard
= AVDISCARD_ALL
;
683 *((int*)arg
) = ds
->id
= newid
;
685 priv
->avfc
->streams
[newid
]->discard
= AVDISCARD_NONE
;
686 return DEMUXER_CTRL_OK
;
689 case DEMUXER_CTRL_IDENTIFY_PROGRAM
:
691 demux_program_t
*prog
= arg
;
695 if(priv
->avfc
->nb_programs
< 2)
696 return DEMUXER_CTRL_NOTIMPL
;
698 if(prog
->progid
== -1)
701 while(p
<priv
->avfc
->nb_programs
&& priv
->avfc
->programs
[p
]->id
!= priv
->cur_program
)
703 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
707 for(i
=0; i
<priv
->avfc
->nb_programs
; i
++)
708 if(priv
->avfc
->programs
[i
]->id
== prog
->progid
)
710 if(i
==priv
->avfc
->nb_programs
)
711 return DEMUXER_CTRL_NOTIMPL
;
714 prog
->vid
= prog
->aid
= prog
->sid
= -2; //no audio and no video by default
716 program
= priv
->avfc
->programs
[p
];
717 for(i
=0; i
<program
->nb_stream_indexes
; i
++)
719 switch(priv
->avfc
->streams
[program
->stream_index
[i
]]->codec
->codec_type
)
721 case CODEC_TYPE_VIDEO
:
723 prog
->vid
= program
->stream_index
[i
];
725 case CODEC_TYPE_AUDIO
:
727 prog
->aid
= program
->stream_index
[i
];
729 case CODEC_TYPE_SUBTITLE
:
730 if(prog
->sid
== -2 && priv
->avfc
->streams
[program
->stream_index
[i
]]->codec
->codec_id
== CODEC_ID_TEXT
)
731 prog
->sid
= program
->stream_index
[i
];
735 if(prog
->progid
== -1 && prog
->vid
== -2 && prog
->aid
== -2)
737 p
= (p
+ 1) % priv
->avfc
->nb_programs
;
740 priv
->cur_program
= prog
->progid
= program
->id
;
741 return DEMUXER_CTRL_OK
;
744 return DEMUXER_CTRL_NOTIMPL
;
748 /** \brief Get the language code for a subtitle track.
750 Retrieves the language code for a subtitle track.
752 \param demuxer The demuxer to work on
753 \param track_num The subtitle track number to get the language from
755 char *demux_lavf_sub_lang(demuxer_t
*demuxer
, int track_num
)
757 lavf_priv_t
*priv
= demuxer
->priv
;
758 return priv
->avfc
->streams
[priv
->sstreams
[track_num
]]->language
;
761 static void demux_close_lavf(demuxer_t
*demuxer
)
763 lavf_priv_t
* priv
= demuxer
->priv
;
767 av_freep(&priv
->avfc
->key
);
768 av_close_input_stream(priv
->avfc
);
771 free(priv
); demuxer
->priv
= NULL
;
776 demuxer_desc_t demuxer_desc_lavf
= {
777 "libavformat demuxer",
780 "Michael Niedermayer",
781 "supports many formats, requires libavformat",
783 0, // Check after other demuxer
785 demux_lavf_fill_buffer
,
792 demuxer_desc_t demuxer_desc_lavf_preferred
= {
793 "libavformat preferred demuxer",
796 "Michael Niedermayer",
797 "supports many formats, requires libavformat",
798 DEMUXER_TYPE_LAVF_PREFERRED
,
800 lavf_check_preferred_file
,
801 demux_lavf_fill_buffer
,