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>
58 # define v4l2_close close
60 # define v4l2_ioctl ioctl
61 # define v4l2_read read
62 # define v4l2_mmap mmap
63 # define v4l2_munmap munmap
66 /*****************************************************************************
68 *****************************************************************************/
70 static int DemuxOpen ( vlc_object_t * );
71 static void DemuxClose( vlc_object_t * );
72 static int AccessOpen ( vlc_object_t * );
73 static void AccessClose( vlc_object_t * );
75 #define STANDARD_TEXT N_( "Standard" )
76 #define STANDARD_LONGTEXT N_( \
77 "Video standard (Default, SECAM, PAL, or NTSC)." )
78 #define CHROMA_TEXT N_("Video input chroma format")
79 #define CHROMA_LONGTEXT N_( \
80 "Force the Video4Linux2 video device to use a specific chroma format " \
81 "(eg. I420 or I422 for raw images, MJPG for M-JPEG compressed input) " \
82 "(Complete list: GREY, I240, RV16, RV15, RV24, RV32, YUY2, YUYV, UYVY, " \
83 "I41N, I422, I420, I411, I410, MJPG)")
84 #define INPUT_TEXT N_( "Input" )
85 #define INPUT_LONGTEXT N_( \
86 "Input of the card to use (see debug)." )
87 #define AUDIO_INPUT_TEXT N_( "Audio input" )
88 #define AUDIO_INPUT_LONGTEXT N_( \
89 "Audio input of the card to use (see debug)." )
90 #define IOMETHOD_TEXT N_( "IO Method" )
91 #define IOMETHOD_LONGTEXT N_( \
92 "IO Method (READ, MMAP, USERPTR)." )
93 #define WIDTH_TEXT N_( "Width" )
94 #define WIDTH_LONGTEXT N_( \
95 "Force width (-1 for autodetect, 0 for driver default)." )
96 #define HEIGHT_TEXT N_( "Height" )
97 #define HEIGHT_LONGTEXT N_( \
98 "Force height (-1 for autodetect, 0 for driver default)." )
99 #define FPS_TEXT N_( "Framerate" )
100 #define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
101 "(0 for autodetect)." )
103 #define CTRL_RESET_TEXT N_( "Reset v4l2 controls" )
104 #define CTRL_RESET_LONGTEXT N_( \
105 "Reset controls to defaults provided by the v4l2 driver." )
106 #define BRIGHTNESS_TEXT N_( "Brightness" )
107 #define BRIGHTNESS_LONGTEXT N_( \
108 "Brightness of the video input (if supported by the v4l2 driver)." )
109 #define CONTRAST_TEXT N_( "Contrast" )
110 #define CONTRAST_LONGTEXT N_( \
111 "Contrast of the video input (if supported by the v4l2 driver)." )
112 #define SATURATION_TEXT N_( "Saturation" )
113 #define SATURATION_LONGTEXT N_( \
114 "Saturation of the video input (if supported by the v4l2 driver)." )
115 #define HUE_TEXT N_( "Hue" )
116 #define HUE_LONGTEXT N_( \
117 "Hue of the video input (if supported by the v4l2 driver)." )
118 #define BLACKLEVEL_TEXT N_( "Black level" )
119 #define BLACKLEVEL_LONGTEXT N_( \
120 "Black level of the video input (if supported by the v4l2 driver)." )
121 #define AUTOWHITEBALANCE_TEXT N_( "Auto white balance" )
122 #define AUTOWHITEBALANCE_LONGTEXT N_( \
123 "Automatically set the white balance of the video input " \
124 "(if supported by the v4l2 driver)." )
125 #define DOWHITEBALANCE_TEXT N_( "Do white balance" )
126 #define DOWHITEBALANCE_LONGTEXT N_( \
127 "Trigger a white balancing action, useless if auto white balance is " \
128 "activated (if supported by the v4l2 driver)." )
129 #define REDBALANCE_TEXT N_( "Red balance" )
130 #define REDBALANCE_LONGTEXT N_( \
131 "Red balance of the video input (if supported by the v4l2 driver)." )
132 #define BLUEBALANCE_TEXT N_( "Blue balance" )
133 #define BLUEBALANCE_LONGTEXT N_( \
134 "Blue balance of the video input (if supported by the v4l2 driver)." )
135 #define GAMMA_TEXT N_( "Gamma" )
136 #define GAMMA_LONGTEXT N_( \
137 "Gamma of the video input (if supported by the v4l2 driver)." )
138 #define EXPOSURE_TEXT N_( "Exposure" )
139 #define EXPOSURE_LONGTEXT N_( \
140 "Exposure of the video input (if supported by the v4L2 driver)." )
141 #define AUTOGAIN_TEXT N_( "Auto gain" )
142 #define AUTOGAIN_LONGTEXT N_( \
143 "Automatically set the video input's gain (if supported by the " \
145 #define GAIN_TEXT N_( "Gain" )
146 #define GAIN_LONGTEXT N_( \
147 "Video input's gain (if supported by the v4l2 driver)." )
148 #define HFLIP_TEXT N_( "Horizontal flip" )
149 #define HFLIP_LONGTEXT N_( \
150 "Flip the video horizontally (if supported by the v4l2 driver)." )
151 #define VFLIP_TEXT N_( "Vertical flip" )
152 #define VFLIP_LONGTEXT N_( \
153 "Flip the video vertically (if supported by the v4l2 driver)." )
154 #define HCENTER_TEXT N_( "Horizontal centering" )
155 #define HCENTER_LONGTEXT N_( \
156 "Set the camera's horizontal centering (if supported by the v4l2 driver)." )
157 #define VCENTER_TEXT N_( "Vertical centering" )
158 #define VCENTER_LONGTEXT N_( \
159 "Set the camera's vertical centering (if supported by the v4l2 driver)." )
161 #define AUDIO_VOLUME_TEXT N_( "Volume" )
162 #define AUDIO_VOLUME_LONGTEXT N_( \
163 "Volume of the audio input (if supported by the v4l2 driver)." )
164 #define AUDIO_BALANCE_TEXT N_( "Balance" )
165 #define AUDIO_BALANCE_LONGTEXT N_( \
166 "Balance of the audio input (if supported by the v4l2 driver)." )
167 #define AUDIO_MUTE_TEXT N_( "Mute" )
168 #define AUDIO_MUTE_LONGTEXT N_( \
169 "Mute audio input (if supported by the v4l2 driver)." )
170 #define AUDIO_BASS_TEXT N_( "Bass" )
171 #define AUDIO_BASS_LONGTEXT N_( \
172 "Bass level of the audio input (if supported by the v4l2 driver)." )
173 #define AUDIO_TREBLE_TEXT N_( "Treble" )
174 #define AUDIO_TREBLE_LONGTEXT N_( \
175 "Treble level of the audio input (if supported by the v4l2 driver)." )
176 #define AUDIO_LOUDNESS_TEXT N_( "Loudness" )
177 #define AUDIO_LOUDNESS_LONGTEXT N_( \
178 "Loudness of the audio input (if supported by the v4l2 driver)." )
180 #define CACHING_TEXT N_("Caching value in ms")
181 #define CACHING_LONGTEXT N_( \
182 "Caching value for V4L2 captures. This " \
183 "value should be set in milliseconds." )
184 #define S_CTRLS_TEXT N_("v4l2 driver controls")
185 #define S_CTRLS_LONGTEXT N_( \
186 "Set the v4l2 driver controls to the values specified using a comma " \
187 "separated list optionally encapsulated by curly braces " \
188 "(e.g.: {video_bitrate=6000000,audio_crc=0,stream_type=3} ). " \
189 "To list available controls, increase verbosity (-vvv) " \
190 "or use the v4l2-ctl application." )
192 #define TUNER_TEXT N_("Tuner id")
193 #define TUNER_LONGTEXT N_( \
194 "Tuner id (see debug output)." )
195 #define FREQUENCY_TEXT N_("Frequency")
196 #define FREQUENCY_LONGTEXT N_( \
197 "Tuner frequency in Hz or kHz (see debug output)" )
198 #define TUNER_AUDIO_MODE_TEXT N_("Audio mode")
199 #define TUNER_AUDIO_MODE_LONGTEXT N_( \
200 "Tuner audio mono/stereo and track selection." )
202 #define AUDIO_DEPRECATED_ERROR N_( \
203 "Alsa or OSS audio capture in the v4l2 access is deprecated. " \
204 "please use 'v4l2:/""/ :input-slave=alsa:/""/' or " \
205 "'v4l2:/""/ :input-slave=oss:/""/' instead." )
213 static const int i_standards_list[] =
214 { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC };
215 static const char *const psz_standards_list_text[] =
216 { N_("Default"), N_("SECAM"), N_("PAL"), N_("NTSC") };
218 static const int i_iomethod_list[] =
219 { IO_METHOD_READ, IO_METHOD_MMAP, IO_METHOD_USERPTR };
220 static const char *const psz_iomethod_list_text[] =
221 { N_("READ"), N_("MMAP"), N_("USERPTR") };
223 static const int i_tuner_audio_modes_list[] =
224 { V4L2_TUNER_MODE_MONO, V4L2_TUNER_MODE_STEREO,
225 V4L2_TUNER_MODE_LANG1, V4L2_TUNER_MODE_LANG2,
226 V4L2_TUNER_MODE_SAP, V4L2_TUNER_MODE_LANG1_LANG2 };
227 static const char *const psz_tuner_audio_modes_list_text[] =
230 N_( "Primary language (Analog TV tuners only)" ),
231 N_( "Secondary language (Analog TV tuners only)" ),
232 N_( "Second audio program (Analog TV tuners only)" ),
233 N_( "Primary language left, Secondary language right" ) };
235 #define V4L2_DEFAULT "/dev/video0"
236 #define CFG_PREFIX "v4l2-"
239 set_shortname( N_("Video4Linux2") )
240 set_description( N_("Video4Linux2 input") )
241 set_category( CAT_INPUT )
242 set_subcategory( SUBCAT_INPUT_ACCESS )
244 set_section( N_( "Video input" ), NULL )
245 add_integer( CFG_PREFIX "standard", 0, NULL, STANDARD_TEXT,
246 STANDARD_LONGTEXT, false );
247 change_integer_list( i_standards_list, psz_standards_list_text, NULL );
248 add_string( CFG_PREFIX "chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
250 add_integer( CFG_PREFIX "input", 0, NULL, INPUT_TEXT, INPUT_LONGTEXT,
252 add_integer( CFG_PREFIX "audio-input", 0, NULL, AUDIO_INPUT_TEXT,
253 AUDIO_INPUT_LONGTEXT, true );
254 add_integer( CFG_PREFIX "io", IO_METHOD_MMAP, NULL, IOMETHOD_TEXT,
255 IOMETHOD_LONGTEXT, true );
256 change_integer_list( i_iomethod_list, psz_iomethod_list_text, NULL );
257 add_integer( CFG_PREFIX "width", -1, NULL, WIDTH_TEXT,
258 WIDTH_LONGTEXT, true );
259 add_integer( CFG_PREFIX "height", -1, NULL, HEIGHT_TEXT,
260 HEIGHT_LONGTEXT, true );
261 add_float( CFG_PREFIX "fps", 0, NULL, FPS_TEXT, FPS_LONGTEXT, true )
262 add_integer( CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
263 CACHING_TEXT, CACHING_LONGTEXT, true );
265 set_section( N_( "Tuner" ), NULL )
266 add_integer( CFG_PREFIX "tuner", 0, NULL, TUNER_TEXT, TUNER_LONGTEXT,
268 add_integer( CFG_PREFIX "tuner-frequency", -1, NULL, FREQUENCY_TEXT,
269 FREQUENCY_LONGTEXT, true );
270 add_integer( CFG_PREFIX "tuner-audio-mode", -1, NULL, TUNER_AUDIO_MODE_TEXT,
271 TUNER_AUDIO_MODE_LONGTEXT, true );
272 change_integer_list( i_tuner_audio_modes_list,
273 psz_tuner_audio_modes_list_text, 0 );
275 set_section( N_( "Controls" ),
276 N_( "v4l2 driver controls, if supported by your v4l2 driver." ) );
277 add_bool( CFG_PREFIX "controls-reset", false, NULL, CTRL_RESET_TEXT,
278 CTRL_RESET_LONGTEXT, true );
279 add_integer( CFG_PREFIX "brightness", -1, NULL, BRIGHTNESS_TEXT,
280 BRIGHTNESS_LONGTEXT, true );
281 add_integer( CFG_PREFIX "contrast", -1, NULL, CONTRAST_TEXT,
282 CONTRAST_LONGTEXT, true );
283 add_integer( CFG_PREFIX "saturation", -1, NULL, SATURATION_TEXT,
284 SATURATION_LONGTEXT, true );
285 add_integer( CFG_PREFIX "hue", -1, NULL, HUE_TEXT,
286 HUE_LONGTEXT, true );
287 add_integer( CFG_PREFIX "black-level", -1, NULL, BLACKLEVEL_TEXT,
288 BLACKLEVEL_LONGTEXT, true );
289 add_integer( CFG_PREFIX "auto-white-balance", -1, NULL,
290 AUTOWHITEBALANCE_TEXT, AUTOWHITEBALANCE_LONGTEXT, true );
291 add_integer( CFG_PREFIX "do-white-balance", -1, NULL, DOWHITEBALANCE_TEXT,
292 DOWHITEBALANCE_LONGTEXT, true );
293 add_integer( CFG_PREFIX "red-balance", -1, NULL, REDBALANCE_TEXT,
294 REDBALANCE_LONGTEXT, true );
295 add_integer( CFG_PREFIX "blue-balance", -1, NULL, BLUEBALANCE_TEXT,
296 BLUEBALANCE_LONGTEXT, true );
297 add_integer( CFG_PREFIX "gamma", -1, NULL, GAMMA_TEXT,
298 GAMMA_LONGTEXT, true );
299 add_integer( CFG_PREFIX "exposure", -1, NULL, EXPOSURE_TEXT,
300 EXPOSURE_LONGTEXT, true );
301 add_integer( CFG_PREFIX "autogain", -1, NULL, AUTOGAIN_TEXT,
302 AUTOGAIN_LONGTEXT, true );
303 add_integer( CFG_PREFIX "gain", -1, NULL, GAIN_TEXT,
304 GAIN_LONGTEXT, true );
305 add_integer( CFG_PREFIX "hflip", -1, NULL, HFLIP_TEXT,
306 HFLIP_LONGTEXT, true );
307 add_integer( CFG_PREFIX "vflip", -1, NULL, VFLIP_TEXT,
308 VFLIP_LONGTEXT, true );
309 add_integer( CFG_PREFIX "hcenter", -1, NULL, HCENTER_TEXT,
310 HCENTER_LONGTEXT, true );
311 add_integer( CFG_PREFIX "vcenter", -1, NULL, VCENTER_TEXT,
312 VCENTER_LONGTEXT, true );
313 add_integer( CFG_PREFIX "audio-volume", -1, NULL, AUDIO_VOLUME_TEXT,
314 AUDIO_VOLUME_LONGTEXT, true );
315 add_integer( CFG_PREFIX "audio-balance", -1, NULL, AUDIO_BALANCE_TEXT,
316 AUDIO_BALANCE_LONGTEXT, true );
317 add_bool( CFG_PREFIX "audio-mute", false, NULL, AUDIO_MUTE_TEXT,
318 AUDIO_MUTE_LONGTEXT, true );
319 add_integer( CFG_PREFIX "audio-bass", -1, NULL, AUDIO_BASS_TEXT,
320 AUDIO_BASS_LONGTEXT, true );
321 add_integer( CFG_PREFIX "audio-treble", -1, NULL, AUDIO_TREBLE_TEXT,
322 AUDIO_TREBLE_LONGTEXT, true );
323 add_integer( CFG_PREFIX "audio-loudness", -1, NULL, AUDIO_LOUDNESS_TEXT,
324 AUDIO_LOUDNESS_LONGTEXT, true );
325 add_string( CFG_PREFIX "set-ctrls", NULL, NULL, S_CTRLS_TEXT,
326 S_CTRLS_LONGTEXT, true );
328 add_obsolete_string( CFG_PREFIX "dev" );
330 add_obsolete_string( CFG_PREFIX "adev" );
331 add_obsolete_integer( CFG_PREFIX "audio-method" );
332 add_obsolete_bool( CFG_PREFIX "stereo" );
333 add_obsolete_integer( CFG_PREFIX "samplerate" );
335 add_shortcut( "v4l2" )
336 set_capability( "access_demux", 10 )
337 set_callbacks( DemuxOpen, DemuxClose )
340 add_shortcut( "v4l2c" )
341 set_description( N_("Video4Linux2 Compressed A/V") )
342 set_capability( "access", 0 )
343 /* use these when open as access_demux fails; VLC will use another demux */
344 set_callbacks( AccessOpen, AccessClose )
348 /*****************************************************************************
349 * Access: local prototypes
350 *****************************************************************************/
352 static void CommonClose( vlc_object_t *, demux_sys_t * );
353 static void ParseMRL( demux_sys_t *, char *, vlc_object_t * );
354 static void GetV4L2Params( demux_sys_t *, vlc_object_t * );
355 static void SetAvailControlsByString( vlc_object_t *, demux_sys_t *, int );
357 static int DemuxControl( demux_t *, int, va_list );
358 static int AccessControl( access_t *, int, va_list );
360 static int Demux( demux_t * );
361 static ssize_t AccessRead( access_t *, uint8_t *, size_t );
363 static block_t* GrabVideo( demux_t *p_demux );
364 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t );
366 static bool IsPixelFormatSupported( demux_t *p_demux,
367 unsigned int i_pixelformat );
369 static int OpenVideoDev( vlc_object_t *, demux_sys_t *, bool );
370 static bool ProbeVideoDev( vlc_object_t *, demux_sys_t *,
371 const char *psz_device );
373 static int ControlList( vlc_object_t *, int , bool, bool );
374 static int Control( vlc_object_t *, int i_fd,
375 const char *psz_name, int i_cid, int i_value );
377 static int DemuxControlCallback( vlc_object_t *p_this, const char *psz_var,
378 vlc_value_t oldval, vlc_value_t newval,
380 static int DemuxControlResetCallback( vlc_object_t *p_this, const char *psz_var,
381 vlc_value_t oldval, vlc_value_t newval,
383 static int AccessControlCallback( vlc_object_t *p_this, const char *psz_var,
384 vlc_value_t oldval, vlc_value_t newval,
386 static int AccessControlResetCallback( vlc_object_t *p_this,
387 const char *psz_var, vlc_value_t oldval,
388 vlc_value_t newval, void *p_data );
397 } v4l2chroma_to_fourcc[] =
400 { V4L2_PIX_FMT_GREY, VLC_FOURCC('G','R','E','Y'), 0, 0, 0 },
401 { V4L2_PIX_FMT_HI240, VLC_FOURCC('I','2','4','0'), 0, 0, 0 },
402 { V4L2_PIX_FMT_RGB555, VLC_FOURCC('R','V','1','5'), 0x001f,0x03e0,0x7c00 },
403 { V4L2_PIX_FMT_RGB565, VLC_FOURCC('R','V','1','6'), 0x001f,0x07e0,0xf800 },
404 /* Won't work since we don't know how to handle such gmask values
406 { V4L2_PIX_FMT_RGB555X, VLC_FOURCC('R','V','1','5'), 0x007c,0xe003,0x1f00 },
407 { V4L2_PIX_FMT_RGB565X, VLC_FOURCC('R','V','1','6'), 0x00f8,0xe007,0x1f00 },
409 { V4L2_PIX_FMT_BGR24, VLC_FOURCC('R','V','2','4'), 0xff0000,0xff00,0xff },
410 { V4L2_PIX_FMT_RGB24, VLC_FOURCC('R','V','2','4'), 0xff,0xff00,0xff0000 },
411 { V4L2_PIX_FMT_BGR32, VLC_FOURCC('R','V','3','2'), 0xff0000,0xff00,0xff },
412 { V4L2_PIX_FMT_RGB32, VLC_FOURCC('R','V','3','2'), 0xff,0xff00,0xff0000 },
413 { V4L2_PIX_FMT_YUYV, VLC_FOURCC('Y','U','Y','2'), 0, 0, 0 },
414 { V4L2_PIX_FMT_YUYV, VLC_FOURCC('Y','U','Y','V'), 0, 0, 0 },
415 { V4L2_PIX_FMT_UYVY, VLC_FOURCC('U','Y','V','Y'), 0, 0, 0 },
416 { V4L2_PIX_FMT_Y41P, VLC_FOURCC('I','4','1','N'), 0, 0, 0 },
417 { V4L2_PIX_FMT_YUV422P, VLC_FOURCC('I','4','2','2'), 0, 0, 0 },
418 { V4L2_PIX_FMT_YVU420, VLC_FOURCC('Y','V','1','2'), 0, 0, 0 },
419 { V4L2_PIX_FMT_YUV411P, VLC_FOURCC('I','4','1','1'), 0, 0, 0 },
420 { V4L2_PIX_FMT_YUV410, VLC_FOURCC('I','4','1','0'), 0, 0, 0 },
422 /* Raw data types, not in V4L2 spec but still in videodev2.h and supported
424 { V4L2_PIX_FMT_YUV420, VLC_FOURCC('I','4','2','0'), 0, 0, 0 },
425 /* FIXME { V4L2_PIX_FMT_RGB444, VLC_FOURCC('R','V','3','2') }, */
427 /* Compressed data types */
428 { V4L2_PIX_FMT_MJPEG, VLC_FOURCC('M','J','P','G'), 0, 0, 0 },
430 { V4L2_PIX_FMT_JPEG, VLC_FOURCC('J','P','E','G') },
431 { V4L2_PIX_FMT_DV, VLC_FOURCC('?','?','?','?') },
432 { V4L2_PIX_FMT_MPEG, VLC_FOURCC('?','?','?','?') },
438 * List of V4L2 chromas were confident enough to use as fallbacks if the
439 * user hasn't provided a --v4l2-chroma value.
441 * Try YUV chromas first, then RGB little endian and MJPEG as last resort.
443 static const __u32 p_chroma_fallbacks[] =
444 { V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_YVU420, V4L2_PIX_FMT_YUV422P,
445 V4L2_PIX_FMT_YUYV, V4L2_PIX_FMT_UYVY, V4L2_PIX_FMT_BGR24,
446 V4L2_PIX_FMT_BGR32, V4L2_PIX_FMT_MJPEG };
450 const char *psz_name;
454 { "brightness", V4L2_CID_BRIGHTNESS },
455 { "contrast", V4L2_CID_CONTRAST },
456 { "saturation", V4L2_CID_SATURATION },
457 { "hue", V4L2_CID_HUE },
458 { "audio-volume", V4L2_CID_AUDIO_VOLUME },
459 { "audio-balance", V4L2_CID_AUDIO_BALANCE },
460 { "audio-bass", V4L2_CID_AUDIO_BASS },
461 { "audio-treble", V4L2_CID_AUDIO_TREBLE },
462 { "audio-mute", V4L2_CID_AUDIO_MUTE },
463 { "audio-loudness", V4L2_CID_AUDIO_LOUDNESS },
464 { "black-level", V4L2_CID_BLACK_LEVEL },
465 { "auto-white-balance", V4L2_CID_AUTO_WHITE_BALANCE },
466 { "do-white-balance", V4L2_CID_DO_WHITE_BALANCE },
467 { "red-balance", V4L2_CID_RED_BALANCE },
468 { "blue-balance", V4L2_CID_BLUE_BALANCE },
469 { "gamma", V4L2_CID_GAMMA },
470 { "exposure", V4L2_CID_EXPOSURE },
471 { "autogain", V4L2_CID_AUTOGAIN },
472 { "gain", V4L2_CID_GAIN },
473 { "hflip", V4L2_CID_HFLIP },
474 { "vflip", V4L2_CID_VFLIP },
475 { "hcenter", V4L2_CID_HCENTER },
476 { "vcenter", V4L2_CID_VCENTER },
488 char *psz_device; /* Main device from MRL */
491 char *psz_requested_chroma;
498 struct v4l2_capability dev_cap;
501 struct v4l2_input *p_inputs;
502 int i_selected_input;
505 struct v4l2_standard *p_standards;
506 v4l2_std_id i_selected_standard_id;
509 /* V4L2 devices cannot have more than 32 audio inputs */
510 struct v4l2_audio p_audios[32];
511 int i_selected_audio_input;
514 struct v4l2_tuner *p_tuners;
517 struct v4l2_fmtdesc *p_codecs;
519 struct buffer_t *p_buffers;
520 unsigned int i_nbuffers;
524 float f_fps; /* <= 0.0 mean to grab at full rate */
525 mtime_t i_video_pts; /* only used when f_fps > 0 */
539 static int FindMainDevice( vlc_object_t *p_this, demux_sys_t *p_sys,
542 /* TODO: if using default device, loop through all /dev/video* until
544 msg_Dbg( p_this, "opening device '%s'", p_sys->psz_device );
545 if( ProbeVideoDev( p_this, p_sys, p_sys->psz_device ) )
547 msg_Dbg( p_this, "'%s' is a video device", p_sys->psz_device );
548 p_sys->i_fd = OpenVideoDev( p_this, p_sys, b_demux );
551 if( p_sys->i_fd < 0 ) return VLC_EGENERIC;
555 /*****************************************************************************
556 * DemuxOpen: opens v4l2 device, access_demux callback
557 *****************************************************************************
559 * url: <video device>::::
561 *****************************************************************************/
562 static int DemuxOpen( vlc_object_t *p_this )
564 demux_t *p_demux = (demux_t*)p_this;
567 /* Only when selected */
568 if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;
571 p_demux->pf_control = DemuxControl;
572 p_demux->pf_demux = Demux;
573 p_demux->info.i_update = 0;
574 p_demux->info.i_title = 0;
575 p_demux->info.i_seekpoint = 0;
577 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
578 if( p_sys == NULL ) return VLC_ENOMEM;
580 GetV4L2Params(p_sys, (vlc_object_t *) p_demux);
582 ParseMRL( p_sys, p_demux->psz_path, (vlc_object_t *) p_demux );
584 if( FindMainDevice( p_this, p_sys, true ) != VLC_SUCCESS)
586 DemuxClose( p_this );
593 /*****************************************************************************
594 * GetV4L2Params: fill in p_sys parameters (shared by DemuxOpen and AccessOpen)
595 *****************************************************************************/
596 static void GetV4L2Params( demux_sys_t *p_sys, vlc_object_t *p_obj )
598 p_sys->i_video_pts = -1;
600 p_sys->i_selected_standard_id =
601 i_standards_list[var_CreateGetInteger( p_obj, "v4l2-standard" )];
603 p_sys->i_selected_input = var_CreateGetInteger( p_obj, "v4l2-input" );
604 p_sys->i_selected_audio_input =
605 var_CreateGetInteger( p_obj, "v4l2-audio-input" );
607 p_sys->io = var_CreateGetInteger( p_obj, "v4l2-io" );
609 p_sys->i_width = var_CreateGetInteger( p_obj, "v4l2-width" );
610 p_sys->i_height = var_CreateGetInteger( p_obj, "v4l2-height" );
612 var_Create( p_obj, "v4l2-controls-reset", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
614 p_sys->f_fps = var_CreateGetFloat( p_obj, "v4l2-fps" );
615 p_sys->psz_requested_chroma = var_CreateGetString( p_obj, "v4l2-chroma" );
617 p_sys->i_cache = var_CreateGetInteger( p_obj, "v4l2-caching" );
619 p_sys->i_cur_tuner = var_CreateGetInteger( p_obj, "v4l2-tuner" );
620 p_sys->i_frequency = var_CreateGetInteger( p_obj, "v4l2-tuner-frequency" );
621 p_sys->i_audio_mode = var_CreateGetInteger( p_obj, "v4l2-tuner-audio-mode" );
623 p_sys->psz_set_ctrls = var_CreateGetString( p_obj, "v4l2-set-ctrls" );
625 p_sys->psz_device = NULL;
631 /*****************************************************************************
632 * ParseMRL: parse the options contained in the MRL
633 *****************************************************************************/
634 static void ParseMRL( demux_sys_t *p_sys, char *psz_path, vlc_object_t *p_obj )
636 char *psz_dup = strdup( psz_path );
637 char *psz_parser = psz_dup;
639 while( *psz_parser && *psz_parser != ':' )
644 if( *psz_parser == ':' )
649 *psz_parser++ = '\0';
651 if( !strncmp( psz_parser, "standard=", strlen( "standard=" ) ) )
653 psz_parser += strlen( "standard=" );
654 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
656 p_sys->i_selected_standard_id = V4L2_STD_PAL;
657 psz_parser += strlen( "pal" );
659 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
661 p_sys->i_selected_standard_id = V4L2_STD_NTSC;
662 psz_parser += strlen( "ntsc" );
664 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
666 p_sys->i_selected_standard_id = V4L2_STD_SECAM;
667 psz_parser += strlen( "secam" );
669 else if( !strncmp( psz_parser, "default", strlen( "default" ) ) )
671 p_sys->i_selected_standard_id = V4L2_STD_UNKNOWN;
672 psz_parser += strlen( "default" );
676 p_sys->i_selected_standard_id = i_standards_list[strtol( psz_parser, &psz_parser, 0 )];
679 else if( !strncmp( psz_parser, "chroma=", strlen( "chroma=" ) ) )
683 psz_parser += strlen( "chroma=" );
684 if( strchr( psz_parser, ':' ) )
686 i_len = strchr( psz_parser, ':' ) - psz_parser;
690 i_len = strlen( psz_parser );
693 free( p_sys->psz_requested_chroma );
694 p_sys->psz_requested_chroma = strndup( psz_parser, i_len );
698 else if( !strncmp( psz_parser, "input=", strlen( "input=" ) ) )
700 p_sys->i_selected_input = strtol( psz_parser + strlen( "input=" ),
703 else if( !strncmp( psz_parser, "audio-input=", strlen( "audio-input=" ) ) )
705 p_sys->i_selected_audio_input = strtol( psz_parser + strlen( "audio-input=" ),
708 else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
710 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
713 else if( !strncmp( psz_parser, "io=", strlen( "io=" ) ) )
715 psz_parser += strlen( "io=" );
716 if( !strncmp( psz_parser, "read", strlen( "read" ) ) )
718 p_sys->io = IO_METHOD_READ;
719 psz_parser += strlen( "read" );
721 else if( !strncmp( psz_parser, "mmap", strlen( "mmap" ) ) )
723 p_sys->io = IO_METHOD_MMAP;
724 psz_parser += strlen( "mmap" );
726 else if( !strncmp( psz_parser, "userptr", strlen( "userptr" ) ) )
728 p_sys->io = IO_METHOD_USERPTR;
729 psz_parser += strlen( "userptr" );
733 p_sys->io = strtol( psz_parser, &psz_parser, 0 );
736 else if( !strncmp( psz_parser, "width=",
737 strlen( "width=" ) ) )
740 strtol( psz_parser + strlen( "width=" ),
743 else if( !strncmp( psz_parser, "height=",
744 strlen( "height=" ) ) )
747 strtol( psz_parser + strlen( "height=" ),
750 else if( !strncmp( psz_parser, "controls-reset",
751 strlen( "controls-reset" ) ) )
753 var_SetBool( p_obj, "v4l2-controls-reset", true );
754 psz_parser += strlen( "controls-reset" );
757 else if( !strncmp( psz_parser, "brightness=",
758 strlen( "brightness=" ) ) )
760 var_SetInteger( p_obj, "brightness",
761 strtol( psz_parser + strlen( "brightness=" ),
764 else if( !strncmp( psz_parser, "contrast=",
765 strlen( "contrast=" ) ) )
767 var_SetInteger( p_obj, "contrast",
768 strtol( psz_parser + strlen( "contrast=" ),
771 else if( !strncmp( psz_parser, "saturation=",
772 strlen( "saturation=" ) ) )
774 var_SetInteger( p_obj, "saturation",
775 strtol( psz_parser + strlen( "saturation=" ),
778 else if( !strncmp( psz_parser, "hue=",
781 var_SetInteger( p_obj, "hue",
782 strtol( psz_parser + strlen( "hue=" ),
785 else if( !strncmp( psz_parser, "gamma=",
786 strlen( "gamma=" ) ) )
788 var_SetInteger( p_obj, "gamma",
789 strtol( psz_parser + strlen( "gamma=" ),
793 else if( !strncmp( psz_parser, "caching=", strlen( "caching=" ) ) )
795 p_sys->i_cache = strtol( psz_parser + strlen( "caching=" ),
798 else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
800 p_sys->i_cur_tuner = strtol( psz_parser + strlen( "tuner=" ),
803 else if( !strncmp( psz_parser, "tuner-frequency=", strlen( "tuner-frequency=" ) ) )
805 p_sys->i_frequency = strtol( psz_parser
806 + strlen( "tuner-frequency=" ),
809 else if( !strncmp( psz_parser, "tuner-audio-mode=", strlen( "tuner-audio-mode=" ) ) )
811 p_sys->i_audio_mode = strtol( psz_parser
812 + strlen( "tuner-audio-mode=" ),
815 else if( !strncmp( psz_parser, "set-ctrls=", strlen( "set-ctrls=" )) )
819 psz_parser += strlen( "set-ctrls=" );
820 if( strchr( psz_parser, ':' ) )
822 i_len = strchr( psz_parser, ':' ) - psz_parser;
826 i_len = strlen( psz_parser );
829 p_sys->psz_set_ctrls = strndup( psz_parser, i_len );
833 else if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) )
834 || !strncmp( psz_parser, "samplerate=", strlen( "samplerate=" ) )
835 || !strncmp( psz_parser, "audio-method", strlen( "audio-method" ) )
836 || !strncmp( psz_parser, "stereo", strlen( "stereo" ) )
837 || !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
839 if( strchr( psz_parser, ':' ) )
841 psz_parser = strchr( psz_parser, ':' );
845 psz_parser += strlen( psz_parser );
848 msg_Err( p_obj, AUDIO_DEPRECATED_ERROR );
852 char *psz_unk = strchr( psz_parser, ':' );
854 psz_unk = strndup( psz_parser, psz_unk - psz_parser );
856 psz_unk = strdup( psz_parser);
857 msg_Warn( p_obj, "unknown option %s", psz_unk );
861 while( *psz_parser && *psz_parser != ':' )
866 if( *psz_parser == '\0' )
875 p_sys->psz_device = strdup( psz_dup );
877 p_sys->psz_device = strdup( V4L2_DEFAULT );
881 /*****************************************************************************
882 * Close: close device, free resources
883 *****************************************************************************/
884 static void AccessClose( vlc_object_t *p_this )
886 access_t *p_access = (access_t *)p_this;
887 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
889 CommonClose( p_this, p_sys );
892 static void DemuxClose( vlc_object_t *p_this )
894 struct v4l2_buffer buf;
895 enum v4l2_buf_type buf_type;
898 demux_t *p_demux = (demux_t *)p_this;
899 demux_sys_t *p_sys = p_demux->p_sys;
901 /* Stop video capture */
902 if( p_sys->i_fd >= 0 )
911 case IO_METHOD_USERPTR:
912 /* Some drivers 'hang' internally if this is not done before streamoff */
913 for( unsigned int i = 0; i < p_sys->i_nbuffers; i++ )
915 memset( &buf, 0, sizeof(buf) );
916 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
917 buf.memory = ( p_sys->io == IO_METHOD_USERPTR ) ?
918 V4L2_MEMORY_USERPTR : V4L2_MEMORY_MMAP;
919 v4l2_ioctl( p_sys->i_fd, VIDIOC_DQBUF, &buf ); /* ignore result */
922 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
923 if( v4l2_ioctl( p_sys->i_fd, VIDIOC_STREAMOFF, &buf_type ) < 0 ) {
924 msg_Err( p_this, "VIDIOC_STREAMOFF failed" );
931 /* Free Video Buffers */
932 if( p_sys->p_buffers ) {
936 free( p_sys->p_buffers[0].start );
940 for( i = 0; i < p_sys->i_nbuffers; ++i )
942 if( v4l2_munmap( p_sys->p_buffers[i].start, p_sys->p_buffers[i].length ) )
944 msg_Err( p_this, "munmap failed" );
949 case IO_METHOD_USERPTR:
950 for( i = 0; i < p_sys->i_nbuffers; ++i )
952 free( p_sys->p_buffers[i].start );
956 free( p_sys->p_buffers );
959 CommonClose( p_this, p_sys );
962 static void CommonClose( vlc_object_t *p_this, demux_sys_t *p_sys )
966 if( p_sys->i_fd >= 0 ) v4l2_close( p_sys->i_fd );
967 free( p_sys->psz_device );
968 free( p_sys->p_standards );
969 free( p_sys->p_inputs );
970 free( p_sys->p_tuners );
971 free( p_sys->p_codecs );
972 free( p_sys->psz_requested_chroma );
973 free( p_sys->psz_set_ctrls );
978 /*****************************************************************************
979 * AccessOpen: opens v4l2 device, access callback
980 *****************************************************************************
982 * url: <video device>::::
984 *****************************************************************************/
985 static int AccessOpen( vlc_object_t * p_this )
987 access_t *p_access = (access_t*) p_this;
990 /* Only when selected */
991 if( *p_access->psz_access == '\0' ) return VLC_EGENERIC;
993 p_access->pf_read = AccessRead;
994 p_access->pf_block = NULL;
995 p_access->pf_seek = NULL;
996 p_access->pf_control = AccessControl;
997 p_access->info.i_update = 0;
998 p_access->info.i_size = 0;
999 p_access->info.i_pos = 0;
1000 p_access->info.b_eof = false;
1001 p_access->info.i_title = 0;
1002 p_access->info.i_seekpoint = 0;
1004 p_sys = calloc( 1, sizeof( demux_sys_t ) );
1005 p_access->p_sys = (access_sys_t *) p_sys;
1006 if( p_sys == NULL ) return VLC_ENOMEM;
1008 GetV4L2Params( p_sys, (vlc_object_t *) p_access );
1010 ParseMRL( p_sys, p_access->psz_path, (vlc_object_t *) p_access );
1012 if( FindMainDevice( p_this, p_sys, false ) != VLC_SUCCESS )
1014 AccessClose( p_this );
1015 return VLC_EGENERIC;
1021 /*****************************************************************************
1023 *****************************************************************************/
1024 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
1026 demux_sys_t *p_sys = p_demux->p_sys;
1032 /* Special for access_demux */
1033 case DEMUX_CAN_PAUSE:
1034 case DEMUX_CAN_SEEK:
1035 case DEMUX_SET_PAUSE_STATE:
1036 case DEMUX_CAN_CONTROL_PACE:
1037 pb = (bool*)va_arg( args, bool * );
1041 case DEMUX_GET_PTS_DELAY:
1042 pi64 = (int64_t*)va_arg( args, int64_t * );
1043 *pi64 = (int64_t)p_sys->i_cache * 1000;
1046 case DEMUX_GET_TIME:
1047 pi64 = (int64_t*)va_arg( args, int64_t * );
1051 /* TODO implement others */
1053 return VLC_EGENERIC;
1056 return VLC_EGENERIC;
1059 /*****************************************************************************
1060 * AccessControl: access callback
1061 *****************************************************************************/
1062 static int AccessControl( access_t *p_access, int i_query, va_list args )
1067 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1072 case ACCESS_CAN_SEEK:
1073 case ACCESS_CAN_FASTSEEK:
1074 pb_bool = (bool*)va_arg( args, bool* );
1077 case ACCESS_CAN_PAUSE:
1078 pb_bool = (bool*)va_arg( args, bool* );
1081 case ACCESS_CAN_CONTROL_PACE:
1082 pb_bool = (bool*)va_arg( args, bool* );
1087 case ACCESS_GET_MTU:
1088 pi_int = (int*)va_arg( args, int * );
1092 case ACCESS_GET_PTS_DELAY:
1093 pi_64 = (int64_t*)va_arg( args, int64_t * );
1094 *pi_64 = (int64_t) p_sys->i_cache * 1000;
1098 case ACCESS_SET_PAUSE_STATE:
1102 case ACCESS_GET_TITLE_INFO:
1103 case ACCESS_SET_TITLE:
1104 case ACCESS_SET_SEEKPOINT:
1105 case ACCESS_SET_PRIVATE_ID_STATE:
1106 case ACCESS_GET_CONTENT_TYPE:
1107 case ACCESS_GET_META:
1108 return VLC_EGENERIC;
1111 msg_Warn( p_access, "Unimplemented query in control(%d).", i_query);
1112 return VLC_EGENERIC;
1118 /*****************************************************************************
1119 * AccessRead: access callback
1120 ******************************************************************************/
1121 static ssize_t AccessRead( access_t * p_access, uint8_t * p_buffer, size_t i_len )
1123 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1127 ufd.fd = p_sys->i_fd;
1128 ufd.events = POLLIN;
1130 if( p_access->info.b_eof )
1135 if( !vlc_object_alive (p_access) )
1140 while( ( i_ret = poll( &ufd, 1, 500 ) ) == 0 );
1144 msg_Err( p_access, "Polling error (%m)." );
1148 i_ret = v4l2_read( p_sys->i_fd, p_buffer, i_len );
1151 p_access->info.b_eof = true;
1153 else if( i_ret > 0 )
1155 p_access->info.i_pos += i_ret;
1161 /*****************************************************************************
1162 * Demux: Processes the audio or video frame
1163 *****************************************************************************/
1164 static int Demux( demux_t *p_demux )
1166 demux_sys_t *p_sys = p_demux->p_sys;
1169 fd.fd = p_sys->i_fd;
1170 fd.events = POLLIN|POLLPRI;
1174 if( poll( &fd, 1, 500 ) ) /* Timeout after 0.5 seconds since I don't know if pf_demux can be blocking. */
1176 if( fd.revents & (POLLIN|POLLPRI) )
1178 block_t *p_block = GrabVideo( p_demux );
1181 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
1182 es_out_Send( p_demux->out, p_sys->p_es, p_block );
1190 /*****************************************************************************
1191 * GrabVideo: Grab a video frame
1192 *****************************************************************************/
1193 static block_t* GrabVideo( demux_t *p_demux )
1195 demux_sys_t *p_sys = p_demux->p_sys;
1197 block_t *p_block = NULL;
1198 struct v4l2_buffer buf;
1201 if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
1203 mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
1205 /* Did we wait long enough ? (frame rate reduction) */
1206 if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
1209 /* Grab Video Frame */
1212 case IO_METHOD_READ:
1213 i_ret = v4l2_read( p_sys->i_fd, p_sys->p_buffers[0].start, p_sys->p_buffers[0].length );
1221 /* Could ignore EIO, see spec. */
1224 msg_Err( p_demux, "Failed to read frame" );
1229 p_block = ProcessVideoFrame( p_demux, (uint8_t*)p_sys->p_buffers[0].start, i_ret );
1230 if( !p_block ) return 0;
1234 case IO_METHOD_MMAP:
1235 memset( &buf, 0, sizeof(buf) );
1236 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1237 buf.memory = V4L2_MEMORY_MMAP;
1239 /* Wait for next frame */
1240 if (v4l2_ioctl( p_sys->i_fd, VIDIOC_DQBUF, &buf ) < 0 )
1247 /* Could ignore EIO, see spec. */
1250 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
1255 if( buf.index >= p_sys->i_nbuffers ) {
1256 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
1260 p_block = ProcessVideoFrame( p_demux, p_sys->p_buffers[buf.index].start, buf.bytesused );
1261 if( !p_block ) return 0;
1264 if( v4l2_ioctl( p_sys->i_fd, VIDIOC_QBUF, &buf ) < 0 )
1266 msg_Err( p_demux, "Failed to unlock (VIDIOC_QBUF)" );
1267 block_Release( p_block );
1273 case IO_METHOD_USERPTR:
1274 memset( &buf, 0, sizeof(buf) );
1275 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1276 buf.memory = V4L2_MEMORY_USERPTR;
1278 /* Wait for next frame */
1279 if (v4l2_ioctl( p_sys->i_fd, VIDIOC_DQBUF, &buf ) < 0 )
1286 /* Could ignore EIO, see spec. */
1289 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
1296 for( i = 0; i < p_sys->i_nbuffers; i++ )
1298 if( buf.m.userptr == (unsigned long)p_sys->p_buffers[i].start &&
1299 buf.length == p_sys->p_buffers[i].length ) break;
1302 if( i >= p_sys->i_nbuffers )
1304 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
1308 p_block = ProcessVideoFrame( p_demux, (uint8_t*)buf.m.userptr, buf.bytesused );
1309 if( !p_block ) return 0;
1312 if( v4l2_ioctl( p_sys->i_fd, VIDIOC_QBUF, &buf ) < 0 )
1314 msg_Err( p_demux, "Failed to unlock (VIDIOC_QBUF)" );
1315 block_Release( p_block );
1324 p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();
1329 /*****************************************************************************
1330 * ProcessVideoFrame: Helper function to take a buffer and copy it into
1332 *****************************************************************************/
1333 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t i_size )
1337 if( !p_frame ) return 0;
1340 if( !( p_block = block_New( p_demux, i_size ) ) )
1342 msg_Warn( p_demux, "Cannot get new block" );
1347 memcpy( p_block->p_buffer, p_frame, i_size );
1352 /*****************************************************************************
1353 * Helper function to initalise video IO using the Read method
1354 *****************************************************************************/
1355 static int InitRead( demux_t *p_demux, unsigned int i_buffer_size )
1357 demux_sys_t *p_sys = p_demux->p_sys;
1359 p_sys->p_buffers = calloc( 1, sizeof( *p_sys->p_buffers ) );
1360 if( !p_sys->p_buffers )
1361 return VLC_EGENERIC;
1363 p_sys->p_buffers[0].length = i_buffer_size;
1364 p_sys->p_buffers[0].start = malloc( i_buffer_size );
1365 if( !p_sys->p_buffers[0].start )
1371 /*****************************************************************************
1372 * Helper function to initalise video IO using the mmap method
1373 *****************************************************************************/
1374 static int InitMmap( demux_t *p_demux, int i_fd )
1376 demux_sys_t *p_sys = p_demux->p_sys;
1377 struct v4l2_requestbuffers req;
1379 memset( &req, 0, sizeof(req) );
1381 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1382 req.memory = V4L2_MEMORY_MMAP;
1384 if( v4l2_ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1386 msg_Err( p_demux, "device does not support mmap i/o" );
1392 msg_Err( p_demux, "Insufficient buffer memory" );
1396 p_sys->p_buffers = calloc( req.count, sizeof( *p_sys->p_buffers ) );
1397 if( !p_sys->p_buffers )
1400 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < req.count; ++p_sys->i_nbuffers )
1402 struct v4l2_buffer buf;
1404 memset( &buf, 0, sizeof(buf) );
1405 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1406 buf.memory = V4L2_MEMORY_MMAP;
1407 buf.index = p_sys->i_nbuffers;
1409 if( v4l2_ioctl( i_fd, VIDIOC_QUERYBUF, &buf ) < 0 )
1411 msg_Err( p_demux, "VIDIOC_QUERYBUF" );
1415 p_sys->p_buffers[p_sys->i_nbuffers].length = buf.length;
1416 p_sys->p_buffers[p_sys->i_nbuffers].start =
1417 v4l2_mmap( NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, buf.m.offset );
1419 if( p_sys->p_buffers[p_sys->i_nbuffers].start == MAP_FAILED )
1421 msg_Err( p_demux, "mmap failed (%m)" );
1429 return VLC_EGENERIC;
1433 /*****************************************************************************
1434 * Helper function to initalise video IO using the userbuf method
1435 *****************************************************************************/
1436 static int InitUserP( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
1438 demux_sys_t *p_sys = p_demux->p_sys;
1439 struct v4l2_requestbuffers req;
1440 unsigned int i_page_size;
1442 i_page_size = sysconf(_SC_PAGESIZE);
1443 i_buffer_size = ( i_buffer_size + i_page_size - 1 ) & ~( i_page_size - 1);
1445 memset( &req, 0, sizeof(req) );
1447 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1448 req.memory = V4L2_MEMORY_USERPTR;
1450 if( v4l2_ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1452 msg_Err( p_demux, "device does not support user pointer i/o" );
1453 return VLC_EGENERIC;
1456 p_sys->p_buffers = calloc( 4, sizeof( *p_sys->p_buffers ) );
1457 if( !p_sys->p_buffers )
1460 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < 4; ++p_sys->i_nbuffers )
1462 p_sys->p_buffers[p_sys->i_nbuffers].length = i_buffer_size;
1463 if( posix_memalign( &p_sys->p_buffers[p_sys->i_nbuffers].start,
1464 /* boundary */ i_page_size, i_buffer_size ) )
1471 free( p_sys->p_buffers );
1472 return VLC_EGENERIC;
1476 /*****************************************************************************
1477 * IsPixelFormatSupported: returns true if the specified V4L2 pixel format is
1478 * in the array of supported formats returned by the driver
1479 *****************************************************************************/
1480 static bool IsPixelFormatSupported( demux_t *p_demux, unsigned int i_pixelformat )
1482 demux_sys_t *p_sys = p_demux->p_sys;
1484 for( int i_index = 0; i_index < p_sys->i_codec; i_index++ )
1486 if( p_sys->p_codecs[i_index].pixelformat == i_pixelformat )
1493 static float GetMaxFrameRate( demux_t *p_demux, int i_fd,
1494 uint32_t i_pixel_format,
1495 uint32_t i_width, uint32_t i_height )
1498 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1499 /* This is new in Linux 2.6.19 */
1500 struct v4l2_frmivalenum frmival;
1502 frmival.pixel_format = i_pixel_format;
1503 frmival.width = i_width;
1504 frmival.height = i_height;
1505 if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 )
1507 switch( frmival.type )
1509 case V4L2_FRMIVAL_TYPE_DISCRETE:
1511 float f_fps_max = -1;
1514 float f_fps = (float)frmival.discrete.denominator
1515 / (float)frmival.discrete.numerator;
1516 if( f_fps > f_fps_max ) f_fps_max = f_fps;
1518 } while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS,
1522 case V4L2_FRMSIZE_TYPE_STEPWISE:
1523 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
1524 return __MAX( (float)frmival.stepwise.max.denominator
1525 / (float)frmival.stepwise.max.numerator,
1526 (float)frmival.stepwise.min.denominator
1527 / (float)frmival.stepwise.min.numerator );
1534 static float GetAbsoluteMaxFrameRate( demux_t *p_demux, int i_fd,
1535 uint32_t i_pixel_format )
1537 float f_fps_max = -1.;
1538 #ifdef VIDIOC_ENUM_FRAMESIZES
1539 /* This is new in Linux 2.6.19 */
1540 struct v4l2_frmsizeenum frmsize;
1542 frmsize.pixel_format = i_pixel_format;
1543 if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) >= 0 )
1545 switch( frmsize.type )
1547 case V4L2_FRMSIZE_TYPE_DISCRETE:
1551 float f_fps = GetMaxFrameRate( p_demux, i_fd,
1553 frmsize.discrete.width,
1554 frmsize.discrete.height );
1555 if( f_fps > f_fps_max ) f_fps_max = f_fps;
1556 } while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES,
1559 case V4L2_FRMSIZE_TYPE_STEPWISE:
1561 uint32_t i_width = frmsize.stepwise.min_width;
1562 uint32_t i_height = frmsize.stepwise.min_height;
1564 i_width <= frmsize.stepwise.max_width &&
1565 i_height <= frmsize.stepwise.max_width;
1566 i_width += frmsize.stepwise.step_width,
1567 i_height += frmsize.stepwise.step_height )
1569 float f_fps = GetMaxFrameRate( p_demux, i_fd,
1571 i_width, i_height );
1572 if( f_fps > f_fps_max ) f_fps_max = f_fps;
1576 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
1578 msg_Err( p_demux, "GetAbsoluteMaxFrameRate implementation for V4L2_FRMSIZE_TYPE_CONTINUOUS isn't correct" );
1579 f_fps_max = GetMaxFrameRate( p_demux, i_fd,
1581 frmsize.stepwise.max_width,
1582 frmsize.stepwise.max_height );
1590 static void GetMaxDimensions( demux_t *p_demux, int i_fd,
1591 uint32_t i_pixel_format, float f_fps_min,
1592 uint32_t *pi_width, uint32_t *pi_height )
1596 #ifdef VIDIOC_ENUM_FRAMESIZES
1597 /* This is new in Linux 2.6.19 */
1598 struct v4l2_frmsizeenum frmsize;
1600 frmsize.pixel_format = i_pixel_format;
1601 if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) >= 0 )
1603 switch( frmsize.type )
1605 case V4L2_FRMSIZE_TYPE_DISCRETE:
1609 float f_fps = GetMaxFrameRate( p_demux, i_fd,
1611 frmsize.discrete.width,
1612 frmsize.discrete.height );
1613 if( f_fps >= f_fps_min &&
1614 frmsize.discrete.width > *pi_width )
1616 *pi_width = frmsize.discrete.width;
1617 *pi_height = frmsize.discrete.height;
1619 } while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES,
1622 case V4L2_FRMSIZE_TYPE_STEPWISE:
1624 uint32_t i_width = frmsize.stepwise.min_width;
1625 uint32_t i_height = frmsize.stepwise.min_height;
1627 i_width <= frmsize.stepwise.max_width &&
1628 i_height <= frmsize.stepwise.max_width;
1629 i_width += frmsize.stepwise.step_width,
1630 i_height += frmsize.stepwise.step_height )
1632 float f_fps = GetMaxFrameRate( p_demux, i_fd,
1634 i_width, i_height );
1635 if( f_fps >= f_fps_min && i_width > *pi_width )
1637 *pi_width = i_width;
1638 *pi_height = i_height;
1643 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
1645 msg_Err( p_demux, "GetMaxDimension implementation for V4L2_FRMSIZE_TYPE_CONTINUOUS isn't correct" );
1646 float f_fps = GetMaxFrameRate( p_demux, i_fd,
1648 frmsize.stepwise.max_width,
1649 frmsize.stepwise.max_height );
1650 if( f_fps >= f_fps_min &&
1651 frmsize.stepwise.max_width > *pi_width )
1653 *pi_width = frmsize.stepwise.max_width;
1654 *pi_height = frmsize.stepwise.max_height;
1662 /*****************************************************************************
1663 * OpenVideoDev: open and set up the video device and probe for capabilities
1664 *****************************************************************************/
1665 static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
1668 struct v4l2_cropcap cropcap;
1669 struct v4l2_crop crop;
1670 struct v4l2_format fmt;
1672 enum v4l2_buf_type buf_type;
1673 const char *psz_device = p_sys->psz_device;
1677 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
1679 msg_Err( p_obj, "cannot open device (%m)" );
1684 /* Note the v4l2_xxx functions are designed so that if they get passed an
1685 unknown fd, the will behave exactly as their regular xxx counterparts,
1686 so if v4l2_fd_open fails, we continue as normal (missing the libv4l2
1687 custom cam format to normal formats conversion). Chances are big we will
1688 still fail then though, as normally v4l2_fd_open only fails if the
1689 device is not a v4l2 device. */
1690 libv4l2_fd = v4l2_fd_open( i_fd, V4L2_ENABLE_ENUM_FMT_EMULATION );
1691 if( libv4l2_fd != -1 )
1697 /* Tune the tuner */
1698 if( p_sys->i_frequency >= 0 )
1700 if( p_sys->i_cur_tuner < 0 || p_sys->i_cur_tuner >= p_sys->i_tuner )
1702 msg_Err( p_obj, "invalid tuner %d.", p_sys->i_cur_tuner );
1705 struct v4l2_frequency frequency;
1706 memset( &frequency, 0, sizeof( frequency ) );
1707 frequency.tuner = p_sys->i_cur_tuner;
1708 frequency.type = p_sys->p_tuners[p_sys->i_cur_tuner].type;
1709 frequency.frequency = p_sys->i_frequency / 62.5;
1710 if( v4l2_ioctl( i_fd, VIDIOC_S_FREQUENCY, &frequency ) < 0 )
1712 msg_Err( p_obj, "cannot set tuner frequency (%m)" );
1715 msg_Dbg( p_obj, "Tuner frequency set" );
1718 /* Set the tuner's audio mode */
1719 if( p_sys->i_audio_mode >= 0 )
1721 if( p_sys->i_cur_tuner < 0 || p_sys->i_cur_tuner >= p_sys->i_tuner )
1723 msg_Err( p_obj, "invalid tuner %d.", p_sys->i_cur_tuner );
1726 struct v4l2_tuner tuner;
1727 memset( &tuner, 0, sizeof( tuner ) );
1728 tuner.index = p_sys->i_cur_tuner;
1729 tuner.audmode = p_sys->i_audio_mode;
1730 if( v4l2_ioctl( i_fd, VIDIOC_S_TUNER, &tuner ) < 0 )
1732 msg_Err( p_obj, "cannot set tuner audio mode (%m)" );
1735 msg_Dbg( p_obj, "Tuner audio mode set" );
1738 /* Select standard */
1740 if( p_sys->i_selected_standard_id != V4L2_STD_UNKNOWN )
1742 if( v4l2_ioctl( i_fd, VIDIOC_S_STD, &p_sys->i_selected_standard_id ) < 0 )
1744 msg_Err( p_obj, "cannot set standard (%m)" );
1747 msg_Dbg( p_obj, "Set standard" );
1752 if( p_sys->i_selected_input >= p_sys->i_input )
1754 msg_Warn( p_obj, "invalid input. Using the default one" );
1755 p_sys->i_selected_input = 0;
1758 if( v4l2_ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_selected_input ) < 0 )
1760 msg_Err( p_obj, "cannot set input (%m)" );
1764 /* Set audio input */
1766 if( p_sys->i_audio > 0 )
1768 if( p_sys->i_selected_audio_input < 0
1769 || p_sys->i_selected_audio_input >= p_sys->i_audio )
1771 msg_Warn( p_obj, "invalid audio input. Using the default one" );
1772 p_sys->i_selected_audio_input = 0;
1775 if( v4l2_ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_selected_audio_input] ) < 0 )
1777 msg_Err( p_obj, "cannot set audio input (%m)" );
1780 msg_Dbg( p_obj, "Audio input set to %d", p_sys->i_selected_audio_input );
1784 /* TODO: Move the resolution stuff up here */
1785 /* if MPEG encoder card, no need to do anything else after this */
1786 ControlList( p_obj, i_fd,
1787 var_GetBool( p_obj, "v4l2-controls-reset" ), b_demux );
1788 SetAvailControlsByString( p_obj, p_sys, i_fd );
1789 if( false == b_demux)
1794 demux_t *p_demux = (demux_t *) p_obj;
1796 /* Verify device support for the various IO methods */
1799 case IO_METHOD_READ:
1800 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE) )
1802 msg_Err( p_demux, "device does not support read i/o" );
1807 case IO_METHOD_MMAP:
1808 case IO_METHOD_USERPTR:
1809 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING) )
1811 msg_Err( p_demux, "device does not support streaming i/o" );
1817 msg_Err( p_demux, "io method not supported" );
1821 /* Reset Cropping */
1822 memset( &cropcap, 0, sizeof(cropcap) );
1823 cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1824 if( v4l2_ioctl( i_fd, VIDIOC_CROPCAP, &cropcap ) >= 0 )
1826 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1827 crop.c = cropcap.defrect; /* reset to default */
1828 if( v4l2_ioctl( i_fd, VIDIOC_S_CROP, &crop ) < 0 )
1833 /* Cropping not supported. */
1836 /* Errors ignored. */
1842 /* Try and find default resolution if not specified */
1843 memset( &fmt, 0, sizeof(fmt) );
1844 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1846 if( p_sys->i_width == 0 || p_sys->i_height == 0 )
1848 /* Use current width and height settings */
1849 if( v4l2_ioctl( i_fd, VIDIOC_G_FMT, &fmt ) < 0 )
1851 msg_Err( p_demux, "Cannot get default width and height." );
1855 p_sys->i_width = fmt.fmt.pix.width;
1856 p_sys->i_height = fmt.fmt.pix.height;
1858 if( fmt.fmt.pix.field == V4L2_FIELD_ALTERNATE )
1860 p_sys->i_height = p_sys->i_height * 2;
1863 else if( p_sys->i_width < 0 || p_sys->i_height < 0 )
1865 msg_Dbg( p_demux, "will try to find optimal width and height." );
1869 /* Use user specified width and height */
1870 msg_Dbg( p_demux, "trying specified size %dx%d", p_sys->i_width, p_sys->i_height );
1873 fmt.fmt.pix.width = p_sys->i_width;
1874 fmt.fmt.pix.height = p_sys->i_height;
1875 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
1877 /* Test and set Chroma */
1878 fmt.fmt.pix.pixelformat = 0;
1879 if( p_sys->psz_requested_chroma && strlen( p_sys->psz_requested_chroma ) > 0 )
1881 /* User specified chroma */
1882 if( strlen( p_sys->psz_requested_chroma ) >= 4 )
1884 int i_requested_fourcc = VLC_FOURCC(
1885 p_sys->psz_requested_chroma[0], p_sys->psz_requested_chroma[1],
1886 p_sys->psz_requested_chroma[2], p_sys->psz_requested_chroma[3] );
1887 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
1889 if( v4l2chroma_to_fourcc[i].i_fourcc == i_requested_fourcc )
1891 fmt.fmt.pix.pixelformat = v4l2chroma_to_fourcc[i].i_v4l2;
1896 /* Try and set user chroma */
1897 if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ( fmt.fmt.pix.pixelformat && v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 ) )
1899 msg_Warn( p_demux, "Driver is unable to use specified chroma %s. Trying defaults.", p_sys->psz_requested_chroma );
1900 fmt.fmt.pix.pixelformat = 0;
1904 /* If no user specified chroma, find best */
1905 /* This also decides if MPEG encoder card or not */
1906 if( !fmt.fmt.pix.pixelformat )
1909 for( i = 0; i < ARRAY_SIZE( p_chroma_fallbacks ); i++ )
1911 fmt.fmt.pix.pixelformat = p_chroma_fallbacks[i];
1912 if( IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat )
1913 && v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) >= 0 )
1916 if( i == ARRAY_SIZE( p_chroma_fallbacks ) )
1918 msg_Warn( p_demux, "Could not select any of the default chromas; attempting to open as MPEG encoder card (access)" );
1923 if( p_sys->i_width < 0 || p_sys->i_height < 0 )
1925 if( p_sys->f_fps <= 0 )
1927 p_sys->f_fps = GetAbsoluteMaxFrameRate( p_demux, i_fd,
1928 fmt.fmt.pix.pixelformat );
1929 msg_Dbg( p_demux, "Found maximum framerate of %f", p_sys->f_fps );
1931 GetMaxDimensions( p_demux, i_fd,
1932 fmt.fmt.pix.pixelformat, p_sys->f_fps,
1933 &fmt.fmt.pix.width, &fmt.fmt.pix.height );
1934 msg_Dbg( p_demux, "Found optimal dimensions for framerate %f of %dx%d",
1935 p_sys->f_fps, fmt.fmt.pix.width, fmt.fmt.pix.height );
1936 if( v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 ) {;}
1939 /* Reassign width, height and chroma incase driver override */
1940 p_sys->i_width = fmt.fmt.pix.width;
1941 p_sys->i_height = fmt.fmt.pix.height;
1943 /* Look up final fourcc */
1944 p_sys->i_fourcc = 0;
1945 for( int i = 0; v4l2chroma_to_fourcc[i].i_fourcc != 0; i++ )
1947 if( v4l2chroma_to_fourcc[i].i_v4l2 == fmt.fmt.pix.pixelformat )
1949 p_sys->i_fourcc = v4l2chroma_to_fourcc[i].i_fourcc;
1950 es_format_Init( &es_fmt, VIDEO_ES, p_sys->i_fourcc );
1951 es_fmt.video.i_rmask = v4l2chroma_to_fourcc[i].i_rmask;
1952 es_fmt.video.i_gmask = v4l2chroma_to_fourcc[i].i_gmask;
1953 es_fmt.video.i_bmask = v4l2chroma_to_fourcc[i].i_bmask;
1958 /* Buggy driver paranoia */
1959 i_min = fmt.fmt.pix.width * 2;
1960 if( fmt.fmt.pix.bytesperline < i_min )
1961 fmt.fmt.pix.bytesperline = i_min;
1962 i_min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
1963 if( fmt.fmt.pix.sizeimage < i_min )
1964 fmt.fmt.pix.sizeimage = i_min;
1966 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1967 /* This is new in Linux 2.6.19 */
1968 /* List supported frame rates */
1969 struct v4l2_frmivalenum frmival;
1971 frmival.pixel_format = fmt.fmt.pix.pixelformat;
1972 frmival.width = p_sys->i_width;
1973 frmival.height = p_sys->i_height;
1974 if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 )
1977 memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
1978 vlc_fourcc_to_char( p_sys->i_fourcc, &psz_fourcc );
1979 msg_Dbg( p_demux, "supported frame intervals for %4s, %dx%d:",
1980 psz_fourcc, frmival.width, frmival.height );
1981 switch( frmival.type )
1983 case V4L2_FRMIVAL_TYPE_DISCRETE:
1986 msg_Dbg( p_demux, " supported frame interval: %d/%d",
1987 frmival.discrete.numerator,
1988 frmival.discrete.denominator );
1990 } while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 );
1992 case V4L2_FRMIVAL_TYPE_STEPWISE:
1993 msg_Dbg( p_demux, " supported frame intervals: %d/%d to "
1994 "%d/%d using %d/%d increments",
1995 frmival.stepwise.min.numerator,
1996 frmival.stepwise.min.denominator,
1997 frmival.stepwise.max.numerator,
1998 frmival.stepwise.max.denominator,
1999 frmival.stepwise.step.numerator,
2000 frmival.stepwise.step.denominator );
2002 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
2003 msg_Dbg( p_demux, " supported frame intervals: %d/%d to %d/%d",
2004 frmival.stepwise.min.numerator,
2005 frmival.stepwise.min.denominator,
2006 frmival.stepwise.max.numerator,
2007 frmival.stepwise.max.denominator );
2014 /* Init IO method */
2017 case IO_METHOD_READ:
2018 if( InitRead( p_demux, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
2021 case IO_METHOD_MMAP:
2022 if( InitMmap( p_demux, i_fd ) != VLC_SUCCESS ) goto open_failed;
2025 case IO_METHOD_USERPTR:
2026 if( InitUserP( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
2032 es_fmt.video.i_width = p_sys->i_width;
2033 es_fmt.video.i_height = p_sys->i_height;
2034 es_fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
2036 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
2037 (char*)&es_fmt.i_codec, es_fmt.video.i_width, es_fmt.video.i_height );
2038 p_sys->p_es = es_out_Add( p_demux->out, &es_fmt );
2044 case IO_METHOD_READ:
2048 case IO_METHOD_MMAP:
2049 for (unsigned int i = 0; i < p_sys->i_nbuffers; ++i)
2051 struct v4l2_buffer buf;
2053 memset( &buf, 0, sizeof(buf) );
2054 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2055 buf.memory = V4L2_MEMORY_MMAP;
2058 if( v4l2_ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
2060 msg_Err( p_demux, "VIDIOC_QBUF failed" );
2065 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2066 if( v4l2_ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
2068 msg_Err( p_demux, "VIDIOC_STREAMON failed" );
2074 case IO_METHOD_USERPTR:
2075 for( unsigned int i = 0; i < p_sys->i_nbuffers; ++i )
2077 struct v4l2_buffer buf;
2079 memset( &buf, 0, sizeof(buf) );
2080 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2081 buf.memory = V4L2_MEMORY_USERPTR;
2083 buf.m.userptr = (unsigned long)p_sys->p_buffers[i].start;
2084 buf.length = p_sys->p_buffers[i].length;
2086 if( v4l2_ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
2088 msg_Err( p_demux, "VIDIOC_QBUF failed" );
2093 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2094 if( v4l2_ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
2096 msg_Err( p_demux, "VIDIOC_STREAMON failed" );
2104 if( p_sys->f_fps >= 0.1 )
2106 msg_Dbg( p_demux, "User set fps=%f", p_sys->f_fps );
2112 if( i_fd >= 0 ) v4l2_close( i_fd );
2117 /*****************************************************************************
2118 * ProbeVideoDev: probe video for capabilities
2119 *****************************************************************************/
2120 static bool ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
2121 const char *psz_device )
2129 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
2131 msg_Err( p_obj, "cannot open video device '%s' (%m)", psz_device );
2136 /* Note the v4l2_xxx functions are designed so that if they get passed an
2137 unknown fd, the will behave exactly as their regular xxx counterparts,
2138 so if v4l2_fd_open fails, we continue as normal (missing the libv4l2
2139 custom cam format to normal formats conversion). Chances are big we will
2140 still fail then though, as normally v4l2_fd_open only fails if the
2141 device is not a v4l2 device. */
2142 libv4l2_fd = v4l2_fd_open( i_fd, V4L2_ENABLE_ENUM_FMT_EMULATION );
2143 if( libv4l2_fd != -1 )
2149 /* Get device capabilites */
2151 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCAP, &p_sys->dev_cap ) < 0 )
2153 msg_Err( p_obj, "cannot get video capabilities (%m)" );
2157 msg_Dbg( p_obj, "V4L2 device: %s using driver: %s (version: %u.%u.%u) on %s",
2158 p_sys->dev_cap.card,
2159 p_sys->dev_cap.driver,
2160 (p_sys->dev_cap.version >> 16) & 0xFF,
2161 (p_sys->dev_cap.version >> 8) & 0xFF,
2162 p_sys->dev_cap.version & 0xFF,
2163 p_sys->dev_cap.bus_info );
2165 msg_Dbg( p_obj, "the device has the capabilities: (%c) Video Capure, "
2168 ( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE ? 'X':' '),
2169 ( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO ? 'X':' '),
2170 ( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER ? 'X':' ') );
2172 msg_Dbg( p_obj, "supported I/O methods are: (%c) Read/Write, "
2174 "(%c) Asynchronous",
2175 ( p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE ? 'X':' ' ),
2176 ( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING ? 'X':' ' ),
2177 ( p_sys->dev_cap.capabilities & V4L2_CAP_ASYNCIO ? 'X':' ' ) );
2179 /* Now, enumerate all the video inputs. This is useless at the moment
2180 since we have no way to present that info to the user except with
2183 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2185 struct v4l2_input t_input;
2187 while( v4l2_ioctl( i_fd, VIDIOC_ENUMINPUT, &t_input ) >= 0 )
2190 t_input.index = p_sys->i_input;
2193 p_sys->p_inputs = calloc( 1, p_sys->i_input * sizeof( struct v4l2_input ) );
2194 if( !p_sys->p_inputs ) goto open_failed;
2196 for( i_index = 0; i_index < p_sys->i_input; i_index++ )
2198 p_sys->p_inputs[i_index].index = i_index;
2200 if( v4l2_ioctl( i_fd, VIDIOC_ENUMINPUT, &p_sys->p_inputs[i_index] ) )
2202 msg_Err( p_obj, "cannot get video input characteristics (%m)" );
2205 msg_Dbg( p_obj, "video input %i (%s) has type: %s %c",
2207 p_sys->p_inputs[i_index].name,
2208 p_sys->p_inputs[i_index].type
2209 == V4L2_INPUT_TYPE_TUNER ?
2211 "External analog input",
2212 i_index == p_sys->i_selected_input ? '*' : ' ' );
2216 /* Probe video standards */
2217 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2219 struct v4l2_standard t_standards;
2220 t_standards.index = 0;
2221 while( v4l2_ioctl( i_fd, VIDIOC_ENUMSTD, &t_standards ) >=0 )
2223 p_sys->i_standard++;
2224 t_standards.index = p_sys->i_standard;
2227 p_sys->p_standards = calloc( 1, p_sys->i_standard * sizeof( struct v4l2_standard ) );
2228 if( !p_sys->p_standards ) goto open_failed;
2230 for( i_standard = 0; i_standard < p_sys->i_standard; i_standard++ )
2232 p_sys->p_standards[i_standard].index = i_standard;
2234 if( v4l2_ioctl( i_fd, VIDIOC_ENUMSTD, &p_sys->p_standards[i_standard] ) )
2236 msg_Err( p_obj, "cannot get video input standards (%m)" );
2239 msg_Dbg( p_obj, "video standard %i is: %s %c",
2241 p_sys->p_standards[i_standard].name,
2242 (unsigned)i_standard == p_sys->i_selected_standard_id ? '*' : ' ' );
2246 /* initialize the structures for the ioctls */
2247 for( i_index = 0; i_index < 32; i_index++ )
2249 p_sys->p_audios[i_index].index = i_index;
2252 /* Probe audio inputs */
2253 if( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO )
2255 while( p_sys->i_audio < 32 &&
2256 v4l2_ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_audio] ) >= 0 )
2258 if( v4l2_ioctl( i_fd, VIDIOC_G_AUDIO, &p_sys->p_audios[ p_sys->i_audio] ) < 0 )
2260 msg_Err( p_obj, "cannot get audio input characteristics (%m)" );
2264 msg_Dbg( p_obj, "audio input %i (%s) is %s %s %c",
2266 p_sys->p_audios[p_sys->i_audio].name,
2267 p_sys->p_audios[p_sys->i_audio].capability &
2268 V4L2_AUDCAP_STEREO ?
2270 p_sys->p_audios[p_sys->i_audio].capability &
2272 "(Automatic Volume Level supported)" : "",
2273 p_sys->i_audio == p_sys->i_selected_audio_input ? '*' : ' ' );
2279 /* List tuner caps */
2280 if( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER )
2282 struct v4l2_tuner tuner;
2283 memset( &tuner, 0, sizeof(tuner) );
2284 while( v4l2_ioctl( i_fd, VIDIOC_G_TUNER, &tuner ) >= 0 )
2287 memset( &tuner, 0, sizeof(tuner) );
2288 tuner.index = p_sys->i_tuner;
2291 p_sys->p_tuners = calloc( 1, p_sys->i_tuner * sizeof( struct v4l2_tuner ) );
2292 if( !p_sys->p_tuners ) goto open_failed;
2294 for( i_index = 0; i_index < p_sys->i_tuner; i_index++ )
2296 p_sys->p_tuners[i_index].index = i_index;
2298 if( v4l2_ioctl( i_fd, VIDIOC_G_TUNER, &p_sys->p_tuners[i_index] ) )
2300 msg_Err( p_obj, "cannot get tuner characteristics (%m)" );
2303 msg_Dbg( p_obj, "tuner %i (%s) has type: %s, "
2304 "frequency range: %.1f %s -> %.1f %s",
2306 p_sys->p_tuners[i_index].name,
2307 p_sys->p_tuners[i_index].type
2308 == V4L2_TUNER_RADIO ?
2309 "Radio" : "Analog TV",
2310 p_sys->p_tuners[i_index].rangelow * 62.5,
2311 p_sys->p_tuners[i_index].capability &
2312 V4L2_TUNER_CAP_LOW ?
2314 p_sys->p_tuners[i_index].rangehigh * 62.5,
2315 p_sys->p_tuners[i_index].capability &
2316 V4L2_TUNER_CAP_LOW ?
2319 struct v4l2_frequency frequency;
2320 memset( &frequency, 0, sizeof( frequency ) );
2321 if( v4l2_ioctl( i_fd, VIDIOC_G_FREQUENCY, &frequency ) < 0 )
2323 msg_Err( p_obj, "cannot get tuner frequency (%m)" );
2326 msg_Dbg( p_obj, "tuner %i (%s) frequency: %.1f %s",
2328 p_sys->p_tuners[i_index].name,
2329 frequency.frequency * 62.5,
2330 p_sys->p_tuners[i_index].capability &
2331 V4L2_TUNER_CAP_LOW ?
2336 /* Probe for available chromas */
2337 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2339 struct v4l2_fmtdesc codec;
2342 memset( &codec, 0, sizeof(codec) );
2343 codec.index = i_index;
2344 codec.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2346 while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FMT, &codec ) >= 0 )
2349 codec.index = i_index;
2352 p_sys->i_codec = i_index;
2354 p_sys->p_codecs = calloc( 1, p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
2356 for( i_index = 0; i_index < p_sys->i_codec; i_index++ )
2358 p_sys->p_codecs[i_index].index = i_index;
2359 p_sys->p_codecs[i_index].type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2361 if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FMT, &p_sys->p_codecs[i_index] ) < 0 )
2363 msg_Err( p_obj, "cannot get codec description (%m)" );
2367 /* only print if vlc supports the format */
2368 char psz_fourcc_v4l2[5];
2369 memset( &psz_fourcc_v4l2, 0, sizeof( psz_fourcc_v4l2 ) );
2370 vlc_fourcc_to_char( p_sys->p_codecs[i_index].pixelformat,
2372 bool b_codec_supported = false;
2373 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
2375 if( v4l2chroma_to_fourcc[i].i_v4l2 == p_sys->p_codecs[i_index].pixelformat )
2377 b_codec_supported = true;
2380 memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
2381 vlc_fourcc_to_char( v4l2chroma_to_fourcc[i].i_fourcc,
2383 msg_Dbg( p_obj, "device supports chroma %4s [%s, %s]",
2385 p_sys->p_codecs[i_index].description,
2388 #ifdef VIDIOC_ENUM_FRAMESIZES
2389 /* This is new in Linux 2.6.19 */
2390 /* List valid frame sizes for this format */
2391 struct v4l2_frmsizeenum frmsize;
2393 frmsize.pixel_format = p_sys->p_codecs[i_index].pixelformat;
2394 if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) < 0 )
2396 /* Not all devices support this ioctl */
2397 msg_Warn( p_obj, "Unable to query for frame sizes" );
2401 switch( frmsize.type )
2403 case V4L2_FRMSIZE_TYPE_DISCRETE:
2407 " device supports size %dx%d",
2408 frmsize.discrete.width, frmsize.discrete.height );
2410 } while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) >= 0 );
2412 case V4L2_FRMSIZE_TYPE_STEPWISE:
2414 " device supports sizes %dx%d to %dx%d using %dx%d increments",
2415 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
2416 frmsize.stepwise.max_width, frmsize.stepwise.max_height,
2417 frmsize.stepwise.step_width, frmsize.stepwise.step_height );
2419 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
2421 " device supports all sizes %dx%d to %dx%d",
2422 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
2423 frmsize.stepwise.max_width, frmsize.stepwise.max_height );
2430 if( !b_codec_supported )
2433 "device codec %4s (%s) not supported",
2435 p_sys->p_codecs[i_index].description );
2441 if( i_fd >= 0 ) v4l2_close( i_fd );
2446 if( i_fd >= 0 ) v4l2_close( i_fd );
2451 static void name2var( unsigned char *name )
2453 for( ; *name; name++ )
2454 *name = (*name == ' ') ? '_' : tolower( *name );
2457 /*****************************************************************************
2458 * Print a user-class v4l2 control's details, create the relevant variable,
2459 * change the value if needed.
2460 *****************************************************************************/
2461 static void ControlListPrint( vlc_object_t *p_obj, int i_fd,
2462 struct v4l2_queryctrl queryctrl,
2463 bool b_reset, bool b_demux )
2465 struct v4l2_querymenu querymenu;
2472 vlc_value_t val, val2;
2474 if( queryctrl.flags & V4L2_CTRL_FLAG_GRABBED )
2475 msg_Dbg( p_obj, " control is busy" );
2476 if( queryctrl.flags & V4L2_CTRL_FLAG_READ_ONLY )
2477 msg_Dbg( p_obj, " control is read-only" );
2479 for( i = 0; controls[i].psz_name != NULL; i++ )
2480 if( controls[i].i_cid == queryctrl.id ) break;
2482 if( controls[i].psz_name )
2484 psz_name = strdup( controls[i].psz_name );
2485 char psz_cfg_name[40];
2486 sprintf( psz_cfg_name, CFG_PREFIX "%s", psz_name );
2487 i_val = var_CreateGetInteger( p_obj, psz_cfg_name );
2488 var_Destroy( p_obj, psz_cfg_name );
2492 psz_name = strdup( (const char *)queryctrl.name );
2493 name2var( (unsigned char *)psz_name );
2497 switch( queryctrl.type )
2499 case V4L2_CTRL_TYPE_INTEGER:
2500 msg_Dbg( p_obj, " integer control" );
2502 " valid values: %d to %d by steps of %d",
2503 queryctrl.minimum, queryctrl.maximum,
2506 var_Create( p_obj, psz_name,
2507 VLC_VAR_INTEGER | VLC_VAR_HASMIN | VLC_VAR_HASMAX
2508 | VLC_VAR_HASSTEP | VLC_VAR_ISCOMMAND );
2509 val.i_int = queryctrl.minimum;
2510 var_Change( p_obj, psz_name, VLC_VAR_SETMIN, &val, NULL );
2511 val.i_int = queryctrl.maximum;
2512 var_Change( p_obj, psz_name, VLC_VAR_SETMAX, &val, NULL );
2513 val.i_int = queryctrl.step;
2514 var_Change( p_obj, psz_name, VLC_VAR_SETSTEP, &val, NULL );
2516 case V4L2_CTRL_TYPE_BOOLEAN:
2517 msg_Dbg( p_obj, " boolean control" );
2518 var_Create( p_obj, psz_name,
2519 VLC_VAR_BOOL | VLC_VAR_ISCOMMAND );
2521 case V4L2_CTRL_TYPE_MENU:
2522 msg_Dbg( p_obj, " menu control" );
2523 var_Create( p_obj, psz_name,
2524 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE
2525 | VLC_VAR_ISCOMMAND );
2526 memset( &querymenu, 0, sizeof( querymenu ) );
2527 for( i_mid = queryctrl.minimum;
2528 i_mid <= (unsigned)queryctrl.maximum;
2531 querymenu.index = i_mid;
2532 querymenu.id = queryctrl.id;
2533 if( v4l2_ioctl( i_fd, VIDIOC_QUERYMENU, &querymenu ) >= 0 )
2535 msg_Dbg( p_obj, " %d: %s",
2536 querymenu.index, querymenu.name );
2537 val.i_int = querymenu.index;
2538 val2.psz_string = (char *)querymenu.name;
2539 var_Change( p_obj, psz_name,
2540 VLC_VAR_ADDCHOICE, &val, &val2 );
2544 case V4L2_CTRL_TYPE_BUTTON:
2545 msg_Dbg( p_obj, " button control" );
2546 var_Create( p_obj, psz_name,
2547 VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
2549 case V4L2_CTRL_TYPE_CTRL_CLASS:
2550 msg_Dbg( p_obj, " control class" );
2551 var_Create( p_obj, psz_name, VLC_VAR_VOID );
2554 msg_Dbg( p_obj, " unknown control type (FIXME)" );
2559 switch( queryctrl.type )
2561 case V4L2_CTRL_TYPE_INTEGER:
2562 case V4L2_CTRL_TYPE_BOOLEAN:
2563 case V4L2_CTRL_TYPE_MENU:
2565 struct v4l2_control control;
2566 msg_Dbg( p_obj, " default value: %d",
2567 queryctrl.default_value );
2568 memset( &control, 0, sizeof( control ) );
2569 control.id = queryctrl.id;
2570 if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
2572 msg_Dbg( p_obj, " current value: %d", control.value );
2576 i_val = control.value;
2577 if( b_reset && queryctrl.default_value != control.value )
2579 msg_Dbg( p_obj, " reset value to default" );
2580 Control( p_obj, i_fd, psz_name,
2581 queryctrl.id, queryctrl.default_value );
2586 Control( p_obj, i_fd, psz_name,
2587 queryctrl.id, i_val );
2595 val.psz_string = (char *)queryctrl.name;
2596 var_Change( p_obj, psz_name, VLC_VAR_SETTEXT, &val, NULL );
2597 val.i_int = queryctrl.id;
2598 val2.psz_string = (char *)psz_name;
2599 var_Change( p_obj, "allcontrols", VLC_VAR_ADDCHOICE, &val, &val2 );
2600 /* bad things happen changing MPEG mid-stream
2601 * so don't add to Ext Settings GUI */
2602 if( V4L2_CTRL_ID2CLASS( queryctrl.id ) != V4L2_CTRL_CLASS_MPEG )
2603 var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
2605 switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
2608 var_SetBool( p_obj, psz_name, i_val );
2610 case VLC_VAR_INTEGER:
2611 var_SetInteger( p_obj, psz_name, i_val );
2616 msg_Warn( p_obj, "FIXME: %s %s %d", __FILE__, __func__,
2622 var_AddCallback( p_obj, psz_name,
2623 DemuxControlCallback, (void*)(intptr_t)queryctrl.id );
2625 var_AddCallback( p_obj, psz_name,
2626 AccessControlCallback, (void*)(intptr_t)queryctrl.id );
2631 /*****************************************************************************
2632 * List all user-class v4l2 controls, set them to the user specified
2633 * value and create the relevant variables to enable runtime changes
2634 *****************************************************************************/
2635 static int ControlList( vlc_object_t *p_obj, int i_fd,
2636 bool b_reset, bool b_demux )
2638 struct v4l2_queryctrl queryctrl;
2641 memset( &queryctrl, 0, sizeof( queryctrl ) );
2643 /* A list of available controls (aka the variable name) will be
2644 * stored as choices in the "allcontrols" variable. We'll thus be able
2645 * to use those to create an appropriate interface
2646 * A list of available controls that can be changed mid-stream will
2647 * be stored in the "controls" variable */
2648 var_Create( p_obj, "controls", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
2649 var_Create( p_obj, "allcontrols", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
2651 var_Create( p_obj, "controls-update", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
2653 /* Add a control to reset all controls to their default values */
2654 vlc_value_t val, val2;
2655 var_Create( p_obj, "controls-reset", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
2656 val.psz_string = _( "Reset controls to default" );
2657 var_Change( p_obj, "controls-reset", VLC_VAR_SETTEXT, &val, NULL );
2659 val2.psz_string = (char *)"controls-reset";
2660 var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
2662 var_AddCallback( p_obj, "controls-reset", DemuxControlResetCallback, NULL );
2664 var_AddCallback( p_obj, "controls-reset", AccessControlResetCallback, NULL );
2666 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
2667 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2669 msg_Dbg( p_obj, "Extended control API supported by v4l2 driver" );
2671 /* List extended controls */
2672 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
2673 while( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2675 if( queryctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS )
2677 msg_Dbg( p_obj, "%s", queryctrl.name );
2678 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
2681 switch( V4L2_CTRL_ID2CLASS( queryctrl.id ) )
2683 case V4L2_CTRL_CLASS_USER:
2684 msg_Dbg( p_obj, "Available control: %s (%x)",
2685 queryctrl.name, queryctrl.id );
2687 case V4L2_CTRL_CLASS_MPEG:
2688 name2var( queryctrl.name );
2689 msg_Dbg( p_obj, "Available MPEG control: %s (%x)",
2690 queryctrl.name, queryctrl.id );
2693 msg_Dbg( p_obj, "Available private control: %s (%x)",
2694 queryctrl.name, queryctrl.id );
2697 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
2698 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
2703 msg_Dbg( p_obj, "Extended control API not supported by v4l2 driver" );
2705 /* List public controls */
2706 for( i_cid = V4L2_CID_BASE;
2707 i_cid < V4L2_CID_LASTP1;
2710 queryctrl.id = i_cid;
2711 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2713 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2715 msg_Dbg( p_obj, "Available control: %s (%x)",
2716 queryctrl.name, queryctrl.id );
2717 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
2721 /* List private controls */
2722 for( i_cid = V4L2_CID_PRIVATE_BASE;
2726 queryctrl.id = i_cid;
2727 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2729 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2731 msg_Dbg( p_obj, "Available private control: %s (%x)",
2732 queryctrl.name, queryctrl.id );
2733 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
2743 static void SetAvailControlsByString( vlc_object_t *p_obj, demux_sys_t *p_sys,
2746 char *psz_parser = p_sys->psz_set_ctrls;
2747 vlc_value_t val, text, name;
2749 if( psz_parser == NULL )
2752 if( *psz_parser == '{' )
2755 int i_ret = var_Change( p_obj, "allcontrols", VLC_VAR_GETCHOICES,
2759 msg_Err( p_obj, "Oops, can't find 'allcontrols' variable." );
2763 while( *psz_parser && *psz_parser != '}' )
2765 char *psz_delim, *psz_assign;
2767 while( *psz_parser == ',' || *psz_parser == ' ' )
2770 psz_delim = strchr( psz_parser, ',' );
2771 if( psz_delim == NULL )
2772 psz_delim = strchr( psz_parser, '}' );
2773 if( psz_delim == NULL )
2774 psz_delim = psz_parser + strlen( psz_parser );
2776 psz_assign = memchr( psz_parser, '=', psz_delim - psz_parser );
2777 if( psz_assign == NULL )
2779 char *psz_name = strndup( psz_parser, psz_delim - psz_parser );
2780 msg_Err( p_obj, "%s missing '='", psz_name );
2782 psz_parser = psz_delim + 1;
2787 i < val.p_list->i_count ;//&& psz_parser < psz_assign;
2790 const char *psz_var = text.p_list->p_values[i].psz_string;
2791 int i_cid = val.p_list->p_values[i].i_int;
2792 var_Change( p_obj, psz_var, VLC_VAR_GETTEXT, &name, NULL );
2793 const char *psz_name = name.psz_string;
2795 int i_availstrlen = strlen( psz_name );
2796 int i_parsestrlen = psz_assign - psz_parser;
2797 int i_maxstrlen = __MAX( i_availstrlen, i_parsestrlen);
2799 if( !strncasecmp( psz_name, psz_parser, i_maxstrlen ) )
2801 Control( p_obj, i_fd, psz_name, i_cid,
2802 strtol( ++psz_assign, &psz_parser, 0) );
2806 if( psz_parser < psz_assign )
2808 char *psz_name = strndup( psz_parser, psz_assign - psz_parser );
2809 msg_Err( p_obj, "Control %s not available", psz_name );
2811 psz_parser = ( *psz_delim ) ? ( psz_delim + 1 ) : psz_delim;
2816 /*****************************************************************************
2817 * Reset all user-class v4l2 controls to their default value
2818 *****************************************************************************/
2819 static int ControlReset( vlc_object_t *p_obj, int i_fd )
2821 struct v4l2_queryctrl queryctrl;
2823 memset( &queryctrl, 0, sizeof( queryctrl ) );
2825 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
2826 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2828 /* Extended control API supported */
2829 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
2830 while( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2832 if( queryctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS
2833 || V4L2_CTRL_ID2CLASS( queryctrl.id ) == V4L2_CTRL_CLASS_MPEG )
2835 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
2838 struct v4l2_control control;
2839 memset( &control, 0, sizeof( control ) );
2840 control.id = queryctrl.id;
2841 if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
2842 && queryctrl.default_value != control.value )
2845 for( i = 0; controls[i].psz_name != NULL; i++ )
2846 if( controls[i].i_cid == queryctrl.id ) break;
2847 name2var( queryctrl.name );
2848 Control( p_obj, i_fd,
2849 controls[i].psz_name ? : (const char *)queryctrl.name,
2850 queryctrl.id, queryctrl.default_value );
2852 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
2858 /* public controls */
2859 for( i_cid = V4L2_CID_BASE;
2860 i_cid < V4L2_CID_LASTP1;
2863 queryctrl.id = i_cid;
2864 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2866 struct v4l2_control control;
2867 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2869 memset( &control, 0, sizeof( control ) );
2870 control.id = queryctrl.id;
2871 if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
2872 && queryctrl.default_value != control.value )
2875 for( i = 0; controls[i].psz_name != NULL; i++ )
2876 if( controls[i].i_cid == queryctrl.id ) break;
2877 name2var( queryctrl.name );
2878 Control( p_obj, i_fd,
2879 controls[i].psz_name ? : (const char *)queryctrl.name,
2880 queryctrl.id, queryctrl.default_value );
2885 /* private controls */
2886 for( i_cid = V4L2_CID_PRIVATE_BASE;
2890 queryctrl.id = i_cid;
2891 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2893 struct v4l2_control control;
2894 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2896 memset( &control, 0, sizeof( control ) );
2897 control.id = queryctrl.id;
2898 if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
2899 && queryctrl.default_value != control.value )
2901 name2var( queryctrl.name );
2902 Control( p_obj, i_fd, (const char *)queryctrl.name,
2903 queryctrl.id, queryctrl.default_value );
2913 /*****************************************************************************
2914 * Issue user-class v4l2 controls
2915 *****************************************************************************/
2916 static int Control( vlc_object_t *p_obj, int i_fd,
2917 const char *psz_name, int i_cid, int i_value )
2919 struct v4l2_queryctrl queryctrl;
2920 struct v4l2_control control;
2921 struct v4l2_ext_control ext_control;
2922 struct v4l2_ext_controls ext_controls;
2927 memset( &queryctrl, 0, sizeof( queryctrl ) );
2929 queryctrl.id = i_cid;
2931 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) < 0
2932 || queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2934 msg_Dbg( p_obj, "%s (%x) control is not supported.", psz_name, i_cid );
2935 return VLC_EGENERIC;
2938 memset( &control, 0, sizeof( control ) );
2939 memset( &ext_control, 0, sizeof( ext_control ) );
2940 memset( &ext_controls, 0, sizeof( ext_controls ) );
2942 ext_control.id = i_cid;
2943 ext_controls.ctrl_class = V4L2_CTRL_ID2CLASS( i_cid );
2944 ext_controls.count = 1;
2945 ext_controls.controls = &ext_control;
2951 ext_control.value = i_value;
2952 if( v4l2_ioctl( i_fd, VIDIOC_S_EXT_CTRLS, &ext_controls ) < 0 )
2954 control.value = i_value;
2955 if( v4l2_ioctl( i_fd, VIDIOC_S_CTRL, &control ) < 0 )
2957 msg_Err( p_obj, "unable to set %s (%x) to %d (%m)",
2958 psz_name, i_cid, i_value );
2959 return VLC_EGENERIC;
2961 i_ret = v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control );
2965 i_ret = v4l2_ioctl( i_fd, VIDIOC_G_EXT_CTRLS, &ext_controls );
2966 control.value = ext_control.value;
2973 msg_Dbg( p_obj, "video %s: %d", psz_name, control.value );
2974 switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
2977 val.b_bool = control.value;
2978 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
2979 var_SetVoid( p_obj, "controls-update" );
2981 case VLC_VAR_INTEGER:
2982 val.i_int = control.value;
2983 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
2984 var_SetVoid( p_obj, "controls-update" );
2991 /*****************************************************************************
2992 * On the fly change settings callback
2993 *****************************************************************************/
2994 static int DemuxControlCallback( vlc_object_t *p_this,
2995 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
2999 demux_t *p_demux = (demux_t*)p_this;
3000 demux_sys_t *p_sys = p_demux->p_sys;
3001 int i_cid = (long int)p_data;
3003 int i_fd = p_sys->i_fd;
3006 return VLC_EGENERIC;
3008 Control( p_this, i_fd, psz_var, i_cid, newval.i_int );
3010 return VLC_EGENERIC;
3013 static int DemuxControlResetCallback( vlc_object_t *p_this,
3014 const char *psz_var, vlc_value_t oldval, vlc_value_t newval, void *p_data )
3016 (void)psz_var; (void)oldval; (void)newval; (void)p_data;
3017 demux_t *p_demux = (demux_t*)p_this;
3018 demux_sys_t *p_sys = p_demux->p_sys;
3020 int i_fd = p_sys->i_fd;
3023 return VLC_EGENERIC;
3025 ControlReset( p_this, i_fd );
3027 return VLC_EGENERIC;
3030 static int AccessControlCallback( vlc_object_t *p_this,
3031 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3035 access_t *p_access = (access_t *)p_this;
3036 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
3037 int i_cid = (long int)p_data;
3039 int i_fd = p_sys->i_fd;
3042 return VLC_EGENERIC;
3044 Control( p_this, i_fd, psz_var, i_cid, newval.i_int );
3046 return VLC_EGENERIC;
3049 static int AccessControlResetCallback( vlc_object_t *p_this,
3050 const char *psz_var, vlc_value_t oldval, vlc_value_t newval, void *p_data )
3052 (void)psz_var; (void)oldval; (void)newval; (void)p_data;
3053 access_t *p_access = (access_t *)p_this;
3054 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
3056 int i_fd = p_sys->i_fd;
3059 return VLC_EGENERIC;
3061 ControlReset( p_this, i_fd );
3063 return VLC_EGENERIC;