demux_lavf: Print PROGRAM_ID -identify output similar to demux_ts
[mplayer/glamo.git] / libmpdemux / demux_lavf.c
blob3f1fd8955d3347c012a395a518563836d61c2cce
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>
25 #include <stdbool.h>
26 #include <string.h>
28 #include "config.h"
29 #include "options.h"
30 #include "mp_msg.h"
31 #include "av_opts.h"
32 #include "bstr.h"
34 #include "stream/stream.h"
35 #include "aviprint.h"
36 #include "demuxer.h"
37 #include "stheader.h"
38 #include "m_option.h"
39 #include "libvo/sub.h"
41 #include "libavformat/avformat.h"
42 #include "libavformat/avio.h"
43 #include "libavutil/avutil.h"
44 #include "libavutil/avstring.h"
45 #include "libavcodec/opt.h"
47 #include "mp_taglists.h"
49 #define INITIAL_PROBE_SIZE STREAM_BUFFER_SIZE
50 #define SMALL_MAX_PROBE_SIZE (32 * 1024)
51 #define PROBE_BUF_SIZE (2*1024*1024)
53 const m_option_t lavfdopts_conf[] = {
54 OPT_INTRANGE("probesize", lavfdopts.probesize, 0, 32, INT_MAX),
55 OPT_STRING("format", lavfdopts.format, 0),
56 OPT_INTRANGE("analyzeduration", lavfdopts.analyzeduration, 0, 0, INT_MAX),
57 OPT_STRING("cryptokey", lavfdopts.cryptokey, 0),
58 OPT_STRING("o", lavfdopts.avopt, 0),
59 {NULL, NULL, 0, 0, 0, 0, NULL}
62 #define BIO_BUFFER_SIZE 32768
64 typedef struct lavf_priv {
65 AVInputFormat *avif;
66 AVFormatContext *avfc;
67 ByteIOContext *pb;
68 uint8_t buffer[BIO_BUFFER_SIZE];
69 int audio_streams;
70 int video_streams;
71 int sub_streams;
72 int64_t last_pts;
73 int astreams[MAX_A_STREAMS];
74 int vstreams[MAX_V_STREAMS];
75 int sstreams[MAX_S_STREAMS];
76 int cur_program;
77 int nb_streams_last;
78 bool internet_radio_hack;
79 bool use_dts;
80 }lavf_priv_t;
82 static int mp_read(void *opaque, uint8_t *buf, int size) {
83 struct demuxer *demuxer = opaque;
84 struct stream *stream = demuxer->stream;
85 int ret;
87 ret=stream_read(stream, buf, size);
89 mp_msg(MSGT_HEADER,MSGL_DBG2,"%d=mp_read(%p, %p, %d), pos: %"PRId64", eof:%d\n",
90 ret, stream, buf, size, stream_tell(stream), stream->eof);
91 return ret;
94 static int64_t mp_seek(void *opaque, int64_t pos, int whence) {
95 struct demuxer *demuxer = opaque;
96 struct stream *stream = demuxer->stream;
97 int64_t current_pos;
98 mp_msg(MSGT_HEADER,MSGL_DBG2,"mp_seek(%p, %"PRId64", %d)\n", stream, pos, whence);
99 if(whence == SEEK_CUR)
100 pos +=stream_tell(stream);
101 else if(whence == SEEK_END && stream->end_pos > 0)
102 pos += stream->end_pos;
103 else if(whence == SEEK_SET)
104 pos += stream->start_pos;
105 else if(whence == AVSEEK_SIZE && stream->end_pos > 0)
106 return stream->end_pos - stream->start_pos;
107 else
108 return -1;
110 if(pos<0)
111 return -1;
112 current_pos = stream_tell(stream);
113 if(stream_seek(stream, pos)==0) {
114 stream_reset(stream);
115 stream_seek(stream, current_pos);
116 return -1;
119 return pos - stream->start_pos;
122 static int64_t mp_read_seek(void *opaque, int stream_idx, int64_t ts, int flags)
124 struct demuxer *demuxer = opaque;
125 struct stream *stream = demuxer->stream;
126 struct lavf_priv *priv = demuxer->priv;
128 AVStream *st = priv->avfc->streams[stream_idx];
129 double pts = (double)ts * st->time_base.num / st->time_base.den;
130 int ret = stream_control(stream, STREAM_CTRL_SEEK_TO_TIME, &pts);
131 if (ret < 0)
132 ret = AVERROR(ENOSYS);
133 return ret;
136 static void list_formats(void) {
137 mp_msg(MSGT_DEMUX, MSGL_INFO, "Available lavf input formats:\n");
138 AVInputFormat *fmt = NULL;
139 while (fmt = av_iformat_next(fmt))
140 mp_msg(MSGT_DEMUX, MSGL_INFO, "%15s : %s\n", fmt->name, fmt->long_name);
143 static int lavf_check_file(demuxer_t *demuxer){
144 struct MPOpts *opts = demuxer->opts;
145 struct lavfdopts *lavfdopts = &opts->lavfdopts;
146 AVProbeData avpd;
147 lavf_priv_t *priv;
148 int probe_data_size = 0;
149 int read_size = INITIAL_PROBE_SIZE;
150 int score;
152 if(!demuxer->priv)
153 demuxer->priv=calloc(sizeof(lavf_priv_t),1);
154 priv= demuxer->priv;
156 av_register_all();
158 char *format = lavfdopts->format;
159 if (!format)
160 format = demuxer->stream->lavf_type;
161 if (format) {
162 if (strcmp(format, "help") == 0) {
163 list_formats();
164 return 0;
166 priv->avif = av_find_input_format(format);
167 if (!priv->avif) {
168 mp_msg(MSGT_DEMUX, MSGL_FATAL, "Unknown lavf format %s\n", format);
169 return 0;
171 mp_msg(MSGT_DEMUX,MSGL_INFO,"Forced lavf %s demuxer\n", priv->avif->long_name);
172 return DEMUXER_TYPE_LAVF;
175 avpd.buf = av_mallocz(FFMAX(BIO_BUFFER_SIZE, PROBE_BUF_SIZE) +
176 FF_INPUT_BUFFER_PADDING_SIZE);
177 do {
178 read_size = stream_read(demuxer->stream, avpd.buf + probe_data_size, read_size);
179 if(read_size < 0) {
180 av_free(avpd.buf);
181 return 0;
183 probe_data_size += read_size;
184 avpd.filename= demuxer->stream->url;
185 if (!avpd.filename) {
186 mp_msg(MSGT_DEMUX, MSGL_WARN, "Stream url is not set!\n");
187 avpd.filename = "";
189 if (!strncmp(avpd.filename, "ffmpeg://", 9))
190 avpd.filename += 9;
191 avpd.buf_size= probe_data_size;
193 score = 0;
194 priv->avif= av_probe_input_format2(&avpd, probe_data_size > 0, &score);
195 read_size = FFMIN(2*read_size, PROBE_BUF_SIZE - probe_data_size);
196 } while ((demuxer->desc->type != DEMUXER_TYPE_LAVF_PREFERRED ||
197 probe_data_size < SMALL_MAX_PROBE_SIZE) &&
198 score <= AVPROBE_SCORE_MAX / 4 &&
199 read_size > 0 && probe_data_size < PROBE_BUF_SIZE);
200 av_free(avpd.buf);
202 if(!priv->avif){
203 mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: no clue about this gibberish!\n");
204 return 0;
205 }else
206 mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: %s\n", priv->avif->long_name);
208 return DEMUXER_TYPE_LAVF;
211 static bool matches_avinputformat_name(struct lavf_priv *priv,
212 const char *name)
214 const char *avifname = priv->avif->name;
215 while (1) {
216 const char *next = strchr(avifname, ',');
217 if (!next)
218 return !strcmp(avifname, name);
219 int len = next - avifname;
220 if (len == strlen(name) && !memcmp(avifname, name, len))
221 return true;
222 avifname = next + 1;
226 static const char * const preferred_list[] = {
227 "dxa",
228 "flv",
229 "gxf",
230 "nut",
231 "nuv",
232 "mov", "mp4", // "mov,mp4,m4a,3gp,3g2,mj2" is one AVInputFormat
233 "mpc",
234 "mpc8",
235 "mxf",
236 "ogg",
237 "swf",
238 "vqf",
239 "w64",
240 "wv",
241 NULL
244 static int lavf_check_preferred_file(demuxer_t *demuxer){
245 if (lavf_check_file(demuxer)) {
246 const char * const *p;
247 lavf_priv_t *priv = demuxer->priv;
248 for (p = preferred_list; *p; p++)
249 if (matches_avinputformat_name(priv, *p))
250 return DEMUXER_TYPE_LAVF_PREFERRED;
252 return 0;
255 static uint8_t char2int(char c) {
256 if (c >= '0' && c <= '9') return c - '0';
257 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
258 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
259 return 0;
262 static void parse_cryptokey(AVFormatContext *avfc, const char *str) {
263 int len = strlen(str) / 2;
264 uint8_t *key = av_mallocz(len);
265 int i;
266 avfc->keylen = len;
267 avfc->key = key;
268 for (i = 0; i < len; i++, str += 2)
269 *key++ = (char2int(str[0]) << 4) | char2int(str[1]);
272 static void handle_stream(demuxer_t *demuxer, AVFormatContext *avfc, int i) {
273 lavf_priv_t *priv= demuxer->priv;
274 AVStream *st= avfc->streams[i];
275 AVCodecContext *codec= st->codec;
276 char *stream_type = NULL;
277 int stream_id;
278 AVMetadataTag *lang = av_metadata_get(st->metadata, "language", NULL, 0);
279 AVMetadataTag *title= av_metadata_get(st->metadata, "title", NULL, 0);
280 int g, override_tag = mp_av_codec_get_tag(mp_codecid_override_taglists,
281 codec->codec_id);
282 // For some formats (like PCM) always trust CODEC_ID_* more than codec_tag
283 if (override_tag)
284 codec->codec_tag = override_tag;
286 switch(codec->codec_type){
287 case CODEC_TYPE_AUDIO:{
288 WAVEFORMATEX *wf;
289 sh_audio_t* sh_audio;
290 sh_audio = new_sh_audio_aid(demuxer, i, priv->audio_streams);
291 if(!sh_audio)
292 break;
293 stream_type = "audio";
294 priv->astreams[priv->audio_streams] = i;
295 wf= calloc(sizeof(*wf) + codec->extradata_size, 1);
296 // mp4a tag is used for all mp4 files no matter what they actually contain
297 if(codec->codec_tag == MKTAG('m', 'p', '4', 'a'))
298 codec->codec_tag= 0;
299 if(!codec->codec_tag)
300 codec->codec_tag= mp_av_codec_get_tag(mp_wav_taglists, codec->codec_id);
301 wf->wFormatTag= codec->codec_tag;
302 wf->nChannels= codec->channels;
303 wf->nSamplesPerSec= codec->sample_rate;
304 wf->nAvgBytesPerSec= codec->bit_rate/8;
305 wf->nBlockAlign= codec->block_align ? codec->block_align : 1;
306 wf->wBitsPerSample= codec->bits_per_coded_sample;
307 wf->cbSize= codec->extradata_size;
308 if(codec->extradata_size)
309 memcpy(wf + 1, codec->extradata, codec->extradata_size);
310 sh_audio->wf= wf;
311 sh_audio->audio.dwSampleSize= codec->block_align;
312 if(codec->frame_size && codec->sample_rate){
313 sh_audio->audio.dwScale=codec->frame_size;
314 sh_audio->audio.dwRate= codec->sample_rate;
315 }else{
316 sh_audio->audio.dwScale= codec->block_align ? codec->block_align*8 : 8;
317 sh_audio->audio.dwRate = codec->bit_rate;
319 g= av_gcd(sh_audio->audio.dwScale, sh_audio->audio.dwRate);
320 sh_audio->audio.dwScale /= g;
321 sh_audio->audio.dwRate /= g;
322 // 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);
323 sh_audio->ds= demuxer->audio;
324 sh_audio->format= codec->codec_tag;
325 sh_audio->channels= codec->channels;
326 sh_audio->samplerate= codec->sample_rate;
327 sh_audio->i_bps= codec->bit_rate/8;
328 switch (codec->codec_id) {
329 case CODEC_ID_PCM_S8:
330 case CODEC_ID_PCM_U8:
331 sh_audio->samplesize = 1;
332 break;
333 case CODEC_ID_PCM_S16LE:
334 case CODEC_ID_PCM_S16BE:
335 case CODEC_ID_PCM_U16LE:
336 case CODEC_ID_PCM_U16BE:
337 sh_audio->samplesize = 2;
338 break;
339 case CODEC_ID_PCM_ALAW:
340 sh_audio->format = 0x6;
341 break;
342 case CODEC_ID_PCM_MULAW:
343 sh_audio->format = 0x7;
344 break;
346 if (title && title->value)
347 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_NAME=%s\n", priv->audio_streams, title->value);
348 if (lang && lang->value) {
349 sh_audio->lang = strdup(lang->value);
350 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_LANG=%s\n", priv->audio_streams, sh_audio->lang);
352 if (st->disposition & AV_DISPOSITION_DEFAULT)
353 sh_audio->default_track = 1;
354 if(mp_msg_test(MSGT_HEADER,MSGL_V) ) print_wave_header(sh_audio->wf, MSGL_V);
355 // select the first audio stream
356 if (!demuxer->audio->sh) {
357 demuxer->audio->id = i;
358 demuxer->audio->sh= demuxer->a_streams[i];
359 } else
360 st->discard= AVDISCARD_ALL;
361 stream_id = priv->audio_streams++;
362 break;
364 case CODEC_TYPE_VIDEO:{
365 sh_video_t* sh_video;
366 BITMAPINFOHEADER *bih;
367 sh_video=new_sh_video_vid(demuxer, i, priv->video_streams);
368 if(!sh_video) break;
369 stream_type = "video";
370 priv->vstreams[priv->video_streams] = i;
371 bih=calloc(sizeof(*bih) + codec->extradata_size,1);
373 if(codec->codec_id == CODEC_ID_RAWVIDEO) {
374 switch (codec->pix_fmt) {
375 case PIX_FMT_RGB24:
376 codec->codec_tag= MKTAG(24, 'B', 'G', 'R');
379 if(!codec->codec_tag)
380 codec->codec_tag= mp_av_codec_get_tag(mp_bmp_taglists, codec->codec_id);
381 bih->biSize= sizeof(*bih) + codec->extradata_size;
382 bih->biWidth= codec->width;
383 bih->biHeight= codec->height;
384 bih->biBitCount= codec->bits_per_coded_sample;
385 bih->biSizeImage = bih->biWidth * bih->biHeight * bih->biBitCount/8;
386 bih->biCompression= codec->codec_tag;
387 sh_video->bih= bih;
388 sh_video->disp_w= codec->width;
389 sh_video->disp_h= codec->height;
390 if (st->time_base.den) { /* if container has time_base, use that */
391 sh_video->video.dwRate= st->time_base.den;
392 sh_video->video.dwScale= st->time_base.num;
393 } else {
394 sh_video->video.dwRate= codec->time_base.den;
395 sh_video->video.dwScale= codec->time_base.num;
397 sh_video->fps=av_q2d(st->r_frame_rate);
398 sh_video->frametime=1/av_q2d(st->r_frame_rate);
399 sh_video->format=bih->biCompression;
400 if(st->sample_aspect_ratio.num)
401 sh_video->aspect = codec->width * st->sample_aspect_ratio.num
402 / (float)(codec->height * st->sample_aspect_ratio.den);
403 else
404 sh_video->aspect=codec->width * codec->sample_aspect_ratio.num
405 / (float)(codec->height * codec->sample_aspect_ratio.den);
406 sh_video->i_bps=codec->bit_rate/8;
407 if (title && title->value)
408 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VID_%d_NAME=%s\n", priv->video_streams, title->value);
409 mp_msg(MSGT_DEMUX,MSGL_DBG2,"aspect= %d*%d/(%d*%d)\n",
410 codec->width, codec->sample_aspect_ratio.num,
411 codec->height, codec->sample_aspect_ratio.den);
413 sh_video->ds= demuxer->video;
414 if(codec->extradata_size)
415 memcpy(sh_video->bih + 1, codec->extradata, codec->extradata_size);
416 if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_video_header(sh_video->bih, MSGL_V);
418 short biPlanes;
419 int biXPelsPerMeter;
420 int biYPelsPerMeter;
421 int biClrUsed;
422 int biClrImportant;
424 if(demuxer->video->id != i && demuxer->video->id != -1)
425 st->discard= AVDISCARD_ALL;
426 else{
427 demuxer->video->id = i;
428 demuxer->video->sh= demuxer->v_streams[i];
430 stream_id = priv->video_streams++;
431 break;
433 case CODEC_TYPE_SUBTITLE:{
434 sh_sub_t* sh_sub;
435 char type;
436 /* only support text subtitles for now */
437 if(codec->codec_id == CODEC_ID_TEXT)
438 type = 't';
439 else if(codec->codec_id == CODEC_ID_MOV_TEXT)
440 type = 'm';
441 else if(codec->codec_id == CODEC_ID_SSA)
442 type = 'a';
443 else if(codec->codec_id == CODEC_ID_DVD_SUBTITLE)
444 type = 'v';
445 else if(codec->codec_id == CODEC_ID_XSUB)
446 type = 'x';
447 else if(codec->codec_id == CODEC_ID_DVB_SUBTITLE)
448 type = 'b';
449 else if(codec->codec_id == CODEC_ID_DVB_TELETEXT)
450 type = 'd';
451 else if(codec->codec_id == CODEC_ID_HDMV_PGS_SUBTITLE)
452 type = 'p';
453 else
454 break;
455 sh_sub = new_sh_sub_sid(demuxer, i, priv->sub_streams);
456 if(!sh_sub) break;
457 stream_type = "subtitle";
458 priv->sstreams[priv->sub_streams] = i;
459 sh_sub->type = type;
460 if (codec->extradata_size) {
461 sh_sub->extradata = malloc(codec->extradata_size);
462 memcpy(sh_sub->extradata, codec->extradata, codec->extradata_size);
463 sh_sub->extradata_len = codec->extradata_size;
465 if (title && title->value)
466 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_NAME=%s\n", priv->sub_streams, title->value);
467 if (lang && lang->value) {
468 sh_sub->lang = strdup(lang->value);
469 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_LANG=%s\n", priv->sub_streams, sh_sub->lang);
471 if (st->disposition & AV_DISPOSITION_DEFAULT)
472 sh_sub->default_track = 1;
473 stream_id = priv->sub_streams++;
474 break;
476 case CODEC_TYPE_ATTACHMENT:{
477 if (st->codec->codec_id == CODEC_ID_TTF)
478 demuxer_add_attachment(demuxer, BSTR(st->filename),
479 BSTR("application/x-truetype-font"),
480 (struct bstr){codec->extradata,
481 codec->extradata_size});
482 break;
484 default:
485 st->discard= AVDISCARD_ALL;
487 if (stream_type) {
488 AVCodec *avc = avcodec_find_decoder(codec->codec_id);
489 const char *codec_name = avc ? avc->name : "unknown";
490 if (!avc && *stream_type == 's' && demuxer->s_streams[stream_id])
491 codec_name = sh_sub_type2str(((sh_sub_t *)demuxer->s_streams[stream_id])->type);
492 mp_msg(MSGT_DEMUX, MSGL_INFO, "[lavf] stream %d: %s (%s), -%cid %d", i, stream_type, codec_name, *stream_type, stream_id);
493 if (lang && lang->value && *stream_type != 'v')
494 mp_msg(MSGT_DEMUX, MSGL_INFO, ", -%clang %s", *stream_type, lang->value);
495 if (title && title->value)
496 mp_msg(MSGT_DEMUX, MSGL_INFO, ", %s", title->value);
497 mp_msg(MSGT_DEMUX, MSGL_INFO, "\n");
501 static demuxer_t* demux_open_lavf(demuxer_t *demuxer){
502 struct MPOpts *opts = demuxer->opts;
503 struct lavfdopts *lavfdopts = &opts->lavfdopts;
504 AVFormatContext *avfc;
505 AVFormatParameters ap;
506 const AVOption *opt;
507 AVMetadataTag *t = NULL;
508 lavf_priv_t *priv= demuxer->priv;
509 int i;
510 char mp_filename[256]="mp:";
512 memset(&ap, 0, sizeof(AVFormatParameters));
514 stream_seek(demuxer->stream, 0);
516 avfc = avformat_alloc_context();
518 if (lavfdopts->cryptokey)
519 parse_cryptokey(avfc, lavfdopts->cryptokey);
520 if (matches_avinputformat_name(priv, "avi")) {
521 /* for avi libavformat returns the avi timestamps in .dts,
522 * some made-up stuff that's not really pts in .pts */
523 priv->use_dts = true;
524 } else {
525 if (opts->user_correct_pts != 0)
526 avfc->flags |= AVFMT_FLAG_GENPTS;
528 if (index_mode == 0)
529 avfc->flags |= AVFMT_FLAG_IGNIDX;
531 ap.prealloced_context = 1;
532 if (lavfdopts->probesize) {
533 opt = av_set_int(avfc, "probesize", lavfdopts->probesize);
534 if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option probesize to %u\n", lavfdopts->probesize);
536 if (lavfdopts->analyzeduration) {
537 opt = av_set_int(avfc, "analyzeduration",
538 lavfdopts->analyzeduration * AV_TIME_BASE);
539 if (!opt)
540 mp_msg(MSGT_HEADER, MSGL_ERR, "demux_lavf, couldn't set option "
541 "analyzeduration to %u\n", lavfdopts->analyzeduration);
544 if (lavfdopts->avopt){
545 if(parse_avopts(avfc, lavfdopts->avopt) < 0){
546 mp_msg(MSGT_HEADER,MSGL_ERR, "Your options /%s/ look like gibberish to me pal\n", lavfdopts->avopt);
547 return NULL;
551 if(demuxer->stream->url) {
552 if (!strncmp(demuxer->stream->url, "ffmpeg://rtsp:", 14))
553 av_strlcpy(mp_filename, demuxer->stream->url + 9, sizeof(mp_filename));
554 else
555 av_strlcat(mp_filename, demuxer->stream->url, sizeof(mp_filename));
556 } else
557 av_strlcat(mp_filename, "foobar.dummy", sizeof(mp_filename));
559 priv->pb = av_alloc_put_byte(priv->buffer, BIO_BUFFER_SIZE, 0,
560 demuxer, mp_read, NULL, mp_seek);
561 priv->pb->read_seek = mp_read_seek;
562 priv->pb->is_streamed = !demuxer->stream->end_pos || (demuxer->stream->flags & MP_STREAM_SEEK) != MP_STREAM_SEEK;
564 if(av_open_input_stream(&avfc, priv->pb, mp_filename, priv->avif, &ap)<0){
565 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_open_input_stream() failed\n");
566 return NULL;
569 priv->avfc= avfc;
571 if(av_find_stream_info(avfc) < 0){
572 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_find_stream_info() failed\n");
573 return NULL;
576 /* Add metadata. */
577 av_metadata_conv(avfc, NULL, avfc->iformat->metadata_conv);
578 while((t = av_metadata_get(avfc->metadata, "", t, AV_METADATA_IGNORE_SUFFIX)))
579 demux_info_add(demuxer, t->key, t->value);
581 for(i=0; i < avfc->nb_chapters; i++) {
582 AVChapter *c = avfc->chapters[i];
583 uint64_t start = av_rescale_q(c->start, c->time_base, (AVRational){1,1000});
584 uint64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,1000});
585 t = av_metadata_get(c->metadata, "title", NULL, 0);
586 demuxer_add_chapter(demuxer, t ? BSTR(t->value) : BSTR(NULL), start, end);
589 for(i=0; i<avfc->nb_streams; i++)
590 handle_stream(demuxer, avfc, i);
591 priv->nb_streams_last = avfc->nb_streams;
593 if(avfc->nb_programs) {
594 int p;
595 for (p = 0; p < avfc->nb_programs; p++) {
596 AVProgram *program = avfc->programs[p];
597 t = av_metadata_get(program->metadata, "title", NULL, 0);
598 mp_msg(MSGT_HEADER,MSGL_INFO,"LAVF: Program %d %s\n", program->id, t ? t->value : "");
599 mp_msg(MSGT_IDENTIFY, MSGL_V, "PROGRAM_ID=%d\n", program->id);
603 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: %d audio and %d video streams found\n",priv->audio_streams,priv->video_streams);
604 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: build %d\n", LIBAVFORMAT_BUILD);
605 if(!priv->audio_streams) demuxer->audio->id=-2; // nosound
606 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio;
607 if(!priv->video_streams){
608 if(!priv->audio_streams){
609 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF: no audio or video headers found - broken file?\n");
610 return NULL;
612 demuxer->video->id=-2; // audio-only
613 } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video;
615 demuxer->accurate_seek = true;
617 return demuxer;
620 static void check_internet_radio_hack(struct demuxer *demuxer)
622 struct lavf_priv *priv = demuxer->priv;
623 struct AVFormatContext *avfc = priv->avfc;
625 if (!matches_avinputformat_name(priv, "ogg"))
626 return;
627 if (priv->nb_streams_last == avfc->nb_streams)
628 return;
629 if (avfc->nb_streams - priv->nb_streams_last == 1
630 && priv->video_streams == 0 && priv->sub_streams == 0
631 && demuxer->a_streams[priv->audio_streams-1]->format == 0x566f // vorbis
632 && (priv->audio_streams == 2 || priv->internet_radio_hack)
633 && demuxer->a_streams[0]->format == 0x566f) {
634 // extradata match could be checked but would require parsing
635 // headers, as the comment section will vary
636 if (!priv->internet_radio_hack) {
637 mp_msg(MSGT_DEMUX, MSGL_V,
638 "[lavf] enabling internet ogg radio hack\n");
639 #if LIBAVFORMAT_VERSION_MAJOR < 53
640 mp_tmsg(MSGT_DEMUX, MSGL_WARN, "[lavf] This looks like an "
641 "internet radio ogg stream with track changes.\n"
642 "Playback will likely fail after %d track changes "
643 "due to libavformat limitations.\n"
644 "You may be able to work around that limitation by "
645 "using -demuxer ogg.\n", MAX_STREAMS);
646 #endif
648 #if LIBAVFORMAT_VERSION_MAJOR < 53
649 if (avfc->nb_streams == MAX_STREAMS) {
650 mp_tmsg(MSGT_DEMUX, MSGL_WARN, "[lavf] This is the %dth "
651 "track.\nPlayback will likely fail at the next change.\n"
652 "You may be able to work around this limitation by "
653 "using -demuxer ogg.\n", MAX_STREAMS);
655 #endif
656 priv->internet_radio_hack = true;
657 // use new per-track metadata as global metadata
658 AVMetadataTag *t = NULL;
659 AVStream *stream = avfc->streams[avfc->nb_streams - 1];
660 while ((t = av_metadata_get(stream->metadata, "", t,
661 AV_METADATA_IGNORE_SUFFIX)))
662 demux_info_add(demuxer, t->key, t->value);
663 } else {
664 if (priv->internet_radio_hack)
665 mp_tmsg(MSGT_DEMUX, MSGL_WARN, "[lavf] Internet radio ogg hack "
666 "was enabled, but stream characteristics changed.\n"
667 "This may or may not work.\n");
668 priv->internet_radio_hack = false;
672 static int demux_lavf_fill_buffer(demuxer_t *demux, demux_stream_t *dsds){
673 lavf_priv_t *priv= demux->priv;
674 AVPacket pkt;
675 demux_packet_t *dp;
676 demux_stream_t *ds;
677 int id;
678 mp_msg(MSGT_DEMUX,MSGL_DBG2,"demux_lavf_fill_buffer()\n");
680 demux->filepos=stream_tell(demux->stream);
682 if(av_read_frame(priv->avfc, &pkt) < 0)
683 return 0;
685 // handle any new streams that might have been added
686 for (id = priv->nb_streams_last; id < priv->avfc->nb_streams; id++)
687 handle_stream(demux, priv->avfc, id);
688 check_internet_radio_hack(demux);
690 priv->nb_streams_last = priv->avfc->nb_streams;
692 id= pkt.stream_index;
694 if (id == demux->audio->id || priv->internet_radio_hack) {
695 // audio
696 ds=demux->audio;
697 if(!ds->sh){
698 ds->sh=demux->a_streams[id];
699 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected LAVF audio ID = %d\n",ds->id);
701 } else if(id==demux->video->id){
702 // video
703 ds=demux->video;
704 if(!ds->sh){
705 ds->sh=demux->v_streams[id];
706 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected LAVF video ID = %d\n",ds->id);
708 } else if(id==demux->sub->id){
709 // subtitle
710 ds=demux->sub;
711 sub_utf8=1;
712 } else {
713 av_free_packet(&pkt);
714 return 1;
717 if(0/*pkt.destruct == av_destruct_packet*/){
718 //ok kids, dont try this at home :)
719 dp=malloc(sizeof(demux_packet_t));
720 dp->len=pkt.size;
721 dp->next=NULL;
722 dp->refcount=1;
723 dp->master=NULL;
724 dp->buffer=pkt.data;
725 pkt.destruct= NULL;
726 }else{
727 dp=new_demux_packet(pkt.size);
728 memcpy(dp->buffer, pkt.data, pkt.size);
729 av_free_packet(&pkt);
732 int64_t ts = priv->use_dts ? pkt.dts : pkt.pts;
733 if(ts != AV_NOPTS_VALUE){
734 dp->pts = ts * av_q2d(priv->avfc->streams[id]->time_base);
735 priv->last_pts= dp->pts * AV_TIME_BASE;
736 // always set endpts for subtitles, even if PKT_FLAG_KEY is not set,
737 // otherwise they will stay on screen to long if e.g. ASS is demuxed from mkv
738 if((ds == demux->sub || (pkt.flags & PKT_FLAG_KEY)) &&
739 pkt.convergence_duration > 0)
740 dp->endpts = dp->pts + pkt.convergence_duration * av_q2d(priv->avfc->streams[id]->time_base);
742 dp->pos=demux->filepos;
743 dp->flags= !!(pkt.flags&PKT_FLAG_KEY);
744 // append packet to DS stream:
745 ds_add_packet(ds,dp);
746 return 1;
749 static void demux_seek_lavf(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags){
750 lavf_priv_t *priv = demuxer->priv;
751 int avsflags = 0;
752 mp_msg(MSGT_DEMUX,MSGL_DBG2,"demux_seek_lavf(%p, %f, %f, %d)\n", demuxer, rel_seek_secs, audio_delay, flags);
754 if (flags & SEEK_ABSOLUTE) {
755 priv->last_pts = 0;
756 } else {
757 if (rel_seek_secs < 0) avsflags = AVSEEK_FLAG_BACKWARD;
759 if (flags & SEEK_FORWARD)
760 avsflags = 0;
761 else if (flags & SEEK_BACKWARD)
762 avsflags = AVSEEK_FLAG_BACKWARD;
763 if (flags & SEEK_FACTOR) {
764 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
765 return;
766 priv->last_pts += rel_seek_secs * priv->avfc->duration;
767 } else {
768 priv->last_pts += rel_seek_secs * AV_TIME_BASE;
770 if (av_seek_frame(priv->avfc, -1, priv->last_pts, avsflags) < 0) {
771 avsflags ^= AVSEEK_FLAG_BACKWARD;
772 av_seek_frame(priv->avfc, -1, priv->last_pts, avsflags);
776 static int demux_lavf_control(demuxer_t *demuxer, int cmd, void *arg)
778 lavf_priv_t *priv = demuxer->priv;
780 switch (cmd) {
781 case DEMUXER_CTRL_CORRECT_PTS:
782 return DEMUXER_CTRL_OK;
783 case DEMUXER_CTRL_GET_TIME_LENGTH:
784 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
785 return DEMUXER_CTRL_DONTKNOW;
787 *((double *)arg) = (double)priv->avfc->duration / AV_TIME_BASE;
788 return DEMUXER_CTRL_OK;
790 case DEMUXER_CTRL_GET_PERCENT_POS:
791 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
792 return DEMUXER_CTRL_DONTKNOW;
794 *((int *)arg) = (int)((priv->last_pts - priv->avfc->start_time)*100 / priv->avfc->duration);
795 return DEMUXER_CTRL_OK;
796 case DEMUXER_CTRL_SWITCH_AUDIO:
797 case DEMUXER_CTRL_SWITCH_VIDEO:
799 int id = *((int*)arg);
800 int newid = -2;
801 int i, curridx = -1;
802 int nstreams, *pstreams;
803 demux_stream_t *ds;
805 if(cmd == DEMUXER_CTRL_SWITCH_VIDEO)
807 ds = demuxer->video;
808 nstreams = priv->video_streams;
809 pstreams = priv->vstreams;
811 else
813 ds = demuxer->audio;
814 nstreams = priv->audio_streams;
815 pstreams = priv->astreams;
817 for(i = 0; i < nstreams; i++)
819 if(pstreams[i] == ds->id) //current stream id
821 curridx = i;
822 break;
826 if(id == -2) { // no sound
827 i = -1;
828 } else if(id == -1) { // next track
829 i = (curridx + 2) % (nstreams + 1) - 1;
830 if (i >= 0)
831 newid = pstreams[i];
833 else // select track by id
835 if (id >= 0 && id < nstreams) {
836 i = id;
837 newid = pstreams[i];
840 if (i == curridx) {
841 *(int *) arg = curridx;
842 return DEMUXER_CTRL_OK;
843 } else {
844 ds_free_packs(ds);
845 if(ds->id >= 0)
846 priv->avfc->streams[ds->id]->discard = AVDISCARD_ALL;
847 ds->id = newid;
848 *(int *) arg = i < 0 ? -2 : i;
849 if(newid >= 0)
850 priv->avfc->streams[newid]->discard = AVDISCARD_NONE;
851 return DEMUXER_CTRL_OK;
854 case DEMUXER_CTRL_IDENTIFY_PROGRAM:
856 demux_program_t *prog = arg;
857 AVProgram *program;
858 int p, i;
859 int start;
861 prog->vid = prog->aid = prog->sid = -2; //no audio and no video by default
862 if(priv->avfc->nb_programs < 1)
863 return DEMUXER_CTRL_DONTKNOW;
865 if(prog->progid == -1)
867 p = 0;
868 while(p<priv->avfc->nb_programs && priv->avfc->programs[p]->id != priv->cur_program)
869 p++;
870 p = (p + 1) % priv->avfc->nb_programs;
872 else
874 for(i=0; i<priv->avfc->nb_programs; i++)
875 if(priv->avfc->programs[i]->id == prog->progid)
876 break;
877 if(i==priv->avfc->nb_programs)
878 return DEMUXER_CTRL_DONTKNOW;
879 p = i;
881 start = p;
882 redo:
883 program = priv->avfc->programs[p];
884 for(i=0; i<program->nb_stream_indexes; i++)
886 switch(priv->avfc->streams[program->stream_index[i]]->codec->codec_type)
888 case CODEC_TYPE_VIDEO:
889 if(prog->vid == -2)
890 prog->vid = program->stream_index[i];
891 break;
892 case CODEC_TYPE_AUDIO:
893 if(prog->aid == -2)
894 prog->aid = program->stream_index[i];
895 break;
896 case CODEC_TYPE_SUBTITLE:
897 if(prog->sid == -2 && priv->avfc->streams[program->stream_index[i]]->codec->codec_id == CODEC_ID_TEXT)
898 prog->sid = program->stream_index[i];
899 break;
902 if(prog->progid == -1 && prog->vid == -2 && prog->aid == -2)
904 p = (p + 1) % priv->avfc->nb_programs;
905 if (p == start)
906 return DEMUXER_CTRL_DONTKNOW;
907 goto redo;
909 priv->cur_program = prog->progid = program->id;
910 return DEMUXER_CTRL_OK;
912 default:
913 return DEMUXER_CTRL_NOTIMPL;
917 static void demux_close_lavf(demuxer_t *demuxer)
919 lavf_priv_t* priv = demuxer->priv;
920 if (priv){
921 if(priv->avfc)
923 av_freep(&priv->avfc->key);
924 av_close_input_stream(priv->avfc);
926 av_freep(&priv->pb);
927 free(priv); demuxer->priv= NULL;
932 const demuxer_desc_t demuxer_desc_lavf = {
933 "libavformat demuxer",
934 "lavf",
935 "libavformat",
936 "Michael Niedermayer",
937 "supports many formats, requires libavformat",
938 DEMUXER_TYPE_LAVF,
939 0, // Check after other demuxer
940 lavf_check_file,
941 demux_lavf_fill_buffer,
942 demux_open_lavf,
943 demux_close_lavf,
944 demux_seek_lavf,
945 demux_lavf_control
948 const demuxer_desc_t demuxer_desc_lavf_preferred = {
949 "libavformat preferred demuxer",
950 "lavfpref",
951 "libavformat",
952 "Michael Niedermayer",
953 "supports many formats, requires libavformat",
954 DEMUXER_TYPE_LAVF_PREFERRED,
956 lavf_check_preferred_file,
957 demux_lavf_fill_buffer,
958 demux_open_lavf,
959 demux_close_lavf,
960 demux_seek_lavf,
961 demux_lavf_control