]> git.sesse.net Git - vlc/blob - modules/demux/avformat/demux.c
avformat: fix non-file usage after [1644d683e9df]
[vlc] / modules / demux / avformat / demux.c
1 /*****************************************************************************
2  * demux.c: demuxer using ffmpeg (libavformat).
3  *****************************************************************************
4  * Copyright (C) 2004-2009 the VideoLAN team
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
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 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 General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, 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 /* ffmpeg header */
42 #if defined(HAVE_LIBAVFORMAT_AVFORMAT_H)
43 #   include <libavformat/avformat.h>
44 #elif defined(HAVE_FFMPEG_AVFORMAT_H)
45 #   include <ffmpeg/avformat.h>
46 #endif
47
48 #include "../../codec/avcodec/avcodec.h"
49 #include "avformat.h"
50 #include "../xiph.h"
51 #include "../vobsub.h"
52
53 //#define AVFORMAT_DEBUG 1
54
55 /* Version checking */
56 #if defined(HAVE_FFMPEG_AVFORMAT_H) || defined(HAVE_LIBAVFORMAT_AVFORMAT_H)
57
58 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(50<<8)+0) )
59 #   define HAVE_FFMPEG_CODEC_ATTACHMENT 1
60 #endif
61
62 #if (LIBAVFORMAT_VERSION_INT >= ((52<<16)+(15<<8)+0) )
63 #   define HAVE_FFMPEG_CHAPTERS 1
64 #endif
65
66 /*****************************************************************************
67  * demux_sys_t: demux descriptor
68  *****************************************************************************/
69 struct demux_sys_t
70 {
71     ByteIOContext   io;
72     int             io_buffer_size;
73     uint8_t        *io_buffer;
74
75     AVInputFormat  *fmt;
76     AVFormatContext *ic;
77     URLContext     url;
78     URLProtocol    prot;
79
80     int             i_tk;
81     es_out_id_t     **tk;
82
83     int64_t     i_pcr;
84     int64_t     i_pcr_inc;
85     int         i_pcr_tk;
86
87     unsigned    i_ssa_order;
88
89     int                i_attachments;
90     input_attachment_t **attachments;
91
92     /* Only one title with seekpoints possible atm. */
93     input_title_t *p_title;
94 };
95
96 /*****************************************************************************
97  * Local prototypes
98  *****************************************************************************/
99 static int Demux  ( demux_t *p_demux );
100 static int Control( demux_t *p_demux, int i_query, va_list args );
101
102 static int IORead( void *opaque, uint8_t *buf, int buf_size );
103 static int64_t IOSeek( void *opaque, int64_t offset, int whence );
104
105 static block_t *BuildSsaFrame( const AVPacket *p_pkt, unsigned i_order );
106 static void UpdateSeekPoint( demux_t *p_demux, int64_t i_time );
107
108 /*****************************************************************************
109  * Open
110  *****************************************************************************/
111 int OpenDemux( vlc_object_t *p_this )
112 {
113     demux_t       *p_demux = (demux_t*)p_this;
114     demux_sys_t   *p_sys;
115     AVProbeData   pd;
116     AVInputFormat *fmt;
117     unsigned int  i;
118     int64_t       i_start_time = -1;
119     bool          b_can_seek;
120     char         *psz_url;
121
122     if( p_demux->psz_file )
123         psz_url = strdup( p_demux->psz_file );
124     else
125     {
126         if( asprintf( &psz_url, "%s://%s", p_demux->psz_access, p_demux->psz_location ) == -1)
127             return VLC_ENOMEM;
128     }
129     msg_Dbg( p_demux, "trying url: %s", psz_url );
130     /* Init Probe data */
131     pd.filename = psz_url;
132     if( ( pd.buf_size = stream_Peek( p_demux->s, &pd.buf, 2048 + 213 ) ) <= 0 )
133     {
134         free( psz_url );
135         msg_Warn( p_demux, "cannot peek" );
136         return VLC_EGENERIC;
137     }
138
139     vlc_avcodec_lock();
140     av_register_all(); /* Can be called several times */
141     vlc_avcodec_unlock();
142
143     /* Guess format */
144     if( !( fmt = av_probe_input_format( &pd, 1 ) ) )
145     {
146         msg_Dbg( p_demux, "couldn't guess format" );
147         free( psz_url );
148         return VLC_EGENERIC;
149     }
150
151     /* Don't try to handle MPEG unless forced */
152     if( !p_demux->b_force &&
153         ( !strcmp( fmt->name, "mpeg" ) ||
154           !strcmp( fmt->name, "vcd" ) ||
155           !strcmp( fmt->name, "vob" ) ||
156           !strcmp( fmt->name, "mpegts" ) ||
157           /* libavformat's redirector won't work */
158           !strcmp( fmt->name, "redir" ) ||
159           !strcmp( fmt->name, "sdp" ) ) )
160     {
161         free( psz_url );
162         return VLC_EGENERIC;
163     }
164
165     /* Don't trigger false alarms on bin files */
166     if( !p_demux->b_force && !strcmp( fmt->name, "psxstr" ) )
167     {
168         int i_len;
169
170         if( !p_demux->psz_file )
171         {
172             free( psz_url );
173             return VLC_EGENERIC;
174         }
175
176         i_len = strlen( p_demux->psz_file );
177         if( i_len < 4 )
178         {
179             free( psz_url );
180             return VLC_EGENERIC;
181         }
182
183         if( strcasecmp( &p_demux->psz_file[i_len - 4], ".str" ) &&
184             strcasecmp( &p_demux->psz_file[i_len - 4], ".xai" ) &&
185             strcasecmp( &p_demux->psz_file[i_len - 3], ".xa" ) )
186         {
187             free( psz_url );
188             return VLC_EGENERIC;
189         }
190     }
191
192     msg_Dbg( p_demux, "detected format: %s", fmt->name );
193
194     /* Fill p_demux fields */
195     p_demux->pf_demux = Demux;
196     p_demux->pf_control = Control;
197     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
198     p_sys->ic = 0;
199     p_sys->fmt = fmt;
200     p_sys->i_tk = 0;
201     p_sys->tk = NULL;
202     p_sys->i_pcr_tk = -1;
203     p_sys->i_pcr = -1;
204     p_sys->i_ssa_order = 0;
205     TAB_INIT( p_sys->i_attachments, p_sys->attachments);
206     p_sys->p_title = NULL;
207
208     /* Create I/O wrapper */
209     p_sys->io_buffer_size = 32768;  /* FIXME */
210     p_sys->io_buffer = malloc( p_sys->io_buffer_size );
211     p_sys->url.priv_data = p_demux;
212     p_sys->url.prot = &p_sys->prot;
213     p_sys->url.prot->name = "VLC I/O wrapper";
214     p_sys->url.prot->url_open = 0;
215     p_sys->url.prot->url_read =
216                     (int (*) (URLContext *, unsigned char *, int))IORead;
217     p_sys->url.prot->url_write = 0;
218     p_sys->url.prot->url_seek =
219                     (int64_t (*) (URLContext *, int64_t, int))IOSeek;
220     p_sys->url.prot->url_close = 0;
221     p_sys->url.prot->next = 0;
222     init_put_byte( &p_sys->io, p_sys->io_buffer, p_sys->io_buffer_size,
223                    0, &p_sys->url, IORead, NULL, IOSeek );
224
225     stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_can_seek );
226     if( !b_can_seek )
227     {
228        /* Tell avformat that input is stream, so it doesn't get stuck
229        when trying av_find_stream_info() trying to seek all the wrong places
230        init_put_byte defaults io.is_streamed=0, so thats why we set them after it
231        */
232        p_sys->url.is_streamed = 1;
233        p_sys->io.is_streamed = 1;
234     }
235
236
237     /* Open it */
238     if( av_open_input_stream( &p_sys->ic, &p_sys->io, psz_url,
239                               p_sys->fmt, NULL ) )
240     {
241         msg_Err( p_demux, "av_open_input_stream failed" );
242         free( psz_url );
243         CloseDemux( p_this );
244         return VLC_EGENERIC;
245     }
246     free( psz_url );
247     psz_url = NULL;
248
249     vlc_avcodec_lock(); /* avformat calls avcodec behind our back!!! */
250     if( av_find_stream_info( p_sys->ic ) < 0 )
251     {
252         msg_Warn( p_demux, "av_find_stream_info failed" );
253     }
254     vlc_avcodec_unlock();
255
256     for( i = 0; i < p_sys->ic->nb_streams; i++ )
257     {
258         AVStream *s = p_sys->ic->streams[i];
259         AVCodecContext *cc = s->codec;
260
261         es_out_id_t  *es;
262         es_format_t  fmt;
263         vlc_fourcc_t fcc;
264         const char *psz_type = "unknown";
265
266         if( !GetVlcFourcc( cc->codec_id, NULL, &fcc, NULL ) )
267             fcc = VLC_FOURCC( 'u', 'n', 'd', 'f' );
268
269         switch( cc->codec_type )
270         {
271         case CODEC_TYPE_AUDIO:
272             es_format_Init( &fmt, AUDIO_ES, fcc );
273             fmt.i_bitrate = cc->bit_rate;
274             fmt.audio.i_channels = cc->channels;
275             fmt.audio.i_rate = cc->sample_rate;
276 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
277             fmt.audio.i_bitspersample = cc->bits_per_sample;
278 #else
279             fmt.audio.i_bitspersample = cc->bits_per_coded_sample;
280 #endif
281             fmt.audio.i_blockalign = cc->block_align;
282             psz_type = "audio";
283             break;
284
285         case CODEC_TYPE_VIDEO:
286             es_format_Init( &fmt, VIDEO_ES, fcc );
287
288             /* Special case for raw video data */
289             if( cc->codec_id == CODEC_ID_RAWVIDEO )
290             {
291                 msg_Dbg( p_demux, "raw video, pixel format: %i", cc->pix_fmt );
292                 if( GetVlcChroma( &fmt.video, cc->pix_fmt ) != VLC_SUCCESS)
293                 {
294                     msg_Err( p_demux, "was unable to find a FourCC match for raw video" );
295                 }
296                 else
297                     fmt.i_codec = fmt.video.i_chroma;
298             }
299             /* We need this for the h264 packetizer */
300             else if( cc->codec_id == CODEC_ID_H264 && ( !strcmp( p_sys->fmt->name, "flv" ) ||
301                 !strcmp( p_sys->fmt->name, "matroska" ) || !strcmp( p_sys->fmt->name, "mp4" ) ) )
302                 fmt.i_original_fourcc = VLC_FOURCC( 'a', 'v', 'c', '1' );
303
304             fmt.video.i_width = cc->width;
305             fmt.video.i_height = cc->height;
306             if( cc->palctrl )
307             {
308                 fmt.video.p_palette = malloc( sizeof(video_palette_t) );
309                 *fmt.video.p_palette = *(video_palette_t *)cc->palctrl;
310             }
311             psz_type = "video";
312             fmt.video.i_frame_rate = cc->time_base.den;
313             fmt.video.i_frame_rate_base = cc->time_base.num;
314             break;
315
316         case CODEC_TYPE_SUBTITLE:
317             es_format_Init( &fmt, SPU_ES, fcc );
318             if( strncmp( p_sys->ic->iformat->name, "matroska", 8 ) == 0 &&
319                 cc->codec_id == CODEC_ID_DVD_SUBTITLE &&
320                 cc->extradata != NULL &&
321                 cc->extradata_size > 0 )
322             {
323                 char *psz_start;
324                 char *psz_buf = malloc( cc->extradata_size + 1);
325                 if( psz_buf != NULL )
326                 {
327                     memcpy( psz_buf, cc->extradata , cc->extradata_size );
328                     psz_buf[cc->extradata_size] = '\0';
329
330                     psz_start = strstr( psz_buf, "size:" );
331                     if( psz_start &&
332                         vobsub_size_parse( psz_start,
333                                            &fmt.subs.spu.i_original_frame_width,
334                                            &fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
335                     {
336                         msg_Dbg( p_demux, "original frame size: %dx%d",
337                                  fmt.subs.spu.i_original_frame_width,
338                                  fmt.subs.spu.i_original_frame_height );
339                     }
340                     else
341                     {
342                         msg_Warn( p_demux, "reading original frame size failed" );
343                     }
344
345                     psz_start = strstr( psz_buf, "palette:" );
346                     if( psz_start &&
347                         vobsub_palette_parse( psz_start, &fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
348                     {
349                         fmt.subs.spu.palette[0] =  0xBeef;
350                         msg_Dbg( p_demux, "vobsub palette read" );
351                     }
352                     else
353                     {
354                         msg_Warn( p_demux, "reading original palette failed" );
355                     }
356                     free( psz_buf );
357                 }
358             }
359
360             psz_type = "subtitle";
361             break;
362
363         default:
364             es_format_Init( &fmt, UNKNOWN_ES, 0 );
365 #ifdef HAVE_FFMPEG_CODEC_ATTACHMENT
366             if( cc->codec_type == CODEC_TYPE_ATTACHMENT )
367             {
368                 input_attachment_t *p_attachment;
369                 psz_type = "attachment";
370                 if( cc->codec_id == CODEC_ID_TTF )
371                 {
372                     p_attachment = vlc_input_attachment_New( s->filename, "application/x-truetype-font", NULL,
373                                              cc->extradata, (int)cc->extradata_size );
374                     TAB_APPEND( p_sys->i_attachments, p_sys->attachments, p_attachment );
375                 }
376                 else msg_Warn( p_demux, "unsupported attachment type in ffmpeg demux" );
377             }
378             break;
379 #endif
380
381             if( cc->codec_type == CODEC_TYPE_DATA )
382                 psz_type = "data";
383
384             msg_Warn( p_demux, "unsupported track type in ffmpeg demux" );
385             break;
386         }
387         fmt.psz_language = strdup( s->language );
388         if( s->disposition & AV_DISPOSITION_DEFAULT )
389             fmt.i_priority = 1000;
390
391 #ifdef HAVE_FFMPEG_CODEC_ATTACHMENT
392         if( cc->codec_type != CODEC_TYPE_ATTACHMENT )
393 #endif
394         {
395             const bool    b_ogg = !strcmp( p_sys->fmt->name, "ogg" );
396             const uint8_t *p_extra = cc->extradata;
397             unsigned      i_extra  = cc->extradata_size;
398
399             if( cc->codec_id == CODEC_ID_THEORA && b_ogg )
400             {
401                 unsigned pi_size[3];
402                 void     *pp_data[3];
403                 unsigned i_count;
404                 for( i_count = 0; i_count < 3; i_count++ )
405                 {
406                     if( i_extra < 2 )
407                         break;
408                     pi_size[i_count] = GetWBE( p_extra );
409                     pp_data[i_count] = (uint8_t*)&p_extra[2];
410                     if( i_extra < pi_size[i_count] + 2 )
411                         break;
412
413                     p_extra += 2 + pi_size[i_count];
414                     i_extra -= 2 + pi_size[i_count];
415                 }
416                 if( i_count > 0 && xiph_PackHeaders( &fmt.i_extra, &fmt.p_extra,
417                                                      pi_size, pp_data, i_count ) )
418                 {
419                     fmt.i_extra = 0;
420                     fmt.p_extra = NULL;
421                 }
422             }
423             else if( cc->codec_id == CODEC_ID_SPEEX && b_ogg )
424             {
425                 uint8_t p_dummy_comment[] = {
426                     0, 0, 0, 0,
427                     0, 0, 0, 0,
428                 };
429                 unsigned pi_size[2];
430                 void     *pp_data[2];
431
432                 pi_size[0] = i_extra;
433                 pp_data[0] = (uint8_t*)p_extra;
434
435                 pi_size[1] = sizeof(p_dummy_comment);
436                 pp_data[1] = p_dummy_comment;
437
438                 if( pi_size[0] > 0 && xiph_PackHeaders( &fmt.i_extra, &fmt.p_extra,
439                                                         pi_size, pp_data, 2 ) )
440                 {
441                     fmt.i_extra = 0;
442                     fmt.p_extra = NULL;
443                 }
444             }
445             else if( cc->extradata_size > 0 )
446             {
447                 fmt.p_extra = malloc( i_extra );
448                 if( fmt.p_extra )
449                 {
450                     fmt.i_extra = i_extra;
451                     memcpy( fmt.p_extra, p_extra, i_extra );
452                 }
453             }
454         }
455         es = es_out_Add( p_demux->out, &fmt );
456         if( s->disposition & AV_DISPOSITION_DEFAULT )
457             es_out_Control( p_demux->out, ES_OUT_SET_ES_DEFAULT, es );
458         es_format_Clean( &fmt );
459
460         msg_Dbg( p_demux, "adding es: %s codec = %4.4s",
461                  psz_type, (char*)&fcc );
462         TAB_APPEND( p_sys->i_tk, p_sys->tk, es );
463     }
464     if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
465         i_start_time = p_sys->ic->start_time * 1000000 / AV_TIME_BASE;
466
467     msg_Dbg( p_demux, "AVFormat supported stream" );
468     msg_Dbg( p_demux, "    - format = %s (%s)",
469              p_sys->fmt->name, p_sys->fmt->long_name );
470     msg_Dbg( p_demux, "    - start time = %"PRId64, i_start_time );
471     msg_Dbg( p_demux, "    - duration = %"PRId64,
472              ( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE ) ?
473              p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 );
474
475 #ifdef HAVE_FFMPEG_CHAPTERS
476     if( p_sys->ic->nb_chapters > 0 )
477         p_sys->p_title = vlc_input_title_New();
478     for( i = 0; i < p_sys->ic->nb_chapters; i++ )
479     {
480         seekpoint_t *s = vlc_seekpoint_New();
481
482         if( p_sys->ic->chapters[i]->title )
483         {
484             s->psz_name = strdup( p_sys->ic->chapters[i]->title );
485             EnsureUTF8( s->psz_name );
486             msg_Dbg( p_demux, "    - chapter %d: %s", i, s->psz_name );
487         }
488         s->i_time_offset = p_sys->ic->chapters[i]->start * 1000000 *
489             p_sys->ic->chapters[i]->time_base.num /
490             p_sys->ic->chapters[i]->time_base.den -
491             (i_start_time != -1 ? i_start_time : 0 );
492         TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
493     }
494 #endif
495
496     return VLC_SUCCESS;
497 }
498
499 /*****************************************************************************
500  * Close
501  *****************************************************************************/
502 void CloseDemux( vlc_object_t *p_this )
503 {
504     demux_t     *p_demux = (demux_t*)p_this;
505     demux_sys_t *p_sys = p_demux->p_sys;
506
507     FREENULL( p_sys->tk );
508
509     if( p_sys->ic ) av_close_input_stream( p_sys->ic );
510
511     for( int i = 0; i < p_sys->i_attachments; i++ )
512         free( p_sys->attachments[i] );
513     TAB_CLEAN( p_sys->i_attachments, p_sys->attachments);
514
515     if( p_sys->p_title )
516         vlc_input_title_Delete( p_sys->p_title );
517
518     free( p_sys->io_buffer );
519     free( p_sys );
520 }
521
522 /*****************************************************************************
523  * Demux:
524  *****************************************************************************/
525 static int Demux( demux_t *p_demux )
526 {
527     demux_sys_t *p_sys = p_demux->p_sys;
528     AVPacket    pkt;
529     block_t     *p_frame;
530     int64_t     i_start_time;
531
532     /* Read a frame */
533     if( av_read_frame( p_sys->ic, &pkt ) )
534     {
535         return 0;
536     }
537     if( pkt.stream_index < 0 || pkt.stream_index >= p_sys->i_tk )
538     {
539         av_free_packet( &pkt );
540         return 1;
541     }
542     const AVStream *p_stream = p_sys->ic->streams[pkt.stream_index];
543     if( p_stream->time_base.den <= 0 )
544     {
545         msg_Warn( p_demux, "Invalid time base for the stream %d", pkt.stream_index );
546         av_free_packet( &pkt );
547         return 1;
548     }
549     if( p_stream->codec->codec_id == CODEC_ID_SSA )
550     {
551         p_frame = BuildSsaFrame( &pkt, p_sys->i_ssa_order++ );
552         if( !p_frame )
553         {
554             av_free_packet( &pkt );
555             return 1;
556         }
557     }
558     else
559     {
560         if( ( p_frame = block_New( p_demux, pkt.size ) ) == NULL )
561         {
562             av_free_packet( &pkt );
563             return 0;
564         }
565         memcpy( p_frame->p_buffer, pkt.data, pkt.size );
566     }
567
568     if( pkt.flags & PKT_FLAG_KEY )
569         p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
570
571     i_start_time = ( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) ?
572         ( p_sys->ic->start_time * 1000000 / AV_TIME_BASE )  : 0;
573
574     p_frame->i_dts = ( pkt.dts == (int64_t)AV_NOPTS_VALUE ) ?
575         VLC_TS_INVALID : (pkt.dts) * 1000000 *
576         p_stream->time_base.num /
577         p_stream->time_base.den - i_start_time + VLC_TS_0;
578     p_frame->i_pts = ( pkt.pts == (int64_t)AV_NOPTS_VALUE ) ?
579         VLC_TS_INVALID : (pkt.pts) * 1000000 *
580         p_stream->time_base.num /
581         p_stream->time_base.den - i_start_time + VLC_TS_0;
582     if( pkt.duration > 0 && p_frame->i_length <= 0 )
583         p_frame->i_length = pkt.duration * 1000000 *
584             p_stream->time_base.num /
585             p_stream->time_base.den;
586
587     if( pkt.dts != AV_NOPTS_VALUE && pkt.dts == pkt.pts &&
588         p_stream->codec->codec_type == CODEC_TYPE_VIDEO )
589     {
590         /* Add here notoriously bugged file formats/samples regarding PTS */
591         if( !strcmp( p_sys->fmt->name, "flv" ) )
592             p_frame->i_pts = VLC_TS_INVALID;
593     }
594 #ifdef AVFORMAT_DEBUG
595     msg_Dbg( p_demux, "tk[%d] dts=%"PRId64" pts=%"PRId64,
596              pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
597 #endif
598
599     if( p_frame->i_dts > VLC_TS_INVALID  &&
600         ( pkt.stream_index == p_sys->i_pcr_tk || p_sys->i_pcr_tk < 0 ) )
601     {
602         p_sys->i_pcr_tk = pkt.stream_index;
603         p_sys->i_pcr = p_frame->i_dts;
604
605         es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
606     }
607
608     es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame );
609
610     UpdateSeekPoint( p_demux, p_sys->i_pcr);
611     av_free_packet( &pkt );
612     return 1;
613 }
614
615 static void UpdateSeekPoint( demux_t *p_demux, int64_t i_time )
616 {
617     demux_sys_t *p_sys = p_demux->p_sys;
618     int i;
619
620     if( !p_sys->p_title )
621         return;
622
623     for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
624     {
625         if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
626             break;
627     }
628     i--;
629
630     if( i != p_demux->info.i_seekpoint && i >= 0 )
631     {
632         p_demux->info.i_seekpoint = i;
633         p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
634     }
635 }
636
637 static block_t *BuildSsaFrame( const AVPacket *p_pkt, unsigned i_order )
638 {
639     if( p_pkt->size <= 0 )
640         return NULL;
641
642     char buffer[256];
643     const size_t i_buffer_size = __MIN( sizeof(buffer) - 1, p_pkt->size );
644     memcpy( buffer, p_pkt->data, i_buffer_size );
645     buffer[i_buffer_size] = '\0';
646
647     /* */
648     int i_layer;
649     int h0, m0, s0, c0;
650     int h1, m1, s1, c1;
651     int i_position = 0;
652     if( sscanf( buffer, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,%n", &i_layer,
653                 &h0, &m0, &s0, &c0, &h1, &m1, &s1, &c1, &i_position ) < 9 )
654         return NULL;
655     if( i_position <= 0 || i_position >= i_buffer_size )
656         return NULL;
657
658     char *p;
659     if( asprintf( &p, "%u,%d,%.*s", i_order, i_layer, p_pkt->size - i_position, p_pkt->data + i_position ) < 0 )
660         return NULL;
661
662     block_t *p_frame = block_heap_Alloc( p, p, strlen(p) + 1 );
663     if( p_frame )
664         p_frame->i_length = CLOCK_FREQ * ((h1 - h1) * 3600 +
665                                           (m1-m0) * 60 +
666                                           (s1-s0) * 1) +
667                             CLOCK_FREQ * (c1-c0) / 100;
668     return p_frame;
669 }
670
671 /*****************************************************************************
672  * Control:
673  *****************************************************************************/
674 static int Control( demux_t *p_demux, int i_query, va_list args )
675 {
676     demux_sys_t *p_sys = p_demux->p_sys;
677     double f, *pf;
678     int64_t i64, *pi64;
679
680     switch( i_query )
681     {
682         case DEMUX_GET_POSITION:
683             pf = (double*) va_arg( args, double* ); *pf = 0.0;
684             i64 = stream_Size( p_demux->s );
685             if( i64 > 0 )
686             {
687                 double current = stream_Tell( p_demux->s );
688                 *pf = current / (double)i64;
689             }
690
691             if( (p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE) && (p_sys->i_pcr > 0) )
692             {
693                 *pf = (double)p_sys->i_pcr / (double)p_sys->ic->duration;
694             }
695
696             return VLC_SUCCESS;
697
698         case DEMUX_SET_POSITION:
699             f = (double) va_arg( args, double );
700             if( p_sys->i_pcr > 0 )
701             {
702                 i64 = p_sys->ic->duration * f;
703                 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
704                     i64 += p_sys->ic->start_time;
705
706                 msg_Warn( p_demux, "DEMUX_SET_POSITION: %"PRId64, i64 );
707
708                 /* If we have a duration, we prefer to seek by time
709                    but if we don't, or if the seek fails, try BYTE seeking */
710                 if( p_sys->ic->duration == (int64_t)AV_NOPTS_VALUE ||
711                     (av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0) )
712                 {
713                     int64_t i_size = stream_Size( p_demux->s );
714                     i64 = (i_size * f);
715
716                     msg_Warn( p_demux, "DEMUX_SET_BYTE_POSITION: %"PRId64, i64 );
717                     if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BYTE ) < 0 )
718                         return VLC_EGENERIC;
719                 }
720                 else
721                 {
722                     UpdateSeekPoint( p_demux, i64 );
723                 }
724                 p_sys->i_pcr = -1; /* Invalidate time display */
725             }
726             return VLC_SUCCESS;
727
728         case DEMUX_GET_LENGTH:
729             pi64 = (int64_t*)va_arg( args, int64_t * );
730             if( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE )
731                 *pi64 = p_sys->ic->duration * 1000000 / AV_TIME_BASE;
732             else
733                 *pi64 = 0;
734             return VLC_SUCCESS;
735
736         case DEMUX_GET_TIME:
737             pi64 = (int64_t*)va_arg( args, int64_t * );
738             *pi64 = p_sys->i_pcr;
739             return VLC_SUCCESS;
740
741         case DEMUX_SET_TIME:
742             i64 = (int64_t)va_arg( args, int64_t );
743             i64 = i64 *AV_TIME_BASE / 1000000;
744             if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
745                 i64 += p_sys->ic->start_time;
746
747             msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
748
749             if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
750             {
751                 return VLC_EGENERIC;
752             }
753             p_sys->i_pcr = -1; /* Invalidate time display */
754             UpdateSeekPoint( p_demux, i64 );
755             return VLC_SUCCESS;
756
757         case DEMUX_HAS_UNSUPPORTED_META:
758         {
759             bool *pb_bool = (bool*)va_arg( args, bool* );
760             *pb_bool = true;
761             return VLC_SUCCESS;
762         }
763
764
765         case DEMUX_GET_META:
766         {
767             vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
768
769             if( !p_sys->ic->title[0] || !p_sys->ic->author[0] ||
770                 !p_sys->ic->copyright[0] || !p_sys->ic->comment[0] ||
771                 /*!p_sys->ic->album[0] ||*/ !p_sys->ic->genre[0] )
772             {
773                 return VLC_EGENERIC;
774             }
775
776             if( p_sys->ic->title[0] )
777                 vlc_meta_SetTitle( p_meta, p_sys->ic->title );
778             if( p_sys->ic->author[0] )
779                 vlc_meta_SetArtist( p_meta, p_sys->ic->author );
780             if( p_sys->ic->copyright[0] )
781                 vlc_meta_SetCopyright( p_meta, p_sys->ic->copyright );
782             if( p_sys->ic->comment[0] )
783                 vlc_meta_SetDescription( p_meta, p_sys->ic->comment );
784             if( p_sys->ic->genre[0] )
785                 vlc_meta_SetGenre( p_meta, p_sys->ic->genre );
786             return VLC_SUCCESS;
787         }
788
789         case DEMUX_GET_ATTACHMENTS:
790         {
791             input_attachment_t ***ppp_attach =
792                 (input_attachment_t***)va_arg( args, input_attachment_t*** );
793             int *pi_int = (int*)va_arg( args, int * );
794             int i;
795
796             if( p_sys->i_attachments <= 0 )
797                 return VLC_EGENERIC;
798
799             *pi_int = p_sys->i_attachments;;
800             *ppp_attach = malloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
801             for( i = 0; i < p_sys->i_attachments; i++ )
802                 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
803             return VLC_SUCCESS;
804         }
805
806         case DEMUX_GET_TITLE_INFO:
807         {
808             input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
809             int *pi_int    = (int*)va_arg( args, int* );
810             int *pi_title_offset = (int*)va_arg( args, int* );
811             int *pi_seekpoint_offset = (int*)va_arg( args, int* );
812
813             if( !p_sys->p_title )
814                 return VLC_EGENERIC;
815
816             *pi_int = 1;
817             *ppp_title = malloc( sizeof( input_title_t**) );
818             (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
819             *pi_title_offset = 0;
820             *pi_seekpoint_offset = 0;
821             return VLC_SUCCESS;
822         }
823         case DEMUX_SET_TITLE:
824         {
825             const int i_title = (int)va_arg( args, int );
826             if( !p_sys->p_title || i_title != 0 )
827                 return VLC_EGENERIC;
828             return VLC_SUCCESS;
829         }
830         case DEMUX_SET_SEEKPOINT:
831         {
832             const int i_seekpoint = (int)va_arg( args, int );
833             if( !p_sys->p_title )
834                 return VLC_EGENERIC;
835
836             i64 = p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset *AV_TIME_BASE / 1000000;
837             if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
838                 i64 += p_sys->ic->start_time;
839
840             msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
841
842             if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
843             {
844                 return VLC_EGENERIC;
845             }
846             p_sys->i_pcr = -1; /* Invalidate time display */
847             UpdateSeekPoint( p_demux, i64 );
848             return VLC_SUCCESS;
849         }
850
851
852         default:
853             return VLC_EGENERIC;
854     }
855 }
856
857 /*****************************************************************************
858  * I/O wrappers for libavformat
859  *****************************************************************************/
860 static int IORead( void *opaque, uint8_t *buf, int buf_size )
861 {
862     URLContext *p_url = opaque;
863     demux_t *p_demux = p_url->priv_data;
864     if( buf_size < 0 ) return -1;
865     int i_ret = stream_Read( p_demux->s, buf, buf_size );
866     return i_ret ? i_ret : -1;
867 }
868
869 static int64_t IOSeek( void *opaque, int64_t offset, int whence )
870 {
871     URLContext *p_url = opaque;
872     demux_t *p_demux = p_url->priv_data;
873     int64_t i_absolute;
874     int64_t i_size = stream_Size( p_demux->s );
875
876 #ifdef AVFORMAT_DEBUG
877     msg_Warn( p_demux, "IOSeek offset: %"PRId64", whence: %i", offset, whence );
878 #endif
879
880     switch( whence )
881     {
882 #ifdef AVSEEK_SIZE
883         case AVSEEK_SIZE:
884             return i_size;
885 #endif
886         case SEEK_SET:
887             i_absolute = (int64_t)offset;
888             break;
889         case SEEK_CUR:
890             i_absolute = stream_Tell( p_demux->s ) + (int64_t)offset;
891             break;
892         case SEEK_END:
893             i_absolute = i_size + (int64_t)offset;
894             break;
895         default:
896             return -1;
897
898     }
899
900     if( i_absolute < 0 )
901     {
902         msg_Dbg( p_demux, "Trying to seek before the beginning" );
903         return -1;
904     }
905
906     if( i_size > 0 && i_absolute >= i_size )
907     {
908         msg_Dbg( p_demux, "Trying to seek too far : EOF?" );
909         return -1;
910     }
911
912     if( stream_Seek( p_demux->s, i_absolute ) )
913     {
914         msg_Warn( p_demux, "we were not allowed to seek, or EOF " );
915         return -1;
916     }
917
918     return stream_Tell( p_demux->s );
919 }
920
921 #endif /* HAVE_LIBAVFORMAT_AVFORMAT_H */