]> git.sesse.net Git - vlc/blobdiff - modules/codec/ffmpeg/demux.c
Too many problems with new ffmpeg detection, reverting to last known good (rev 25403).
[vlc] / modules / codec / ffmpeg / demux.c
index 9db7a548897a0a519e55129298dbc8613017bf1d..07f948cee80a7c6d4d2c113139da44f2b37be724 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() */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 
 #include <vlc/vlc.h>
-#include <vlc/input.h>
-#include "vlc_meta.h"
+#include <vlc_demux.h>
+#include <vlc_stream.h>
+#include <vlc_meta.h>
 
 /* ffmpeg header */
 #ifdef HAVE_FFMPEG_AVFORMAT_H
 #   include <ffmpeg/avformat.h>
-#else
+#elif defined(HAVE_LIBAVFORMAT_TREE)
 #   include <avformat.h>
 #endif
 
@@ -43,7 +47,7 @@
 //#define AVFORMAT_DEBUG 1
 
 /* Version checking */
-#if (LIBAVFORMAT_BUILD >= 4629) && (defined(HAVE_FFMPEG_AVFORMAT_H) || defined(HAVE_LIBAVFORMAT_TREE))
+#if defined(HAVE_FFMPEG_AVFORMAT_H) || defined(HAVE_LIBAVFORMAT_TREE)
 
 /*****************************************************************************
  * demux_sys_t: demux descriptor
@@ -86,6 +90,7 @@ int E_(OpenDemux)( vlc_object_t *p_this )
     AVProbeData   pd;
     AVInputFormat *fmt;
     int i;
+    vlc_bool_t b_avfmt_nofile;
 
     /* Init Probe data */
     pd.filename = p_demux->psz_path;
@@ -165,6 +170,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 */
@@ -172,6 +178,7 @@ int E_(OpenDemux)( vlc_object_t *p_this )
                               p_sys->fmt, NULL ) )
     {
         msg_Err( p_demux, "av_open_input_stream failed" );
+        if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
         E_(CloseDemux)( p_this );
         return VLC_EGENERIC;
     }
@@ -179,9 +186,11 @@ int E_(OpenDemux)( vlc_object_t *p_this )
     if( av_find_stream_info( p_sys->ic ) < 0 )
     {
         msg_Err( p_demux, "av_find_stream_info failed" );
+        if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
         E_(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++ )
     {
@@ -221,10 +230,15 @@ int E_(OpenDemux)( vlc_object_t *p_this )
                 *fmt.video.p_palette = *(video_palette_t *)cc->palctrl;
             }
             break;
+        case CODEC_TYPE_SUBTITLE:
+            es_format_Init( &fmt, SPU_ES, fcc );
+            break;
         default:
+            msg_Warn( p_demux, "unsupported track type in ffmpeg demux" );
             break;
         }
 
+        fmt.psz_language = strdup( p_sys->ic->streams[i]->language );
         fmt.i_extra = cc->extradata_size;
         fmt.p_extra = cc->extradata;
         es = es_out_Add( p_demux->out, &fmt );
@@ -255,9 +269,15 @@ void E_(CloseDemux)( vlc_object_t *p_this )
 {
     demux_t     *p_demux = (demux_t*)p_this;
     demux_sys_t *p_sys = p_demux->p_sys;
+    vlc_bool_t 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( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE;
+
     if( p_sys->io_buffer ) free( p_sys->io_buffer );
     free( p_sys );
 }
@@ -308,7 +328,7 @@ static int Demux( demux_t *p_demux )
 
     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;
 
@@ -349,22 +369,25 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
         case DEMUX_SET_POSITION:
             f = (double) va_arg( args, double );
             i64 = stream_Tell( p_demux->s );
-            if( i64 && p_sys->i_pcr > 0 )
+            if( p_sys->i_pcr > 0 )
             {
-                int64_t i_size = stream_Size( p_demux->s );
-
-                i64 = p_sys->i_pcr * i_size / i64 * f;
+                i64 = p_sys->ic->duration * f;
                 if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
                     i64 += p_sys->ic->start_time;
 
-                if( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE )
-                    i64 = p_sys->ic->duration * f;
-
                 msg_Warn( p_demux, "DEMUX_SET_POSITION: "I64Fd, i64 );
 
-                if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 )
+                /* If we have a duration, we prefer to seek by time
+                   but if we don't, or if the seek fails, try BYTE seeking */
+                if( p_sys->ic->duration == (int64_t)AV_NOPTS_VALUE ||
+                    (av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0) )
                 {
-                    return VLC_EGENERIC;
+                    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 );
+                    if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BYTE ) < 0 )
+                        return VLC_EGENERIC;
                 }
                 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
                 p_sys->i_pcr = -1; /* Invalidate time display */
@@ -444,7 +467,8 @@ static offset_t IOSeek( void *opaque, offset_t offset, int whence )
 {
     URLContext *p_url = opaque;
     demux_t *p_demux = p_url->priv_data;
-    int64_t i_absolute;
+    int64_t i_absolute = (int64_t)offset;
+    int64_t i_size = stream_Size( p_demux->s );
 
 #ifdef AVFORMAT_DEBUG
     msg_Warn( p_demux, "IOSeek offset: "I64Fd", whence: %i", offset, whence );
@@ -453,36 +477,28 @@ static offset_t IOSeek( void *opaque, offset_t offset, int whence )
     switch( whence )
     {
         case SEEK_SET:
-            i_absolute = offset;
             break;
         case SEEK_CUR:
             i_absolute = stream_Tell( p_demux->s ) + offset;
             break;
         case SEEK_END:
-            i_absolute = stream_Size( p_demux->s ) - offset;
+            i_absolute = i_size + offset;
             break;
         default:
             return -1;
 
     }
+    if( i_absolute < 0 )
+        i_absolute = 0;
+    if( i_size && i_absolute > i_size )
+        i_absolute = i_size;
 
     if( stream_Seek( p_demux->s, i_absolute ) )
     {
         return -1;
     }
 
-    return 0;
-}
-
-#else /* LIBAVFORMAT_BUILD >= 4611 */
-
-int E_(OpenDemux)( vlc_object_t *p_this )
-{
-    return VLC_EGENERIC;
-}
-
-void E_(CloseDemux)( vlc_object_t *p_this )
-{
+    return stream_Tell( p_demux->s );
 }
 
-#endif /* LIBAVFORMAT_BUILD >= 4629 */
+#endif /* HAVE_FFMPEG_AVFORMAT_H */