demux: provide full URL instead of access name (refs #18504)
[vlc.git] / modules / demux / avformat / demux.c
blobd45d3073f46c30b8ce4c7a16ef7f1984e1553e70
1 /*****************************************************************************
2 * demux.c: demuxer using libavformat
3 *****************************************************************************
4 * Copyright (C) 2004-2009 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <vlc_common.h>
34 #include <vlc_demux.h>
35 #include <vlc_stream.h>
36 #include <vlc_meta.h>
37 #include <vlc_input.h>
38 #include <vlc_charset.h>
39 #include <vlc_avcodec.h>
41 #include "../../codec/avcodec/avcodec.h"
42 #include "../../codec/avcodec/chroma.h"
43 #include "../../codec/avcodec/avcommon_compat.h"
44 #include "avformat.h"
45 #include "../xiph.h"
46 #include "../vobsub.h"
48 #include <libavformat/avformat.h>
49 #include <libavutil/display.h>
51 //#define AVFORMAT_DEBUG 1
53 # define HAVE_AVUTIL_CODEC_ATTACHMENT 1
55 struct avformat_track_s
57 es_out_id_t *p_es;
58 mtime_t i_pcr;
61 /*****************************************************************************
62 * demux_sys_t: demux descriptor
63 *****************************************************************************/
64 struct demux_sys_t
66 AVInputFormat *fmt;
67 AVFormatContext *ic;
69 struct avformat_track_s *tracks;
71 int64_t i_pcr;
73 unsigned i_ssa_order;
75 int i_attachments;
76 input_attachment_t **attachments;
78 /* Only one title with seekpoints possible atm. */
79 input_title_t *p_title;
82 #define AVFORMAT_IOBUFFER_SIZE 32768 /* FIXME */
84 /*****************************************************************************
85 * Local prototypes
86 *****************************************************************************/
87 static int Demux ( demux_t *p_demux );
88 static int Control( demux_t *p_demux, int i_query, va_list args );
90 static int IORead( void *opaque, uint8_t *buf, int buf_size );
91 static int64_t IOSeek( void *opaque, int64_t offset, int whence );
93 static block_t *BuildSsaFrame( const AVPacket *p_pkt, unsigned i_order );
94 static void UpdateSeekPoint( demux_t *p_demux, int64_t i_time );
95 static void ResetTime( demux_t *p_demux, int64_t i_time );
97 static vlc_fourcc_t CodecTagToFourcc( uint32_t codec_tag )
99 // convert from little-endian avcodec codec_tag to VLC native-endian fourcc
100 #ifdef WORDS_BIGENDIAN
101 return bswap32(codec_tag);
102 #else
103 return codec_tag;
104 #endif
107 /*****************************************************************************
108 * Open
109 *****************************************************************************/
111 static void get_rotation(es_format_t *fmt, AVStream *s)
113 char const *kRotateKey = "rotate";
114 AVDictionaryEntry *rotation = av_dict_get(s->metadata, kRotateKey, NULL, 0);
115 long angle = 0;
117 if( rotation )
119 angle = strtol(rotation->value, NULL, 10);
121 if (angle > 45 && angle < 135)
122 fmt->video.orientation = ORIENT_ROTATED_90;
124 else if (angle > 135 && angle < 225)
125 fmt->video.orientation = ORIENT_ROTATED_180;
127 else if (angle > 225 && angle < 315)
128 fmt->video.orientation = ORIENT_ROTATED_270;
130 else
131 fmt->video.orientation = ORIENT_NORMAL;
133 int32_t *matrix = (int32_t *)av_stream_get_side_data(s, AV_PKT_DATA_DISPLAYMATRIX, NULL);
134 if( matrix ) {
135 angle = lround(av_display_rotation_get(matrix));
137 if (angle > 45 && angle < 135)
138 fmt->video.orientation = ORIENT_ROTATED_270;
140 else if (angle > 135 || angle < -135)
141 fmt->video.orientation = ORIENT_ROTATED_180;
143 else if (angle < -45 && angle > -135)
144 fmt->video.orientation = ORIENT_ROTATED_90;
146 else
147 fmt->video.orientation = ORIENT_NORMAL;
151 int avformat_OpenDemux( vlc_object_t *p_this )
153 demux_t *p_demux = (demux_t*)p_this;
154 demux_sys_t *p_sys;
155 AVProbeData pd = { };
156 AVInputFormat *fmt = NULL;
157 int64_t i_start_time = -1;
158 bool b_can_seek;
159 const char *psz_url;
160 const uint8_t *peek;
161 int error;
163 /* Init Probe data */
164 pd.buf_size = vlc_stream_Peek( p_demux->s, &peek, 2048 + 213 );
165 if( pd.buf_size <= 0 )
167 msg_Warn( p_demux, "cannot peek" );
168 return VLC_EGENERIC;
171 pd.buf = malloc( pd.buf_size + AVPROBE_PADDING_SIZE );
172 if( unlikely(pd.buf == NULL) )
173 return VLC_ENOMEM;
175 memcpy( pd.buf, peek, pd.buf_size );
176 memset( pd.buf + pd.buf_size, 0, AVPROBE_PADDING_SIZE );
178 if( p_demux->psz_filepath )
179 psz_url = p_demux->psz_filepath;
180 else
181 psz_url = p_demux->psz_url;
183 if( psz_url != NULL )
184 msg_Dbg( p_demux, "trying url: %s", psz_url );
186 pd.filename = psz_url;
188 vlc_stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_can_seek );
190 vlc_init_avformat(p_this);
192 /* Guess format */
193 char *psz_format = var_InheritString( p_this, "avformat-format" );
194 if( psz_format )
196 if( (fmt = av_find_input_format(psz_format)) )
197 msg_Dbg( p_demux, "forcing format: %s", fmt->name );
198 free( psz_format );
201 if( fmt == NULL )
202 fmt = av_probe_input_format( &pd, 1 );
204 free( pd.buf );
206 if( fmt == NULL )
208 msg_Dbg( p_demux, "couldn't guess format" );
209 return VLC_EGENERIC;
212 if( !p_demux->obj.force )
214 static const char ppsz_blacklist[][16] = {
215 /* Don't handle MPEG unless forced */
216 "mpeg", "vcd", "vob", "mpegts",
217 /* libavformat's redirector won't work */
218 "redir", "sdp",
219 /* Don't handle subtitles format */
220 "ass", "srt", "microdvd",
221 /* No timestamps at all */
222 "hevc", "h264",
226 for( int i = 0; *ppsz_blacklist[i]; i++ )
228 if( !strcmp( fmt->name, ppsz_blacklist[i] ) )
229 return VLC_EGENERIC;
233 /* Don't trigger false alarms on bin files */
234 if( !p_demux->obj.force && !strcmp( fmt->name, "psxstr" ) )
236 int i_len;
238 if( !p_demux->psz_filepath )
239 return VLC_EGENERIC;
241 i_len = strlen( p_demux->psz_filepath );
242 if( i_len < 4 )
243 return VLC_EGENERIC;
245 if( strcasecmp( &p_demux->psz_filepath[i_len - 4], ".str" ) &&
246 strcasecmp( &p_demux->psz_filepath[i_len - 4], ".xai" ) &&
247 strcasecmp( &p_demux->psz_filepath[i_len - 3], ".xa" ) )
249 return VLC_EGENERIC;
253 msg_Dbg( p_demux, "detected format: %s", fmt->name );
255 /* Fill p_demux fields */
256 p_demux->pf_demux = Demux;
257 p_demux->pf_control = Control;
258 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
259 if( !p_sys )
260 return VLC_ENOMEM;
262 p_sys->ic = 0;
263 p_sys->fmt = fmt;
264 p_sys->tracks = NULL;
265 p_sys->i_ssa_order = 0;
266 TAB_INIT( p_sys->i_attachments, p_sys->attachments);
267 p_sys->p_title = NULL;
269 /* Create I/O wrapper */
270 unsigned char * p_io_buffer = av_malloc( AVFORMAT_IOBUFFER_SIZE );
271 if( !p_io_buffer )
273 avformat_CloseDemux( p_this );
274 return VLC_ENOMEM;
277 p_sys->ic = avformat_alloc_context();
278 if( !p_sys->ic )
280 av_free( p_io_buffer );
281 avformat_CloseDemux( p_this );
282 return VLC_ENOMEM;
285 AVIOContext *pb = p_sys->ic->pb = avio_alloc_context( p_io_buffer,
286 AVFORMAT_IOBUFFER_SIZE, 0, p_demux, IORead, NULL, IOSeek );
287 if( !pb )
289 av_free( p_io_buffer );
290 avformat_CloseDemux( p_this );
291 return VLC_ENOMEM;
294 p_sys->ic->pb->seekable = b_can_seek ? AVIO_SEEKABLE_NORMAL : 0;
295 error = avformat_open_input(&p_sys->ic, psz_url, p_sys->fmt, NULL);
297 if( error < 0 )
299 msg_Err( p_demux, "Could not open %s: %s", psz_url,
300 vlc_strerror_c(AVUNERROR(error)) );
301 av_free( pb->buffer );
302 av_free( pb );
303 p_sys->ic = NULL;
304 avformat_CloseDemux( p_this );
305 return VLC_EGENERIC;
308 char *psz_opts = var_InheritString( p_demux, "avformat-options" );
309 unsigned nb_streams = p_sys->ic->nb_streams;
311 AVDictionary *options[nb_streams ? nb_streams : 1];
312 options[0] = NULL;
313 for (unsigned i = 1; i < nb_streams; i++)
314 options[i] = NULL;
315 if (psz_opts) {
316 vlc_av_get_options(psz_opts, &options[0]);
317 for (unsigned i = 1; i < nb_streams; i++) {
318 av_dict_copy(&options[i], options[0], 0);
320 free(psz_opts);
322 vlc_avcodec_lock(); /* avformat calls avcodec behind our back!!! */
323 error = avformat_find_stream_info( p_sys->ic, options );
324 vlc_avcodec_unlock();
325 AVDictionaryEntry *t = NULL;
326 while ((t = av_dict_get(options[0], "", t, AV_DICT_IGNORE_SUFFIX))) {
327 msg_Err( p_demux, "Unknown option \"%s\"", t->key );
329 av_dict_free(&options[0]);
330 for (unsigned i = 1; i < nb_streams; i++) {
331 av_dict_free(&options[i]);
334 nb_streams = p_sys->ic->nb_streams; /* it may have changed */
335 if( !nb_streams )
337 msg_Err( p_demux, "No streams found");
338 avformat_CloseDemux( p_this );
339 return VLC_EGENERIC;
341 p_sys->tracks = calloc( nb_streams, sizeof(*p_sys->tracks) );
342 if( !p_sys->tracks )
344 avformat_CloseDemux( p_this );
345 return VLC_ENOMEM;
348 if( error < 0 )
350 msg_Warn( p_demux, "Could not find stream info: %s",
351 vlc_strerror_c(AVUNERROR(error)) );
354 for( unsigned i = 0; i < nb_streams; i++ )
356 struct avformat_track_s *p_track = &p_sys->tracks[i];
357 AVStream *s = p_sys->ic->streams[i];
358 const AVCodecParameters *cp = s->codecpar;
359 es_format_t es_fmt;
360 const char *psz_type = "unknown";
362 /* Do not use the cover art as a stream */
363 if( s->disposition == AV_DISPOSITION_ATTACHED_PIC )
364 continue;
366 vlc_fourcc_t fcc = GetVlcFourcc( cp->codec_id );
367 switch( cp->codec_type )
369 case AVMEDIA_TYPE_AUDIO:
370 es_format_Init( &es_fmt, AUDIO_ES, fcc );
371 es_fmt.i_original_fourcc = CodecTagToFourcc( cp->codec_tag );
372 es_fmt.i_bitrate = cp->bit_rate;
373 es_fmt.audio.i_channels = cp->channels;
374 es_fmt.audio.i_rate = cp->sample_rate;
375 es_fmt.audio.i_bitspersample = cp->bits_per_coded_sample;
376 es_fmt.audio.i_blockalign = cp->block_align;
377 psz_type = "audio";
379 if(cp->codec_id == AV_CODEC_ID_AAC_LATM)
381 es_fmt.i_original_fourcc = VLC_FOURCC('L','A','T','M');
382 es_fmt.b_packetized = false;
384 else if(cp->codec_id == AV_CODEC_ID_AAC &&
385 strstr(p_sys->fmt->long_name, "raw ADTS AAC"))
387 es_fmt.i_original_fourcc = VLC_FOURCC('A','D','T','S');
388 es_fmt.b_packetized = false;
390 break;
392 case AVMEDIA_TYPE_VIDEO:
393 es_format_Init( &es_fmt, VIDEO_ES, fcc );
394 es_fmt.i_original_fourcc = CodecTagToFourcc( cp->codec_tag );
396 es_fmt.video.i_bits_per_pixel = cp->bits_per_coded_sample;
397 /* Special case for raw video data */
398 if( cp->codec_id == AV_CODEC_ID_RAWVIDEO )
400 msg_Dbg( p_demux, "raw video, pixel format: %i", cp->format );
401 if( GetVlcChroma( &es_fmt.video, cp->format ) != VLC_SUCCESS)
403 msg_Err( p_demux, "was unable to find a FourCC match for raw video" );
405 else
406 es_fmt.i_codec = es_fmt.video.i_chroma;
408 /* We need this for the h264 packetizer */
409 else if( cp->codec_id == AV_CODEC_ID_H264 && ( p_sys->fmt == av_find_input_format("flv") ||
410 p_sys->fmt == av_find_input_format("matroska") || p_sys->fmt == av_find_input_format("mp4") ) )
411 es_fmt.i_original_fourcc = VLC_FOURCC( 'a', 'v', 'c', '1' );
413 es_fmt.video.i_width = cp->width;
414 es_fmt.video.i_height = cp->height;
415 es_fmt.video.i_visible_width = es_fmt.video.i_width;
416 es_fmt.video.i_visible_height = es_fmt.video.i_height;
418 get_rotation(&es_fmt, s);
420 # warning FIXME: implement palette transmission
421 psz_type = "video";
422 #if (LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(55, 20, 0))
423 es_fmt.video.i_frame_rate = s->time_base.num;
424 es_fmt.video.i_frame_rate_base = s->time_base.den;
425 if( s->codec->ticks_per_frame > 0 )
426 es_fmt.video.i_frame_rate_base *= s->codec->ticks_per_frame;
427 #else
428 es_fmt.video.i_frame_rate = s->codec->time_base.num;
429 es_fmt.video.i_frame_rate_base = s->codec->time_base.den * __MAX( s->codec->ticks_per_frame, 1 );
430 #endif
431 es_fmt.video.i_sar_num = s->sample_aspect_ratio.num;
432 if (s->sample_aspect_ratio.num > 0)
433 es_fmt.video.i_sar_den = s->sample_aspect_ratio.den;
434 else
435 es_fmt.video.i_sar_den = 0;
436 break;
438 case AVMEDIA_TYPE_SUBTITLE:
439 es_format_Init( &es_fmt, SPU_ES, fcc );
440 es_fmt.i_original_fourcc = CodecTagToFourcc( cp->codec_tag );
441 if( strncmp( p_sys->ic->iformat->name, "matroska", 8 ) == 0 &&
442 cp->codec_id == AV_CODEC_ID_DVD_SUBTITLE &&
443 cp->extradata != NULL &&
444 cp->extradata_size > 0 )
446 char *psz_start;
447 char *psz_buf = malloc( cp->extradata_size + 1);
448 if( psz_buf != NULL )
450 memcpy( psz_buf, cp->extradata , cp->extradata_size );
451 psz_buf[cp->extradata_size] = '\0';
453 psz_start = strstr( psz_buf, "size:" );
454 if( psz_start &&
455 vobsub_size_parse( psz_start,
456 &es_fmt.subs.spu.i_original_frame_width,
457 &es_fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
459 msg_Dbg( p_demux, "original frame size: %dx%d",
460 es_fmt.subs.spu.i_original_frame_width,
461 es_fmt.subs.spu.i_original_frame_height );
463 else
465 msg_Warn( p_demux, "reading original frame size failed" );
468 psz_start = strstr( psz_buf, "palette:" );
469 if( psz_start &&
470 vobsub_palette_parse( psz_start, &es_fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
472 es_fmt.subs.spu.palette[0] = SPU_PALETTE_DEFINED;
473 msg_Dbg( p_demux, "vobsub palette read" );
475 else
477 msg_Warn( p_demux, "reading original palette failed" );
479 free( psz_buf );
482 else if( !strcmp( p_sys->ic->iformat->name, "wtv" ) &&
483 cp->codec_id == AV_CODEC_ID_DVB_SUBTITLE &&
484 cp->extradata_size > 3 )
486 es_fmt.subs.dvb.i_id = GetWBE( cp->extradata ) |
487 (GetWBE( cp->extradata + 2 ) << 16);
490 psz_type = "subtitle";
491 break;
493 default:
494 es_format_Init( &es_fmt, UNKNOWN_ES, 0 );
495 es_fmt.i_original_fourcc = CodecTagToFourcc( cp->codec_tag );
496 #ifdef HAVE_AVUTIL_CODEC_ATTACHMENT
497 if( cp->codec_type == AVMEDIA_TYPE_ATTACHMENT )
499 input_attachment_t *p_attachment;
501 psz_type = "attachment";
502 if( cp->codec_id == AV_CODEC_ID_TTF )
504 AVDictionaryEntry *filename = av_dict_get( s->metadata, "filename", NULL, 0 );
505 if( filename && filename->value )
507 p_attachment = vlc_input_attachment_New(
508 filename->value, "application/x-truetype-font",
509 NULL, cp->extradata, (int)cp->extradata_size );
510 if( p_attachment )
511 TAB_APPEND( p_sys->i_attachments, p_sys->attachments,
512 p_attachment );
515 else msg_Warn( p_demux, "unsupported attachment type (%u) in avformat demux", cp->codec_id );
517 else
518 #endif
520 if( cp->codec_type == AVMEDIA_TYPE_DATA )
521 psz_type = "data";
523 msg_Warn( p_demux, "unsupported track type (%u:%u) in avformat demux", cp->codec_type, cp->codec_id );
525 break;
528 AVDictionaryEntry *language = av_dict_get( s->metadata, "language", NULL, 0 );
529 if ( language && language->value )
530 es_fmt.psz_language = strdup( language->value );
532 if( s->disposition & AV_DISPOSITION_DEFAULT )
533 es_fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN + 1000;
535 #ifdef HAVE_AVUTIL_CODEC_ATTACHMENT
536 if( cp->codec_type != AVMEDIA_TYPE_ATTACHMENT )
537 #endif
538 if( cp->codec_type != AVMEDIA_TYPE_DATA )
540 const bool b_ogg = !strcmp( p_sys->fmt->name, "ogg" );
541 const uint8_t *p_extra = cp->extradata;
542 unsigned i_extra = cp->extradata_size;
544 if( cp->codec_id == AV_CODEC_ID_THEORA && b_ogg )
546 unsigned pi_size[3];
547 const void *pp_data[3];
548 unsigned i_count;
549 for( i_count = 0; i_count < 3; i_count++ )
551 if( i_extra < 2 )
552 break;
553 pi_size[i_count] = GetWBE( p_extra );
554 pp_data[i_count] = &p_extra[2];
555 if( i_extra < pi_size[i_count] + 2 )
556 break;
558 p_extra += 2 + pi_size[i_count];
559 i_extra -= 2 + pi_size[i_count];
561 if( i_count > 0 && xiph_PackHeaders( &es_fmt.i_extra, &es_fmt.p_extra,
562 pi_size, pp_data, i_count ) )
564 es_fmt.i_extra = 0;
565 es_fmt.p_extra = NULL;
568 else if( cp->codec_id == AV_CODEC_ID_SPEEX && b_ogg )
570 const uint8_t p_dummy_comment[] = {
571 0, 0, 0, 0,
572 0, 0, 0, 0,
574 unsigned pi_size[2];
575 const void *pp_data[2];
577 pi_size[0] = i_extra;
578 pp_data[0] = p_extra;
580 pi_size[1] = sizeof(p_dummy_comment);
581 pp_data[1] = p_dummy_comment;
583 if( pi_size[0] > 0 && xiph_PackHeaders( &es_fmt.i_extra, &es_fmt.p_extra,
584 pi_size, pp_data, 2 ) )
586 es_fmt.i_extra = 0;
587 es_fmt.p_extra = NULL;
590 else if( cp->codec_id == AV_CODEC_ID_OPUS )
592 const uint8_t p_dummy_comment[] = {
593 'O', 'p', 'u', 's',
594 'T', 'a', 'g', 's',
595 0, 0, 0, 0, /* Vendor String length */
596 /* Vendor String */
597 0, 0, 0, 0, /* User Comment List Length */
600 unsigned pi_size[2];
601 const void *pp_data[2];
603 pi_size[0] = i_extra;
604 pp_data[0] = p_extra;
606 pi_size[1] = sizeof(p_dummy_comment);
607 pp_data[1] = p_dummy_comment;
609 if( pi_size[0] > 0 && xiph_PackHeaders( &es_fmt.i_extra, &es_fmt.p_extra,
610 pi_size, pp_data, 2 ) )
612 es_fmt.i_extra = 0;
613 es_fmt.p_extra = NULL;
616 else if( cp->extradata_size > 0 )
618 es_fmt.p_extra = malloc( i_extra );
619 if( es_fmt.p_extra )
621 es_fmt.i_extra = i_extra;
622 memcpy( es_fmt.p_extra, p_extra, i_extra );
626 p_track->p_es = es_out_Add( p_demux->out, &es_fmt );
627 if( p_track->p_es && (s->disposition & AV_DISPOSITION_DEFAULT) )
628 es_out_Control( p_demux->out, ES_OUT_SET_ES_DEFAULT, p_track->p_es );
630 msg_Dbg( p_demux, "adding es: %s codec = %4.4s (%d)",
631 psz_type, (char*)&fcc, cp->codec_id );
633 es_format_Clean( &es_fmt );
636 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
637 i_start_time = p_sys->ic->start_time * 1000000 / AV_TIME_BASE;
639 msg_Dbg( p_demux, "AVFormat(%s %s) supported stream", AVPROVIDER(LIBAVFORMAT), LIBAVFORMAT_IDENT );
640 msg_Dbg( p_demux, " - format = %s (%s)",
641 p_sys->fmt->name, p_sys->fmt->long_name );
642 msg_Dbg( p_demux, " - start time = %"PRId64, i_start_time );
643 msg_Dbg( p_demux, " - duration = %"PRId64,
644 ( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE ) ?
645 p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 );
647 if( p_sys->ic->nb_chapters > 0 )
649 p_sys->p_title = vlc_input_title_New();
650 p_sys->p_title->i_length = p_sys->ic->duration * 1000000 / AV_TIME_BASE;
653 for( unsigned i = 0; i < p_sys->ic->nb_chapters; i++ )
655 seekpoint_t *s = vlc_seekpoint_New();
657 AVDictionaryEntry *title = av_dict_get( p_sys->ic->metadata, "title", NULL, 0);
658 if( title && title->value )
660 s->psz_name = strdup( title->value );
661 EnsureUTF8( s->psz_name );
662 msg_Dbg( p_demux, " - chapter %d: %s", i, s->psz_name );
664 s->i_time_offset = p_sys->ic->chapters[i]->start * 1000000 *
665 p_sys->ic->chapters[i]->time_base.num /
666 p_sys->ic->chapters[i]->time_base.den -
667 (i_start_time != -1 ? i_start_time : 0 );
668 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
671 ResetTime( p_demux, 0 );
672 return VLC_SUCCESS;
675 /*****************************************************************************
676 * Close
677 *****************************************************************************/
678 void avformat_CloseDemux( vlc_object_t *p_this )
680 demux_t *p_demux = (demux_t*)p_this;
681 demux_sys_t *p_sys = p_demux->p_sys;
683 free( p_sys->tracks );
685 if( p_sys->ic )
687 if( p_sys->ic->pb )
689 av_free( p_sys->ic->pb->buffer );
690 av_free( p_sys->ic->pb );
692 avformat_close_input( &p_sys->ic );
695 for( int i = 0; i < p_sys->i_attachments; i++ )
696 vlc_input_attachment_Delete( p_sys->attachments[i] );
697 TAB_CLEAN( p_sys->i_attachments, p_sys->attachments);
699 if( p_sys->p_title )
700 vlc_input_title_Delete( p_sys->p_title );
702 free( p_sys );
705 /*****************************************************************************
706 * Demux:
707 *****************************************************************************/
708 static int Demux( demux_t *p_demux )
710 demux_sys_t *p_sys = p_demux->p_sys;
711 AVPacket pkt;
712 block_t *p_frame;
713 int64_t i_start_time;
715 /* Read a frame */
716 int i_av_ret = av_read_frame( p_sys->ic, &pkt );
717 if( i_av_ret )
719 /* Avoid EOF if av_read_frame returns AVERROR(EAGAIN) */
720 if( i_av_ret == AVERROR(EAGAIN) )
721 return 1;
723 return 0;
725 if( pkt.stream_index < 0 || (unsigned) pkt.stream_index >= p_sys->ic->nb_streams )
727 av_packet_unref( &pkt );
728 return 1;
730 struct avformat_track_s *p_track = &p_sys->tracks[pkt.stream_index];
731 const AVStream *p_stream = p_sys->ic->streams[pkt.stream_index];
732 if( p_stream->time_base.den <= 0 )
734 msg_Warn( p_demux, "Invalid time base for the stream %d", pkt.stream_index );
735 av_packet_unref( &pkt );
736 return 1;
738 if( p_stream->codecpar->codec_id == AV_CODEC_ID_SSA )
740 p_frame = BuildSsaFrame( &pkt, p_sys->i_ssa_order++ );
741 if( !p_frame )
743 av_packet_unref( &pkt );
744 return 1;
747 else if( !strcmp( p_sys->fmt->name, "wtv" ) &&
748 p_stream->codecpar->codec_id == AV_CODEC_ID_DVB_SUBTITLE )
750 if( ( p_frame = block_Alloc( pkt.size + 3 ) ) == NULL )
752 av_packet_unref( &pkt );
753 return 0;
755 p_frame->p_buffer[0] = 0x20;
756 p_frame->p_buffer[1] = 0x00;
757 memcpy( &p_frame->p_buffer[2], pkt.data, pkt.size );
758 p_frame->p_buffer[p_frame->i_buffer - 1] = 0x3f;
760 else
762 if( ( p_frame = block_Alloc( pkt.size ) ) == NULL )
764 av_packet_unref( &pkt );
765 return 0;
767 memcpy( p_frame->p_buffer, pkt.data, pkt.size );
770 if( pkt.flags & AV_PKT_FLAG_KEY )
771 p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
773 /* Used to avoid timestamps overlow */
774 lldiv_t q;
775 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
777 q = lldiv( p_sys->ic->start_time, AV_TIME_BASE);
778 i_start_time = q.quot * CLOCK_FREQ + q.rem * CLOCK_FREQ / AV_TIME_BASE;
780 else
781 i_start_time = 0;
783 if( pkt.dts == (int64_t)AV_NOPTS_VALUE )
784 p_frame->i_dts = VLC_TS_INVALID;
785 else
787 q = lldiv( pkt.dts, p_stream->time_base.den );
788 p_frame->i_dts = q.quot * CLOCK_FREQ *
789 p_stream->time_base.num + q.rem * CLOCK_FREQ *
790 p_stream->time_base.num /
791 p_stream->time_base.den - i_start_time + VLC_TS_0;
794 if( pkt.pts == (int64_t)AV_NOPTS_VALUE )
795 p_frame->i_pts = VLC_TS_INVALID;
796 else
798 q = lldiv( pkt.pts, p_stream->time_base.den );
799 p_frame->i_pts = q.quot * CLOCK_FREQ *
800 p_stream->time_base.num + q.rem * CLOCK_FREQ *
801 p_stream->time_base.num /
802 p_stream->time_base.den - i_start_time + VLC_TS_0;
804 if( pkt.duration > 0 && p_frame->i_length <= 0 )
805 p_frame->i_length = pkt.duration * CLOCK_FREQ *
806 p_stream->time_base.num /
807 p_stream->time_base.den;
809 /* Add here notoriously bugged file formats/samples */
810 if( !strcmp( p_sys->fmt->name, "flv" ) )
812 /* FLV and video PTS */
813 if( p_stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
814 pkt.dts != (int64_t)AV_NOPTS_VALUE && pkt.dts == pkt.pts )
815 p_frame->i_pts = VLC_TS_INVALID;
817 /* Handle broken dts/pts increase with AAC. Duration is correct.
818 * sky_the80s_aacplus.flv #8195 */
819 if( p_stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
820 p_stream->codecpar->codec_id == AV_CODEC_ID_AAC )
822 if( p_track->i_pcr != VLC_TS_INVALID &&
823 p_track->i_pcr + p_frame->i_length > p_frame->i_dts )
825 p_frame->i_dts = p_frame->i_pts = p_track->i_pcr + p_frame->i_length;
829 #ifdef AVFORMAT_DEBUG
830 msg_Dbg( p_demux, "tk[%d] dts=%"PRId64" pts=%"PRId64,
831 pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
832 #endif
833 if( p_frame->i_dts > VLC_TS_INVALID && p_track->p_es != NULL )
834 p_track->i_pcr = p_frame->i_dts;
836 int64_t i_ts_max = INT64_MIN;
837 for( unsigned i = 0; i < p_sys->ic->nb_streams; i++ )
839 if( p_sys->tracks[i].p_es != NULL )
840 i_ts_max = __MAX( i_ts_max, p_sys->tracks[i].i_pcr );
843 int64_t i_ts_min = INT64_MAX;
844 for( unsigned i = 0; i < p_sys->ic->nb_streams; i++ )
846 if( p_sys->tracks[i].p_es != NULL &&
847 p_sys->tracks[i].i_pcr > VLC_TS_INVALID &&
848 p_sys->tracks[i].i_pcr + 10 * CLOCK_FREQ >= i_ts_max )
849 i_ts_min = __MIN( i_ts_min, p_sys->tracks[i].i_pcr );
851 if( i_ts_min >= p_sys->i_pcr && likely(i_ts_min != INT64_MAX) )
853 p_sys->i_pcr = i_ts_min;
854 es_out_SetPCR( p_demux->out, p_sys->i_pcr );
855 UpdateSeekPoint( p_demux, p_sys->i_pcr );
858 if( p_track->p_es != NULL )
859 es_out_Send( p_demux->out, p_track->p_es, p_frame );
860 else
861 block_Release( p_frame );
863 av_packet_unref( &pkt );
864 return 1;
867 static void UpdateSeekPoint( demux_t *p_demux, int64_t i_time )
869 demux_sys_t *p_sys = p_demux->p_sys;
870 int i;
872 if( !p_sys->p_title )
873 return;
875 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
877 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
878 break;
880 i--;
882 if( i != p_demux->info.i_seekpoint && i >= 0 )
884 p_demux->info.i_seekpoint = i;
885 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
889 static void ResetTime( demux_t *p_demux, int64_t i_time )
891 demux_sys_t *p_sys = p_demux->p_sys;
893 if( p_sys->ic->start_time == (int64_t)AV_NOPTS_VALUE || i_time < 0 )
894 i_time = VLC_TS_INVALID;
895 else if( i_time == 0 )
896 i_time = 1;
898 p_sys->i_pcr = i_time;
899 for( unsigned i = 0; i < p_sys->ic->nb_streams; i++ )
900 p_sys->tracks[i].i_pcr = i_time;
902 if( i_time > VLC_TS_INVALID )
904 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_time );
905 UpdateSeekPoint( p_demux, i_time );
909 static block_t *BuildSsaFrame( const AVPacket *p_pkt, unsigned i_order )
911 if( p_pkt->size <= 0 )
912 return NULL;
914 char buffer[256];
915 const size_t i_buffer_size = __MIN( (int)sizeof(buffer) - 1, p_pkt->size );
916 memcpy( buffer, p_pkt->data, i_buffer_size );
917 buffer[i_buffer_size] = '\0';
919 /* */
920 int i_layer;
921 int h0, m0, s0, c0;
922 int h1, m1, s1, c1;
923 int i_position = 0;
924 if( sscanf( buffer, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,%n", &i_layer,
925 &h0, &m0, &s0, &c0, &h1, &m1, &s1, &c1, &i_position ) < 9 )
926 return NULL;
927 if( i_position <= 0 || (unsigned)i_position >= i_buffer_size )
928 return NULL;
930 char *p;
931 if( asprintf( &p, "%u,%d,%.*s", i_order, i_layer, p_pkt->size - i_position, p_pkt->data + i_position ) < 0 )
932 return NULL;
934 block_t *p_frame = block_heap_Alloc( p, strlen(p) + 1 );
935 if( p_frame )
936 p_frame->i_length = CLOCK_FREQ * ((h1-h0) * 3600 +
937 (m1-m0) * 60 +
938 (s1-s0) * 1) +
939 CLOCK_FREQ * (c1-c0) / 100;
940 return p_frame;
943 /*****************************************************************************
944 * Control:
945 *****************************************************************************/
946 static int Control( demux_t *p_demux, int i_query, va_list args )
948 demux_sys_t *p_sys = p_demux->p_sys;
949 const int64_t i_start_time = p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ? p_sys->ic->start_time : 0;
950 double f, *pf;
951 int64_t i64, *pi64;
953 switch( i_query )
955 case DEMUX_CAN_SEEK:
956 *va_arg( args, bool * ) = true;
957 return VLC_SUCCESS;
959 case DEMUX_GET_POSITION:
960 pf = va_arg( args, double * ); *pf = 0.0;
961 i64 = stream_Size( p_demux->s );
962 if( i64 > 0 )
964 double current = vlc_stream_Tell( p_demux->s );
965 *pf = current / (double)i64;
968 if( (p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE) && (p_sys->i_pcr > 0) )
970 *pf = (double)p_sys->i_pcr / (double)p_sys->ic->duration;
973 return VLC_SUCCESS;
975 case DEMUX_SET_POSITION:
976 f = va_arg( args, double );
977 i64 = p_sys->ic->duration * f + i_start_time;
979 msg_Warn( p_demux, "DEMUX_SET_POSITION: %"PRId64, i64 );
981 /* If we have a duration, we prefer to seek by time
982 but if we don't, or if the seek fails, try BYTE seeking */
983 if( p_sys->ic->duration == (int64_t)AV_NOPTS_VALUE ||
984 (av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BACKWARD ) < 0) )
986 int64_t i_size = stream_Size( p_demux->s );
987 i64 = (i_size * f);
989 msg_Warn( p_demux, "DEMUX_SET_BYTE_POSITION: %"PRId64, i64 );
990 if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BYTE ) < 0 )
991 return VLC_EGENERIC;
993 ResetTime( p_demux, -1 );
995 else
997 ResetTime( p_demux, i64 - i_start_time );
999 return VLC_SUCCESS;
1001 case DEMUX_GET_LENGTH:
1002 pi64 = va_arg( args, int64_t * );
1003 if( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE )
1004 *pi64 = p_sys->ic->duration * 1000000 / AV_TIME_BASE;
1005 else
1006 *pi64 = 0;
1007 return VLC_SUCCESS;
1009 case DEMUX_GET_TIME:
1010 pi64 = va_arg( args, int64_t * );
1011 *pi64 = p_sys->i_pcr;
1012 return VLC_SUCCESS;
1014 case DEMUX_SET_TIME:
1016 i64 = va_arg( args, int64_t );
1017 i64 = i64 *AV_TIME_BASE / 1000000 + i_start_time;
1019 msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
1021 if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BACKWARD ) < 0 )
1023 return VLC_EGENERIC;
1025 ResetTime( p_demux, i64 - i_start_time );
1026 return VLC_SUCCESS;
1029 case DEMUX_HAS_UNSUPPORTED_META:
1031 bool *pb_bool = va_arg( args, bool* );
1032 *pb_bool = true;
1033 return VLC_SUCCESS;
1037 case DEMUX_GET_META:
1039 static const char names[][10] = {
1040 [vlc_meta_Title] = "title",
1041 [vlc_meta_Artist] = "artist",
1042 [vlc_meta_Genre] = "genre",
1043 [vlc_meta_Copyright] = "copyright",
1044 [vlc_meta_Album] = "album",
1045 //[vlc_meta_TrackNumber] -- TODO: parse number/total value
1046 [vlc_meta_Description] = "comment",
1047 //[vlc_meta_Rating]
1048 [vlc_meta_Date] = "date",
1049 [vlc_meta_Setting] = "encoder",
1050 //[vlc_meta_URL]
1051 [vlc_meta_Language] = "language",
1052 //[vlc_meta_NowPlaying]
1053 [vlc_meta_Publisher] = "publisher",
1054 [vlc_meta_EncodedBy] = "encoded_by",
1055 //[vlc_meta_ArtworkURL]
1056 //[vlc_meta_TrackID]
1057 //[vlc_meta_TrackTotal]
1059 vlc_meta_t *p_meta = va_arg( args, vlc_meta_t * );
1060 AVDictionary *dict = p_sys->ic->metadata;
1062 for( unsigned i = 0; i < sizeof(names) / sizeof(*names); i++)
1064 if( !names[i][0] )
1065 continue;
1067 AVDictionaryEntry *e = av_dict_get( dict, names[i], NULL, 0 );
1068 if( e != NULL && e->value != NULL && IsUTF8(e->value) )
1069 vlc_meta_Set( p_meta, i, e->value );
1071 return VLC_SUCCESS;
1074 case DEMUX_GET_ATTACHMENTS:
1076 input_attachment_t ***ppp_attach =
1077 va_arg( args, input_attachment_t*** );
1078 int *pi_int = va_arg( args, int * );
1079 int i;
1081 if( p_sys->i_attachments <= 0 )
1082 return VLC_EGENERIC;
1084 *ppp_attach = vlc_alloc( p_sys->i_attachments, sizeof(input_attachment_t*) );
1085 if( *ppp_attach == NULL )
1086 return VLC_EGENERIC;
1088 for( i = 0; i < p_sys->i_attachments; i++ )
1090 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
1091 if((*ppp_attach)[i] == NULL)
1092 break;
1094 *pi_int = i;
1095 return VLC_SUCCESS;
1098 case DEMUX_GET_TITLE_INFO:
1100 input_title_t ***ppp_title = va_arg( args, input_title_t *** );
1101 int *pi_int = va_arg( args, int * );
1102 int *pi_title_offset = va_arg( args, int * );
1103 int *pi_seekpoint_offset = va_arg( args, int * );
1105 if( !p_sys->p_title )
1106 return VLC_EGENERIC;
1108 *ppp_title = malloc( sizeof( input_title_t*) );
1109 if( *ppp_title == NULL )
1110 return VLC_EGENERIC;
1111 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
1112 *pi_int = (*ppp_title)[0] ? 1 : 0;
1113 *pi_title_offset = 0;
1114 *pi_seekpoint_offset = 0;
1115 return VLC_SUCCESS;
1117 case DEMUX_SET_TITLE:
1119 const int i_title = va_arg( args, int );
1120 if( !p_sys->p_title || i_title != 0 )
1121 return VLC_EGENERIC;
1122 return VLC_SUCCESS;
1124 case DEMUX_SET_SEEKPOINT:
1126 const int i_seekpoint = va_arg( args, int );
1127 if( !p_sys->p_title )
1128 return VLC_EGENERIC;
1130 i64 = p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset *
1131 AV_TIME_BASE / 1000000 + i_start_time;
1133 msg_Warn( p_demux, "DEMUX_SET_SEEKPOINT: %"PRId64, i64 );
1135 if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BACKWARD ) < 0 )
1137 return VLC_EGENERIC;
1139 ResetTime( p_demux, i64 - i_start_time );
1140 return VLC_SUCCESS;
1144 default:
1145 return VLC_EGENERIC;
1149 /*****************************************************************************
1150 * I/O wrappers for libavformat
1151 *****************************************************************************/
1152 static int IORead( void *opaque, uint8_t *buf, int buf_size )
1154 demux_t *p_demux = opaque;
1155 if( buf_size < 0 ) return -1;
1156 int i_ret = vlc_stream_Read( p_demux->s, buf, buf_size );
1157 return i_ret >= 0 ? i_ret : -1;
1160 static int64_t IOSeek( void *opaque, int64_t offset, int whence )
1162 demux_t *p_demux = opaque;
1163 int64_t i_absolute;
1164 int64_t i_size = stream_Size( p_demux->s );
1166 #ifdef AVFORMAT_DEBUG
1167 msg_Warn( p_demux, "IOSeek offset: %"PRId64", whence: %i", offset, whence );
1168 #endif
1170 switch( whence )
1172 #ifdef AVSEEK_SIZE
1173 case AVSEEK_SIZE:
1174 return i_size;
1175 #endif
1176 case SEEK_SET:
1177 i_absolute = (int64_t)offset;
1178 break;
1179 case SEEK_CUR:
1180 i_absolute = vlc_stream_Tell( p_demux->s ) + (int64_t)offset;
1181 break;
1182 case SEEK_END:
1183 i_absolute = i_size + (int64_t)offset;
1184 break;
1185 default:
1186 return -1;
1190 if( i_absolute < 0 )
1192 msg_Dbg( p_demux, "Trying to seek before the beginning" );
1193 return -1;
1196 if( i_size > 0 && i_absolute >= i_size )
1198 msg_Dbg( p_demux, "Trying to seek too far : EOF?" );
1199 return -1;
1202 if( vlc_stream_Seek( p_demux->s, i_absolute ) )
1204 msg_Warn( p_demux, "we were not allowed to seek, or EOF " );
1205 return -1;
1208 return vlc_stream_Tell( p_demux->s );