100l, do not read probe buffer if it will not be used because a format was forced.
[mplayer/glamo.git] / libmpdemux / demux_lavf.c
blob42e9dfc0719b23b1e2fd213be542594c101e99e7
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 "demuxer.h"
33 #include "stheader.h"
34 #include "m_option.h"
35 #include "libvo/sub.h"
37 #include "libavformat/avformat.h"
38 #include "libavformat/avio.h"
39 #include "libavutil/avutil.h"
40 #include "libavcodec/opt.h"
42 #include "mp_taglists.h"
44 #define PROBE_BUF_SIZE (32*1024)
46 extern char *audio_lang;
47 extern char *dvdsub_lang;
48 extern int dvdsub_id;
49 static unsigned int opt_probesize = 0;
50 static unsigned int opt_analyzeduration = 0;
51 static char *opt_format;
52 static char *opt_cryptokey;
53 extern int ts_prog;
54 static char *opt_avopt = NULL;
56 const m_option_t lavfdopts_conf[] = {
57 {"probesize", &(opt_probesize), CONF_TYPE_INT, CONF_RANGE, 32, INT_MAX, NULL},
58 {"format", &(opt_format), CONF_TYPE_STRING, 0, 0, 0, NULL},
59 {"analyzeduration", &(opt_analyzeduration), CONF_TYPE_INT, CONF_RANGE, 0, INT_MAX, NULL},
60 {"cryptokey", &(opt_cryptokey), CONF_TYPE_STRING, 0, 0, 0, NULL},
61 {"o", &opt_avopt, CONF_TYPE_STRING, 0, 0, 0, NULL},
62 {NULL, NULL, 0, 0, 0, 0, NULL}
65 #define BIO_BUFFER_SIZE 32768
67 typedef struct lavf_priv_t{
68 AVInputFormat *avif;
69 AVFormatContext *avfc;
70 ByteIOContext *pb;
71 uint8_t buffer[BIO_BUFFER_SIZE];
72 int audio_streams;
73 int video_streams;
74 int sub_streams;
75 int64_t last_pts;
76 int astreams[MAX_A_STREAMS];
77 int vstreams[MAX_V_STREAMS];
78 int sstreams[MAX_S_STREAMS];
79 int cur_program;
80 }lavf_priv_t;
82 void print_wave_header(WAVEFORMATEX *h, int verbose_level);
83 void print_video_header(BITMAPINFOHEADER *h, int verbose_level);
85 static int mp_read(void *opaque, uint8_t *buf, int size) {
86 stream_t *stream = opaque;
87 int ret;
89 if(stream_eof(stream)) //needed?
90 return -1;
91 ret=stream_read(stream, buf, size);
93 mp_msg(MSGT_HEADER,MSGL_DBG2,"%d=mp_read(%p, %p, %d), eof:%d\n", ret, stream, buf, size, stream->eof);
94 return ret;
97 static int64_t mp_seek(void *opaque, int64_t pos, int whence) {
98 stream_t *stream = opaque;
99 int64_t current_pos;
100 mp_msg(MSGT_HEADER,MSGL_DBG2,"mp_seek(%p, %d, %d)\n", stream, (int)pos, whence);
101 if(whence == SEEK_CUR)
102 pos +=stream_tell(stream);
103 else if(whence == SEEK_END && stream->end_pos > 0)
104 pos += stream->end_pos;
105 else if(whence == SEEK_SET)
106 pos += stream->start_pos;
107 else if(whence == AVSEEK_SIZE && stream->end_pos > 0)
108 return stream->end_pos - stream->start_pos;
109 else
110 return -1;
112 if(pos<0)
113 return -1;
114 if(pos<stream->end_pos && stream->eof)
115 stream_reset(stream);
116 current_pos = stream_tell(stream);
117 if(stream_seek(stream, pos)==0) {
118 stream_reset(stream);
119 stream_seek(stream, current_pos);
120 return -1;
123 return pos - stream->start_pos;
126 static void list_formats(void) {
127 AVInputFormat *fmt;
128 mp_msg(MSGT_DEMUX, MSGL_INFO, "Available lavf input formats:\n");
129 for (fmt = first_iformat; fmt; fmt = fmt->next)
130 mp_msg(MSGT_DEMUX, MSGL_INFO, "%15s : %s\n", fmt->name, fmt->long_name);
133 static int lavf_check_file(demuxer_t *demuxer){
134 AVProbeData avpd;
135 uint8_t buf[PROBE_BUF_SIZE];
136 lavf_priv_t *priv;
138 if(!demuxer->priv)
139 demuxer->priv=calloc(sizeof(lavf_priv_t),1);
140 priv= demuxer->priv;
142 av_register_all();
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;
158 if(stream_read(demuxer->stream, buf, PROBE_BUF_SIZE)!=PROBE_BUF_SIZE)
159 return 0;
160 avpd.filename= demuxer->stream->url;
161 avpd.buf= buf;
162 avpd.buf_size= PROBE_BUF_SIZE;
164 priv->avif= av_probe_input_format(&avpd, 1);
165 if(!priv->avif){
166 mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: no clue about this gibberish!\n");
167 return 0;
168 }else
169 mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: %s\n", priv->avif->long_name);
171 return DEMUXER_TYPE_LAVF;
174 static const char *preferred_list[] = {
175 "dxa",
176 "wv",
177 "nuv",
178 "nut",
179 "gxf",
180 "mxf",
181 "flv",
182 "swf",
183 "mov,mp4,m4a,3gp,3g2,mj2",
184 "mpc",
185 "mpc8",
186 NULL
189 static int lavf_check_preferred_file(demuxer_t *demuxer){
190 if (lavf_check_file(demuxer)) {
191 char **p = preferred_list;
192 lavf_priv_t *priv = demuxer->priv;
193 while (*p) {
194 if (strcmp(*p, priv->avif->name) == 0)
195 return DEMUXER_TYPE_LAVF_PREFERRED;
196 p++;
199 return 0;
202 static uint8_t char2int(char c) {
203 if (c >= '0' && c <= '9') return c - '0';
204 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
205 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
206 return 0;
209 static void parse_cryptokey(AVFormatContext *avfc, const char *str) {
210 int len = strlen(str) / 2;
211 uint8_t *key = av_mallocz(len);
212 int i;
213 avfc->keylen = len;
214 avfc->key = key;
215 for (i = 0; i < len; i++, str += 2)
216 *key++ = (char2int(str[0]) << 4) | char2int(str[1]);
219 static void handle_stream(demuxer_t *demuxer, AVFormatContext *avfc, int i) {
220 lavf_priv_t *priv= demuxer->priv;
221 AVStream *st= avfc->streams[i];
222 AVCodecContext *codec= st->codec;
223 int g;
225 switch(codec->codec_type){
226 case CODEC_TYPE_AUDIO:{
227 int override_tag;
228 WAVEFORMATEX *wf= calloc(sizeof(WAVEFORMATEX) + codec->extradata_size, 1);
229 sh_audio_t* sh_audio;
230 if(priv->audio_streams >= MAX_A_STREAMS)
231 break;
232 sh_audio=new_sh_audio(demuxer, i);
233 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_AudioID, "lavf", i);
234 if(!sh_audio)
235 break;
236 priv->astreams[priv->audio_streams] = i;
237 priv->audio_streams++;
238 // For some formats (like PCM) always trust CODEC_ID_* more than codec_tag
239 override_tag= av_codec_get_tag(mp_wav_override_taglists, codec->codec_id);
240 if (override_tag)
241 codec->codec_tag= override_tag;
242 // mp4a tag is used for all mp4 files no matter what they actually contain
243 if(codec->codec_tag == MKTAG('m', 'p', '4', 'a'))
244 codec->codec_tag= 0;
245 if(codec->codec_id == CODEC_ID_ADPCM_IMA_AMV)
246 codec->codec_tag= MKTAG('A','M','V','A');
247 if(!codec->codec_tag)
248 codec->codec_tag= av_codec_get_tag(mp_wav_taglists, codec->codec_id);
249 wf->wFormatTag= codec->codec_tag;
250 wf->nChannels= codec->channels;
251 wf->nSamplesPerSec= codec->sample_rate;
252 wf->nAvgBytesPerSec= codec->bit_rate/8;
253 wf->nBlockAlign= codec->block_align ? codec->block_align : 1;
254 wf->wBitsPerSample= codec->bits_per_coded_sample;
255 wf->cbSize= codec->extradata_size;
256 if(codec->extradata_size)
257 memcpy(wf + 1, codec->extradata, codec->extradata_size);
258 sh_audio->wf= wf;
259 sh_audio->audio.dwSampleSize= codec->block_align;
260 if(codec->frame_size && codec->sample_rate){
261 sh_audio->audio.dwScale=codec->frame_size;
262 sh_audio->audio.dwRate= codec->sample_rate;
263 }else{
264 sh_audio->audio.dwScale= codec->block_align ? codec->block_align*8 : 8;
265 sh_audio->audio.dwRate = codec->bit_rate;
267 g= av_gcd(sh_audio->audio.dwScale, sh_audio->audio.dwRate);
268 sh_audio->audio.dwScale /= g;
269 sh_audio->audio.dwRate /= g;
270 // 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);
271 sh_audio->ds= demuxer->audio;
272 sh_audio->format= codec->codec_tag;
273 sh_audio->channels= codec->channels;
274 sh_audio->samplerate= codec->sample_rate;
275 sh_audio->i_bps= codec->bit_rate/8;
276 switch (codec->codec_id) {
277 case CODEC_ID_PCM_S8:
278 case CODEC_ID_PCM_U8:
279 sh_audio->samplesize = 1;
280 break;
281 case CODEC_ID_PCM_S16LE:
282 case CODEC_ID_PCM_S16BE:
283 case CODEC_ID_PCM_U16LE:
284 case CODEC_ID_PCM_U16BE:
285 sh_audio->samplesize = 2;
286 break;
287 case CODEC_ID_PCM_ALAW:
288 sh_audio->format = 0x6;
289 break;
290 case CODEC_ID_PCM_MULAW:
291 sh_audio->format = 0x7;
292 break;
294 if (st->language)
295 sh_audio->lang = strdup(st->language);
296 if (st->disposition & AV_DISPOSITION_DEFAULT)
297 sh_audio->default_track = 1;
298 if(mp_msg_test(MSGT_HEADER,MSGL_V) ) print_wave_header(sh_audio->wf, MSGL_V);
299 // select the first audio stream
300 if (!demuxer->audio->sh) {
301 demuxer->audio->id = i;
302 demuxer->audio->sh= demuxer->a_streams[i];
303 } else
304 st->discard= AVDISCARD_ALL;
305 break;
307 case CODEC_TYPE_VIDEO:{
308 sh_video_t* sh_video;
309 BITMAPINFOHEADER *bih;
310 if(priv->video_streams >= MAX_V_STREAMS)
311 break;
312 sh_video=new_sh_video(demuxer, i);
313 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_VideoID, "lavf", i);
314 if(!sh_video) break;
315 priv->vstreams[priv->video_streams] = i;
316 priv->video_streams++;
317 bih=calloc(sizeof(BITMAPINFOHEADER) + codec->extradata_size,1);
319 if(codec->codec_id == CODEC_ID_RAWVIDEO) {
320 switch (codec->pix_fmt) {
321 case PIX_FMT_RGB24:
322 codec->codec_tag= MKTAG(24, 'B', 'G', 'R');
325 if(!codec->codec_tag)
326 codec->codec_tag= av_codec_get_tag(mp_bmp_taglists, codec->codec_id);
327 bih->biSize= sizeof(BITMAPINFOHEADER) + codec->extradata_size;
328 bih->biWidth= codec->width;
329 bih->biHeight= codec->height;
330 bih->biBitCount= codec->bits_per_coded_sample;
331 bih->biSizeImage = bih->biWidth * bih->biHeight * bih->biBitCount/8;
332 bih->biCompression= codec->codec_tag;
333 sh_video->bih= bih;
334 sh_video->disp_w= codec->width;
335 sh_video->disp_h= codec->height;
336 if (st->time_base.den) { /* if container has time_base, use that */
337 sh_video->video.dwRate= st->time_base.den;
338 sh_video->video.dwScale= st->time_base.num;
339 } else {
340 sh_video->video.dwRate= codec->time_base.den;
341 sh_video->video.dwScale= codec->time_base.num;
343 sh_video->fps=av_q2d(st->r_frame_rate);
344 sh_video->frametime=1/av_q2d(st->r_frame_rate);
345 sh_video->format=bih->biCompression;
346 if(st->sample_aspect_ratio.num)
347 sh_video->aspect = codec->width * st->sample_aspect_ratio.num
348 / (float)(codec->height * st->sample_aspect_ratio.den);
349 else
350 sh_video->aspect=codec->width * codec->sample_aspect_ratio.num
351 / (float)(codec->height * codec->sample_aspect_ratio.den);
352 sh_video->i_bps=codec->bit_rate/8;
353 mp_msg(MSGT_DEMUX,MSGL_DBG2,"aspect= %d*%d/(%d*%d)\n",
354 codec->width, codec->sample_aspect_ratio.num,
355 codec->height, codec->sample_aspect_ratio.den);
357 sh_video->ds= demuxer->video;
358 if(codec->extradata_size)
359 memcpy(sh_video->bih + 1, codec->extradata, codec->extradata_size);
360 if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_video_header(sh_video->bih, MSGL_V);
362 short biPlanes;
363 int biXPelsPerMeter;
364 int biYPelsPerMeter;
365 int biClrUsed;
366 int biClrImportant;
368 if(demuxer->video->id != i && demuxer->video->id != -1)
369 st->discard= AVDISCARD_ALL;
370 else{
371 demuxer->video->id = i;
372 demuxer->video->sh= demuxer->v_streams[i];
374 break;
376 case CODEC_TYPE_SUBTITLE:{
377 sh_sub_t* sh_sub;
378 char type;
379 if(priv->sub_streams >= MAX_S_STREAMS)
380 break;
381 /* only support text subtitles for now */
382 if(codec->codec_id == CODEC_ID_TEXT)
383 type = 't';
384 else if(codec->codec_id == CODEC_ID_MOV_TEXT)
385 type = 'm';
386 else if(codec->codec_id == CODEC_ID_SSA)
387 type = 'a';
388 else if(codec->codec_id == CODEC_ID_DVD_SUBTITLE)
389 type = 'v';
390 else
391 break;
392 sh_sub = new_sh_sub_sid(demuxer, i, priv->sub_streams);
393 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_SubtitleID, "lavf", priv->sub_streams);
394 if(!sh_sub) break;
395 priv->sstreams[priv->sub_streams] = i;
396 sh_sub->type = type;
397 if (codec->extradata_size) {
398 sh_sub->extradata = malloc(codec->extradata_size);
399 memcpy(sh_sub->extradata, codec->extradata, codec->extradata_size);
400 sh_sub->extradata_len = codec->extradata_size;
402 if (st->language)
403 sh_sub->lang = strdup(st->language);
404 if (st->disposition & AV_DISPOSITION_DEFAULT)
405 sh_sub->default_track = 1;
406 priv->sub_streams++;
407 break;
409 case CODEC_TYPE_ATTACHMENT:{
410 if (st->codec->codec_id == CODEC_ID_TTF)
411 demuxer_add_attachment(demuxer, st->filename,
412 "application/x-truetype-font",
413 codec->extradata, codec->extradata_size);
414 break;
416 default:
417 st->discard= AVDISCARD_ALL;
421 static demuxer_t* demux_open_lavf(demuxer_t *demuxer){
422 AVFormatContext *avfc;
423 AVFormatParameters ap;
424 const AVOption *opt;
425 lavf_priv_t *priv= demuxer->priv;
426 int i;
427 char mp_filename[256]="mp:";
429 memset(&ap, 0, sizeof(AVFormatParameters));
431 stream_seek(demuxer->stream, 0);
433 avfc = av_alloc_format_context();
435 if (opt_cryptokey)
436 parse_cryptokey(avfc, opt_cryptokey);
437 if (user_correct_pts != 0)
438 avfc->flags |= AVFMT_FLAG_GENPTS;
439 if (index_mode == 0)
440 avfc->flags |= AVFMT_FLAG_IGNIDX;
442 ap.prealloced_context = 1;
443 if(opt_probesize) {
444 opt = av_set_int(avfc, "probesize", opt_probesize);
445 if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option probesize to %u\n", opt_probesize);
447 if(opt_analyzeduration) {
448 opt = av_set_int(avfc, "analyzeduration", opt_analyzeduration * AV_TIME_BASE);
449 if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option analyzeduration to %u\n", opt_analyzeduration);
452 if(opt_avopt){
453 if(parse_avopts(avfc, opt_avopt) < 0){
454 mp_msg(MSGT_HEADER,MSGL_ERR, "Your options /%s/ look like gibberish to me pal\n", opt_avopt);
455 return NULL;
459 if(demuxer->stream->url)
460 strncpy(mp_filename + 3, demuxer->stream->url, sizeof(mp_filename)-3);
461 else
462 strncpy(mp_filename + 3, "foobar.dummy", sizeof(mp_filename)-3);
464 priv->pb = av_alloc_put_byte(priv->buffer, BIO_BUFFER_SIZE, 0,
465 demuxer->stream, mp_read, NULL, mp_seek);
466 priv->pb->is_streamed = !demuxer->stream->end_pos || (demuxer->stream->flags & STREAM_SEEK) != STREAM_SEEK;
468 if(av_open_input_stream(&avfc, priv->pb, mp_filename, priv->avif, &ap)<0){
469 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_open_input_stream() failed\n");
470 return NULL;
473 priv->avfc= avfc;
475 if(av_find_stream_info(avfc) < 0){
476 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_find_stream_info() failed\n");
477 return NULL;
480 if(avfc->title [0]) demux_info_add(demuxer, "title" , avfc->title );
481 if(avfc->author [0]) demux_info_add(demuxer, "author" , avfc->author );
482 if(avfc->copyright[0]) demux_info_add(demuxer, "copyright", avfc->copyright);
483 if(avfc->comment [0]) demux_info_add(demuxer, "comments" , avfc->comment );
484 if(avfc->album [0]) demux_info_add(demuxer, "album" , avfc->album );
485 // if(avfc->year ) demux_info_add(demuxer, "year" , avfc->year );
486 // if(avfc->track ) demux_info_add(demuxer, "track" , avfc->track );
487 if(avfc->genre [0]) demux_info_add(demuxer, "genre" , avfc->genre );
489 for(i=0; i < avfc->nb_chapters; i++) {
490 AVChapter *c = avfc->chapters[i];
491 uint64_t start = av_rescale_q(c->start, c->time_base, (AVRational){1,1000});
492 uint64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,1000});
493 demuxer_add_chapter(demuxer, c->title, start, end);
496 if(avfc->nb_programs) {
497 int p, start=0, found=0;
499 if(ts_prog) {
500 for(p=0; p<avfc->nb_programs; p++) {
501 if(avfc->programs[p]->id == ts_prog) {
502 start = p;
503 found = 1;
504 break;
507 if(!found) {
508 mp_msg(MSGT_HEADER,MSGL_ERR,"DEMUX_LAVF: program %d doesn't seem to be present\n", ts_prog);
509 return NULL;
512 p = start;
513 do {
514 AVProgram *program = avfc->programs[p];
515 mp_msg(MSGT_HEADER,MSGL_INFO,"LAVF: Program %d %s\n", program->id, (program->name ? program->name : ""));
516 for(i=0; i<program->nb_stream_indexes; i++)
517 handle_stream(demuxer, avfc, program->stream_index[i]);
518 if(!priv->cur_program && (demuxer->video->sh || demuxer->audio->sh))
519 priv->cur_program = program->id;
520 p = (p + 1) % avfc->nb_programs;
521 } while(p!=start);
522 } else
523 for(i=0; i<avfc->nb_streams; i++)
524 handle_stream(demuxer, avfc, i);
526 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: %d audio and %d video streams found\n",priv->audio_streams,priv->video_streams);
527 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: build %d\n", LIBAVFORMAT_BUILD);
528 if(!priv->audio_streams) demuxer->audio->id=-2; // nosound
529 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio;
530 if(!priv->video_streams){
531 if(!priv->audio_streams){
532 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF: no audio or video headers found - broken file?\n");
533 return NULL;
535 demuxer->video->id=-2; // audio-only
536 } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video;
538 return demuxer;
541 static int demux_lavf_fill_buffer(demuxer_t *demux, demux_stream_t *dsds){
542 lavf_priv_t *priv= demux->priv;
543 AVPacket pkt;
544 demux_packet_t *dp;
545 demux_stream_t *ds;
546 int id;
547 mp_msg(MSGT_DEMUX,MSGL_DBG2,"demux_lavf_fill_buffer()\n");
549 demux->filepos=stream_tell(demux->stream);
551 if(av_read_frame(priv->avfc, &pkt) < 0)
552 return 0;
554 id= pkt.stream_index;
556 if(id==demux->audio->id){
557 // audio
558 ds=demux->audio;
559 if(!ds->sh){
560 ds->sh=demux->a_streams[id];
561 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected LAVF audio ID = %d\n",ds->id);
563 } else if(id==demux->video->id){
564 // video
565 ds=demux->video;
566 if(!ds->sh){
567 ds->sh=demux->v_streams[id];
568 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected LAVF video ID = %d\n",ds->id);
570 } else if(id==demux->sub->id){
571 // subtitle
572 ds=demux->sub;
573 sub_utf8=1;
574 } else {
575 av_free_packet(&pkt);
576 return 1;
579 if(0/*pkt.destruct == av_destruct_packet*/){
580 //ok kids, dont try this at home :)
581 dp=malloc(sizeof(demux_packet_t));
582 dp->len=pkt.size;
583 dp->next=NULL;
584 dp->refcount=1;
585 dp->master=NULL;
586 dp->buffer=pkt.data;
587 pkt.destruct= NULL;
588 }else{
589 dp=new_demux_packet(pkt.size);
590 memcpy(dp->buffer, pkt.data, pkt.size);
591 av_free_packet(&pkt);
594 if(pkt.pts != AV_NOPTS_VALUE){
595 dp->pts=pkt.pts * av_q2d(priv->avfc->streams[id]->time_base);
596 priv->last_pts= dp->pts * AV_TIME_BASE;
597 if(pkt.convergence_duration)
598 dp->endpts = dp->pts + pkt.convergence_duration * av_q2d(priv->avfc->streams[id]->time_base);
600 dp->pos=demux->filepos;
601 dp->flags= !!(pkt.flags&PKT_FLAG_KEY);
602 // append packet to DS stream:
603 ds_add_packet(ds,dp);
604 return 1;
607 static void demux_seek_lavf(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags){
608 lavf_priv_t *priv = demuxer->priv;
609 int avsflags = 0;
610 mp_msg(MSGT_DEMUX,MSGL_DBG2,"demux_seek_lavf(%p, %f, %f, %d)\n", demuxer, rel_seek_secs, audio_delay, flags);
612 if (flags & SEEK_ABSOLUTE) {
613 priv->last_pts = priv->avfc->start_time;
614 } else {
615 if (rel_seek_secs < 0) avsflags = AVSEEK_FLAG_BACKWARD;
617 if (flags & SEEK_FACTOR) {
618 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
619 return;
620 priv->last_pts += rel_seek_secs * priv->avfc->duration;
621 } else {
622 priv->last_pts += rel_seek_secs * AV_TIME_BASE;
624 av_seek_frame(priv->avfc, -1, priv->last_pts, avsflags);
627 static int demux_lavf_control(demuxer_t *demuxer, int cmd, void *arg)
629 lavf_priv_t *priv = demuxer->priv;
631 switch (cmd) {
632 case DEMUXER_CTRL_CORRECT_PTS:
633 return DEMUXER_CTRL_OK;
634 case DEMUXER_CTRL_GET_TIME_LENGTH:
635 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
636 return DEMUXER_CTRL_DONTKNOW;
638 *((double *)arg) = (double)priv->avfc->duration / AV_TIME_BASE;
639 return DEMUXER_CTRL_OK;
641 case DEMUXER_CTRL_GET_PERCENT_POS:
642 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
643 return DEMUXER_CTRL_DONTKNOW;
645 *((int *)arg) = (int)((priv->last_pts - priv->avfc->start_time)*100 / priv->avfc->duration);
646 return DEMUXER_CTRL_OK;
647 case DEMUXER_CTRL_SWITCH_AUDIO:
648 case DEMUXER_CTRL_SWITCH_VIDEO:
650 int id = *((int*)arg);
651 int newid = -2;
652 int i, curridx = -1;
653 int nstreams, *pstreams;
654 demux_stream_t *ds;
656 if(cmd == DEMUXER_CTRL_SWITCH_VIDEO)
658 ds = demuxer->video;
659 nstreams = priv->video_streams;
660 pstreams = priv->vstreams;
662 else
664 ds = demuxer->audio;
665 nstreams = priv->audio_streams;
666 pstreams = priv->astreams;
668 for(i = 0; i < nstreams; i++)
670 if(pstreams[i] == ds->id) //current stream id
672 curridx = i;
673 break;
677 if(id == -2) { // no sound
678 i = -1;
679 } else if(id == -1) { // next track
680 i = (curridx + 2) % (nstreams + 1) - 1;
681 if (i >= 0)
682 newid = pstreams[i];
684 else // select track by id
686 for(i = 0; i < nstreams; i++)
688 if(pstreams[i] == id)
690 newid = id;
691 break;
695 if(i == curridx)
696 return DEMUXER_CTRL_NOTIMPL;
697 else
699 ds_free_packs(ds);
700 if(ds->id >= 0)
701 priv->avfc->streams[ds->id]->discard = AVDISCARD_ALL;
702 *((int*)arg) = ds->id = newid;
703 if(newid >= 0)
704 priv->avfc->streams[newid]->discard = AVDISCARD_NONE;
705 return DEMUXER_CTRL_OK;
708 case DEMUXER_CTRL_IDENTIFY_PROGRAM:
710 demux_program_t *prog = arg;
711 AVProgram *program;
712 int p, i;
714 if(priv->avfc->nb_programs < 2)
715 return DEMUXER_CTRL_NOTIMPL;
717 if(prog->progid == -1)
719 p = 0;
720 while(p<priv->avfc->nb_programs && priv->avfc->programs[p]->id != priv->cur_program)
721 p++;
722 p = (p + 1) % priv->avfc->nb_programs;
724 else
726 for(i=0; i<priv->avfc->nb_programs; i++)
727 if(priv->avfc->programs[i]->id == prog->progid)
728 break;
729 if(i==priv->avfc->nb_programs)
730 return DEMUXER_CTRL_NOTIMPL;
731 p = i;
733 prog->vid = prog->aid = prog->sid = -2; //no audio and no video by default
734 redo:
735 program = priv->avfc->programs[p];
736 for(i=0; i<program->nb_stream_indexes; i++)
738 switch(priv->avfc->streams[program->stream_index[i]]->codec->codec_type)
740 case CODEC_TYPE_VIDEO:
741 if(prog->vid == -2)
742 prog->vid = program->stream_index[i];
743 break;
744 case CODEC_TYPE_AUDIO:
745 if(prog->aid == -2)
746 prog->aid = program->stream_index[i];
747 break;
748 case CODEC_TYPE_SUBTITLE:
749 if(prog->sid == -2 && priv->avfc->streams[program->stream_index[i]]->codec->codec_id == CODEC_ID_TEXT)
750 prog->sid = program->stream_index[i];
751 break;
754 if(prog->progid == -1 && prog->vid == -2 && prog->aid == -2)
756 p = (p + 1) % priv->avfc->nb_programs;
757 goto redo;
759 priv->cur_program = prog->progid = program->id;
760 return DEMUXER_CTRL_OK;
762 default:
763 return DEMUXER_CTRL_NOTIMPL;
767 static void demux_close_lavf(demuxer_t *demuxer)
769 lavf_priv_t* priv = demuxer->priv;
770 if (priv){
771 if(priv->avfc)
773 av_freep(&priv->avfc->key);
774 av_close_input_stream(priv->avfc);
776 av_freep(&priv->pb);
777 free(priv); demuxer->priv= NULL;
782 const demuxer_desc_t demuxer_desc_lavf = {
783 "libavformat demuxer",
784 "lavf",
785 "libavformat",
786 "Michael Niedermayer",
787 "supports many formats, requires libavformat",
788 DEMUXER_TYPE_LAVF,
789 0, // Check after other demuxer
790 lavf_check_file,
791 demux_lavf_fill_buffer,
792 demux_open_lavf,
793 demux_close_lavf,
794 demux_seek_lavf,
795 demux_lavf_control
798 const demuxer_desc_t demuxer_desc_lavf_preferred = {
799 "libavformat preferred demuxer",
800 "lavfpref",
801 "libavformat",
802 "Michael Niedermayer",
803 "supports many formats, requires libavformat",
804 DEMUXER_TYPE_LAVF_PREFERRED,
806 lavf_check_preferred_file,
807 demux_lavf_fill_buffer,
808 demux_open_lavf,
809 demux_close_lavf,
810 demux_seek_lavf,
811 demux_lavf_control