]> git.sesse.net Git - vlc/blob - modules/access/v4l2/v4l2.c
Fix confusing variable name.
[vlc] / modules / access / v4l2 / v4l2.c
1 /*****************************************************************************
2  * v4l2.c : Video4Linux2 input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Benjamin Pracht <bigben at videolan dot org>
8  *          Richard Hosking <richard at hovis dot net>
9  *          Antoine Cellerier <dionoea at videolan d.t org>
10  *          Dennis Lou <dlou99 at yahoo dot com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*
28  * Sections based on the reference V4L2 capture example at
29  * http://v4l2spec.bytesex.org/spec/capture-example.html
30  */
31
32 /*****************************************************************************
33  * Preamble
34  *****************************************************************************/
35
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
39
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
42 #include <vlc_access.h>
43 #include <vlc_demux.h>
44 #include <vlc_input.h>
45 #include <vlc_vout.h>
46
47 #include <ctype.h>
48 #include <fcntl.h>
49 #include <unistd.h>
50 #include <sys/ioctl.h>
51 #include <sys/mman.h>
52
53 #include <linux/videodev2.h>
54
55 #include <sys/soundcard.h>
56
57 #ifdef HAVE_ALSA
58 #   define ALSA_PCM_NEW_HW_PARAMS_API
59 #   define ALSA_PCM_NEW_SW_PARAMS_API
60 #   include <alsa/asoundlib.h>
61 #endif
62
63 #include <poll.h>
64
65 #ifdef HAVE_LIBV4L2
66 #   include <libv4l2.h>
67 #else
68 #   define v4l2_close close
69 #   define v4l2_dup dup
70 #   define v4l2_ioctl ioctl
71 #   define v4l2_read read
72 #   define v4l2_mmap mmap
73 #   define v4l2_munmap munmap
74 #endif
75
76 /*****************************************************************************
77  * Module descriptior
78  *****************************************************************************/
79
80 static int  DemuxOpen ( vlc_object_t * );
81 static void DemuxClose( vlc_object_t * );
82 static int  AccessOpen ( vlc_object_t * );
83 static void AccessClose( vlc_object_t * );
84
85 #define STANDARD_TEXT N_( "Standard" )
86 #define STANDARD_LONGTEXT N_( \
87     "Video standard (Default, SECAM, PAL, or NTSC)." )
88 #define CHROMA_TEXT N_("Video input chroma format")
89 #define CHROMA_LONGTEXT N_( \
90     "Force the Video4Linux2 video device to use a specific chroma format " \
91     "(eg. I420 or I422 for raw images, MJPG for M-JPEG compressed input) " \
92     "(Complete list: GREY, I240, RV16, RV15, RV24, RV32, YUY2, YUYV, UYVY, " \
93     "I41N, I422, I420, I411, I410, MJPG)")
94 #define INPUT_TEXT N_( "Input" )
95 #define INPUT_LONGTEXT N_( \
96     "Input of the card to use (see debug)." )
97 #define AUDIO_INPUT_TEXT N_( "Audio input" )
98 #define AUDIO_INPUT_LONGTEXT N_( \
99     "Audio input of the card to use (see debug)." )
100 #define IOMETHOD_TEXT N_( "IO Method" )
101 #define IOMETHOD_LONGTEXT N_( \
102     "IO Method (READ, MMAP, USERPTR)." )
103 #define WIDTH_TEXT N_( "Width" )
104 #define WIDTH_LONGTEXT N_( \
105     "Force width (-1 for autodetect)." )
106 #define HEIGHT_TEXT N_( "Height" )
107 #define HEIGHT_LONGTEXT N_( \
108     "Force height (-1 for autodetect)." )
109 #define FPS_TEXT N_( "Framerate" )
110 #define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
111     "(-1 for autodetect)." )
112
113 #define CTRL_RESET_TEXT N_( "Reset v4l2 controls" )
114 #define CTRL_RESET_LONGTEXT N_( \
115     "Reset controls to defaults provided by the v4l2 driver." )
116 #define BRIGHTNESS_TEXT N_( "Brightness" )
117 #define BRIGHTNESS_LONGTEXT N_( \
118     "Brightness of the video input (if supported by the v4l2 driver)." )
119 #define CONTRAST_TEXT N_( "Contrast" )
120 #define CONTRAST_LONGTEXT N_( \
121     "Contrast of the video input (if supported by the v4l2 driver)." )
122 #define SATURATION_TEXT N_( "Saturation" )
123 #define SATURATION_LONGTEXT N_( \
124     "Saturation of the video input (if supported by the v4l2 driver)." )
125 #define HUE_TEXT N_( "Hue" )
126 #define HUE_LONGTEXT N_( \
127     "Hue of the video input (if supported by the v4l2 driver)." )
128 #define BLACKLEVEL_TEXT N_( "Black level" )
129 #define BLACKLEVEL_LONGTEXT N_( \
130     "Black level of the video input (if supported by the v4l2 driver)." )
131 #define AUTOWHITEBALANCE_TEXT N_( "Auto white balance" )
132 #define AUTOWHITEBALANCE_LONGTEXT N_( \
133     "Automatically set the white balance of the video input " \
134     "(if supported by the v4l2 driver)." )
135 #define DOWHITEBALANCE_TEXT N_( "Do white balance" )
136 #define DOWHITEBALANCE_LONGTEXT N_( \
137     "Trigger a white balancing action, useless if auto white balance is " \
138     "activated (if supported by the v4l2 driver)." )
139 #define REDBALANCE_TEXT N_( "Red balance" )
140 #define REDBALANCE_LONGTEXT N_( \
141     "Red balance of the video input (if supported by the v4l2 driver)." )
142 #define BLUEBALANCE_TEXT N_( "Blue balance" )
143 #define BLUEBALANCE_LONGTEXT N_( \
144     "Blue balance of the video input (if supported by the v4l2 driver)." )
145 #define GAMMA_TEXT N_( "Gamma" )
146 #define GAMMA_LONGTEXT N_( \
147     "Gamma of the video input (if supported by the v4l2 driver)." )
148 #define EXPOSURE_TEXT N_( "Exposure" )
149 #define EXPOSURE_LONGTEXT N_( \
150     "Exposure of the video input (if supported by the v4L2 driver)." )
151 #define AUTOGAIN_TEXT N_( "Auto gain" )
152 #define AUTOGAIN_LONGTEXT N_( \
153     "Automatically set the video input's gain (if supported by the " \
154     "v4l2 driver)." )
155 #define GAIN_TEXT N_( "Gain" )
156 #define GAIN_LONGTEXT N_( \
157     "Video input's gain (if supported by the v4l2 driver)." )
158 #define HFLIP_TEXT N_( "Horizontal flip" )
159 #define HFLIP_LONGTEXT N_( \
160     "Flip the video horizontally (if supported by the v4l2 driver)." )
161 #define VFLIP_TEXT N_( "Vertical flip" )
162 #define VFLIP_LONGTEXT N_( \
163     "Flip the video vertically (if supported by the v4l2 driver)." )
164 #define HCENTER_TEXT N_( "Horizontal centering" )
165 #define HCENTER_LONGTEXT N_( \
166     "Set the camera's horizontal centering (if supported by the v4l2 driver)." )
167 #define VCENTER_TEXT N_( "Vertical centering" )
168 #define VCENTER_LONGTEXT N_( \
169     "Set the camera's vertical centering (if supported by the v4l2 driver)." )
170
171 #define AUDIO_VOLUME_TEXT N_( "Volume" )
172 #define AUDIO_VOLUME_LONGTEXT N_( \
173     "Volume of the audio input (if supported by the v4l2 driver)." )
174 #define AUDIO_BALANCE_TEXT N_( "Balance" )
175 #define AUDIO_BALANCE_LONGTEXT N_( \
176     "Balance of the audio input (if supported by the v4l2 driver)." )
177 #define AUDIO_MUTE_TEXT N_( "Mute" )
178 #define AUDIO_MUTE_LONGTEXT N_( \
179     "Mute audio input (if supported by the v4l2 driver)." )
180 #define AUDIO_BASS_TEXT N_( "Bass" )
181 #define AUDIO_BASS_LONGTEXT N_( \
182     "Bass level of the audio input (if supported by the v4l2 driver)." )
183 #define AUDIO_TREBLE_TEXT N_( "Treble" )
184 #define AUDIO_TREBLE_LONGTEXT N_( \
185     "Treble level of the audio input (if supported by the v4l2 driver)." )
186 #define AUDIO_LOUDNESS_TEXT N_( "Loudness" )
187 #define AUDIO_LOUDNESS_LONGTEXT N_( \
188     "Loudness of the audio input (if supported by the v4l2 driver)." )
189
190 #define CACHING_TEXT N_("Caching value in ms")
191 #define CACHING_LONGTEXT N_( \
192     "Caching value for V4L2 captures. This " \
193     "value should be set in milliseconds." )
194 #define S_CTRLS_TEXT N_("v4l2 driver controls")
195 #define S_CTRLS_LONGTEXT N_( \
196     "Set the v4l2 driver controls to the values specified using a comma " \
197     "separated list optionally encapsulated by curly braces " \
198     "(e.g.: {video_bitrate=6000000,audio_crc=0,stream_type=3} ). " \
199     "To list available controls, increase verbosity (-vvv) " \
200     "or use the v4l2-ctl application." )
201
202 #define TUNER_TEXT N_("Tuner id")
203 #define TUNER_LONGTEXT N_( \
204     "Tuner id (see debug output)." )
205 #define FREQUENCY_TEXT N_("Frequency")
206 #define FREQUENCY_LONGTEXT N_( \
207     "Tuner frequency in Hz or kHz (see debug output)" )
208 #define TUNER_AUDIO_MODE_TEXT N_("Audio mode")
209 #define TUNER_AUDIO_MODE_LONGTEXT N_( \
210     "Tuner audio mono/stereo and track selection." )
211
212 #define AUDIO_DEPRECATED_ERROR N_( \
213     "Alsa or OSS audio capture in the v4l2 access is deprecated. " \
214     "please use 'v4l2:/""/ --input-slave alsa:/""/' or " \
215     "'v4l2:/""/ --input-slave oss:/""/' instead." )
216
217 typedef enum {
218     IO_METHOD_READ,
219     IO_METHOD_MMAP,
220     IO_METHOD_USERPTR,
221 } io_method;
222
223 static const int i_standards_list[] =
224     { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC };
225 static const char *const psz_standards_list_text[] =
226     { N_("Default"), N_("SECAM"), N_("PAL"),  N_("NTSC") };
227
228 static const int i_iomethod_list[] =
229     { IO_METHOD_READ, IO_METHOD_MMAP, IO_METHOD_USERPTR };
230 static const char *const psz_iomethod_list_text[] =
231     { N_("READ"), N_("MMAP"),  N_("USERPTR") };
232
233 static const int i_tuner_audio_modes_list[] =
234     { V4L2_TUNER_MODE_MONO, V4L2_TUNER_MODE_STEREO,
235       V4L2_TUNER_MODE_LANG1, V4L2_TUNER_MODE_LANG2,
236       V4L2_TUNER_MODE_SAP, V4L2_TUNER_MODE_LANG1_LANG2 };
237 static const char *const psz_tuner_audio_modes_list_text[] =
238     { N_( "Mono" ),
239       N_( "Stereo" ),
240       N_( "Primary language (Analog TV tuners only)" ),
241       N_( "Secondary language (Analog TV tuners only)" ),
242       N_( "Second audio program (Analog TV tuners only)" ),
243       N_( "Primary language left, Secondary language right" ) };
244
245 #define V4L2_DEFAULT "/dev/video0"
246 #define CFG_PREFIX "v4l2-"
247
248 vlc_module_begin ()
249     set_shortname( N_("Video4Linux2") )
250     set_description( N_("Video4Linux2 input") )
251     set_category( CAT_INPUT )
252     set_subcategory( SUBCAT_INPUT_ACCESS )
253
254     set_section( N_( "Video input" ), NULL )
255     add_integer( CFG_PREFIX "standard", 0, NULL, STANDARD_TEXT,
256                  STANDARD_LONGTEXT, false );
257         change_integer_list( i_standards_list, psz_standards_list_text, NULL );
258     add_string( CFG_PREFIX "chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
259                 true );
260     add_integer( CFG_PREFIX "input", 0, NULL, INPUT_TEXT, INPUT_LONGTEXT,
261                 true );
262     add_integer( CFG_PREFIX "audio-input", 0, NULL, AUDIO_INPUT_TEXT,
263                  AUDIO_INPUT_LONGTEXT, true );
264     add_integer( CFG_PREFIX "io", IO_METHOD_MMAP, NULL, IOMETHOD_TEXT,
265                  IOMETHOD_LONGTEXT, true );
266         change_integer_list( i_iomethod_list, psz_iomethod_list_text, NULL );
267     add_integer( CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
268                 WIDTH_LONGTEXT, true );
269     add_integer( CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
270                 HEIGHT_LONGTEXT, true );
271     add_float( CFG_PREFIX "fps", 0, NULL, FPS_TEXT, FPS_LONGTEXT, true )
272     add_integer( CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
273                 CACHING_TEXT, CACHING_LONGTEXT, true );
274
275     set_section( N_( "Tuner" ), NULL )
276     add_integer( CFG_PREFIX "tuner", 0, NULL, TUNER_TEXT, TUNER_LONGTEXT,
277                  true );
278     add_integer( CFG_PREFIX "tuner-frequency", -1, NULL, FREQUENCY_TEXT,
279                  FREQUENCY_LONGTEXT, true );
280     add_integer( CFG_PREFIX "tuner-audio-mode", -1, NULL, TUNER_AUDIO_MODE_TEXT,
281                  TUNER_AUDIO_MODE_LONGTEXT, true );
282         change_integer_list( i_tuner_audio_modes_list,
283                              psz_tuner_audio_modes_list_text, 0 );
284
285     set_section( N_( "Controls" ),
286                  N_( "v4l2 driver controls, if supported by your v4l2 driver." ) );
287     add_bool( CFG_PREFIX "controls-reset", false, NULL, CTRL_RESET_TEXT,
288               CTRL_RESET_LONGTEXT, true );
289     add_integer( CFG_PREFIX "brightness", -1, NULL, BRIGHTNESS_TEXT,
290                  BRIGHTNESS_LONGTEXT, true );
291     add_integer( CFG_PREFIX "contrast", -1, NULL, CONTRAST_TEXT,
292                  CONTRAST_LONGTEXT, true );
293     add_integer( CFG_PREFIX "saturation", -1, NULL, SATURATION_TEXT,
294                  SATURATION_LONGTEXT, true );
295     add_integer( CFG_PREFIX "hue", -1, NULL, HUE_TEXT,
296                  HUE_LONGTEXT, true );
297     add_integer( CFG_PREFIX "black-level", -1, NULL, BLACKLEVEL_TEXT,
298                  BLACKLEVEL_LONGTEXT, true );
299     add_integer( CFG_PREFIX "auto-white-balance", -1, NULL,
300                  AUTOWHITEBALANCE_TEXT, AUTOWHITEBALANCE_LONGTEXT, true );
301     add_integer( CFG_PREFIX "do-white-balance", -1, NULL, DOWHITEBALANCE_TEXT,
302                  DOWHITEBALANCE_LONGTEXT, true );
303     add_integer( CFG_PREFIX "red-balance", -1, NULL, REDBALANCE_TEXT,
304                  REDBALANCE_LONGTEXT, true );
305     add_integer( CFG_PREFIX "blue-balance", -1, NULL, BLUEBALANCE_TEXT,
306                  BLUEBALANCE_LONGTEXT, true );
307     add_integer( CFG_PREFIX "gamma", -1, NULL, GAMMA_TEXT,
308                  GAMMA_LONGTEXT, true );
309     add_integer( CFG_PREFIX "exposure", -1, NULL, EXPOSURE_TEXT,
310                  EXPOSURE_LONGTEXT, true );
311     add_integer( CFG_PREFIX "autogain", -1, NULL, AUTOGAIN_TEXT,
312                  AUTOGAIN_LONGTEXT, true );
313     add_integer( CFG_PREFIX "gain", -1, NULL, GAIN_TEXT,
314                  GAIN_LONGTEXT, true );
315     add_integer( CFG_PREFIX "hflip", -1, NULL, HFLIP_TEXT,
316                  HFLIP_LONGTEXT, true );
317     add_integer( CFG_PREFIX "vflip", -1, NULL, VFLIP_TEXT,
318                  VFLIP_LONGTEXT, true );
319     add_integer( CFG_PREFIX "hcenter", -1, NULL, HCENTER_TEXT,
320                  HCENTER_LONGTEXT, true );
321     add_integer( CFG_PREFIX "vcenter", -1, NULL, VCENTER_TEXT,
322                  VCENTER_LONGTEXT, true );
323     add_integer( CFG_PREFIX "audio-volume", -1, NULL, AUDIO_VOLUME_TEXT,
324                 AUDIO_VOLUME_LONGTEXT, true );
325     add_integer( CFG_PREFIX "audio-balance", -1, NULL, AUDIO_BALANCE_TEXT,
326                 AUDIO_BALANCE_LONGTEXT, true );
327     add_bool( CFG_PREFIX "audio-mute", false, NULL, AUDIO_MUTE_TEXT,
328               AUDIO_MUTE_LONGTEXT, true );
329     add_integer( CFG_PREFIX "audio-bass", -1, NULL, AUDIO_BASS_TEXT,
330                 AUDIO_BASS_LONGTEXT, true );
331     add_integer( CFG_PREFIX "audio-treble", -1, NULL, AUDIO_TREBLE_TEXT,
332                 AUDIO_TREBLE_LONGTEXT, true );
333     add_integer( CFG_PREFIX "audio-loudness", -1, NULL, AUDIO_LOUDNESS_TEXT,
334                 AUDIO_LOUDNESS_LONGTEXT, true );
335     add_string( CFG_PREFIX "set-ctrls", NULL, NULL, S_CTRLS_TEXT,
336               S_CTRLS_LONGTEXT, true );
337
338     add_obsolete_string( CFG_PREFIX "dev" );
339
340     add_obsolete_string( CFG_PREFIX "adev" );
341     add_obsolete_integer( CFG_PREFIX "audio-method" );
342     add_obsolete_bool( CFG_PREFIX "stereo" );
343     add_obsolete_integer( CFG_PREFIX "samplerate" );
344
345     add_shortcut( "v4l2" )
346     set_capability( "access_demux", 10 )
347     set_callbacks( DemuxOpen, DemuxClose )
348
349     add_submodule ()
350     add_shortcut( "v4l2c" )
351     set_description( N_("Video4Linux2 Compressed A/V") )
352     set_capability( "access", 0 )
353     /* use these when open as access_demux fails; VLC will use another demux */
354     set_callbacks( AccessOpen, AccessClose )
355
356 vlc_module_end ()
357
358 /*****************************************************************************
359  * Access: local prototypes
360  *****************************************************************************/
361
362 static void CommonClose( vlc_object_t *, demux_sys_t * );
363 static void ParseMRL( demux_sys_t *, char *, vlc_object_t * );
364 static void GetV4L2Params( demux_sys_t *, vlc_object_t * );
365 static void SetAvailControlsByString( vlc_object_t *, demux_sys_t *, int );
366
367 static int DemuxControl( demux_t *, int, va_list );
368 static int AccessControl( access_t *, int, va_list );
369
370 static int Demux( demux_t * );
371 static ssize_t AccessRead( access_t *, uint8_t *, size_t );
372
373 static block_t* GrabVideo( demux_t *p_demux );
374 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t );
375
376 static bool IsPixelFormatSupported( demux_t *p_demux,
377                                           unsigned int i_pixelformat );
378
379 static int OpenVideoDev( vlc_object_t *, demux_sys_t *, bool );
380 static bool ProbeVideoDev( vlc_object_t *, demux_sys_t *,
381                                  const char *psz_device );
382
383 static int ControlList( vlc_object_t *, int , bool, bool );
384 static int Control( vlc_object_t *, int i_fd,
385                     const char *psz_name, int i_cid, int i_value );
386
387 static int DemuxControlCallback( vlc_object_t *p_this, const char *psz_var,
388                                  vlc_value_t oldval, vlc_value_t newval,
389                                  void *p_data );
390 static int DemuxControlResetCallback( vlc_object_t *p_this, const char *psz_var,
391                                       vlc_value_t oldval, vlc_value_t newval,
392                                       void *p_data );
393 static int AccessControlCallback( vlc_object_t *p_this, const char *psz_var,
394                                   vlc_value_t oldval, vlc_value_t newval,
395                                   void *p_data );
396 static int AccessControlResetCallback( vlc_object_t *p_this,
397                                        const char *psz_var, vlc_value_t oldval,
398                                        vlc_value_t newval, void *p_data );
399
400 static const struct
401 {
402     unsigned int i_v4l2;
403     int i_fourcc;
404     int i_rmask;
405     int i_gmask;
406     int i_bmask;
407 } v4l2chroma_to_fourcc[] =
408 {
409     /* Raw data types */
410     { V4L2_PIX_FMT_GREY,    VLC_FOURCC('G','R','E','Y'), 0, 0, 0 },
411     { V4L2_PIX_FMT_HI240,   VLC_FOURCC('I','2','4','0'), 0, 0, 0 },
412     { V4L2_PIX_FMT_RGB555,  VLC_FOURCC('R','V','1','5'), 0x001f,0x03e0,0x7c00 },
413     { V4L2_PIX_FMT_RGB565,  VLC_FOURCC('R','V','1','6'), 0x001f,0x07e0,0xf800 },
414     /* Won't work since we don't know how to handle such gmask values
415      * correctly
416     { V4L2_PIX_FMT_RGB555X, VLC_FOURCC('R','V','1','5'), 0x007c,0xe003,0x1f00 },
417     { V4L2_PIX_FMT_RGB565X, VLC_FOURCC('R','V','1','6'), 0x00f8,0xe007,0x1f00 },
418     */
419     { V4L2_PIX_FMT_BGR24,   VLC_FOURCC('R','V','2','4'), 0xff0000,0xff00,0xff },
420     { V4L2_PIX_FMT_RGB24,   VLC_FOURCC('R','V','2','4'), 0xff,0xff00,0xff0000 },
421     { V4L2_PIX_FMT_BGR32,   VLC_FOURCC('R','V','3','2'), 0xff0000,0xff00,0xff },
422     { V4L2_PIX_FMT_RGB32,   VLC_FOURCC('R','V','3','2'), 0xff,0xff00,0xff0000 },
423     { V4L2_PIX_FMT_YUYV,    VLC_FOURCC('Y','U','Y','2'), 0, 0, 0 },
424     { V4L2_PIX_FMT_YUYV,    VLC_FOURCC('Y','U','Y','V'), 0, 0, 0 },
425     { V4L2_PIX_FMT_UYVY,    VLC_FOURCC('U','Y','V','Y'), 0, 0, 0 },
426     { V4L2_PIX_FMT_Y41P,    VLC_FOURCC('I','4','1','N'), 0, 0, 0 },
427     { V4L2_PIX_FMT_YUV422P, VLC_FOURCC('I','4','2','2'), 0, 0, 0 },
428     { V4L2_PIX_FMT_YVU420,  VLC_FOURCC('Y','V','1','2'), 0, 0, 0 },
429     { V4L2_PIX_FMT_YUV411P, VLC_FOURCC('I','4','1','1'), 0, 0, 0 },
430     { V4L2_PIX_FMT_YUV410,  VLC_FOURCC('I','4','1','0'), 0, 0, 0 },
431
432     /* Raw data types, not in V4L2 spec but still in videodev2.h and supported
433      * by VLC */
434     { V4L2_PIX_FMT_YUV420,  VLC_FOURCC('I','4','2','0'), 0, 0, 0 },
435     /* FIXME { V4L2_PIX_FMT_RGB444,  VLC_FOURCC('R','V','3','2') }, */
436
437     /* Compressed data types */
438     { V4L2_PIX_FMT_MJPEG,   VLC_FOURCC('M','J','P','G'), 0, 0, 0 },
439 #if 0
440     { V4L2_PIX_FMT_JPEG,    VLC_FOURCC('J','P','E','G') },
441     { V4L2_PIX_FMT_DV,      VLC_FOURCC('?','?','?','?') },
442     { V4L2_PIX_FMT_MPEG,    VLC_FOURCC('?','?','?','?') },
443 #endif
444     { 0, 0, 0, 0, 0 }
445 };
446
447 /**
448  * List of V4L2 chromas were confident enough to use as fallbacks if the
449  * user hasn't provided a --v4l2-chroma value.
450  *
451  * Try YUV chromas first, then RGB little endian and MJPEG as last resort.
452  */
453 static const __u32 p_chroma_fallbacks[] =
454 { V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_YVU420, V4L2_PIX_FMT_YUV422P,
455   V4L2_PIX_FMT_YUYV, V4L2_PIX_FMT_UYVY, V4L2_PIX_FMT_BGR24,
456   V4L2_PIX_FMT_BGR32, V4L2_PIX_FMT_MJPEG };
457
458 static const struct
459 {
460     const char *psz_name;
461     unsigned int i_cid;
462 } controls[] =
463 {
464     { "brightness", V4L2_CID_BRIGHTNESS },
465     { "contrast", V4L2_CID_CONTRAST },
466     { "saturation", V4L2_CID_SATURATION },
467     { "hue", V4L2_CID_HUE },
468     { "audio-volume", V4L2_CID_AUDIO_VOLUME },
469     { "audio-balance", V4L2_CID_AUDIO_BALANCE },
470     { "audio-bass", V4L2_CID_AUDIO_BASS },
471     { "audio-treble", V4L2_CID_AUDIO_TREBLE },
472     { "audio-mute", V4L2_CID_AUDIO_MUTE },
473     { "audio-loudness", V4L2_CID_AUDIO_LOUDNESS },
474     { "black-level", V4L2_CID_BLACK_LEVEL },
475     { "auto-white-balance", V4L2_CID_AUTO_WHITE_BALANCE },
476     { "do-white-balance", V4L2_CID_DO_WHITE_BALANCE },
477     { "red-balance", V4L2_CID_RED_BALANCE },
478     { "blue-balance", V4L2_CID_BLUE_BALANCE },
479     { "gamma", V4L2_CID_GAMMA },
480     { "exposure", V4L2_CID_EXPOSURE },
481     { "autogain", V4L2_CID_AUTOGAIN },
482     { "gain", V4L2_CID_GAIN },
483     { "hflip", V4L2_CID_HFLIP },
484     { "vflip", V4L2_CID_VFLIP },
485     { "hcenter", V4L2_CID_HCENTER },
486     { "vcenter", V4L2_CID_VCENTER },
487     { NULL, 0 }
488 };
489
490 struct buffer_t
491 {
492     void *  start;
493     size_t  length;
494 };
495
496 struct demux_sys_t
497 {
498     char *psz_device;  /* Main device from MRL */
499     int  i_fd;
500
501     char *psz_requested_chroma;
502
503     /* Video */
504     io_method io;
505
506     int i_cache;
507
508     struct v4l2_capability dev_cap;
509
510     int i_input;
511     struct v4l2_input *p_inputs;
512     int i_selected_input;
513
514     int i_standard;
515     struct v4l2_standard *p_standards;
516     v4l2_std_id i_selected_standard_id;
517
518     int i_audio;
519     /* V4L2 devices cannot have more than 32 audio inputs */
520     struct v4l2_audio p_audios[32];
521     int i_selected_audio_input;
522
523     int i_tuner;
524     struct v4l2_tuner *p_tuners;
525
526     int i_codec;
527     struct v4l2_fmtdesc *p_codecs;
528
529     struct buffer_t *p_buffers;
530     unsigned int i_nbuffers;
531
532     int i_width;
533     int i_height;
534     float f_fps;            /* <= 0.0 mean to grab at full rate */
535     mtime_t i_video_pts;    /* only used when f_fps > 0 */
536     int i_fourcc;
537
538     es_out_id_t *p_es;
539
540     /* Tuner */
541     int i_cur_tuner;
542     int i_frequency;
543     int i_audio_mode;
544
545     /* Controls */
546     char *psz_set_ctrls;
547 };
548
549 static int FindMainDevice( vlc_object_t *p_this, demux_sys_t *p_sys,
550                            bool b_demux )
551 {
552     /* TODO: if using default device, loop through all /dev/video* until
553      * one works */
554     msg_Dbg( p_this, "opening device '%s'", p_sys->psz_device );
555     if( ProbeVideoDev( p_this, p_sys, p_sys->psz_device ) )
556     {
557         msg_Dbg( p_this, "'%s' is a video device", p_sys->psz_device );
558         p_sys->i_fd = OpenVideoDev( p_this, p_sys, b_demux );
559     }
560
561     if( p_sys->i_fd < 0 ) return VLC_EGENERIC;
562     return VLC_SUCCESS;
563 }
564
565 /*****************************************************************************
566  * DemuxOpen: opens v4l2 device, access_demux callback
567  *****************************************************************************
568  *
569  * url: <video device>::::
570  *
571  *****************************************************************************/
572 static int DemuxOpen( vlc_object_t *p_this )
573 {
574     demux_t     *p_demux = (demux_t*)p_this;
575     demux_sys_t *p_sys;
576
577     /* Only when selected */
578     if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;
579
580     /* Set up p_demux */
581     p_demux->pf_control = DemuxControl;
582     p_demux->pf_demux = Demux;
583     p_demux->info.i_update = 0;
584     p_demux->info.i_title = 0;
585     p_demux->info.i_seekpoint = 0;
586
587     p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
588     if( p_sys == NULL ) return VLC_ENOMEM;
589
590     GetV4L2Params(p_sys, (vlc_object_t *) p_demux);
591
592     ParseMRL( p_sys, p_demux->psz_path, (vlc_object_t *) p_demux );
593
594     if( FindMainDevice( p_this, p_sys, true ) != VLC_SUCCESS)
595     {
596         DemuxClose( p_this );
597         return VLC_EGENERIC;
598     }
599
600     return VLC_SUCCESS;
601 }
602
603 /*****************************************************************************
604  * GetV4L2Params: fill in p_sys parameters (shared by DemuxOpen and AccessOpen)
605  *****************************************************************************/
606 static void GetV4L2Params( demux_sys_t *p_sys, vlc_object_t *p_obj )
607 {
608     p_sys->i_video_pts = -1;
609
610     p_sys->i_selected_standard_id =
611         i_standards_list[var_CreateGetInteger( p_obj, "v4l2-standard" )];
612
613     p_sys->i_selected_input = var_CreateGetInteger( p_obj, "v4l2-input" );
614     p_sys->i_selected_audio_input =
615         var_CreateGetInteger( p_obj, "v4l2-audio-input" );
616
617     p_sys->io = var_CreateGetInteger( p_obj, "v4l2-io" );
618
619     p_sys->i_width = var_CreateGetInteger( p_obj, "v4l2-width" );
620     p_sys->i_height = var_CreateGetInteger( p_obj, "v4l2-height" );
621
622     var_Create( p_obj, "v4l2-controls-reset", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
623
624     p_sys->f_fps = var_CreateGetFloat( p_obj, "v4l2-fps" );
625     p_sys->psz_requested_chroma = var_CreateGetString( p_obj, "v4l2-chroma" );
626
627     p_sys->i_cache = var_CreateGetInteger( p_obj, "v4l2-caching" );
628
629     p_sys->i_cur_tuner = var_CreateGetInteger( p_obj, "v4l2-tuner" );
630     p_sys->i_frequency = var_CreateGetInteger( p_obj, "v4l2-tuner-frequency" );
631     p_sys->i_audio_mode = var_CreateGetInteger( p_obj, "v4l2-tuner-audio-mode" );
632
633     p_sys->psz_set_ctrls = var_CreateGetString( p_obj, "v4l2-set-ctrls" );
634
635     p_sys->psz_device = NULL;
636     p_sys->i_fd = -1;
637
638     p_sys->p_es = NULL;
639 }
640
641 /*****************************************************************************
642  * ParseMRL: parse the options contained in the MRL
643  *****************************************************************************/
644 static void ParseMRL( demux_sys_t *p_sys, char *psz_path, vlc_object_t *p_obj )
645 {
646     char *psz_dup = strdup( psz_path );
647     char *psz_parser = psz_dup;
648
649     while( *psz_parser && *psz_parser != ':' )
650     {
651         psz_parser++;
652     }
653
654     if( *psz_parser == ':' )
655     {
656         /* read options */
657         for( ;; )
658         {
659             *psz_parser++ = '\0';
660
661             if( !strncmp( psz_parser, "standard=", strlen( "standard=" ) ) )
662             {
663                 psz_parser += strlen( "standard=" );
664                 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
665                 {
666                     p_sys->i_selected_standard_id = V4L2_STD_PAL;
667                     psz_parser += strlen( "pal" );
668                 }
669                 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
670                 {
671                     p_sys->i_selected_standard_id = V4L2_STD_NTSC;
672                     psz_parser += strlen( "ntsc" );
673                 }
674                 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
675                 {
676                     p_sys->i_selected_standard_id = V4L2_STD_SECAM;
677                     psz_parser += strlen( "secam" );
678                 }
679                 else if( !strncmp( psz_parser, "default", strlen( "default" ) ) )
680                 {
681                     p_sys->i_selected_standard_id = V4L2_STD_UNKNOWN;
682                     psz_parser += strlen( "default" );
683                 }
684                 else
685                 {
686                     p_sys->i_selected_standard_id = i_standards_list[strtol( psz_parser, &psz_parser, 0 )];
687                 }
688             }
689             else if( !strncmp( psz_parser, "chroma=", strlen( "chroma=" ) ) )
690             {
691                 int  i_len;
692
693                 psz_parser += strlen( "chroma=" );
694                 if( strchr( psz_parser, ':' ) )
695                 {
696                     i_len = strchr( psz_parser, ':' ) - psz_parser;
697                 }
698                 else
699                 {
700                     i_len = strlen( psz_parser );
701                 }
702
703                 free( p_sys->psz_requested_chroma );
704                 p_sys->psz_requested_chroma = strndup( psz_parser, i_len );
705
706                 psz_parser += i_len;
707             }
708             else if( !strncmp( psz_parser, "input=", strlen( "input=" ) ) )
709             {
710                 p_sys->i_selected_input = strtol( psz_parser + strlen( "input=" ),
711                                        &psz_parser, 0 );
712             }
713             else if( !strncmp( psz_parser, "audio-input=", strlen( "audio-input=" ) ) )
714             {
715                 p_sys->i_selected_audio_input = strtol( psz_parser + strlen( "audio-input=" ),
716                                        &psz_parser, 0 );
717             }
718             else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
719             {
720                 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
721                                        &psz_parser );
722             }
723             else if( !strncmp( psz_parser, "io=", strlen( "io=" ) ) )
724             {
725                 psz_parser += strlen( "io=" );
726                 if( !strncmp( psz_parser, "read", strlen( "read" ) ) )
727                 {
728                     p_sys->io = IO_METHOD_READ;
729                     psz_parser += strlen( "read" );
730                 }
731                 else if( !strncmp( psz_parser, "mmap", strlen( "mmap" ) ) )
732                 {
733                     p_sys->io = IO_METHOD_MMAP;
734                     psz_parser += strlen( "mmap" );
735                 }
736                 else if( !strncmp( psz_parser, "userptr", strlen( "userptr" ) ) )
737                 {
738                     p_sys->io = IO_METHOD_USERPTR;
739                     psz_parser += strlen( "userptr" );
740                 }
741                 else
742                 {
743                     p_sys->io = strtol( psz_parser, &psz_parser, 0 );
744                 }
745             }
746             else if( !strncmp( psz_parser, "width=",
747                                strlen( "width=" ) ) )
748             {
749                 p_sys->i_width =
750                     strtol( psz_parser + strlen( "width=" ),
751                             &psz_parser, 0 );
752             }
753             else if( !strncmp( psz_parser, "height=",
754                                strlen( "height=" ) ) )
755             {
756                 p_sys->i_height =
757                     strtol( psz_parser + strlen( "height=" ),
758                             &psz_parser, 0 );
759             }
760             else if( !strncmp( psz_parser, "controls-reset",
761                                strlen( "controls-reset" ) ) )
762             {
763                 var_SetBool( p_obj, "v4l2-controls-reset", true );
764                 psz_parser += strlen( "controls-reset" );
765             }
766 #if 0
767             else if( !strncmp( psz_parser, "brightness=",
768                                strlen( "brightness=" ) ) )
769             {
770                 var_SetInteger( p_obj, "brightness",
771                     strtol( psz_parser + strlen( "brightness=" ),
772                             &psz_parser, 0 ) );
773             }
774             else if( !strncmp( psz_parser, "contrast=",
775                                strlen( "contrast=" ) ) )
776             {
777                 var_SetInteger( p_obj, "contrast",
778                     strtol( psz_parser + strlen( "contrast=" ),
779                             &psz_parser, 0 ) );
780             }
781             else if( !strncmp( psz_parser, "saturation=",
782                                strlen( "saturation=" ) ) )
783             {
784                 var_SetInteger( p_obj, "saturation",
785                     strtol( psz_parser + strlen( "saturation=" ),
786                             &psz_parser, 0 ) );
787             }
788             else if( !strncmp( psz_parser, "hue=",
789                                strlen( "hue=" ) ) )
790             {
791                 var_SetInteger( p_obj, "hue",
792                     strtol( psz_parser + strlen( "hue=" ),
793                             &psz_parser, 0 ) );
794             }
795             else if( !strncmp( psz_parser, "gamma=",
796                                strlen( "gamma=" ) ) )
797             {
798                 var_SetInteger( p_obj, "gamma",
799                     strtol( psz_parser + strlen( "gamma=" ),
800                             &psz_parser, 0 ) );
801             }
802 #endif
803             else if( !strncmp( psz_parser, "caching=", strlen( "caching=" ) ) )
804             {
805                 p_sys->i_cache = strtol( psz_parser + strlen( "caching=" ),
806                                          &psz_parser, 0 );
807             }
808             else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
809             {
810                 p_sys->i_cur_tuner = strtol( psz_parser + strlen( "tuner=" ),
811                                          &psz_parser, 0 );
812             }
813             else if( !strncmp( psz_parser, "tuner-frequency=", strlen( "tuner-frequency=" ) ) )
814             {
815                 p_sys->i_frequency = strtol( psz_parser
816                                           + strlen( "tuner-frequency=" ),
817                                           &psz_parser, 0 );
818             }
819             else if( !strncmp( psz_parser, "tuner-audio-mode=", strlen( "tuner-audio-mode=" ) ) )
820             {
821                 p_sys->i_audio_mode = strtol( psz_parser
822                                           + strlen( "tuner-audio-mode=" ),
823                                           &psz_parser, 0 );
824             }
825             else if( !strncmp( psz_parser, "set-ctrls=", strlen( "set-ctrls=" )) )
826             {
827                 int  i_len;
828
829                 psz_parser += strlen( "set-ctrls=" );
830                 if( strchr( psz_parser, ':' ) )
831                 {
832                     i_len = strchr( psz_parser, ':' ) - psz_parser;
833                 }
834                 else
835                 {
836                     i_len = strlen( psz_parser );
837                 }
838
839                 p_sys->psz_set_ctrls = strndup( psz_parser, i_len );
840
841                 psz_parser += i_len;
842             }
843             else if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) )
844              || !strncmp( psz_parser, "samplerate=", strlen( "samplerate=" ) )
845              || !strncmp( psz_parser, "audio-method", strlen( "audio-method" ) )
846              || !strncmp( psz_parser, "stereo", strlen( "stereo" ) )
847              || !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
848             {
849                 if( strchr( psz_parser, ':' ) )
850                 {
851                     psz_parser = strchr( psz_parser, ':' );
852                 }
853                 else
854                 {
855                     psz_parser += strlen( psz_parser );
856                 }
857
858                 msg_Err( p_obj, AUDIO_DEPRECATED_ERROR );
859             }
860             else
861             {
862                 char *psz_unk = strchr( psz_parser, ':' );
863                 if (psz_unk)
864                     psz_unk = strndup( psz_parser, psz_unk - psz_parser );
865                 else
866                     psz_unk = strdup( psz_parser);
867                 msg_Warn( p_obj, "unknown option %s", psz_unk );
868                 free (psz_unk);
869             }
870
871             while( *psz_parser && *psz_parser != ':' )
872             {
873                 psz_parser++;
874             }
875
876             if( *psz_parser == '\0' )
877             {
878                 break;
879             }
880         }
881     }
882
883     /* Main device */
884     if( *psz_dup )
885         p_sys->psz_device = strdup( psz_dup );
886     else
887         p_sys->psz_device = strdup( V4L2_DEFAULT );
888     free( psz_dup );
889 }
890
891 /*****************************************************************************
892  * Close: close device, free resources
893  *****************************************************************************/
894 static void AccessClose( vlc_object_t *p_this )
895 {
896     access_t    *p_access = (access_t *)p_this;
897     demux_sys_t *p_sys   = (demux_sys_t *) p_access->p_sys;
898
899     CommonClose( p_this, p_sys );
900 }
901
902 static void DemuxClose( vlc_object_t *p_this )
903 {
904     struct v4l2_buffer buf;
905     enum v4l2_buf_type buf_type;
906     unsigned int i;
907
908     demux_t     *p_demux = (demux_t *)p_this;
909     demux_sys_t *p_sys   = p_demux->p_sys;
910
911     /* Stop video capture */
912     if( p_sys->i_fd >= 0 )
913     {
914         switch( p_sys->io )
915         {
916         case IO_METHOD_READ:
917             /* Nothing to do */
918             break;
919
920         case IO_METHOD_MMAP:
921         case IO_METHOD_USERPTR:
922             /* Some drivers 'hang' internally if this is not done before streamoff */
923             for( unsigned int i = 0; i < p_sys->i_nbuffers; i++ )
924             {
925                 memset( &buf, 0, sizeof(buf) );
926                 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
927                 buf.memory = ( p_sys->io == IO_METHOD_USERPTR ) ?
928                     V4L2_MEMORY_USERPTR : V4L2_MEMORY_MMAP;
929                 v4l2_ioctl( p_sys->i_fd, VIDIOC_DQBUF, &buf ); /* ignore result */
930             }
931
932             buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
933             if( v4l2_ioctl( p_sys->i_fd, VIDIOC_STREAMOFF, &buf_type ) < 0 ) {
934                 msg_Err( p_this, "VIDIOC_STREAMOFF failed" );
935             }
936
937             break;
938         }
939     }
940
941     /* Free Video Buffers */
942     if( p_sys->p_buffers ) {
943         switch( p_sys->io )
944         {
945         case IO_METHOD_READ:
946             free( p_sys->p_buffers[0].start );
947             break;
948
949         case IO_METHOD_MMAP:
950             for( i = 0; i < p_sys->i_nbuffers; ++i )
951             {
952                 if( v4l2_munmap( p_sys->p_buffers[i].start, p_sys->p_buffers[i].length ) )
953                 {
954                     msg_Err( p_this, "munmap failed" );
955                 }
956             }
957             break;
958
959         case IO_METHOD_USERPTR:
960             for( i = 0; i < p_sys->i_nbuffers; ++i )
961             {
962                free( p_sys->p_buffers[i].start );
963             }
964             break;
965         }
966         free( p_sys->p_buffers );
967     }
968
969     CommonClose( p_this, p_sys );
970 }
971
972 static void CommonClose( vlc_object_t *p_this, demux_sys_t *p_sys )
973 {
974     (void)p_this;
975     /* Close */
976     if( p_sys->i_fd >= 0 ) v4l2_close( p_sys->i_fd );
977     free( p_sys->psz_device );
978     free( p_sys->p_standards );
979     free( p_sys->p_inputs );
980     free( p_sys->p_tuners );
981     free( p_sys->p_codecs );
982     free( p_sys->psz_requested_chroma );
983     free( p_sys->psz_set_ctrls );
984
985     free( p_sys );
986 }
987
988 /*****************************************************************************
989  * AccessOpen: opens v4l2 device, access callback
990  *****************************************************************************
991  *
992  * url: <video device>::::
993  *
994  *****************************************************************************/
995 static int AccessOpen( vlc_object_t * p_this )
996 {
997     access_t *p_access = (access_t*) p_this;
998     demux_sys_t * p_sys;
999
1000     /* Only when selected */
1001     if( *p_access->psz_access == '\0' ) return VLC_EGENERIC;
1002
1003     p_access->pf_read = AccessRead;
1004     p_access->pf_block = NULL;
1005     p_access->pf_seek = NULL;
1006     p_access->pf_control = AccessControl;
1007     p_access->info.i_update = 0;
1008     p_access->info.i_size = 0;
1009     p_access->info.i_pos = 0;
1010     p_access->info.b_eof = false;
1011     p_access->info.i_title = 0;
1012     p_access->info.i_seekpoint = 0;
1013
1014     p_sys = calloc( 1, sizeof( demux_sys_t ) );
1015     p_access->p_sys = (access_sys_t *) p_sys;
1016     if( p_sys == NULL ) return VLC_ENOMEM;
1017
1018     GetV4L2Params( p_sys, (vlc_object_t *) p_access );
1019
1020     ParseMRL( p_sys, p_access->psz_path, (vlc_object_t *) p_access );
1021
1022     if( FindMainDevice( p_this, p_sys, false ) != VLC_SUCCESS )
1023     {
1024         AccessClose( p_this );
1025         return VLC_EGENERIC;
1026     }
1027
1028     return VLC_SUCCESS;
1029 }
1030
1031 /*****************************************************************************
1032  * DemuxControl:
1033  *****************************************************************************/
1034 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
1035 {
1036     demux_sys_t *p_sys = p_demux->p_sys;
1037     bool *pb;
1038     int64_t    *pi64;
1039
1040     switch( i_query )
1041     {
1042         /* Special for access_demux */
1043         case DEMUX_CAN_PAUSE:
1044         case DEMUX_CAN_SEEK:
1045         case DEMUX_SET_PAUSE_STATE:
1046         case DEMUX_CAN_CONTROL_PACE:
1047             pb = (bool*)va_arg( args, bool * );
1048             *pb = false;
1049             return VLC_SUCCESS;
1050
1051         case DEMUX_GET_PTS_DELAY:
1052             pi64 = (int64_t*)va_arg( args, int64_t * );
1053             *pi64 = (int64_t)p_sys->i_cache * 1000;
1054             return VLC_SUCCESS;
1055
1056         case DEMUX_GET_TIME:
1057             pi64 = (int64_t*)va_arg( args, int64_t * );
1058             *pi64 = mdate();
1059             return VLC_SUCCESS;
1060
1061         /* TODO implement others */
1062         default:
1063             return VLC_EGENERIC;
1064     }
1065
1066     return VLC_EGENERIC;
1067 }
1068
1069 /*****************************************************************************
1070  * AccessControl: access callback
1071  *****************************************************************************/
1072 static int AccessControl( access_t *p_access, int i_query, va_list args )
1073 {
1074     bool   *pb_bool;
1075     int          *pi_int;
1076     int64_t      *pi_64;
1077     demux_sys_t  *p_sys = (demux_sys_t *) p_access->p_sys;
1078
1079     switch( i_query )
1080     {
1081         /* */
1082         case ACCESS_CAN_SEEK:
1083         case ACCESS_CAN_FASTSEEK:
1084             pb_bool = (bool*)va_arg( args, bool* );
1085             *pb_bool = false;
1086             break;
1087         case ACCESS_CAN_PAUSE:
1088             pb_bool = (bool*)va_arg( args, bool* );
1089             *pb_bool = false;
1090             break;
1091         case ACCESS_CAN_CONTROL_PACE:
1092             pb_bool = (bool*)va_arg( args, bool* );
1093             *pb_bool = false;
1094             break;
1095
1096         /* */
1097         case ACCESS_GET_MTU:
1098             pi_int = (int*)va_arg( args, int * );
1099             *pi_int = 0;
1100             break;
1101
1102         case ACCESS_GET_PTS_DELAY:
1103             pi_64 = (int64_t*)va_arg( args, int64_t * );
1104             *pi_64 = (int64_t) p_sys->i_cache * 1000;
1105             break;
1106
1107         /* */
1108         case ACCESS_SET_PAUSE_STATE:
1109             /* Nothing to do */
1110             break;
1111
1112         case ACCESS_GET_TITLE_INFO:
1113         case ACCESS_SET_TITLE:
1114         case ACCESS_SET_SEEKPOINT:
1115         case ACCESS_SET_PRIVATE_ID_STATE:
1116         case ACCESS_GET_CONTENT_TYPE:
1117         case ACCESS_GET_META:
1118             return VLC_EGENERIC;
1119
1120         default:
1121             msg_Warn( p_access, "Unimplemented query in control(%d).", i_query);
1122             return VLC_EGENERIC;
1123
1124     }
1125     return VLC_SUCCESS;
1126 }
1127
1128 /*****************************************************************************
1129  * AccessRead: access callback
1130  ******************************************************************************/
1131 static ssize_t AccessRead( access_t * p_access, uint8_t * p_buffer, size_t i_len )
1132 {
1133     demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1134     struct pollfd ufd;
1135     int i_ret;
1136
1137     ufd.fd = p_sys->i_fd;
1138     ufd.events = POLLIN;
1139
1140     if( p_access->info.b_eof )
1141         return 0;
1142
1143     do
1144     {
1145         if( !vlc_object_alive (p_access) )
1146             return 0;
1147
1148         ufd.revents = 0;
1149     }
1150     while( ( i_ret = poll( &ufd, 1, 500 ) ) == 0 );
1151
1152     if( i_ret < 0 )
1153     {
1154         msg_Err( p_access, "Polling error (%m)." );
1155         return -1;
1156     }
1157
1158     i_ret = v4l2_read( p_sys->i_fd, p_buffer, i_len );
1159     if( i_ret == 0 )
1160     {
1161         p_access->info.b_eof = true;
1162     }
1163     else if( i_ret > 0 )
1164     {
1165         p_access->info.i_pos += i_ret;
1166     }
1167
1168     return i_ret;
1169 }
1170
1171 /*****************************************************************************
1172  * Demux: Processes the audio or video frame
1173  *****************************************************************************/
1174 static int Demux( demux_t *p_demux )
1175 {
1176     demux_sys_t *p_sys = p_demux->p_sys;
1177
1178     struct pollfd fd;
1179     fd.fd = p_sys->i_fd;
1180     fd.events = POLLIN|POLLPRI;
1181     fd.revents = 0;
1182
1183     /* Wait for data */
1184     if( poll( &fd, 1, 500 ) ) /* Timeout after 0.5 seconds since I don't know if pf_demux can be blocking. */
1185     {
1186         if( fd.revents & (POLLIN|POLLPRI) )
1187         {
1188             block_t *p_block = GrabVideo( p_demux );
1189             if( p_block )
1190             {
1191                 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
1192                 es_out_Send( p_demux->out, p_sys->p_es, p_block );
1193             }
1194         }
1195     }
1196
1197     return 1;
1198 }
1199
1200 /*****************************************************************************
1201  * GrabVideo: Grab a video frame
1202  *****************************************************************************/
1203 static block_t* GrabVideo( demux_t *p_demux )
1204 {
1205     demux_sys_t *p_sys = p_demux->p_sys;
1206
1207     block_t *p_block = NULL;
1208     struct v4l2_buffer buf;
1209     ssize_t i_ret;
1210
1211     if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
1212     {
1213         mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
1214
1215         /* Did we wait long enough ? (frame rate reduction) */
1216         if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
1217     }
1218
1219     /* Grab Video Frame */
1220     switch( p_sys->io )
1221     {
1222     case IO_METHOD_READ:
1223         i_ret = v4l2_read( p_sys->i_fd, p_sys->p_buffers[0].start, p_sys->p_buffers[0].length );
1224         if( i_ret == -1 )
1225         {
1226             switch( errno )
1227             {
1228             case EAGAIN:
1229                 return 0;
1230             case EIO:
1231                 /* Could ignore EIO, see spec. */
1232                 /* fall through */
1233             default:
1234                 msg_Err( p_demux, "Failed to read frame" );
1235                 return 0;
1236                }
1237         }
1238
1239         p_block = ProcessVideoFrame( p_demux, (uint8_t*)p_sys->p_buffers[0].start, i_ret );
1240         if( !p_block ) return 0;
1241
1242         break;
1243
1244     case IO_METHOD_MMAP:
1245         memset( &buf, 0, sizeof(buf) );
1246         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1247         buf.memory = V4L2_MEMORY_MMAP;
1248
1249         /* Wait for next frame */
1250         if (v4l2_ioctl( p_sys->i_fd, VIDIOC_DQBUF, &buf ) < 0 )
1251         {
1252             switch( errno )
1253             {
1254             case EAGAIN:
1255                 return 0;
1256             case EIO:
1257                 /* Could ignore EIO, see spec. */
1258                 /* fall through */
1259             default:
1260                 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
1261                 return 0;
1262                }
1263         }
1264
1265         if( buf.index >= p_sys->i_nbuffers ) {
1266             msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
1267             return 0;
1268         }
1269
1270         p_block = ProcessVideoFrame( p_demux, p_sys->p_buffers[buf.index].start, buf.bytesused );
1271         if( !p_block ) return 0;
1272
1273         /* Unlock */
1274         if( v4l2_ioctl( p_sys->i_fd, VIDIOC_QBUF, &buf ) < 0 )
1275         {
1276             msg_Err( p_demux, "Failed to unlock (VIDIOC_QBUF)" );
1277             block_Release( p_block );
1278             return 0;
1279         }
1280
1281         break;
1282
1283     case IO_METHOD_USERPTR:
1284         memset( &buf, 0, sizeof(buf) );
1285         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1286         buf.memory = V4L2_MEMORY_USERPTR;
1287
1288         /* Wait for next frame */
1289         if (v4l2_ioctl( p_sys->i_fd, VIDIOC_DQBUF, &buf ) < 0 )
1290         {
1291             switch( errno )
1292             {
1293             case EAGAIN:
1294                 return 0;
1295             case EIO:
1296                 /* Could ignore EIO, see spec. */
1297                 /* fall through */
1298             default:
1299                 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
1300                 return 0;
1301             }
1302         }
1303
1304         /* Find frame? */
1305         unsigned int i;
1306         for( i = 0; i < p_sys->i_nbuffers; i++ )
1307         {
1308             if( buf.m.userptr == (unsigned long)p_sys->p_buffers[i].start &&
1309                 buf.length == p_sys->p_buffers[i].length ) break;
1310         }
1311
1312         if( i >= p_sys->i_nbuffers )
1313         {
1314             msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
1315             return 0;
1316         }
1317
1318         p_block = ProcessVideoFrame( p_demux, (uint8_t*)buf.m.userptr, buf.bytesused );
1319         if( !p_block ) return 0;
1320
1321         /* Unlock */
1322         if( v4l2_ioctl( p_sys->i_fd, VIDIOC_QBUF, &buf ) < 0 )
1323         {
1324             msg_Err( p_demux, "Failed to unlock (VIDIOC_QBUF)" );
1325             block_Release( p_block );
1326             return 0;
1327         }
1328
1329         break;
1330
1331     }
1332
1333     /* Timestamp */
1334     p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();
1335
1336     return p_block;
1337 }
1338
1339 /*****************************************************************************
1340  * ProcessVideoFrame: Helper function to take a buffer and copy it into
1341  * a new block
1342  *****************************************************************************/
1343 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t i_size )
1344 {
1345     block_t *p_block;
1346
1347     if( !p_frame ) return 0;
1348
1349     /* New block */
1350     if( !( p_block = block_New( p_demux, i_size ) ) )
1351     {
1352         msg_Warn( p_demux, "Cannot get new block" );
1353         return 0;
1354     }
1355
1356     /* Copy frame */
1357     memcpy( p_block->p_buffer, p_frame, i_size );
1358
1359     return p_block;
1360 }
1361
1362 /*****************************************************************************
1363  * Helper function to initalise video IO using the Read method
1364  *****************************************************************************/
1365 static int InitRead( demux_t *p_demux, unsigned int i_buffer_size )
1366 {
1367     demux_sys_t *p_sys = p_demux->p_sys;
1368
1369     p_sys->p_buffers = calloc( 1, sizeof( *p_sys->p_buffers ) );
1370     if( !p_sys->p_buffers )
1371         return VLC_EGENERIC;
1372
1373     p_sys->p_buffers[0].length = i_buffer_size;
1374     p_sys->p_buffers[0].start = malloc( i_buffer_size );
1375     if( !p_sys->p_buffers[0].start )
1376         return VLC_ENOMEM;
1377
1378     return VLC_SUCCESS;
1379 }
1380
1381 /*****************************************************************************
1382  * Helper function to initalise video IO using the mmap method
1383  *****************************************************************************/
1384 static int InitMmap( demux_t *p_demux, int i_fd )
1385 {
1386     demux_sys_t *p_sys = p_demux->p_sys;
1387     struct v4l2_requestbuffers req;
1388
1389     memset( &req, 0, sizeof(req) );
1390     req.count = 4;
1391     req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1392     req.memory = V4L2_MEMORY_MMAP;
1393
1394     if( v4l2_ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1395     {
1396         msg_Err( p_demux, "device does not support mmap i/o" );
1397         goto open_failed;
1398     }
1399
1400     if( req.count < 2 )
1401     {
1402         msg_Err( p_demux, "Insufficient buffer memory" );
1403         goto open_failed;
1404     }
1405
1406     p_sys->p_buffers = calloc( req.count, sizeof( *p_sys->p_buffers ) );
1407     if( !p_sys->p_buffers )
1408         goto open_failed;
1409
1410     for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < req.count; ++p_sys->i_nbuffers )
1411     {
1412         struct v4l2_buffer buf;
1413
1414         memset( &buf, 0, sizeof(buf) );
1415         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1416         buf.memory = V4L2_MEMORY_MMAP;
1417         buf.index = p_sys->i_nbuffers;
1418
1419         if( v4l2_ioctl( i_fd, VIDIOC_QUERYBUF, &buf ) < 0 )
1420         {
1421             msg_Err( p_demux, "VIDIOC_QUERYBUF" );
1422             goto open_failed;
1423         }
1424
1425         p_sys->p_buffers[p_sys->i_nbuffers].length = buf.length;
1426         p_sys->p_buffers[p_sys->i_nbuffers].start =
1427             v4l2_mmap( NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, buf.m.offset );
1428
1429         if( p_sys->p_buffers[p_sys->i_nbuffers].start == MAP_FAILED )
1430         {
1431             msg_Err( p_demux, "mmap failed (%m)" );
1432             goto open_failed;
1433         }
1434     }
1435
1436     return VLC_SUCCESS;
1437
1438 open_failed:
1439     return VLC_EGENERIC;
1440
1441 }
1442
1443 /*****************************************************************************
1444  * Helper function to initalise video IO using the userbuf method
1445  *****************************************************************************/
1446 static int InitUserP( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
1447 {
1448     demux_sys_t *p_sys = p_demux->p_sys;
1449     struct v4l2_requestbuffers req;
1450     unsigned int i_page_size;
1451
1452     i_page_size = sysconf(_SC_PAGESIZE);
1453     i_buffer_size = ( i_buffer_size + i_page_size - 1 ) & ~( i_page_size - 1);
1454
1455     memset( &req, 0, sizeof(req) );
1456     req.count = 4;
1457     req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1458     req.memory = V4L2_MEMORY_USERPTR;
1459
1460     if( v4l2_ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1461     {
1462         msg_Err( p_demux, "device does not support user pointer i/o" );
1463         return VLC_EGENERIC;
1464     }
1465
1466     p_sys->p_buffers = calloc( 4, sizeof( *p_sys->p_buffers ) );
1467     if( !p_sys->p_buffers )
1468         goto open_failed;
1469
1470     for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < 4; ++p_sys->i_nbuffers )
1471     {
1472         p_sys->p_buffers[p_sys->i_nbuffers].length = i_buffer_size;
1473         if( posix_memalign( &p_sys->p_buffers[p_sys->i_nbuffers].start,
1474                 /* boundary */ i_page_size, i_buffer_size ) )
1475             goto open_failed;
1476     }
1477
1478     return VLC_SUCCESS;
1479
1480 open_failed:
1481     free( p_sys->p_buffers );
1482     return VLC_EGENERIC;
1483
1484 }
1485
1486 /*****************************************************************************
1487  * IsPixelFormatSupported: returns true if the specified V4L2 pixel format is
1488  * in the array of supported formats returned by the driver
1489  *****************************************************************************/
1490 static bool IsPixelFormatSupported( demux_t *p_demux, unsigned int i_pixelformat )
1491 {
1492     demux_sys_t *p_sys = p_demux->p_sys;
1493
1494     for( int i_index = 0; i_index < p_sys->i_codec; i_index++ )
1495     {
1496         if( p_sys->p_codecs[i_index].pixelformat == i_pixelformat )
1497             return true;
1498     }
1499
1500     return false;
1501 }
1502
1503 /*****************************************************************************
1504  * OpenVideoDev: open and set up the video device and probe for capabilities
1505  *****************************************************************************/
1506 static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
1507 {
1508     int i_fd;
1509     struct v4l2_cropcap cropcap;
1510     struct v4l2_crop crop;
1511     struct v4l2_format fmt;
1512     unsigned int i_min;
1513     enum v4l2_buf_type buf_type;
1514     const char *psz_device = p_sys->psz_device;
1515     es_format_t es_fmt;
1516     int libv4l2_fd;
1517
1518     if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
1519     {
1520         msg_Err( p_obj, "cannot open device (%m)" );
1521         goto open_failed;
1522     }
1523
1524 #ifdef HAVE_LIBV4L2
1525     /* Note the v4l2_xxx functions are designed so that if they get passed an
1526        unknown fd, the will behave exactly as their regular xxx counterparts,
1527        so if v4l2_fd_open fails, we continue as normal (missing the libv4l2
1528        custom cam format to normal formats conversion). Chances are big we will
1529        still fail then though, as normally v4l2_fd_open only fails if the
1530        device is not a v4l2 device. */
1531     libv4l2_fd = v4l2_fd_open( i_fd, V4L2_ENABLE_ENUM_FMT_EMULATION );
1532     if( libv4l2_fd != -1 )
1533         i_fd = libv4l2_fd;
1534 #else
1535     libv4l2_fd = i_fd;
1536 #endif
1537
1538     /* Tune the tuner */
1539     if( p_sys->i_frequency >= 0 )
1540     {
1541         if( p_sys->i_cur_tuner < 0 || p_sys->i_cur_tuner >= p_sys->i_tuner )
1542         {
1543             msg_Err( p_obj, "invalid tuner %d.", p_sys->i_cur_tuner );
1544             goto open_failed;
1545         }
1546         struct v4l2_frequency frequency;
1547         memset( &frequency, 0, sizeof( frequency ) );
1548         frequency.tuner = p_sys->i_cur_tuner;
1549         frequency.type = p_sys->p_tuners[p_sys->i_cur_tuner].type;
1550         frequency.frequency = p_sys->i_frequency / 62.5;
1551         if( v4l2_ioctl( i_fd, VIDIOC_S_FREQUENCY, &frequency ) < 0 )
1552         {
1553             msg_Err( p_obj, "cannot set tuner frequency (%m)" );
1554             goto open_failed;
1555         }
1556         msg_Dbg( p_obj, "Tuner frequency set" );
1557     }
1558
1559     /* Set the tuner's audio mode */
1560     if( p_sys->i_audio_mode >= 0 )
1561     {
1562         if( p_sys->i_cur_tuner < 0 || p_sys->i_cur_tuner >= p_sys->i_tuner )
1563         {
1564             msg_Err( p_obj, "invalid tuner %d.", p_sys->i_cur_tuner );
1565             goto open_failed;
1566         }
1567         struct v4l2_tuner tuner;
1568         memset( &tuner, 0, sizeof( tuner ) );
1569         tuner.index = p_sys->i_cur_tuner;
1570         tuner.audmode = p_sys->i_audio_mode;
1571         if( v4l2_ioctl( i_fd, VIDIOC_S_TUNER, &tuner ) < 0 )
1572         {
1573             msg_Err( p_obj, "cannot set tuner audio mode (%m)" );
1574             goto open_failed;
1575         }
1576         msg_Dbg( p_obj, "Tuner audio mode set" );
1577     }
1578
1579     /* Select standard */
1580
1581     if( p_sys->i_selected_standard_id != V4L2_STD_UNKNOWN )
1582     {
1583         if( v4l2_ioctl( i_fd, VIDIOC_S_STD, &p_sys->i_selected_standard_id ) < 0 )
1584         {
1585             msg_Err( p_obj, "cannot set standard (%m)" );
1586             goto open_failed;
1587         }
1588         msg_Dbg( p_obj, "Set standard" );
1589     }
1590
1591     /* Select input */
1592
1593     if( p_sys->i_selected_input >= p_sys->i_input )
1594     {
1595         msg_Warn( p_obj, "invalid input. Using the default one" );
1596         p_sys->i_selected_input = 0;
1597     }
1598
1599     if( v4l2_ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_selected_input ) < 0 )
1600     {
1601         msg_Err( p_obj, "cannot set input (%m)" );
1602         goto open_failed;
1603     }
1604
1605     /* Set audio input */
1606
1607     if( p_sys->i_audio > 0 )
1608     {
1609         if( p_sys->i_selected_audio_input < 0
1610          || p_sys->i_selected_audio_input >= p_sys->i_audio )
1611         {
1612             msg_Warn( p_obj, "invalid audio input. Using the default one" );
1613             p_sys->i_selected_audio_input = 0;
1614         }
1615
1616         if( v4l2_ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_selected_audio_input] ) < 0 )
1617         {
1618             msg_Err( p_obj, "cannot set audio input (%m)" );
1619             goto open_failed;
1620         }
1621         msg_Dbg( p_obj, "Audio input set to %d", p_sys->i_selected_audio_input );
1622     }
1623
1624
1625     /* TODO: Move the resolution stuff up here */
1626     /* if MPEG encoder card, no need to do anything else after this */
1627     ControlList( p_obj, i_fd,
1628                   var_GetBool( p_obj, "v4l2-controls-reset" ), b_demux );
1629     SetAvailControlsByString( p_obj, p_sys, i_fd );
1630     if( false == b_demux)
1631     {
1632         return i_fd;
1633     }
1634
1635     demux_t *p_demux = (demux_t *) p_obj;
1636
1637     /* Verify device support for the various IO methods */
1638     switch( p_sys->io )
1639     {
1640         case IO_METHOD_READ:
1641             if( !(p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE) )
1642             {
1643                 msg_Err( p_demux, "device does not support read i/o" );
1644                 goto open_failed;
1645             }
1646             break;
1647
1648         case IO_METHOD_MMAP:
1649         case IO_METHOD_USERPTR:
1650             if( !(p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING) )
1651             {
1652                 msg_Err( p_demux, "device does not support streaming i/o" );
1653                 goto open_failed;
1654             }
1655             break;
1656
1657         default:
1658             msg_Err( p_demux, "io method not supported" );
1659             goto open_failed;
1660     }
1661
1662     /* Reset Cropping */
1663     memset( &cropcap, 0, sizeof(cropcap) );
1664     cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1665     if( v4l2_ioctl( i_fd, VIDIOC_CROPCAP, &cropcap ) >= 0 )
1666     {
1667         crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1668         crop.c = cropcap.defrect; /* reset to default */
1669         if( v4l2_ioctl( i_fd, VIDIOC_S_CROP, &crop ) < 0 )
1670         {
1671             switch( errno )
1672             {
1673                 case EINVAL:
1674                     /* Cropping not supported. */
1675                     break;
1676                 default:
1677                     /* Errors ignored. */
1678                     break;
1679             }
1680         }
1681     }
1682
1683     /* Try and find default resolution if not specified */
1684     memset( &fmt, 0, sizeof(fmt) );
1685     fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1686
1687     if( p_sys->i_width <= 0 || p_sys->i_height <= 0 )
1688     {
1689         if( v4l2_ioctl( i_fd, VIDIOC_G_FMT, &fmt ) < 0 )
1690         {
1691             msg_Err( p_demux, "Cannot get default width and height." );
1692             goto open_failed;
1693         }
1694
1695         p_sys->i_width = fmt.fmt.pix.width;
1696         p_sys->i_height = fmt.fmt.pix.height;
1697
1698         if( fmt.fmt.pix.field == V4L2_FIELD_ALTERNATE )
1699         {
1700             p_sys->i_height = p_sys->i_height * 2;
1701         }
1702     }
1703     else
1704     {
1705         msg_Dbg( p_demux, "trying specified size %dx%d", p_sys->i_width, p_sys->i_height );
1706     }
1707
1708     fmt.fmt.pix.width = p_sys->i_width;
1709     fmt.fmt.pix.height = p_sys->i_height;
1710     fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
1711
1712     /* Test and set Chroma */
1713     fmt.fmt.pix.pixelformat = 0;
1714     if( p_sys->psz_requested_chroma && strlen( p_sys->psz_requested_chroma ) > 0 )
1715     {
1716         /* User specified chroma */
1717         if( strlen( p_sys->psz_requested_chroma ) >= 4 )
1718         {
1719             int i_requested_fourcc = VLC_FOURCC(
1720                 p_sys->psz_requested_chroma[0], p_sys->psz_requested_chroma[1],
1721                 p_sys->psz_requested_chroma[2], p_sys->psz_requested_chroma[3] );
1722             for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
1723             {
1724                 if( v4l2chroma_to_fourcc[i].i_fourcc == i_requested_fourcc )
1725                 {
1726                     fmt.fmt.pix.pixelformat = v4l2chroma_to_fourcc[i].i_v4l2;
1727                     break;
1728                 }
1729             }
1730         }
1731         /* Try and set user chroma */
1732         if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ( fmt.fmt.pix.pixelformat && v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 ) )
1733         {
1734             msg_Warn( p_demux, "Driver is unable to use specified chroma %s. Trying defaults.", p_sys->psz_requested_chroma );
1735             fmt.fmt.pix.pixelformat = 0;
1736         }
1737     }
1738
1739     /* If no user specified chroma, find best */
1740     /* This also decides if MPEG encoder card or not */
1741     if( !fmt.fmt.pix.pixelformat )
1742     {
1743         unsigned int i;
1744         for( i = 0; i < ARRAY_SIZE( p_chroma_fallbacks ); i++ )
1745         {
1746             fmt.fmt.pix.pixelformat = p_chroma_fallbacks[i];
1747             if( IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat )
1748              && v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) >= 0 )
1749                 break;
1750         }
1751         if( i == ARRAY_SIZE( p_chroma_fallbacks ) )
1752         {
1753             msg_Warn( p_demux, "Could not select any of the default chromas; attempting to open as MPEG encoder card (access)" );
1754             goto open_failed;
1755         }
1756     }
1757
1758     /* Reassign width, height and chroma incase driver override */
1759     p_sys->i_width = fmt.fmt.pix.width;
1760     p_sys->i_height = fmt.fmt.pix.height;
1761
1762     /* Look up final fourcc */
1763     p_sys->i_fourcc = 0;
1764     for( int i = 0; v4l2chroma_to_fourcc[i].i_fourcc != 0; i++ )
1765     {
1766         if( v4l2chroma_to_fourcc[i].i_v4l2 == fmt.fmt.pix.pixelformat )
1767         {
1768             p_sys->i_fourcc = v4l2chroma_to_fourcc[i].i_fourcc;
1769             es_format_Init( &es_fmt, VIDEO_ES, p_sys->i_fourcc );
1770             es_fmt.video.i_rmask = v4l2chroma_to_fourcc[i].i_rmask;
1771             es_fmt.video.i_gmask = v4l2chroma_to_fourcc[i].i_gmask;
1772             es_fmt.video.i_bmask = v4l2chroma_to_fourcc[i].i_bmask;
1773             break;
1774         }
1775     }
1776
1777     /* Buggy driver paranoia */
1778     i_min = fmt.fmt.pix.width * 2;
1779     if( fmt.fmt.pix.bytesperline < i_min )
1780         fmt.fmt.pix.bytesperline = i_min;
1781     i_min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
1782     if( fmt.fmt.pix.sizeimage < i_min )
1783         fmt.fmt.pix.sizeimage = i_min;
1784
1785 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1786     /* This is new in Linux 2.6.19 */
1787     /* List supported frame rates */
1788     struct v4l2_frmivalenum frmival;
1789     frmival.index = 0;
1790     frmival.pixel_format = fmt.fmt.pix.pixelformat;
1791     frmival.width = p_sys->i_width;
1792     frmival.height = p_sys->i_height;
1793     if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 )
1794     {
1795         char psz_fourcc[5];
1796         memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
1797         vlc_fourcc_to_char( p_sys->i_fourcc, &psz_fourcc );
1798         msg_Dbg( p_demux, "supported frame intervals for %4s, %dx%d:",
1799                  psz_fourcc, frmival.width, frmival.height );
1800         switch( frmival.type )
1801         {
1802             case V4L2_FRMIVAL_TYPE_DISCRETE:
1803                 do
1804                 {
1805                     msg_Dbg( p_demux, "    supported frame interval: %d/%d",
1806                              frmival.discrete.numerator,
1807                              frmival.discrete.denominator );
1808                     frmival.index++;
1809                 } while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 );
1810                 break;
1811             case V4L2_FRMIVAL_TYPE_STEPWISE:
1812                 msg_Dbg( p_demux, "    supported frame intervals: %d/%d to "
1813                          "%d/%d using %d/%d increments",
1814                          frmival.stepwise.min.numerator,
1815                          frmival.stepwise.min.denominator,
1816                          frmival.stepwise.max.numerator,
1817                          frmival.stepwise.max.denominator,
1818                          frmival.stepwise.step.numerator,
1819                          frmival.stepwise.step.denominator );
1820                 break;
1821             case V4L2_FRMIVAL_TYPE_CONTINUOUS:
1822                 msg_Dbg( p_demux, "    supported frame intervals: %d/%d to %d/%d",
1823                          frmival.stepwise.min.numerator,
1824                          frmival.stepwise.min.denominator,
1825                          frmival.stepwise.max.numerator,
1826                          frmival.stepwise.max.denominator );
1827                 break;
1828         }
1829     }
1830 #endif
1831
1832     /* Init IO method */
1833     switch( p_sys->io )
1834     {
1835     case IO_METHOD_READ:
1836         if( InitRead( p_demux, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
1837         break;
1838
1839     case IO_METHOD_MMAP:
1840         if( InitMmap( p_demux, i_fd ) != VLC_SUCCESS ) goto open_failed;
1841         break;
1842
1843     case IO_METHOD_USERPTR:
1844         if( InitUserP( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
1845         break;
1846
1847     }
1848
1849     /* Add */
1850     es_fmt.video.i_width  = p_sys->i_width;
1851     es_fmt.video.i_height = p_sys->i_height;
1852     es_fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
1853
1854     msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
1855         (char*)&es_fmt.i_codec, es_fmt.video.i_width, es_fmt.video.i_height );
1856     p_sys->p_es = es_out_Add( p_demux->out, &es_fmt );
1857
1858     /* Start Capture */
1859
1860     switch( p_sys->io )
1861     {
1862     case IO_METHOD_READ:
1863         /* Nothing to do */
1864         break;
1865
1866     case IO_METHOD_MMAP:
1867         for (unsigned int i = 0; i < p_sys->i_nbuffers; ++i)
1868         {
1869             struct v4l2_buffer buf;
1870
1871             memset( &buf, 0, sizeof(buf) );
1872             buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1873             buf.memory = V4L2_MEMORY_MMAP;
1874             buf.index = i;
1875
1876             if( v4l2_ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
1877             {
1878                 msg_Err( p_demux, "VIDIOC_QBUF failed" );
1879                 goto open_failed;
1880             }
1881         }
1882
1883         buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1884         if( v4l2_ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
1885         {
1886             msg_Err( p_demux, "VIDIOC_STREAMON failed" );
1887             goto open_failed;
1888         }
1889
1890         break;
1891
1892     case IO_METHOD_USERPTR:
1893         for( unsigned int i = 0; i < p_sys->i_nbuffers; ++i )
1894         {
1895             struct v4l2_buffer buf;
1896
1897             memset( &buf, 0, sizeof(buf) );
1898             buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1899             buf.memory = V4L2_MEMORY_USERPTR;
1900             buf.index = i;
1901             buf.m.userptr = (unsigned long)p_sys->p_buffers[i].start;
1902             buf.length = p_sys->p_buffers[i].length;
1903
1904             if( v4l2_ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
1905             {
1906                 msg_Err( p_demux, "VIDIOC_QBUF failed" );
1907                 goto open_failed;
1908             }
1909         }
1910
1911         buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1912         if( v4l2_ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
1913         {
1914             msg_Err( p_demux, "VIDIOC_STREAMON failed" );
1915             goto open_failed;
1916         }
1917
1918         break;
1919     }
1920
1921     /* report fps */
1922     if( p_sys->f_fps >= 0.1 )
1923     {
1924         msg_Dbg( p_demux, "User set fps=%f", p_sys->f_fps );
1925     }
1926
1927     return i_fd;
1928
1929 open_failed:
1930     if( i_fd >= 0 ) v4l2_close( i_fd );
1931     return -1;
1932
1933 }
1934
1935 /*****************************************************************************
1936  * ProbeVideoDev: probe video for capabilities
1937  *****************************************************************************/
1938 static bool ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
1939                                  const char *psz_device )
1940 {
1941     int i_index;
1942     int i_standard;
1943
1944     int i_fd;
1945     int libv4l2_fd;
1946
1947     if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
1948     {
1949         msg_Err( p_obj, "cannot open video device '%s' (%m)", psz_device );
1950         goto open_failed;
1951     }
1952
1953 #ifdef HAVE_LIBV4L2
1954     /* Note the v4l2_xxx functions are designed so that if they get passed an
1955        unknown fd, the will behave exactly as their regular xxx counterparts,
1956        so if v4l2_fd_open fails, we continue as normal (missing the libv4l2
1957        custom cam format to normal formats conversion). Chances are big we will
1958        still fail then though, as normally v4l2_fd_open only fails if the
1959        device is not a v4l2 device. */
1960     libv4l2_fd = v4l2_fd_open( i_fd, V4L2_ENABLE_ENUM_FMT_EMULATION );
1961     if( libv4l2_fd != -1 )
1962         i_fd = libv4l2_fd;
1963 #else
1964     libv4l2_fd = i_fd;
1965 #endif
1966
1967     /* Get device capabilites */
1968
1969     if( v4l2_ioctl( i_fd, VIDIOC_QUERYCAP, &p_sys->dev_cap ) < 0 )
1970     {
1971         msg_Err( p_obj, "cannot get video capabilities (%m)" );
1972         goto open_failed;
1973     }
1974
1975     msg_Dbg( p_obj, "V4L2 device: %s using driver: %s (version: %u.%u.%u) on %s",
1976                             p_sys->dev_cap.card,
1977                             p_sys->dev_cap.driver,
1978                             (p_sys->dev_cap.version >> 16) & 0xFF,
1979                             (p_sys->dev_cap.version >> 8) & 0xFF,
1980                             p_sys->dev_cap.version & 0xFF,
1981                             p_sys->dev_cap.bus_info );
1982
1983     msg_Dbg( p_obj, "the device has the capabilities: (%c) Video Capure, "
1984                                                        "(%c) Audio, "
1985                                                        "(%c) Tuner",
1986              ( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE  ? 'X':' '),
1987              ( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO  ? 'X':' '),
1988              ( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER  ? 'X':' ') );
1989
1990     msg_Dbg( p_obj, "supported I/O methods are: (%c) Read/Write, "
1991                                                  "(%c) Streaming, "
1992                                                  "(%c) Asynchronous",
1993             ( p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE ? 'X':' ' ),
1994             ( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING ? 'X':' ' ),
1995             ( p_sys->dev_cap.capabilities & V4L2_CAP_ASYNCIO ? 'X':' ' ) );
1996
1997     /* Now, enumerate all the video inputs. This is useless at the moment
1998        since we have no way to present that info to the user except with
1999        debug messages */
2000
2001     if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2002     {
2003         struct v4l2_input t_input;
2004         t_input.index = 0;
2005         while( v4l2_ioctl( i_fd, VIDIOC_ENUMINPUT, &t_input ) >= 0 )
2006         {
2007             p_sys->i_input++;
2008             t_input.index = p_sys->i_input;
2009         }
2010
2011         p_sys->p_inputs = calloc( 1, p_sys->i_input * sizeof( struct v4l2_input ) );
2012         if( !p_sys->p_inputs ) goto open_failed;
2013
2014         for( i_index = 0; i_index < p_sys->i_input; i_index++ )
2015         {
2016             p_sys->p_inputs[i_index].index = i_index;
2017
2018             if( v4l2_ioctl( i_fd, VIDIOC_ENUMINPUT, &p_sys->p_inputs[i_index] ) )
2019             {
2020                 msg_Err( p_obj, "cannot get video input characteristics (%m)" );
2021                 goto open_failed;
2022             }
2023             msg_Dbg( p_obj, "video input %i (%s) has type: %s %c",
2024                                 i_index,
2025                                 p_sys->p_inputs[i_index].name,
2026                                 p_sys->p_inputs[i_index].type
2027                                         == V4L2_INPUT_TYPE_TUNER ?
2028                                         "Tuner adapter" :
2029                                         "External analog input",
2030                                 i_index == p_sys->i_selected_input ? '*' : ' ' );
2031         }
2032     }
2033
2034     /* Probe video standards */
2035     if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2036     {
2037         struct v4l2_standard t_standards;
2038         t_standards.index = 0;
2039         while( v4l2_ioctl( i_fd, VIDIOC_ENUMSTD, &t_standards ) >=0 )
2040         {
2041             p_sys->i_standard++;
2042             t_standards.index = p_sys->i_standard;
2043         }
2044
2045         p_sys->p_standards = calloc( 1, p_sys->i_standard * sizeof( struct v4l2_standard ) );
2046         if( !p_sys->p_standards ) goto open_failed;
2047
2048         for( i_standard = 0; i_standard < p_sys->i_standard; i_standard++ )
2049         {
2050             p_sys->p_standards[i_standard].index = i_standard;
2051
2052             if( v4l2_ioctl( i_fd, VIDIOC_ENUMSTD, &p_sys->p_standards[i_standard] ) )
2053             {
2054                 msg_Err( p_obj, "cannot get video input standards (%m)" );
2055                 goto open_failed;
2056             }
2057             msg_Dbg( p_obj, "video standard %i is: %s %c",
2058                                 i_standard,
2059                                 p_sys->p_standards[i_standard].name,
2060                                 (unsigned)i_standard == p_sys->i_selected_standard_id ? '*' : ' ' );
2061         }
2062     }
2063
2064     /* initialize the structures for the ioctls */
2065     for( i_index = 0; i_index < 32; i_index++ )
2066     {
2067         p_sys->p_audios[i_index].index = i_index;
2068     }
2069
2070     /* Probe audio inputs */
2071     if( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO )
2072     {
2073         while( p_sys->i_audio < 32 &&
2074                v4l2_ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_audio] ) >= 0 )
2075         {
2076             if( v4l2_ioctl( i_fd, VIDIOC_G_AUDIO, &p_sys->p_audios[ p_sys->i_audio] ) < 0 )
2077             {
2078                 msg_Err( p_obj, "cannot get audio input characteristics (%m)" );
2079                 goto open_failed;
2080             }
2081
2082             msg_Dbg( p_obj, "audio input %i (%s) is %s %s %c",
2083                                 p_sys->i_audio,
2084                                 p_sys->p_audios[p_sys->i_audio].name,
2085                                 p_sys->p_audios[p_sys->i_audio].capability &
2086                                                     V4L2_AUDCAP_STEREO ?
2087                                         "Stereo" : "Mono",
2088                                 p_sys->p_audios[p_sys->i_audio].capability &
2089                                                     V4L2_AUDCAP_AVL ?
2090                                     "(Automatic Volume Level supported)" : "",
2091                                 p_sys->i_audio == p_sys->i_selected_audio_input ? '*' : ' ' );
2092
2093             p_sys->i_audio++;
2094         }
2095     }
2096
2097     /* List tuner caps */
2098     if( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER )
2099     {
2100         struct v4l2_tuner tuner;
2101         memset( &tuner, 0, sizeof(tuner) );
2102         while( v4l2_ioctl( i_fd, VIDIOC_G_TUNER, &tuner ) >= 0 )
2103         {
2104             p_sys->i_tuner++;
2105             memset( &tuner, 0, sizeof(tuner) );
2106             tuner.index = p_sys->i_tuner;
2107         }
2108
2109         p_sys->p_tuners = calloc( 1, p_sys->i_tuner * sizeof( struct v4l2_tuner ) );
2110         if( !p_sys->p_tuners ) goto open_failed;
2111
2112         for( i_index = 0; i_index < p_sys->i_tuner; i_index++ )
2113         {
2114             p_sys->p_tuners[i_index].index = i_index;
2115
2116             if( v4l2_ioctl( i_fd, VIDIOC_G_TUNER, &p_sys->p_tuners[i_index] ) )
2117             {
2118                 msg_Err( p_obj, "cannot get tuner characteristics (%m)" );
2119                 goto open_failed;
2120             }
2121             msg_Dbg( p_obj, "tuner %i (%s) has type: %s, "
2122                               "frequency range: %.1f %s -> %.1f %s",
2123                                 i_index,
2124                                 p_sys->p_tuners[i_index].name,
2125                                 p_sys->p_tuners[i_index].type
2126                                         == V4L2_TUNER_RADIO ?
2127                                         "Radio" : "Analog TV",
2128                                 p_sys->p_tuners[i_index].rangelow * 62.5,
2129                                 p_sys->p_tuners[i_index].capability &
2130                                         V4L2_TUNER_CAP_LOW ?
2131                                         "Hz" : "kHz",
2132                                 p_sys->p_tuners[i_index].rangehigh * 62.5,
2133                                 p_sys->p_tuners[i_index].capability &
2134                                         V4L2_TUNER_CAP_LOW ?
2135                                         "Hz" : "kHz" );
2136
2137             struct v4l2_frequency frequency;
2138             memset( &frequency, 0, sizeof( frequency ) );
2139             if( v4l2_ioctl( i_fd, VIDIOC_G_FREQUENCY, &frequency ) < 0 )
2140             {
2141                 msg_Err( p_obj, "cannot get tuner frequency (%m)" );
2142                 goto open_failed;
2143             }
2144             msg_Dbg( p_obj, "tuner %i (%s) frequency: %.1f %s",
2145                      i_index,
2146                      p_sys->p_tuners[i_index].name,
2147                      frequency.frequency * 62.5,
2148                      p_sys->p_tuners[i_index].capability &
2149                              V4L2_TUNER_CAP_LOW ?
2150                              "Hz" : "kHz" );
2151         }
2152     }
2153
2154     /* Probe for available chromas */
2155     if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2156     {
2157         struct v4l2_fmtdesc codec;
2158
2159         i_index = 0;
2160         memset( &codec, 0, sizeof(codec) );
2161         codec.index = i_index;
2162         codec.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2163
2164         while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FMT, &codec ) >= 0 )
2165         {
2166             i_index++;
2167             codec.index = i_index;
2168         }
2169
2170         p_sys->i_codec = i_index;
2171
2172         p_sys->p_codecs = calloc( 1, p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
2173
2174         for( i_index = 0; i_index < p_sys->i_codec; i_index++ )
2175         {
2176             p_sys->p_codecs[i_index].index = i_index;
2177             p_sys->p_codecs[i_index].type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2178
2179             if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FMT, &p_sys->p_codecs[i_index] ) < 0 )
2180             {
2181                 msg_Err( p_obj, "cannot get codec description (%m)" );
2182                 goto open_failed;
2183             }
2184
2185             /* only print if vlc supports the format */
2186             char psz_fourcc_v4l2[5];
2187             memset( &psz_fourcc_v4l2, 0, sizeof( psz_fourcc_v4l2 ) );
2188             vlc_fourcc_to_char( p_sys->p_codecs[i_index].pixelformat,
2189                                 &psz_fourcc_v4l2 );
2190             bool b_codec_supported = false;
2191             for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
2192             {
2193                 if( v4l2chroma_to_fourcc[i].i_v4l2 == p_sys->p_codecs[i_index].pixelformat )
2194                 {
2195                     b_codec_supported = true;
2196
2197                     char psz_fourcc[5];
2198                     memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
2199                     vlc_fourcc_to_char( v4l2chroma_to_fourcc[i].i_fourcc,
2200                                         &psz_fourcc );
2201                     msg_Dbg( p_obj, "device supports chroma %4s [%s, %s]",
2202                                 psz_fourcc,
2203                                 p_sys->p_codecs[i_index].description,
2204                                 psz_fourcc_v4l2 );
2205
2206 #ifdef VIDIOC_ENUM_FRAMESIZES
2207                     /* This is new in Linux 2.6.19 */
2208                     /* List valid frame sizes for this format */
2209                     struct v4l2_frmsizeenum frmsize;
2210                     frmsize.index = 0;
2211                     frmsize.pixel_format = p_sys->p_codecs[i_index].pixelformat;
2212                     if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) < 0 )
2213                     {
2214                         /* Not all devices support this ioctl */
2215                         msg_Warn( p_obj, "Unable to query for frame sizes" );
2216                     }
2217                     else
2218                     {
2219                         switch( frmsize.type )
2220                         {
2221                             case V4L2_FRMSIZE_TYPE_DISCRETE:
2222                                 do
2223                                 {
2224                                     msg_Dbg( p_obj,
2225                 "    device supports size %dx%d",
2226                 frmsize.discrete.width, frmsize.discrete.height );
2227                                     frmsize.index++;
2228                                 } while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) >= 0 );
2229                                 break;
2230                             case V4L2_FRMSIZE_TYPE_STEPWISE:
2231                                 msg_Dbg( p_obj,
2232                 "    device supports sizes %dx%d to %dx%d using %dx%d increments",
2233                 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
2234                 frmsize.stepwise.max_width, frmsize.stepwise.max_height,
2235                 frmsize.stepwise.step_width, frmsize.stepwise.step_height );
2236                                 break;
2237                             case V4L2_FRMSIZE_TYPE_CONTINUOUS:
2238                                 msg_Dbg( p_obj,
2239                 "    device supports all sizes %dx%d to %dx%d",
2240                 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
2241                 frmsize.stepwise.max_width, frmsize.stepwise.max_height );
2242                                 break;
2243                         }
2244                     }
2245 #endif
2246                 }
2247             }
2248             if( !b_codec_supported )
2249             {
2250                     msg_Dbg( p_obj,
2251                          "device codec %4s (%s) not supported as access_demux",
2252                          psz_fourcc_v4l2,
2253                          p_sys->p_codecs[i_index].description );
2254             }
2255
2256         }
2257     }
2258
2259
2260     if( i_fd >= 0 ) v4l2_close( i_fd );
2261     return true;
2262
2263 open_failed:
2264
2265     if( i_fd >= 0 ) v4l2_close( i_fd );
2266     return false;
2267
2268 }
2269
2270 static void name2var( unsigned char *name )
2271 {
2272     for( ; *name; name++ )
2273         *name = (*name == ' ') ? '_' : tolower( *name );
2274 }
2275
2276 /*****************************************************************************
2277  * Print a user-class v4l2 control's details, create the relevant variable,
2278  * change the value if needed.
2279  *****************************************************************************/
2280 static void ControlListPrint( vlc_object_t *p_obj, int i_fd,
2281                               struct v4l2_queryctrl queryctrl,
2282                               bool b_reset, bool b_demux )
2283 {
2284     struct v4l2_querymenu querymenu;
2285     unsigned int i_mid;
2286
2287     int i;
2288     int i_val;
2289
2290     char *psz_name;
2291     vlc_value_t val, val2;
2292
2293     if( queryctrl.flags & V4L2_CTRL_FLAG_GRABBED )
2294         msg_Dbg( p_obj, "    control is busy" );
2295     if( queryctrl.flags & V4L2_CTRL_FLAG_READ_ONLY )
2296         msg_Dbg( p_obj, "    control is read-only" );
2297
2298     for( i = 0; controls[i].psz_name != NULL; i++ )
2299         if( controls[i].i_cid == queryctrl.id ) break;
2300
2301     if( controls[i].psz_name )
2302     {
2303         psz_name = strdup( controls[i].psz_name );
2304         char psz_cfg_name[40];
2305         sprintf( psz_cfg_name, CFG_PREFIX "%s", psz_name );
2306         i_val = var_CreateGetInteger( p_obj, psz_cfg_name );
2307         var_Destroy( p_obj, psz_cfg_name );
2308     }
2309     else
2310     {
2311         psz_name = strdup( (const char *)queryctrl.name );
2312         name2var( (unsigned char *)psz_name );
2313         i_val = -1;
2314     }
2315
2316     switch( queryctrl.type )
2317     {
2318         case V4L2_CTRL_TYPE_INTEGER:
2319             msg_Dbg( p_obj, "    integer control" );
2320             msg_Dbg( p_obj,
2321                      "    valid values: %d to %d by steps of %d",
2322                      queryctrl.minimum, queryctrl.maximum,
2323                      queryctrl.step );
2324
2325             var_Create( p_obj, psz_name,
2326                         VLC_VAR_INTEGER | VLC_VAR_HASMIN | VLC_VAR_HASMAX
2327                       | VLC_VAR_HASSTEP | VLC_VAR_ISCOMMAND );
2328             val.i_int = queryctrl.minimum;
2329             var_Change( p_obj, psz_name, VLC_VAR_SETMIN, &val, NULL );
2330             val.i_int = queryctrl.maximum;
2331             var_Change( p_obj, psz_name, VLC_VAR_SETMAX, &val, NULL );
2332             val.i_int = queryctrl.step;
2333             var_Change( p_obj, psz_name, VLC_VAR_SETSTEP, &val, NULL );
2334             break;
2335         case V4L2_CTRL_TYPE_BOOLEAN:
2336             msg_Dbg( p_obj, "    boolean control" );
2337             var_Create( p_obj, psz_name,
2338                         VLC_VAR_BOOL | VLC_VAR_ISCOMMAND );
2339             break;
2340         case V4L2_CTRL_TYPE_MENU:
2341             msg_Dbg( p_obj, "    menu control" );
2342             var_Create( p_obj, psz_name,
2343                         VLC_VAR_INTEGER | VLC_VAR_HASCHOICE
2344                       | VLC_VAR_ISCOMMAND );
2345             memset( &querymenu, 0, sizeof( querymenu ) );
2346             for( i_mid = queryctrl.minimum;
2347                  i_mid <= (unsigned)queryctrl.maximum;
2348                  i_mid++ )
2349             {
2350                 querymenu.index = i_mid;
2351                 querymenu.id = queryctrl.id;
2352                 if( v4l2_ioctl( i_fd, VIDIOC_QUERYMENU, &querymenu ) >= 0 )
2353                 {
2354                     msg_Dbg( p_obj, "        %d: %s",
2355                              querymenu.index, querymenu.name );
2356                     val.i_int = querymenu.index;
2357                     val2.psz_string = (char *)querymenu.name;
2358                     var_Change( p_obj, psz_name,
2359                                 VLC_VAR_ADDCHOICE, &val, &val2 );
2360                 }
2361             }
2362             break;
2363         case V4L2_CTRL_TYPE_BUTTON:
2364             msg_Dbg( p_obj, "    button control" );
2365             var_Create( p_obj, psz_name,
2366                         VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
2367             break;
2368         case V4L2_CTRL_TYPE_CTRL_CLASS:
2369             msg_Dbg( p_obj, "    control class" );
2370             var_Create( p_obj, psz_name, VLC_VAR_VOID );
2371             break;
2372         default:
2373             msg_Dbg( p_obj, "    unknown control type (FIXME)" );
2374             /* FIXME */
2375             break;
2376     }
2377
2378     switch( queryctrl.type )
2379     {
2380         case V4L2_CTRL_TYPE_INTEGER:
2381         case V4L2_CTRL_TYPE_BOOLEAN:
2382         case V4L2_CTRL_TYPE_MENU:
2383             {
2384                 struct v4l2_control control;
2385                 msg_Dbg( p_obj, "    default value: %d",
2386                          queryctrl.default_value );
2387                 memset( &control, 0, sizeof( control ) );
2388                 control.id = queryctrl.id;
2389                 if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
2390                 {
2391                     msg_Dbg( p_obj, "    current value: %d", control.value );
2392                 }
2393                 if( i_val == -1 )
2394                 {
2395                     i_val = control.value;
2396                     if( b_reset && queryctrl.default_value != control.value )
2397                     {
2398                         msg_Dbg( p_obj, "    reset value to default" );
2399                         Control( p_obj, i_fd, psz_name,
2400                                       queryctrl.id, queryctrl.default_value );
2401                     }
2402                 }
2403                 else
2404                 {
2405                     Control( p_obj, i_fd, psz_name,
2406                                   queryctrl.id, i_val );
2407                 }
2408             }
2409             break;
2410         default:
2411             break;
2412     }
2413
2414     val.psz_string = (char *)queryctrl.name;
2415     var_Change( p_obj, psz_name, VLC_VAR_SETTEXT, &val, NULL );
2416     val.i_int = queryctrl.id;
2417     val2.psz_string = (char *)psz_name;
2418     var_Change( p_obj, "allcontrols", VLC_VAR_ADDCHOICE, &val, &val2 );
2419     /* bad things happen changing MPEG mid-stream
2420      * so don't add to Ext Settings GUI */
2421     if( V4L2_CTRL_ID2CLASS( queryctrl.id ) != V4L2_CTRL_CLASS_MPEG )
2422         var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
2423
2424     switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
2425     {
2426         case VLC_VAR_BOOL:
2427             var_SetBool( p_obj, psz_name, i_val );
2428             break;
2429         case VLC_VAR_INTEGER:
2430             var_SetInteger( p_obj, psz_name, i_val );
2431             break;
2432         case VLC_VAR_VOID:
2433             break;
2434         default:
2435             msg_Warn( p_obj, "FIXME: %s %s %d", __FILE__, __func__,
2436                       __LINE__ );
2437             break;
2438     }
2439
2440     if( b_demux )
2441         var_AddCallback( p_obj, psz_name,
2442                         DemuxControlCallback, (void*)(intptr_t)queryctrl.id );
2443     else
2444         var_AddCallback( p_obj, psz_name,
2445                         AccessControlCallback, (void*)(intptr_t)queryctrl.id );
2446
2447     free( psz_name );
2448 }
2449
2450 /*****************************************************************************
2451  * List all user-class v4l2 controls, set them to the user specified
2452  * value and create the relevant variables to enable runtime changes
2453  *****************************************************************************/
2454 static int ControlList( vlc_object_t *p_obj, int i_fd,
2455                         bool b_reset, bool b_demux )
2456 {
2457     struct v4l2_queryctrl queryctrl;
2458     int i_cid;
2459
2460     memset( &queryctrl, 0, sizeof( queryctrl ) );
2461
2462     /* A list of available controls (aka the variable name) will be
2463      * stored as choices in the "allcontrols" variable. We'll thus be able
2464      * to use those to create an appropriate interface
2465      * A list of available controls that can be changed mid-stream will
2466      * be stored in the "controls" variable */
2467     var_Create( p_obj, "controls", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
2468     var_Create( p_obj, "allcontrols", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
2469
2470     var_Create( p_obj, "controls-update", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
2471
2472     /* Add a control to reset all controls to their default values */
2473     vlc_value_t val, val2;
2474     var_Create( p_obj, "controls-reset", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
2475     val.psz_string = _( "Reset controls to default" );
2476     var_Change( p_obj, "controls-reset", VLC_VAR_SETTEXT, &val, NULL );
2477     val.i_int = -1;
2478     val2.psz_string = (char *)"controls-reset";
2479     var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
2480     if (b_demux)
2481         var_AddCallback( p_obj, "controls-reset", DemuxControlResetCallback, NULL );
2482     else
2483         var_AddCallback( p_obj, "controls-reset", AccessControlResetCallback, NULL );
2484
2485     queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
2486     if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2487     {
2488         msg_Dbg( p_obj, "Extended control API supported by v4l2 driver" );
2489
2490         /* List extended controls */
2491         queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
2492         while( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2493         {
2494             if( queryctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS )
2495             {
2496                 msg_Dbg( p_obj, "%s", queryctrl.name );
2497                 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
2498                 continue;
2499             }
2500             switch( V4L2_CTRL_ID2CLASS( queryctrl.id ) )
2501             {
2502                 case V4L2_CTRL_CLASS_USER:
2503                     msg_Dbg( p_obj, "Available control: %s (%x)",
2504                              queryctrl.name, queryctrl.id );
2505                     break;
2506                 case V4L2_CTRL_CLASS_MPEG:
2507                     name2var( queryctrl.name );
2508                     msg_Dbg( p_obj, "Available MPEG control: %s (%x)",
2509                              queryctrl.name, queryctrl.id );
2510                     break;
2511                 default:
2512                     msg_Dbg( p_obj, "Available private control: %s (%x)",
2513                              queryctrl.name, queryctrl.id );
2514                     break;
2515             }
2516             ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
2517             queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
2518         }
2519     }
2520     else
2521     {
2522         msg_Dbg( p_obj, "Extended control API not supported by v4l2 driver" );
2523
2524         /* List public controls */
2525         for( i_cid = V4L2_CID_BASE;
2526              i_cid < V4L2_CID_LASTP1;
2527              i_cid ++ )
2528         {
2529             queryctrl.id = i_cid;
2530             if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2531             {
2532                 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2533                     continue;
2534                 msg_Dbg( p_obj, "Available control: %s (%x)",
2535                          queryctrl.name, queryctrl.id );
2536                 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
2537             }
2538         }
2539
2540         /* List private controls */
2541         for( i_cid = V4L2_CID_PRIVATE_BASE;
2542              ;
2543              i_cid ++ )
2544         {
2545             queryctrl.id = i_cid;
2546             if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2547             {
2548                 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2549                     continue;
2550                 msg_Dbg( p_obj, "Available private control: %s (%x)",
2551                          queryctrl.name, queryctrl.id );
2552                 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
2553             }
2554             else
2555                 break;
2556         }
2557     }
2558
2559     return VLC_SUCCESS;
2560 }
2561
2562 static void SetAvailControlsByString( vlc_object_t *p_obj, demux_sys_t *p_sys,
2563                                       int i_fd )
2564 {
2565     char *psz_parser = p_sys->psz_set_ctrls;
2566     vlc_value_t val, text, name;
2567
2568     if( psz_parser == NULL )
2569         return;
2570
2571     if( *psz_parser == '{' )
2572         psz_parser++;
2573
2574     int i_ret = var_Change( p_obj, "allcontrols", VLC_VAR_GETCHOICES,
2575                             &val, &text );
2576     if( i_ret < 0 )
2577     {
2578         msg_Err( p_obj, "Oops, can't find 'allcontrols' variable." );
2579         return;
2580     }
2581
2582     while( *psz_parser && *psz_parser != '}' )
2583     {
2584         char *psz_delim, *psz_assign;
2585
2586         while( *psz_parser == ',' || *psz_parser == ' ' )
2587             psz_parser++;
2588
2589         psz_delim = strchr( psz_parser, ',' );
2590         if( psz_delim == NULL )
2591             psz_delim = strchr( psz_parser, '}' );
2592         if( psz_delim == NULL )
2593             psz_delim = psz_parser + strlen( psz_parser );
2594
2595         psz_assign = memchr( psz_parser, '=', psz_delim - psz_parser );
2596         if( psz_assign == NULL )
2597         {
2598             char *psz_name = strndup( psz_parser, psz_delim - psz_parser );
2599             msg_Err( p_obj, "%s missing '='", psz_name );
2600             free( psz_name );
2601             psz_parser = psz_delim + 1;
2602             continue;
2603         }
2604
2605         for( int i = 0;
2606              i < val.p_list->i_count ;//&& psz_parser < psz_assign;
2607              i++ )
2608         {
2609             const char *psz_var = text.p_list->p_values[i].psz_string;
2610             int i_cid = val.p_list->p_values[i].i_int;
2611             var_Change( p_obj, psz_var, VLC_VAR_GETTEXT, &name, NULL );
2612             const char *psz_name = name.psz_string;
2613
2614             int i_availstrlen = strlen( psz_name );
2615             int i_parsestrlen = psz_assign - psz_parser;
2616             int i_maxstrlen = __MAX( i_availstrlen, i_parsestrlen);
2617
2618             if( !strncasecmp( psz_name, psz_parser, i_maxstrlen ) )
2619             {
2620                 Control( p_obj, i_fd, psz_name, i_cid,
2621                          strtol( ++psz_assign, &psz_parser, 0) );
2622             }
2623         }
2624
2625         if( psz_parser < psz_assign )
2626         {
2627             char *psz_name = strndup( psz_parser, psz_assign - psz_parser );
2628             msg_Err( p_obj, "Control %s not available", psz_name );
2629             free( psz_name );
2630             psz_parser = ( *psz_delim ) ? ( psz_delim + 1 ) : psz_delim;
2631         }
2632     }
2633 }
2634
2635 /*****************************************************************************
2636  * Reset all user-class v4l2 controls to their default value
2637  *****************************************************************************/
2638 static int ControlReset( vlc_object_t *p_obj, int i_fd )
2639 {
2640     struct v4l2_queryctrl queryctrl;
2641     int i_cid;
2642     memset( &queryctrl, 0, sizeof( queryctrl ) );
2643
2644     queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
2645     if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2646     {
2647         /* Extended control API supported */
2648         queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
2649         while( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2650         {
2651             if( queryctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS
2652              || V4L2_CTRL_ID2CLASS( queryctrl.id ) == V4L2_CTRL_CLASS_MPEG )
2653             {
2654                 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
2655                 continue;
2656             }
2657             struct v4l2_control control;
2658             memset( &control, 0, sizeof( control ) );
2659             control.id = queryctrl.id;
2660             if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
2661              && queryctrl.default_value != control.value )
2662             {
2663                 int i;
2664                 for( i = 0; controls[i].psz_name != NULL; i++ )
2665                     if( controls[i].i_cid == queryctrl.id ) break;
2666                 name2var( queryctrl.name );
2667                 Control( p_obj, i_fd,
2668                          controls[i].psz_name ? : (const char *)queryctrl.name,
2669                          queryctrl.id, queryctrl.default_value );
2670             }
2671             queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
2672         }
2673     }
2674     else
2675     {
2676
2677         /* public controls */
2678         for( i_cid = V4L2_CID_BASE;
2679              i_cid < V4L2_CID_LASTP1;
2680              i_cid ++ )
2681         {
2682             queryctrl.id = i_cid;
2683             if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2684             {
2685                 struct v4l2_control control;
2686                 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2687                     continue;
2688                 memset( &control, 0, sizeof( control ) );
2689                 control.id = queryctrl.id;
2690                 if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
2691                  && queryctrl.default_value != control.value )
2692                 {
2693                     int i;
2694                     for( i = 0; controls[i].psz_name != NULL; i++ )
2695                         if( controls[i].i_cid == queryctrl.id ) break;
2696                     name2var( queryctrl.name );
2697                     Control( p_obj, i_fd,
2698                              controls[i].psz_name ? : (const char *)queryctrl.name,
2699                              queryctrl.id, queryctrl.default_value );
2700                 }
2701             }
2702         }
2703
2704         /* private controls */
2705         for( i_cid = V4L2_CID_PRIVATE_BASE;
2706              ;
2707              i_cid ++ )
2708         {
2709             queryctrl.id = i_cid;
2710             if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2711             {
2712                 struct v4l2_control control;
2713                 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2714                     continue;
2715                 memset( &control, 0, sizeof( control ) );
2716                 control.id = queryctrl.id;
2717                 if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
2718                  && queryctrl.default_value != control.value )
2719                 {
2720                     name2var( queryctrl.name );
2721                     Control( p_obj, i_fd, (const char *)queryctrl.name,
2722                              queryctrl.id, queryctrl.default_value );
2723                 }
2724             }
2725             else
2726                 break;
2727         }
2728     }
2729     return VLC_SUCCESS;
2730 }
2731
2732 /*****************************************************************************
2733  * Issue user-class v4l2 controls
2734  *****************************************************************************/
2735 static int Control( vlc_object_t *p_obj, int i_fd,
2736                     const char *psz_name, int i_cid, int i_value )
2737 {
2738     struct v4l2_queryctrl queryctrl;
2739     struct v4l2_control control;
2740     struct v4l2_ext_control ext_control;
2741     struct v4l2_ext_controls ext_controls;
2742
2743     if( i_value == -1 )
2744         return VLC_SUCCESS;
2745
2746     memset( &queryctrl, 0, sizeof( queryctrl ) );
2747
2748     queryctrl.id = i_cid;
2749
2750     if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) < 0
2751         || queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2752     {
2753         msg_Dbg( p_obj, "%s (%x) control is not supported.", psz_name, i_cid );
2754         return VLC_EGENERIC;
2755     }
2756
2757     memset( &control, 0, sizeof( control ) );
2758     memset( &ext_control, 0, sizeof( ext_control ) );
2759     memset( &ext_controls, 0, sizeof( ext_controls ) );
2760     control.id = i_cid;
2761     ext_control.id = i_cid;
2762     ext_controls.ctrl_class = V4L2_CTRL_ID2CLASS( i_cid );
2763     ext_controls.count = 1;
2764     ext_controls.controls = &ext_control;
2765
2766     int i_ret = -1;
2767
2768     if( i_value >= 0 )
2769     {
2770         ext_control.value = i_value;
2771         if( v4l2_ioctl( i_fd, VIDIOC_S_EXT_CTRLS, &ext_controls ) < 0 )
2772         {
2773             control.value = i_value;
2774             if( v4l2_ioctl( i_fd, VIDIOC_S_CTRL, &control ) < 0 )
2775             {
2776                 msg_Err( p_obj, "unable to set %s (%x) to %d (%m)",
2777                          psz_name, i_cid, i_value );
2778                 return VLC_EGENERIC;
2779             }
2780             i_ret = v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control );
2781         }
2782         else
2783         {
2784             i_ret = v4l2_ioctl( i_fd, VIDIOC_G_EXT_CTRLS, &ext_controls );
2785             control.value = ext_control.value;
2786         }
2787     }
2788
2789     if( i_ret >= 0 )
2790     {
2791         vlc_value_t val;
2792         msg_Dbg( p_obj, "video %s: %d", psz_name, control.value );
2793         switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
2794         {
2795             case VLC_VAR_BOOL:
2796                 val.b_bool = control.value;
2797                 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
2798                 var_SetVoid( p_obj, "controls-update" );
2799                 break;
2800             case VLC_VAR_INTEGER:
2801                 val.i_int = control.value;
2802                 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
2803                 var_SetVoid( p_obj, "controls-update" );
2804                 break;
2805         }
2806     }
2807     return VLC_SUCCESS;
2808 }
2809
2810 /*****************************************************************************
2811  * On the fly change settings callback
2812  *****************************************************************************/
2813 static int DemuxControlCallback( vlc_object_t *p_this,
2814     const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
2815     void *p_data )
2816 {
2817     (void)oldval;
2818     demux_t *p_demux = (demux_t*)p_this;
2819     demux_sys_t *p_sys = p_demux->p_sys;
2820     int i_cid = (long int)p_data;
2821
2822     int i_fd = p_sys->i_fd;
2823
2824     if( i_fd < 0 )
2825         return VLC_EGENERIC;
2826
2827     Control( p_this, i_fd, psz_var, i_cid, newval.i_int );
2828
2829     return VLC_EGENERIC;
2830 }
2831
2832 static int DemuxControlResetCallback( vlc_object_t *p_this,
2833     const char *psz_var, vlc_value_t oldval, vlc_value_t newval, void *p_data )
2834 {
2835     (void)psz_var;    (void)oldval;    (void)newval;    (void)p_data;
2836     demux_t *p_demux = (demux_t*)p_this;
2837     demux_sys_t *p_sys = p_demux->p_sys;
2838
2839     int i_fd = p_sys->i_fd;
2840
2841     if( i_fd < 0 )
2842         return VLC_EGENERIC;
2843
2844     ControlReset( p_this, i_fd );
2845
2846     return VLC_EGENERIC;
2847 }
2848
2849 static int AccessControlCallback( vlc_object_t *p_this,
2850     const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
2851     void *p_data )
2852 {
2853     (void)oldval;
2854     access_t *p_access = (access_t *)p_this;
2855     demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
2856     int i_cid = (long int)p_data;
2857
2858     int i_fd = p_sys->i_fd;
2859
2860     if( i_fd < 0 )
2861         return VLC_EGENERIC;
2862
2863     Control( p_this, i_fd, psz_var, i_cid, newval.i_int );
2864
2865     return VLC_EGENERIC;
2866 }
2867
2868 static int AccessControlResetCallback( vlc_object_t *p_this,
2869     const char *psz_var, vlc_value_t oldval, vlc_value_t newval, void *p_data )
2870 {
2871     (void)psz_var;     (void)oldval;     (void)newval;     (void)p_data;
2872     access_t *p_access = (access_t *)p_this;
2873     demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
2874
2875     int i_fd = p_sys->i_fd;
2876
2877     if( i_fd < 0 )
2878         return VLC_EGENERIC;
2879
2880     ControlReset( p_this, i_fd );
2881
2882     return VLC_EGENERIC;
2883 }