1 /*****************************************************************************
2 * demux.c: demuxer using ffmpeg (libavformat).
3 *****************************************************************************
4 * Copyright (C) 2004-2009 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_demux.h>
35 #include <vlc_stream.h>
37 #include <vlc_input.h>
38 #include <vlc_charset.h>
39 #include <vlc_avcodec.h>
42 #if defined(HAVE_LIBAVFORMAT_AVFORMAT_H)
43 # include <libavformat/avformat.h>
44 #elif defined(HAVE_FFMPEG_AVFORMAT_H)
45 # include <ffmpeg/avformat.h>
48 #include "../../codec/avcodec/avcodec.h"
51 #include "../vobsub.h"
53 //#define AVFORMAT_DEBUG 1
55 /* Version checking */
56 #if defined(HAVE_FFMPEG_AVFORMAT_H) || defined(HAVE_LIBAVFORMAT_AVFORMAT_H)
58 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(50<<8)+0) )
59 # define HAVE_FFMPEG_CODEC_ATTACHMENT 1
62 #if (LIBAVFORMAT_VERSION_INT >= ((52<<16)+(15<<8)+0) )
63 # define HAVE_FFMPEG_CHAPTERS 1
66 /*****************************************************************************
67 * demux_sys_t: demux descriptor
68 *****************************************************************************/
90 input_attachment_t **attachments;
92 /* Only one title with seekpoints possible atm. */
93 input_title_t *p_title;
96 /*****************************************************************************
98 *****************************************************************************/
99 static int Demux ( demux_t *p_demux );
100 static int Control( demux_t *p_demux, int i_query, va_list args );
102 static int IORead( void *opaque, uint8_t *buf, int buf_size );
103 static int64_t IOSeek( void *opaque, int64_t offset, int whence );
105 static block_t *BuildSsaFrame( const AVPacket *p_pkt, unsigned i_order );
106 static void UpdateSeekPoint( demux_t *p_demux, int64_t i_time );
108 /*****************************************************************************
110 *****************************************************************************/
111 int OpenDemux( vlc_object_t *p_this )
113 demux_t *p_demux = (demux_t*)p_this;
118 int64_t i_start_time = -1;
122 if( p_demux->psz_file )
123 psz_url = strdup( p_demux->psz_file );
126 if( asprintf( &psz_url, "%s://%s", p_demux->psz_access, p_demux->psz_location ) == -1)
129 msg_Dbg( p_demux, "trying url: %s", psz_url );
130 /* Init Probe data */
131 pd.filename = psz_url;
132 if( ( pd.buf_size = stream_Peek( p_demux->s, &pd.buf, 2048 + 213 ) ) <= 0 )
135 msg_Warn( p_demux, "cannot peek" );
140 av_register_all(); /* Can be called several times */
141 vlc_avcodec_unlock();
144 if( !( fmt = av_probe_input_format( &pd, 1 ) ) )
146 msg_Dbg( p_demux, "couldn't guess format" );
151 /* Don't try to handle MPEG unless forced */
152 if( !p_demux->b_force &&
153 ( !strcmp( fmt->name, "mpeg" ) ||
154 !strcmp( fmt->name, "vcd" ) ||
155 !strcmp( fmt->name, "vob" ) ||
156 !strcmp( fmt->name, "mpegts" ) ||
157 /* libavformat's redirector won't work */
158 !strcmp( fmt->name, "redir" ) ||
159 !strcmp( fmt->name, "sdp" ) ) )
165 /* Don't trigger false alarms on bin files */
166 if( !p_demux->b_force && !strcmp( fmt->name, "psxstr" ) )
170 if( !p_demux->psz_file )
176 i_len = strlen( p_demux->psz_file );
183 if( strcasecmp( &p_demux->psz_file[i_len - 4], ".str" ) &&
184 strcasecmp( &p_demux->psz_file[i_len - 4], ".xai" ) &&
185 strcasecmp( &p_demux->psz_file[i_len - 3], ".xa" ) )
192 msg_Dbg( p_demux, "detected format: %s", fmt->name );
194 /* Fill p_demux fields */
195 p_demux->pf_demux = Demux;
196 p_demux->pf_control = Control;
197 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
202 p_sys->i_pcr_tk = -1;
204 p_sys->i_ssa_order = 0;
205 TAB_INIT( p_sys->i_attachments, p_sys->attachments);
206 p_sys->p_title = NULL;
208 /* Create I/O wrapper */
209 p_sys->io_buffer_size = 32768; /* FIXME */
210 p_sys->io_buffer = malloc( p_sys->io_buffer_size );
211 p_sys->url.priv_data = p_demux;
212 p_sys->url.prot = &p_sys->prot;
213 p_sys->url.prot->name = "VLC I/O wrapper";
214 p_sys->url.prot->url_open = 0;
215 p_sys->url.prot->url_read =
216 (int (*) (URLContext *, unsigned char *, int))IORead;
217 p_sys->url.prot->url_write = 0;
218 p_sys->url.prot->url_seek =
219 (int64_t (*) (URLContext *, int64_t, int))IOSeek;
220 p_sys->url.prot->url_close = 0;
221 p_sys->url.prot->next = 0;
222 init_put_byte( &p_sys->io, p_sys->io_buffer, p_sys->io_buffer_size,
223 0, &p_sys->url, IORead, NULL, IOSeek );
225 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_can_seek );
228 /* Tell avformat that input is stream, so it doesn't get stuck
229 when trying av_find_stream_info() trying to seek all the wrong places
230 init_put_byte defaults io.is_streamed=0, so thats why we set them after it
232 p_sys->url.is_streamed = 1;
233 p_sys->io.is_streamed = 1;
238 if( av_open_input_stream( &p_sys->ic, &p_sys->io, psz_url,
241 msg_Err( p_demux, "av_open_input_stream failed" );
243 CloseDemux( p_this );
249 vlc_avcodec_lock(); /* avformat calls avcodec behind our back!!! */
250 if( av_find_stream_info( p_sys->ic ) < 0 )
252 msg_Warn( p_demux, "av_find_stream_info failed" );
254 vlc_avcodec_unlock();
256 for( i = 0; i < p_sys->ic->nb_streams; i++ )
258 AVStream *s = p_sys->ic->streams[i];
259 AVCodecContext *cc = s->codec;
264 const char *psz_type = "unknown";
266 if( !GetVlcFourcc( cc->codec_id, NULL, &fcc, NULL ) )
267 fcc = VLC_FOURCC( 'u', 'n', 'd', 'f' );
269 switch( cc->codec_type )
271 case CODEC_TYPE_AUDIO:
272 es_format_Init( &fmt, AUDIO_ES, fcc );
273 fmt.i_bitrate = cc->bit_rate;
274 fmt.audio.i_channels = cc->channels;
275 fmt.audio.i_rate = cc->sample_rate;
276 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
277 fmt.audio.i_bitspersample = cc->bits_per_sample;
279 fmt.audio.i_bitspersample = cc->bits_per_coded_sample;
281 fmt.audio.i_blockalign = cc->block_align;
285 case CODEC_TYPE_VIDEO:
286 es_format_Init( &fmt, VIDEO_ES, fcc );
288 /* Special case for raw video data */
289 if( cc->codec_id == CODEC_ID_RAWVIDEO )
291 msg_Dbg( p_demux, "raw video, pixel format: %i", cc->pix_fmt );
292 if( GetVlcChroma( &fmt.video, cc->pix_fmt ) != VLC_SUCCESS)
294 msg_Err( p_demux, "was unable to find a FourCC match for raw video" );
297 fmt.i_codec = fmt.video.i_chroma;
299 /* We need this for the h264 packetizer */
300 else if( cc->codec_id == CODEC_ID_H264 && ( !strcmp( p_sys->fmt->name, "flv" ) ||
301 !strcmp( p_sys->fmt->name, "matroska" ) || !strcmp( p_sys->fmt->name, "mp4" ) ) )
302 fmt.i_original_fourcc = VLC_FOURCC( 'a', 'v', 'c', '1' );
304 fmt.video.i_width = cc->width;
305 fmt.video.i_height = cc->height;
308 fmt.video.p_palette = malloc( sizeof(video_palette_t) );
309 *fmt.video.p_palette = *(video_palette_t *)cc->palctrl;
312 fmt.video.i_frame_rate = cc->time_base.den;
313 fmt.video.i_frame_rate_base = cc->time_base.num;
316 case CODEC_TYPE_SUBTITLE:
317 es_format_Init( &fmt, SPU_ES, fcc );
318 if( strncmp( p_sys->ic->iformat->name, "matroska", 8 ) == 0 &&
319 cc->codec_id == CODEC_ID_DVD_SUBTITLE &&
320 cc->extradata != NULL &&
321 cc->extradata_size > 0 )
324 char *psz_buf = malloc( cc->extradata_size + 1);
325 if( psz_buf != NULL )
327 memcpy( psz_buf, cc->extradata , cc->extradata_size );
328 psz_buf[cc->extradata_size] = '\0';
330 psz_start = strstr( psz_buf, "size:" );
332 vobsub_size_parse( psz_start,
333 &fmt.subs.spu.i_original_frame_width,
334 &fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
336 msg_Dbg( p_demux, "original frame size: %dx%d",
337 fmt.subs.spu.i_original_frame_width,
338 fmt.subs.spu.i_original_frame_height );
342 msg_Warn( p_demux, "reading original frame size failed" );
345 psz_start = strstr( psz_buf, "palette:" );
347 vobsub_palette_parse( psz_start, &fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
349 fmt.subs.spu.palette[0] = 0xBeef;
350 msg_Dbg( p_demux, "vobsub palette read" );
354 msg_Warn( p_demux, "reading original palette failed" );
360 psz_type = "subtitle";
364 es_format_Init( &fmt, UNKNOWN_ES, 0 );
365 #ifdef HAVE_FFMPEG_CODEC_ATTACHMENT
366 if( cc->codec_type == CODEC_TYPE_ATTACHMENT )
368 input_attachment_t *p_attachment;
369 psz_type = "attachment";
370 if( cc->codec_id == CODEC_ID_TTF )
372 p_attachment = vlc_input_attachment_New( s->filename, "application/x-truetype-font", NULL,
373 cc->extradata, (int)cc->extradata_size );
374 TAB_APPEND( p_sys->i_attachments, p_sys->attachments, p_attachment );
376 else msg_Warn( p_demux, "unsupported attachment type in ffmpeg demux" );
381 if( cc->codec_type == CODEC_TYPE_DATA )
384 msg_Warn( p_demux, "unsupported track type in ffmpeg demux" );
387 fmt.psz_language = strdup( s->language );
388 if( s->disposition & AV_DISPOSITION_DEFAULT )
389 fmt.i_priority = 1000;
391 #ifdef HAVE_FFMPEG_CODEC_ATTACHMENT
392 if( cc->codec_type != CODEC_TYPE_ATTACHMENT )
395 const bool b_ogg = !strcmp( p_sys->fmt->name, "ogg" );
396 const uint8_t *p_extra = cc->extradata;
397 unsigned i_extra = cc->extradata_size;
399 if( cc->codec_id == CODEC_ID_THEORA && b_ogg )
404 for( i_count = 0; i_count < 3; i_count++ )
408 pi_size[i_count] = GetWBE( p_extra );
409 pp_data[i_count] = (uint8_t*)&p_extra[2];
410 if( i_extra < pi_size[i_count] + 2 )
413 p_extra += 2 + pi_size[i_count];
414 i_extra -= 2 + pi_size[i_count];
416 if( i_count > 0 && xiph_PackHeaders( &fmt.i_extra, &fmt.p_extra,
417 pi_size, pp_data, i_count ) )
423 else if( cc->codec_id == CODEC_ID_SPEEX && b_ogg )
425 uint8_t p_dummy_comment[] = {
432 pi_size[0] = i_extra;
433 pp_data[0] = (uint8_t*)p_extra;
435 pi_size[1] = sizeof(p_dummy_comment);
436 pp_data[1] = p_dummy_comment;
438 if( pi_size[0] > 0 && xiph_PackHeaders( &fmt.i_extra, &fmt.p_extra,
439 pi_size, pp_data, 2 ) )
445 else if( cc->extradata_size > 0 )
447 fmt.p_extra = malloc( i_extra );
450 fmt.i_extra = i_extra;
451 memcpy( fmt.p_extra, p_extra, i_extra );
455 es = es_out_Add( p_demux->out, &fmt );
456 if( s->disposition & AV_DISPOSITION_DEFAULT )
457 es_out_Control( p_demux->out, ES_OUT_SET_ES_DEFAULT, es );
458 es_format_Clean( &fmt );
460 msg_Dbg( p_demux, "adding es: %s codec = %4.4s",
461 psz_type, (char*)&fcc );
462 TAB_APPEND( p_sys->i_tk, p_sys->tk, es );
464 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
465 i_start_time = p_sys->ic->start_time * 1000000 / AV_TIME_BASE;
467 msg_Dbg( p_demux, "AVFormat supported stream" );
468 msg_Dbg( p_demux, " - format = %s (%s)",
469 p_sys->fmt->name, p_sys->fmt->long_name );
470 msg_Dbg( p_demux, " - start time = %"PRId64, i_start_time );
471 msg_Dbg( p_demux, " - duration = %"PRId64,
472 ( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE ) ?
473 p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 );
475 #ifdef HAVE_FFMPEG_CHAPTERS
476 if( p_sys->ic->nb_chapters > 0 )
477 p_sys->p_title = vlc_input_title_New();
478 for( i = 0; i < p_sys->ic->nb_chapters; i++ )
480 seekpoint_t *s = vlc_seekpoint_New();
482 if( p_sys->ic->chapters[i]->title )
484 s->psz_name = strdup( p_sys->ic->chapters[i]->title );
485 EnsureUTF8( s->psz_name );
486 msg_Dbg( p_demux, " - chapter %d: %s", i, s->psz_name );
488 s->i_time_offset = p_sys->ic->chapters[i]->start * 1000000 *
489 p_sys->ic->chapters[i]->time_base.num /
490 p_sys->ic->chapters[i]->time_base.den -
491 (i_start_time != -1 ? i_start_time : 0 );
492 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
499 /*****************************************************************************
501 *****************************************************************************/
502 void CloseDemux( vlc_object_t *p_this )
504 demux_t *p_demux = (demux_t*)p_this;
505 demux_sys_t *p_sys = p_demux->p_sys;
507 FREENULL( p_sys->tk );
509 if( p_sys->ic ) av_close_input_stream( p_sys->ic );
511 for( int i = 0; i < p_sys->i_attachments; i++ )
512 free( p_sys->attachments[i] );
513 TAB_CLEAN( p_sys->i_attachments, p_sys->attachments);
516 vlc_input_title_Delete( p_sys->p_title );
518 free( p_sys->io_buffer );
522 /*****************************************************************************
524 *****************************************************************************/
525 static int Demux( demux_t *p_demux )
527 demux_sys_t *p_sys = p_demux->p_sys;
530 int64_t i_start_time;
533 if( av_read_frame( p_sys->ic, &pkt ) )
537 if( pkt.stream_index < 0 || pkt.stream_index >= p_sys->i_tk )
539 av_free_packet( &pkt );
542 const AVStream *p_stream = p_sys->ic->streams[pkt.stream_index];
543 if( p_stream->time_base.den <= 0 )
545 msg_Warn( p_demux, "Invalid time base for the stream %d", pkt.stream_index );
546 av_free_packet( &pkt );
549 if( p_stream->codec->codec_id == CODEC_ID_SSA )
551 p_frame = BuildSsaFrame( &pkt, p_sys->i_ssa_order++ );
554 av_free_packet( &pkt );
560 if( ( p_frame = block_New( p_demux, pkt.size ) ) == NULL )
562 av_free_packet( &pkt );
565 memcpy( p_frame->p_buffer, pkt.data, pkt.size );
568 if( pkt.flags & PKT_FLAG_KEY )
569 p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
571 i_start_time = ( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) ?
572 ( p_sys->ic->start_time * 1000000 / AV_TIME_BASE ) : 0;
574 p_frame->i_dts = ( pkt.dts == (int64_t)AV_NOPTS_VALUE ) ?
575 VLC_TS_INVALID : (pkt.dts) * 1000000 *
576 p_stream->time_base.num /
577 p_stream->time_base.den - i_start_time + VLC_TS_0;
578 p_frame->i_pts = ( pkt.pts == (int64_t)AV_NOPTS_VALUE ) ?
579 VLC_TS_INVALID : (pkt.pts) * 1000000 *
580 p_stream->time_base.num /
581 p_stream->time_base.den - i_start_time + VLC_TS_0;
582 if( pkt.duration > 0 && p_frame->i_length <= 0 )
583 p_frame->i_length = pkt.duration * 1000000 *
584 p_stream->time_base.num /
585 p_stream->time_base.den;
587 if( pkt.dts != AV_NOPTS_VALUE && pkt.dts == pkt.pts &&
588 p_stream->codec->codec_type == CODEC_TYPE_VIDEO )
590 /* Add here notoriously bugged file formats/samples regarding PTS */
591 if( !strcmp( p_sys->fmt->name, "flv" ) )
592 p_frame->i_pts = VLC_TS_INVALID;
594 #ifdef AVFORMAT_DEBUG
595 msg_Dbg( p_demux, "tk[%d] dts=%"PRId64" pts=%"PRId64,
596 pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
599 if( p_frame->i_dts > VLC_TS_INVALID &&
600 ( pkt.stream_index == p_sys->i_pcr_tk || p_sys->i_pcr_tk < 0 ) )
602 p_sys->i_pcr_tk = pkt.stream_index;
603 p_sys->i_pcr = p_frame->i_dts;
605 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
608 es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame );
610 UpdateSeekPoint( p_demux, p_sys->i_pcr);
611 av_free_packet( &pkt );
615 static void UpdateSeekPoint( demux_t *p_demux, int64_t i_time )
617 demux_sys_t *p_sys = p_demux->p_sys;
620 if( !p_sys->p_title )
623 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
625 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
630 if( i != p_demux->info.i_seekpoint && i >= 0 )
632 p_demux->info.i_seekpoint = i;
633 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
637 static block_t *BuildSsaFrame( const AVPacket *p_pkt, unsigned i_order )
639 if( p_pkt->size <= 0 )
643 const size_t i_buffer_size = __MIN( sizeof(buffer) - 1, p_pkt->size );
644 memcpy( buffer, p_pkt->data, i_buffer_size );
645 buffer[i_buffer_size] = '\0';
652 if( sscanf( buffer, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,%n", &i_layer,
653 &h0, &m0, &s0, &c0, &h1, &m1, &s1, &c1, &i_position ) < 9 )
655 if( i_position <= 0 || i_position >= i_buffer_size )
659 if( asprintf( &p, "%u,%d,%.*s", i_order, i_layer, p_pkt->size - i_position, p_pkt->data + i_position ) < 0 )
662 block_t *p_frame = block_heap_Alloc( p, p, strlen(p) + 1 );
664 p_frame->i_length = CLOCK_FREQ * ((h1 - h1) * 3600 +
667 CLOCK_FREQ * (c1-c0) / 100;
671 /*****************************************************************************
673 *****************************************************************************/
674 static int Control( demux_t *p_demux, int i_query, va_list args )
676 demux_sys_t *p_sys = p_demux->p_sys;
682 case DEMUX_GET_POSITION:
683 pf = (double*) va_arg( args, double* ); *pf = 0.0;
684 i64 = stream_Size( p_demux->s );
687 double current = stream_Tell( p_demux->s );
688 *pf = current / (double)i64;
691 if( (p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE) && (p_sys->i_pcr > 0) )
693 *pf = (double)p_sys->i_pcr / (double)p_sys->ic->duration;
698 case DEMUX_SET_POSITION:
699 f = (double) va_arg( args, double );
700 if( p_sys->i_pcr > 0 )
702 i64 = p_sys->ic->duration * f;
703 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
704 i64 += p_sys->ic->start_time;
706 msg_Warn( p_demux, "DEMUX_SET_POSITION: %"PRId64, i64 );
708 /* If we have a duration, we prefer to seek by time
709 but if we don't, or if the seek fails, try BYTE seeking */
710 if( p_sys->ic->duration == (int64_t)AV_NOPTS_VALUE ||
711 (av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0) )
713 int64_t i_size = stream_Size( p_demux->s );
716 msg_Warn( p_demux, "DEMUX_SET_BYTE_POSITION: %"PRId64, i64 );
717 if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BYTE ) < 0 )
722 UpdateSeekPoint( p_demux, i64 );
724 p_sys->i_pcr = -1; /* Invalidate time display */
728 case DEMUX_GET_LENGTH:
729 pi64 = (int64_t*)va_arg( args, int64_t * );
730 if( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE )
731 *pi64 = p_sys->ic->duration * 1000000 / AV_TIME_BASE;
737 pi64 = (int64_t*)va_arg( args, int64_t * );
738 *pi64 = p_sys->i_pcr;
742 i64 = (int64_t)va_arg( args, int64_t );
743 i64 = i64 *AV_TIME_BASE / 1000000;
744 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
745 i64 += p_sys->ic->start_time;
747 msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
749 if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
753 p_sys->i_pcr = -1; /* Invalidate time display */
754 UpdateSeekPoint( p_demux, i64 );
757 case DEMUX_HAS_UNSUPPORTED_META:
759 bool *pb_bool = (bool*)va_arg( args, bool* );
767 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
769 if( p_sys->ic->title[0] )
770 vlc_meta_SetTitle( p_meta, p_sys->ic->title );
771 if( p_sys->ic->author[0] )
772 vlc_meta_SetArtist( p_meta, p_sys->ic->author );
773 if( p_sys->ic->copyright[0] )
774 vlc_meta_SetCopyright( p_meta, p_sys->ic->copyright );
775 if( p_sys->ic->comment[0] )
776 vlc_meta_SetDescription( p_meta, p_sys->ic->comment );
777 if( p_sys->ic->genre[0] )
778 vlc_meta_SetGenre( p_meta, p_sys->ic->genre );
782 case DEMUX_GET_ATTACHMENTS:
784 input_attachment_t ***ppp_attach =
785 (input_attachment_t***)va_arg( args, input_attachment_t*** );
786 int *pi_int = (int*)va_arg( args, int * );
789 if( p_sys->i_attachments <= 0 )
792 *pi_int = p_sys->i_attachments;;
793 *ppp_attach = malloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
794 for( i = 0; i < p_sys->i_attachments; i++ )
795 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
799 case DEMUX_GET_TITLE_INFO:
801 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
802 int *pi_int = (int*)va_arg( args, int* );
803 int *pi_title_offset = (int*)va_arg( args, int* );
804 int *pi_seekpoint_offset = (int*)va_arg( args, int* );
806 if( !p_sys->p_title )
810 *ppp_title = malloc( sizeof( input_title_t**) );
811 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
812 *pi_title_offset = 0;
813 *pi_seekpoint_offset = 0;
816 case DEMUX_SET_TITLE:
818 const int i_title = (int)va_arg( args, int );
819 if( !p_sys->p_title || i_title != 0 )
823 case DEMUX_SET_SEEKPOINT:
825 const int i_seekpoint = (int)va_arg( args, int );
826 if( !p_sys->p_title )
829 i64 = p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset *AV_TIME_BASE / 1000000;
830 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
831 i64 += p_sys->ic->start_time;
833 msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
835 if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
839 p_sys->i_pcr = -1; /* Invalidate time display */
840 UpdateSeekPoint( p_demux, i64 );
850 /*****************************************************************************
851 * I/O wrappers for libavformat
852 *****************************************************************************/
853 static int IORead( void *opaque, uint8_t *buf, int buf_size )
855 URLContext *p_url = opaque;
856 demux_t *p_demux = p_url->priv_data;
857 if( buf_size < 0 ) return -1;
858 int i_ret = stream_Read( p_demux->s, buf, buf_size );
859 return i_ret ? i_ret : -1;
862 static int64_t IOSeek( void *opaque, int64_t offset, int whence )
864 URLContext *p_url = opaque;
865 demux_t *p_demux = p_url->priv_data;
867 int64_t i_size = stream_Size( p_demux->s );
869 #ifdef AVFORMAT_DEBUG
870 msg_Warn( p_demux, "IOSeek offset: %"PRId64", whence: %i", offset, whence );
880 i_absolute = (int64_t)offset;
883 i_absolute = stream_Tell( p_demux->s ) + (int64_t)offset;
886 i_absolute = i_size + (int64_t)offset;
895 msg_Dbg( p_demux, "Trying to seek before the beginning" );
899 if( i_size > 0 && i_absolute >= i_size )
901 msg_Dbg( p_demux, "Trying to seek too far : EOF?" );
905 if( stream_Seek( p_demux->s, i_absolute ) )
907 msg_Warn( p_demux, "we were not allowed to seek, or EOF " );
911 return stream_Tell( p_demux->s );
914 #endif /* HAVE_LIBAVFORMAT_AVFORMAT_H */