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