]> git.sesse.net Git - vlc/blob - modules/demux/avformat/demux.c
avformat: Don't cast function return value.
[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                 double current = stream_Tell( p_demux->s );
536                 *pf = current / (double)i64;
537             }
538
539             if( (p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE) && (p_sys->i_pcr > 0) )
540             {
541                 *pf = (double)p_sys->i_pcr / (double)p_sys->ic->duration;
542             }
543
544             return VLC_SUCCESS;
545
546         case DEMUX_SET_POSITION:
547             f = (double) va_arg( args, double );
548             if( p_sys->i_pcr > 0 )
549             {
550                 i64 = p_sys->ic->duration * f;
551                 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
552                     i64 += p_sys->ic->start_time;
553
554                 msg_Warn( p_demux, "DEMUX_SET_POSITION: %"PRId64, i64 );
555
556                 /* If we have a duration, we prefer to seek by time
557                    but if we don't, or if the seek fails, try BYTE seeking */
558                 if( p_sys->ic->duration == (int64_t)AV_NOPTS_VALUE ||
559                     (av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0) )
560                 {
561                     int64_t i_size = stream_Size( p_demux->s );
562                     i64 = (i_size * f);
563
564                     msg_Warn( p_demux, "DEMUX_SET_BYTE_POSITION: %"PRId64, i64 );
565                     if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BYTE ) < 0 )
566                         return VLC_EGENERIC;
567                 }
568                 else
569                 {
570                     UpdateSeekPoint( p_demux, i64 );
571                 }
572                 p_sys->i_pcr = -1; /* Invalidate time display */
573             }
574             return VLC_SUCCESS;
575
576         case DEMUX_GET_LENGTH:
577             pi64 = (int64_t*)va_arg( args, int64_t * );
578             if( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE )
579             {
580                 *pi64 = p_sys->ic->duration;
581             }
582             else *pi64 = 0;
583             return VLC_SUCCESS;
584
585         case DEMUX_GET_TIME:
586             pi64 = (int64_t*)va_arg( args, int64_t * );
587             *pi64 = p_sys->i_pcr;
588             return VLC_SUCCESS;
589
590         case DEMUX_SET_TIME:
591             i64 = (int64_t)va_arg( args, int64_t );
592             i64 = i64 *AV_TIME_BASE / 1000000;
593             if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
594                 i64 += p_sys->ic->start_time;
595
596             msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
597
598             if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
599             {
600                 return VLC_EGENERIC;
601             }
602             p_sys->i_pcr = -1; /* Invalidate time display */
603             UpdateSeekPoint( p_demux, i64 );
604             return VLC_SUCCESS;
605
606         case DEMUX_HAS_UNSUPPORTED_META:
607         {
608             bool *pb_bool = (bool*)va_arg( args, bool* );
609             *pb_bool = true;
610             return VLC_SUCCESS;
611         }
612
613
614         case DEMUX_GET_META:
615         {
616             vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
617
618             if( !p_sys->ic->title[0] || !p_sys->ic->author[0] ||
619                 !p_sys->ic->copyright[0] || !p_sys->ic->comment[0] ||
620                 /*!p_sys->ic->album[0] ||*/ !p_sys->ic->genre[0] )
621             {
622                 return VLC_EGENERIC;
623             }
624
625             if( p_sys->ic->title[0] )
626                 vlc_meta_SetTitle( p_meta, p_sys->ic->title );
627             if( p_sys->ic->author[0] )
628                 vlc_meta_SetArtist( p_meta, p_sys->ic->author );
629             if( p_sys->ic->copyright[0] )
630                 vlc_meta_SetCopyright( p_meta, p_sys->ic->copyright );
631             if( p_sys->ic->comment[0] )
632                 vlc_meta_SetDescription( p_meta, p_sys->ic->comment );
633             if( p_sys->ic->genre[0] )
634                 vlc_meta_SetGenre( p_meta, p_sys->ic->genre );
635             return VLC_SUCCESS;
636         }
637
638         case DEMUX_GET_ATTACHMENTS:
639         {
640             input_attachment_t ***ppp_attach =
641                 (input_attachment_t***)va_arg( args, input_attachment_t*** );
642             int *pi_int = (int*)va_arg( args, int * );
643             int i;
644
645             if( p_sys->i_attachments <= 0 )
646                 return VLC_EGENERIC;
647
648             *pi_int = p_sys->i_attachments;;
649             *ppp_attach = malloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
650             for( i = 0; i < p_sys->i_attachments; i++ )
651                 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
652             return VLC_SUCCESS;
653         }
654
655         case DEMUX_GET_TITLE_INFO:
656         {
657             input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
658             int *pi_int    = (int*)va_arg( args, int* );
659             int *pi_title_offset = (int*)va_arg( args, int* );
660             int *pi_seekpoint_offset = (int*)va_arg( args, int* );
661
662             if( !p_sys->p_title )
663                 return VLC_EGENERIC;
664
665             *pi_int = 1;
666             *ppp_title = malloc( sizeof( input_title_t**) );
667             (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
668             *pi_title_offset = 0;
669             *pi_seekpoint_offset = 0;
670             return VLC_SUCCESS;
671         }
672         case DEMUX_SET_TITLE:
673         {
674             const int i_title = (int)va_arg( args, int );
675             if( !p_sys->p_title || i_title != 0 )
676                 return VLC_EGENERIC;
677             return VLC_SUCCESS;
678         }
679         case DEMUX_SET_SEEKPOINT:
680         {
681             const int i_seekpoint = (int)va_arg( args, int );
682             if( !p_sys->p_title )
683                 return VLC_EGENERIC;
684
685             i64 = p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset *AV_TIME_BASE / 1000000;
686             if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
687                 i64 += p_sys->ic->start_time;
688
689             msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
690
691             if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
692             {
693                 return VLC_EGENERIC;
694             }
695             p_sys->i_pcr = -1; /* Invalidate time display */
696             UpdateSeekPoint( p_demux, i64 );
697             return VLC_SUCCESS;
698         }
699
700
701         default:
702             return VLC_EGENERIC;
703     }
704 }
705
706 /*****************************************************************************
707  * I/O wrappers for libavformat
708  *****************************************************************************/
709 static int IORead( void *opaque, uint8_t *buf, int buf_size )
710 {
711     URLContext *p_url = opaque;
712     demux_t *p_demux = p_url->priv_data;
713     if( buf_size < 0 ) return -1;
714     int i_ret = stream_Read( p_demux->s, buf, buf_size );
715     return i_ret ? i_ret : -1;
716 }
717
718 static int64_t IOSeek( void *opaque, int64_t offset, int whence )
719 {
720     URLContext *p_url = opaque;
721     demux_t *p_demux = p_url->priv_data;
722     int64_t i_absolute;
723     int64_t i_size = stream_Size( p_demux->s );
724
725 #ifdef AVFORMAT_DEBUG
726     msg_Warn( p_demux, "IOSeek offset: %"PRId64", whence: %i", offset, whence );
727 #endif
728
729     switch( whence )
730     {
731 #ifdef AVSEEK_SIZE
732         case AVSEEK_SIZE:
733             return i_size;
734 #endif
735         case SEEK_SET:
736             i_absolute = (int64_t)offset;
737             break;
738         case SEEK_CUR:
739             i_absolute = stream_Tell( p_demux->s ) + (int64_t)offset;
740             break;
741         case SEEK_END:
742             i_absolute = i_size + (int64_t)offset;
743             break;
744         default:
745             return -1;
746
747     }
748
749     if( i_absolute < 0 )
750     {
751         msg_Dbg( p_demux, "Trying to seek before the beginning" );
752         return -1;
753     }
754
755     if( i_size > 0 && i_absolute >= i_size )
756     {
757         msg_Dbg( p_demux, "Trying to seek too far : EOF?" );
758         return -1;
759     }
760
761     if( stream_Seek( p_demux->s, i_absolute ) )
762     {
763         msg_Warn( p_demux, "we were not allowed to seek, or EOF " );
764         return -1;
765     }
766
767     return stream_Tell( p_demux->s );
768 }
769
770 #endif /* HAVE_LIBAVFORMAT_AVFORMAT_H */