1 /*****************************************************************************
2 * demux.c: demuxer using libavformat
3 *****************************************************************************
4 * Copyright (C) 2004-2009 VLC authors and VideoLAN
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * 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>
41 #include <libavformat/avformat.h>
43 #include "../../codec/avcodec/avcodec.h"
44 #include "../../codec/avcodec/chroma.h"
45 #include "../../codec/avcodec/avcommon.h"
48 #include "../vobsub.h"
50 /* Support for deprecated APIs */
52 #if LIBAVFORMAT_VERSION_MAJOR < 54
53 # define AVDictionaryEntry AVMetadataTag
54 # define av_dict_get av_metadata_get
57 //#define AVFORMAT_DEBUG 1
59 /* Version checking */
60 #if defined(HAVE_FFMPEG_AVFORMAT_H) || defined(HAVE_LIBAVFORMAT_AVFORMAT_H)
62 # define HAVE_AVUTIL_CODEC_ATTACHMENT 1
64 /*****************************************************************************
65 * demux_sys_t: demux descriptor
66 *****************************************************************************/
83 input_attachment_t **attachments;
85 /* Only one title with seekpoints possible atm. */
86 input_title_t *p_title;
89 /*****************************************************************************
91 *****************************************************************************/
92 static int Demux ( demux_t *p_demux );
93 static int Control( demux_t *p_demux, int i_query, va_list args );
95 static int IORead( void *opaque, uint8_t *buf, int buf_size );
96 static int64_t IOSeek( void *opaque, int64_t offset, int whence );
98 static block_t *BuildSsaFrame( const AVPacket *p_pkt, unsigned i_order );
99 static void UpdateSeekPoint( demux_t *p_demux, int64_t i_time );
100 static void ResetTime( demux_t *p_demux, int64_t i_time );
102 /*****************************************************************************
104 *****************************************************************************/
105 int OpenDemux( vlc_object_t *p_this )
107 demux_t *p_demux = (demux_t*)p_this;
110 AVInputFormat *fmt = NULL;
112 int64_t i_start_time = -1;
117 if( p_demux->psz_file )
118 psz_url = strdup( p_demux->psz_file );
121 if( asprintf( &psz_url, "%s://%s", p_demux->psz_access, p_demux->psz_location ) == -1)
124 msg_Dbg( p_demux, "trying url: %s", psz_url );
125 /* Init Probe data */
126 pd.filename = psz_url;
127 if( ( pd.buf_size = stream_Peek( p_demux->s, (const uint8_t**)&pd.buf, 2048 + 213 ) ) <= 0 )
130 msg_Warn( p_demux, "cannot peek" );
133 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_can_seek );
137 char *psz_format = var_InheritString( p_this, "avformat-format" );
140 if( (fmt = av_find_input_format(psz_format)) )
141 msg_Dbg( p_demux, "forcing format: %s", fmt->name );
146 if( !fmt && !( fmt = av_probe_input_format( &pd, 1 ) ) )
148 msg_Dbg( p_demux, "couldn't guess format" );
153 if( !p_demux->b_force )
155 static const char ppsz_blacklist[][16] = {
156 /* Don't handle MPEG unless forced */
157 "mpeg", "vcd", "vob", "mpegts",
158 /* libavformat's redirector won't work */
160 /* Don't handle subtitles format */
161 "ass", "srt", "microdvd",
165 for( int i = 0; *ppsz_blacklist[i]; i++ )
167 if( !strcmp( fmt->name, ppsz_blacklist[i] ) )
175 /* Don't trigger false alarms on bin files */
176 if( !p_demux->b_force && !strcmp( fmt->name, "psxstr" ) )
180 if( !p_demux->psz_file )
186 i_len = strlen( p_demux->psz_file );
193 if( strcasecmp( &p_demux->psz_file[i_len - 4], ".str" ) &&
194 strcasecmp( &p_demux->psz_file[i_len - 4], ".xai" ) &&
195 strcasecmp( &p_demux->psz_file[i_len - 3], ".xa" ) )
202 msg_Dbg( p_demux, "detected format: %s", fmt->name );
204 /* Fill p_demux fields */
205 p_demux->pf_demux = Demux;
206 p_demux->pf_control = Control;
207 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
212 p_sys->tk_pcr = NULL;
213 p_sys->i_ssa_order = 0;
214 TAB_INIT( p_sys->i_attachments, p_sys->attachments);
215 p_sys->p_title = NULL;
217 /* Create I/O wrapper */
218 p_sys->io_buffer_size = 32768; /* FIXME */
219 p_sys->io_buffer = malloc( p_sys->io_buffer_size );
221 p_sys->ic = avformat_alloc_context();
222 p_sys->ic->pb = avio_alloc_context( p_sys->io_buffer,
223 p_sys->io_buffer_size, 0, p_demux, IORead, NULL, IOSeek );
224 p_sys->ic->pb->seekable = b_can_seek ? AVIO_SEEKABLE_NORMAL : 0;
225 error = avformat_open_input(&p_sys->ic, psz_url, p_sys->fmt, NULL);
229 errno = AVUNERROR(error);
230 msg_Err( p_demux, "Could not open %s: %m", psz_url );
233 CloseDemux( p_this );
238 #if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(26<<8)+0)
239 char *psz_opts = var_InheritString( p_demux, "avformat-options" );
240 AVDictionary *options[p_sys->ic->nb_streams ? p_sys->ic->nb_streams : 1];
242 unsigned int nb_streams = p_sys->ic->nb_streams;
243 for (unsigned i = 1; i < nb_streams; i++)
245 if (psz_opts && *psz_opts) {
246 options[0] = vlc_av_get_options(psz_opts);
247 for (unsigned i = 1; i < nb_streams; i++) {
248 av_dict_copy(&options[i], options[0], 0);
252 vlc_avcodec_lock(); /* avformat calls avcodec behind our back!!! */
253 error = avformat_find_stream_info( p_sys->ic, options );
254 /* FIXME: what if nb_streams change after that call? */
255 vlc_avcodec_unlock();
256 AVDictionaryEntry *t = NULL;
257 while ((t = av_dict_get(options[0], "", t, AV_DICT_IGNORE_SUFFIX))) {
258 msg_Err( p_demux, "Unknown option \"%s\"", t->key );
260 av_dict_free(&options[0]);
261 for (unsigned i = 1; i < nb_streams; i++) {
262 av_dict_free(&options[i]);
265 vlc_avcodec_lock(); /* avformat calls avcodec behind our back!!! */
266 error = av_find_stream_info( p_sys->ic );
267 vlc_avcodec_unlock();
272 errno = AVUNERROR(error);
273 msg_Warn( p_demux, "Could not find stream info: %m" );
276 for( i = 0; i < p_sys->ic->nb_streams; i++ )
278 AVStream *s = p_sys->ic->streams[i];
279 AVCodecContext *cc = s->codec;
284 const char *psz_type = "unknown";
286 if( !GetVlcFourcc( cc->codec_id, NULL, &fcc, NULL ) )
287 fcc = VLC_FOURCC( 'u', 'n', 'd', 'f' );
289 #if LIBAVFORMAT_VERSION_INT >= ((54<<16)+(2<<8)+0)
290 /* Do not use the cover art as a stream */
291 if( s->disposition == AV_DISPOSITION_ATTACHED_PIC )
295 switch( cc->codec_type )
297 case AVMEDIA_TYPE_AUDIO:
298 es_format_Init( &fmt, AUDIO_ES, fcc );
299 fmt.i_bitrate = cc->bit_rate;
300 fmt.audio.i_channels = cc->channels;
301 fmt.audio.i_rate = cc->sample_rate;
302 fmt.audio.i_bitspersample = cc->bits_per_coded_sample;
303 fmt.audio.i_blockalign = cc->block_align;
307 case AVMEDIA_TYPE_VIDEO:
308 es_format_Init( &fmt, VIDEO_ES, fcc );
310 /* Special case for raw video data */
311 if( cc->codec_id == AV_CODEC_ID_RAWVIDEO )
313 msg_Dbg( p_demux, "raw video, pixel format: %i", cc->pix_fmt );
314 if( GetVlcChroma( &fmt.video, cc->pix_fmt ) != VLC_SUCCESS)
316 msg_Err( p_demux, "was unable to find a FourCC match for raw video" );
319 fmt.i_codec = fmt.video.i_chroma;
321 /* We need this for the h264 packetizer */
322 else if( cc->codec_id == AV_CODEC_ID_H264 && ( p_sys->fmt == av_find_input_format("flv") ||
323 p_sys->fmt == av_find_input_format("matroska") || p_sys->fmt == av_find_input_format("mp4") ) )
324 fmt.i_original_fourcc = VLC_FOURCC( 'a', 'v', 'c', '1' );
326 fmt.video.i_width = cc->width;
327 fmt.video.i_height = cc->height;
328 #if LIBAVCODEC_VERSION_MAJOR < 54
331 fmt.video.p_palette = malloc( sizeof(video_palette_t) );
332 *fmt.video.p_palette = *(video_palette_t *)cc->palctrl;
335 # warning FIXME: implement palette transmission
338 fmt.video.i_frame_rate = cc->time_base.den;
339 fmt.video.i_frame_rate_base = cc->time_base.num * __MAX( cc->ticks_per_frame, 1 );
342 case AVMEDIA_TYPE_SUBTITLE:
343 es_format_Init( &fmt, SPU_ES, fcc );
344 if( strncmp( p_sys->ic->iformat->name, "matroska", 8 ) == 0 &&
345 cc->codec_id == AV_CODEC_ID_DVD_SUBTITLE &&
346 cc->extradata != NULL &&
347 cc->extradata_size > 0 )
350 char *psz_buf = malloc( cc->extradata_size + 1);
351 if( psz_buf != NULL )
353 memcpy( psz_buf, cc->extradata , cc->extradata_size );
354 psz_buf[cc->extradata_size] = '\0';
356 psz_start = strstr( psz_buf, "size:" );
358 vobsub_size_parse( psz_start,
359 &fmt.subs.spu.i_original_frame_width,
360 &fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
362 msg_Dbg( p_demux, "original frame size: %dx%d",
363 fmt.subs.spu.i_original_frame_width,
364 fmt.subs.spu.i_original_frame_height );
368 msg_Warn( p_demux, "reading original frame size failed" );
371 psz_start = strstr( psz_buf, "palette:" );
373 vobsub_palette_parse( psz_start, &fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
375 fmt.subs.spu.palette[0] = 0xBeef;
376 msg_Dbg( p_demux, "vobsub palette read" );
380 msg_Warn( p_demux, "reading original palette failed" );
386 psz_type = "subtitle";
390 es_format_Init( &fmt, UNKNOWN_ES, 0 );
391 #ifdef HAVE_AVUTIL_CODEC_ATTACHMENT
392 if( cc->codec_type == AVMEDIA_TYPE_ATTACHMENT )
394 input_attachment_t *p_attachment;
396 psz_type = "attachment";
397 if( cc->codec_id == AV_CODEC_ID_TTF )
399 AVDictionaryEntry *filename = av_dict_get( s->metadata, "filename", NULL, 0 );
400 if( filename && filename->value )
402 p_attachment = vlc_input_attachment_New(
403 filename->value, "application/x-truetype-font",
404 NULL, cc->extradata, (int)cc->extradata_size );
405 TAB_APPEND( p_sys->i_attachments, p_sys->attachments,
409 else msg_Warn( p_demux, "unsupported attachment type (%u) in avformat demux", cc->codec_id );
414 if( cc->codec_type == AVMEDIA_TYPE_DATA )
417 msg_Warn( p_demux, "unsupported track type (%u:%u) in avformat demux", cc->codec_type, cc->codec_id );
422 AVDictionaryEntry *language = av_dict_get( s->metadata, "language", NULL, 0 );
423 if ( language && language->value )
424 fmt.psz_language = strdup( language->value );
426 if( s->disposition & AV_DISPOSITION_DEFAULT )
427 fmt.i_priority = 1000;
429 #ifdef HAVE_AVUTIL_CODEC_ATTACHMENT
430 if( cc->codec_type != AVMEDIA_TYPE_ATTACHMENT )
432 if( cc->codec_type != AVMEDIA_TYPE_DATA )
434 const bool b_ogg = !strcmp( p_sys->fmt->name, "ogg" );
435 const uint8_t *p_extra = cc->extradata;
436 unsigned i_extra = cc->extradata_size;
438 if( cc->codec_id == AV_CODEC_ID_THEORA && b_ogg )
441 const void *pp_data[3];
443 for( i_count = 0; i_count < 3; i_count++ )
447 pi_size[i_count] = GetWBE( p_extra );
448 pp_data[i_count] = &p_extra[2];
449 if( i_extra < pi_size[i_count] + 2 )
452 p_extra += 2 + pi_size[i_count];
453 i_extra -= 2 + pi_size[i_count];
455 if( i_count > 0 && xiph_PackHeaders( &fmt.i_extra, &fmt.p_extra,
456 pi_size, pp_data, i_count ) )
462 else if( cc->codec_id == AV_CODEC_ID_SPEEX && b_ogg )
464 const uint8_t p_dummy_comment[] = {
469 const void *pp_data[2];
471 pi_size[0] = i_extra;
472 pp_data[0] = p_extra;
474 pi_size[1] = sizeof(p_dummy_comment);
475 pp_data[1] = p_dummy_comment;
477 if( pi_size[0] > 0 && xiph_PackHeaders( &fmt.i_extra, &fmt.p_extra,
478 pi_size, pp_data, 2 ) )
484 else if( cc->extradata_size > 0 )
486 fmt.p_extra = malloc( i_extra );
489 fmt.i_extra = i_extra;
490 memcpy( fmt.p_extra, p_extra, i_extra );
493 es = es_out_Add( p_demux->out, &fmt );
494 if( s->disposition & AV_DISPOSITION_DEFAULT )
495 es_out_Control( p_demux->out, ES_OUT_SET_ES_DEFAULT, es );
496 es_format_Clean( &fmt );
498 msg_Dbg( p_demux, "adding es: %s codec = %4.4s",
499 psz_type, (char*)&fcc );
500 TAB_APPEND( p_sys->i_tk, p_sys->tk, es );
503 p_sys->tk_pcr = calloc( p_sys->i_tk, sizeof(*p_sys->tk_pcr) );
505 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
506 i_start_time = p_sys->ic->start_time * 1000000 / AV_TIME_BASE;
508 msg_Dbg( p_demux, "AVFormat supported stream" );
509 msg_Dbg( p_demux, " - format = %s (%s)",
510 p_sys->fmt->name, p_sys->fmt->long_name );
511 msg_Dbg( p_demux, " - start time = %"PRId64, i_start_time );
512 msg_Dbg( p_demux, " - duration = %"PRId64,
513 ( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE ) ?
514 p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 );
516 if( p_sys->ic->nb_chapters > 0 )
518 p_sys->p_title = vlc_input_title_New();
519 p_sys->p_title->i_length = p_sys->ic->duration * 1000000 / AV_TIME_BASE;
522 for( i = 0; i < p_sys->ic->nb_chapters; i++ )
524 seekpoint_t *s = vlc_seekpoint_New();
526 AVDictionaryEntry *title = av_dict_get( p_sys->ic->metadata, "title", NULL, 0);
527 if( title && title->value )
529 s->psz_name = strdup( title->value );
530 EnsureUTF8( s->psz_name );
531 msg_Dbg( p_demux, " - chapter %d: %s", i, s->psz_name );
533 s->i_time_offset = p_sys->ic->chapters[i]->start * 1000000 *
534 p_sys->ic->chapters[i]->time_base.num /
535 p_sys->ic->chapters[i]->time_base.den -
536 (i_start_time != -1 ? i_start_time : 0 );
537 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
540 ResetTime( p_demux, 0 );
544 /*****************************************************************************
546 *****************************************************************************/
547 void CloseDemux( vlc_object_t *p_this )
549 demux_t *p_demux = (demux_t*)p_this;
550 demux_sys_t *p_sys = p_demux->p_sys;
552 FREENULL( p_sys->tk );
553 free( p_sys->tk_pcr );
557 av_free( p_sys->ic->pb );
558 #if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(26<<8)+0)
559 avformat_close_input( &p_sys->ic );
561 av_close_input_stream( p_sys->ic );
565 for( int i = 0; i < p_sys->i_attachments; i++ )
566 free( p_sys->attachments[i] );
567 TAB_CLEAN( p_sys->i_attachments, p_sys->attachments);
570 vlc_input_title_Delete( p_sys->p_title );
572 free( p_sys->io_buffer );
576 /*****************************************************************************
578 *****************************************************************************/
579 static int Demux( demux_t *p_demux )
581 demux_sys_t *p_sys = p_demux->p_sys;
584 int64_t i_start_time;
587 int i_av_ret = av_read_frame( p_sys->ic, &pkt );
590 /* Avoid EOF if av_read_frame returns AVERROR(EAGAIN) */
591 if( i_av_ret == AVERROR(EAGAIN) )
596 if( pkt.stream_index < 0 || pkt.stream_index >= p_sys->i_tk )
598 av_free_packet( &pkt );
601 const AVStream *p_stream = p_sys->ic->streams[pkt.stream_index];
602 if( p_stream->time_base.den <= 0 )
604 msg_Warn( p_demux, "Invalid time base for the stream %d", pkt.stream_index );
605 av_free_packet( &pkt );
608 if( p_stream->codec->codec_id == AV_CODEC_ID_SSA )
610 p_frame = BuildSsaFrame( &pkt, p_sys->i_ssa_order++ );
613 av_free_packet( &pkt );
619 if( ( p_frame = block_Alloc( pkt.size ) ) == NULL )
621 av_free_packet( &pkt );
624 memcpy( p_frame->p_buffer, pkt.data, pkt.size );
627 if( pkt.flags & AV_PKT_FLAG_KEY )
628 p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
630 /* Used to avoid timestamps overlow */
632 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
634 q = lldiv( p_sys->ic->start_time, AV_TIME_BASE);
635 i_start_time = q.quot * (int64_t)1000000 + q.rem * (int64_t)1000000 / AV_TIME_BASE;
640 if( pkt.dts == (int64_t)AV_NOPTS_VALUE )
641 p_frame->i_dts = VLC_TS_INVALID;
644 q = lldiv( pkt.dts, p_stream->time_base.den );
645 p_frame->i_dts = q.quot * (int64_t)1000000 *
646 p_stream->time_base.num + q.rem * (int64_t)1000000 *
647 p_stream->time_base.num /
648 p_stream->time_base.den - i_start_time + VLC_TS_0;
651 if( pkt.pts == (int64_t)AV_NOPTS_VALUE )
652 p_frame->i_pts = VLC_TS_INVALID;
655 q = lldiv( pkt.pts, p_stream->time_base.den );
656 p_frame->i_pts = q.quot * (int64_t)1000000 *
657 p_stream->time_base.num + q.rem * (int64_t)1000000 *
658 p_stream->time_base.num /
659 p_stream->time_base.den - i_start_time + VLC_TS_0;
661 if( pkt.duration > 0 && p_frame->i_length <= 0 )
662 p_frame->i_length = pkt.duration * 1000000 *
663 p_stream->time_base.num /
664 p_stream->time_base.den;
666 if( pkt.dts != (int64_t)AV_NOPTS_VALUE && pkt.dts == pkt.pts &&
667 p_stream->codec->codec_type == AVMEDIA_TYPE_VIDEO )
669 /* Add here notoriously bugged file formats/samples regarding PTS */
670 if( !strcmp( p_sys->fmt->name, "flv" ) )
671 p_frame->i_pts = VLC_TS_INVALID;
673 #ifdef AVFORMAT_DEBUG
674 msg_Dbg( p_demux, "tk[%d] dts=%"PRId64" pts=%"PRId64,
675 pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
677 if( p_frame->i_dts > VLC_TS_INVALID )
678 p_sys->tk_pcr[pkt.stream_index] = p_frame->i_dts;
680 int64_t i_ts_max = INT64_MIN;
681 for( int i = 0; i < p_sys->i_tk; i++ )
682 i_ts_max = __MAX( i_ts_max, p_sys->tk_pcr[i] );
684 int64_t i_ts_min = INT64_MAX;
685 for( int i = 0; i < p_sys->i_tk; i++ )
687 if( p_sys->tk_pcr[i] > VLC_TS_INVALID && p_sys->tk_pcr[i] + 10 * CLOCK_FREQ >= i_ts_max )
688 i_ts_min = __MIN( i_ts_min, p_sys->tk_pcr[i] );
690 if( i_ts_min >= p_sys->i_pcr )
692 p_sys->i_pcr = i_ts_min;
693 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
694 UpdateSeekPoint( p_demux, p_sys->i_pcr );
697 es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame );
699 av_free_packet( &pkt );
703 static void UpdateSeekPoint( demux_t *p_demux, int64_t i_time )
705 demux_sys_t *p_sys = p_demux->p_sys;
708 if( !p_sys->p_title )
711 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
713 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
718 if( i != p_demux->info.i_seekpoint && i >= 0 )
720 p_demux->info.i_seekpoint = i;
721 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
725 static void ResetTime( demux_t *p_demux, int64_t i_time )
727 demux_sys_t *p_sys = p_demux->p_sys;
729 if( p_sys->ic->start_time == (int64_t)AV_NOPTS_VALUE || i_time < 0 )
730 i_time = VLC_TS_INVALID;
731 else if( i_time == 0 )
734 p_sys->i_pcr = i_time;
735 for( int i = 0; i < p_sys->i_tk; i++ )
736 p_sys->tk_pcr[i] = i_time;
738 if( i_time > VLC_TS_INVALID )
740 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_time );
741 UpdateSeekPoint( p_demux, i_time );
745 static block_t *BuildSsaFrame( const AVPacket *p_pkt, unsigned i_order )
747 if( p_pkt->size <= 0 )
751 const size_t i_buffer_size = __MIN( (int)sizeof(buffer) - 1, p_pkt->size );
752 memcpy( buffer, p_pkt->data, i_buffer_size );
753 buffer[i_buffer_size] = '\0';
760 if( sscanf( buffer, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,%n", &i_layer,
761 &h0, &m0, &s0, &c0, &h1, &m1, &s1, &c1, &i_position ) < 9 )
763 if( i_position <= 0 || (unsigned)i_position >= i_buffer_size )
767 if( asprintf( &p, "%u,%d,%.*s", i_order, i_layer, p_pkt->size - i_position, p_pkt->data + i_position ) < 0 )
770 block_t *p_frame = block_heap_Alloc( p, strlen(p) + 1 );
772 p_frame->i_length = CLOCK_FREQ * ((h1-h0) * 3600 +
775 CLOCK_FREQ * (c1-c0) / 100;
779 /*****************************************************************************
781 *****************************************************************************/
782 static int Control( demux_t *p_demux, int i_query, va_list args )
784 demux_sys_t *p_sys = p_demux->p_sys;
785 const int64_t i_start_time = p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ? p_sys->ic->start_time : 0;
791 case DEMUX_GET_POSITION:
792 pf = (double*) va_arg( args, double* ); *pf = 0.0;
793 i64 = stream_Size( p_demux->s );
796 double current = stream_Tell( p_demux->s );
797 *pf = current / (double)i64;
800 if( (p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE) && (p_sys->i_pcr > 0) )
802 *pf = (double)p_sys->i_pcr / (double)p_sys->ic->duration;
807 case DEMUX_SET_POSITION:
808 f = (double) va_arg( args, double );
809 i64 = p_sys->ic->duration * f + i_start_time;
811 msg_Warn( p_demux, "DEMUX_SET_POSITION: %"PRId64, i64 );
813 /* If we have a duration, we prefer to seek by time
814 but if we don't, or if the seek fails, try BYTE seeking */
815 if( p_sys->ic->duration == (int64_t)AV_NOPTS_VALUE ||
816 (av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BACKWARD ) < 0) )
818 int64_t i_size = stream_Size( p_demux->s );
821 msg_Warn( p_demux, "DEMUX_SET_BYTE_POSITION: %"PRId64, i64 );
822 if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BYTE ) < 0 )
825 ResetTime( p_demux, -1 );
829 ResetTime( p_demux, i64 - i_start_time );
833 case DEMUX_GET_LENGTH:
834 pi64 = (int64_t*)va_arg( args, int64_t * );
835 if( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE )
836 *pi64 = p_sys->ic->duration * 1000000 / AV_TIME_BASE;
842 pi64 = (int64_t*)va_arg( args, int64_t * );
843 *pi64 = p_sys->i_pcr;
848 i64 = (int64_t)va_arg( args, int64_t );
849 i64 = i64 *AV_TIME_BASE / 1000000 + i_start_time;
851 msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
853 if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BACKWARD ) < 0 )
857 ResetTime( p_demux, i64 - i_start_time );
861 case DEMUX_HAS_UNSUPPORTED_META:
863 bool *pb_bool = (bool*)va_arg( args, bool* );
871 static const char names[][10] = {
872 [vlc_meta_Title] = "title",
873 [vlc_meta_Artist] = "artist",
874 [vlc_meta_Genre] = "genre",
875 [vlc_meta_Copyright] = "copyright",
876 [vlc_meta_Album] = "album",
877 //[vlc_meta_TrackNumber] -- TODO: parse number/total value
878 [vlc_meta_Description] = "comment",
880 [vlc_meta_Date] = "date",
881 [vlc_meta_Setting] = "encoder",
883 [vlc_meta_Language] = "language",
884 //[vlc_meta_NowPlaying]
885 [vlc_meta_Publisher] = "publisher",
886 [vlc_meta_EncodedBy] = "encoded_by",
887 //[vlc_meta_ArtworkURL]
889 //[vlc_meta_TrackTotal]
891 vlc_meta_t *p_meta = va_arg( args, vlc_meta_t * );
892 AVDictionary *dict = p_sys->ic->metadata;
894 for( unsigned i = 0; i < sizeof(names) / sizeof(*names); i++)
899 AVDictionaryEntry *e = av_dict_get( dict, names[i], NULL, 0 );
900 if( e != NULL && e->value != NULL && IsUTF8(e->value) )
901 vlc_meta_Set( p_meta, i, e->value );
906 case DEMUX_GET_ATTACHMENTS:
908 input_attachment_t ***ppp_attach =
909 (input_attachment_t***)va_arg( args, input_attachment_t*** );
910 int *pi_int = (int*)va_arg( args, int * );
913 if( p_sys->i_attachments <= 0 )
916 *pi_int = p_sys->i_attachments;;
917 *ppp_attach = malloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
918 for( i = 0; i < p_sys->i_attachments; i++ )
919 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
923 case DEMUX_GET_TITLE_INFO:
925 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
926 int *pi_int = (int*)va_arg( args, int* );
927 int *pi_title_offset = (int*)va_arg( args, int* );
928 int *pi_seekpoint_offset = (int*)va_arg( args, int* );
930 if( !p_sys->p_title )
934 *ppp_title = malloc( sizeof( input_title_t**) );
935 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
936 *pi_title_offset = 0;
937 *pi_seekpoint_offset = 0;
940 case DEMUX_SET_TITLE:
942 const int i_title = (int)va_arg( args, int );
943 if( !p_sys->p_title || i_title != 0 )
947 case DEMUX_SET_SEEKPOINT:
949 const int i_seekpoint = (int)va_arg( args, int );
950 if( !p_sys->p_title )
953 i64 = p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset *
954 AV_TIME_BASE / 1000000 + i_start_time;
956 msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
958 if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BACKWARD ) < 0 )
962 ResetTime( p_demux, i64 - i_start_time );
972 /*****************************************************************************
973 * I/O wrappers for libavformat
974 *****************************************************************************/
975 static int IORead( void *opaque, uint8_t *buf, int buf_size )
977 demux_t *p_demux = opaque;
978 if( buf_size < 0 ) return -1;
979 int i_ret = stream_Read( p_demux->s, buf, buf_size );
980 return i_ret ? i_ret : -1;
983 static int64_t IOSeek( void *opaque, int64_t offset, int whence )
985 demux_t *p_demux = opaque;
987 int64_t i_size = stream_Size( p_demux->s );
989 #ifdef AVFORMAT_DEBUG
990 msg_Warn( p_demux, "IOSeek offset: %"PRId64", whence: %i", offset, whence );
1000 i_absolute = (int64_t)offset;
1003 i_absolute = stream_Tell( p_demux->s ) + (int64_t)offset;
1006 i_absolute = i_size + (int64_t)offset;
1013 if( i_absolute < 0 )
1015 msg_Dbg( p_demux, "Trying to seek before the beginning" );
1019 if( i_size > 0 && i_absolute >= i_size )
1021 msg_Dbg( p_demux, "Trying to seek too far : EOF?" );
1025 if( stream_Seek( p_demux->s, i_absolute ) )
1027 msg_Warn( p_demux, "we were not allowed to seek, or EOF " );
1031 return stream_Tell( p_demux->s );
1034 #endif /* HAVE_LIBAVFORMAT_AVFORMAT_H */