root/modules/access/v4l.c

Revision 857535cbac9ec9bc54dec6f567dca6738c273e25, 52.4 kB (checked in by Antoine Cellerier <dionoea@videolan.org>, 2 weeks ago)

Fix segfault on 64 bit archs. (0 != NULL)

  • Property mode set to 100644
Line 
1 /*****************************************************************************
2  * v4l.c : Video4Linux input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2004 the VideoLAN team
5  * $Id$
6  *
7  * Author: Laurent Aimar <fenrir@via.ecp.fr>
8  *         Paul Forgey <paulf at aphrodite dot com>
9  *         Gildas Bazin <gbazin@videolan.org>
10  *         Benjamin Pracht <bigben at videolan dot org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_input.h>
38 #include <vlc_demux.h>
39 #include <vlc_access.h>
40 #include <vlc_vout.h>
41 #include <vlc_codecs.h>
42
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <sys/ioctl.h>
46 #include <unistd.h>
47 #include <sys/mman.h>
48 #include <errno.h>
49 #include <fcntl.h>
50
51 /* From GStreamer's v4l plugin:
52  * Because of some really cool feature in video4linux1, also known as
53  * 'not including sys/types.h and sys/time.h', we had to include it
54  * ourselves. In all their intelligence, these people decided to fix
55  * this in the next version (video4linux2) in such a cool way that it
56  * breaks all compilations of old stuff...
57  * The real problem is actually that linux/time.h doesn't use proper
58  * macro checks before defining types like struct timeval. The proper
59  * fix here is to either fuck the kernel header (which is what we do
60  * by defining _LINUX_TIME_H, an innocent little hack) or by fixing it
61  * upstream, which I'll consider doing later on. If you get compiler
62  * errors here, check your linux/time.h && sys/time.h header setup.
63 */
64 #define _LINUX_TIME_H
65
66 #include <linux/videodev.h>
67 #include "videodev_mjpeg.h"
68
69 #include <sys/soundcard.h>
70
71 /*****************************************************************************
72  * Module descriptior
73  *****************************************************************************/
74 static int  Open ( vlc_object_t * );
75 static void Close( vlc_object_t * );
76
77 #define CACHING_TEXT N_("Caching value in ms")
78 #define CACHING_LONGTEXT N_( \
79     "Caching value for V4L captures. This " \
80     "value should be set in milliseconds." )
81 #define VDEV_TEXT N_("Video device name")
82 #define VDEV_LONGTEXT N_( \
83     "Name of the video device to use. " \
84     "If you don't specify anything, no video device will be used.")
85 #define ADEV_TEXT N_("Audio device name")
86 #define ADEV_LONGTEXT N_( \
87     "Name of the audio device to use. " \
88     "If you don't specify anything, no audio device will be used.")
89 #define CHROMA_TEXT N_("Video input chroma format")
90 #define CHROMA_LONGTEXT N_( \
91     "Force the Video4Linux video device to use a specific chroma format " \
92     "(eg. I420 (default), RV24, etc.)")
93 #define FREQUENCY_TEXT N_( "Frequency" )
94 #define FREQUENCY_LONGTEXT N_( \
95     "Frequency to capture (in kHz), if applicable." )
96 #define CHANNEL_TEXT N_( "Channel" )
97 #define CHANNEL_LONGTEXT N_( \
98     "Channel of the card to use (Usually, 0 = tuner, " \
99     "1 = composite, 2 = svideo)." )
100 #define NORM_TEXT N_( "Norm" )
101 #define NORM_LONGTEXT N_( \
102     "Norm of the stream (Automatic, SECAM, PAL, or NTSC)." )
103 #define AUDIO_TEXT N_( "Audio Channel" )
104 #define AUDIO_LONGTEXT N_( \
105     "Audio Channel to use, if there are several audio inputs." )
106 #define WIDTH_TEXT N_( "Width" )
107 #define WIDTH_LONGTEXT N_( "Width of the stream to capture " \
108     "(-1 for autodetect)." )
109 #define HEIGHT_TEXT N_( "Height" )
110 #define HEIGHT_LONGTEXT N_( "Height of the stream to capture " \
111     "(-1 for autodetect)." )
112 #define BRIGHTNESS_TEXT N_( "Brightness" )
113 #define BRIGHTNESS_LONGTEXT N_( \
114     "Brightness of the video input." )
115 #define HUE_TEXT N_( "Hue" )
116 #define HUE_LONGTEXT N_( \
117     "Hue of the video input." )
118 #define COLOUR_TEXT N_( "Color" )
119 #define COLOUR_LONGTEXT N_( \
120     "Color of the video input." )
121 #define CONTRAST_TEXT N_( "Contrast" )
122 #define CONTRAST_LONGTEXT N_( \
123     "Contrast of the video input." )
124 #define TUNER_TEXT N_( "Tuner" )
125 #define TUNER_LONGTEXT N_( "Tuner to use, if there are several ones." )
126 #define SAMPLERATE_TEXT N_( "Samplerate" )
127 #define SAMPLERATE_LONGTEXT N_( \
128     "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100)" )
129 #define STEREO_TEXT N_( "Stereo" )
130 #define STEREO_LONGTEXT N_( \
131     "Capture the audio stream in stereo." )
132 #define MJPEG_TEXT N_( "MJPEG" )
133 #define MJPEG_LONGTEXT N_(  \
134     "Set this option if the capture device outputs MJPEG" )
135 #define DECIMATION_TEXT N_( "Decimation" )
136 #define DECIMATION_LONGTEXT N_( \
137     "Decimation level for MJPEG streams" )
138 #define QUALITY_TEXT N_( "Quality" )
139 #define QUALITY_LONGTEXT N_( "Quality of the stream." )
140 #define FPS_TEXT N_( "Framerate" )
141 #define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
142     "(-1 for autodetect)." )
143
144 static const int i_norm_list[] =
145     { VIDEO_MODE_AUTO, VIDEO_MODE_SECAM, VIDEO_MODE_PAL, VIDEO_MODE_NTSC };
146 static const char *const psz_norm_list_text[] =
147     { N_("Automatic"), N_("SECAM"), N_("PAL"),  N_("NTSC") };
148
149 vlc_module_begin();
150     set_shortname( N_("Video4Linux") );
151     set_description( N_("Video4Linux input") );
152     set_category( CAT_INPUT );
153     set_subcategory( SUBCAT_INPUT_ACCESS );
154
155     add_integer( "v4l-caching", DEFAULT_PTS_DELAY / 1000, NULL,
156                  CACHING_TEXT, CACHING_LONGTEXT, true );
157     add_string( "v4l-vdev", "/dev/video", 0, VDEV_TEXT, VDEV_LONGTEXT,
158                 false );
159     add_string( "v4l-adev", "/dev/dsp", 0, ADEV_TEXT, ADEV_LONGTEXT,
160                 false );
161     add_string( "v4l-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
162                 true );
163     add_float( "v4l-fps", -1.0, NULL, FPS_TEXT, FPS_LONGTEXT, true );
164     add_integer( "v4l-samplerate", 44100, NULL, SAMPLERATE_TEXT,
165                 SAMPLERATE_LONGTEXT, true );
166     add_integer( "v4l-channel", 0, NULL, CHANNEL_TEXT, CHANNEL_LONGTEXT,
167                 true );
168     add_integer( "v4l-tuner", -1, NULL, TUNER_TEXT, TUNER_LONGTEXT, true );
169     add_integer( "v4l-norm", VIDEO_MODE_AUTO, NULL, NORM_TEXT, NORM_LONGTEXT,
170                 false );
171         change_integer_list( i_norm_list, psz_norm_list_text, NULL );
172     add_integer( "v4l-frequency", -1, NULL, FREQUENCY_TEXT, FREQUENCY_LONGTEXT,
173                 false );
174     add_integer( "v4l-audio", -1, NULL, AUDIO_TEXT, AUDIO_LONGTEXT, true );
175     add_bool( "v4l-stereo", true, NULL, STEREO_TEXT, STEREO_LONGTEXT,
176             true );
177     add_integer( "v4l-width", 0, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, true );
178     add_integer( "v4l-height", 0, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT,
179                 true );
180     add_integer( "v4l-brightness", -1, NULL, BRIGHTNESS_TEXT,
181                 BRIGHTNESS_LONGTEXT, true );
182     add_integer( "v4l-colour", -1, NULL, COLOUR_TEXT, COLOUR_LONGTEXT,
183                 true );
184     add_integer( "v4l-hue", -1, NULL, HUE_TEXT, HUE_LONGTEXT, true );
185     add_integer( "v4l-contrast", -1, NULL, CONTRAST_TEXT, CONTRAST_LONGTEXT,
186                 true );
187     add_bool( "v4l-mjpeg", false, NULL, MJPEG_TEXT, MJPEG_LONGTEXT,
188             true );
189     add_integer( "v4l-decimation", 1, NULL, DECIMATION_TEXT,
190             DECIMATION_LONGTEXT, true );
191     add_integer( "v4l-quality", 100, NULL, QUALITY_TEXT, QUALITY_LONGTEXT,
192             true );
193
194     add_shortcut( "v4l" );
195     set_capability( "access_demux", 10 );
196     set_callbacks( Open, Close );
197 vlc_module_end();
198
199 /*****************************************************************************
200  * Access: local prototypes
201  *****************************************************************************/
202 static int Demux  ( demux_t * );
203 static int Control( demux_t *, int, va_list );
204
205 static void ParseMRL    ( demux_t * );
206 static int  OpenVideoDev( demux_t *, char * );
207 static int  OpenAudioDev( demux_t *, char * );
208
209 static block_t *GrabAudio( demux_t * );
210 static block_t *GrabVideo( demux_t * );
211
212 #define MJPEG_BUFFER_SIZE (256*1024)
213
214 struct quicktime_mjpeg_app1
215 {
216     uint32_t    i_reserved;             /* set to 0 */
217     uint32_t    i_tag;                  /* 'mjpg' */
218     uint32_t    i_field_size;           /* offset following EOI */
219     uint32_t    i_padded_field_size;    /* offset following EOI+pad */
220     uint32_t    i_next_field;           /* offset to next field */
221     uint32_t    i_DQT_offset;
222     uint32_t    i_DHT_offset;
223     uint32_t    i_SOF_offset;
224     uint32_t    i_SOS_offset;
225     uint32_t    i_data_offset;          /* following SOS marker data */
226 };
227
228 static const struct
229 {
230     int i_v4l;
231     int i_fourcc;
232
233 } v4lchroma_to_fourcc[] =
234 {
235     { VIDEO_PALETTE_GREY, VLC_FOURCC( 'G', 'R', 'E', 'Y' ) },
236     { VIDEO_PALETTE_HI240, VLC_FOURCC( 'I', '2', '4', '0' ) },
237     { VIDEO_PALETTE_RGB565, VLC_FOURCC( 'R', 'V', '1', '6' ) },
238     { VIDEO_PALETTE_RGB555, VLC_FOURCC( 'R', 'V', '1', '5' ) },
239     { VIDEO_PALETTE_RGB24, VLC_FOURCC( 'R', 'V', '2', '4' ) },
240     { VIDEO_PALETTE_RGB32, VLC_FOURCC( 'R', 'V', '3', '2' ) },
241     { VIDEO_PALETTE_YUV422, VLC_FOURCC( 'Y', 'U', 'Y', '2' ) },
242     { VIDEO_PALETTE_YUV422, VLC_FOURCC( 'Y', 'U', 'Y', 'V' ) },
243     { VIDEO_PALETTE_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', '2' ) },
244     { VIDEO_PALETTE_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', 'V' ) },
245     { VIDEO_PALETTE_UYVY, VLC_FOURCC( 'U', 'Y', 'V', 'Y' ) },
246     { VIDEO_PALETTE_YUV420, VLC_FOURCC( 'I', '4', '2', 'N' ) },
247     { VIDEO_PALETTE_YUV411, VLC_FOURCC( 'I', '4', '1', 'N' ) },
248     { VIDEO_PALETTE_RAW, VLC_FOURCC( 'G', 'R', 'A', 'W' ) },
249     { VIDEO_PALETTE_YUV422P, VLC_FOURCC( 'I', '4', '2', '2' ) },
250     { VIDEO_PALETTE_YUV420P, VLC_FOURCC( 'I', '4', '2', '0' ) },
251     { VIDEO_PALETTE_YUV411P, VLC_FOURCC( 'I', '4', '1', '1' ) },
252     { 0, 0 }
253 };
254
255 struct demux_sys_t
256 {
257     /* Devices */
258     char *psz_device;         /* Main device from MRL, can be video or audio */
259
260     char *psz_vdev;
261     int  fd_video;
262
263     char *psz_adev;
264     int  fd_audio;
265
266     /* Video properties */
267     picture_t pic;
268
269     int i_fourcc;
270     int i_channel;
271     int i_audio;
272     int i_norm;
273     int i_tuner;
274     int i_frequency;
275     int i_width;
276     int i_height;
277
278     int i_brightness;
279     int i_hue;
280     int i_colour;
281     int i_contrast;
282
283     float f_fps;            /* <= 0.0 mean to grab at full rate */
284     mtime_t i_video_pts;    /* only used when f_fps > 0 */
285
286     bool b_mjpeg;
287     int i_decimation;
288     int i_quality;
289
290     struct video_capability vid_cap;
291     struct video_mbuf       vid_mbuf;
292     struct mjpeg_requestbuffers mjpeg_buffers;
293
294     uint8_t *p_video_mmap;
295     int     i_frame_pos;
296
297     struct video_mmap   vid_mmap;
298     struct video_picture vid_picture;
299
300     int          i_video_frame_size;
301     es_out_id_t  *p_es_video;
302
303     /* Audio properties */
304     vlc_fourcc_t i_acodec_raw;
305     int          i_sample_rate;
306     bool   b_stereo;
307     int          i_audio_max_frame_size;
308     block_t      *p_block_audio;
309     es_out_id_t  *p_es_audio;
310 };
311
312 /*****************************************************************************
313  * Open: opens v4l device
314  *****************************************************************************
315  *
316  * url: <video device>::::
317  *
318  *****************************************************************************/
319 static int Open( vlc_object_t *p_this )
320 {
321     demux_t     *p_demux = (demux_t*)p_this;
322     demux_sys_t *p_sys;
323     vlc_value_t val;
324
325     /* Only when selected */
326     if( *p_demux->psz_access == '\0' )
327         return VLC_EGENERIC;
328
329     /* Set up p_demux */
330     p_demux->pf_demux = Demux;
331     p_demux->pf_control = Control;
332     p_demux->info.i_update = 0;
333     p_demux->info.i_title = 0;
334     p_demux->info.i_seekpoint = 0;
335     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
336     memset( p_sys, 0, sizeof( demux_sys_t ) );
337
338     var_Create( p_demux, "v4l-audio", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
339     var_Get( p_demux, "v4l-audio", &val );
340     p_sys->i_audio          = val.i_int;
341
342     var_Create( p_demux, "v4l-channel", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
343     var_Get( p_demux, "v4l-channel", &val );
344     p_sys->i_channel        = val.i_int;
345
346     var_Create( p_demux, "v4l-norm", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
347     var_Get( p_demux, "v4l-norm", &val );
348     p_sys->i_norm           = val.i_int;
349
350     var_Create( p_demux, "v4l-tuner", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
351     var_Get( p_demux, "v4l-tuner", &val );
352     p_sys->i_tuner          = val.i_int;
353
354     var_Create( p_demux, "v4l-frequency",
355                                     VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
356     var_Get( p_demux, "v4l-frequency", &val );
357     p_sys->i_frequency      = val.i_int;
358
359     var_Create( p_demux, "v4l-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
360     var_Get( p_demux, "v4l-fps", &val );
361     p_sys->f_fps            = val.f_float;
362
363     var_Create( p_demux, "v4l-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
364     var_Get( p_demux, "v4l-width", &val );
365     p_sys->i_width          = val.i_int;
366
367     var_Create( p_demux, "v4l-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
368     var_Get( p_demux, "v4l-height", &val );
369     p_sys->i_height         = val.i_int;
370
371     p_sys->i_video_pts      = -1;
372
373     var_Create( p_demux, "v4l-brightness", VLC_VAR_INTEGER |
374                                                         VLC_VAR_DOINHERIT );
375     var_Get( p_demux, "v4l-brightness", &val );
376     p_sys->i_brightness     = val.i_int;
377
378     var_Create( p_demux, "v4l-hue", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
379     var_Get( p_demux, "v4l-hue", &val );
380     p_sys->i_hue            = -1;
381
382     var_Create( p_demux, "v4l-colour", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
383     var_Get( p_demux, "v4l-colour", &val );
384     p_sys->i_colour         = val.i_int;
385
386     var_Create( p_demux, "v4l-contrast", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
387     var_Get( p_demux, "v4l-contrast", &val );
388     p_sys->i_contrast       = val.i_int;
389
390     var_Create( p_demux, "v4l-mjpeg", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
391     var_Get( p_demux, "v4l-mjpeg", &val );
392     p_sys->b_mjpeg     = val.b_bool;
393
394     var_Create( p_demux, "v4l-decimation", VLC_VAR_INTEGER |
395                                                             VLC_VAR_DOINHERIT );
396     var_Get( p_demux, "v4l-decimation", &val );
397     p_sys->i_decimation = val.i_int;
398
399     var_Create( p_demux, "v4l-quality", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
400     var_Get( p_demux, "v4l-quality", &val );
401     p_sys->i_quality = val.i_int;
402
403     var_Create( p_demux, "v4l-samplerate",
404                                     VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
405     var_Get( p_demux, "v4l-samplerate", &val );
406     p_sys->i_sample_rate  = val.i_int;
407
408     var_Create( p_demux, "v4l-stereo", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
409     var_Get( p_demux, "v4l-stereo", &val );
410     p_sys->b_stereo       = val.b_bool;
411
412     p_sys->psz_device = p_sys->psz_vdev = p_sys->psz_adev = NULL;
413     p_sys->fd_video = -1;
414     p_sys->fd_audio = -1;
415
416     p_sys->p_es_video = p_sys->p_es_audio = 0;
417     p_sys->p_block_audio = 0;
418
419     ParseMRL( p_demux );
420
421     /* Find main device (video or audio) */
422     if( p_sys->psz_device && *p_sys->psz_device )
423     {
424         msg_Dbg( p_demux, "main device=`%s'", p_sys->psz_device );
425
426         /* Try to open as video device */
427         p_sys->fd_video = OpenVideoDev( p_demux, p_sys->psz_device );
428
429         if( p_sys->fd_video < 0 )
430         {
431             /* Try to open as audio device */
432             p_sys->fd_audio = OpenAudioDev( p_demux, p_sys->psz_device );
433             if( p_sys->fd_audio >= 0 )
434             {
435                 free( p_sys->psz_adev );
436                 p_sys->psz_adev = p_sys->psz_device;
437                 p_sys->psz_device = NULL;
438             }
439         }
440         else
441         {
442             free( p_sys->psz_vdev );
443             p_sys->psz_vdev = p_sys->psz_device;
444             p_sys->psz_device = NULL;
445         }
446     }
447
448     /* If no device opened, only continue if the access was forced */
449     if( p_sys->fd_video < 0 && p_sys->fd_audio < 0 )
450     {
451         if( strcmp( p_demux->psz_access, "v4l" ) )
452         {
453             Close( p_this );
454             return VLC_EGENERIC;
455         }
456     }
457
458     /* Find video device */
459     if( p_sys->fd_video < 0 )
460     {
461         if( !p_sys->psz_vdev || !*p_sys->psz_vdev )
462         {
463             free( p_sys->psz_vdev );
464             p_sys->psz_vdev = var_CreateGetString( p_demux, "v4l-vdev" );;
465         }
466
467         if( p_sys->psz_vdev && *p_sys->psz_vdev )
468         {
469             p_sys->fd_video = OpenVideoDev( p_demux, p_sys->psz_vdev );
470         }
471     }
472
473     /* Find audio device */
474     if( p_sys->fd_audio < 0 )
475     {
476         if( !p_sys->psz_adev || !*p_sys->psz_adev )
477         {
478             free( p_sys->psz_adev );
479             p_sys->psz_adev = var_CreateGetString( p_demux, "v4l-adev" );;
480         }
481
482         if( p_sys->psz_adev && *p_sys->psz_adev )
483         {
484             p_sys->fd_audio = OpenAudioDev( p_demux, p_sys->psz_adev );
485         }
486     }
487
488     if( p_sys->fd_video < 0 && p_sys->fd_audio < 0 )
489     {
490         Close( p_this );
491         return VLC_EGENERIC;
492     }
493
494     msg_Dbg( p_demux, "v4l grabbing started" );
495
496     /* Declare elementary streams */
497     if( p_sys->fd_video >= 0 )
498     {
499         es_format_t fmt;
500         es_format_Init( &fmt, VIDEO_ES, p_sys->i_fourcc );
501         fmt.video.i_width  = p_sys->i_width;
502         fmt.video.i_height = p_sys->i_height;
503         fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
504
505         /* Setup rgb mask for RGB formats */
506         if( p_sys->i_fourcc == VLC_FOURCC('R','V','2','4') )
507         {
508             /* This is in BGR format */
509             fmt.video.i_bmask = 0x00ff0000;
510             fmt.video.i_gmask = 0x0000ff00;
511             fmt.video.i_rmask = 0x000000ff;
512         }
513
514         msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
515                  (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
516         p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
517     }
518
519     if( p_sys->fd_audio >= 0 )
520     {
521         es_format_t fmt;
522         es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
523
524         fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
525         fmt.audio.i_rate = p_sys->i_sample_rate;
526         fmt.audio.i_bitspersample = 16; // FIXME ?
527         fmt.audio.i_blockalign = fmt.audio.i_channels *
528             fmt.audio.i_bitspersample / 8;
529         fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate *
530             fmt.audio.i_bitspersample;
531
532         msg_Dbg( p_demux, "new audio es %d channels %dHz",
533                  fmt.audio.i_channels, fmt.audio.i_rate );
534
535         p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
536     }
537
538     /* Update default_pts to a suitable value for access */
539     var_Create( p_demux, "v4l-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
540
541     return VLC_SUCCESS;
542 }
543
544 /*****************************************************************************
545  * Close: close device, free resources
546  *****************************************************************************/
547 static void Close( vlc_object_t *p_this )
548 {
549     demux_t     *p_demux = (demux_t *)p_this;
550     demux_sys_t *p_sys   = p_demux->p_sys;
551
552     free( p_sys->psz_device );
553     free( p_sys->psz_vdev );
554     free( p_sys->psz_adev );
555     if( p_sys->fd_video >= 0 ) close( p_sys->fd_video );
556     if( p_sys->fd_audio >= 0 ) close( p_sys->fd_audio );
557     if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio );
558
559     if( p_sys->b_mjpeg )
560     {
561         int i_noframe = -1;
562         ioctl( p_sys->fd_video, MJPIOC_QBUF_CAPT, &i_noframe );
563     }
564
565     if( p_sys->p_video_mmap && p_sys->p_video_mmap != MAP_FAILED )
566     {
567         if( p_sys->b_mjpeg )
568             munmap( p_sys->p_video_mmap, p_sys->mjpeg_buffers.size *
569                     p_sys->mjpeg_buffers.count );
570         else
571             munmap( p_sys->p_video_mmap, p_sys->vid_mbuf.size );
572     }
573
574     free( p_sys );
575 }
576
577 /*****************************************************************************
578  * Control:
579  *****************************************************************************/
580 static int Control( demux_t *p_demux, int i_query, va_list args )
581 {
582     bool *pb;
583     int64_t    *pi64;
584
585     switch( i_query )
586     {
587         /* Special for access_demux */
588         case DEMUX_CAN_PAUSE:
589         case DEMUX_CAN_SEEK:
590         case DEMUX_SET_PAUSE_STATE:
591         case DEMUX_CAN_CONTROL_PACE:
592             pb = (bool*)va_arg( args, bool * );
593             *pb = false;
594             return VLC_SUCCESS;
595
596         case DEMUX_GET_PTS_DELAY:
597             pi64 = (int64_t*)va_arg( args, int64_t * );
598             *pi64 = (int64_t)var_GetInteger( p_demux, "v4l-caching" ) * 1000;
599             return VLC_SUCCESS;
600
601         case DEMUX_GET_TIME:
602             pi64 = (int64_t*)va_arg( args, int64_t * );
603             *pi64 = mdate();
604             return VLC_SUCCESS;
605
606         /* TODO implement others */
607         default:
608             return VLC_EGENERIC;
609     }
610
611     return VLC_EGENERIC;
612 }
613
614 /*****************************************************************************
615  * Demux:
616  *****************************************************************************/
617 static int Demux( demux_t *p_demux )
618 {
619     demux_sys_t *p_sys = p_demux->p_sys;
620     es_out_id_t  *p_es = p_sys->p_es_audio;
621     block_t *p_block = NULL;
622
623     /* Try grabbing audio frames first */
624     if( p_sys->fd_audio < 0 || !( p_block = GrabAudio( p_demux ) ) )
625     {
626         /* Try grabbing video frame */
627         p_es = p_sys->p_es_video;
628         if( p_sys->fd_video > 0 ) p_block = GrabVideo( p_demux );
629     }
630
631     if( !p_block )
632     {
633         /* Sleep so we do not consume all the cpu, 10ms seems
634          * like a good value (100fps) */
635         msleep( 10000 );
636         return 1;
637     }
638
639     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
640     es_out_Send( p_demux->out, p_es, p_block );
641
642     return 1;
643 }
644
645 /*****************************************************************************
646  * ParseMRL: parse the options contained in the MRL
647  *****************************************************************************/
648 static void ParseMRL( demux_t *p_demux )
649 {
650     demux_sys_t *p_sys = p_demux->p_sys;
651
652     char *psz_dup = strdup( p_demux->psz_path );
653     char *psz_parser = psz_dup;
654
655     while( *psz_parser && *psz_parser != ':' )
656     {
657         psz_parser++;
658     }
659
660     if( *psz_parser == ':' )
661     {
662         /* read options */
663         for( ;; )
664         {
665             *psz_parser++ = '\0';
666             if( !strncmp( psz_parser, "channel=", strlen( "channel=" ) ) )
667             {
668                 p_sys->i_channel = strtol( psz_parser + strlen( "channel=" ),
669                                            &psz_parser, 0 );
670             }
671             else if( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
672             {
673                 psz_parser += strlen( "norm=" );
674                 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
675                 {
676                     p_sys->i_norm = VIDEO_MODE_PAL;
677                     psz_parser += strlen( "pal" );
678                 }
679                 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
680                 {
681                     p_sys->i_norm = VIDEO_MODE_NTSC;
682                     psz_parser += strlen( "ntsc" );
683                 }
684                 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
685                 {
686                     p_sys->i_norm = VIDEO_MODE_SECAM;
687                     psz_parser += strlen( "secam" );
688                 }
689                 else if( !strncmp( psz_parser, "auto", strlen( "auto" ) ) )
690                 {
691                     p_sys->i_norm = VIDEO_MODE_AUTO;
692                     psz_parser += strlen( "auto" );
693                 }
694                 else
695                 {
696                     p_sys->i_norm = strtol( psz_parser, &psz_parser, 0 );
697                 }
698             }
699             else if( !strncmp( psz_parser, "frequency=",
700                                strlen( "frequency=" ) ) )
701             {
702                 p_sys->i_frequency =
703                     strtol( psz_parser + strlen( "frequency=" ),
704                             &psz_parser, 0 );
705                 if( p_sys->i_frequency < 30000 )
706                 {
707                     msg_Warn( p_demux, "v4l syntax has changed : "
708                               "'frequency' is now channel frequency in kHz");
709                 }
710             }
711             else if( !strncmp( psz_parser, "audio=", strlen( "audio=" ) ) )
712             {
713                 p_sys->i_audio = strtol( psz_parser + strlen( "audio=" ),
714                                          &psz_parser, 0 );
715             }
716             else if( !strncmp( psz_parser, "size=", strlen( "size=" ) ) )
717             {
718                 psz_parser += strlen( "size=" );
719                 if( !strncmp( psz_parser, "subqcif", strlen( "subqcif" ) ) )
720                 {
721                     p_sys->i_width  = 128;
722                     p_sys->i_height = 96;
723                 }
724                 else if( !strncmp( psz_parser, "qsif", strlen( "qsif" ) ) )
725                 {
726                     p_sys->i_width  = 160;
727                     p_sys->i_height = 120;
728                 }
729                 else if( !strncmp( psz_parser, "qcif", strlen( "qcif" ) ) )
730                 {
731                     p_sys->i_width  = 176;
732                     p_sys->i_height = 144;
733                 }
734                 else if( !strncmp( psz_parser, "sif", strlen( "sif" ) ) )
735                 {
736                     p_sys->i_width  = 320;
737                     p_sys->i_height = 244;
738                 }
739                 else if( !strncmp( psz_parser, "cif", strlen( "cif" ) ) )
740                 {
741                     p_sys->i_width  = 352;
742                     p_sys->i_height = 288;
743                 }
744                 else if( !strncmp( psz_parser, "vga", strlen( "vga" ) ) )
745                 {
746                     p_sys->i_width  = 640;
747                     p_sys->i_height = 480;
748                 }
749                 else
750                 {
751                     /* widthxheight */
752                     p_sys->i_width = strtol( psz_parser, &psz_parser, 0 );
753                     if( *psz_parser == 'x' || *psz_parser == 'X')
754                     {
755                         p_sys->i_height = strtol( psz_parser + 1,
756                                                   &psz_parser, 0 );
757                     }
758                     msg_Dbg( p_demux, "WxH %dx%d", p_sys->i_width,
759                              p_sys->i_height );
760                 }
761             }
762             else if( !strncmp( psz_parser, "brightness=", strlen( "brightness=" ) ) )
763             {
764                 p_sys->i_brightness = strtol( psz_parser + strlen( "brightness=" ),
765                                               &psz_parser, 0 );
766             }
767             else if( !strncmp( psz_parser, "colour=", strlen( "colour=" ) ) )
768             {
769                 p_sys->i_colour = strtol( psz_parser + strlen( "colour=" ),
770                                           &psz_parser, 0 );
771             }
772             else if( !strncmp( psz_parser, "hue=", strlen( "hue=" ) ) )
773             {
774                 p_sys->i_hue = strtol( psz_parser + strlen( "hue=" ),
775                                        &psz_parser, 0 );
776             }
777             else if( !strncmp( psz_parser, "contrast=", strlen( "contrast=" ) ) )
778             {
779                 p_sys->i_contrast = strtol( psz_parser + strlen( "contrast=" ),
780                                             &psz_parser, 0 );
781             }
782             else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
783             {
784                 p_sys->i_tuner = strtol( psz_parser + strlen( "tuner=" ),
785                                          &psz_parser, 0 );
786             }
787             else if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
788             {
789                 int  i_len;
790
791                 psz_parser += strlen( "adev=" );
792                 if( strchr( psz_parser, ':' ) )
793                 {
794                     i_len = strchr( psz_parser, ':' ) - psz_parser;
795                 }
796                 else
797                 {
798                     i_len = strlen( psz_parser );
799                 }
800
801                 p_sys->psz_adev = strndup( psz_parser, i_len );
802
803                 psz_parser += i_len;
804             }
805             else if( !strncmp( psz_parser, "samplerate=",
806                                strlen( "samplerate=" ) ) )
807             {
808                 p_sys->i_sample_rate =
809                     strtol( psz_parser + strlen( "samplerate=" ),
810                             &psz_parser, 0 );
811             }
812             else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
813             {
814                 psz_parser += strlen( "stereo" );
815
816                 p_sys->b_stereo = true;
817             }
818             else if( !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
819             {
820                 psz_parser += strlen( "mono" );
821