demux: avformat: handle fast seeking
[vlc.git] / modules / demux / avformat / demux.c
blobb9125095a301905eb97f0066bec6c84cfe1c2d89
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 vlc_tick_t i_pcr;
61 /*****************************************************************************
62 * demux_sys_t: demux descriptor
63 *****************************************************************************/
64 typedef struct
66 AVInputFormat *fmt;
67 AVFormatContext *ic;
69 struct avformat_track_s *tracks;
70 unsigned i_tracks;
72 vlc_tick_t i_pcr;
74 unsigned i_ssa_order;
76 int i_attachments;
77 input_attachment_t **attachments;
79 /* Only one title with seekpoints possible atm. */
80 input_title_t *p_title;
81 int i_seekpoint;
82 unsigned i_update;
83 } demux_sys_t;
85 #define AVFORMAT_IOBUFFER_SIZE 32768 /* FIXME */
87 /*****************************************************************************
88 * Local prototypes
89 *****************************************************************************/
90 static int Demux ( demux_t *p_demux );
91 static int Control( demux_t *p_demux, int i_query, va_list args );
93 static int IORead( void *opaque, uint8_t *buf, int buf_size );
94 static int64_t IOSeek( void *opaque, int64_t offset, int whence );
96 static block_t *BuildSsaFrame( const AVPacket *p_pkt, unsigned i_order );
97 static void UpdateSeekPoint( demux_t *p_demux, int64_t i_time );
98 static void ResetTime( demux_t *p_demux, int64_t i_time );
100 static vlc_fourcc_t CodecTagToFourcc( uint32_t codec_tag )
102 // convert from little-endian avcodec codec_tag to VLC native-endian fourcc
103 #ifdef WORDS_BIGENDIAN
104 return vlc_bswap32(codec_tag);
105 #else
106 return codec_tag;
107 #endif
110 /*****************************************************************************
111 * Open
112 *****************************************************************************/
114 static void get_rotation(es_format_t *fmt, AVStream *s)
116 char const *kRotateKey = "rotate";
117 AVDictionaryEntry *rotation = av_dict_get(s->metadata, kRotateKey, NULL, 0);
118 long angle = 0;
120 if( rotation )
122 angle = strtol(rotation->value, NULL, 10);
124 if (angle > 45 && angle < 135)
125 fmt->video.orientation = ORIENT_ROTATED_90;
127 else if (angle > 135 && angle < 225)
128 fmt->video.orientation = ORIENT_ROTATED_180;
130 else if (angle > 225 && angle < 315)
131 fmt->video.orientation = ORIENT_ROTATED_270;
133 else
134 fmt->video.orientation = ORIENT_NORMAL;
136 int32_t *matrix = (int32_t *)av_stream_get_side_data(s, AV_PKT_DATA_DISPLAYMATRIX, NULL);
137 if( matrix ) {
138 angle = lround(av_display_rotation_get(matrix));
140 if (angle > 45 && angle < 135)
141 fmt->video.orientation = ORIENT_ROTATED_270;
143 else if (angle > 135 || angle < -135)
144 fmt->video.orientation = ORIENT_ROTATED_180;
146 else if (angle < -45 && angle > -135)
147 fmt->video.orientation = ORIENT_ROTATED_90;
149 else
150 fmt->video.orientation = ORIENT_NORMAL;
154 static int avformat_ProbeDemux( vlc_object_t *p_this,
155 AVInputFormat **pp_fmt, const char *psz_url )
157 demux_t *p_demux = (demux_t*)p_this;
158 AVProbeData pd = { 0 };
159 const uint8_t *peek;
161 /* Init Probe data */
162 pd.buf_size = vlc_stream_Peek( p_demux->s, &peek, 2048 + 213 );
163 if( pd.buf_size <= 0 )
165 msg_Warn( p_demux, "cannot peek" );
166 return VLC_EGENERIC;
169 pd.buf = malloc( pd.buf_size + AVPROBE_PADDING_SIZE );
170 if( unlikely(pd.buf == NULL) )
171 return VLC_ENOMEM;
173 memcpy( pd.buf, peek, pd.buf_size );
174 memset( pd.buf + pd.buf_size, 0, AVPROBE_PADDING_SIZE );
176 if( psz_url != NULL )
177 msg_Dbg( p_demux, "trying url: %s", psz_url );
179 pd.filename = psz_url;
181 vlc_init_avformat(p_this);
183 /* Guess format */
184 char *psz_format = var_InheritString( p_this, "avformat-format" );
185 if( psz_format )
187 if( (*pp_fmt = av_find_input_format(psz_format)) )
188 msg_Dbg( p_demux, "forcing format: %s", (*pp_fmt)->name );
189 free( psz_format );
192 if( *pp_fmt == NULL )
193 *pp_fmt = av_probe_input_format( &pd, 1 );
195 free( pd.buf );
197 if( *pp_fmt == NULL )
199 msg_Dbg( p_demux, "couldn't guess format" );
200 return VLC_EGENERIC;
203 if( !p_demux->obj.force )
205 static const char ppsz_blacklist[][16] = {
206 /* Don't handle MPEG unless forced */
207 "mpeg", "vcd", "vob", "mpegts",
208 /* libavformat's redirector won't work */
209 "redir", "sdp",
210 /* Don't handle subtitles format */
211 "ass", "srt", "microdvd",
212 /* No timestamps at all */
213 "hevc", "h264",
217 for( int i = 0; *ppsz_blacklist[i]; i++ )
219 if( !strcmp( (*pp_fmt)->name, ppsz_blacklist[i] ) )
220 return VLC_EGENERIC;
224 /* Don't trigger false alarms on bin files */
225 if( !p_demux->obj.force && !strcmp( (*pp_fmt)->name, "psxstr" ) )
227 int i_len;
229 if( !p_demux->psz_filepath )
230 return VLC_EGENERIC;
232 i_len = strlen( p_demux->psz_filepath );
233 if( i_len < 4 )
234 return VLC_EGENERIC;
236 if( strcasecmp( &p_demux->psz_filepath[i_len - 4], ".str" ) &&
237 strcasecmp( &p_demux->psz_filepath[i_len - 4], ".xai" ) &&
238 strcasecmp( &p_demux->psz_filepath[i_len - 3], ".xa" ) )
240 return VLC_EGENERIC;
244 msg_Dbg( p_demux, "detected format: %s", (*pp_fmt)->name );
246 return VLC_SUCCESS;
249 int avformat_OpenDemux( vlc_object_t *p_this )
251 demux_t *p_demux = (demux_t*)p_this;
252 demux_sys_t *p_sys;
253 AVInputFormat *fmt = NULL;
254 int64_t i_start_time = -1;
255 bool b_can_seek;
256 const char *psz_url;
257 int error;
259 if( p_demux->psz_filepath )
260 psz_url = p_demux->psz_filepath;
261 else
262 psz_url = p_demux->psz_url;
264 if( avformat_ProbeDemux( p_this, &fmt, psz_url ) != VLC_SUCCESS )
265 return VLC_EGENERIC;
267 vlc_stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_can_seek );
269 /* Fill p_demux fields */
270 p_demux->pf_demux = Demux;
271 p_demux->pf_control = Control;
272 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
273 if( !p_sys )
274 return VLC_ENOMEM;
276 p_sys->ic = 0;
277 p_sys->fmt = fmt;
278 p_sys->tracks = NULL;
279 p_sys->i_ssa_order = 0;
280 TAB_INIT( p_sys->i_attachments, p_sys->attachments);
281 p_sys->p_title = NULL;
282 p_sys->i_seekpoint = 0;
283 p_sys->i_update = 0;
285 /* Create I/O wrapper */
286 unsigned char * p_io_buffer = av_malloc( AVFORMAT_IOBUFFER_SIZE );
287 if( !p_io_buffer )
289 avformat_CloseDemux( p_this );
290 return VLC_ENOMEM;
293 p_sys->ic = avformat_alloc_context();
294 if( !p_sys->ic )
296 av_free( p_io_buffer );
297 avformat_CloseDemux( p_this );
298 return VLC_ENOMEM;
301 AVIOContext *pb = p_sys->ic->pb = avio_alloc_context( p_io_buffer,
302 AVFORMAT_IOBUFFER_SIZE, 0, p_demux, IORead, NULL, IOSeek );
303 if( !pb )
305 av_free( p_io_buffer );
306 avformat_CloseDemux( p_this );
307 return VLC_ENOMEM;
310 p_sys->ic->pb->seekable = b_can_seek ? AVIO_SEEKABLE_NORMAL : 0;
311 error = avformat_open_input(&p_sys->ic, psz_url, p_sys->fmt, NULL);
313 if( error < 0 )
315 msg_Err( p_demux, "Could not open %s: %s", psz_url,
316 vlc_strerror_c(AVUNERROR(error)) );
317 av_free( pb->buffer );
318 av_free( pb );
319 p_sys->ic = NULL;
320 avformat_CloseDemux( p_this );
321 return VLC_EGENERIC;
324 char *psz_opts = var_InheritString( p_demux, "avformat-options" );
325 unsigned nb_streams = p_sys->ic->nb_streams;
327 AVDictionary *options[nb_streams ? nb_streams : 1];
328 options[0] = NULL;
329 for (unsigned i = 1; i < nb_streams; i++)
330 options[i] = NULL;
331 if (psz_opts) {
332 vlc_av_get_options(psz_opts, &options[0]);
333 for (unsigned i = 1; i < nb_streams; i++) {
334 av_dict_copy(&options[i], options[0], 0);
336 free(psz_opts);
338 vlc_avcodec_lock(); /* avformat calls avcodec behind our back!!! */
339 error = avformat_find_stream_info( p_sys->ic, options );
340 vlc_avcodec_unlock();
341 AVDictionaryEntry *t = NULL;
342 while ((t = av_dict_get(options[0], "", t, AV_DICT_IGNORE_SUFFIX))) {
343 msg_Err( p_demux, "Unknown option \"%s\"", t->key );
345 av_dict_free(&options[0]);
346 for (unsigned i = 1; i < nb_streams; i++) {
347 av_dict_free(&options[i]);
350 nb_streams = p_sys->ic->nb_streams; /* it may have changed */
351 if( !nb_streams )
353 msg_Err( p_demux, "No streams found");
354 avformat_CloseDemux( p_this );
355 return VLC_EGENERIC;
357 p_sys->tracks = calloc( nb_streams, sizeof(*p_sys->tracks) );
358 if( !p_sys->tracks )
360 avformat_CloseDemux( p_this );
361 return VLC_ENOMEM;
363 p_sys->i_tracks = nb_streams;
365 if( error < 0 )
367 msg_Warn( p_demux, "Could not find stream info: %s",
368 vlc_strerror_c(AVUNERROR(error)) );
371 for( unsigned i = 0; i < nb_streams; i++ )
373 struct avformat_track_s *p_track = &p_sys->tracks[i];
374 AVStream *s = p_sys->ic->streams[i];
375 const AVCodecParameters *cp = s->codecpar;
376 es_format_t es_fmt;
377 const char *psz_type = "unknown";
379 /* Do not use the cover art as a stream */
380 if( s->disposition == AV_DISPOSITION_ATTACHED_PIC )
381 continue;
383 vlc_fourcc_t fcc = GetVlcFourcc( cp->codec_id );
384 switch( cp->codec_type )
386 case AVMEDIA_TYPE_AUDIO:
387 es_format_Init( &es_fmt, AUDIO_ES, fcc );
388 es_fmt.i_original_fourcc = CodecTagToFourcc( cp->codec_tag );
389 es_fmt.i_bitrate = cp->bit_rate;
390 es_fmt.audio.i_channels = cp->channels;
391 es_fmt.audio.i_rate = cp->sample_rate;
392 es_fmt.audio.i_bitspersample = cp->bits_per_coded_sample;
393 es_fmt.audio.i_blockalign = cp->block_align;
394 psz_type = "audio";
396 if(cp->codec_id == AV_CODEC_ID_AAC_LATM)
398 es_fmt.i_original_fourcc = VLC_FOURCC('L','A','T','M');
399 es_fmt.b_packetized = false;
401 else if(cp->codec_id == AV_CODEC_ID_AAC && p_sys->fmt->long_name &&
402 strstr(p_sys->fmt->long_name, "raw ADTS AAC"))
404 es_fmt.i_original_fourcc = VLC_FOURCC('A','D','T','S');
405 es_fmt.b_packetized = false;
407 break;
409 case AVMEDIA_TYPE_VIDEO:
410 es_format_Init( &es_fmt, VIDEO_ES, fcc );
411 es_fmt.i_original_fourcc = CodecTagToFourcc( cp->codec_tag );
413 es_fmt.video.i_bits_per_pixel = cp->bits_per_coded_sample;
414 /* Special case for raw video data */
415 if( cp->codec_id == AV_CODEC_ID_RAWVIDEO )
417 msg_Dbg( p_demux, "raw video, pixel format: %i", cp->format );
418 if( GetVlcChroma( &es_fmt.video, cp->format ) != VLC_SUCCESS)
420 msg_Err( p_demux, "was unable to find a FourCC match for raw video" );
422 else
423 es_fmt.i_codec = es_fmt.video.i_chroma;
425 /* We need this for the h264 packetizer */
426 else if( cp->codec_id == AV_CODEC_ID_H264 && ( p_sys->fmt == av_find_input_format("flv") ||
427 p_sys->fmt == av_find_input_format("matroska") || p_sys->fmt == av_find_input_format("mp4") ) )
428 es_fmt.i_original_fourcc = VLC_FOURCC( 'a', 'v', 'c', '1' );
430 es_fmt.video.i_width = cp->width;
431 es_fmt.video.i_height = cp->height;
432 es_fmt.video.i_visible_width = es_fmt.video.i_width;
433 es_fmt.video.i_visible_height = es_fmt.video.i_height;
435 get_rotation(&es_fmt, s);
437 # warning FIXME: implement palette transmission
438 psz_type = "video";
440 AVRational rate;
441 #if (LIBAVUTIL_VERSION_MICRO < 100) /* libav */
442 # if (LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(55, 20, 0))
443 rate.num = s->time_base.num;
444 rate.den = s->time_base.den;
445 # else
446 rate.num = s->codec->time_base.num;
447 rate.den = s->codec->time_base.den;
448 # endif
449 rate.den *= __MAX( s->codec->ticks_per_frame, 1 );
450 #else /* ffmpeg */
451 rate = av_guess_frame_rate( p_sys->ic, s, NULL );
452 #endif
453 if( rate.den && rate.num )
455 es_fmt.video.i_frame_rate = rate.num;
456 es_fmt.video.i_frame_rate_base = rate.den;
459 AVRational ar;
460 #if (LIBAVUTIL_VERSION_MICRO < 100) /* libav */
461 ar.num = s->sample_aspect_ratio.num;
462 ar.den = s->sample_aspect_ratio.den;
463 #else
464 ar = av_guess_sample_aspect_ratio( p_sys->ic, s, NULL );
465 #endif
466 if( ar.num && ar.den )
468 es_fmt.video.i_sar_den = ar.den;
469 es_fmt.video.i_sar_num = ar.num;
471 break;
473 case AVMEDIA_TYPE_SUBTITLE:
474 es_format_Init( &es_fmt, SPU_ES, fcc );
475 es_fmt.i_original_fourcc = CodecTagToFourcc( cp->codec_tag );
476 if( strncmp( p_sys->ic->iformat->name, "matroska", 8 ) == 0 &&
477 cp->codec_id == AV_CODEC_ID_DVD_SUBTITLE &&
478 cp->extradata != NULL &&
479 cp->extradata_size > 0 )
481 char *psz_start;
482 char *psz_buf = malloc( cp->extradata_size + 1);
483 if( psz_buf != NULL )
485 memcpy( psz_buf, cp->extradata , cp->extradata_size );
486 psz_buf[cp->extradata_size] = '\0';
488 psz_start = strstr( psz_buf, "size:" );
489 if( psz_start &&
490 vobsub_size_parse( psz_start,
491 &es_fmt.subs.spu.i_original_frame_width,
492 &es_fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
494 msg_Dbg( p_demux, "original frame size: %dx%d",
495 es_fmt.subs.spu.i_original_frame_width,
496 es_fmt.subs.spu.i_original_frame_height );
498 else
500 msg_Warn( p_demux, "reading original frame size failed" );
503 psz_start = strstr( psz_buf, "palette:" );
504 if( psz_start &&
505 vobsub_palette_parse( psz_start, &es_fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
507 es_fmt.subs.spu.palette[0] = SPU_PALETTE_DEFINED;
508 msg_Dbg( p_demux, "vobsub palette read" );
510 else
512 msg_Warn( p_demux, "reading original palette failed" );
514 free( psz_buf );
517 else if( cp->codec_id == AV_CODEC_ID_DVB_SUBTITLE &&
518 cp->extradata_size > 3 )
520 es_fmt.subs.dvb.i_id = GetWBE( cp->extradata ) |
521 (GetWBE( cp->extradata + 2 ) << 16);
523 else if( cp->codec_id == AV_CODEC_ID_MOV_TEXT )
525 if( cp->extradata_size && (es_fmt.p_extra = malloc(cp->extradata_size)) )
527 memcpy( es_fmt.p_extra, cp->extradata, cp->extradata_size );
528 es_fmt.i_extra = cp->extradata_size;
531 psz_type = "subtitle";
532 break;
534 default:
535 es_format_Init( &es_fmt, UNKNOWN_ES, 0 );
536 es_fmt.i_original_fourcc = CodecTagToFourcc( cp->codec_tag );
537 #ifdef HAVE_AVUTIL_CODEC_ATTACHMENT
538 if( cp->codec_type == AVMEDIA_TYPE_ATTACHMENT )
540 input_attachment_t *p_attachment;
542 psz_type = "attachment";
543 if( cp->codec_id == AV_CODEC_ID_TTF )
545 AVDictionaryEntry *filename = av_dict_get( s->metadata, "filename", NULL, 0 );
546 if( filename && filename->value )
548 p_attachment = vlc_input_attachment_New(
549 filename->value, "application/x-truetype-font",
550 NULL, cp->extradata, (int)cp->extradata_size );
551 if( p_attachment )
552 TAB_APPEND( p_sys->i_attachments, p_sys->attachments,
553 p_attachment );
556 else msg_Warn( p_demux, "unsupported attachment type (%u) in avformat demux", cp->codec_id );
558 else
559 #endif
561 if( cp->codec_type == AVMEDIA_TYPE_DATA )
562 psz_type = "data";
564 msg_Warn( p_demux, "unsupported track type (%u:%u) in avformat demux", cp->codec_type, cp->codec_id );
566 break;
569 AVDictionaryEntry *language = av_dict_get( s->metadata, "language", NULL, 0 );
570 if ( language && language->value )
571 es_fmt.psz_language = strdup( language->value );
573 if( s->disposition & AV_DISPOSITION_DEFAULT )
574 es_fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN + 1000;
576 #ifdef HAVE_AVUTIL_CODEC_ATTACHMENT
577 if( cp->codec_type != AVMEDIA_TYPE_ATTACHMENT )
578 #endif
579 if( cp->codec_type != AVMEDIA_TYPE_DATA )
581 const bool b_ogg = !strcmp( p_sys->fmt->name, "ogg" );
582 const uint8_t *p_extra = cp->extradata;
583 unsigned i_extra = cp->extradata_size;
585 if( cp->codec_id == AV_CODEC_ID_THEORA && b_ogg )
587 unsigned pi_size[3];
588 const void *pp_data[3];
589 unsigned i_count;
590 for( i_count = 0; i_count < 3; i_count++ )
592 if( i_extra < 2 )
593 break;
594 pi_size[i_count] = GetWBE( p_extra );
595 pp_data[i_count] = &p_extra[2];
596 if( i_extra < pi_size[i_count] + 2 )
597 break;
599 p_extra += 2 + pi_size[i_count];
600 i_extra -= 2 + pi_size[i_count];
602 if( i_count > 0 && xiph_PackHeaders( &es_fmt.i_extra, &es_fmt.p_extra,
603 pi_size, pp_data, i_count ) )
605 es_fmt.i_extra = 0;
606 es_fmt.p_extra = NULL;
609 else if( cp->codec_id == AV_CODEC_ID_SPEEX && b_ogg )
611 const uint8_t p_dummy_comment[] = {
612 0, 0, 0, 0,
613 0, 0, 0, 0,
615 unsigned pi_size[2];
616 const void *pp_data[2];
618 pi_size[0] = i_extra;
619 pp_data[0] = p_extra;
621 pi_size[1] = sizeof(p_dummy_comment);
622 pp_data[1] = p_dummy_comment;
624 if( pi_size[0] > 0 && xiph_PackHeaders( &es_fmt.i_extra, &es_fmt.p_extra,
625 pi_size, pp_data, 2 ) )
627 es_fmt.i_extra = 0;
628 es_fmt.p_extra = NULL;
631 else if( cp->codec_id == AV_CODEC_ID_OPUS )
633 const uint8_t p_dummy_comment[] = {
634 'O', 'p', 'u', 's',
635 'T', 'a', 'g', 's',
636 0, 0, 0, 0, /* Vendor String length */
637 /* Vendor String */
638 0, 0, 0, 0, /* User Comment List Length */
641 unsigned pi_size[2];
642 const void *pp_data[2];
644 pi_size[0] = i_extra;
645 pp_data[0] = p_extra;
647 pi_size[1] = sizeof(p_dummy_comment);
648 pp_data[1] = p_dummy_comment;
650 if( pi_size[0] > 0 && xiph_PackHeaders( &es_fmt.i_extra, &es_fmt.p_extra,
651 pi_size, pp_data, 2 ) )
653 es_fmt.i_extra = 0;
654 es_fmt.p_extra = NULL;
657 else if( cp->extradata_size > 0 )
659 es_fmt.p_extra = malloc( i_extra );
660 if( es_fmt.p_extra )
662 es_fmt.i_extra = i_extra;
663 memcpy( es_fmt.p_extra, p_extra, i_extra );
667 p_track->p_es = es_out_Add( p_demux->out, &es_fmt );
668 if( p_track->p_es && (s->disposition & AV_DISPOSITION_DEFAULT) )
669 es_out_Control( p_demux->out, ES_OUT_SET_ES_DEFAULT, p_track->p_es );
671 msg_Dbg( p_demux, "adding es: %s codec = %4.4s (%d)",
672 psz_type, (char*)&fcc, cp->codec_id );
674 es_format_Clean( &es_fmt );
677 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
678 i_start_time = p_sys->ic->start_time * 1000000 / AV_TIME_BASE;
680 msg_Dbg( p_demux, "AVFormat(%s %s) supported stream", AVPROVIDER(LIBAVFORMAT), LIBAVFORMAT_IDENT );
681 msg_Dbg( p_demux, " - format = %s (%s)",
682 p_sys->fmt->name, p_sys->fmt->long_name );
683 msg_Dbg( p_demux, " - start time = %"PRId64, i_start_time );
684 msg_Dbg( p_demux, " - duration = %"PRId64,
685 ( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE ) ?
686 p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 );
688 if( p_sys->ic->nb_chapters > 0 )
690 p_sys->p_title = vlc_input_title_New();
691 p_sys->p_title->i_length = p_sys->ic->duration * 1000000 / AV_TIME_BASE;
694 for( unsigned i = 0; i < p_sys->ic->nb_chapters; i++ )
696 seekpoint_t *s = vlc_seekpoint_New();
698 AVDictionaryEntry *title = av_dict_get( p_sys->ic->metadata, "title", NULL, 0);
699 if( title && title->value )
701 s->psz_name = strdup( title->value );
702 EnsureUTF8( s->psz_name );
703 msg_Dbg( p_demux, " - chapter %d: %s", i, s->psz_name );
705 s->i_time_offset = p_sys->ic->chapters[i]->start * CLOCK_FREQ *
706 p_sys->ic->chapters[i]->time_base.num /
707 p_sys->ic->chapters[i]->time_base.den -
708 (i_start_time != -1 ? i_start_time : 0 );
709 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
712 ResetTime( p_demux, 0 );
713 return VLC_SUCCESS;
716 /*****************************************************************************
717 * Close
718 *****************************************************************************/
719 void avformat_CloseDemux( vlc_object_t *p_this )
721 demux_t *p_demux = (demux_t*)p_this;
722 demux_sys_t *p_sys = p_demux->p_sys;
724 free( p_sys->tracks );
726 if( p_sys->ic )
728 if( p_sys->ic->pb )
730 av_free( p_sys->ic->pb->buffer );
731 av_free( p_sys->ic->pb );
733 avformat_close_input( &p_sys->ic );
736 for( int i = 0; i < p_sys->i_attachments; i++ )
737 vlc_input_attachment_Delete( p_sys->attachments[i] );
738 TAB_CLEAN( p_sys->i_attachments, p_sys->attachments);
740 if( p_sys->p_title )
741 vlc_input_title_Delete( p_sys->p_title );
743 free( p_sys );
746 /*****************************************************************************
747 * Demux:
748 *****************************************************************************/
749 static int Demux( demux_t *p_demux )
751 demux_sys_t *p_sys = p_demux->p_sys;
752 AVPacket pkt;
753 block_t *p_frame;
754 int64_t i_start_time;
756 /* Read a frame */
757 int i_av_ret = av_read_frame( p_sys->ic, &pkt );
758 if( i_av_ret )
760 /* Avoid EOF if av_read_frame returns AVERROR(EAGAIN) */
761 if( i_av_ret == AVERROR(EAGAIN) )
762 return 1;
764 return 0;
766 if( pkt.stream_index < 0 || (unsigned) pkt.stream_index >= p_sys->i_tracks )
768 av_packet_unref( &pkt );
769 return 1;
771 struct avformat_track_s *p_track = &p_sys->tracks[pkt.stream_index];
772 const AVStream *p_stream = p_sys->ic->streams[pkt.stream_index];
773 if( p_stream->time_base.den <= 0 )
775 msg_Warn( p_demux, "Invalid time base for the stream %d", pkt.stream_index );
776 av_packet_unref( &pkt );
777 return 1;
779 if( p_stream->codecpar->codec_id == AV_CODEC_ID_SSA )
781 p_frame = BuildSsaFrame( &pkt, p_sys->i_ssa_order++ );
782 if( !p_frame )
784 av_packet_unref( &pkt );
785 return 1;
788 else if( p_stream->codecpar->codec_id == AV_CODEC_ID_DVB_SUBTITLE )
790 if( ( p_frame = block_Alloc( pkt.size + 3 ) ) == NULL )
792 av_packet_unref( &pkt );
793 return 0;
795 p_frame->p_buffer[0] = 0x20;
796 p_frame->p_buffer[1] = 0x00;
797 memcpy( &p_frame->p_buffer[2], pkt.data, pkt.size );
798 p_frame->p_buffer[p_frame->i_buffer - 1] = 0x3f;
800 else
802 if( ( p_frame = block_Alloc( pkt.size ) ) == NULL )
804 av_packet_unref( &pkt );
805 return 0;
807 memcpy( p_frame->p_buffer, pkt.data, pkt.size );
810 if( pkt.flags & AV_PKT_FLAG_KEY )
811 p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
813 /* Used to avoid timestamps overlow */
814 lldiv_t q;
815 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
817 q = lldiv( p_sys->ic->start_time, AV_TIME_BASE);
818 i_start_time = q.quot * CLOCK_FREQ + q.rem * CLOCK_FREQ / AV_TIME_BASE;
820 else
821 i_start_time = 0;
823 if( pkt.dts == (int64_t)AV_NOPTS_VALUE )
824 p_frame->i_dts = VLC_TS_INVALID;
825 else
827 q = lldiv( pkt.dts, p_stream->time_base.den );
828 p_frame->i_dts = q.quot * CLOCK_FREQ *
829 p_stream->time_base.num + q.rem * CLOCK_FREQ *
830 p_stream->time_base.num /
831 p_stream->time_base.den - i_start_time + VLC_TS_0;
834 if( pkt.pts == (int64_t)AV_NOPTS_VALUE )
835 p_frame->i_pts = VLC_TS_INVALID;
836 else
838 q = lldiv( pkt.pts, p_stream->time_base.den );
839 p_frame->i_pts = q.quot * CLOCK_FREQ *
840 p_stream->time_base.num + q.rem * CLOCK_FREQ *
841 p_stream->time_base.num /
842 p_stream->time_base.den - i_start_time + VLC_TS_0;
844 if( pkt.duration > 0 && p_frame->i_length <= 0 )
845 p_frame->i_length = pkt.duration * CLOCK_FREQ *
846 p_stream->time_base.num /
847 p_stream->time_base.den;
849 /* Add here notoriously bugged file formats/samples */
850 if( !strcmp( p_sys->fmt->name, "flv" ) )
852 /* FLV and video PTS */
853 if( p_stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
854 pkt.dts != (int64_t)AV_NOPTS_VALUE && pkt.dts == pkt.pts )
855 p_frame->i_pts = VLC_TS_INVALID;
857 /* Handle broken dts/pts increase with AAC. Duration is correct.
858 * sky_the80s_aacplus.flv #8195 */
859 if( p_stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
860 p_stream->codecpar->codec_id == AV_CODEC_ID_AAC )
862 if( p_track->i_pcr != VLC_TS_INVALID &&
863 p_track->i_pcr + p_frame->i_length > p_frame->i_dts )
865 p_frame->i_dts = p_frame->i_pts = p_track->i_pcr + p_frame->i_length;
869 #ifdef AVFORMAT_DEBUG
870 msg_Dbg( p_demux, "tk[%d] dts=%"PRId64" pts=%"PRId64,
871 pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
872 #endif
873 if( p_frame->i_dts != VLC_TS_INVALID && p_track->p_es != NULL )
874 p_track->i_pcr = p_frame->i_dts;
876 vlc_tick_t i_ts_max = INT64_MIN;
877 for( unsigned i = 0; i < p_sys->i_tracks; i++ )
879 if( p_sys->tracks[i].p_es != NULL )
880 i_ts_max = __MAX( i_ts_max, p_sys->tracks[i].i_pcr );
883 vlc_tick_t i_ts_min = INT64_MAX;
884 for( unsigned i = 0; i < p_sys->i_tracks; i++ )
886 if( p_sys->tracks[i].p_es != NULL &&
887 p_sys->tracks[i].i_pcr != VLC_TS_INVALID &&
888 p_sys->tracks[i].i_pcr + 10 * CLOCK_FREQ >= i_ts_max )
889 i_ts_min = __MIN( i_ts_min, p_sys->tracks[i].i_pcr );
891 if( i_ts_min >= p_sys->i_pcr && likely(i_ts_min != INT64_MAX) )
893 p_sys->i_pcr = i_ts_min;
894 es_out_SetPCR( p_demux->out, p_sys->i_pcr );
895 UpdateSeekPoint( p_demux, p_sys->i_pcr );
898 if( p_track->p_es != NULL )
899 es_out_Send( p_demux->out, p_track->p_es, p_frame );
900 else
901 block_Release( p_frame );
903 av_packet_unref( &pkt );
904 return 1;
907 static void UpdateSeekPoint( demux_t *p_demux, int64_t i_time )
909 demux_sys_t *p_sys = p_demux->p_sys;
910 int i;
912 if( !p_sys->p_title )
913 return;
915 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
917 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
918 break;
920 i--;
922 if( i != p_sys->i_seekpoint && i >= 0 )
924 p_sys->i_seekpoint = i;
925 p_sys->i_update |= INPUT_UPDATE_SEEKPOINT;
929 static void ResetTime( demux_t *p_demux, vlc_tick_t i_time )
931 demux_sys_t *p_sys = p_demux->p_sys;
933 if( p_sys->ic->start_time == (int64_t)AV_NOPTS_VALUE || i_time < 0 )
934 i_time = VLC_TS_INVALID;
935 else if( i_time == 0 )
936 i_time = 1;
938 p_sys->i_pcr = i_time;
939 for( unsigned i = 0; i < p_sys->i_tracks; i++ )
940 p_sys->tracks[i].i_pcr = VLC_TS_INVALID;
942 if( i_time != VLC_TS_INVALID )
944 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_time );
945 UpdateSeekPoint( p_demux, i_time );
949 static block_t *BuildSsaFrame( const AVPacket *p_pkt, unsigned i_order )
951 if( p_pkt->size <= 0 )
952 return NULL;
954 char buffer[256];
955 const size_t i_buffer_size = __MIN( (int)sizeof(buffer) - 1, p_pkt->size );
956 memcpy( buffer, p_pkt->data, i_buffer_size );
957 buffer[i_buffer_size] = '\0';
959 /* */
960 int i_layer;
961 int h0, m0, s0, c0;
962 int h1, m1, s1, c1;
963 int i_position = 0;
964 if( sscanf( buffer, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,%n", &i_layer,
965 &h0, &m0, &s0, &c0, &h1, &m1, &s1, &c1, &i_position ) < 9 )
966 return NULL;
967 if( i_position <= 0 || (unsigned)i_position >= i_buffer_size )
968 return NULL;
970 char *p;
971 if( asprintf( &p, "%u,%d,%.*s", i_order, i_layer, p_pkt->size - i_position, p_pkt->data + i_position ) < 0 )
972 return NULL;
974 block_t *p_frame = block_heap_Alloc( p, strlen(p) + 1 );
975 if( p_frame )
976 p_frame->i_length = CLOCK_FREQ * ((h1-h0) * 3600 +
977 (m1-m0) * 60 +
978 (s1-s0) * 1) +
979 CLOCK_FREQ * (c1-c0) / 100;
980 return p_frame;
983 /*****************************************************************************
984 * Control:
985 *****************************************************************************/
986 static int Control( demux_t *p_demux, int i_query, va_list args )
988 demux_sys_t *p_sys = p_demux->p_sys;
989 const int64_t i_start_time = p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ? p_sys->ic->start_time : 0;
990 double f, *pf;
991 int64_t i64, *pi64;
993 switch( i_query )
995 case DEMUX_CAN_SEEK:
996 *va_arg( args, bool * ) = true;
997 return VLC_SUCCESS;
999 case DEMUX_GET_POSITION:
1000 pf = va_arg( args, double * ); *pf = 0.0;
1001 i64 = stream_Size( p_demux->s );
1002 if( i64 > 0 )
1004 double current = vlc_stream_Tell( p_demux->s );
1005 *pf = current / (double)i64;
1008 if( (p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE) && (p_sys->i_pcr > 0) )
1010 *pf = (double)p_sys->i_pcr / (double)p_sys->ic->duration;
1013 return VLC_SUCCESS;
1015 case DEMUX_SET_POSITION:
1017 f = va_arg( args, double );
1018 bool precise = va_arg( args, int );
1019 i64 = p_sys->ic->duration * f + i_start_time;
1021 msg_Warn( p_demux, "DEMUX_SET_POSITION: %"PRId64, i64 );
1023 /* If we have a duration, we prefer to seek by time
1024 but if we don't, or if the seek fails, try BYTE seeking */
1025 if( p_sys->ic->duration == (int64_t)AV_NOPTS_VALUE ||
1026 (av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BACKWARD ) < 0) )
1028 int64_t i_size = stream_Size( p_demux->s );
1029 i64 = (i_size * f);
1031 msg_Warn( p_demux, "DEMUX_SET_BYTE_POSITION: %"PRId64, i64 );
1032 if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BYTE ) < 0 )
1033 return VLC_EGENERIC;
1035 ResetTime( p_demux, -1 );
1037 else
1039 if( precise )
1040 ResetTime( p_demux, i64 - i_start_time );
1041 else
1042 ResetTime( p_demux, -1 );
1044 return VLC_SUCCESS;
1047 case DEMUX_GET_LENGTH:
1048 pi64 = va_arg( args, int64_t * );
1049 if( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE )
1050 *pi64 = p_sys->ic->duration * CLOCK_FREQ / AV_TIME_BASE;
1051 else
1052 *pi64 = 0;
1053 return VLC_SUCCESS;
1055 case DEMUX_GET_TIME:
1056 pi64 = va_arg( args, int64_t * );
1057 *pi64 = p_sys->i_pcr;
1058 return VLC_SUCCESS;
1060 case DEMUX_SET_TIME:
1062 i64 = va_arg( args, int64_t );
1063 bool precise = va_arg( args, int );
1064 i64 = i64 * AV_TIME_BASE / CLOCK_FREQ + i_start_time;
1066 msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
1068 if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BACKWARD ) < 0 )
1070 return VLC_EGENERIC;
1072 if( precise )
1073 ResetTime( p_demux, i64 - i_start_time );
1074 else
1075 ResetTime( p_demux, -1 );
1076 return VLC_SUCCESS;
1079 case DEMUX_HAS_UNSUPPORTED_META:
1081 bool *pb_bool = va_arg( args, bool* );
1082 *pb_bool = true;
1083 return VLC_SUCCESS;
1087 case DEMUX_GET_META:
1089 static const char names[][10] = {
1090 [vlc_meta_Title] = "title",
1091 [vlc_meta_Artist] = "artist",
1092 [vlc_meta_Genre] = "genre",
1093 [vlc_meta_Copyright] = "copyright",
1094 [vlc_meta_Album] = "album",
1095 //[vlc_meta_TrackNumber] -- TODO: parse number/total value
1096 [vlc_meta_Description] = "comment",
1097 //[vlc_meta_Rating]
1098 [vlc_meta_Date] = "date",
1099 [vlc_meta_Setting] = "encoder",
1100 //[vlc_meta_URL]
1101 [vlc_meta_Language] = "language",
1102 //[vlc_meta_NowPlaying]
1103 [vlc_meta_Publisher] = "publisher",
1104 [vlc_meta_EncodedBy] = "encoded_by",
1105 //[vlc_meta_ArtworkURL]
1106 //[vlc_meta_TrackID]
1107 //[vlc_meta_TrackTotal]
1109 vlc_meta_t *p_meta = va_arg( args, vlc_meta_t * );
1110 AVDictionary *dict = p_sys->ic->metadata;
1112 for( unsigned i = 0; i < sizeof(names) / sizeof(*names); i++)
1114 if( !names[i][0] )
1115 continue;
1117 AVDictionaryEntry *e = av_dict_get( dict, names[i], NULL, 0 );
1118 if( e != NULL && e->value != NULL && IsUTF8(e->value) )
1119 vlc_meta_Set( p_meta, i, e->value );
1121 return VLC_SUCCESS;
1124 case DEMUX_GET_ATTACHMENTS:
1126 input_attachment_t ***ppp_attach =
1127 va_arg( args, input_attachment_t*** );
1128 int *pi_int = va_arg( args, int * );
1129 int i;
1131 if( p_sys->i_attachments <= 0 )
1132 return VLC_EGENERIC;
1134 *ppp_attach = vlc_alloc( p_sys->i_attachments, sizeof(input_attachment_t*) );
1135 if( *ppp_attach == NULL )
1136 return VLC_EGENERIC;
1138 for( i = 0; i < p_sys->i_attachments; i++ )
1140 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
1141 if((*ppp_attach)[i] == NULL)
1142 break;
1144 *pi_int = i;
1145 return VLC_SUCCESS;
1148 case DEMUX_GET_TITLE_INFO:
1150 input_title_t ***ppp_title = va_arg( args, input_title_t *** );
1151 int *pi_int = va_arg( args, int * );
1152 int *pi_title_offset = va_arg( args, int * );
1153 int *pi_seekpoint_offset = va_arg( args, int * );
1155 if( !p_sys->p_title )
1156 return VLC_EGENERIC;
1158 *ppp_title = malloc( sizeof( input_title_t*) );
1159 if( *ppp_title == NULL )
1160 return VLC_EGENERIC;
1161 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
1162 *pi_int = (*ppp_title)[0] ? 1 : 0;
1163 *pi_title_offset = 0;
1164 *pi_seekpoint_offset = 0;
1165 return VLC_SUCCESS;
1167 case DEMUX_SET_TITLE:
1169 const int i_title = va_arg( args, int );
1170 if( !p_sys->p_title || i_title != 0 )
1171 return VLC_EGENERIC;
1172 return VLC_SUCCESS;
1174 case DEMUX_SET_SEEKPOINT:
1176 const int i_seekpoint = va_arg( args, int );
1177 if( !p_sys->p_title )
1178 return VLC_EGENERIC;
1180 i64 = p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset *
1181 AV_TIME_BASE / CLOCK_FREQ + i_start_time;
1183 msg_Warn( p_demux, "DEMUX_SET_SEEKPOINT: %"PRId64, i64 );
1185 if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BACKWARD ) < 0 )
1187 return VLC_EGENERIC;
1189 ResetTime( p_demux, i64 - i_start_time );
1190 return VLC_SUCCESS;
1192 case DEMUX_TEST_AND_CLEAR_FLAGS:
1194 unsigned *restrict flags = va_arg(args, unsigned *);
1195 *flags &= p_sys->i_update;
1196 p_sys->i_update &= ~*flags;
1197 return VLC_SUCCESS;
1199 case DEMUX_GET_TITLE:
1200 if( p_sys->p_title == NULL )
1201 return VLC_EGENERIC;
1202 *va_arg( args, int * ) = 0;
1203 return VLC_SUCCESS;
1204 case DEMUX_GET_SEEKPOINT:
1205 if( p_sys->p_title == NULL )
1206 return VLC_EGENERIC;
1207 *va_arg( args, int * ) = p_sys->i_seekpoint;
1208 return VLC_SUCCESS;
1209 case DEMUX_CAN_PAUSE:
1210 case DEMUX_SET_PAUSE_STATE:
1211 case DEMUX_CAN_CONTROL_PACE:
1212 case DEMUX_GET_PTS_DELAY:
1213 return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
1214 default:
1215 return VLC_EGENERIC;
1219 /*****************************************************************************
1220 * I/O wrappers for libavformat
1221 *****************************************************************************/
1222 static int IORead( void *opaque, uint8_t *buf, int buf_size )
1224 demux_t *p_demux = opaque;
1225 if( buf_size < 0 ) return -1;
1226 int i_ret = vlc_stream_Read( p_demux->s, buf, buf_size );
1227 return i_ret >= 0 ? i_ret : -1;
1230 static int64_t IOSeek( void *opaque, int64_t offset, int whence )
1232 demux_t *p_demux = opaque;
1233 int64_t i_absolute;
1234 int64_t i_size = stream_Size( p_demux->s );
1236 #ifdef AVFORMAT_DEBUG
1237 msg_Warn( p_demux, "IOSeek offset: %"PRId64", whence: %i", offset, whence );
1238 #endif
1240 switch( whence )
1242 #ifdef AVSEEK_SIZE
1243 case AVSEEK_SIZE:
1244 return i_size;
1245 #endif
1246 case SEEK_SET:
1247 i_absolute = (int64_t)offset;
1248 break;
1249 case SEEK_CUR:
1250 i_absolute = vlc_stream_Tell( p_demux->s ) + (int64_t)offset;
1251 break;
1252 case SEEK_END:
1253 i_absolute = i_size + (int64_t)offset;
1254 break;
1255 default:
1256 return -1;
1260 if( i_absolute < 0 )
1262 msg_Dbg( p_demux, "Trying to seek before the beginning" );
1263 return -1;
1266 if( i_size > 0 && i_absolute >= i_size )
1268 msg_Dbg( p_demux, "Trying to seek too far : EOF?" );
1269 return -1;
1272 if( vlc_stream_Seek( p_demux->s, i_absolute ) )
1274 msg_Warn( p_demux, "we were not allowed to seek, or EOF " );
1275 return -1;
1278 return vlc_stream_Tell( p_demux->s );