1 /*****************************************************************************
2 * v4l2.c : Video4Linux2 input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2009 the VideoLAN team
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>
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.
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.
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 *****************************************************************************/
28 * Sections based on the reference V4L2 capture example at
29 * http://v4l2spec.bytesex.org/spec/capture-example.html
32 /*****************************************************************************
34 *****************************************************************************/
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>
48 #include <sys/ioctl.h>
51 #include <linux/videodev2.h>
59 /*****************************************************************************
61 *****************************************************************************/
63 static int DemuxOpen ( vlc_object_t * );
64 static void DemuxClose( vlc_object_t * );
65 static int AccessOpen ( vlc_object_t * );
66 static void AccessClose( vlc_object_t * );
68 #define STANDARD_TEXT N_( "Standard" )
69 #define STANDARD_LONGTEXT N_( \
70 "Video standard (Default, SECAM, PAL, or NTSC)." )
71 #define CHROMA_TEXT N_("Video input chroma format")
72 #define CHROMA_LONGTEXT N_( \
73 "Force the Video4Linux2 video device to use a specific chroma format " \
74 "(eg. I420 or I422 for raw images, MJPG for M-JPEG compressed input) " \
75 "(Complete list: GREY, I240, RV16, RV15, RV24, RV32, YUY2, YUYV, UYVY, " \
76 "I41N, I422, I420, I411, I410, MJPG)")
77 #define INPUT_TEXT N_( "Input" )
78 #define INPUT_LONGTEXT N_( \
79 "Input of the card to use (see debug)." )
80 #define AUDIO_INPUT_TEXT N_( "Audio input" )
81 #define AUDIO_INPUT_LONGTEXT N_( \
82 "Audio input of the card to use (see debug)." )
83 #define IOMETHOD_TEXT N_( "IO Method" )
84 #define IOMETHOD_LONGTEXT N_( \
85 "IO Method (READ, MMAP, USERPTR)." )
86 #define WIDTH_TEXT N_( "Width" )
87 #define WIDTH_LONGTEXT N_( \
88 "Force width (-1 for autodetect, 0 for driver default)." )
89 #define HEIGHT_TEXT N_( "Height" )
90 #define HEIGHT_LONGTEXT N_( \
91 "Force height (-1 for autodetect, 0 for driver default)." )
92 #define FPS_TEXT N_( "Framerate" )
93 #define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
94 "(0 for autodetect)." )
97 #define LIBV4L2_TEXT N_( "Use libv4l2" )
98 #define LIBV4L2_LONGTEXT N_( \
99 "Force usage of the libv4l2 wrapper." )
102 #define CTRL_RESET_TEXT N_( "Reset v4l2 controls" )
103 #define CTRL_RESET_LONGTEXT N_( \
104 "Reset controls to defaults provided by the v4l2 driver." )
105 #define BRIGHTNESS_TEXT N_( "Brightness" )
106 #define BRIGHTNESS_LONGTEXT N_( \
107 "Brightness of the video input (if supported by the v4l2 driver)." )
108 #define CONTRAST_TEXT N_( "Contrast" )
109 #define CONTRAST_LONGTEXT N_( \
110 "Contrast of the video input (if supported by the v4l2 driver)." )
111 #define SATURATION_TEXT N_( "Saturation" )
112 #define SATURATION_LONGTEXT N_( \
113 "Saturation of the video input (if supported by the v4l2 driver)." )
114 #define HUE_TEXT N_( "Hue" )
115 #define HUE_LONGTEXT N_( \
116 "Hue of the video input (if supported by the v4l2 driver)." )
117 #define BLACKLEVEL_TEXT N_( "Black level" )
118 #define BLACKLEVEL_LONGTEXT N_( \
119 "Black level of the video input (if supported by the v4l2 driver)." )
120 #define AUTOWHITEBALANCE_TEXT N_( "Auto white balance" )
121 #define AUTOWHITEBALANCE_LONGTEXT N_( \
122 "Automatically set the white balance of the video input " \
123 "(if supported by the v4l2 driver)." )
124 #define DOWHITEBALANCE_TEXT N_( "Do white balance" )
125 #define DOWHITEBALANCE_LONGTEXT N_( \
126 "Trigger a white balancing action, useless if auto white balance is " \
127 "activated (if supported by the v4l2 driver)." )
128 #define REDBALANCE_TEXT N_( "Red balance" )
129 #define REDBALANCE_LONGTEXT N_( \
130 "Red balance of the video input (if supported by the v4l2 driver)." )
131 #define BLUEBALANCE_TEXT N_( "Blue balance" )
132 #define BLUEBALANCE_LONGTEXT N_( \
133 "Blue balance of the video input (if supported by the v4l2 driver)." )
134 #define GAMMA_TEXT N_( "Gamma" )
135 #define GAMMA_LONGTEXT N_( \
136 "Gamma of the video input (if supported by the v4l2 driver)." )
137 #define EXPOSURE_TEXT N_( "Exposure" )
138 #define EXPOSURE_LONGTEXT N_( \
139 "Exposure of the video input (if supported by the v4L2 driver)." )
140 #define AUTOGAIN_TEXT N_( "Auto gain" )
141 #define AUTOGAIN_LONGTEXT N_( \
142 "Automatically set the video input's gain (if supported by the " \
144 #define GAIN_TEXT N_( "Gain" )
145 #define GAIN_LONGTEXT N_( \
146 "Video input's gain (if supported by the v4l2 driver)." )
147 #define HFLIP_TEXT N_( "Horizontal flip" )
148 #define HFLIP_LONGTEXT N_( \
149 "Flip the video horizontally (if supported by the v4l2 driver)." )
150 #define VFLIP_TEXT N_( "Vertical flip" )
151 #define VFLIP_LONGTEXT N_( \
152 "Flip the video vertically (if supported by the v4l2 driver)." )
153 #define HCENTER_TEXT N_( "Horizontal centering" )
154 #define HCENTER_LONGTEXT N_( \
155 "Set the camera's horizontal centering (if supported by the v4l2 driver)." )
156 #define VCENTER_TEXT N_( "Vertical centering" )
157 #define VCENTER_LONGTEXT N_( \
158 "Set the camera's vertical centering (if supported by the v4l2 driver)." )
160 #define AUDIO_VOLUME_TEXT N_( "Volume" )
161 #define AUDIO_VOLUME_LONGTEXT N_( \
162 "Volume of the audio input (if supported by the v4l2 driver)." )
163 #define AUDIO_BALANCE_TEXT N_( "Balance" )
164 #define AUDIO_BALANCE_LONGTEXT N_( \
165 "Balance of the audio input (if supported by the v4l2 driver)." )
166 #define AUDIO_MUTE_TEXT N_( "Mute" )
167 #define AUDIO_MUTE_LONGTEXT N_( \
168 "Mute audio input (if supported by the v4l2 driver)." )
169 #define AUDIO_BASS_TEXT N_( "Bass" )
170 #define AUDIO_BASS_LONGTEXT N_( \
171 "Bass level of the audio input (if supported by the v4l2 driver)." )
172 #define AUDIO_TREBLE_TEXT N_( "Treble" )
173 #define AUDIO_TREBLE_LONGTEXT N_( \
174 "Treble level of the audio input (if supported by the v4l2 driver)." )
175 #define AUDIO_LOUDNESS_TEXT N_( "Loudness" )
176 #define AUDIO_LOUDNESS_LONGTEXT N_( \
177 "Loudness of the audio input (if supported by the v4l2 driver)." )
179 #define CACHING_TEXT N_("Caching value in ms")
180 #define CACHING_LONGTEXT N_( \
181 "Caching value for V4L2 captures. This " \
182 "value should be set in milliseconds." )
183 #define S_CTRLS_TEXT N_("v4l2 driver controls")
184 #define S_CTRLS_LONGTEXT N_( \
185 "Set the v4l2 driver controls to the values specified using a comma " \
186 "separated list optionally encapsulated by curly braces " \
187 "(e.g.: {video_bitrate=6000000,audio_crc=0,stream_type=3} ). " \
188 "To list available controls, increase verbosity (-vvv) " \
189 "or use the v4l2-ctl application." )
191 #define TUNER_TEXT N_("Tuner id")
192 #define TUNER_LONGTEXT N_( \
193 "Tuner id (see debug output)." )
194 #define FREQUENCY_TEXT N_("Frequency")
195 #define FREQUENCY_LONGTEXT N_( \
196 "Tuner frequency in Hz or kHz (see debug output)" )
197 #define TUNER_AUDIO_MODE_TEXT N_("Audio mode")
198 #define TUNER_AUDIO_MODE_LONGTEXT N_( \
199 "Tuner audio mono/stereo and track selection." )
201 #define AUDIO_DEPRECATED_ERROR N_( \
202 "Alsa or OSS audio capture in the v4l2 access is deprecated. " \
203 "please use 'v4l2:/""/ :input-slave=alsa:/""/' or " \
204 "'v4l2:/""/ :input-slave=oss:/""/' instead." )
212 static const int i_standards_list[] =
213 { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC };
214 static const char *const psz_standards_list_text[] =
215 { N_("Default"), N_("SECAM"), N_("PAL"), N_("NTSC") };
217 static const int i_iomethod_list[] =
218 { IO_METHOD_READ, IO_METHOD_MMAP, IO_METHOD_USERPTR };
219 static const char *const psz_iomethod_list_text[] =
220 { N_("READ"), N_("MMAP"), N_("USERPTR") };
222 static const int i_tuner_audio_modes_list[] =
223 { V4L2_TUNER_MODE_MONO, V4L2_TUNER_MODE_STEREO,
224 V4L2_TUNER_MODE_LANG1, V4L2_TUNER_MODE_LANG2,
225 V4L2_TUNER_MODE_SAP, V4L2_TUNER_MODE_LANG1_LANG2 };
226 static const char *const psz_tuner_audio_modes_list_text[] =
229 N_( "Primary language (Analog TV tuners only)" ),
230 N_( "Secondary language (Analog TV tuners only)" ),
231 N_( "Second audio program (Analog TV tuners only)" ),
232 N_( "Primary language left, Secondary language right" ) };
234 #define V4L2_DEFAULT "/dev/video0"
235 #define CFG_PREFIX "v4l2-"
238 set_shortname( N_("Video4Linux2") )
239 set_description( N_("Video4Linux2 input") )
240 set_category( CAT_INPUT )
241 set_subcategory( SUBCAT_INPUT_ACCESS )
243 set_section( N_( "Video input" ), NULL )
244 add_integer( CFG_PREFIX "standard", 0, NULL, STANDARD_TEXT,
245 STANDARD_LONGTEXT, false )
246 change_integer_list( i_standards_list, psz_standards_list_text, NULL )
247 add_string( CFG_PREFIX "chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
249 add_integer( CFG_PREFIX "input", 0, NULL, INPUT_TEXT, INPUT_LONGTEXT,
251 add_integer( CFG_PREFIX "audio-input", 0, NULL, AUDIO_INPUT_TEXT,
252 AUDIO_INPUT_LONGTEXT, true )
253 add_integer( CFG_PREFIX "io", IO_METHOD_MMAP, NULL, IOMETHOD_TEXT,
254 IOMETHOD_LONGTEXT, true )
255 change_integer_list( i_iomethod_list, psz_iomethod_list_text, NULL )
256 add_integer( CFG_PREFIX "width", -1, NULL, WIDTH_TEXT,
257 WIDTH_LONGTEXT, true )
258 add_integer( CFG_PREFIX "height", -1, NULL, HEIGHT_TEXT,
259 HEIGHT_LONGTEXT, true )
260 add_float( CFG_PREFIX "fps", 0, NULL, FPS_TEXT, FPS_LONGTEXT, true )
261 add_integer( CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
262 CACHING_TEXT, CACHING_LONGTEXT, true )
264 add_bool( CFG_PREFIX "use-libv4l2", false, NULL, LIBV4L2_TEXT, LIBV4L2_LONGTEXT, true );
267 set_section( N_( "Tuner" ), NULL )
268 add_integer( CFG_PREFIX "tuner", 0, NULL, TUNER_TEXT, TUNER_LONGTEXT,
270 add_integer( CFG_PREFIX "tuner-frequency", -1, NULL, FREQUENCY_TEXT,
271 FREQUENCY_LONGTEXT, true )
272 add_integer( CFG_PREFIX "tuner-audio-mode", -1, NULL, TUNER_AUDIO_MODE_TEXT,
273 TUNER_AUDIO_MODE_LONGTEXT, true )
274 change_integer_list( i_tuner_audio_modes_list,
275 psz_tuner_audio_modes_list_text, 0 )
277 set_section( N_( "Controls" ),
278 N_( "v4l2 driver controls, if supported by your v4l2 driver." ) )
279 add_bool( CFG_PREFIX "controls-reset", false, NULL, CTRL_RESET_TEXT,
280 CTRL_RESET_LONGTEXT, true )
281 add_integer( CFG_PREFIX "brightness", -1, NULL, BRIGHTNESS_TEXT,
282 BRIGHTNESS_LONGTEXT, true )
283 add_integer( CFG_PREFIX "contrast", -1, NULL, CONTRAST_TEXT,
284 CONTRAST_LONGTEXT, true )
285 add_integer( CFG_PREFIX "saturation", -1, NULL, SATURATION_TEXT,
286 SATURATION_LONGTEXT, true )
287 add_integer( CFG_PREFIX "hue", -1, NULL, HUE_TEXT,
289 add_integer( CFG_PREFIX "black-level", -1, NULL, BLACKLEVEL_TEXT,
290 BLACKLEVEL_LONGTEXT, true )
291 add_integer( CFG_PREFIX "auto-white-balance", -1, NULL,
292 AUTOWHITEBALANCE_TEXT, AUTOWHITEBALANCE_LONGTEXT, true )
293 add_integer( CFG_PREFIX "do-white-balance", -1, NULL, DOWHITEBALANCE_TEXT,
294 DOWHITEBALANCE_LONGTEXT, true )
295 add_integer( CFG_PREFIX "red-balance", -1, NULL, REDBALANCE_TEXT,
296 REDBALANCE_LONGTEXT, true )
297 add_integer( CFG_PREFIX "blue-balance", -1, NULL, BLUEBALANCE_TEXT,
298 BLUEBALANCE_LONGTEXT, true )
299 add_integer( CFG_PREFIX "gamma", -1, NULL, GAMMA_TEXT,
300 GAMMA_LONGTEXT, true )
301 add_integer( CFG_PREFIX "exposure", -1, NULL, EXPOSURE_TEXT,
302 EXPOSURE_LONGTEXT, true )
303 add_integer( CFG_PREFIX "autogain", -1, NULL, AUTOGAIN_TEXT,
304 AUTOGAIN_LONGTEXT, true )
305 add_integer( CFG_PREFIX "gain", -1, NULL, GAIN_TEXT,
306 GAIN_LONGTEXT, true )
307 add_integer( CFG_PREFIX "hflip", -1, NULL, HFLIP_TEXT,
308 HFLIP_LONGTEXT, true )
309 add_integer( CFG_PREFIX "vflip", -1, NULL, VFLIP_TEXT,
310 VFLIP_LONGTEXT, true )
311 add_integer( CFG_PREFIX "hcenter", -1, NULL, HCENTER_TEXT,
312 HCENTER_LONGTEXT, true )
313 add_integer( CFG_PREFIX "vcenter", -1, NULL, VCENTER_TEXT,
314 VCENTER_LONGTEXT, true )
315 add_integer( CFG_PREFIX "audio-volume", -1, NULL, AUDIO_VOLUME_TEXT,
316 AUDIO_VOLUME_LONGTEXT, true )
317 add_integer( CFG_PREFIX "audio-balance", -1, NULL, AUDIO_BALANCE_TEXT,
318 AUDIO_BALANCE_LONGTEXT, true )
319 add_bool( CFG_PREFIX "audio-mute", false, NULL, AUDIO_MUTE_TEXT,
320 AUDIO_MUTE_LONGTEXT, true )
321 add_integer( CFG_PREFIX "audio-bass", -1, NULL, AUDIO_BASS_TEXT,
322 AUDIO_BASS_LONGTEXT, true )
323 add_integer( CFG_PREFIX "audio-treble", -1, NULL, AUDIO_TREBLE_TEXT,
324 AUDIO_TREBLE_LONGTEXT, true )
325 add_integer( CFG_PREFIX "audio-loudness", -1, NULL, AUDIO_LOUDNESS_TEXT,
326 AUDIO_LOUDNESS_LONGTEXT, true )
327 add_string( CFG_PREFIX "set-ctrls", NULL, NULL, S_CTRLS_TEXT,
328 S_CTRLS_LONGTEXT, true )
330 add_obsolete_string( CFG_PREFIX "dev" )
332 add_obsolete_string( CFG_PREFIX "adev" )
333 add_obsolete_integer( CFG_PREFIX "audio-method" )
334 add_obsolete_bool( CFG_PREFIX "stereo" )
335 add_obsolete_integer( CFG_PREFIX "samplerate" )
337 add_shortcut( "v4l2" )
338 set_capability( "access_demux", 10 )
339 set_callbacks( DemuxOpen, DemuxClose )
342 add_shortcut( "v4l2" )
343 add_shortcut( "v4l2c" )
344 set_description( N_("Video4Linux2 Compressed A/V") )
345 set_capability( "access", 0 )
346 /* use these when open as access_demux fails; VLC will use another demux */
347 set_callbacks( AccessOpen, AccessClose )
351 /*****************************************************************************
352 * Access: local prototypes
353 *****************************************************************************/
355 static void CommonClose( vlc_object_t *, demux_sys_t * );
356 static void ParseMRL( demux_sys_t *, char *, vlc_object_t * );
357 static void GetV4L2Params( demux_sys_t *, vlc_object_t * );
358 static void SetAvailControlsByString( vlc_object_t *, demux_sys_t *, int );
360 static int DemuxControl( demux_t *, int, va_list );
361 static int AccessControl( access_t *, int, va_list );
363 static int Demux( demux_t * );
364 static block_t *AccessRead( access_t * );
366 static block_t* GrabVideo( vlc_object_t *p_demux, demux_sys_t *p_sys );
367 static block_t* ProcessVideoFrame( vlc_object_t *p_demux, uint8_t *p_frame, size_t );
369 static bool IsPixelFormatSupported( demux_t *p_demux,
370 unsigned int i_pixelformat );
372 static int OpenVideoDev( vlc_object_t *, demux_sys_t *, bool );
373 static bool ProbeVideoDev( vlc_object_t *, demux_sys_t *,
374 const char *psz_device );
376 static int ControlList( vlc_object_t *, demux_sys_t *, int , bool, bool );
377 static int Control( vlc_object_t *, demux_sys_t *, int i_fd,
378 const char *psz_name, int i_cid, int i_value );
380 static int DemuxControlCallback( vlc_object_t *p_this, const char *psz_var,
381 vlc_value_t oldval, vlc_value_t newval,
383 static int DemuxControlResetCallback( vlc_object_t *p_this, const char *psz_var,
384 vlc_value_t oldval, vlc_value_t newval,
386 static int AccessControlCallback( vlc_object_t *p_this, const char *psz_var,
387 vlc_value_t oldval, vlc_value_t newval,
389 static int AccessControlResetCallback( vlc_object_t *p_this,
390 const char *psz_var, vlc_value_t oldval,
391 vlc_value_t newval, void *p_data );
396 vlc_fourcc_t i_fourcc;
400 } v4l2chroma_to_fourcc[] =
403 { V4L2_PIX_FMT_GREY, VLC_CODEC_GREY, 0, 0, 0 },
404 { V4L2_PIX_FMT_HI240, VLC_FOURCC('I','2','4','0'), 0, 0, 0 },
405 { V4L2_PIX_FMT_RGB555, VLC_CODEC_RGB15, 0x001f,0x03e0,0x7c00 },
406 { V4L2_PIX_FMT_RGB565, VLC_CODEC_RGB16, 0x001f,0x07e0,0xf800 },
407 /* Won't work since we don't know how to handle such gmask values
409 { V4L2_PIX_FMT_RGB555X, VLC_CODEC_RGB15, 0x007c,0xe003,0x1f00 },
410 { V4L2_PIX_FMT_RGB565X, VLC_CODEC_RGB16, 0x00f8,0xe007,0x1f00 },
412 { V4L2_PIX_FMT_BGR24, VLC_CODEC_RGB24, 0xff0000,0xff00,0xff },
413 { V4L2_PIX_FMT_RGB24, VLC_CODEC_RGB24, 0xff,0xff00,0xff0000 },
414 { V4L2_PIX_FMT_BGR32, VLC_CODEC_RGB32, 0xff0000,0xff00,0xff },
415 { V4L2_PIX_FMT_RGB32, VLC_CODEC_RGB32, 0xff,0xff00,0xff0000 },
416 { V4L2_PIX_FMT_YUYV, VLC_CODEC_YUYV, 0, 0, 0 },
417 { V4L2_PIX_FMT_UYVY, VLC_CODEC_UYVY, 0, 0, 0 },
418 { V4L2_PIX_FMT_Y41P, VLC_FOURCC('I','4','1','N'), 0, 0, 0 },
419 { V4L2_PIX_FMT_YUV422P, VLC_CODEC_I422, 0, 0, 0 },
420 { V4L2_PIX_FMT_YVU420, VLC_CODEC_YV12, 0, 0, 0 },
421 { V4L2_PIX_FMT_YUV411P, VLC_CODEC_I411, 0, 0, 0 },
422 { V4L2_PIX_FMT_YUV410, VLC_CODEC_I410, 0, 0, 0 },
424 /* Raw data types, not in V4L2 spec but still in videodev2.h and supported
426 { V4L2_PIX_FMT_YUV420, VLC_CODEC_I420, 0, 0, 0 },
427 /* FIXME { V4L2_PIX_FMT_RGB444, VLC_CODEC_RGB32 }, */
429 /* Compressed data types */
430 { V4L2_PIX_FMT_MJPEG, VLC_CODEC_MJPG, 0, 0, 0 },
431 { V4L2_PIX_FMT_JPEG, VLC_CODEC_JPEG, 0, 0, 0 },
433 { V4L2_PIX_FMT_DV, VLC_FOURCC('?','?','?','?') },
434 { V4L2_PIX_FMT_MPEG, VLC_FOURCC('?','?','?','?') },
440 * List of V4L2 chromas were confident enough to use as fallbacks if the
441 * user hasn't provided a --v4l2-chroma value.
443 * Try YUV chromas first, then RGB little endian and MJPEG as last resort.
445 static const __u32 p_chroma_fallbacks[] =
446 { V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_YVU420, V4L2_PIX_FMT_YUV422P,
447 V4L2_PIX_FMT_YUYV, V4L2_PIX_FMT_UYVY, V4L2_PIX_FMT_BGR24,
448 V4L2_PIX_FMT_BGR32, V4L2_PIX_FMT_MJPEG, V4L2_PIX_FMT_JPEG };
452 const char *psz_name;
456 { "brightness", V4L2_CID_BRIGHTNESS },
457 { "contrast", V4L2_CID_CONTRAST },
458 { "saturation", V4L2_CID_SATURATION },
459 { "hue", V4L2_CID_HUE },
460 { "audio-volume", V4L2_CID_AUDIO_VOLUME },
461 { "audio-balance", V4L2_CID_AUDIO_BALANCE },
462 { "audio-bass", V4L2_CID_AUDIO_BASS },
463 { "audio-treble", V4L2_CID_AUDIO_TREBLE },
464 { "audio-mute", V4L2_CID_AUDIO_MUTE },
465 { "audio-loudness", V4L2_CID_AUDIO_LOUDNESS },
466 { "black-level", V4L2_CID_BLACK_LEVEL },
467 { "auto-white-balance", V4L2_CID_AUTO_WHITE_BALANCE },
468 { "do-white-balance", V4L2_CID_DO_WHITE_BALANCE },
469 { "red-balance", V4L2_CID_RED_BALANCE },
470 { "blue-balance", V4L2_CID_BLUE_BALANCE },
471 { "gamma", V4L2_CID_GAMMA },
472 { "exposure", V4L2_CID_EXPOSURE },
473 { "autogain", V4L2_CID_AUTOGAIN },
474 { "gain", V4L2_CID_GAIN },
475 { "hflip", V4L2_CID_HFLIP },
476 { "vflip", V4L2_CID_VFLIP },
477 { "hcenter", V4L2_CID_HCENTER },
478 { "vcenter", V4L2_CID_VCENTER },
490 char *psz_device; /* Main device from MRL */
493 char *psz_requested_chroma;
500 struct v4l2_capability dev_cap;
503 struct v4l2_input *p_inputs;
504 int i_selected_input;
507 struct v4l2_standard *p_standards;
508 v4l2_std_id i_selected_standard_id;
511 /* V4L2 devices cannot have more than 32 audio inputs */
512 struct v4l2_audio p_audios[32];
513 int i_selected_audio_input;
516 struct v4l2_tuner *p_tuners;
519 struct v4l2_fmtdesc *p_codecs;
521 struct buffer_t *p_buffers;
522 unsigned int i_nbuffers;
526 float f_fps; /* <= 0.0 mean to grab at full rate */
527 mtime_t i_video_pts; /* only used when f_fps > 0 */
543 int (*pf_close)( int );
544 int (*pf_dup)( int );
545 int (*pf_ioctl)( int, unsigned long int, ... );
546 ssize_t (*pf_read)( int, void *, size_t );
547 void *(*pf_mmap)( void *, size_t, int, int, int, off_t );
548 int (*pf_munmap)( void *, size_t );
554 static void use_kernel_v4l2( demux_sys_t *p_sys )
556 p_sys->pf_close = close;
558 p_sys->pf_ioctl = ioctl;
559 p_sys->pf_read = read;
560 p_sys->pf_mmap = mmap;
561 p_sys->pf_munmap = munmap;
562 p_sys->b_libv4l2 = false;
565 static void use_libv4l2( demux_sys_t *p_sys )
567 p_sys->pf_close = v4l2_close;
568 p_sys->pf_dup = v4l2_dup;
569 p_sys->pf_ioctl = v4l2_ioctl;
570 p_sys->pf_read = v4l2_read;
571 p_sys->pf_mmap = v4l2_mmap;
572 p_sys->pf_munmap = v4l2_munmap;
573 p_sys->b_libv4l2 = true;
576 # define v4l2_close (p_sys->pf_close)
577 # define v4l2_dup (p_sys->pf_dup)
578 # define v4l2_ioctl (p_sys->pf_ioctl)
579 # define v4l2_read (p_sys->pf_read)
580 # define v4l2_mmap (p_sys->pf_mmap)
581 # define v4l2_munmap (p_sys->pf_munmap)
583 # define v4l2_close close
584 # define v4l2_dup dup
585 # define v4l2_ioctl ioctl
586 # define v4l2_read read
587 # define v4l2_mmap mmap
588 # define v4l2_munmap munmap
591 static int FindMainDevice( vlc_object_t *p_this, demux_sys_t *p_sys,
594 /* TODO: if using default device, loop through all /dev/video* until
596 msg_Dbg( p_this, "opening device '%s'", p_sys->psz_device );
597 if( ProbeVideoDev( p_this, p_sys, p_sys->psz_device ) )
599 msg_Dbg( p_this, "'%s' is a video device", p_sys->psz_device );
600 p_sys->i_fd = OpenVideoDev( p_this, p_sys, b_demux );
603 if( p_sys->i_fd < 0 ) return VLC_EGENERIC;
607 /*****************************************************************************
608 * DemuxOpen: opens v4l2 device, access_demux callback
609 *****************************************************************************
611 * url: <video device>::::
613 *****************************************************************************/
614 static int DemuxOpen( vlc_object_t *p_this )
616 demux_t *p_demux = (demux_t*)p_this;
619 /* Only when selected */
620 if( strcmp( p_demux->psz_access, "v4l2" ) )
624 p_demux->pf_control = DemuxControl;
625 p_demux->pf_demux = Demux;
626 p_demux->info.i_update = 0;
627 p_demux->info.i_title = 0;
628 p_demux->info.i_seekpoint = 0;
630 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
631 if( p_sys == NULL ) return VLC_ENOMEM;
633 GetV4L2Params(p_sys, (vlc_object_t *) p_demux);
635 ParseMRL( p_sys, p_demux->psz_path, (vlc_object_t *) p_demux );
638 if( !config_GetInt( p_this, CFG_PREFIX "use-libv4l2" ) )
640 msg_Dbg( p_this, "Trying direct kernel v4l2" );
641 use_kernel_v4l2( p_sys );
642 if( FindMainDevice( p_this, p_sys, true ) == VLC_SUCCESS)
646 msg_Dbg( p_this, "Trying libv4l2 wrapper" );
647 use_libv4l2( p_sys );
649 if( FindMainDevice( p_this, p_sys, true ) == VLC_SUCCESS)
652 DemuxClose( p_this );
656 /*****************************************************************************
657 * GetV4L2Params: fill in p_sys parameters (shared by DemuxOpen and AccessOpen)
658 *****************************************************************************/
659 static void GetV4L2Params( demux_sys_t *p_sys, vlc_object_t *p_obj )
661 p_sys->i_video_pts = -1;
663 p_sys->i_selected_standard_id =
664 i_standards_list[var_CreateGetInteger( p_obj, "v4l2-standard" )];
666 p_sys->i_selected_input = var_CreateGetInteger( p_obj, "v4l2-input" );
667 p_sys->i_selected_audio_input =
668 var_CreateGetInteger( p_obj, "v4l2-audio-input" );
670 p_sys->io = var_CreateGetInteger( p_obj, "v4l2-io" );
672 p_sys->i_width = var_CreateGetInteger( p_obj, "v4l2-width" );
673 p_sys->i_height = var_CreateGetInteger( p_obj, "v4l2-height" );
675 var_Create( p_obj, "v4l2-controls-reset", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
677 p_sys->f_fps = var_CreateGetFloat( p_obj, "v4l2-fps" );
678 p_sys->psz_requested_chroma = var_CreateGetString( p_obj, "v4l2-chroma" );
680 p_sys->i_cache = var_CreateGetInteger( p_obj, "v4l2-caching" );
682 p_sys->i_cur_tuner = var_CreateGetInteger( p_obj, "v4l2-tuner" );
683 p_sys->i_frequency = var_CreateGetInteger( p_obj, "v4l2-tuner-frequency" );
684 p_sys->i_audio_mode = var_CreateGetInteger( p_obj, "v4l2-tuner-audio-mode" );
686 p_sys->psz_set_ctrls = var_CreateGetString( p_obj, "v4l2-set-ctrls" );
688 p_sys->psz_device = NULL;
694 /*****************************************************************************
695 * ParseMRL: parse the options contained in the MRL
696 *****************************************************************************/
697 static void ParseMRL( demux_sys_t *p_sys, char *psz_path, vlc_object_t *p_obj )
699 char *psz_dup = strdup( psz_path );
700 char *psz_parser = psz_dup;
702 while( *psz_parser && *psz_parser != ':' )
707 if( *psz_parser == ':' )
712 *psz_parser++ = '\0';
714 if( !strncmp( psz_parser, "standard=", strlen( "standard=" ) ) )
716 psz_parser += strlen( "standard=" );
717 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
719 p_sys->i_selected_standard_id = V4L2_STD_PAL;
720 psz_parser += strlen( "pal" );
722 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
724 p_sys->i_selected_standard_id = V4L2_STD_NTSC;
725 psz_parser += strlen( "ntsc" );
727 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
729 p_sys->i_selected_standard_id = V4L2_STD_SECAM;
730 psz_parser += strlen( "secam" );
732 else if( !strncmp( psz_parser, "default", strlen( "default" ) ) )
734 p_sys->i_selected_standard_id = V4L2_STD_UNKNOWN;
735 psz_parser += strlen( "default" );
739 p_sys->i_selected_standard_id = i_standards_list[strtol( psz_parser, &psz_parser, 0 )];
742 else if( !strncmp( psz_parser, "chroma=", strlen( "chroma=" ) ) )
746 psz_parser += strlen( "chroma=" );
747 if( strchr( psz_parser, ':' ) )
749 i_len = strchr( psz_parser, ':' ) - psz_parser;
753 i_len = strlen( psz_parser );
756 free( p_sys->psz_requested_chroma );
757 p_sys->psz_requested_chroma = strndup( psz_parser, i_len );
761 else if( !strncmp( psz_parser, "input=", strlen( "input=" ) ) )
763 p_sys->i_selected_input = strtol( psz_parser + strlen( "input=" ),
766 else if( !strncmp( psz_parser, "audio-input=", strlen( "audio-input=" ) ) )
768 p_sys->i_selected_audio_input = strtol( psz_parser + strlen( "audio-input=" ),
771 else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
773 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
776 else if( !strncmp( psz_parser, "io=", strlen( "io=" ) ) )
778 psz_parser += strlen( "io=" );
779 if( !strncmp( psz_parser, "read", strlen( "read" ) ) )
781 p_sys->io = IO_METHOD_READ;
782 psz_parser += strlen( "read" );
784 else if( !strncmp( psz_parser, "mmap", strlen( "mmap" ) ) )
786 p_sys->io = IO_METHOD_MMAP;
787 psz_parser += strlen( "mmap" );
789 else if( !strncmp( psz_parser, "userptr", strlen( "userptr" ) ) )
791 p_sys->io = IO_METHOD_USERPTR;
792 psz_parser += strlen( "userptr" );
796 p_sys->io = strtol( psz_parser, &psz_parser, 0 );
799 else if( !strncmp( psz_parser, "width=",
800 strlen( "width=" ) ) )
803 strtol( psz_parser + strlen( "width=" ),
806 else if( !strncmp( psz_parser, "height=",
807 strlen( "height=" ) ) )
810 strtol( psz_parser + strlen( "height=" ),
813 else if( !strncmp( psz_parser, "controls-reset",
814 strlen( "controls-reset" ) ) )
816 var_SetBool( p_obj, "v4l2-controls-reset", true );
817 psz_parser += strlen( "controls-reset" );
820 else if( !strncmp( psz_parser, "brightness=",
821 strlen( "brightness=" ) ) )
823 var_SetInteger( p_obj, "brightness",
824 strtol( psz_parser + strlen( "brightness=" ),
827 else if( !strncmp( psz_parser, "contrast=",
828 strlen( "contrast=" ) ) )
830 var_SetInteger( p_obj, "contrast",
831 strtol( psz_parser + strlen( "contrast=" ),
834 else if( !strncmp( psz_parser, "saturation=",
835 strlen( "saturation=" ) ) )
837 var_SetInteger( p_obj, "saturation",
838 strtol( psz_parser + strlen( "saturation=" ),
841 else if( !strncmp( psz_parser, "hue=",
844 var_SetInteger( p_obj, "hue",
845 strtol( psz_parser + strlen( "hue=" ),
848 else if( !strncmp( psz_parser, "gamma=",
849 strlen( "gamma=" ) ) )
851 var_SetInteger( p_obj, "gamma",
852 strtol( psz_parser + strlen( "gamma=" ),
856 else if( !strncmp( psz_parser, "caching=", strlen( "caching=" ) ) )
858 p_sys->i_cache = strtol( psz_parser + strlen( "caching=" ),
861 else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
863 p_sys->i_cur_tuner = strtol( psz_parser + strlen( "tuner=" ),
866 else if( !strncmp( psz_parser, "tuner-frequency=", strlen( "tuner-frequency=" ) ) )
868 p_sys->i_frequency = strtol( psz_parser
869 + strlen( "tuner-frequency=" ),
872 else if( !strncmp( psz_parser, "tuner-audio-mode=", strlen( "tuner-audio-mode=" ) ) )
874 p_sys->i_audio_mode = strtol( psz_parser
875 + strlen( "tuner-audio-mode=" ),
878 else if( !strncmp( psz_parser, "set-ctrls=", strlen( "set-ctrls=" )) )
882 psz_parser += strlen( "set-ctrls=" );
883 if( strchr( psz_parser, ':' ) )
885 i_len = strchr( psz_parser, ':' ) - psz_parser;
889 i_len = strlen( psz_parser );
892 p_sys->psz_set_ctrls = strndup( psz_parser, i_len );
896 else if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) )
897 || !strncmp( psz_parser, "samplerate=", strlen( "samplerate=" ) )
898 || !strncmp( psz_parser, "audio-method", strlen( "audio-method" ) )
899 || !strncmp( psz_parser, "stereo", strlen( "stereo" ) )
900 || !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
902 if( strchr( psz_parser, ':' ) )
904 psz_parser = strchr( psz_parser, ':' );
908 psz_parser += strlen( psz_parser );
911 msg_Err( p_obj, AUDIO_DEPRECATED_ERROR );
915 char *psz_unk = strchr( psz_parser, ':' );
917 psz_unk = strndup( psz_parser, psz_unk - psz_parser );
919 psz_unk = strdup( psz_parser);
920 msg_Warn( p_obj, "unknown option %s", psz_unk );
924 while( *psz_parser && *psz_parser != ':' )
929 if( *psz_parser == '\0' )
938 p_sys->psz_device = strdup( psz_dup );
940 p_sys->psz_device = strdup( V4L2_DEFAULT );
944 /*****************************************************************************
945 * Close: close device, free resources
946 *****************************************************************************/
947 static void AccessClose( vlc_object_t *p_this )
949 access_t *p_access = (access_t *)p_this;
950 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
952 CommonClose( p_this, p_sys );
955 static void DemuxClose( vlc_object_t *p_this )
957 struct v4l2_buffer buf;
958 enum v4l2_buf_type buf_type;
961 demux_t *p_demux = (demux_t *)p_this;
962 demux_sys_t *p_sys = p_demux->p_sys;
964 /* Stop video capture */
965 if( p_sys->i_fd >= 0 )
974 case IO_METHOD_USERPTR:
975 /* Some drivers 'hang' internally if this is not done before streamoff */
976 for( unsigned int i = 0; i < p_sys->i_nbuffers; i++ )
978 memset( &buf, 0, sizeof(buf) );
979 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
980 buf.memory = ( p_sys->io == IO_METHOD_USERPTR ) ?
981 V4L2_MEMORY_USERPTR : V4L2_MEMORY_MMAP;
982 v4l2_ioctl( p_sys->i_fd, VIDIOC_DQBUF, &buf ); /* ignore result */
985 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
986 if( v4l2_ioctl( p_sys->i_fd, VIDIOC_STREAMOFF, &buf_type ) < 0 ) {
987 msg_Err( p_this, "VIDIOC_STREAMOFF failed" );
994 /* Free Video Buffers */
995 if( p_sys->p_buffers ) {
999 free( p_sys->p_buffers[0].start );
1002 case IO_METHOD_MMAP:
1003 for( i = 0; i < p_sys->i_nbuffers; ++i )
1005 if( v4l2_munmap( p_sys->p_buffers[i].start, p_sys->p_buffers[i].length ) )
1007 msg_Err( p_this, "munmap failed" );
1012 case IO_METHOD_USERPTR:
1013 for( i = 0; i < p_sys->i_nbuffers; ++i )
1015 free( p_sys->p_buffers[i].start );
1019 free( p_sys->p_buffers );
1022 CommonClose( p_this, p_sys );
1025 static void CommonClose( vlc_object_t *p_this, demux_sys_t *p_sys )
1029 if( p_sys->i_fd >= 0 ) v4l2_close( p_sys->i_fd );
1030 free( p_sys->psz_device );
1031 free( p_sys->p_standards );
1032 free( p_sys->p_inputs );
1033 free( p_sys->p_tuners );
1034 free( p_sys->p_codecs );
1035 free( p_sys->psz_requested_chroma );
1036 free( p_sys->psz_set_ctrls );
1041 /*****************************************************************************
1042 * AccessOpen: opens v4l2 device, access callback
1043 *****************************************************************************
1045 * url: <video device>::::
1047 *****************************************************************************/
1048 static int AccessOpen( vlc_object_t * p_this )
1050 access_t *p_access = (access_t*) p_this;
1051 demux_sys_t * p_sys;
1053 /* Only when selected */
1054 if( *p_access->psz_access == '\0' ) return VLC_EGENERIC;
1056 access_InitFields( p_access );
1057 ACCESS_SET_CALLBACKS( NULL, AccessRead, AccessControl, NULL );
1058 p_sys = calloc( 1, sizeof( demux_sys_t ));
1059 if( !p_sys ) return VLC_ENOMEM;
1060 p_access->p_sys = (access_sys_t*)p_sys;
1062 GetV4L2Params( p_sys, (vlc_object_t *) p_access );
1064 ParseMRL( p_sys, p_access->psz_path, (vlc_object_t *) p_access );
1067 if( !config_GetInt( p_this, CFG_PREFIX "use-libv4l2" ) )
1069 msg_Dbg( p_this, "Trying direct kernel v4l2" );
1070 use_kernel_v4l2( p_sys );
1071 if( FindMainDevice( p_this, p_sys, false ) == VLC_SUCCESS)
1075 msg_Dbg( p_this, "Trying libv4l2 wrapper" );
1076 use_libv4l2( p_sys );
1078 if( FindMainDevice( p_this, p_sys, false ) == VLC_SUCCESS )
1081 AccessClose( p_this );
1082 return VLC_EGENERIC;
1085 /*****************************************************************************
1087 *****************************************************************************/
1088 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
1092 /* Special for access_demux */
1093 case DEMUX_CAN_PAUSE:
1094 case DEMUX_CAN_SEEK:
1095 case DEMUX_SET_PAUSE_STATE:
1096 case DEMUX_CAN_CONTROL_PACE:
1097 *va_arg( args, bool * ) = false;
1100 case DEMUX_GET_PTS_DELAY:
1101 *va_arg(args,int64_t *) = (int64_t)p_demux->p_sys->i_cache*1000;
1104 case DEMUX_GET_TIME:
1105 *va_arg( args, int64_t * ) = mdate();
1108 /* TODO implement others */
1110 return VLC_EGENERIC;
1113 return VLC_EGENERIC;
1116 /*****************************************************************************
1117 * AccessControl: access callback
1118 *****************************************************************************/
1119 static int AccessControl( access_t *p_access, int i_query, va_list args )
1121 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1126 case ACCESS_CAN_SEEK:
1127 case ACCESS_CAN_FASTSEEK:
1128 case ACCESS_CAN_PAUSE:
1129 case ACCESS_CAN_CONTROL_PACE:
1130 *va_arg( args, bool* ) = false;
1134 case ACCESS_GET_PTS_DELAY:
1135 *va_arg(args,int64_t *) = (int64_t)p_sys->i_cache*1000;
1139 case ACCESS_SET_PAUSE_STATE:
1143 case ACCESS_GET_TITLE_INFO:
1144 case ACCESS_SET_TITLE:
1145 case ACCESS_SET_SEEKPOINT:
1146 case ACCESS_SET_PRIVATE_ID_STATE:
1147 case ACCESS_GET_CONTENT_TYPE:
1148 case ACCESS_GET_META:
1149 return VLC_EGENERIC;
1152 msg_Warn( p_access, "Unimplemented query in control(%d).", i_query);
1153 return VLC_EGENERIC;
1159 /*****************************************************************************
1160 * AccessRead: access callback
1161 ******************************************************************************/
1162 static block_t *AccessRead( access_t * p_access )
1164 demux_sys_t *p_sys = (demux_sys_t *)p_access->p_sys;
1167 fd.fd = p_sys->i_fd;
1168 fd.events = POLLIN|POLLPRI;
1172 if( poll( &fd, 1, 500 ) ) /* Timeout after 0.5 seconds since I don't know if pf_demux can be blocking. */
1174 if( fd.revents & (POLLIN|POLLPRI) )
1176 return GrabVideo( VLC_OBJECT(p_access), p_sys );
1183 /*****************************************************************************
1184 * Demux: Processes the audio or video frame
1185 *****************************************************************************/
1186 static int Demux( demux_t *p_demux )
1188 demux_sys_t *p_sys = p_demux->p_sys;
1191 fd.fd = p_sys->i_fd;
1192 fd.events = POLLIN|POLLPRI;
1196 if( poll( &fd, 1, 500 ) ) /* Timeout after 0.5 seconds since I don't know if pf_demux can be blocking. */
1198 if( fd.revents & (POLLIN|POLLPRI) )
1200 block_t *p_block = GrabVideo( VLC_OBJECT(p_demux), p_sys );
1203 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
1204 es_out_Send( p_demux->out, p_sys->p_es, p_block );
1212 /*****************************************************************************
1213 * GrabVideo: Grab a video frame
1214 *****************************************************************************/
1215 static block_t* GrabVideo( vlc_object_t *p_demux, demux_sys_t *p_sys )
1217 block_t *p_block = NULL;
1218 struct v4l2_buffer buf;
1221 if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
1223 mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
1225 /* Did we wait long enough ? (frame rate reduction) */
1226 if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
1229 /* Grab Video Frame */
1232 case IO_METHOD_READ:
1233 i_ret = v4l2_read( p_sys->i_fd, p_sys->p_buffers[0].start, p_sys->p_buffers[0].length );
1241 /* Could ignore EIO, see spec. */
1244 msg_Err( p_demux, "Failed to read frame" );
1249 p_block = ProcessVideoFrame( p_demux, (uint8_t*)p_sys->p_buffers[0].start, i_ret );
1250 if( !p_block ) return 0;
1254 case IO_METHOD_MMAP:
1255 memset( &buf, 0, sizeof(buf) );
1256 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1257 buf.memory = V4L2_MEMORY_MMAP;
1259 /* Wait for next frame */
1260 if (v4l2_ioctl( p_sys->i_fd, VIDIOC_DQBUF, &buf ) < 0 )
1267 /* Could ignore EIO, see spec. */
1270 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
1275 if( buf.index >= p_sys->i_nbuffers ) {
1276 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
1280 p_block = ProcessVideoFrame( p_demux, p_sys->p_buffers[buf.index].start, buf.bytesused );
1281 if( !p_block ) return 0;
1284 if( v4l2_ioctl( p_sys->i_fd, VIDIOC_QBUF, &buf ) < 0 )
1286 msg_Err( p_demux, "Failed to unlock (VIDIOC_QBUF)" );
1287 block_Release( p_block );
1293 case IO_METHOD_USERPTR:
1294 memset( &buf, 0, sizeof(buf) );
1295 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1296 buf.memory = V4L2_MEMORY_USERPTR;
1298 /* Wait for next frame */
1299 if (v4l2_ioctl( p_sys->i_fd, VIDIOC_DQBUF, &buf ) < 0 )
1306 /* Could ignore EIO, see spec. */
1309 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
1316 for( i = 0; i < p_sys->i_nbuffers; i++ )
1318 if( buf.m.userptr == (unsigned long)p_sys->p_buffers[i].start &&
1319 buf.length == p_sys->p_buffers[i].length ) break;
1322 if( i >= p_sys->i_nbuffers )
1324 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
1328 p_block = ProcessVideoFrame( p_demux, (uint8_t*)buf.m.userptr, buf.bytesused );
1329 if( !p_block ) return 0;
1332 if( v4l2_ioctl( p_sys->i_fd, VIDIOC_QBUF, &buf ) < 0 )
1334 msg_Err( p_demux, "Failed to unlock (VIDIOC_QBUF)" );
1335 block_Release( p_block );
1344 p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();
1349 /*****************************************************************************
1350 * ProcessVideoFrame: Helper function to take a buffer and copy it into
1352 *****************************************************************************/
1353 static block_t* ProcessVideoFrame( vlc_object_t *p_demux, uint8_t *p_frame, size_t i_size )
1357 if( !p_frame ) return 0;
1360 if( !( p_block = block_New( p_demux, i_size ) ) )
1362 msg_Warn( p_demux, "Cannot get new block" );
1367 memcpy( p_block->p_buffer, p_frame, i_size );
1372 /*****************************************************************************
1373 * Helper function to initalise video IO using the Read method
1374 *****************************************************************************/
1375 static int InitRead( vlc_object_t *p_demux, demux_sys_t *p_sys, unsigned int i_buffer_size )
1379 p_sys->p_buffers = calloc( 1, sizeof( *p_sys->p_buffers ) );
1380 if( !p_sys->p_buffers )
1381 return VLC_EGENERIC;
1383 p_sys->p_buffers[0].length = i_buffer_size;
1384 p_sys->p_buffers[0].start = malloc( i_buffer_size );
1385 if( !p_sys->p_buffers[0].start )
1391 /*****************************************************************************
1392 * Helper function to initalise video IO using the mmap method
1393 *****************************************************************************/
1394 static int InitMmap( vlc_object_t *p_demux, demux_sys_t *p_sys, int i_fd )
1396 struct v4l2_requestbuffers req;
1398 memset( &req, 0, sizeof(req) );
1400 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1401 req.memory = V4L2_MEMORY_MMAP;
1403 if( v4l2_ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1405 msg_Err( p_demux, "device does not support mmap i/o" );
1411 msg_Err( p_demux, "Insufficient buffer memory" );
1415 p_sys->p_buffers = calloc( req.count, sizeof( *p_sys->p_buffers ) );
1416 if( !p_sys->p_buffers )
1419 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < req.count; ++p_sys->i_nbuffers )
1421 struct v4l2_buffer buf;
1423 memset( &buf, 0, sizeof(buf) );
1424 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1425 buf.memory = V4L2_MEMORY_MMAP;
1426 buf.index = p_sys->i_nbuffers;
1428 if( v4l2_ioctl( i_fd, VIDIOC_QUERYBUF, &buf ) < 0 )
1430 msg_Err( p_demux, "VIDIOC_QUERYBUF" );
1434 p_sys->p_buffers[p_sys->i_nbuffers].length = buf.length;
1435 p_sys->p_buffers[p_sys->i_nbuffers].start =
1436 v4l2_mmap( NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, buf.m.offset );
1438 if( p_sys->p_buffers[p_sys->i_nbuffers].start == MAP_FAILED )
1440 msg_Err( p_demux, "mmap failed (%m)" );
1448 return VLC_EGENERIC;
1452 /*****************************************************************************
1453 * Helper function to initalise video IO using the userbuf method
1454 *****************************************************************************/
1455 static int InitUserP( vlc_object_t *p_demux, demux_sys_t *p_sys, int i_fd, unsigned int i_buffer_size )
1457 struct v4l2_requestbuffers req;
1458 unsigned int i_page_size;
1460 i_page_size = sysconf(_SC_PAGESIZE);
1461 i_buffer_size = ( i_buffer_size + i_page_size - 1 ) & ~( i_page_size - 1);
1463 memset( &req, 0, sizeof(req) );
1465 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1466 req.memory = V4L2_MEMORY_USERPTR;
1468 if( v4l2_ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1470 msg_Err( p_demux, "device does not support user pointer i/o" );
1471 return VLC_EGENERIC;
1474 p_sys->p_buffers = calloc( 4, sizeof( *p_sys->p_buffers ) );
1475 if( !p_sys->p_buffers )
1478 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < 4; ++p_sys->i_nbuffers )
1480 p_sys->p_buffers[p_sys->i_nbuffers].length = i_buffer_size;
1481 if( posix_memalign( &p_sys->p_buffers[p_sys->i_nbuffers].start,
1482 /* boundary */ i_page_size, i_buffer_size ) )
1489 free( p_sys->p_buffers );
1490 return VLC_EGENERIC;
1494 /*****************************************************************************
1495 * IsPixelFormatSupported: returns true if the specified V4L2 pixel format is
1496 * in the array of supported formats returned by the driver
1497 *****************************************************************************/
1498 static bool IsPixelFormatSupported( demux_t *p_demux, unsigned int i_pixelformat )
1500 demux_sys_t *p_sys = p_demux->p_sys;
1502 for( int i_index = 0; i_index < p_sys->i_codec; i_index++ )
1504 if( p_sys->p_codecs[i_index].pixelformat == i_pixelformat )
1511 static float GetMaxFrameRate( demux_t *p_demux, int i_fd,
1512 uint32_t i_pixel_format,
1513 uint32_t i_width, uint32_t i_height )
1516 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1518 demux_sys_t *p_sys = p_demux->p_sys;
1520 /* This is new in Linux 2.6.19 */
1521 struct v4l2_frmivalenum frmival;
1522 memset( &frmival, 0, sizeof(frmival) );
1523 frmival.pixel_format = i_pixel_format;
1524 frmival.width = i_width;
1525 frmival.height = i_height;
1526 if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 )
1528 switch( frmival.type )
1530 case V4L2_FRMIVAL_TYPE_DISCRETE:
1532 float f_fps_max = -1;
1535 float f_fps = (float)frmival.discrete.denominator
1536 / (float)frmival.discrete.numerator;
1537 if( f_fps > f_fps_max ) f_fps_max = f_fps;
1539 } while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS,
1543 case V4L2_FRMSIZE_TYPE_STEPWISE:
1544 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
1545 return __MAX( (float)frmival.stepwise.max.denominator
1546 / (float)frmival.stepwise.max.numerator,
1547 (float)frmival.stepwise.min.denominator
1548 / (float)frmival.stepwise.min.numerator );
1555 static float GetAbsoluteMaxFrameRate( demux_t *p_demux, int i_fd,
1556 uint32_t i_pixel_format )
1558 float f_fps_max = -1.;
1559 #ifdef VIDIOC_ENUM_FRAMESIZES
1561 demux_sys_t *p_sys = p_demux->p_sys;
1563 /* This is new in Linux 2.6.19 */
1564 struct v4l2_frmsizeenum frmsize;
1565 memset( &frmsize, 0, sizeof(frmsize) );
1566 frmsize.pixel_format = i_pixel_format;
1567 if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) >= 0 )
1569 switch( frmsize.type )
1571 case V4L2_FRMSIZE_TYPE_DISCRETE:
1575 float f_fps = GetMaxFrameRate( p_demux, i_fd,
1577 frmsize.discrete.width,
1578 frmsize.discrete.height );
1579 if( f_fps > f_fps_max ) f_fps_max = f_fps;
1580 } while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES,
1583 case V4L2_FRMSIZE_TYPE_STEPWISE:
1585 uint32_t i_width = frmsize.stepwise.min_width;
1586 uint32_t i_height = frmsize.stepwise.min_height;
1588 i_width <= frmsize.stepwise.max_width &&
1589 i_height <= frmsize.stepwise.max_width;
1590 i_width += frmsize.stepwise.step_width,
1591 i_height += frmsize.stepwise.step_height )
1593 float f_fps = GetMaxFrameRate( p_demux, i_fd,
1595 i_width, i_height );
1596 if( f_fps > f_fps_max ) f_fps_max = f_fps;
1600 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
1602 msg_Err( p_demux, "GetAbsoluteMaxFrameRate implementation for V4L2_FRMSIZE_TYPE_CONTINUOUS isn't correct" );
1603 f_fps_max = GetMaxFrameRate( p_demux, i_fd,
1605 frmsize.stepwise.max_width,
1606 frmsize.stepwise.max_height );
1614 static void GetMaxDimensions( demux_t *p_demux, int i_fd,
1615 uint32_t i_pixel_format, float f_fps_min,
1616 uint32_t *pi_width, uint32_t *pi_height )
1620 #ifdef VIDIOC_ENUM_FRAMESIZES
1622 demux_sys_t *p_sys = p_demux->p_sys;
1624 /* This is new in Linux 2.6.19 */
1625 struct v4l2_frmsizeenum frmsize;
1626 memset( &frmsize, 0, sizeof(frmsize) );
1627 frmsize.pixel_format = i_pixel_format;
1628 if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) >= 0 )
1630 switch( frmsize.type )
1632 case V4L2_FRMSIZE_TYPE_DISCRETE:
1636 float f_fps = GetMaxFrameRate( p_demux, i_fd,
1638 frmsize.discrete.width,
1639 frmsize.discrete.height );
1640 if( f_fps >= f_fps_min &&
1641 frmsize.discrete.width > *pi_width )
1643 *pi_width = frmsize.discrete.width;
1644 *pi_height = frmsize.discrete.height;
1646 } while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES,
1649 case V4L2_FRMSIZE_TYPE_STEPWISE:
1651 uint32_t i_width = frmsize.stepwise.min_width;
1652 uint32_t i_height = frmsize.stepwise.min_height;
1654 i_width <= frmsize.stepwise.max_width &&
1655 i_height <= frmsize.stepwise.max_width;
1656 i_width += frmsize.stepwise.step_width,
1657 i_height += frmsize.stepwise.step_height )
1659 float f_fps = GetMaxFrameRate( p_demux, i_fd,
1661 i_width, i_height );
1662 if( f_fps >= f_fps_min && i_width > *pi_width )
1664 *pi_width = i_width;
1665 *pi_height = i_height;
1670 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
1672 msg_Err( p_demux, "GetMaxDimension implementation for V4L2_FRMSIZE_TYPE_CONTINUOUS isn't correct" );
1673 float f_fps = GetMaxFrameRate( p_demux, i_fd,
1675 frmsize.stepwise.max_width,
1676 frmsize.stepwise.max_height );
1677 if( f_fps >= f_fps_min &&
1678 frmsize.stepwise.max_width > *pi_width )
1680 *pi_width = frmsize.stepwise.max_width;
1681 *pi_height = frmsize.stepwise.max_height;
1689 /*****************************************************************************
1690 * OpenVideoDev: open and set up the video device and probe for capabilities
1691 *****************************************************************************/
1692 static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
1695 struct v4l2_cropcap cropcap;
1696 struct v4l2_crop crop;
1697 struct v4l2_format fmt;
1699 enum v4l2_buf_type buf_type;
1700 const char *psz_device = p_sys->psz_device;
1703 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
1705 msg_Err( p_obj, "cannot open device (%m)" );
1710 /* Note the v4l2_xxx functions are designed so that if they get passed an
1711 unknown fd, the will behave exactly as their regular xxx counterparts,
1712 so if v4l2_fd_open fails, we continue as normal (missing the libv4l2
1713 custom cam format to normal formats conversion). Chances are big we will
1714 still fail then though, as normally v4l2_fd_open only fails if the
1715 device is not a v4l2 device. */
1716 if( p_sys->b_libv4l2 )
1719 libv4l2_fd = v4l2_fd_open( i_fd, V4L2_ENABLE_ENUM_FMT_EMULATION );
1720 if( libv4l2_fd != -1 )
1725 /* Tune the tuner */
1726 if( p_sys->i_frequency >= 0 )
1728 if( p_sys->i_cur_tuner < 0 || p_sys->i_cur_tuner >= p_sys->i_tuner )
1730 msg_Err( p_obj, "invalid tuner %d.", p_sys->i_cur_tuner );
1733 struct v4l2_frequency frequency;
1734 memset( &frequency, 0, sizeof( frequency ) );
1735 frequency.tuner = p_sys->i_cur_tuner;
1736 frequency.type = p_sys->p_tuners[p_sys->i_cur_tuner].type;
1737 frequency.frequency = p_sys->i_frequency / 62.5;
1738 if( v4l2_ioctl( i_fd, VIDIOC_S_FREQUENCY, &frequency ) < 0 )
1740 msg_Err( p_obj, "cannot set tuner frequency (%m)" );
1743 msg_Dbg( p_obj, "Tuner frequency set" );
1746 /* Set the tuner's audio mode */
1747 if( p_sys->i_audio_mode >= 0 )
1749 if( p_sys->i_cur_tuner < 0 || p_sys->i_cur_tuner >= p_sys->i_tuner )
1751 msg_Err( p_obj, "invalid tuner %d.", p_sys->i_cur_tuner );
1754 struct v4l2_tuner tuner;
1755 memset( &tuner, 0, sizeof( tuner ) );
1756 tuner.index = p_sys->i_cur_tuner;
1757 tuner.audmode = p_sys->i_audio_mode;
1758 if( v4l2_ioctl( i_fd, VIDIOC_S_TUNER, &tuner ) < 0 )
1760 msg_Err( p_obj, "cannot set tuner audio mode (%m)" );
1763 msg_Dbg( p_obj, "Tuner audio mode set" );
1766 /* Select standard */
1768 if( p_sys->i_selected_standard_id != V4L2_STD_UNKNOWN )
1770 if( v4l2_ioctl( i_fd, VIDIOC_S_STD, &p_sys->i_selected_standard_id ) < 0 )
1772 msg_Err( p_obj, "cannot set standard (%m)" );
1775 msg_Dbg( p_obj, "Set standard" );
1780 if( p_sys->i_selected_input >= p_sys->i_input )
1782 msg_Warn( p_obj, "invalid input. Using the default one" );
1783 p_sys->i_selected_input = 0;
1786 if( v4l2_ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_selected_input ) < 0 )
1788 msg_Err( p_obj, "cannot set input (%m)" );
1792 /* Set audio input */
1794 if( p_sys->i_audio > 0 )
1796 if( p_sys->i_selected_audio_input < 0
1797 || p_sys->i_selected_audio_input >= p_sys->i_audio )
1799 msg_Warn( p_obj, "invalid audio input. Using the default one" );
1800 p_sys->i_selected_audio_input = 0;
1803 if( v4l2_ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_selected_audio_input] ) < 0 )
1805 msg_Err( p_obj, "cannot set audio input (%m)" );
1808 msg_Dbg( p_obj, "Audio input set to %d", p_sys->i_selected_audio_input );
1812 /* TODO: Move the resolution stuff up here */
1813 /* if MPEG encoder card, no need to do anything else after this */
1814 ControlList( p_obj, p_sys, i_fd,
1815 var_GetBool( p_obj, "v4l2-controls-reset" ), b_demux );
1816 SetAvailControlsByString( p_obj, p_sys, i_fd );
1818 /* Verify device support for the various IO methods */
1821 case IO_METHOD_READ:
1822 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE) )
1824 msg_Err( p_obj, "device does not support read i/o" );
1829 case IO_METHOD_MMAP:
1830 case IO_METHOD_USERPTR:
1831 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING) )
1833 msg_Err( p_obj, "device does not support streaming i/o" );
1839 msg_Err( p_obj, "io method not supported" );
1843 /* Reset Cropping */
1844 memset( &cropcap, 0, sizeof(cropcap) );
1845 cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1846 if( v4l2_ioctl( i_fd, VIDIOC_CROPCAP, &cropcap ) >= 0 )
1848 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1849 crop.c = cropcap.defrect; /* reset to default */
1850 if( v4l2_ioctl( i_fd, VIDIOC_S_CROP, &crop ) < 0 )
1855 /* Cropping not supported. */
1858 /* Errors ignored. */
1864 /* Try and find default resolution if not specified */
1865 memset( &fmt, 0, sizeof(fmt) );
1866 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1868 if( p_sys->i_width == 0 || p_sys->i_height == 0 )
1870 /* Use current width and height settings */
1871 if( v4l2_ioctl( i_fd, VIDIOC_G_FMT, &fmt ) < 0 )
1873 msg_Err( p_obj, "Cannot get default width and height." );
1877 p_sys->i_width = fmt.fmt.pix.width;
1878 p_sys->i_height = fmt.fmt.pix.height;
1880 if( fmt.fmt.pix.field == V4L2_FIELD_ALTERNATE )
1882 p_sys->i_height = p_sys->i_height * 2;
1885 else if( p_sys->i_width < 0 || p_sys->i_height < 0 )
1887 msg_Dbg( p_obj, "will try to find optimal width and height." );
1891 /* Use user specified width and height */
1892 msg_Dbg( p_obj, "trying specified size %dx%d", p_sys->i_width, p_sys->i_height );
1895 fmt.fmt.pix.width = p_sys->i_width;
1896 fmt.fmt.pix.height = p_sys->i_height;
1897 fmt.fmt.pix.field = V4L2_FIELD_NONE;
1901 demux_t *p_demux = (demux_t *) p_obj;
1903 /* Test and set Chroma */
1904 fmt.fmt.pix.pixelformat = 0;
1905 if( p_sys->psz_requested_chroma && *p_sys->psz_requested_chroma )
1907 /* User specified chroma */
1908 const vlc_fourcc_t i_requested_fourcc =
1909 vlc_fourcc_GetCodecFromString( VIDEO_ES, p_sys->psz_requested_chroma );
1911 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
1913 if( v4l2chroma_to_fourcc[i].i_fourcc == i_requested_fourcc )
1915 fmt.fmt.pix.pixelformat = v4l2chroma_to_fourcc[i].i_v4l2;
1919 /* Try and set user chroma */
1920 bool b_error = !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat );
1921 if( !b_error && fmt.fmt.pix.pixelformat )
1923 if( v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 )
1925 fmt.fmt.pix.field = V4L2_FIELD_ANY;
1926 if( v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 )
1928 fmt.fmt.pix.field = V4L2_FIELD_NONE;
1935 msg_Warn( p_demux, "Driver is unable to use specified chroma %s. Trying defaults.", p_sys->psz_requested_chroma );
1936 fmt.fmt.pix.pixelformat = 0;
1940 /* If no user specified chroma, find best */
1941 /* This also decides if MPEG encoder card or not */
1942 if( !fmt.fmt.pix.pixelformat )
1945 for( i = 0; i < ARRAY_SIZE( p_chroma_fallbacks ); i++ )
1947 fmt.fmt.pix.pixelformat = p_chroma_fallbacks[i];
1948 if( IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) )
1950 if( v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) >= 0 )
1952 fmt.fmt.pix.field = V4L2_FIELD_ANY;
1953 if( v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) >= 0 )
1955 fmt.fmt.pix.field = V4L2_FIELD_NONE;
1958 if( i == ARRAY_SIZE( p_chroma_fallbacks ) )
1960 msg_Warn( p_demux, "Could not select any of the default chromas; attempting to open as MPEG encoder card (access)" );
1965 if( p_sys->i_width < 0 || p_sys->i_height < 0 )
1967 if( p_sys->f_fps <= 0 )
1969 p_sys->f_fps = GetAbsoluteMaxFrameRate( p_demux, i_fd,
1970 fmt.fmt.pix.pixelformat );
1971 msg_Dbg( p_demux, "Found maximum framerate of %f", p_sys->f_fps );
1973 GetMaxDimensions( p_demux, i_fd,
1974 fmt.fmt.pix.pixelformat, p_sys->f_fps,
1975 &fmt.fmt.pix.width, &fmt.fmt.pix.height );
1976 msg_Dbg( p_demux, "Found optimal dimensions for framerate %f of %dx%d",
1977 p_sys->f_fps, fmt.fmt.pix.width, fmt.fmt.pix.height );
1978 if( v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 ) {;}
1981 /* Reassign width, height and chroma incase driver override */
1982 p_sys->i_width = fmt.fmt.pix.width;
1983 p_sys->i_height = fmt.fmt.pix.height;
1986 if( v4l2_ioctl( i_fd, VIDIOC_G_FMT, &fmt ) < 0 ) {;}
1987 /* Print extra info */
1988 msg_Dbg( p_obj, "Driver requires at most %d bytes to store a complete image", fmt.fmt.pix.sizeimage );
1989 /* Check interlacing */
1990 switch( fmt.fmt.pix.field )
1992 case V4L2_FIELD_NONE:
1993 msg_Dbg( p_obj, "Interlacing setting: progressive" );
1995 case V4L2_FIELD_TOP:
1996 msg_Dbg( p_obj, "Interlacing setting: top field only" );
1998 case V4L2_FIELD_BOTTOM:
1999 msg_Dbg( p_obj, "Interlacing setting: bottom field only" );
2001 case V4L2_FIELD_INTERLACED:
2002 msg_Dbg( p_obj, "Interlacing setting: interleaved (bottom top if M/NTSC, top bottom otherwise)" );
2004 case V4L2_FIELD_SEQ_TB:
2005 msg_Dbg( p_obj, "Interlacing setting: sequential top bottom" );
2007 case V4L2_FIELD_SEQ_BT:
2008 msg_Dbg( p_obj, "Interlacing setting: sequential bottom top" );
2010 case V4L2_FIELD_ALTERNATE:
2011 msg_Dbg( p_obj, "Interlacing setting: alternate fields" );
2013 case V4L2_FIELD_INTERLACED_TB:
2014 msg_Dbg( p_obj, "Interlacing setting: interleaved top bottom" );
2016 case V4L2_FIELD_INTERLACED_BT:
2017 msg_Dbg( p_obj, "Interlacing setting: interleaved bottom top" );
2020 msg_Warn( p_obj, "Interlacing setting: unknown type (%d)",
2021 fmt.fmt.pix.field );
2024 if( fmt.fmt.pix.field != V4L2_FIELD_NONE )
2025 msg_Warn( p_obj, "Interlaced inputs haven't been tested. Please report any issue." );
2027 /* Look up final fourcc */
2028 p_sys->i_fourcc = 0;
2029 for( int i = 0; v4l2chroma_to_fourcc[i].i_fourcc != 0; i++ )
2031 if( v4l2chroma_to_fourcc[i].i_v4l2 == fmt.fmt.pix.pixelformat )
2033 p_sys->i_fourcc = v4l2chroma_to_fourcc[i].i_fourcc;
2034 es_format_Init( &es_fmt, VIDEO_ES, p_sys->i_fourcc );
2035 es_fmt.video.i_rmask = v4l2chroma_to_fourcc[i].i_rmask;
2036 es_fmt.video.i_gmask = v4l2chroma_to_fourcc[i].i_gmask;
2037 es_fmt.video.i_bmask = v4l2chroma_to_fourcc[i].i_bmask;
2042 /* Buggy driver paranoia */
2043 i_min = fmt.fmt.pix.width * 2;
2044 if( fmt.fmt.pix.bytesperline < i_min )
2045 fmt.fmt.pix.bytesperline = i_min;
2046 i_min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
2047 if( fmt.fmt.pix.sizeimage < i_min )
2048 fmt.fmt.pix.sizeimage = i_min;
2050 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
2051 /* This is new in Linux 2.6.19 */
2052 /* List supported frame rates */
2053 struct v4l2_frmivalenum frmival;
2054 memset( &frmival, 0, sizeof(frmival) );
2055 frmival.pixel_format = fmt.fmt.pix.pixelformat;
2056 frmival.width = p_sys->i_width;
2057 frmival.height = p_sys->i_height;
2058 if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 )
2061 memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
2062 vlc_fourcc_to_char( p_sys->i_fourcc, &psz_fourcc );
2063 msg_Dbg( p_obj, "supported frame intervals for %4s, %dx%d:",
2064 psz_fourcc, frmival.width, frmival.height );
2065 switch( frmival.type )
2067 case V4L2_FRMIVAL_TYPE_DISCRETE:
2070 msg_Dbg( p_obj, " supported frame interval: %d/%d",
2071 frmival.discrete.numerator,
2072 frmival.discrete.denominator );
2074 } while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 );
2076 case V4L2_FRMIVAL_TYPE_STEPWISE:
2077 msg_Dbg( p_obj, " supported frame intervals: %d/%d to "
2078 "%d/%d using %d/%d increments",
2079 frmival.stepwise.min.numerator,
2080 frmival.stepwise.min.denominator,
2081 frmival.stepwise.max.numerator,
2082 frmival.stepwise.max.denominator,
2083 frmival.stepwise.step.numerator,
2084 frmival.stepwise.step.denominator );
2086 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
2087 msg_Dbg( p_obj, " supported frame intervals: %d/%d to %d/%d",
2088 frmival.stepwise.min.numerator,
2089 frmival.stepwise.min.denominator,
2090 frmival.stepwise.max.numerator,
2091 frmival.stepwise.max.denominator );
2098 /* Init IO method */
2101 case IO_METHOD_READ:
2102 if( InitRead( p_obj, p_sys, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
2105 case IO_METHOD_MMAP:
2106 if( InitMmap( p_obj, p_sys, i_fd ) != VLC_SUCCESS ) goto open_failed;
2109 case IO_METHOD_USERPTR:
2110 if( InitUserP( p_obj, p_sys, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
2116 es_fmt.video.i_width = p_sys->i_width;
2117 es_fmt.video.i_height = p_sys->i_height;
2118 es_fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
2122 demux_t *p_demux = (demux_t *) p_obj;
2123 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
2124 (char*)&es_fmt.i_codec, es_fmt.video.i_width, es_fmt.video.i_height );
2125 p_sys->p_es = es_out_Add( p_demux->out, &es_fmt );
2132 case IO_METHOD_READ:
2136 case IO_METHOD_MMAP:
2137 for (unsigned int i = 0; i < p_sys->i_nbuffers; ++i)
2139 struct v4l2_buffer buf;
2141 memset( &buf, 0, sizeof(buf) );
2142 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2143 buf.memory = V4L2_MEMORY_MMAP;
2146 if( v4l2_ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
2148 msg_Err( p_obj, "VIDIOC_QBUF failed" );
2153 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2154 if( v4l2_ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
2156 msg_Err( p_obj, "VIDIOC_STREAMON failed" );
2162 case IO_METHOD_USERPTR:
2163 for( unsigned int i = 0; i < p_sys->i_nbuffers; ++i )
2165 struct v4l2_buffer buf;
2167 memset( &buf, 0, sizeof(buf) );
2168 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2169 buf.memory = V4L2_MEMORY_USERPTR;
2171 buf.m.userptr = (unsigned long)p_sys->p_buffers[i].start;
2172 buf.length = p_sys->p_buffers[i].length;
2174 if( v4l2_ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
2176 msg_Err( p_obj, "VIDIOC_QBUF failed" );
2181 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2182 if( v4l2_ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
2184 msg_Err( p_obj, "VIDIOC_STREAMON failed" );
2192 if( p_sys->f_fps >= 0.1 )
2194 msg_Dbg( p_obj, "User set fps=%f", p_sys->f_fps );
2200 if( i_fd >= 0 ) v4l2_close( i_fd );
2205 /*****************************************************************************
2206 * ProbeVideoDev: probe video for capabilities
2207 *****************************************************************************/
2208 static bool ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
2209 const char *psz_device )
2216 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
2218 msg_Err( p_obj, "cannot open video device '%s' (%m)", psz_device );
2223 /* Note the v4l2_xxx functions are designed so that if they get passed an
2224 unknown fd, the will behave exactly as their regular xxx counterparts,
2225 so if v4l2_fd_open fails, we continue as normal (missing the libv4l2
2226 custom cam format to normal formats conversion). Chances are big we will
2227 still fail then though, as normally v4l2_fd_open only fails if the
2228 device is not a v4l2 device. */
2229 if( p_sys->b_libv4l2 )
2232 libv4l2_fd = v4l2_fd_open( i_fd, V4L2_ENABLE_ENUM_FMT_EMULATION );
2233 if( libv4l2_fd != -1 )
2238 /* Get device capabilites */
2240 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCAP, &p_sys->dev_cap ) < 0 )
2242 msg_Err( p_obj, "cannot get video capabilities (%m)" );
2246 msg_Dbg( p_obj, "V4L2 device: %s using driver: %s (version: %u.%u.%u) on %s",
2247 p_sys->dev_cap.card,
2248 p_sys->dev_cap.driver,
2249 (p_sys->dev_cap.version >> 16) & 0xFF,
2250 (p_sys->dev_cap.version >> 8) & 0xFF,
2251 p_sys->dev_cap.version & 0xFF,
2252 p_sys->dev_cap.bus_info );
2254 msg_Dbg( p_obj, "the device has the capabilities: (%c) Video Capure, "
2257 ( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE ? 'X':' '),
2258 ( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO ? 'X':' '),
2259 ( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER ? 'X':' ') );
2261 msg_Dbg( p_obj, "supported I/O methods are: (%c) Read/Write, "
2263 "(%c) Asynchronous",
2264 ( p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE ? 'X':' ' ),
2265 ( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING ? 'X':' ' ),
2266 ( p_sys->dev_cap.capabilities & V4L2_CAP_ASYNCIO ? 'X':' ' ) );
2268 /* Now, enumerate all the video inputs. This is useless at the moment
2269 since we have no way to present that info to the user except with
2272 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2274 struct v4l2_input t_input;
2275 memset( &t_input, 0, sizeof(t_input) );
2276 while( v4l2_ioctl( i_fd, VIDIOC_ENUMINPUT, &t_input ) >= 0 )
2279 t_input.index = p_sys->i_input;
2282 p_sys->p_inputs = calloc( 1, p_sys->i_input * sizeof( struct v4l2_input ) );
2283 if( !p_sys->p_inputs ) goto open_failed;
2285 for( i_index = 0; i_index < p_sys->i_input; i_index++ )
2287 p_sys->p_inputs[i_index].index = i_index;
2289 if( v4l2_ioctl( i_fd, VIDIOC_ENUMINPUT, &p_sys->p_inputs[i_index] ) )
2291 msg_Err( p_obj, "cannot get video input characteristics (%m)" );
2294 msg_Dbg( p_obj, "video input %i (%s) has type: %s %c",
2296 p_sys->p_inputs[i_index].name,
2297 p_sys->p_inputs[i_index].type
2298 == V4L2_INPUT_TYPE_TUNER ?
2300 "External analog input",
2301 i_index == p_sys->i_selected_input ? '*' : ' ' );
2305 /* Probe video standards */
2306 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2308 struct v4l2_standard t_standards;
2309 t_standards.index = 0;
2310 p_sys->i_standard = 0;
2311 while( v4l2_ioctl( i_fd, VIDIOC_ENUMSTD, &t_standards ) >=0 )
2313 p_sys->i_standard++;
2314 t_standards.index = p_sys->i_standard;
2317 p_sys->p_standards = calloc( 1, p_sys->i_standard * sizeof( struct v4l2_standard ) );
2318 if( !p_sys->p_standards ) goto open_failed;
2320 for( i_standard = 0; i_standard < p_sys->i_standard; i_standard++ )
2322 p_sys->p_standards[i_standard].index = i_standard;
2324 if( v4l2_ioctl( i_fd, VIDIOC_ENUMSTD, &p_sys->p_standards[i_standard] ) )
2326 msg_Err( p_obj, "cannot get video input standards (%m)" );
2329 msg_Dbg( p_obj, "video standard %i is: %s %c",
2331 p_sys->p_standards[i_standard].name,
2332 (unsigned)i_standard == p_sys->i_selected_standard_id ? '*' : ' ' );
2336 /* initialize the structures for the ioctls */
2337 for( i_index = 0; i_index < 32; i_index++ )
2339 p_sys->p_audios[i_index].index = i_index;
2342 /* Probe audio inputs */
2343 if( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO )
2345 while( p_sys->i_audio < 32 &&
2346 v4l2_ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_audio] ) >= 0 )
2348 if( v4l2_ioctl( i_fd, VIDIOC_G_AUDIO, &p_sys->p_audios[ p_sys->i_audio] ) < 0 )
2350 msg_Err( p_obj, "cannot get audio input characteristics (%m)" );
2354 msg_Dbg( p_obj, "audio input %i (%s) is %s %s %c",
2356 p_sys->p_audios[p_sys->i_audio].name,
2357 p_sys->p_audios[p_sys->i_audio].capability &
2358 V4L2_AUDCAP_STEREO ?
2360 p_sys->p_audios[p_sys->i_audio].capability &
2362 "(Automatic Volume Level supported)" : "",
2363 p_sys->i_audio == p_sys->i_selected_audio_input ? '*' : ' ' );
2369 /* List tuner caps */
2370 if( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER )
2372 struct v4l2_tuner tuner;
2373 memset( &tuner, 0, sizeof(tuner) );
2375 while( v4l2_ioctl( i_fd, VIDIOC_G_TUNER, &tuner ) >= 0 )
2378 memset( &tuner, 0, sizeof(tuner) );
2379 tuner.index = p_sys->i_tuner;
2382 p_sys->p_tuners = calloc( 1, p_sys->i_tuner * sizeof( struct v4l2_tuner ) );
2383 if( !p_sys->p_tuners ) goto open_failed;
2385 for( i_index = 0; i_index < p_sys->i_tuner; i_index++ )
2387 p_sys->p_tuners[i_index].index = i_index;
2389 if( v4l2_ioctl( i_fd, VIDIOC_G_TUNER, &p_sys->p_tuners[i_index] ) )
2391 msg_Err( p_obj, "cannot get tuner characteristics (%m)" );
2394 msg_Dbg( p_obj, "tuner %i (%s) has type: %s, "
2395 "frequency range: %.1f %s -> %.1f %s",
2397 p_sys->p_tuners[i_index].name,
2398 p_sys->p_tuners[i_index].type
2399 == V4L2_TUNER_RADIO ?
2400 "Radio" : "Analog TV",
2401 p_sys->p_tuners[i_index].rangelow * 62.5,
2402 p_sys->p_tuners[i_index].capability &
2403 V4L2_TUNER_CAP_LOW ?
2405 p_sys->p_tuners[i_index].rangehigh * 62.5,
2406 p_sys->p_tuners[i_index].capability &
2407 V4L2_TUNER_CAP_LOW ?
2410 struct v4l2_frequency frequency;
2411 memset( &frequency, 0, sizeof( frequency ) );
2412 if( v4l2_ioctl( i_fd, VIDIOC_G_FREQUENCY, &frequency ) < 0 )
2414 msg_Err( p_obj, "cannot get tuner frequency (%m)" );
2417 msg_Dbg( p_obj, "tuner %i (%s) frequency: %.1f %s",
2419 p_sys->p_tuners[i_index].name,
2420 frequency.frequency * 62.5,
2421 p_sys->p_tuners[i_index].capability &
2422 V4L2_TUNER_CAP_LOW ?
2427 /* Probe for available chromas */
2428 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2430 struct v4l2_fmtdesc codec;
2433 memset( &codec, 0, sizeof(codec) );
2434 codec.index = i_index;
2435 codec.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2437 while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FMT, &codec ) >= 0 )
2440 codec.index = i_index;
2443 p_sys->i_codec = i_index;
2445 p_sys->p_codecs = calloc( 1, p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
2447 for( i_index = 0; i_index < p_sys->i_codec; i_index++ )
2449 p_sys->p_codecs[i_index].index = i_index;
2450 p_sys->p_codecs[i_index].type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2452 if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FMT, &p_sys->p_codecs[i_index] ) < 0 )
2454 msg_Err( p_obj, "cannot get codec description (%m)" );
2458 /* only print if vlc supports the format */
2459 char psz_fourcc_v4l2[5];
2460 memset( &psz_fourcc_v4l2, 0, sizeof( psz_fourcc_v4l2 ) );
2461 vlc_fourcc_to_char( p_sys->p_codecs[i_index].pixelformat,
2463 bool b_codec_supported = false;
2464 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
2466 if( v4l2chroma_to_fourcc[i].i_v4l2 == p_sys->p_codecs[i_index].pixelformat )
2468 b_codec_supported = true;
2471 memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
2472 vlc_fourcc_to_char( v4l2chroma_to_fourcc[i].i_fourcc,
2474 msg_Dbg( p_obj, "device supports chroma %4s [%s, %s]",
2476 p_sys->p_codecs[i_index].description,
2479 #ifdef VIDIOC_ENUM_FRAMESIZES
2480 /* This is new in Linux 2.6.19 */
2481 /* List valid frame sizes for this format */
2482 struct v4l2_frmsizeenum frmsize;
2483 memset( &frmsize, 0, sizeof(frmsize) );
2484 frmsize.pixel_format = p_sys->p_codecs[i_index].pixelformat;
2485 if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) < 0 )
2487 /* Not all devices support this ioctl */
2488 msg_Warn( p_obj, "Unable to query for frame sizes" );
2492 switch( frmsize.type )
2494 case V4L2_FRMSIZE_TYPE_DISCRETE:
2498 " device supports size %dx%d",
2499 frmsize.discrete.width, frmsize.discrete.height );
2501 } while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) >= 0 );
2503 case V4L2_FRMSIZE_TYPE_STEPWISE:
2505 " device supports sizes %dx%d to %dx%d using %dx%d increments",
2506 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
2507 frmsize.stepwise.max_width, frmsize.stepwise.max_height,
2508 frmsize.stepwise.step_width, frmsize.stepwise.step_height );
2510 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
2512 " device supports all sizes %dx%d to %dx%d",
2513 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
2514 frmsize.stepwise.max_width, frmsize.stepwise.max_height );
2521 if( !b_codec_supported )
2524 "device codec %4s (%s) not supported",
2526 p_sys->p_codecs[i_index].description );
2532 if( i_fd >= 0 ) v4l2_close( i_fd );
2537 if( i_fd >= 0 ) v4l2_close( i_fd );
2542 static void name2var( unsigned char *name )
2544 for( ; *name; name++ )
2545 *name = (*name == ' ') ? '_' : tolower( *name );
2548 /*****************************************************************************
2549 * Print a user-class v4l2 control's details, create the relevant variable,
2550 * change the value if needed.
2551 *****************************************************************************/
2552 static void ControlListPrint( vlc_object_t *p_obj, demux_sys_t *p_sys, int i_fd,
2553 struct v4l2_queryctrl queryctrl,
2554 bool b_reset, bool b_demux )
2556 struct v4l2_querymenu querymenu;
2563 vlc_value_t val, val2;
2565 if( queryctrl.flags & V4L2_CTRL_FLAG_GRABBED )
2566 msg_Dbg( p_obj, " control is busy" );
2567 if( queryctrl.flags & V4L2_CTRL_FLAG_READ_ONLY )
2568 msg_Dbg( p_obj, " control is read-only" );
2570 for( i = 0; controls[i].psz_name != NULL; i++ )
2571 if( controls[i].i_cid == queryctrl.id ) break;
2573 if( controls[i].psz_name )
2575 psz_name = strdup( controls[i].psz_name );
2576 char psz_cfg_name[40];
2577 sprintf( psz_cfg_name, CFG_PREFIX "%s", psz_name );
2578 i_val = var_CreateGetInteger( p_obj, psz_cfg_name );
2579 var_Destroy( p_obj, psz_cfg_name );
2583 psz_name = strdup( (const char *)queryctrl.name );
2584 name2var( (unsigned char *)psz_name );
2588 switch( queryctrl.type )
2590 case V4L2_CTRL_TYPE_INTEGER:
2591 msg_Dbg( p_obj, " integer control" );
2593 " valid values: %d to %d by steps of %d",
2594 queryctrl.minimum, queryctrl.maximum,
2597 var_Create( p_obj, psz_name,
2598 VLC_VAR_INTEGER | VLC_VAR_HASMIN | VLC_VAR_HASMAX
2599 | VLC_VAR_HASSTEP | VLC_VAR_ISCOMMAND );
2600 val.i_int = queryctrl.minimum;
2601 var_Change( p_obj, psz_name, VLC_VAR_SETMIN, &val, NULL );
2602 val.i_int = queryctrl.maximum;
2603 var_Change( p_obj, psz_name, VLC_VAR_SETMAX, &val, NULL );
2604 val.i_int = queryctrl.step;
2605 var_Change( p_obj, psz_name, VLC_VAR_SETSTEP, &val, NULL );
2607 case V4L2_CTRL_TYPE_BOOLEAN:
2608 msg_Dbg( p_obj, " boolean control" );
2609 var_Create( p_obj, psz_name,
2610 VLC_VAR_BOOL | VLC_VAR_ISCOMMAND );
2612 case V4L2_CTRL_TYPE_MENU:
2613 msg_Dbg( p_obj, " menu control" );
2614 var_Create( p_obj, psz_name,
2615 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE
2616 | VLC_VAR_ISCOMMAND );
2617 memset( &querymenu, 0, sizeof( querymenu ) );
2618 for( i_mid = queryctrl.minimum;
2619 i_mid <= (unsigned)queryctrl.maximum;
2622 querymenu.index = i_mid;
2623 querymenu.id = queryctrl.id;
2624 if( v4l2_ioctl( i_fd, VIDIOC_QUERYMENU, &querymenu ) >= 0 )
2626 msg_Dbg( p_obj, " %d: %s",
2627 querymenu.index, querymenu.name );
2628 val.i_int = querymenu.index;
2629 val2.psz_string = (char *)querymenu.name;
2630 var_Change( p_obj, psz_name,
2631 VLC_VAR_ADDCHOICE, &val, &val2 );
2635 case V4L2_CTRL_TYPE_BUTTON:
2636 msg_Dbg( p_obj, " button control" );
2637 var_Create( p_obj, psz_name,
2638 VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
2640 case V4L2_CTRL_TYPE_CTRL_CLASS:
2641 msg_Dbg( p_obj, " control class" );
2642 var_Create( p_obj, psz_name, VLC_VAR_VOID );
2645 msg_Dbg( p_obj, " unknown control type (FIXME)" );
2650 switch( queryctrl.type )
2652 case V4L2_CTRL_TYPE_INTEGER:
2653 case V4L2_CTRL_TYPE_BOOLEAN:
2654 case V4L2_CTRL_TYPE_MENU:
2656 struct v4l2_control control;
2657 msg_Dbg( p_obj, " default value: %d",
2658 queryctrl.default_value );
2659 memset( &control, 0, sizeof( control ) );
2660 control.id = queryctrl.id;
2661 if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
2663 msg_Dbg( p_obj, " current value: %d", control.value );
2667 i_val = control.value;
2668 if( b_reset && queryctrl.default_value != control.value )
2670 msg_Dbg( p_obj, " reset value to default" );
2671 Control( p_obj, p_sys, i_fd, psz_name,
2672 queryctrl.id, queryctrl.default_value );
2677 Control( p_obj, p_sys, i_fd, psz_name,
2678 queryctrl.id, i_val );
2686 val.psz_string = (char *)queryctrl.name;
2687 var_Change( p_obj, psz_name, VLC_VAR_SETTEXT, &val, NULL );
2688 val.i_int = queryctrl.id;
2689 val2.psz_string = (char *)psz_name;
2690 var_Change( p_obj, "allcontrols", VLC_VAR_ADDCHOICE, &val, &val2 );
2691 /* bad things happen changing MPEG mid-stream
2692 * so don't add to Ext Settings GUI */
2693 if( V4L2_CTRL_ID2CLASS( queryctrl.id ) != V4L2_CTRL_CLASS_MPEG )
2694 var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
2696 switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
2699 var_SetBool( p_obj, psz_name, i_val );
2701 case VLC_VAR_INTEGER:
2702 var_SetInteger( p_obj, psz_name, i_val );
2707 msg_Warn( p_obj, "FIXME: %s %s %d", __FILE__, __func__,
2713 var_AddCallback( p_obj, psz_name,
2714 DemuxControlCallback, (void*)(intptr_t)queryctrl.id );
2716 var_AddCallback( p_obj, psz_name,
2717 AccessControlCallback, (void*)(intptr_t)queryctrl.id );
2722 /*****************************************************************************
2723 * List all user-class v4l2 controls, set them to the user specified
2724 * value and create the relevant variables to enable runtime changes
2725 *****************************************************************************/
2726 static int ControlList( vlc_object_t *p_obj, demux_sys_t *p_sys, int i_fd,
2727 bool b_reset, bool b_demux )
2729 struct v4l2_queryctrl queryctrl;
2732 memset( &queryctrl, 0, sizeof( queryctrl ) );
2734 /* A list of available controls (aka the variable name) will be
2735 * stored as choices in the "allcontrols" variable. We'll thus be able
2736 * to use those to create an appropriate interface
2737 * A list of available controls that can be changed mid-stream will
2738 * be stored in the "controls" variable */
2739 var_Create( p_obj, "controls", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
2740 var_Create( p_obj, "allcontrols", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
2742 var_Create( p_obj, "controls-update", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
2744 /* Add a control to reset all controls to their default values */
2745 vlc_value_t val, val2;
2746 var_Create( p_obj, "controls-reset", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
2747 val.psz_string = _( "Reset controls to default" );
2748 var_Change( p_obj, "controls-reset", VLC_VAR_SETTEXT, &val, NULL );
2750 val2.psz_string = (char *)"controls-reset";
2751 var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
2753 var_AddCallback( p_obj, "controls-reset", DemuxControlResetCallback, NULL );
2755 var_AddCallback( p_obj, "controls-reset", AccessControlResetCallback, NULL );
2757 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
2758 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2760 msg_Dbg( p_obj, "Extended control API supported by v4l2 driver" );
2762 /* List extended controls */
2763 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
2764 while( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2766 if( queryctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS )
2768 msg_Dbg( p_obj, "%s", queryctrl.name );
2769 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
2772 switch( V4L2_CTRL_ID2CLASS( queryctrl.id ) )
2774 case V4L2_CTRL_CLASS_USER:
2775 msg_Dbg( p_obj, "Available control: %s (%x)",
2776 queryctrl.name, queryctrl.id );
2778 case V4L2_CTRL_CLASS_MPEG:
2779 name2var( queryctrl.name );
2780 msg_Dbg( p_obj, "Available MPEG control: %s (%x)",
2781 queryctrl.name, queryctrl.id );
2784 msg_Dbg( p_obj, "Available private control: %s (%x)",
2785 queryctrl.name, queryctrl.id );
2788 ControlListPrint( p_obj, p_sys, i_fd, queryctrl, b_reset, b_demux );
2789 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
2794 msg_Dbg( p_obj, "Extended control API not supported by v4l2 driver" );
2796 /* List public controls */
2797 for( i_cid = V4L2_CID_BASE;
2798 i_cid < V4L2_CID_LASTP1;
2801 queryctrl.id = i_cid;
2802 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2804 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2806 msg_Dbg( p_obj, "Available control: %s (%x)",
2807 queryctrl.name, queryctrl.id );
2808 ControlListPrint( p_obj, p_sys, i_fd, queryctrl,
2813 /* List private controls */
2814 for( i_cid = V4L2_CID_PRIVATE_BASE;
2818 queryctrl.id = i_cid;
2819 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2821 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2823 msg_Dbg( p_obj, "Available private control: %s (%x)",
2824 queryctrl.name, queryctrl.id );
2825 ControlListPrint( p_obj, p_sys, i_fd, queryctrl,
2836 static void SetAvailControlsByString( vlc_object_t *p_obj, demux_sys_t *p_sys,
2839 char *psz_parser = p_sys->psz_set_ctrls;
2840 vlc_value_t val, text, name;
2842 if( psz_parser == NULL )
2845 if( *psz_parser == '{' )
2848 int i_ret = var_Change( p_obj, "allcontrols", VLC_VAR_GETCHOICES,
2852 msg_Err( p_obj, "Oops, can't find 'allcontrols' variable." );
2856 while( *psz_parser && *psz_parser != '}' )
2858 char *psz_delim, *psz_assign;
2860 while( *psz_parser == ',' || *psz_parser == ' ' )
2863 psz_delim = strchr( psz_parser, ',' );
2864 if( psz_delim == NULL )
2865 psz_delim = strchr( psz_parser, '}' );
2866 if( psz_delim == NULL )
2867 psz_delim = psz_parser + strlen( psz_parser );
2869 psz_assign = memchr( psz_parser, '=', psz_delim - psz_parser );
2870 if( psz_assign == NULL )
2872 char *psz_name = strndup( psz_parser, psz_delim - psz_parser );
2873 msg_Err( p_obj, "%s missing '='", psz_name );
2875 psz_parser = psz_delim + 1;
2880 i < val.p_list->i_count ;//&& psz_parser < psz_assign;
2883 const char *psz_var = text.p_list->p_values[i].psz_string;
2884 int i_cid = val.p_list->p_values[i].i_int;
2885 var_Change( p_obj, psz_var, VLC_VAR_GETTEXT, &name, NULL );
2886 const char *psz_name = name.psz_string;
2888 int i_availstrlen = strlen( psz_name );
2889 int i_parsestrlen = psz_assign - psz_parser;
2890 int i_maxstrlen = __MAX( i_availstrlen, i_parsestrlen);
2892 if( !strncasecmp( psz_name, psz_parser, i_maxstrlen ) )
2894 Control( p_obj, p_sys, i_fd, psz_name, i_cid,
2895 strtol( ++psz_assign, &psz_parser, 0) );
2897 free( name.psz_string );
2900 if( psz_parser < psz_assign )
2902 char *psz_name = strndup( psz_parser, psz_assign - psz_parser );
2903 msg_Err( p_obj, "Control %s not available", psz_name );
2905 psz_parser = ( *psz_delim ) ? ( psz_delim + 1 ) : psz_delim;
2908 var_FreeList( &val, &text );
2911 /*****************************************************************************
2912 * Reset all user-class v4l2 controls to their default value
2913 *****************************************************************************/
2914 static int ControlReset( vlc_object_t *p_obj, demux_sys_t *p_sys, int i_fd )
2916 struct v4l2_queryctrl queryctrl;
2918 memset( &queryctrl, 0, sizeof( queryctrl ) );
2920 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
2921 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2923 /* Extended control API supported */
2924 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
2925 while( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2927 if( queryctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS
2928 || V4L2_CTRL_ID2CLASS( queryctrl.id ) == V4L2_CTRL_CLASS_MPEG )
2930 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
2933 struct v4l2_control control;
2934 memset( &control, 0, sizeof( control ) );
2935 control.id = queryctrl.id;
2936 if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
2937 && queryctrl.default_value != control.value )
2940 for( i = 0; controls[i].psz_name != NULL; i++ )
2941 if( controls[i].i_cid == queryctrl.id ) break;
2942 name2var( queryctrl.name );
2943 Control( p_obj, p_sys, i_fd,
2944 controls[i].psz_name ? controls[i].psz_name
2945 : (const char *)queryctrl.name,
2946 queryctrl.id, queryctrl.default_value );
2948 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
2954 /* public controls */
2955 for( i_cid = V4L2_CID_BASE;
2956 i_cid < V4L2_CID_LASTP1;
2959 queryctrl.id = i_cid;
2960 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2962 struct v4l2_control control;
2963 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2965 memset( &control, 0, sizeof( control ) );
2966 control.id = queryctrl.id;
2967 if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
2968 && queryctrl.default_value != control.value )
2971 for( i = 0; controls[i].psz_name != NULL; i++ )
2972 if( controls[i].i_cid == queryctrl.id ) break;
2973 name2var( queryctrl.name );
2974 Control( p_obj, p_sys, i_fd,
2975 controls[i].psz_name ? controls[i].psz_name
2976 : (const char *)queryctrl.name,
2977 queryctrl.id, queryctrl.default_value );
2982 /* private controls */
2983 for( i_cid = V4L2_CID_PRIVATE_BASE;
2987 queryctrl.id = i_cid;
2988 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2990 struct v4l2_control control;
2991 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2993 memset( &control, 0, sizeof( control ) );
2994 control.id = queryctrl.id;
2995 if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
2996 && queryctrl.default_value != control.value )
2998 name2var( queryctrl.name );
2999 Control( p_obj, p_sys, i_fd, (const char *)queryctrl.name,
3000 queryctrl.id, queryctrl.default_value );
3010 /*****************************************************************************
3011 * Issue user-class v4l2 controls
3012 *****************************************************************************/
3013 static int Control( vlc_object_t *p_obj, demux_sys_t *p_sys, int i_fd,
3014 const char *psz_name, int i_cid, int i_value )
3017 struct v4l2_queryctrl queryctrl;
3018 struct v4l2_control control;
3019 struct v4l2_ext_control ext_control;
3020 struct v4l2_ext_controls ext_controls;
3025 memset( &queryctrl, 0, sizeof( queryctrl ) );
3027 queryctrl.id = i_cid;
3029 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) < 0
3030 || queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3032 msg_Dbg( p_obj, "%s (%x) control is not supported.", psz_name, i_cid );
3033 return VLC_EGENERIC;
3036 memset( &control, 0, sizeof( control ) );
3037 memset( &ext_control, 0, sizeof( ext_control ) );
3038 memset( &ext_controls, 0, sizeof( ext_controls ) );
3040 ext_control.id = i_cid;
3041 ext_controls.ctrl_class = V4L2_CTRL_ID2CLASS( i_cid );
3042 ext_controls.count = 1;
3043 ext_controls.controls = &ext_control;
3047 if( i_value >= queryctrl.minimum && i_value <= queryctrl.maximum )
3049 ext_control.value = i_value;
3050 if( v4l2_ioctl( i_fd, VIDIOC_S_EXT_CTRLS, &ext_controls ) < 0 )
3052 control.value = i_value;
3053 if( v4l2_ioctl( i_fd, VIDIOC_S_CTRL, &control ) < 0 )
3055 msg_Err( p_obj, "unable to set %s (%x) to %d (%m)",
3056 psz_name, i_cid, i_value );
3057 return VLC_EGENERIC;
3059 i_ret = v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control );
3063 i_ret = v4l2_ioctl( i_fd, VIDIOC_G_EXT_CTRLS, &ext_controls );
3064 control.value = ext_control.value;
3071 msg_Dbg( p_obj, "video %s: %d", psz_name, control.value );
3072 switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
3075 val.b_bool = control.value;
3076 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
3077 var_TriggerCallback( p_obj, "controls-update" );
3079 case VLC_VAR_INTEGER:
3080 val.i_int = control.value;
3081 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
3082 var_TriggerCallback( p_obj, "controls-update" );
3089 /*****************************************************************************
3090 * On the fly change settings callback
3091 *****************************************************************************/
3092 static int DemuxControlCallback( vlc_object_t *p_this,
3093 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3097 demux_t *p_demux = (demux_t*)p_this;
3098 demux_sys_t *p_sys = p_demux->p_sys;
3099 int i_cid = (long int)p_data;
3101 int i_fd = p_sys->i_fd;
3104 return VLC_EGENERIC;
3106 Control( p_this, p_sys, i_fd, psz_var, i_cid, newval.i_int );
3108 return VLC_EGENERIC;
3111 static int DemuxControlResetCallback( vlc_object_t *p_this,
3112 const char *psz_var, vlc_value_t oldval, vlc_value_t newval, void *p_data )
3114 (void)psz_var; (void)oldval; (void)newval; (void)p_data;
3115 demux_t *p_demux = (demux_t*)p_this;
3116 demux_sys_t *p_sys = p_demux->p_sys;
3118 int i_fd = p_sys->i_fd;
3121 return VLC_EGENERIC;
3123 ControlReset( p_this, p_sys, i_fd );
3125 return VLC_EGENERIC;
3128 static int AccessControlCallback( vlc_object_t *p_this,
3129 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3133 access_t *p_access = (access_t *)p_this;
3134 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
3135 int i_cid = (long int)p_data;
3137 int i_fd = p_sys->i_fd;
3140 return VLC_EGENERIC;
3142 Control( p_this, p_sys, i_fd, psz_var, i_cid, newval.i_int );
3144 return VLC_EGENERIC;
3147 static int AccessControlResetCallback( vlc_object_t *p_this,
3148 const char *psz_var, vlc_value_t oldval, vlc_value_t newval, void *p_data )
3150 (void)psz_var; (void)oldval; (void)newval; (void)p_data;
3151 access_t *p_access = (access_t *)p_this;
3152 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
3154 int i_fd = p_sys->i_fd;
3157 return VLC_EGENERIC;
3159 ControlReset( p_this, p_sys, i_fd );
3161 return VLC_EGENERIC;