/*****************************************************************************
* demux.c: demuxer using ffmpeg (libavformat).
*****************************************************************************
- * Copyright (C) 2004 the VideoLAN team
+ * Copyright (C) 2004-2007 the VideoLAN team
* $Id$
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
/*****************************************************************************
* Preamble
*****************************************************************************/
-#include <stdlib.h> /* malloc(), free() */
-#include <vlc/vlc.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
#include <vlc_demux.h>
#include <vlc_stream.h>
-#include "vlc_meta.h"
+#include <vlc_meta.h>
/* ffmpeg header */
-#ifdef HAVE_FFMPEG_AVFORMAT_H
+#if defined(HAVE_LIBAVFORMAT_AVFORMAT_H)
+# include <libavformat/avformat.h>
+#elif defined(HAVE_FFMPEG_AVFORMAT_H)
# include <ffmpeg/avformat.h>
-#elif defined(HAVE_LIBAVFORMAT_TREE)
-# include <avformat.h>
#endif
-#include "ffmpeg.h"
+#include "fourcc.h"
+#include "chroma.h"
//#define AVFORMAT_DEBUG 1
/* Version checking */
-#if defined(HAVE_FFMPEG_AVFORMAT_H) || defined(HAVE_LIBAVFORMAT_TREE)
+#if defined(HAVE_FFMPEG_AVFORMAT_H) || defined(HAVE_LIBAVFORMAT_AVFORMAT_H)
/*****************************************************************************
* demux_sys_t: demux descriptor
/*****************************************************************************
* Open
*****************************************************************************/
-int E_(OpenDemux)( vlc_object_t *p_this )
+int OpenDemux( vlc_object_t *p_this )
{
demux_t *p_demux = (demux_t*)p_this;
demux_sys_t *p_sys;
AVProbeData pd;
AVInputFormat *fmt;
- int i;
+ unsigned int i;
+ bool b_avfmt_nofile;
/* Init Probe data */
pd.filename = p_demux->psz_path;
init_put_byte( &p_sys->io, p_sys->io_buffer, p_sys->io_buffer_size,
0, &p_sys->url, IORead, NULL, IOSeek );
+ b_avfmt_nofile = p_sys->fmt->flags & AVFMT_NOFILE;
p_sys->fmt->flags |= AVFMT_NOFILE; /* libavformat must not fopen/fclose */
/* Open it */
p_sys->fmt, NULL ) )
{
msg_Err( p_demux, "av_open_input_stream failed" );
- E_(CloseDemux)( p_this );
+ if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
+ CloseDemux( p_this );
return VLC_EGENERIC;
}
if( av_find_stream_info( p_sys->ic ) < 0 )
{
msg_Err( p_demux, "av_find_stream_info failed" );
- E_(CloseDemux)( p_this );
+ if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
+ CloseDemux( p_this );
return VLC_EGENERIC;
}
+ if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
for( i = 0; i < p_sys->ic->nb_streams; i++ )
{
es_format_t fmt;
vlc_fourcc_t fcc;
- if( !E_(GetVlcFourcc)( cc->codec_id, NULL, &fcc, NULL ) )
+ if( !GetVlcFourcc( cc->codec_id, NULL, &fcc, NULL ) )
{
fcc = VLC_FOURCC( 'u', 'n', 'd', 'f' );
if( cc->codec_id == CODEC_ID_RAWVIDEO )
{
msg_Dbg( p_demux, "raw video, pixel format: %i", cc->pix_fmt );
- fcc = E_(GetVlcChroma)( cc->pix_fmt );
+ fcc = GetVlcChroma( cc->pix_fmt );
}
}
msg_Dbg( p_demux, "AVFormat supported stream" );
msg_Dbg( p_demux, " - format = %s (%s)",
p_sys->fmt->name, p_sys->fmt->long_name );
- msg_Dbg( p_demux, " - start time = "I64Fd,
+ msg_Dbg( p_demux, " - start time = %"PRId64,
( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) ?
p_sys->ic->start_time * 1000000 / AV_TIME_BASE : -1 );
- msg_Dbg( p_demux, " - duration = "I64Fd,
+ msg_Dbg( p_demux, " - duration = %"PRId64,
( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE ) ?
p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 );
/*****************************************************************************
* Close
*****************************************************************************/
-void E_(CloseDemux)( vlc_object_t *p_this )
+void CloseDemux( vlc_object_t *p_this )
{
demux_t *p_demux = (demux_t*)p_this;
demux_sys_t *p_sys = p_demux->p_sys;
+ bool b_avfmt_nofile;
FREENULL( p_sys->tk );
+
+ b_avfmt_nofile = p_sys->fmt->flags & AVFMT_NOFILE;
+ p_sys->fmt->flags |= AVFMT_NOFILE; /* libavformat must not fopen/fclose */
if( p_sys->ic ) av_close_input_file( p_sys->ic );
- if( p_sys->io_buffer ) free( p_sys->io_buffer );
+ if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
+
+ free( p_sys->io_buffer );
free( p_sys );
}
memcpy( p_frame->p_buffer, pkt.data, pkt.size );
+ if( pkt.flags & PKT_FLAG_KEY )
+ p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
+
i_start_time = ( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) ?
- p_sys->ic->start_time : 0;
+ ( p_sys->ic->start_time / AV_TIME_BASE ) : 0;
p_frame->i_dts = ( pkt.dts == (int64_t)AV_NOPTS_VALUE ) ?
0 : (pkt.dts - i_start_time) * 1000000 *
p_sys->ic->streams[pkt.stream_index]->time_base.den;
#ifdef AVFORMAT_DEBUG
- msg_Dbg( p_demux, "tk[%d] dts="I64Fd" pts="I64Fd,
+ msg_Dbg( p_demux, "tk[%d] dts=%"PRId64" pts=%"PRId64,
pkt.stream_index, p_frame->i_dts, p_frame->i_pts );
#endif
if( pkt.dts > 0 &&
( pkt.stream_index == p_sys->i_pcr_tk || p_sys->i_pcr_tk < 0 ) )
- {
+ {
p_sys->i_pcr_tk = pkt.stream_index;
p_sys->i_pcr = p_frame->i_dts;
if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
i64 += p_sys->ic->start_time;
- msg_Warn( p_demux, "DEMUX_SET_POSITION: "I64Fd, i64 );
+ msg_Warn( p_demux, "DEMUX_SET_POSITION: %"PRId64, i64 );
/* If we have a duration, we prefer to seek by time
but if we don't, or if the seek fails, try BYTE seeking */
int64_t i_size = stream_Size( p_demux->s );
i64 = (int64_t)i_size * f;
- msg_Warn( p_demux, "DEMUX_SET_BYTE_POSITION: "I64Fd, i64 );
+ msg_Warn( p_demux, "DEMUX_SET_BYTE_POSITION: %"PRId64, i64 );
if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BYTE ) < 0 )
return VLC_EGENERIC;
}
if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
i64 += p_sys->ic->start_time;
- msg_Warn( p_demux, "DEMUX_SET_TIME: "I64Fd, i64 );
+ msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 );
if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
{
int64_t i_size = stream_Size( p_demux->s );
#ifdef AVFORMAT_DEBUG
- msg_Warn( p_demux, "IOSeek offset: "I64Fd", whence: %i", offset, whence );
+ msg_Warn( p_demux, "IOSeek offset: %"PRId64", whence: %i", offset, whence );
#endif
switch( whence )
}
if( i_absolute < 0 )
i_absolute = 0;
- if( i_size && i_absolute > i_size )
- i_absolute = i_size;
+ if( i_size )
+ {
+ if( i_absolute > i_size )
+ i_absolute = i_size;
+ if( stream_Tell( p_demux->s ) >= i_size )
+ {
+ msg_Err( p_demux, "Seeking too far : EOF?" );
+ return -1;
+ }
+ }
if( stream_Seek( p_demux->s, i_absolute ) )
{
return stream_Tell( p_demux->s );
}
-#else /* HAVE_FFMPEG_AVFORMAT_H */
-
-int E_(OpenDemux)( vlc_object_t *p_this )
-{
- return VLC_EGENERIC;
-}
-
-void E_(CloseDemux)( vlc_object_t *p_this )
-{
-}
-
-#endif /* HAVE_FFMPEG_AVFORMAT_H */
+#endif /* HAVE_LIBAVFORMAT_AVFORMAT_H */