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