]> git.sesse.net Git - vlc/blobdiff - modules/codec/ffmpeg/demux.c
Split should now be complete (postproc still needs to be fixed).
[vlc] / modules / codec / ffmpeg / demux.c
index 6b9e818e2cb1c411b0cff6eb4e0b834774b56bf9..815e31be755df22eff039f7dd4abdf56502702cb 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * 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
@@ -80,13 +84,14 @@ static offset_t IOSeek( void *opaque, offset_t offset, int whence );
 /*****************************************************************************
  * 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;
@@ -166,6 +171,7 @@ int E_(OpenDemux)( vlc_object_t *p_this )
     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 */
@@ -173,16 +179,19 @@ int E_(OpenDemux)( vlc_object_t *p_this )
                               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++ )
     {
@@ -191,7 +200,7 @@ int E_(OpenDemux)( vlc_object_t *p_this )
         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' );
 
@@ -199,7 +208,7 @@ int E_(OpenDemux)( vlc_object_t *p_this )
             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 );
             }
         }
 
@@ -244,10 +253,10 @@ int E_(OpenDemux)( vlc_object_t *p_this )
     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 );
 
@@ -257,14 +266,20 @@ int E_(OpenDemux)( vlc_object_t *p_this )
 /*****************************************************************************
  * 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 );
 }
 
@@ -295,8 +310,11 @@ static int Demux( demux_t *p_demux )
 
     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 *
@@ -308,13 +326,13 @@ static int Demux( demux_t *p_demux )
         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;
 
@@ -361,7 +379,7 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
                 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 */
@@ -371,7 +389,7 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
                     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;
                 }
@@ -399,7 +417,7 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             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 )
             {
@@ -457,7 +475,7 @@ static offset_t IOSeek( void *opaque, offset_t offset, int whence )
     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 )
@@ -476,8 +494,16 @@ static offset_t IOSeek( void *opaque, offset_t offset, int 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 ) )
     {
@@ -487,15 +513,4 @@ static offset_t IOSeek( void *opaque, offset_t offset, int whence )
     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 */