Fix compilation after FFmpeg r23485.
[mplayer/glamo.git] / libmpdemux / demux_lavf.c
blob72363ef520e6e65a1481b731a9cf78d32e13023f
1 /*
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.
21 // #include <stdio.h>
22 #include <stdlib.h>
23 // #include <unistd.h>
24 #include <limits.h>
26 #include "config.h"
27 #include "mp_msg.h"
28 #include "help_mp.h"
29 #include "av_opts.h"
31 #include "stream/stream.h"
32 #include "aviprint.h"
33 #include "demuxer.h"
34 #include "stheader.h"
35 #include "m_option.h"
36 #include "libvo/sub.h"
38 #include "libavformat/avformat.h"
39 #include "libavformat/avio.h"
40 #include "libavutil/avutil.h"
41 #include "libavutil/avstring.h"
42 #include "libavcodec/opt.h"
44 #include "mp_taglists.h"
46 #define INITIAL_PROBE_SIZE STREAM_BUFFER_SIZE
47 #define SMALL_MAX_PROBE_SIZE (32 * 1024)
48 #define PROBE_BUF_SIZE (2*1024*1024)
50 extern char *audio_lang;
51 extern char *dvdsub_lang;
52 extern int dvdsub_id;
53 static unsigned int opt_probesize = 0;
54 static unsigned int opt_analyzeduration = 0;
55 static char *opt_format;
56 static char *opt_cryptokey;
57 static char *opt_avopt = NULL;
59 const m_option_t lavfdopts_conf[] = {
60 {"probesize", &(opt_probesize), CONF_TYPE_INT, CONF_RANGE, 32, INT_MAX, NULL},
61 {"format", &(opt_format), CONF_TYPE_STRING, 0, 0, 0, NULL},
62 {"analyzeduration", &(opt_analyzeduration), CONF_TYPE_INT, CONF_RANGE, 0, INT_MAX, NULL},
63 {"cryptokey", &(opt_cryptokey), CONF_TYPE_STRING, 0, 0, 0, NULL},
64 {"o", &opt_avopt, CONF_TYPE_STRING, 0, 0, 0, NULL},
65 {NULL, NULL, 0, 0, 0, 0, NULL}
68 #define BIO_BUFFER_SIZE 32768
70 typedef struct lavf_priv {
71 AVInputFormat *avif;
72 AVFormatContext *avfc;
73 ByteIOContext *pb;
74 uint8_t buffer[BIO_BUFFER_SIZE];
75 int audio_streams;
76 int video_streams;
77 int sub_streams;
78 int64_t last_pts;
79 int astreams[MAX_A_STREAMS];
80 int vstreams[MAX_V_STREAMS];
81 int sstreams[MAX_S_STREAMS];
82 int cur_program;
83 }lavf_priv_t;
85 static int mp_read(void *opaque, uint8_t *buf, int size) {
86 demuxer_t *demuxer = opaque;
87 stream_t *stream = demuxer->stream;
88 int ret;
90 if(stream_eof(stream)) //needed?
91 return -1;
92 ret=stream_read(stream, buf, size);
94 mp_msg(MSGT_HEADER,MSGL_DBG2,"%d=mp_read(%p, %p, %d), eof:%d\n", ret, stream, buf, size, stream->eof);
95 return ret;
98 static int64_t mp_seek(void *opaque, int64_t pos, int whence) {
99 demuxer_t *demuxer = opaque;
100 stream_t *stream = demuxer->stream;
101 int64_t current_pos;
102 mp_msg(MSGT_HEADER,MSGL_DBG2,"mp_seek(%p, %"PRId64", %d)\n", stream, pos, whence);
103 if(whence == SEEK_CUR)
104 pos +=stream_tell(stream);
105 else if(whence == SEEK_END && stream->end_pos > 0)
106 pos += stream->end_pos;
107 else if(whence == SEEK_SET)
108 pos += stream->start_pos;
109 else if(whence == AVSEEK_SIZE && stream->end_pos > 0)
110 return stream->end_pos - stream->start_pos;
111 else
112 return -1;
114 if(pos<0)
115 return -1;
116 if(pos<stream->end_pos && stream->eof)
117 stream_reset(stream);
118 current_pos = stream_tell(stream);
119 if(stream_seek(stream, pos)==0) {
120 stream_reset(stream);
121 stream_seek(stream, current_pos);
122 return -1;
125 return pos - stream->start_pos;
128 static int64_t mp_read_seek(void *opaque, int stream_idx, int64_t ts, int flags) {
129 demuxer_t *demuxer = opaque;
130 stream_t *stream = demuxer->stream;
131 lavf_priv_t *priv = demuxer->priv;
132 AVStream *st = priv->avfc->streams[stream_idx];
133 int ret;
134 double pts;
136 pts = (double)ts * st->time_base.num / st->time_base.den;
137 ret = stream_control(stream, STREAM_CTRL_SEEK_TO_TIME, &pts);
138 if (ret < 0)
139 ret = AVERROR(ENOSYS);
140 return ret;
143 static void list_formats(void) {
144 AVInputFormat *fmt;
145 mp_msg(MSGT_DEMUX, MSGL_INFO, "Available lavf input formats:\n");
146 for (fmt = first_iformat; fmt; fmt = fmt->next)
147 mp_msg(MSGT_DEMUX, MSGL_INFO, "%15s : %s\n", fmt->name, fmt->long_name);
150 static int lavf_check_file(demuxer_t *demuxer){
151 AVProbeData avpd;
152 lavf_priv_t *priv;
153 int probe_data_size = 0;
154 int read_size = INITIAL_PROBE_SIZE;
155 int score;
157 if(!demuxer->priv)
158 demuxer->priv=calloc(sizeof(lavf_priv_t),1);
159 priv= demuxer->priv;
161 av_register_all();
163 if (opt_format) {
164 if (strcmp(opt_format, "help") == 0) {
165 list_formats();
166 return 0;
168 priv->avif= av_find_input_format(opt_format);
169 if (!priv->avif) {
170 mp_msg(MSGT_DEMUX,MSGL_FATAL,"Unknown lavf format %s\n", opt_format);
171 return 0;
173 mp_msg(MSGT_DEMUX,MSGL_INFO,"Forced lavf %s demuxer\n", priv->avif->long_name);
174 return DEMUXER_TYPE_LAVF;
177 avpd.buf = av_mallocz(FFMAX(BIO_BUFFER_SIZE, PROBE_BUF_SIZE) +
178 FF_INPUT_BUFFER_PADDING_SIZE);
179 do {
180 read_size = stream_read(demuxer->stream, avpd.buf + probe_data_size, read_size);
181 if(read_size < 0) {
182 av_free(avpd.buf);
183 return 0;
185 probe_data_size += read_size;
186 avpd.filename= demuxer->stream->url;
187 if (!strncmp(avpd.filename, "ffmpeg://", 9))
188 avpd.filename += 9;
189 avpd.buf_size= probe_data_size;
191 score = 0;
192 priv->avif= av_probe_input_format2(&avpd, probe_data_size > 0, &score);
193 read_size = FFMIN(2*read_size, PROBE_BUF_SIZE - probe_data_size);
194 } while ((demuxer->desc->type != DEMUXER_TYPE_LAVF_PREFERRED ||
195 probe_data_size < SMALL_MAX_PROBE_SIZE) &&
196 score <= AVPROBE_SCORE_MAX / 4 &&
197 read_size > 0 && probe_data_size < PROBE_BUF_SIZE);
198 av_free(avpd.buf);
200 if(!priv->avif){
201 mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: no clue about this gibberish!\n");
202 return 0;
203 }else
204 mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: %s\n", priv->avif->long_name);
206 return DEMUXER_TYPE_LAVF;
209 static const char * const preferred_list[] = {
210 "dxa",
211 "flv",
212 "gxf",
213 "nut",
214 "nuv",
215 "matroska",
216 "mov,mp4,m4a,3gp,3g2,mj2",
217 "mpc",
218 "mpc8",
219 "mxf",
220 "ogg",
221 "swf",
222 "vqf",
223 "w64",
224 "wv",
225 NULL
228 static int lavf_check_preferred_file(demuxer_t *demuxer){
229 if (lavf_check_file(demuxer)) {
230 const char * const *p = preferred_list;
231 lavf_priv_t *priv = demuxer->priv;
232 while (*p) {
233 if (strcmp(*p, priv->avif->name) == 0)
234 return DEMUXER_TYPE_LAVF_PREFERRED;
235 p++;
238 return 0;
241 static uint8_t char2int(char c) {
242 if (c >= '0' && c <= '9') return c - '0';
243 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
244 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
245 return 0;
248 static void parse_cryptokey(AVFormatContext *avfc, const char *str) {
249 int len = strlen(str) / 2;
250 uint8_t *key = av_mallocz(len);
251 int i;
252 avfc->keylen = len;
253 avfc->key = key;
254 for (i = 0; i < len; i++, str += 2)
255 *key++ = (char2int(str[0]) << 4) | char2int(str[1]);
258 static void handle_stream(demuxer_t *demuxer, AVFormatContext *avfc, int i) {
259 lavf_priv_t *priv= demuxer->priv;
260 AVStream *st= avfc->streams[i];
261 AVCodecContext *codec= st->codec;
262 char *stream_type = NULL;
263 int stream_id;
264 AVMetadataTag *lang = av_metadata_get(st->metadata, "language", NULL, 0);
265 AVMetadataTag *title= av_metadata_get(st->metadata, "title", NULL, 0);
266 int g, override_tag = av_codec_get_tag(mp_codecid_override_taglists,
267 codec->codec_id);
268 // For some formats (like PCM) always trust CODEC_ID_* more than codec_tag
269 if (override_tag)
270 codec->codec_tag = override_tag;
272 switch(codec->codec_type){
273 case CODEC_TYPE_AUDIO:{
274 WAVEFORMATEX *wf;
275 sh_audio_t* sh_audio;
276 sh_audio = new_sh_audio_aid(demuxer, i, priv->audio_streams);
277 if(!sh_audio)
278 break;
279 stream_type = "audio";
280 priv->astreams[priv->audio_streams] = i;
281 wf= calloc(sizeof(WAVEFORMATEX) + codec->extradata_size, 1);
282 // mp4a tag is used for all mp4 files no matter what they actually contain
283 if(codec->codec_tag == MKTAG('m', 'p', '4', 'a'))
284 codec->codec_tag= 0;
285 if(!codec->codec_tag)
286 codec->codec_tag= av_codec_get_tag(mp_wav_taglists, codec->codec_id);
287 wf->wFormatTag= codec->codec_tag;
288 wf->nChannels= codec->channels;
289 wf->nSamplesPerSec= codec->sample_rate;
290 wf->nAvgBytesPerSec= codec->bit_rate/8;
291 wf->nBlockAlign= codec->block_align ? codec->block_align : 1;
292 wf->wBitsPerSample= codec->bits_per_coded_sample;
293 wf->cbSize= codec->extradata_size;
294 if(codec->extradata_size)
295 memcpy(wf + 1, codec->extradata, codec->extradata_size);
296 sh_audio->wf= wf;
297 sh_audio->audio.dwSampleSize= codec->block_align;
298 if(codec->frame_size && codec->sample_rate){
299 sh_audio->audio.dwScale=codec->frame_size;
300 sh_audio->audio.dwRate= codec->sample_rate;
301 }else{
302 sh_audio->audio.dwScale= codec->block_align ? codec->block_align*8 : 8;
303 sh_audio->audio.dwRate = codec->bit_rate;
305 g= av_gcd(sh_audio->audio.dwScale, sh_audio->audio.dwRate);
306 sh_audio->audio.dwScale /= g;
307 sh_audio->audio.dwRate /= g;
308 // 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);
309 sh_audio->ds= demuxer->audio;
310 sh_audio->format= codec->codec_tag;
311 sh_audio->channels= codec->channels;
312 sh_audio->samplerate= codec->sample_rate;
313 sh_audio->i_bps= codec->bit_rate/8;
314 switch (codec->codec_id) {
315 case CODEC_ID_PCM_S8:
316 case CODEC_ID_PCM_U8:
317 sh_audio->samplesize = 1;
318 break;
319 case CODEC_ID_PCM_S16LE:
320 case CODEC_ID_PCM_S16BE:
321 case CODEC_ID_PCM_U16LE:
322 case CODEC_ID_PCM_U16BE:
323 sh_audio->samplesize = 2;
324 break;
325 case CODEC_ID_PCM_ALAW:
326 sh_audio->format = 0x6;
327 break;
328 case CODEC_ID_PCM_MULAW:
329 sh_audio->format = 0x7;
330 break;
332 if (title && title->value)
333 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_NAME=%s\n", priv->audio_streams, title->value);
334 if (lang && lang->value) {
335 sh_audio->lang = strdup(lang->value);
336 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_LANG=%s\n", priv->audio_streams, sh_audio->lang);
338 if (st->disposition & AV_DISPOSITION_DEFAULT)
339 sh_audio->default_track = 1;
340 if(mp_msg_test(MSGT_HEADER,MSGL_V) ) print_wave_header(sh_audio->wf, MSGL_V);
341 // select the first audio stream
342 if (!demuxer->audio->sh) {
343 demuxer->audio->id = i;
344 demuxer->audio->sh= demuxer->a_streams[i];
345 } else
346 st->discard= AVDISCARD_ALL;
347 stream_id = priv->audio_streams++;
348 break;
350 case CODEC_TYPE_VIDEO:{
351 sh_video_t* sh_video;
352 BITMAPINFOHEADER *bih;
353 sh_video=new_sh_video_vid(demuxer, i, priv->video_streams);
354 if(!sh_video) break;
355 stream_type = "video";
356 priv->vstreams[priv->video_streams] = i;
357 bih=calloc(sizeof(BITMAPINFOHEADER) + codec->extradata_size,1);
359 if(codec->codec_id == CODEC_ID_RAWVIDEO) {
360 switch (codec->pix_fmt) {
361 case PIX_FMT_RGB24:
362 codec->codec_tag= MKTAG(24, 'B', 'G', 'R');
365 if(!codec->codec_tag)
366 codec->codec_tag= av_codec_get_tag(mp_bmp_taglists, codec->codec_id);
367 bih->biSize= sizeof(BITMAPINFOHEADER) + codec->extradata_size;
368 bih->biWidth= codec->width;
369 bih->biHeight= codec->height;
370 bih->biBitCount= codec->bits_per_coded_sample;
371 bih->biSizeImage = bih->biWidth * bih->biHeight * bih->biBitCount/8;
372 bih->biCompression= codec->codec_tag;
373 sh_video->bih= bih;
374 sh_video->disp_w= codec->width;
375 sh_video->disp_h= codec->height;
376 if (st->time_base.den) { /* if container has time_base, use that */
377 sh_video->video.dwRate= st->time_base.den;
378 sh_video->video.dwScale= st->time_base.num;
379 } else {
380 sh_video->video.dwRate= codec->time_base.den;
381 sh_video->video.dwScale= codec->time_base.num;
383 sh_video->fps=av_q2d(st->r_frame_rate);
384 sh_video->frametime=1/av_q2d(st->r_frame_rate);
385 sh_video->format=bih->biCompression;
386 if(st->sample_aspect_ratio.num)
387 sh_video->aspect = codec->width * st->sample_aspect_ratio.num
388 / (float)(codec->height * st->sample_aspect_ratio.den);
389 else
390 sh_video->aspect=codec->width * codec->sample_aspect_ratio.num
391 / (float)(codec->height * codec->sample_aspect_ratio.den);
392 sh_video->i_bps=codec->bit_rate/8;
393 if (title && title->value)
394 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VID_%d_NAME=%s\n", priv->video_streams, title->value);
395 mp_msg(MSGT_DEMUX,MSGL_DBG2,"aspect= %d*%d/(%d*%d)\n",
396 codec->width, codec->sample_aspect_ratio.num,
397 codec->height, codec->sample_aspect_ratio.den);
399 sh_video->ds= demuxer->video;
400 if(codec->extradata_size)
401 memcpy(sh_video->bih + 1, codec->extradata, codec->extradata_size);
402 if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_video_header(sh_video->bih, MSGL_V);
404 short biPlanes;
405 int biXPelsPerMeter;
406 int biYPelsPerMeter;
407 int biClrUsed;
408 int biClrImportant;
410 if(demuxer->video->id != i && demuxer->video->id != -1)
411 st->discard= AVDISCARD_ALL;
412 else{
413 demuxer->video->id = i;
414 demuxer->video->sh= demuxer->v_streams[i];
416 stream_id = priv->video_streams++;
417 break;
419 case CODEC_TYPE_SUBTITLE:{
420 sh_sub_t* sh_sub;
421 char type;
422 /* only support text subtitles for now */
423 if(codec->codec_id == CODEC_ID_TEXT)
424 type = 't';
425 else if(codec->codec_id == CODEC_ID_MOV_TEXT)
426 type = 'm';
427 else if(codec->codec_id == CODEC_ID_SSA)
428 type = 'a';
429 else if(codec->codec_id == CODEC_ID_DVD_SUBTITLE)
430 type = 'v';
431 else if(codec->codec_id == CODEC_ID_DVB_TELETEXT)
432 type = 'd';
433 else
434 break;
435 sh_sub = new_sh_sub_sid(demuxer, i, priv->sub_streams);
436 if(!sh_sub) break;
437 stream_type = "subtitle";
438 priv->sstreams[priv->sub_streams] = i;
439 sh_sub->type = type;
440 if (codec->extradata_size) {
441 sh_sub->extradata = malloc(codec->extradata_size);
442 memcpy(sh_sub->extradata, codec->extradata, codec->extradata_size);
443 sh_sub->extradata_len = codec->extradata_size;
445 if (title && title->value)
446 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_NAME=%s\n", priv->sub_streams, title->value);
447 if (lang && lang->value) {
448 sh_sub->lang = strdup(lang->value);
449 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_LANG=%s\n", priv->sub_streams, sh_sub->lang);
451 if (st->disposition & AV_DISPOSITION_DEFAULT)
452 sh_sub->default_track = 1;
453 stream_id = priv->sub_streams++;
454 break;
456 case CODEC_TYPE_ATTACHMENT:{
457 if (st->codec->codec_id == CODEC_ID_TTF)
458 demuxer_add_attachment(demuxer, st->filename,
459 "application/x-truetype-font",
460 codec->extradata, codec->extradata_size);
461 break;
463 default:
464 st->discard= AVDISCARD_ALL;
466 if (stream_type) {
467 AVCodec *avc = avcodec_find_decoder(codec->codec_id);
468 mp_msg(MSGT_DEMUX, MSGL_INFO, "[lavf] stream %d: %s (%s), -%cid %d", i, stream_type, avc ? avc->name : "unknown", *stream_type, stream_id);
469 if (lang && lang->value && *stream_type != 'v')
470 mp_msg(MSGT_DEMUX, MSGL_INFO, ", -%clang %s", *stream_type, lang->value);
471 if (title && title->value)
472 mp_msg(MSGT_DEMUX, MSGL_INFO, ", %s", title->value);
473 mp_msg(MSGT_DEMUX, MSGL_INFO, "\n");
477 static demuxer_t* demux_open_lavf(demuxer_t *demuxer){
478 AVFormatContext *avfc;
479 AVFormatParameters ap;
480 const AVOption *opt;
481 AVMetadataTag *t = NULL;
482 lavf_priv_t *priv= demuxer->priv;
483 int i;
484 char mp_filename[256]="mp:";
486 memset(&ap, 0, sizeof(AVFormatParameters));
488 stream_seek(demuxer->stream, 0);
490 avfc = avformat_alloc_context();
492 if (opt_cryptokey)
493 parse_cryptokey(avfc, opt_cryptokey);
494 if (user_correct_pts != 0)
495 avfc->flags |= AVFMT_FLAG_GENPTS;
496 if (index_mode == 0)
497 avfc->flags |= AVFMT_FLAG_IGNIDX;
499 ap.prealloced_context = 1;
500 if(opt_probesize) {
501 opt = av_set_int(avfc, "probesize", opt_probesize);
502 if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option probesize to %u\n", opt_probesize);
504 if(opt_analyzeduration) {
505 opt = av_set_int(avfc, "analyzeduration", opt_analyzeduration * AV_TIME_BASE);
506 if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option analyzeduration to %u\n", opt_analyzeduration);
509 if(opt_avopt){
510 if(parse_avopts(avfc, opt_avopt) < 0){
511 mp_msg(MSGT_HEADER,MSGL_ERR, "Your options /%s/ look like gibberish to me pal\n", opt_avopt);
512 return NULL;
516 if(demuxer->stream->url) {
517 if (!strncmp(demuxer->stream->url, "ffmpeg://rtsp:", 14))
518 av_strlcpy(mp_filename, demuxer->stream->url + 9, sizeof(mp_filename));
519 else
520 av_strlcat(mp_filename, demuxer->stream->url, sizeof(mp_filename));
521 } else
522 av_strlcat(mp_filename, "foobar.dummy", sizeof(mp_filename));
524 priv->pb = av_alloc_put_byte(priv->buffer, BIO_BUFFER_SIZE, 0,
525 demuxer, mp_read, NULL, mp_seek);
526 priv->pb->read_seek = mp_read_seek;
527 priv->pb->is_streamed = !demuxer->stream->end_pos || (demuxer->stream->flags & MP_STREAM_SEEK) != MP_STREAM_SEEK;
529 if(av_open_input_stream(&avfc, priv->pb, mp_filename, priv->avif, &ap)<0){
530 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_open_input_stream() failed\n");
531 return NULL;
534 priv->avfc= avfc;
536 if(av_find_stream_info(avfc) < 0){
537 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_find_stream_info() failed\n");
538 return NULL;
541 /* Add metadata. */
542 av_metadata_conv(avfc, NULL, avfc->iformat->metadata_conv);
543 while((t = av_metadata_get(avfc->metadata, "", t, AV_METADATA_IGNORE_SUFFIX)))
544 demux_info_add(demuxer, t->key, t->value);
546 for(i=0; i < avfc->nb_chapters; i++) {
547 AVChapter *c = avfc->chapters[i];
548 uint64_t start = av_rescale_q(c->start, c->time_base, (AVRational){1,1000});
549 uint64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,1000});
550 t = av_metadata_get(c->metadata, "title", NULL, 0);
551 demuxer_add_chapter(demuxer, t ? t->value : NULL, start, end);
554 for(i=0; i<avfc->nb_streams; i++)
555 handle_stream(demuxer, avfc, i);
556 if(avfc->nb_programs) {
557 int p;
558 for (p = 0; p < avfc->nb_programs; p++) {
559 AVProgram *program = avfc->programs[p];
560 t = av_metadata_get(program->metadata, "title", NULL, 0);
561 mp_msg(MSGT_HEADER,MSGL_INFO,"LAVF: Program %d %s\n", program->id, t ? t->value : "");
565 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: %d audio and %d video streams found\n",priv->audio_streams,priv->video_streams);
566 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: build %d\n", LIBAVFORMAT_BUILD);
567 if(!priv->audio_streams) demuxer->audio->id=-2; // nosound
568 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio;
569 if(!priv->video_streams){
570 if(!priv->audio_streams){
571 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF: no audio or video headers found - broken file?\n");
572 return NULL;
574 demuxer->video->id=-2; // audio-only
575 } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video;
577 return demuxer;
580 static int demux_lavf_fill_buffer(demuxer_t *demux, demux_stream_t *dsds){
581 lavf_priv_t *priv= demux->priv;
582 AVPacket pkt;
583 demux_packet_t *dp;
584 demux_stream_t *ds;
585 int id;
586 mp_msg(MSGT_DEMUX,MSGL_DBG2,"demux_lavf_fill_buffer()\n");
588 demux->filepos=stream_tell(demux->stream);
590 if(av_read_frame(priv->avfc, &pkt) < 0)
591 return 0;
593 id= pkt.stream_index;
595 if(id==demux->audio->id){
596 // audio
597 ds=demux->audio;
598 if(!ds->sh){
599 ds->sh=demux->a_streams[id];
600 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected LAVF audio ID = %d\n",ds->id);
602 } else if(id==demux->video->id){
603 // video
604 ds=demux->video;
605 if(!ds->sh){
606 ds->sh=demux->v_streams[id];
607 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected LAVF video ID = %d\n",ds->id);
609 } else if(id==demux->sub->id){
610 // subtitle
611 ds=demux->sub;
612 sub_utf8=1;
613 } else {
614 av_free_packet(&pkt);
615 return 1;
618 if(0/*pkt.destruct == av_destruct_packet*/){
619 //ok kids, dont try this at home :)
620 dp=malloc(sizeof(demux_packet_t));
621 dp->len=pkt.size;
622 dp->next=NULL;
623 dp->refcount=1;
624 dp->master=NULL;
625 dp->buffer=pkt.data;
626 pkt.destruct= NULL;
627 }else{
628 dp=new_demux_packet(pkt.size);
629 memcpy(dp->buffer, pkt.data, pkt.size);
630 av_free_packet(&pkt);
633 if(pkt.pts != AV_NOPTS_VALUE){
634 dp->pts=pkt.pts * av_q2d(priv->avfc->streams[id]->time_base);
635 priv->last_pts= dp->pts * AV_TIME_BASE;
636 if(pkt.convergence_duration)
637 dp->endpts = dp->pts + pkt.convergence_duration * av_q2d(priv->avfc->streams[id]->time_base);
639 dp->pos=demux->filepos;
640 dp->flags= !!(pkt.flags&PKT_FLAG_KEY);
641 // append packet to DS stream:
642 ds_add_packet(ds,dp);
643 return 1;
646 static void demux_seek_lavf(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags){
647 lavf_priv_t *priv = demuxer->priv;
648 int avsflags = 0;
649 mp_msg(MSGT_DEMUX,MSGL_DBG2,"demux_seek_lavf(%p, %f, %f, %d)\n", demuxer, rel_seek_secs, audio_delay, flags);
651 if (flags & SEEK_ABSOLUTE) {
652 priv->last_pts = priv->avfc->start_time;
653 } else {
654 if (rel_seek_secs < 0) avsflags = AVSEEK_FLAG_BACKWARD;
656 if (flags & SEEK_FACTOR) {
657 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
658 return;
659 priv->last_pts += rel_seek_secs * priv->avfc->duration;
660 } else {
661 priv->last_pts += rel_seek_secs * AV_TIME_BASE;
663 if (av_seek_frame(priv->avfc, -1, priv->last_pts, avsflags) < 0) {
664 avsflags ^= AVSEEK_FLAG_BACKWARD;
665 av_seek_frame(priv->avfc, -1, priv->last_pts, avsflags);
669 static int demux_lavf_control(demuxer_t *demuxer, int cmd, void *arg)
671 lavf_priv_t *priv = demuxer->priv;
673 switch (cmd) {
674 case DEMUXER_CTRL_CORRECT_PTS:
675 return DEMUXER_CTRL_OK;
676 case DEMUXER_CTRL_GET_TIME_LENGTH:
677 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
678 return DEMUXER_CTRL_DONTKNOW;
680 *((double *)arg) = (double)priv->avfc->duration / AV_TIME_BASE;
681 return DEMUXER_CTRL_OK;
683 case DEMUXER_CTRL_GET_PERCENT_POS:
684 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
685 return DEMUXER_CTRL_DONTKNOW;
687 *((int *)arg) = (int)((priv->last_pts - priv->avfc->start_time)*100 / priv->avfc->duration);
688 return DEMUXER_CTRL_OK;
689 case DEMUXER_CTRL_SWITCH_AUDIO:
690 case DEMUXER_CTRL_SWITCH_VIDEO:
692 int id = *((int*)arg);
693 int newid = -2;
694 int i, curridx = -1;
695 int nstreams, *pstreams;
696 demux_stream_t *ds;
698 if(cmd == DEMUXER_CTRL_SWITCH_VIDEO)
700 ds = demuxer->video;
701 nstreams = priv->video_streams;
702 pstreams = priv->vstreams;
704 else
706 ds = demuxer->audio;
707 nstreams = priv->audio_streams;
708 pstreams = priv->astreams;
710 for(i = 0; i < nstreams; i++)
712 if(pstreams[i] == ds->id) //current stream id
714 curridx = i;
715 break;
719 if(id == -2) { // no sound
720 i = -1;
721 } else if(id == -1) { // next track
722 i = (curridx + 2) % (nstreams + 1) - 1;
723 if (i >= 0)
724 newid = pstreams[i];
726 else // select track by id
728 if (id >= 0 && id < nstreams) {
729 i = id;
730 newid = pstreams[i];
733 if(i == curridx)
734 return DEMUXER_CTRL_NOTIMPL;
735 else
737 ds_free_packs(ds);
738 if(ds->id >= 0)
739 priv->avfc->streams[ds->id]->discard = AVDISCARD_ALL;
740 *((int*)arg) = ds->id = newid;
741 if(newid >= 0)
742 priv->avfc->streams[newid]->discard = AVDISCARD_NONE;
743 return DEMUXER_CTRL_OK;
746 case DEMUXER_CTRL_IDENTIFY_PROGRAM:
748 demux_program_t *prog = arg;
749 AVProgram *program;
750 int p, i;
751 int start;
753 prog->vid = prog->aid = prog->sid = -2; //no audio and no video by default
754 if(priv->avfc->nb_programs < 1)
755 return DEMUXER_CTRL_DONTKNOW;
757 if(prog->progid == -1)
759 p = 0;
760 while(p<priv->avfc->nb_programs && priv->avfc->programs[p]->id != priv->cur_program)
761 p++;
762 p = (p + 1) % priv->avfc->nb_programs;
764 else
766 for(i=0; i<priv->avfc->nb_programs; i++)
767 if(priv->avfc->programs[i]->id == prog->progid)
768 break;
769 if(i==priv->avfc->nb_programs)
770 return DEMUXER_CTRL_DONTKNOW;
771 p = i;
773 start = p;
774 redo:
775 program = priv->avfc->programs[p];
776 for(i=0; i<program->nb_stream_indexes; i++)
778 switch(priv->avfc->streams[program->stream_index[i]]->codec->codec_type)
780 case CODEC_TYPE_VIDEO:
781 if(prog->vid == -2)
782 prog->vid = program->stream_index[i];
783 break;
784 case CODEC_TYPE_AUDIO:
785 if(prog->aid == -2)
786 prog->aid = program->stream_index[i];
787 break;
788 case CODEC_TYPE_SUBTITLE:
789 if(prog->sid == -2 && priv->avfc->streams[program->stream_index[i]]->codec->codec_id == CODEC_ID_TEXT)
790 prog->sid = program->stream_index[i];
791 break;
794 if(prog->progid == -1 && prog->vid == -2 && prog->aid == -2)
796 p = (p + 1) % priv->avfc->nb_programs;
797 if (p == start)
798 return DEMUXER_CTRL_DONTKNOW;
799 goto redo;
801 priv->cur_program = prog->progid = program->id;
802 return DEMUXER_CTRL_OK;
804 default:
805 return DEMUXER_CTRL_NOTIMPL;
809 static void demux_close_lavf(demuxer_t *demuxer)
811 lavf_priv_t* priv = demuxer->priv;
812 if (priv){
813 if(priv->avfc)
815 av_freep(&priv->avfc->key);
816 av_close_input_stream(priv->avfc);
818 av_freep(&priv->pb);
819 free(priv); demuxer->priv= NULL;
824 const demuxer_desc_t demuxer_desc_lavf = {
825 "libavformat demuxer",
826 "lavf",
827 "libavformat",
828 "Michael Niedermayer",
829 "supports many formats, requires libavformat",
830 DEMUXER_TYPE_LAVF,
831 0, // Check after other demuxer
832 lavf_check_file,
833 demux_lavf_fill_buffer,
834 demux_open_lavf,
835 demux_close_lavf,
836 demux_seek_lavf,
837 demux_lavf_control
840 const demuxer_desc_t demuxer_desc_lavf_preferred = {
841 "libavformat preferred demuxer",
842 "lavfpref",
843 "libavformat",
844 "Michael Niedermayer",
845 "supports many formats, requires libavformat",
846 DEMUXER_TYPE_LAVF_PREFERRED,
848 lavf_check_preferred_file,
849 demux_lavf_fill_buffer,
850 demux_open_lavf,
851 demux_close_lavf,
852 demux_seek_lavf,
853 demux_lavf_control