Add a comment that explains why this header has no multiple inclusion guards.
[mplayer/greg.git] / libmpdemux / demux_lavf.c
blob894b951e340d9525594eb73d490efaa51af852d2
1 /*
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
19 // #include <stdio.h>
20 #include <stdlib.h>
21 // #include <unistd.h>
22 #include <limits.h>
24 #include "config.h"
25 #include "mp_msg.h"
26 #include "help_mp.h"
28 #include "stream/stream.h"
29 #include "demuxer.h"
30 #include "stheader.h"
31 #include "m_option.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>
38 #else
39 #include "avformat.h"
40 #include "avutil.h"
41 #include "avi.h"
42 #include "opt.h"
43 #endif
44 #include "riff.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;
53 extern int ts_prog;
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{
66 AVInputFormat *avif;
67 AVFormatContext *avfc;
68 ByteIOContext *pb;
69 uint8_t buffer[BIO_BUFFER_SIZE];
70 int audio_streams;
71 int video_streams;
72 int sub_streams;
73 int64_t last_pts;
74 int astreams[MAX_A_STREAMS];
75 int vstreams[MAX_V_STREAMS];
76 int sstreams[MAX_S_STREAMS];
77 int cur_program;
78 }lavf_priv_t;
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')},
104 { 0, 0 },
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')},
126 { 0, 0 },
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;
133 int ret;
135 if(stream_eof(stream)) //needed?
136 return -1;
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);
140 return ret;
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;
154 else
155 return -1;
157 if(pos<0)
158 return -1;
159 if(pos<stream->end_pos && stream->eof)
160 stream_reset(stream);
161 if(stream_seek(stream, pos)==0)
162 return -1;
164 return pos - stream->start_pos;
167 static void list_formats(void) {
168 AVInputFormat *fmt;
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){
175 AVProbeData avpd;
176 uint8_t buf[PROBE_BUF_SIZE];
177 lavf_priv_t *priv;
179 if(!demuxer->priv)
180 demuxer->priv=calloc(sizeof(lavf_priv_t),1);
181 priv= demuxer->priv;
183 av_register_all();
185 if(stream_read(demuxer->stream, buf, PROBE_BUF_SIZE)!=PROBE_BUF_SIZE)
186 return 0;
187 avpd.filename= demuxer->stream->url;
188 avpd.buf= buf;
189 avpd.buf_size= PROBE_BUF_SIZE;
191 if (opt_format) {
192 if (strcmp(opt_format, "help") == 0) {
193 list_formats();
194 return 0;
196 priv->avif= av_find_input_format(opt_format);
197 if (!priv->avif) {
198 mp_msg(MSGT_DEMUX,MSGL_FATAL,"Unknown lavf format %s\n", opt_format);
199 return 0;
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);
205 if(!priv->avif){
206 mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: no clue about this gibberish!\n");
207 return 0;
208 }else
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[] = {
215 "dxa",
216 "wv",
217 "nuv",
218 "nut",
219 "gxf",
220 "mxf",
221 "flv",
222 "swf",
223 NULL
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;
230 while (*p) {
231 if (strcmp(*p, priv->avif->name) == 0)
232 return DEMUXER_TYPE_LAVF_PREFERRED;
233 p++;
236 return 0;
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;
243 return 0;
246 static void parse_cryptokey(AVFormatContext *avfc, const char *str) {
247 int len = strlen(str) / 2;
248 uint8_t *key = av_mallocz(len);
249 int i;
250 avfc->keylen = len;
251 avfc->key = key;
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;
260 int g;
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)
267 break;
268 sh_audio=new_sh_audio(demuxer, i);
269 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_AudioID, "lavf", i);
270 if(!sh_audio)
271 break;
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'))
276 codec->codec_tag= 0;
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);
290 sh_audio->wf= wf;
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;
295 }else{
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;
312 break;
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;
318 break;
319 case CODEC_ID_PCM_ALAW:
320 sh_audio->format = 0x6;
321 break;
322 case CODEC_ID_PCM_MULAW:
323 sh_audio->format = 0x7;
324 break;
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];
331 } else
332 st->discard= AVDISCARD_ALL;
333 break;
335 case CODEC_TYPE_VIDEO:{
336 sh_video_t* sh_video;
337 BITMAPINFOHEADER *bih;
338 if(priv->video_streams >= MAX_V_STREAMS)
339 break;
340 sh_video=new_sh_video(demuxer, i);
341 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_VideoID, "lavf", i);
342 if(!sh_video) break;
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;
355 sh_video->bih= bih;
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;
361 } else {
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);
380 short biPlanes;
381 int biXPelsPerMeter;
382 int biYPelsPerMeter;
383 int biClrUsed;
384 int biClrImportant;
386 if(demuxer->video->id != i && demuxer->video->id != -1)
387 st->discard= AVDISCARD_ALL;
388 else{
389 demuxer->video->id = i;
390 demuxer->video->sh= demuxer->v_streams[i];
392 break;
394 case CODEC_TYPE_SUBTITLE:{
395 sh_sub_t* sh_sub;
396 if(priv->sub_streams >= MAX_S_STREAMS)
397 break;
398 /* only support text subtitles for now */
399 if(codec->codec_id != CODEC_ID_TEXT)
400 break;
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);
403 if(!sh_sub) break;
404 priv->sstreams[priv->sub_streams] = i;
405 sh_sub->type = 't';
406 demuxer->sub->sh = demuxer->s_streams[priv->sub_streams++];
407 break;
409 default:
410 st->discard= AVDISCARD_ALL;
414 static demuxer_t* demux_open_lavf(demuxer_t *demuxer){
415 AVFormatContext *avfc;
416 AVFormatParameters ap;
417 const AVOption *opt;
418 lavf_priv_t *priv= demuxer->priv;
419 int i;
420 char mp_filename[256]="mp:";
422 memset(&ap, 0, sizeof(AVFormatParameters));
424 stream_seek(demuxer->stream, 0);
426 avfc = av_alloc_format_context();
428 if (opt_cryptokey)
429 parse_cryptokey(avfc, opt_cryptokey);
430 if (correct_pts)
431 avfc->flags |= AVFMT_FLAG_GENPTS;
432 if (index_mode == 0)
433 avfc->flags |= AVFMT_FLAG_IGNIDX;
435 ap.prealloced_context = 1;
436 if(opt_probesize) {
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);
447 else
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");
456 return NULL;
459 priv->avfc= avfc;
461 if(av_find_stream_info(avfc) < 0){
462 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_find_stream_info() failed\n");
463 return NULL;
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;
478 if(ts_prog) {
479 for(p=0; p<avfc->nb_programs; p++) {
480 if(avfc->programs[p]->id == ts_prog) {
481 start = p;
482 found = 1;
483 break;
486 if(!found) {
487 mp_msg(MSGT_HEADER,MSGL_ERR,"DEMUX_LAVF: program %d doesn't seem to be present\n", ts_prog);
488 return NULL;
491 p = start;
492 do {
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;
500 } while(p!=start);
501 } else
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");
512 return NULL;
514 demuxer->video->id=-2; // audio-only
515 } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video;
517 return demuxer;
520 static int demux_lavf_fill_buffer(demuxer_t *demux, demux_stream_t *dsds){
521 lavf_priv_t *priv= demux->priv;
522 AVPacket pkt;
523 demux_packet_t *dp;
524 demux_stream_t *ds;
525 int id;
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)
531 return 0;
533 id= pkt.stream_index;
535 if(id==demux->audio->id){
536 // audio
537 ds=demux->audio;
538 if(!ds->sh){
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){
543 // video
544 ds=demux->video;
545 if(!ds->sh){
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){
550 // subtitle
551 ds=demux->sub;
552 sub_utf8=1;
553 } else {
554 av_free_packet(&pkt);
555 return 1;
558 if(0/*pkt.destruct == av_destruct_packet*/){
559 //ok kids, dont try this at home :)
560 dp=malloc(sizeof(demux_packet_t));
561 dp->len=pkt.size;
562 dp->next=NULL;
563 dp->refcount=1;
564 dp->master=NULL;
565 dp->buffer=pkt.data;
566 pkt.destruct= NULL;
567 }else{
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;
576 if(pkt.duration)
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);
583 return 1;
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;
588 int avsflags = 0;
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)
595 return;
596 priv->last_pts += rel_seek_secs * priv->avfc->duration;
597 } else {
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;
608 switch (cmd) {
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);
626 int newid = -2;
627 int i, curridx = -2;
628 int nstreams, *pstreams;
629 demux_stream_t *ds;
631 if(cmd == DEMUXER_CTRL_SWITCH_VIDEO)
633 ds = demuxer->video;
634 nstreams = priv->video_streams;
635 pstreams = priv->vstreams;
637 else
639 ds = demuxer->audio;
640 nstreams = priv->audio_streams;
641 pstreams = priv->astreams;
643 if(id == -2)
645 if(ds->id >= 0)
646 priv->avfc->streams[ds->id]->discard = AVDISCARD_ALL;
647 ds_free_packs(ds);
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
655 curridx = i;
656 break;
660 if(id < 0)
662 i = (curridx + 1) % nstreams;
663 newid = pstreams[i];
665 else
667 for(i = 0; i < nstreams; i++)
669 if(pstreams[i] == id)
671 newid = id;
672 break;
676 if(i == curridx)
677 return DEMUXER_CTRL_NOTIMPL;
678 else
680 ds_free_packs(ds);
681 if(ds->id >= 0)
682 priv->avfc->streams[ds->id]->discard = AVDISCARD_ALL;
683 *((int*)arg) = ds->id = newid;
684 if(newid >= 0)
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;
692 AVProgram *program;
693 int p, i;
695 if(priv->avfc->nb_programs < 2)
696 return DEMUXER_CTRL_NOTIMPL;
698 if(prog->progid == -1)
700 p = 0;
701 while(p<priv->avfc->nb_programs && priv->avfc->programs[p]->id != priv->cur_program)
702 p++;
703 p = (p + 1) % priv->avfc->nb_programs;
705 else
707 for(i=0; i<priv->avfc->nb_programs; i++)
708 if(priv->avfc->programs[i]->id == prog->progid)
709 break;
710 if(i==priv->avfc->nb_programs)
711 return DEMUXER_CTRL_NOTIMPL;
712 p = i;
714 prog->vid = prog->aid = prog->sid = -2; //no audio and no video by default
715 redo:
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:
722 if(prog->vid == -2)
723 prog->vid = program->stream_index[i];
724 break;
725 case CODEC_TYPE_AUDIO:
726 if(prog->aid == -2)
727 prog->aid = program->stream_index[i];
728 break;
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];
732 break;
735 if(prog->progid == -1 && prog->vid == -2 && prog->aid == -2)
737 p = (p + 1) % priv->avfc->nb_programs;
738 goto redo;
740 priv->cur_program = prog->progid = program->id;
741 return DEMUXER_CTRL_OK;
743 default:
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;
764 if (priv){
765 if(priv->avfc)
767 av_freep(&priv->avfc->key);
768 av_close_input_stream(priv->avfc);
770 av_freep(&priv->pb);
771 free(priv); demuxer->priv= NULL;
776 demuxer_desc_t demuxer_desc_lavf = {
777 "libavformat demuxer",
778 "lavf",
779 "libavformat",
780 "Michael Niedermayer",
781 "supports many formats, requires libavformat",
782 DEMUXER_TYPE_LAVF,
783 0, // Check after other demuxer
784 lavf_check_file,
785 demux_lavf_fill_buffer,
786 demux_open_lavf,
787 demux_close_lavf,
788 demux_seek_lavf,
789 demux_lavf_control
792 demuxer_desc_t demuxer_desc_lavf_preferred = {
793 "libavformat preferred demuxer",
794 "lavfpref",
795 "libavformat",
796 "Michael Niedermayer",
797 "supports many formats, requires libavformat",
798 DEMUXER_TYPE_LAVF_PREFERRED,
800 lavf_check_preferred_file,
801 demux_lavf_fill_buffer,
802 demux_open_lavf,
803 demux_close_lavf,
804 demux_seek_lavf,
805 demux_lavf_control