]> git.sesse.net Git - vlc/blob - modules/access/v4l2.c
eca71f8c00b7ef7b92a57bce28d51d1b8e0ecf99
[vlc] / modules / access / v4l2.c
1 /*****************************************************************************
2  * v4l2.c : Video4Linux2 input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2007 the VideoLAN team
5  * $Id$
6  *
7  * Author: Benjamin Pracht <bigben at videolan dot org>
8  *         Richard Hosking <richard at hovis dot net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*
26  * Sections based on the reference V4L2 capture example at
27  * http://v4l2spec.bytesex.org/spec/capture-example.html
28  */
29
30 /*
31  * TODO: No mjpeg support yet.
32  * TODO: Tuner partial implementation.
33  * TODO: Alsa input support?
34  */
35
36 /*****************************************************************************
37  * Preamble
38  *****************************************************************************/
39
40 #include <vlc/vlc.h>
41 #include <vlc_access.h>
42 #include <vlc_demux.h>
43 #include <vlc_input.h>
44 #include <vlc_vout.h>
45
46 #include <fcntl.h>
47 #include <unistd.h>
48 #include <sys/ioctl.h>
49 #include <sys/mman.h>
50
51 #include <linux/videodev2.h>
52
53 #include <sys/soundcard.h>
54
55 /*****************************************************************************
56  * Module descriptior
57  *****************************************************************************/
58
59 static int  Open ( vlc_object_t * );
60 static void Close( vlc_object_t * );
61
62 #define DEV_TEXT N_("Device name")
63 #define DEV_LONGTEXT N_( \
64     "Name of the device to use. " \
65     "If you don't specify anything, /dev/video0 will be used.")
66 #define ADEV_TEXT N_("Audio device name")
67 #define ADEV_LONGTEXT N_( \
68     "Name of the audio device to use. " \
69     "If you don't specify anything, /dev/dsp will be used.")
70 #define STANDARD_TEXT N_( "Standard" )
71 #define STANDARD_LONGTEXT N_( \
72     "Video standard (Default, SECAM, PAL, or NTSC)." )
73 #define CHROMA_TEXT N_("Video input chroma format")
74 #define CHROMA_LONGTEXT N_( \
75     "Force the Video4Linux2 video device to use a specific chroma format " \
76     "(eg. I420, RV24, etc.)")
77 #define INPUT_TEXT N_( "Input" )
78 #define INPUT_LONGTEXT N_( \
79     "Input of the card to use (Usually, 0 = tuner, " \
80     "1 = composite, 2 = svideo)." )
81 #define IOMETHOD_TEXT N_( "IO Method" )
82 #define IOMETHOD_LONGTEXT N_( \
83     "IO Method (READ, MMAP, USERPTR)." )
84 #define WIDTH_TEXT N_( "Width" )
85 #define WIDTH_LONGTEXT N_( \
86     "Force width (-1 for autodetect)." )
87 #define HEIGHT_TEXT N_( "Height" )
88 #define HEIGHT_LONGTEXT N_( \
89     "Force height (-1 for autodetect)." )
90 #define BRIGHTNESS_TEXT N_( "Brightness" )
91 #define BRIGHTNESS_LONGTEXT N_( \
92     "Brightness of the video input." )
93 #define CONTRAST_TEXT N_( "Contrast" )
94 #define CONTRAST_LONGTEXT N_( \
95     "Contrast of the video input." )
96 #define SATURATION_TEXT N_( "Saturation" )
97 #define SATURATION_LONGTEXT N_( \
98     "Saturation of the video input." )
99 #define HUE_TEXT N_( "Hue" )
100 #define HUE_LONGTEXT N_( \
101     "Hue of the video input." )
102 #define FPS_TEXT N_( "Framerate" )
103 #define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
104     "(-1 for autodetect)." )
105 #define STEREO_TEXT N_( "Stereo" )
106 #define STEREO_LONGTEXT N_( \
107     "Capture the audio stream in stereo." )
108 #define SAMPLERATE_TEXT N_( "Samplerate" )
109 #define SAMPLERATE_LONGTEXT N_( \
110     "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100, 48000)" )
111 #define CACHING_TEXT N_("Caching value in ms")
112 #define CACHING_LONGTEXT N_( \
113     "Caching value for V4L2 captures. This " \
114     "value should be set in milliseconds." )
115
116 typedef enum {
117     IO_METHOD_READ,
118     IO_METHOD_MMAP,
119     IO_METHOD_USERPTR,
120 } io_method;
121
122 static int i_standards_list[] =
123     { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC };
124 static const char *psz_standards_list_text[] =
125     { N_("Default"), N_("SECAM"), N_("PAL"),  N_("NTSC") };
126
127 static int i_iomethod_list[] =
128     { IO_METHOD_READ, IO_METHOD_MMAP, IO_METHOD_USERPTR };
129 static const char *psz_iomethod_list_text[] =
130     { N_("READ"), N_("MMAP"),  N_("USERPTR") };
131
132 vlc_module_begin();
133     set_shortname( _("Video4Linux2") );
134     set_description( _("Video4Linux2 input") );
135     set_category( CAT_INPUT );
136     set_subcategory( SUBCAT_INPUT_ACCESS );
137
138     add_string( "v4l2-dev", "/dev/video0", 0, DEV_TEXT, DEV_LONGTEXT,
139                 VLC_FALSE );
140     add_string( "v4l2-adev", "/dev/dsp", 0, ADEV_TEXT, ADEV_LONGTEXT,
141                 VLC_FALSE );
142     add_integer( "v4l2-standard", 0, NULL, STANDARD_TEXT, STANDARD_LONGTEXT,
143                 VLC_FALSE );
144         change_integer_list( i_standards_list, psz_standards_list_text, 0 );
145     add_string( "v4l2-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
146                 VLC_TRUE );
147     add_integer( "v4l2-input", 0, NULL, INPUT_TEXT, INPUT_LONGTEXT,
148                 VLC_TRUE );
149     add_integer( "v4l2-io", IO_METHOD_MMAP, NULL, IOMETHOD_TEXT,
150                  IOMETHOD_LONGTEXT, VLC_TRUE );
151         change_integer_list( i_iomethod_list, psz_iomethod_list_text, 0 );
152     add_integer( "v4l2-width", 0, NULL, WIDTH_TEXT,
153                 WIDTH_LONGTEXT, VLC_TRUE );
154     add_integer( "v4l2-height", 0, NULL, HEIGHT_TEXT,
155                 HEIGHT_LONGTEXT, VLC_TRUE );
156     add_integer( "v4l2-brightness", -1, NULL, BRIGHTNESS_TEXT,
157                 BRIGHTNESS_LONGTEXT, VLC_TRUE );
158     add_integer( "v4l2-contrast", -1, NULL, CONTRAST_TEXT,
159                 CONTRAST_LONGTEXT, VLC_TRUE );
160     add_integer( "v4l2-saturation", -1, NULL, SATURATION_TEXT,
161                 SATURATION_LONGTEXT, VLC_TRUE );
162     add_integer( "v4l2-hue", -1, NULL, HUE_TEXT,
163                 HUE_LONGTEXT, VLC_TRUE );
164     add_float( "v4l2-fps", 0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_TRUE );
165     add_bool( "v4l2-stereo", VLC_TRUE, NULL, STEREO_TEXT, STEREO_LONGTEXT,
166                 VLC_TRUE );
167     add_integer( "v4l2-samplerate", 48000, NULL, SAMPLERATE_TEXT,
168                 SAMPLERATE_LONGTEXT, VLC_TRUE );
169     add_integer( "v4l2-caching", DEFAULT_PTS_DELAY / 1000, NULL,
170                 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
171
172     add_shortcut( "v4l2" );
173     set_capability( "access_demux", 10 );
174     set_callbacks( Open, Close );
175 vlc_module_end();
176
177 /*****************************************************************************
178  * Access: local prototypes
179  *****************************************************************************/
180
181 static void ParseMRL( demux_t * );
182
183 static int Control( demux_t *, int, va_list );
184
185 static int Demux( demux_t * );
186 static block_t* GrabVideo( demux_t *p_demux );
187 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame );
188 static block_t* GrabAudio( demux_t *p_demux );
189
190 vlc_bool_t IsPixelFormatSupported( demux_t *p_demux, unsigned int i_pixelformat );
191
192 static int OpenVideoDev( demux_t *, char *psz_device );
193 static int OpenAudioDev( demux_t *, char *psz_device );
194 static vlc_bool_t ProbeVideoDev( demux_t *, char *psz_device );
195 static vlc_bool_t ProbeAudioDev( demux_t *, char *psz_device );
196
197 static struct
198 {
199     unsigned int i_v4l2;
200     int i_fourcc;
201 } v4l2chroma_to_fourcc[] =
202 {
203     { V4L2_PIX_FMT_GREY, VLC_FOURCC( 'G', 'R', 'E', 'Y' ) },
204     { V4L2_PIX_FMT_HI240, VLC_FOURCC( 'I', '2', '4', '0' ) },
205     { V4L2_PIX_FMT_RGB565, VLC_FOURCC( 'R', 'V', '1', '6' ) },
206     { V4L2_PIX_FMT_RGB555, VLC_FOURCC( 'R', 'V', '1', '5' ) },
207     { V4L2_PIX_FMT_BGR24, VLC_FOURCC( 'R', 'V', '2', '4' ) },
208     { V4L2_PIX_FMT_BGR32, VLC_FOURCC( 'R', 'V', '3', '2' ) },
209     { V4L2_PIX_FMT_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', '2' ) },
210     { V4L2_PIX_FMT_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', 'V' ) },
211     { V4L2_PIX_FMT_UYVY, VLC_FOURCC( 'U', 'Y', 'V', 'Y' ) },
212     { V4L2_PIX_FMT_Y41P, VLC_FOURCC( 'I', '4', '1', 'N' ) },
213     { V4L2_PIX_FMT_YUV422P, VLC_FOURCC( 'I', '4', '2', '2' ) },
214     { V4L2_PIX_FMT_YVU420, VLC_FOURCC( 'I', '4', '2', '0' ) },
215     { V4L2_PIX_FMT_YUV411P, VLC_FOURCC( 'I', '4', '1', '1' ) },
216     { V4L2_PIX_FMT_YUV410, VLC_FOURCC( 'I', '4', '1', '0' ) },
217     { 0, 0 }
218 };
219
220 struct buffer_t
221 {
222     void *  start;
223     size_t  length;
224     void *  orig_userp;
225 };
226
227 struct demux_sys_t
228 {
229     char *psz_device;  /* Main device from MRL, can be video or audio */
230
231     char *psz_vdev;
232     int  i_fd_video;
233
234     char *psz_adev;
235     int  i_fd_audio;
236
237     char *psz_requested_chroma;
238
239     /* Video */
240     io_method io;
241
242     int i_pts;
243
244     struct v4l2_capability dev_cap;
245
246     int i_input;
247     struct v4l2_input *p_inputs;
248     int i_selected_input;
249
250     int i_standard;
251     struct v4l2_standard *p_standards;
252     v4l2_std_id i_selected_standard_id;
253
254     int i_audio;
255     /* V4L2 devices cannot have more than 32 audio inputs */
256     struct v4l2_audio p_audios[32];
257
258     int i_tuner;
259     struct v4l2_tuner *p_tuners;
260
261     int i_codec;
262     struct v4l2_fmtdesc *p_codecs;
263
264     struct buffer_t *p_buffers;
265     unsigned int i_nbuffers;
266
267     int i_width;
268     int i_height;
269     float f_fps;            /* <= 0.0 mean to grab at full rate */
270     mtime_t i_video_pts;    /* only used when f_fps > 0 */
271     int i_fourcc;
272
273     int i_brightness;
274     int i_contrast;
275     int i_saturation;
276     int i_hue;
277
278     picture_t pic;
279     int i_video_frame_size;
280
281     es_out_id_t *p_es_video;
282
283     /* Audio */
284     int i_sample_rate;
285     vlc_bool_t b_stereo;
286     int i_audio_max_frame_size;
287     block_t *p_block_audio;
288     es_out_id_t *p_es_audio;
289 };
290
291 /*****************************************************************************
292  * Open: opens v4l2 device
293  *****************************************************************************
294  *
295  * url: <video device>::::
296  *
297  *****************************************************************************/
298 static int Open( vlc_object_t *p_this )
299 {
300     demux_t     *p_demux = (demux_t*)p_this;
301     demux_sys_t *p_sys;
302     vlc_value_t val;
303
304     /* Only when selected */
305     if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;
306
307     /* Set up p_demux */
308     p_demux->pf_control = Control;
309     p_demux->pf_demux = Demux;
310     p_demux->info.i_update = 0;
311     p_demux->info.i_title = 0;
312     p_demux->info.i_seekpoint = 0;
313
314     p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
315     if( p_sys == NULL ) return VLC_ENOMEM;
316
317     p_sys->i_video_pts = -1;
318
319     var_Create( p_demux, "v4l2-standard", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
320     var_Get( p_demux, "v4l2-standard", &val );
321     p_sys->i_selected_standard_id = i_standards_list[val.i_int];
322
323     p_sys->i_selected_input = var_CreateGetInteger( p_demux, "v4l2-input" );
324
325     p_sys->io = var_CreateGetInteger( p_demux, "v4l2-io" );
326
327     p_sys->i_width = var_CreateGetInteger( p_demux, "v4l2-width" );
328     p_sys->i_height = var_CreateGetInteger( p_demux, "v4l2-height" );
329
330     p_sys->i_brightness = var_CreateGetInteger( p_demux, "v4l2-brightness" );
331     p_sys->i_contrast = var_CreateGetInteger( p_demux, "v4l2-contrast" );
332     p_sys->i_saturation = var_CreateGetInteger( p_demux, "v4l2-saturation" );
333     p_sys->i_hue = var_CreateGetInteger( p_demux, "v4l2-hue" );
334
335     var_Create( p_demux, "v4l2-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
336     var_Get( p_demux, "v4l2-fps", &val );
337     p_sys->f_fps = val.f_float;
338
339     var_Create( p_demux, "v4l2-samplerate", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
340     var_Get( p_demux, "v4l2-samplerate", &val );
341     p_sys->i_sample_rate = val.i_int;
342
343     p_sys->psz_requested_chroma = var_CreateGetString( p_demux, "v4l2-chroma" );
344
345     var_Create( p_demux, "v4l2-stereo", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
346     var_Get( p_demux, "v4l2-stereo", &val );
347     p_sys->b_stereo = val.b_bool;
348
349     p_sys->i_pts = var_CreateGetInteger( p_demux, "v4l2-caching" );
350
351     p_sys->psz_device = p_sys->psz_vdev = p_sys->psz_adev = NULL;
352     p_sys->i_fd_video = -1;
353     p_sys->i_fd_audio = -1;
354
355     p_sys->p_es_video = p_sys->p_es_audio = 0;
356     p_sys->p_block_audio = 0;
357
358     ParseMRL( p_demux );
359
360     /* Find main device (video or audio) */
361     if( p_sys->psz_device && *p_sys->psz_device )
362     {
363         msg_Dbg( p_demux, "main device='%s'", p_sys->psz_device );
364
365         /* Try to open as video device */
366         msg_Dbg( p_demux, "trying device '%s' as video", p_sys->psz_device );
367         if( ProbeVideoDev( p_demux, p_sys->psz_device ) )
368         {
369             msg_Dbg( p_demux, "'%s' is a video device", p_sys->psz_device );
370             /* Device was a video device */
371             if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
372             p_sys->psz_vdev = p_sys->psz_device;
373             p_sys->psz_device = NULL;
374             p_sys->i_fd_video = OpenVideoDev( p_demux, p_sys->psz_vdev );
375             if( p_sys->i_fd_video < 0 )
376             {
377                 Close( p_this );
378                 return VLC_EGENERIC;
379             }
380         }
381         else
382         {
383             /* Try to open as audio device */
384             msg_Dbg( p_demux, "trying device '%s' as audio", p_sys->psz_device );
385             if( ProbeAudioDev( p_demux, p_sys->psz_device ) )
386             {
387                 msg_Dbg( p_demux, "'%s' is an audio device", p_sys->psz_device );
388                 /* Device was an audio device */
389                 if( p_sys->psz_adev ) free( p_sys->psz_adev );
390                 p_sys->psz_adev = p_sys->psz_device;
391                 p_sys->psz_device = NULL;
392                 p_sys->i_fd_audio = OpenAudioDev( p_demux, p_sys->psz_adev );
393                 if( p_sys->i_fd_audio < 0 )
394                 {
395                     Close( p_this );
396                     return VLC_EGENERIC;
397                 }
398             }
399         }
400     }
401
402     /* If no device opened, only continue if the access was forced */
403     if( p_sys->i_fd_video < 0 && p_sys->i_fd_audio < 0 )
404     {
405         if( strcmp( p_demux->psz_access, "v4l2" ) )
406         {
407             Close( p_this );
408             return VLC_EGENERIC;
409         }
410     }
411
412     /* Find video device */
413     if( p_sys->i_fd_video < 0 )
414     {
415         if( !p_sys->psz_vdev || !*p_sys->psz_vdev )
416         {
417             if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
418             p_sys->psz_vdev = var_CreateGetString( p_demux, "v4l2-dev" );
419         }
420
421         msg_Dbg( p_demux, "opening '%s' as video", p_sys->psz_vdev );
422         if( p_sys->psz_vdev && *p_sys->psz_vdev && ProbeVideoDev( p_demux, p_sys->psz_vdev ) )
423         {
424             p_sys->i_fd_video = OpenVideoDev( p_demux, p_sys->psz_vdev );
425         }
426     }
427
428     /* Find audio device */
429     if( p_sys->i_fd_audio < 0 )
430     {
431         if( !p_sys->psz_adev || !*p_sys->psz_adev )
432         {
433             if( p_sys->psz_adev ) free( p_sys->psz_adev );
434             p_sys->psz_adev = var_CreateGetString( p_demux, "v4l2-adev" );
435         }
436
437         msg_Dbg( p_demux, "opening '%s' as audio", p_sys->psz_adev );
438         if( p_sys->psz_adev && *p_sys->psz_adev && ProbeAudioDev( p_demux, p_sys->psz_adev ) )
439         {
440             p_sys->i_fd_audio = OpenAudioDev( p_demux, p_sys->psz_adev );
441         }
442     }
443
444     if( p_sys->i_fd_video < 0 && p_sys->i_fd_audio < 0 )
445     {
446         Close( p_this );
447         return VLC_EGENERIC;
448     }
449
450     return VLC_SUCCESS;
451 }
452
453 /*****************************************************************************
454  * ParseMRL: parse the options contained in the MRL
455  *****************************************************************************/
456 static void ParseMRL( demux_t *p_demux )
457 {
458     demux_sys_t *p_sys = p_demux->p_sys;
459
460     char *psz_dup = strdup( p_demux->psz_path );
461     char *psz_parser = psz_dup;
462
463     while( *psz_parser && *psz_parser != ':' )
464     {
465         psz_parser++;
466     }
467
468     if( *psz_parser == ':' )
469     {
470         /* read options */
471         for( ;; )
472         {
473             *psz_parser++ = '\0';
474
475             if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
476             {
477                 int  i_len;
478
479                 psz_parser += strlen( "adev=" );
480                 if( strchr( psz_parser, ':' ) )
481                 {
482                     i_len = strchr( psz_parser, ':' ) - psz_parser;
483                 }
484                 else
485                 {
486                     i_len = strlen( psz_parser );
487                 }
488
489                 p_sys->psz_adev = strndup( psz_parser, i_len );
490
491                 psz_parser += i_len;
492             }
493             else if( !strncmp( psz_parser, "standard=", strlen( "standard=" ) ) )
494             {
495                 psz_parser += strlen( "standard=" );
496                 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
497                 {
498                     p_sys->i_selected_standard_id = V4L2_STD_PAL;
499                     psz_parser += strlen( "pal" );
500                 }
501                 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
502                 {
503                     p_sys->i_selected_standard_id = V4L2_STD_NTSC;
504                     psz_parser += strlen( "ntsc" );
505                 }
506                 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
507                 {
508                     p_sys->i_selected_standard_id = V4L2_STD_SECAM;
509                     psz_parser += strlen( "secam" );
510                 }
511                 else if( !strncmp( psz_parser, "default", strlen( "default" ) ) )
512                 {
513                     p_sys->i_selected_standard_id = V4L2_STD_UNKNOWN;
514                     psz_parser += strlen( "default" );
515                 }
516                 else
517                 {
518                     p_sys->i_selected_standard_id = i_standards_list[strtol( psz_parser, &psz_parser, 0 )];
519                 }
520             }
521             else if( !strncmp( psz_parser, "chroma=", strlen( "chroma=" ) ) )
522             {
523                 int  i_len;
524
525                 psz_parser += strlen( "chroma=" );
526                 if( strchr( psz_parser, ':' ) )
527                 {
528                     i_len = strchr( psz_parser, ':' ) - psz_parser;
529                 }
530                 else
531                 {
532                     i_len = strlen( psz_parser );
533                 }
534
535                 if( p_sys->psz_requested_chroma ) free( p_sys->psz_requested_chroma );
536                 p_sys->psz_requested_chroma = strndup( psz_parser, i_len );
537
538                 psz_parser += i_len;
539             }
540             else if( !strncmp( psz_parser, "input=", strlen( "input=" ) ) )
541             {
542                 p_sys->i_selected_input = strtol( psz_parser + strlen( "input=" ),
543                                        &psz_parser, 0 );
544             }
545             else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
546             {
547                 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
548                                        &psz_parser );
549             }
550             else if( !strncmp( psz_parser, "io=", strlen( "io=" ) ) )
551             {
552                 psz_parser += strlen( "io=" );
553                 if( !strncmp( psz_parser, "read", strlen( "read" ) ) )
554                 {
555                     p_sys->io = IO_METHOD_READ;
556                     psz_parser += strlen( "read" );
557                 }
558                 else if( !strncmp( psz_parser, "mmap", strlen( "mmap" ) ) )
559                 {
560                     p_sys->io = IO_METHOD_MMAP;
561                     psz_parser += strlen( "mmap" );
562                 }
563                 else if( !strncmp( psz_parser, "userptr", strlen( "userptr" ) ) )
564                 {
565                     p_sys->io = IO_METHOD_USERPTR;
566                     psz_parser += strlen( "userptr" );
567                 }
568                 else
569                 {
570                     p_sys->io = strtol( psz_parser, &psz_parser, 0 );
571                 }
572             }
573             else if( !strncmp( psz_parser, "width=",
574                                strlen( "width=" ) ) )
575             {
576                 p_sys->i_width =
577                     strtol( psz_parser + strlen( "width=" ),
578                             &psz_parser, 0 );
579             }
580             else if( !strncmp( psz_parser, "height=",
581                                strlen( "height=" ) ) )
582             {
583                 p_sys->i_height =
584                     strtol( psz_parser + strlen( "height=" ),
585                             &psz_parser, 0 );
586             }
587             else if( !strncmp( psz_parser, "brightness=",
588                                strlen( "brightness=" ) ) )
589             {
590                 p_sys->i_brightness =
591                     strtol( psz_parser + strlen( "brightness=" ),
592                             &psz_parser, 0 );
593             }
594             else if( !strncmp( psz_parser, "contrast=",
595                                strlen( "contrast=" ) ) )
596             {
597                 p_sys->i_contrast =
598                     strtol( psz_parser + strlen( "contrast=" ),
599                             &psz_parser, 0 );
600             }
601             else if( !strncmp( psz_parser, "saturation=",
602                                strlen( "saturation=" ) ) )
603             {
604                 p_sys->i_saturation =
605                     strtol( psz_parser + strlen( "saturation=" ),
606                             &psz_parser, 0 );
607             }
608             else if( !strncmp( psz_parser, "hue=",
609                                strlen( "hue=" ) ) )
610             {
611                 p_sys->i_hue =
612                     strtol( psz_parser + strlen( "hue=" ),
613                             &psz_parser, 0 );
614             }
615             else if( !strncmp( psz_parser, "samplerate=",
616                                strlen( "samplerate=" ) ) )
617             {
618                 p_sys->i_sample_rate =
619                     strtol( psz_parser + strlen( "samplerate=" ),
620                             &psz_parser, 0 );
621             }
622             else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
623             {
624                 psz_parser += strlen( "stereo" );
625                 p_sys->b_stereo = VLC_TRUE;
626             }
627             else if( !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
628             {
629                 psz_parser += strlen( "mono" );
630                 p_sys->b_stereo = VLC_FALSE;
631             }
632             else if( !strncmp( psz_parser, "caching=", strlen( "caching=" ) ) )
633             {
634                 p_sys->i_pts = strtol( psz_parser + strlen( "caching=" ),
635                                        &psz_parser, 0 );
636             }
637             else
638             {
639                 msg_Warn( p_demux, "unknown option" );
640             }
641
642             while( *psz_parser && *psz_parser != ':' )
643             {
644                 psz_parser++;
645             }
646
647             if( *psz_parser == '\0' )
648             {
649                 break;
650             }
651         }
652     }
653
654     /* Main device */
655     if( *psz_dup )
656     {
657         p_sys->psz_device = strdup( psz_dup );
658     }
659     if( psz_dup ) free( psz_dup );
660 }
661
662 /*****************************************************************************
663  * Close: close device, free resources
664  *****************************************************************************/
665 static void Close( vlc_object_t *p_this )
666 {
667     demux_t     *p_demux = (demux_t *)p_this;
668     demux_sys_t *p_sys   = p_demux->p_sys;
669     struct v4l2_buffer buf;
670     enum v4l2_buf_type buf_type;
671     unsigned int i;
672
673     /* Stop video capture */
674     if( p_sys->i_fd_video >= 0 )
675     {
676         switch( p_sys->io )
677         {
678         case IO_METHOD_READ:
679             /* Nothing to do */
680             break;
681
682         case IO_METHOD_MMAP:
683         case IO_METHOD_USERPTR:
684             /* Some drivers 'hang' internally if this is not done before streamoff */
685             for( unsigned int i = 0; i < p_sys->i_nbuffers; i++ )
686             {
687                 memset( &buf, 0, sizeof(buf) );
688                 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
689                 buf.memory = ( p_sys->io == IO_METHOD_USERPTR ) ?
690                     V4L2_MEMORY_USERPTR : V4L2_MEMORY_MMAP;
691                 ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ); /* ignore result */
692             }
693
694             buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
695             if( ioctl( p_sys->i_fd_video, VIDIOC_STREAMOFF, &buf_type ) < 0 ) {
696                 msg_Err( p_demux, "VIDIOC_STREAMOFF failed" );
697             }
698
699             break;
700         }
701     }
702
703     /* Free Video Buffers */
704     if( p_sys->p_buffers ) {
705         switch( p_sys->io )
706         {
707         case IO_METHOD_READ:
708             free( p_sys->p_buffers[0].start );
709             break;
710
711         case IO_METHOD_MMAP:
712             for( i = 0; i < p_sys->i_nbuffers; ++i )
713             {
714                 if( munmap( p_sys->p_buffers[i].start, p_sys->p_buffers[i].length ) )
715                 {
716                     msg_Err( p_demux, "munmap failed" );
717                 }
718             }
719             break;
720
721         case IO_METHOD_USERPTR:
722             for( i = 0; i < p_sys->i_nbuffers; ++i )
723             {
724                free( p_sys->p_buffers[i].orig_userp );
725             }
726             break;
727         }
728         free( p_sys->p_buffers );
729     }
730
731     /* Close */
732     if( p_sys->i_fd_video >= 0 ) close( p_sys->i_fd_video );
733     if( p_sys->i_fd_audio >= 0 ) close( p_sys->i_fd_audio );
734
735     if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio );
736     if( p_sys->psz_device ) free( p_sys->psz_device );
737     if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
738     if( p_sys->psz_adev ) free( p_sys->psz_adev );
739     if( p_sys->p_standards ) free( p_sys->p_standards );
740     if( p_sys->p_inputs ) free( p_sys->p_inputs );
741     if( p_sys->p_tuners ) free( p_sys->p_tuners );
742     if( p_sys->p_codecs ) free( p_sys->p_codecs );
743     if( p_sys->psz_requested_chroma ) free( p_sys->psz_requested_chroma );
744
745     free( p_sys );
746 }
747
748 /*****************************************************************************
749  * Control:
750  *****************************************************************************/
751 static int Control( demux_t *p_demux, int i_query, va_list args )
752 {
753     demux_sys_t *p_sys = p_demux->p_sys;
754     vlc_bool_t *pb;
755     int64_t    *pi64;
756
757     switch( i_query )
758     {
759         /* Special for access_demux */
760         case DEMUX_CAN_PAUSE:
761         case DEMUX_SET_PAUSE_STATE:
762         case DEMUX_CAN_CONTROL_PACE:
763             pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
764             *pb = VLC_FALSE;
765             return VLC_SUCCESS;
766
767         case DEMUX_GET_PTS_DELAY:
768             pi64 = (int64_t*)va_arg( args, int64_t * );
769             *pi64 = (int64_t)p_sys->i_pts * 1000;
770             return VLC_SUCCESS;
771
772         case DEMUX_GET_TIME:
773             pi64 = (int64_t*)va_arg( args, int64_t * );
774             *pi64 = mdate();
775             return VLC_SUCCESS;
776
777         /* TODO implement others */
778         default:
779             return VLC_EGENERIC;
780     }
781
782     return VLC_EGENERIC;
783 }
784
785 /*****************************************************************************
786  * Demux: Processes the audio or video frame
787  *****************************************************************************/
788 static int Demux( demux_t *p_demux )
789 {
790     demux_sys_t *p_sys = p_demux->p_sys;
791     es_out_id_t *p_es = p_sys->p_es_audio;
792     block_t *p_block = NULL;
793
794     /* Try grabbing audio frames first */
795     if( p_sys->i_fd_audio < 0 || !( p_block = GrabAudio( p_demux ) ) )
796     {
797         /* Try grabbing video frame */
798         p_es = p_sys->p_es_video;
799         if( p_sys->i_fd_video > 0 ) p_block = GrabVideo( p_demux );
800     }
801
802     if( !p_block )
803     {
804         /* Sleep so we do not consume all the cpu, 10ms seems
805          * like a good value (100fps) */
806         msleep( 10 );
807         return 1;
808     }
809
810     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
811     es_out_Send( p_demux->out, p_es, p_block );
812
813     return 1;
814 }
815
816 /*****************************************************************************
817  * GrabVideo: Grab a video frame
818  *****************************************************************************/
819 static block_t* GrabVideo( demux_t *p_demux )
820 {
821     demux_sys_t *p_sys = p_demux->p_sys;
822
823     block_t *p_block = NULL;
824     struct v4l2_buffer buf;
825
826     if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
827     {
828         mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
829
830         /* Did we wait long enough ? (frame rate reduction) */
831         if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
832     }
833
834     /* Grab Video Frame */
835     switch( p_sys->io )
836     {
837     case IO_METHOD_READ:
838         if( read( p_sys->i_fd_video, p_sys->p_buffers[0].start, p_sys->p_buffers[0].length ) )
839         {
840             switch( errno )
841             {
842             case EAGAIN:
843                 return 0;
844             case EIO:
845                 /* Could ignore EIO, see spec. */
846                 /* fall through */
847             default:
848                 msg_Err( p_demux, "Failed to read frame" );
849                 return 0;
850                }
851         }
852
853         p_block = ProcessVideoFrame( p_demux, (uint8_t*)p_sys->p_buffers[0].start );
854         if( !p_block ) return 0;
855
856         break;
857
858     case IO_METHOD_MMAP:
859         memset( &buf, 0, sizeof(buf) );
860         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
861         buf.memory = V4L2_MEMORY_MMAP;
862
863         /* Wait for next frame */
864         if (ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ) < 0 )
865         {
866             switch( errno )
867             {
868             case EAGAIN:
869                 return 0;
870             case EIO:
871                 /* Could ignore EIO, see spec. */
872                 /* fall through */
873             default:
874                 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
875                 return 0;
876                }
877         }
878
879         if( buf.index >= p_sys->i_nbuffers ) {
880             msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
881             return 0;
882         }
883
884         p_block = ProcessVideoFrame( p_demux, p_sys->p_buffers[buf.index].start );
885         if( !p_block ) return 0;
886
887         /* Unlock */
888         if( ioctl( p_sys->i_fd_video, VIDIOC_QBUF, &buf ) < 0 )
889         {
890             msg_Err (p_demux, "Failed to unlock (VIDIOC_QBUF)");
891             return 0;
892         }
893
894         break;
895
896     case IO_METHOD_USERPTR:
897         memset( &buf, 0, sizeof(buf) );
898         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
899         buf.memory = V4L2_MEMORY_USERPTR;
900
901         /* Wait for next frame */
902         if (ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ) < 0 )
903         {
904             switch( errno )
905             {
906             case EAGAIN:
907                 return 0;
908             case EIO:
909                 /* Could ignore EIO, see spec. */
910                 /* fall through */
911             default:
912                 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
913                 return 0;
914             }
915         }
916
917         /* Find frame? */
918         unsigned int i;
919         for( i = 0; i < p_sys->i_nbuffers; i++ )
920         {
921             if( buf.m.userptr == (unsigned long)p_sys->p_buffers[i].start &&
922                 buf.length == p_sys->p_buffers[i].length ) break;
923         }
924
925         if( i >= p_sys->i_nbuffers )
926         {
927             msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
928             return 0;
929         }
930
931         p_block = ProcessVideoFrame( p_demux, (uint8_t*)buf.m.userptr );
932         if( !p_block ) return 0;
933
934         /* Unlock */
935         if( ioctl( p_sys->i_fd_video, VIDIOC_QBUF, &buf ) < 0 )
936         {
937             msg_Err (p_demux, "Failed to unlock (VIDIOC_QBUF)");
938             return 0;
939         }
940
941         break;
942
943     }
944
945     /* Timestamp */
946     p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();
947
948     return p_block;
949 }
950
951 /*****************************************************************************
952  * ProcessVideoFrame: Helper function to take a buffer and copy it into
953  * a new block
954  *****************************************************************************/
955 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame )
956 {
957     demux_sys_t *p_sys = p_demux->p_sys;
958     block_t *p_block;
959
960     if( !p_frame ) return 0;
961
962     /* New block */
963     if( !( p_block = block_New( p_demux, p_sys->i_video_frame_size ) ) )
964     {
965         msg_Warn( p_demux, "Cannot get new block" );
966         return 0;
967     }
968
969     /* Copy frame */
970     memcpy( p_block->p_buffer, p_frame, p_sys->i_video_frame_size );
971
972     return p_block;
973 }
974
975 /*****************************************************************************
976  * GrabAudio: Grab an audio frame
977  *****************************************************************************/
978 static block_t* GrabAudio( demux_t *p_demux )
979 {
980     demux_sys_t *p_sys = p_demux->p_sys;
981     struct audio_buf_info buf_info;
982     int i_read, i_correct;
983     block_t *p_block;
984
985     /* Copied from v4l.c */
986
987     if( p_sys->p_block_audio ) p_block = p_sys->p_block_audio;
988     else p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
989
990     if( !p_block )
991     {
992         msg_Warn( p_demux, "cannot get block" );
993         return 0;
994     }
995
996     p_sys->p_block_audio = p_block;
997
998     i_read = read( p_sys->i_fd_audio, p_block->p_buffer,
999                    p_sys->i_audio_max_frame_size );
1000
1001     if( i_read <= 0 ) return 0;
1002
1003     p_block->i_buffer = i_read;
1004     p_sys->p_block_audio = 0;
1005
1006     /* Correct the date because of kernel buffering */
1007     i_correct = i_read;
1008     if( ioctl( p_sys->i_fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
1009     {
1010         i_correct += buf_info.bytes;
1011     }
1012
1013     /* Timestamp */
1014     p_block->i_pts = p_block->i_dts =
1015         mdate() - I64C(1000000) * (mtime_t)i_correct /
1016         2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
1017
1018     return p_block;
1019 }
1020
1021 /*****************************************************************************
1022  * Helper function to initalise video IO using the Read method
1023  *****************************************************************************/
1024 static int InitRead( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
1025 {
1026     demux_sys_t *p_sys = p_demux->p_sys;
1027
1028     p_sys->p_buffers = calloc( 1, sizeof( *p_sys->p_buffers ) );
1029     if( !p_sys->p_buffers )
1030     {
1031         msg_Err( p_demux, "Out of memory" );
1032         goto open_failed;
1033     }
1034
1035     p_sys->p_buffers[0].length = i_buffer_size;
1036     p_sys->p_buffers[0].start = malloc( i_buffer_size );
1037     if( !p_sys->p_buffers[0].start )
1038     {
1039         msg_Err( p_demux, "Out of memory" );
1040         goto open_failed;
1041     }
1042
1043     return VLC_SUCCESS;
1044
1045 open_failed:
1046     return VLC_EGENERIC;
1047
1048 }
1049
1050 /*****************************************************************************
1051  * Helper function to initalise video IO using the mmap method
1052  *****************************************************************************/
1053 static int InitMmap( demux_t *p_demux, int i_fd )
1054 {
1055     demux_sys_t *p_sys = p_demux->p_sys;
1056     struct v4l2_requestbuffers req;
1057
1058     memset( &req, 0, sizeof(req) );
1059     req.count = 4;
1060     req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1061     req.memory = V4L2_MEMORY_MMAP;
1062
1063     if( ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1064     {
1065         msg_Err( p_demux, "device does not support mmap i/o" );
1066         goto open_failed;
1067     }
1068
1069     if( req.count < 2 )
1070     {
1071         msg_Err( p_demux, "Insufficient buffer memory" );
1072         goto open_failed;
1073     }
1074
1075     p_sys->p_buffers = calloc( req.count, sizeof( *p_sys->p_buffers ) );
1076     if( !p_sys->p_buffers )
1077     {
1078         msg_Err( p_demux, "Out of memory" );
1079         goto open_failed;
1080     }
1081
1082     for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < req.count; ++p_sys->i_nbuffers )
1083     {
1084         struct v4l2_buffer buf;
1085
1086         memset( &buf, 0, sizeof(buf) );
1087         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1088         buf.memory = V4L2_MEMORY_MMAP;
1089         buf.index = p_sys->i_nbuffers;
1090
1091         if( ioctl( i_fd, VIDIOC_QUERYBUF, &buf ) < 0 )
1092         {
1093             msg_Err( p_demux, "VIDIOC_QUERYBUF" );
1094             goto open_failed;
1095         }
1096
1097         p_sys->p_buffers[p_sys->i_nbuffers].length = buf.length;
1098         p_sys->p_buffers[p_sys->i_nbuffers].start =
1099             mmap( NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, buf.m.offset );
1100
1101         if( p_sys->p_buffers[p_sys->i_nbuffers].start == MAP_FAILED )
1102         {
1103             msg_Err( p_demux, "mmap failed (%m)" );
1104             goto open_failed;
1105         }
1106     }
1107
1108     return VLC_SUCCESS;
1109
1110 open_failed:
1111     return VLC_EGENERIC;
1112
1113 }
1114
1115 /*****************************************************************************
1116  * Helper function to initalise video IO using the userbuf method
1117  *****************************************************************************/
1118 static int InitUserP( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
1119 {
1120     demux_sys_t *p_sys = p_demux->p_sys;
1121     struct v4l2_requestbuffers req;
1122     unsigned int i_page_size;
1123
1124     i_page_size = getpagesize();
1125     i_buffer_size = ( i_buffer_size + i_page_size - 1 ) & ~( i_page_size - 1);
1126
1127     memset( &req, 0, sizeof(req) );
1128     req.count = 4;
1129     req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1130     req.memory = V4L2_MEMORY_USERPTR;
1131
1132     if( ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1133     {
1134         msg_Err( p_demux, "device does not support user pointer i/o" );
1135         goto open_failed;
1136     }
1137
1138     p_sys->p_buffers = calloc( 4, sizeof( *p_sys->p_buffers ) );
1139     if( !p_sys->p_buffers )
1140     {
1141         msg_Err( p_demux, "Out of memory" );
1142         goto open_failed;
1143     }
1144
1145     for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < 4; ++p_sys->i_nbuffers )
1146     {
1147         p_sys->p_buffers[p_sys->i_nbuffers].length = i_buffer_size;
1148         p_sys->p_buffers[p_sys->i_nbuffers].start =
1149             vlc_memalign( &p_sys->p_buffers[p_sys->i_nbuffers].orig_userp,
1150                 /* boundary */ i_page_size, i_buffer_size );
1151
1152         if( !p_sys->p_buffers[p_sys->i_nbuffers].start )
1153         {
1154             msg_Err( p_demux, "out of memory" );
1155             goto open_failed;
1156         }
1157     }
1158
1159     return VLC_SUCCESS;
1160
1161 open_failed:
1162     return VLC_EGENERIC;
1163
1164 }
1165
1166 /*****************************************************************************
1167  * IsPixelFormatSupported: returns true if the specified V4L2 pixel format is
1168  * in the array of supported formats returned by the driver
1169  *****************************************************************************/
1170 vlc_bool_t IsPixelFormatSupported( demux_t *p_demux, unsigned int i_pixelformat )
1171 {
1172     demux_sys_t *p_sys = p_demux->p_sys;
1173
1174     for( int i_index = 0; i_index < p_sys->i_codec; i_index++ )
1175     {
1176         if( p_sys->p_codecs[i_index].pixelformat == i_pixelformat ) return VLC_TRUE;
1177     }
1178
1179     return VLC_FALSE;
1180 }
1181
1182 /*****************************************************************************
1183  * OpenVideoDev: open and set up the video device and probe for capabilities
1184  *****************************************************************************/
1185 int OpenVideoDev( demux_t *p_demux, char *psz_device )
1186 {
1187     int i_fd;
1188     demux_sys_t *p_sys = p_demux->p_sys;
1189     struct v4l2_cropcap cropcap;
1190     struct v4l2_crop crop;
1191     struct v4l2_format fmt;
1192     unsigned int i_min;
1193     enum v4l2_buf_type buf_type;
1194
1195     if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
1196     {
1197         msg_Err( p_demux, "cannot open device (%m)" );
1198         goto open_failed;
1199     }
1200
1201     /* Select standard */
1202
1203     if( p_sys->i_selected_standard_id != V4L2_STD_UNKNOWN )
1204     {
1205         if( ioctl( i_fd, VIDIOC_S_STD, &p_sys->i_selected_standard_id ) < 0 )
1206         {
1207             msg_Err( p_demux, "cannot set standard (%m)" );
1208             goto open_failed;
1209         }
1210         msg_Dbg( p_demux, "Set standard" );
1211     }
1212
1213     /* Select input */
1214
1215     if( p_sys->i_selected_input > p_sys->i_input )
1216     {
1217         msg_Warn( p_demux, "invalid input. Using the default one" );
1218         p_sys->i_selected_input = 0;
1219     }
1220
1221     if( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_selected_input ) < 0 )
1222     {
1223         msg_Err( p_demux, "cannot set input (%m)" );
1224         goto open_failed;
1225     }
1226
1227     /* Verify device support for the various IO methods */
1228     switch( p_sys->io )
1229     {
1230     case IO_METHOD_READ:
1231         if( !(p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE) )
1232         {
1233             msg_Err( p_demux, "device does not support read i/o" );
1234             goto open_failed;
1235         }
1236         break;
1237
1238     case IO_METHOD_MMAP:
1239     case IO_METHOD_USERPTR:
1240         if( !(p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING) )
1241         {
1242             msg_Err( p_demux, "device does not support streaming i/o" );
1243             goto open_failed;
1244         }
1245         break;
1246
1247     default:
1248         msg_Err( p_demux, "io method not supported" );
1249         goto open_failed;
1250     }
1251
1252     /* Reset Cropping */
1253     memset( &cropcap, 0, sizeof(cropcap) );
1254     cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1255     if( ioctl( i_fd, VIDIOC_CROPCAP, &cropcap ) >= 0 )
1256     {
1257         crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1258         crop.c = cropcap.defrect; /* reset to default */
1259         if( ioctl( i_fd, VIDIOC_S_CROP, &crop ) < 0 )
1260         {
1261             switch( errno )
1262             {
1263             case EINVAL:
1264                 /* Cropping not supported. */
1265                 break;
1266             default:
1267                 /* Errors ignored. */
1268                 break;
1269             }
1270         }
1271     }
1272
1273     /* Try and find default resolution if not specified */
1274     memset( &fmt, 0, sizeof(fmt) );
1275     fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1276
1277     if( p_sys->i_width <= 0 || p_sys->i_height <= 0 )
1278     {
1279         if( ioctl( i_fd, VIDIOC_G_FMT, &fmt ) < 0 )
1280         {
1281             msg_Err( p_demux, "Cannot get default width and height." );
1282             goto open_failed;
1283         }
1284
1285         p_sys->i_width = fmt.fmt.pix.width;
1286         p_sys->i_height = fmt.fmt.pix.height;
1287
1288         if( fmt.fmt.pix.field == V4L2_FIELD_ALTERNATE )
1289         {
1290             p_sys->i_height = p_sys->i_height * 2;
1291         }
1292     }
1293     else
1294     {
1295         msg_Dbg( p_demux, "trying specified size %dx%d", p_sys->i_width, p_sys->i_height );
1296     }
1297
1298     fmt.fmt.pix.width = p_sys->i_width;
1299     fmt.fmt.pix.height = p_sys->i_height;
1300     fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
1301
1302     /* Test and set Chroma */
1303     fmt.fmt.pix.pixelformat = 0;
1304     if( p_sys->psz_requested_chroma && strlen( p_sys->psz_requested_chroma ) > 0 )
1305     {
1306         /* User specified chroma */
1307         if( strlen( p_sys->psz_requested_chroma ) >= 4 )
1308         {
1309             int i_requested_fourcc = VLC_FOURCC(
1310                 p_sys->psz_requested_chroma[0], p_sys->psz_requested_chroma[1],
1311                 p_sys->psz_requested_chroma[2], p_sys->psz_requested_chroma[3] );
1312             for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
1313             {
1314                 if( v4l2chroma_to_fourcc[i].i_fourcc == i_requested_fourcc )
1315                 {
1316                     fmt.fmt.pix.pixelformat = v4l2chroma_to_fourcc[i].i_v4l2;
1317                     break;
1318                 }
1319             }
1320         }
1321         /* Try and set user chroma */
1322         if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ( fmt.fmt.pix.pixelformat && ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 ) )
1323         {
1324             msg_Warn( p_demux, "Driver is unable to use specified chroma %s. Trying defaults.", p_sys->psz_requested_chroma );
1325             fmt.fmt.pix.pixelformat = 0;
1326         }
1327     }
1328
1329     /* If no user specified chroma, find best */
1330     if( !fmt.fmt.pix.pixelformat )
1331     {
1332         fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YVU420;
1333         if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 )
1334         {
1335             fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
1336             if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 )
1337             {
1338                 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1339                 if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 )
1340                 {
1341                     msg_Err( p_demux, "Could not select any of the default chromas!" );
1342                     goto open_failed;
1343                 }
1344             }
1345         }
1346     }
1347
1348     /* Reassign width, height and chroma incase driver override */
1349     p_sys->i_width = fmt.fmt.pix.width;
1350     p_sys->i_height = fmt.fmt.pix.height;
1351
1352     /* Look up final fourcc */
1353     p_sys->i_fourcc = 0;
1354     for( int i = 0; v4l2chroma_to_fourcc[i].i_fourcc != 0; i++ )
1355     {
1356         if( v4l2chroma_to_fourcc[i].i_v4l2 == fmt.fmt.pix.pixelformat )
1357         {
1358             p_sys->i_fourcc = v4l2chroma_to_fourcc[i].i_fourcc;
1359             break;
1360         }
1361     }
1362
1363     /* Buggy driver paranoia */
1364     i_min = fmt.fmt.pix.width * 2;
1365     if( fmt.fmt.pix.bytesperline < i_min )
1366         fmt.fmt.pix.bytesperline = i_min;
1367     i_min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
1368     if( fmt.fmt.pix.sizeimage < i_min )
1369         fmt.fmt.pix.sizeimage = i_min;
1370
1371 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1372     /* This is new in Linux 2.6.19 */
1373     /* List supported frame rates */
1374     struct v4l2_frmivalenum frmival;
1375     frmival.index = 0;
1376     frmival.pixel_format = fmt.fmt.pix.pixelformat;
1377     frmival.width = p_sys->i_width;
1378     frmival.height = p_sys->i_height;
1379     if( ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 )
1380     {
1381         char sz_fourcc[5];
1382         memset( &sz_fourcc, 0, sizeof( sz_fourcc ) );
1383         vlc_fourcc_to_char( p_sys->i_fourcc, &sz_fourcc );
1384         msg_Dbg( p_demux, "supported frame intervals for %4s, %dx%d:",
1385                  sz_fourcc, frmival.width, frmival.height );
1386         switch( frmival.type )
1387         {
1388             case V4L2_FRMIVAL_TYPE_DISCRETE:
1389                 do
1390                 {
1391                     msg_Dbg( p_demux, "    supported frame interval: %d/%d",
1392                              frmival.discrete.numerator,
1393                              frmival.discrete.denominator );
1394                     frmival.index++;
1395                 } while( ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 );
1396                 break;
1397             case V4L2_FRMIVAL_TYPE_STEPWISE:
1398                 msg_Dbg( p_demux, "    supported frame intervals: %d/%d to "
1399                          "%d/%d using %d/%d increments",
1400                          frmival.stepwise.min.numerator,
1401                          frmival.stepwise.min.denominator,
1402                          frmival.stepwise.max.numerator,
1403                          frmival.stepwise.max.denominator,
1404                          frmival.stepwise.step.numerator,
1405                          frmival.stepwise.step.denominator );
1406                 break;
1407             case V4L2_FRMIVAL_TYPE_CONTINUOUS:
1408                 msg_Dbg( p_demux, "    supported frame intervals: %d/%d to %d/%d",
1409                          frmival.stepwise.min.numerator,
1410                          frmival.stepwise.min.denominator,
1411                          frmival.stepwise.max.numerator,
1412                          frmival.stepwise.max.denominator );
1413                 break;
1414         }
1415     }
1416 #endif
1417
1418     /* Set picture controls... */
1419     struct v4l2_control control;
1420
1421     /* brightness */
1422     memset( &control, 0, sizeof( control ) );
1423     control.id = V4L2_CID_BRIGHTNESS;
1424     if( p_sys->i_brightness >= 0 )
1425     {
1426         control.value = p_sys->i_brightness;
1427         if( ioctl( i_fd, VIDIOC_S_CTRL, &control ) < 0 )
1428         {
1429             msg_Err( p_demux, "unable to set brightness to %d (%m)", p_sys->i_brightness );
1430             goto open_failed;
1431         }
1432     }
1433     if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
1434     {
1435         msg_Dbg( p_demux, "video brightness: %d", control.value );
1436     }
1437
1438     /* contrast */
1439     memset( &control, 0, sizeof( control ) );
1440     control.id = V4L2_CID_CONTRAST;
1441     if( p_sys->i_contrast >= 0 )
1442     {
1443         control.value = p_sys->i_contrast;
1444         if( ioctl( i_fd, VIDIOC_S_CTRL, &control ) < 0 )
1445         {
1446             msg_Err( p_demux, "unable to set contrast to %d (%m)", p_sys->i_contrast );
1447             goto open_failed;
1448         }
1449     }
1450     if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
1451     {
1452         msg_Dbg( p_demux, "video contrast: %d", control.value );
1453     }
1454
1455     /* saturation */
1456     memset( &control, 0, sizeof( control ) );
1457     control.id = V4L2_CID_SATURATION;
1458     if( p_sys->i_saturation >= 0 )
1459     {
1460         control.value = p_sys->i_saturation;
1461         if( ioctl( i_fd, VIDIOC_S_CTRL, &control ) < 0 )
1462         {
1463             msg_Err( p_demux, "unable to set saturation to %d (%m)", p_sys->i_saturation );
1464             goto open_failed;
1465         }
1466     }
1467     if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
1468     {
1469         msg_Dbg( p_demux, "video saturation: %d", control.value );
1470     }
1471
1472     /* hue */
1473     memset( &control, 0, sizeof( control ) );
1474     control.id = V4L2_CID_HUE;
1475     if( p_sys->i_hue >= 0 )
1476     {
1477         control.value = p_sys->i_hue;
1478         if( ioctl( i_fd, VIDIOC_S_CTRL, &control ) < 0 )
1479         {
1480             msg_Err( p_demux, "unable to set hue to %d (%m)", p_sys->i_hue );
1481             goto open_failed;
1482         }
1483     }
1484     if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
1485     {
1486         msg_Dbg( p_demux, "video hue: %d", control.value );
1487     }
1488
1489     /* Init vout Picture */
1490     vout_InitPicture( VLC_OBJECT(p_demux), &p_sys->pic, p_sys->i_fourcc,
1491         p_sys->i_width, p_sys->i_height, p_sys->i_width *
1492         VOUT_ASPECT_FACTOR / p_sys->i_height );
1493     if( !p_sys->pic.i_planes )
1494     {
1495         msg_Err( p_demux, "unsupported chroma" );
1496         goto open_failed;
1497     }
1498     p_sys->i_video_frame_size = 0;
1499     for( int i = 0; i < p_sys->pic.i_planes; i++ )
1500     {
1501          p_sys->i_video_frame_size += p_sys->pic.p[i].i_visible_lines * p_sys->pic.p[i].i_visible_pitch;
1502     }
1503
1504     /* Init IO method */
1505     switch( p_sys->io )
1506     {
1507     case IO_METHOD_READ:
1508         if( InitRead( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
1509         break;
1510
1511     case IO_METHOD_MMAP:
1512         if( InitMmap( p_demux, i_fd ) != VLC_SUCCESS ) goto open_failed;
1513         break;
1514
1515     case IO_METHOD_USERPTR:
1516         if( InitUserP( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
1517         break;
1518
1519     }
1520
1521     /* Add */
1522     es_format_t es_fmt;
1523     es_format_Init( &es_fmt, VIDEO_ES, p_sys->i_fourcc );
1524     es_fmt.video.i_width  = p_sys->i_width;
1525     es_fmt.video.i_height = p_sys->i_height;
1526     es_fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
1527
1528     /* Setup rgb mask for RGB formats */
1529     if( p_sys->i_fourcc == VLC_FOURCC( 'R','V','2','4' ) )
1530     {
1531         /* This is in BGR format */
1532         es_fmt.video.i_bmask = 0x00ff0000;
1533         es_fmt.video.i_gmask = 0x0000ff00;
1534         es_fmt.video.i_rmask = 0x000000ff;
1535     }
1536
1537     msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
1538         (char*)&es_fmt.i_codec, es_fmt.video.i_width, es_fmt.video.i_height );
1539     p_sys->p_es_video = es_out_Add( p_demux->out, &es_fmt );
1540
1541     /* Start Capture */
1542
1543     switch( p_sys->io )
1544     {
1545     case IO_METHOD_READ:
1546         /* Nothing to do */
1547         break;
1548
1549     case IO_METHOD_MMAP:
1550         for (unsigned int i = 0; i < p_sys->i_nbuffers; ++i)
1551         {
1552             struct v4l2_buffer buf;
1553
1554             memset( &buf, 0, sizeof(buf) );
1555             buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1556             buf.memory = V4L2_MEMORY_MMAP;
1557             buf.index = i;
1558
1559             if( ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
1560             {
1561                 msg_Err( p_demux, "VIDIOC_QBUF failed" );
1562                 goto open_failed;
1563             }
1564         }
1565
1566         buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1567         if( ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
1568         {
1569             msg_Err( p_demux, "VIDIOC_STREAMON failed" );
1570             goto open_failed;
1571         }
1572
1573         break;
1574
1575     case IO_METHOD_USERPTR:
1576         for( unsigned int i = 0; i < p_sys->i_nbuffers; ++i )
1577         {
1578             struct v4l2_buffer buf;
1579
1580             memset( &buf, 0, sizeof(buf) );
1581             buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1582             buf.memory = V4L2_MEMORY_USERPTR;
1583             buf.index = i;
1584             buf.m.userptr = (unsigned long)p_sys->p_buffers[i].start;
1585             buf.length = p_sys->p_buffers[i].length;
1586
1587             if( ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
1588             {
1589                 msg_Err( p_demux, "VIDIOC_QBUF failed" );
1590                 goto open_failed;
1591             }
1592         }
1593
1594         buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1595         if( ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
1596         {
1597             msg_Err( p_demux, "VIDIOC_STREAMON failed" );
1598             goto open_failed;
1599         }
1600
1601         break;
1602     }
1603
1604     /* report fps */
1605     if( p_sys->f_fps >= 0.1 )
1606     {
1607         msg_Dbg( p_demux, "User set fps=%f", p_sys->f_fps );
1608     }
1609
1610     return i_fd;
1611
1612 open_failed:
1613     if( i_fd >= 0 ) close( i_fd );
1614     return -1;
1615
1616 }
1617
1618 /*****************************************************************************
1619  * OpenAudioDev: open and set up the audio device and probe for capabilities
1620  *****************************************************************************/
1621 int OpenAudioDev( demux_t *p_demux, char *psz_device )
1622 {
1623     demux_sys_t *p_sys = p_demux->p_sys;
1624     int i_fd, i_format;
1625
1626     if( (i_fd = open( psz_device, O_RDONLY | O_NONBLOCK )) < 0 )
1627     {
1628         msg_Err( p_demux, "cannot open audio device (%m)" );
1629         goto adev_fail;
1630     }
1631
1632     i_format = AFMT_S16_LE;
1633     if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
1634         || i_format != AFMT_S16_LE )
1635     {
1636         msg_Err( p_demux, "cannot set audio format (16b little endian) "
1637                  "(%m)" );
1638         goto adev_fail;
1639     }
1640
1641     if( ioctl( i_fd, SNDCTL_DSP_STEREO,
1642                &p_sys->b_stereo ) < 0 )
1643     {
1644         msg_Err( p_demux, "cannot set audio channels count (%m)" );
1645         goto adev_fail;
1646     }
1647
1648     if( ioctl( i_fd, SNDCTL_DSP_SPEED,
1649                &p_sys->i_sample_rate ) < 0 )
1650     {
1651         msg_Err( p_demux, "cannot set audio sample rate (%m)" );
1652         goto adev_fail;
1653     }
1654
1655     msg_Dbg( p_demux, "opened adev=`%s' %s %dHz",
1656              psz_device, p_sys->b_stereo ? "stereo" : "mono",
1657              p_sys->i_sample_rate );
1658
1659     p_sys->i_audio_max_frame_size = 6 * 1024;
1660
1661     es_format_t fmt;
1662     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
1663
1664     fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
1665     fmt.audio.i_rate = p_sys->i_sample_rate;
1666     fmt.audio.i_bitspersample = 16; /* FIXME ? */
1667     fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8;
1668     fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample;
1669
1670     msg_Dbg( p_demux, "new audio es %d channels %dHz",
1671       fmt.audio.i_channels, fmt.audio.i_rate );
1672
1673     p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
1674
1675     return i_fd;
1676
1677  adev_fail:
1678
1679     if( i_fd >= 0 ) close( i_fd );
1680     return -1;
1681
1682 }
1683
1684 /*****************************************************************************
1685  * ProbeVideoDev: probe video for capabilities
1686  *****************************************************************************/
1687 vlc_bool_t ProbeVideoDev( demux_t *p_demux, char *psz_device )
1688 {
1689     int i_index;
1690     int i_standard;
1691
1692     int i_fd;
1693     demux_sys_t *p_sys = p_demux->p_sys;
1694
1695     if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
1696     {
1697         msg_Err( p_demux, "cannot open video device (%m)" );
1698         goto open_failed;
1699     }
1700
1701     /* Get device capabilites */
1702
1703     if( ioctl( i_fd, VIDIOC_QUERYCAP, &p_sys->dev_cap ) < 0 )
1704     {
1705         msg_Err( p_demux, "cannot get video capabilities (%m)" );
1706         goto open_failed;
1707     }
1708
1709     msg_Dbg( p_demux, "V4L2 device: %s using driver: %s (version: %u.%u.%u) on %s",
1710                             p_sys->dev_cap.card,
1711                             p_sys->dev_cap.driver,
1712                             (p_sys->dev_cap.version >> 16) & 0xFF,
1713                             (p_sys->dev_cap.version >> 8) & 0xFF,
1714                             p_sys->dev_cap.version & 0xFF,
1715                             p_sys->dev_cap.bus_info );
1716
1717     msg_Dbg( p_demux, "the device has the capabilities: (%c) Video Capure, "
1718                                                        "(%c) Audio, "
1719                                                        "(%c) Tuner",
1720              ( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE  ? 'X':' '),
1721              ( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO  ? 'X':' '),
1722              ( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER  ? 'X':' ') );
1723
1724     msg_Dbg( p_demux, "supported I/O methods are: (%c) Read/Write, "
1725                                                  "(%c) Streaming, "
1726                                                  "(%c) Asynchronous",
1727             ( p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE ? 'X':' ' ),
1728             ( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING ? 'X':' ' ),
1729             ( p_sys->dev_cap.capabilities & V4L2_CAP_ASYNCIO ? 'X':' ' ) );
1730
1731     /* Now, enumerate all the video inputs. This is useless at the moment
1732        since we have no way to present that info to the user except with
1733        debug messages */
1734
1735     if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
1736     {
1737         while( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_input ) >= 0 )
1738         {
1739             p_sys->i_input++;
1740         }
1741
1742         p_sys->p_inputs = calloc( 1, p_sys->i_input * sizeof( struct v4l2_input ) );
1743         if( !p_sys->p_inputs ) goto open_failed;
1744
1745         for( i_index = 0; i_index < p_sys->i_input; i_index++ )
1746         {
1747             p_sys->p_inputs[i_index].index = i_index;
1748
1749             if( ioctl( i_fd, VIDIOC_ENUMINPUT, &p_sys->p_inputs[i_index] ) )
1750             {
1751                 msg_Err( p_demux, "cannot get video input characteristics (%m)" );
1752                 goto open_failed;
1753             }
1754             msg_Dbg( p_demux, "video input %i (%s) has type: %s",
1755                                 i_index,
1756                                 p_sys->p_inputs[i_index].name,
1757                                 p_sys->p_inputs[i_index].type
1758                                         == V4L2_INPUT_TYPE_TUNER ?
1759                                         "Tuner adapter" :
1760                                         "External analog input" );
1761         }
1762     }
1763
1764     /* Probe video standards */
1765     if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
1766     {
1767         struct v4l2_standard t_standards;
1768         t_standards.index = 0;
1769         while( ioctl( i_fd, VIDIOC_ENUMSTD, &t_standards ) >=0 )
1770         {
1771             p_sys->i_standard++;
1772             t_standards.index = p_sys->i_standard;
1773         }
1774
1775         p_sys->p_standards = calloc( 1, p_sys->i_standard * sizeof( struct v4l2_standard ) );
1776         if( !p_sys->p_standards ) goto open_failed;
1777
1778         for( i_standard = 0; i_standard < p_sys->i_standard; i_standard++ )
1779         {
1780             p_sys->p_standards[i_standard].index = i_standard;
1781
1782             if( ioctl( i_fd, VIDIOC_ENUMSTD, &p_sys->p_standards[i_standard] ) )
1783             {
1784                 msg_Err( p_demux, "cannot get video input standards (%m)" );
1785                 goto open_failed;
1786             }
1787             msg_Dbg( p_demux, "video standard %i is: %s",
1788                                 i_standard,
1789                                 p_sys->p_standards[i_standard].name);
1790         }
1791     }
1792
1793     /* initialize the structures for the ioctls */
1794     for( i_index = 0; i_index < 32; i_index++ )
1795     {
1796         p_sys->p_audios[i_index].index = i_index;
1797     }
1798
1799     /* Probe audio inputs */
1800
1801     if( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO )
1802     {
1803         while( p_sys->i_audio < 32 &&
1804                ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_audio] ) >= 0 )
1805         {
1806             if( ioctl( i_fd, VIDIOC_G_AUDIO, &p_sys->p_audios[ p_sys->i_audio] ) < 0 )
1807             {
1808                 msg_Err( p_demux, "cannot get audio input characteristics (%m)" );
1809                 goto open_failed;
1810             }
1811
1812             msg_Dbg( p_demux, "audio device %i (%s) is %s",
1813                                 p_sys->i_audio,
1814                                 p_sys->p_audios[p_sys->i_audio].name,
1815                                 p_sys->p_audios[p_sys->i_audio].capability &
1816                                                     V4L2_AUDCAP_STEREO ?
1817                                         "Stereo" : "Mono" );
1818
1819             p_sys->i_audio++;
1820         }
1821     }
1822
1823     if( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER )
1824     {
1825         struct v4l2_tuner tuner;
1826
1827         memset( &tuner, 0, sizeof(tuner) );
1828         while( ioctl( i_fd, VIDIOC_S_TUNER, &tuner ) >= 0 )
1829         {
1830             p_sys->i_tuner++;
1831             tuner.index = p_sys->i_tuner;
1832         }
1833
1834         p_sys->p_tuners = calloc( 1, p_sys->i_tuner * sizeof( struct v4l2_tuner ) );
1835         if( !p_sys->p_tuners ) goto open_failed;
1836
1837         for( i_index = 0; i_index < p_sys->i_tuner; i_index++ )
1838         {
1839             p_sys->p_tuners[i_index].index = i_index;
1840
1841             if( ioctl( i_fd, VIDIOC_G_TUNER, &p_sys->p_tuners[i_index] ) )
1842             {
1843                 msg_Err( p_demux, "cannot get tuner characteristics (%m)" );
1844                 goto open_failed;
1845             }
1846             msg_Dbg( p_demux, "tuner %i (%s) has type: %s, "
1847                               "frequency range: %.1f %s -> %.1f %s",
1848                                 i_index,
1849                                 p_sys->p_tuners[i_index].name,
1850                                 p_sys->p_tuners[i_index].type
1851                                         == V4L2_TUNER_RADIO ?
1852                                         "Radio" : "Analog TV",
1853                                 p_sys->p_tuners[i_index].rangelow * 62.5,
1854                                 p_sys->p_tuners[i_index].capability &
1855                                         V4L2_TUNER_CAP_LOW ?
1856                                         "Hz" : "kHz",
1857                                 p_sys->p_tuners[i_index].rangehigh * 62.5,
1858                                 p_sys->p_tuners[i_index].capability &
1859                                         V4L2_TUNER_CAP_LOW ?
1860                                         "Hz" : "kHz" );
1861         }
1862     }
1863
1864     /* Probe for available chromas */
1865     if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
1866     {
1867         struct v4l2_fmtdesc codec;
1868
1869         i_index = 0;
1870         memset( &codec, 0, sizeof(codec) );
1871         codec.index = i_index;
1872         codec.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1873
1874         while( ioctl( i_fd, VIDIOC_ENUM_FMT, &codec ) >= 0 )
1875         {
1876             i_index++;
1877             codec.index = i_index;
1878         }
1879
1880         p_sys->i_codec = i_index;
1881
1882         p_sys->p_codecs = calloc( 1, p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
1883
1884         for( i_index = 0; i_index < p_sys->i_codec; i_index++ )
1885         {
1886             p_sys->p_codecs[i_index].index = i_index;
1887             p_sys->p_codecs[i_index].type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1888
1889             if( ioctl( i_fd, VIDIOC_ENUM_FMT, &p_sys->p_codecs[i_index] ) < 0 )
1890             {
1891                 msg_Err( p_demux, "cannot get codec description (%m)" );
1892                 goto open_failed;
1893             }
1894
1895             /* only print if vlc supports the format */
1896             vlc_bool_t b_codec_supported = VLC_FALSE;
1897             for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
1898             {
1899                 if( v4l2chroma_to_fourcc[i].i_v4l2 == p_sys->p_codecs[i_index].pixelformat )
1900                 {
1901                     b_codec_supported = VLC_TRUE;
1902
1903                     char sz_fourcc[5];
1904                     memset( &sz_fourcc, 0, sizeof( sz_fourcc ) );
1905                     vlc_fourcc_to_char( v4l2chroma_to_fourcc[i].i_fourcc, &sz_fourcc );
1906                     msg_Dbg( p_demux, "device supports chroma %4s [%s]",
1907                                 sz_fourcc,
1908                                 p_sys->p_codecs[i_index].description );
1909
1910 #ifdef VIDIOC_ENUM_FRAMESIZES
1911                     /* This is new in Linux 2.6.19 */
1912                     /* List valid frame sizes for this format */
1913                     struct v4l2_frmsizeenum frmsize;
1914                     frmsize.index = 0;
1915                     frmsize.pixel_format = p_sys->p_codecs[i_index].pixelformat;
1916                     if( ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) < 0 )
1917                     {
1918                         msg_Err( p_demux, "Error while querying for frame size" );
1919                     }
1920                     else
1921                     {
1922                         switch( frmsize.type )
1923                         {
1924                             case V4L2_FRMSIZE_TYPE_DISCRETE:
1925                                 do
1926                                 {
1927                                     msg_Dbg( p_demux,
1928                 "    device supports size %dx%d",
1929                 frmsize.discrete.width, frmsize.discrete.height );
1930                                     frmsize.index++;
1931                                 } while( ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) >= 0 );
1932                                 break;
1933                             case V4L2_FRMSIZE_TYPE_STEPWISE:
1934                                 msg_Dbg( p_demux,
1935                 "    device supports sizes %dx%d to %dx%d using %dx%d increments",
1936                 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
1937                 frmsize.stepwise.max_width, frmsize.stepwise.max_height,
1938                 frmsize.stepwise.step_width, frmsize.stepwise.step_height );
1939                                 break;
1940                             case V4L2_FRMSIZE_TYPE_CONTINUOUS:
1941                                 msg_Dbg( p_demux,
1942                 "    device supports all sizes %dx%d to %dx%d",
1943                 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
1944                 frmsize.stepwise.max_width, frmsize.stepwise.max_height );
1945                                 break;
1946                         }
1947                     }
1948 #endif
1949                 }
1950             }
1951             if( !b_codec_supported )
1952             {
1953                 msg_Dbg( p_demux, "device codec %s not supported",
1954                     p_sys->p_codecs[i_index].description );
1955             }
1956
1957         }
1958     }
1959
1960
1961     if( i_fd >= 0 ) close( i_fd );
1962     return VLC_TRUE;
1963
1964 open_failed:
1965
1966     if( i_fd >= 0 ) close( i_fd );
1967     return VLC_FALSE;
1968
1969 }
1970
1971 /*****************************************************************************
1972  * ProbeAudioDev: probe audio for capabilities
1973  *****************************************************************************/
1974 vlc_bool_t ProbeAudioDev( demux_t *p_demux, char *psz_device )
1975 {
1976     int i_fd = 0;
1977     int i_caps;
1978
1979     if( ( i_fd = open( psz_device, O_RDONLY | O_NONBLOCK ) ) < 0 )
1980     {
1981         msg_Err( p_demux, "cannot open audio device (%m)" );
1982         goto open_failed;
1983     }
1984
1985     /* this will fail if the device is video */
1986     if( ioctl( i_fd, SNDCTL_DSP_GETCAPS, &i_caps ) < 0 )
1987     {
1988         msg_Err( p_demux, "cannot get audio caps (%m)" );
1989         goto open_failed;
1990     }
1991
1992     if( i_fd >= 0 ) close( i_fd );
1993     return VLC_TRUE;
1994
1995 open_failed:
1996     if( i_fd >= 0 ) close( i_fd );
1997     return VLC_FALSE;
1998
1999 }
2000