]> git.sesse.net Git - vlc/blobdiff - modules/access/v4l/v4l.c
* include/modules_inner.h, include/modules.h: added a shortname field to the module...
[vlc] / modules / access / v4l / v4l.c
index 42b194b0ee88b849bc385b08e439ca4d46108f09..edddf6e2b4949e0e31603ba3e319abc10f3675dc 100644 (file)
@@ -1,10 +1,12 @@
 /*****************************************************************************
  * v4l.c : Video4Linux input module for vlc
  *****************************************************************************
- * Copyright (C) 2002 VideoLAN
- * $Id: v4l.c,v 1.5 2003/03/31 03:46:11 fenrir Exp $
+ * Copyright (C) 2002-2004 VideoLAN
+ * $Id$
  *
- * Author: Samuel Hocevar <sam@zoy.org>
+ * Author: Laurent Aimar <fenrir@via.ecp.fr>
+ *         Paul Forgey <paulf at aphrodite dot com>
+ *         Gildas Bazin <gbazin@videolan.org>
  *
  * 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
@@ -32,7 +34,6 @@
 #include <vlc/input.h>
 #include <vlc/vout.h>
 #include <codecs.h>
-#include "encoder.h"
 
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <sys/mman.h>
 #include <errno.h>
-
 #include <fcntl.h>
-#include <linux/videodev.h>
 
-/* enable audio grabbing */
-#undef _V4L_AUDIO_
+/* From GStreamer's v4l plugin:
+ * Because of some really cool feature in video4linux1, also known as
+ * 'not including sys/types.h and sys/time.h', we had to include it
+ * ourselves. In all their intelligence, these people decided to fix
+ * this in the next version (video4linux2) in such a cool way that it
+ * breaks all compilations of old stuff...
+ * The real problem is actually that linux/time.h doesn't use proper
+ * macro checks before defining types like struct timeval. The proper
+ * fix here is to either fuck the kernel header (which is what we do
+ * by defining _LINUX_TIME_H, an innocent little hack) or by fixing it
+ * upstream, which I'll consider doing later on. If you get compiler
+ * errors here, check your linux/time.h && sys/time.h header setup.
+*/
+#define _LINUX_TIME_H
 
-#ifdef _V4L_AUDIO_
-    #include <sys/soundcard.h>
-#endif
+#include <linux/videodev.h>
+#include "videodev_mjpeg.h"
+
+#include <sys/soundcard.h>
 
 /*****************************************************************************
- * Local prototypes
+ * Module descriptior
  *****************************************************************************/
-static int  AccessOpen  ( vlc_object_t * );
-static void AccessClose ( vlc_object_t * );
-static int  Read        ( input_thread_t *, byte_t *, size_t );
+static int  AccessOpen ( vlc_object_t * );
+static void AccessClose( vlc_object_t * );
 
 static int  DemuxOpen  ( vlc_object_t * );
 static void DemuxClose ( vlc_object_t * );
-static int  Demux      ( input_thread_t * );
 
-
-/*****************************************************************************
- * Module descriptior
- *****************************************************************************/
-#define CACHING_TEXT N_("caching value in ms")
+#define CACHING_TEXT N_("Caching value in ms")
 #define CACHING_LONGTEXT N_( \
     "Allows you to modify the default caching value for v4l streams. This " \
-    "value should be set in miliseconds units." )
+    "value should be set in millisecond units." )
+#define VDEV_TEXT N_("Video device name")
+#define VDEV_LONGTEXT N_( \
+    "Specify the name of the video device that will be used. " \
+    "If you don't specify anything, no video device will be used.")
+#define ADEV_TEXT N_("Audio device name")
+#define ADEV_LONGTEXT N_( \
+    "Specify the name of the audio device that will be used. " \
+    "If you don't specify anything, no audio device will be used.")
+#define CHROMA_TEXT N_("Video input chroma format")
+#define CHROMA_LONGTEXT N_( \
+    "Force the Video4Linux video device to use a specific chroma format " \
+    "(eg. I420 (default), RV24, etc.)")
 
 vlc_module_begin();
+    set_shortname( _("Video4Linux") );
     set_description( _("Video4Linux input") );
-    add_category_hint( N_("v4l"), NULL, VLC_TRUE );
-        add_integer( "v4l-caching", DEFAULT_PTS_DELAY / 1000, NULL,
-                     CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
-    add_submodule();
-        add_shortcut( "v4l" );
-        set_capability( "access", 0 );
-        set_callbacks( AccessOpen, AccessClose );
+
+    add_integer( "v4l-caching", DEFAULT_PTS_DELAY / 1000, NULL,
+                 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
+    add_string( "v4l-vdev", "/dev/video", 0, VDEV_TEXT, VDEV_LONGTEXT,
+                VLC_FALSE );
+    add_string( "v4l-adev", "/dev/dsp", 0, ADEV_TEXT, ADEV_LONGTEXT,
+                VLC_FALSE );
+    add_string( "v4l-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
+                VLC_TRUE );
+
+    add_shortcut( "v4l" );
+    set_capability( "access", 10 );
+    set_callbacks( AccessOpen, AccessClose );
+
     add_submodule();
+        set_description( _("Video4Linux demuxer") );
         add_shortcut( "v4l" );
         set_capability( "demux", 200 );
         set_callbacks( DemuxOpen, DemuxClose );
 vlc_module_end();
 
 
-/****************************************************************************
- * I. Access Part
- ****************************************************************************/
+/*****************************************************************************
+ * Access: local prototypes
+ *****************************************************************************/
+static int  Read        ( input_thread_t *, byte_t *, size_t );
+
+static void ParseMRL    ( input_thread_t * );
+static int  OpenVideoDev( input_thread_t *, char * );
+static int  OpenAudioDev( input_thread_t *, char * );
+
+#define MJPEG_BUFFER_SIZE (256*1024)
+
+struct quicktime_mjpeg_app1
+{
+    uint32_t    i_reserved;             /* set to 0 */
+    uint32_t    i_tag;                  /* 'mjpg' */
+    uint32_t    i_field_size;           /* offset following EOI */
+    uint32_t    i_padded_field_size;    /* offset following EOI+pad */
+    uint32_t    i_next_field;           /* offset to next field */
+    uint32_t    i_DQT_offset;
+    uint32_t    i_DHT_offset;
+    uint32_t    i_SOF_offset;
+    uint32_t    i_SOS_offset;
+    uint32_t    i_data_offset;          /* following SOS marker data */
+};
+
+static struct
+{
+    int i_v4l;
+    int i_fourcc;
+
+} v4lchroma_to_fourcc[] =
+{
+    { VIDEO_PALETTE_GREY, VLC_FOURCC( 'G', 'R', 'E', 'Y' ) },
+    { VIDEO_PALETTE_HI240, VLC_FOURCC( 'I', '2', '4', '0' ) },
+    { VIDEO_PALETTE_RGB565, VLC_FOURCC( 'R', 'V', '1', '6' ) },
+    { VIDEO_PALETTE_RGB555, VLC_FOURCC( 'R', 'V', '1', '5' ) },
+    { VIDEO_PALETTE_RGB24, VLC_FOURCC( 'R', 'V', '2', '4' ) },
+    { VIDEO_PALETTE_RGB32, VLC_FOURCC( 'R', 'V', '3', '2' ) },
+    { VIDEO_PALETTE_YUV422, VLC_FOURCC( 'I', '4', '2', '2' ) },
+    { VIDEO_PALETTE_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', 'V' ) },
+    { VIDEO_PALETTE_UYVY, VLC_FOURCC( 'U', 'Y', 'V', 'Y' ) },
+    { VIDEO_PALETTE_YUV420, VLC_FOURCC( 'I', '4', '2', 'N' ) },
+    { VIDEO_PALETTE_YUV411, VLC_FOURCC( 'I', '4', '1', 'N' ) },
+    { VIDEO_PALETTE_RAW, VLC_FOURCC( 'G', 'R', 'A', 'W' ) },
+    { VIDEO_PALETTE_YUV422P, VLC_FOURCC( 'I', '4', '2', '2' ) },
+    { VIDEO_PALETTE_YUV420P, VLC_FOURCC( 'I', '4', '2', '0' ) },
+    { VIDEO_PALETTE_YUV411P, VLC_FOURCC( 'I', '4', '1', '1' ) },
+    { 0, 0 }
+};
+
 struct access_sys_t
 {
-    char    *psz_video_device;
-    int     fd;
+    /* Devices */
+    char *psz_device;         /* Main device from MRL, can be video or audio */
 
-    vlc_fourcc_t    i_codec;   // if i_codec != i_chroma then we need a compressor
-    video_encoder_t *p_encoder;
-    picture_t       pic;
+    char *psz_vdev;
+    int  fd_video;
 
+    char *psz_adev;
+    int  fd_audio;
+
+    /* Video properties */
+    picture_t pic;
+
+    int i_fourcc;
     int i_channel;
     int i_audio;
     int i_norm;
     int i_tuner;
     int i_frequency;
-    int i_chroma;
     int i_width;
     int i_height;
 
+    int i_brightness;
+    int i_hue;
+    int i_colour;
+    int i_contrast;
+
+    float f_fps;            /* <= 0.0 mean to grab at full rate */
+    mtime_t i_video_pts;    /* only used when f_fps > 0 */
+
+    vlc_bool_t b_mjpeg;
+    int i_decimation;
+    int i_quality;
+
     struct video_capability vid_cap;
     struct video_mbuf       vid_mbuf;
+    struct mjpeg_requestbuffers mjpeg_buffers;
 
     uint8_t *p_video_mmap;
     int     i_frame_pos;
+
     struct video_mmap   vid_mmap;
+    struct video_picture vid_picture;
 
     uint8_t *p_video_frame;
     int     i_video_frame_size;
     int     i_video_frame_size_allocated;
 
-#ifdef _V4L_AUDIO_
-    char         *psz_adev;
-    int          fd_audio;
+    /* Audio properties */
     vlc_fourcc_t i_acodec_raw;
     int          i_sample_rate;
     vlc_bool_t   b_stereo;
@@ -130,9 +222,8 @@ struct access_sys_t
     int     i_audio_frame_size;
     int     i_audio_frame_size_allocated;
 
-#endif
-
     /* header */
+    int     i_streams;
     int     i_header_size;
     int     i_header_pos;
     uint8_t *p_header;      // at lest 8 bytes allocated
@@ -165,7 +256,7 @@ struct access_sys_t
  *  u8      data
  */
 
-static void    SetDWBE( uint8_t *p, uint32_t dw )
+static void SetDWBE( uint8_t *p, uint32_t dw )
 {
     p[0] = (dw >> 24)&0xff;
     p[1] = (dw >> 16)&0xff;
@@ -173,14 +264,14 @@ static void    SetDWBE( uint8_t *p, uint32_t dw )
     p[3] = (dw      )&0xff;
 }
 
-static uint32_t GetDWBE( uint8_t *p_buff )
+static void SetQWBE( uint8_t *p, uint64_t qw )
 {
-    return( ( p_buff[0] << 24 ) + ( p_buff[1] << 16 ) +
-            ( p_buff[2] <<  8 ) + p_buff[3] );
+    SetDWBE( p,     (qw >> 32)&0xffffffff );
+    SetDWBE( &p[4], qw&0xffffffff);
 }
 
 /*****************************************************************************
- * Open: open device:
+ * AccessOpen: opens v4l device
  *****************************************************************************
  *
  * url: <video device>::::
@@ -190,42 +281,205 @@ static int AccessOpen( vlc_object_t *p_this )
 {
     input_thread_t *p_input = (input_thread_t *)p_this;
     access_sys_t   *p_sys;
-    char           *psz_dup, *psz_parser;
-
-    struct video_channel    vid_channel;
+    vlc_value_t    val;
 
     /* create access private data */
-    p_sys = malloc( sizeof( access_sys_t ) );
+    p_input->p_access_data = p_sys = malloc( sizeof( access_sys_t ) );
     memset( p_sys, 0, sizeof( access_sys_t ) );
 
-    p_sys->psz_video_device = NULL;
-    p_sys->fd               = -1;
-    p_sys->i_channel        = -1;
     p_sys->i_audio          = -1;
     p_sys->i_norm           = VIDEO_MODE_AUTO;    // auto
     p_sys->i_tuner          = -1;
     p_sys->i_frequency      = -1;
-    p_sys->i_width          = 0;
-    p_sys->i_height         = 0;
 
-    p_sys->i_frame_pos = 0;
+    p_sys->f_fps            = -1.0;
+    p_sys->i_video_pts      = -1;
+
+    p_sys->i_brightness     = -1;
+    p_sys->i_hue            = -1;
+    p_sys->i_colour         = -1;
+    p_sys->i_contrast       = -1;
+
+    p_sys->b_mjpeg     = VLC_FALSE;
+    p_sys->i_decimation = 1;
+    p_sys->i_quality = 100;
 
-    p_sys->i_codec          = VLC_FOURCC( 0, 0, 0, 0 );
     p_sys->i_video_frame_size_allocated = 0;
-#ifdef _V4L_AUDIO_
-    p_sys->psz_adev         = NULL;
-    p_sys->fd_audio         = -1;
-    p_sys->i_sample_rate    = 44100;
-    p_sys->b_stereo         = VLC_TRUE;
-#endif
+    p_sys->i_frame_pos = 0;
 
+    p_sys->p_audio_frame  = 0;
+    p_sys->i_sample_rate  = 44100;
+    p_sys->b_stereo       = VLC_TRUE;
+
+    p_sys->p_header    = NULL;
     p_sys->i_data_size = 0;
     p_sys->i_data_pos  = 0;
     p_sys->p_data      = NULL;
 
-    /* parse url and open device(s) */
-    psz_dup = strdup( p_input->psz_name );
-    psz_parser = psz_dup;
+    p_sys->psz_device = p_sys->psz_vdev = p_sys->psz_adev = NULL;
+    p_sys->fd_video = -1;
+    p_sys->fd_audio = -1;
+
+    ParseMRL( p_input );
+
+    /* Find main device (video or audio) */
+    if( p_sys->psz_device && *p_sys->psz_device )
+    {
+        msg_Dbg( p_input, "main device=`%s'", p_sys->psz_device );
+
+        /* Try to open as video device */
+        p_sys->fd_video = OpenVideoDev( p_input, p_sys->psz_device );
+
+        if( p_sys->fd_video < 0 )
+        {
+            /* Try to open as audio device */
+            p_sys->fd_audio = OpenAudioDev( p_input, p_sys->psz_device );
+            if( p_sys->fd_audio >= 0 )
+            {
+                if( p_sys->psz_adev ) free( p_sys->psz_adev );
+                p_sys->psz_adev = p_sys->psz_device;
+                p_sys->psz_device = NULL;
+            }
+        }
+        else
+        {
+            if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
+            p_sys->psz_vdev = p_sys->psz_device;
+            p_sys->psz_device = NULL;
+        }
+    }
+
+    /* If no device opened, only continue if the access was forced */
+    if( p_sys->fd_video < 0 && p_sys->fd_audio < 0 )
+    {
+        if( !p_input->psz_access ||
+            strcmp( p_input->psz_access, "v4l" ) )
+        {
+            AccessClose( p_this );
+            return VLC_EGENERIC;
+        }
+    }
+
+    /* Find video device */
+    if( p_sys->fd_video < 0 )
+    {
+        if( !p_sys->psz_vdev || !*p_sys->psz_vdev )
+        {
+            var_Create( p_input, "v4l-vdev",
+                        VLC_VAR_STRING | VLC_VAR_DOINHERIT );
+            var_Get( p_input, "v4l-vdev", &val );
+
+            if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
+            p_sys->psz_vdev = val.psz_string;
+        }
+
+        if( p_sys->psz_vdev && *p_sys->psz_vdev )
+        {
+            p_sys->fd_video = OpenVideoDev( p_input, p_sys->psz_vdev );
+        }
+    }
+
+    /* Find audio device */
+    if( p_sys->fd_audio < 0 )
+    {
+        if( !p_sys->psz_adev || !*p_sys->psz_adev )
+        {
+            var_Create( p_input, "v4l-adev",
+                        VLC_VAR_STRING | VLC_VAR_DOINHERIT );
+            var_Get( p_input, "v4l-adev", &val );
+
+            if( p_sys->psz_adev ) free( p_sys->psz_adev );
+            p_sys->psz_adev = val.psz_string;
+        }
+
+        if( p_sys->psz_adev && *p_sys->psz_adev )
+        {
+            p_sys->fd_audio = OpenAudioDev( p_input, p_sys->psz_adev );
+        }
+    }
+
+    if( p_sys->fd_video < 0 && p_sys->fd_audio < 0 )
+    {
+        AccessClose( p_this );
+        return VLC_EGENERIC;
+    }
+
+    p_input->pf_read        = Read;
+    p_input->pf_seek        = NULL;
+    p_input->pf_set_area    = NULL;
+    p_input->pf_set_program = NULL;
+
+    vlc_mutex_lock( &p_input->stream.stream_lock );
+    p_input->stream.b_pace_control = 0;
+    p_input->stream.b_seekable = 0;
+    p_input->stream.p_selected_area->i_size = 0;
+    p_input->stream.p_selected_area->i_tell = 0;
+    p_input->stream.i_method = INPUT_METHOD_FILE;
+    vlc_mutex_unlock( &p_input->stream.stream_lock );
+
+    /* Update default_pts to a suitable value for access */
+    var_Create( p_input, "v4l-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
+    var_Get( p_input, "v4l-caching", &val );
+    p_input->i_pts_delay = val.i_int * 1000;
+
+    msg_Info( p_input, "v4l grabbing started" );
+
+    /* create header */
+    p_sys->i_streams     = 0;
+    p_sys->i_header_size = 8;
+    p_sys->i_header_pos  = 8;
+    p_sys->p_header      = malloc( p_sys->i_header_size );
+
+    memcpy( p_sys->p_header, ".v4l", 4 );
+
+    if( p_sys->fd_video >= 0 )
+    {
+        p_sys->i_header_size += 20;
+        p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header_size );
+
+        SetDWBE( &p_sys->p_header[4], ++p_sys->i_streams );
+
+        memcpy(  &p_sys->p_header[p_sys->i_header_pos], "vids", 4 );
+        memcpy(  &p_sys->p_header[p_sys->i_header_pos+4],
+                 &p_sys->i_fourcc, 4 );
+        SetDWBE( &p_sys->p_header[p_sys->i_header_pos+8], p_sys->i_width );
+        SetDWBE( &p_sys->p_header[p_sys->i_header_pos+12], p_sys->i_height );
+        SetDWBE( &p_sys->p_header[p_sys->i_header_pos+16], 0 );
+
+        p_sys->i_header_pos = p_sys->i_header_size;
+    }
+
+    if( p_sys->fd_audio >= 0 )
+    {
+        p_sys->i_header_size += 20;
+        p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header_size );
+
+        SetDWBE( &p_sys->p_header[4], ++p_sys->i_streams );
+
+        memcpy(  &p_sys->p_header[p_sys->i_header_pos], "auds", 4 );
+        memcpy(  &p_sys->p_header[p_sys->i_header_pos+4], "araw", 4 );
+        SetDWBE( &p_sys->p_header[p_sys->i_header_pos+8],
+                 p_sys->b_stereo ? 2 : 1 );
+        SetDWBE( &p_sys->p_header[p_sys->i_header_pos+12],
+                 p_sys->i_sample_rate );
+        SetDWBE( &p_sys->p_header[p_sys->i_header_pos+16], 16 );
+
+        p_sys->i_header_pos = p_sys->i_header_size;
+    }
+    p_sys->i_header_pos = 0;
+
+    return VLC_SUCCESS;
+}
+
+/*****************************************************************************
+ * ParseMRL: parse the options contained in the MRL
+ *****************************************************************************/
+static void ParseMRL( input_thread_t *p_input )
+{
+    access_sys_t *p_sys = p_input->p_access_data;
+
+    char *psz_dup = strdup( p_input->psz_name );
+    char *psz_parser = psz_dup;
 
     while( *psz_parser && *psz_parser != ':' )
     {
@@ -240,8 +494,8 @@ static int AccessOpen( vlc_object_t *p_this )
             *psz_parser++ = '\0';
             if( !strncmp( psz_parser, "channel=", strlen( "channel=" ) ) )
             {
-                p_sys->i_channel =
-                    strtol( psz_parser + strlen( "channel=" ), &psz_parser, 0 );
+                p_sys->i_channel = strtol( psz_parser + strlen( "channel=" ),
+                                           &psz_parser, 0 );
             }
             else if( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
             {
@@ -271,14 +525,22 @@ static int AccessOpen( vlc_object_t *p_this )
                     p_sys->i_norm = strtol( psz_parser, &psz_parser, 0 );
                 }
             }
-            else if( !strncmp( psz_parser, "frequency=", strlen( "frequency=" ) ) )
+            else if( !strncmp( psz_parser, "frequency=",
+                               strlen( "frequency=" ) ) )
             {
                 p_sys->i_frequency =
-                    strtol( psz_parser + strlen( "frequency=" ), &psz_parser, 0 );
+                    strtol( psz_parser + strlen( "frequency=" ),
+                            &psz_parser, 0 );
+                if( p_sys->i_frequency < 30000 )
+                {
+                    msg_Warn( p_input, "v4l syntax has changed : "
+                              "'frequency' is now channel frequency in kHz");
+                }
             }
             else if( !strncmp( psz_parser, "audio=", strlen( "audio=" ) ) )
             {
-                p_sys->i_audio = strtol( psz_parser + strlen( "audio=" ), &psz_parser, 0 );
+                p_sys->i_audio = strtol( psz_parser + strlen( "audio=" ),
+                                         &psz_parser, 0 );
             }
             else if( !strncmp( psz_parser, "size=", strlen( "size=" ) ) )
             {
@@ -319,32 +581,38 @@ static int AccessOpen( vlc_object_t *p_this )
                     p_sys->i_width = strtol( psz_parser, &psz_parser, 0 );
                     if( *psz_parser == 'x' || *psz_parser == 'X')
                     {
-                        p_sys->i_height = strtol( psz_parser + 1, &psz_parser, 0 );
+                        p_sys->i_height = strtol( psz_parser + 1,
+                                                  &psz_parser, 0 );
                     }
-                    msg_Dbg( p_input, "WxH %dx%d", p_sys->i_width, p_sys->i_height );
+                    msg_Dbg( p_input, "WxH %dx%d", p_sys->i_width,
+                             p_sys->i_height );
                 }
             }
-            else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
+            else if( !strncmp( psz_parser, "brightness=", strlen( "brightness=" ) ) )
             {
-                p_sys->i_tuner = strtol( psz_parser + strlen( "tuner=" ), &psz_parser, 0 );
+                p_sys->i_brightness = strtol( psz_parser + strlen( "brightness=" ),
+                                              &psz_parser, 0 );
             }
-            else if( !strncmp( psz_parser, "codec=", strlen( "codec=" ) ) )
+            else if( !strncmp( psz_parser, "colour=", strlen( "colour=" ) ) )
             {
-                psz_parser += strlen( "codec=" );
-                if( !strncmp( psz_parser, "mpeg4", strlen( "mpeg4" ) ) )
-                {
-                    p_sys->i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
-                }
-                else if( !strncmp( psz_parser, "mpeg1", strlen( "mpeg1" ) ) )
-                {
-                    p_sys->i_codec = VLC_FOURCC( 'm', 'p', '1', 'v' );
-                }
-                else
-                {
-                    msg_Warn( p_input, "unknow codec" );
-                }
+                p_sys->i_colour = strtol( psz_parser + strlen( "colour=" ),
+                                          &psz_parser, 0 );
+            }
+            else if( !strncmp( psz_parser, "hue=", strlen( "hue=" ) ) )
+            {
+                p_sys->i_hue = strtol( psz_parser + strlen( "hue=" ), 
+                                       &psz_parser, 0 );
+            }
+            else if( !strncmp( psz_parser, "contrast=", strlen( "contrast=" ) ) )
+            {
+                p_sys->i_contrast = strtol( psz_parser + strlen( "contrast=" ),
+                                            &psz_parser, 0 );
+            }
+            else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
+            {
+                p_sys->i_tuner = strtol( psz_parser + strlen( "tuner=" ),
+                                         &psz_parser, 0 );
             }
-#ifdef _V4L_AUDIO_
             else if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
             {
                 int  i_len;
@@ -363,9 +631,12 @@ static int AccessOpen( vlc_object_t *p_this )
 
                 psz_parser += i_len;
             }
-            else if( !strncmp( psz_parser, "samplerate=", strlen( "samplerate=" ) ) )
+            else if( !strncmp( psz_parser, "samplerate=",
+                               strlen( "samplerate=" ) ) )
             {
-                p_sys->i_sample_rate = strtol( psz_parser + strlen( "samplerate=" ), &psz_parser, 0 );
+                p_sys->i_sample_rate =
+                    strtol( psz_parser + strlen( "samplerate=" ),
+                            &psz_parser, 0 );
             }
             else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
             {
@@ -379,10 +650,34 @@ static int AccessOpen( vlc_object_t *p_this )
 
                 p_sys->b_stereo = VLC_FALSE;
             }
-#endif
+            else if( !strncmp( psz_parser, "mjpeg", strlen( "mjpeg" ) ) )
+            {
+                psz_parser += strlen( "mjpeg" );
+
+                p_sys->b_mjpeg = VLC_TRUE;
+            }
+            else if( !strncmp( psz_parser, "decimation=",
+                        strlen( "decimation=" ) ) )
+            {
+                p_sys->i_decimation =
+                    strtol( psz_parser + strlen( "decimation=" ),
+                            &psz_parser, 0 );
+            }
+            else if( !strncmp( psz_parser, "quality=",
+                        strlen( "quality=" ) ) )
+            {
+                p_sys->i_quality =
+                    strtol( psz_parser + strlen( "quality=" ),
+                            &psz_parser, 0 );
+            }
+            else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
+            {
+                p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
+                                       &psz_parser );
+            }
             else
             {
-                msg_Warn( p_input, "unknow option" );
+                msg_Warn( p_input, "unknown option" );
             }
 
             while( *psz_parser && *psz_parser != ':' )
@@ -399,34 +694,33 @@ static int AccessOpen( vlc_object_t *p_this )
 
     if( *psz_dup )
     {
-        p_sys->psz_video_device = strdup( psz_dup );
-    }
-    else
-    {
-        p_sys->psz_video_device = strdup( "/dev/video" );
-    }
-    msg_Dbg( p_input, "video device=`%s'", p_sys->psz_video_device );
-
-#ifdef _V4L_AUDIO_
-    if( p_sys->psz_adev && *p_sys->psz_adev == '\0' )
-    {
-        p_sys->psz_adev = strdup( "/dev/dsp" );
+        p_sys->psz_device = strdup( psz_dup );
     }
-    msg_Dbg( p_input, "audio device=`%s'", p_sys->psz_adev );
-#endif
+    if( psz_dup ) free( psz_dup );
+}
 
+/*****************************************************************************
+ * OpenVideoDev:
+ *****************************************************************************/
+int OpenVideoDev( input_thread_t *p_input, char *psz_device )
+{
+    access_sys_t *p_sys = p_input->p_access_data;
+    int i_fd;
 
+    struct video_channel vid_channel;
+    struct mjpeg_params mjpeg;
+    int i;
 
-    if( ( p_sys->fd = open( p_sys->psz_video_device, O_RDWR ) ) < 0 )
+    if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
     {
-        msg_Err( p_input, "cannot open device" );
-        goto failed;
+        msg_Err( p_input, "cannot open device (%s)", strerror( errno ) );
+        goto vdev_failed;
     }
 
-    if( ioctl( p_sys->fd, VIDIOCGCAP, &p_sys->vid_cap ) < 0 )
+    if( ioctl( i_fd, VIDIOCGCAP, &p_sys->vid_cap ) < 0 )
     {
-        msg_Err( p_input, "cannot get capabilities" );
-        goto failed;
+        msg_Err( p_input, "cannot get capabilities (%s)", strerror( errno ) );
+        goto vdev_failed;
     }
 
     msg_Dbg( p_input,
@@ -451,27 +745,28 @@ static int AccessOpen( vlc_object_t *p_this )
     if( p_sys->i_width < p_sys->vid_cap.minwidth ||
         p_sys->i_width > p_sys->vid_cap.maxwidth )
     {
-        msg_Dbg( p_input, "invalid width" );
+        msg_Dbg( p_input, "invalid width %i", p_sys->i_width );
         p_sys->i_width = 0;
     }
     if( p_sys->i_height < p_sys->vid_cap.minheight ||
         p_sys->i_height > p_sys->vid_cap.maxheight )
     {
-        msg_Dbg( p_input, "invalid height" );
+        msg_Dbg( p_input, "invalid height %i", p_sys->i_height );
         p_sys->i_height = 0;
     }
 
-    if( !( p_sys->vid_cap.type&VID_TYPE_CAPTURE ) )
+    if( !( p_sys->vid_cap.type & VID_TYPE_CAPTURE ) )
     {
         msg_Err( p_input, "cannot grab" );
-        goto failed;
+        goto vdev_failed;
     }
 
     vid_channel.channel = p_sys->i_channel;
-    if( ioctl( p_sys->fd, VIDIOCGCHAN, &vid_channel ) < 0 )
+    if( ioctl( i_fd, VIDIOCGCHAN, &vid_channel ) < 0 )
     {
-        msg_Err( p_input, "cannot get channel infos" );
-        goto failed;
+        msg_Err( p_input, "cannot get channel infos (%s)",
+                          strerror( errno ) );
+        goto vdev_failed;
     }
     msg_Dbg( p_input,
              "setting channel %s(%d) %d tuners flags=0x%x type=0x%x norm=0x%x",
@@ -489,10 +784,10 @@ static int AccessOpen( vlc_object_t *p_this )
     }
 
     vid_channel.norm = p_sys->i_norm;
-    if( ioctl( p_sys->fd, VIDIOCSCHAN, &vid_channel ) < 0 )
+    if( ioctl( i_fd, VIDIOCSCHAN, &vid_channel ) < 0 )
     {
-        msg_Err( p_input, "cannot set channel" );
-        goto failed;
+        msg_Err( p_input, "cannot set channel (%s)", strerror( errno ) );
+        goto vdev_failed;
     }
 
     if( vid_channel.flags & VIDEO_VC_TUNER )
@@ -504,40 +799,45 @@ static int AccessOpen( vlc_object_t *p_this )
         if( p_sys->i_tuner >= 0 )
         {
             vid_tuner.tuner = p_sys->i_tuner;
-            if( ioctl( p_sys->fd, VIDIOCGTUNER, &vid_tuner ) < 0 )
+            if( ioctl( i_fd, VIDIOCGTUNER, &vid_tuner ) < 0 )
             {
-                msg_Err( p_input, "cannot get tuner" );
-                goto failed;
+                msg_Err( p_input, "cannot get tuner (%s)", strerror( errno ) );
+                goto vdev_failed;
             }
-            msg_Dbg( p_input,
-                     "tuner %s low=%d high=%d, flags=0x%x mode=0x%x signal=0x%x",
-                     vid_tuner.name,
-                     vid_tuner.rangelow, vid_tuner.rangehigh,
-                     vid_tuner.flags,
-                     vid_tuner.mode,
-                     vid_tuner.signal );
-
-            msg_Dbg( p_input,
-                     "setting tuner %s (%d)",
+            msg_Dbg( p_input, "tuner %s low=%d high=%d, flags=0x%x "
+                     "mode=0x%x signal=0x%x",
+                     vid_tuner.name, vid_tuner.rangelow, vid_tuner.rangehigh,
+                     vid_tuner.flags, vid_tuner.mode, vid_tuner.signal );
+
+            msg_Dbg( p_input, "setting tuner %s (%d)",
                      vid_tuner.name, vid_tuner.tuner );
 
-            //vid_tuner.mode = p_sys->i_norm; /* FIXME FIXME to be checked FIXME FIXME */
-            if( ioctl( p_sys->fd, VIDIOCSTUNER, &vid_tuner ) < 0 )
+            /* FIXME FIXME to be checked FIXME FIXME */
+            //vid_tuner.mode = p_sys->i_norm;
+            if( ioctl( i_fd, VIDIOCSTUNER, &vid_tuner ) < 0 )
             {
-                msg_Err( p_input, "cannot set tuner" );
-                goto failed;
+                msg_Err( p_input, "cannot set tuner (%s)", strerror( errno ) );
+                goto vdev_failed;
             }
         }
 #endif
+
+        /* Show a warning if frequency is < than 30000.
+         * User is certainly usint old syntax. */
+
+
         /* set frequency */
         if( p_sys->i_frequency >= 0 )
         {
-            if( ioctl( p_sys->fd, VIDIOCSFREQ, &p_sys->i_frequency ) < 0 )
+            int driver_frequency = p_sys->i_frequency * 16 /1000;
+            if( ioctl( i_fd, VIDIOCSFREQ, &driver_frequency ) < 0 )
             {
-                msg_Err( p_input, "cannot set frequency" );
-                goto failed;
+                msg_Err( p_input, "cannot set frequency (%s)",
+                                  strerror( errno ) );
+                goto vdev_failed;
             }
-            msg_Dbg( p_input, "frequency %d", p_sys->i_frequency );
+            msg_Dbg( p_input, "frequency %d (%d)", p_sys->i_frequency,
+                                                   driver_frequency );
         }
     }
 
@@ -550,74 +850,103 @@ static int AccessOpen( vlc_object_t *p_this )
         if( p_sys->i_audio >= 0 )
         {
             vid_audio.audio = p_sys->i_audio;
-            if( ioctl( p_sys->fd, VIDIOCGAUDIO, &vid_audio ) < 0 )
+            if( ioctl( i_fd, VIDIOCGAUDIO, &vid_audio ) < 0 )
             {
-                msg_Err( p_input, "cannot get audio" );
-                goto failed;
+                msg_Err( p_input, "cannot get audio (%s)", strerror( errno ) );
+                goto vdev_failed;
             }
 
             /* unmute audio */
             vid_audio.flags &= ~VIDEO_AUDIO_MUTE;
 
-            if( ioctl( p_sys->fd, VIDIOCSAUDIO, &vid_audio ) < 0 )
+            if( ioctl( i_fd, VIDIOCSAUDIO, &vid_audio ) < 0 )
             {
-                msg_Err( p_input, "cannot set audio" );
-                goto failed;
+                msg_Err( p_input, "cannot set audio (%s)", strerror( errno ) );
+                goto vdev_failed;
             }
+        }
 
-#ifdef _V4L_AUDIO_
-            if( p_sys->psz_adev )
-            {
-                int    i_format;
-                if( ( p_sys->fd_audio = open( p_sys->psz_adev, O_RDONLY ) ) < 0 )
-                {
-                    msg_Err( p_input, "cannot open audio device" );
-                    goto failed;
-                }
-
-                i_format = AFMT_S16_LE;
-                if( ioctl( p_sys->fd_audio, SNDCTL_DSP_SETFMT, &i_format ) < 0 ||
-                    i_format != AFMT_S16_LE )
-                {
-                    msg_Err( p_input, "cannot set audio format (16b little endian)" );
-                    goto failed;
-                }
-
-                if( ioctl( p_sys->fd_audio, SNDCTL_DSP_STEREO, &p_sys->b_stereo ) < 0 )
-                {
-                    msg_Err( p_input, "cannot set audio channels count" );
-                    goto failed;
-                }
-
-                if( ioctl( p_sys->fd_audio, SNDCTL_DSP_SPEED, &p_sys->i_sample_rate ) < 0 )
-                {
-                    msg_Err( p_input, "cannot set audio sample rate" );
-                    goto failed;
-                }
+    }
 
-                msg_Dbg( p_input,
-                         "adev=`%s' %s %dHz",
-                         p_sys->psz_adev,
-                         p_sys->b_stereo ? "stereo" : "mono",
-                         p_sys->i_sample_rate );
+    /* establish basic params with input and norm before feeling width
+     * or height */
+    if( p_sys->b_mjpeg )
+    {
+        struct quicktime_mjpeg_app1 *p_app1;
+        int32_t i_offset;
 
-                p_sys->i_audio_frame_size = 0;
-                p_sys->i_audio_frame_size_allocated = 10*1024;
-                p_sys->p_audio_frame = malloc( p_sys->i_audio_frame_size_allocated );
-            }
-#endif
+        if( ioctl( i_fd, MJPIOC_G_PARAMS, &mjpeg ) < 0 )
+        {
+            msg_Err( p_input, "cannot get mjpeg params (%s)",
+                              strerror( errno ) );
+            goto vdev_failed;
+        }
+        mjpeg.input = p_sys->i_channel;
+        mjpeg.norm  = p_sys->i_norm;
+        mjpeg.decimation = p_sys->i_decimation;
+
+        if( p_sys->i_width )
+            mjpeg.img_width = p_sys->i_width / p_sys->i_decimation;
+        if( p_sys->i_height )
+            mjpeg.img_height = p_sys->i_height / p_sys->i_decimation;
+
+        /* establish Quicktime APP1 marker while we are here */
+        mjpeg.APPn = 1;
+        mjpeg.APP_len = 40;
+
+        /* aligned */
+        p_app1 = (struct quicktime_mjpeg_app1 *)mjpeg.APP_data;
+        p_app1->i_reserved = 0;
+        p_app1->i_tag = VLC_FOURCC( 'm','j','p','g' );
+        p_app1->i_field_size = 0;
+        p_app1->i_padded_field_size = 0;
+        p_app1->i_next_field = 0;
+        /* XXX WARNING XXX */
+        /* these's nothing magic about these values.  We are dangerously
+         * assuming the encoder card is encoding mjpeg-a and is not throwing
+         * in marker tags we aren't expecting.  It's bad enough we have to
+         * search through the jpeg output for every frame we grab just to
+         * find the first field's end marker, so we take this risk to boost
+         * performance.
+         * This is really something the driver could do for us because this
+         * does conform to standards outside of Apple Quicktime.
+         */
+        i_offset = 0x2e;
+        p_app1->i_DQT_offset = hton32( i_offset );
+        i_offset = 0xb4;
+        p_app1->i_DHT_offset = hton32( i_offset );
+        i_offset = 0x258;
+        p_app1->i_SOF_offset = hton32( i_offset );
+        i_offset = 0x26b;
+        p_app1->i_SOS_offset = hton32( i_offset );
+        i_offset = 0x279;
+        p_app1->i_data_offset = hton32( i_offset );
+
+        /* SOF and SOS aren't specified by the mjpeg API because they aren't
+         * optional.  They will be present in the output. */
+        mjpeg.jpeg_markers = JPEG_MARKER_DHT | JPEG_MARKER_DQT;
+
+        if( ioctl( i_fd, MJPIOC_S_PARAMS, &mjpeg ) < 0 )
+        {
+            msg_Err( p_input, "cannot set mjpeg params (%s)",
+                              strerror( errno ) );
+            goto vdev_failed;
         }
+
+        p_sys->i_width = mjpeg.img_width * mjpeg.HorDcm;
+        p_sys->i_height = mjpeg.img_height * mjpeg.VerDcm *
+            mjpeg.field_per_buff;
     }
 
-    /* fix width/heigh */
-    if( p_sys->i_width == 0 || p_sys->i_height == 0 )
+    /* fix width/height */
+    if( !p_sys->b_mjpeg && ( p_sys->i_width == 0 || p_sys->i_height == 0 ) )
     {
         struct video_window vid_win;
 
-        if( ioctl( p_sys->fd, VIDIOCGWIN, &vid_win ) < 0 )
+        if( ioctl( i_fd, VIDIOCGWIN, &vid_win ) < 0 )
         {
-            msg_Err( p_input, "cannot get win" );
-            goto failed;
+            msg_Err( p_input, "cannot get win (%s)", strerror( errno ) );
+            goto vdev_failed;
         }
         p_sys->i_width  = vid_win.width;
         p_sys->i_height = vid_win.height;
@@ -625,322 +954,513 @@ static int AccessOpen( vlc_object_t *p_this )
         msg_Dbg( p_input, "will use %dx%d", p_sys->i_width, p_sys->i_height );
     }
 
-    if( ioctl( p_sys->fd, VIDIOCGMBUF, &p_sys->vid_mbuf ) < 0 )
-    {
-        msg_Err( p_input, "mmap unsupported" );
-        goto failed;
-    }
-
-    p_sys->p_video_mmap = mmap( 0, p_sys->vid_mbuf.size,
-                                PROT_READ|PROT_WRITE,
-                                MAP_SHARED,
-                                p_sys->fd,
-                                0 );
-    if( p_sys->p_video_mmap == MAP_FAILED )
-    {
-        /* FIXME -> normal read */
-        msg_Err( p_input, "mmap failed" );
-        goto failed;
-    }
-
     p_sys->p_video_frame = NULL;
 
-    /* init grabbing */
-    p_sys->vid_mmap.frame  = 0;
-    p_sys->vid_mmap.width  = p_sys->i_width;
-    p_sys->vid_mmap.height = p_sys->i_height;
-    p_sys->vid_mmap.format = VIDEO_PALETTE_YUV420P;
-    p_sys->i_chroma = VLC_FOURCC( 'I', '4', '2', '0' );
-    if( ioctl( p_sys->fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
+    if( !p_sys->b_mjpeg )
     {
-        msg_Warn( p_input, "%4.4s refused", (char*)&p_sys->i_chroma );
+        /* set hue/color/.. */
+        if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
+        {
+            struct video_picture vid_picture = p_sys->vid_picture;
 
-        p_sys->vid_mmap.format = VIDEO_PALETTE_YUV422P;
-        p_sys->i_chroma = VLC_FOURCC( 'I', '4', '2', '2' );
+            if( p_sys->i_brightness >= 0 && p_sys->i_brightness < 65536 )
+            {
+                vid_picture.brightness = p_sys->i_brightness;
+            }
+            if( p_sys->i_colour >= 0 && p_sys->i_colour < 65536 )
+            {
+                vid_picture.colour = p_sys->i_colour;
+            }
+            if( p_sys->i_hue >= 0 && p_sys->i_hue < 65536 )
+            {
+                vid_picture.hue = p_sys->i_hue;
+            }
+            if( p_sys->i_contrast  >= 0 && p_sys->i_contrast < 65536 )
+            {
+                vid_picture.contrast = p_sys->i_contrast;
+            }
+            if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
+            {
+                msg_Dbg( p_input, "v4l device uses brightness: %d", vid_picture.brightness );
+                msg_Dbg( p_input, "v4l device uses colour: %d", vid_picture.colour );
+                msg_Dbg( p_input, "v4l device uses hue: %d", vid_picture.hue );
+                msg_Dbg( p_input, "v4l device uses contrast: %d", vid_picture.contrast );
+                p_sys->vid_picture = vid_picture;
+            }
+        }
 
-        if( ioctl( p_sys->fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
+        /* Find out video format used by device */
+        if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
         {
-            msg_Warn( p_input, "%4.4s refused", (char*)&p_sys->i_chroma );
+            struct video_picture vid_picture = p_sys->vid_picture;
+            vlc_value_t val;
+            int i;
+
+            vid_picture.palette = 0;
+            p_sys->i_fourcc = 0;
+
+            var_Create( p_input, "v4l-chroma",
+                        VLC_VAR_STRING | VLC_VAR_DOINHERIT );
+            var_Get( p_input, "v4l-chroma", &val );
+            if( val.psz_string && strlen( val.psz_string ) >= 4 )
+            {
+                int i_chroma =
+                    VLC_FOURCC( val.psz_string[0], val.psz_string[1],
+                                val.psz_string[2], val.psz_string[3] );
+
+                /* Find out v4l chroma code */
+                for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ )
+                {
+                    if( v4lchroma_to_fourcc[i].i_fourcc == i_chroma )
+                    {
+                        vid_picture.palette = v4lchroma_to_fourcc[i].i_v4l;
+                        break;
+                    }
+                }
+            }
+            if( val.psz_string ) free( val.psz_string );
+
+            if( vid_picture.palette &&
+                !ioctl( i_fd, VIDIOCSPICT, &vid_picture ) )
+            {
+                p_sys->vid_picture = vid_picture;
+            }
+            else
+            {
+                /* Try to set the format to something easy to encode */
+                vid_picture.palette = VIDEO_PALETTE_YUV420P;
+                if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
+                {
+                    p_sys->vid_picture = vid_picture;
+                }
+                else
+                {
+                    vid_picture.palette = VIDEO_PALETTE_YUV422P;
+                    if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
+                    {
+                        p_sys->vid_picture = vid_picture;
+                    }
+                }
+            }
+
+            /* Find out final format */
+            for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ )
+            {
+                if( v4lchroma_to_fourcc[i].i_v4l == p_sys->vid_picture.palette)
+                {
+                    p_sys->i_fourcc = v4lchroma_to_fourcc[i].i_fourcc;
+                    break;
+                }
+            }
 
-            msg_Err( p_input, "chroma selection failed" );
-            goto failed;
         }
         else
         {
-            p_sys->i_video_frame_size = p_sys->i_width * p_sys->i_height * 2;
+            msg_Err( p_input, "ioctl VIDIOCGPICT failed" );
+            goto vdev_failed;
         }
     }
-    else
-    {
-        p_sys->i_video_frame_size = p_sys->i_width * p_sys->i_height * 3 / 2;
-    }
-    /* encoder part */
-    if( p_sys->i_codec != VLC_FOURCC( 0, 0, 0, 0 ) )
+
+    if( p_sys->b_mjpeg )
     {
-        msg_Dbg( p_input,
-                 "need a rencoder from %4.4s to %4.4s",
-                 (char*)&p_sys->i_chroma,
-                 (char*)&p_sys->i_codec );
-#define p_enc p_sys->p_encoder
-        p_enc = vlc_object_create( p_input, sizeof( video_encoder_t ) );
-        p_enc->i_codec = p_sys->i_codec;
-        p_enc->i_chroma= p_sys->i_chroma;
-        p_enc->i_width = p_sys->i_width;
-        p_enc->i_height= p_sys->i_height;
-        p_enc->i_aspect= 0;
-
-
-        p_enc->p_module = module_Need( p_enc,
-                                       "video encoder",
-                                       "$video-encoder" );
-        if( !p_enc->p_module )
-        {
-            msg_Warn( p_input,
-                      "no suitable encoder to %4.4s",
-                      (char*)&p_enc->i_codec );
-            vlc_object_destroy( p_enc );
-            goto failed;
-        }
+        int i;
 
-        /* *** init the codec *** */
-        if( p_enc->pf_init( p_enc ) )
-        {
-            msg_Err( p_input, "failed to initialize video encoder plugin" );
-            vlc_object_destroy( p_enc );
-            goto failed;
-        }
+        p_sys->mjpeg_buffers.count = 8;
+        p_sys->mjpeg_buffers.size = MJPEG_BUFFER_SIZE;
 
-        /* *** alloacted buffer *** */
-        if( p_enc->i_buffer_size <= 0 )
+        if( ioctl( i_fd, MJPIOC_REQBUFS, &p_sys->mjpeg_buffers ) < 0 )
         {
-            p_enc->i_buffer_size = 10 * p_enc->i_width * p_enc->i_height;
+            msg_Err( p_input, "mmap unsupported" );
+            goto vdev_failed;
         }
-        p_sys->i_video_frame_size = p_enc->i_buffer_size;
-        p_sys->i_video_frame_size_allocated = p_enc->i_buffer_size;
-        if( !( p_sys->p_video_frame = malloc( p_enc->i_buffer_size ) ) )
+
+        p_sys->p_video_mmap = mmap( 0,
+                p_sys->mjpeg_buffers.size * p_sys->mjpeg_buffers.count,
+                PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, 0 );
+        if( p_sys->p_video_mmap == MAP_FAILED )
         {
-            msg_Err( p_input, "out of memory" );
-            goto failed;
+            msg_Err( p_input, "mmap failed" );
+            goto vdev_failed;
         }
 
-        switch( p_sys->i_chroma )
+        p_sys->i_fourcc  = VLC_FOURCC( 'm','j','p','g' );
+        p_sys->i_frame_pos = -1;
+
+        /* queue up all the frames */
+        for( i = 0; i < (int)p_sys->mjpeg_buffers.count; i++ )
         {
-            case VLC_FOURCC( 'I', '4', '2', '0' ):
-                p_sys->pic.i_planes = 3;
-                p_sys->pic.p[0].i_pitch = p_sys->i_width;
-                p_sys->pic.p[0].i_lines = p_sys->i_height;
-                p_sys->pic.p[0].i_pixel_pitch = 1;
-                p_sys->pic.p[0].i_visible_pitch = p_sys->i_width;
-
-                p_sys->pic.p[1].i_pitch = p_sys->i_width / 2;
-                p_sys->pic.p[1].i_lines = p_sys->i_height / 2;
-                p_sys->pic.p[1].i_pixel_pitch = 1;
-                p_sys->pic.p[1].i_visible_pitch = p_sys->i_width / 2;
-
-                p_sys->pic.p[2].i_pitch = p_sys->i_width / 2;
-                p_sys->pic.p[2].i_lines = p_sys->i_height / 2;
-                p_sys->pic.p[2].i_pixel_pitch = 1;
-                p_sys->pic.p[2].i_visible_pitch = p_sys->i_width / 2;
-                break;
-            default:
-                msg_Err( p_input, "unsuported chroma" );
-                vlc_object_destroy( p_enc );
-                goto failed;
+            if( ioctl( i_fd, MJPIOC_QBUF_CAPT, &i ) < 0 )
+            {
+                msg_Err( p_input, "unable to queue frame" );
+                goto vdev_failed;
+            }
         }
-#undef p_enc
     }
     else
     {
-        p_sys->i_codec  = p_sys->i_chroma;
-        p_sys->p_encoder = NULL;
-    }
-
+        /* Fill in picture_t fields */
+        vout_InitPicture( VLC_OBJECT(p_input), &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_input, "unsupported chroma" );
+            goto vdev_failed;
+        }
+        p_sys->i_video_frame_size = 0;
+        for( i = 0; i < p_sys->pic.i_planes; i++ )
+        {
+            p_sys->i_video_frame_size += p_sys->pic.p[i].i_lines *
+              p_sys->pic.p[i].i_visible_pitch;
+        }
 
-    p_input->pf_read        = Read;
-    p_input->pf_seek        = NULL;
-    p_input->pf_set_area    = NULL;
-    p_input->pf_set_program = NULL;
+        msg_Dbg( p_input, "v4l device uses frame size: %i",
+                 p_sys->i_video_frame_size );
+        msg_Dbg( p_input, "v4l device uses chroma: %4.4s",
+                (char*)&p_sys->i_fourcc );
 
-    p_input->p_access_data  = p_sys;
+        /* Allocate mmap buffer */
+        if( ioctl( i_fd, VIDIOCGMBUF, &p_sys->vid_mbuf ) < 0 )
+        {
+            msg_Err( p_input, "mmap unsupported" );
+            goto vdev_failed;
+        }
 
-    vlc_mutex_lock( &p_input->stream.stream_lock );
-    p_input->stream.b_pace_control = 0;
-    p_input->stream.b_seekable = 0;
-    p_input->stream.p_selected_area->i_size = 0;
-    p_input->stream.p_selected_area->i_tell = 0;
-    p_input->stream.i_method = INPUT_METHOD_FILE;
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
+        p_sys->p_video_mmap = mmap( 0, p_sys->vid_mbuf.size,
+                                    PROT_READ|PROT_WRITE, MAP_SHARED,
+                                    i_fd, 0 );
+        if( p_sys->p_video_mmap == MAP_FAILED )
+        {
+            /* FIXME -> normal read */
+            msg_Err( p_input, "mmap failed" );
+            goto vdev_failed;
+        }
 
-    /* Update default_pts to a suitable value for access */
-    p_input->i_pts_delay = config_GetInt( p_input, "v4l-caching" ) * 1000;
+        /* init grabbing */
+        p_sys->vid_mmap.frame  = 0;
+        p_sys->vid_mmap.width  = p_sys->i_width;
+        p_sys->vid_mmap.height = p_sys->i_height;
+        p_sys->vid_mmap.format = p_sys->vid_picture.palette;
+        if( ioctl( i_fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
+        {
+            msg_Warn( p_input, "%4.4s refused", (char*)&p_sys->i_fourcc );
+            msg_Err( p_input, "chroma selection failed" );
+            goto vdev_failed;
+        }
+    }
+    return i_fd;
 
-    msg_Info( p_input, "v4l grabbing started" );
+vdev_failed:
 
-    /* create header */
-    p_sys->i_header_size = 8 + 20;
-    p_sys->i_header_pos  = 0;
-    p_sys->p_header      = malloc( p_sys->i_header_size );
-
-    memcpy(  &p_sys->p_header[0], ".v4l", 4 );
-    SetDWBE( &p_sys->p_header[4], 1 );
+    if( i_fd >= 0 ) close( i_fd );
+    return -1;
+}
 
-    memcpy(  &p_sys->p_header[ 8], "vids", 4 );
-    memcpy(  &p_sys->p_header[12], &p_sys->i_codec, 4 );
-    SetDWBE( &p_sys->p_header[16], p_sys->i_width );
-    SetDWBE( &p_sys->p_header[20], p_sys->i_height );
-    SetDWBE( &p_sys->p_header[24], 0 );
+/*****************************************************************************
+ * OpenAudioDev:
+ *****************************************************************************/
+int OpenAudioDev( input_thread_t *p_input, char *psz_device )
+{
+    access_sys_t *p_sys = p_input->p_access_data;
+    int i_fd, i_format;
 
-#ifdef _V4L_AUDIO_
-    if( p_sys->fd_audio > 0 )
+    if( (i_fd = open( psz_device, O_RDONLY | O_NONBLOCK )) < 0 )
     {
-        p_sys->i_header_size += 20;
-        p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header_size );
+        msg_Err( p_input, "cannot open audio device (%s)", strerror( errno ) );
+        goto adev_fail;
+    }
 
-        SetDWBE( &p_sys->p_header[4], 2 );
+    i_format = AFMT_S16_LE;
+    if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
+        || i_format != AFMT_S16_LE )
+    {
+        msg_Err( p_input, "cannot set audio format (16b little endian) "
+                 "(%s)", strerror( errno ) );
+        goto adev_fail;
+    }
 
-        memcpy(  &p_sys->p_header[28], "auds", 4 );
-        memcpy(  &p_sys->p_header[32], "araw", 4 );
-        SetDWBE( &p_sys->p_header[36], p_sys->b_stereo ? 2 : 1 );
-        SetDWBE( &p_sys->p_header[40], p_sys->i_sample_rate );
-        SetDWBE( &p_sys->p_header[44], 16 );
+    if( ioctl( i_fd, SNDCTL_DSP_STEREO,
+               &p_sys->b_stereo ) < 0 )
+    {
+        msg_Err( p_input, "cannot set audio channels count (%s)",
+                 strerror( errno ) );
+        goto adev_fail;
     }
-#endif
-    return VLC_SUCCESS;
 
-failed:
-    free( p_sys->psz_video_device );
-    if( p_sys->fd >= 0 )
+    if( ioctl( i_fd, SNDCTL_DSP_SPEED,
+               &p_sys->i_sample_rate ) < 0 )
     {
-        close( p_sys->fd );
+        msg_Err( p_input, "cannot set audio sample rate (%s)",
+                 strerror( errno ) );
+        goto adev_fail;
     }
-    free( p_sys );
-    return VLC_EGENERIC;
+
+    msg_Dbg( p_input, "openened adev=`%s' %s %dHz",
+             psz_device, p_sys->b_stereo ? "stereo" : "mono",
+             p_sys->i_sample_rate );
+
+    p_sys->i_audio_frame_size = 0;
+    p_sys->i_audio_frame_size_allocated = 6*1024;
+    p_sys->p_audio_frame = malloc( p_sys->i_audio_frame_size_allocated );
+
+    return i_fd;
+
+ adev_fail:
+
+    if( i_fd >= 0 ) close( i_fd );
+    return -1;
 }
 
 /*****************************************************************************
- * V4lClose: close device
+ * AccessClose: close device, free resources
  *****************************************************************************/
 static void AccessClose( vlc_object_t *p_this )
 {
-    input_thread_t  *p_input = (input_thread_t *)p_this;
-    access_sys_t    *p_sys   = p_input->p_access_data;
+    input_thread_t *p_input = (input_thread_t *)p_this;
+    access_sys_t   *p_sys   = p_input->p_access_data;
 
-    msg_Info( p_input, "v4l grabbing stoped" );
+    if( p_sys->psz_device ) free( p_sys->psz_device );
+    if( p_sys->psz_vdev )   free( p_sys->psz_vdev );
+    if( p_sys->psz_adev )   free( p_sys->psz_adev );
+    if( p_sys->fd_video >= 0 ) close( p_sys->fd_video );
+    if( p_sys->fd_audio >= 0 ) close( p_sys->fd_audio );
 
-    free( p_sys->psz_video_device );
-    close( p_sys->fd );
+    if( p_sys->p_header ) free( p_sys->p_header );
+    if( p_sys->p_audio_frame ) free( p_sys->p_audio_frame );
 
-    if( p_sys->p_encoder )
+    if( p_sys->b_mjpeg )
     {
-        p_sys->p_encoder->pf_end( p_sys->p_encoder );
-
-        module_Unneed( p_sys->p_encoder,
-                       p_sys->p_encoder->p_module );
-        vlc_object_destroy( p_sys->p_encoder );
+        int i_noframe = -1;
+        ioctl( p_sys->fd_video, MJPIOC_QBUF_CAPT, &i_noframe );
+    }
 
-        free( p_sys->p_video_frame );
+    if( p_sys->p_video_mmap && p_sys->p_video_mmap != MAP_FAILED )
+    {
+        if( p_sys->b_mjpeg )
+            munmap( p_sys->p_video_mmap, p_sys->mjpeg_buffers.size *
+                    p_sys->mjpeg_buffers.count );
+        else
+            munmap( p_sys->p_video_mmap, p_sys->vid_mbuf.size );
     }
+
     free( p_sys );
 }
 
-#ifdef _V4L_AUDIO_
+/*****************************************************************************
+ * GrabAudio: grab audio
+ *****************************************************************************/
 static int GrabAudio( input_thread_t * p_input,
                       uint8_t **pp_data,
-                      int      *pi_data )
+                      int      *pi_data,
+                      mtime_t  *pi_pts )
 {
     access_sys_t    *p_sys   = p_input->p_access_data;
-    fd_set  fds;
-    struct timeval  timeout;
-    int i_ret;
+    struct audio_buf_info buf_info;
     int i_read;
+    int i_correct;
 
-    /* we first try to get an audio frame */
-    FD_ZERO( &fds );
-    FD_SET( p_sys->fd_audio, &fds );
+    i_read = read( p_sys->fd_audio, p_sys->p_audio_frame,
+                   p_sys->i_audio_frame_size_allocated );
 
-    timeout.tv_sec  = 0;
-    timeout.tv_usec = 0;
-
-    i_ret = select( p_sys->fd_audio + 1, &fds, NULL, NULL, &timeout );
-
-    if( i_ret < 0 && errno != EINTR )
-    {
-        msg_Warn( p_input, "audio select failed" );
-        return VLC_EGENERIC;
-    }
-
-    if( !FD_ISSET( p_sys->fd_audio , &fds) )
+    if( i_read <= 0 )
     {
         return VLC_EGENERIC;
     }
 
-    i_read = read( p_sys->fd_audio, p_sys->p_audio_frame, p_sys->i_audio_frame_size_allocated );
+    p_sys->i_audio_frame_size = i_read;
 
-    if( i_read <= 0 )
+    /* from vls : correct the date because of kernel buffering */
+    i_correct = i_read;
+    if( ioctl( p_sys->fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
     {
-        return VLC_EGENERIC;
+        i_correct += buf_info.bytes;
     }
 
-    p_sys->i_audio_frame_size = i_read;
-
 
     *pp_data = p_sys->p_audio_frame;
     *pi_data = p_sys->i_audio_frame_size;
+    *pi_pts  = mdate() - (mtime_t)1000000 * (mtime_t)i_correct /
+                         2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
     return VLC_SUCCESS;
 }
-#endif
 
-static int GrabVideo( input_thread_t * p_input,
-                      uint8_t **pp_data,
-                      int      *pi_data )
+/*****************************************************************************
+ * GrabVideo:
+ *****************************************************************************/
+static uint8_t *GrabCapture( input_thread_t *p_input )
 {
-    access_sys_t    *p_sys   = p_input->p_access_data;
+    access_sys_t *p_sys = p_input->p_access_data;
+    int i_captured_frame = p_sys->i_frame_pos;
 
-    p_sys->vid_mmap.frame = ( p_sys->i_frame_pos + 1 ) % p_sys->vid_mbuf.frames;
-    for( ;; )
+    p_sys->vid_mmap.frame = (p_sys->i_frame_pos + 1) % p_sys->vid_mbuf.frames;
+
+    while( ioctl( p_sys->fd_video, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
     {
-        if( ioctl( p_sys->fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) >= 0 )
+        if( errno != EAGAIN )
         {
-            break;
+            msg_Err( p_input, "failed capturing new frame" );
+            return NULL;
         }
 
-        if( errno != EAGAIN )
+        if( p_input->b_die )
+        {
+            return NULL;
+        }
+
+        msg_Dbg( p_input, "grab failed, trying again" );
+    }
+
+    while( ioctl(p_sys->fd_video, VIDIOCSYNC, &p_sys->i_frame_pos) < 0 )
+    {
+        if( errno != EAGAIN && errno != EINTR )    
+        {
+            msg_Err( p_input, "failed syncing new frame" );
+            return NULL;
+        }
+    }
+
+    p_sys->i_frame_pos = p_sys->vid_mmap.frame;
+    /* leave i_video_frame_size alone */
+    return p_sys->p_video_mmap + p_sys->vid_mbuf.offsets[i_captured_frame];
+}
+
+static uint8_t *GrabMJPEG( input_thread_t *p_input )
+{
+    access_sys_t *p_sys = p_input->p_access_data;
+    struct mjpeg_sync sync;
+    uint8_t *p_frame, *p_field, *p;
+    uint16_t tag;
+    uint32_t i_size;
+    struct quicktime_mjpeg_app1 *p_app1 = NULL;
+
+    /* re-queue the last frame we sync'd */
+    if( p_sys->i_frame_pos != -1 )
+    {
+        while( ioctl( p_sys->fd_video, MJPIOC_QBUF_CAPT,
+                                       &p_sys->i_frame_pos ) < 0 )
         {
-            msg_Err( p_input, "failed while grabbing new frame" );
-            return( -1 );
+            if( errno != EAGAIN && errno != EINTR )    
+            {
+                msg_Err( p_input, "failed capturing new frame" );
+                return NULL;
+            }
         }
-        msg_Dbg( p_input, "another try ?" );
     }
 
-    //msg_Warn( p_input, "grab a new frame" );
+    /* sync on the next frame */
+    while( ioctl( p_sys->fd_video, MJPIOC_SYNC, &sync ) < 0 )
+    {
+        if( errno != EAGAIN && errno != EINTR )    
+        {
+            msg_Err( p_input, "failed syncing new frame" );
+            return NULL;
+        }
+    }
 
-    while( ioctl(p_sys->fd, VIDIOCSYNC, &p_sys->i_frame_pos) < 0 &&
-           ( errno == EAGAIN || errno == EINTR ) );
+    p_sys->i_frame_pos = sync.frame;
+    p_frame = p_sys->p_video_mmap + p_sys->mjpeg_buffers.size * sync.frame;
 
+    /* p_frame now points to the data.  fix up the Quicktime APP1 marker */
+    tag = 0xffd9;
+    tag = hton16( tag );
+    p_field = p_frame;
 
-    p_sys->i_frame_pos = p_sys->vid_mmap.frame;
+    /* look for EOI */
+    p = memmem( p_field, sync.length, &tag, 2 );
 
-    if( p_sys->p_encoder )
+    if( p )
     {
-        p_sys->pic.p[0].p_pixels = p_sys->p_video_mmap + p_sys->vid_mbuf.offsets[p_sys->i_frame_pos];
-        p_sys->pic.p[1].p_pixels = p_sys->pic.p[0].p_pixels +
-                                        p_sys->pic.p[0].i_pitch * p_sys->pic.p[0].i_lines;
-        p_sys->pic.p[2].p_pixels = p_sys->pic.p[1].p_pixels +
-                                        p_sys->pic.p[1].i_pitch * p_sys->pic.p[1].i_lines;
-
-        p_sys->i_video_frame_size = p_sys->i_video_frame_size_allocated;
-        p_sys->p_encoder->pf_encode( p_sys->p_encoder,
-                                     &p_sys->pic,
-                                     p_sys->p_video_frame,
-                                     &p_sys->i_video_frame_size );
+        p += 2; /* data immediately following EOI */
+        /* UNALIGNED! */
+        p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
+
+        i_size = ((uint32_t)(p - p_field));
+        i_size = hton32( i_size );
+        memcpy( &p_app1->i_field_size, &i_size, 4 );
+
+        while( *p == 0xff && *(p+1) == 0xff )
+            p++;
+
+        i_size = ((uint32_t)(p - p_field));
+        i_size = hton32( i_size );
+        memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
     }
-    else
+
+    tag = 0xffd8;
+    tag = hton16( tag );
+    p_field = memmem( p, sync.length - (size_t)(p - p_frame), &tag, 2 );
+
+    if( p_field )
     {
-        p_sys->p_video_frame = p_sys->p_video_mmap + p_sys->vid_mbuf.offsets[p_sys->i_frame_pos];
+        i_size = (uint32_t)(p_field - p_frame);
+        i_size = hton32( i_size );
+        memcpy( &p_app1->i_next_field, &i_size, 4 );
+
+        /* UNALIGNED! */
+        p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
+        tag = 0xffd9;
+        tag = hton16( tag );
+        p = memmem( p_field, sync.length - (size_t)(p_field - p_frame),
+                &tag, 2 );
+
+        if( !p )
+        {
+            /* sometimes the second field doesn't have the EOI.  just put it
+             * there
+             */
+            p = p_frame + sync.length;
+            memcpy( p, &tag, 2 );
+            sync.length += 2;
+        }
+
+        p += 2;
+        i_size = (uint32_t)(p - p_field);
+        i_size = hton32( i_size );
+        memcpy( &p_app1->i_field_size, &i_size, 4 );
+        i_size = (uint32_t)(sync.length - (uint32_t)(p_field - p_frame));
+        i_size = hton32( i_size );
+        memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
     }
 
+    p_sys->i_video_frame_size = sync.length;
+    return p_frame;
+}
+
+static int GrabVideo( input_thread_t * p_input,
+                      uint8_t **pp_data,
+                      int *pi_data,
+                      mtime_t  *pi_pts )
+{
+    access_sys_t *p_sys   = p_input->p_access_data;
+    uint8_t      *p_frame;
+
+
+    if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
+    {
+        mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
+
+        /* Did we wait long enougth ? */
+        if( p_sys->i_video_pts + i_dur > mdate() )
+        {
+            return VLC_ETIMEOUT;
+        }
+    }
+
+    if( p_sys->b_mjpeg )
+        p_frame = GrabMJPEG( p_input );
+    else
+        p_frame = GrabCapture( p_input );
+
+    if( !p_frame )
+        return VLC_EGENERIC;
+
+    p_sys->i_video_pts   = mdate();
+    p_sys->p_video_frame = p_frame;
+
     *pp_data = p_sys->p_video_frame;
     *pi_data = p_sys->i_video_frame_size;
+    *pi_pts  = p_sys->i_video_pts;
+
     return VLC_SUCCESS;
 }
 
@@ -950,27 +1470,23 @@ static int GrabVideo( input_thread_t * p_input,
  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
  * bytes.
  *****************************************************************************/
-static int Read( input_thread_t * p_input, byte_t * p_buffer,
-                 size_t i_len )
+static int Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
 {
-    access_sys_t    *p_sys   = p_input->p_access_data;
-    int i_data = 0;
-    int i_stream;
+    access_sys_t *p_sys = p_input->p_access_data;
+    int          i_data = 0;
+    int          i_stream;
+    mtime_t      i_pts;
 
-    msg_Info( p_input, "access read" );
     while( i_len > 0 )
     {
-
         /* First copy header if any */
-        if( i_len > 0 && p_sys->i_header_pos < p_sys->i_header_size )
+        if( p_sys->i_header_pos < p_sys->i_header_size )
         {
             int i_copy;
 
             i_copy = __MIN( p_sys->i_header_size - p_sys->i_header_pos,
                             (int)i_len );
-            memcpy( p_buffer,
-                    &p_sys->p_header[p_sys->i_header_pos],
-                    i_copy );
+            memcpy( p_buffer, &p_sys->p_header[p_sys->i_header_pos], i_copy );
             p_sys->i_header_pos += i_copy;
 
             p_buffer += i_copy;
@@ -986,9 +1502,7 @@ static int Read( input_thread_t * p_input, byte_t * p_buffer,
             i_copy = __MIN( p_sys->i_data_size - p_sys->i_data_pos,
                             (int)i_len );
 
-            memcpy( p_buffer,
-                    &p_sys->p_data[p_sys->i_data_pos],
-                    i_copy );
+            memcpy( p_buffer, &p_sys->p_data[p_sys->i_data_pos], i_copy );
             p_sys->i_data_pos += i_copy;
 
             p_buffer += i_copy;
@@ -996,263 +1510,229 @@ static int Read( input_thread_t * p_input, byte_t * p_buffer,
             i_data += i_copy;
         }
 
-        if( i_len <= 0 )
+        /* The caller got what he wanted */
+        if( i_len == 0 )
         {
-            return( i_data );
+            return i_data;
         }
 
-        /* re fill data by grabbing audio/video */
-        p_sys->i_data_pos = 0;
+        /* Read no more than one frame at a time.
+         * That kills latency, especially for encoded v4l streams */
+        if( p_sys->i_data_size && p_sys->i_data_pos == p_sys->i_data_size )
+        {
+            p_sys->i_data_pos = 0; p_sys->i_data_size = 0;
+            return i_data;
+        }
 
-        /* try grabbing audio frames */
-#ifdef _V4L_AUDIO_
-        i_stream = 1;
-        if( p_sys->fd_audio < 0 || GrabAudio( p_input, &p_sys->p_data, &p_sys->i_data_size ) )
+        /* Re-fill data by grabbing audio/video */
+        p_sys->i_data_pos = p_sys->i_data_size = 0;
+
+        /* Try grabbing audio frames first */
+        i_stream = p_sys->i_streams - 1;
+        if( p_sys->fd_audio < 0 ||
+            GrabAudio( p_input, &p_sys->p_data,
+                       &p_sys->i_data_size, &i_pts ) != VLC_SUCCESS )
         {
-            /* and then get video frame if no audio */
+            int i_ret = VLC_ETIMEOUT;
+
+            /* Try grabbing video frame */
             i_stream = 0;
-            if( GrabVideo( p_input, &p_sys->p_data, &p_sys->i_data_size ) )
+            if( p_sys->fd_video > 0 )
+            {
+                i_ret = GrabVideo( p_input, &p_sys->p_data,
+                                   &p_sys->i_data_size, &i_pts );
+            }
+
+            /* No video or timeout */
+            if( i_ret == VLC_ETIMEOUT )
+            {
+                /* Sleep so we do not consume all the cpu, 10ms seems
+                 * like a good value (100fps) */
+                msleep( 10000 );
+                continue;
+            }
+            else if( i_ret != VLC_SUCCESS )
             {
+                msg_Err( p_input, "Error during capture!" );
                 return -1;
             }
         }
-#else
-        /* and then get video frame if no audio */
-        i_stream = 0;
-        if( GrabVideo( p_input, &p_sys->p_data, &p_sys->i_data_size ) )
-        {
-            return -1;
-        }
-#endif
 
         /* create pseudo header */
-        p_sys->i_header_size = 8;
+        p_sys->i_header_size = 16;
         p_sys->i_header_pos  = 0;
         SetDWBE( &p_sys->p_header[0], i_stream );
         SetDWBE( &p_sys->p_header[4], p_sys->i_data_size );
+        SetQWBE( &p_sys->p_header[8], i_pts );
     }
 
     return i_data;
 }
 
+
+
+/*****************************************************************************
+ * Demux: local prototypes
+ *****************************************************************************/
+struct demux_sys_t
+{
+    int         i_es;
+    es_out_id_t **es;
+};
+
+static int  Demux      ( input_thread_t * );
+
 /****************************************************************************
- * I. Demux Part
+ * DemuxOpen:
  ****************************************************************************/
-#define MAX_PACKETS_IN_FIFO 3
-
-static int  DemuxOpen  ( vlc_object_t *p_this )
+static int DemuxOpen( vlc_object_t *p_this )
 {
     input_thread_t *p_input = (input_thread_t *)p_this;
-    //demux_sys_t    *p_sys;
+    demux_sys_t    *p_sys;
 
     uint8_t        *p_peek;
-    int            i_streams;
+    int            i_es;
     int            i;
 
-    data_packet_t  *p_pk;
-
-    /* Initialize access plug-in structures. */
-    if( p_input->i_mtu == 0 )
-    {
-        /* Improve speed. */
-        p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE ;
-    }
-
     /* a little test to see if it's a v4l stream */
-    if( input_Peek( p_input, &p_peek, 8 ) < 8 )
+    if( stream_Peek( p_input->s, &p_peek, 8 ) < 8 )
     {
-        msg_Warn( p_input, "v4l plugin discarded (cannot peek)" );
-        return( VLC_EGENERIC );
+        return VLC_EGENERIC;
     }
 
-    if( strncmp( p_peek, ".v4l", 4 ) || GetDWBE( &p_peek[4] ) <= 0 )
+    if( strncmp( p_peek, ".v4l", 4 ) || ( i_es = GetDWBE( &p_peek[4] ) ) <= 0 )
     {
-        msg_Warn( p_input, "v4l plugin discarded (not a valid stream)" );
         return VLC_EGENERIC;
     }
 
-    /*  create one program */
     vlc_mutex_lock( &p_input->stream.stream_lock );
     if( input_InitStream( p_input, 0 ) == -1)
     {
         vlc_mutex_unlock( &p_input->stream.stream_lock );
         msg_Err( p_input, "cannot init stream" );
-        return( VLC_EGENERIC );
-    }
-    if( input_AddProgram( p_input, 0, 0) == NULL )
-    {
-        vlc_mutex_unlock( &p_input->stream.stream_lock );
-        msg_Err( p_input, "cannot add program" );
-        return( VLC_EGENERIC );
+        return VLC_EGENERIC;
     }
+    p_input->stream.i_mux_rate =  0 / 50;
+    vlc_mutex_unlock( &p_input->stream.stream_lock );
 
-    p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
-    p_input->stream.i_mux_rate =  0;
+    p_input->pf_demux = Demux;
+    p_input->pf_demux_control = demux_vaControlDefault;
+    p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
+    p_sys->i_es = 0;
+    p_sys->es   = NULL;
 
-    i_streams = GetDWBE( &p_peek[4] );
-    if( input_Peek( p_input, &p_peek, 8 + 20 * i_streams ) < 8 + 20 * i_streams )
+    if( stream_Peek( p_input->s, &p_peek, 8 + 20 * i_es ) < 8 + 20 * i_es )
     {
         msg_Err( p_input, "v4l plugin discarded (cannot peek)" );
-        return( VLC_EGENERIC );
+        return VLC_EGENERIC;
     }
     p_peek += 8;
 
-    for( i = 0; i < i_streams; i++ )
+    for( i = 0; i < i_es; i++ )
     {
-        es_descriptor_t *p_es;
-
+        es_format_t fmt;
 
-        p_es = input_AddES( p_input, p_input->stream.pp_programs[0], i + 1, 0 );
-        p_es->i_stream_id   = i + 1;
         if( !strncmp( p_peek, "auds", 4 ) )
         {
-#define wf ((WAVEFORMATEX*)p_es->p_waveformatex)
-            p_es->i_cat         = AUDIO_ES;
-            p_es->i_fourcc      = VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] );
-            p_es->p_waveformatex= malloc( sizeof( WAVEFORMATEX ) );
-
-            wf->wFormatTag      = WAVE_FORMAT_UNKNOWN;
-            wf->nChannels       = GetDWBE( &p_peek[8] );
-            wf->nSamplesPerSec  = GetDWBE( &p_peek[12] );
-            wf->wBitsPerSample  = GetDWBE( &p_peek[16] );
-            wf->nBlockAlign     = wf->wBitsPerSample * wf->nChannels / 8;
-            wf->nAvgBytesPerSec = wf->nBlockAlign * wf->nSamplesPerSec;
-            wf->cbSize          = 0;
-
-            msg_Dbg( p_input, "added new audio es %d channels %dHz",
-                     wf->nChannels,
-                     wf->nSamplesPerSec );
-
-            input_SelectES( p_input, p_es );
-#undef wf
+            es_format_Init( &fmt, AUDIO_ES,
+                VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) );
+
+            fmt.audio.i_channels = GetDWBE( &p_peek[8] );
+            fmt.audio.i_rate = GetDWBE( &p_peek[12] );
+            fmt.audio.i_bitspersample = GetDWBE( &p_peek[16] );
+            fmt.audio.i_blockalign = fmt.audio.i_channels *
+                                     fmt.audio.i_bitspersample / 8;
+            fmt.i_bitrate = fmt.audio.i_channels *
+                            fmt.audio.i_rate *
+                            fmt.audio.i_bitspersample;
+
+            msg_Dbg( p_input, "new audio es %d channels %dHz",
+                     fmt.audio.i_channels, fmt.audio.i_rate );
+
+            TAB_APPEND( p_sys->i_es, p_sys->es,
+                        es_out_Add( p_input->p_es_out, &fmt ) );
         }
         else if( !strncmp( p_peek, "vids", 4 ) )
         {
-#define bih ((BITMAPINFOHEADER*)p_es->p_bitmapinfoheader)
-            p_es->i_cat = VIDEO_ES;
-            p_es->i_fourcc      = VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] );
-
-            p_es->p_bitmapinfoheader = malloc( sizeof( BITMAPINFOHEADER ) );
-
-            bih->biSize     = sizeof( BITMAPINFOHEADER );
-            bih->biWidth    = GetDWBE( &p_peek[8] );
-            bih->biHeight   = GetDWBE( &p_peek[12] );
-            bih->biPlanes   = 0;
-            bih->biBitCount = 0;
-            bih->biCompression      = 0;
-            bih->biSizeImage= 0;
-            bih->biXPelsPerMeter    = 0;
-            bih->biYPelsPerMeter    = 0;
-            bih->biClrUsed  = 0;
-            bih->biClrImportant     = 0;
+            es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( p_peek[4], p_peek[5],
+                            p_peek[6], p_peek[7] ) );
+            fmt.video.i_width  = GetDWBE( &p_peek[8] );
+            fmt.video.i_height = GetDWBE( &p_peek[12] );
 
             msg_Dbg( p_input, "added new video es %4.4s %dx%d",
-                     (char*)&p_es->i_fourcc,
-                     bih->biWidth,
-                     bih->biHeight );
-
-            input_SelectES( p_input, p_es );
-#undef bih
+                     (char*)&fmt.i_codec,
+                     fmt.video.i_width, fmt.video.i_height );
+            TAB_APPEND( p_sys->i_es, p_sys->es,
+                        es_out_Add( p_input->p_es_out, &fmt ) );
         }
 
         p_peek += 20;
     }
 
-    p_input->stream.p_selected_program->b_is_ok = 1;
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
-
-    if( input_SplitBuffer( p_input, &p_pk, 8 + i_streams * 20 ) > 0 )
-    {
-        input_DeletePacket( p_input->p_method_data, p_pk );
-    }
+    /* Skip header */
+    stream_Read( p_input->s, NULL, 8 + 20 * i_es );
 
-    p_input->pf_demux = Demux;
     return VLC_SUCCESS;
 }
 
-static void DemuxClose ( vlc_object_t *p_this )
+/****************************************************************************
+ * DemuxClose:
+ ****************************************************************************/
+static void DemuxClose( vlc_object_t *p_this )
 {
-    return;
-}
+    input_thread_t *p_input = (input_thread_t *)p_this;
+    demux_sys_t    *p_sys = p_input->p_demux_data;
 
+    if( p_sys->i_es > 0 )
+    {
+        free( p_sys->es );
+    }
+    free( p_sys );
+}
 
-static int  Demux      ( input_thread_t *p_input )
+/****************************************************************************
+ * Demux:
+ ****************************************************************************/
+static int Demux( input_thread_t *p_input )
 {
-    es_descriptor_t *p_es;
-    pes_packet_t    *p_pes;
+    demux_sys_t *p_sys = p_input->p_demux_data;
+    block_t     *p_block;
 
-    int i_stream;
+    int i_es;
     int i_size;
+
     uint8_t *p_peek;
+    mtime_t i_pts;
 
-    if( input_Peek( p_input, &p_peek, 8 ) < 8 )
+    if( stream_Peek( p_input->s, &p_peek, 16 ) < 16 )
     {
         msg_Warn( p_input, "cannot peek (EOF ?)" );
-        return( 0 );
-    }
-
-    i_stream = GetDWBE( &p_peek[0] );
-    i_size   = GetDWBE( &p_peek[4] );
-
-    msg_Dbg( p_input, "stream=%d size=%d", i_stream, i_size );
-//    p_es = input_FindES( p_input, i_stream );
-    p_es = p_input->stream.p_selected_program->pp_es[i_stream];
-
-    if( p_es == NULL || p_es->p_decoder_fifo == NULL )
-    {
-        msg_Err( p_input, "cannot find decoder" );
         return 0;
     }
 
-    p_pes = input_NewPES( p_input->p_method_data );
-    if( p_pes == NULL )
+    i_es   = GetDWBE( &p_peek[0] );
+    if( i_es < 0 || i_es >= p_sys->i_es )
     {
-        msg_Warn( p_input, "cannot allocate PES" );
-        msleep( 1000 );
-        return( 1 );
+        msg_Err( p_input, "cannot find ES" );
+        return -1;
     }
-    i_size += 8;
-    while( i_size > 0 )
-    {
-        data_packet_t   *p_data;
-        int i_read;
 
-        if( (i_read = input_SplitBuffer( p_input,
-                                         &p_data,
-                                         __MIN( i_size, 10000 ) ) ) <= 0 )
-        {
-            input_DeletePES( p_input->p_method_data, p_pes );
-            return( 0 );
-        }
-        if( !p_pes->p_first )
-        {
-            p_pes->p_first = p_data;
-            p_pes->i_nb_data = 1;
-            p_pes->i_pes_size = i_read;
-        }
-        else
-        {
-            p_pes->p_last->p_next  = p_data;
-            p_pes->i_nb_data++;
-            p_pes->i_pes_size += i_read;
-        }
-        p_pes->p_last  = p_data;
-        i_size -= i_read;
-    }
-//    input_SplitBuffer( p_input, &p_pk, i_size + 8 );
-    p_pes->p_first->p_payload_start += 8;
+    i_size = GetDWBE( &p_peek[4] );
+    i_pts  = GetQWBE( &p_peek[8] );
 
-    vlc_mutex_lock( &p_es->p_decoder_fifo->data_lock );
-    if( p_es->p_decoder_fifo->i_depth >= MAX_PACKETS_IN_FIFO )
+    if( ( p_block = stream_Block( p_input->s, 16 + i_size ) ) == NULL )
     {
-        /* Wait for the decoder. */
-        vlc_cond_wait( &p_es->p_decoder_fifo->data_wait, &p_es->p_decoder_fifo->data_lock );
+        msg_Warn( p_input, "cannot read data" );
+        return 0;
     }
-    vlc_mutex_unlock( &p_es->p_decoder_fifo->data_lock );
 
-    p_pes->i_pts = mdate() + p_input->i_pts_delay;
+    p_block->p_buffer += 16;
+    p_block->i_buffer -= 16;
+
+    p_block->i_dts =
+    p_block->i_pts = i_pts + p_input->i_pts_delay;
 
-    input_DecodePES( p_es->p_decoder_fifo, p_pes );
+    es_out_Send( p_input->p_es_out, p_sys->es[i_es], p_block );
 
     return 1;
 }