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;
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 )
124 msg_Warn( p_demux, "cannot peek" );
129 av_register_all(); /* Can be called several times */
130 vlc_avcodec_unlock();
133 if( !( fmt = av_probe_input_format( &pd, 1 ) ) )
135 msg_Dbg( p_demux, "couldn't guess format" );
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" ) ) )
152 /* Don't trigger false alarms on bin files */
153 if( !p_demux->b_force && !strcmp( fmt->name, "psxstr" ) )
157 if( !p_demux->psz_path ) return VLC_EGENERIC;
159 i_len = strlen( p_demux->psz_path );
160 if( i_len < 4 ) return VLC_EGENERIC;
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" ) )
170 msg_Dbg( p_demux, "detected format: %s", fmt->name );
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 ) );
180 p_sys->i_pcr_tk = -1;
182 p_sys->i_ssa_order = 0;
183 TAB_INIT( p_sys->i_attachments, p_sys->attachments);
184 p_sys->p_title = NULL;
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 );
204 if( av_open_input_stream( &p_sys->ic, &p_sys->io, p_demux->psz_path,
207 msg_Err( p_demux, "av_open_input_stream failed" );
208 CloseDemux( p_this );
212 vlc_avcodec_lock(); /* avformat calls avcodec behind our back!!! */
213 if( av_find_stream_info( p_sys->ic ) < 0 )
215 vlc_avcodec_unlock();
216 msg_Warn( p_demux, "av_find_stream_info failed" );
218 vlc_avcodec_unlock();
220 for( i = 0; i < p_sys->ic->nb_streams; i++ )
222 AVStream *s = p_sys->ic->streams[i];
223 AVCodecContext *cc = s->codec;
228 const char *psz_type = "unknown";
230 if( !GetVlcFourcc( cc->codec_id, NULL, &fcc, NULL ) )
231 fcc = VLC_FOURCC( 'u', 'n', 'd', 'f' );
233 switch( cc->codec_type )
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;
243 fmt.audio.i_bitspersample = cc->bits_per_coded_sample;
245 fmt.audio.i_blockalign = cc->block_align;
249 case CODEC_TYPE_VIDEO:
250 es_format_Init( &fmt, VIDEO_ES, fcc );
252 /* Special case for raw video data */
253 if( cc->codec_id == CODEC_ID_RAWVIDEO )
255 msg_Dbg( p_demux, "raw video, pixel format: %i", cc->pix_fmt );
256 if( GetVlcChroma( &fmt.video, cc->pix_fmt ) != VLC_SUCCESS)
258 msg_Err( p_demux, "was unable to find a FourCC match for raw video" );
261 fmt.i_codec = fmt.video.i_chroma;
264 fmt.video.i_width = cc->width;
265 fmt.video.i_height = cc->height;
268 fmt.video.p_palette = malloc( sizeof(video_palette_t) );
269 *fmt.video.p_palette = *(video_palette_t *)cc->palctrl;
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 )
282 char *psz_buf = malloc( cc->extradata_size + 1);
283 if( psz_buf != NULL )
285 memcpy( psz_buf, cc->extradata , cc->extradata_size );
286 psz_buf[cc->extradata_size] = '\0';
288 psz_start = strstr( psz_buf, "size:" );
290 vobsub_size_parse( psz_start,
291 &fmt.subs.spu.i_original_frame_width,
292 &fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
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 );
300 msg_Warn( p_demux, "reading original frame size failed" );
303 psz_start = strstr( psz_buf, "palette:" );
305 vobsub_palette_parse( psz_start, &fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
307 fmt.subs.spu.palette[0] = 0xBeef;
308 msg_Dbg( p_demux, "vobsub palette read" );
312 msg_Warn( p_demux, "reading original palette failed" );
318 psz_type = "subtitle";
322 es_format_Init( &fmt, UNKNOWN_ES, 0 );
323 #ifdef HAVE_FFMPEG_CODEC_ATTACHMENT
324 if( cc->codec_type == CODEC_TYPE_ATTACHMENT )
326 input_attachment_t *p_attachment;
327 psz_type = "attachment";
328 if( cc->codec_id == CODEC_ID_TTF )
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 );
334 else msg_Warn( p_demux, "unsupported attachment type in ffmpeg demux" );
339 if( cc->codec_type == CODEC_TYPE_DATA )
342 msg_Warn( p_demux, "unsupported track type in ffmpeg demux" );
345 fmt.psz_language = strdup( s->language );
346 if( s->disposition & AV_DISPOSITION_DEFAULT )
347 fmt.i_priority = 1000;
349 #ifdef HAVE_FFMPEG_CODEC_ATTACHMENT
350 if( cc->codec_type != CODEC_TYPE_ATTACHMENT )
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;
357 if( cc->codec_id == CODEC_ID_THEORA && b_ogg )
362 for( i_count = 0; i_count < 3; i_count++ )
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 )
371 p_extra += 2 + pi_size[i_count];
372 i_extra -= 2 + pi_size[i_count];
374 if( i_count > 0 && xiph_PackHeaders( &fmt.i_extra, &fmt.p_extra,
375 pi_size, pp_data, i_count ) )
381 else if( cc->codec_id == CODEC_ID_SPEEX && b_ogg )
383 uint8_t p_dummy_comment[] = {
390 pi_size[0] = i_extra;
391 pp_data[0] = (uint8_t*)p_extra;
393 pi_size[1] = sizeof(p_dummy_comment);
394 pp_data[1] = p_dummy_comment;
396 if( pi_size[0] > 0 && xiph_PackHeaders( &fmt.i_extra, &fmt.p_extra,
397 pi_size, pp_data, 2 ) )
403 else if( cc->extradata_size > 0 )
405 fmt.p_extra = malloc( i_extra );
408 fmt.i_extra = i_extra;
409 memcpy( fmt.p_extra, p_extra, i_extra );
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 );
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 );
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;
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 );
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++ )
438 seekpoint_t *s = vlc_seekpoint_New();
440 if( p_sys->ic->chapters[i]->title )
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 );
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 );
457 /*****************************************************************************
459 *****************************************************************************/
460 void CloseDemux( vlc_object_t *p_this )
462 demux_t *p_demux = (demux_t*)p_this;
463 demux_sys_t *p_sys = p_demux->p_sys;
465 FREENULL( p_sys->tk );
467 if( p_sys->ic ) av_close_input_stream( p_sys->ic );
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);
474 vlc_input_title_Delete( p_sys->p_title );
476 free( p_sys->io_buffer );
480 /*****************************************************************************
482 *****************************************************************************/
483 static int Demux( demux_t *p_demux )
485 demux_sys_t *p_sys = p_demux->p_sys;
488 int64_t i_start_time;
491 if( av_read_frame( p_sys->ic, &pkt ) )
495 if( pkt.stream_index < 0 || pkt.stream_index >= p_sys->i_tk )
497 av_free_packet( &pkt );
500 const AVStream *p_stream = p_sys->ic->streams[pkt.stream_index];
501 if( p_stream->codec->codec_id == CODEC_ID_SSA )
503 p_frame = BuildSsaFrame( &pkt, p_sys->i_ssa_order++ );
509 if( ( p_frame = block_New( p_demux, pkt.size ) ) == NULL )
511 memcpy( p_frame->p_buffer, pkt.data, pkt.size );
514 if( pkt.flags & PKT_FLAG_KEY )
515 p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
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;
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;
533 if( pkt.dts != AV_NOPTS_VALUE && pkt.dts == pkt.pts &&
534 p_stream->codec->codec_type == CODEC_TYPE_VIDEO )
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;
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 );
545 if( p_frame->i_dts > VLC_TS_INVALID &&
546 ( pkt.stream_index == p_sys->i_pcr_tk || p_sys->i_pcr_tk < 0 ) )
548 p_sys->i_pcr_tk = pkt.stream_index;
549 p_sys->i_pcr = p_frame->i_dts;
551 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
554 es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame );
556 UpdateSeekPoint( p_demux, p_sys->i_pcr);
557 av_free_packet( &pkt );
561 static void UpdateSeekPoint( demux_t *p_demux, int64_t i_time )
563 demux_sys_t *p_sys = p_demux->p_sys;
566 if( !p_sys->p_title )
569 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
571 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
576 if( i != p_demux->info.i_seekpoint && i >= 0 )
578 p_demux->info.i_seekpoint = i;
579 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
583 static block_t *BuildSsaFrame( const AVPacket *p_pkt, unsigned i_order )
585 if( p_pkt->size <= 0 )
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';
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 )
601 if( i_position <= 0 || i_position >= i_buffer_size )
605 if( asprintf( &p, "%u,%d,%.*s", i_order, i_layer, p_pkt->size - i_position, p_pkt->data + i_position ) < 0 )
608 block_t *p_frame = block_heap_Alloc( p, p, strlen(p) + 1 );
610 p_frame->i_length = CLOCK_FREQ * ((h1 - h1) * 3600 +
613 CLOCK_FREQ * (c1-c0) / 100;
617 /*****************************************************************************
619 *****************************************************************************/
620 static int Control( demux_t *p_demux, int i_query, va_list args )
622 demux_sys_t *p_sys = p_demux->p_sys;
628 case DEMUX_GET_POSITION:
629 pf = (double*) va_arg( args, double* ); *pf = 0.0;
630 i64 = stream_Size( p_demux->s );
633 double current = stream_Tell( p_demux->s );
634 *pf = current / (double)i64;
637 if( (p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE) && (p_sys->i_pcr > 0) )
639 *pf = (double)p_sys->i_pcr / (double)p_sys->ic->duration;
644 case DEMUX_SET_POSITION:
645 f = (double) va_arg( args, double );
646 if( p_sys->i_pcr > 0 )
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;
652 msg_Warn( p_demux, "DEMUX_SET_POSITION: %"PRId64, i64 );
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) )
659 int64_t i_size = stream_Size( p_demux->s );
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 )
668 UpdateSeekPoint( p_demux, i64 );
670 p_sys->i_pcr = -1; /* Invalidate time display */
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 *pi64 = p_sys->ic->duration * 1000000 / AV_TIME_BASE;
683 pi64 = (int64_t*)va_arg( args, int64_t * );
684 *pi64 = p_sys->i_pcr;
688 i64 = (int64_t)va_arg( args, int64_t );
689 i64 = i64 *AV_TIME_BASE / 1000000;
690 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
691 i64 += p_sys->ic->start_time;
693 msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
695 if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
699 p_sys->i_pcr = -1; /* Invalidate time display */
700 UpdateSeekPoint( p_demux, i64 );
703 case DEMUX_HAS_UNSUPPORTED_META:
705 bool *pb_bool = (bool*)va_arg( args, bool* );
713 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
715 if( !p_sys->ic->title[0] || !p_sys->ic->author[0] ||
716 !p_sys->ic->copyright[0] || !p_sys->ic->comment[0] ||
717 /*!p_sys->ic->album[0] ||*/ !p_sys->ic->genre[0] )
722 if( p_sys->ic->title[0] )
723 vlc_meta_SetTitle( p_meta, p_sys->ic->title );
724 if( p_sys->ic->author[0] )
725 vlc_meta_SetArtist( p_meta, p_sys->ic->author );
726 if( p_sys->ic->copyright[0] )
727 vlc_meta_SetCopyright( p_meta, p_sys->ic->copyright );
728 if( p_sys->ic->comment[0] )
729 vlc_meta_SetDescription( p_meta, p_sys->ic->comment );
730 if( p_sys->ic->genre[0] )
731 vlc_meta_SetGenre( p_meta, p_sys->ic->genre );
735 case DEMUX_GET_ATTACHMENTS:
737 input_attachment_t ***ppp_attach =
738 (input_attachment_t***)va_arg( args, input_attachment_t*** );
739 int *pi_int = (int*)va_arg( args, int * );
742 if( p_sys->i_attachments <= 0 )
745 *pi_int = p_sys->i_attachments;;
746 *ppp_attach = malloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
747 for( i = 0; i < p_sys->i_attachments; i++ )
748 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
752 case DEMUX_GET_TITLE_INFO:
754 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
755 int *pi_int = (int*)va_arg( args, int* );
756 int *pi_title_offset = (int*)va_arg( args, int* );
757 int *pi_seekpoint_offset = (int*)va_arg( args, int* );
759 if( !p_sys->p_title )
763 *ppp_title = malloc( sizeof( input_title_t**) );
764 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
765 *pi_title_offset = 0;
766 *pi_seekpoint_offset = 0;
769 case DEMUX_SET_TITLE:
771 const int i_title = (int)va_arg( args, int );
772 if( !p_sys->p_title || i_title != 0 )
776 case DEMUX_SET_SEEKPOINT:
778 const int i_seekpoint = (int)va_arg( args, int );
779 if( !p_sys->p_title )
782 i64 = p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset *AV_TIME_BASE / 1000000;
783 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
784 i64 += p_sys->ic->start_time;
786 msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
788 if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
792 p_sys->i_pcr = -1; /* Invalidate time display */
793 UpdateSeekPoint( p_demux, i64 );
803 /*****************************************************************************
804 * I/O wrappers for libavformat
805 *****************************************************************************/
806 static int IORead( void *opaque, uint8_t *buf, int buf_size )
808 URLContext *p_url = opaque;
809 demux_t *p_demux = p_url->priv_data;
810 if( buf_size < 0 ) return -1;
811 int i_ret = stream_Read( p_demux->s, buf, buf_size );
812 return i_ret ? i_ret : -1;
815 static int64_t IOSeek( void *opaque, int64_t offset, int whence )
817 URLContext *p_url = opaque;
818 demux_t *p_demux = p_url->priv_data;
820 int64_t i_size = stream_Size( p_demux->s );
822 #ifdef AVFORMAT_DEBUG
823 msg_Warn( p_demux, "IOSeek offset: %"PRId64", whence: %i", offset, whence );
833 i_absolute = (int64_t)offset;
836 i_absolute = stream_Tell( p_demux->s ) + (int64_t)offset;
839 i_absolute = i_size + (int64_t)offset;
848 msg_Dbg( p_demux, "Trying to seek before the beginning" );
852 if( i_size > 0 && i_absolute >= i_size )
854 msg_Dbg( p_demux, "Trying to seek too far : EOF?" );
858 if( stream_Seek( p_demux->s, i_absolute ) )
860 msg_Warn( p_demux, "we were not allowed to seek, or EOF " );
864 return stream_Tell( p_demux->s );
867 #endif /* HAVE_LIBAVFORMAT_AVFORMAT_H */