]> git.sesse.net Git - vlc/blobdiff - modules/demux/mjpeg.c
SSA: do not use memmove
[vlc] / modules / demux / mjpeg.c
index 406ac6e7a3a47775a0295f8227bde62bdf8a332e..8315ca2e12ccf75426074c2317cf975bd1103ac2 100644 (file)
@@ -35,8 +35,7 @@
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_demux.h>
-
-#include <vlc_codecs.h>
+#include "mxpeg_helper.h"
 
 /*****************************************************************************
  * Module descriptor
@@ -56,7 +55,7 @@ vlc_module_begin ()
     set_callbacks( Open, Close )
     set_category( CAT_INPUT )
     set_subcategory( SUBCAT_INPUT_DEMUX )
-    add_float( "mjpeg-fps", 0.0, NULL, FPS_TEXT, FPS_LONGTEXT, false )
+    add_float( "mjpeg-fps", 0.0, FPS_TEXT, FPS_LONGTEXT, false )
 vlc_module_end ()
 
 /*****************************************************************************
@@ -128,7 +127,7 @@ static char* GetLine( demux_t *p_demux, int *p_pos )
     int         i;
     char        *p_line;
 
-    while( *p_pos > p_sys->i_data_peeked )
+    while( *p_pos >= p_sys->i_data_peeked )
     {
         if( ! Peek( p_demux, false ) )
         {
@@ -174,7 +173,7 @@ static char* GetLine( demux_t *p_demux, int *p_pos )
 static bool CheckMimeHeader( demux_t *p_demux, int *p_header_size )
 {
     bool  b_jpeg = false;
-    int         i_pos;
+    int         i_pos = 0;
     char        *psz_line;
     char        *p_ch;
     demux_sys_t *p_sys = p_demux->p_sys;
@@ -194,33 +193,61 @@ static bool CheckMimeHeader( demux_t *p_demux, int *p_header_size )
     if( strncmp( (char *)p_sys->p_peek, "--", 2 ) != 0
         && strncmp( (char *)p_sys->p_peek, "\r\n--", 4 ) != 0 )
     {
-        *p_header_size = 0;
-        return false;
-    }
-    i_pos = *p_sys->p_peek == '-' ? 2 : 4;
-    psz_line = GetLine( p_demux, &i_pos );
-    if( NULL == psz_line )
-    {
-        msg_Err( p_demux, "no EOL" );
-        *p_header_size = -3;
-        return false;
-    }
-
-    /* Read the separator and remember it if not yet stored */
-    if( p_sys->psz_separator == NULL )
-    {
-        p_sys->psz_separator = psz_line;
-        msg_Dbg( p_demux, "Multipart MIME detected, using separator: %s",
-                 p_sys->psz_separator );
+        /* Some broken stream may lack the first boundary */
+        if ( p_sys->psz_separator == NULL )
+        {
+            msg_Warn( p_demux, "Misformed stream. Trying to work around");
+            char *content_type = stream_ContentType( p_demux->s );
+            if ( content_type == NULL )
+                return false;
+            const char* boundary = strstr( content_type, "boundary=--" );
+            if ( boundary != NULL )
+            {
+                p_sys->psz_separator = strdup( boundary + strlen( "boundary=--" ) );
+                msg_Dbg( p_demux, "Video boundary extracted from Content-Type: %s", p_sys->psz_separator );
+                free( content_type );
+                /* Skip to HTTP header parsing as there's no boundary to extract
+                 * from the stream */
+            }
+            else
+            {
+                free( content_type );
+                return false;
+            }
+        }
+        else
+        {
+            *p_header_size = 0;
+            return false;
+        }
     }
     else
     {
-        if( strcmp( psz_line, p_sys->psz_separator ) )
+        i_pos = *p_sys->p_peek == '-' ? 2 : 4;
+        psz_line = GetLine( p_demux, &i_pos );
+        if( NULL == psz_line )
         {
-            msg_Warn( p_demux, "separator %s does not match %s", psz_line,
-                      p_sys->psz_separator );
+            msg_Err( p_demux, "no EOL" );
+            *p_header_size = -3;
+            return false;
+        }
+
+        /* Read the separator and remember it if not yet stored */
+        if( p_sys->psz_separator == NULL )
+        {
+            p_sys->psz_separator = psz_line;
+            msg_Dbg( p_demux, "Multipart MIME detected, using separator: %s",
+                     p_sys->psz_separator );
+        }
+        else
+        {
+            if( strcmp( psz_line, p_sys->psz_separator ) )
+            {
+                msg_Warn( p_demux, "separator %s does not match %s", psz_line,
+                          p_sys->psz_separator );
+            }
+            free( psz_line );
         }
-        free( psz_line );
     }
 
     psz_line = GetLine( p_demux, &i_pos );
@@ -278,7 +305,7 @@ static int SendBlock( demux_t *p_demux, int i )
     }
     else
     {
-        p_block->i_dts = p_block->i_pts = p_sys->i_time;
+        p_block->i_dts = p_block->i_pts = VLC_TS_0 + p_sys->i_time;
         p_sys->i_time += p_sys->i_frame_length;
     }
 
@@ -303,10 +330,14 @@ static int Open( vlc_object_t * p_this )
     demux_sys_t *p_sys;
     int         i_size;
     bool        b_matched = false;
-    vlc_value_t val;
+    float       f_fps;
+
+    p_sys = malloc( sizeof( demux_sys_t ) );
+    if( unlikely(p_sys == NULL) )
+        return VLC_ENOMEM;
 
     p_demux->pf_control = Control;
-    p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
+    p_demux->p_sys      = p_sys;
     p_sys->p_es         = NULL;
     p_sys->i_time       = 0;
     p_sys->i_level      = 0;
@@ -314,6 +345,12 @@ static int Open( vlc_object_t * p_this )
     p_sys->psz_separator = NULL;
     p_sys->i_frame_size_estimate = 15 * 1024;
 
+    if( IsMxpeg( p_demux->s ) && !p_demux->b_force )
+    {
+        // let avformat handle this case
+        goto error;
+    }
+
     b_matched = CheckMimeHeader( p_demux, &i_size);
     if( b_matched )
     {
@@ -340,8 +377,7 @@ static int Open( vlc_object_t * p_this )
     }
 
 
-    var_Create( p_demux, "mjpeg-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
-    var_Get( p_demux, "mjpeg-fps", &val );
+    f_fps = var_CreateGetFloat( p_demux, "mjpeg-fps" );
     p_sys->i_frame_length = 0;
 
     /* Check for jpeg file extension */
@@ -351,9 +387,9 @@ static int Open( vlc_object_t * p_this )
         demux_IsPathExtension( p_demux, ".jpg" ) )
     {
         p_sys->b_still = true;
-        if( val.f_float)
+        if( f_fps )
         {
-            p_sys->i_still_length = 1000000.0 / val.f_float;
+            p_sys->i_still_length = 1000000.0 / f_fps;
         }
         else
         {
@@ -361,9 +397,9 @@ static int Open( vlc_object_t * p_this )
             p_sys->i_still_length = 1000000;
         }
     }
-    else if ( val.f_float )
+    else if ( f_fps )
     {
-        p_sys->i_frame_length = 1000000.0 / val.f_float;
+        p_sys->i_frame_length = 1000000.0 / f_fps;
     }
 
     es_format_Init( &p_sys->fmt, VIDEO_ES, 0 );