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