Merge svn changes up to r27092
[mplayer.git] / libmpdemux / demux_lavf.c
blob96db00a4efbc7b1ec3f03999b9aec678db0439e6
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 "options.h"
28 #include "mp_msg.h"
29 #include "help_mp.h"
30 #include "av_opts.h"
32 #include "stream/stream.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 "libavutil/avutil.h"
40 #include "libavformat/avi.h"
41 #include "libavcodec/opt.h"
42 #include "libavformat/riff.h"
44 #include "mp_taglists.h"
46 #define PROBE_BUF_SIZE 2048
48 static unsigned int opt_probesize = 0;
49 static unsigned int opt_analyzeduration = 0;
50 static char *opt_format;
51 static char *opt_cryptokey;
52 extern int ts_prog;
53 static char *opt_avopt = NULL;
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 {"o", &opt_avopt, CONF_TYPE_STRING, 0, 0, 0, NULL},
61 {NULL, NULL, 0, 0, 0, 0, NULL}
64 #define BIO_BUFFER_SIZE 32768
66 typedef struct lavf_priv_t{
67 AVInputFormat *avif;
68 AVFormatContext *avfc;
69 ByteIOContext *pb;
70 uint8_t buffer[BIO_BUFFER_SIZE];
71 int audio_streams;
72 int video_streams;
73 int sub_streams;
74 int64_t last_pts;
75 int astreams[MAX_A_STREAMS];
76 int vstreams[MAX_V_STREAMS];
77 int sstreams[MAX_S_STREAMS];
78 int cur_program;
79 }lavf_priv_t;
81 extern void print_wave_header(WAVEFORMATEX *h, int verbose_level);
82 extern void print_video_header(BITMAPINFOHEADER *h, int verbose_level);
84 static int mp_read(void *opaque, uint8_t *buf, int size) {
85 stream_t *stream = opaque;
86 int ret;
88 if(stream_eof(stream)) //needed?
89 return -1;
90 ret=stream_read(stream, buf, size);
92 mp_msg(MSGT_HEADER,MSGL_DBG2,"%d=mp_read(%p, %p, %d), eof:%d\n", ret, stream, buf, size, stream->eof);
93 return ret;
96 static offset_t mp_seek(void *opaque, offset_t pos, int whence) {
97 stream_t *stream = opaque;
98 mp_msg(MSGT_HEADER,MSGL_DBG2,"mp_seek(%p, %d, %d)\n", stream, (int)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 if(pos<stream->end_pos && stream->eof)
113 stream_reset(stream);
114 if(stream_seek(stream, pos)==0)
115 return -1;
117 return pos - stream->start_pos;
120 static void list_formats(void) {
121 AVInputFormat *fmt;
122 mp_msg(MSGT_DEMUX, MSGL_INFO, "Available lavf input formats:\n");
123 for (fmt = first_iformat; fmt; fmt = fmt->next)
124 mp_msg(MSGT_DEMUX, MSGL_INFO, "%15s : %s\n", fmt->name, fmt->long_name);
127 static int lavf_check_file(demuxer_t *demuxer){
128 AVProbeData avpd;
129 uint8_t buf[PROBE_BUF_SIZE];
130 lavf_priv_t *priv;
132 if(!demuxer->priv)
133 demuxer->priv=calloc(sizeof(lavf_priv_t),1);
134 priv= demuxer->priv;
136 av_register_all();
138 if(stream_read(demuxer->stream, buf, PROBE_BUF_SIZE)!=PROBE_BUF_SIZE)
139 return 0;
140 avpd.filename= demuxer->stream->url;
141 avpd.buf= buf;
142 avpd.buf_size= PROBE_BUF_SIZE;
144 if (opt_format) {
145 if (strcmp(opt_format, "help") == 0) {
146 list_formats();
147 return 0;
149 priv->avif= av_find_input_format(opt_format);
150 if (!priv->avif) {
151 mp_msg(MSGT_DEMUX,MSGL_FATAL,"Unknown lavf format %s\n", opt_format);
152 return 0;
154 mp_msg(MSGT_DEMUX,MSGL_INFO,"Forced lavf %s demuxer\n", priv->avif->long_name);
155 return DEMUXER_TYPE_LAVF;
157 priv->avif= av_probe_input_format(&avpd, 1);
158 if(!priv->avif){
159 mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: no clue about this gibberish!\n");
160 return 0;
161 }else
162 mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: %s\n", priv->avif->long_name);
164 return DEMUXER_TYPE_LAVF;
167 static const char *preferred_list[] = {
168 "dxa",
169 "wv",
170 "nuv",
171 "nut",
172 "gxf",
173 "mxf",
174 "flv",
175 "swf",
176 "mov,mp4,m4a,3gp,3g2,mj2",
177 "mpc",
178 "mpc8",
179 NULL
182 static int lavf_check_preferred_file(demuxer_t *demuxer){
183 if (lavf_check_file(demuxer)) {
184 char **p = preferred_list;
185 lavf_priv_t *priv = demuxer->priv;
186 while (*p) {
187 if (strcmp(*p, priv->avif->name) == 0)
188 return DEMUXER_TYPE_LAVF_PREFERRED;
189 p++;
192 return 0;
195 static uint8_t char2int(char c) {
196 if (c >= '0' && c <= '9') return c - '0';
197 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
198 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
199 return 0;
202 static void parse_cryptokey(AVFormatContext *avfc, const char *str) {
203 int len = strlen(str) / 2;
204 uint8_t *key = av_mallocz(len);
205 int i;
206 avfc->keylen = len;
207 avfc->key = key;
208 for (i = 0; i < len; i++, str += 2)
209 *key++ = (char2int(str[0]) << 4) | char2int(str[1]);
212 static void handle_stream(demuxer_t *demuxer, AVFormatContext *avfc, int i) {
213 lavf_priv_t *priv= demuxer->priv;
214 AVStream *st= avfc->streams[i];
215 AVCodecContext *codec= st->codec;
216 int g;
218 switch(codec->codec_type){
219 case CODEC_TYPE_AUDIO:{
220 int override_tag;
221 WAVEFORMATEX *wf= calloc(sizeof(WAVEFORMATEX) + codec->extradata_size, 1);
222 sh_audio_t* sh_audio;
223 if(priv->audio_streams >= MAX_A_STREAMS)
224 break;
225 sh_audio=new_sh_audio(demuxer, i);
226 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_AudioID, "lavf", i);
227 if(!sh_audio)
228 break;
229 priv->astreams[priv->audio_streams] = i;
230 priv->audio_streams++;
231 // For some formats (like PCM) always trust CODEC_ID_* more than codec_tag
232 override_tag= av_codec_get_tag(mp_wav_override_taglists, codec->codec_id);
233 if (override_tag)
234 codec->codec_tag= override_tag;
235 // mp4a tag is used for all mp4 files no matter what they actually contain
236 if(codec->codec_tag == MKTAG('m', 'p', '4', 'a'))
237 codec->codec_tag= 0;
238 if(codec->codec_id == CODEC_ID_ADPCM_IMA_AMV)
239 codec->codec_tag= MKTAG('A','M','V','A');
240 if(!codec->codec_tag)
241 codec->codec_tag= av_codec_get_tag(mp_wav_taglists, codec->codec_id);
242 wf->wFormatTag= codec->codec_tag;
243 wf->nChannels= codec->channels;
244 wf->nSamplesPerSec= codec->sample_rate;
245 wf->nAvgBytesPerSec= codec->bit_rate/8;
246 wf->nBlockAlign= codec->block_align ? codec->block_align : 1;
247 wf->wBitsPerSample= codec->bits_per_sample;
248 wf->cbSize= codec->extradata_size;
249 if(codec->extradata_size)
250 memcpy(wf + 1, codec->extradata, codec->extradata_size);
251 sh_audio->wf= wf;
252 sh_audio->audio.dwSampleSize= codec->block_align;
253 if(codec->frame_size && codec->sample_rate){
254 sh_audio->audio.dwScale=codec->frame_size;
255 sh_audio->audio.dwRate= codec->sample_rate;
256 }else{
257 sh_audio->audio.dwScale= codec->block_align ? codec->block_align*8 : 8;
258 sh_audio->audio.dwRate = codec->bit_rate;
260 g= ff_gcd(sh_audio->audio.dwScale, sh_audio->audio.dwRate);
261 sh_audio->audio.dwScale /= g;
262 sh_audio->audio.dwRate /= g;
263 // 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);
264 sh_audio->ds= demuxer->audio;
265 sh_audio->format= codec->codec_tag;
266 sh_audio->channels= codec->channels;
267 sh_audio->samplerate= codec->sample_rate;
268 sh_audio->i_bps= codec->bit_rate/8;
269 switch (codec->codec_id) {
270 case CODEC_ID_PCM_S8:
271 case CODEC_ID_PCM_U8:
272 sh_audio->samplesize = 1;
273 break;
274 case CODEC_ID_PCM_S16LE:
275 case CODEC_ID_PCM_S16BE:
276 case CODEC_ID_PCM_U16LE:
277 case CODEC_ID_PCM_U16BE:
278 sh_audio->samplesize = 2;
279 break;
280 case CODEC_ID_PCM_ALAW:
281 sh_audio->format = 0x6;
282 break;
283 case CODEC_ID_PCM_MULAW:
284 sh_audio->format = 0x7;
285 break;
287 if (st->language)
288 sh_audio->lang = strdup(st->language);
289 if (st->disposition & AV_DISPOSITION_DEFAULT)
290 sh_audio->default_track = 1;
291 if(mp_msg_test(MSGT_HEADER,MSGL_V) ) print_wave_header(sh_audio->wf, MSGL_V);
292 // select the first audio stream
293 if (!demuxer->audio->sh) {
294 demuxer->audio->id = i;
295 demuxer->audio->sh= demuxer->a_streams[i];
296 } else
297 st->discard= AVDISCARD_ALL;
298 break;
300 case CODEC_TYPE_VIDEO:{
301 sh_video_t* sh_video;
302 BITMAPINFOHEADER *bih;
303 if(priv->video_streams >= MAX_V_STREAMS)
304 break;
305 sh_video=new_sh_video(demuxer, i);
306 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_VideoID, "lavf", i);
307 if(!sh_video) break;
308 priv->vstreams[priv->video_streams] = i;
309 priv->video_streams++;
310 bih=calloc(sizeof(BITMAPINFOHEADER) + codec->extradata_size,1);
312 if(codec->codec_id == CODEC_ID_RAWVIDEO) {
313 switch (codec->pix_fmt) {
314 case PIX_FMT_RGB24:
315 codec->codec_tag= MKTAG(24, 'B', 'G', 'R');
318 if(!codec->codec_tag)
319 codec->codec_tag= av_codec_get_tag(mp_bmp_taglists, codec->codec_id);
320 bih->biSize= sizeof(BITMAPINFOHEADER) + codec->extradata_size;
321 bih->biWidth= codec->width;
322 bih->biHeight= codec->height;
323 bih->biBitCount= codec->bits_per_sample;
324 bih->biSizeImage = bih->biWidth * bih->biHeight * bih->biBitCount/8;
325 bih->biCompression= codec->codec_tag;
326 sh_video->bih= bih;
327 sh_video->disp_w= codec->width;
328 sh_video->disp_h= codec->height;
329 if (st->time_base.den) { /* if container has time_base, use that */
330 sh_video->video.dwRate= st->time_base.den;
331 sh_video->video.dwScale= st->time_base.num;
332 } else {
333 sh_video->video.dwRate= codec->time_base.den;
334 sh_video->video.dwScale= codec->time_base.num;
336 sh_video->fps=av_q2d(st->r_frame_rate);
337 sh_video->frametime=1/av_q2d(st->r_frame_rate);
338 sh_video->format=bih->biCompression;
339 sh_video->aspect=codec->width * codec->sample_aspect_ratio.num
340 / (float)(codec->height * codec->sample_aspect_ratio.den);
341 sh_video->i_bps=codec->bit_rate/8;
342 mp_msg(MSGT_DEMUX,MSGL_DBG2,"aspect= %d*%d/(%d*%d)\n",
343 codec->width, codec->sample_aspect_ratio.num,
344 codec->height, codec->sample_aspect_ratio.den);
346 sh_video->ds= demuxer->video;
347 if(codec->extradata_size)
348 memcpy(sh_video->bih + 1, codec->extradata, codec->extradata_size);
349 if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_video_header(sh_video->bih, MSGL_V);
351 short biPlanes;
352 int biXPelsPerMeter;
353 int biYPelsPerMeter;
354 int biClrUsed;
355 int biClrImportant;
357 if(demuxer->video->id != i && demuxer->video->id != -1)
358 st->discard= AVDISCARD_ALL;
359 else{
360 demuxer->video->id = i;
361 demuxer->video->sh= demuxer->v_streams[i];
363 break;
365 case CODEC_TYPE_SUBTITLE:{
366 sh_sub_t* sh_sub;
367 char type;
368 if(priv->sub_streams >= MAX_S_STREAMS)
369 break;
370 /* only support text subtitles for now */
371 if(codec->codec_id == CODEC_ID_TEXT)
372 type = 't';
373 else if(codec->codec_id == CODEC_ID_MOV_TEXT)
374 type = 'm';
375 else if(codec->codec_id == CODEC_ID_SSA)
376 type = 'a';
377 else if(codec->codec_id == CODEC_ID_DVD_SUBTITLE)
378 type = 'v';
379 else
380 break;
381 sh_sub = new_sh_sub_sid(demuxer, i, priv->sub_streams);
382 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_SubtitleID, "lavf", priv->sub_streams);
383 if(!sh_sub) break;
384 priv->sstreams[priv->sub_streams] = i;
385 sh_sub->type = type;
386 if (codec->extradata_size) {
387 sh_sub->extradata = malloc(codec->extradata_size);
388 memcpy(sh_sub->extradata, codec->extradata, codec->extradata_size);
389 sh_sub->extradata_len = codec->extradata_size;
391 if (st->language)
392 sh_sub->lang = strdup(st->language);
393 if (st->disposition & AV_DISPOSITION_DEFAULT)
394 sh_sub->default_track = 1;
395 priv->sub_streams++;
396 break;
398 case CODEC_TYPE_ATTACHMENT:{
399 if (st->codec->codec_id == CODEC_ID_TTF)
400 demuxer_add_attachment(demuxer, st->filename,
401 "application/x-truetype-font",
402 codec->extradata, codec->extradata_size);
403 break;
405 default:
406 st->discard= AVDISCARD_ALL;
410 static demuxer_t* demux_open_lavf(demuxer_t *demuxer){
411 struct MPOpts *opts = demuxer->opts;
412 AVFormatContext *avfc;
413 AVFormatParameters ap;
414 const AVOption *opt;
415 lavf_priv_t *priv= demuxer->priv;
416 int i;
417 char mp_filename[256]="mp:";
419 memset(&ap, 0, sizeof(AVFormatParameters));
421 stream_seek(demuxer->stream, 0);
423 avfc = av_alloc_format_context();
425 if (opt_cryptokey)
426 parse_cryptokey(avfc, opt_cryptokey);
427 if (opts->user_correct_pts != 0)
428 avfc->flags |= AVFMT_FLAG_GENPTS;
429 if (index_mode == 0)
430 avfc->flags |= AVFMT_FLAG_IGNIDX;
432 ap.prealloced_context = 1;
433 if(opt_probesize) {
434 opt = av_set_int(avfc, "probesize", opt_probesize);
435 if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option probesize to %u\n", opt_probesize);
437 if(opt_analyzeduration) {
438 opt = av_set_int(avfc, "analyzeduration", opt_analyzeduration * AV_TIME_BASE);
439 if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option analyzeduration to %u\n", opt_analyzeduration);
442 if(opt_avopt){
443 if(parse_avopts(avfc, opt_avopt) < 0){
444 mp_msg(MSGT_HEADER,MSGL_ERR, "Your options /%s/ look like gibberish to me pal\n", opt_avopt);
445 return NULL;
449 if(demuxer->stream->url)
450 strncpy(mp_filename + 3, demuxer->stream->url, sizeof(mp_filename)-3);
451 else
452 strncpy(mp_filename + 3, "foobar.dummy", sizeof(mp_filename)-3);
454 priv->pb = av_alloc_put_byte(priv->buffer, BIO_BUFFER_SIZE, 0,
455 demuxer->stream, mp_read, NULL, mp_seek);
456 priv->pb->is_streamed = !demuxer->stream->end_pos;
458 if(av_open_input_stream(&avfc, priv->pb, mp_filename, priv->avif, &ap)<0){
459 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_open_input_stream() failed\n");
460 return NULL;
463 priv->avfc= avfc;
465 if(av_find_stream_info(avfc) < 0){
466 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_find_stream_info() failed\n");
467 return NULL;
470 if(avfc->title [0]) demux_info_add(demuxer, "name" , avfc->title );
471 if(avfc->author [0]) demux_info_add(demuxer, "author" , avfc->author );
472 if(avfc->copyright[0]) demux_info_add(demuxer, "copyright", avfc->copyright);
473 if(avfc->comment [0]) demux_info_add(demuxer, "comments" , avfc->comment );
474 if(avfc->album [0]) demux_info_add(demuxer, "album" , avfc->album );
475 // if(avfc->year ) demux_info_add(demuxer, "year" , avfc->year );
476 // if(avfc->track ) demux_info_add(demuxer, "track" , avfc->track );
477 if(avfc->genre [0]) demux_info_add(demuxer, "genre" , avfc->genre );
479 for(i=0; i < avfc->nb_chapters; i++) {
480 AVChapter *c = avfc->chapters[i];
481 uint64_t start = av_rescale_q(c->start, c->time_base, (AVRational){1,1000});
482 uint64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,1000});
483 demuxer_add_chapter(demuxer, c->title, start, end);
486 if(avfc->nb_programs) {
487 int p, start=0, found=0;
489 if(ts_prog) {
490 for(p=0; p<avfc->nb_programs; p++) {
491 if(avfc->programs[p]->id == ts_prog) {
492 start = p;
493 found = 1;
494 break;
497 if(!found) {
498 mp_msg(MSGT_HEADER,MSGL_ERR,"DEMUX_LAVF: program %d doesn't seem to be present\n", ts_prog);
499 return NULL;
502 p = start;
503 do {
504 AVProgram *program = avfc->programs[p];
505 mp_msg(MSGT_HEADER,MSGL_INFO,"LAVF: Program %d %s\n", program->id, (program->name ? program->name : ""));
506 for(i=0; i<program->nb_stream_indexes; i++)
507 handle_stream(demuxer, avfc, program->stream_index[i]);
508 if(!priv->cur_program && (demuxer->video->sh || demuxer->audio->sh))
509 priv->cur_program = program->id;
510 p = (p + 1) % avfc->nb_programs;
511 } while(p!=start);
512 } else
513 for(i=0; i<avfc->nb_streams; i++)
514 handle_stream(demuxer, avfc, i);
516 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: %d audio and %d video streams found\n",priv->audio_streams,priv->video_streams);
517 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: build %d\n", LIBAVFORMAT_BUILD);
518 if(!priv->audio_streams) demuxer->audio->id=-2; // nosound
519 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio;
520 if(!priv->video_streams){
521 if(!priv->audio_streams){
522 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF: no audio or video headers found - broken file?\n");
523 return NULL;
525 demuxer->video->id=-2; // audio-only
526 } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video;
528 return demuxer;
531 static int demux_lavf_fill_buffer(demuxer_t *demux, demux_stream_t *dsds){
532 lavf_priv_t *priv= demux->priv;
533 AVPacket pkt;
534 demux_packet_t *dp;
535 demux_stream_t *ds;
536 int id;
537 mp_msg(MSGT_DEMUX,MSGL_DBG2,"demux_lavf_fill_buffer()\n");
539 demux->filepos=stream_tell(demux->stream);
541 if(av_read_frame(priv->avfc, &pkt) < 0)
542 return 0;
544 id= pkt.stream_index;
546 if(id==demux->audio->id){
547 // audio
548 ds=demux->audio;
549 if(!ds->sh){
550 ds->sh=demux->a_streams[id];
551 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected LAVF audio ID = %d\n",ds->id);
553 } else if(id==demux->video->id){
554 // video
555 ds=demux->video;
556 if(!ds->sh){
557 ds->sh=demux->v_streams[id];
558 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected LAVF video ID = %d\n",ds->id);
560 } else if(id==demux->sub->id){
561 // subtitle
562 ds=demux->sub;
563 sub_utf8=1;
564 } else {
565 av_free_packet(&pkt);
566 return 1;
569 if(0/*pkt.destruct == av_destruct_packet*/){
570 //ok kids, dont try this at home :)
571 dp=malloc(sizeof(demux_packet_t));
572 dp->len=pkt.size;
573 dp->next=NULL;
574 dp->refcount=1;
575 dp->master=NULL;
576 dp->buffer=pkt.data;
577 pkt.destruct= NULL;
578 }else{
579 dp=new_demux_packet(pkt.size);
580 memcpy(dp->buffer, pkt.data, pkt.size);
581 av_free_packet(&pkt);
584 if(pkt.pts != AV_NOPTS_VALUE){
585 dp->pts=pkt.pts * av_q2d(priv->avfc->streams[id]->time_base);
586 priv->last_pts= dp->pts * AV_TIME_BASE;
587 if(pkt.duration)
588 dp->endpts = dp->pts + pkt.duration * av_q2d(priv->avfc->streams[id]->time_base);
590 dp->pos=demux->filepos;
591 dp->flags= !!(pkt.flags&PKT_FLAG_KEY);
592 // append packet to DS stream:
593 ds_add_packet(ds,dp);
594 return 1;
597 static void demux_seek_lavf(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags){
598 lavf_priv_t *priv = demuxer->priv;
599 int avsflags = 0;
600 mp_msg(MSGT_DEMUX,MSGL_DBG2,"demux_seek_lavf(%p, %f, %f, %d)\n", demuxer, rel_seek_secs, audio_delay, flags);
602 if (flags & SEEK_ABSOLUTE) {
603 priv->last_pts = priv->avfc->start_time;
604 } else {
605 if (rel_seek_secs < 0) avsflags = AVSEEK_FLAG_BACKWARD;
607 if (flags & SEEK_FACTOR) {
608 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
609 return;
610 priv->last_pts += rel_seek_secs * priv->avfc->duration;
611 } else {
612 priv->last_pts += rel_seek_secs * AV_TIME_BASE;
614 av_seek_frame(priv->avfc, -1, priv->last_pts, avsflags);
617 static int demux_lavf_control(demuxer_t *demuxer, int cmd, void *arg)
619 lavf_priv_t *priv = demuxer->priv;
621 switch (cmd) {
622 case DEMUXER_CTRL_CORRECT_PTS:
623 return DEMUXER_CTRL_OK;
624 case DEMUXER_CTRL_GET_TIME_LENGTH:
625 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
626 return DEMUXER_CTRL_DONTKNOW;
628 *((double *)arg) = (double)priv->avfc->duration / AV_TIME_BASE;
629 return DEMUXER_CTRL_OK;
631 case DEMUXER_CTRL_GET_PERCENT_POS:
632 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
633 return DEMUXER_CTRL_DONTKNOW;
635 *((int *)arg) = (int)((priv->last_pts - priv->avfc->start_time)*100 / priv->avfc->duration);
636 return DEMUXER_CTRL_OK;
637 case DEMUXER_CTRL_SWITCH_AUDIO:
638 case DEMUXER_CTRL_SWITCH_VIDEO:
640 int id = *((int*)arg);
641 int newid = -2;
642 int i, curridx = -1;
643 int nstreams, *pstreams;
644 demux_stream_t *ds;
646 if(cmd == DEMUXER_CTRL_SWITCH_VIDEO)
648 ds = demuxer->video;
649 nstreams = priv->video_streams;
650 pstreams = priv->vstreams;
652 else
654 ds = demuxer->audio;
655 nstreams = priv->audio_streams;
656 pstreams = priv->astreams;
658 for(i = 0; i < nstreams; i++)
660 if(pstreams[i] == ds->id) //current stream id
662 curridx = i;
663 break;
667 if(id == -2) { // no sound
668 i = -1;
669 } else if(id == -1) { // next track
670 i = (curridx + 2) % (nstreams + 1) - 1;
671 if (i >= 0)
672 newid = pstreams[i];
674 else // select track by id
676 for(i = 0; i < nstreams; i++)
678 if(pstreams[i] == id)
680 newid = id;
681 break;
685 if(i == curridx)
686 return DEMUXER_CTRL_NOTIMPL;
687 else
689 ds_free_packs(ds);
690 if(ds->id >= 0)
691 priv->avfc->streams[ds->id]->discard = AVDISCARD_ALL;
692 *((int*)arg) = ds->id = newid;
693 if(newid >= 0)
694 priv->avfc->streams[newid]->discard = AVDISCARD_NONE;
695 return DEMUXER_CTRL_OK;
698 case DEMUXER_CTRL_IDENTIFY_PROGRAM:
700 demux_program_t *prog = arg;
701 AVProgram *program;
702 int p, i;
704 if(priv->avfc->nb_programs < 2)
705 return DEMUXER_CTRL_NOTIMPL;
707 if(prog->progid == -1)
709 p = 0;
710 while(p<priv->avfc->nb_programs && priv->avfc->programs[p]->id != priv->cur_program)
711 p++;
712 p = (p + 1) % priv->avfc->nb_programs;
714 else
716 for(i=0; i<priv->avfc->nb_programs; i++)
717 if(priv->avfc->programs[i]->id == prog->progid)
718 break;
719 if(i==priv->avfc->nb_programs)
720 return DEMUXER_CTRL_NOTIMPL;
721 p = i;
723 prog->vid = prog->aid = prog->sid = -2; //no audio and no video by default
724 redo:
725 program = priv->avfc->programs[p];
726 for(i=0; i<program->nb_stream_indexes; i++)
728 switch(priv->avfc->streams[program->stream_index[i]]->codec->codec_type)
730 case CODEC_TYPE_VIDEO:
731 if(prog->vid == -2)
732 prog->vid = program->stream_index[i];
733 break;
734 case CODEC_TYPE_AUDIO:
735 if(prog->aid == -2)
736 prog->aid = program->stream_index[i];
737 break;
738 case CODEC_TYPE_SUBTITLE:
739 if(prog->sid == -2 && priv->avfc->streams[program->stream_index[i]]->codec->codec_id == CODEC_ID_TEXT)
740 prog->sid = program->stream_index[i];
741 break;
744 if(prog->progid == -1 && prog->vid == -2 && prog->aid == -2)
746 p = (p + 1) % priv->avfc->nb_programs;
747 goto redo;
749 priv->cur_program = prog->progid = program->id;
750 return DEMUXER_CTRL_OK;
752 default:
753 return DEMUXER_CTRL_NOTIMPL;
757 static void demux_close_lavf(demuxer_t *demuxer)
759 lavf_priv_t* priv = demuxer->priv;
760 if (priv){
761 if(priv->avfc)
763 av_freep(&priv->avfc->key);
764 av_close_input_stream(priv->avfc);
766 av_freep(&priv->pb);
767 free(priv); demuxer->priv= NULL;
772 const demuxer_desc_t demuxer_desc_lavf = {
773 "libavformat demuxer",
774 "lavf",
775 "libavformat",
776 "Michael Niedermayer",
777 "supports many formats, requires libavformat",
778 DEMUXER_TYPE_LAVF,
779 0, // Check after other demuxer
780 lavf_check_file,
781 demux_lavf_fill_buffer,
782 demux_open_lavf,
783 demux_close_lavf,
784 demux_seek_lavf,
785 demux_lavf_control
788 const demuxer_desc_t demuxer_desc_lavf_preferred = {
789 "libavformat preferred demuxer",
790 "lavfpref",
791 "libavformat",
792 "Michael Niedermayer",
793 "supports many formats, requires libavformat",
794 DEMUXER_TYPE_LAVF_PREFERRED,
796 lavf_check_preferred_file,
797 demux_lavf_fill_buffer,
798 demux_open_lavf,
799 demux_close_lavf,
800 demux_seek_lavf,
801 demux_lavf_control