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