]> git.sesse.net Git - vlc/blobdiff - modules/access/v4l2.c
Missing #include <errno.h>
[vlc] / modules / access / v4l2.c
index c6688d6b1114bd4fbc5bdfcf084cd3097b684708..01a7a01eb373a2bfad5c155fa9539ce38fa70afa 100644 (file)
@@ -44,6 +44,7 @@
 #include <vlc_input.h>
 
 #include <ctype.h>
+#include <errno.h>
 #include <fcntl.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
@@ -203,6 +204,9 @@ static void AccessClose( vlc_object_t * );
     "please use 'v4l2:/""/ :input-slave=alsa:/""/' or " \
     "'v4l2:/""/ :input-slave=oss:/""/' instead." )
 
+#define ASPECT_TEXT N_("Picture aspect-ratio n:m")
+#define ASPECT_LONGTEXT N_("Define input picture aspect-ratio to use. Default is 4:3" )
+
 typedef enum {
     IO_METHOD_READ,
     IO_METHOD_MMAP,
@@ -257,6 +261,8 @@ vlc_module_begin ()
                 WIDTH_LONGTEXT, true )
     add_integer( CFG_PREFIX "height", -1, NULL, HEIGHT_TEXT,
                 HEIGHT_LONGTEXT, true )
+    add_string( CFG_PREFIX "aspect-ratio", "4:3", NULL, ASPECT_TEXT,
+              ASPECT_LONGTEXT, true )
     add_float( CFG_PREFIX "fps", 0, NULL, FPS_TEXT, FPS_LONGTEXT, true )
     add_integer( CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
                 CACHING_TEXT, CACHING_LONGTEXT, true )
@@ -361,10 +367,10 @@ static int DemuxControl( demux_t *, int, va_list );
 static int AccessControl( access_t *, int, va_list );
 
 static int Demux( demux_t * );
-static ssize_t AccessRead( access_t *, uint8_t *, size_t );
+static block_t *AccessRead( access_t * );
 
-static block_t* GrabVideo( demux_t *p_demux );
-static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t );
+static block_t* GrabVideo( vlc_object_t *p_demux, demux_sys_t *p_sys );
+static block_t* ProcessVideoFrame( vlc_object_t *p_demux, uint8_t *p_frame, size_t );
 
 static bool IsPixelFormatSupported( demux_t *p_demux,
                                           unsigned int i_pixelformat );
@@ -393,7 +399,7 @@ static int AccessControlResetCallback( vlc_object_t *p_this,
 static const struct
 {
     unsigned int i_v4l2;
-    int i_fourcc;
+    vlc_fourcc_t i_fourcc;
     int i_rmask;
     int i_gmask;
     int i_bmask;
@@ -428,8 +434,8 @@ static const struct
 
     /* Compressed data types */
     { V4L2_PIX_FMT_MJPEG,   VLC_CODEC_MJPG, 0, 0, 0 },
+    { V4L2_PIX_FMT_JPEG,    VLC_CODEC_JPEG, 0, 0, 0 },
 #if 0
-    { V4L2_PIX_FMT_JPEG,    VLC_FOURCC('J','P','E','G') },
     { V4L2_PIX_FMT_DV,      VLC_FOURCC('?','?','?','?') },
     { V4L2_PIX_FMT_MPEG,    VLC_FOURCC('?','?','?','?') },
 #endif
@@ -445,7 +451,7 @@ static const struct
 static const __u32 p_chroma_fallbacks[] =
 { V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_YVU420, V4L2_PIX_FMT_YUV422P,
   V4L2_PIX_FMT_YUYV, V4L2_PIX_FMT_UYVY, V4L2_PIX_FMT_BGR24,
-  V4L2_PIX_FMT_BGR32, V4L2_PIX_FMT_MJPEG };
+  V4L2_PIX_FMT_BGR32, V4L2_PIX_FMT_MJPEG, V4L2_PIX_FMT_JPEG };
 
 static const struct
 {
@@ -523,6 +529,7 @@ struct demux_sys_t
 
     int i_width;
     int i_height;
+    unsigned int i_aspect;
     float f_fps;            /* <= 0.0 mean to grab at full rate */
     mtime_t i_video_pts;    /* only used when f_fps > 0 */
     int i_fourcc;
@@ -685,6 +692,19 @@ static void GetV4L2Params( demux_sys_t *p_sys, vlc_object_t *p_obj )
 
     p_sys->psz_set_ctrls = var_CreateGetString( p_obj, "v4l2-set-ctrls" );
 
+    char *psz_aspect = var_CreateGetString( p_obj, "v4l2-aspect-ratio" );
+    if( psz_aspect && *psz_aspect && strchr( psz_aspect, ':' ) )
+    {
+        char *psz_delim = strchr( psz_aspect, ':' );
+        p_sys->i_aspect = atoi( psz_aspect ) * VOUT_ASPECT_FACTOR / atoi( psz_delim + 1 );
+    }
+    else
+    {
+        p_sys->i_aspect = 4 * VOUT_ASPECT_FACTOR / 3 ;
+
+    }
+    free( psz_aspect );
+
     p_sys->psz_device = NULL;
     p_sys->i_fd = -1;
 
@@ -810,6 +830,17 @@ static void ParseMRL( demux_sys_t *p_sys, char *psz_path, vlc_object_t *p_obj )
                     strtol( psz_parser + strlen( "height=" ),
                             &psz_parser, 0 );
             }
+            else if( !strncmp( psz_parser, "aspect-ratio=",
+                               strlen( "aspect-ratio=" ) ) )
+            {
+                unsigned int num,den;
+                num = strtol( psz_parser + strlen( "aspect-ratio=" ),
+                              &psz_parser, 0 );
+                den = strtol( psz_parser + strlen( ":" ),
+                              &psz_parser, 0 );
+                if( num && den )
+                    p_sys->i_aspect = num * VOUT_ASPECT_FACTOR / den;
+            }
             else if( !strncmp( psz_parser, "controls-reset",
                                strlen( "controls-reset" ) ) )
             {
@@ -1054,7 +1085,7 @@ static int AccessOpen( vlc_object_t * p_this )
     if( *p_access->psz_access == '\0' ) return VLC_EGENERIC;
 
     access_InitFields( p_access );
-    ACCESS_SET_CALLBACKS( AccessRead, NULL, AccessControl, NULL );
+    ACCESS_SET_CALLBACKS( NULL, AccessRead, AccessControl, NULL );
     p_sys = calloc( 1, sizeof( demux_sys_t ));
     if( !p_sys ) return VLC_ENOMEM;
     p_access->p_sys = (access_sys_t*)p_sys;
@@ -1087,29 +1118,21 @@ static int AccessOpen( vlc_object_t * p_this )
  *****************************************************************************/
 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
 {
-    demux_sys_t *p_sys = p_demux->p_sys;
-    bool *pb;
-    int64_t    *pi64;
-
     switch( i_query )
     {
         /* Special for access_demux */
         case DEMUX_CAN_PAUSE:
         case DEMUX_CAN_SEEK:
-        case DEMUX_SET_PAUSE_STATE:
         case DEMUX_CAN_CONTROL_PACE:
-            pb = (bool*)va_arg( args, bool * );
-            *pb = false;
+            *va_arg( args, bool * ) = false;
             return VLC_SUCCESS;
 
         case DEMUX_GET_PTS_DELAY:
-            pi64 = (int64_t*)va_arg( args, int64_t * );
-            *pi64 = (int64_t)p_sys->i_cache * 1000;
+            *va_arg(args,int64_t *) = (int64_t)p_demux->p_sys->i_cache*1000;
             return VLC_SUCCESS;
 
         case DEMUX_GET_TIME:
-            pi64 = (int64_t*)va_arg( args, int64_t * );
-            *pi64 = mdate();
+            *va_arg( args, int64_t * ) = mdate();
             return VLC_SUCCESS;
 
         /* TODO implement others */
@@ -1125,8 +1148,6 @@ static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
  *****************************************************************************/
 static int AccessControl( access_t *p_access, int i_query, va_list args )
 {
-    bool    *pb_bool;
-    int64_t *pi_64;
     demux_sys_t  *p_sys = (demux_sys_t *) p_access->p_sys;
 
     switch( i_query )
@@ -1134,22 +1155,14 @@ static int AccessControl( access_t *p_access, int i_query, va_list args )
         /* */
         case ACCESS_CAN_SEEK:
         case ACCESS_CAN_FASTSEEK:
-            pb_bool = (bool*)va_arg( args, bool* );
-            *pb_bool = false;
-            break;
         case ACCESS_CAN_PAUSE:
-            pb_bool = (bool*)va_arg( args, bool* );
-            *pb_bool = false;
-            break;
         case ACCESS_CAN_CONTROL_PACE:
-            pb_bool = (bool*)va_arg( args, bool* );
-            *pb_bool = false;
+            *va_arg( args, bool* ) = false;
             break;
 
         /* */
         case ACCESS_GET_PTS_DELAY:
-            pi_64 = (int64_t*)va_arg( args, int64_t * );
-            *pi_64 = (int64_t) p_sys->i_cache * 1000;
+            *va_arg(args,int64_t *) = (int64_t)p_sys->i_cache*1000;
             break;
 
         /* */
@@ -1176,44 +1189,25 @@ static int AccessControl( access_t *p_access, int i_query, va_list args )
 /*****************************************************************************
  * AccessRead: access callback
  ******************************************************************************/
-static ssize_t AccessRead( access_t * p_access, uint8_t * p_buffer, size_t i_len )
+static block_t *AccessRead( access_t * p_access )
 {
-    demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
-    struct pollfd ufd;
-    int i_ret;
-
-    ufd.fd = p_sys->i_fd;
-    ufd.events = POLLIN;
+    demux_sys_t *p_sys = (demux_sys_t *)p_access->p_sys;
 
-    if( p_access->info.b_eof )
-        return 0;
-
-    do
-    {
-        if( !vlc_object_alive (p_access) )
-            return 0;
-
-        ufd.revents = 0;
-    }
-    while( ( i_ret = poll( &ufd, 1, 500 ) ) == 0 );
-
-    if( i_ret < 0 )
-    {
-        msg_Err( p_access, "Polling error (%m)." );
-        return -1;
-    }
+    struct pollfd fd;
+    fd.fd = p_sys->i_fd;
+    fd.events = POLLIN|POLLPRI;
+    fd.revents = 0;
 
-    i_ret = v4l2_read( p_sys->i_fd, p_buffer, i_len );
-    if( i_ret == 0 )
-    {
-        p_access->info.b_eof = true;
-    }
-    else if( i_ret > 0 )
+    /* Wait for data */
+    if( poll( &fd, 1, 500 ) ) /* Timeout after 0.5 seconds since I don't know if pf_demux can be blocking. */
     {
-        p_access->info.i_pos += i_ret;
+        if( fd.revents & (POLLIN|POLLPRI) )
+        {
+            return GrabVideo( VLC_OBJECT(p_access), p_sys );
+        }
     }
 
-    return i_ret;
+    return NULL;
 }
 
 /*****************************************************************************
@@ -1233,7 +1227,7 @@ static int Demux( demux_t *p_demux )
     {
         if( fd.revents & (POLLIN|POLLPRI) )
         {
-            block_t *p_block = GrabVideo( p_demux );
+            block_t *p_block = GrabVideo( VLC_OBJECT(p_demux), p_sys );
             if( p_block )
             {
                 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
@@ -1248,10 +1242,8 @@ static int Demux( demux_t *p_demux )
 /*****************************************************************************
  * GrabVideo: Grab a video frame
  *****************************************************************************/
-static block_t* GrabVideo( demux_t *p_demux )
+static block_t* GrabVideo( vlc_object_t *p_demux, demux_sys_t *p_sys )
 {
-    demux_sys_t *p_sys = p_demux->p_sys;
-
     block_t *p_block = NULL;
     struct v4l2_buffer buf;
     ssize_t i_ret;
@@ -1388,7 +1380,7 @@ static block_t* GrabVideo( demux_t *p_demux )
  * ProcessVideoFrame: Helper function to take a buffer and copy it into
  * a new block
  *****************************************************************************/
-static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t i_size )
+static block_t* ProcessVideoFrame( vlc_object_t *p_demux, uint8_t *p_frame, size_t i_size )
 {
     block_t *p_block;
 
@@ -1410,9 +1402,9 @@ static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t i_
 /*****************************************************************************
  * Helper function to initalise video IO using the Read method
  *****************************************************************************/
-static int InitRead( demux_t *p_demux, unsigned int i_buffer_size )
+static int InitRead( vlc_object_t *p_demux, demux_sys_t *p_sys, unsigned int i_buffer_size )
 {
-    demux_sys_t *p_sys = p_demux->p_sys;
+    (void)p_demux;
 
     p_sys->p_buffers = calloc( 1, sizeof( *p_sys->p_buffers ) );
     if( !p_sys->p_buffers )
@@ -1429,9 +1421,8 @@ static int InitRead( demux_t *p_demux, unsigned int i_buffer_size )
 /*****************************************************************************
  * Helper function to initalise video IO using the mmap method
  *****************************************************************************/
-static int InitMmap( demux_t *p_demux, int i_fd )
+static int InitMmap( vlc_object_t *p_demux, demux_sys_t *p_sys, int i_fd )
 {
-    demux_sys_t *p_sys = p_demux->p_sys;
     struct v4l2_requestbuffers req;
 
     memset( &req, 0, sizeof(req) );
@@ -1491,9 +1482,8 @@ open_failed:
 /*****************************************************************************
  * Helper function to initalise video IO using the userbuf method
  *****************************************************************************/
-static int InitUserP( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
+static int InitUserP( vlc_object_t *p_demux, demux_sys_t *p_sys, int i_fd, unsigned int i_buffer_size )
 {
-    demux_sys_t *p_sys = p_demux->p_sys;
     struct v4l2_requestbuffers req;
     unsigned int i_page_size;
 
@@ -1855,13 +1845,6 @@ static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
                   var_GetBool( p_obj, "v4l2-controls-reset" ), b_demux );
     SetAvailControlsByString( p_obj, p_sys, i_fd );
 
-    if( false == b_demux)
-    {
-        return i_fd;
-    }
-
-    demux_t *p_demux = (demux_t *) p_obj;
-
     /* Verify device support for the various IO methods */
     switch( p_sys->io )
     {
@@ -1917,7 +1900,7 @@ static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
         /* Use current width and height settings */
         if( v4l2_ioctl( i_fd, VIDIOC_G_FMT, &fmt ) < 0 )
         {
-            msg_Err( p_demux, "Cannot get default width and height." );
+            msg_Err( p_obj, "Cannot get default width and height." );
             goto open_failed;
         }
 
@@ -1931,29 +1914,30 @@ static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
     }
     else if( p_sys->i_width < 0 || p_sys->i_height < 0 )
     {
-        msg_Dbg( p_demux, "will try to find optimal width and height." );
+        msg_Dbg( p_obj, "will try to find optimal width and height." );
     }
     else
     {
         /* Use user specified width and height */
-        msg_Dbg( p_demux, "trying specified size %dx%d", p_sys->i_width, p_sys->i_height );
+        msg_Dbg( p_obj, "trying specified size %dx%d", p_sys->i_width, p_sys->i_height );
     }
 
     fmt.fmt.pix.width = p_sys->i_width;
     fmt.fmt.pix.height = p_sys->i_height;
     fmt.fmt.pix.field = V4L2_FIELD_NONE;
 
-    /* Test and set Chroma */
-    fmt.fmt.pix.pixelformat = 0;
-    if( p_sys->psz_requested_chroma && strlen( p_sys->psz_requested_chroma ) > 0 )
+    if (b_demux)
     {
-        /* User specified chroma */
-        if( strlen( p_sys->psz_requested_chroma ) >= 4 )
+        demux_t *p_demux = (demux_t *) p_obj;
+
+        /* Test and set Chroma */
+        fmt.fmt.pix.pixelformat = 0;
+        if( p_sys->psz_requested_chroma && *p_sys->psz_requested_chroma )
         {
-            int i_requested_fourcc = vlc_fourcc_GetCodec( VIDEO_ES,
-                                                          VLC_FOURCC(
-                p_sys->psz_requested_chroma[0], p_sys->psz_requested_chroma[1],
-                p_sys->psz_requested_chroma[2], p_sys->psz_requested_chroma[3] ) );
+            /* User specified chroma */
+            const vlc_fourcc_t i_requested_fourcc =
+                vlc_fourcc_GetCodecFromString( VIDEO_ES, p_sys->psz_requested_chroma );
+
             for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
             {
                 if( v4l2chroma_to_fourcc[i].i_fourcc == i_requested_fourcc )
@@ -1962,92 +1946,113 @@ static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
                     break;
                 }
             }
+            /* Try and set user chroma */
+            bool b_error = !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat );
+            if( !b_error && fmt.fmt.pix.pixelformat )
+            {
+                if( v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 )
+                {
+                    fmt.fmt.pix.field = V4L2_FIELD_ANY;
+                    if( v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 )
+                    {
+                        fmt.fmt.pix.field = V4L2_FIELD_NONE;
+                        b_error = true;
+                    }
+                }
+            }
+            if( b_error )
+            {
+                msg_Warn( p_demux, "Driver is unable to use specified chroma %s. Trying defaults.", p_sys->psz_requested_chroma );
+                fmt.fmt.pix.pixelformat = 0;
+            }
         }
-        /* Try and set user chroma */
-        if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ( fmt.fmt.pix.pixelformat && v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 ) )
-        {
-            msg_Warn( p_demux, "Driver is unable to use specified chroma %s. Trying defaults.", p_sys->psz_requested_chroma );
-            fmt.fmt.pix.pixelformat = 0;
-        }
-    }
 
-    /* If no user specified chroma, find best */
-    /* This also decides if MPEG encoder card or not */
-    if( !fmt.fmt.pix.pixelformat )
-    {
-        unsigned int i;
-        for( i = 0; i < ARRAY_SIZE( p_chroma_fallbacks ); i++ )
-        {
-            fmt.fmt.pix.pixelformat = p_chroma_fallbacks[i];
-            if( IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat )
-             && v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) >= 0 )
-                break;
-        }
-        if( i == ARRAY_SIZE( p_chroma_fallbacks ) )
+        /* If no user specified chroma, find best */
+        /* This also decides if MPEG encoder card or not */
+        if( !fmt.fmt.pix.pixelformat )
         {
-            msg_Warn( p_demux, "Could not select any of the default chromas; attempting to open as MPEG encoder card (access)" );
-            goto open_failed;
+            unsigned int i;
+            for( i = 0; i < ARRAY_SIZE( p_chroma_fallbacks ); i++ )
+            {
+                fmt.fmt.pix.pixelformat = p_chroma_fallbacks[i];
+                if( IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) )
+                {
+                    if( v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) >= 0 )
+                        break;
+                    fmt.fmt.pix.field = V4L2_FIELD_ANY;
+                    if( v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) >= 0 )
+                        break;
+                    fmt.fmt.pix.field = V4L2_FIELD_NONE;
+                }
+            }
+            if( i == ARRAY_SIZE( p_chroma_fallbacks ) )
+            {
+                msg_Warn( p_demux, "Could not select any of the default chromas; attempting to open as MPEG encoder card (access)" );
+                goto open_failed;
+            }
         }
-    }
 
-    if( p_sys->i_width < 0 || p_sys->i_height < 0 )
-    {
-        if( p_sys->f_fps <= 0 )
+        if( p_sys->i_width < 0 || p_sys->i_height < 0 )
         {
-            p_sys->f_fps = GetAbsoluteMaxFrameRate( p_demux, i_fd,
-                                                    fmt.fmt.pix.pixelformat );
-            msg_Dbg( p_demux, "Found maximum framerate of %f", p_sys->f_fps );
+            if( p_sys->f_fps <= 0 )
+            {
+                p_sys->f_fps = GetAbsoluteMaxFrameRate( p_demux, i_fd,
+                                                        fmt.fmt.pix.pixelformat );
+                msg_Dbg( p_demux, "Found maximum framerate of %f", p_sys->f_fps );
+            }
+            GetMaxDimensions( p_demux, i_fd,
+                              fmt.fmt.pix.pixelformat, p_sys->f_fps,
+                              &fmt.fmt.pix.width, &fmt.fmt.pix.height );
+            msg_Dbg( p_demux, "Found optimal dimensions for framerate %f of %dx%d",
+                     p_sys->f_fps, fmt.fmt.pix.width, fmt.fmt.pix.height );
+            if( v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 ) {;}
         }
-        GetMaxDimensions( p_demux, i_fd,
-                          fmt.fmt.pix.pixelformat, p_sys->f_fps,
-                          &fmt.fmt.pix.width, &fmt.fmt.pix.height );
-        msg_Dbg( p_demux, "Found optimal dimensions for framerate %f of %dx%d",
-                 p_sys->f_fps, fmt.fmt.pix.width, fmt.fmt.pix.height );
-        if( v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 ) {;}
-    }
 
-    /* Reassign width, height and chroma incase driver override */
-    p_sys->i_width = fmt.fmt.pix.width;
-    p_sys->i_height = fmt.fmt.pix.height;
+        /* Reassign width, height and chroma incase driver override */
+        p_sys->i_width = fmt.fmt.pix.width;
+        p_sys->i_height = fmt.fmt.pix.height;
+    }
 
-    /* Check interlacing */
     if( v4l2_ioctl( i_fd, VIDIOC_G_FMT, &fmt ) < 0 ) {;}
+    /* Print extra info */
+    msg_Dbg( p_obj, "Driver requires at most %d bytes to store a complete image", fmt.fmt.pix.sizeimage );
+    /* Check interlacing */
     switch( fmt.fmt.pix.field )
     {
         case V4L2_FIELD_NONE:
-            msg_Dbg( p_demux, "Interlacing setting: progressive" );
+            msg_Dbg( p_obj, "Interlacing setting: progressive" );
             break;
         case V4L2_FIELD_TOP:
-            msg_Dbg( p_demux, "Interlacing setting: top field only" );
+            msg_Dbg( p_obj, "Interlacing setting: top field only" );
             break;
         case V4L2_FIELD_BOTTOM:
-            msg_Dbg( p_demux, "Interlacing setting: bottom field only" );
+            msg_Dbg( p_obj, "Interlacing setting: bottom field only" );
             break;
         case V4L2_FIELD_INTERLACED:
-            msg_Dbg( p_demux, "Interlacing setting: interleaved (bottom top if M/NTSC, top bottom otherwise)" );
+            msg_Dbg( p_obj, "Interlacing setting: interleaved (bottom top if M/NTSC, top bottom otherwise)" );
             break;
         case V4L2_FIELD_SEQ_TB:
-            msg_Dbg( p_demux, "Interlacing setting: sequential top bottom" );
+            msg_Dbg( p_obj, "Interlacing setting: sequential top bottom" );
             break;
         case V4L2_FIELD_SEQ_BT:
-            msg_Dbg( p_demux, "Interlacing setting: sequential bottom top" );
+            msg_Dbg( p_obj, "Interlacing setting: sequential bottom top" );
             break;
         case V4L2_FIELD_ALTERNATE:
-            msg_Dbg( p_demux, "Interlacing setting: alternate fields" );
+            msg_Dbg( p_obj, "Interlacing setting: alternate fields" );
             break;
         case V4L2_FIELD_INTERLACED_TB:
-            msg_Dbg( p_demux, "Interlacing setting: interleaved top bottom" );
+            msg_Dbg( p_obj, "Interlacing setting: interleaved top bottom" );
             break;
         case V4L2_FIELD_INTERLACED_BT:
-            msg_Dbg( p_demux, "Interlacing setting: interleaved bottom top" );
+            msg_Dbg( p_obj, "Interlacing setting: interleaved bottom top" );
             break;
         default:
-            msg_Warn( p_demux, "Interlacing setting: unknown type (%d)",
+            msg_Warn( p_obj, "Interlacing setting: unknown type (%d)",
                       fmt.fmt.pix.field );
             break;
     }
     if( fmt.fmt.pix.field != V4L2_FIELD_NONE )
-        msg_Warn( p_demux, "Interlaced inputs haven't been tested. Please report any issue." );
+        msg_Warn( p_obj, "Interlaced inputs haven't been tested. Please report any issue." );
 
     /* Look up final fourcc */
     p_sys->i_fourcc = 0;
@@ -2085,21 +2090,21 @@ static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
         char psz_fourcc[5];
         memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
         vlc_fourcc_to_char( p_sys->i_fourcc, &psz_fourcc );
-        msg_Dbg( p_demux, "supported frame intervals for %4s, %dx%d:",
+        msg_Dbg( p_obj, "supported frame intervals for %4s, %dx%d:",
                  psz_fourcc, frmival.width, frmival.height );
         switch( frmival.type )
         {
             case V4L2_FRMIVAL_TYPE_DISCRETE:
                 do
                 {
-                    msg_Dbg( p_demux, "    supported frame interval: %d/%d",
+                    msg_Dbg( p_obj, "    supported frame interval: %d/%d",
                              frmival.discrete.numerator,
                              frmival.discrete.denominator );
                     frmival.index++;
                 } while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 );
                 break;
             case V4L2_FRMIVAL_TYPE_STEPWISE:
-                msg_Dbg( p_demux, "    supported frame intervals: %d/%d to "
+                msg_Dbg( p_obj, "    supported frame intervals: %d/%d to "
                          "%d/%d using %d/%d increments",
                          frmival.stepwise.min.numerator,
                          frmival.stepwise.min.denominator,
@@ -2109,7 +2114,7 @@ static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
                          frmival.stepwise.step.denominator );
                 break;
             case V4L2_FRMIVAL_TYPE_CONTINUOUS:
-                msg_Dbg( p_demux, "    supported frame intervals: %d/%d to %d/%d",
+                msg_Dbg( p_obj, "    supported frame intervals: %d/%d to %d/%d",
                          frmival.stepwise.min.numerator,
                          frmival.stepwise.min.denominator,
                          frmival.stepwise.max.numerator,
@@ -2124,15 +2129,15 @@ static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
     switch( p_sys->io )
     {
     case IO_METHOD_READ:
-        if( InitRead( p_demux, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
+        if( InitRead( p_obj, p_sys, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
         break;
 
     case IO_METHOD_MMAP:
-        if( InitMmap( p_demux, i_fd ) != VLC_SUCCESS ) goto open_failed;
+        if( InitMmap( p_obj, p_sys, i_fd ) != VLC_SUCCESS ) goto open_failed;
         break;
 
     case IO_METHOD_USERPTR:
-        if( InitUserP( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
+        if( InitUserP( p_obj, p_sys, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
         break;
 
     }
@@ -2140,11 +2145,17 @@ static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
     /* Add */
     es_fmt.video.i_width  = p_sys->i_width;
     es_fmt.video.i_height = p_sys->i_height;
-    es_fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
 
-    msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
-        (char*)&es_fmt.i_codec, es_fmt.video.i_width, es_fmt.video.i_height );
-    p_sys->p_es = es_out_Add( p_demux->out, &es_fmt );
+    /* Get aspect-ratio */
+    es_fmt.video.i_aspect = p_sys->i_aspect;
+
+    if( b_demux )
+    {
+        demux_t *p_demux = (demux_t *) p_obj;
+        msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
+            (char*)&es_fmt.i_codec, es_fmt.video.i_width, es_fmt.video.i_height );
+        p_sys->p_es = es_out_Add( p_demux->out, &es_fmt );
+    }
 
     /* Start Capture */
 
@@ -2166,7 +2177,7 @@ static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
 
             if( v4l2_ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
             {
-                msg_Err( p_demux, "VIDIOC_QBUF failed" );
+                msg_Err( p_obj, "VIDIOC_QBUF failed" );
                 goto open_failed;
             }
         }
@@ -2174,7 +2185,7 @@ static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
         buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
         if( v4l2_ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
         {
-            msg_Err( p_demux, "VIDIOC_STREAMON failed" );
+            msg_Err( p_obj, "VIDIOC_STREAMON failed" );
             goto open_failed;
         }
 
@@ -2194,7 +2205,7 @@ static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
 
             if( v4l2_ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
             {
-                msg_Err( p_demux, "VIDIOC_QBUF failed" );
+                msg_Err( p_obj, "VIDIOC_QBUF failed" );
                 goto open_failed;
             }
         }
@@ -2202,7 +2213,7 @@ static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
         buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
         if( v4l2_ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
         {
-            msg_Err( p_demux, "VIDIOC_STREAMON failed" );
+            msg_Err( p_obj, "VIDIOC_STREAMON failed" );
             goto open_failed;
         }
 
@@ -2212,7 +2223,7 @@ static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
     /* report fps */
     if( p_sys->f_fps >= 0.1 )
     {
-        msg_Dbg( p_demux, "User set fps=%f", p_sys->f_fps );
+        msg_Dbg( p_obj, "User set fps=%f", p_sys->f_fps );
     }
 
     return i_fd;
@@ -2328,6 +2339,7 @@ static bool ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
     {
         struct v4l2_standard t_standards;
         t_standards.index = 0;
+        p_sys->i_standard = 0;
         while( v4l2_ioctl( i_fd, VIDIOC_ENUMSTD, &t_standards ) >=0 )
         {
             p_sys->i_standard++;
@@ -2391,6 +2403,7 @@ static bool ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
     {
         struct v4l2_tuner tuner;
         memset( &tuner, 0, sizeof(tuner) );
+        p_sys->i_tuner = 0;
         while( v4l2_ioctl( i_fd, VIDIOC_G_TUNER, &tuner ) >= 0 )
         {
             p_sys->i_tuner++;
@@ -2913,6 +2926,7 @@ static void SetAvailControlsByString( vlc_object_t *p_obj, demux_sys_t *p_sys,
                 Control( p_obj, p_sys, i_fd, psz_name, i_cid,
                          strtol( ++psz_assign, &psz_parser, 0) );
             }
+            free( name.psz_string );
         }
 
         if( psz_parser < psz_assign )
@@ -2923,6 +2937,7 @@ static void SetAvailControlsByString( vlc_object_t *p_obj, demux_sys_t *p_sys,
             psz_parser = ( *psz_delim ) ? ( psz_delim + 1 ) : psz_delim;
         }
     }
+    var_FreeList( &val, &text );
 }
 
 /*****************************************************************************
@@ -2958,7 +2973,8 @@ static int ControlReset( vlc_object_t *p_obj, demux_sys_t *p_sys, int i_fd )
                     if( controls[i].i_cid == queryctrl.id ) break;
                 name2var( queryctrl.name );
                 Control( p_obj, p_sys, i_fd,
-                         controls[i].psz_name ? : (const char *)queryctrl.name,
+                         controls[i].psz_name ? controls[i].psz_name
+                          : (const char *)queryctrl.name,
                          queryctrl.id, queryctrl.default_value );
             }
             queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
@@ -2988,7 +3004,8 @@ static int ControlReset( vlc_object_t *p_obj, demux_sys_t *p_sys, int i_fd )
                         if( controls[i].i_cid == queryctrl.id ) break;
                     name2var( queryctrl.name );
                     Control( p_obj, p_sys, i_fd,
-                             controls[i].psz_name ? : (const char *)queryctrl.name,
+                             controls[i].psz_name ? controls[i].psz_name
+                              : (const char *)queryctrl.name,
                              queryctrl.id, queryctrl.default_value );
                 }
             }
@@ -3089,12 +3106,12 @@ static int Control( vlc_object_t *p_obj, demux_sys_t *p_sys, int i_fd,
             case VLC_VAR_BOOL:
                 val.b_bool = control.value;
                 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
-                var_SetVoid( p_obj, "controls-update" );
+                var_TriggerCallback( p_obj, "controls-update" );
                 break;
             case VLC_VAR_INTEGER:
                 val.i_int = control.value;
                 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
-                var_SetVoid( p_obj, "controls-update" );
+                var_TriggerCallback( p_obj, "controls-update" );
                 break;
         }
     }