typo fix
[mplayer/greg.git] / libmpdemux / demux_lavf.c
blobf906f6688776bee8e7a85183478313e8cbd08b66
1 /*
2 Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 // #include <stdio.h>
20 #include <stdlib.h>
21 // #include <unistd.h>
22 #include <limits.h>
24 #include "config.h"
25 #include "mp_msg.h"
26 #include "help_mp.h"
28 #include "stream/stream.h"
29 #include "demuxer.h"
30 #include "stheader.h"
31 #include "m_option.h"
32 #include "libvo/sub.h"
34 #ifdef USE_LIBAVFORMAT_SO
35 #include <ffmpeg/avformat.h>
36 #include <ffmpeg/avutil.h>
37 #include <ffmpeg/opt.h>
38 #else
39 #include "avformat.h"
40 #include "avutil.h"
41 #include "avi.h"
42 #include "opt.h"
43 #endif
44 #include "riff.h"
46 #define PROBE_BUF_SIZE 2048
48 extern char *audio_lang;
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;
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 {NULL, NULL, 0, 0, 0, 0, NULL}
63 #define BIO_BUFFER_SIZE 32768
65 typedef struct lavf_priv_t{
66 AVInputFormat *avif;
67 AVFormatContext *avfc;
68 ByteIOContext *pb;
69 uint8_t buffer[BIO_BUFFER_SIZE];
70 int audio_streams;
71 int video_streams;
72 int sub_streams;
73 int64_t last_pts;
74 int astreams[MAX_A_STREAMS];
75 int vstreams[MAX_V_STREAMS];
76 int sstreams[MAX_S_STREAMS];
77 int cur_program;
78 }lavf_priv_t;
80 extern void print_wave_header(WAVEFORMATEX *h, int verbose_level);
81 extern void print_video_header(BITMAPINFOHEADER *h, int verbose_level);
83 static const AVCodecTag mp_wav_tags[] = {
84 { CODEC_ID_ADPCM_4XM, MKTAG('4', 'X', 'M', 'A')},
85 { CODEC_ID_ADPCM_EA, MKTAG('A', 'D', 'E', 'A')},
86 { CODEC_ID_ADPCM_IMA_WS, MKTAG('A', 'I', 'W', 'S')},
87 { CODEC_ID_ADPCM_THP, MKTAG('T', 'H', 'P', 'A')},
88 { CODEC_ID_AMR_NB, MKTAG('n', 'b', 0, 0)},
89 { CODEC_ID_COOK, MKTAG('c', 'o', 'o', 'k')},
90 { CODEC_ID_DSICINAUDIO, MKTAG('D', 'C', 'I', 'A')},
91 { CODEC_ID_INTERPLAY_DPCM, MKTAG('I', 'N', 'P', 'A')},
92 { CODEC_ID_MUSEPACK7, MKTAG('M', 'P', 'C', ' ')},
93 { CODEC_ID_MUSEPACK8, MKTAG('M', 'P', 'C', '8')},
94 { CODEC_ID_NELLYMOSER, MKTAG('N', 'E', 'L', 'L')},
95 { CODEC_ID_PCM_S24BE, MKTAG('i', 'n', '2', '4')},
96 { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's')},
97 { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's')},
98 { CODEC_ID_ROQ_DPCM, MKTAG('R', 'o', 'Q', 'A')},
99 { CODEC_ID_SHORTEN, MKTAG('s', 'h', 'r', 'n')},
100 { CODEC_ID_TTA, MKTAG('T', 'T', 'A', '1')},
101 { CODEC_ID_WAVPACK, MKTAG('W', 'V', 'P', 'K')},
102 { CODEC_ID_WESTWOOD_SND1, MKTAG('S', 'N', 'D', '1')},
103 { CODEC_ID_XAN_DPCM, MKTAG('A', 'x', 'a', 'n')},
104 { 0, 0 },
107 const struct AVCodecTag *mp_wav_taglists[] = {codec_wav_tags, mp_wav_tags, 0};
109 static const AVCodecTag mp_bmp_tags[] = {
110 { CODEC_ID_AMV, MKTAG('A', 'M', 'V', 'V')},
111 { CODEC_ID_BETHSOFTVID, MKTAG('B', 'E', 'T', 'H')},
112 { CODEC_ID_C93, MKTAG('C', '9', '3', 'V')},
113 { CODEC_ID_DSICINVIDEO, MKTAG('D', 'C', 'I', 'V')},
114 { CODEC_ID_DXA, MKTAG('D', 'X', 'A', '1')},
115 { CODEC_ID_FLIC, MKTAG('F', 'L', 'I', 'C')},
116 { CODEC_ID_IDCIN, MKTAG('I', 'D', 'C', 'I')},
117 { CODEC_ID_INTERPLAY_VIDEO, MKTAG('I', 'N', 'P', 'V')},
118 { CODEC_ID_ROQ, MKTAG('R', 'o', 'Q', 'V')},
119 { CODEC_ID_THP, MKTAG('T', 'H', 'P', 'V')},
120 { CODEC_ID_TIERTEXSEQVIDEO, MKTAG('T', 'S', 'E', 'Q')},
121 { CODEC_ID_TXD, MKTAG('T', 'X', 'D', 'V')},
122 { CODEC_ID_VMDVIDEO, MKTAG('V', 'M', 'D', 'V')},
123 { CODEC_ID_WS_VQA, MKTAG('V', 'Q', 'A', 'V')},
124 { CODEC_ID_XAN_WC3, MKTAG('W', 'C', '3', 'V')},
125 { CODEC_ID_NUV, MKTAG('N', 'U', 'V', '1')},
126 { 0, 0 },
129 const struct AVCodecTag *mp_bmp_taglists[] = {codec_bmp_tags, mp_bmp_tags, 0};
131 static int mp_read(void *opaque, uint8_t *buf, int size) {
132 stream_t *stream = opaque;
133 int ret;
135 if(stream_eof(stream)) //needed?
136 return -1;
137 ret=stream_read(stream, buf, size);
139 mp_msg(MSGT_HEADER,MSGL_DBG2,"%d=mp_read(%p, %p, %d), eof:%d\n", ret, stream, buf, size, stream->eof);
140 return ret;
143 static offset_t mp_seek(void *opaque, offset_t pos, int whence) {
144 stream_t *stream = opaque;
145 mp_msg(MSGT_HEADER,MSGL_DBG2,"mp_seek(%p, %d, %d)\n", stream, (int)pos, whence);
146 if(whence == SEEK_CUR)
147 pos +=stream_tell(stream);
148 else if(whence == SEEK_END && stream->end_pos > 0)
149 pos += stream->end_pos;
150 else if(whence == SEEK_SET)
151 pos += stream->start_pos;
152 else if(whence == AVSEEK_SIZE && stream->end_pos > 0)
153 return stream->end_pos - stream->start_pos;
154 else
155 return -1;
157 if(pos<0)
158 return -1;
159 if(pos<stream->end_pos && stream->eof)
160 stream_reset(stream);
161 if(stream_seek(stream, pos)==0)
162 return -1;
164 return pos - stream->start_pos;
167 static void list_formats(void) {
168 AVInputFormat *fmt;
169 mp_msg(MSGT_DEMUX, MSGL_INFO, "Available lavf input formats:\n");
170 for (fmt = first_iformat; fmt; fmt = fmt->next)
171 mp_msg(MSGT_DEMUX, MSGL_INFO, "%15s : %s\n", fmt->name, fmt->long_name);
174 static int lavf_check_file(demuxer_t *demuxer){
175 AVProbeData avpd;
176 uint8_t buf[PROBE_BUF_SIZE];
177 lavf_priv_t *priv;
179 if(!demuxer->priv)
180 demuxer->priv=calloc(sizeof(lavf_priv_t),1);
181 priv= demuxer->priv;
183 av_register_all();
185 if(stream_read(demuxer->stream, buf, PROBE_BUF_SIZE)!=PROBE_BUF_SIZE)
186 return 0;
187 avpd.filename= demuxer->stream->url;
188 avpd.buf= buf;
189 avpd.buf_size= PROBE_BUF_SIZE;
191 if (opt_format) {
192 if (strcmp(opt_format, "help") == 0) {
193 list_formats();
194 return 0;
196 priv->avif= av_find_input_format(opt_format);
197 if (!priv->avif) {
198 mp_msg(MSGT_DEMUX,MSGL_FATAL,"Unknown lavf format %s\n", opt_format);
199 return 0;
201 mp_msg(MSGT_DEMUX,MSGL_INFO,"Forced lavf %s demuxer\n", priv->avif->long_name);
202 return DEMUXER_TYPE_LAVF;
204 priv->avif= av_probe_input_format(&avpd, 1);
205 if(!priv->avif){
206 mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: no clue about this gibberish!\n");
207 return 0;
208 }else
209 mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: %s\n", priv->avif->long_name);
211 return DEMUXER_TYPE_LAVF;
214 static const char *preferred_list[] = {
215 "dxa",
216 "wv",
217 "nuv",
218 "nut",
219 "gxf",
220 "mxf",
221 "flv",
222 "swf",
223 NULL
226 static int lavf_check_preferred_file(demuxer_t *demuxer){
227 if (lavf_check_file(demuxer)) {
228 char **p = preferred_list;
229 lavf_priv_t *priv = demuxer->priv;
230 while (*p) {
231 if (strcmp(*p, priv->avif->name) == 0)
232 return DEMUXER_TYPE_LAVF_PREFERRED;
233 p++;
236 return 0;
239 static uint8_t char2int(char c) {
240 if (c >= '0' && c <= '9') return c - '0';
241 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
242 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
243 return 0;
246 static void parse_cryptokey(AVFormatContext *avfc, const char *str) {
247 int len = strlen(str) / 2;
248 uint8_t *key = av_mallocz(len);
249 int i;
250 avfc->keylen = len;
251 avfc->key = key;
252 for (i = 0; i < len; i++, str += 2)
253 *key++ = (char2int(str[0]) << 4) | char2int(str[1]);
256 static void handle_stream(demuxer_t *demuxer, AVFormatContext *avfc, int i) {
257 lavf_priv_t *priv= demuxer->priv;
258 AVStream *st= avfc->streams[i];
259 AVCodecContext *codec= st->codec;
260 int g;
262 switch(codec->codec_type){
263 case CODEC_TYPE_AUDIO:{
264 WAVEFORMATEX *wf= calloc(sizeof(WAVEFORMATEX) + codec->extradata_size, 1);
265 sh_audio_t* sh_audio;
266 if(priv->audio_streams >= MAX_A_STREAMS)
267 break;
268 sh_audio=new_sh_audio(demuxer, i);
269 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_AudioID, "lavf", i);
270 if(!sh_audio)
271 break;
272 priv->astreams[priv->audio_streams] = i;
273 priv->audio_streams++;
274 // mp4a tag is used for all mp4 files no matter what they actually contain
275 if(codec->codec_tag == MKTAG('m', 'p', '4', 'a'))
276 codec->codec_tag= 0;
277 if(codec->codec_id == CODEC_ID_ADPCM_IMA_AMV)
278 codec->codec_tag= MKTAG('A','M','V','A');
279 if(!codec->codec_tag)
280 codec->codec_tag= av_codec_get_tag(mp_wav_taglists, codec->codec_id);
281 wf->wFormatTag= codec->codec_tag;
282 wf->nChannels= codec->channels;
283 wf->nSamplesPerSec= codec->sample_rate;
284 wf->nAvgBytesPerSec= codec->bit_rate/8;
285 wf->nBlockAlign= codec->block_align ? codec->block_align : 1;
286 wf->wBitsPerSample= codec->bits_per_sample;
287 wf->cbSize= codec->extradata_size;
288 if(codec->extradata_size)
289 memcpy(wf + 1, codec->extradata, codec->extradata_size);
290 sh_audio->wf= wf;
291 sh_audio->audio.dwSampleSize= codec->block_align;
292 if(codec->frame_size && codec->sample_rate){
293 sh_audio->audio.dwScale=codec->frame_size;
294 sh_audio->audio.dwRate= codec->sample_rate;
295 }else{
296 sh_audio->audio.dwScale= codec->block_align ? codec->block_align*8 : 8;
297 sh_audio->audio.dwRate = codec->bit_rate;
299 g= ff_gcd(sh_audio->audio.dwScale, sh_audio->audio.dwRate);
300 sh_audio->audio.dwScale /= g;
301 sh_audio->audio.dwRate /= g;
302 // 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);
303 sh_audio->ds= demuxer->audio;
304 sh_audio->format= codec->codec_tag;
305 sh_audio->channels= codec->channels;
306 sh_audio->samplerate= codec->sample_rate;
307 sh_audio->i_bps= codec->bit_rate/8;
308 switch (codec->codec_id) {
309 case CODEC_ID_PCM_S8:
310 case CODEC_ID_PCM_U8:
311 sh_audio->samplesize = 1;
312 break;
313 case CODEC_ID_PCM_S16LE:
314 case CODEC_ID_PCM_S16BE:
315 case CODEC_ID_PCM_U16LE:
316 case CODEC_ID_PCM_U16BE:
317 sh_audio->samplesize = 2;
318 break;
319 case CODEC_ID_PCM_ALAW:
320 sh_audio->format = 0x6;
321 break;
322 case CODEC_ID_PCM_MULAW:
323 sh_audio->format = 0x7;
324 break;
326 if(mp_msg_test(MSGT_HEADER,MSGL_V) ) print_wave_header(sh_audio->wf, MSGL_V);
327 if((audio_lang && st->language[0] && !strncmp(audio_lang, st->language, 3))
328 || (demuxer->audio->id == i || demuxer->audio->id == -1)) {
329 demuxer->audio->id = i;
330 demuxer->audio->sh= demuxer->a_streams[i];
331 } else
332 st->discard= AVDISCARD_ALL;
333 break;
335 case CODEC_TYPE_VIDEO:{
336 sh_video_t* sh_video;
337 BITMAPINFOHEADER *bih;
338 if(priv->video_streams >= MAX_V_STREAMS)
339 break;
340 sh_video=new_sh_video(demuxer, i);
341 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_VideoID, "lavf", i);
342 if(!sh_video) break;
343 priv->vstreams[priv->video_streams] = i;
344 priv->video_streams++;
345 bih=calloc(sizeof(BITMAPINFOHEADER) + codec->extradata_size,1);
347 if(!codec->codec_tag)
348 codec->codec_tag= av_codec_get_tag(mp_bmp_taglists, codec->codec_id);
349 bih->biSize= sizeof(BITMAPINFOHEADER) + codec->extradata_size;
350 bih->biWidth= codec->width;
351 bih->biHeight= codec->height;
352 bih->biBitCount= codec->bits_per_sample;
353 bih->biSizeImage = bih->biWidth * bih->biHeight * bih->biBitCount/8;
354 bih->biCompression= codec->codec_tag;
355 sh_video->bih= bih;
356 sh_video->disp_w= codec->width;
357 sh_video->disp_h= codec->height;
358 if (st->time_base.den) { /* if container has time_base, use that */
359 sh_video->video.dwRate= st->time_base.den;
360 sh_video->video.dwScale= st->time_base.num;
361 } else {
362 sh_video->video.dwRate= codec->time_base.den;
363 sh_video->video.dwScale= codec->time_base.num;
365 sh_video->fps=av_q2d(st->r_frame_rate);
366 sh_video->frametime=1/av_q2d(st->r_frame_rate);
367 sh_video->format=bih->biCompression;
368 sh_video->aspect=codec->width * codec->sample_aspect_ratio.num
369 / (float)(codec->height * codec->sample_aspect_ratio.den);
370 sh_video->i_bps=codec->bit_rate/8;
371 mp_msg(MSGT_DEMUX,MSGL_DBG2,"aspect= %d*%d/(%d*%d)\n",
372 codec->width, codec->sample_aspect_ratio.num,
373 codec->height, codec->sample_aspect_ratio.den);
375 sh_video->ds= demuxer->video;
376 if(codec->extradata_size)
377 memcpy(sh_video->bih + 1, codec->extradata, codec->extradata_size);
378 if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_video_header(sh_video->bih, MSGL_V);
380 short biPlanes;
381 int biXPelsPerMeter;
382 int biYPelsPerMeter;
383 int biClrUsed;
384 int biClrImportant;
386 if(demuxer->video->id != i && demuxer->video->id != -1)
387 st->discard= AVDISCARD_ALL;
388 else{
389 demuxer->video->id = i;
390 demuxer->video->sh= demuxer->v_streams[i];
392 break;
394 case CODEC_TYPE_SUBTITLE:{
395 sh_sub_t* sh_sub;
396 char type;
397 if(priv->sub_streams >= MAX_S_STREAMS)
398 break;
399 /* only support text subtitles for now */
400 if(codec->codec_id == CODEC_ID_TEXT)
401 type = 't';
402 else if(codec->codec_id == CODEC_ID_SSA)
403 type = 'a';
404 else
405 break;
406 sh_sub = new_sh_sub_sid(demuxer, i, priv->sub_streams);
407 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_SubtitleID, "lavf", priv->sub_streams);
408 if(!sh_sub) break;
409 priv->sstreams[priv->sub_streams] = i;
410 sh_sub->type = type;
411 if (codec->extradata_size) {
412 sh_sub->extradata = malloc(codec->extradata_size);
413 memcpy(sh_sub->extradata, codec->extradata, codec->extradata_size);
414 sh_sub->extradata_len = codec->extradata_size;
416 demuxer->sub->sh = demuxer->s_streams[priv->sub_streams++];
417 break;
419 default:
420 st->discard= AVDISCARD_ALL;
424 static demuxer_t* demux_open_lavf(demuxer_t *demuxer){
425 AVFormatContext *avfc;
426 AVFormatParameters ap;
427 const AVOption *opt;
428 lavf_priv_t *priv= demuxer->priv;
429 int i;
430 char mp_filename[256]="mp:";
432 memset(&ap, 0, sizeof(AVFormatParameters));
434 stream_seek(demuxer->stream, 0);
436 avfc = av_alloc_format_context();
438 if (opt_cryptokey)
439 parse_cryptokey(avfc, opt_cryptokey);
440 if (correct_pts)
441 avfc->flags |= AVFMT_FLAG_GENPTS;
442 if (index_mode == 0)
443 avfc->flags |= AVFMT_FLAG_IGNIDX;
445 ap.prealloced_context = 1;
446 if(opt_probesize) {
447 opt = av_set_int(avfc, "probesize", opt_probesize);
448 if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option probesize to %u\n", opt_probesize);
450 if(opt_analyzeduration) {
451 opt = av_set_int(avfc, "analyzeduration", opt_analyzeduration * AV_TIME_BASE);
452 if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option analyzeduration to %u\n", opt_analyzeduration);
455 if(demuxer->stream->url)
456 strncpy(mp_filename + 3, demuxer->stream->url, sizeof(mp_filename)-3);
457 else
458 strncpy(mp_filename + 3, "foobar.dummy", sizeof(mp_filename)-3);
460 priv->pb = av_alloc_put_byte(priv->buffer, BIO_BUFFER_SIZE, 0,
461 demuxer->stream, mp_read, NULL, mp_seek);
462 priv->pb->is_streamed = !demuxer->stream->end_pos;
464 if(av_open_input_stream(&avfc, priv->pb, mp_filename, priv->avif, &ap)<0){
465 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_open_input_stream() failed\n");
466 return NULL;
469 priv->avfc= avfc;
471 if(av_find_stream_info(avfc) < 0){
472 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_find_stream_info() failed\n");
473 return NULL;
476 if(avfc->title [0]) demux_info_add(demuxer, "name" , avfc->title );
477 if(avfc->author [0]) demux_info_add(demuxer, "author" , avfc->author );
478 if(avfc->copyright[0]) demux_info_add(demuxer, "copyright", avfc->copyright);
479 if(avfc->comment [0]) demux_info_add(demuxer, "comments" , avfc->comment );
480 if(avfc->album [0]) demux_info_add(demuxer, "album" , avfc->album );
481 // if(avfc->year ) demux_info_add(demuxer, "year" , avfc->year );
482 // if(avfc->track ) demux_info_add(demuxer, "track" , avfc->track );
483 if(avfc->genre [0]) demux_info_add(demuxer, "genre" , avfc->genre );
485 if(avfc->nb_programs) {
486 int p, start=0, found=0;
488 if(ts_prog) {
489 for(p=0; p<avfc->nb_programs; p++) {
490 if(avfc->programs[p]->id == ts_prog) {
491 start = p;
492 found = 1;
493 break;
496 if(!found) {
497 mp_msg(MSGT_HEADER,MSGL_ERR,"DEMUX_LAVF: program %d doesn't seem to be present\n", ts_prog);
498 return NULL;
501 p = start;
502 do {
503 AVProgram *program = avfc->programs[p];
504 mp_msg(MSGT_HEADER,MSGL_INFO,"LAVF: Program %d %s\n", program->id, (program->name ? program->name : ""));
505 for(i=0; i<program->nb_stream_indexes; i++)
506 handle_stream(demuxer, avfc, program->stream_index[i]);
507 if(!priv->cur_program && (demuxer->video->sh || demuxer->audio->sh))
508 priv->cur_program = program->id;
509 p = (p + 1) % avfc->nb_programs;
510 } while(p!=start);
511 } else
512 for(i=0; i<avfc->nb_streams; i++)
513 handle_stream(demuxer, avfc, i);
515 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: %d audio and %d video streams found\n",priv->audio_streams,priv->video_streams);
516 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: build %d\n", LIBAVFORMAT_BUILD);
517 if(!priv->audio_streams) demuxer->audio->id=-2; // nosound
518 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio;
519 if(!priv->video_streams){
520 if(!priv->audio_streams){
521 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF: no audio or video headers found - broken file?\n");
522 return NULL;
524 demuxer->video->id=-2; // audio-only
525 } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video;
527 return demuxer;
530 static int demux_lavf_fill_buffer(demuxer_t *demux, demux_stream_t *dsds){
531 lavf_priv_t *priv= demux->priv;
532 AVPacket pkt;
533 demux_packet_t *dp;
534 demux_stream_t *ds;
535 int id;
536 mp_msg(MSGT_DEMUX,MSGL_DBG2,"demux_lavf_fill_buffer()\n");
538 demux->filepos=stream_tell(demux->stream);
540 if(av_read_frame(priv->avfc, &pkt) < 0)
541 return 0;
543 id= pkt.stream_index;
545 if(id==demux->audio->id){
546 // audio
547 ds=demux->audio;
548 if(!ds->sh){
549 ds->sh=demux->a_streams[id];
550 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected LAVF audio ID = %d\n",ds->id);
552 } else if(id==demux->video->id){
553 // video
554 ds=demux->video;
555 if(!ds->sh){
556 ds->sh=demux->v_streams[id];
557 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected LAVF video ID = %d\n",ds->id);
559 } else if(id==demux->sub->id){
560 // subtitle
561 ds=demux->sub;
562 sub_utf8=1;
563 } else {
564 av_free_packet(&pkt);
565 return 1;
568 if(0/*pkt.destruct == av_destruct_packet*/){
569 //ok kids, dont try this at home :)
570 dp=malloc(sizeof(demux_packet_t));
571 dp->len=pkt.size;
572 dp->next=NULL;
573 dp->refcount=1;
574 dp->master=NULL;
575 dp->buffer=pkt.data;
576 pkt.destruct= NULL;
577 }else{
578 dp=new_demux_packet(pkt.size);
579 memcpy(dp->buffer, pkt.data, pkt.size);
580 av_free_packet(&pkt);
583 if(pkt.pts != AV_NOPTS_VALUE){
584 dp->pts=pkt.pts * av_q2d(priv->avfc->streams[id]->time_base);
585 priv->last_pts= dp->pts * AV_TIME_BASE;
586 if(pkt.duration)
587 dp->endpts = dp->pts + pkt.duration * av_q2d(priv->avfc->streams[id]->time_base);
589 dp->pos=demux->filepos;
590 dp->flags= !!(pkt.flags&PKT_FLAG_KEY);
591 // append packet to DS stream:
592 ds_add_packet(ds,dp);
593 return 1;
596 static void demux_seek_lavf(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags){
597 lavf_priv_t *priv = demuxer->priv;
598 int avsflags = 0;
599 mp_msg(MSGT_DEMUX,MSGL_DBG2,"demux_seek_lavf(%p, %f, %f, %d)\n", demuxer, rel_seek_secs, audio_delay, flags);
601 if (flags & 1) // absolute seek
602 priv->last_pts = priv->avfc->start_time;
603 if (flags & 2) { // percent seek
604 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
605 return;
606 priv->last_pts += rel_seek_secs * priv->avfc->duration;
607 } else {
608 priv->last_pts += rel_seek_secs * AV_TIME_BASE;
609 if (rel_seek_secs < 0) avsflags = AVSEEK_FLAG_BACKWARD;
611 av_seek_frame(priv->avfc, -1, priv->last_pts, avsflags);
614 static int demux_lavf_control(demuxer_t *demuxer, int cmd, void *arg)
616 lavf_priv_t *priv = demuxer->priv;
618 switch (cmd) {
619 case DEMUXER_CTRL_GET_TIME_LENGTH:
620 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
621 return DEMUXER_CTRL_DONTKNOW;
623 *((double *)arg) = (double)priv->avfc->duration / AV_TIME_BASE;
624 return DEMUXER_CTRL_OK;
626 case DEMUXER_CTRL_GET_PERCENT_POS:
627 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
628 return DEMUXER_CTRL_DONTKNOW;
630 *((int *)arg) = (int)((priv->last_pts - priv->avfc->start_time)*100 / priv->avfc->duration);
631 return DEMUXER_CTRL_OK;
632 case DEMUXER_CTRL_SWITCH_AUDIO:
633 case DEMUXER_CTRL_SWITCH_VIDEO:
635 int id = *((int*)arg);
636 int newid = -2;
637 int i, curridx = -2;
638 int nstreams, *pstreams;
639 demux_stream_t *ds;
641 if(cmd == DEMUXER_CTRL_SWITCH_VIDEO)
643 ds = demuxer->video;
644 nstreams = priv->video_streams;
645 pstreams = priv->vstreams;
647 else
649 ds = demuxer->audio;
650 nstreams = priv->audio_streams;
651 pstreams = priv->astreams;
653 if(id == -2)
655 if(ds->id >= 0)
656 priv->avfc->streams[ds->id]->discard = AVDISCARD_ALL;
657 ds_free_packs(ds);
658 *((int*)arg) = ds->id = -2;
659 return DEMUXER_CTRL_OK;
661 for(i = 0; i < nstreams; i++)
663 if(pstreams[i] == ds->id) //current stream id
665 curridx = i;
666 break;
670 if(id < 0)
672 i = (curridx + 1) % nstreams;
673 newid = pstreams[i];
675 else
677 for(i = 0; i < nstreams; i++)
679 if(pstreams[i] == id)
681 newid = id;
682 break;
686 if(i == curridx)
687 return DEMUXER_CTRL_NOTIMPL;
688 else
690 ds_free_packs(ds);
691 if(ds->id >= 0)
692 priv->avfc->streams[ds->id]->discard = AVDISCARD_ALL;
693 *((int*)arg) = ds->id = newid;
694 if(newid >= 0)
695 priv->avfc->streams[newid]->discard = AVDISCARD_NONE;
696 return DEMUXER_CTRL_OK;
699 case DEMUXER_CTRL_IDENTIFY_PROGRAM:
701 demux_program_t *prog = arg;
702 AVProgram *program;
703 int p, i;
705 if(priv->avfc->nb_programs < 2)
706 return DEMUXER_CTRL_NOTIMPL;
708 if(prog->progid == -1)
710 p = 0;
711 while(p<priv->avfc->nb_programs && priv->avfc->programs[p]->id != priv->cur_program)
712 p++;
713 p = (p + 1) % priv->avfc->nb_programs;
715 else
717 for(i=0; i<priv->avfc->nb_programs; i++)
718 if(priv->avfc->programs[i]->id == prog->progid)
719 break;
720 if(i==priv->avfc->nb_programs)
721 return DEMUXER_CTRL_NOTIMPL;
722 p = i;
724 prog->vid = prog->aid = prog->sid = -2; //no audio and no video by default
725 redo:
726 program = priv->avfc->programs[p];
727 for(i=0; i<program->nb_stream_indexes; i++)
729 switch(priv->avfc->streams[program->stream_index[i]]->codec->codec_type)
731 case CODEC_TYPE_VIDEO:
732 if(prog->vid == -2)
733 prog->vid = program->stream_index[i];
734 break;
735 case CODEC_TYPE_AUDIO:
736 if(prog->aid == -2)
737 prog->aid = program->stream_index[i];
738 break;
739 case CODEC_TYPE_SUBTITLE:
740 if(prog->sid == -2 && priv->avfc->streams[program->stream_index[i]]->codec->codec_id == CODEC_ID_TEXT)
741 prog->sid = program->stream_index[i];
742 break;
745 if(prog->progid == -1 && prog->vid == -2 && prog->aid == -2)
747 p = (p + 1) % priv->avfc->nb_programs;
748 goto redo;
750 priv->cur_program = prog->progid = program->id;
751 return DEMUXER_CTRL_OK;
753 default:
754 return DEMUXER_CTRL_NOTIMPL;
758 /** \brief Get the language code for a subtitle track.
760 Retrieves the language code for a subtitle track.
762 \param demuxer The demuxer to work on
763 \param track_num The subtitle track number to get the language from
765 char *demux_lavf_sub_lang(demuxer_t *demuxer, int track_num)
767 lavf_priv_t *priv = demuxer->priv;
768 return priv->avfc->streams[priv->sstreams[track_num]]->language;
771 static void demux_close_lavf(demuxer_t *demuxer)
773 lavf_priv_t* priv = demuxer->priv;
774 if (priv){
775 if(priv->avfc)
777 av_freep(&priv->avfc->key);
778 av_close_input_stream(priv->avfc);
780 av_freep(&priv->pb);
781 free(priv); demuxer->priv= NULL;
786 const demuxer_desc_t demuxer_desc_lavf = {
787 "libavformat demuxer",
788 "lavf",
789 "libavformat",
790 "Michael Niedermayer",
791 "supports many formats, requires libavformat",
792 DEMUXER_TYPE_LAVF,
793 0, // Check after other demuxer
794 lavf_check_file,
795 demux_lavf_fill_buffer,
796 demux_open_lavf,
797 demux_close_lavf,
798 demux_seek_lavf,
799 demux_lavf_control
802 const demuxer_desc_t demuxer_desc_lavf_preferred = {
803 "libavformat preferred demuxer",
804 "lavfpref",
805 "libavformat",
806 "Michael Niedermayer",
807 "supports many formats, requires libavformat",
808 DEMUXER_TYPE_LAVF_PREFERRED,
810 lavf_check_preferred_file,
811 demux_lavf_fill_buffer,
812 demux_open_lavf,
813 demux_close_lavf,
814 demux_seek_lavf,
815 demux_lavf_control