]> git.sesse.net Git - vlc/blob - modules/demux/avformat/demux.c
avformat: validate encoding of meta informations (fixes #8390)
[vlc] / modules / demux / avformat / demux.c
1 /*****************************************************************************
2  * demux.c: demuxer using libavformat
3  *****************************************************************************
4  * Copyright (C) 2004-2009 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
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>
40
41 #include <libavformat/avformat.h>
42
43 #include "../../codec/avcodec/avcodec.h"
44 #include "../../codec/avcodec/chroma.h"
45 #include "../../codec/avcodec/avcommon.h"
46 #include "avformat.h"
47 #include "../xiph.h"
48 #include "../vobsub.h"
49
50 /* Support for deprecated APIs */
51
52 #if LIBAVFORMAT_VERSION_MAJOR < 54
53 # define AVDictionaryEntry AVMetadataTag
54 # define av_dict_get av_metadata_get
55 #endif
56
57 //#define AVFORMAT_DEBUG 1
58
59 /* Version checking */
60 #if defined(HAVE_FFMPEG_AVFORMAT_H) || defined(HAVE_LIBAVFORMAT_AVFORMAT_H)
61
62 # define HAVE_AVUTIL_CODEC_ATTACHMENT 1
63
64 /*****************************************************************************
65  * demux_sys_t: demux descriptor
66  *****************************************************************************/
67 struct demux_sys_t
68 {
69     int             io_buffer_size;
70     uint8_t        *io_buffer;
71
72     AVInputFormat  *fmt;
73     AVFormatContext *ic;
74
75     int             i_tk;
76     es_out_id_t     **tk;
77     int64_t         *tk_pcr;
78     int64_t         i_pcr;
79
80     unsigned    i_ssa_order;
81
82     int                i_attachments;
83     input_attachment_t **attachments;
84
85     /* Only one title with seekpoints possible atm. */
86     input_title_t *p_title;
87 };
88
89 /*****************************************************************************
90  * Local prototypes
91  *****************************************************************************/
92 static int Demux  ( demux_t *p_demux );
93 static int Control( demux_t *p_demux, int i_query, va_list args );
94
95 static int IORead( void *opaque, uint8_t *buf, int buf_size );
96 static int64_t IOSeek( void *opaque, int64_t offset, int whence );
97
98 static block_t *BuildSsaFrame( const AVPacket *p_pkt, unsigned i_order );
99 static void UpdateSeekPoint( demux_t *p_demux, int64_t i_time );
100 static void ResetTime( demux_t *p_demux, int64_t i_time );
101
102 /*****************************************************************************
103  * Open
104  *****************************************************************************/
105 int OpenDemux( vlc_object_t *p_this )
106 {
107     demux_t       *p_demux = (demux_t*)p_this;
108     demux_sys_t   *p_sys;
109     AVProbeData   pd;
110     AVInputFormat *fmt = NULL;
111     unsigned int  i;
112     int64_t       i_start_time = -1;
113     bool          b_can_seek;
114     char         *psz_url;
115     int           error;
116
117     if( p_demux->psz_file )
118         psz_url = strdup( p_demux->psz_file );
119     else
120     {
121         if( asprintf( &psz_url, "%s://%s", p_demux->psz_access, p_demux->psz_location ) == -1)
122             return VLC_ENOMEM;
123     }
124     msg_Dbg( p_demux, "trying url: %s", psz_url );
125     /* Init Probe data */
126     pd.filename = psz_url;
127     if( ( pd.buf_size = stream_Peek( p_demux->s, (const uint8_t**)&pd.buf, 2048 + 213 ) ) <= 0 )
128     {
129         free( psz_url );
130         msg_Warn( p_demux, "cannot peek" );
131         return VLC_EGENERIC;
132     }
133     stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_can_seek );
134
135     vlc_init_avformat();
136
137     char *psz_format = var_InheritString( p_this, "avformat-format" );
138     if( psz_format )
139     {
140         if( (fmt = av_find_input_format(psz_format)) )
141             msg_Dbg( p_demux, "forcing format: %s", fmt->name );
142         free( psz_format );
143     }
144
145     /* Guess format */
146     if( !fmt && !( fmt = av_probe_input_format( &pd, 1 ) ) )
147     {
148         msg_Dbg( p_demux, "couldn't guess format" );
149         free( psz_url );
150         return VLC_EGENERIC;
151     }
152
153     if( !p_demux->b_force )
154     {
155         static const char ppsz_blacklist[][16] = {
156             /* Don't handle MPEG unless forced */
157             "mpeg", "vcd", "vob", "mpegts",
158             /* libavformat's redirector won't work */
159             "redir", "sdp",
160             /* Don't handle subtitles format */
161             "ass", "srt", "microdvd",
162             ""
163         };
164
165         for( int i = 0; *ppsz_blacklist[i]; i++ )
166         {
167             if( !strcmp( fmt->name, ppsz_blacklist[i] ) )
168             {
169                 free( psz_url );
170                 return VLC_EGENERIC;
171             }
172         }
173     }
174
175     /* Don't trigger false alarms on bin files */
176     if( !p_demux->b_force && !strcmp( fmt->name, "psxstr" ) )
177     {
178         int i_len;
179
180         if( !p_demux->psz_file )
181         {
182             free( psz_url );
183             return VLC_EGENERIC;
184         }
185
186         i_len = strlen( p_demux->psz_file );
187         if( i_len < 4 )
188         {
189             free( psz_url );
190             return VLC_EGENERIC;
191         }
192
193         if( strcasecmp( &p_demux->psz_file[i_len - 4], ".str" ) &&
194             strcasecmp( &p_demux->psz_file[i_len - 4], ".xai" ) &&
195             strcasecmp( &p_demux->psz_file[i_len - 3], ".xa" ) )
196         {
197             free( psz_url );
198             return VLC_EGENERIC;
199         }
200     }
201
202     msg_Dbg( p_demux, "detected format: %s", fmt->name );
203
204     /* Fill p_demux fields */
205     p_demux->pf_demux = Demux;
206     p_demux->pf_control = Control;
207     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
208     p_sys->ic = 0;
209     p_sys->fmt = fmt;
210     p_sys->i_tk = 0;
211     p_sys->tk = NULL;
212     p_sys->tk_pcr = NULL;
213     p_sys->i_ssa_order = 0;
214     TAB_INIT( p_sys->i_attachments, p_sys->attachments);
215     p_sys->p_title = NULL;
216
217     /* Create I/O wrapper */
218     p_sys->io_buffer_size = 32768;  /* FIXME */
219     p_sys->io_buffer = malloc( p_sys->io_buffer_size );
220
221     p_sys->ic = avformat_alloc_context();
222     p_sys->ic->pb = avio_alloc_context( p_sys->io_buffer,
223         p_sys->io_buffer_size, 0, p_demux, IORead, NULL, IOSeek );
224     p_sys->ic->pb->seekable = b_can_seek ? AVIO_SEEKABLE_NORMAL : 0;
225     error = avformat_open_input(&p_sys->ic, psz_url, p_sys->fmt, NULL);
226
227     if( error < 0 )
228     {
229         errno = AVUNERROR(error);
230         msg_Err( p_demux, "Could not open %s: %m", psz_url );
231         p_sys->ic = NULL;
232         free( psz_url );
233         CloseDemux( p_this );
234         return VLC_EGENERIC;
235     }
236     free( psz_url );
237
238 #if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(26<<8)+0)
239     char *psz_opts = var_InheritString( p_demux, "avformat-options" );
240     AVDictionary *options[p_sys->ic->nb_streams ? p_sys->ic->nb_streams : 1];
241     options[0] = NULL;
242     unsigned int nb_streams = p_sys->ic->nb_streams;
243     for (unsigned i = 1; i < nb_streams; i++)
244         options[i] = NULL;
245     if (psz_opts && *psz_opts) {
246         options[0] = vlc_av_get_options(psz_opts);
247         for (unsigned i = 1; i < nb_streams; i++) {
248             av_dict_copy(&options[i], options[0], 0);
249         }
250     }
251     free(psz_opts);
252     vlc_avcodec_lock(); /* avformat calls avcodec behind our back!!! */
253     error = avformat_find_stream_info( p_sys->ic, options );
254     /* FIXME: what if nb_streams change after that call? */
255     vlc_avcodec_unlock();
256     AVDictionaryEntry *t = NULL;
257     while ((t = av_dict_get(options[0], "", t, AV_DICT_IGNORE_SUFFIX))) {
258         msg_Err( p_demux, "Unknown option \"%s\"", t->key );
259     }
260     av_dict_free(&options[0]);
261     for (unsigned i = 1; i < nb_streams; i++) {
262         av_dict_free(&options[i]);
263     }
264 #else
265     vlc_avcodec_lock(); /* avformat calls avcodec behind our back!!! */
266     error = av_find_stream_info( p_sys->ic );
267     vlc_avcodec_unlock();
268 #endif
269
270     if( error < 0 )
271     {
272         errno = AVUNERROR(error);
273         msg_Warn( p_demux, "Could not find stream info: %m" );
274     }
275
276     for( i = 0; i < p_sys->ic->nb_streams; i++ )
277     {
278         AVStream *s = p_sys->ic->streams[i];
279         AVCodecContext *cc = s->codec;
280
281         es_out_id_t  *es;
282         es_format_t  fmt;
283         vlc_fourcc_t fcc;
284         const char *psz_type = "unknown";
285
286         if( !GetVlcFourcc( cc->codec_id, NULL, &fcc, NULL ) )
287             fcc = VLC_FOURCC( 'u', 'n', 'd', 'f' );
288
289 #if LIBAVFORMAT_VERSION_INT >= ((54<<16)+(2<<8)+0)
290         /* Do not use the cover art as a stream */
291         if( s->disposition == AV_DISPOSITION_ATTACHED_PIC )
292             continue;
293 #endif
294
295         switch( cc->codec_type )
296         {
297         case AVMEDIA_TYPE_AUDIO:
298             es_format_Init( &fmt, AUDIO_ES, fcc );
299             fmt.i_bitrate = cc->bit_rate;
300             fmt.audio.i_channels = cc->channels;
301             fmt.audio.i_rate = cc->sample_rate;
302             fmt.audio.i_bitspersample = cc->bits_per_coded_sample;
303             fmt.audio.i_blockalign = cc->block_align;
304             psz_type = "audio";
305             break;
306
307         case AVMEDIA_TYPE_VIDEO:
308             es_format_Init( &fmt, VIDEO_ES, fcc );
309
310             /* Special case for raw video data */
311             if( cc->codec_id == AV_CODEC_ID_RAWVIDEO )
312             {
313                 msg_Dbg( p_demux, "raw video, pixel format: %i", cc->pix_fmt );
314                 if( GetVlcChroma( &fmt.video, cc->pix_fmt ) != VLC_SUCCESS)
315                 {
316                     msg_Err( p_demux, "was unable to find a FourCC match for raw video" );
317                 }
318                 else
319                     fmt.i_codec = fmt.video.i_chroma;
320             }
321             /* We need this for the h264 packetizer */
322             else if( cc->codec_id == AV_CODEC_ID_H264 && ( p_sys->fmt == av_find_input_format("flv") ||
323                 p_sys->fmt == av_find_input_format("matroska") || p_sys->fmt == av_find_input_format("mp4") ) )
324                 fmt.i_original_fourcc = VLC_FOURCC( 'a', 'v', 'c', '1' );
325
326             fmt.video.i_width = cc->width;
327             fmt.video.i_height = cc->height;
328 #if LIBAVCODEC_VERSION_MAJOR < 54
329             if( cc->palctrl )
330             {
331                 fmt.video.p_palette = malloc( sizeof(video_palette_t) );
332                 *fmt.video.p_palette = *(video_palette_t *)cc->palctrl;
333             }
334 #else
335 # warning FIXME: implement palette transmission
336 #endif
337             psz_type = "video";
338             fmt.video.i_frame_rate = cc->time_base.den;
339             fmt.video.i_frame_rate_base = cc->time_base.num * __MAX( cc->ticks_per_frame, 1 );
340             break;
341
342         case AVMEDIA_TYPE_SUBTITLE:
343             es_format_Init( &fmt, SPU_ES, fcc );
344             if( strncmp( p_sys->ic->iformat->name, "matroska", 8 ) == 0 &&
345                 cc->codec_id == AV_CODEC_ID_DVD_SUBTITLE &&
346                 cc->extradata != NULL &&
347                 cc->extradata_size > 0 )
348             {
349                 char *psz_start;
350                 char *psz_buf = malloc( cc->extradata_size + 1);
351                 if( psz_buf != NULL )
352                 {
353                     memcpy( psz_buf, cc->extradata , cc->extradata_size );
354                     psz_buf[cc->extradata_size] = '\0';
355
356                     psz_start = strstr( psz_buf, "size:" );
357                     if( psz_start &&
358                         vobsub_size_parse( psz_start,
359                                            &fmt.subs.spu.i_original_frame_width,
360                                            &fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
361                     {
362                         msg_Dbg( p_demux, "original frame size: %dx%d",
363                                  fmt.subs.spu.i_original_frame_width,
364                                  fmt.subs.spu.i_original_frame_height );
365                     }
366                     else
367                     {
368                         msg_Warn( p_demux, "reading original frame size failed" );
369                     }
370
371                     psz_start = strstr( psz_buf, "palette:" );
372                     if( psz_start &&
373                         vobsub_palette_parse( psz_start, &fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
374                     {
375                         fmt.subs.spu.palette[0] =  0xBeef;
376                         msg_Dbg( p_demux, "vobsub palette read" );
377                     }
378                     else
379                     {
380                         msg_Warn( p_demux, "reading original palette failed" );
381                     }
382                     free( psz_buf );
383                 }
384             }
385
386             psz_type = "subtitle";
387             break;
388
389         default:
390             es_format_Init( &fmt, UNKNOWN_ES, 0 );
391 #ifdef HAVE_AVUTIL_CODEC_ATTACHMENT
392             if( cc->codec_type == AVMEDIA_TYPE_ATTACHMENT )
393             {
394                 input_attachment_t *p_attachment;
395
396                 psz_type = "attachment";
397                 if( cc->codec_id == AV_CODEC_ID_TTF )
398                 {
399                     AVDictionaryEntry *filename = av_dict_get( s->metadata, "filename", NULL, 0 );
400                     if( filename && filename->value )
401                     {
402                         p_attachment = vlc_input_attachment_New(
403                                 filename->value, "application/x-truetype-font",
404                                 NULL, cc->extradata, (int)cc->extradata_size );
405                         TAB_APPEND( p_sys->i_attachments, p_sys->attachments,
406                                 p_attachment );
407                     }
408                 }
409                 else msg_Warn( p_demux, "unsupported attachment type (%u) in avformat demux", cc->codec_id );
410             }
411             else
412 #endif
413             {
414                 if( cc->codec_type == AVMEDIA_TYPE_DATA )
415                     psz_type = "data";
416
417                 msg_Warn( p_demux, "unsupported track type (%u:%u) in avformat demux", cc->codec_type, cc->codec_id );
418             }
419             break;
420         }
421
422         AVDictionaryEntry *language = av_dict_get( s->metadata, "language", NULL, 0 );
423         if ( language && language->value )
424             fmt.psz_language = strdup( language->value );
425
426         if( s->disposition & AV_DISPOSITION_DEFAULT )
427             fmt.i_priority = 1000;
428
429 #ifdef HAVE_AVUTIL_CODEC_ATTACHMENT
430         if( cc->codec_type != AVMEDIA_TYPE_ATTACHMENT )
431 #endif
432         if( cc->codec_type != AVMEDIA_TYPE_DATA )
433         {
434             const bool    b_ogg = !strcmp( p_sys->fmt->name, "ogg" );
435             const uint8_t *p_extra = cc->extradata;
436             unsigned      i_extra  = cc->extradata_size;
437
438             if( cc->codec_id == AV_CODEC_ID_THEORA && b_ogg )
439             {
440                 unsigned pi_size[3];
441                 const void *pp_data[3];
442                 unsigned i_count;
443                 for( i_count = 0; i_count < 3; i_count++ )
444                 {
445                     if( i_extra < 2 )
446                         break;
447                     pi_size[i_count] = GetWBE( p_extra );
448                     pp_data[i_count] = &p_extra[2];
449                     if( i_extra < pi_size[i_count] + 2 )
450                         break;
451
452                     p_extra += 2 + pi_size[i_count];
453                     i_extra -= 2 + pi_size[i_count];
454                 }
455                 if( i_count > 0 && xiph_PackHeaders( &fmt.i_extra, &fmt.p_extra,
456                                                      pi_size, pp_data, i_count ) )
457                 {
458                     fmt.i_extra = 0;
459                     fmt.p_extra = NULL;
460                 }
461             }
462             else if( cc->codec_id == AV_CODEC_ID_SPEEX && b_ogg )
463             {
464                 const uint8_t p_dummy_comment[] = {
465                     0, 0, 0, 0,
466                     0, 0, 0, 0,
467                 };
468                 unsigned pi_size[2];
469                 const void *pp_data[2];
470
471                 pi_size[0] = i_extra;
472                 pp_data[0] = p_extra;
473
474                 pi_size[1] = sizeof(p_dummy_comment);
475                 pp_data[1] = p_dummy_comment;
476
477                 if( pi_size[0] > 0 && xiph_PackHeaders( &fmt.i_extra, &fmt.p_extra,
478                                                         pi_size, pp_data, 2 ) )
479                 {
480                     fmt.i_extra = 0;
481                     fmt.p_extra = NULL;
482                 }
483             }
484             else if( cc->extradata_size > 0 )
485             {
486                 fmt.p_extra = malloc( i_extra );
487                 if( fmt.p_extra )
488                 {
489                     fmt.i_extra = i_extra;
490                     memcpy( fmt.p_extra, p_extra, i_extra );
491                 }
492             }
493             es = es_out_Add( p_demux->out, &fmt );
494             if( s->disposition & AV_DISPOSITION_DEFAULT )
495                 es_out_Control( p_demux->out, ES_OUT_SET_ES_DEFAULT, es );
496             es_format_Clean( &fmt );
497
498             msg_Dbg( p_demux, "adding es: %s codec = %4.4s",
499                      psz_type, (char*)&fcc );
500             TAB_APPEND( p_sys->i_tk, p_sys->tk, es );
501         }
502     }
503     p_sys->tk_pcr = calloc( p_sys->i_tk, sizeof(*p_sys->tk_pcr) );
504
505     if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
506         i_start_time = p_sys->ic->start_time * 1000000 / AV_TIME_BASE;
507
508     msg_Dbg( p_demux, "AVFormat supported stream" );
509     msg_Dbg( p_demux, "    - format = %s (%s)",
510              p_sys->fmt->name, p_sys->fmt->long_name );
511     msg_Dbg( p_demux, "    - start time = %"PRId64, i_start_time );
512     msg_Dbg( p_demux, "    - duration = %"PRId64,
513              ( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE ) ?
514              p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 );
515
516     if( p_sys->ic->nb_chapters > 0 )
517     {
518         p_sys->p_title = vlc_input_title_New();
519         p_sys->p_title->i_length = p_sys->ic->duration * 1000000 / AV_TIME_BASE;
520     }
521
522     for( i = 0; i < p_sys->ic->nb_chapters; i++ )
523     {
524         seekpoint_t *s = vlc_seekpoint_New();
525
526         AVDictionaryEntry *title = av_dict_get( p_sys->ic->metadata, "title", NULL, 0);
527         if( title && title->value )
528         {
529             s->psz_name = strdup( title->value );
530             EnsureUTF8( s->psz_name );
531             msg_Dbg( p_demux, "    - chapter %d: %s", i, s->psz_name );
532         }
533         s->i_time_offset = p_sys->ic->chapters[i]->start * 1000000 *
534             p_sys->ic->chapters[i]->time_base.num /
535             p_sys->ic->chapters[i]->time_base.den -
536             (i_start_time != -1 ? i_start_time : 0 );
537         TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
538     }
539
540     ResetTime( p_demux, 0 );
541     return VLC_SUCCESS;
542 }
543
544 /*****************************************************************************
545  * Close
546  *****************************************************************************/
547 void CloseDemux( vlc_object_t *p_this )
548 {
549     demux_t     *p_demux = (demux_t*)p_this;
550     demux_sys_t *p_sys = p_demux->p_sys;
551
552     FREENULL( p_sys->tk );
553     free( p_sys->tk_pcr );
554
555     if( p_sys->ic )
556     {
557         av_free( p_sys->ic->pb );
558 #if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(26<<8)+0)
559         avformat_close_input( &p_sys->ic );
560 #else
561         av_close_input_stream( p_sys->ic );
562 #endif
563     }
564
565     for( int i = 0; i < p_sys->i_attachments; i++ )
566         free( p_sys->attachments[i] );
567     TAB_CLEAN( p_sys->i_attachments, p_sys->attachments);
568
569     if( p_sys->p_title )
570         vlc_input_title_Delete( p_sys->p_title );
571
572     free( p_sys->io_buffer );
573     free( p_sys );
574 }
575
576 /*****************************************************************************
577  * Demux:
578  *****************************************************************************/
579 static int Demux( demux_t *p_demux )
580 {
581     demux_sys_t *p_sys = p_demux->p_sys;
582     AVPacket    pkt;
583     block_t     *p_frame;
584     int64_t     i_start_time;
585
586     /* Read a frame */
587     int i_av_ret = av_read_frame( p_sys->ic, &pkt );
588     if( i_av_ret )
589     {
590         /* Avoid EOF if av_read_frame returns AVERROR(EAGAIN) */
591         if( i_av_ret == AVERROR(EAGAIN) )
592             return 1;
593
594         return 0;
595     }
596     if( pkt.stream_index < 0 || pkt.stream_index >= p_sys->i_tk )
597     {
598         av_free_packet( &pkt );
599         return 1;
600     }
601     const AVStream *p_stream = p_sys->ic->streams[pkt.stream_index];
602     if( p_stream->time_base.den <= 0 )
603     {
604         msg_Warn( p_demux, "Invalid time base for the stream %d", pkt.stream_index );
605         av_free_packet( &pkt );
606         return 1;
607     }
608     if( p_stream->codec->codec_id == AV_CODEC_ID_SSA )
609     {
610         p_frame = BuildSsaFrame( &pkt, p_sys->i_ssa_order++ );
611         if( !p_frame )
612         {
613             av_free_packet( &pkt );
614             return 1;
615         }
616     }
617     else
618     {
619         if( ( p_frame = block_Alloc( pkt.size ) ) == NULL )
620         {
621             av_free_packet( &pkt );
622             return 0;
623         }
624         memcpy( p_frame->p_buffer, pkt.data, pkt.size );
625     }
626
627     if( pkt.flags & AV_PKT_FLAG_KEY )
628         p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
629
630     /* Used to avoid timestamps overlow */
631     lldiv_t q;
632     if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
633     {
634         q = lldiv( p_sys->ic->start_time, AV_TIME_BASE);
635         i_start_time = q.quot * (int64_t)1000000 + q.rem * (int64_t)1000000 / AV_TIME_BASE;
636     }
637     else
638         i_start_time = 0;
639
640     if( pkt.dts == (int64_t)AV_NOPTS_VALUE )
641         p_frame->i_dts = VLC_TS_INVALID;
642     else
643     {
644         q = lldiv( pkt.dts, p_stream->time_base.den );
645         p_frame->i_dts = q.quot * (int64_t)1000000 *
646             p_stream->time_base.num + q.rem * (int64_t)1000000 *
647             p_stream->time_base.num /
648             p_stream->time_base.den - i_start_time + VLC_TS_0;
649     }
650
651     if( pkt.pts == (int64_t)AV_NOPTS_VALUE )
652         p_frame->i_pts = VLC_TS_INVALID;
653     else
654     {
655         q = lldiv( pkt.pts, p_stream->time_base.den );
656         p_frame->i_pts = q.quot * (int64_t)1000000 *
657             p_stream->time_base.num + q.rem * (int64_t)1000000 *
658             p_stream->time_base.num /
659             p_stream->time_base.den - i_start_time + VLC_TS_0;
660     }
661     if( pkt.duration > 0 && p_frame->i_length <= 0 )
662         p_frame->i_length = pkt.duration * 1000000 *
663             p_stream->time_base.num /
664             p_stream->time_base.den;
665
666     if( pkt.dts != (int64_t)AV_NOPTS_VALUE && pkt.dts == pkt.pts &&
667         p_stream->codec->codec_type == AVMEDIA_TYPE_VIDEO )
668     {
669         /* Add here notoriously bugged file formats/samples regarding PTS */
670         if( !strcmp( p_sys->fmt->name, "flv" ) )
671             p_frame->i_pts = VLC_TS_INVALID;
672     }
673 #ifdef AVFORMAT_DEBUG
674     msg_Dbg( p_demux, "tk[%d] dts=%"PRId64" pts=%"PRId64,
675              pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
676 #endif
677     if( p_frame->i_dts > VLC_TS_INVALID )
678         p_sys->tk_pcr[pkt.stream_index] = p_frame->i_dts;
679
680     int64_t i_ts_max = INT64_MIN;
681     for( int i = 0; i < p_sys->i_tk; i++ )
682         i_ts_max = __MAX( i_ts_max, p_sys->tk_pcr[i] );
683
684     int64_t i_ts_min = INT64_MAX;
685     for( int i = 0; i < p_sys->i_tk; i++ )
686     {
687         if( p_sys->tk_pcr[i] > VLC_TS_INVALID && p_sys->tk_pcr[i] + 10 * CLOCK_FREQ >= i_ts_max )
688             i_ts_min = __MIN( i_ts_min, p_sys->tk_pcr[i] );
689     }
690     if( i_ts_min >= p_sys->i_pcr )
691     {
692         p_sys->i_pcr = i_ts_min;
693         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
694         UpdateSeekPoint( p_demux, p_sys->i_pcr );
695     }
696
697     es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame );
698
699     av_free_packet( &pkt );
700     return 1;
701 }
702
703 static void UpdateSeekPoint( demux_t *p_demux, int64_t i_time )
704 {
705     demux_sys_t *p_sys = p_demux->p_sys;
706     int i;
707
708     if( !p_sys->p_title )
709         return;
710
711     for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
712     {
713         if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
714             break;
715     }
716     i--;
717
718     if( i != p_demux->info.i_seekpoint && i >= 0 )
719     {
720         p_demux->info.i_seekpoint = i;
721         p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
722     }
723 }
724
725 static void ResetTime( demux_t *p_demux, int64_t i_time )
726 {
727     demux_sys_t *p_sys = p_demux->p_sys;
728
729     if( p_sys->ic->start_time == (int64_t)AV_NOPTS_VALUE || i_time < 0 )
730         i_time = VLC_TS_INVALID;
731     else if( i_time == 0 )
732         i_time = 1;
733
734     p_sys->i_pcr = i_time;
735     for( int i = 0; i < p_sys->i_tk; i++ )
736         p_sys->tk_pcr[i] = i_time;
737
738     if( i_time > VLC_TS_INVALID )
739     {
740         es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_time );
741         UpdateSeekPoint( p_demux, i_time );
742     }
743 }
744
745 static block_t *BuildSsaFrame( const AVPacket *p_pkt, unsigned i_order )
746 {
747     if( p_pkt->size <= 0 )
748         return NULL;
749
750     char buffer[256];
751     const size_t i_buffer_size = __MIN( (int)sizeof(buffer) - 1, p_pkt->size );
752     memcpy( buffer, p_pkt->data, i_buffer_size );
753     buffer[i_buffer_size] = '\0';
754
755     /* */
756     int i_layer;
757     int h0, m0, s0, c0;
758     int h1, m1, s1, c1;
759     int i_position = 0;
760     if( sscanf( buffer, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,%n", &i_layer,
761                 &h0, &m0, &s0, &c0, &h1, &m1, &s1, &c1, &i_position ) < 9 )
762         return NULL;
763     if( i_position <= 0 || (unsigned)i_position >= i_buffer_size )
764         return NULL;
765
766     char *p;
767     if( asprintf( &p, "%u,%d,%.*s", i_order, i_layer, p_pkt->size - i_position, p_pkt->data + i_position ) < 0 )
768         return NULL;
769
770     block_t *p_frame = block_heap_Alloc( p, strlen(p) + 1 );
771     if( p_frame )
772         p_frame->i_length = CLOCK_FREQ * ((h1-h0) * 3600 +
773                                           (m1-m0) * 60 +
774                                           (s1-s0) * 1) +
775                             CLOCK_FREQ * (c1-c0) / 100;
776     return p_frame;
777 }
778
779 /*****************************************************************************
780  * Control:
781  *****************************************************************************/
782 static int Control( demux_t *p_demux, int i_query, va_list args )
783 {
784     demux_sys_t *p_sys = p_demux->p_sys;
785     const int64_t i_start_time = p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ? p_sys->ic->start_time : 0;
786     double f, *pf;
787     int64_t i64, *pi64;
788
789     switch( i_query )
790     {
791         case DEMUX_GET_POSITION:
792             pf = (double*) va_arg( args, double* ); *pf = 0.0;
793             i64 = stream_Size( p_demux->s );
794             if( i64 > 0 )
795             {
796                 double current = stream_Tell( p_demux->s );
797                 *pf = current / (double)i64;
798             }
799
800             if( (p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE) && (p_sys->i_pcr > 0) )
801             {
802                 *pf = (double)p_sys->i_pcr / (double)p_sys->ic->duration;
803             }
804
805             return VLC_SUCCESS;
806
807         case DEMUX_SET_POSITION:
808             f = (double) va_arg( args, double );
809             i64 = p_sys->ic->duration * f + i_start_time;
810
811             msg_Warn( p_demux, "DEMUX_SET_POSITION: %"PRId64, i64 );
812
813             /* If we have a duration, we prefer to seek by time
814                but if we don't, or if the seek fails, try BYTE seeking */
815             if( p_sys->ic->duration == (int64_t)AV_NOPTS_VALUE ||
816                 (av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BACKWARD ) < 0) )
817             {
818                 int64_t i_size = stream_Size( p_demux->s );
819                 i64 = (i_size * f);
820
821                 msg_Warn( p_demux, "DEMUX_SET_BYTE_POSITION: %"PRId64, i64 );
822                 if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BYTE ) < 0 )
823                     return VLC_EGENERIC;
824
825                 ResetTime( p_demux, -1 );
826             }
827             else
828             {
829                 ResetTime( p_demux, i64 - i_start_time );
830             }
831             return VLC_SUCCESS;
832
833         case DEMUX_GET_LENGTH:
834             pi64 = (int64_t*)va_arg( args, int64_t * );
835             if( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE )
836                 *pi64 = p_sys->ic->duration * 1000000 / AV_TIME_BASE;
837             else
838                 *pi64 = 0;
839             return VLC_SUCCESS;
840
841         case DEMUX_GET_TIME:
842             pi64 = (int64_t*)va_arg( args, int64_t * );
843             *pi64 = p_sys->i_pcr;
844             return VLC_SUCCESS;
845
846         case DEMUX_SET_TIME:
847         {
848             i64 = (int64_t)va_arg( args, int64_t );
849             i64 = i64 *AV_TIME_BASE / 1000000 + i_start_time;
850
851             msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
852
853             if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BACKWARD ) < 0 )
854             {
855                 return VLC_EGENERIC;
856             }
857             ResetTime( p_demux, i64 - i_start_time );
858             return VLC_SUCCESS;
859         }
860
861         case DEMUX_HAS_UNSUPPORTED_META:
862         {
863             bool *pb_bool = (bool*)va_arg( args, bool* );
864             *pb_bool = true;
865             return VLC_SUCCESS;
866         }
867
868
869         case DEMUX_GET_META:
870         {
871             static const char names[][10] = {
872                 [vlc_meta_Title] = "title",
873                 [vlc_meta_Artist] = "artist",
874                 [vlc_meta_Genre] = "genre",
875                 [vlc_meta_Copyright] = "copyright",
876                 [vlc_meta_Album] = "album",
877                 //[vlc_meta_TrackNumber] -- TODO: parse number/total value
878                 [vlc_meta_Description] = "comment",
879                 //[vlc_meta_Rating]
880                 [vlc_meta_Date] = "date",
881                 [vlc_meta_Setting] = "encoder",
882                 //[vlc_meta_URL]
883                 [vlc_meta_Language] = "language",
884                 //[vlc_meta_NowPlaying]
885                 [vlc_meta_Publisher] = "publisher",
886                 [vlc_meta_EncodedBy] = "encoded_by",
887                 //[vlc_meta_ArtworkURL]
888                 //[vlc_meta_TrackID]
889                 //[vlc_meta_TrackTotal]
890             };
891             vlc_meta_t *p_meta = va_arg( args, vlc_meta_t * );
892             AVDictionary *dict = p_sys->ic->metadata;
893
894             for( unsigned i = 0; i < sizeof(names) / sizeof(*names); i++)
895             {
896                 if( !names[i][0] )
897                     continue;
898
899                 AVDictionaryEntry *e = av_dict_get( dict, names[i], NULL, 0 );
900                 if( e != NULL && e->value != NULL && IsUTF8(e->value) )
901                     vlc_meta_Set( p_meta, i, e->value );
902             }
903             return VLC_SUCCESS;
904         }
905
906         case DEMUX_GET_ATTACHMENTS:
907         {
908             input_attachment_t ***ppp_attach =
909                 (input_attachment_t***)va_arg( args, input_attachment_t*** );
910             int *pi_int = (int*)va_arg( args, int * );
911             int i;
912
913             if( p_sys->i_attachments <= 0 )
914                 return VLC_EGENERIC;
915
916             *pi_int = p_sys->i_attachments;;
917             *ppp_attach = malloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
918             for( i = 0; i < p_sys->i_attachments; i++ )
919                 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
920             return VLC_SUCCESS;
921         }
922
923         case DEMUX_GET_TITLE_INFO:
924         {
925             input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
926             int *pi_int    = (int*)va_arg( args, int* );
927             int *pi_title_offset = (int*)va_arg( args, int* );
928             int *pi_seekpoint_offset = (int*)va_arg( args, int* );
929
930             if( !p_sys->p_title )
931                 return VLC_EGENERIC;
932
933             *pi_int = 1;
934             *ppp_title = malloc( sizeof( input_title_t**) );
935             (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
936             *pi_title_offset = 0;
937             *pi_seekpoint_offset = 0;
938             return VLC_SUCCESS;
939         }
940         case DEMUX_SET_TITLE:
941         {
942             const int i_title = (int)va_arg( args, int );
943             if( !p_sys->p_title || i_title != 0 )
944                 return VLC_EGENERIC;
945             return VLC_SUCCESS;
946         }
947         case DEMUX_SET_SEEKPOINT:
948         {
949             const int i_seekpoint = (int)va_arg( args, int );
950             if( !p_sys->p_title )
951                 return VLC_EGENERIC;
952
953             i64 = p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset *
954                   AV_TIME_BASE / 1000000 + i_start_time;
955
956             msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
957
958             if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BACKWARD ) < 0 )
959             {
960                 return VLC_EGENERIC;
961             }
962             ResetTime( p_demux, i64 - i_start_time );
963             return VLC_SUCCESS;
964         }
965
966
967         default:
968             return VLC_EGENERIC;
969     }
970 }
971
972 /*****************************************************************************
973  * I/O wrappers for libavformat
974  *****************************************************************************/
975 static int IORead( void *opaque, uint8_t *buf, int buf_size )
976 {
977     demux_t *p_demux = opaque;
978     if( buf_size < 0 ) return -1;
979     int i_ret = stream_Read( p_demux->s, buf, buf_size );
980     return i_ret ? i_ret : -1;
981 }
982
983 static int64_t IOSeek( void *opaque, int64_t offset, int whence )
984 {
985     demux_t *p_demux = opaque;
986     int64_t i_absolute;
987     int64_t i_size = stream_Size( p_demux->s );
988
989 #ifdef AVFORMAT_DEBUG
990     msg_Warn( p_demux, "IOSeek offset: %"PRId64", whence: %i", offset, whence );
991 #endif
992
993     switch( whence )
994     {
995 #ifdef AVSEEK_SIZE
996         case AVSEEK_SIZE:
997             return i_size;
998 #endif
999         case SEEK_SET:
1000             i_absolute = (int64_t)offset;
1001             break;
1002         case SEEK_CUR:
1003             i_absolute = stream_Tell( p_demux->s ) + (int64_t)offset;
1004             break;
1005         case SEEK_END:
1006             i_absolute = i_size + (int64_t)offset;
1007             break;
1008         default:
1009             return -1;
1010
1011     }
1012
1013     if( i_absolute < 0 )
1014     {
1015         msg_Dbg( p_demux, "Trying to seek before the beginning" );
1016         return -1;
1017     }
1018
1019     if( i_size > 0 && i_absolute >= i_size )
1020     {
1021         msg_Dbg( p_demux, "Trying to seek too far : EOF?" );
1022         return -1;
1023     }
1024
1025     if( stream_Seek( p_demux->s, i_absolute ) )
1026     {
1027         msg_Warn( p_demux, "we were not allowed to seek, or EOF " );
1028         return -1;
1029     }
1030
1031     return stream_Tell( p_demux->s );
1032 }
1033
1034 #endif /* HAVE_LIBAVFORMAT_AVFORMAT_H */