]> git.sesse.net Git - vlc/blobdiff - modules/access/v4l2.c
Update LGPL license blurb, choosing v2.1+.
[vlc] / modules / access / v4l2.c
index be2690f57dab867c77c835e0b9fb2b5b0e4359f1..a20b79dc9f82b8089c8a03bf581a2ba412d21387 100644 (file)
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_access.h>
+#include <vlc_charset.h>
+#include <vlc_fs.h>
 #include <vlc_demux.h>
 #include <vlc_input.h>
-#include <vlc_vout.h>
 
 #include <ctype.h>
+#include <errno.h>
 #include <fcntl.h>
-#include <unistd.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
 
-#include <linux/videodev2.h>
-
-#include <sys/soundcard.h>
-
-#ifdef HAVE_ALSA
-#   define ALSA_PCM_NEW_HW_PARAMS_API
-#   define ALSA_PCM_NEW_SW_PARAMS_API
-#   include <alsa/asoundlib.h>
+#if defined(HAVE_LINUX_VIDEODEV2_H)
+#   include <linux/videodev2.h>
+#elif defined(HAVE_SYS_VIDEOIO_H)
+#   include <sys/videoio.h>
+#else
+#   error "No Video4Linux2 headers found."
 #endif
 
 #include <poll.h>
 
 #ifdef HAVE_LIBV4L2
 #   include <libv4l2.h>
-#else
-#   define v4l2_close close
-#   define v4l2_dup dup
-#   define v4l2_ioctl ioctl
-#   define v4l2_read read
-#   define v4l2_mmap mmap
-#   define v4l2_munmap munmap
 #endif
 
 /*****************************************************************************
@@ -102,13 +94,19 @@ static void AccessClose( vlc_object_t * );
     "IO Method (READ, MMAP, USERPTR)." )
 #define WIDTH_TEXT N_( "Width" )
 #define WIDTH_LONGTEXT N_( \
-    "Force width (-1 for autodetect)." )
+    "Force width (-1 for autodetect, 0 for driver default)." )
 #define HEIGHT_TEXT N_( "Height" )
 #define HEIGHT_LONGTEXT N_( \
-    "Force height (-1 for autodetect)." )
+    "Force height (-1 for autodetect, 0 for driver default)." )
 #define FPS_TEXT N_( "Framerate" )
 #define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
-    "(-1 for autodetect)." )
+    "(0 for autodetect)." )
+
+#ifdef HAVE_LIBV4L2
+#define LIBV4L2_TEXT N_( "Use libv4l2" )
+#define LIBV4L2_LONGTEXT N_( \
+    "Force usage of the libv4l2 wrapper." )
+#endif
 
 #define CTRL_RESET_TEXT N_( "Reset v4l2 controls" )
 #define CTRL_RESET_LONGTEXT N_( \
@@ -211,24 +209,45 @@ static void AccessClose( vlc_object_t * );
 
 #define AUDIO_DEPRECATED_ERROR N_( \
     "Alsa or OSS audio capture in the v4l2 access is deprecated. " \
-    "please use 'v4l2:/""/ --input-slave alsa:/""/' or " \
-    "'v4l2:/""/ --input-slave oss:/""/' instead." )
+    "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_AUTO,
     IO_METHOD_READ,
     IO_METHOD_MMAP,
     IO_METHOD_USERPTR,
 } io_method;
 
 static const int i_standards_list[] =
-    { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC };
+    { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC,
+      V4L2_STD_PAL_B, V4L2_STD_PAL_B1, V4L2_STD_PAL_G, V4L2_STD_PAL_H,
+      V4L2_STD_PAL_I, V4L2_STD_PAL_D, V4L2_STD_PAL_D1, V4L2_STD_PAL_K,
+      V4L2_STD_PAL_M, V4L2_STD_PAL_N, V4L2_STD_PAL_Nc, V4L2_STD_PAL_60,
+      V4L2_STD_NTSC_M, V4L2_STD_NTSC_M_JP, V4L2_STD_NTSC_443,
+      V4L2_STD_NTSC_M_KR,
+      V4L2_STD_SECAM_B, V4L2_STD_SECAM_D, V4L2_STD_SECAM_G,
+      V4L2_STD_SECAM_H, V4L2_STD_SECAM_K, V4L2_STD_SECAM_K1,
+      V4L2_STD_SECAM_L, V4L2_STD_SECAM_LC,
+      V4L2_STD_ATSC_8_VSB, V4L2_STD_ATSC_16_VSB,
+      };
 static const char *const psz_standards_list_text[] =
-    { N_("Default"), N_("SECAM"), N_("PAL"),  N_("NTSC") };
+    { N_("Default"), "SECAM", "PAL",  "NTSC",
+      "PAL_B", "PAL_B1", "PAL_G", "PAL_H", "PAL_I", "PAL_D",
+      "PAL_D1", "PAL_K", "PAL_M", "PAL_N", "PAL_Nc", "PAL_60",
+      "NTSC_M", "NTSC_M_JP", "NTSC_443", "NTSC_M_KR",
+      "SECAM_B", "SECAM_D", "SECAM_G", "SECAM_H", "SECAM_K",
+      "SECAM_K1", "SECAM_L", "SECAM_LC",
+      "ATSC_8_VSB", "ATSC_16_VSB"
+    };
 
 static const int i_iomethod_list[] =
-    { IO_METHOD_READ, IO_METHOD_MMAP, IO_METHOD_USERPTR };
+    { IO_METHOD_AUTO, IO_METHOD_READ, IO_METHOD_MMAP, IO_METHOD_USERPTR };
 static const char *const psz_iomethod_list_text[] =
-    { N_("READ"), N_("MMAP"),  N_("USERPTR") };
+    { N_("AUTO"), N_("READ"), N_("MMAP"),  N_("USERPTR") };
 
 static const int i_tuner_audio_modes_list[] =
     { V4L2_TUNER_MODE_MONO, V4L2_TUNER_MODE_STEREO,
@@ -245,6 +264,16 @@ static const char *const psz_tuner_audio_modes_list_text[] =
 #define V4L2_DEFAULT "/dev/video0"
 #define CFG_PREFIX "v4l2-"
 
+#ifdef HAVE_MAEMO
+# define DEFAULT_WIDTH 640
+# define DEFAULT_HEIGHT        492
+#endif
+
+#ifndef DEFAULT_WIDTH
+# define DEFAULT_WIDTH (-1)
+# define DEFAULT_HEIGHT        (-1)
+#endif
+
 vlc_module_begin ()
     set_shortname( N_("Video4Linux2") )
     set_description( N_("Video4Linux2 input") )
@@ -253,101 +282,106 @@ vlc_module_begin ()
 
     set_section( N_( "Video input" ), NULL )
     add_integer( CFG_PREFIX "standard", 0, NULL, STANDARD_TEXT,
-                 STANDARD_LONGTEXT, false );
-        change_integer_list( i_standards_list, psz_standards_list_text, NULL );
+                 STANDARD_LONGTEXT, false )
+        change_integer_list( i_standards_list, psz_standards_list_text, NULL )
     add_string( CFG_PREFIX "chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
-                true );
+                true )
     add_integer( CFG_PREFIX "input", 0, NULL, INPUT_TEXT, INPUT_LONGTEXT,
-                true );
+                true )
     add_integer( CFG_PREFIX "audio-input", 0, NULL, AUDIO_INPUT_TEXT,
-                 AUDIO_INPUT_LONGTEXT, true );
-    add_integer( CFG_PREFIX "io", IO_METHOD_MMAP, NULL, IOMETHOD_TEXT,
-                 IOMETHOD_LONGTEXT, true );
-        change_integer_list( i_iomethod_list, psz_iomethod_list_text, NULL );
-    add_integer( CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
-                WIDTH_LONGTEXT, true );
-    add_integer( CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
-                HEIGHT_LONGTEXT, true );
+                 AUDIO_INPUT_LONGTEXT, true )
+    add_integer( CFG_PREFIX "io", IO_METHOD_AUTO, NULL, IOMETHOD_TEXT,
+                 IOMETHOD_LONGTEXT, true )
+        change_integer_list( i_iomethod_list, psz_iomethod_list_text, NULL )
+    add_integer( CFG_PREFIX "width", DEFAULT_WIDTH, NULL, WIDTH_TEXT,
+                WIDTH_LONGTEXT, true )
+    add_integer( CFG_PREFIX "height", DEFAULT_HEIGHT, 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 );
+    add_integer( CFG_PREFIX "caching", 50 /* ms */, NULL,
+                CACHING_TEXT, CACHING_LONGTEXT, true )
+#ifdef HAVE_LIBV4L2
+    add_bool( CFG_PREFIX "use-libv4l2", false, NULL, LIBV4L2_TEXT, LIBV4L2_LONGTEXT, true );
+#endif
 
     set_section( N_( "Tuner" ), NULL )
     add_integer( CFG_PREFIX "tuner", 0, NULL, TUNER_TEXT, TUNER_LONGTEXT,
-                 true );
+                 true )
     add_integer( CFG_PREFIX "tuner-frequency", -1, NULL, FREQUENCY_TEXT,
-                 FREQUENCY_LONGTEXT, true );
+                 FREQUENCY_LONGTEXT, true )
     add_integer( CFG_PREFIX "tuner-audio-mode", -1, NULL, TUNER_AUDIO_MODE_TEXT,
-                 TUNER_AUDIO_MODE_LONGTEXT, true );
+                 TUNER_AUDIO_MODE_LONGTEXT, true )
         change_integer_list( i_tuner_audio_modes_list,
-                             psz_tuner_audio_modes_list_text, 0 );
+                             psz_tuner_audio_modes_list_text, 0 )
 
     set_section( N_( "Controls" ),
-                 N_( "v4l2 driver controls, if supported by your v4l2 driver." ) );
+                 N_( "v4l2 driver controls, if supported by your v4l2 driver." ) )
     add_bool( CFG_PREFIX "controls-reset", false, NULL, CTRL_RESET_TEXT,
-              CTRL_RESET_LONGTEXT, true );
+              CTRL_RESET_LONGTEXT, true )
     add_integer( CFG_PREFIX "brightness", -1, NULL, BRIGHTNESS_TEXT,
-                 BRIGHTNESS_LONGTEXT, true );
+                 BRIGHTNESS_LONGTEXT, true )
     add_integer( CFG_PREFIX "contrast", -1, NULL, CONTRAST_TEXT,
-                 CONTRAST_LONGTEXT, true );
+                 CONTRAST_LONGTEXT, true )
     add_integer( CFG_PREFIX "saturation", -1, NULL, SATURATION_TEXT,
-                 SATURATION_LONGTEXT, true );
+                 SATURATION_LONGTEXT, true )
     add_integer( CFG_PREFIX "hue", -1, NULL, HUE_TEXT,
-                 HUE_LONGTEXT, true );
+                 HUE_LONGTEXT, true )
     add_integer( CFG_PREFIX "black-level", -1, NULL, BLACKLEVEL_TEXT,
-                 BLACKLEVEL_LONGTEXT, true );
+                 BLACKLEVEL_LONGTEXT, true )
     add_integer( CFG_PREFIX "auto-white-balance", -1, NULL,
-                 AUTOWHITEBALANCE_TEXT, AUTOWHITEBALANCE_LONGTEXT, true );
+                 AUTOWHITEBALANCE_TEXT, AUTOWHITEBALANCE_LONGTEXT, true )
     add_integer( CFG_PREFIX "do-white-balance", -1, NULL, DOWHITEBALANCE_TEXT,
-                 DOWHITEBALANCE_LONGTEXT, true );
+                 DOWHITEBALANCE_LONGTEXT, true )
     add_integer( CFG_PREFIX "red-balance", -1, NULL, REDBALANCE_TEXT,
-                 REDBALANCE_LONGTEXT, true );
+                 REDBALANCE_LONGTEXT, true )
     add_integer( CFG_PREFIX "blue-balance", -1, NULL, BLUEBALANCE_TEXT,
-                 BLUEBALANCE_LONGTEXT, true );
+                 BLUEBALANCE_LONGTEXT, true )
     add_integer( CFG_PREFIX "gamma", -1, NULL, GAMMA_TEXT,
-                 GAMMA_LONGTEXT, true );
+                 GAMMA_LONGTEXT, true )
     add_integer( CFG_PREFIX "exposure", -1, NULL, EXPOSURE_TEXT,
-                 EXPOSURE_LONGTEXT, true );
+                 EXPOSURE_LONGTEXT, true )
     add_integer( CFG_PREFIX "autogain", -1, NULL, AUTOGAIN_TEXT,
-                 AUTOGAIN_LONGTEXT, true );
+                 AUTOGAIN_LONGTEXT, true )
     add_integer( CFG_PREFIX "gain", -1, NULL, GAIN_TEXT,
-                 GAIN_LONGTEXT, true );
+                 GAIN_LONGTEXT, true )
     add_integer( CFG_PREFIX "hflip", -1, NULL, HFLIP_TEXT,
-                 HFLIP_LONGTEXT, true );
+                 HFLIP_LONGTEXT, true )
     add_integer( CFG_PREFIX "vflip", -1, NULL, VFLIP_TEXT,
-                 VFLIP_LONGTEXT, true );
+                 VFLIP_LONGTEXT, true )
     add_integer( CFG_PREFIX "hcenter", -1, NULL, HCENTER_TEXT,
-                 HCENTER_LONGTEXT, true );
+                 HCENTER_LONGTEXT, true )
     add_integer( CFG_PREFIX "vcenter", -1, NULL, VCENTER_TEXT,
-                 VCENTER_LONGTEXT, true );
+                 VCENTER_LONGTEXT, true )
     add_integer( CFG_PREFIX "audio-volume", -1, NULL, AUDIO_VOLUME_TEXT,
-                AUDIO_VOLUME_LONGTEXT, true );
+                AUDIO_VOLUME_LONGTEXT, true )
     add_integer( CFG_PREFIX "audio-balance", -1, NULL, AUDIO_BALANCE_TEXT,
-                AUDIO_BALANCE_LONGTEXT, true );
+                AUDIO_BALANCE_LONGTEXT, true )
     add_bool( CFG_PREFIX "audio-mute", false, NULL, AUDIO_MUTE_TEXT,
-              AUDIO_MUTE_LONGTEXT, true );
+              AUDIO_MUTE_LONGTEXT, true )
     add_integer( CFG_PREFIX "audio-bass", -1, NULL, AUDIO_BASS_TEXT,
-                AUDIO_BASS_LONGTEXT, true );
+                AUDIO_BASS_LONGTEXT, true )
     add_integer( CFG_PREFIX "audio-treble", -1, NULL, AUDIO_TREBLE_TEXT,
-                AUDIO_TREBLE_LONGTEXT, true );
+                AUDIO_TREBLE_LONGTEXT, true )
     add_integer( CFG_PREFIX "audio-loudness", -1, NULL, AUDIO_LOUDNESS_TEXT,
-                AUDIO_LOUDNESS_LONGTEXT, true );
+                AUDIO_LOUDNESS_LONGTEXT, true )
     add_string( CFG_PREFIX "set-ctrls", NULL, NULL, S_CTRLS_TEXT,
-              S_CTRLS_LONGTEXT, true );
+              S_CTRLS_LONGTEXT, true )
 
-    add_obsolete_string( CFG_PREFIX "dev" );
+    add_obsolete_string( CFG_PREFIX "dev" )
 
-    add_obsolete_string( CFG_PREFIX "adev" );
-    add_obsolete_integer( CFG_PREFIX "audio-method" );
-    add_obsolete_bool( CFG_PREFIX "stereo" );
-    add_obsolete_integer( CFG_PREFIX "samplerate" );
+    add_obsolete_string( CFG_PREFIX "adev" )
+    add_obsolete_integer( CFG_PREFIX "audio-method" )
+    add_obsolete_bool( CFG_PREFIX "stereo" )
+    add_obsolete_integer( CFG_PREFIX "samplerate" )
 
     add_shortcut( "v4l2" )
     set_capability( "access_demux", 10 )
     set_callbacks( DemuxOpen, DemuxClose )
 
     add_submodule ()
-    add_shortcut( "v4l2c" )
+    add_shortcut( "v4l2", "v4l2c" )
     set_description( N_("Video4Linux2 Compressed A/V") )
     set_capability( "access", 0 )
     /* use these when open as access_demux fails; VLC will use another demux */
@@ -368,10 +402,11 @@ 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 ssize_t AccessReadStream( access_t * p_access, uint8_t * p_buffer, size_t i_len );
 
-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 );
@@ -380,8 +415,8 @@ static int OpenVideoDev( vlc_object_t *, demux_sys_t *, bool );
 static bool ProbeVideoDev( vlc_object_t *, demux_sys_t *,
                                  const char *psz_device );
 
-static int ControlList( vlc_object_t *, int , bool, bool );
-static int Control( vlc_object_t *, int i_fd,
+static int ControlList( vlc_object_t *, demux_sys_t *, int , bool, bool );
+static int Control( vlc_object_t *, demux_sys_t *, int i_fd,
                     const char *psz_name, int i_cid, int i_value );
 
 static int DemuxControlCallback( vlc_object_t *p_this, const char *psz_var,
@@ -400,44 +435,43 @@ 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;
 } v4l2chroma_to_fourcc[] =
 {
     /* Raw data types */
-    { V4L2_PIX_FMT_GREY,    VLC_FOURCC('G','R','E','Y'), 0, 0, 0 },
+    { V4L2_PIX_FMT_GREY,    VLC_CODEC_GREY, 0, 0, 0 },
     { V4L2_PIX_FMT_HI240,   VLC_FOURCC('I','2','4','0'), 0, 0, 0 },
-    { V4L2_PIX_FMT_RGB555,  VLC_FOURCC('R','V','1','5'), 0x001f,0x03e0,0x7c00 },
-    { V4L2_PIX_FMT_RGB565,  VLC_FOURCC('R','V','1','6'), 0x001f,0x07e0,0xf800 },
+    { V4L2_PIX_FMT_RGB555,  VLC_CODEC_RGB15, 0x001f,0x03e0,0x7c00 },
+    { V4L2_PIX_FMT_RGB565,  VLC_CODEC_RGB16, 0x001f,0x07e0,0xf800 },
     /* Won't work since we don't know how to handle such gmask values
      * correctly
-    { V4L2_PIX_FMT_RGB555X, VLC_FOURCC('R','V','1','5'), 0x007c,0xe003,0x1f00 },
-    { V4L2_PIX_FMT_RGB565X, VLC_FOURCC('R','V','1','6'), 0x00f8,0xe007,0x1f00 },
+    { V4L2_PIX_FMT_RGB555X, VLC_CODEC_RGB15, 0x007c,0xe003,0x1f00 },
+    { V4L2_PIX_FMT_RGB565X, VLC_CODEC_RGB16, 0x00f8,0xe007,0x1f00 },
     */
-    { V4L2_PIX_FMT_BGR24,   VLC_FOURCC('R','V','2','4'), 0xff0000,0xff00,0xff },
-    { V4L2_PIX_FMT_RGB24,   VLC_FOURCC('R','V','2','4'), 0xff,0xff00,0xff0000 },
-    { V4L2_PIX_FMT_BGR32,   VLC_FOURCC('R','V','3','2'), 0xff0000,0xff00,0xff },
-    { V4L2_PIX_FMT_RGB32,   VLC_FOURCC('R','V','3','2'), 0xff,0xff00,0xff0000 },
-    { V4L2_PIX_FMT_YUYV,    VLC_FOURCC('Y','U','Y','2'), 0, 0, 0 },
-    { V4L2_PIX_FMT_YUYV,    VLC_FOURCC('Y','U','Y','V'), 0, 0, 0 },
-    { V4L2_PIX_FMT_UYVY,    VLC_FOURCC('U','Y','V','Y'), 0, 0, 0 },
+    { V4L2_PIX_FMT_BGR24,   VLC_CODEC_RGB24, 0xff0000,0xff00,0xff },
+    { V4L2_PIX_FMT_RGB24,   VLC_CODEC_RGB24, 0xff,0xff00,0xff0000 },
+    { V4L2_PIX_FMT_BGR32,   VLC_CODEC_RGB32, 0xff0000,0xff00,0xff },
+    { V4L2_PIX_FMT_RGB32,   VLC_CODEC_RGB32, 0xff,0xff00,0xff0000 },
+    { V4L2_PIX_FMT_YUYV,    VLC_CODEC_YUYV, 0, 0, 0 },
+    { V4L2_PIX_FMT_UYVY,    VLC_CODEC_UYVY, 0, 0, 0 },
     { V4L2_PIX_FMT_Y41P,    VLC_FOURCC('I','4','1','N'), 0, 0, 0 },
-    { V4L2_PIX_FMT_YUV422P, VLC_FOURCC('I','4','2','2'), 0, 0, 0 },
-    { V4L2_PIX_FMT_YVU420,  VLC_FOURCC('Y','V','1','2'), 0, 0, 0 },
-    { V4L2_PIX_FMT_YUV411P, VLC_FOURCC('I','4','1','1'), 0, 0, 0 },
-    { V4L2_PIX_FMT_YUV410,  VLC_FOURCC('I','4','1','0'), 0, 0, 0 },
+    { V4L2_PIX_FMT_YUV422P, VLC_CODEC_I422, 0, 0, 0 },
+    { V4L2_PIX_FMT_YVU420,  VLC_CODEC_YV12, 0, 0, 0 },
+    { V4L2_PIX_FMT_YUV411P, VLC_CODEC_I411, 0, 0, 0 },
+    { V4L2_PIX_FMT_YUV410,  VLC_CODEC_I410, 0, 0, 0 },
 
     /* Raw data types, not in V4L2 spec but still in videodev2.h and supported
      * by VLC */
-    { V4L2_PIX_FMT_YUV420,  VLC_FOURCC('I','4','2','0'), 0, 0, 0 },
-    /* FIXME { V4L2_PIX_FMT_RGB444,  VLC_FOURCC('R','V','3','2') }, */
+    { V4L2_PIX_FMT_YUV420,  VLC_CODEC_I420, 0, 0, 0 },
+    /* FIXME { V4L2_PIX_FMT_RGB444,  VLC_CODEC_RGB32 }, */
 
     /* Compressed data types */
-    { V4L2_PIX_FMT_MJPEG,   VLC_FOURCC('M','J','P','G'), 0, 0, 0 },
+    { 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
@@ -450,10 +484,10 @@ static const struct
  *
  * Try YUV chromas first, then RGB little endian and MJPEG as last resort.
  */
-static const __u32 p_chroma_fallbacks[] =
+static const uint32_t 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
 {
@@ -531,9 +565,11 @@ 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;
+    uint32_t i_block_flags;
 
     es_out_id_t *p_es;
 
@@ -544,8 +580,63 @@ struct demux_sys_t
 
     /* Controls */
     char *psz_set_ctrls;
+
+
+#ifdef HAVE_LIBV4L2
+    /* */
+    int (*pf_open)(const char *, int, ...);
+    int (*pf_close)( int );
+    int (*pf_dup)( int );
+    int (*pf_ioctl)( int, unsigned long int, ... );
+    ssize_t (*pf_read)( int, void *, size_t );
+    void *(*pf_mmap)( void *, size_t, int, int, int, off_t );
+    int (*pf_munmap)( void *, size_t );
+    bool b_libv4l2;
+#endif
 };
 
+#ifdef HAVE_LIBV4L2
+static void use_kernel_v4l2( demux_sys_t *p_sys )
+{
+    p_sys->pf_open = vlc_open;
+    p_sys->pf_close = close;
+    p_sys->pf_dup = dup;
+    p_sys->pf_ioctl = ioctl;
+    p_sys->pf_read = read;
+    p_sys->pf_mmap = mmap;
+    p_sys->pf_munmap = munmap;
+    p_sys->b_libv4l2 = false;
+}
+
+static void use_libv4l2( demux_sys_t *p_sys )
+{
+    p_sys->pf_open = v4l2_open;
+    p_sys->pf_close = v4l2_close;
+    p_sys->pf_dup = v4l2_dup;
+    p_sys->pf_ioctl = v4l2_ioctl;
+    p_sys->pf_read = v4l2_read;
+    p_sys->pf_mmap = v4l2_mmap;
+    p_sys->pf_munmap = v4l2_munmap;
+    p_sys->b_libv4l2 = true;
+}
+
+#   define v4l2_open (p_sys->pf_open)
+#   define v4l2_close (p_sys->pf_close)
+#   define v4l2_dup (p_sys->pf_dup)
+#   define v4l2_ioctl (p_sys->pf_ioctl)
+#   define v4l2_read (p_sys->pf_read)
+#   define v4l2_mmap (p_sys->pf_mmap)
+#   define v4l2_munmap (p_sys->pf_munmap)
+#else
+#   define v4l2_open vlc_open
+#   define v4l2_close close
+#   define v4l2_dup dup
+#   define v4l2_ioctl ioctl
+#   define v4l2_read read
+#   define v4l2_mmap mmap
+#   define v4l2_munmap munmap
+#endif
+
 static int FindMainDevice( vlc_object_t *p_this, demux_sys_t *p_sys,
                            bool b_demux )
 {
@@ -575,7 +666,8 @@ static int DemuxOpen( vlc_object_t *p_this )
     demux_sys_t *p_sys;
 
     /* Only when selected */
-    if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;
+    if( strcmp( p_demux->psz_access, "v4l2" ) )
+        return VLC_EGENERIC;
 
     /* Set up p_demux */
     p_demux->pf_control = DemuxControl;
@@ -589,15 +681,25 @@ static int DemuxOpen( vlc_object_t *p_this )
 
     GetV4L2Params(p_sys, (vlc_object_t *) p_demux);
 
-    ParseMRL( p_sys, p_demux->psz_path, (vlc_object_t *) p_demux );
+    ParseMRL( p_sys, p_demux->psz_location, (vlc_object_t *) p_demux );
 
-    if( FindMainDevice( p_this, p_sys, true ) != VLC_SUCCESS)
+#ifdef HAVE_LIBV4L2
+    if( !var_InheritBool( p_this, CFG_PREFIX "use-libv4l2" ) )
     {
-        DemuxClose( p_this );
-        return VLC_EGENERIC;
+        msg_Dbg( p_this, "Trying direct kernel v4l2" );
+        use_kernel_v4l2( p_sys );
+        if( FindMainDevice( p_this, p_sys, true ) == VLC_SUCCESS)
+            return VLC_SUCCESS;
     }
 
-    return VLC_SUCCESS;
+    msg_Dbg( p_this, "Trying libv4l2 wrapper" );
+    use_libv4l2( p_sys );
+#endif
+    if( FindMainDevice( p_this, p_sys, true ) == VLC_SUCCESS)
+        return VLC_SUCCESS;
+
+    DemuxClose( p_this );
+    return VLC_EGENERIC;
 }
 
 /*****************************************************************************
@@ -632,6 +734,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" );
+    char *psz_delim = !EMPTY_STR(psz_aspect) ? strchr( psz_aspect, ':' ) : NULL;
+    if( psz_delim )
+    {
+        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;
 
@@ -661,30 +776,22 @@ static void ParseMRL( demux_sys_t *p_sys, char *psz_path, vlc_object_t *p_obj )
             if( !strncmp( psz_parser, "standard=", strlen( "standard=" ) ) )
             {
                 psz_parser += strlen( "standard=" );
-                if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
-                {
-                    p_sys->i_selected_standard_id = V4L2_STD_PAL;
-                    psz_parser += strlen( "pal" );
-                }
-                else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
-                {
-                    p_sys->i_selected_standard_id = V4L2_STD_NTSC;
-                    psz_parser += strlen( "ntsc" );
-                }
-                else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
-                {
-                    p_sys->i_selected_standard_id = V4L2_STD_SECAM;
-                    psz_parser += strlen( "secam" );
-                }
-                else if( !strncmp( psz_parser, "default", strlen( "default" ) ) )
+                size_t i;
+                for( i = 0; i < ARRAY_SIZE(psz_standards_list_text); i++ )
                 {
-                    p_sys->i_selected_standard_id = V4L2_STD_UNKNOWN;
-                    psz_parser += strlen( "default" );
+                    const char *psz_value = psz_standards_list_text[i];
+                    size_t i_len = strlen( psz_value );
+                    if( !strncasecmp( psz_parser, psz_value, i_len ) &&
+                        ( psz_parser[i_len] == ':' || psz_parser[i_len] == 0 ) )
+                    {
+                        p_sys->i_selected_standard_id = i_standards_list[i];
+                        psz_parser += i_len;
+                        break;
+                    }
                 }
-                else
-                {
+
+                if( i == ARRAY_SIZE(psz_standards_list_text) )
                     p_sys->i_selected_standard_id = i_standards_list[strtol( psz_parser, &psz_parser, 0 )];
-                }
             }
             else if( !strncmp( psz_parser, "chroma=", strlen( "chroma=" ) ) )
             {
@@ -717,8 +824,8 @@ static void ParseMRL( demux_sys_t *p_sys, char *psz_path, vlc_object_t *p_obj )
             }
             else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
             {
-                p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
-                                       &psz_parser );
+                p_sys->f_fps = us_strtof( psz_parser + strlen( "fps=" ),
+                                          &psz_parser );
             }
             else if( !strncmp( psz_parser, "io=", strlen( "io=" ) ) )
             {
@@ -738,6 +845,11 @@ static void ParseMRL( demux_sys_t *p_sys, char *psz_path, vlc_object_t *p_obj )
                     p_sys->io = IO_METHOD_USERPTR;
                     psz_parser += strlen( "userptr" );
                 }
+                else if( !strncmp( psz_parser, "auto", strlen( "auto" ) ) )
+                {
+                    p_sys->io = IO_METHOD_AUTO;
+                    psz_parser += strlen( "auto" );
+                }
                 else
                 {
                     p_sys->io = strtol( psz_parser, &psz_parser, 0 );
@@ -757,6 +869,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" ) ) )
             {
@@ -935,6 +1058,9 @@ static void DemuxClose( vlc_object_t *p_this )
             }
 
             break;
+
+        default:
+            break;
         }
     }
 
@@ -962,6 +1088,9 @@ static void DemuxClose( vlc_object_t *p_this )
                free( p_sys->p_buffers[i].start );
             }
             break;
+
+        default:
+            break;
         }
         free( p_sys->p_buffers );
     }
@@ -1000,32 +1129,52 @@ static int AccessOpen( vlc_object_t * p_this )
     /* Only when selected */
     if( *p_access->psz_access == '\0' ) return VLC_EGENERIC;
 
-    p_access->pf_read = AccessRead;
-    p_access->pf_block = NULL;
-    p_access->pf_seek = NULL;
-    p_access->pf_control = AccessControl;
-    p_access->info.i_update = 0;
-    p_access->info.i_size = 0;
-    p_access->info.i_pos = 0;
-    p_access->info.b_eof = false;
-    p_access->info.i_title = 0;
-    p_access->info.i_seekpoint = 0;
-
-    p_sys = calloc( 1, sizeof( demux_sys_t ) );
-    p_access->p_sys = (access_sys_t *) p_sys;
-    if( p_sys == NULL ) return VLC_ENOMEM;
+    access_InitFields( p_access );
+    p_sys = calloc( 1, sizeof( demux_sys_t ));
+    if( !p_sys ) return VLC_ENOMEM;
+    p_access->p_sys = (access_sys_t*)p_sys;
 
     GetV4L2Params( p_sys, (vlc_object_t *) p_access );
 
-    ParseMRL( p_sys, p_access->psz_path, (vlc_object_t *) p_access );
+    ParseMRL( p_sys, p_access->psz_location, (vlc_object_t *) p_access );
 
-    if( FindMainDevice( p_this, p_sys, false ) != VLC_SUCCESS )
+#ifdef HAVE_LIBV4L2
+    if( !var_InheritBool( p_this, CFG_PREFIX "use-libv4l2" ) )
     {
-        AccessClose( p_this );
-        return VLC_EGENERIC;
+        msg_Dbg( p_this, "Trying direct kernel v4l2" );
+        use_kernel_v4l2( p_sys );
+        if( FindMainDevice( p_this, p_sys, false ) == VLC_SUCCESS)
+        {
+            if( p_sys->io == IO_METHOD_READ )
+            {
+                ACCESS_SET_CALLBACKS( AccessReadStream, NULL, AccessControl, NULL );
+            }
+            else
+            {
+                ACCESS_SET_CALLBACKS( NULL, AccessRead, AccessControl, NULL );
+            }
+            return VLC_SUCCESS;
+        }
     }
 
-    return VLC_SUCCESS;
+    msg_Dbg( p_this, "Trying libv4l2 wrapper" );
+    use_libv4l2( p_sys );
+#endif
+    if( FindMainDevice( p_this, p_sys, false ) == VLC_SUCCESS )
+    {
+        if( p_sys->io == IO_METHOD_READ )
+        {
+            ACCESS_SET_CALLBACKS( AccessReadStream, NULL, AccessControl, NULL );
+        }
+        else
+        {
+            ACCESS_SET_CALLBACKS( NULL, AccessRead, AccessControl, NULL );
+        }
+        return VLC_SUCCESS;
+    }
+
+    AccessClose( p_this );
+    return VLC_EGENERIC;
 }
 
 /*****************************************************************************
@@ -1033,29 +1182,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 */
@@ -1071,9 +1212,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;
-    int          *pi_int;
-    int64_t      *pi_64;
     demux_sys_t  *p_sys = (demux_sys_t *) p_access->p_sys;
 
     switch( i_query )
@@ -1081,27 +1219,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_MTU:
-            pi_int = (int*)va_arg( args, int * );
-            *pi_int = 0;
-            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;
 
         /* */
@@ -1128,7 +1253,28 @@ 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 fd;
+    fd.fd = p_sys->i_fd;
+    fd.events = POLLIN|POLLPRI;
+    fd.revents = 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. */
+    {
+        if( fd.revents & (POLLIN|POLLPRI) )
+        {
+            return GrabVideo( VLC_OBJECT(p_access), p_sys );
+        }
+    }
+
+    return NULL;
+}
+
+static ssize_t AccessReadStream( access_t * p_access, uint8_t * p_buffer, size_t i_len )
 {
     demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
     struct pollfd ufd;
@@ -1185,7 +1331,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 );
@@ -1200,10 +1346,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;
@@ -1213,7 +1357,8 @@ static block_t* GrabVideo( demux_t *p_demux )
         mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
 
         /* Did we wait long enough ? (frame rate reduction) */
-        if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
+        if( p_sys->i_video_pts + i_dur > mdate() )
+            return NULL;
     }
 
     /* Grab Video Frame */
@@ -1226,7 +1371,7 @@ static block_t* GrabVideo( demux_t *p_demux )
             switch( errno )
             {
             case EAGAIN:
-                return 0;
+                return NULL;
             case EIO:
                 /* Could ignore EIO, see spec. */
                 /* fall through */
@@ -1237,7 +1382,8 @@ static block_t* GrabVideo( demux_t *p_demux )
         }
 
         p_block = ProcessVideoFrame( p_demux, (uint8_t*)p_sys->p_buffers[0].start, i_ret );
-        if( !p_block ) return 0;
+        if( !p_block )
+            return NULL;
 
         break;
 
@@ -1252,30 +1398,31 @@ static block_t* GrabVideo( demux_t *p_demux )
             switch( errno )
             {
             case EAGAIN:
-                return 0;
+                return NULL;
             case EIO:
                 /* Could ignore EIO, see spec. */
                 /* fall through */
             default:
                 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
-                return 0;
+                return NULL;
                }
         }
 
         if( buf.index >= p_sys->i_nbuffers ) {
             msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
-            return 0;
+            return NULL;
         }
 
         p_block = ProcessVideoFrame( p_demux, p_sys->p_buffers[buf.index].start, buf.bytesused );
-        if( !p_block ) return 0;
+        if( !p_block )
+            return NULL;
 
         /* Unlock */
         if( v4l2_ioctl( p_sys->i_fd, VIDIOC_QBUF, &buf ) < 0 )
         {
             msg_Err( p_demux, "Failed to unlock (VIDIOC_QBUF)" );
             block_Release( p_block );
-            return 0;
+            return NULL;
         }
 
         break;
@@ -1291,13 +1438,13 @@ static block_t* GrabVideo( demux_t *p_demux )
             switch( errno )
             {
             case EAGAIN:
-                return 0;
+                return NULL;
             case EIO:
                 /* Could ignore EIO, see spec. */
                 /* fall through */
             default:
                 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
-                return 0;
+                return NULL;
             }
         }
 
@@ -1312,26 +1459,30 @@ static block_t* GrabVideo( demux_t *p_demux )
         if( i >= p_sys->i_nbuffers )
         {
             msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
-            return 0;
+            return NULL;
         }
 
         p_block = ProcessVideoFrame( p_demux, (uint8_t*)buf.m.userptr, buf.bytesused );
-        if( !p_block ) return 0;
+        if( !p_block )
+            return NULL;
 
         /* Unlock */
         if( v4l2_ioctl( p_sys->i_fd, VIDIOC_QBUF, &buf ) < 0 )
         {
             msg_Err( p_demux, "Failed to unlock (VIDIOC_QBUF)" );
             block_Release( p_block );
-            return 0;
+            return NULL;
         }
 
         break;
 
+    default:
+        break;
     }
 
     /* Timestamp */
     p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();
+    p_block->i_flags |= p_sys->i_block_flags;
 
     return p_block;
 }
@@ -1340,17 +1491,17 @@ 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;
 
-    if( !p_frame ) return 0;
+    if( !p_frame ) return NULL;
 
     /* New block */
     if( !( p_block = block_New( p_demux, i_size ) ) )
     {
         msg_Warn( p_demux, "Cannot get new block" );
-        return 0;
+        return NULL;
     }
 
     /* Copy frame */
@@ -1362,9 +1513,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 )
@@ -1381,9 +1532,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) );
@@ -1443,9 +1593,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;
 
@@ -1500,6 +1649,184 @@ static bool IsPixelFormatSupported( demux_t *p_demux, unsigned int i_pixelformat
     return false;
 }
 
+static float GetMaxFrameRate( demux_t *p_demux, int i_fd,
+                              uint32_t i_pixel_format,
+                              uint32_t i_width, uint32_t i_height )
+{
+    (void)p_demux;
+#ifdef VIDIOC_ENUM_FRAMEINTERVALS
+#ifdef HAVE_LIBV4L2
+    demux_sys_t *p_sys = p_demux->p_sys;
+#endif
+    /* This is new in Linux 2.6.19 */
+    struct v4l2_frmivalenum frmival;
+    memset( &frmival, 0, sizeof(frmival) );
+    frmival.pixel_format = i_pixel_format;
+    frmival.width = i_width;
+    frmival.height = i_height;
+    if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 )
+    {
+        switch( frmival.type )
+        {
+            case V4L2_FRMIVAL_TYPE_DISCRETE:
+            {
+                float f_fps_max = -1;
+                do
+                {
+                    float f_fps = (float)frmival.discrete.denominator
+                                / (float)frmival.discrete.numerator;
+                    if( f_fps > f_fps_max ) f_fps_max = f_fps;
+                    frmival.index++;
+                } while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS,
+                                     &frmival ) >= 0 );
+                return f_fps_max;
+            }
+            case V4L2_FRMSIZE_TYPE_STEPWISE:
+            case V4L2_FRMIVAL_TYPE_CONTINUOUS:
+                return __MAX( (float)frmival.stepwise.max.denominator
+                            / (float)frmival.stepwise.max.numerator,
+                              (float)frmival.stepwise.min.denominator
+                            / (float)frmival.stepwise.min.numerator );
+        }
+    }
+#endif
+    return -1.;
+}
+
+static float GetAbsoluteMaxFrameRate( demux_t *p_demux, int i_fd,
+                                      uint32_t i_pixel_format )
+{
+    float f_fps_max = -1.;
+#ifdef VIDIOC_ENUM_FRAMESIZES
+#ifdef HAVE_LIBV4L2
+    demux_sys_t *p_sys = p_demux->p_sys;
+#endif
+    /* This is new in Linux 2.6.19 */
+    struct v4l2_frmsizeenum frmsize;
+    memset( &frmsize, 0, sizeof(frmsize) );
+    frmsize.pixel_format = i_pixel_format;
+    if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) >= 0 )
+    {
+        switch( frmsize.type )
+        {
+            case V4L2_FRMSIZE_TYPE_DISCRETE:
+                do
+                {
+                    frmsize.index++;
+                    float f_fps = GetMaxFrameRate( p_demux, i_fd,
+                                                   i_pixel_format,
+                                                   frmsize.discrete.width,
+                                                   frmsize.discrete.height );
+                    if( f_fps > f_fps_max ) f_fps_max = f_fps;
+                } while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES,
+                         &frmsize ) >= 0 );
+                break;
+            case V4L2_FRMSIZE_TYPE_STEPWISE:
+            {
+                uint32_t i_width = frmsize.stepwise.min_width;
+                uint32_t i_height = frmsize.stepwise.min_height;
+                for( ;
+                     i_width <= frmsize.stepwise.max_width &&
+                     i_height <= frmsize.stepwise.max_width;
+                     i_width += frmsize.stepwise.step_width,
+                     i_height += frmsize.stepwise.step_height )
+                {
+                    float f_fps = GetMaxFrameRate( p_demux, i_fd,
+                                                   i_pixel_format,
+                                                   i_width, i_height );
+                    if( f_fps > f_fps_max ) f_fps_max = f_fps;
+                }
+                break;
+            }
+            case V4L2_FRMSIZE_TYPE_CONTINUOUS:
+                /* FIXME */
+                msg_Err( p_demux, "GetAbsoluteMaxFrameRate implementation for V4L2_FRMSIZE_TYPE_CONTINUOUS isn't correct" );
+                 f_fps_max = GetMaxFrameRate( p_demux, i_fd,
+                                              i_pixel_format,
+                                              frmsize.stepwise.max_width,
+                                              frmsize.stepwise.max_height );
+                break;
+        }
+    }
+#endif
+    return f_fps_max;
+}
+
+static void GetMaxDimensions( demux_t *p_demux, int i_fd,
+                              uint32_t i_pixel_format, float f_fps_min,
+                              uint32_t *pi_width, uint32_t *pi_height )
+{
+    *pi_width = 0;
+    *pi_height = 0;
+#ifdef VIDIOC_ENUM_FRAMESIZES
+#ifdef HAVE_LIBV4L2
+    demux_sys_t *p_sys = p_demux->p_sys;
+#endif
+    /* This is new in Linux 2.6.19 */
+    struct v4l2_frmsizeenum frmsize;
+    memset( &frmsize, 0, sizeof(frmsize) );
+    frmsize.pixel_format = i_pixel_format;
+    if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) >= 0 )
+    {
+        switch( frmsize.type )
+        {
+            case V4L2_FRMSIZE_TYPE_DISCRETE:
+                do
+                {
+                    frmsize.index++;
+                    float f_fps = GetMaxFrameRate( p_demux, i_fd,
+                                                   i_pixel_format,
+                                                   frmsize.discrete.width,
+                                                   frmsize.discrete.height );
+                    if( f_fps >= f_fps_min &&
+                        frmsize.discrete.width > *pi_width )
+                    {
+                        *pi_width = frmsize.discrete.width;
+                        *pi_height = frmsize.discrete.height;
+                    }
+                } while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES,
+                         &frmsize ) >= 0 );
+                break;
+            case V4L2_FRMSIZE_TYPE_STEPWISE:
+            {
+                uint32_t i_width = frmsize.stepwise.min_width;
+                uint32_t i_height = frmsize.stepwise.min_height;
+                for( ;
+                     i_width <= frmsize.stepwise.max_width &&
+                     i_height <= frmsize.stepwise.max_width;
+                     i_width += frmsize.stepwise.step_width,
+                     i_height += frmsize.stepwise.step_height )
+                {
+                    float f_fps = GetMaxFrameRate( p_demux, i_fd,
+                                                   i_pixel_format,
+                                                   i_width, i_height );
+                    if( f_fps >= f_fps_min && i_width > *pi_width )
+                    {
+                        *pi_width = i_width;
+                        *pi_height = i_height;
+                    }
+                }
+                break;
+            }
+            case V4L2_FRMSIZE_TYPE_CONTINUOUS:
+                /* FIXME */
+                msg_Err( p_demux, "GetMaxDimension implementation for V4L2_FRMSIZE_TYPE_CONTINUOUS isn't correct" );
+                float f_fps = GetMaxFrameRate( p_demux, i_fd,
+                                               i_pixel_format,
+                                               frmsize.stepwise.max_width,
+                                               frmsize.stepwise.max_height );
+                if( f_fps >= f_fps_min &&
+                    frmsize.stepwise.max_width > *pi_width )
+                {
+                    *pi_width = frmsize.stepwise.max_width;
+                    *pi_height = frmsize.stepwise.max_height;
+                }
+                break;
+        }
+    }
+#endif
+}
+
 /*****************************************************************************
  * OpenVideoDev: open and set up the video device and probe for capabilities
  *****************************************************************************/
@@ -1513,9 +1840,8 @@ static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
     enum v4l2_buf_type buf_type;
     const char *psz_device = p_sys->psz_device;
     es_format_t es_fmt;
-    int libv4l2_fd;
 
-    if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
+    if( ( i_fd = v4l2_open( psz_device, O_RDWR ) ) < 0 )
     {
         msg_Err( p_obj, "cannot open device (%m)" );
         goto open_failed;
@@ -1528,11 +1854,13 @@ static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
        custom cam format to normal formats conversion). Chances are big we will
        still fail then though, as normally v4l2_fd_open only fails if the
        device is not a v4l2 device. */
-    libv4l2_fd = v4l2_fd_open( i_fd, V4L2_ENABLE_ENUM_FMT_EMULATION );
-    if( libv4l2_fd != -1 )
-        i_fd = libv4l2_fd;
-#else
-    libv4l2_fd = i_fd;
+    if( p_sys->b_libv4l2 )
+    {
+        int libv4l2_fd;
+        libv4l2_fd = v4l2_fd_open( i_fd, V4L2_ENABLE_ENUM_FMT_EMULATION );
+        if( libv4l2_fd != -1 )
+            i_fd = libv4l2_fd;
+    }
 #endif
 
     /* Tune the tuner */
@@ -1585,7 +1913,21 @@ static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
             msg_Err( p_obj, "cannot set standard (%m)" );
             goto open_failed;
         }
-        msg_Dbg( p_obj, "Set standard" );
+        if( v4l2_ioctl( i_fd, VIDIOC_G_STD, &p_sys->i_selected_standard_id ) < 0 )
+        {
+            msg_Err( p_obj, "cannot get standard (%m). This should never happen!" );
+            goto open_failed;
+        }
+        msg_Dbg( p_obj, "Set standard to (0x%"PRIx64"):", p_sys->i_selected_standard_id );
+        int i_standard;
+        for( i_standard = 0; i_standard<p_sys->i_standard; i_standard++)
+        {
+            if( p_sys->p_standards[i_standard].id & p_sys->i_selected_standard_id )
+            {
+                msg_Dbg( p_obj, "  %s",
+                        p_sys->p_standards[i_standard].name );
+            }
+        }
     }
 
     /* Select input */
@@ -1624,15 +1966,9 @@ static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
 
     /* TODO: Move the resolution stuff up here */
     /* if MPEG encoder card, no need to do anything else after this */
-    ControlList( p_obj, i_fd,
+    ControlList( p_obj, p_sys, i_fd,
                   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 )
@@ -1640,22 +1976,27 @@ static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
         case IO_METHOD_READ:
             if( !(p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE) )
             {
-                msg_Err( p_demux, "device does not support read i/o" );
+                msg_Err( p_obj, "device does not support read i/o" );
                 goto open_failed;
             }
+            msg_Dbg( p_obj, "using read i/o" );
             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" );
+                msg_Err( p_obj, "device does not support streaming i/o" );
                 goto open_failed;
             }
+            if( p_sys->io == IO_METHOD_MMAP )
+                msg_Dbg( p_obj, "using streaming i/o (mmap)" );
+            else
+                msg_Dbg( p_obj, "using streaming i/o (userptr)" );
             break;
 
         default:
-            msg_Err( p_demux, "io method not supported" );
+            msg_Err( p_obj, "io method not supported" );
             goto open_failed;
     }
 
@@ -1666,16 +2007,19 @@ static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
     {
         crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
         crop.c = cropcap.defrect; /* reset to default */
-        if( v4l2_ioctl( i_fd, VIDIOC_S_CROP, &crop ) < 0 )
+        if( crop.c.width > 0 && crop.c.height > 0 ) /* Fix for fm tuners */
         {
-            switch( errno )
+            if( v4l2_ioctl( i_fd, VIDIOC_S_CROP, &crop ) < 0 )
             {
-                case EINVAL:
-                    /* Cropping not supported. */
-                    break;
-                default:
-                    /* Errors ignored. */
-                    break;
+                switch( errno )
+                {
+                    case EINVAL:
+                        /* Cropping not supported. */
+                        break;
+                    default:
+                        /* Errors ignored. */
+                        break;
+                }
             }
         }
     }
@@ -1686,39 +2030,43 @@ static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
 
     if( p_sys->i_width <= 0 || p_sys->i_height <= 0 )
     {
+        /* 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;
         }
 
-        p_sys->i_width = fmt.fmt.pix.width;
-        p_sys->i_height = fmt.fmt.pix.height;
+        msg_Dbg( p_obj, "found default width and height of %ux%u",
+                 fmt.fmt.pix.width, fmt.fmt.pix.height );
 
-        if( fmt.fmt.pix.field == V4L2_FIELD_ALTERNATE )
+        if( p_sys->i_width < 0 || p_sys->i_height < 0 )
         {
-            p_sys->i_height = p_sys->i_height * 2;
+            msg_Dbg( p_obj, "will try to find optimal width and height." );
         }
     }
     else
     {
-        msg_Dbg( p_demux, "trying specified size %dx%d", p_sys->i_width, p_sys->i_height );
+        /* Use user specified width and 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.width = p_sys->i_width;
-    fmt.fmt.pix.height = p_sys->i_height;
-    fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
+    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(
-                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 )
@@ -1727,38 +2075,134 @@ 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++ )
+        /* If no user specified chroma, find best */
+        /* This also decides if MPEG encoder card or not */
+        if( !fmt.fmt.pix.pixelformat )
         {
-            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;
+            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( i == ARRAY_SIZE( p_chroma_fallbacks ) )
+
+        if( p_sys->i_width < 0 || p_sys->i_height < 0 )
         {
-            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->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 );
+            }
+            uint32_t i_width, i_height;
+            GetMaxDimensions( p_demux, i_fd,
+                              fmt.fmt.pix.pixelformat, p_sys->f_fps,
+                              &i_width, &i_height );
+            if( i_width || i_height )
+            {
+                msg_Dbg( p_demux, "Found optimal dimensions for framerate %f "
+                                  "of %ux%u", p_sys->f_fps, i_width, i_height );
+                fmt.fmt.pix.width = i_width;
+                fmt.fmt.pix.height = i_height;
+                if( v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 )
+                {
+                    msg_Err( p_obj, "Cannot set size to optimal dimensions "
+                                    "%ux%u", i_width, i_height );
+                    goto open_failed;
+                }
+            }
+            else
+            {
+                msg_Warn( p_obj, "Could not find optimal width and height, "
+                                 "falling back to driver default." );
+            }
         }
     }
 
-    /* Reassign width, height and chroma incase driver override */
     p_sys->i_width = fmt.fmt.pix.width;
     p_sys->i_height = fmt.fmt.pix.height;
 
+    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_obj, "Interlacing setting: progressive" );
+            break;
+        case V4L2_FIELD_TOP:
+            msg_Dbg( p_obj, "Interlacing setting: top field only" );
+            break;
+        case V4L2_FIELD_BOTTOM:
+            msg_Dbg( p_obj, "Interlacing setting: bottom field only" );
+            break;
+        case V4L2_FIELD_INTERLACED:
+            msg_Dbg( p_obj, "Interlacing setting: interleaved (bottom top if M/NTSC, top bottom otherwise)" );
+            if( p_sys->i_selected_standard_id == V4L2_STD_NTSC )
+                p_sys->i_block_flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST;
+            else
+                p_sys->i_block_flags = BLOCK_FLAG_TOP_FIELD_FIRST;
+            break;
+        case V4L2_FIELD_SEQ_TB:
+            msg_Dbg( p_obj, "Interlacing setting: sequential top bottom (TODO)" );
+            break;
+        case V4L2_FIELD_SEQ_BT:
+            msg_Dbg( p_obj, "Interlacing setting: sequential bottom top (TODO)" );
+            break;
+        case V4L2_FIELD_ALTERNATE:
+            msg_Dbg( p_obj, "Interlacing setting: alternate fields (TODO)" );
+            p_sys->i_height = p_sys->i_height * 2;
+            break;
+        case V4L2_FIELD_INTERLACED_TB:
+            msg_Dbg( p_obj, "Interlacing setting: interleaved top bottom" );
+            p_sys->i_block_flags = BLOCK_FLAG_TOP_FIELD_FIRST;
+            break;
+        case V4L2_FIELD_INTERLACED_BT:
+            msg_Dbg( p_obj, "Interlacing setting: interleaved bottom top" );
+            p_sys->i_block_flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST;
+            break;
+        default:
+            msg_Warn( p_obj, "Interlacing setting: unknown type (%d)",
+                      fmt.fmt.pix.field );
+            break;
+    }
+
     /* Look up final fourcc */
     p_sys->i_fourcc = 0;
     for( int i = 0; v4l2chroma_to_fourcc[i].i_fourcc != 0; i++ )
@@ -1786,7 +2230,7 @@ static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
     /* This is new in Linux 2.6.19 */
     /* List supported frame rates */
     struct v4l2_frmivalenum frmival;
-    frmival.index = 0;
+    memset( &frmival, 0, sizeof(frmival) );
     frmival.pixel_format = fmt.fmt.pix.pixelformat;
     frmival.width = p_sys->i_width;
     frmival.height = p_sys->i_height;
@@ -1795,21 +2239,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 %4.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,
@@ -1819,7 +2263,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,
@@ -1829,31 +2273,43 @@ static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
     }
 #endif
 
+
     /* Init IO method */
     switch( p_sys->io )
     {
     case IO_METHOD_READ:
-        if( InitRead( p_demux, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
+        if( b_demux )
+            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;
 
+    default:
+        goto open_failed;
+        break;
     }
 
-    /* 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;
+    if( b_demux )
+    {
+        /* Add */
+        es_fmt.video.i_width  = p_sys->i_width;
+        es_fmt.video.i_height = p_sys->i_height;
 
-    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_sar_num = p_sys->i_aspect    * es_fmt.video.i_height;
+        es_fmt.video.i_sar_den = VOUT_ASPECT_FACTOR * es_fmt.video.i_width;
+
+        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 */
 
@@ -1875,7 +2331,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;
             }
         }
@@ -1883,7 +2339,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;
         }
 
@@ -1903,7 +2359,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;
             }
         }
@@ -1911,17 +2367,21 @@ 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;
         }
 
         break;
+
+    default:
+        goto open_failed;
+        break;
     }
 
     /* 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;
@@ -1942,9 +2402,8 @@ static bool ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
     int i_standard;
 
     int i_fd;
-    int libv4l2_fd;
 
-    if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
+    if( ( i_fd = v4l2_open( psz_device, O_RDWR ) ) < 0 )
     {
         msg_Err( p_obj, "cannot open video device '%s' (%m)", psz_device );
         goto open_failed;
@@ -1957,11 +2416,13 @@ static bool ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
        custom cam format to normal formats conversion). Chances are big we will
        still fail then though, as normally v4l2_fd_open only fails if the
        device is not a v4l2 device. */
-    libv4l2_fd = v4l2_fd_open( i_fd, V4L2_ENABLE_ENUM_FMT_EMULATION );
-    if( libv4l2_fd != -1 )
-        i_fd = libv4l2_fd;
-#else
-    libv4l2_fd = i_fd;
+    if( p_sys->b_libv4l2 )
+    {
+        int libv4l2_fd;
+        libv4l2_fd = v4l2_fd_open( i_fd, V4L2_ENABLE_ENUM_FMT_EMULATION );
+        if( libv4l2_fd != -1 )
+            i_fd = libv4l2_fd;
+    }
 #endif
 
     /* Get device capabilites */
@@ -1980,12 +2441,14 @@ static bool ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
                             p_sys->dev_cap.version & 0xFF,
                             p_sys->dev_cap.bus_info );
 
-    msg_Dbg( p_obj, "the device has the capabilities: (%c) Video Capure, "
+    msg_Dbg( p_obj, "the device has the capabilities: (%c) Video Capture, "
                                                        "(%c) Audio, "
-                                                       "(%c) Tuner",
+                                                       "(%c) Tuner, "
+                                                       "(%c) Radio",
              ( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE  ? 'X':' '),
              ( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO  ? 'X':' '),
-             ( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER  ? 'X':' ') );
+             ( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER  ? 'X':' '),
+             ( p_sys->dev_cap.capabilities & V4L2_CAP_RADIO  ? 'X':' ') );
 
     msg_Dbg( p_obj, "supported I/O methods are: (%c) Read/Write, "
                                                  "(%c) Streaming, "
@@ -1994,6 +2457,29 @@ static bool ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
             ( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING ? 'X':' ' ),
             ( p_sys->dev_cap.capabilities & V4L2_CAP_ASYNCIO ? 'X':' ' ) );
 
+    if( p_sys->io == IO_METHOD_AUTO )
+    {
+        if( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING )
+            p_sys->io = IO_METHOD_MMAP;
+        else if( p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE )
+            p_sys->io = IO_METHOD_READ;
+        else
+            msg_Err( p_obj, "No known I/O method supported" );
+    }
+
+    if( p_sys->dev_cap.capabilities & V4L2_CAP_RDS_CAPTURE )
+        msg_Dbg( p_obj, "device supports RDS" );
+
+#ifdef V4L2_CAP_HW_FREQ_SEEK
+    if( p_sys->dev_cap.capabilities & V4L2_CAP_HW_FREQ_SEEK )
+        msg_Dbg( p_obj, "device supports hardware frequency seeking" );
+#endif
+    if( p_sys->dev_cap.capabilities & V4L2_CAP_VBI_CAPTURE )
+        msg_Dbg( p_obj, "device supports raw VBI capture" );
+
+    if( p_sys->dev_cap.capabilities & V4L2_CAP_SLICED_VBI_CAPTURE )
+        msg_Dbg( p_obj, "device supports sliced VBI capture" );
+
     /* Now, enumerate all the video inputs. This is useless at the moment
        since we have no way to present that info to the user except with
        debug messages */
@@ -2001,13 +2487,15 @@ static bool ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
     if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
     {
         struct v4l2_input t_input;
-        t_input.index = 0;
+        memset( &t_input, 0, sizeof(t_input) );
+        p_sys->i_input = 0;
         while( v4l2_ioctl( i_fd, VIDIOC_ENUMINPUT, &t_input ) >= 0 )
         {
             p_sys->i_input++;
             t_input.index = p_sys->i_input;
         }
 
+        free( p_sys->p_inputs );
         p_sys->p_inputs = calloc( 1, p_sys->i_input * sizeof( struct v4l2_input ) );
         if( !p_sys->p_inputs ) goto open_failed;
 
@@ -2036,12 +2524,14 @@ 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++;
             t_standards.index = p_sys->i_standard;
         }
 
+        free( p_sys->p_standards );
         p_sys->p_standards = calloc( 1, p_sys->i_standard * sizeof( struct v4l2_standard ) );
         if( !p_sys->p_standards ) goto open_failed;
 
@@ -2057,7 +2547,7 @@ static bool ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
             msg_Dbg( p_obj, "video standard %i is: %s %c",
                                 i_standard,
                                 p_sys->p_standards[i_standard].name,
-                                (unsigned)i_standard == p_sys->i_selected_standard_id ? '*' : ' ' );
+                                (p_sys->p_standards[i_standard].id & p_sys->i_selected_standard_id) ? '*' : ' ' );
         }
     }
 
@@ -2099,6 +2589,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++;
@@ -2106,6 +2597,7 @@ static bool ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
             tuner.index = p_sys->i_tuner;
         }
 
+        free( p_sys->p_tuners );
         p_sys->p_tuners = calloc( 1, p_sys->i_tuner * sizeof( struct v4l2_tuner ) );
         if( !p_sys->p_tuners ) goto open_failed;
 
@@ -2169,6 +2661,7 @@ static bool ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
 
         p_sys->i_codec = i_index;
 
+        free( p_sys->p_codecs );
         p_sys->p_codecs = calloc( 1, p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
 
         for( i_index = 0; i_index < p_sys->i_codec; i_index++ )
@@ -2198,7 +2691,7 @@ static bool ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
                     memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
                     vlc_fourcc_to_char( v4l2chroma_to_fourcc[i].i_fourcc,
                                         &psz_fourcc );
-                    msg_Dbg( p_obj, "device supports chroma %4s [%s, %s]",
+                    msg_Dbg( p_obj, "device supports chroma %4.4s [%s, %s]",
                                 psz_fourcc,
                                 p_sys->p_codecs[i_index].description,
                                 psz_fourcc_v4l2 );
@@ -2207,7 +2700,7 @@ static bool ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
                     /* This is new in Linux 2.6.19 */
                     /* List valid frame sizes for this format */
                     struct v4l2_frmsizeenum frmsize;
-                    frmsize.index = 0;
+                    memset( &frmsize, 0, sizeof(frmsize) );
                     frmsize.pixel_format = p_sys->p_codecs[i_index].pixelformat;
                     if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) < 0 )
                     {
@@ -2248,11 +2741,10 @@ static bool ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
             if( !b_codec_supported )
             {
                     msg_Dbg( p_obj,
-                         "device codec %4s (%s) not supported as access_demux",
+                         "device codec %4.4s (%s) not supported",
                          psz_fourcc_v4l2,
                          p_sys->p_codecs[i_index].description );
             }
-
         }
     }
 
@@ -2277,7 +2769,7 @@ static void name2var( unsigned char *name )
  * Print a user-class v4l2 control's details, create the relevant variable,
  * change the value if needed.
  *****************************************************************************/
-static void ControlListPrint( vlc_object_t *p_obj, int i_fd,
+static void ControlListPrint( vlc_object_t *p_obj, demux_sys_t *p_sys, int i_fd,
                               struct v4l2_queryctrl queryctrl,
                               bool b_reset, bool b_demux )
 {
@@ -2396,14 +2888,14 @@ static void ControlListPrint( vlc_object_t *p_obj, int i_fd,
                     if( b_reset && queryctrl.default_value != control.value )
                     {
                         msg_Dbg( p_obj, "    reset value to default" );
-                        Control( p_obj, i_fd, psz_name,
-                                      queryctrl.id, queryctrl.default_value );
+                        Control( p_obj, p_sys, i_fd, psz_name,
+                                 queryctrl.id, queryctrl.default_value );
                     }
                 }
                 else
                 {
-                    Control( p_obj, i_fd, psz_name,
-                                  queryctrl.id, i_val );
+                    Control( p_obj, p_sys, i_fd, psz_name,
+                             queryctrl.id, i_val );
                 }
             }
             break;
@@ -2451,7 +2943,7 @@ static void ControlListPrint( vlc_object_t *p_obj, int i_fd,
  * List all user-class v4l2 controls, set them to the user specified
  * value and create the relevant variables to enable runtime changes
  *****************************************************************************/
-static int ControlList( vlc_object_t *p_obj, int i_fd,
+static int ControlList( vlc_object_t *p_obj, demux_sys_t *p_sys, int i_fd,
                         bool b_reset, bool b_demux )
 {
     struct v4l2_queryctrl queryctrl;
@@ -2513,7 +3005,7 @@ static int ControlList( vlc_object_t *p_obj, int i_fd,
                              queryctrl.name, queryctrl.id );
                     break;
             }
-            ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
+            ControlListPrint( p_obj, p_sys, i_fd, queryctrl, b_reset, b_demux );
             queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
         }
     }
@@ -2533,7 +3025,8 @@ static int ControlList( vlc_object_t *p_obj, int i_fd,
                     continue;
                 msg_Dbg( p_obj, "Available control: %s (%x)",
                          queryctrl.name, queryctrl.id );
-                ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
+                ControlListPrint( p_obj, p_sys, i_fd, queryctrl,
+                                  b_reset, b_demux );
             }
         }
 
@@ -2549,7 +3042,8 @@ static int ControlList( vlc_object_t *p_obj, int i_fd,
                     continue;
                 msg_Dbg( p_obj, "Available private control: %s (%x)",
                          queryctrl.name, queryctrl.id );
-                ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
+                ControlListPrint( p_obj, p_sys, i_fd, queryctrl,
+                                  b_reset, b_demux );
             }
             else
                 break;
@@ -2617,9 +3111,10 @@ static void SetAvailControlsByString( vlc_object_t *p_obj, demux_sys_t *p_sys,
 
             if( !strncasecmp( psz_name, psz_parser, i_maxstrlen ) )
             {
-                Control( p_obj, i_fd, psz_name, i_cid,
+                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 )
@@ -2630,12 +3125,13 @@ 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 );
 }
 
 /*****************************************************************************
  * Reset all user-class v4l2 controls to their default value
  *****************************************************************************/
-static int ControlReset( vlc_object_t *p_obj, int i_fd )
+static int ControlReset( vlc_object_t *p_obj, demux_sys_t *p_sys, int i_fd )
 {
     struct v4l2_queryctrl queryctrl;
     int i_cid;
@@ -2664,8 +3160,9 @@ static int ControlReset( vlc_object_t *p_obj, int i_fd )
                 for( i = 0; controls[i].psz_name != NULL; i++ )
                     if( controls[i].i_cid == queryctrl.id ) break;
                 name2var( queryctrl.name );
-                Control( p_obj, i_fd,
-                         controls[i].psz_name ? : (const char *)queryctrl.name,
+                Control( p_obj, p_sys, i_fd,
+                         controls[i].psz_name ? controls[i].psz_name
+                          : (const char *)queryctrl.name,
                          queryctrl.id, queryctrl.default_value );
             }
             queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
@@ -2694,8 +3191,9 @@ static int ControlReset( vlc_object_t *p_obj, int i_fd )
                     for( i = 0; controls[i].psz_name != NULL; i++ )
                         if( controls[i].i_cid == queryctrl.id ) break;
                     name2var( queryctrl.name );
-                    Control( p_obj, i_fd,
-                             controls[i].psz_name ? : (const char *)queryctrl.name,
+                    Control( p_obj, p_sys, i_fd,
+                             controls[i].psz_name ? controls[i].psz_name
+                              : (const char *)queryctrl.name,
                              queryctrl.id, queryctrl.default_value );
                 }
             }
@@ -2718,7 +3216,7 @@ static int ControlReset( vlc_object_t *p_obj, int i_fd )
                  && queryctrl.default_value != control.value )
                 {
                     name2var( queryctrl.name );
-                    Control( p_obj, i_fd, (const char *)queryctrl.name,
+                    Control( p_obj, p_sys, i_fd, (const char *)queryctrl.name,
                              queryctrl.id, queryctrl.default_value );
                 }
             }
@@ -2732,9 +3230,10 @@ static int ControlReset( vlc_object_t *p_obj, int i_fd )
 /*****************************************************************************
  * Issue user-class v4l2 controls
  *****************************************************************************/
-static int Control( vlc_object_t *p_obj, int i_fd,
+static int Control( vlc_object_t *p_obj, demux_sys_t *p_sys, int i_fd,
                     const char *psz_name, int i_cid, int i_value )
 {
+    (void)p_sys;
     struct v4l2_queryctrl queryctrl;
     struct v4l2_control control;
     struct v4l2_ext_control ext_control;
@@ -2765,7 +3264,7 @@ static int Control( vlc_object_t *p_obj, int i_fd,
 
     int i_ret = -1;
 
-    if( i_value >= 0 )
+    if( i_value >= queryctrl.minimum && i_value <= queryctrl.maximum )
     {
         ext_control.value = i_value;
         if( v4l2_ioctl( i_fd, VIDIOC_S_EXT_CTRLS, &ext_controls ) < 0 )
@@ -2795,12 +3294,12 @@ static int Control( vlc_object_t *p_obj, 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;
         }
     }
@@ -2824,7 +3323,7 @@ static int DemuxControlCallback( vlc_object_t *p_this,
     if( i_fd < 0 )
         return VLC_EGENERIC;
 
-    Control( p_this, i_fd, psz_var, i_cid, newval.i_int );
+    Control( p_this, p_sys, i_fd, psz_var, i_cid, newval.i_int );
 
     return VLC_EGENERIC;
 }
@@ -2841,7 +3340,7 @@ static int DemuxControlResetCallback( vlc_object_t *p_this,
     if( i_fd < 0 )
         return VLC_EGENERIC;
 
-    ControlReset( p_this, i_fd );
+    ControlReset( p_this, p_sys, i_fd );
 
     return VLC_EGENERIC;
 }
@@ -2860,7 +3359,7 @@ static int AccessControlCallback( vlc_object_t *p_this,
     if( i_fd < 0 )
         return VLC_EGENERIC;
 
-    Control( p_this, i_fd, psz_var, i_cid, newval.i_int );
+    Control( p_this, p_sys, i_fd, psz_var, i_cid, newval.i_int );
 
     return VLC_EGENERIC;
 }
@@ -2877,7 +3376,7 @@ static int AccessControlResetCallback( vlc_object_t *p_this,
     if( i_fd < 0 )
         return VLC_EGENERIC;
 
-    ControlReset( p_this, i_fd );
+    ControlReset( p_this, p_sys, i_fd );
 
     return VLC_EGENERIC;
 }