1 /*****************************************************************************
2 * demux.c: demuxer using libavformat
3 *****************************************************************************
4 * Copyright (C) 2004-2009 VLC authors and VideoLAN
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 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_demux.h>
35 #include <vlc_stream.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"
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
61 /*****************************************************************************
62 * demux_sys_t: demux descriptor
63 *****************************************************************************/
69 struct avformat_track_s
*tracks
;
77 input_attachment_t
**attachments
;
79 /* Only one title with seekpoints possible atm. */
80 input_title_t
*p_title
;
85 #define AVFORMAT_IOBUFFER_SIZE 32768 /* FIXME */
87 /*****************************************************************************
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
);
110 /*****************************************************************************
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);
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
;
134 fmt
->video
.orientation
= ORIENT_NORMAL
;
136 int32_t *matrix
= (int32_t *)av_stream_get_side_data(s
, AV_PKT_DATA_DISPLAYMATRIX
, NULL
);
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
;
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 };
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" );
169 pd
.buf
= malloc( pd
.buf_size
+ AVPROBE_PADDING_SIZE
);
170 if( unlikely(pd
.buf
== NULL
) )
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
);
184 char *psz_format
= var_InheritString( p_this
, "avformat-format" );
187 if( (*pp_fmt
= av_find_input_format(psz_format
)) )
188 msg_Dbg( p_demux
, "forcing format: %s", (*pp_fmt
)->name
);
192 if( *pp_fmt
== NULL
)
193 *pp_fmt
= av_probe_input_format( &pd
, 1 );
197 if( *pp_fmt
== NULL
)
199 msg_Dbg( p_demux
, "couldn't guess format" );
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 */
210 /* Don't handle subtitles format */
211 "ass", "srt", "microdvd",
212 /* No timestamps at all */
217 for( int i
= 0; *ppsz_blacklist
[i
]; i
++ )
219 if( !strcmp( (*pp_fmt
)->name
, ppsz_blacklist
[i
] ) )
224 /* Don't trigger false alarms on bin files */
225 if( !p_demux
->obj
.force
&& !strcmp( (*pp_fmt
)->name
, "psxstr" ) )
229 if( !p_demux
->psz_filepath
)
232 i_len
= strlen( p_demux
->psz_filepath
);
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" ) )
244 msg_Dbg( p_demux
, "detected format: %s", (*pp_fmt
)->name
);
249 int avformat_OpenDemux( vlc_object_t
*p_this
)
251 demux_t
*p_demux
= (demux_t
*)p_this
;
253 AVInputFormat
*fmt
= NULL
;
254 int64_t i_start_time
= -1;
259 if( p_demux
->psz_filepath
)
260 psz_url
= p_demux
->psz_filepath
;
262 psz_url
= p_demux
->psz_url
;
264 if( avformat_ProbeDemux( p_this
, &fmt
, psz_url
) != VLC_SUCCESS
)
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
) );
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;
285 /* Create I/O wrapper */
286 unsigned char * p_io_buffer
= av_malloc( AVFORMAT_IOBUFFER_SIZE
);
289 avformat_CloseDemux( p_this
);
293 p_sys
->ic
= avformat_alloc_context();
296 av_free( p_io_buffer
);
297 avformat_CloseDemux( p_this
);
301 AVIOContext
*pb
= p_sys
->ic
->pb
= avio_alloc_context( p_io_buffer
,
302 AVFORMAT_IOBUFFER_SIZE
, 0, p_demux
, IORead
, NULL
, IOSeek
);
305 av_free( p_io_buffer
);
306 avformat_CloseDemux( p_this
);
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
);
315 msg_Err( p_demux
, "Could not open %s: %s", psz_url
,
316 vlc_strerror_c(AVUNERROR(error
)) );
317 av_free( pb
->buffer
);
320 avformat_CloseDemux( p_this
);
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];
329 for (unsigned i
= 1; i
< nb_streams
; i
++)
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);
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 */
353 msg_Err( p_demux
, "No streams found");
354 avformat_CloseDemux( p_this
);
357 p_sys
->tracks
= calloc( nb_streams
, sizeof(*p_sys
->tracks
) );
360 avformat_CloseDemux( p_this
);
363 p_sys
->i_tracks
= nb_streams
;
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
;
377 const char *psz_type
= "unknown";
379 /* Do not use the cover art as a stream */
380 if( s
->disposition
== AV_DISPOSITION_ATTACHED_PIC
)
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
;
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;
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" );
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
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
;
446 rate
.num
= s
->codec
->time_base
.num
;
447 rate
.den
= s
->codec
->time_base
.den
;
449 rate
.den
*= __MAX( s
->codec
->ticks_per_frame
, 1 );
451 rate
= av_guess_frame_rate( p_sys
->ic
, s
, NULL
);
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
;
460 #if (LIBAVUTIL_VERSION_MICRO < 100) /* libav */
461 ar
.num
= s
->sample_aspect_ratio
.num
;
462 ar
.den
= s
->sample_aspect_ratio
.den
;
464 ar
= av_guess_sample_aspect_ratio( p_sys
->ic
, s
, NULL
);
466 if( ar
.num
&& ar
.den
)
468 es_fmt
.video
.i_sar_den
= ar
.den
;
469 es_fmt
.video
.i_sar_num
= ar
.num
;
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 )
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:" );
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
);
500 msg_Warn( p_demux
, "reading original frame size failed" );
503 psz_start
= strstr( psz_buf
, "palette:" );
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" );
512 msg_Warn( p_demux
, "reading original palette failed" );
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";
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
);
552 TAB_APPEND( p_sys
->i_attachments
, p_sys
->attachments
,
556 else msg_Warn( p_demux
, "unsupported attachment type (%u) in avformat demux", cp
->codec_id
);
561 if( cp
->codec_type
== AVMEDIA_TYPE_DATA
)
564 msg_Warn( p_demux
, "unsupported track type (%u:%u) in avformat demux", cp
->codec_type
, cp
->codec_id
);
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
)
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
)
588 const void *pp_data
[3];
590 for( i_count
= 0; i_count
< 3; i_count
++ )
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 )
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
) )
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
[] = {
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 ) )
628 es_fmt
.p_extra
= NULL
;
631 else if( cp
->codec_id
== AV_CODEC_ID_OPUS
)
633 const uint8_t p_dummy_comment
[] = {
636 0, 0, 0, 0, /* Vendor String length */
638 0, 0, 0, 0, /* User Comment List Length */
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 ) )
654 es_fmt
.p_extra
= NULL
;
657 else if( cp
->extradata_size
> 0 )
659 es_fmt
.p_extra
= malloc( i_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 );
716 /*****************************************************************************
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
);
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
);
741 vlc_input_title_Delete( p_sys
->p_title
);
746 /*****************************************************************************
748 *****************************************************************************/
749 static int Demux( demux_t
*p_demux
)
751 demux_sys_t
*p_sys
= p_demux
->p_sys
;
754 int64_t i_start_time
;
757 int i_av_ret
= av_read_frame( p_sys
->ic
, &pkt
);
760 /* Avoid EOF if av_read_frame returns AVERROR(EAGAIN) */
761 if( i_av_ret
== AVERROR(EAGAIN
) )
766 if( pkt
.stream_index
< 0 || (unsigned) pkt
.stream_index
>= p_sys
->i_tracks
)
768 av_packet_unref( &pkt
);
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
);
779 if( p_stream
->codecpar
->codec_id
== AV_CODEC_ID_SSA
)
781 p_frame
= BuildSsaFrame( &pkt
, p_sys
->i_ssa_order
++ );
784 av_packet_unref( &pkt
);
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
);
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;
802 if( ( p_frame
= block_Alloc( pkt
.size
) ) == NULL
)
804 av_packet_unref( &pkt
);
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 */
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
;
823 if( pkt
.dts
== (int64_t)AV_NOPTS_VALUE
)
824 p_frame
->i_dts
= VLC_TS_INVALID
;
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
;
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
);
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
);
901 block_Release( p_frame
);
903 av_packet_unref( &pkt
);
907 static void UpdateSeekPoint( demux_t
*p_demux
, int64_t i_time
)
909 demux_sys_t
*p_sys
= p_demux
->p_sys
;
912 if( !p_sys
->p_title
)
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
)
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 )
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 )
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';
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 )
967 if( i_position
<= 0 || (unsigned)i_position
>= i_buffer_size
)
971 if( asprintf( &p
, "%u,%d,%.*s", i_order
, i_layer
, p_pkt
->size
- i_position
, p_pkt
->data
+ i_position
) < 0 )
974 block_t
*p_frame
= block_heap_Alloc( p
, strlen(p
) + 1 );
976 p_frame
->i_length
= CLOCK_FREQ
* ((h1
-h0
) * 3600 +
979 CLOCK_FREQ
* (c1
-c0
) / 100;
983 /*****************************************************************************
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;
996 *va_arg( args
, bool * ) = true;
999 case DEMUX_GET_POSITION
:
1000 pf
= va_arg( args
, double * ); *pf
= 0.0;
1001 i64
= stream_Size( p_demux
->s
);
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
;
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
);
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 );
1040 ResetTime( p_demux
, i64
- i_start_time
);
1042 ResetTime( p_demux
, -1 );
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
;
1055 case DEMUX_GET_TIME
:
1056 pi64
= va_arg( args
, int64_t * );
1057 *pi64
= p_sys
->i_pcr
;
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
;
1073 ResetTime( p_demux
, i64
- i_start_time
);
1075 ResetTime( p_demux
, -1 );
1079 case DEMUX_HAS_UNSUPPORTED_META
:
1081 bool *pb_bool
= va_arg( args
, bool* );
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",
1098 [vlc_meta_Date
] = "date",
1099 [vlc_meta_Setting
] = "encoder",
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
++)
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
);
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 * );
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
)
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;
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
;
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
);
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
;
1199 case DEMUX_GET_TITLE
:
1200 if( p_sys
->p_title
== NULL
)
1201 return VLC_EGENERIC
;
1202 *va_arg( args
, int * ) = 0;
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
;
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
);
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
;
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
);
1247 i_absolute
= (int64_t)offset
;
1250 i_absolute
= vlc_stream_Tell( p_demux
->s
) + (int64_t)offset
;
1253 i_absolute
= i_size
+ (int64_t)offset
;
1260 if( i_absolute
< 0 )
1262 msg_Dbg( p_demux
, "Trying to seek before the beginning" );
1266 if( i_size
> 0 && i_absolute
>= i_size
)
1268 msg_Dbg( p_demux
, "Trying to seek too far : EOF?" );
1272 if( vlc_stream_Seek( p_demux
->s
, i_absolute
) )
1274 msg_Warn( p_demux
, "we were not allowed to seek, or EOF " );
1278 return vlc_stream_Tell( p_demux
->s
);