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