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