]> git.sesse.net Git - vlc/blobdiff - modules/access/v4l2.c
Ooops, this wasn't supposed to be here anymore.
[vlc] / modules / access / v4l2.c
index 999404045f2a93e068666ca8e478099c6f02ed79..6a0b9378ea3b6c2464e83a13a4b52ca1fbe10191 100644 (file)
@@ -4,8 +4,9 @@
  * Copyright (C) 2002-2007 the VideoLAN team
  * $Id$
  *
- * Author: Benjamin Pracht <bigben at videolan dot org>
- *         Richard Hosking <richard at hovis dot net>
+ * Authors: Benjamin Pracht <bigben at videolan dot org>
+ *          Richard Hosking <richard at hovis dot net>
+ *          Antoine Cellerier <dionoea at videolan d.t net>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -74,17 +75,15 @@ static void Close( vlc_object_t * );
 #define DEV_LONGTEXT N_( \
     "Name of the device to use. " \
     "If you don't specify anything, /dev/video0 will be used.")
-#define ADEV_TEXT N_("Audio device name")
-#define ADEV_LONGTEXT N_( \
-    "Name of the audio device to use. " \
-    "If you don't specify anything, /dev/dsp will be used.")
 #define STANDARD_TEXT N_( "Standard" )
 #define STANDARD_LONGTEXT N_( \
     "Video standard (Default, SECAM, PAL, or NTSC)." )
 #define CHROMA_TEXT N_("Video input chroma format")
 #define CHROMA_LONGTEXT N_( \
     "Force the Video4Linux2 video device to use a specific chroma format " \
-    "(eg. I420, RV24, etc.)")
+    "(eg. I420 or I422 for raw images, MJPEG for M-JPEG compressed input) " \
+    "(Complete list: GREY, I240, RV16, RV15, RV24, RV32, YUY2, YUYV, UYVY, " \
+    "I41N, I422, I420, I411, I410, MJPG)")
 #define INPUT_TEXT N_( "Input" )
 #define INPUT_LONGTEXT N_( \
     "Input of the card to use (Usually, 0 = tuner, " \
@@ -98,6 +97,13 @@ static void Close( vlc_object_t * );
 #define HEIGHT_TEXT N_( "Height" )
 #define HEIGHT_LONGTEXT N_( \
     "Force height (-1 for autodetect)." )
+#define FPS_TEXT N_( "Framerate" )
+#define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
+    "(-1 for autodetect)." )
+
+#define CTRL_RESET_TEXT N_( "Reset v4l2 controls" )
+#define CTRL_RESET_LONGTEXT N_( \
+    "Reset controls to defaults provided by the v4l2 driver." )
 #define BRIGHTNESS_TEXT N_( "Brightness" )
 #define BRIGHTNESS_LONGTEXT N_( \
     "Brightness of the video input." )
@@ -110,9 +116,14 @@ static void Close( vlc_object_t * );
 #define HUE_TEXT N_( "Hue" )
 #define HUE_LONGTEXT N_( \
     "Hue of the video input." )
-#define FPS_TEXT N_( "Framerate" )
-#define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
-    "(-1 for autodetect)." )
+#define GAMMA_TEXT N_( "Gamma" )
+#define GAMMA_LONGTEXT N_( \
+    "Gamma of the video input." )
+
+#define ADEV_TEXT N_("Audio device name")
+#define ADEV_LONGTEXT N_( \
+    "Name of the audio device to use. " \
+    "If you don't specify anything, /dev/dsp will be used.")
 #define ALSA_TEXT N_( "Use Alsa" )
 #define ALSA_LONGTEXT N_( \
     "Use ALSA instead of OSS for audio" )
@@ -122,6 +133,7 @@ static void Close( vlc_object_t * );
 #define SAMPLERATE_TEXT N_( "Samplerate" )
 #define SAMPLERATE_LONGTEXT N_( \
     "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100, 48000)" )
+
 #define CACHING_TEXT N_("Caching value in ms")
 #define CACHING_LONGTEXT N_( \
     "Caching value for V4L2 captures. This " \
@@ -143,48 +155,59 @@ static int i_iomethod_list[] =
 static const char *psz_iomethod_list_text[] =
     { N_("READ"), N_("MMAP"),  N_("USERPTR") };
 
+#define CFG_PREFIX "v4l2-"
+
 vlc_module_begin();
     set_shortname( _("Video4Linux2") );
     set_description( _("Video4Linux2 input") );
     set_category( CAT_INPUT );
     set_subcategory( SUBCAT_INPUT_ACCESS );
 
-    add_string( "v4l2-dev", "/dev/video0", 0, DEV_TEXT, DEV_LONGTEXT,
-                VLC_FALSE );
-    add_string( "v4l2-adev", "/dev/dsp", 0, ADEV_TEXT, ADEV_LONGTEXT,
-                VLC_FALSE );
-    add_integer( "v4l2-standard", 0, NULL, STANDARD_TEXT, STANDARD_LONGTEXT,
+    set_section( N_( "Video input" ), NULL );
+    add_string( CFG_PREFIX "dev", "/dev/video0", 0, DEV_TEXT, DEV_LONGTEXT,
                 VLC_FALSE );
+    add_integer( CFG_PREFIX "standard", 0, NULL, STANDARD_TEXT,
+                 STANDARD_LONGTEXT, VLC_FALSE );
         change_integer_list( i_standards_list, psz_standards_list_text, 0 );
-    add_string( "v4l2-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
+    add_string( CFG_PREFIX "chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
                 VLC_TRUE );
-    add_integer( "v4l2-input", 0, NULL, INPUT_TEXT, INPUT_LONGTEXT,
+    add_integer( CFG_PREFIX "input", 0, NULL, INPUT_TEXT, INPUT_LONGTEXT,
                 VLC_TRUE );
-    add_integer( "v4l2-io", IO_METHOD_MMAP, NULL, IOMETHOD_TEXT,
+    add_integer( CFG_PREFIX "io", IO_METHOD_MMAP, NULL, IOMETHOD_TEXT,
                  IOMETHOD_LONGTEXT, VLC_TRUE );
         change_integer_list( i_iomethod_list, psz_iomethod_list_text, 0 );
-    add_integer( "v4l2-width", 0, NULL, WIDTH_TEXT,
+    add_integer( CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
                 WIDTH_LONGTEXT, VLC_TRUE );
-    add_integer( "v4l2-height", 0, NULL, HEIGHT_TEXT,
+    add_integer( CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
                 HEIGHT_LONGTEXT, VLC_TRUE );
-    add_integer( "v4l2-brightness", -1, NULL, BRIGHTNESS_TEXT,
+    add_float( CFG_PREFIX "fps", 0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_TRUE );
+
+    set_section( N_( "Video controls" ), NULL );
+    add_bool( CFG_PREFIX "control-reset", VLC_FALSE, NULL, CTRL_RESET_TEXT,
+              CTRL_RESET_LONGTEXT, VLC_TRUE );
+    add_integer( CFG_PREFIX "brightness", -1, NULL, BRIGHTNESS_TEXT,
                 BRIGHTNESS_LONGTEXT, VLC_TRUE );
-    add_integer( "v4l2-contrast", -1, NULL, CONTRAST_TEXT,
+    add_integer( CFG_PREFIX "contrast", -1, NULL, CONTRAST_TEXT,
                 CONTRAST_LONGTEXT, VLC_TRUE );
-    add_integer( "v4l2-saturation", -1, NULL, SATURATION_TEXT,
+    add_integer( CFG_PREFIX "saturation", -1, NULL, SATURATION_TEXT,
                 SATURATION_LONGTEXT, VLC_TRUE );
-    add_integer( "v4l2-hue", -1, NULL, HUE_TEXT,
+    add_integer( CFG_PREFIX "hue", -1, NULL, HUE_TEXT,
                 HUE_LONGTEXT, VLC_TRUE );
-    add_float( "v4l2-fps", 0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_TRUE );
+    add_integer( CFG_PREFIX "gamma", -1, NULL, GAMMA_TEXT,
+                GAMMA_LONGTEXT, VLC_TRUE );
+
+    set_section( N_( "Audio input" ), NULL );
+    add_string( CFG_PREFIX "adev", "/dev/dsp", 0, ADEV_TEXT, ADEV_LONGTEXT,
+                VLC_FALSE );
 #ifdef HAVE_ALSA
-    add_bool( "v4l2-alsa", VLC_FALSE, NULL, ALSA_TEXT, ALSA_LONGTEXT,
+    add_bool( CFG_PREFIX "alsa", VLC_FALSE, NULL, ALSA_TEXT, ALSA_LONGTEXT,
                 VLC_TRUE );
 #endif
-    add_bool( "v4l2-stereo", VLC_TRUE, NULL, STEREO_TEXT, STEREO_LONGTEXT,
+    add_bool( CFG_PREFIX "stereo", VLC_TRUE, NULL, STEREO_TEXT, STEREO_LONGTEXT,
                 VLC_TRUE );
-    add_integer( "v4l2-samplerate", 48000, NULL, SAMPLERATE_TEXT,
+    add_integer( CFG_PREFIX "samplerate", 48000, NULL, SAMPLERATE_TEXT,
                 SAMPLERATE_LONGTEXT, VLC_TRUE );
-    add_integer( "v4l2-caching", DEFAULT_PTS_DELAY / 1000, NULL,
+    add_integer( CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
                 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
 
     add_shortcut( "v4l2" );
@@ -202,7 +225,7 @@ static int Control( demux_t *, int, va_list );
 
 static int Demux( demux_t * );
 static block_t* GrabVideo( demux_t *p_demux );
-static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame );
+static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t );
 static block_t* GrabAudio( demux_t *p_demux );
 
 vlc_bool_t IsPixelFormatSupported( demux_t *p_demux, unsigned int i_pixelformat );
@@ -213,9 +236,9 @@ static int OpenAudioDev( demux_t *, char *psz_device );
 static vlc_bool_t ProbeVideoDev( demux_t *, char *psz_device );
 static vlc_bool_t ProbeAudioDev( demux_t *, char *psz_device );
 
-static int VideoControlList( demux_t *p_demux, int i_fd );
+static int VideoControlList( demux_t *p_demux, int i_fd, vlc_bool_t b_reset );
 static int VideoControl( demux_t *, int i_fd,
-                         const char *psz_label, int i_cid, int i_value );
+                         const char *psz_name, int i_cid, int i_value );
 static int VideoControlCallback( vlc_object_t *p_this,
     const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
     void *p_data );
@@ -226,23 +249,63 @@ static struct
     int i_fourcc;
 } v4l2chroma_to_fourcc[] =
 {
-    { V4L2_PIX_FMT_GREY,    VLC_FOURCC( 'G', 'R', 'E', 'Y' ) },
-    { V4L2_PIX_FMT_HI240,   VLC_FOURCC( 'I', '2', '4', '0' ) },
-    { V4L2_PIX_FMT_RGB565,  VLC_FOURCC( 'R', 'V', '1', '6' ) },
-    { V4L2_PIX_FMT_RGB555,  VLC_FOURCC( 'R', 'V', '1', '5' ) },
-    { V4L2_PIX_FMT_BGR24,   VLC_FOURCC( 'R', 'V', '2', '4' ) },
-    { V4L2_PIX_FMT_BGR32,   VLC_FOURCC( 'R', 'V', '3', '2' ) },
-    { V4L2_PIX_FMT_YUYV,    VLC_FOURCC( 'Y', 'U', 'Y', '2' ) },
-    { V4L2_PIX_FMT_YUYV,    VLC_FOURCC( 'Y', 'U', 'Y', 'V' ) },
-    { V4L2_PIX_FMT_UYVY,    VLC_FOURCC( 'U', 'Y', 'V', 'Y' ) },
-    { V4L2_PIX_FMT_Y41P,    VLC_FOURCC( 'I', '4', '1', 'N' ) },
-    { V4L2_PIX_FMT_YUV422P, VLC_FOURCC( 'I', '4', '2', '2' ) },
-    { V4L2_PIX_FMT_YVU420,  VLC_FOURCC( 'I', '4', '2', '0' ) },
-    { V4L2_PIX_FMT_YUV411P, VLC_FOURCC( 'I', '4', '1', '1' ) },
-    { V4L2_PIX_FMT_YUV410,  VLC_FOURCC( 'I', '4', '1', '0' ) },
+    /* Raw data types */
+    { V4L2_PIX_FMT_GREY,    VLC_FOURCC('G','R','E','Y') },
+    { V4L2_PIX_FMT_HI240,   VLC_FOURCC('I','2','4','0') },
+    { V4L2_PIX_FMT_RGB565,  VLC_FOURCC('R','V','1','6') },
+    { V4L2_PIX_FMT_RGB555,  VLC_FOURCC('R','V','1','5') },
+    { V4L2_PIX_FMT_BGR24,   VLC_FOURCC('R','V','2','4') },
+    { V4L2_PIX_FMT_BGR32,   VLC_FOURCC('R','V','3','2') },
+    { V4L2_PIX_FMT_YUYV,    VLC_FOURCC('Y','U','Y','2') },
+    { V4L2_PIX_FMT_YUYV,    VLC_FOURCC('Y','U','Y','V') },
+    { V4L2_PIX_FMT_UYVY,    VLC_FOURCC('U','Y','V','Y') },
+    { V4L2_PIX_FMT_Y41P,    VLC_FOURCC('I','4','1','N') },
+    { V4L2_PIX_FMT_YUV422P, VLC_FOURCC('I','4','2','2') },
+    { V4L2_PIX_FMT_YVU420,  VLC_FOURCC('I','4','2','0') },
+    { V4L2_PIX_FMT_YUV411P, VLC_FOURCC('I','4','1','1') },
+    { V4L2_PIX_FMT_YUV410,  VLC_FOURCC('I','4','1','0') },
+    /* Compressed data types */
+    { V4L2_PIX_FMT_MJPEG,   VLC_FOURCC('M','J','P','G') },
+#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
     { 0, 0 }
 };
 
+static struct
+{
+    const char *psz_name;
+    unsigned int i_cid;
+} controls[] =
+{
+    { "brightness", V4L2_CID_BRIGHTNESS },
+    { "contrast", V4L2_CID_CONTRAST },
+    { "saturation", V4L2_CID_SATURATION },
+    { "hue", V4L2_CID_HUE },
+    { "audio-volume", V4L2_CID_AUDIO_VOLUME },
+    { "audio-balance", V4L2_CID_AUDIO_BALANCE },
+    { "audio-bass", V4L2_CID_AUDIO_BASS },
+    { "audio-treble", V4L2_CID_AUDIO_TREBLE },
+    { "audio-mute", V4L2_CID_AUDIO_MUTE },
+    { "audio-loudness", V4L2_CID_AUDIO_LOUDNESS },
+    { "black-level", V4L2_CID_BLACK_LEVEL },
+    { "auto-white-balance", V4L2_CID_AUTO_WHITE_BALANCE },
+    { "do-white-balance", V4L2_CID_DO_WHITE_BALANCE },
+    { "red-balance", V4L2_CID_RED_BALANCE },
+    { "blue-balance", V4L2_CID_BLUE_BALANCE },
+    { "gamma", V4L2_CID_GAMMA },
+    { "exposure", V4L2_CID_EXPOSURE },
+    { "autogain", V4L2_CID_AUTOGAIN },
+    { "gain", V4L2_CID_GAIN },
+    { "hflip", V4L2_CID_HFLIP },
+    { "vflip", V4L2_CID_VFLIP },
+    { "hcenter", V4L2_CID_HCENTER },
+    { "vcenter", V4L2_CID_VCENTER },
+    { NULL, 0 }
+};
+
 struct buffer_t
 {
     void *  start;
@@ -296,14 +359,6 @@ struct demux_sys_t
     mtime_t i_video_pts;    /* only used when f_fps > 0 */
     int i_fourcc;
 
-    int i_brightness;
-    int i_contrast;
-    int i_saturation;
-    int i_hue;
-
-    picture_t pic;
-    int i_video_frame_size;
-
     es_out_id_t *p_es_video;
 
     /* Audio */
@@ -361,19 +416,7 @@ static int Open( vlc_object_t *p_this )
     p_sys->i_width = var_CreateGetInteger( p_demux, "v4l2-width" );
     p_sys->i_height = var_CreateGetInteger( p_demux, "v4l2-height" );
 
-    p_sys->i_brightness =
-        var_CreateGetIntegerCommand( p_demux, "v4l2-brightness" );
-    p_sys->i_contrast =
-        var_CreateGetIntegerCommand( p_demux, "v4l2-contrast" );
-    p_sys->i_saturation =
-        var_CreateGetIntegerCommand( p_demux, "v4l2-saturation" );
-    p_sys->i_hue =
-        var_CreateGetIntegerCommand( p_demux, "v4l2-hue" );
-
-    var_AddCallback( p_demux, "v4l2-brightness", VideoControlCallback, NULL );
-    var_AddCallback( p_demux, "v4l2-contrast", VideoControlCallback, NULL );
-    var_AddCallback( p_demux, "v4l2-saturation", VideoControlCallback, NULL );
-    var_AddCallback( p_demux, "v4l2-hue", VideoControlCallback, NULL );
+    var_CreateGetBool( p_demux, "v4l2-control-reset" );
 
     var_Create( p_demux, "v4l2-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
     var_Get( p_demux, "v4l2-fps", &val );
@@ -638,34 +681,49 @@ static void ParseMRL( demux_t *p_demux )
                     strtol( psz_parser + strlen( "height=" ),
                             &psz_parser, 0 );
             }
+            else if( !strncmp( psz_parser, "control-reset",
+                               strlen( "control-reset" ) ) )
+            {
+                var_SetBool( p_demux, "v4l2-control-reset", VLC_TRUE );
+                psz_parser += strlen( "control-reset" );
+            }
+#if 0
             else if( !strncmp( psz_parser, "brightness=",
                                strlen( "brightness=" ) ) )
             {
-                p_sys->i_brightness =
+                var_SetInteger( p_demux, "brightness",
                     strtol( psz_parser + strlen( "brightness=" ),
-                            &psz_parser, 0 );
+                            &psz_parser, 0 ) );
             }
             else if( !strncmp( psz_parser, "contrast=",
                                strlen( "contrast=" ) ) )
             {
-                p_sys->i_contrast =
+                var_SetInteger( p_demux, "contrast",
                     strtol( psz_parser + strlen( "contrast=" ),
-                            &psz_parser, 0 );
+                            &psz_parser, 0 ) );
             }
             else if( !strncmp( psz_parser, "saturation=",
                                strlen( "saturation=" ) ) )
             {
-                p_sys->i_saturation =
+                var_SetInteger( p_demux, "saturation",
                     strtol( psz_parser + strlen( "saturation=" ),
-                            &psz_parser, 0 );
+                            &psz_parser, 0 ) );
             }
             else if( !strncmp( psz_parser, "hue=",
                                strlen( "hue=" ) ) )
             {
-                p_sys->i_hue =
+                var_SetInteger( p_demux, "hue",
                     strtol( psz_parser + strlen( "hue=" ),
-                            &psz_parser, 0 );
+                            &psz_parser, 0 ) );
             }
+            else if( !strncmp( psz_parser, "gamma=",
+                               strlen( "gamma=" ) ) )
+            {
+                var_SetInteger( p_demux, "gamma",
+                    strtol( psz_parser + strlen( "gamma=" ),
+                            &psz_parser, 0 ) );
+            }
+#endif
             else if( !strncmp( psz_parser, "samplerate=",
                                strlen( "samplerate=" ) ) )
             {
@@ -892,6 +950,7 @@ static block_t* GrabVideo( demux_t *p_demux )
 
     block_t *p_block = NULL;
     struct v4l2_buffer buf;
+    ssize_t i_ret;
 
     if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
     {
@@ -905,7 +964,8 @@ static block_t* GrabVideo( demux_t *p_demux )
     switch( p_sys->io )
     {
     case IO_METHOD_READ:
-        if( read( p_sys->i_fd_video, p_sys->p_buffers[0].start, p_sys->p_buffers[0].length ) )
+        i_ret = read( p_sys->i_fd_video, p_sys->p_buffers[0].start, p_sys->p_buffers[0].length );
+        if( i_ret == -1 )
         {
             switch( errno )
             {
@@ -920,7 +980,7 @@ static block_t* GrabVideo( demux_t *p_demux )
                }
         }
 
-        p_block = ProcessVideoFrame( p_demux, (uint8_t*)p_sys->p_buffers[0].start );
+        p_block = ProcessVideoFrame( p_demux, (uint8_t*)p_sys->p_buffers[0].start, i_ret );
         if( !p_block ) return 0;
 
         break;
@@ -951,7 +1011,7 @@ static block_t* GrabVideo( demux_t *p_demux )
             return 0;
         }
 
-        p_block = ProcessVideoFrame( p_demux, p_sys->p_buffers[buf.index].start );
+        p_block = ProcessVideoFrame( p_demux, p_sys->p_buffers[buf.index].start, buf.bytesused );
         if( !p_block ) return 0;
 
         /* Unlock */
@@ -998,7 +1058,7 @@ static block_t* GrabVideo( demux_t *p_demux )
             return 0;
         }
 
-        p_block = ProcessVideoFrame( p_demux, (uint8_t*)buf.m.userptr );
+        p_block = ProcessVideoFrame( p_demux, (uint8_t*)buf.m.userptr, buf.bytesused );
         if( !p_block ) return 0;
 
         /* Unlock */
@@ -1022,22 +1082,21 @@ 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 )
+static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t i_size )
 {
-    demux_sys_t *p_sys = p_demux->p_sys;
     block_t *p_block;
 
     if( !p_frame ) return 0;
 
     /* New block */
-    if( !( p_block = block_New( p_demux, p_sys->i_video_frame_size ) ) )
+    if( !( p_block = block_New( p_demux, i_size ) ) )
     {
         msg_Warn( p_demux, "Cannot get new block" );
         return 0;
     }
 
     /* Copy frame */
-    memcpy( p_block->p_buffer, p_frame, p_sys->i_video_frame_size );
+    memcpy( p_block->p_buffer, p_frame, i_size );
 
     return p_block;
 }
@@ -1069,6 +1128,9 @@ static block_t* GrabAudio( demux_t *p_demux )
         /* ALSA */
         i_read = snd_pcm_readi( p_sys->p_alsa_pcm, p_block->p_buffer, p_sys->i_alsa_chunk_size );
         /* TODO: ALSA ERROR HANDLING?? xrun?? */
+
+        /* convert from frames to bytes */
+        if( i_read > 0 ) i_read *= p_sys->i_alsa_frame_size;
     }
     else
 #endif
@@ -1303,6 +1365,7 @@ int OpenVideoDev( demux_t *p_demux, char *psz_device )
     struct v4l2_format fmt;
     unsigned int i_min;
     enum v4l2_buf_type buf_type;
+    int i;
 
     if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
     {
@@ -1339,26 +1402,26 @@ int OpenVideoDev( demux_t *p_demux, char *psz_device )
     /* Verify device support for the various IO methods */
     switch( p_sys->io )
     {
-    case IO_METHOD_READ:
-        if( !(p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE) )
-        {
-            msg_Err( p_demux, "device does not support read i/o" );
-            goto open_failed;
-        }
-        break;
+        case IO_METHOD_READ:
+            if( !(p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE) )
+            {
+                msg_Err( p_demux, "device does not support read i/o" );
+                goto open_failed;
+            }
+            break;
 
-    case IO_METHOD_MMAP:
-    case IO_METHOD_USERPTR:
-        if( !(p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING) )
-        {
-            msg_Err( p_demux, "device does not support streaming i/o" );
-            goto open_failed;
-        }
-        break;
+        case IO_METHOD_MMAP:
+        case IO_METHOD_USERPTR:
+            if( !(p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING) )
+            {
+                msg_Err( p_demux, "device does not support streaming i/o" );
+                goto open_failed;
+            }
+            break;
 
-    default:
-        msg_Err( p_demux, "io method not supported" );
-        goto open_failed;
+        default:
+            msg_Err( p_demux, "io method not supported" );
+            goto open_failed;
     }
 
     /* Reset Cropping */
@@ -1372,12 +1435,12 @@ int OpenVideoDev( demux_t *p_demux, char *psz_device )
         {
             switch( errno )
             {
-            case EINVAL:
-                /* Cropping not supported. */
-                break;
-            default:
-                /* Errors ignored. */
-                break;
+                case EINVAL:
+                    /* Cropping not supported. */
+                    break;
+                default:
+                    /* Errors ignored. */
+                    break;
             }
         }
     }
@@ -1527,30 +1590,8 @@ int OpenVideoDev( demux_t *p_demux, char *psz_device )
     }
 #endif
 
-    VideoControlList( p_demux, i_fd );
-
-    VideoControl( p_demux, i_fd,
-                  "brightness", V4L2_CID_BRIGHTNESS, p_sys->i_brightness );
-    VideoControl( p_demux, i_fd,
-                  "contrast", V4L2_CID_CONTRAST, p_sys->i_contrast );
-    VideoControl( p_demux, i_fd,
-                  "saturation", V4L2_CID_SATURATION, p_sys->i_saturation );
-    VideoControl( p_demux, i_fd, "hue", V4L2_CID_HUE, p_sys->i_hue );
-
-    /* Init vout Picture */
-    vout_InitPicture( VLC_OBJECT(p_demux), &p_sys->pic, p_sys->i_fourcc,
-        p_sys->i_width, p_sys->i_height, p_sys->i_width *
-        VOUT_ASPECT_FACTOR / p_sys->i_height );
-    if( !p_sys->pic.i_planes )
-    {
-        msg_Err( p_demux, "unsupported chroma" );
-        goto open_failed;
-    }
-    p_sys->i_video_frame_size = 0;
-    for( int i = 0; i < p_sys->pic.i_planes; i++ )
-    {
-         p_sys->i_video_frame_size += p_sys->pic.p[i].i_visible_lines * p_sys->pic.p[i].i_visible_pitch;
-    }
+    VideoControlList( p_demux, i_fd,
+                      var_GetBool( p_demux, "v4l2-control-reset" ) );
 
     /* Init IO method */
     switch( p_sys->io )
@@ -1987,9 +2028,12 @@ vlc_bool_t ProbeVideoDev( demux_t *p_demux, char *psz_device )
 
     if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
     {
-        while( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_input ) >= 0 )
+        struct v4l2_input t_input;
+        t_input.index = 0;
+        while( ioctl( i_fd, VIDIOC_ENUMINPUT, &t_input ) >= 0 )
         {
             p_sys->i_input++;
+            t_input.index = p_sys->i_input;
         }
 
         p_sys->p_inputs = calloc( 1, p_sys->i_input * sizeof( struct v4l2_input ) );
@@ -2050,7 +2094,6 @@ vlc_bool_t ProbeVideoDev( demux_t *p_demux, char *psz_device )
     }
 
     /* Probe audio inputs */
-
     if( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO )
     {
         while( p_sys->i_audio < 32 &&
@@ -2073,14 +2116,15 @@ vlc_bool_t ProbeVideoDev( demux_t *p_demux, char *psz_device )
         }
     }
 
+    /* List tuner caps */
     if( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER )
     {
         struct v4l2_tuner tuner;
-
         memset( &tuner, 0, sizeof(tuner) );
-        while( ioctl( i_fd, VIDIOC_S_TUNER, &tuner ) >= 0 )
+        while( ioctl( i_fd, VIDIOC_G_TUNER, &tuner ) >= 0 )
         {
             p_sys->i_tuner++;
+            memset( &tuner, 0, sizeof(tuner) );
             tuner.index = p_sys->i_tuner;
         }
 
@@ -2168,7 +2212,8 @@ vlc_bool_t ProbeVideoDev( demux_t *p_demux, char *psz_device )
                     frmsize.pixel_format = p_sys->p_codecs[i_index].pixelformat;
                     if( ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) < 0 )
                     {
-                        msg_Err( p_demux, "Error while querying for frame size" );
+                        /* Not all devices support this ioctl */
+                        msg_Warn( p_demux, "Unable to query for frame sizes" );
                     }
                     else
                     {
@@ -2282,82 +2327,209 @@ open_failed:
 /*****************************************************************************
  * List available controls
  *****************************************************************************/
-static int VideoControlList( demux_t *p_demux, int i_fd )
+static void VideoControlListPrint( demux_t *p_demux, int i_fd,
+                                   struct v4l2_queryctrl queryctrl,
+                                   vlc_bool_t b_reset )
 {
-    struct v4l2_queryctrl queryctrl;
     struct v4l2_querymenu querymenu;
+    unsigned int i_mid;
+
+    int i;
+    int i_val;
+
+    char *psz_name;
+    vlc_value_t val, val2;
+
+    if( queryctrl.flags & V4L2_CTRL_FLAG_GRABBED )
+        msg_Dbg( p_demux, "    control is busy" );
+    if( queryctrl.flags & V4L2_CTRL_FLAG_READ_ONLY )
+        msg_Dbg( p_demux, "    control is read-only" );
+
+    for( i = 0; controls[i].psz_name != NULL; i++ )
+        if( controls[i].i_cid == queryctrl.id ) break;
+
+    if( controls[i].psz_name )
+    {
+        psz_name = strdup( controls[i].psz_name );
+        char psz_cfg_name[40];
+        sprintf( psz_cfg_name, CFG_PREFIX "%s", psz_name );
+        i_val = var_CreateGetInteger( p_demux, psz_cfg_name );
+        var_Destroy( p_demux, psz_cfg_name );
+    }
+    else
+    {
+        char *psz_buf;
+        psz_name = strdup( (const char *)queryctrl.name );
+        for( psz_buf = psz_name; *psz_buf; psz_buf++ )
+        {
+            if( *psz_buf == ' ' ) *psz_buf = '-';
+        }
+        i_val = -1;
+    }
+
+    switch( queryctrl.type )
+    {
+        case V4L2_CTRL_TYPE_INTEGER:
+            msg_Dbg( p_demux, "    integer control" );
+            msg_Dbg( p_demux,
+                     "    valid values: %d to %d by steps of %d",
+                     queryctrl.minimum, queryctrl.maximum,
+                     queryctrl.step );
+
+            var_Create( p_demux, psz_name,
+                        VLC_VAR_INTEGER | VLC_VAR_HASMIN | VLC_VAR_HASMAX
+                      | VLC_VAR_HASSTEP | VLC_VAR_ISCOMMAND );
+            val.i_int = queryctrl.minimum;
+            var_Change( p_demux, psz_name, VLC_VAR_SETMIN, &val, NULL );
+            val.i_int = queryctrl.maximum;
+            var_Change( p_demux, psz_name, VLC_VAR_SETMAX, &val, NULL );
+            val.i_int = queryctrl.step;
+            var_Change( p_demux, psz_name, VLC_VAR_SETSTEP, &val, NULL );
+            break;
+        case V4L2_CTRL_TYPE_BOOLEAN:
+            msg_Dbg( p_demux, "    boolean control" );
+            var_Create( p_demux, psz_name,
+                        VLC_VAR_BOOL | VLC_VAR_ISCOMMAND );
+            break;
+        case V4L2_CTRL_TYPE_MENU:
+            msg_Dbg( p_demux, "    menu control" );
+            var_Create( p_demux, psz_name,
+                        VLC_VAR_INTEGER | VLC_VAR_HASCHOICE
+                      | VLC_VAR_ISCOMMAND );
+            memset( &querymenu, 0, sizeof( querymenu ) );
+            for( i_mid = queryctrl.minimum;
+                 i_mid <= (unsigned)queryctrl.maximum;
+                 i_mid++ )
+            {
+                querymenu.index = i_mid;
+                querymenu.id = queryctrl.id;
+                if( ioctl( i_fd, VIDIOC_QUERYMENU, &querymenu ) >= 0 )
+                {
+                    msg_Dbg( p_demux, "        %d: %s",
+                             querymenu.index, querymenu.name );
+                    val.i_int = querymenu.index;
+                    val2.psz_string = (char *)querymenu.name;
+                    var_Change( p_demux, psz_name,
+                                VLC_VAR_ADDCHOICE, &val, &val2 );
+                }
+            }
+            break;
+        case V4L2_CTRL_TYPE_BUTTON:
+            msg_Dbg( p_demux, "    button control" );
+            var_Create( p_demux, psz_name,
+                        VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
+            break;
+        default:
+            msg_Dbg( p_demux, "    unknown control type (FIXME)" );
+            /* FIXME */
+            break;
+    }
+
+    switch( queryctrl.type )
+    {
+        case V4L2_CTRL_TYPE_INTEGER:
+        case V4L2_CTRL_TYPE_BOOLEAN:
+        case V4L2_CTRL_TYPE_MENU:
+            {
+                struct v4l2_control control;
+                msg_Dbg( p_demux, "    default value: %d",
+                         queryctrl.default_value );
+                memset( &control, 0, sizeof( control ) );
+                control.id = queryctrl.id;
+                if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
+                {
+                    msg_Dbg( p_demux, "    current value: %d", control.value );
+                }
+                if( i_val == -1 )
+                {
+                    i_val = control.value;
+                    if( b_reset && queryctrl.default_value != control.value )
+                    {
+                        msg_Dbg( p_demux, "    reset value to default" );
+                        VideoControl( p_demux, i_fd, psz_name,
+                                      queryctrl.id, queryctrl.default_value );
+                    }
+                }
+                else
+                {
+                    VideoControl( p_demux, i_fd, psz_name,
+                                  queryctrl.id, i_val );
+                }
+            }
+            break;
+        default:
+            break;
+    }
+
+    val.psz_string = (char *)queryctrl.name;
+    var_Change( p_demux, psz_name, VLC_VAR_SETTEXT, &val, NULL );
+    val.i_int = queryctrl.id;
+    val2.psz_string = (char *)psz_name;
+    var_Change( p_demux, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
+
+    switch( var_Type( p_demux, psz_name ) & VLC_VAR_TYPE )
+    {
+        case VLC_VAR_BOOL:
+            var_SetBool( p_demux, psz_name, i_val );
+            break;
+        case VLC_VAR_INTEGER:
+            var_SetInteger( p_demux, psz_name, i_val );
+            break;
+        case VLC_VAR_VOID:
+            break;
+        default:
+            msg_Warn( p_demux, "FIXME: %s %s %d", __FILE__, __func__,
+                      __LINE__ );
+            break;
+    }
+
+    var_AddCallback( p_demux, psz_name,
+                    VideoControlCallback, (void*)queryctrl.id );
+
+    free( psz_name );
+}
+
+static int VideoControlList( demux_t *p_demux, int i_fd, vlc_bool_t b_reset )
+{
+    struct v4l2_queryctrl queryctrl;
+    int i_cid;
 
     memset( &queryctrl, 0, sizeof( queryctrl ) );
 
+    /* A list of available controls (aka the variable name) will be
+     * stored as choices in the "controls" variable. We'll thus be able
+     * to use those to create an appropriate interface */
+    var_Create( p_demux, "controls", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
+
     /* List public controls */
-    for( queryctrl.id = V4L2_CID_BASE;
-         queryctrl.id < V4L2_CID_LASTP1;
-         queryctrl.id ++ )
+    for( i_cid = V4L2_CID_BASE;
+         i_cid < V4L2_CID_LASTP1;
+         i_cid ++ )
     {
+        queryctrl.id = i_cid;
         if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
         {
             if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
                 continue;
             msg_Dbg( p_demux, "Available control: %s (%x)",
                      queryctrl.name, queryctrl.id );
-            if( queryctrl.flags & V4L2_CTRL_FLAG_GRABBED )
-                msg_Dbg( p_demux, "    control is busy" );
-            if( queryctrl.flags & V4L2_CTRL_FLAG_READ_ONLY )
-                msg_Dbg( p_demux, "    control is read-only" );
-            switch( queryctrl.type )
-            {
-                case V4L2_CTRL_TYPE_INTEGER:
-                    msg_Dbg( p_demux, "    integer control" );
-                    msg_Dbg( p_demux,
-                             "    valid values: %d to %d by steps of %d",
-                             queryctrl.minimum, queryctrl.maximum,
-                             queryctrl.step );
-                    msg_Dbg( p_demux, "    default value: %d",
-                             queryctrl.default_value );
-                    break;
-                case V4L2_CTRL_TYPE_BOOLEAN:
-                    msg_Dbg( p_demux, "    boolean control" );
-                    msg_Dbg( p_demux, "    default value: %d",
-                             queryctrl.default_value );
-                    break;
-                case V4L2_CTRL_TYPE_MENU:
-                    msg_Dbg( p_demux, "    menu control" );
-                    memset( &querymenu, 0, sizeof( querymenu ) );
-                    for( querymenu.index = queryctrl.minimum;
-                         querymenu.index <= (unsigned)queryctrl.maximum;
-                         querymenu.index++ )
-                    {
-                        if( ioctl( i_fd, VIDIOC_QUERYMENU, &querymenu ) >= 0 )
-                        {
-                            msg_Dbg( p_demux, "        %d: %s",
-                                     querymenu.index, querymenu.name );
-                        }
-                    }
-                    msg_Dbg( p_demux, "    default value: %d",
-                             queryctrl.default_value );
-                    break;
-                case V4L2_CTRL_TYPE_BUTTON:
-                    msg_Dbg( p_demux, "    button control" );
-                    break;
-                default:
-                    msg_Dbg( p_demux, "    unknown control type (FIXME)" );
-                    /* FIXME */
-                    break;
-            }
+            VideoControlListPrint( p_demux, i_fd, queryctrl, b_reset );
         }
     }
 
     /* List private controls */
-    for( queryctrl.id = V4L2_CID_PRIVATE_BASE;
+    for( i_cid = V4L2_CID_PRIVATE_BASE;
          ;
-         queryctrl.id ++ )
+         i_cid ++ )
     {
+        queryctrl.id = i_cid;
         if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
         {
             if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
                 continue;
-            msg_Dbg( p_demux, "Available private control: %s",
-                     queryctrl.name );
+            msg_Dbg( p_demux, "Available private control: %s (%x)",
+                     queryctrl.name, queryctrl.id );
+            VideoControlListPrint( p_demux, i_fd, queryctrl, b_reset );
         }
         else
             break;
@@ -2369,22 +2541,23 @@ static int VideoControlList( demux_t *p_demux, int i_fd )
  * Issue video controls
  *****************************************************************************/
 static int VideoControl( demux_t *p_demux, int i_fd,
-                         const char *psz_label, int i_cid, int i_value )
+                         const char *psz_name, int i_cid, int i_value )
 {
     struct v4l2_queryctrl queryctrl;
     struct v4l2_control control;
-    memset( &queryctrl, 0, sizeof( queryctrl ) );
 
     if( i_value == -1 )
         return VLC_SUCCESS;
 
+    memset( &queryctrl, 0, sizeof( queryctrl ) );
+
     queryctrl.id = i_cid;
 
     if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) < 0
         || queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
     {
-        msg_Err( p_demux, "%s (%x) control is not supported.", psz_label,
-                 i_value );
+        msg_Dbg( p_demux, "%s (%x) control is not supported.", psz_name,
+                 i_cid );
         return VLC_EGENERIC;
     }
 
@@ -2396,14 +2569,26 @@ static int VideoControl( demux_t *p_demux, int i_fd,
         control.value = i_value;
         if( ioctl( i_fd, VIDIOC_S_CTRL, &control ) < 0 )
         {
-            msg_Err( p_demux, "unable to set %s to %d (%m)", psz_label,
+            msg_Err( p_demux, "unable to set %s to %d (%m)", psz_name,
                      i_value );
             return VLC_EGENERIC;
         }
     }
     if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
     {
-        msg_Dbg( p_demux, "video %s: %d", psz_label, control.value );
+        vlc_value_t val;
+        msg_Dbg( p_demux, "video %s: %d", psz_name, control.value );
+        switch( var_Type( p_demux, psz_name ) & VLC_VAR_TYPE )
+        {
+            case VLC_VAR_BOOL:
+                val.b_bool = control.value;
+                var_Change( p_demux, psz_name, VLC_VAR_SETVALUE, &val, NULL );
+                break;
+            case VLC_VAR_INTEGER:
+                val.i_int = control.value;
+                var_Change( p_demux, psz_name, VLC_VAR_SETVALUE, &val, NULL );
+                break;
+        }
     }
     return VLC_SUCCESS;
 }
@@ -2417,36 +2602,14 @@ static int VideoControlCallback( vlc_object_t *p_this,
 {
     demux_t *p_demux = (demux_t*)p_this;
     demux_sys_t *p_sys = p_demux->p_sys;
+    int i_cid = (int)p_data;
 
     int i_fd = p_sys->i_fd_video;
 
     if( i_fd < 0 )
         return VLC_EGENERIC;
 
-    if( !strcmp( psz_var, "v4l2-brightness" ) )
-    {
-        p_sys->i_brightness = newval.i_int;
-        return VideoControl( p_demux, i_fd,
-                    "brightness", V4L2_CID_BRIGHTNESS, p_sys->i_brightness );
-    }
-    else if( !strcmp( psz_var, "v4l2-contrast" ) )
-    {
-        p_sys->i_contrast = newval.i_int;
-        return VideoControl( p_demux, i_fd,
-                    "contrast", V4L2_CID_CONTRAST, p_sys->i_contrast );
-    }
-    else if( !strcmp( psz_var, "v4l2-saturation" ) )
-    {
-        p_sys->i_saturation = newval.i_int;
-        return VideoControl( p_demux, i_fd,
-                    "saturation", V4L2_CID_SATURATION, p_sys->i_saturation );
-    }
-    else if( !strcmp( psz_var, "v4l2-hue" ) )
-    {
-        p_sys->i_hue = newval.i_int;
-        return VideoControl( p_demux, i_fd,
-                    "hue", V4L2_CID_HUE, p_sys->i_hue );
-    }
+    VideoControl( p_demux, i_fd, psz_var, i_cid, newval.i_int );
 
     return VLC_EGENERIC;
 }