1 /*
2 * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of MPlayer.
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 // #include <stdio.h>
22 #include <stdlib.h>
23 // #include <unistd.h>
24 #include <limits.h>
25
26 #include "config.h"
27 #include "mp_msg.h"
28 #include "help_mp.h"
29 #include "av_opts.h"
30 #include "av_helpers.h"
31
32 #include "stream/stream.h"
33 #include "aviprint.h"
34 #include "demuxer.h"
35 #include "stheader.h"
36 #include "m_option.h"
37 #include "sub/sub.h"
38
39 #include "libavformat/avformat.h"
40 #include "libavformat/avio.h"
41 #include "libavutil/avutil.h"
42 #include "libavutil/avstring.h"
43 #include "libavutil/mathematics.h"
44 #include "libavutil/opt.h"
45 #include "libavutil/replaygain.h"
46
47 #include "mp_taglists.h"
48
49 #define INITIAL_PROBE_SIZE STREAM_BUFFER_MIN
50 #define SMALL_MAX_PROBE_SIZE (32 * 1024)
51 #define PROBE_BUF_SIZE (2*1024*1024)
52
53 static unsigned int opt_probesize = 0;
54 static unsigned int opt_analyzeduration = 0;
55 static char *opt_format;
56 static char *opt_cryptokey;
57 static char *opt_avopt = NULL;
58
59 const m_option_t lavfdopts_conf[] = {
60 {"probesize", &(opt_probesize), CONF_TYPE_INT, CONF_RANGE, 32, INT_MAX, NULL},
61 {"format", &(opt_format), CONF_TYPE_STRING, 0, 0, 0, NULL},
62 {"analyzeduration", &(opt_analyzeduration), CONF_TYPE_INT, CONF_RANGE, 0, INT_MAX, NULL},
63 {"cryptokey", &(opt_cryptokey), CONF_TYPE_STRING, 0, 0, 0, NULL},
64 {"o", &opt_avopt, CONF_TYPE_STRING, 0, 0, 0, NULL},
65 {NULL, NULL, 0, 0, 0, 0, NULL}
66 };
67
68 #define BIO_BUFFER_SIZE 32768
69
70 typedef struct lavf_priv {
71 const AVInputFormat *avif;
72 AVFormatContext *avfc;
73 AVIOContext *pb;
74 int audio_streams;
75 int video_streams;
76 int sub_streams;
77 int64_t last_pts;
78 int astreams[MAX_A_STREAMS];
79 int vstreams[MAX_V_STREAMS];
80 int sstreams[MAX_S_STREAMS];
81 int cur_program;
82 int nb_streams_last;
83 int use_lavf_netstream;
84 int r_gain;
85 }lavf_priv_t;
86
mp_read(void * opaque,uint8_t * buf,int size)87 static int mp_read(void *opaque, uint8_t *buf, int size) {
88 demuxer_t *demuxer = opaque;
89 stream_t *stream = demuxer->stream;
90 int ret;
91
92 ret=stream_read(stream, buf, size);
93 if (!ret && stream->eof)
94 ret = AVERROR_EOF;
95
96 mp_msg(MSGT_HEADER,MSGL_DBG2,"%d=mp_read(%p, %p, %d), pos: %"PRId64", eof:%d\n",
97 ret, stream, buf, size, stream_tell(stream), stream->eof);
98 return ret;
99 }
100
mp_seek(void * opaque,int64_t pos,int whence)101 static int64_t mp_seek(void *opaque, int64_t pos, int whence) {
102 demuxer_t *demuxer = opaque;
103 stream_t *stream = demuxer->stream;
104 int64_t current_pos;
105 mp_msg(MSGT_HEADER,MSGL_DBG2,"mp_seek(%p, %"PRId64", %d)\n", stream, pos, whence);
106 if(whence == SEEK_CUR)
107 pos +=stream_tell(stream);
108 else if(whence == SEEK_END && stream->end_pos > 0)
109 pos += stream->end_pos;
110 else if(whence == SEEK_SET)
111 pos += stream->start_pos;
112 else if(whence == AVSEEK_SIZE && stream->end_pos > 0) {
113 uint64_t size = 0;
114 stream_control(stream, STREAM_CTRL_GET_SIZE, &size);
115 if (size > stream->end_pos)
116 stream->end_pos = size;
117 return stream->end_pos - stream->start_pos;
118 } else
119 return -1;
120
121 if(pos<0)
122 return -1;
123 current_pos = stream_tell(stream);
124 if(stream_seek(stream, pos)==0) {
125 stream_reset(stream);
126 stream_seek(stream, current_pos);
127 return -1;
128 }
129
130 return pos - stream->start_pos;
131 }
132
mp_read_seek(void * opaque,int stream_idx,int64_t ts,int flags)133 static int64_t mp_read_seek(void *opaque, int stream_idx, int64_t ts, int flags) {
134 demuxer_t *demuxer = opaque;
135 stream_t *stream = demuxer->stream;
136 lavf_priv_t *priv = demuxer->priv;
137 AVStream *st = priv->avfc->streams[stream_idx];
138 int ret;
139 double pts;
140
141 pts = (double)ts * st->time_base.num / st->time_base.den;
142 ret = stream_control(stream, STREAM_CTRL_SEEK_TO_TIME, &pts);
143 if (ret < 0)
144 ret = AVERROR(ENOSYS);
145 return ret;
146 }
147
list_formats(void)148 static void list_formats(void) {
149 void *i = 0;
150 const AVInputFormat *fmt;
151 mp_msg(MSGT_DEMUX, MSGL_INFO, "Available lavf input formats:\n");
152 while ((fmt = av_demuxer_iterate(&i)))
153 mp_msg(MSGT_DEMUX, MSGL_INFO, "%15s : %s\n", fmt->name, fmt->long_name);
154 }
155
lavf_check_file(demuxer_t * demuxer)156 static int lavf_check_file(demuxer_t *demuxer){
157 AVProbeData avpd = { 0 };
158 lavf_priv_t *priv;
159 int probe_data_size = 0;
160 int read_size = INITIAL_PROBE_SIZE;
161 int score;
162
163 if(!demuxer->priv) {
164 demuxer->priv=calloc(sizeof(lavf_priv_t),1);
165 ((lavf_priv_t *)demuxer->priv)->r_gain = INT32_MIN;
166 }
167 priv= demuxer->priv;
168
169 init_avformat();
170
171 if (opt_format) {
172 if (strcmp(opt_format, "help") == 0) {
173 list_formats();
174 return 0;
175 }
176 priv->avif= av_find_input_format(opt_format);
177 if (!priv->avif) {
178 mp_msg(MSGT_DEMUX,MSGL_FATAL,"Unknown lavf format %s\n", opt_format);
179 return 0;
180 }
181 mp_msg(MSGT_DEMUX,MSGL_INFO,"Forced lavf %s demuxer\n", priv->avif->long_name);
182 return DEMUXER_TYPE_LAVF;
183 }
184
185 avpd.buf = av_mallocz(FFMAX(BIO_BUFFER_SIZE, PROBE_BUF_SIZE) +
186 AV_INPUT_BUFFER_PADDING_SIZE);
187 do {
188 read_size = stream_read(demuxer->stream, avpd.buf + probe_data_size, read_size);
189 if(read_size < 0) {
190 av_free(avpd.buf);
191 return 0;
192 }
193 probe_data_size += read_size;
194 avpd.filename= demuxer->stream->url;
195 if (!avpd.filename) {
196 mp_msg(MSGT_DEMUX, MSGL_WARN, "Stream url is not set!\n");
197 avpd.filename = "";
198 }
199 if (!strncmp(avpd.filename, "ffmpeg://", 9))
200 avpd.filename += 9;
201 avpd.buf_size= probe_data_size;
202
203 score = 0;
204 priv->avif= av_probe_input_format2(&avpd, probe_data_size > 0, &score);
205 read_size = FFMIN(2*read_size, PROBE_BUF_SIZE - probe_data_size);
206 } while ((demuxer->desc->type != DEMUXER_TYPE_LAVF_PREFERRED ||
207 probe_data_size < SMALL_MAX_PROBE_SIZE) &&
208 score <= AVPROBE_SCORE_MAX / 4 &&
209 read_size > 0 && probe_data_size < PROBE_BUF_SIZE);
210 av_free(avpd.buf);
211
212 if(!priv->avif){
213 mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: no clue about this gibberish!\n");
214 return 0;
215 }else{
216 mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: %s\n", priv->avif->long_name);
217 if (!strcmp(priv->avif->name, "hls,applehttp")) {
218 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: network streaming with lavf\n");
219 avformat_network_init();
220 priv->use_lavf_netstream = 1;
221 }
222 }
223
224 return DEMUXER_TYPE_LAVF;
225 }
226
227 /* Before adding anything to this list please stop and consider why.
228 * There are two good reasons
229 * 1) to reduce startup time when streaming these file types
230 * 2) workarounds around bugs in our native demuxers that are not reasonable to
231 * fix
232 * For the case 2) that means the issue should be understood well
233 * enough to be able to decide that a fix is not reasonable.
234 */
235 static const char * const preferred_list[] = {
236 "cdxl",
237 "dxa",
238 "flv",
239 "flac",
240 "gxf",
241 "nut",
242 "nuv",
243 "matroska,webm",
244 "mov,mp4,m4a,3gp,3g2,mj2",
245 "mpc",
246 "mpc8",
247 "mxf",
248 "ogg",
249 "pva",
250 "qcp",
251 "swf",
252 "vqf",
253 "w64",
254 "wv",
255 "yuv4mpegpipe",
256 NULL
257 };
258
lavf_check_preferred_file(demuxer_t * demuxer)259 static int lavf_check_preferred_file(demuxer_t *demuxer){
260 if (lavf_check_file(demuxer)) {
261 const char * const *p = preferred_list;
262 lavf_priv_t *priv = demuxer->priv;
263 while (*p) {
264 if (strcmp(*p, priv->avif->name) == 0)
265 return DEMUXER_TYPE_LAVF_PREFERRED;
266 p++;
267 }
268 }
269 return 0;
270 }
271
char2int(char c)272 static uint8_t char2int(char c) {
273 if (c >= '0' && c <= '9') return c - '0';
274 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
275 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
276 return 0;
277 }
278
parse_cryptokey(AVFormatContext * avfc,const char * str)279 static void parse_cryptokey(AVFormatContext *avfc, const char *str) {
280 int len = strlen(str) / 2;
281 uint8_t *key = av_mallocz(len);
282 int i;
283 avfc->keylen = len;
284 avfc->key = key;
285 for (i = 0; i < len; i++, str += 2)
286 *key++ = (char2int(str[0]) << 4) | char2int(str[1]);
287 }
288
handle_stream(demuxer_t * demuxer,AVFormatContext * avfc,int i)289 static void handle_stream(demuxer_t *demuxer, AVFormatContext *avfc, int i) {
290 lavf_priv_t *priv= demuxer->priv;
291 AVStream *st= avfc->streams[i];
292 AVCodecParameters *codec= st->codecpar;
293 char *stream_type = NULL;
294 int stream_id;
295 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
296 AVDictionaryEntry *title= av_dict_get(st->metadata, "title", NULL, 0);
297 AVDictionaryEntry *rot = av_dict_get(st->metadata, "rotate", NULL, 0);
298 int g;
299
300 switch(codec->codec_type){
301 case AVMEDIA_TYPE_AUDIO:{
302 WAVEFORMATEX *wf;
303 sh_audio_t* sh_audio;
304 sh_audio = new_sh_audio_aid(demuxer, i, priv->audio_streams, lang ? lang->value : NULL);
305 if(!sh_audio)
306 break;
307 stream_type = "audio";
308 priv->astreams[priv->audio_streams] = i;
309 wf= calloc(sizeof(*wf) + codec->extradata_size, 1);
310 codec->codec_tag = mp_codec_id2tag(codec->codec_id, codec->codec_tag, 1);
311 wf->wFormatTag= codec->codec_tag;
312 wf->nChannels= codec->channels;
313 sh_audio->channel_layout = codec->channel_layout;
314 wf->nSamplesPerSec= codec->sample_rate;
315 wf->nAvgBytesPerSec= codec->bit_rate/8;
316 wf->nBlockAlign= codec->block_align ? codec->block_align : 1;
317 wf->wBitsPerSample= codec->bits_per_coded_sample;
318 wf->cbSize= codec->extradata_size;
319 if(codec->extradata_size)
320 memcpy(wf + 1, codec->extradata, codec->extradata_size);
321 sh_audio->wf= wf;
322 sh_audio->audio.dwSampleSize= codec->block_align;
323 if(codec->frame_size && codec->sample_rate){
324 sh_audio->audio.dwScale=codec->frame_size;
325 sh_audio->audio.dwRate= codec->sample_rate;
326 }else{
327 sh_audio->audio.dwScale= codec->block_align ? codec->block_align*8 : 8;
328 sh_audio->audio.dwRate = codec->bit_rate;
329 }
330 g= av_gcd(sh_audio->audio.dwScale, sh_audio->audio.dwRate);
331 sh_audio->audio.dwScale /= g;
332 sh_audio->audio.dwRate /= g;
333 // 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);
334 sh_audio->format= codec->codec_tag;
335 sh_audio->channels= codec->channels;
336 sh_audio->samplerate= codec->sample_rate;
337 sh_audio->i_bps= codec->bit_rate/8;
338 switch (codec->codec_id) {
339 case AV_CODEC_ID_PCM_S8:
340 case AV_CODEC_ID_PCM_U8:
341 sh_audio->samplesize = 1;
342 break;
343 case AV_CODEC_ID_PCM_S16LE:
344 case AV_CODEC_ID_PCM_S16BE:
345 case AV_CODEC_ID_PCM_U16LE:
346 case AV_CODEC_ID_PCM_U16BE:
347 sh_audio->samplesize = 2;
348 break;
349 case AV_CODEC_ID_PCM_ALAW:
350 sh_audio->format = 0x6;
351 break;
352 case AV_CODEC_ID_PCM_MULAW:
353 sh_audio->format = 0x7;
354 break;
355 }
356 if (title && title->value)
357 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_NAME=%s\n", priv->audio_streams, title->value);
358 if (st->disposition & AV_DISPOSITION_DEFAULT)
359 sh_audio->default_track = 1;
360 if(mp_msg_test(MSGT_HEADER,MSGL_V) ) print_wave_header(sh_audio->wf, MSGL_V);
361 // select the first audio stream if auto-selection is requested
362 if (demuxer->audio->id == -1) {
363 demuxer->audio->id = i;
364 demuxer->audio->sh= demuxer->a_streams[i];
365 }
366 if (demuxer->audio->id != i)
367 st->discard= AVDISCARD_ALL;
368 if (priv->audio_streams == 0) {
369 size_t rg_size;
370 AVReplayGain *rg = (AVReplayGain*)av_stream_get_side_data(st, AV_PKT_DATA_REPLAYGAIN, &rg_size);
371 if (rg && rg_size >= sizeof(*rg)) {
372 priv->r_gain = rg->track_gain / 10000;
373 }
374 } else
375 priv->r_gain = INT32_MIN;
376 stream_id = priv->audio_streams++;
377 break;
378 }
379 case AVMEDIA_TYPE_VIDEO:{
380 sh_video_t* sh_video;
381 BITMAPINFOHEADER *bih;
382 sh_video=new_sh_video_vid(demuxer, i, priv->video_streams);
383 if(!sh_video) break;
384 stream_type = "video";
385 priv->vstreams[priv->video_streams] = i;
386 if (codec->extradata_size >= 9 &&
387 !memcmp(codec->extradata + codec->extradata_size - 9, "BottomUp", 9))
388 {
389 codec->extradata_size -= 9;
390 sh_video->flipped_input ^= 1;
391 }
392 // always reserve space for palette
393 sh_video->bih_size = sizeof(*bih) + codec->extradata_size + 1024;
394 bih=calloc(sh_video->bih_size,1);
395
396 if (codec->codec_id == AV_CODEC_ID_RAWVIDEO) {
397 if (codec->bits_per_coded_sample && codec->bits_per_coded_sample > 0 &&
398 codec->codec_tag == MKTAG('r', 'a', 'w', 32))
399 codec->codec_tag = 0;
400 switch (codec->format) {
401 case AV_PIX_FMT_RGB24:
402 codec->codec_tag= MKTAG(24, 'B', 'G', 'R');
403 break;
404 case AV_PIX_FMT_BGR24:
405 codec->codec_tag= MKTAG(24, 'R', 'G', 'B');
406 break;
407 }
408 }
409 codec->codec_tag = mp_codec_id2tag(codec->codec_id, codec->codec_tag, 0);
410 bih->biSize= sizeof(*bih) + codec->extradata_size;
411 bih->biWidth= codec->width;
412 bih->biHeight= codec->height;
413 bih->biPlanes= 1;
414 bih->biBitCount= codec->bits_per_coded_sample;
415 bih->biSizeImage = bih->biWidth * bih->biHeight * bih->biBitCount/8;
416 bih->biCompression= codec->codec_tag;
417 sh_video->bih= bih;
418 sh_video->disp_w= codec->width;
419 sh_video->disp_h= codec->height;
420 if (st->time_base.den) { /* if container has time_base, use that */
421 sh_video->video.dwRate= st->time_base.den;
422 sh_video->video.dwScale= st->time_base.num;
423 #if 0
424 } else {
425 // not available in latest FFmpeg API, ok to just remove?
426 sh_video->video.dwRate= st->codec->time_base.den;
427 sh_video->video.dwScale= st->codec->time_base.num;
428 #endif
429 }
430 sh_video->fps=av_q2d(st->r_frame_rate);
431 sh_video->frametime=1/av_q2d(st->r_frame_rate);
432 sh_video->format=bih->biCompression;
433 if(st->sample_aspect_ratio.num)
434 sh_video->original_aspect = codec->width * st->sample_aspect_ratio.num
435 / (float)(codec->height * st->sample_aspect_ratio.den);
436 else
437 sh_video->original_aspect = codec->width * codec->sample_aspect_ratio.num
438 / (float)(codec->height * codec->sample_aspect_ratio.den);
439 sh_video->i_bps=codec->bit_rate/8;
440 if (title && title->value)
441 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VID_%d_NAME=%s\n", priv->video_streams, title->value);
442 if (st->disposition & AV_DISPOSITION_DEFAULT)
443 sh_video->default_track = 1;
444 if (rot && rot->value)
445 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VID_%d_ROTATE=%s\n", priv->video_streams, rot->value);
446 mp_msg(MSGT_DEMUX,MSGL_DBG2,"stream aspect= %d*%d/(%d*%d)\n",
447 codec->width, st->sample_aspect_ratio.num,
448 codec->height, st->sample_aspect_ratio.den);
449 mp_msg(MSGT_DEMUX,MSGL_DBG2,"codec aspect= %d*%d/(%d*%d)\n",
450 codec->width, codec->sample_aspect_ratio.num,
451 codec->height, codec->sample_aspect_ratio.den);
452
453 if(codec->extradata_size)
454 memcpy(sh_video->bih + 1, codec->extradata, codec->extradata_size);
455 if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_video_header(sh_video->bih, MSGL_V);
456 /*
457 short biPlanes;
458 int biXPelsPerMeter;
459 int biYPelsPerMeter;
460 int biClrUsed;
461 int biClrImportant;
462 */
463 // select the first video stream if auto-selection is requested
464 if(demuxer->video->id == -1) {
465 demuxer->video->id = i;
466 demuxer->video->sh= demuxer->v_streams[i];
467 }
468 if(demuxer->video->id != i)
469 st->discard= AVDISCARD_ALL;
470 stream_id = priv->video_streams++;
471 break;
472 }
473 case AVMEDIA_TYPE_SUBTITLE:{
474 sh_sub_t* sh_sub;
475 char type;
476 if (codec->codec_id == AV_CODEC_ID_TEXT
477 || codec->codec_id == AV_CODEC_ID_SUBRIP
478 )
479 type = 't';
480 else if (codec->codec_id == AV_CODEC_ID_MOV_TEXT)
481 type = 'm';
482 else if (codec->codec_id == AV_CODEC_ID_SSA
483 || codec->codec_id == AV_CODEC_ID_ASS
484 )
485 type = 'a';
486 else if (codec->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
487 type = 'v';
488 else if (codec->codec_id == AV_CODEC_ID_XSUB)
489 type = 'x';
490 else if (codec->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
491 type = 'b';
492 else if (codec->codec_id == AV_CODEC_ID_DVB_TELETEXT)
493 type = 'd';
494 else if (codec->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE)
495 type = 'p';
496 else if (codec->codec_id == AV_CODEC_ID_EIA_608)
497 type = 'c';
498 else if(codec->codec_tag == MKTAG('c', '6', '0', '8'))
499 type = 'c';
500 else
501 break;
502 sh_sub = new_sh_sub_sid(demuxer, i, priv->sub_streams, lang ? lang->value : NULL);
503 if(!sh_sub) break;
504 stream_type = "subtitle";
505 priv->sstreams[priv->sub_streams] = i;
506 sh_sub->type = type;
507 if (codec->extradata_size) {
508 sh_sub->extradata = malloc(codec->extradata_size);
509 memcpy(sh_sub->extradata, codec->extradata, codec->extradata_size);
510 sh_sub->extradata_len = codec->extradata_size;
511 }
512 if (title && title->value)
513 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_NAME=%s\n", priv->sub_streams, title->value);
514 if (st->disposition & AV_DISPOSITION_DEFAULT)
515 sh_sub->default_track = 1;
516 stream_id = priv->sub_streams++;
517 break;
518 }
519 case AVMEDIA_TYPE_ATTACHMENT:{
520 if (st->codecpar->codec_id == AV_CODEC_ID_TTF || st->codecpar->codec_id == AV_CODEC_ID_OTF) {
521 AVDictionaryEntry *fnametag = av_dict_get(st->metadata, "filename", NULL, 0);
522 AVDictionaryEntry *mimetype = av_dict_get(st->metadata, "mimetype", NULL, 0);
523 demuxer_add_attachment(demuxer, fnametag ? fnametag->value : NULL,
524 mimetype ? mimetype->value : "application/x-font",
525 codec->extradata, codec->extradata_size);
526 }
527 break;
528 }
529 default:
530 st->discard= AVDISCARD_ALL;
531 }
532 if (stream_type) {
533 const AVCodec *avc = avcodec_find_decoder(codec->codec_id);
534 const char *codec_name = avc ? avc->name : "unknown";
535 if (!avc && *stream_type == 's' && demuxer->s_streams[i])
536 codec_name = sh_sub_type2str(((sh_sub_t *)demuxer->s_streams[i])->type);
537 mp_msg(MSGT_DEMUX, MSGL_INFO, "[lavf] stream %d: %s (%s), -%cid %d", i, stream_type, codec_name, *stream_type, stream_id);
538 if (lang && lang->value && *stream_type != 'v')
539 mp_msg(MSGT_DEMUX, MSGL_INFO, ", -%clang %s", *stream_type, lang->value);
540 if (title && title->value)
541 mp_msg(MSGT_DEMUX, MSGL_INFO, ", %s", title->value);
542 mp_msg(MSGT_DEMUX, MSGL_INFO, "\n");
543 }
544 }
545
demux_open_lavf(demuxer_t * demuxer)546 static demuxer_t* demux_open_lavf(demuxer_t *demuxer){
547 AVDictionary *opts = NULL;
548 AVFormatContext *avfc;
549 AVDictionaryEntry *t = NULL;
550 lavf_priv_t *priv= demuxer->priv;
551 int i;
552 char mp_filename[2048]="mp:";
553
554 stream_seek(demuxer->stream, 0);
555
556 avfc = avformat_alloc_context();
557
558 if (opt_cryptokey)
559 parse_cryptokey(avfc, opt_cryptokey);
560 if (user_correct_pts != 0)
561 avfc->flags |= AVFMT_FLAG_GENPTS;
562 if (index_mode == 0)
563 avfc->flags |= AVFMT_FLAG_IGNIDX;
564
565 if(opt_probesize) {
566 if (av_opt_set_int(avfc, "probesize", opt_probesize, 0) < 0)
567 mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option probesize to %u\n", opt_probesize);
568 }
569 if(opt_analyzeduration) {
570 if (av_opt_set_int(avfc, "analyzeduration", opt_analyzeduration * AV_TIME_BASE, 0) < 0)
571 mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option analyzeduration to %u\n", opt_analyzeduration);
572 }
573
574 if (rtsp_transport_http || rtsp_transport_tcp)
575 av_dict_set(&opts, "rtsp_transport", rtsp_transport_http ? "http" : "tcp", 0);
576
577 if(opt_avopt){
578 if(av_dict_parse_string(&opts, opt_avopt, "=", ",", 0) < 0){
579 mp_msg(MSGT_HEADER,MSGL_ERR, "Your options /%s/ look like gibberish to me pal\n", opt_avopt);
580 return NULL;
581 }
582 }
583
584 if(demuxer->stream->url) {
585 if (!strncmp(demuxer->stream->url, "ffmpeg://dummy://", 17))
586 av_strlcpy(mp_filename, demuxer->stream->url + 17, sizeof(mp_filename));
587 else if (!strncmp(demuxer->stream->url, "ffmpeg://", 9))
588 av_strlcpy(mp_filename, demuxer->stream->url + 9, sizeof(mp_filename));
589 else if (!strncmp(demuxer->stream->url, "rtsp://", 7))
590 av_strlcpy(mp_filename, demuxer->stream->url, sizeof(mp_filename));
591 else if (priv->use_lavf_netstream)
592 av_strlcpy(mp_filename, demuxer->stream->url, sizeof(mp_filename));
593 else
594 av_strlcat(mp_filename, demuxer->stream->url, sizeof(mp_filename));
595 } else
596 av_strlcat(mp_filename, "foobar.dummy", sizeof(mp_filename));
597
598 if (!(priv->avif->flags & AVFMT_NOFILE)) {
599 uint8_t *buffer = av_mallocz(BIO_BUFFER_SIZE);
600 priv->pb = avio_alloc_context(buffer, BIO_BUFFER_SIZE, 0,
601 demuxer, mp_read, NULL, mp_seek);
602 priv->pb->read_seek = mp_read_seek;
603 if (!demuxer->stream->end_pos || (demuxer->stream->flags & MP_STREAM_SEEK) != MP_STREAM_SEEK)
604 priv->pb->seekable = 0;
605 avfc->pb = priv->pb;
606 }
607
608 if(avformat_open_input(&avfc, mp_filename, priv->avif, &opts)<0){
609 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_open_input_stream() failed\n");
610 return NULL;
611 }
612 if (av_dict_count(opts)) {
613 AVDictionaryEntry *e = NULL;
614 int invalid = 0;
615 while ((e = av_dict_get(opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
616 if (strcmp(e->key, "rtsp_transport")) {
617 invalid++;
618 mp_msg(MSGT_HEADER,MSGL_ERR,"Unknown option %s\n", e->key);
619 }
620 }
621 if (invalid)
622 return 0;
623 }
624 av_dict_free(&opts);
625
626 priv->avfc= avfc;
627
628 if(avformat_find_stream_info(avfc, NULL) < 0){
629 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_find_stream_info() failed\n");
630 }
631
632 /* Add metadata. */
633 while((t = av_dict_get(avfc->metadata, "", t, AV_DICT_IGNORE_SUFFIX)))
634 demux_info_add(demuxer, t->key, t->value);
635
636 for(i=0; i < avfc->nb_chapters; i++) {
637 AVChapter *c = avfc->chapters[i];
638 uint64_t start = av_rescale_q(c->start, c->time_base, (AVRational){1,1000});
639 uint64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,1000});
640 t = av_dict_get(c->metadata, "title", NULL, 0);
641 demuxer_add_chapter(demuxer, t ? t->value : NULL, start, end);
642 }
643
644 for(i=0; i<avfc->nb_streams; i++)
645 handle_stream(demuxer, avfc, i);
646 priv->nb_streams_last = avfc->nb_streams;
647
648 if(avfc->nb_programs) {
649 int p;
650 for (p = 0; p < avfc->nb_programs; p++) {
651 AVProgram *program = avfc->programs[p];
652 t = av_dict_get(program->metadata, "title", NULL, 0);
653 mp_msg(MSGT_HEADER,MSGL_INFO,"LAVF: Program %d %s\n", program->id, t ? t->value : "");
654 mp_msg(MSGT_IDENTIFY, MSGL_V, "PROGRAM_ID=%d\n", program->id);
655 }
656 }
657
658 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: %d audio and %d video streams found\n",priv->audio_streams,priv->video_streams);
659 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: build %d\n", LIBAVFORMAT_BUILD);
660 if(!priv->audio_streams) demuxer->audio->id=-2; // nosound
661 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio;
662 if(!priv->video_streams){
663 if(!priv->audio_streams){
664 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF: no audio or video headers found - broken file?\n");
665 if (!priv->sub_streams)
666 return NULL;
667 }
668 demuxer->video->id=-2; // audio-only
669 } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video;
670
671 return demuxer;
672 }
673
demux_lavf_fill_buffer(demuxer_t * demux,demux_stream_t * dsds)674 static int demux_lavf_fill_buffer(demuxer_t *demux, demux_stream_t *dsds){
675 lavf_priv_t *priv= demux->priv;
676 AVPacket pkt;
677 demux_packet_t *dp;
678 demux_stream_t *ds;
679 int id;
680 double stream_pts = MP_NOPTS_VALUE;
681 mp_msg(MSGT_DEMUX,MSGL_DBG2,"demux_lavf_fill_buffer()\n");
682
683 demux->filepos=stream_tell(demux->stream);
684
685 if(av_read_frame(priv->avfc, &pkt) < 0)
686 return 0;
687
688 // handle any new streams that might have been added
689 for (id = priv->nb_streams_last; id < priv->avfc->nb_streams; id++)
690 handle_stream(demux, priv->avfc, id);
691 priv->nb_streams_last = priv->avfc->nb_streams;
692
693 id= pkt.stream_index;
694
695 if(id==demux->audio->id){
696 // audio
697 ds=demux->audio;
698 if(!ds->sh){
699 ds->sh=demux->a_streams[id];
700 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected LAVF audio ID = %d\n",ds->id);
701 }
702 } else if(id==demux->video->id){
703 // video
704 sh_video_t *sh;
705 ds=demux->video;
706 if(!ds->sh){
707 ds->sh=demux->v_streams[id];
708 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected LAVF video ID = %d\n",ds->id);
709 }
710 sh = ds->sh;
711 if (sh && sh->bih) {
712 size_t size = 0;
713 const uint8_t *pal = av_packet_get_side_data(&pkt, AV_PKT_DATA_PALETTE, &size);
714 if (pal && size)
715 memcpy(((uint8_t *)sh->bih) + sh->bih->biSize, pal, FFMIN(size, 1024));
716 }
717 } else if(id==demux->sub->id){
718 // subtitle
719 ds=demux->sub;
720 sub_utf8=1;
721 } else {
722 av_packet_unref(&pkt);
723 return 1;
724 }
725
726 mp_packet_merge_side_data(&pkt);
727 dp=new_demux_packet(pkt.size);
728 memcpy(dp->buffer, pkt.data, pkt.size);
729
730 if(pkt.pts != AV_NOPTS_VALUE){
731 dp->pts=pkt.pts * av_q2d(priv->avfc->streams[id]->time_base);
732 priv->last_pts= dp->pts * AV_TIME_BASE;
733 if(pkt.duration > 0)
734 dp->endpts = dp->pts + pkt.duration * av_q2d(priv->avfc->streams[id]->time_base);
735 }
736 dp->pos=demux->filepos;
737 dp->flags= !!(pkt.flags&AV_PKT_FLAG_KEY);
738 if (ds == demux->video &&
739 stream_control(demux->stream, STREAM_CTRL_GET_CURRENT_TIME, (void *)&stream_pts) != STREAM_UNSUPPORTED)
740 dp->stream_pts = stream_pts;
741 // append packet to DS stream:
742 ds_add_packet(ds,dp);
743 av_packet_unref(&pkt);
744 return 1;
745 }
746
demux_seek_lavf(demuxer_t * demuxer,float rel_seek_secs,float audio_delay,int flags)747 static void demux_seek_lavf(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags){
748 lavf_priv_t *priv = demuxer->priv;
749 int avsflags = 0;
750 mp_msg(MSGT_DEMUX,MSGL_DBG2,"demux_seek_lavf(%p, %f, %f, %d)\n", demuxer, rel_seek_secs, audio_delay, flags);
751
752 if (flags & SEEK_ABSOLUTE) {
753 priv->last_pts = priv->avfc->start_time != AV_NOPTS_VALUE ?
754 priv->avfc->start_time : 0;
755 }
756 // This is important also for SEEK_ABSOLUTE because seeking
757 // is done by dts, while start_time is relative to pts and thus
758 // usually too large.
759 if (rel_seek_secs <= 0) avsflags = AVSEEK_FLAG_BACKWARD;
760 if (flags & SEEK_FACTOR) {
761 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
762 return;
763 priv->last_pts += rel_seek_secs * priv->avfc->duration;
764 } else {
765 priv->last_pts += rel_seek_secs * AV_TIME_BASE;
766 }
767 if (av_seek_frame(priv->avfc, -1, priv->last_pts, avsflags) < 0) {
768 avsflags ^= AVSEEK_FLAG_BACKWARD;
769 av_seek_frame(priv->avfc, -1, priv->last_pts, avsflags);
770 }
771 }
772
demux_lavf_control(demuxer_t * demuxer,int cmd,void * arg)773 static int demux_lavf_control(demuxer_t *demuxer, int cmd, void *arg)
774 {
775 lavf_priv_t *priv = demuxer->priv;
776
777 switch (cmd) {
778 case DEMUXER_CTRL_CORRECT_PTS:
779 return DEMUXER_CTRL_OK;
780 case DEMUXER_CTRL_GET_TIME_LENGTH:
781 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
782 return DEMUXER_CTRL_DONTKNOW;
783
784 *((double *)arg) = (double)priv->avfc->duration / AV_TIME_BASE;
785 return DEMUXER_CTRL_OK;
786
787 case DEMUXER_CTRL_GET_PERCENT_POS:
788 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE)
789 return DEMUXER_CTRL_DONTKNOW;
790
791 *((int *)arg) = (int)((priv->last_pts - priv->avfc->start_time)*100 / priv->avfc->duration);
792 return DEMUXER_CTRL_OK;
793 case DEMUXER_CTRL_SWITCH_AUDIO:
794 case DEMUXER_CTRL_SWITCH_VIDEO:
795 {
796 int id = *((int*)arg);
797 int newid = -2;
798 int i, curridx = -1;
799 int nstreams, *pstreams;
800 demux_stream_t *ds;
801
802 if(cmd == DEMUXER_CTRL_SWITCH_VIDEO)
803 {
804 ds = demuxer->video;
805 nstreams = priv->video_streams;
806 pstreams = priv->vstreams;
807 }
808 else
809 {
810 ds = demuxer->audio;
811 nstreams = priv->audio_streams;
812 pstreams = priv->astreams;
813 }
814 for(i = 0; i < nstreams; i++)
815 {
816 if(pstreams[i] == ds->id) //current stream id
817 {
818 curridx = i;
819 break;
820 }
821 }
822
823 if(id == -2) { // no sound
824 i = -1;
825 } else if(id == -1) { // next track
826 i = (curridx + 2) % (nstreams + 1) - 1;
827 if (i >= 0)
828 newid = pstreams[i];
829 }
830 else // select track by id
831 {
832 if (id >= 0 && id < nstreams) {
833 i = id;
834 newid = pstreams[i];
835 }
836 }
837 if(i == curridx)
838 return DEMUXER_CTRL_NOTIMPL;
839 else
840 {
841 ds_free_packs(ds);
842 if(ds->id >= 0 && ds->id < nstreams)
843 priv->avfc->streams[ds->id]->discard = AVDISCARD_ALL;
844 *((int*)arg) = ds->id = newid;
845 if(newid >= 0)
846 priv->avfc->streams[newid]->discard = AVDISCARD_NONE;
847 return DEMUXER_CTRL_OK;
848 }
849 }
850 case DEMUXER_CTRL_IDENTIFY_PROGRAM:
851 {
852 demux_program_t *prog = arg;
853 AVProgram *program;
854 int p, i;
855 int start;
856
857 prog->vid = prog->aid = prog->sid = -2; //no audio and no video by default
858 if(priv->avfc->nb_programs < 1)
859 return DEMUXER_CTRL_DONTKNOW;
860
861 if(prog->progid == -1)
862 {
863 p = 0;
864 while(p<priv->avfc->nb_programs && priv->avfc->programs[p]->id != priv->cur_program)
865 p++;
866 p = (p + 1) % priv->avfc->nb_programs;
867 }
868 else
869 {
870 for(i=0; i<priv->avfc->nb_programs; i++)
871 if(priv->avfc->programs[i]->id == prog->progid)
872 break;
873 if(i==priv->avfc->nb_programs)
874 return DEMUXER_CTRL_DONTKNOW;
875 p = i;
876 }
877 start = p;
878 redo:
879 program = priv->avfc->programs[p];
880 for(i=0; i<program->nb_stream_indexes; i++)
881 {
882 switch(priv->avfc->streams[program->stream_index[i]]->codecpar->codec_type)
883 {
884 case AVMEDIA_TYPE_VIDEO:
885 if(prog->vid == -2)
886 prog->vid = program->stream_index[i];
887 break;
888 case AVMEDIA_TYPE_AUDIO:
889 if(prog->aid == -2)
890 prog->aid = program->stream_index[i];
891 break;
892 case AVMEDIA_TYPE_SUBTITLE:
893 if(prog->sid == -2)
894 prog->sid = program->stream_index[i];
895 break;
896 }
897 }
898 if (prog->aid >= 0 && prog->aid < MAX_A_STREAMS &&
899 demuxer->a_streams[prog->aid]) {
900 sh_audio_t *sh = demuxer->a_streams[prog->aid];
901 prog->aid = sh->aid;
902 } else
903 prog->aid = -2;
904 if (prog->vid >= 0 && prog->vid < MAX_V_STREAMS &&
905 demuxer->v_streams[prog->vid]) {
906 sh_video_t *sh = demuxer->v_streams[prog->vid];
907 prog->vid = sh->vid;
908 } else
909 prog->vid = -2;
910 if(prog->progid == -1 && prog->vid == -2 && prog->aid == -2)
911 {
912 p = (p + 1) % priv->avfc->nb_programs;
913 if (p == start)
914 return DEMUXER_CTRL_DONTKNOW;
915 goto redo;
916 }
917 priv->cur_program = prog->progid = program->id;
918 return DEMUXER_CTRL_OK;
919 }
920 case DEMUXER_CTRL_GET_REPLAY_GAIN:
921 if (priv->r_gain == INT32_MIN)
922 return DEMUXER_CTRL_DONTKNOW;
923 *((int *)arg) = priv->r_gain;
924 return DEMUXER_CTRL_OK;
925 default:
926 return DEMUXER_CTRL_NOTIMPL;
927 }
928 }
929
demux_close_lavf(demuxer_t * demuxer)930 static void demux_close_lavf(demuxer_t *demuxer)
931 {
932 lavf_priv_t* priv = demuxer->priv;
933 if (priv){
934 if(priv->avfc)
935 {
936 av_freep(&priv->avfc->key);
937 avformat_close_input(&priv->avfc);
938 }
939 if (priv->pb) av_freep(&priv->pb->buffer);
940 av_freep(&priv->pb);
941 free(priv); demuxer->priv= NULL;
942 }
943 }
944
945
946 const demuxer_desc_t demuxer_desc_lavf = {
947 "libavformat demuxer",
948 "lavf",
949 "libavformat",
950 "Michael Niedermayer",
951 "supports many formats, requires libavformat",
952 DEMUXER_TYPE_LAVF,
953 0, // Check after other demuxer
954 lavf_check_file,
955 demux_lavf_fill_buffer,
956 demux_open_lavf,
957 demux_close_lavf,
958 demux_seek_lavf,
959 demux_lavf_control
960 };
961
962 const demuxer_desc_t demuxer_desc_lavf_preferred = {
963 "libavformat preferred demuxer",
964 "lavfpref",
965 "libavformat",
966 "Michael Niedermayer",
967 "supports many formats, requires libavformat",
968 DEMUXER_TYPE_LAVF_PREFERRED,
969 1,
970 lavf_check_preferred_file,
971 demux_lavf_fill_buffer,
972 demux_open_lavf,
973 demux_close_lavf,
974 demux_seek_lavf,
975 demux_lavf_control
976 };
977