]> git.sesse.net Git - vlc/blobdiff - modules/demux/nsv.c
Distribute lua/lib/misc.h
[vlc] / modules / demux / nsv.c
index 24fe1fbe62b557672bf749b84e04f6d91d0632f0..0e35e162960834db99261dedf247f10eaa6bd1ad 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * nsv.c: NullSoft Video demuxer.
  *****************************************************************************
- * 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>
-#include <vlc/input.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_demux.h>
 
 /* TODO:
  *  - implement NSVf parsing (to get meta data)
@@ -42,8 +46,8 @@ static int  Open    ( vlc_object_t * );
 static void Close  ( vlc_object_t * );
 
 vlc_module_begin();
-    set_description( _("NullSoft demuxer" ) );
-    set_capability( "demux2", 10 );
+    set_description( N_("NullSoft demuxer" ) );
+    set_capability( "demux", 10 );
     set_category( CAT_INPUT );
     set_subcategory( SUBCAT_INPUT_DEMUX );
     set_callbacks( Open, Close );
@@ -68,9 +72,6 @@ struct demux_sys_t
     int64_t     i_pcr;
     int64_t     i_time;
     int64_t     i_pcr_inc;
-    int64_t     i_audio_desync; /* The amount the audio is ahead */
-
-    vlc_bool_t  b_reinit;
 };
 
 static int Demux  ( demux_t *p_demux );
@@ -89,15 +90,23 @@ static int Open( vlc_object_t *p_this )
     demux_t     *p_demux = (demux_t*)p_this;
     demux_sys_t *p_sys;
 
-    uint8_t     *p_peek;
+    const uint8_t *p_peek;
 
     if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
         return VLC_EGENERIC;
 
+    if( memcmp( p_peek, "NSVf", 4 ) && memcmp( p_peek, "NSVs", 4 ) )
+    {
+       /* In case we had force this demuxer we try to resynch */
+        if( !p_demux->b_force || ReSynch( p_demux ) )
+            return VLC_EGENERIC;
+    }
+
     /* Fill p_demux field */
     p_demux->pf_demux = Demux;
     p_demux->pf_control = Control;
     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
+
     es_format_Init( &p_sys->fmt_audio, AUDIO_ES, 0 );
     p_sys->p_audio = NULL;
 
@@ -111,18 +120,6 @@ static int Open( vlc_object_t *p_this )
     p_sys->i_time  = 0;
     p_sys->i_pcr_inc = 0;
 
-    p_sys->b_reinit = VLC_TRUE;
-
-    if( strncmp( (char *)p_peek, "NSVf", 4 )
-            && strncmp( (char *)p_peek, "NSVs", 4 ))
-    {
-        /* In case we have forced this demuxer we try to resynch */
-        if( strcmp( p_demux->psz_demux, "nsv" ) || ReSynch( p_demux ) )
-        {
-            free( p_sys );
-            return VLC_EGENERIC;
-        }
-    }
     return VLC_SUCCESS;
 }
 
@@ -146,7 +143,7 @@ static int Demux( demux_t *p_demux )
     demux_sys_t *p_sys = p_demux->p_sys;
 
     uint8_t     header[5];
-    uint8_t     *p_peek;
+    const uint8_t *p_peek;
 
     int         i_size;
     block_t     *p_frame;
@@ -159,14 +156,14 @@ static int Demux( demux_t *p_demux )
             return 0;
         }
 
-        if( !strncmp( (char *)p_peek, "NSVf", 4 ) )
+        if( !memcmp( p_peek, "NSVf", 4 ) )
         {
             if( ReadNSVf( p_demux ) )
             {
                 return -1;
             }
         }
-        else if( !strncmp( (char *)p_peek, "NSVs", 4 ) )
+        else if( !memcmp( p_peek, "NSVs", 4 ) )
         {
             if( ReadNSVs( p_demux ) )
             {
@@ -186,7 +183,7 @@ static int Demux( demux_t *p_demux )
         }
         else
         {
-            msg_Err( p_demux, "invalid signature 0x%x (%4.4s)", *(uint32_t*)p_peek, (char*)p_peek );
+            msg_Err( p_demux, "invalid signature 0x%x (%4.4s)", GetDWLE( p_peek ), (const char*)p_peek );
             if( ReSynch( p_demux ) )
             {
                 return -1;
@@ -232,11 +229,8 @@ static int Demux( demux_t *p_demux )
                     p_sys->p_sub = es_out_Add( p_demux->out, &p_sys->fmt_sub );
                     es_out_Control( p_demux->out, ES_OUT_SET_ES, p_sys->p_sub );
                 }
-                if( stream_Read( p_demux->s, NULL, 2 ) < 2 )
-                {
-                    msg_Warn( p_demux, "cannot read" );
-                    return 0;
-                }
+                stream_Read( p_demux->s, NULL, 2 );
+
                 if( ( p_frame = stream_Block( p_demux->s, i_aux - 2 ) ) )
                 {
                     uint8_t *p = p_frame->p_buffer;
@@ -273,11 +267,6 @@ static int Demux( demux_t *p_demux )
         /* msg_Dbg( p_demux, "frame video size=%d", i_size ); */
         if( i_size > 0 && ( p_frame = stream_Block( p_demux->s, i_size ) ) )
         {
-            if( p_frame->i_buffer < i_size )
-            {
-                msg_Warn( p_demux, "cannot read" );
-                return 0;
-            }
             p_frame->i_dts = p_sys->i_pcr;
             es_out_Send( p_demux->out, p_sys->p_video, p_frame );
         }
@@ -291,11 +280,7 @@ static int Demux( demux_t *p_demux )
         if( p_sys->fmt_audio.i_codec == VLC_FOURCC( 'a', 'r', 'a', 'w' ) )
         {
             uint8_t h[4];
-            if( stream_Read( p_demux->s, h, 4 ) < 4 )
-            {
-                msg_Warn( p_demux, "cannot read" );
-                return 0;
-            }
+            stream_Read( p_demux->s, h, 4 );
 
             p_sys->fmt_audio.audio.i_channels = h[1];
             p_sys->fmt_audio.audio.i_rate = GetWLE( &h[2] );
@@ -309,13 +294,8 @@ static int Demux( demux_t *p_demux )
 
         if( ( p_frame = stream_Block( p_demux->s, i_size ) ) )
         {
-            if( p_frame->i_buffer < i_size )
-            {
-                msg_Warn( p_demux, "cannot read" );
-                return 0;
-            }
             p_frame->i_dts =
-            p_frame->i_pts = p_sys->i_pcr - ((p_sys->i_pcr > p_sys->i_audio_desync) ? p_sys->i_audio_desync : 0);
+            p_frame->i_pts = p_sys->i_pcr;
             es_out_Send( p_demux->out, p_sys->p_audio, p_frame );
         }
     }
@@ -403,15 +383,12 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
  *****************************************************************************/
 static int ReSynch( demux_t *p_demux )
 {
-    uint8_t  *p_peek;
+    const uint8_t *p_peek;
     int      i_skip;
     int      i_peek;
 
-    msg_Dbg( p_demux, "a ReSynch was requested");
     while( !p_demux->b_die )
     {
-        p_demux->p_sys->b_reinit = VLC_TRUE;
-
         if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 1024 ) ) < 8 )
         {
             return VLC_EGENERIC;
@@ -420,8 +397,8 @@ static int ReSynch( demux_t *p_demux )
 
         while( i_skip < i_peek - 4 )
         {
-            if( !strncmp( (char *)p_peek, "NSVf", 4 ) || 
-                !strncmp( (char *)p_peek, "NSVs", 4 ) )
+            if( !memcmp( p_peek, "NSVf", 4 )
+             || !memcmp( p_peek, "NSVs", 4 ) )
             {
                 if( i_skip > 0 )
                 {
@@ -433,7 +410,7 @@ static int ReSynch( demux_t *p_demux )
             i_skip++;
         }
 
-        stream_Read( p_demux->s, NULL, i_peek );
+        stream_Read( p_demux->s, NULL, i_skip );
     }
     return VLC_EGENERIC;
 }
@@ -444,7 +421,7 @@ static int ReSynch( demux_t *p_demux )
 static int ReadNSVf( demux_t *p_demux )
 {
     /* demux_sys_t *p_sys = p_demux->p_sys; */
-    uint8_t     *p;
+    const uint8_t     *p;
     int         i_size;
 
     msg_Dbg( p_demux, "new NSVf chunk" );
@@ -458,9 +435,8 @@ static int ReadNSVf( demux_t *p_demux )
 
     return stream_Read( p_demux->s, NULL, i_size ) == i_size ? VLC_SUCCESS : VLC_EGENERIC;
 }
-
 /*****************************************************************************
- * ReadNSVs:
+ * ReadNSVf:
  *****************************************************************************/
 static int ReadNSVs( demux_t *p_demux )
 {
@@ -474,7 +450,6 @@ static int ReadNSVs( demux_t *p_demux )
         return VLC_EGENERIC;
     }
 
-    msg_Dbg( p_demux, "new NSVs chunk" );
     /* Video */
     switch( ( fcc = VLC_FOURCC( header[4], header[5], header[6], header[7] ) ) )
     {
@@ -482,13 +457,17 @@ static int ReadNSVs( demux_t *p_demux )
         case VLC_FOURCC( 'V', 'P', '3', '1' ):
             fcc = VLC_FOURCC( 'V', 'P', '3', '1' );
             break;
+        case VLC_FOURCC( 'V', 'P', '6', '0' ):
+        case VLC_FOURCC( 'V', 'P', '6', '1' ):
+        case VLC_FOURCC( 'V', 'P', '6', '2' ):
+        case VLC_FOURCC( 'H', '2', '6', '4' ):
         case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
             break;
         default:
-            msg_Warn( p_demux, "unknown codec" );
+            msg_Warn( p_demux, "unknown codec %4.4s", (char *)&fcc );
             break;
     }
-    if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && ( fcc != p_sys->fmt_video.i_codec || p_sys->b_reinit ) )
+    if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_video.i_codec  )
     {
         es_format_Init( &p_sys->fmt_video, VIDEO_ES, fcc );
         p_sys->fmt_video.video.i_width = GetWLE( &header[12] );
@@ -514,22 +493,18 @@ static int ReadNSVs( demux_t *p_demux )
         case VLC_FOURCC( 'P', 'C', 'M', ' ' ):
             fcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
             break;
-        case VLC_FOURCC( 'V', 'L', 'B', ' ' ):
         case VLC_FOURCC( 'A', 'A', 'C', ' ' ):
         case VLC_FOURCC( 'A', 'A', 'C', 'P' ):
             fcc = VLC_FOURCC( 'm', 'p', '4', 'a' );
             break;
-        case VLC_FOURCC( 'S', 'P', 'X', ' ' ):
-            fcc = VLC_FOURCC( 's', 'p', 'x', ' ' );
-            break;
         case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
             break;
         default:
-            msg_Warn( p_demux, "unknown codec" );
+            msg_Warn( p_demux, "unknown codec %4.4s", (char *)&fcc );
             break;
     }
 
-    if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && ( fcc != p_sys->fmt_audio.i_codec || p_sys->b_reinit ) )
+    if( fcc != VLC_FOURCC( 'N', 'O', 'N', 'E' ) && fcc != p_sys->fmt_audio.i_codec )
     {
         msg_Dbg( p_demux, "    - audio `%4.4s'", (char*)&fcc );
 
@@ -544,15 +519,26 @@ static int ReadNSVs( demux_t *p_demux )
     if( header[16]&0x80 )
     {
         /* Fractional frame rate */
-        float s = 0;
-        int t = ( header[16]&0x7f ) >> 2;
-        if( t < 16 ) s = 1.0 / t+1;
-        else s = t-15;
-
-        if( header[16]&0x01 ) s = s * 1000.0 / 1001.0;
-        if( (header[16]&0x03) == 3) p_sys->i_pcr_inc = (int)1000000 / s * 24;
-        else if( (header[16]&0x03) == 2 ) p_sys->i_pcr_inc = (int)1000000 / s * 25;
-        else p_sys->i_pcr_inc = (int)1000000 / s * 30;
+        switch( header[16]&0x03 )
+        {
+            case 0: /* 30 fps */
+                p_sys->i_pcr_inc = 33333; /* 300000/9 */
+                break;
+            case 1: /* 29.97 fps */
+                p_sys->i_pcr_inc = 33367; /* 300300/9 */
+                break;
+            case 2: /* 25 fps */
+                p_sys->i_pcr_inc = 40000; /* 360000/9 */
+                break;
+            case 3: /* 23.98 fps */
+                p_sys->i_pcr_inc = 41700; /* 375300/9 */
+                break;
+        }
+
+        if( header[16] < 0xc0 )
+            p_sys->i_pcr_inc = p_sys->i_pcr_inc * (((header[16] ^ 0x80) >> 2 ) +1 );
+        else
+            p_sys->i_pcr_inc = p_sys->i_pcr_inc / (((header[16] ^ 0xc0) >> 2 ) +1 );
     }
     else if( header[16] != 0 )
     {
@@ -564,13 +550,7 @@ static int ReadNSVs( demux_t *p_demux )
         msg_Dbg( p_demux, "invalid fps (0x00)" );
         p_sys->i_pcr_inc = 40000;
     }
-    msg_Dbg( p_demux, "    - fps=%.3f", 1000000.0 / (double)p_sys->i_pcr_inc );
-
-    /* amount of miliseconds the audio is ahead of the video frame */
-    p_sys->i_audio_desync = (int64_t)1000 * GetWLE( &header[18] );
-    msg_Dbg( p_demux, "    - desync=%lld", p_sys->i_audio_desync);
-
-    p_sys->b_reinit = VLC_FALSE;
+    //msg_Dbg( p_demux, "    - fps=%.3f", 1000000.0 / (double)p_sys->i_pcr_inc );
 
     return VLC_SUCCESS;
 }