demux: avformat: do not take account of invalid streams (e.g., data_es) for PCR updating
[vlc.git] / modules / demux / avformat / demux.c
blobafaf7ad90e418f5471f1007b307da90799aae23d
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 /*****************************************************************************
56 * demux_sys_t: demux descriptor
57 *****************************************************************************/
58 struct demux_sys_t
60 AVInputFormat *fmt;
61 AVFormatContext *ic;
63 int i_tk;
64 es_out_id_t **tk;
65 int64_t *tk_pcr;
66 int64_t i_pcr;
68 unsigned i_ssa_order;
70 int i_attachments;
71 input_attachment_t **attachments;
73 /* Only one title with seekpoints possible atm. */
74 input_title_t *p_title;
77 #define AVFORMAT_IOBUFFER_SIZE 32768 /* FIXME */
79 /*****************************************************************************
80 * Local prototypes
81 *****************************************************************************/
82 static int Demux ( demux_t *p_demux );
83 static int Control( demux_t *p_demux, int i_query, va_list args );
85 static int IORead( void *opaque, uint8_t *buf, int buf_size );
86 static int64_t IOSeek( void *opaque, int64_t offset, int whence );
88 static block_t *BuildSsaFrame( const AVPacket *p_pkt, unsigned i_order );
89 static void UpdateSeekPoint( demux_t *p_demux, int64_t i_time );
90 static void ResetTime( demux_t *p_demux, int64_t i_time );
92 static vlc_fourcc_t CodecTagToFourcc( uint32_t codec_tag )
94 // convert from little-endian avcodec codec_tag to VLC native-endian fourcc
95 #ifdef WORDS_BIGENDIAN
96 return bswap32(codec_tag);
97 #else
98 return codec_tag;
99 #endif
102 /*****************************************************************************
103 * Open
104 *****************************************************************************/
106 static void get_rotation(es_format_t *fmt, AVStream *s)
108 char const *kRotateKey = "rotate";
109 AVDictionaryEntry *rotation = av_dict_get(s->metadata, kRotateKey, NULL, 0);
110 long angle = 0;
112 if( rotation )
114 angle = strtol(rotation->value, NULL, 10);
116 if (angle > 45 && angle < 135)
117 fmt->video.orientation = ORIENT_ROTATED_90;
119 else if (angle > 135 && angle < 225)
120 fmt->video.orientation = ORIENT_ROTATED_180;
122 else if (angle > 225 && angle < 315)
123 fmt->video.orientation = ORIENT_ROTATED_270;
125 else
126 fmt->video.orientation = ORIENT_NORMAL;
128 int32_t *matrix = (int32_t *)av_stream_get_side_data(s, AV_PKT_DATA_DISPLAYMATRIX, NULL);
129 if( matrix ) {
130 angle = lround(av_display_rotation_get(matrix));
132 if (angle > 45 && angle < 135)
133 fmt->video.orientation = ORIENT_ROTATED_270;
135 else if (angle > 135 || angle < -135)
136 fmt->video.orientation = ORIENT_ROTATED_180;
138 else if (angle < -45 && angle > -135)
139 fmt->video.orientation = ORIENT_ROTATED_90;
141 else
142 fmt->video.orientation = ORIENT_NORMAL;
146 int OpenDemux( vlc_object_t *p_this )
148 demux_t *p_demux = (demux_t*)p_this;
149 demux_sys_t *p_sys;
150 AVProbeData pd = { };
151 AVInputFormat *fmt = NULL;
152 int64_t i_start_time = -1;
153 bool b_can_seek;
154 char *psz_url;
155 const uint8_t *peek;
156 int error;
158 /* Init Probe data */
159 pd.buf_size = vlc_stream_Peek( p_demux->s, &peek, 2048 + 213 );
160 if( pd.buf_size <= 0 )
162 msg_Warn( p_demux, "cannot peek" );
163 return VLC_EGENERIC;
166 pd.buf = malloc( pd.buf_size + AVPROBE_PADDING_SIZE );
167 if( unlikely(pd.buf == NULL) )
168 return VLC_ENOMEM;
170 memcpy( pd.buf, peek, pd.buf_size );
171 memset( pd.buf + pd.buf_size, 0, AVPROBE_PADDING_SIZE );
173 if( p_demux->psz_file )
174 psz_url = strdup( p_demux->psz_file );
175 else
177 if( asprintf( &psz_url, "%s://%s", p_demux->psz_access,
178 p_demux->psz_location ) == -1)
179 psz_url = NULL;
182 if( psz_url != NULL )
183 msg_Dbg( p_demux, "trying url: %s", psz_url );
185 pd.filename = psz_url;
187 vlc_stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_can_seek );
189 vlc_init_avformat(p_this);
191 /* Guess format */
192 char *psz_format = var_InheritString( p_this, "avformat-format" );
193 if( psz_format )
195 if( (fmt = av_find_input_format(psz_format)) )
196 msg_Dbg( p_demux, "forcing format: %s", fmt->name );
197 free( psz_format );
200 if( fmt == NULL )
201 fmt = av_probe_input_format( &pd, 1 );
203 free( pd.buf );
205 if( fmt == NULL )
207 msg_Dbg( p_demux, "couldn't guess format" );
208 free( psz_url );
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] ) )
230 free( psz_url );
231 return VLC_EGENERIC;
236 /* Don't trigger false alarms on bin files */
237 if( !p_demux->obj.force && !strcmp( fmt->name, "psxstr" ) )
239 int i_len;
241 if( !p_demux->psz_file )
243 free( psz_url );
244 return VLC_EGENERIC;
247 i_len = strlen( p_demux->psz_file );
248 if( i_len < 4 )
250 free( psz_url );
251 return VLC_EGENERIC;
254 if( strcasecmp( &p_demux->psz_file[i_len - 4], ".str" ) &&
255 strcasecmp( &p_demux->psz_file[i_len - 4], ".xai" ) &&
256 strcasecmp( &p_demux->psz_file[i_len - 3], ".xa" ) )
258 free( psz_url );
259 return VLC_EGENERIC;
263 msg_Dbg( p_demux, "detected format: %s", fmt->name );
265 /* Fill p_demux fields */
266 p_demux->pf_demux = Demux;
267 p_demux->pf_control = Control;
268 p_demux->p_sys = p_sys = xmalloc( sizeof( demux_sys_t ) );
269 p_sys->ic = 0;
270 p_sys->fmt = fmt;
271 p_sys->i_tk = 0;
272 p_sys->tk = NULL;
273 p_sys->tk_pcr = NULL;
274 p_sys->i_ssa_order = 0;
275 TAB_INIT( p_sys->i_attachments, p_sys->attachments);
276 p_sys->p_title = NULL;
278 /* Create I/O wrapper */
279 unsigned char * p_io_buffer = av_malloc( AVFORMAT_IOBUFFER_SIZE );
280 if( !p_io_buffer )
282 free( psz_url );
283 CloseDemux( p_this );
284 return VLC_ENOMEM;
287 p_sys->ic = avformat_alloc_context();
288 if( !p_sys->ic )
290 av_free( p_io_buffer );
291 free( psz_url );
292 CloseDemux( p_this );
293 return VLC_ENOMEM;
296 AVIOContext *pb = p_sys->ic->pb = avio_alloc_context( p_io_buffer,
297 AVFORMAT_IOBUFFER_SIZE, 0, p_demux, IORead, NULL, IOSeek );
298 if( !pb )
300 av_free( p_io_buffer );
301 free( psz_url );
302 CloseDemux( p_this );
303 return VLC_ENOMEM;
306 p_sys->ic->pb->seekable = b_can_seek ? AVIO_SEEKABLE_NORMAL : 0;
307 error = avformat_open_input(&p_sys->ic, psz_url, p_sys->fmt, NULL);
309 if( error < 0 )
311 msg_Err( p_demux, "Could not open %s: %s", psz_url,
312 vlc_strerror_c(AVUNERROR(error)) );
313 av_free( pb->buffer );
314 av_free( pb );
315 p_sys->ic = NULL;
316 free( psz_url );
317 CloseDemux( p_this );
318 return VLC_EGENERIC;
320 free( psz_url );
322 char *psz_opts = var_InheritString( p_demux, "avformat-options" );
323 AVDictionary *options[p_sys->ic->nb_streams ? p_sys->ic->nb_streams : 1];
324 options[0] = NULL;
325 unsigned int nb_streams = p_sys->ic->nb_streams;
326 for (unsigned i = 1; i < nb_streams; i++)
327 options[i] = NULL;
328 if (psz_opts) {
329 vlc_av_get_options(psz_opts, &options[0]);
330 for (unsigned i = 1; i < nb_streams; i++) {
331 av_dict_copy(&options[i], options[0], 0);
333 free(psz_opts);
335 vlc_avcodec_lock(); /* avformat calls avcodec behind our back!!! */
336 error = avformat_find_stream_info( p_sys->ic, options );
337 /* FIXME: what if nb_streams change after that call? */
338 vlc_avcodec_unlock();
339 AVDictionaryEntry *t = NULL;
340 while ((t = av_dict_get(options[0], "", t, AV_DICT_IGNORE_SUFFIX))) {
341 msg_Err( p_demux, "Unknown option \"%s\"", t->key );
343 av_dict_free(&options[0]);
344 for (unsigned i = 1; i < nb_streams; i++) {
345 av_dict_free(&options[i]);
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 < p_sys->ic->nb_streams; i++ )
356 AVStream *s = p_sys->ic->streams[i];
357 const AVCodecParameters *cp = s->codecpar;
358 es_out_id_t *es = NULL;
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 )
365 TAB_APPEND( p_sys->i_tk, p_sys->tk, NULL );
366 continue;
369 vlc_fourcc_t fcc = GetVlcFourcc( cp->codec_id );
370 switch( cp->codec_type )
372 case AVMEDIA_TYPE_AUDIO:
373 es_format_Init( &es_fmt, AUDIO_ES, fcc );
374 es_fmt.i_original_fourcc = CodecTagToFourcc( cp->codec_tag );
375 es_fmt.i_bitrate = cp->bit_rate;
376 es_fmt.audio.i_channels = cp->channels;
377 es_fmt.audio.i_rate = cp->sample_rate;
378 es_fmt.audio.i_bitspersample = cp->bits_per_coded_sample;
379 es_fmt.audio.i_blockalign = cp->block_align;
380 psz_type = "audio";
382 if(cp->codec_id == AV_CODEC_ID_AAC_LATM)
384 es_fmt.i_original_fourcc = VLC_FOURCC('L','A','T','M');
385 es_fmt.b_packetized = false;
387 else if(cp->codec_id == AV_CODEC_ID_AAC &&
388 strstr(p_sys->fmt->long_name, "raw ADTS AAC"))
390 es_fmt.i_original_fourcc = VLC_FOURCC('A','D','T','S');
391 es_fmt.b_packetized = false;
393 break;
395 case AVMEDIA_TYPE_VIDEO:
396 es_format_Init( &es_fmt, VIDEO_ES, fcc );
397 es_fmt.i_original_fourcc = CodecTagToFourcc( cp->codec_tag );
399 es_fmt.video.i_bits_per_pixel = cp->bits_per_coded_sample;
400 /* Special case for raw video data */
401 if( cp->codec_id == AV_CODEC_ID_RAWVIDEO )
403 msg_Dbg( p_demux, "raw video, pixel format: %i", cp->format );
404 if( GetVlcChroma( &es_fmt.video, cp->format ) != VLC_SUCCESS)
406 msg_Err( p_demux, "was unable to find a FourCC match for raw video" );
408 else
409 es_fmt.i_codec = es_fmt.video.i_chroma;
411 /* We need this for the h264 packetizer */
412 else if( cp->codec_id == AV_CODEC_ID_H264 && ( p_sys->fmt == av_find_input_format("flv") ||
413 p_sys->fmt == av_find_input_format("matroska") || p_sys->fmt == av_find_input_format("mp4") ) )
414 es_fmt.i_original_fourcc = VLC_FOURCC( 'a', 'v', 'c', '1' );
416 es_fmt.video.i_width = cp->width;
417 es_fmt.video.i_height = cp->height;
418 es_fmt.video.i_visible_width = es_fmt.video.i_width;
419 es_fmt.video.i_visible_height = es_fmt.video.i_height;
421 get_rotation(&es_fmt, s);
423 # warning FIXME: implement palette transmission
424 psz_type = "video";
425 es_fmt.video.i_frame_rate = s->codec->time_base.num;
426 es_fmt.video.i_frame_rate_base = s->codec->time_base.den * __MAX( s->codec->ticks_per_frame, 1 );
427 es_fmt.video.i_sar_num = s->sample_aspect_ratio.num;
428 if (s->sample_aspect_ratio.num > 0)
429 es_fmt.video.i_sar_den = s->sample_aspect_ratio.den;
430 else
431 es_fmt.video.i_sar_den = 0;
432 break;
434 case AVMEDIA_TYPE_SUBTITLE:
435 es_format_Init( &es_fmt, SPU_ES, fcc );
436 es_fmt.i_original_fourcc = CodecTagToFourcc( cp->codec_tag );
437 if( strncmp( p_sys->ic->iformat->name, "matroska", 8 ) == 0 &&
438 cp->codec_id == AV_CODEC_ID_DVD_SUBTITLE &&
439 cp->extradata != NULL &&
440 cp->extradata_size > 0 )
442 char *psz_start;
443 char *psz_buf = malloc( cp->extradata_size + 1);
444 if( psz_buf != NULL )
446 memcpy( psz_buf, cp->extradata , cp->extradata_size );
447 psz_buf[cp->extradata_size] = '\0';
449 psz_start = strstr( psz_buf, "size:" );
450 if( psz_start &&
451 vobsub_size_parse( psz_start,
452 &es_fmt.subs.spu.i_original_frame_width,
453 &es_fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
455 msg_Dbg( p_demux, "original frame size: %dx%d",
456 es_fmt.subs.spu.i_original_frame_width,
457 es_fmt.subs.spu.i_original_frame_height );
459 else
461 msg_Warn( p_demux, "reading original frame size failed" );
464 psz_start = strstr( psz_buf, "palette:" );
465 if( psz_start &&
466 vobsub_palette_parse( psz_start, &es_fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
468 es_fmt.subs.spu.palette[0] = SPU_PALETTE_DEFINED;
469 msg_Dbg( p_demux, "vobsub palette read" );
471 else
473 msg_Warn( p_demux, "reading original palette failed" );
475 free( psz_buf );
478 else if( !strcmp( p_sys->ic->iformat->name, "wtv" ) &&
479 cp->codec_id == AV_CODEC_ID_DVB_SUBTITLE &&
480 cp->extradata_size > 3 )
482 es_fmt.subs.dvb.i_id = GetWBE( cp->extradata ) |
483 (GetWBE( cp->extradata + 2 ) << 16);
486 psz_type = "subtitle";
487 break;
489 default:
490 es_format_Init( &es_fmt, UNKNOWN_ES, 0 );
491 es_fmt.i_original_fourcc = CodecTagToFourcc( cp->codec_tag );
492 #ifdef HAVE_AVUTIL_CODEC_ATTACHMENT
493 if( cp->codec_type == AVMEDIA_TYPE_ATTACHMENT )
495 input_attachment_t *p_attachment;
497 psz_type = "attachment";
498 if( cp->codec_id == AV_CODEC_ID_TTF )
500 AVDictionaryEntry *filename = av_dict_get( s->metadata, "filename", NULL, 0 );
501 if( filename && filename->value )
503 p_attachment = vlc_input_attachment_New(
504 filename->value, "application/x-truetype-font",
505 NULL, cp->extradata, (int)cp->extradata_size );
506 if( p_attachment )
507 TAB_APPEND( p_sys->i_attachments, p_sys->attachments,
508 p_attachment );
511 else msg_Warn( p_demux, "unsupported attachment type (%u) in avformat demux", cp->codec_id );
513 else
514 #endif
516 if( cp->codec_type == AVMEDIA_TYPE_DATA )
517 psz_type = "data";
519 msg_Warn( p_demux, "unsupported track type (%u:%u) in avformat demux", cp->codec_type, cp->codec_id );
521 break;
524 AVDictionaryEntry *language = av_dict_get( s->metadata, "language", NULL, 0 );
525 if ( language && language->value )
526 es_fmt.psz_language = strdup( language->value );
528 if( s->disposition & AV_DISPOSITION_DEFAULT )
529 es_fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN + 1000;
531 #ifdef HAVE_AVUTIL_CODEC_ATTACHMENT
532 if( cp->codec_type != AVMEDIA_TYPE_ATTACHMENT )
533 #endif
534 if( cp->codec_type != AVMEDIA_TYPE_DATA )
536 const bool b_ogg = !strcmp( p_sys->fmt->name, "ogg" );
537 const uint8_t *p_extra = cp->extradata;
538 unsigned i_extra = cp->extradata_size;
540 if( cp->codec_id == AV_CODEC_ID_THEORA && b_ogg )
542 unsigned pi_size[3];
543 const void *pp_data[3];
544 unsigned i_count;
545 for( i_count = 0; i_count < 3; i_count++ )
547 if( i_extra < 2 )
548 break;
549 pi_size[i_count] = GetWBE( p_extra );
550 pp_data[i_count] = &p_extra[2];
551 if( i_extra < pi_size[i_count] + 2 )
552 break;
554 p_extra += 2 + pi_size[i_count];
555 i_extra -= 2 + pi_size[i_count];
557 if( i_count > 0 && xiph_PackHeaders( &es_fmt.i_extra, &es_fmt.p_extra,
558 pi_size, pp_data, i_count ) )
560 es_fmt.i_extra = 0;
561 es_fmt.p_extra = NULL;
564 else if( cp->codec_id == AV_CODEC_ID_SPEEX && b_ogg )
566 const uint8_t p_dummy_comment[] = {
567 0, 0, 0, 0,
568 0, 0, 0, 0,
570 unsigned pi_size[2];
571 const void *pp_data[2];
573 pi_size[0] = i_extra;
574 pp_data[0] = p_extra;
576 pi_size[1] = sizeof(p_dummy_comment);
577 pp_data[1] = p_dummy_comment;
579 if( pi_size[0] > 0 && xiph_PackHeaders( &es_fmt.i_extra, &es_fmt.p_extra,
580 pi_size, pp_data, 2 ) )
582 es_fmt.i_extra = 0;
583 es_fmt.p_extra = NULL;
586 else if( cp->codec_id == AV_CODEC_ID_OPUS )
588 const uint8_t p_dummy_comment[] = {
589 'O', 'p', 'u', 's',
590 'T', 'a', 'g', 's',
591 0, 0, 0, 0, /* Vendor String length */
592 /* Vendor String */
593 0, 0, 0, 0, /* User Comment List Length */
596 unsigned pi_size[2];
597 const void *pp_data[2];
599 pi_size[0] = i_extra;
600 pp_data[0] = p_extra;
602 pi_size[1] = sizeof(p_dummy_comment);
603 pp_data[1] = p_dummy_comment;
605 if( pi_size[0] > 0 && xiph_PackHeaders( &es_fmt.i_extra, &es_fmt.p_extra,
606 pi_size, pp_data, 2 ) )
608 es_fmt.i_extra = 0;
609 es_fmt.p_extra = NULL;
612 else if( cp->extradata_size > 0 )
614 es_fmt.p_extra = malloc( i_extra );
615 if( es_fmt.p_extra )
617 es_fmt.i_extra = i_extra;
618 memcpy( es_fmt.p_extra, p_extra, i_extra );
621 es = es_out_Add( p_demux->out, &es_fmt );
622 if( s->disposition & AV_DISPOSITION_DEFAULT )
623 es_out_Control( p_demux->out, ES_OUT_SET_ES_DEFAULT, es );
625 msg_Dbg( p_demux, "adding es: %s codec = %4.4s (%d)",
626 psz_type, (char*)&fcc, cp->codec_id );
628 es_format_Clean( &es_fmt );
629 TAB_APPEND( p_sys->i_tk, p_sys->tk, es );
631 p_sys->tk_pcr = xcalloc( p_sys->i_tk, sizeof(*p_sys->tk_pcr) );
633 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
634 i_start_time = p_sys->ic->start_time * 1000000 / AV_TIME_BASE;
636 msg_Dbg( p_demux, "AVFormat(%s %s) supported stream", AVPROVIDER(LIBAVFORMAT), LIBAVFORMAT_IDENT );
637 msg_Dbg( p_demux, " - format = %s (%s)",
638 p_sys->fmt->name, p_sys->fmt->long_name );
639 msg_Dbg( p_demux, " - start time = %"PRId64, i_start_time );
640 msg_Dbg( p_demux, " - duration = %"PRId64,
641 ( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE ) ?
642 p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 );
644 if( p_sys->ic->nb_chapters > 0 )
646 p_sys->p_title = vlc_input_title_New();
647 p_sys->p_title->i_length = p_sys->ic->duration * 1000000 / AV_TIME_BASE;
650 for( unsigned i = 0; i < p_sys->ic->nb_chapters; i++ )
652 seekpoint_t *s = vlc_seekpoint_New();
654 AVDictionaryEntry *title = av_dict_get( p_sys->ic->metadata, "title", NULL, 0);
655 if( title && title->value )
657 s->psz_name = strdup( title->value );
658 EnsureUTF8( s->psz_name );
659 msg_Dbg( p_demux, " - chapter %d: %s", i, s->psz_name );
661 s->i_time_offset = p_sys->ic->chapters[i]->start * 1000000 *
662 p_sys->ic->chapters[i]->time_base.num /
663 p_sys->ic->chapters[i]->time_base.den -
664 (i_start_time != -1 ? i_start_time : 0 );
665 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
668 ResetTime( p_demux, 0 );
669 return VLC_SUCCESS;
672 /*****************************************************************************
673 * Close
674 *****************************************************************************/
675 void CloseDemux( vlc_object_t *p_this )
677 demux_t *p_demux = (demux_t*)p_this;
678 demux_sys_t *p_sys = p_demux->p_sys;
680 free( p_sys->tk );
681 free( p_sys->tk_pcr );
683 if( p_sys->ic )
685 if( p_sys->ic->pb )
687 av_free( p_sys->ic->pb->buffer );
688 av_free( p_sys->ic->pb );
690 avformat_close_input( &p_sys->ic );
693 for( int i = 0; i < p_sys->i_attachments; i++ )
694 vlc_input_attachment_Delete( p_sys->attachments[i] );
695 TAB_CLEAN( p_sys->i_attachments, p_sys->attachments);
697 if( p_sys->p_title )
698 vlc_input_title_Delete( p_sys->p_title );
700 free( p_sys );
703 /*****************************************************************************
704 * Demux:
705 *****************************************************************************/
706 static int Demux( demux_t *p_demux )
708 demux_sys_t *p_sys = p_demux->p_sys;
709 AVPacket pkt;
710 block_t *p_frame;
711 int64_t i_start_time;
713 /* Read a frame */
714 int i_av_ret = av_read_frame( p_sys->ic, &pkt );
715 if( i_av_ret )
717 /* Avoid EOF if av_read_frame returns AVERROR(EAGAIN) */
718 if( i_av_ret == AVERROR(EAGAIN) )
719 return 1;
721 return 0;
723 if( pkt.stream_index < 0 || pkt.stream_index >= p_sys->i_tk )
725 av_packet_unref( &pkt );
726 return 1;
728 const AVStream *p_stream = p_sys->ic->streams[pkt.stream_index];
729 if( p_stream->time_base.den <= 0 )
731 msg_Warn( p_demux, "Invalid time base for the stream %d", pkt.stream_index );
732 av_packet_unref( &pkt );
733 return 1;
735 if( p_stream->codecpar->codec_id == AV_CODEC_ID_SSA )
737 p_frame = BuildSsaFrame( &pkt, p_sys->i_ssa_order++ );
738 if( !p_frame )
740 av_packet_unref( &pkt );
741 return 1;
744 else if( !strcmp( p_sys->fmt->name, "wtv" ) &&
745 p_stream->codecpar->codec_id == AV_CODEC_ID_DVB_SUBTITLE )
747 if( ( p_frame = block_Alloc( pkt.size + 3 ) ) == NULL )
749 av_packet_unref( &pkt );
750 return 0;
752 p_frame->p_buffer[0] = 0x20;
753 p_frame->p_buffer[1] = 0x00;
754 memcpy( &p_frame->p_buffer[2], pkt.data, pkt.size );
755 p_frame->p_buffer[p_frame->i_buffer - 1] = 0x3f;
757 else
759 if( ( p_frame = block_Alloc( pkt.size ) ) == NULL )
761 av_packet_unref( &pkt );
762 return 0;
764 memcpy( p_frame->p_buffer, pkt.data, pkt.size );
767 if( pkt.flags & AV_PKT_FLAG_KEY )
768 p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
770 /* Used to avoid timestamps overlow */
771 lldiv_t q;
772 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
774 q = lldiv( p_sys->ic->start_time, AV_TIME_BASE);
775 i_start_time = q.quot * CLOCK_FREQ + q.rem * CLOCK_FREQ / AV_TIME_BASE;
777 else
778 i_start_time = 0;
780 if( pkt.dts == (int64_t)AV_NOPTS_VALUE )
781 p_frame->i_dts = VLC_TS_INVALID;
782 else
784 q = lldiv( pkt.dts, p_stream->time_base.den );
785 p_frame->i_dts = q.quot * CLOCK_FREQ *
786 p_stream->time_base.num + q.rem * CLOCK_FREQ *
787 p_stream->time_base.num /
788 p_stream->time_base.den - i_start_time + VLC_TS_0;
791 if( pkt.pts == (int64_t)AV_NOPTS_VALUE )
792 p_frame->i_pts = VLC_TS_INVALID;
793 else
795 q = lldiv( pkt.pts, p_stream->time_base.den );
796 p_frame->i_pts = q.quot * CLOCK_FREQ *
797 p_stream->time_base.num + q.rem * CLOCK_FREQ *
798 p_stream->time_base.num /
799 p_stream->time_base.den - i_start_time + VLC_TS_0;
801 if( pkt.duration > 0 && p_frame->i_length <= 0 )
802 p_frame->i_length = pkt.duration * CLOCK_FREQ *
803 p_stream->time_base.num /
804 p_stream->time_base.den;
806 /* Add here notoriously bugged file formats/samples */
807 if( !strcmp( p_sys->fmt->name, "flv" ) )
809 /* FLV and video PTS */
810 if( p_stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
811 pkt.dts != (int64_t)AV_NOPTS_VALUE && pkt.dts == pkt.pts )
812 p_frame->i_pts = VLC_TS_INVALID;
814 /* Handle broken dts/pts increase with AAC. Duration is correct.
815 * sky_the80s_aacplus.flv #8195 */
816 if( p_stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
817 p_stream->codecpar->codec_id == AV_CODEC_ID_AAC )
819 if( p_sys->tk_pcr[pkt.stream_index] != VLC_TS_INVALID &&
820 p_sys->tk_pcr[pkt.stream_index] + p_frame->i_length > p_frame->i_dts )
822 p_frame->i_dts = p_frame->i_pts = p_sys->tk_pcr[pkt.stream_index] + p_frame->i_length;
826 #ifdef AVFORMAT_DEBUG
827 msg_Dbg( p_demux, "tk[%d] dts=%"PRId64" pts=%"PRId64,
828 pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
829 #endif
830 if( p_frame->i_dts > VLC_TS_INVALID && p_sys->tk[pkt.stream_index] != NULL )
831 p_sys->tk_pcr[pkt.stream_index] = p_frame->i_dts;
833 int64_t i_ts_max = INT64_MIN;
834 for( int i = 0; i < p_sys->i_tk; i++ )
836 if( p_sys->tk[i] != NULL )
837 i_ts_max = __MAX( i_ts_max, p_sys->tk_pcr[i] );
840 int64_t i_ts_min = INT64_MAX;
841 for( int i = 0; i < p_sys->i_tk; i++ )
843 if( p_sys->tk[i] != NULL &&
844 p_sys->tk_pcr[i] > VLC_TS_INVALID &&
845 p_sys->tk_pcr[i] + 10 * CLOCK_FREQ >= i_ts_max )
846 i_ts_min = __MIN( i_ts_min, p_sys->tk_pcr[i] );
848 if( i_ts_min >= p_sys->i_pcr && likely(i_ts_min != INT64_MAX) )
850 p_sys->i_pcr = i_ts_min;
851 es_out_SetPCR( p_demux->out, p_sys->i_pcr );
852 UpdateSeekPoint( p_demux, p_sys->i_pcr );
855 if( p_sys->tk[pkt.stream_index] != NULL )
856 es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame );
857 else
858 block_Release( p_frame );
860 av_packet_unref( &pkt );
861 return 1;
864 static void UpdateSeekPoint( demux_t *p_demux, int64_t i_time )
866 demux_sys_t *p_sys = p_demux->p_sys;
867 int i;
869 if( !p_sys->p_title )
870 return;
872 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
874 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
875 break;
877 i--;
879 if( i != p_demux->info.i_seekpoint && i >= 0 )
881 p_demux->info.i_seekpoint = i;
882 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
886 static void ResetTime( demux_t *p_demux, int64_t i_time )
888 demux_sys_t *p_sys = p_demux->p_sys;
890 if( p_sys->ic->start_time == (int64_t)AV_NOPTS_VALUE || i_time < 0 )
891 i_time = VLC_TS_INVALID;
892 else if( i_time == 0 )
893 i_time = 1;
895 p_sys->i_pcr = i_time;
896 for( int i = 0; i < p_sys->i_tk; i++ )
897 p_sys->tk_pcr[i] = i_time;
899 if( i_time > VLC_TS_INVALID )
901 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_time );
902 UpdateSeekPoint( p_demux, i_time );
906 static block_t *BuildSsaFrame( const AVPacket *p_pkt, unsigned i_order )
908 if( p_pkt->size <= 0 )
909 return NULL;
911 char buffer[256];
912 const size_t i_buffer_size = __MIN( (int)sizeof(buffer) - 1, p_pkt->size );
913 memcpy( buffer, p_pkt->data, i_buffer_size );
914 buffer[i_buffer_size] = '\0';
916 /* */
917 int i_layer;
918 int h0, m0, s0, c0;
919 int h1, m1, s1, c1;
920 int i_position = 0;
921 if( sscanf( buffer, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,%n", &i_layer,
922 &h0, &m0, &s0, &c0, &h1, &m1, &s1, &c1, &i_position ) < 9 )
923 return NULL;
924 if( i_position <= 0 || (unsigned)i_position >= i_buffer_size )
925 return NULL;
927 char *p;
928 if( asprintf( &p, "%u,%d,%.*s", i_order, i_layer, p_pkt->size - i_position, p_pkt->data + i_position ) < 0 )
929 return NULL;
931 block_t *p_frame = block_heap_Alloc( p, strlen(p) + 1 );
932 if( p_frame )
933 p_frame->i_length = CLOCK_FREQ * ((h1-h0) * 3600 +
934 (m1-m0) * 60 +
935 (s1-s0) * 1) +
936 CLOCK_FREQ * (c1-c0) / 100;
937 return p_frame;
940 /*****************************************************************************
941 * Control:
942 *****************************************************************************/
943 static int Control( demux_t *p_demux, int i_query, va_list args )
945 demux_sys_t *p_sys = p_demux->p_sys;
946 const int64_t i_start_time = p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ? p_sys->ic->start_time : 0;
947 double f, *pf;
948 int64_t i64, *pi64;
950 switch( i_query )
952 case DEMUX_CAN_SEEK:
953 *va_arg( args, bool * ) = true;
954 return VLC_SUCCESS;
956 case DEMUX_GET_POSITION:
957 pf = va_arg( args, double * ); *pf = 0.0;
958 i64 = stream_Size( p_demux->s );
959 if( i64 > 0 )
961 double current = vlc_stream_Tell( p_demux->s );
962 *pf = current / (double)i64;
965 if( (p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE) && (p_sys->i_pcr > 0) )
967 *pf = (double)p_sys->i_pcr / (double)p_sys->ic->duration;
970 return VLC_SUCCESS;
972 case DEMUX_SET_POSITION:
973 f = va_arg( args, double );
974 i64 = p_sys->ic->duration * f + i_start_time;
976 msg_Warn( p_demux, "DEMUX_SET_POSITION: %"PRId64, i64 );
978 /* If we have a duration, we prefer to seek by time
979 but if we don't, or if the seek fails, try BYTE seeking */
980 if( p_sys->ic->duration == (int64_t)AV_NOPTS_VALUE ||
981 (av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BACKWARD ) < 0) )
983 int64_t i_size = stream_Size( p_demux->s );
984 i64 = (i_size * f);
986 msg_Warn( p_demux, "DEMUX_SET_BYTE_POSITION: %"PRId64, i64 );
987 if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BYTE ) < 0 )
988 return VLC_EGENERIC;
990 ResetTime( p_demux, -1 );
992 else
994 ResetTime( p_demux, i64 - i_start_time );
996 return VLC_SUCCESS;
998 case DEMUX_GET_LENGTH:
999 pi64 = va_arg( args, int64_t * );
1000 if( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE )
1001 *pi64 = p_sys->ic->duration * 1000000 / AV_TIME_BASE;
1002 else
1003 *pi64 = 0;
1004 return VLC_SUCCESS;
1006 case DEMUX_GET_TIME:
1007 pi64 = va_arg( args, int64_t * );
1008 *pi64 = p_sys->i_pcr;
1009 return VLC_SUCCESS;
1011 case DEMUX_SET_TIME:
1013 i64 = va_arg( args, int64_t );
1014 i64 = i64 *AV_TIME_BASE / 1000000 + i_start_time;
1016 msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
1018 if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BACKWARD ) < 0 )
1020 return VLC_EGENERIC;
1022 ResetTime( p_demux, i64 - i_start_time );
1023 return VLC_SUCCESS;
1026 case DEMUX_HAS_UNSUPPORTED_META:
1028 bool *pb_bool = va_arg( args, bool* );
1029 *pb_bool = true;
1030 return VLC_SUCCESS;
1034 case DEMUX_GET_META:
1036 static const char names[][10] = {
1037 [vlc_meta_Title] = "title",
1038 [vlc_meta_Artist] = "artist",
1039 [vlc_meta_Genre] = "genre",
1040 [vlc_meta_Copyright] = "copyright",
1041 [vlc_meta_Album] = "album",
1042 //[vlc_meta_TrackNumber] -- TODO: parse number/total value
1043 [vlc_meta_Description] = "comment",
1044 //[vlc_meta_Rating]
1045 [vlc_meta_Date] = "date",
1046 [vlc_meta_Setting] = "encoder",
1047 //[vlc_meta_URL]
1048 [vlc_meta_Language] = "language",
1049 //[vlc_meta_NowPlaying]
1050 [vlc_meta_Publisher] = "publisher",
1051 [vlc_meta_EncodedBy] = "encoded_by",
1052 //[vlc_meta_ArtworkURL]
1053 //[vlc_meta_TrackID]
1054 //[vlc_meta_TrackTotal]
1056 vlc_meta_t *p_meta = va_arg( args, vlc_meta_t * );
1057 AVDictionary *dict = p_sys->ic->metadata;
1059 for( unsigned i = 0; i < sizeof(names) / sizeof(*names); i++)
1061 if( !names[i][0] )
1062 continue;
1064 AVDictionaryEntry *e = av_dict_get( dict, names[i], NULL, 0 );
1065 if( e != NULL && e->value != NULL && IsUTF8(e->value) )
1066 vlc_meta_Set( p_meta, i, e->value );
1068 return VLC_SUCCESS;
1071 case DEMUX_GET_ATTACHMENTS:
1073 input_attachment_t ***ppp_attach =
1074 va_arg( args, input_attachment_t*** );
1075 int *pi_int = va_arg( args, int * );
1076 int i;
1078 if( p_sys->i_attachments <= 0 )
1079 return VLC_EGENERIC;
1081 *pi_int = p_sys->i_attachments;;
1082 *ppp_attach = xmalloc( sizeof(input_attachment_t*) * p_sys->i_attachments );
1083 for( i = 0; i < p_sys->i_attachments; i++ )
1084 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
1085 return VLC_SUCCESS;
1088 case DEMUX_GET_TITLE_INFO:
1090 input_title_t ***ppp_title = va_arg( args, input_title_t *** );
1091 int *pi_int = va_arg( args, int * );
1092 int *pi_title_offset = va_arg( args, int * );
1093 int *pi_seekpoint_offset = va_arg( args, int * );
1095 if( !p_sys->p_title )
1096 return VLC_EGENERIC;
1098 *pi_int = 1;
1099 *ppp_title = xmalloc( sizeof( input_title_t*) );
1100 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
1101 *pi_title_offset = 0;
1102 *pi_seekpoint_offset = 0;
1103 return VLC_SUCCESS;
1105 case DEMUX_SET_TITLE:
1107 const int i_title = va_arg( args, int );
1108 if( !p_sys->p_title || i_title != 0 )
1109 return VLC_EGENERIC;
1110 return VLC_SUCCESS;
1112 case DEMUX_SET_SEEKPOINT:
1114 const int i_seekpoint = va_arg( args, int );
1115 if( !p_sys->p_title )
1116 return VLC_EGENERIC;
1118 i64 = p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset *
1119 AV_TIME_BASE / 1000000 + i_start_time;
1121 msg_Warn( p_demux, "DEMUX_SET_SEEKPOINT: %"PRId64, i64 );
1123 if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BACKWARD ) < 0 )
1125 return VLC_EGENERIC;
1127 ResetTime( p_demux, i64 - i_start_time );
1128 return VLC_SUCCESS;
1132 default:
1133 return VLC_EGENERIC;
1137 /*****************************************************************************
1138 * I/O wrappers for libavformat
1139 *****************************************************************************/
1140 static int IORead( void *opaque, uint8_t *buf, int buf_size )
1142 demux_t *p_demux = opaque;
1143 if( buf_size < 0 ) return -1;
1144 int i_ret = vlc_stream_Read( p_demux->s, buf, buf_size );
1145 return i_ret >= 0 ? i_ret : -1;
1148 static int64_t IOSeek( void *opaque, int64_t offset, int whence )
1150 demux_t *p_demux = opaque;
1151 int64_t i_absolute;
1152 int64_t i_size = stream_Size( p_demux->s );
1154 #ifdef AVFORMAT_DEBUG
1155 msg_Warn( p_demux, "IOSeek offset: %"PRId64", whence: %i", offset, whence );
1156 #endif
1158 switch( whence )
1160 #ifdef AVSEEK_SIZE
1161 case AVSEEK_SIZE:
1162 return i_size;
1163 #endif
1164 case SEEK_SET:
1165 i_absolute = (int64_t)offset;
1166 break;
1167 case SEEK_CUR:
1168 i_absolute = vlc_stream_Tell( p_demux->s ) + (int64_t)offset;
1169 break;
1170 case SEEK_END:
1171 i_absolute = i_size + (int64_t)offset;
1172 break;
1173 default:
1174 return -1;
1178 if( i_absolute < 0 )
1180 msg_Dbg( p_demux, "Trying to seek before the beginning" );
1181 return -1;
1184 if( i_size > 0 && i_absolute >= i_size )
1186 msg_Dbg( p_demux, "Trying to seek too far : EOF?" );
1187 return -1;
1190 if( vlc_stream_Seek( p_demux->s, i_absolute ) )
1192 msg_Warn( p_demux, "we were not allowed to seek, or EOF " );
1193 return -1;
1196 return vlc_stream_Tell( p_demux->s );