]> git.sesse.net Git - vlc/blob - modules/demux/avformat/demux.c
4bc6873e5af5cbb6e1c741d4439822c43bc3ca26
[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 "../vobsub.h"
51
52 //#define AVFORMAT_DEBUG 1
53
54 /* Version checking */
55 #if defined(HAVE_FFMPEG_AVFORMAT_H) || defined(HAVE_LIBAVFORMAT_AVFORMAT_H)
56
57 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(50<<8)+0) )
58 #   define HAVE_FFMPEG_CODEC_ATTACHMENT 1
59 #endif
60
61 #if (LIBAVFORMAT_VERSION_INT >= ((52<<16)+(15<<8)+0) )
62 #   define HAVE_FFMPEG_CHAPTERS 1
63 #endif
64
65 /*****************************************************************************
66  * demux_sys_t: demux descriptor
67  *****************************************************************************/
68 struct demux_sys_t
69 {
70     ByteIOContext   io;
71     int             io_buffer_size;
72     uint8_t        *io_buffer;
73
74     AVInputFormat  *fmt;
75     AVFormatContext *ic;
76     URLContext     url;
77     URLProtocol    prot;
78
79     int             i_tk;
80     es_out_id_t     **tk;
81
82     int64_t     i_pcr;
83     int64_t     i_pcr_inc;
84     int         i_pcr_tk;
85
86     int                i_attachments;
87     input_attachment_t **attachments;
88
89     /* Only one title with seekpoints possible atm. */
90     input_title_t *p_title;
91 };
92
93 /*****************************************************************************
94  * Local prototypes
95  *****************************************************************************/
96 static int Demux  ( demux_t *p_demux );
97 static int Control( demux_t *p_demux, int i_query, va_list args );
98
99 static int IORead( void *opaque, uint8_t *buf, int buf_size );
100 static int64_t IOSeek( void *opaque, int64_t offset, int whence );
101
102 static void UpdateSeekPoint( demux_t *p_demux, int64_t i_time );
103
104 /*****************************************************************************
105  * Open
106  *****************************************************************************/
107 int OpenDemux( vlc_object_t *p_this )
108 {
109     demux_t       *p_demux = (demux_t*)p_this;
110     demux_sys_t   *p_sys;
111     AVProbeData   pd;
112     AVInputFormat *fmt;
113     unsigned int  i;
114     bool          b_avfmt_nofile;
115     int64_t       i_start_time = -1;
116
117     /* Init Probe data */
118     pd.filename = p_demux->psz_path;
119     if( ( pd.buf_size = stream_Peek( p_demux->s, &pd.buf, 2048 ) ) <= 0 )
120     {
121         msg_Warn( p_demux, "cannot peek" );
122         return VLC_EGENERIC;
123     }
124
125     av_register_all(); /* Can be called several times */
126
127     /* Guess format */
128     if( !( fmt = av_probe_input_format( &pd, 1 ) ) )
129     {
130         msg_Dbg( p_demux, "couldn't guess format" );
131         return VLC_EGENERIC;
132     }
133
134     /* Don't try to handle MPEG unless forced */
135     if( !p_demux->b_force &&
136         ( !strcmp( fmt->name, "mpeg" ) ||
137           !strcmp( fmt->name, "vcd" ) ||
138           !strcmp( fmt->name, "vob" ) ||
139           !strcmp( fmt->name, "mpegts" ) ||
140           /* libavformat's redirector won't work */
141           !strcmp( fmt->name, "redir" ) ||
142           !strcmp( fmt->name, "sdp" ) ) )
143     {
144         return VLC_EGENERIC;
145     }
146
147     /* Don't trigger false alarms on bin files */
148     if( !p_demux->b_force && !strcmp( fmt->name, "psxstr" ) )
149     {
150         int i_len;
151
152         if( !p_demux->psz_path ) return VLC_EGENERIC;
153
154         i_len = strlen( p_demux->psz_path );
155         if( i_len < 4 ) return VLC_EGENERIC;
156
157         if( strcasecmp( &p_demux->psz_path[i_len - 4], ".str" ) &&
158             strcasecmp( &p_demux->psz_path[i_len - 4], ".xai" ) &&
159             strcasecmp( &p_demux->psz_path[i_len - 3], ".xa" ) )
160         {
161             return VLC_EGENERIC;
162         }
163     }
164
165     msg_Dbg( p_demux, "detected format: %s", fmt->name );
166
167     /* Fill p_demux fields */
168     p_demux->pf_demux = Demux;
169     p_demux->pf_control = Control;
170     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
171     p_sys->ic = 0;
172     p_sys->fmt = fmt;
173     p_sys->i_tk = 0;
174     p_sys->tk = NULL;
175     p_sys->i_pcr_tk = -1;
176     p_sys->i_pcr = -1;
177     TAB_INIT( p_sys->i_attachments, p_sys->attachments);
178     p_sys->p_title = NULL;
179
180     /* Create I/O wrapper */
181     p_sys->io_buffer_size = 32768;  /* FIXME */
182     p_sys->io_buffer = malloc( p_sys->io_buffer_size );
183     p_sys->url.priv_data = p_demux;
184     p_sys->url.prot = &p_sys->prot;
185     p_sys->url.prot->name = "VLC I/O wrapper";
186     p_sys->url.prot->url_open = 0;
187     p_sys->url.prot->url_read =
188                     (int (*) (URLContext *, unsigned char *, int))IORead;
189     p_sys->url.prot->url_write = 0;
190     p_sys->url.prot->url_seek =
191                     (int64_t (*) (URLContext *, int64_t, int))IOSeek;
192     p_sys->url.prot->url_close = 0;
193     p_sys->url.prot->next = 0;
194     init_put_byte( &p_sys->io, p_sys->io_buffer, p_sys->io_buffer_size,
195                    0, &p_sys->url, IORead, NULL, IOSeek );
196
197     b_avfmt_nofile = p_sys->fmt->flags & AVFMT_NOFILE;
198     p_sys->fmt->flags |= AVFMT_NOFILE; /* libavformat must not fopen/fclose */
199
200     /* Open it */
201     if( av_open_input_stream( &p_sys->ic, &p_sys->io, p_demux->psz_path,
202                               p_sys->fmt, NULL ) )
203     {
204         msg_Err( p_demux, "av_open_input_stream failed" );
205         if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
206         CloseDemux( p_this );
207         return VLC_EGENERIC;
208     }
209
210     vlc_avcodec_lock(); /* avformat calls avcodec behind our back!!! */
211     if( av_find_stream_info( p_sys->ic ) < 0 )
212     {
213         vlc_avcodec_unlock();
214         msg_Err( p_demux, "av_find_stream_info failed" );
215         if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
216         CloseDemux( p_this );
217         return VLC_EGENERIC;
218     }
219     vlc_avcodec_unlock();
220     if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
221
222     for( i = 0; i < p_sys->ic->nb_streams; i++ )
223     {
224         AVCodecContext *cc = p_sys->ic->streams[i]->codec;
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.audio.i_channels = cc->channels;
238             fmt.audio.i_rate = cc->sample_rate;
239 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
240             fmt.audio.i_bitspersample = cc->bits_per_sample;
241 #else
242             fmt.audio.i_bitspersample = cc->bits_per_coded_sample;
243 #endif
244             fmt.audio.i_blockalign = cc->block_align;
245             psz_type = "audio";
246             break;
247
248         case CODEC_TYPE_VIDEO:
249             es_format_Init( &fmt, VIDEO_ES, fcc );
250
251             /* Special case for raw video data */
252             if( cc->codec_id == CODEC_ID_RAWVIDEO )
253             {
254                 msg_Dbg( p_demux, "raw video, pixel format: %i", cc->pix_fmt );
255                 if( GetVlcChroma( &fmt.video, cc->pix_fmt ) != VLC_SUCCESS)
256                 {
257                     msg_Err( p_demux, "was unable to find a FourCC match for raw video" );
258                 }
259                 else
260                     fmt.i_codec = fmt.video.i_chroma;
261             }
262
263             fmt.video.i_width = cc->width;
264             fmt.video.i_height = cc->height;
265             if( cc->palctrl )
266             {
267                 fmt.video.p_palette = malloc( sizeof(video_palette_t) );
268                 *fmt.video.p_palette = *(video_palette_t *)cc->palctrl;
269             }
270             psz_type = "video";
271             break;
272
273         case CODEC_TYPE_SUBTITLE:
274             es_format_Init( &fmt, SPU_ES, fcc );
275             if( strncmp( p_sys->ic->iformat->name, "matroska", 8 ) == 0 &&
276                 cc->codec_id == CODEC_ID_DVD_SUBTITLE &&
277                 cc->extradata != NULL &&
278                 cc->extradata_size > 0 )
279             {
280                 char *psz_start;
281                 char *psz_buf = malloc( cc->extradata_size + 1);
282                 if( psz_buf != NULL )
283                 {
284                     memcpy( psz_buf, cc->extradata , cc->extradata_size );
285                     psz_buf[cc->extradata_size] = '\0';
286
287                     psz_start = strstr( psz_buf, "size:" );
288                     if( psz_start &&
289                         vobsub_size_parse( psz_start,
290                                            &fmt.subs.spu.i_original_frame_width,
291                                            &fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
292                     {
293                         msg_Dbg( p_demux, "original frame size: %dx%d",
294                                  fmt.subs.spu.i_original_frame_width,
295                                  fmt.subs.spu.i_original_frame_height );
296                     }
297                     else
298                     {
299                         msg_Warn( p_demux, "reading original frame size failed" );
300                     }
301
302                     psz_start = strstr( psz_buf, "palette:" );
303                     if( psz_start &&
304                         vobsub_palette_parse( psz_start, &fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
305                     {
306                         fmt.subs.spu.palette[0] =  0xBeef;
307                         msg_Dbg( p_demux, "vobsub palette read" );
308                     }
309                     else
310                     {
311                         msg_Warn( p_demux, "reading original palette failed" );
312                     }
313                     free( psz_buf );
314                 }
315             }
316
317             psz_type = "subtitle";
318             break;
319
320         default:
321             es_format_Init( &fmt, UNKNOWN_ES, 0 );
322 #ifdef HAVE_FFMPEG_CODEC_ATTACHMENT
323             if( cc->codec_type == CODEC_TYPE_ATTACHMENT )
324             {
325                 input_attachment_t *p_attachment;
326                 psz_type = "attachment";
327                 if( cc->codec_id == CODEC_ID_TTF )
328                 {
329                     p_attachment = vlc_input_attachment_New( p_sys->ic->streams[i]->filename, "application/x-truetype-font", NULL,
330                                              cc->extradata, (int)cc->extradata_size );
331                     TAB_APPEND( p_sys->i_attachments, p_sys->attachments, p_attachment );
332                 }
333                 else msg_Warn( p_demux, "unsupported attachment type in ffmpeg demux" );
334             }
335             break;
336 #endif
337
338             if( cc->codec_type == CODEC_TYPE_DATA )
339                 psz_type = "data";
340
341             msg_Warn( p_demux, "unsupported track type in ffmpeg demux" );
342             break;
343         }
344         fmt.psz_language = p_sys->ic->streams[i]->language;
345
346 #ifdef HAVE_FFMPEG_CODEC_ATTACHMENT
347         if( cc->codec_type != CODEC_TYPE_ATTACHMENT )
348 #endif
349         {
350             fmt.i_extra = cc->extradata_size;
351             fmt.p_extra = cc->extradata;
352         }
353         es = es_out_Add( p_demux->out, &fmt );
354
355         msg_Dbg( p_demux, "adding es: %s codec = %4.4s",
356                  psz_type, (char*)&fcc );
357         TAB_APPEND( p_sys->i_tk, p_sys->tk, es );
358     }
359     if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
360         i_start_time = p_sys->ic->start_time * 1000000 / AV_TIME_BASE;
361
362     msg_Dbg( p_demux, "AVFormat supported stream" );
363     msg_Dbg( p_demux, "    - format = %s (%s)",
364              p_sys->fmt->name, p_sys->fmt->long_name );
365     msg_Dbg( p_demux, "    - start time = %"PRId64, i_start_time );
366     msg_Dbg( p_demux, "    - duration = %"PRId64,
367              ( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE ) ?
368              p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 );
369
370 #ifdef HAVE_FFMPEG_CHAPTERS
371     if( p_sys->ic->nb_chapters > 0 )
372         p_sys->p_title = vlc_input_title_New();
373     for( i = 0; i < p_sys->ic->nb_chapters; i++ )
374     {
375         seekpoint_t *s = vlc_seekpoint_New();
376
377         if( p_sys->ic->chapters[i]->title )
378         {
379             s->psz_name = strdup( p_sys->ic->chapters[i]->title );
380             EnsureUTF8( s->psz_name );
381             msg_Dbg( p_demux, "    - chapter %d: %s", i, s->psz_name );
382         }
383         s->i_time_offset = p_sys->ic->chapters[i]->start * 1000000 *
384             p_sys->ic->chapters[i]->time_base.num /
385             p_sys->ic->chapters[i]->time_base.den -
386             (i_start_time != -1 ? i_start_time : 0 );
387         TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
388     }
389 #endif
390
391     return VLC_SUCCESS;
392 }
393
394 /*****************************************************************************
395  * Close
396  *****************************************************************************/
397 void CloseDemux( vlc_object_t *p_this )
398 {
399     demux_t     *p_demux = (demux_t*)p_this;
400     demux_sys_t *p_sys = p_demux->p_sys;
401     bool b_avfmt_nofile;
402
403     FREENULL( p_sys->tk );
404
405     b_avfmt_nofile = p_sys->fmt->flags & AVFMT_NOFILE;
406     p_sys->fmt->flags |= AVFMT_NOFILE; /* libavformat must not fopen/fclose */
407     if( p_sys->ic ) av_close_input_file( p_sys->ic );
408     if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
409
410     for( int i = 0; i < p_sys->i_attachments; i++ )
411         free( p_sys->attachments[i] );
412     TAB_CLEAN( p_sys->i_attachments, p_sys->attachments);
413
414     if( p_sys->p_title )
415         vlc_input_title_Delete( p_sys->p_title );
416
417     free( p_sys->io_buffer );
418     free( p_sys );
419 }
420
421 /*****************************************************************************
422  * Demux:
423  *****************************************************************************/
424 static int Demux( demux_t *p_demux )
425 {
426     demux_sys_t *p_sys = p_demux->p_sys;
427     AVPacket    pkt;
428     block_t     *p_frame;
429     int64_t     i_start_time;
430
431     /* Read a frame */
432     if( av_read_frame( p_sys->ic, &pkt ) )
433     {
434         return 0;
435     }
436     if( pkt.stream_index < 0 || pkt.stream_index >= p_sys->i_tk )
437     {
438         av_free_packet( &pkt );
439         return 1;
440     }
441     const AVStream *p_stream = p_sys->ic->streams[pkt.stream_index];
442
443     if( ( p_frame = block_New( p_demux, pkt.size ) ) == NULL )
444     {
445         return 0;
446     }
447
448     memcpy( p_frame->p_buffer, pkt.data, pkt.size );
449
450     if( pkt.flags & PKT_FLAG_KEY )
451         p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
452
453     i_start_time = ( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) ?
454         ( p_sys->ic->start_time * 1000000 / AV_TIME_BASE )  : 0;
455
456     p_frame->i_dts = ( pkt.dts == (int64_t)AV_NOPTS_VALUE ) ?
457         0 : (pkt.dts) * 1000000 *
458         p_stream->time_base.num /
459         p_stream->time_base.den - i_start_time;
460     p_frame->i_pts = ( pkt.pts == (int64_t)AV_NOPTS_VALUE ) ?
461         0 : (pkt.pts) * 1000000 *
462         p_stream->time_base.num /
463         p_stream->time_base.den - i_start_time;
464     if( pkt.duration > 0 )
465         p_frame->i_length = pkt.duration * 1000000 *
466             p_stream->time_base.num /
467             p_stream->time_base.den - i_start_time;
468
469     if( pkt.dts != AV_NOPTS_VALUE && pkt.dts == pkt.pts &&
470         p_stream->codec->codec_type == CODEC_TYPE_VIDEO )
471     {
472         /* Add here notoriously bugged file formats/samples regarding PTS */
473         if( !strcmp( p_sys->fmt->name, "flv" ) )
474             p_frame->i_pts = 0;
475     }
476 #ifdef AVFORMAT_DEBUG
477     msg_Dbg( p_demux, "tk[%d] dts=%"PRId64" pts=%"PRId64,
478              pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
479 #endif
480
481     if( pkt.dts > 0  &&
482         ( pkt.stream_index == p_sys->i_pcr_tk || p_sys->i_pcr_tk < 0 ) )
483     {
484         p_sys->i_pcr_tk = pkt.stream_index;
485         p_sys->i_pcr = p_frame->i_dts;
486
487         es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
488     }
489
490     es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame );
491
492     UpdateSeekPoint( p_demux, p_sys->i_pcr);
493     av_free_packet( &pkt );
494     return 1;
495 }
496
497 static void UpdateSeekPoint( demux_t *p_demux, int64_t i_time )
498 {
499     demux_sys_t *p_sys = p_demux->p_sys;
500     int i;
501
502     if( !p_sys->p_title )
503         return;
504
505     for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
506     {
507         if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
508             break;
509     }
510     i--;
511
512     if( i != p_demux->info.i_seekpoint && i >= 0 )
513     {
514         p_demux->info.i_seekpoint = i;
515         p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
516     }
517 }
518
519 /*****************************************************************************
520  * Control:
521  *****************************************************************************/
522 static int Control( demux_t *p_demux, int i_query, va_list args )
523 {
524     demux_sys_t *p_sys = p_demux->p_sys;
525     double f, *pf;
526     int64_t i64, *pi64;
527
528     switch( i_query )
529     {
530         case DEMUX_GET_POSITION:
531             pf = (double*) va_arg( args, double* ); *pf = 0.0;
532             i64 = stream_Size( p_demux->s );
533             if( i64 > 0 )
534             {
535                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
536             }
537
538             if( (p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE) && (p_sys->i_pcr > 0) )
539             {
540                 *pf = (double)p_sys->i_pcr / (double)p_sys->ic->duration;
541             }
542
543             return VLC_SUCCESS;
544
545         case DEMUX_SET_POSITION:
546             f = (double) va_arg( args, double );
547             if( p_sys->i_pcr > 0 )
548             {
549                 i64 = p_sys->ic->duration * f;
550                 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
551                     i64 += p_sys->ic->start_time;
552
553                 msg_Warn( p_demux, "DEMUX_SET_POSITION: %"PRId64, i64 );
554
555                 /* If we have a duration, we prefer to seek by time
556                    but if we don't, or if the seek fails, try BYTE seeking */
557                 if( p_sys->ic->duration == (int64_t)AV_NOPTS_VALUE ||
558                     (av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0) )
559                 {
560                     int64_t i_size = stream_Size( p_demux->s );
561                     i64 = (i_size * f);
562
563                     msg_Warn( p_demux, "DEMUX_SET_BYTE_POSITION: %"PRId64, i64 );
564                     if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BYTE ) < 0 )
565                         return VLC_EGENERIC;
566                 }
567                 else
568                 {
569                     UpdateSeekPoint( p_demux, i64 );
570                 }
571                 p_sys->i_pcr = -1; /* Invalidate time display */
572             }
573             return VLC_SUCCESS;
574
575         case DEMUX_GET_LENGTH:
576             pi64 = (int64_t*)va_arg( args, int64_t * );
577             if( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE )
578             {
579                 *pi64 = p_sys->ic->duration;
580             }
581             else *pi64 = 0;
582             return VLC_SUCCESS;
583
584         case DEMUX_GET_TIME:
585             pi64 = (int64_t*)va_arg( args, int64_t * );
586             *pi64 = p_sys->i_pcr;
587             return VLC_SUCCESS;
588
589         case DEMUX_SET_TIME:
590             i64 = (int64_t)va_arg( args, int64_t );
591             i64 = i64 *AV_TIME_BASE / 1000000;
592             if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
593                 i64 += p_sys->ic->start_time;
594
595             msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
596
597             if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
598             {
599                 return VLC_EGENERIC;
600             }
601             p_sys->i_pcr = -1; /* Invalidate time display */
602             UpdateSeekPoint( p_demux, i64 );
603             return VLC_SUCCESS;
604
605         case DEMUX_HAS_UNSUPPORTED_META:
606         {
607             bool *pb_bool = (bool*)va_arg( args, bool* );
608             *pb_bool = true;
609             return VLC_SUCCESS;
610         }
611
612
613         case DEMUX_GET_META:
614         {
615             vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
616
617             if( !p_sys->ic->title[0] || !p_sys->ic->author[0] ||
618                 !p_sys->ic->copyright[0] || !p_sys->ic->comment[0] ||
619                 /*!p_sys->ic->album[0] ||*/ !p_sys->ic->genre[0] )
620             {
621                 return VLC_EGENERIC;
622             }
623
624             if( p_sys->ic->title[0] )
625                 vlc_meta_SetTitle( p_meta, p_sys->ic->title );
626             if( p_sys->ic->author[0] )
627                 vlc_meta_SetArtist( p_meta, p_sys->ic->author );
628             if( p_sys->ic->copyright[0] )
629                 vlc_meta_SetCopyright( p_meta, p_sys->ic->copyright );
630             if( p_sys->ic->comment[0] )
631                 vlc_meta_SetDescription( p_meta, p_sys->ic->comment );
632             if( p_sys->ic->genre[0] )
633                 vlc_meta_SetGenre( p_meta, p_sys->ic->genre );
634             return VLC_SUCCESS;
635         }
636
637         case DEMUX_GET_ATTACHMENTS:
638         {
639             input_attachment_t ***ppp_attach =
640                 (input_attachment_t***)va_arg( args, input_attachment_t*** );
641             int *pi_int = (int*)va_arg( args, int * );
642             int i;
643
644             if( p_sys->i_attachments <= 0 )
645                 return VLC_EGENERIC;
646
647             *pi_int = p_sys->i_attachments;;
648             *ppp_attach = malloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
649             for( i = 0; i < p_sys->i_attachments; i++ )
650                 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
651             return VLC_SUCCESS;
652         }
653
654         case DEMUX_GET_TITLE_INFO:
655         {
656             input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
657             int *pi_int    = (int*)va_arg( args, int* );
658             int *pi_title_offset = (int*)va_arg( args, int* );
659             int *pi_seekpoint_offset = (int*)va_arg( args, int* );
660
661             if( !p_sys->p_title )
662                 return VLC_EGENERIC;
663
664             *pi_int = 1;
665             *ppp_title = malloc( sizeof( input_title_t**) );
666             (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
667             *pi_title_offset = 0;
668             *pi_seekpoint_offset = 0;
669             return VLC_SUCCESS;
670         }
671         case DEMUX_SET_TITLE:
672         {
673             const int i_title = (int)va_arg( args, int );
674             if( !p_sys->p_title || i_title != 0 )
675                 return VLC_EGENERIC;
676             return VLC_SUCCESS;
677         }
678         case DEMUX_SET_SEEKPOINT:
679         {
680             const int i_seekpoint = (int)va_arg( args, int );
681             if( !p_sys->p_title )
682                 return VLC_EGENERIC;
683
684             i64 = p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset *AV_TIME_BASE / 1000000;
685             if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
686                 i64 += p_sys->ic->start_time;
687
688             msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
689
690             if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
691             {
692                 return VLC_EGENERIC;
693             }
694             p_sys->i_pcr = -1; /* Invalidate time display */
695             UpdateSeekPoint( p_demux, i64 );
696             return VLC_SUCCESS;
697         }
698
699
700         default:
701             return VLC_EGENERIC;
702     }
703 }
704
705 /*****************************************************************************
706  * I/O wrappers for libavformat
707  *****************************************************************************/
708 static int IORead( void *opaque, uint8_t *buf, int buf_size )
709 {
710     URLContext *p_url = opaque;
711     demux_t *p_demux = p_url->priv_data;
712     if( buf_size < 0 ) return -1;
713     int i_ret = stream_Read( p_demux->s, buf, buf_size );
714     return i_ret ? i_ret : -1;
715 }
716
717 static int64_t IOSeek( void *opaque, int64_t offset, int whence )
718 {
719     URLContext *p_url = opaque;
720     demux_t *p_demux = p_url->priv_data;
721     int64_t i_absolute;
722     int64_t i_size = stream_Size( p_demux->s );
723
724 #ifdef AVFORMAT_DEBUG
725     msg_Warn( p_demux, "IOSeek offset: %"PRId64", whence: %i", offset, whence );
726 #endif
727
728     switch( whence )
729     {
730 #ifdef AVSEEK_SIZE
731         case AVSEEK_SIZE:
732             return i_size;
733 #endif
734         case SEEK_SET:
735             i_absolute = (int64_t)offset;
736             break;
737         case SEEK_CUR:
738             i_absolute = stream_Tell( p_demux->s ) + (int64_t)offset;
739             break;
740         case SEEK_END:
741             i_absolute = i_size + (int64_t)offset;
742             break;
743         default:
744             return -1;
745
746     }
747
748     if( i_absolute < 0 )
749     {
750         msg_Dbg( p_demux, "Trying to seek before the beginning" );
751         return -1;
752     }
753
754     if( i_size > 0 && i_absolute >= i_size )
755     {
756         msg_Dbg( p_demux, "Trying to seek too far : EOF?" );
757         return -1;
758     }
759
760     if( stream_Seek( p_demux->s, i_absolute ) )
761     {
762         msg_Warn( p_demux, "we were not allowed to seek, or EOF " );
763         return -1;
764     }
765
766     return stream_Tell( p_demux->s );
767 }
768
769 #endif /* HAVE_LIBAVFORMAT_AVFORMAT_H */