]> git.sesse.net Git - vlc/blob - modules/demux/avformat/demux.c
Merge branch 1.0-bugfix
[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 #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     if( ( p_frame = block_New( p_demux, pkt.size ) ) == NULL )
442     {
443         return 0;
444     }
445
446     memcpy( p_frame->p_buffer, pkt.data, pkt.size );
447
448     if( pkt.flags & PKT_FLAG_KEY )
449         p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
450
451     i_start_time = ( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) ?
452         ( p_sys->ic->start_time * 1000000 / AV_TIME_BASE )  : 0;
453
454     p_frame->i_dts = ( pkt.dts == (int64_t)AV_NOPTS_VALUE ) ?
455         0 : (pkt.dts) * 1000000 *
456         p_sys->ic->streams[pkt.stream_index]->time_base.num /
457         p_sys->ic->streams[pkt.stream_index]->time_base.den - i_start_time;
458     p_frame->i_pts = ( pkt.pts == (int64_t)AV_NOPTS_VALUE ) ?
459         0 : (pkt.pts) * 1000000 *
460         p_sys->ic->streams[pkt.stream_index]->time_base.num /
461         p_sys->ic->streams[pkt.stream_index]->time_base.den - i_start_time;
462     if( pkt.duration > 0 )
463         p_frame->i_length = pkt.duration * 1000000 *
464             p_sys->ic->streams[pkt.stream_index]->time_base.num /
465             p_sys->ic->streams[pkt.stream_index]->time_base.den - i_start_time;
466
467 #ifdef AVFORMAT_DEBUG
468     msg_Dbg( p_demux, "tk[%d] dts=%"PRId64" pts=%"PRId64,
469              pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
470 #endif
471
472     if( pkt.dts > 0  &&
473         ( pkt.stream_index == p_sys->i_pcr_tk || p_sys->i_pcr_tk < 0 ) )
474     {
475         p_sys->i_pcr_tk = pkt.stream_index;
476         p_sys->i_pcr = p_frame->i_dts;
477
478         es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
479     }
480
481     es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame );
482
483     UpdateSeekPoint( p_demux, p_sys->i_pcr);
484     av_free_packet( &pkt );
485     return 1;
486 }
487
488 static void UpdateSeekPoint( demux_t *p_demux, int64_t i_time )
489 {
490     demux_sys_t *p_sys = p_demux->p_sys;
491     int i;
492
493     if( !p_sys->p_title )
494         return;
495
496     for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
497     {
498         if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
499             break;
500     }
501     i--;
502
503     if( i != p_demux->info.i_seekpoint && i >= 0 )
504     {
505         p_demux->info.i_seekpoint = i;
506         p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
507     }
508 }
509
510 /*****************************************************************************
511  * Control:
512  *****************************************************************************/
513 static int Control( demux_t *p_demux, int i_query, va_list args )
514 {
515     demux_sys_t *p_sys = p_demux->p_sys;
516     double f, *pf;
517     int64_t i64, *pi64;
518
519     switch( i_query )
520     {
521         case DEMUX_GET_POSITION:
522             pf = (double*) va_arg( args, double* ); *pf = 0.0;
523             i64 = stream_Size( p_demux->s );
524             if( i64 > 0 )
525             {
526                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
527             }
528
529             if( (p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE) && (p_sys->i_pcr > 0) )
530             {
531                 *pf = (double)p_sys->i_pcr / (double)p_sys->ic->duration;
532             }
533
534             return VLC_SUCCESS;
535
536         case DEMUX_SET_POSITION:
537             f = (double) va_arg( args, double );
538             i64 = stream_Tell( p_demux->s );
539             if( p_sys->i_pcr > 0 )
540             {
541                 i64 = p_sys->ic->duration * f;
542                 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
543                     i64 += p_sys->ic->start_time;
544
545                 msg_Warn( p_demux, "DEMUX_SET_POSITION: %"PRId64, i64 );
546
547                 /* If we have a duration, we prefer to seek by time
548                    but if we don't, or if the seek fails, try BYTE seeking */
549                 if( p_sys->ic->duration == (int64_t)AV_NOPTS_VALUE ||
550                     (av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0) )
551                 {
552                     int64_t i_size = stream_Size( p_demux->s );
553
554                     msg_Warn( p_demux, "DEMUX_SET_BYTE_POSITION: %"PRId64, i64 );
555                     if( av_seek_frame( p_sys->ic, -1, (i_size * f), AVSEEK_FLAG_BYTE ) < 0 )
556                         return VLC_EGENERIC;
557                 }
558                 UpdateSeekPoint( p_demux, i64 );
559                 p_sys->i_pcr = -1; /* Invalidate time display */
560             }
561             return VLC_SUCCESS;
562
563         case DEMUX_GET_LENGTH:
564             pi64 = (int64_t*)va_arg( args, int64_t * );
565             if( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE )
566             {
567                 *pi64 = p_sys->ic->duration;
568             }
569             else *pi64 = 0;
570             return VLC_SUCCESS;
571
572         case DEMUX_GET_TIME:
573             pi64 = (int64_t*)va_arg( args, int64_t * );
574             *pi64 = p_sys->i_pcr;
575             return VLC_SUCCESS;
576
577         case DEMUX_SET_TIME:
578             i64 = (int64_t)va_arg( args, int64_t );
579             i64 = i64 *AV_TIME_BASE / 1000000;
580             if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
581                 i64 += p_sys->ic->start_time;
582
583             msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
584
585             if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
586             {
587                 return VLC_EGENERIC;
588             }
589             p_sys->i_pcr = -1; /* Invalidate time display */
590             UpdateSeekPoint( p_demux, i64 );
591             return VLC_SUCCESS;
592
593         case DEMUX_HAS_UNSUPPORTED_META:
594         {
595             bool *pb_bool = (bool*)va_arg( args, bool* );
596             *pb_bool = true;
597             return VLC_SUCCESS;
598         }
599
600
601         case DEMUX_GET_META:
602         {
603             vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
604
605             if( !p_sys->ic->title[0] || !p_sys->ic->author[0] ||
606                 !p_sys->ic->copyright[0] || !p_sys->ic->comment[0] ||
607                 /*!p_sys->ic->album[0] ||*/ !p_sys->ic->genre[0] )
608             {
609                 return VLC_EGENERIC;
610             }
611
612             if( p_sys->ic->title[0] )
613                 vlc_meta_SetTitle( p_meta, p_sys->ic->title );
614             if( p_sys->ic->author[0] )
615                 vlc_meta_SetArtist( p_meta, p_sys->ic->author );
616             if( p_sys->ic->copyright[0] )
617                 vlc_meta_SetCopyright( p_meta, p_sys->ic->copyright );
618             if( p_sys->ic->comment[0] )
619                 vlc_meta_SetDescription( p_meta, p_sys->ic->comment );
620             if( p_sys->ic->genre[0] )
621                 vlc_meta_SetGenre( p_meta, p_sys->ic->genre );
622             return VLC_SUCCESS;
623         }
624
625         case DEMUX_GET_ATTACHMENTS:
626         {
627             input_attachment_t ***ppp_attach =
628                 (input_attachment_t***)va_arg( args, input_attachment_t*** );
629             int *pi_int = (int*)va_arg( args, int * );
630             int i;
631
632             if( p_sys->i_attachments <= 0 )
633                 return VLC_EGENERIC;
634
635             *pi_int = p_sys->i_attachments;;
636             *ppp_attach = malloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
637             for( i = 0; i < p_sys->i_attachments; i++ )
638                 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
639             return VLC_SUCCESS;
640         }
641
642         case DEMUX_GET_TITLE_INFO:
643         {
644             input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
645             int *pi_int    = (int*)va_arg( args, int* );
646             int *pi_title_offset = (int*)va_arg( args, int* );
647             int *pi_seekpoint_offset = (int*)va_arg( args, int* );
648
649             if( !p_sys->p_title )
650                 return VLC_EGENERIC;
651
652             *pi_int = 1;
653             *ppp_title = malloc( sizeof( input_title_t**) );
654             (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
655             *pi_title_offset = 0;
656             *pi_seekpoint_offset = 0;
657             return VLC_SUCCESS;
658         }
659         case DEMUX_SET_TITLE:
660         {
661             const int i_title = (int)va_arg( args, int );
662             if( !p_sys->p_title || i_title != 0 )
663                 return VLC_EGENERIC;
664             return VLC_SUCCESS;
665         }
666         case DEMUX_SET_SEEKPOINT:
667         {
668             const int i_seekpoint = (int)va_arg( args, int );
669             if( !p_sys->p_title )
670                 return VLC_EGENERIC;
671
672             i64 = p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset *AV_TIME_BASE / 1000000;
673             if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
674                 i64 += p_sys->ic->start_time;
675
676             msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
677
678             if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
679             {
680                 return VLC_EGENERIC;
681             }
682             p_sys->i_pcr = -1; /* Invalidate time display */
683             UpdateSeekPoint( p_demux, i64 );
684             return VLC_SUCCESS;
685         }
686
687
688         default:
689             return VLC_EGENERIC;
690     }
691 }
692
693 /*****************************************************************************
694  * I/O wrappers for libavformat
695  *****************************************************************************/
696 static int IORead( void *opaque, uint8_t *buf, int buf_size )
697 {
698     URLContext *p_url = opaque;
699     demux_t *p_demux = p_url->priv_data;
700     if( buf_size < 0 ) return -1;
701     int i_ret = stream_Read( p_demux->s, buf, buf_size );
702     return i_ret ? i_ret : -1;
703 }
704
705 static int64_t IOSeek( void *opaque, int64_t offset, int whence )
706 {
707     URLContext *p_url = opaque;
708     demux_t *p_demux = p_url->priv_data;
709     int64_t i_absolute = (int64_t)offset;
710     int64_t i_size = stream_Size( p_demux->s );
711
712 #ifdef AVFORMAT_DEBUG
713     msg_Warn( p_demux, "IOSeek offset: %"PRId64", whence: %i", offset, whence );
714 #endif
715
716     switch( whence )
717     {
718 #ifdef AVSEEK_SIZE
719         case AVSEEK_SIZE:
720             return i_size;
721 #endif
722         case SEEK_SET:
723             i_absolute = (int64_t)offset;
724             break;
725         case SEEK_CUR:
726             i_absolute = stream_Tell( p_demux->s ) + (int64_t)offset;
727             break;
728         case SEEK_END:
729             i_absolute = i_size + (int64_t)offset;
730             break;
731         default:
732             return -1;
733
734     }
735
736     if( i_absolute < 0 )
737     {
738         msg_Dbg( p_demux, "Trying to seek before the beginning" );
739         return -1;
740     }
741
742     if( i_size > 0 && i_absolute >= i_size )
743     {
744         msg_Dbg( p_demux, "Trying to seek too far : EOF?" );
745         return -1;
746     }
747
748     if( stream_Seek( p_demux->s, i_absolute ) )
749     {
750         msg_Warn( p_demux, "we were not allowed to seek, or EOF " );
751         return -1;
752     }
753
754     return stream_Tell( p_demux->s );
755 }
756
757 #endif /* HAVE_LIBAVFORMAT_AVFORMAT_H */