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->time_base.den <= 0 )
503 msg_Warn( p_demux, "Invalid time base for the stream %d", pkt.stream_index );
504 av_free_packet( &pkt );
507 if( p_stream->codec->codec_id == CODEC_ID_SSA )
509 p_frame = BuildSsaFrame( &pkt, p_sys->i_ssa_order++ );
515 if( ( p_frame = block_New( p_demux, pkt.size ) ) == NULL )
517 memcpy( p_frame->p_buffer, pkt.data, pkt.size );
520 if( pkt.flags & PKT_FLAG_KEY )
521 p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
523 i_start_time = ( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) ?
524 ( p_sys->ic->start_time * 1000000 / AV_TIME_BASE ) : 0;
526 p_frame->i_dts = ( pkt.dts == (int64_t)AV_NOPTS_VALUE ) ?
527 VLC_TS_INVALID : (pkt.dts) * 1000000 *
528 p_stream->time_base.num /
529 p_stream->time_base.den - i_start_time + VLC_TS_0;
530 p_frame->i_pts = ( pkt.pts == (int64_t)AV_NOPTS_VALUE ) ?
531 VLC_TS_INVALID : (pkt.pts) * 1000000 *
532 p_stream->time_base.num /
533 p_stream->time_base.den - i_start_time + VLC_TS_0;
534 if( pkt.duration > 0 && p_frame->i_length <= 0 )
535 p_frame->i_length = pkt.duration * 1000000 *
536 p_stream->time_base.num /
537 p_stream->time_base.den;
539 if( pkt.dts != AV_NOPTS_VALUE && pkt.dts == pkt.pts &&
540 p_stream->codec->codec_type == CODEC_TYPE_VIDEO )
542 /* Add here notoriously bugged file formats/samples regarding PTS */
543 if( !strcmp( p_sys->fmt->name, "flv" ) )
544 p_frame->i_pts = VLC_TS_INVALID;
546 #ifdef AVFORMAT_DEBUG
547 msg_Dbg( p_demux, "tk[%d] dts=%"PRId64" pts=%"PRId64,
548 pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
551 if( p_frame->i_dts > VLC_TS_INVALID &&
552 ( pkt.stream_index == p_sys->i_pcr_tk || p_sys->i_pcr_tk < 0 ) )
554 p_sys->i_pcr_tk = pkt.stream_index;
555 p_sys->i_pcr = p_frame->i_dts;
557 es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );
560 es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame );
562 UpdateSeekPoint( p_demux, p_sys->i_pcr);
563 av_free_packet( &pkt );
567 static void UpdateSeekPoint( demux_t *p_demux, int64_t i_time )
569 demux_sys_t *p_sys = p_demux->p_sys;
572 if( !p_sys->p_title )
575 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
577 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
582 if( i != p_demux->info.i_seekpoint && i >= 0 )
584 p_demux->info.i_seekpoint = i;
585 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
589 static block_t *BuildSsaFrame( const AVPacket *p_pkt, unsigned i_order )
591 if( p_pkt->size <= 0 )
595 const size_t i_buffer_size = __MIN( sizeof(buffer) - 1, p_pkt->size );
596 memcpy( buffer, p_pkt->data, i_buffer_size );
597 buffer[i_buffer_size] = '\0';
604 if( sscanf( buffer, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,%n", &i_layer,
605 &h0, &m0, &s0, &c0, &h1, &m1, &s1, &c1, &i_position ) < 9 )
607 if( i_position <= 0 || i_position >= i_buffer_size )
611 if( asprintf( &p, "%u,%d,%.*s", i_order, i_layer, p_pkt->size - i_position, p_pkt->data + i_position ) < 0 )
614 block_t *p_frame = block_heap_Alloc( p, p, strlen(p) + 1 );
616 p_frame->i_length = CLOCK_FREQ * ((h1 - h1) * 3600 +
619 CLOCK_FREQ * (c1-c0) / 100;
623 /*****************************************************************************
625 *****************************************************************************/
626 static int Control( demux_t *p_demux, int i_query, va_list args )
628 demux_sys_t *p_sys = p_demux->p_sys;
634 case DEMUX_GET_POSITION:
635 pf = (double*) va_arg( args, double* ); *pf = 0.0;
636 i64 = stream_Size( p_demux->s );
639 double current = stream_Tell( p_demux->s );
640 *pf = current / (double)i64;
643 if( (p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE) && (p_sys->i_pcr > 0) )
645 *pf = (double)p_sys->i_pcr / (double)p_sys->ic->duration;
650 case DEMUX_SET_POSITION:
651 f = (double) va_arg( args, double );
652 if( p_sys->i_pcr > 0 )
654 i64 = p_sys->ic->duration * f;
655 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
656 i64 += p_sys->ic->start_time;
658 msg_Warn( p_demux, "DEMUX_SET_POSITION: %"PRId64, i64 );
660 /* If we have a duration, we prefer to seek by time
661 but if we don't, or if the seek fails, try BYTE seeking */
662 if( p_sys->ic->duration == (int64_t)AV_NOPTS_VALUE ||
663 (av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0) )
665 int64_t i_size = stream_Size( p_demux->s );
668 msg_Warn( p_demux, "DEMUX_SET_BYTE_POSITION: %"PRId64, i64 );
669 if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BYTE ) < 0 )
674 UpdateSeekPoint( p_demux, i64 );
676 p_sys->i_pcr = -1; /* Invalidate time display */
680 case DEMUX_GET_LENGTH:
681 pi64 = (int64_t*)va_arg( args, int64_t * );
682 if( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE )
683 *pi64 = p_sys->ic->duration * 1000000 / AV_TIME_BASE;
689 pi64 = (int64_t*)va_arg( args, int64_t * );
690 *pi64 = p_sys->i_pcr;
694 i64 = (int64_t)va_arg( args, int64_t );
695 i64 = i64 *AV_TIME_BASE / 1000000;
696 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
697 i64 += p_sys->ic->start_time;
699 msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
701 if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
705 p_sys->i_pcr = -1; /* Invalidate time display */
706 UpdateSeekPoint( p_demux, i64 );
709 case DEMUX_HAS_UNSUPPORTED_META:
711 bool *pb_bool = (bool*)va_arg( args, bool* );
719 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
721 if( !p_sys->ic->title[0] || !p_sys->ic->author[0] ||
722 !p_sys->ic->copyright[0] || !p_sys->ic->comment[0] ||
723 /*!p_sys->ic->album[0] ||*/ !p_sys->ic->genre[0] )
728 if( p_sys->ic->title[0] )
729 vlc_meta_SetTitle( p_meta, p_sys->ic->title );
730 if( p_sys->ic->author[0] )
731 vlc_meta_SetArtist( p_meta, p_sys->ic->author );
732 if( p_sys->ic->copyright[0] )
733 vlc_meta_SetCopyright( p_meta, p_sys->ic->copyright );
734 if( p_sys->ic->comment[0] )
735 vlc_meta_SetDescription( p_meta, p_sys->ic->comment );
736 if( p_sys->ic->genre[0] )
737 vlc_meta_SetGenre( p_meta, p_sys->ic->genre );
741 case DEMUX_GET_ATTACHMENTS:
743 input_attachment_t ***ppp_attach =
744 (input_attachment_t***)va_arg( args, input_attachment_t*** );
745 int *pi_int = (int*)va_arg( args, int * );
748 if( p_sys->i_attachments <= 0 )
751 *pi_int = p_sys->i_attachments;;
752 *ppp_attach = malloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
753 for( i = 0; i < p_sys->i_attachments; i++ )
754 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
758 case DEMUX_GET_TITLE_INFO:
760 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
761 int *pi_int = (int*)va_arg( args, int* );
762 int *pi_title_offset = (int*)va_arg( args, int* );
763 int *pi_seekpoint_offset = (int*)va_arg( args, int* );
765 if( !p_sys->p_title )
769 *ppp_title = malloc( sizeof( input_title_t**) );
770 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
771 *pi_title_offset = 0;
772 *pi_seekpoint_offset = 0;
775 case DEMUX_SET_TITLE:
777 const int i_title = (int)va_arg( args, int );
778 if( !p_sys->p_title || i_title != 0 )
782 case DEMUX_SET_SEEKPOINT:
784 const int i_seekpoint = (int)va_arg( args, int );
785 if( !p_sys->p_title )
788 i64 = p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset *AV_TIME_BASE / 1000000;
789 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
790 i64 += p_sys->ic->start_time;
792 msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
794 if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
798 p_sys->i_pcr = -1; /* Invalidate time display */
799 UpdateSeekPoint( p_demux, i64 );
809 /*****************************************************************************
810 * I/O wrappers for libavformat
811 *****************************************************************************/
812 static int IORead( void *opaque, uint8_t *buf, int buf_size )
814 URLContext *p_url = opaque;
815 demux_t *p_demux = p_url->priv_data;
816 if( buf_size < 0 ) return -1;
817 int i_ret = stream_Read( p_demux->s, buf, buf_size );
818 return i_ret ? i_ret : -1;
821 static int64_t IOSeek( void *opaque, int64_t offset, int whence )
823 URLContext *p_url = opaque;
824 demux_t *p_demux = p_url->priv_data;
826 int64_t i_size = stream_Size( p_demux->s );
828 #ifdef AVFORMAT_DEBUG
829 msg_Warn( p_demux, "IOSeek offset: %"PRId64", whence: %i", offset, whence );
839 i_absolute = (int64_t)offset;
842 i_absolute = stream_Tell( p_demux->s ) + (int64_t)offset;
845 i_absolute = i_size + (int64_t)offset;
854 msg_Dbg( p_demux, "Trying to seek before the beginning" );
858 if( i_size > 0 && i_absolute >= i_size )
860 msg_Dbg( p_demux, "Trying to seek too far : EOF?" );
864 if( stream_Seek( p_demux->s, i_absolute ) )
866 msg_Warn( p_demux, "we were not allowed to seek, or EOF " );
870 return stream_Tell( p_demux->s );
873 #endif /* HAVE_LIBAVFORMAT_AVFORMAT_H */