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