]> git.sesse.net Git - vlc/blobdiff - modules/demux/mjpeg.c
MKV: cosmetics
[vlc] / modules / demux / mjpeg.c
index 75b29458f17e17d9a3c25337c7d438b8dbcc69d6..31a00e5ce26d58374b6dd872eef00a1920298c65 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
  * mjpeg.c : demuxes mjpeg webcam http streams
  *****************************************************************************
- * Copyright (C) 2004 VideoLAN
- * $Id: mjpeg.c 7196 2004-03-29 21:29:31Z fenrir $
+ * Copyright (C) 2004 the VideoLAN team
+ * $Id$
  *
  * Authors: Henry Jen (slowhog) <henryjen@ztune.net>
  *          Derk-Jan Hartman (thedj)
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <stdlib.h>                                      /* malloc(), free() */
 
-#include <vlc/vlc.h>
-#include <vlc/input.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 
-#include <codecs.h>
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_demux.h>
 
 /*****************************************************************************
  * Module descriptor
@@ -41,15 +43,19 @@ static int  Open ( vlc_object_t * );
 static void Close( vlc_object_t * );
 
 #define FPS_TEXT N_("Frames per Second")
-#define FPS_LONGTEXT N_("Allows you to set the desired frame rate when " \
-    "playing from files, use 0 for live.")
-
-vlc_module_begin();
-    set_description( _("JPEG camera demuxer") );
-    set_capability( "demux2", 5 );
-    set_callbacks( Open, Close );
-    add_float( "mjpeg-fps", 0.0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_FALSE );
-vlc_module_end();
+#define FPS_LONGTEXT N_("This is the desired frame rate when " \
+    "playing MJPEG from a file. Use 0 (this is the default value) for a " \
+    "live stream (from a camera).")
+
+vlc_module_begin ()
+    set_shortname( "MJPEG")
+    set_description( N_("M-JPEG camera demuxer") )
+    set_capability( "demux", 5 )
+    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 )
+vlc_module_end ()
 
 /*****************************************************************************
  * Local prototypes
@@ -63,7 +69,7 @@ struct demux_sys_t
     es_format_t     fmt;
     es_out_id_t     *p_es;
 
-    vlc_bool_t      b_still;
+    bool      b_still;
     mtime_t         i_still_end;
     mtime_t         i_still_length;
 
@@ -71,15 +77,16 @@ struct demux_sys_t
     mtime_t         i_frame_length;
     char            *psz_separator;
     int             i_frame_size_estimate;
-    uint8_t         *p_peek;
+    const uint8_t   *p_peek;
     int             i_data_peeked;
+    int             i_level;
 };
 
 /*****************************************************************************
  * Peek: Helper function to peek data with incremental size.
- * \return VLC_FALSE if peek no more data, VLC_TRUE otherwise.
+ * \return false if peek no more data, true otherwise.
  *****************************************************************************/
-static vlc_bool_t Peek( demux_t *p_demux, vlc_bool_t b_first )
+static bool Peek( demux_t *p_demux, bool b_first )
 {
     int i_data;
     demux_sys_t *p_sys = p_demux->p_sys;
@@ -97,15 +104,15 @@ static vlc_bool_t Peek( demux_t *p_demux, vlc_bool_t b_first )
     if( i_data == p_sys->i_data_peeked )
     {
         msg_Warn( p_demux, "no more data" );
-        return VLC_FALSE;
+        return false;
     }
     p_sys->i_data_peeked = i_data;
     if( i_data <= 0 )
     {
         msg_Warn( p_demux, "cannot peek data" );
-        return VLC_FALSE;
+        return false;
     }
-    return VLC_TRUE;
+    return true;
 }
 
 /*****************************************************************************
@@ -114,14 +121,14 @@ static vlc_bool_t Peek( demux_t *p_demux, vlc_bool_t b_first )
 static char* GetLine( demux_t *p_demux, int *p_pos )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
-    uint8_t     *p_buf;
+    const uint8_t *p_buf;
     int         i_size;
     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, VLC_FALSE ) )
+        if( ! Peek( p_demux, false ) )
         {
             return NULL;
         }
@@ -134,13 +141,13 @@ static char* GetLine( demux_t *p_demux, int *p_pos )
         i++;
         if( i == i_size )
         {
-            if( ! Peek( p_demux, VLC_FALSE ) )
+            if( ! Peek( p_demux, false ) )
             {
                 return NULL;
             }
+            p_buf = p_sys->p_peek + *p_pos;
+            i_size = p_sys->i_data_peeked - *p_pos;
         }
-        p_buf = p_sys->p_peek + *p_pos;
-        i_size = p_sys->i_data_peeked - *p_pos;
     }
     *p_pos += ( i + 1 );
     if( i > 0 && '\r' == p_buf[i - 1] )
@@ -148,12 +155,9 @@ static char* GetLine( demux_t *p_demux, int *p_pos )
         i--;
     }
     p_line = malloc( i + 1 );
-    if( NULL == p_line )
-    {
-        msg_Err( p_demux, "out of memory" );
+    if( p_line == NULL )
         return NULL;
-    }
-    strncpy ( p_line, p_buf, i );
+    strncpy ( p_line, (char*)p_buf, i );
     p_line[i] = '\0';
 //    msg_Dbg( p_demux, "i = %d, pos = %d, %s", i, *p_pos, p_line );
     return p_line;
@@ -163,42 +167,45 @@ static char* GetLine( demux_t *p_demux, int *p_pos )
  * CheckMimeHeader: Internal function used to verify and skip mime header
  * \param p_header_size Return size of MIME header, 0 if no MIME header
  * detected, minus value if error
- * \return VLC_TRUE if content type is image/jpeg, VLC_FALSE otherwise
+ * \return true if content type is image/jpeg, false otherwise
  *****************************************************************************/
-static vlc_bool_t CheckMimeHeader( demux_t *p_demux, int *p_header_size )
+static bool CheckMimeHeader( demux_t *p_demux, int *p_header_size )
 {
-    vlc_bool_t  b_jpeg = VLC_FALSE;
+    bool  b_jpeg = false;
     int         i_pos;
     char        *psz_line;
     char        *p_ch;
     demux_sys_t *p_sys = p_demux->p_sys;
 
-    if( !Peek( p_demux, VLC_TRUE ) )
+    if( !Peek( p_demux, true ) )
     {
         msg_Err( p_demux, "cannot peek" );
         *p_header_size = -1;
-        return VLC_FALSE;
+        return false;
     }
-    if( p_sys->i_data_peeked < 3)
+    if( p_sys->i_data_peeked < 5)
     {
         msg_Err( p_demux, "data shortage" );
         *p_header_size = -2;
-        return VLC_FALSE;
+        return false;
     }
-    if( strncmp( p_sys->p_peek, "--", 2 ) )
+    if( strncmp( (char *)p_sys->p_peek, "--", 2 ) != 0
+        && strncmp( (char *)p_sys->p_peek, "\r\n--", 4 ) != 0 )
     {
         *p_header_size = 0;
-        return VLC_FALSE;
+        return false;
     }
-    i_pos = 2;
+    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 VLC_FALSE;
+        return false;
     }
-    if( NULL == p_sys->psz_separator )
+
+    /* 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",
@@ -213,6 +220,7 @@ static vlc_bool_t CheckMimeHeader( demux_t *p_demux, int *p_header_size )
         }
         free( psz_line );
     }
+
     psz_line = GetLine( p_demux, &i_pos );
     while( psz_line && *psz_line )
     {
@@ -223,16 +231,16 @@ static vlc_bool_t CheckMimeHeader( demux_t *p_demux, int *p_header_size )
             if( strncasecmp( p_ch, "image/jpeg", 10 ) )
             {
                 msg_Warn( p_demux, "%s, image/jpeg is expected", psz_line );
-                b_jpeg = VLC_FALSE;
+                b_jpeg = false;
             }
             else
             {
-                b_jpeg = VLC_TRUE;
+                b_jpeg = true;
             }
         }
         else
         {
-            msg_Dbg( p_demux, "Discard MIME header: %s", psz_line );
+            msg_Dbg( p_demux, "discard MIME header: %s", psz_line );
         }
         free( psz_line );
         psz_line = GetLine( p_demux, &i_pos );
@@ -242,7 +250,7 @@ static vlc_bool_t CheckMimeHeader( demux_t *p_demux, int *p_header_size )
     {
         msg_Err( p_demux, "no EOL" );
         *p_header_size = -3;
-        return VLC_FALSE;
+        return false;
     }
 
     free( psz_line );
@@ -268,7 +276,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;
     }
 
@@ -292,14 +300,14 @@ static int Open( vlc_object_t * p_this )
     demux_t     *p_demux = (demux_t*)p_this;
     demux_sys_t *p_sys;
     int         i_size;
-    int         b_matched = VLC_FALSE;
-    vlc_value_t val;
-    char *psz_ext;
+    bool        b_matched = false;
+    float       f_fps;
 
     p_demux->pf_control = Control;
     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
     p_sys->p_es         = NULL;
     p_sys->i_time       = 0;
+    p_sys->i_level      = 0;
 
     p_sys->psz_separator = NULL;
     p_sys->i_frame_size_estimate = 15 * 1024;
@@ -317,6 +325,7 @@ static int Open( vlc_object_t * p_this )
         {
             msg_Dbg( p_demux, "JPEG SOI marker detected" );
             p_demux->pf_demux = MjpgDemux;
+            p_sys->i_level++;
         }
         else
         {
@@ -329,21 +338,19 @@ 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 */
-    p_sys->b_still = VLC_FALSE;
+    p_sys->b_still = false;
     p_sys->i_still_end = 0;
-    psz_ext = strrchr( p_demux->psz_path, '.' );
-    if( psz_ext && ( !strcasecmp( psz_ext, ".jpeg" ) ||
-                     !strcasecmp( psz_ext, ".jpg" ) ) )
+    if( demux_IsPathExtension( p_demux, ".jpeg" ) ||
+        demux_IsPathExtension( p_demux, ".jpg" ) )
     {
-        p_sys->b_still = VLC_TRUE;
-        if( val.f_float)
+        p_sys->b_still = true;
+        if( f_fps )
         {
-            p_sys->i_still_length =1000000.0 / val.f_float;
+            p_sys->i_still_length = 1000000.0 / f_fps;
         }
         else
         {
@@ -351,13 +358,13 @@ 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 );
-    p_sys->fmt.i_codec = VLC_FOURCC('m','j','p','g');
+    p_sys->fmt.i_codec = VLC_CODEC_MJPG;
 
     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
     return VLC_SUCCESS;
@@ -377,18 +384,15 @@ static int MjpgDemux( demux_t *p_demux )
     demux_sys_t *p_sys = p_demux->p_sys;
     int i;
 
-    if( p_sys->b_still && p_sys->i_still_end && p_sys->i_still_end < mdate() )
+    if( p_sys->b_still && p_sys->i_still_end )
     {
         /* Still frame, wait until the pause delay is gone */
+        mwait( p_sys->i_still_end );
         p_sys->i_still_end = 0;
-    }
-    else if( p_sys->b_still && p_sys->i_still_end )
-    {
-        msleep( 400 );
         return 1;
     }
 
-    if( !Peek( p_demux, VLC_TRUE ) )
+    if( !Peek( p_demux, true ) )
     {
         msg_Warn( p_demux, "cannot peek data" );
         return 0;
@@ -399,16 +403,22 @@ static int MjpgDemux( demux_t *p_demux )
         return 0;
     }
     i = 3;
+FIND_NEXT_EOI:
     while( !( 0xFF == p_sys->p_peek[i-1] && 0xD9 == p_sys->p_peek[i] ) )
     {
+        if( 0xFF == p_sys->p_peek[i-1] && 0xD8 == p_sys->p_peek[i] )
+        {
+            p_sys->i_level++;
+            msg_Dbg( p_demux, "we found another JPEG SOI at %d", i );
+        }
         i++;
         if( i >= p_sys->i_data_peeked )
         {
-            msg_Dbg( p_demux, "Did not find JPEG EOI in %d bytes",
+            msg_Dbg( p_demux, "did not find JPEG EOI in %d bytes",
                      p_sys->i_data_peeked );
-            if( !Peek( p_demux, VLC_FALSE ) )
+            if( !Peek( p_demux, false ) )
             {
-                msg_Warn( p_demux, "No more data is available at the moment" );
+                msg_Warn( p_demux, "no more data is available at the moment" );
                 return 0;
             }
         }
@@ -416,6 +426,10 @@ static int MjpgDemux( demux_t *p_demux )
     i++;
 
     msg_Dbg( p_demux, "JPEG EOI detected at %d", i );
+    p_sys->i_level--;
+
+    if( p_sys->i_level > 0 )
+        goto FIND_NEXT_EOI;
     return SendBlock( p_demux, i );
 }
 
@@ -423,10 +437,9 @@ static int MimeDemux( demux_t *p_demux )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
     int         i_size, i;
-    vlc_bool_t  b_match;
-    vlc_bool_t  b_done;
 
-    b_match = CheckMimeHeader( p_demux, &i_size );
+    bool  b_match = CheckMimeHeader( p_demux, &i_size );
+
     if( i_size > 0 )
     {
         stream_Read( p_demux->s, NULL, i_size );
@@ -438,14 +451,15 @@ static int MimeDemux( demux_t *p_demux )
     else
     {
         // No MIME header, assume OK
-        b_match = VLC_TRUE;
+        b_match = true;
     }
 
-    if( !Peek( p_demux, VLC_TRUE ) )
+    if( !Peek( p_demux, true ) )
     {
         msg_Warn( p_demux, "cannot peek data" );
         return 0;
     }
+
     i = 0;
     i_size = strlen( p_sys->psz_separator ) + 2;
     if( p_sys->i_data_peeked < i_size )
@@ -453,8 +467,8 @@ static int MimeDemux( demux_t *p_demux )
         msg_Warn( p_demux, "data shortage" );
         return 0;
     }
-    b_done = VLC_FALSE;
-    while( !b_done )
+
+    for( ;; )
     {
         while( !( p_sys->p_peek[i] == '-' && p_sys->p_peek[i+1] == '-' ) )
         {
@@ -465,29 +479,28 @@ static int MimeDemux( demux_t *p_demux )
                 msg_Dbg( p_demux, "MIME boundary not found in %d bytes of "
                          "data", p_sys->i_data_peeked );
 
-                if( !Peek( p_demux, VLC_FALSE ) )
+                if( !Peek( p_demux, false ) )
                 {
-                    msg_Warn( p_demux, "No more data is available at the "
+                    msg_Warn( p_demux, "no more data is available at the "
                               "moment" );
                     return 0;
                 }
             }
         }
-        if( !strncmp( p_sys->psz_separator, p_sys->p_peek + i + 2,
+
+        if( !strncmp( p_sys->psz_separator, (char *)(p_sys->p_peek + i + 2),
                       strlen( p_sys->psz_separator ) ) )
         {
-            b_done = VLC_TRUE;
-        }
-        else
-        {
-            i++;
-            i_size++;
+            break;
         }
+
+        i++;
+        i_size++;
     }
 
     if( !b_match )
     {
-        msg_Err( p_demux, "Discard non-JPEG part" );
+        msg_Err( p_demux, "discard non-JPEG part" );
         stream_Read( p_demux->s, NULL, i );
         return 0;
     }
@@ -503,10 +516,7 @@ static void Close ( vlc_object_t * p_this )
     demux_t     *p_demux = (demux_t*)p_this;
     demux_sys_t *p_sys  = p_demux->p_sys;
 
-    if( p_sys->psz_separator )
-    {
-        free( p_sys->psz_separator );
-    }
+    free( p_sys->psz_separator );
     free( p_sys );
 }
 
@@ -515,5 +525,5 @@ static void Close ( vlc_object_t * p_this )
  *****************************************************************************/
 static int Control( demux_t *p_demux, int i_query, va_list args )
 {
-    return demux2_vaControlHelper( p_demux->s, 0, 0, 0, 0, i_query, args );
+    return demux_vaControlHelper( p_demux->s, 0, 0, 0, 0, i_query, args );
 }