1 /*****************************************************************************
2 * demux.c: demuxer using 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>
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 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(50<<8)+0) )
63 # define HAVE_AVUTIL_CODEC_ATTACHMENT 1
66 /*****************************************************************************
67 * demux_sys_t: demux descriptor
68 *****************************************************************************/
71 #if LIBAVFORMAT_VERSION_INT < ((53<<16)+(2<<8)+0)
89 input_attachment_t **attachments;
91 /* Only one title with seekpoints possible atm. */
92 input_title_t *p_title;
95 /*****************************************************************************
97 *****************************************************************************/
98 static int Demux ( demux_t *p_demux );
99 static int Control( demux_t *p_demux, int i_query, va_list args );
101 static int IORead( void *opaque, uint8_t *buf, int buf_size );
102 static int64_t IOSeek( void *opaque, int64_t offset, int whence );
104 static block_t *BuildSsaFrame( const AVPacket *p_pkt, unsigned i_order );
105 static void UpdateSeekPoint( demux_t *p_demux, int64_t i_time );
106 static void ResetTime( 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;
116 AVInputFormat *fmt = NULL;
118 int64_t i_start_time = -1;
123 if( p_demux->psz_file )
124 psz_url = strdup( p_demux->psz_file );
127 if( asprintf( &psz_url, "%s://%s", p_demux->psz_access, p_demux->psz_location ) == -1)
130 msg_Dbg( p_demux, "trying url: %s", psz_url );
131 /* Init Probe data */
132 pd.filename = psz_url;
133 if( ( pd.buf_size = stream_Peek( p_demux->s, (const uint8_t**)&pd.buf, 2048 + 213 ) ) <= 0 )
136 msg_Warn( p_demux, "cannot peek" );
139 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_can_seek );
143 char *psz_format = var_InheritString( p_this, "avformat-format" );
146 if( (fmt = av_find_input_format(psz_format)) )
147 msg_Dbg( p_demux, "forcing format: %s", fmt->name );
152 if( !fmt && !( fmt = av_probe_input_format( &pd, 1 ) ) )
154 msg_Dbg( p_demux, "couldn't guess format" );
159 if( !p_demux->b_force )
161 static const char ppsz_blacklist[][16] = {
162 /* Don't handle MPEG unless forced */
163 "mpeg", "vcd", "vob", "mpegts",
164 /* libavformat's redirector won't work */
166 /* Don't handle subtitles format */
167 "ass", "srt", "microdvd",
171 for( int i = 0; *ppsz_blacklist[i]; i++ )
173 if( !strcmp( fmt->name, ppsz_blacklist[i] ) )
181 /* Don't trigger false alarms on bin files */
182 if( !p_demux->b_force && !strcmp( fmt->name, "psxstr" ) )
186 if( !p_demux->psz_file )
192 i_len = strlen( p_demux->psz_file );
199 if( strcasecmp( &p_demux->psz_file[i_len - 4], ".str" ) &&
200 strcasecmp( &p_demux->psz_file[i_len - 4], ".xai" ) &&
201 strcasecmp( &p_demux->psz_file[i_len - 3], ".xa" ) )
208 msg_Dbg( p_demux, "detected format: %s", fmt->name );
210 /* Fill p_demux fields */
211 p_demux->pf_demux = Demux;
212 p_demux->pf_control = Control;
213 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
218 p_sys->tk_pcr = NULL;
219 p_sys->i_ssa_order = 0;
220 TAB_INIT( p_sys->i_attachments, p_sys->attachments);
221 p_sys->p_title = NULL;
223 /* Create I/O wrapper */
224 p_sys->io_buffer_size = 32768; /* FIXME */
225 p_sys->io_buffer = malloc( p_sys->io_buffer_size );
227 #if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(2<<8)+0)
228 p_sys->ic = avformat_alloc_context();
229 p_sys->ic->pb = avio_alloc_context( p_sys->io_buffer,
230 p_sys->io_buffer_size, 0, p_demux, IORead, NULL, IOSeek );
231 p_sys->ic->pb->seekable = b_can_seek ? AVIO_SEEKABLE_NORMAL : 0;
232 error = avformat_open_input(&p_sys->ic, psz_url, p_sys->fmt, NULL);
234 init_put_byte( &p_sys->io, p_sys->io_buffer, p_sys->io_buffer_size, 0,
235 p_demux, IORead, NULL, IOSeek );
236 p_sys->io.is_streamed = !b_can_seek;
237 # if defined(AVIO_SEEKABLE_NORMAL)
238 p_sys->io.seekable = !!b_can_seek;
240 error = av_open_input_stream(&p_sys->ic, &p_sys->io, psz_url, p_sys->fmt, NULL);
246 errno = AVUNERROR(error);
247 msg_Err( p_demux, "Could not open %s: %m", psz_url );
249 CloseDemux( p_this );
253 #if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(26<<8)+0)
254 char *psz_opts = var_InheritString( p_demux, "avformat-options" );
255 AVDictionary *options[p_sys->ic->nb_streams];
256 for (unsigned i = 0; i < p_sys->ic->nb_streams; i++)
258 if (psz_opts && *psz_opts) {
259 options[0] = vlc_av_get_options(psz_opts);
260 for (unsigned i = 1; i < p_sys->ic->nb_streams; i++) {
261 av_dict_copy(&options[i], options[0], 0);
265 vlc_avcodec_lock(); /* avformat calls avcodec behind our back!!! */
266 error = avformat_find_stream_info( p_sys->ic, options );
267 vlc_avcodec_unlock();
268 AVDictionaryEntry *t = NULL;
269 while ((t = av_dict_get(options[0], "", t, AV_DICT_IGNORE_SUFFIX))) {
270 msg_Err( p_demux, "Unknown option \"%s\"", t->key );
272 for (unsigned i = 0; i < p_sys->ic->nb_streams; i++) {
273 av_dict_free(&options[i]);
276 vlc_avcodec_lock(); /* avformat calls avcodec behind our back!!! */
277 error = av_find_stream_info( p_sys->ic );
278 vlc_avcodec_unlock();
283 errno = AVUNERROR(error);
284 msg_Warn( p_demux, "Could not find stream info: %m" );
287 for( i = 0; i < p_sys->ic->nb_streams; i++ )
289 AVStream *s = p_sys->ic->streams[i];
290 AVCodecContext *cc = s->codec;
295 const char *psz_type = "unknown";
297 if( !GetVlcFourcc( cc->codec_id, NULL, &fcc, NULL ) )
298 fcc = VLC_FOURCC( 'u', 'n', 'd', 'f' );
300 switch( cc->codec_type )
302 case AVMEDIA_TYPE_AUDIO:
303 es_format_Init( &fmt, AUDIO_ES, fcc );
304 fmt.i_bitrate = cc->bit_rate;
305 fmt.audio.i_channels = cc->channels;
306 fmt.audio.i_rate = cc->sample_rate;
307 fmt.audio.i_bitspersample = cc->bits_per_coded_sample;
308 fmt.audio.i_blockalign = cc->block_align;
312 case AVMEDIA_TYPE_VIDEO:
313 es_format_Init( &fmt, VIDEO_ES, fcc );
315 /* Special case for raw video data */
316 if( cc->codec_id == CODEC_ID_RAWVIDEO )
318 msg_Dbg( p_demux, "raw video, pixel format: %i", cc->pix_fmt );
319 if( GetVlcChroma( &fmt.video, cc->pix_fmt ) != VLC_SUCCESS)
321 msg_Err( p_demux, "was unable to find a FourCC match for raw video" );
324 fmt.i_codec = fmt.video.i_chroma;
326 /* We need this for the h264 packetizer */
327 else if( cc->codec_id == CODEC_ID_H264 && ( p_sys->fmt == av_find_input_format("flv") ||
328 p_sys->fmt == av_find_input_format("matroska") || p_sys->fmt == av_find_input_format("mp4") ) )
329 fmt.i_original_fourcc = VLC_FOURCC( 'a', 'v', 'c', '1' );
331 fmt.video.i_width = cc->width;
332 fmt.video.i_height = cc->height;
333 #if LIBAVCODEC_VERSION_MAJOR < 54
336 fmt.video.p_palette = malloc( sizeof(video_palette_t) );
337 *fmt.video.p_palette = *(video_palette_t *)cc->palctrl;
340 # warning FIXME: implement palette transmission
343 fmt.video.i_frame_rate = cc->time_base.den;
344 fmt.video.i_frame_rate_base = cc->time_base.num * __MAX( cc->ticks_per_frame, 1 );
347 case AVMEDIA_TYPE_SUBTITLE:
348 es_format_Init( &fmt, SPU_ES, fcc );
349 if( strncmp( p_sys->ic->iformat->name, "matroska", 8 ) == 0 &&
350 cc->codec_id == CODEC_ID_DVD_SUBTITLE &&
351 cc->extradata != NULL &&
352 cc->extradata_size > 0 )
355 char *psz_buf = malloc( cc->extradata_size + 1);
356 if( psz_buf != NULL )
358 memcpy( psz_buf, cc->extradata , cc->extradata_size );
359 psz_buf[cc->extradata_size] = '\0';
361 psz_start = strstr( psz_buf, "size:" );
363 vobsub_size_parse( psz_start,
364 &fmt.subs.spu.i_original_frame_width,
365 &fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
367 msg_Dbg( p_demux, "original frame size: %dx%d",
368 fmt.subs.spu.i_original_frame_width,
369 fmt.subs.spu.i_original_frame_height );
373 msg_Warn( p_demux, "reading original frame size failed" );
376 psz_start = strstr( psz_buf, "palette:" );
378 vobsub_palette_parse( psz_start, &fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
380 fmt.subs.spu.palette[0] = 0xBeef;
381 msg_Dbg( p_demux, "vobsub palette read" );
385 msg_Warn( p_demux, "reading original palette failed" );
391 psz_type = "subtitle";
395 es_format_Init( &fmt, UNKNOWN_ES, 0 );
396 #ifdef HAVE_AVUTIL_CODEC_ATTACHMENT
397 if( cc->codec_type == AVMEDIA_TYPE_ATTACHMENT )
399 input_attachment_t *p_attachment;
401 psz_type = "attachment";
402 if( cc->codec_id == CODEC_ID_TTF )
404 AVDictionaryEntry *filename = av_dict_get( s->metadata, "filename", NULL, 0 );
405 if( filename && filename->value )
407 p_attachment = vlc_input_attachment_New(
408 filename->value, "application/x-truetype-font",
409 NULL, cc->extradata, (int)cc->extradata_size );
410 TAB_APPEND( p_sys->i_attachments, p_sys->attachments,
414 else msg_Warn( p_demux, "unsupported attachment type (%u) in avformat demux", cc->codec_id );
419 if( cc->codec_type == AVMEDIA_TYPE_DATA )
422 msg_Warn( p_demux, "unsupported track type (%u:%u) in avformat demux", cc->codec_type, cc->codec_id );
427 AVDictionaryEntry *language = av_dict_get( s->metadata, "language", NULL, 0 );
428 if ( language && language->value )
429 fmt.psz_language = strdup( language->value );
431 if( s->disposition & AV_DISPOSITION_DEFAULT )
432 fmt.i_priority = 1000;
434 #ifdef HAVE_AVUTIL_CODEC_ATTACHMENT
435 if( cc->codec_type != AVMEDIA_TYPE_ATTACHMENT )
437 if( cc->codec_type != AVMEDIA_TYPE_DATA )
439 const bool b_ogg = !strcmp( p_sys->fmt->name, "ogg" );
440 const uint8_t *p_extra = cc->extradata;
441 unsigned i_extra = cc->extradata_size;
443 if( cc->codec_id == CODEC_ID_THEORA && b_ogg )
446 const void *pp_data[3];
448 for( i_count = 0; i_count < 3; i_count++ )
452 pi_size[i_count] = GetWBE( p_extra );
453 pp_data[i_count] = &p_extra[2];
454 if( i_extra < pi_size[i_count] + 2 )
457 p_extra += 2 + pi_size[i_count];
458 i_extra -= 2 + pi_size[i_count];
460 if( i_count > 0 && xiph_PackHeaders( &fmt.i_extra, &fmt.p_extra,
461 pi_size, pp_data, i_count ) )
467 else if( cc->codec_id == CODEC_ID_SPEEX && b_ogg )
469 const uint8_t p_dummy_comment[] = {
474 const void *pp_data[2];
476 pi_size[0] = i_extra;
477 pp_data[0] = p_extra;
479 pi_size[1] = sizeof(p_dummy_comment);
480 pp_data[1] = p_dummy_comment;
482 if( pi_size[0] > 0 && xiph_PackHeaders( &fmt.i_extra, &fmt.p_extra,
483 pi_size, pp_data, 2 ) )
489 else if( cc->extradata_size > 0 )
491 fmt.p_extra = malloc( i_extra );
494 fmt.i_extra = i_extra;
495 memcpy( fmt.p_extra, p_extra, i_extra );
498 es = es_out_Add( p_demux->out, &fmt );
499 if( s->disposition & AV_DISPOSITION_DEFAULT )
500 es_out_Control( p_demux->out, ES_OUT_SET_ES_DEFAULT, es );
501 es_format_Clean( &fmt );
503 msg_Dbg( p_demux, "adding es: %s codec = %4.4s",
504 psz_type, (char*)&fcc );
505 TAB_APPEND( p_sys->i_tk, p_sys->tk, es );
508 p_sys->tk_pcr = calloc( p_sys->i_tk, sizeof(*p_sys->tk_pcr) );
510 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
511 i_start_time = p_sys->ic->start_time * 1000000 / AV_TIME_BASE;
513 msg_Dbg( p_demux, "AVFormat supported stream" );
514 msg_Dbg( p_demux, " - format = %s (%s)",
515 p_sys->fmt->name, p_sys->fmt->long_name );
516 msg_Dbg( p_demux, " - start time = %"PRId64, i_start_time );
517 msg_Dbg( p_demux, " - duration = %"PRId64,
518 ( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE ) ?
519 p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 );
521 if( p_sys->ic->nb_chapters > 0 )
523 p_sys->p_title = vlc_input_title_New();
524 p_sys->p_title->i_length = p_sys->ic->duration * 1000000 / AV_TIME_BASE;
527 for( i = 0; i < p_sys->ic->nb_chapters; i++ )
529 seekpoint_t *s = vlc_seekpoint_New();
531 AVDictionaryEntry *title = av_dict_get( p_sys->ic->metadata, "title", NULL, 0);
532 if( title && title->value )
534 s->psz_name = strdup( title->value );
535 EnsureUTF8( s->psz_name );
536 msg_Dbg( p_demux, " - chapter %d: %s", i, s->psz_name );
538 s->i_time_offset = p_sys->ic->chapters[i]->start * 1000000 *
539 p_sys->ic->chapters[i]->time_base.num /
540 p_sys->ic->chapters[i]->time_base.den -
541 (i_start_time != -1 ? i_start_time : 0 );
542 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
545 ResetTime( p_demux, 0 );
549 /*****************************************************************************
551 *****************************************************************************/
552 void CloseDemux( vlc_object_t *p_this )
554 demux_t *p_demux = (demux_t*)p_this;
555 demux_sys_t *p_sys = p_demux->p_sys;
557 FREENULL( p_sys->tk );
558 free( p_sys->tk_pcr );
562 #if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(2<<8)+0)
563 av_free( p_sys->ic->pb );
565 #if LIBAVFORMAT_VERSION_INT >= ((53<<16)+(26<<8)+0)
566 avformat_close_input( &p_sys->ic );
568 av_close_input_stream( p_sys->ic );
572 for( int i = 0; i < p_sys->i_attachments; i++ )
573 free( p_sys->attachments[i] );
574 TAB_CLEAN( p_sys->i_attachments, p_sys->attachments);
577 vlc_input_title_Delete( p_sys->p_title );
579 free( p_sys->io_buffer );
583 /*****************************************************************************
585 *****************************************************************************/
586 static int Demux( demux_t *p_demux )
588 demux_sys_t *p_sys = p_demux->p_sys;
591 int64_t i_start_time;
594 int i_av_ret = av_read_frame( p_sys->ic, &pkt );
597 /* Avoid EOF if av_read_frame returns AVERROR(EAGAIN) */
598 if( i_av_ret == AVERROR(EAGAIN) )
603 if( pkt.stream_index < 0 || pkt.stream_index >= p_sys->i_tk )
605 av_free_packet( &pkt );
608 const AVStream *p_stream = p_sys->ic->streams[pkt.stream_index];
609 if( p_stream->time_base.den <= 0 )
611 msg_Warn( p_demux, "Invalid time base for the stream %d", pkt.stream_index );
612 av_free_packet( &pkt );
615 if( p_stream->codec->codec_id == CODEC_ID_SSA )
617 p_frame = BuildSsaFrame( &pkt, p_sys->i_ssa_order++ );
620 av_free_packet( &pkt );
626 if( ( p_frame = block_New( p_demux, pkt.size ) ) == NULL )
628 av_free_packet( &pkt );
631 memcpy( p_frame->p_buffer, pkt.data, pkt.size );
634 if( pkt.flags & AV_PKT_FLAG_KEY )
635 p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
637 /* Used to avoid timestamps overlow */
639 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
641 q = lldiv( p_sys->ic->start_time, AV_TIME_BASE);
642 i_start_time = q.quot * (int64_t)1000000 + q.rem * (int64_t)1000000 / AV_TIME_BASE;
647 if( pkt.dts == (int64_t)AV_NOPTS_VALUE )
648 p_frame->i_dts = VLC_TS_INVALID;
651 q = lldiv( pkt.dts, p_stream->time_base.den );
652 p_frame->i_dts = q.quot * (int64_t)1000000 *
653 p_stream->time_base.num + q.rem * (int64_t)1000000 *
654 p_stream->time_base.num /
655 p_stream->time_base.den - i_start_time + VLC_TS_0;
658 if( pkt.pts == (int64_t)AV_NOPTS_VALUE )
659 p_frame->i_pts = VLC_TS_INVALID;
662 q = lldiv( pkt.pts, p_stream->time_base.den );
663 p_frame->i_pts = q.quot * (int64_t)1000000 *
664 p_stream->time_base.num + q.rem * (int64_t)1000000 *
665 p_stream->time_base.num /
666 p_stream->time_base.den - i_start_time + VLC_TS_0;
668 if( pkt.duration > 0 && p_frame->i_length <= 0 )
669 p_frame->i_length = pkt.duration * 1000000 *
670 p_stream->time_base.num /
671 p_stream->time_base.den;
673 if( pkt.dts != (int64_t)AV_NOPTS_VALUE && pkt.dts == pkt.pts &&
674 p_stream->codec->codec_type == AVMEDIA_TYPE_VIDEO )
676 /* Add here notoriously bugged file formats/samples regarding PTS */
677 if( !strcmp( p_sys->fmt->name, "flv" ) )
678 p_frame->i_pts = VLC_TS_INVALID;
680 #ifdef AVFORMAT_DEBUG
681 msg_Dbg( p_demux, "tk[%d] dts=%"PRId64" pts=%"PRId64,
682 pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
684 if( p_frame->i_dts > VLC_TS_INVALID )
685 p_sys->tk_pcr[pkt.stream_index] = p_frame->i_dts;
687 int64_t i_ts_max = INT64_MIN;
688 for( int i = 0; i < p_sys->i_tk; i++ )
689 i_ts_max = __MAX( i_ts_max, p_sys->tk_pcr[i] );
691 int64_t i_ts_min = INT64_MAX;
692 for( int i = 0; i < p_sys->i_tk; i++ )
694 if( p_sys->tk_pcr[i] > VLC_TS_INVALID && p_sys->tk_pcr[i] + 10 * CLOCK_FREQ >= i_ts_max )
695 i_ts_min = __MIN( i_ts_min, p_sys->tk_pcr[i] );
697 if( i_ts_min >= p_sys->i_pcr )
699 p_sys->i_pcr = i_ts_min;
700 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
701 UpdateSeekPoint( p_demux, p_sys->i_pcr );
704 es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame );
706 av_free_packet( &pkt );
710 static void UpdateSeekPoint( demux_t *p_demux, int64_t i_time )
712 demux_sys_t *p_sys = p_demux->p_sys;
715 if( !p_sys->p_title )
718 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
720 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
725 if( i != p_demux->info.i_seekpoint && i >= 0 )
727 p_demux->info.i_seekpoint = i;
728 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
732 static void ResetTime( demux_t *p_demux, int64_t i_time )
734 demux_sys_t *p_sys = p_demux->p_sys;
736 if( p_sys->ic->start_time == (int64_t)AV_NOPTS_VALUE || i_time < 0 )
737 i_time = VLC_TS_INVALID;
738 else if( i_time == 0 )
741 p_sys->i_pcr = i_time;
742 for( int i = 0; i < p_sys->i_tk; i++ )
743 p_sys->tk_pcr[i] = i_time;
745 if( i_time > VLC_TS_INVALID )
747 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_time );
748 UpdateSeekPoint( p_demux, i_time );
752 static block_t *BuildSsaFrame( const AVPacket *p_pkt, unsigned i_order )
754 if( p_pkt->size <= 0 )
758 const size_t i_buffer_size = __MIN( (int)sizeof(buffer) - 1, p_pkt->size );
759 memcpy( buffer, p_pkt->data, i_buffer_size );
760 buffer[i_buffer_size] = '\0';
767 if( sscanf( buffer, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,%n", &i_layer,
768 &h0, &m0, &s0, &c0, &h1, &m1, &s1, &c1, &i_position ) < 9 )
770 if( i_position <= 0 || (unsigned)i_position >= i_buffer_size )
774 if( asprintf( &p, "%u,%d,%.*s", i_order, i_layer, p_pkt->size - i_position, p_pkt->data + i_position ) < 0 )
777 block_t *p_frame = block_heap_Alloc( p, strlen(p) + 1 );
779 p_frame->i_length = CLOCK_FREQ * ((h1-h0) * 3600 +
782 CLOCK_FREQ * (c1-c0) / 100;
786 /*****************************************************************************
788 *****************************************************************************/
789 static int Control( demux_t *p_demux, int i_query, va_list args )
791 demux_sys_t *p_sys = p_demux->p_sys;
792 const int64_t i_start_time = p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ? p_sys->ic->start_time : 0;
798 case DEMUX_GET_POSITION:
799 pf = (double*) va_arg( args, double* ); *pf = 0.0;
800 i64 = stream_Size( p_demux->s );
803 double current = stream_Tell( p_demux->s );
804 *pf = current / (double)i64;
807 if( (p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE) && (p_sys->i_pcr > 0) )
809 *pf = (double)p_sys->i_pcr / (double)p_sys->ic->duration;
814 case DEMUX_SET_POSITION:
815 f = (double) va_arg( args, double );
816 i64 = p_sys->ic->duration * f + i_start_time;
818 msg_Warn( p_demux, "DEMUX_SET_POSITION: %"PRId64, i64 );
820 /* If we have a duration, we prefer to seek by time
821 but if we don't, or if the seek fails, try BYTE seeking */
822 if( p_sys->ic->duration == (int64_t)AV_NOPTS_VALUE ||
823 (av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BACKWARD ) < 0) )
825 int64_t i_size = stream_Size( p_demux->s );
828 msg_Warn( p_demux, "DEMUX_SET_BYTE_POSITION: %"PRId64, i64 );
829 if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BYTE ) < 0 )
832 ResetTime( p_demux, -1 );
836 ResetTime( p_demux, i64 - i_start_time );
840 case DEMUX_GET_LENGTH:
841 pi64 = (int64_t*)va_arg( args, int64_t * );
842 if( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE )
843 *pi64 = p_sys->ic->duration * 1000000 / AV_TIME_BASE;
849 pi64 = (int64_t*)va_arg( args, int64_t * );
850 *pi64 = p_sys->i_pcr;
855 i64 = (int64_t)va_arg( args, int64_t );
856 i64 = i64 *AV_TIME_BASE / 1000000 + i_start_time;
858 msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
860 if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BACKWARD ) < 0 )
864 ResetTime( p_demux, i64 - i_start_time );
868 case DEMUX_HAS_UNSUPPORTED_META:
870 bool *pb_bool = (bool*)va_arg( args, bool* );
878 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
880 AVDictionaryEntry *title = av_dict_get( p_sys->ic->metadata, "language", NULL, 0 );
881 AVDictionaryEntry *artist = av_dict_get( p_sys->ic->metadata, "artist", NULL, 0 );
882 AVDictionaryEntry *copyright = av_dict_get( p_sys->ic->metadata, "copyright", NULL, 0 );
883 AVDictionaryEntry *comment = av_dict_get( p_sys->ic->metadata, "comment", NULL, 0 );
884 AVDictionaryEntry *genre = av_dict_get( p_sys->ic->metadata, "genre", NULL, 0 );
886 if( title && title->value )
887 vlc_meta_SetTitle( p_meta, title->value );
888 if( artist && artist->value )
889 vlc_meta_SetArtist( p_meta, artist->value );
890 if( copyright && copyright->value )
891 vlc_meta_SetCopyright( p_meta, copyright->value );
892 if( comment && comment->value )
893 vlc_meta_SetDescription( p_meta, comment->value );
894 if( genre && genre->value )
895 vlc_meta_SetGenre( p_meta, genre->value );
899 case DEMUX_GET_ATTACHMENTS:
901 input_attachment_t ***ppp_attach =
902 (input_attachment_t***)va_arg( args, input_attachment_t*** );
903 int *pi_int = (int*)va_arg( args, int * );
906 if( p_sys->i_attachments <= 0 )
909 *pi_int = p_sys->i_attachments;;
910 *ppp_attach = malloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
911 for( i = 0; i < p_sys->i_attachments; i++ )
912 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
916 case DEMUX_GET_TITLE_INFO:
918 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
919 int *pi_int = (int*)va_arg( args, int* );
920 int *pi_title_offset = (int*)va_arg( args, int* );
921 int *pi_seekpoint_offset = (int*)va_arg( args, int* );
923 if( !p_sys->p_title )
927 *ppp_title = malloc( sizeof( input_title_t**) );
928 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
929 *pi_title_offset = 0;
930 *pi_seekpoint_offset = 0;
933 case DEMUX_SET_TITLE:
935 const int i_title = (int)va_arg( args, int );
936 if( !p_sys->p_title || i_title != 0 )
940 case DEMUX_SET_SEEKPOINT:
942 const int i_seekpoint = (int)va_arg( args, int );
943 if( !p_sys->p_title )
946 i64 = p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset *
947 AV_TIME_BASE / 1000000 + i_start_time;
949 msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
951 if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BACKWARD ) < 0 )
955 ResetTime( p_demux, i64 - i_start_time );
965 /*****************************************************************************
966 * I/O wrappers for libavformat
967 *****************************************************************************/
968 static int IORead( void *opaque, uint8_t *buf, int buf_size )
970 demux_t *p_demux = opaque;
971 if( buf_size < 0 ) return -1;
972 int i_ret = stream_Read( p_demux->s, buf, buf_size );
973 return i_ret ? i_ret : -1;
976 static int64_t IOSeek( void *opaque, int64_t offset, int whence )
978 demux_t *p_demux = opaque;
980 int64_t i_size = stream_Size( p_demux->s );
982 #ifdef AVFORMAT_DEBUG
983 msg_Warn( p_demux, "IOSeek offset: %"PRId64", whence: %i", offset, whence );
993 i_absolute = (int64_t)offset;
996 i_absolute = stream_Tell( p_demux->s ) + (int64_t)offset;
999 i_absolute = i_size + (int64_t)offset;
1006 if( i_absolute < 0 )
1008 msg_Dbg( p_demux, "Trying to seek before the beginning" );
1012 if( i_size > 0 && i_absolute >= i_size )
1014 msg_Dbg( p_demux, "Trying to seek too far : EOF?" );
1018 if( stream_Seek( p_demux->s, i_absolute ) )
1020 msg_Warn( p_demux, "we were not allowed to seek, or EOF " );
1024 return stream_Tell( p_demux->s );
1027 #endif /* HAVE_LIBAVFORMAT_AVFORMAT_H */