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