stream_ffmpeg, demux_lavf: Use flv demuxer for rtmp streams
[mplayer/glamo.git] / libmpdemux / demux_lavf.c
blob1222673d8749e5d91fa0efe88923b2c9a788d28e
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>
25 #include <stdbool.h>
27 #include "config.h"
28 #include "options.h"
29 #include "mp_msg.h"
30 #include "av_opts.h"
32 #include "stream/stream.h"
33 #include "aviprint.h"
34 #include "demuxer.h"
35 #include "stheader.h"
36 #include "m_option.h"
37 #include "libvo/sub.h"
39 #include "libavformat/avformat.h"
40 #include "libavformat/avio.h"
41 #include "libavutil/avutil.h"
42 #include "libavutil/avstring.h"
43 #include "libavcodec/opt.h"
45 #include "mp_taglists.h"
47 #define INITIAL_PROBE_SIZE (32*1024)
48 #define PROBE_BUF_SIZE (2*1024*1024)
50 const m_option_t lavfdopts_conf[] = {
51 OPT_INTRANGE("probesize", lavfdopts.probesize, 0, 32, INT_MAX),
52 OPT_STRING("format", lavfdopts.format, 0),
53 OPT_INTRANGE("analyzeduration", lavfdopts.analyzeduration, 0, 0, INT_MAX),
54 OPT_STRING("cryptokey", lavfdopts.cryptokey, 0),
55 OPT_STRING("o", lavfdopts.avopt, 0),
56 {NULL, NULL, 0, 0, 0, 0, NULL}
59 #define BIO_BUFFER_SIZE 32768
61 typedef struct lavf_priv_t{
62 AVInputFormat *avif;
63 AVFormatContext *avfc;
64 ByteIOContext *pb;
65 uint8_t buffer[BIO_BUFFER_SIZE];
66 int audio_streams;
67 int video_streams;
68 int sub_streams;
69 int64_t last_pts;
70 int astreams[MAX_A_STREAMS];
71 int vstreams[MAX_V_STREAMS];
72 int sstreams[MAX_S_STREAMS];
73 int cur_program;
74 }lavf_priv_t;
76 static int mp_read(void *opaque, uint8_t *buf, int size) {
77 stream_t *stream = opaque;
78 int ret;
80 if(stream_eof(stream)) //needed?
81 return -1;
82 ret=stream_read(stream, buf, size);
84 mp_msg(MSGT_HEADER,MSGL_DBG2,"%d=mp_read(%p, %p, %d), eof:%d\n", ret, stream, buf, size, stream->eof);
85 return ret;
88 static int64_t mp_seek(void *opaque, int64_t pos, int whence) {
89 stream_t *stream = opaque;
90 int64_t current_pos;
91 mp_msg(MSGT_HEADER,MSGL_DBG2,"mp_seek(%p, %"PRId64", %d)\n", stream, pos, whence);
92 if(whence == SEEK_CUR)
93 pos +=stream_tell(stream);
94 else if(whence == SEEK_END && stream->end_pos > 0)
95 pos += stream->end_pos;
96 else if(whence == SEEK_SET)
97 pos += stream->start_pos;
98 else if(whence == AVSEEK_SIZE && stream->end_pos > 0)
99 return stream->end_pos - stream->start_pos;
100 else
101 return -1;
103 if(pos<0)
104 return -1;
105 if(pos<stream->end_pos && stream->eof)
106 stream_reset(stream);
107 current_pos = stream_tell(stream);
108 if(stream_seek(stream, pos)==0) {
109 stream_reset(stream);
110 stream_seek(stream, current_pos);
111 return -1;
114 return pos - stream->start_pos;
117 static void list_formats(void) {
118 AVInputFormat *fmt;
119 mp_msg(MSGT_DEMUX, MSGL_INFO, "Available lavf input formats:\n");
120 for (fmt = first_iformat; fmt; fmt = fmt->next)
121 mp_msg(MSGT_DEMUX, MSGL_INFO, "%15s : %s\n", fmt->name, fmt->long_name);
124 static int lavf_check_file(demuxer_t *demuxer){
125 struct MPOpts *opts = demuxer->opts;
126 struct lavfdopts *lavfdopts = &opts->lavfdopts;
127 AVProbeData avpd;
128 lavf_priv_t *priv;
129 int probe_data_size = 0;
130 int read_size = INITIAL_PROBE_SIZE;
132 if(!demuxer->priv)
133 demuxer->priv=calloc(sizeof(lavf_priv_t),1);
134 priv= demuxer->priv;
136 av_register_all();
138 char *format = lavfdopts->format;
139 if (!format)
140 format = demuxer->stream->lavf_type;
141 if (format) {
142 if (strcmp(format, "help") == 0) {
143 list_formats();
144 return 0;
146 priv->avif = av_find_input_format(format);
147 if (!priv->avif) {
148 mp_msg(MSGT_DEMUX, MSGL_FATAL, "Unknown lavf format %s\n", format);
149 return 0;
151 mp_msg(MSGT_DEMUX,MSGL_INFO,"Forced lavf %s demuxer\n", priv->avif->long_name);
152 return DEMUXER_TYPE_LAVF;
155 avpd.buf = av_mallocz(FFMAX(BIO_BUFFER_SIZE, PROBE_BUF_SIZE) +
156 FF_INPUT_BUFFER_PADDING_SIZE);
157 do {
158 read_size = stream_read(demuxer->stream, avpd.buf + probe_data_size, read_size);
159 if(read_size < 0) {
160 av_free(avpd.buf);
161 return 0;
163 probe_data_size += read_size;
164 avpd.filename= demuxer->stream->url;
165 if (!strncmp(avpd.filename, "ffmpeg://", 9))
166 avpd.filename += 9;
167 avpd.buf_size= probe_data_size;
169 priv->avif= av_probe_input_format(&avpd, probe_data_size > 0);
170 read_size = FFMIN(2*read_size, PROBE_BUF_SIZE - probe_data_size);
171 } while (demuxer->desc->type != DEMUXER_TYPE_LAVF_PREFERRED &&
172 !priv->avif && read_size > 0 && probe_data_size < PROBE_BUF_SIZE);
173 av_free(avpd.buf);
175 if(!priv->avif){
176 mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: no clue about this gibberish!\n");
177 return 0;
178 }else
179 mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: %s\n", priv->avif->long_name);
181 return DEMUXER_TYPE_LAVF;
184 static const char * const preferred_list[] = {
185 "dxa",
186 "flv",
187 "gxf",
188 "nut",
189 "nuv",
190 "mov,mp4,m4a,3gp,3g2,mj2",
191 "mpc",
192 "mpc8",
193 "mxf",
194 "swf",
195 "vqf",
196 "w64",
197 "wv",
198 NULL
201 static int lavf_check_preferred_file(demuxer_t *demuxer){
202 if (lavf_check_file(demuxer)) {
203 const char * const *p = preferred_list;
204 lavf_priv_t *priv = demuxer->priv;
205 while (*p) {
206 if (strcmp(*p, priv->avif->name) == 0)
207 return DEMUXER_TYPE_LAVF_PREFERRED;
208 p++;
211 return 0;
214 static uint8_t char2int(char c) {
215 if (c >= '0' && c <= '9') return c - '0';
216 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
217 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
218 return 0;
221 static void parse_cryptokey(AVFormatContext *avfc, const char *str) {
222 int len = strlen(str) / 2;
223 uint8_t *key = av_mallocz(len);
224 int i;
225 avfc->keylen = len;
226 avfc->key = key;
227 for (i = 0; i < len; i++, str += 2)
228 *key++ = (char2int(str[0]) << 4) | char2int(str[1]);
231 static void handle_stream(demuxer_t *demuxer, AVFormatContext *avfc, int i) {
232 lavf_priv_t *priv= demuxer->priv;
233 AVStream *st= avfc->streams[i];
234 AVCodecContext *codec= st->codec;
235 AVMetadataTag *lang = av_metadata_get(st->metadata, "language", NULL, 0);
236 int g, override_tag = mp_av_codec_get_tag(mp_codecid_override_taglists,
237 codec->codec_id);
238 // For some formats (like PCM) always trust CODEC_ID_* more than codec_tag
239 if (override_tag)
240 codec->codec_tag = override_tag;
242 switch(codec->codec_type){
243 case CODEC_TYPE_AUDIO:{
244 WAVEFORMATEX *wf;
245 sh_audio_t* sh_audio;
246 sh_audio=new_sh_audio(demuxer, i);
247 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "[%s] Audio stream found, -aid %d\n", "lavf", i);
248 if(!sh_audio)
249 break;
250 priv->astreams[priv->audio_streams] = i;
251 priv->audio_streams++;
252 wf= calloc(sizeof(WAVEFORMATEX) + codec->extradata_size, 1);
253 // mp4a tag is used for all mp4 files no matter what they actually contain
254 if(codec->codec_tag == MKTAG('m', 'p', '4', 'a'))
255 codec->codec_tag= 0;
256 if(!codec->codec_tag)
257 codec->codec_tag= mp_av_codec_get_tag(mp_wav_taglists, codec->codec_id);
258 wf->wFormatTag= codec->codec_tag;
259 wf->nChannels= codec->channels;
260 wf->nSamplesPerSec= codec->sample_rate;
261 wf->nAvgBytesPerSec= codec->bit_rate/8;
262 wf->nBlockAlign= codec->block_align ? codec->block_align : 1;
263 wf->wBitsPerSample= codec->bits_per_coded_sample;
264 wf->cbSize= codec->extradata_size;
265 if(codec->extradata_size)
266 memcpy(wf + 1, codec->extradata, codec->extradata_size);
267 sh_audio->wf= wf;
268 sh_audio->audio.dwSampleSize= codec->block_align;
269 if(codec->frame_size && codec->sample_rate){
270 sh_audio->audio.dwScale=codec->frame_size;
271 sh_audio->audio.dwRate= codec->sample_rate;
272 }else{
273 sh_audio->audio.dwScale= codec->block_align ? codec->block_align*8 : 8;
274 sh_audio->audio.dwRate = codec->bit_rate;
276 g= av_gcd(sh_audio->audio.dwScale, sh_audio->audio.dwRate);
277 sh_audio->audio.dwScale /= g;
278 sh_audio->audio.dwRate /= g;
279 // 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);
280 sh_audio->ds= demuxer->audio;
281 sh_audio->format= codec->codec_tag;
282 sh_audio->channels= codec->channels;
283 sh_audio->samplerate= codec->sample_rate;
284 sh_audio->i_bps= codec->bit_rate/8;
285 switch (codec->codec_id) {
286 case CODEC_ID_PCM_S8:
287 case CODEC_ID_PCM_U8:
288 sh_audio->samplesize = 1;
289 break;
290 case CODEC_ID_PCM_S16LE:
291 case CODEC_ID_PCM_S16BE:
292 case CODEC_ID_PCM_U16LE:
293 case CODEC_ID_PCM_U16BE:
294 sh_audio->samplesize = 2;
295 break;
296 case CODEC_ID_PCM_ALAW:
297 sh_audio->format = 0x6;
298 break;
299 case CODEC_ID_PCM_MULAW:
300 sh_audio->format = 0x7;
301 break;
303 if (lang && lang->value) {
304 sh_audio->lang = strdup(lang->value);
305 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_LANG=%s\n", i, sh_audio->lang);
307 if (st->disposition & AV_DISPOSITION_DEFAULT)
308 sh_audio->default_track = 1;
309 if(mp_msg_test(MSGT_HEADER,MSGL_V) ) print_wave_header(sh_audio->wf, MSGL_V);
310 // select the first audio stream
311 if (!demuxer->audio->sh) {
312 demuxer->audio->id = i;
313 demuxer->audio->sh= demuxer->a_streams[i];
314 } else
315 st->discard= AVDISCARD_ALL;
316 break;
318 case CODEC_TYPE_VIDEO:{
319 sh_video_t* sh_video;
320 BITMAPINFOHEADER *bih;
321 sh_video=new_sh_video(demuxer, i);
322 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "[%s] Video stream found, -vid %d\n", "lavf", i);
323 if(!sh_video) break;
324 priv->vstreams[priv->video_streams] = i;
325 priv->video_streams++;
326 bih=calloc(sizeof(BITMAPINFOHEADER) + codec->extradata_size,1);
328 if(codec->codec_id == CODEC_ID_RAWVIDEO) {
329 switch (codec->pix_fmt) {
330 case PIX_FMT_RGB24:
331 codec->codec_tag= MKTAG(24, 'B', 'G', 'R');
334 if(!codec->codec_tag)
335 codec->codec_tag= mp_av_codec_get_tag(mp_bmp_taglists, 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_coded_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 if(st->sample_aspect_ratio.num)
356 sh_video->aspect = codec->width * st->sample_aspect_ratio.num
357 / (float)(codec->height * st->sample_aspect_ratio.den);
358 else
359 sh_video->aspect=codec->width * codec->sample_aspect_ratio.num
360 / (float)(codec->height * codec->sample_aspect_ratio.den);
361 sh_video->i_bps=codec->bit_rate/8;
362 mp_msg(MSGT_DEMUX,MSGL_DBG2,"aspect= %d*%d/(%d*%d)\n",
363 codec->width, codec->sample_aspect_ratio.num,
364 codec->height, codec->sample_aspect_ratio.den);
366 sh_video->ds= demuxer->video;
367 if(codec->extradata_size)
368 memcpy(sh_video->bih + 1, codec->extradata, codec->extradata_size);
369 if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_video_header(sh_video->bih, MSGL_V);
371 short biPlanes;
372 int biXPelsPerMeter;
373 int biYPelsPerMeter;
374 int biClrUsed;
375 int biClrImportant;
377 if(demuxer->video->id != i && demuxer->video->id != -1)
378 st->discard= AVDISCARD_ALL;
379 else{
380 demuxer->video->id = i;
381 demuxer->video->sh= demuxer->v_streams[i];
383 break;
385 case CODEC_TYPE_SUBTITLE:{
386 sh_sub_t* sh_sub;
387 char type;
388 /* only support text subtitles for now */
389 if(codec->codec_id == CODEC_ID_TEXT)
390 type = 't';
391 else if(codec->codec_id == CODEC_ID_MOV_TEXT)
392 type = 'm';
393 else if(codec->codec_id == CODEC_ID_SSA)
394 type = 'a';
395 else if(codec->codec_id == CODEC_ID_DVD_SUBTITLE)
396 type = 'v';
397 else if(codec->codec_id == CODEC_ID_DVB_TELETEXT)
398 type = 'd';
399 else
400 break;
401 sh_sub = new_sh_sub_sid(demuxer, i, priv->sub_streams);
402 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "[%s] Subtitle stream found, -sid %d\n", "lavf", priv->sub_streams);
403 if(!sh_sub) break;
404 priv->sstreams[priv->sub_streams] = i;
405 sh_sub->type = type;
406 if (codec->extradata_size) {
407 sh_sub->extradata = malloc(codec->extradata_size);
408 memcpy(sh_sub->extradata, codec->extradata, codec->extradata_size);
409 sh_sub->extradata_len = codec->extradata_size;
411 if (lang && lang->value) {
412 sh_sub->lang = strdup(lang->value);
413 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_LANG=%s\n", priv->sub_streams, sh_sub->lang);
415 if (st->disposition & AV_DISPOSITION_DEFAULT)
416 sh_sub->default_track = 1;
417 priv->sub_streams++;
418 break;
420 case CODEC_TYPE_ATTACHMENT:{
421 if (st->codec->codec_id == CODEC_ID_TTF)
422 demuxer_add_attachment(demuxer, st->filename, INT_MAX,
423 "application/x-truetype-font", INT_MAX,
424 codec->extradata, codec->extradata_size);
425 break;
427 default:
428 st->discard= AVDISCARD_ALL;
432 static demuxer_t* demux_open_lavf(demuxer_t *demuxer){
433 struct MPOpts *opts = demuxer->opts;
434 struct lavfdopts *lavfdopts = &opts->lavfdopts;
435 AVFormatContext *avfc;
436 AVFormatParameters ap;
437 const AVOption *opt;
438 AVMetadataTag *t = NULL;
439 lavf_priv_t *priv= demuxer->priv;
440 int i;
441 char mp_filename[256]="mp:";
443 memset(&ap, 0, sizeof(AVFormatParameters));
445 stream_seek(demuxer->stream, 0);
447 avfc = avformat_alloc_context();
449 if (lavfdopts->cryptokey)
450 parse_cryptokey(avfc, lavfdopts->cryptokey);
451 if (opts->user_correct_pts != 0)
452 avfc->flags |= AVFMT_FLAG_GENPTS;
453 if (index_mode == 0)
454 avfc->flags |= AVFMT_FLAG_IGNIDX;
456 ap.prealloced_context = 1;
457 if (lavfdopts->probesize) {
458 opt = av_set_int(avfc, "probesize", lavfdopts->probesize);
459 if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option probesize to %u\n", lavfdopts->probesize);
461 if (lavfdopts->analyzeduration) {
462 opt = av_set_int(avfc, "analyzeduration",
463 lavfdopts->analyzeduration * AV_TIME_BASE);
464 if (!opt)
465 mp_msg(MSGT_HEADER, MSGL_ERR, "demux_lavf, couldn't set option "
466 "analyzeduration to %u\n", lavfdopts->analyzeduration);
469 if (lavfdopts->avopt){
470 if(parse_avopts(avfc, lavfdopts->avopt) < 0){
471 mp_msg(MSGT_HEADER,MSGL_ERR, "Your options /%s/ look like gibberish to me pal\n", lavfdopts->avopt);
472 return NULL;
476 if(demuxer->stream->url) {
477 if (!strncmp(demuxer->stream->url, "ffmpeg://rtsp:", 14))
478 av_strlcpy(mp_filename, demuxer->stream->url + 9, sizeof(mp_filename));
479 else
480 av_strlcat(mp_filename, demuxer->stream->url, sizeof(mp_filename));
481 } else
482 av_strlcat(mp_filename, "foobar.dummy", sizeof(mp_filename));
484 priv->pb = av_alloc_put_byte(priv->buffer, BIO_BUFFER_SIZE, 0,
485 demuxer->stream, mp_read, NULL, mp_seek);
486 priv->pb->is_streamed = !demuxer->stream->end_pos || (demuxer->stream->flags & MP_STREAM_SEEK) != MP_STREAM_SEEK;
488 if(av_open_input_stream(&avfc, priv->pb, mp_filename, priv->avif, &ap)<0){
489 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_open_input_stream() failed\n");
490 return NULL;
493 priv->avfc= avfc;
495 if(av_find_stream_info(avfc) < 0){
496 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_find_stream_info() failed\n");
497 return NULL;
500 /* Add metadata. */
501 av_metadata_conv(avfc, NULL, avfc->iformat->metadata_conv);
502 while((t = av_metadata_get(avfc->metadata, "", t, AV_METADATA_IGNORE_SUFFIX)))
503 demux_info_add(demuxer, t->key, t->value);
505 for(i=0; i < avfc->nb_chapters; i++) {
506 AVChapter *c = avfc->chapters[i];
507 uint64_t start = av_rescale_q(c->start, c->time_base, (AVRational){1,1000});
508 uint64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,1000});
509 t = av_metadata_get(c->metadata, "title", NULL, 0);
510 demuxer_add_chapter(demuxer, t ? t->value : NULL, INT_MAX, start, end);
513 for(i=0; i<avfc->nb_streams; i++)
514 handle_stream(demuxer, avfc, i);
515 if(avfc->nb_programs) {
516 int p;
517 for (p = 0; p < avfc->nb_programs; p++) {
518 AVProgram *program = avfc->programs[p];
519 t = av_metadata_get(program->metadata, "title", NULL, 0);
520 mp_msg(MSGT_HEADER,MSGL_INFO,"LAVF: Program %d %s\n", program->id, t ? t->value : "");
524 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: %d audio and %d video streams found\n",priv->audio_streams,priv->video_streams);
525 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: build %d\n", LIBAVFORMAT_BUILD);
526 if(!priv->audio_streams) demuxer->audio->id=-2; // nosound
527 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio;
528 if(!priv->video_streams){
529 if(!priv->audio_streams){
530 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF: no audio or video headers found - broken file?\n");
531 return NULL;
533 demuxer->video->id=-2; // audio-only
534 } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video;
536 demuxer->accurate_seek = true;
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 = 0;
614 } else {
615 if (rel_seek_secs < 0) avsflags = AVSEEK_FLAG_BACKWARD;
617 if (flags & SEEK_FORWARD)
618 avsflags = 0;
619 else if (flags & SEEK_BACKWARD)
620 avsflags = AVSEEK_FLAG_BACKWARD;
621 if (flags & SEEK_FACTOR) {
622 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
623 return;
624 priv->last_pts += rel_seek_secs * priv->avfc->duration;
625 } else {
626 priv->last_pts += rel_seek_secs * AV_TIME_BASE;
628 if (av_seek_frame(priv->avfc, -1, priv->last_pts, avsflags) < 0) {
629 avsflags ^= AVSEEK_FLAG_BACKWARD;
630 av_seek_frame(priv->avfc, -1, priv->last_pts, avsflags);
634 static int demux_lavf_control(demuxer_t *demuxer, int cmd, void *arg)
636 lavf_priv_t *priv = demuxer->priv;
638 switch (cmd) {
639 case DEMUXER_CTRL_CORRECT_PTS:
640 return DEMUXER_CTRL_OK;
641 case DEMUXER_CTRL_GET_TIME_LENGTH:
642 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
643 return DEMUXER_CTRL_DONTKNOW;
645 *((double *)arg) = (double)priv->avfc->duration / AV_TIME_BASE;
646 return DEMUXER_CTRL_OK;
648 case DEMUXER_CTRL_GET_PERCENT_POS:
649 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
650 return DEMUXER_CTRL_DONTKNOW;
652 *((int *)arg) = (int)((priv->last_pts - priv->avfc->start_time)*100 / priv->avfc->duration);
653 return DEMUXER_CTRL_OK;
654 case DEMUXER_CTRL_SWITCH_AUDIO:
655 case DEMUXER_CTRL_SWITCH_VIDEO:
657 int id = *((int*)arg);
658 int newid = -2;
659 int i, curridx = -1;
660 int nstreams, *pstreams;
661 demux_stream_t *ds;
663 if(cmd == DEMUXER_CTRL_SWITCH_VIDEO)
665 ds = demuxer->video;
666 nstreams = priv->video_streams;
667 pstreams = priv->vstreams;
669 else
671 ds = demuxer->audio;
672 nstreams = priv->audio_streams;
673 pstreams = priv->astreams;
675 for(i = 0; i < nstreams; i++)
677 if(pstreams[i] == ds->id) //current stream id
679 curridx = i;
680 break;
684 if(id == -2) { // no sound
685 i = -1;
686 } else if(id == -1) { // next track
687 i = (curridx + 2) % (nstreams + 1) - 1;
688 if (i >= 0)
689 newid = pstreams[i];
691 else // select track by id
693 for(i = 0; i < nstreams; i++)
695 if(pstreams[i] == id)
697 newid = id;
698 break;
702 if(i == curridx)
703 return DEMUXER_CTRL_NOTIMPL;
704 else
706 ds_free_packs(ds);
707 if(ds->id >= 0)
708 priv->avfc->streams[ds->id]->discard = AVDISCARD_ALL;
709 *((int*)arg) = ds->id = newid;
710 if(newid >= 0)
711 priv->avfc->streams[newid]->discard = AVDISCARD_NONE;
712 return DEMUXER_CTRL_OK;
715 case DEMUXER_CTRL_IDENTIFY_PROGRAM:
717 demux_program_t *prog = arg;
718 AVProgram *program;
719 int p, i;
720 int start;
722 prog->vid = prog->aid = prog->sid = -2; //no audio and no video by default
723 if(priv->avfc->nb_programs < 1)
724 return DEMUXER_CTRL_DONTKNOW;
726 if(prog->progid == -1)
728 p = 0;
729 while(p<priv->avfc->nb_programs && priv->avfc->programs[p]->id != priv->cur_program)
730 p++;
731 p = (p + 1) % priv->avfc->nb_programs;
733 else
735 for(i=0; i<priv->avfc->nb_programs; i++)
736 if(priv->avfc->programs[i]->id == prog->progid)
737 break;
738 if(i==priv->avfc->nb_programs)
739 return DEMUXER_CTRL_DONTKNOW;
740 p = i;
742 start = p;
743 redo:
744 program = priv->avfc->programs[p];
745 for(i=0; i<program->nb_stream_indexes; i++)
747 switch(priv->avfc->streams[program->stream_index[i]]->codec->codec_type)
749 case CODEC_TYPE_VIDEO:
750 if(prog->vid == -2)
751 prog->vid = program->stream_index[i];
752 break;
753 case CODEC_TYPE_AUDIO:
754 if(prog->aid == -2)
755 prog->aid = program->stream_index[i];
756 break;
757 case CODEC_TYPE_SUBTITLE:
758 if(prog->sid == -2 && priv->avfc->streams[program->stream_index[i]]->codec->codec_id == CODEC_ID_TEXT)
759 prog->sid = program->stream_index[i];
760 break;
763 if(prog->progid == -1 && prog->vid == -2 && prog->aid == -2)
765 p = (p + 1) % priv->avfc->nb_programs;
766 if (p == start)
767 return DEMUXER_CTRL_DONTKNOW;
768 goto redo;
770 priv->cur_program = prog->progid = program->id;
771 return DEMUXER_CTRL_OK;
773 default:
774 return DEMUXER_CTRL_NOTIMPL;
778 static void demux_close_lavf(demuxer_t *demuxer)
780 lavf_priv_t* priv = demuxer->priv;
781 if (priv){
782 if(priv->avfc)
784 av_freep(&priv->avfc->key);
785 av_close_input_stream(priv->avfc);
787 av_freep(&priv->pb);
788 free(priv); demuxer->priv= NULL;
793 const demuxer_desc_t demuxer_desc_lavf = {
794 "libavformat demuxer",
795 "lavf",
796 "libavformat",
797 "Michael Niedermayer",
798 "supports many formats, requires libavformat",
799 DEMUXER_TYPE_LAVF,
800 0, // Check after other demuxer
801 lavf_check_file,
802 demux_lavf_fill_buffer,
803 demux_open_lavf,
804 demux_close_lavf,
805 demux_seek_lavf,
806 demux_lavf_control
809 const demuxer_desc_t demuxer_desc_lavf_preferred = {
810 "libavformat preferred demuxer",
811 "lavfpref",
812 "libavformat",
813 "Michael Niedermayer",
814 "supports many formats, requires libavformat",
815 DEMUXER_TYPE_LAVF_PREFERRED,
817 lavf_check_preferred_file,
818 demux_lavf_fill_buffer,
819 demux_open_lavf,
820 demux_close_lavf,
821 demux_seek_lavf,
822 demux_lavf_control