Remove the grayscale and 4:2:2 RTjpeg code, it is neither used nor is there
[mplayer/glamo.git] / libmpdemux / demux_lavf.c
blob9749054a03aa65bca2b746dc04787d173de8269f
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 2048
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(stream_read(demuxer->stream, buf, PROBE_BUF_SIZE)!=PROBE_BUF_SIZE)
145 return 0;
146 avpd.filename= demuxer->stream->url;
147 avpd.buf= buf;
148 avpd.buf_size= PROBE_BUF_SIZE;
150 if (opt_format) {
151 if (strcmp(opt_format, "help") == 0) {
152 list_formats();
153 return 0;
155 priv->avif= av_find_input_format(opt_format);
156 if (!priv->avif) {
157 mp_msg(MSGT_DEMUX,MSGL_FATAL,"Unknown lavf format %s\n", opt_format);
158 return 0;
160 mp_msg(MSGT_DEMUX,MSGL_INFO,"Forced lavf %s demuxer\n", priv->avif->long_name);
161 return DEMUXER_TYPE_LAVF;
163 priv->avif= av_probe_input_format(&avpd, 1);
164 if(!priv->avif){
165 mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: no clue about this gibberish!\n");
166 return 0;
167 }else
168 mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: %s\n", priv->avif->long_name);
170 return DEMUXER_TYPE_LAVF;
173 static const char *preferred_list[] = {
174 "dxa",
175 "wv",
176 "nuv",
177 "nut",
178 "gxf",
179 "mxf",
180 "flv",
181 "swf",
182 "mov,mp4,m4a,3gp,3g2,mj2",
183 "mpc",
184 "mpc8",
185 NULL
188 static int lavf_check_preferred_file(demuxer_t *demuxer){
189 if (lavf_check_file(demuxer)) {
190 char **p = preferred_list;
191 lavf_priv_t *priv = demuxer->priv;
192 while (*p) {
193 if (strcmp(*p, priv->avif->name) == 0)
194 return DEMUXER_TYPE_LAVF_PREFERRED;
195 p++;
198 return 0;
201 static uint8_t char2int(char c) {
202 if (c >= '0' && c <= '9') return c - '0';
203 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
204 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
205 return 0;
208 static void parse_cryptokey(AVFormatContext *avfc, const char *str) {
209 int len = strlen(str) / 2;
210 uint8_t *key = av_mallocz(len);
211 int i;
212 avfc->keylen = len;
213 avfc->key = key;
214 for (i = 0; i < len; i++, str += 2)
215 *key++ = (char2int(str[0]) << 4) | char2int(str[1]);
218 static void handle_stream(demuxer_t *demuxer, AVFormatContext *avfc, int i) {
219 lavf_priv_t *priv= demuxer->priv;
220 AVStream *st= avfc->streams[i];
221 AVCodecContext *codec= st->codec;
222 int g;
224 switch(codec->codec_type){
225 case CODEC_TYPE_AUDIO:{
226 int override_tag;
227 WAVEFORMATEX *wf= calloc(sizeof(WAVEFORMATEX) + codec->extradata_size, 1);
228 sh_audio_t* sh_audio;
229 if(priv->audio_streams >= MAX_A_STREAMS)
230 break;
231 sh_audio=new_sh_audio(demuxer, i);
232 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_AudioID, "lavf", i);
233 if(!sh_audio)
234 break;
235 priv->astreams[priv->audio_streams] = i;
236 priv->audio_streams++;
237 // For some formats (like PCM) always trust CODEC_ID_* more than codec_tag
238 override_tag= av_codec_get_tag(mp_wav_override_taglists, codec->codec_id);
239 if (override_tag)
240 codec->codec_tag= override_tag;
241 // mp4a tag is used for all mp4 files no matter what they actually contain
242 if(codec->codec_tag == MKTAG('m', 'p', '4', 'a'))
243 codec->codec_tag= 0;
244 if(codec->codec_id == CODEC_ID_ADPCM_IMA_AMV)
245 codec->codec_tag= MKTAG('A','M','V','A');
246 if(!codec->codec_tag)
247 codec->codec_tag= av_codec_get_tag(mp_wav_taglists, codec->codec_id);
248 wf->wFormatTag= codec->codec_tag;
249 wf->nChannels= codec->channels;
250 wf->nSamplesPerSec= codec->sample_rate;
251 wf->nAvgBytesPerSec= codec->bit_rate/8;
252 wf->nBlockAlign= codec->block_align ? codec->block_align : 1;
253 wf->wBitsPerSample= codec->bits_per_coded_sample;
254 wf->cbSize= codec->extradata_size;
255 if(codec->extradata_size)
256 memcpy(wf + 1, codec->extradata, codec->extradata_size);
257 sh_audio->wf= wf;
258 sh_audio->audio.dwSampleSize= codec->block_align;
259 if(codec->frame_size && codec->sample_rate){
260 sh_audio->audio.dwScale=codec->frame_size;
261 sh_audio->audio.dwRate= codec->sample_rate;
262 }else{
263 sh_audio->audio.dwScale= codec->block_align ? codec->block_align*8 : 8;
264 sh_audio->audio.dwRate = codec->bit_rate;
266 g= av_gcd(sh_audio->audio.dwScale, sh_audio->audio.dwRate);
267 sh_audio->audio.dwScale /= g;
268 sh_audio->audio.dwRate /= g;
269 // 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);
270 sh_audio->ds= demuxer->audio;
271 sh_audio->format= codec->codec_tag;
272 sh_audio->channels= codec->channels;
273 sh_audio->samplerate= codec->sample_rate;
274 sh_audio->i_bps= codec->bit_rate/8;
275 switch (codec->codec_id) {
276 case CODEC_ID_PCM_S8:
277 case CODEC_ID_PCM_U8:
278 sh_audio->samplesize = 1;
279 break;
280 case CODEC_ID_PCM_S16LE:
281 case CODEC_ID_PCM_S16BE:
282 case CODEC_ID_PCM_U16LE:
283 case CODEC_ID_PCM_U16BE:
284 sh_audio->samplesize = 2;
285 break;
286 case CODEC_ID_PCM_ALAW:
287 sh_audio->format = 0x6;
288 break;
289 case CODEC_ID_PCM_MULAW:
290 sh_audio->format = 0x7;
291 break;
293 if (st->language)
294 sh_audio->lang = strdup(st->language);
295 if (st->disposition & AV_DISPOSITION_DEFAULT)
296 sh_audio->default_track = 1;
297 if(mp_msg_test(MSGT_HEADER,MSGL_V) ) print_wave_header(sh_audio->wf, MSGL_V);
298 // select the first audio stream
299 if (!demuxer->audio->sh) {
300 demuxer->audio->id = i;
301 demuxer->audio->sh= demuxer->a_streams[i];
302 } else
303 st->discard= AVDISCARD_ALL;
304 break;
306 case CODEC_TYPE_VIDEO:{
307 sh_video_t* sh_video;
308 BITMAPINFOHEADER *bih;
309 if(priv->video_streams >= MAX_V_STREAMS)
310 break;
311 sh_video=new_sh_video(demuxer, i);
312 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_VideoID, "lavf", i);
313 if(!sh_video) break;
314 priv->vstreams[priv->video_streams] = i;
315 priv->video_streams++;
316 bih=calloc(sizeof(BITMAPINFOHEADER) + codec->extradata_size,1);
318 if(codec->codec_id == CODEC_ID_RAWVIDEO) {
319 switch (codec->pix_fmt) {
320 case PIX_FMT_RGB24:
321 codec->codec_tag= MKTAG(24, 'B', 'G', 'R');
324 if(!codec->codec_tag)
325 codec->codec_tag= av_codec_get_tag(mp_bmp_taglists, codec->codec_id);
326 bih->biSize= sizeof(BITMAPINFOHEADER) + codec->extradata_size;
327 bih->biWidth= codec->width;
328 bih->biHeight= codec->height;
329 bih->biBitCount= codec->bits_per_coded_sample;
330 bih->biSizeImage = bih->biWidth * bih->biHeight * bih->biBitCount/8;
331 bih->biCompression= codec->codec_tag;
332 sh_video->bih= bih;
333 sh_video->disp_w= codec->width;
334 sh_video->disp_h= codec->height;
335 if (st->time_base.den) { /* if container has time_base, use that */
336 sh_video->video.dwRate= st->time_base.den;
337 sh_video->video.dwScale= st->time_base.num;
338 } else {
339 sh_video->video.dwRate= codec->time_base.den;
340 sh_video->video.dwScale= codec->time_base.num;
342 sh_video->fps=av_q2d(st->r_frame_rate);
343 sh_video->frametime=1/av_q2d(st->r_frame_rate);
344 sh_video->format=bih->biCompression;
345 if(st->sample_aspect_ratio.num)
346 sh_video->aspect = codec->width * st->sample_aspect_ratio.num
347 / (float)(codec->height * st->sample_aspect_ratio.den);
348 else
349 sh_video->aspect=codec->width * codec->sample_aspect_ratio.num
350 / (float)(codec->height * codec->sample_aspect_ratio.den);
351 sh_video->i_bps=codec->bit_rate/8;
352 mp_msg(MSGT_DEMUX,MSGL_DBG2,"aspect= %d*%d/(%d*%d)\n",
353 codec->width, codec->sample_aspect_ratio.num,
354 codec->height, codec->sample_aspect_ratio.den);
356 sh_video->ds= demuxer->video;
357 if(codec->extradata_size)
358 memcpy(sh_video->bih + 1, codec->extradata, codec->extradata_size);
359 if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_video_header(sh_video->bih, MSGL_V);
361 short biPlanes;
362 int biXPelsPerMeter;
363 int biYPelsPerMeter;
364 int biClrUsed;
365 int biClrImportant;
367 if(demuxer->video->id != i && demuxer->video->id != -1)
368 st->discard= AVDISCARD_ALL;
369 else{
370 demuxer->video->id = i;
371 demuxer->video->sh= demuxer->v_streams[i];
373 break;
375 case CODEC_TYPE_SUBTITLE:{
376 sh_sub_t* sh_sub;
377 char type;
378 if(priv->sub_streams >= MAX_S_STREAMS)
379 break;
380 /* only support text subtitles for now */
381 if(codec->codec_id == CODEC_ID_TEXT)
382 type = 't';
383 else if(codec->codec_id == CODEC_ID_MOV_TEXT)
384 type = 'm';
385 else if(codec->codec_id == CODEC_ID_SSA)
386 type = 'a';
387 else if(codec->codec_id == CODEC_ID_DVD_SUBTITLE)
388 type = 'v';
389 else
390 break;
391 sh_sub = new_sh_sub_sid(demuxer, i, priv->sub_streams);
392 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_SubtitleID, "lavf", priv->sub_streams);
393 if(!sh_sub) break;
394 priv->sstreams[priv->sub_streams] = i;
395 sh_sub->type = type;
396 if (codec->extradata_size) {
397 sh_sub->extradata = malloc(codec->extradata_size);
398 memcpy(sh_sub->extradata, codec->extradata, codec->extradata_size);
399 sh_sub->extradata_len = codec->extradata_size;
401 if (st->language)
402 sh_sub->lang = strdup(st->language);
403 if (st->disposition & AV_DISPOSITION_DEFAULT)
404 sh_sub->default_track = 1;
405 priv->sub_streams++;
406 break;
408 case CODEC_TYPE_ATTACHMENT:{
409 if (st->codec->codec_id == CODEC_ID_TTF)
410 demuxer_add_attachment(demuxer, st->filename,
411 "application/x-truetype-font",
412 codec->extradata, codec->extradata_size);
413 break;
415 default:
416 st->discard= AVDISCARD_ALL;
420 static demuxer_t* demux_open_lavf(demuxer_t *demuxer){
421 AVFormatContext *avfc;
422 AVFormatParameters ap;
423 const AVOption *opt;
424 lavf_priv_t *priv= demuxer->priv;
425 int i;
426 char mp_filename[256]="mp:";
428 memset(&ap, 0, sizeof(AVFormatParameters));
430 stream_seek(demuxer->stream, 0);
432 avfc = av_alloc_format_context();
434 if (opt_cryptokey)
435 parse_cryptokey(avfc, opt_cryptokey);
436 if (user_correct_pts != 0)
437 avfc->flags |= AVFMT_FLAG_GENPTS;
438 if (index_mode == 0)
439 avfc->flags |= AVFMT_FLAG_IGNIDX;
441 ap.prealloced_context = 1;
442 if(opt_probesize) {
443 opt = av_set_int(avfc, "probesize", opt_probesize);
444 if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option probesize to %u\n", opt_probesize);
446 if(opt_analyzeduration) {
447 opt = av_set_int(avfc, "analyzeduration", opt_analyzeduration * AV_TIME_BASE);
448 if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option analyzeduration to %u\n", opt_analyzeduration);
451 if(opt_avopt){
452 if(parse_avopts(avfc, opt_avopt) < 0){
453 mp_msg(MSGT_HEADER,MSGL_ERR, "Your options /%s/ look like gibberish to me pal\n", opt_avopt);
454 return NULL;
458 if(demuxer->stream->url)
459 strncpy(mp_filename + 3, demuxer->stream->url, sizeof(mp_filename)-3);
460 else
461 strncpy(mp_filename + 3, "foobar.dummy", sizeof(mp_filename)-3);
463 priv->pb = av_alloc_put_byte(priv->buffer, BIO_BUFFER_SIZE, 0,
464 demuxer->stream, mp_read, NULL, mp_seek);
465 priv->pb->is_streamed = !demuxer->stream->end_pos || (demuxer->stream->flags & STREAM_SEEK) != STREAM_SEEK;
467 if(av_open_input_stream(&avfc, priv->pb, mp_filename, priv->avif, &ap)<0){
468 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_open_input_stream() failed\n");
469 return NULL;
472 priv->avfc= avfc;
474 if(av_find_stream_info(avfc) < 0){
475 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_find_stream_info() failed\n");
476 return NULL;
479 if(avfc->title [0]) demux_info_add(demuxer, "name" , avfc->title );
480 if(avfc->author [0]) demux_info_add(demuxer, "author" , avfc->author );
481 if(avfc->copyright[0]) demux_info_add(demuxer, "copyright", avfc->copyright);
482 if(avfc->comment [0]) demux_info_add(demuxer, "comments" , avfc->comment );
483 if(avfc->album [0]) demux_info_add(demuxer, "album" , avfc->album );
484 // if(avfc->year ) demux_info_add(demuxer, "year" , avfc->year );
485 // if(avfc->track ) demux_info_add(demuxer, "track" , avfc->track );
486 if(avfc->genre [0]) demux_info_add(demuxer, "genre" , avfc->genre );
488 for(i=0; i < avfc->nb_chapters; i++) {
489 AVChapter *c = avfc->chapters[i];
490 uint64_t start = av_rescale_q(c->start, c->time_base, (AVRational){1,1000});
491 uint64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,1000});
492 demuxer_add_chapter(demuxer, c->title, start, end);
495 if(avfc->nb_programs) {
496 int p, start=0, found=0;
498 if(ts_prog) {
499 for(p=0; p<avfc->nb_programs; p++) {
500 if(avfc->programs[p]->id == ts_prog) {
501 start = p;
502 found = 1;
503 break;
506 if(!found) {
507 mp_msg(MSGT_HEADER,MSGL_ERR,"DEMUX_LAVF: program %d doesn't seem to be present\n", ts_prog);
508 return NULL;
511 p = start;
512 do {
513 AVProgram *program = avfc->programs[p];
514 mp_msg(MSGT_HEADER,MSGL_INFO,"LAVF: Program %d %s\n", program->id, (program->name ? program->name : ""));
515 for(i=0; i<program->nb_stream_indexes; i++)
516 handle_stream(demuxer, avfc, program->stream_index[i]);
517 if(!priv->cur_program && (demuxer->video->sh || demuxer->audio->sh))
518 priv->cur_program = program->id;
519 p = (p + 1) % avfc->nb_programs;
520 } while(p!=start);
521 } else
522 for(i=0; i<avfc->nb_streams; i++)
523 handle_stream(demuxer, avfc, i);
525 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: %d audio and %d video streams found\n",priv->audio_streams,priv->video_streams);
526 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: build %d\n", LIBAVFORMAT_BUILD);
527 if(!priv->audio_streams) demuxer->audio->id=-2; // nosound
528 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio;
529 if(!priv->video_streams){
530 if(!priv->audio_streams){
531 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF: no audio or video headers found - broken file?\n");
532 return NULL;
534 demuxer->video->id=-2; // audio-only
535 } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video;
537 return demuxer;
540 static int demux_lavf_fill_buffer(demuxer_t *demux, demux_stream_t *dsds){
541 lavf_priv_t *priv= demux->priv;
542 AVPacket pkt;
543 demux_packet_t *dp;
544 demux_stream_t *ds;
545 int id;
546 mp_msg(MSGT_DEMUX,MSGL_DBG2,"demux_lavf_fill_buffer()\n");
548 demux->filepos=stream_tell(demux->stream);
550 if(av_read_frame(priv->avfc, &pkt) < 0)
551 return 0;
553 id= pkt.stream_index;
555 if(id==demux->audio->id){
556 // audio
557 ds=demux->audio;
558 if(!ds->sh){
559 ds->sh=demux->a_streams[id];
560 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected LAVF audio ID = %d\n",ds->id);
562 } else if(id==demux->video->id){
563 // video
564 ds=demux->video;
565 if(!ds->sh){
566 ds->sh=demux->v_streams[id];
567 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected LAVF video ID = %d\n",ds->id);
569 } else if(id==demux->sub->id){
570 // subtitle
571 ds=demux->sub;
572 sub_utf8=1;
573 } else {
574 av_free_packet(&pkt);
575 return 1;
578 if(0/*pkt.destruct == av_destruct_packet*/){
579 //ok kids, dont try this at home :)
580 dp=malloc(sizeof(demux_packet_t));
581 dp->len=pkt.size;
582 dp->next=NULL;
583 dp->refcount=1;
584 dp->master=NULL;
585 dp->buffer=pkt.data;
586 pkt.destruct= NULL;
587 }else{
588 dp=new_demux_packet(pkt.size);
589 memcpy(dp->buffer, pkt.data, pkt.size);
590 av_free_packet(&pkt);
593 if(pkt.pts != AV_NOPTS_VALUE){
594 dp->pts=pkt.pts * av_q2d(priv->avfc->streams[id]->time_base);
595 priv->last_pts= dp->pts * AV_TIME_BASE;
596 if(pkt.convergence_duration)
597 dp->endpts = dp->pts + pkt.convergence_duration * av_q2d(priv->avfc->streams[id]->time_base);
599 dp->pos=demux->filepos;
600 dp->flags= !!(pkt.flags&PKT_FLAG_KEY);
601 // append packet to DS stream:
602 ds_add_packet(ds,dp);
603 return 1;
606 static void demux_seek_lavf(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags){
607 lavf_priv_t *priv = demuxer->priv;
608 int avsflags = 0;
609 mp_msg(MSGT_DEMUX,MSGL_DBG2,"demux_seek_lavf(%p, %f, %f, %d)\n", demuxer, rel_seek_secs, audio_delay, flags);
611 if (flags & SEEK_ABSOLUTE) {
612 priv->last_pts = priv->avfc->start_time;
613 } else {
614 if (rel_seek_secs < 0) avsflags = AVSEEK_FLAG_BACKWARD;
616 if (flags & SEEK_FACTOR) {
617 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
618 return;
619 priv->last_pts += rel_seek_secs * priv->avfc->duration;
620 } else {
621 priv->last_pts += rel_seek_secs * AV_TIME_BASE;
623 av_seek_frame(priv->avfc, -1, priv->last_pts, avsflags);
626 static int demux_lavf_control(demuxer_t *demuxer, int cmd, void *arg)
628 lavf_priv_t *priv = demuxer->priv;
630 switch (cmd) {
631 case DEMUXER_CTRL_CORRECT_PTS:
632 return DEMUXER_CTRL_OK;
633 case DEMUXER_CTRL_GET_TIME_LENGTH:
634 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
635 return DEMUXER_CTRL_DONTKNOW;
637 *((double *)arg) = (double)priv->avfc->duration / AV_TIME_BASE;
638 return DEMUXER_CTRL_OK;
640 case DEMUXER_CTRL_GET_PERCENT_POS:
641 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
642 return DEMUXER_CTRL_DONTKNOW;
644 *((int *)arg) = (int)((priv->last_pts - priv->avfc->start_time)*100 / priv->avfc->duration);
645 return DEMUXER_CTRL_OK;
646 case DEMUXER_CTRL_SWITCH_AUDIO:
647 case DEMUXER_CTRL_SWITCH_VIDEO:
649 int id = *((int*)arg);
650 int newid = -2;
651 int i, curridx = -1;
652 int nstreams, *pstreams;
653 demux_stream_t *ds;
655 if(cmd == DEMUXER_CTRL_SWITCH_VIDEO)
657 ds = demuxer->video;
658 nstreams = priv->video_streams;
659 pstreams = priv->vstreams;
661 else
663 ds = demuxer->audio;
664 nstreams = priv->audio_streams;
665 pstreams = priv->astreams;
667 for(i = 0; i < nstreams; i++)
669 if(pstreams[i] == ds->id) //current stream id
671 curridx = i;
672 break;
676 if(id == -2) { // no sound
677 i = -1;
678 } else if(id == -1) { // next track
679 i = (curridx + 2) % (nstreams + 1) - 1;
680 if (i >= 0)
681 newid = pstreams[i];
683 else // select track by id
685 for(i = 0; i < nstreams; i++)
687 if(pstreams[i] == id)
689 newid = id;
690 break;
694 if(i == curridx)
695 return DEMUXER_CTRL_NOTIMPL;
696 else
698 ds_free_packs(ds);
699 if(ds->id >= 0)
700 priv->avfc->streams[ds->id]->discard = AVDISCARD_ALL;
701 *((int*)arg) = ds->id = newid;
702 if(newid >= 0)
703 priv->avfc->streams[newid]->discard = AVDISCARD_NONE;
704 return DEMUXER_CTRL_OK;
707 case DEMUXER_CTRL_IDENTIFY_PROGRAM:
709 demux_program_t *prog = arg;
710 AVProgram *program;
711 int p, i;
713 if(priv->avfc->nb_programs < 2)
714 return DEMUXER_CTRL_NOTIMPL;
716 if(prog->progid == -1)
718 p = 0;
719 while(p<priv->avfc->nb_programs && priv->avfc->programs[p]->id != priv->cur_program)
720 p++;
721 p = (p + 1) % priv->avfc->nb_programs;
723 else
725 for(i=0; i<priv->avfc->nb_programs; i++)
726 if(priv->avfc->programs[i]->id == prog->progid)
727 break;
728 if(i==priv->avfc->nb_programs)
729 return DEMUXER_CTRL_NOTIMPL;
730 p = i;
732 prog->vid = prog->aid = prog->sid = -2; //no audio and no video by default
733 redo:
734 program = priv->avfc->programs[p];
735 for(i=0; i<program->nb_stream_indexes; i++)
737 switch(priv->avfc->streams[program->stream_index[i]]->codec->codec_type)
739 case CODEC_TYPE_VIDEO:
740 if(prog->vid == -2)
741 prog->vid = program->stream_index[i];
742 break;
743 case CODEC_TYPE_AUDIO:
744 if(prog->aid == -2)
745 prog->aid = program->stream_index[i];
746 break;
747 case CODEC_TYPE_SUBTITLE:
748 if(prog->sid == -2 && priv->avfc->streams[program->stream_index[i]]->codec->codec_id == CODEC_ID_TEXT)
749 prog->sid = program->stream_index[i];
750 break;
753 if(prog->progid == -1 && prog->vid == -2 && prog->aid == -2)
755 p = (p + 1) % priv->avfc->nb_programs;
756 goto redo;
758 priv->cur_program = prog->progid = program->id;
759 return DEMUXER_CTRL_OK;
761 default:
762 return DEMUXER_CTRL_NOTIMPL;
766 static void demux_close_lavf(demuxer_t *demuxer)
768 lavf_priv_t* priv = demuxer->priv;
769 if (priv){
770 if(priv->avfc)
772 av_freep(&priv->avfc->key);
773 av_close_input_stream(priv->avfc);
775 av_freep(&priv->pb);
776 free(priv); demuxer->priv= NULL;
781 const demuxer_desc_t demuxer_desc_lavf = {
782 "libavformat demuxer",
783 "lavf",
784 "libavformat",
785 "Michael Niedermayer",
786 "supports many formats, requires libavformat",
787 DEMUXER_TYPE_LAVF,
788 0, // Check after other demuxer
789 lavf_check_file,
790 demux_lavf_fill_buffer,
791 demux_open_lavf,
792 demux_close_lavf,
793 demux_seek_lavf,
794 demux_lavf_control
797 const demuxer_desc_t demuxer_desc_lavf_preferred = {
798 "libavformat preferred demuxer",
799 "lavfpref",
800 "libavformat",
801 "Michael Niedermayer",
802 "supports many formats, requires libavformat",
803 DEMUXER_TYPE_LAVF_PREFERRED,
805 lavf_check_preferred_file,
806 demux_lavf_fill_buffer,
807 demux_open_lavf,
808 demux_close_lavf,
809 demux_seek_lavf,
810 demux_lavf_control