Use MSGT_DECVIDEO in a video decoder.
[mplayer/glamo.git] / libmpdemux / demux_lavf.c
blobbc1781c8a0ada4a05069a2d0661a318c3b2816a2
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 "mov,mp4,m4a,3gp,3g2,mj2",
216 "mpc",
217 "mpc8",
218 "mxf",
219 "ogg",
220 "swf",
221 "vqf",
222 "w64",
223 "wv",
224 NULL
227 static int lavf_check_preferred_file(demuxer_t *demuxer){
228 if (lavf_check_file(demuxer)) {
229 const char * const *p = preferred_list;
230 lavf_priv_t *priv = demuxer->priv;
231 while (*p) {
232 if (strcmp(*p, priv->avif->name) == 0)
233 return DEMUXER_TYPE_LAVF_PREFERRED;
234 p++;
237 return 0;
240 static uint8_t char2int(char c) {
241 if (c >= '0' && c <= '9') return c - '0';
242 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
243 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
244 return 0;
247 static void parse_cryptokey(AVFormatContext *avfc, const char *str) {
248 int len = strlen(str) / 2;
249 uint8_t *key = av_mallocz(len);
250 int i;
251 avfc->keylen = len;
252 avfc->key = key;
253 for (i = 0; i < len; i++, str += 2)
254 *key++ = (char2int(str[0]) << 4) | char2int(str[1]);
257 static void handle_stream(demuxer_t *demuxer, AVFormatContext *avfc, int i) {
258 lavf_priv_t *priv= demuxer->priv;
259 AVStream *st= avfc->streams[i];
260 AVCodecContext *codec= st->codec;
261 char *stream_type = NULL;
262 int stream_id;
263 AVMetadataTag *lang = av_metadata_get(st->metadata, "language", NULL, 0);
264 AVMetadataTag *title= av_metadata_get(st->metadata, "title", NULL, 0);
265 int g, override_tag = av_codec_get_tag(mp_codecid_override_taglists,
266 codec->codec_id);
267 // For some formats (like PCM) always trust CODEC_ID_* more than codec_tag
268 if (override_tag)
269 codec->codec_tag = override_tag;
271 switch(codec->codec_type){
272 case CODEC_TYPE_AUDIO:{
273 WAVEFORMATEX *wf;
274 sh_audio_t* sh_audio;
275 sh_audio = new_sh_audio_aid(demuxer, i, priv->audio_streams);
276 if(!sh_audio)
277 break;
278 stream_type = "audio";
279 priv->astreams[priv->audio_streams] = i;
280 wf= calloc(sizeof(WAVEFORMATEX) + codec->extradata_size, 1);
281 // mp4a tag is used for all mp4 files no matter what they actually contain
282 if(codec->codec_tag == MKTAG('m', 'p', '4', 'a'))
283 codec->codec_tag= 0;
284 if(!codec->codec_tag)
285 codec->codec_tag= av_codec_get_tag(mp_wav_taglists, codec->codec_id);
286 wf->wFormatTag= codec->codec_tag;
287 wf->nChannels= codec->channels;
288 wf->nSamplesPerSec= codec->sample_rate;
289 wf->nAvgBytesPerSec= codec->bit_rate/8;
290 wf->nBlockAlign= codec->block_align ? codec->block_align : 1;
291 wf->wBitsPerSample= codec->bits_per_coded_sample;
292 wf->cbSize= codec->extradata_size;
293 if(codec->extradata_size)
294 memcpy(wf + 1, codec->extradata, codec->extradata_size);
295 sh_audio->wf= wf;
296 sh_audio->audio.dwSampleSize= codec->block_align;
297 if(codec->frame_size && codec->sample_rate){
298 sh_audio->audio.dwScale=codec->frame_size;
299 sh_audio->audio.dwRate= codec->sample_rate;
300 }else{
301 sh_audio->audio.dwScale= codec->block_align ? codec->block_align*8 : 8;
302 sh_audio->audio.dwRate = codec->bit_rate;
304 g= av_gcd(sh_audio->audio.dwScale, sh_audio->audio.dwRate);
305 sh_audio->audio.dwScale /= g;
306 sh_audio->audio.dwRate /= g;
307 // 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);
308 sh_audio->ds= demuxer->audio;
309 sh_audio->format= codec->codec_tag;
310 sh_audio->channels= codec->channels;
311 sh_audio->samplerate= codec->sample_rate;
312 sh_audio->i_bps= codec->bit_rate/8;
313 switch (codec->codec_id) {
314 case CODEC_ID_PCM_S8:
315 case CODEC_ID_PCM_U8:
316 sh_audio->samplesize = 1;
317 break;
318 case CODEC_ID_PCM_S16LE:
319 case CODEC_ID_PCM_S16BE:
320 case CODEC_ID_PCM_U16LE:
321 case CODEC_ID_PCM_U16BE:
322 sh_audio->samplesize = 2;
323 break;
324 case CODEC_ID_PCM_ALAW:
325 sh_audio->format = 0x6;
326 break;
327 case CODEC_ID_PCM_MULAW:
328 sh_audio->format = 0x7;
329 break;
331 if (title && title->value)
332 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_NAME=%s\n", priv->audio_streams, title->value);
333 if (lang && lang->value) {
334 sh_audio->lang = strdup(lang->value);
335 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_LANG=%s\n", priv->audio_streams, sh_audio->lang);
337 if (st->disposition & AV_DISPOSITION_DEFAULT)
338 sh_audio->default_track = 1;
339 if(mp_msg_test(MSGT_HEADER,MSGL_V) ) print_wave_header(sh_audio->wf, MSGL_V);
340 // select the first audio stream
341 if (!demuxer->audio->sh) {
342 demuxer->audio->id = i;
343 demuxer->audio->sh= demuxer->a_streams[i];
344 } else
345 st->discard= AVDISCARD_ALL;
346 stream_id = priv->audio_streams++;
347 break;
349 case CODEC_TYPE_VIDEO:{
350 sh_video_t* sh_video;
351 BITMAPINFOHEADER *bih;
352 sh_video=new_sh_video_vid(demuxer, i, priv->video_streams);
353 if(!sh_video) break;
354 stream_type = "video";
355 priv->vstreams[priv->video_streams] = i;
356 bih=calloc(sizeof(BITMAPINFOHEADER) + codec->extradata_size,1);
358 if(codec->codec_id == CODEC_ID_RAWVIDEO) {
359 switch (codec->pix_fmt) {
360 case PIX_FMT_RGB24:
361 codec->codec_tag= MKTAG(24, 'B', 'G', 'R');
364 if(!codec->codec_tag)
365 codec->codec_tag= av_codec_get_tag(mp_bmp_taglists, codec->codec_id);
366 bih->biSize= sizeof(BITMAPINFOHEADER) + codec->extradata_size;
367 bih->biWidth= codec->width;
368 bih->biHeight= codec->height;
369 bih->biBitCount= codec->bits_per_coded_sample;
370 bih->biSizeImage = bih->biWidth * bih->biHeight * bih->biBitCount/8;
371 bih->biCompression= codec->codec_tag;
372 sh_video->bih= bih;
373 sh_video->disp_w= codec->width;
374 sh_video->disp_h= codec->height;
375 if (st->time_base.den) { /* if container has time_base, use that */
376 sh_video->video.dwRate= st->time_base.den;
377 sh_video->video.dwScale= st->time_base.num;
378 } else {
379 sh_video->video.dwRate= codec->time_base.den;
380 sh_video->video.dwScale= codec->time_base.num;
382 sh_video->fps=av_q2d(st->r_frame_rate);
383 sh_video->frametime=1/av_q2d(st->r_frame_rate);
384 sh_video->format=bih->biCompression;
385 if(st->sample_aspect_ratio.num)
386 sh_video->aspect = codec->width * st->sample_aspect_ratio.num
387 / (float)(codec->height * st->sample_aspect_ratio.den);
388 else
389 sh_video->aspect=codec->width * codec->sample_aspect_ratio.num
390 / (float)(codec->height * codec->sample_aspect_ratio.den);
391 sh_video->i_bps=codec->bit_rate/8;
392 if (title && title->value)
393 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VID_%d_NAME=%s\n", priv->video_streams, title->value);
394 mp_msg(MSGT_DEMUX,MSGL_DBG2,"aspect= %d*%d/(%d*%d)\n",
395 codec->width, codec->sample_aspect_ratio.num,
396 codec->height, codec->sample_aspect_ratio.den);
398 sh_video->ds= demuxer->video;
399 if(codec->extradata_size)
400 memcpy(sh_video->bih + 1, codec->extradata, codec->extradata_size);
401 if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_video_header(sh_video->bih, MSGL_V);
403 short biPlanes;
404 int biXPelsPerMeter;
405 int biYPelsPerMeter;
406 int biClrUsed;
407 int biClrImportant;
409 if(demuxer->video->id != i && demuxer->video->id != -1)
410 st->discard= AVDISCARD_ALL;
411 else{
412 demuxer->video->id = i;
413 demuxer->video->sh= demuxer->v_streams[i];
415 stream_id = priv->video_streams++;
416 break;
418 case CODEC_TYPE_SUBTITLE:{
419 sh_sub_t* sh_sub;
420 char type;
421 /* only support text subtitles for now */
422 if(codec->codec_id == CODEC_ID_TEXT)
423 type = 't';
424 else if(codec->codec_id == CODEC_ID_MOV_TEXT)
425 type = 'm';
426 else if(codec->codec_id == CODEC_ID_SSA)
427 type = 'a';
428 else if(codec->codec_id == CODEC_ID_DVD_SUBTITLE)
429 type = 'v';
430 else if(codec->codec_id == CODEC_ID_DVB_TELETEXT)
431 type = 'd';
432 else
433 break;
434 sh_sub = new_sh_sub_sid(demuxer, i, priv->sub_streams);
435 if(!sh_sub) break;
436 stream_type = "subtitle";
437 priv->sstreams[priv->sub_streams] = i;
438 sh_sub->type = type;
439 if (codec->extradata_size) {
440 sh_sub->extradata = malloc(codec->extradata_size);
441 memcpy(sh_sub->extradata, codec->extradata, codec->extradata_size);
442 sh_sub->extradata_len = codec->extradata_size;
444 if (title && title->value)
445 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_NAME=%s\n", priv->sub_streams, title->value);
446 if (lang && lang->value) {
447 sh_sub->lang = strdup(lang->value);
448 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_LANG=%s\n", priv->sub_streams, sh_sub->lang);
450 if (st->disposition & AV_DISPOSITION_DEFAULT)
451 sh_sub->default_track = 1;
452 stream_id = priv->sub_streams++;
453 break;
455 case CODEC_TYPE_ATTACHMENT:{
456 if (st->codec->codec_id == CODEC_ID_TTF)
457 demuxer_add_attachment(demuxer, st->filename,
458 "application/x-truetype-font",
459 codec->extradata, codec->extradata_size);
460 break;
462 default:
463 st->discard= AVDISCARD_ALL;
465 if (stream_type) {
466 AVCodec *avc = avcodec_find_decoder(codec->codec_id);
467 mp_msg(MSGT_DEMUX, MSGL_INFO, "[lavf] stream %d: %s (%s), -%cid %d", i, stream_type, avc ? avc->name : "unknown", *stream_type, stream_id);
468 if (lang && lang->value && *stream_type != 'v')
469 mp_msg(MSGT_DEMUX, MSGL_INFO, ", -%clang %s", *stream_type, lang->value);
470 if (title && title->value)
471 mp_msg(MSGT_DEMUX, MSGL_INFO, ", %s", title->value);
472 mp_msg(MSGT_DEMUX, MSGL_INFO, "\n");
476 static demuxer_t* demux_open_lavf(demuxer_t *demuxer){
477 AVFormatContext *avfc;
478 AVFormatParameters ap;
479 const AVOption *opt;
480 AVMetadataTag *t = NULL;
481 lavf_priv_t *priv= demuxer->priv;
482 int i;
483 char mp_filename[256]="mp:";
485 memset(&ap, 0, sizeof(AVFormatParameters));
487 stream_seek(demuxer->stream, 0);
489 avfc = avformat_alloc_context();
491 if (opt_cryptokey)
492 parse_cryptokey(avfc, opt_cryptokey);
493 if (user_correct_pts != 0)
494 avfc->flags |= AVFMT_FLAG_GENPTS;
495 if (index_mode == 0)
496 avfc->flags |= AVFMT_FLAG_IGNIDX;
498 ap.prealloced_context = 1;
499 if(opt_probesize) {
500 opt = av_set_int(avfc, "probesize", opt_probesize);
501 if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option probesize to %u\n", opt_probesize);
503 if(opt_analyzeduration) {
504 opt = av_set_int(avfc, "analyzeduration", opt_analyzeduration * AV_TIME_BASE);
505 if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option analyzeduration to %u\n", opt_analyzeduration);
508 if(opt_avopt){
509 if(parse_avopts(avfc, opt_avopt) < 0){
510 mp_msg(MSGT_HEADER,MSGL_ERR, "Your options /%s/ look like gibberish to me pal\n", opt_avopt);
511 return NULL;
515 if(demuxer->stream->url) {
516 if (!strncmp(demuxer->stream->url, "ffmpeg://rtsp:", 14))
517 av_strlcpy(mp_filename, demuxer->stream->url + 9, sizeof(mp_filename));
518 else
519 av_strlcat(mp_filename, demuxer->stream->url, sizeof(mp_filename));
520 } else
521 av_strlcat(mp_filename, "foobar.dummy", sizeof(mp_filename));
523 priv->pb = av_alloc_put_byte(priv->buffer, BIO_BUFFER_SIZE, 0,
524 demuxer, mp_read, NULL, mp_seek);
525 priv->pb->read_seek = mp_read_seek;
526 priv->pb->is_streamed = !demuxer->stream->end_pos || (demuxer->stream->flags & MP_STREAM_SEEK) != MP_STREAM_SEEK;
528 if(av_open_input_stream(&avfc, priv->pb, mp_filename, priv->avif, &ap)<0){
529 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_open_input_stream() failed\n");
530 return NULL;
533 priv->avfc= avfc;
535 if(av_find_stream_info(avfc) < 0){
536 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_find_stream_info() failed\n");
537 return NULL;
540 /* Add metadata. */
541 av_metadata_conv(avfc, NULL, avfc->iformat->metadata_conv);
542 while((t = av_metadata_get(avfc->metadata, "", t, AV_METADATA_IGNORE_SUFFIX)))
543 demux_info_add(demuxer, t->key, t->value);
545 for(i=0; i < avfc->nb_chapters; i++) {
546 AVChapter *c = avfc->chapters[i];
547 uint64_t start = av_rescale_q(c->start, c->time_base, (AVRational){1,1000});
548 uint64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,1000});
549 t = av_metadata_get(c->metadata, "title", NULL, 0);
550 demuxer_add_chapter(demuxer, t ? t->value : NULL, start, end);
553 for(i=0; i<avfc->nb_streams; i++)
554 handle_stream(demuxer, avfc, i);
555 if(avfc->nb_programs) {
556 int p;
557 for (p = 0; p < avfc->nb_programs; p++) {
558 AVProgram *program = avfc->programs[p];
559 t = av_metadata_get(program->metadata, "title", NULL, 0);
560 mp_msg(MSGT_HEADER,MSGL_INFO,"LAVF: Program %d %s\n", program->id, t ? t->value : "");
564 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: %d audio and %d video streams found\n",priv->audio_streams,priv->video_streams);
565 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: build %d\n", LIBAVFORMAT_BUILD);
566 if(!priv->audio_streams) demuxer->audio->id=-2; // nosound
567 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio;
568 if(!priv->video_streams){
569 if(!priv->audio_streams){
570 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF: no audio or video headers found - broken file?\n");
571 return NULL;
573 demuxer->video->id=-2; // audio-only
574 } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video;
576 return demuxer;
579 static int demux_lavf_fill_buffer(demuxer_t *demux, demux_stream_t *dsds){
580 lavf_priv_t *priv= demux->priv;
581 AVPacket pkt;
582 demux_packet_t *dp;
583 demux_stream_t *ds;
584 int id;
585 mp_msg(MSGT_DEMUX,MSGL_DBG2,"demux_lavf_fill_buffer()\n");
587 demux->filepos=stream_tell(demux->stream);
589 if(av_read_frame(priv->avfc, &pkt) < 0)
590 return 0;
592 id= pkt.stream_index;
594 if(id==demux->audio->id){
595 // audio
596 ds=demux->audio;
597 if(!ds->sh){
598 ds->sh=demux->a_streams[id];
599 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected LAVF audio ID = %d\n",ds->id);
601 } else if(id==demux->video->id){
602 // video
603 ds=demux->video;
604 if(!ds->sh){
605 ds->sh=demux->v_streams[id];
606 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected LAVF video ID = %d\n",ds->id);
608 } else if(id==demux->sub->id){
609 // subtitle
610 ds=demux->sub;
611 sub_utf8=1;
612 } else {
613 av_free_packet(&pkt);
614 return 1;
617 if(0/*pkt.destruct == av_destruct_packet*/){
618 //ok kids, dont try this at home :)
619 dp=malloc(sizeof(demux_packet_t));
620 dp->len=pkt.size;
621 dp->next=NULL;
622 dp->refcount=1;
623 dp->master=NULL;
624 dp->buffer=pkt.data;
625 pkt.destruct= NULL;
626 }else{
627 dp=new_demux_packet(pkt.size);
628 memcpy(dp->buffer, pkt.data, pkt.size);
629 av_free_packet(&pkt);
632 if(pkt.pts != AV_NOPTS_VALUE){
633 dp->pts=pkt.pts * av_q2d(priv->avfc->streams[id]->time_base);
634 priv->last_pts= dp->pts * AV_TIME_BASE;
635 if(pkt.convergence_duration)
636 dp->endpts = dp->pts + pkt.convergence_duration * av_q2d(priv->avfc->streams[id]->time_base);
638 dp->pos=demux->filepos;
639 dp->flags= !!(pkt.flags&PKT_FLAG_KEY);
640 // append packet to DS stream:
641 ds_add_packet(ds,dp);
642 return 1;
645 static void demux_seek_lavf(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags){
646 lavf_priv_t *priv = demuxer->priv;
647 int avsflags = 0;
648 mp_msg(MSGT_DEMUX,MSGL_DBG2,"demux_seek_lavf(%p, %f, %f, %d)\n", demuxer, rel_seek_secs, audio_delay, flags);
650 if (flags & SEEK_ABSOLUTE) {
651 priv->last_pts = priv->avfc->start_time;
652 } else {
653 if (rel_seek_secs < 0) avsflags = AVSEEK_FLAG_BACKWARD;
655 if (flags & SEEK_FACTOR) {
656 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
657 return;
658 priv->last_pts += rel_seek_secs * priv->avfc->duration;
659 } else {
660 priv->last_pts += rel_seek_secs * AV_TIME_BASE;
662 if (av_seek_frame(priv->avfc, -1, priv->last_pts, avsflags) < 0) {
663 avsflags ^= AVSEEK_FLAG_BACKWARD;
664 av_seek_frame(priv->avfc, -1, priv->last_pts, avsflags);
668 static int demux_lavf_control(demuxer_t *demuxer, int cmd, void *arg)
670 lavf_priv_t *priv = demuxer->priv;
672 switch (cmd) {
673 case DEMUXER_CTRL_CORRECT_PTS:
674 if (!strcmp("matroska", priv->avif->name) ||
675 !strcmp("mpeg", priv->avif->name) ||
676 !strcmp("mpegts", priv->avif->name))
677 return DEMUXER_CTRL_NOTIMPL;
678 return DEMUXER_CTRL_OK;
679 case DEMUXER_CTRL_GET_TIME_LENGTH:
680 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
681 return DEMUXER_CTRL_DONTKNOW;
683 *((double *)arg) = (double)priv->avfc->duration / AV_TIME_BASE;
684 return DEMUXER_CTRL_OK;
686 case DEMUXER_CTRL_GET_PERCENT_POS:
687 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
688 return DEMUXER_CTRL_DONTKNOW;
690 *((int *)arg) = (int)((priv->last_pts - priv->avfc->start_time)*100 / priv->avfc->duration);
691 return DEMUXER_CTRL_OK;
692 case DEMUXER_CTRL_SWITCH_AUDIO:
693 case DEMUXER_CTRL_SWITCH_VIDEO:
695 int id = *((int*)arg);
696 int newid = -2;
697 int i, curridx = -1;
698 int nstreams, *pstreams;
699 demux_stream_t *ds;
701 if(cmd == DEMUXER_CTRL_SWITCH_VIDEO)
703 ds = demuxer->video;
704 nstreams = priv->video_streams;
705 pstreams = priv->vstreams;
707 else
709 ds = demuxer->audio;
710 nstreams = priv->audio_streams;
711 pstreams = priv->astreams;
713 for(i = 0; i < nstreams; i++)
715 if(pstreams[i] == ds->id) //current stream id
717 curridx = i;
718 break;
722 if(id == -2) { // no sound
723 i = -1;
724 } else if(id == -1) { // next track
725 i = (curridx + 2) % (nstreams + 1) - 1;
726 if (i >= 0)
727 newid = pstreams[i];
729 else // select track by id
731 if (id >= 0 && id < nstreams) {
732 i = id;
733 newid = pstreams[i];
736 if(i == curridx)
737 return DEMUXER_CTRL_NOTIMPL;
738 else
740 ds_free_packs(ds);
741 if(ds->id >= 0)
742 priv->avfc->streams[ds->id]->discard = AVDISCARD_ALL;
743 *((int*)arg) = ds->id = newid;
744 if(newid >= 0)
745 priv->avfc->streams[newid]->discard = AVDISCARD_NONE;
746 return DEMUXER_CTRL_OK;
749 case DEMUXER_CTRL_IDENTIFY_PROGRAM:
751 demux_program_t *prog = arg;
752 AVProgram *program;
753 int p, i;
754 int start;
756 prog->vid = prog->aid = prog->sid = -2; //no audio and no video by default
757 if(priv->avfc->nb_programs < 1)
758 return DEMUXER_CTRL_DONTKNOW;
760 if(prog->progid == -1)
762 p = 0;
763 while(p<priv->avfc->nb_programs && priv->avfc->programs[p]->id != priv->cur_program)
764 p++;
765 p = (p + 1) % priv->avfc->nb_programs;
767 else
769 for(i=0; i<priv->avfc->nb_programs; i++)
770 if(priv->avfc->programs[i]->id == prog->progid)
771 break;
772 if(i==priv->avfc->nb_programs)
773 return DEMUXER_CTRL_DONTKNOW;
774 p = i;
776 start = p;
777 redo:
778 program = priv->avfc->programs[p];
779 for(i=0; i<program->nb_stream_indexes; i++)
781 switch(priv->avfc->streams[program->stream_index[i]]->codec->codec_type)
783 case CODEC_TYPE_VIDEO:
784 if(prog->vid == -2)
785 prog->vid = program->stream_index[i];
786 break;
787 case CODEC_TYPE_AUDIO:
788 if(prog->aid == -2)
789 prog->aid = program->stream_index[i];
790 break;
791 case CODEC_TYPE_SUBTITLE:
792 if(prog->sid == -2 && priv->avfc->streams[program->stream_index[i]]->codec->codec_id == CODEC_ID_TEXT)
793 prog->sid = program->stream_index[i];
794 break;
797 if(prog->progid == -1 && prog->vid == -2 && prog->aid == -2)
799 p = (p + 1) % priv->avfc->nb_programs;
800 if (p == start)
801 return DEMUXER_CTRL_DONTKNOW;
802 goto redo;
804 priv->cur_program = prog->progid = program->id;
805 return DEMUXER_CTRL_OK;
807 default:
808 return DEMUXER_CTRL_NOTIMPL;
812 static void demux_close_lavf(demuxer_t *demuxer)
814 lavf_priv_t* priv = demuxer->priv;
815 if (priv){
816 if(priv->avfc)
818 av_freep(&priv->avfc->key);
819 av_close_input_stream(priv->avfc);
821 av_freep(&priv->pb);
822 free(priv); demuxer->priv= NULL;
827 const demuxer_desc_t demuxer_desc_lavf = {
828 "libavformat demuxer",
829 "lavf",
830 "libavformat",
831 "Michael Niedermayer",
832 "supports many formats, requires libavformat",
833 DEMUXER_TYPE_LAVF,
834 0, // Check after other demuxer
835 lavf_check_file,
836 demux_lavf_fill_buffer,
837 demux_open_lavf,
838 demux_close_lavf,
839 demux_seek_lavf,
840 demux_lavf_control
843 const demuxer_desc_t demuxer_desc_lavf_preferred = {
844 "libavformat preferred demuxer",
845 "lavfpref",
846 "libavformat",
847 "Michael Niedermayer",
848 "supports many formats, requires libavformat",
849 DEMUXER_TYPE_LAVF_PREFERRED,
851 lavf_check_preferred_file,
852 demux_lavf_fill_buffer,
853 demux_open_lavf,
854 demux_close_lavf,
855 demux_seek_lavf,
856 demux_lavf_control