synced with r21612
[mplayer/greg.git] / libmpdemux / demux_lavf.c
blob5472505adfb18834ad97a4574ce635232c45e245
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.h"
29 #include "demuxer.h"
30 #include "stheader.h"
31 #include "m_option.h"
33 #ifdef USE_LIBAVFORMAT_SO
34 #include <ffmpeg/avformat.h>
35 #include <ffmpeg/opt.h>
36 typedef struct CodecTag {
37 int id;
38 unsigned int tag;
39 unsigned int invalid_asf : 1;
40 } CodecTag;
41 #else
42 #include "avformat.h"
43 #include "riff.h"
44 #include "avi.h"
45 #include "opt.h"
46 #endif
48 #define PROBE_BUF_SIZE 2048
50 extern char *audio_lang;
51 static unsigned int opt_probesize = 0;
53 m_option_t lavfdopts_conf[] = {
54 {"probesize", &(opt_probesize), CONF_TYPE_INT, CONF_RANGE, 32, INT_MAX, NULL},
55 {NULL, NULL, 0, 0, 0, 0, NULL}
59 typedef struct lavf_priv_t{
60 AVInputFormat *avif;
61 AVFormatContext *avfc;
62 ByteIOContext pb;
63 int audio_streams;
64 int video_streams;
65 int64_t last_pts;
66 int astreams[MAX_A_STREAMS];
67 int vstreams[MAX_V_STREAMS];
68 }lavf_priv_t;
70 extern void print_wave_header(WAVEFORMATEX *h, int verbose_level);
71 extern void print_video_header(BITMAPINFOHEADER *h, int verbose_level);
73 int64_t ff_gcd(int64_t a, int64_t b);
75 const CodecTag mp_wav_tags[] = {
76 { CODEC_ID_ADPCM_4XM, MKTAG('4', 'X', 'M', 'A')},
77 { CODEC_ID_ADPCM_EA, MKTAG('A', 'D', 'E', 'A')},
78 { CODEC_ID_ADPCM_IMA_WS, MKTAG('A', 'I', 'W', 'S')},
79 { CODEC_ID_DSICINAUDIO, MKTAG('D', 'C', 'I', 'A')},
80 { CODEC_ID_INTERPLAY_DPCM, MKTAG('I', 'N', 'P', 'A')},
81 { CODEC_ID_MUSEPACK7, MKTAG('M', 'P', 'C', ' ')},
82 { CODEC_ID_PCM_S24BE, MKTAG('i', 'n', '2', '4')},
83 { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's')},
84 { CODEC_ID_ROQ_DPCM, MKTAG('R', 'o', 'Q', 'A')},
85 { CODEC_ID_SHORTEN, MKTAG('s', 'h', 'r', 'n')},
86 { CODEC_ID_TTA, MKTAG('T', 'T', 'A', '1')},
87 { CODEC_ID_WAVPACK, MKTAG('W', 'V', 'P', 'K')},
88 { CODEC_ID_WESTWOOD_SND1, MKTAG('S', 'N', 'D', '1')},
89 { CODEC_ID_XAN_DPCM, MKTAG('A', 'x', 'a', 'n')},
90 { 0, 0 },
93 const CodecTag mp_bmp_tags[] = {
94 { CODEC_ID_DSICINVIDEO, MKTAG('D', 'C', 'I', 'V')},
95 { CODEC_ID_FLIC, MKTAG('F', 'L', 'I', 'C')},
96 { CODEC_ID_IDCIN, MKTAG('I', 'D', 'C', 'I')},
97 { CODEC_ID_INTERPLAY_VIDEO, MKTAG('I', 'N', 'P', 'V')},
98 { CODEC_ID_ROQ, MKTAG('R', 'o', 'Q', 'V')},
99 { CODEC_ID_TIERTEXSEQVIDEO, MKTAG('T', 'S', 'E', 'Q')},
100 { CODEC_ID_VMDVIDEO, MKTAG('V', 'M', 'D', 'V')},
101 { CODEC_ID_WS_VQA, MKTAG('V', 'Q', 'A', 'V')},
102 { CODEC_ID_XAN_WC3, MKTAG('W', 'C', '3', 'V')},
103 { 0, 0 },
106 static int mp_open(URLContext *h, const char *filename, int flags){
107 return 0;
110 static int mp_read(URLContext *h, unsigned char *buf, int size){
111 stream_t *stream = (stream_t*)h->priv_data;
112 int ret;
114 if(stream_eof(stream)) //needed?
115 return -1;
116 ret=stream_read(stream, buf, size);
118 mp_msg(MSGT_HEADER,MSGL_DBG2,"%d=mp_read(%p, %p, %d), eof:%d\n", ret, h, buf, size, stream->eof);
119 return ret;
122 static int mp_write(URLContext *h, unsigned char *buf, int size){
123 return -1;
126 static offset_t mp_seek(URLContext *h, offset_t pos, int whence){
127 stream_t *stream = (stream_t*)h->priv_data;
129 mp_msg(MSGT_HEADER,MSGL_DBG2,"mp_seek(%p, %d, %d)\n", h, (int)pos, whence);
130 if(whence == SEEK_CUR)
131 pos +=stream_tell(stream);
132 else if(whence == SEEK_END)
133 pos += stream->end_pos;
134 else if(whence != SEEK_SET)
135 return -1;
137 if(pos<stream->end_pos && stream->eof)
138 stream_reset(stream);
139 if(stream_seek(stream, pos)==0)
140 return -1;
142 return pos;
145 static int mp_close(URLContext *h){
146 return 0;
149 static URLProtocol mp_protocol = {
150 "mp",
151 mp_open,
152 mp_read,
153 mp_write,
154 mp_seek,
155 mp_close,
158 static int lavf_check_file(demuxer_t *demuxer){
159 AVProbeData avpd;
160 uint8_t buf[PROBE_BUF_SIZE];
161 lavf_priv_t *priv;
163 if(!demuxer->priv)
164 demuxer->priv=calloc(sizeof(lavf_priv_t),1);
165 priv= demuxer->priv;
167 av_register_all();
169 if(stream_read(demuxer->stream, buf, PROBE_BUF_SIZE)!=PROBE_BUF_SIZE)
170 return 0;
171 avpd.filename= demuxer->stream->url;
172 avpd.buf= buf;
173 avpd.buf_size= PROBE_BUF_SIZE;
175 priv->avif= av_probe_input_format(&avpd, 1);
176 if(!priv->avif){
177 mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: no clue about this gibberish!\n");
178 return 0;
179 }else
180 mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: %s\n", priv->avif->long_name);
182 return DEMUXER_TYPE_LAVF;
185 static demuxer_t* demux_open_lavf(demuxer_t *demuxer){
186 AVFormatContext *avfc;
187 AVFormatParameters ap;
188 AVOption *opt;
189 lavf_priv_t *priv= demuxer->priv;
190 int i,g;
191 char mp_filename[256]="mp:";
193 memset(&ap, 0, sizeof(AVFormatParameters));
195 stream_seek(demuxer->stream, 0);
197 register_protocol(&mp_protocol);
199 avfc = av_alloc_format_context();
201 if (correct_pts)
202 avfc->flags |= AVFMT_FLAG_GENPTS;
203 if (index_mode == 0)
204 avfc->flags |= AVFMT_FLAG_IGNIDX;
206 ap.prealloced_context = 1;
207 if(opt_probesize) {
208 double d = (double) opt_probesize;
209 opt = av_set_double(avfc, "probesize", opt_probesize);
210 if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option probesize to %.3f\r\n", d);
213 if(demuxer->stream->url)
214 strncpy(mp_filename + 3, demuxer->stream->url, sizeof(mp_filename)-3);
215 else
216 strncpy(mp_filename + 3, "foobar.dummy", sizeof(mp_filename)-3);
218 url_fopen(&priv->pb, mp_filename, URL_RDONLY);
220 ((URLContext*)(priv->pb.opaque))->priv_data= demuxer->stream;
222 if(av_open_input_stream(&avfc, &priv->pb, mp_filename, priv->avif, &ap)<0){
223 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_open_input_stream() failed\n");
224 return NULL;
227 priv->avfc= avfc;
229 if(av_find_stream_info(avfc) < 0){
230 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_find_stream_info() failed\n");
231 return NULL;
234 if(avfc->title [0]) demux_info_add(demuxer, "name" , avfc->title );
235 if(avfc->author [0]) demux_info_add(demuxer, "author" , avfc->author );
236 if(avfc->copyright[0]) demux_info_add(demuxer, "copyright", avfc->copyright);
237 if(avfc->comment [0]) demux_info_add(demuxer, "comments" , avfc->comment );
238 if(avfc->album [0]) demux_info_add(demuxer, "album" , avfc->album );
239 // if(avfc->year ) demux_info_add(demuxer, "year" , avfc->year );
240 // if(avfc->track ) demux_info_add(demuxer, "track" , avfc->track );
241 if(avfc->genre [0]) demux_info_add(demuxer, "genre" , avfc->genre );
243 for(i=0; i<avfc->nb_streams; i++){
244 AVStream *st= avfc->streams[i];
245 AVCodecContext *codec= st->codec;
247 switch(codec->codec_type){
248 case CODEC_TYPE_AUDIO:{
249 WAVEFORMATEX *wf= calloc(sizeof(WAVEFORMATEX) + codec->extradata_size, 1);
250 sh_audio_t* sh_audio;
251 if(priv->audio_streams >= MAX_A_STREAMS)
252 break;
253 sh_audio=new_sh_audio(demuxer, i);
254 if(!sh_audio)
255 break;
256 priv->astreams[priv->audio_streams] = i;
257 priv->audio_streams++;
258 if(!codec->codec_tag)
259 codec->codec_tag= codec_get_wav_tag(codec->codec_id);
260 if(!codec->codec_tag)
261 codec->codec_tag= codec_get_tag(mp_wav_tags, codec->codec_id);
262 wf->wFormatTag= codec->codec_tag;
263 wf->nChannels= codec->channels;
264 wf->nSamplesPerSec= codec->sample_rate;
265 wf->nAvgBytesPerSec= codec->bit_rate/8;
266 wf->nBlockAlign= codec->block_align;
267 wf->wBitsPerSample= codec->bits_per_sample;
268 wf->cbSize= codec->extradata_size;
269 if(codec->extradata_size){
270 memcpy(
271 wf + 1,
272 codec->extradata,
273 codec->extradata_size);
275 sh_audio->wf= wf;
276 sh_audio->audio.dwSampleSize= codec->block_align;
277 if(codec->frame_size && codec->sample_rate){
278 sh_audio->audio.dwScale=codec->frame_size;
279 sh_audio->audio.dwRate= codec->sample_rate;
280 }else{
281 sh_audio->audio.dwScale= codec->block_align ? codec->block_align*8 : 8;
282 sh_audio->audio.dwRate = codec->bit_rate;
284 g= ff_gcd(sh_audio->audio.dwScale, sh_audio->audio.dwRate);
285 sh_audio->audio.dwScale /= g;
286 sh_audio->audio.dwRate /= g;
287 // 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);
288 sh_audio->ds= demuxer->audio;
289 sh_audio->format= codec->codec_tag;
290 sh_audio->channels= codec->channels;
291 sh_audio->samplerate= codec->sample_rate;
292 sh_audio->i_bps= codec->bit_rate/8;
293 switch (codec->codec_id) {
294 case CODEC_ID_PCM_S8:
295 case CODEC_ID_PCM_U8:
296 sh_audio->samplesize = 1;
297 break;
298 case CODEC_ID_PCM_S16LE:
299 case CODEC_ID_PCM_S16BE:
300 case CODEC_ID_PCM_U16LE:
301 case CODEC_ID_PCM_U16BE:
302 sh_audio->samplesize = 2;
303 break;
304 case CODEC_ID_PCM_ALAW:
305 sh_audio->format = 0x6;
306 break;
307 case CODEC_ID_PCM_MULAW:
308 sh_audio->format = 0x7;
309 break;
311 if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_wave_header(sh_audio->wf, MSGL_V);
312 if((audio_lang && st->language[0] && !strncmp(audio_lang, st->language, 3))
313 || (demuxer->audio->id == i || demuxer->audio->id == -1)
315 demuxer->audio->id = i;
316 demuxer->audio->sh= demuxer->a_streams[i];
318 else
319 st->discard= AVDISCARD_ALL;
320 break;}
321 case CODEC_TYPE_VIDEO:{
322 sh_video_t* sh_video;
323 BITMAPINFOHEADER *bih;
324 if(priv->video_streams >= MAX_V_STREAMS)
325 break;
326 sh_video=new_sh_video(demuxer, i);
327 if(!sh_video) break;
328 priv->vstreams[priv->video_streams] = i;
329 priv->video_streams++;
330 bih=calloc(sizeof(BITMAPINFOHEADER) + codec->extradata_size,1);
332 if(!codec->codec_tag)
333 codec->codec_tag= codec_get_bmp_tag(codec->codec_id);
334 if(!codec->codec_tag)
335 codec->codec_tag= codec_get_tag(mp_bmp_tags, codec->codec_id);
336 bih->biSize= sizeof(BITMAPINFOHEADER) + codec->extradata_size;
337 bih->biWidth= codec->width;
338 bih->biHeight= codec->height;
339 bih->biBitCount= codec->bits_per_sample;
340 bih->biSizeImage = bih->biWidth * bih->biHeight * bih->biBitCount/8;
341 bih->biCompression= codec->codec_tag;
342 sh_video->bih= bih;
343 sh_video->disp_w= codec->width;
344 sh_video->disp_h= codec->height;
345 if (st->time_base.den) { /* if container has time_base, use that */
346 sh_video->video.dwRate= st->time_base.den;
347 sh_video->video.dwScale= st->time_base.num;
348 } else {
349 sh_video->video.dwRate= codec->time_base.den;
350 sh_video->video.dwScale= codec->time_base.num;
352 sh_video->fps=av_q2d(st->r_frame_rate);
353 sh_video->frametime=1/av_q2d(st->r_frame_rate);
354 sh_video->format = bih->biCompression;
355 sh_video->aspect= codec->width * codec->sample_aspect_ratio.num
356 / (float)(codec->height * codec->sample_aspect_ratio.den);
357 sh_video->i_bps= codec->bit_rate/8;
358 mp_msg(MSGT_DEMUX,MSGL_DBG2,"aspect= %d*%d/(%d*%d)\n",
359 codec->width, codec->sample_aspect_ratio.num,
360 codec->height, codec->sample_aspect_ratio.den);
362 sh_video->ds= demuxer->video;
363 if(codec->extradata_size)
364 memcpy(sh_video->bih + 1, codec->extradata, codec->extradata_size);
365 if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_video_header(sh_video->bih, MSGL_V);
366 /* short biPlanes;
367 int biXPelsPerMeter;
368 int biYPelsPerMeter;
369 int biClrUsed;
370 int biClrImportant;*/
371 if(demuxer->video->id != i && demuxer->video->id != -1)
372 st->discard= AVDISCARD_ALL;
373 else{
374 demuxer->video->id = i;
375 demuxer->video->sh= demuxer->v_streams[i];
377 break;}
378 default:
379 st->discard= AVDISCARD_ALL;
383 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: %d audio and %d video streams found\n",priv->audio_streams,priv->video_streams);
384 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: build %d\n", LIBAVFORMAT_BUILD);
385 if(!priv->audio_streams) demuxer->audio->id=-2; // nosound
386 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio;
387 if(!priv->video_streams){
388 if(!priv->audio_streams){
389 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF: no audio or video headers found - broken file?\n");
390 return NULL;
392 demuxer->video->id=-2; // audio-only
393 } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video;
395 return demuxer;
398 static int demux_lavf_fill_buffer(demuxer_t *demux, demux_stream_t *dsds){
399 lavf_priv_t *priv= demux->priv;
400 AVPacket pkt;
401 demux_packet_t *dp;
402 demux_stream_t *ds;
403 int id;
404 mp_msg(MSGT_DEMUX,MSGL_DBG2,"demux_lavf_fill_buffer()\n");
406 demux->filepos=stream_tell(demux->stream);
408 if(av_read_frame(priv->avfc, &pkt) < 0)
409 return 0;
411 id= pkt.stream_index;
413 if(id==demux->audio->id){
414 // audio
415 ds=demux->audio;
416 if(!ds->sh){
417 ds->sh=demux->a_streams[id];
418 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected LAVF audio ID = %d\n",ds->id);
420 } else if(id==demux->video->id){
421 // video
422 ds=demux->video;
423 if(!ds->sh){
424 ds->sh=demux->v_streams[id];
425 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected LAVF video ID = %d\n",ds->id);
427 } else {
428 av_free_packet(&pkt);
429 return 1;
432 if(0/*pkt.destruct == av_destruct_packet*/){
433 //ok kids, dont try this at home :)
434 dp=malloc(sizeof(demux_packet_t));
435 dp->len=pkt.size;
436 dp->next=NULL;
437 dp->refcount=1;
438 dp->master=NULL;
439 dp->buffer=pkt.data;
440 pkt.destruct= NULL;
441 }else{
442 dp=new_demux_packet(pkt.size);
443 memcpy(dp->buffer, pkt.data, pkt.size);
444 av_free_packet(&pkt);
447 if(pkt.pts != AV_NOPTS_VALUE){
448 dp->pts=pkt.pts * av_q2d(priv->avfc->streams[id]->time_base);
449 priv->last_pts= dp->pts * AV_TIME_BASE;
451 dp->pos=demux->filepos;
452 dp->flags= !!(pkt.flags&PKT_FLAG_KEY);
453 // append packet to DS stream:
454 ds_add_packet(ds,dp);
455 return 1;
458 static void demux_seek_lavf(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags){
459 lavf_priv_t *priv = demuxer->priv;
460 int avsflags = 0;
461 mp_msg(MSGT_DEMUX,MSGL_DBG2,"demux_seek_lavf(%p, %f, %f, %d)\n", demuxer, rel_seek_secs, audio_delay, flags);
463 if (flags & 1) // absolute seek
464 priv->last_pts = priv->avfc->start_time;
465 if (flags & 2) { // percent seek
466 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
467 return;
468 priv->last_pts += rel_seek_secs * priv->avfc->duration;
469 } else {
470 priv->last_pts += rel_seek_secs * AV_TIME_BASE;
471 if (rel_seek_secs < 0) avsflags = AVSEEK_FLAG_BACKWARD;
473 av_seek_frame(priv->avfc, -1, priv->last_pts, avsflags);
476 static int demux_lavf_control(demuxer_t *demuxer, int cmd, void *arg)
478 lavf_priv_t *priv = demuxer->priv;
480 switch (cmd) {
481 case DEMUXER_CTRL_GET_TIME_LENGTH:
482 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
483 return DEMUXER_CTRL_DONTKNOW;
485 *((double *)arg) = (double)priv->avfc->duration / AV_TIME_BASE;
486 return DEMUXER_CTRL_OK;
488 case DEMUXER_CTRL_GET_PERCENT_POS:
489 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
490 return DEMUXER_CTRL_DONTKNOW;
492 *((int *)arg) = (int)((priv->last_pts - priv->avfc->start_time)*100 / priv->avfc->duration);
493 return DEMUXER_CTRL_OK;
494 case DEMUXER_CTRL_SWITCH_AUDIO:
495 case DEMUXER_CTRL_SWITCH_VIDEO:
497 int id = *((int*)arg);
498 int newid = -2;
499 int i, curridx = -2;
500 int nstreams, *pstreams;
501 demux_stream_t *ds;
503 if(cmd == DEMUXER_CTRL_SWITCH_VIDEO)
505 ds = demuxer->video;
506 nstreams = priv->video_streams;
507 pstreams = priv->vstreams;
509 else
511 ds = demuxer->audio;
512 nstreams = priv->audio_streams;
513 pstreams = priv->astreams;
515 if(ds->id == -2)
516 return DEMUXER_CTRL_NOTIMPL;
517 for(i = 0; i < nstreams; i++)
519 if(pstreams[i] == ds->id) //current stream id
521 curridx = i;
522 break;
526 if(id < 0)
528 i = (curridx + 1) % nstreams;
529 newid = pstreams[i];
531 else
533 for(i = 0; i < nstreams; i++)
535 if(pstreams[i] == id)
537 newid = id;
538 break;
542 if(newid == -2 || i == curridx)
543 return DEMUXER_CTRL_NOTIMPL;
544 else
546 ds_free_packs(ds);
547 priv->avfc->streams[ds->id]->discard = AVDISCARD_ALL;
548 *((int*)arg) = ds->id = newid;
549 priv->avfc->streams[newid]->discard = AVDISCARD_NONE;
550 return DEMUXER_CTRL_OK;
553 default:
554 return DEMUXER_CTRL_NOTIMPL;
558 static void demux_close_lavf(demuxer_t *demuxer)
560 lavf_priv_t* priv = demuxer->priv;
561 if (priv){
562 if(priv->avfc)
564 av_close_input_file(priv->avfc); priv->avfc= NULL;
566 free(priv); demuxer->priv= NULL;
571 demuxer_desc_t demuxer_desc_lavf = {
572 "libavformat demuxer",
573 "lavf",
574 "libavformat",
575 "Michael Niedermayer",
576 "supports many formats, requires libavformat",
577 DEMUXER_TYPE_LAVF,
578 0, // Check after other demuxer
579 lavf_check_file,
580 demux_lavf_fill_buffer,
581 demux_open_lavf,
582 demux_close_lavf,
583 demux_seek_lavf,
584 demux_lavf_control