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_charset.h>
44 #include <vlc_demux.h>
45 #include <vlc_input.h>
50 #include <sys/ioctl.h>
53 #include <linux/videodev2.h>
61 /*****************************************************************************
63 *****************************************************************************/
65 static int DemuxOpen ( vlc_object_t * );
66 static void DemuxClose( vlc_object_t * );
67 static int AccessOpen ( vlc_object_t * );
68 static void AccessClose( vlc_object_t * );
70 #define STANDARD_TEXT N_( "Standard" )
71 #define STANDARD_LONGTEXT N_( \
72 "Video standard (Default, SECAM, PAL, or NTSC)." )
73 #define CHROMA_TEXT N_("Video input chroma format")
74 #define CHROMA_LONGTEXT N_( \
75 "Force the Video4Linux2 video device to use a specific chroma format " \
76 "(eg. I420 or I422 for raw images, MJPG for M-JPEG compressed input) " \
77 "(Complete list: GREY, I240, RV16, RV15, RV24, RV32, YUY2, YUYV, UYVY, " \
78 "I41N, I422, I420, I411, I410, MJPG)")
79 #define INPUT_TEXT N_( "Input" )
80 #define INPUT_LONGTEXT N_( \
81 "Input of the card to use (see debug)." )
82 #define AUDIO_INPUT_TEXT N_( "Audio input" )
83 #define AUDIO_INPUT_LONGTEXT N_( \
84 "Audio input of the card to use (see debug)." )
85 #define IOMETHOD_TEXT N_( "IO Method" )
86 #define IOMETHOD_LONGTEXT N_( \
87 "IO Method (READ, MMAP, USERPTR)." )
88 #define WIDTH_TEXT N_( "Width" )
89 #define WIDTH_LONGTEXT N_( \
90 "Force width (-1 for autodetect, 0 for driver default)." )
91 #define HEIGHT_TEXT N_( "Height" )
92 #define HEIGHT_LONGTEXT N_( \
93 "Force height (-1 for autodetect, 0 for driver default)." )
94 #define FPS_TEXT N_( "Framerate" )
95 #define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
96 "(0 for autodetect)." )
99 #define LIBV4L2_TEXT N_( "Use libv4l2" )
100 #define LIBV4L2_LONGTEXT N_( \
101 "Force usage of the libv4l2 wrapper." )
104 #define CTRL_RESET_TEXT N_( "Reset v4l2 controls" )
105 #define CTRL_RESET_LONGTEXT N_( \
106 "Reset controls to defaults provided by the v4l2 driver." )
107 #define BRIGHTNESS_TEXT N_( "Brightness" )
108 #define BRIGHTNESS_LONGTEXT N_( \
109 "Brightness of the video input (if supported by the v4l2 driver)." )
110 #define CONTRAST_TEXT N_( "Contrast" )
111 #define CONTRAST_LONGTEXT N_( \
112 "Contrast of the video input (if supported by the v4l2 driver)." )
113 #define SATURATION_TEXT N_( "Saturation" )
114 #define SATURATION_LONGTEXT N_( \
115 "Saturation of the video input (if supported by the v4l2 driver)." )
116 #define HUE_TEXT N_( "Hue" )
117 #define HUE_LONGTEXT N_( \
118 "Hue of the video input (if supported by the v4l2 driver)." )
119 #define BLACKLEVEL_TEXT N_( "Black level" )
120 #define BLACKLEVEL_LONGTEXT N_( \
121 "Black level of the video input (if supported by the v4l2 driver)." )
122 #define AUTOWHITEBALANCE_TEXT N_( "Auto white balance" )
123 #define AUTOWHITEBALANCE_LONGTEXT N_( \
124 "Automatically set the white balance of the video input " \
125 "(if supported by the v4l2 driver)." )
126 #define DOWHITEBALANCE_TEXT N_( "Do white balance" )
127 #define DOWHITEBALANCE_LONGTEXT N_( \
128 "Trigger a white balancing action, useless if auto white balance is " \
129 "activated (if supported by the v4l2 driver)." )
130 #define REDBALANCE_TEXT N_( "Red balance" )
131 #define REDBALANCE_LONGTEXT N_( \
132 "Red balance of the video input (if supported by the v4l2 driver)." )
133 #define BLUEBALANCE_TEXT N_( "Blue balance" )
134 #define BLUEBALANCE_LONGTEXT N_( \
135 "Blue balance of the video input (if supported by the v4l2 driver)." )
136 #define GAMMA_TEXT N_( "Gamma" )
137 #define GAMMA_LONGTEXT N_( \
138 "Gamma of the video input (if supported by the v4l2 driver)." )
139 #define EXPOSURE_TEXT N_( "Exposure" )
140 #define EXPOSURE_LONGTEXT N_( \
141 "Exposure of the video input (if supported by the v4L2 driver)." )
142 #define AUTOGAIN_TEXT N_( "Auto gain" )
143 #define AUTOGAIN_LONGTEXT N_( \
144 "Automatically set the video input's gain (if supported by the " \
146 #define GAIN_TEXT N_( "Gain" )
147 #define GAIN_LONGTEXT N_( \
148 "Video input's gain (if supported by the v4l2 driver)." )
149 #define HFLIP_TEXT N_( "Horizontal flip" )
150 #define HFLIP_LONGTEXT N_( \
151 "Flip the video horizontally (if supported by the v4l2 driver)." )
152 #define VFLIP_TEXT N_( "Vertical flip" )
153 #define VFLIP_LONGTEXT N_( \
154 "Flip the video vertically (if supported by the v4l2 driver)." )
155 #define HCENTER_TEXT N_( "Horizontal centering" )
156 #define HCENTER_LONGTEXT N_( \
157 "Set the camera's horizontal centering (if supported by the v4l2 driver)." )
158 #define VCENTER_TEXT N_( "Vertical centering" )
159 #define VCENTER_LONGTEXT N_( \
160 "Set the camera's vertical centering (if supported by the v4l2 driver)." )
162 #define AUDIO_VOLUME_TEXT N_( "Volume" )
163 #define AUDIO_VOLUME_LONGTEXT N_( \
164 "Volume of the audio input (if supported by the v4l2 driver)." )
165 #define AUDIO_BALANCE_TEXT N_( "Balance" )
166 #define AUDIO_BALANCE_LONGTEXT N_( \
167 "Balance of the audio input (if supported by the v4l2 driver)." )
168 #define AUDIO_MUTE_TEXT N_( "Mute" )
169 #define AUDIO_MUTE_LONGTEXT N_( \
170 "Mute audio input (if supported by the v4l2 driver)." )
171 #define AUDIO_BASS_TEXT N_( "Bass" )
172 #define AUDIO_BASS_LONGTEXT N_( \
173 "Bass level of the audio input (if supported by the v4l2 driver)." )
174 #define AUDIO_TREBLE_TEXT N_( "Treble" )
175 #define AUDIO_TREBLE_LONGTEXT N_( \
176 "Treble level of the audio input (if supported by the v4l2 driver)." )
177 #define AUDIO_LOUDNESS_TEXT N_( "Loudness" )
178 #define AUDIO_LOUDNESS_LONGTEXT N_( \
179 "Loudness of the audio input (if supported by the v4l2 driver)." )
181 #define CACHING_TEXT N_("Caching value in ms")
182 #define CACHING_LONGTEXT N_( \
183 "Caching value for V4L2 captures. This " \
184 "value should be set in milliseconds." )
185 #define S_CTRLS_TEXT N_("v4l2 driver controls")
186 #define S_CTRLS_LONGTEXT N_( \
187 "Set the v4l2 driver controls to the values specified using a comma " \
188 "separated list optionally encapsulated by curly braces " \
189 "(e.g.: {video_bitrate=6000000,audio_crc=0,stream_type=3} ). " \
190 "To list available controls, increase verbosity (-vvv) " \
191 "or use the v4l2-ctl application." )
193 #define TUNER_TEXT N_("Tuner id")
194 #define TUNER_LONGTEXT N_( \
195 "Tuner id (see debug output)." )
196 #define FREQUENCY_TEXT N_("Frequency")
197 #define FREQUENCY_LONGTEXT N_( \
198 "Tuner frequency in Hz or kHz (see debug output)" )
199 #define TUNER_AUDIO_MODE_TEXT N_("Audio mode")
200 #define TUNER_AUDIO_MODE_LONGTEXT N_( \
201 "Tuner audio mono/stereo and track selection." )
203 #define AUDIO_DEPRECATED_ERROR N_( \
204 "Alsa or OSS audio capture in the v4l2 access is deprecated. " \
205 "please use 'v4l2:/""/ :input-slave=alsa:/""/' or " \
206 "'v4l2:/""/ :input-slave=oss:/""/' instead." )
208 #define ASPECT_TEXT N_("Picture aspect-ratio n:m")
209 #define ASPECT_LONGTEXT N_("Define input picture aspect-ratio to use. Default is 4:3" )
218 static const int i_standards_list[] =
219 { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC,
220 V4L2_STD_PAL_B, V4L2_STD_PAL_B1, V4L2_STD_PAL_G, V4L2_STD_PAL_H,
221 V4L2_STD_PAL_I, V4L2_STD_PAL_D, V4L2_STD_PAL_D1, V4L2_STD_PAL_K,
222 V4L2_STD_PAL_M, V4L2_STD_PAL_N, V4L2_STD_PAL_Nc, V4L2_STD_PAL_60,
223 V4L2_STD_NTSC_M, V4L2_STD_NTSC_M_JP, V4L2_STD_NTSC_443,
225 V4L2_STD_SECAM_B, V4L2_STD_SECAM_D, V4L2_STD_SECAM_G,
226 V4L2_STD_SECAM_H, V4L2_STD_SECAM_K, V4L2_STD_SECAM_K1,
227 V4L2_STD_SECAM_L, V4L2_STD_SECAM_LC,
228 V4L2_STD_ATSC_8_VSB, V4L2_STD_ATSC_16_VSB,
230 static const char *const psz_standards_list_text[] =
231 { N_("Default"), "SECAM", "PAL", "NTSC",
232 "PAL_B", "PAL_B1", "PAL_G", "PAL_H", "PAL_I", "PAL_D",
233 "PAL_D1", "PAL_K", "PAL_M", "PAL_N", "PAL_Nc", "PAL_60",
234 "NTSC_M", "NTSC_M_JP", "NTSC_443", "NTSC_M_KR",
235 "SECAM_B", "SECAM_D", "SECAM_G", "SECAM_H", "SECAM_K",
236 "SECAM_K1", "SECAM_L", "SECAM_LC",
237 "ATSC_8_VSB", "ATSC_16_VSB"
240 static const int i_iomethod_list[] =
241 { IO_METHOD_AUTO, IO_METHOD_READ, IO_METHOD_MMAP, IO_METHOD_USERPTR };
242 static const char *const psz_iomethod_list_text[] =
243 { N_("AUTO"), N_("READ"), N_("MMAP"), N_("USERPTR") };
245 static const int i_tuner_audio_modes_list[] =
246 { V4L2_TUNER_MODE_MONO, V4L2_TUNER_MODE_STEREO,
247 V4L2_TUNER_MODE_LANG1, V4L2_TUNER_MODE_LANG2,
248 V4L2_TUNER_MODE_SAP, V4L2_TUNER_MODE_LANG1_LANG2 };
249 static const char *const psz_tuner_audio_modes_list_text[] =
252 N_( "Primary language (Analog TV tuners only)" ),
253 N_( "Secondary language (Analog TV tuners only)" ),
254 N_( "Second audio program (Analog TV tuners only)" ),
255 N_( "Primary language left, Secondary language right" ) };
257 #define V4L2_DEFAULT "/dev/video0"
258 #define CFG_PREFIX "v4l2-"
261 # define DEFAULT_WIDTH 640
262 # define DEFAULT_HEIGHT 492
265 #ifndef DEFAULT_WIDTH
266 # define DEFAULT_WIDTH (-1)
267 # define DEFAULT_HEIGHT (-1)
271 set_shortname( N_("Video4Linux2") )
272 set_description( N_("Video4Linux2 input") )
273 set_category( CAT_INPUT )
274 set_subcategory( SUBCAT_INPUT_ACCESS )
276 set_section( N_( "Video input" ), NULL )
277 add_integer( CFG_PREFIX "standard", 0, NULL, STANDARD_TEXT,
278 STANDARD_LONGTEXT, false )
279 change_integer_list( i_standards_list, psz_standards_list_text, NULL )
280 add_string( CFG_PREFIX "chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
282 add_integer( CFG_PREFIX "input", 0, NULL, INPUT_TEXT, INPUT_LONGTEXT,
284 add_integer( CFG_PREFIX "audio-input", 0, NULL, AUDIO_INPUT_TEXT,
285 AUDIO_INPUT_LONGTEXT, true )
286 add_integer( CFG_PREFIX "io", IO_METHOD_AUTO, NULL, IOMETHOD_TEXT,
287 IOMETHOD_LONGTEXT, true )
288 change_integer_list( i_iomethod_list, psz_iomethod_list_text, NULL )
289 add_integer( CFG_PREFIX "width", DEFAULT_WIDTH, NULL, WIDTH_TEXT,
290 WIDTH_LONGTEXT, true )
291 add_integer( CFG_PREFIX "height", DEFAULT_HEIGHT, NULL, HEIGHT_TEXT,
292 HEIGHT_LONGTEXT, true )
293 add_string( CFG_PREFIX "aspect-ratio", "4:3", NULL, ASPECT_TEXT,
294 ASPECT_LONGTEXT, true )
295 add_float( CFG_PREFIX "fps", 0, NULL, FPS_TEXT, FPS_LONGTEXT, true )
296 add_integer( CFG_PREFIX "caching", 50 /* ms */, NULL,
297 CACHING_TEXT, CACHING_LONGTEXT, true )
299 add_bool( CFG_PREFIX "use-libv4l2", false, NULL, LIBV4L2_TEXT, LIBV4L2_LONGTEXT, true );
302 set_section( N_( "Tuner" ), NULL )
303 add_integer( CFG_PREFIX "tuner", 0, NULL, TUNER_TEXT, TUNER_LONGTEXT,
305 add_integer( CFG_PREFIX "tuner-frequency", -1, NULL, FREQUENCY_TEXT,
306 FREQUENCY_LONGTEXT, true )
307 add_integer( CFG_PREFIX "tuner-audio-mode", -1, NULL, TUNER_AUDIO_MODE_TEXT,
308 TUNER_AUDIO_MODE_LONGTEXT, true )
309 change_integer_list( i_tuner_audio_modes_list,
310 psz_tuner_audio_modes_list_text, 0 )
312 set_section( N_( "Controls" ),
313 N_( "v4l2 driver controls, if supported by your v4l2 driver." ) )
314 add_bool( CFG_PREFIX "controls-reset", false, NULL, CTRL_RESET_TEXT,
315 CTRL_RESET_LONGTEXT, true )
316 add_integer( CFG_PREFIX "brightness", -1, NULL, BRIGHTNESS_TEXT,
317 BRIGHTNESS_LONGTEXT, true )
318 add_integer( CFG_PREFIX "contrast", -1, NULL, CONTRAST_TEXT,
319 CONTRAST_LONGTEXT, true )
320 add_integer( CFG_PREFIX "saturation", -1, NULL, SATURATION_TEXT,
321 SATURATION_LONGTEXT, true )
322 add_integer( CFG_PREFIX "hue", -1, NULL, HUE_TEXT,
324 add_integer( CFG_PREFIX "black-level", -1, NULL, BLACKLEVEL_TEXT,
325 BLACKLEVEL_LONGTEXT, true )
326 add_integer( CFG_PREFIX "auto-white-balance", -1, NULL,
327 AUTOWHITEBALANCE_TEXT, AUTOWHITEBALANCE_LONGTEXT, true )
328 add_integer( CFG_PREFIX "do-white-balance", -1, NULL, DOWHITEBALANCE_TEXT,
329 DOWHITEBALANCE_LONGTEXT, true )
330 add_integer( CFG_PREFIX "red-balance", -1, NULL, REDBALANCE_TEXT,
331 REDBALANCE_LONGTEXT, true )
332 add_integer( CFG_PREFIX "blue-balance", -1, NULL, BLUEBALANCE_TEXT,
333 BLUEBALANCE_LONGTEXT, true )
334 add_integer( CFG_PREFIX "gamma", -1, NULL, GAMMA_TEXT,
335 GAMMA_LONGTEXT, true )
336 add_integer( CFG_PREFIX "exposure", -1, NULL, EXPOSURE_TEXT,
337 EXPOSURE_LONGTEXT, true )
338 add_integer( CFG_PREFIX "autogain", -1, NULL, AUTOGAIN_TEXT,
339 AUTOGAIN_LONGTEXT, true )
340 add_integer( CFG_PREFIX "gain", -1, NULL, GAIN_TEXT,
341 GAIN_LONGTEXT, true )
342 add_integer( CFG_PREFIX "hflip", -1, NULL, HFLIP_TEXT,
343 HFLIP_LONGTEXT, true )
344 add_integer( CFG_PREFIX "vflip", -1, NULL, VFLIP_TEXT,
345 VFLIP_LONGTEXT, true )
346 add_integer( CFG_PREFIX "hcenter", -1, NULL, HCENTER_TEXT,
347 HCENTER_LONGTEXT, true )
348 add_integer( CFG_PREFIX "vcenter", -1, NULL, VCENTER_TEXT,
349 VCENTER_LONGTEXT, true )
350 add_integer( CFG_PREFIX "audio-volume", -1, NULL, AUDIO_VOLUME_TEXT,
351 AUDIO_VOLUME_LONGTEXT, true )
352 add_integer( CFG_PREFIX "audio-balance", -1, NULL, AUDIO_BALANCE_TEXT,
353 AUDIO_BALANCE_LONGTEXT, true )
354 add_bool( CFG_PREFIX "audio-mute", false, NULL, AUDIO_MUTE_TEXT,
355 AUDIO_MUTE_LONGTEXT, true )
356 add_integer( CFG_PREFIX "audio-bass", -1, NULL, AUDIO_BASS_TEXT,
357 AUDIO_BASS_LONGTEXT, true )
358 add_integer( CFG_PREFIX "audio-treble", -1, NULL, AUDIO_TREBLE_TEXT,
359 AUDIO_TREBLE_LONGTEXT, true )
360 add_integer( CFG_PREFIX "audio-loudness", -1, NULL, AUDIO_LOUDNESS_TEXT,
361 AUDIO_LOUDNESS_LONGTEXT, true )
362 add_string( CFG_PREFIX "set-ctrls", NULL, NULL, S_CTRLS_TEXT,
363 S_CTRLS_LONGTEXT, true )
365 add_obsolete_string( CFG_PREFIX "dev" )
367 add_obsolete_string( CFG_PREFIX "adev" )
368 add_obsolete_integer( CFG_PREFIX "audio-method" )
369 add_obsolete_bool( CFG_PREFIX "stereo" )
370 add_obsolete_integer( CFG_PREFIX "samplerate" )
372 add_shortcut( "v4l2" )
373 set_capability( "access_demux", 10 )
374 set_callbacks( DemuxOpen, DemuxClose )
377 add_shortcut( "v4l2" )
378 add_shortcut( "v4l2c" )
379 set_description( N_("Video4Linux2 Compressed A/V") )
380 set_capability( "access", 0 )
381 /* use these when open as access_demux fails; VLC will use another demux */
382 set_callbacks( AccessOpen, AccessClose )
386 /*****************************************************************************
387 * Access: local prototypes
388 *****************************************************************************/
390 static void CommonClose( vlc_object_t *, demux_sys_t * );
391 static void ParseMRL( demux_sys_t *, char *, vlc_object_t * );
392 static void GetV4L2Params( demux_sys_t *, vlc_object_t * );
393 static void SetAvailControlsByString( vlc_object_t *, demux_sys_t *, int );
395 static int DemuxControl( demux_t *, int, va_list );
396 static int AccessControl( access_t *, int, va_list );
398 static int Demux( demux_t * );
399 static block_t *AccessRead( access_t * );
400 static ssize_t AccessReadStream( access_t * p_access, uint8_t * p_buffer, size_t i_len );
402 static block_t* GrabVideo( vlc_object_t *p_demux, demux_sys_t *p_sys );
403 static block_t* ProcessVideoFrame( vlc_object_t *p_demux, uint8_t *p_frame, size_t );
405 static bool IsPixelFormatSupported( demux_t *p_demux,
406 unsigned int i_pixelformat );
408 static int OpenVideoDev( vlc_object_t *, demux_sys_t *, bool );
409 static bool ProbeVideoDev( vlc_object_t *, demux_sys_t *,
410 const char *psz_device );
412 static int ControlList( vlc_object_t *, demux_sys_t *, int , bool, bool );
413 static int Control( vlc_object_t *, demux_sys_t *, int i_fd,
414 const char *psz_name, int i_cid, int i_value );
416 static int DemuxControlCallback( vlc_object_t *p_this, const char *psz_var,
417 vlc_value_t oldval, vlc_value_t newval,
419 static int DemuxControlResetCallback( vlc_object_t *p_this, const char *psz_var,
420 vlc_value_t oldval, vlc_value_t newval,
422 static int AccessControlCallback( vlc_object_t *p_this, const char *psz_var,
423 vlc_value_t oldval, vlc_value_t newval,
425 static int AccessControlResetCallback( vlc_object_t *p_this,
426 const char *psz_var, vlc_value_t oldval,
427 vlc_value_t newval, void *p_data );
432 vlc_fourcc_t i_fourcc;
436 } v4l2chroma_to_fourcc[] =
439 { V4L2_PIX_FMT_GREY, VLC_CODEC_GREY, 0, 0, 0 },
440 { V4L2_PIX_FMT_HI240, VLC_FOURCC('I','2','4','0'), 0, 0, 0 },
441 { V4L2_PIX_FMT_RGB555, VLC_CODEC_RGB15, 0x001f,0x03e0,0x7c00 },
442 { V4L2_PIX_FMT_RGB565, VLC_CODEC_RGB16, 0x001f,0x07e0,0xf800 },
443 /* Won't work since we don't know how to handle such gmask values
445 { V4L2_PIX_FMT_RGB555X, VLC_CODEC_RGB15, 0x007c,0xe003,0x1f00 },
446 { V4L2_PIX_FMT_RGB565X, VLC_CODEC_RGB16, 0x00f8,0xe007,0x1f00 },
448 { V4L2_PIX_FMT_BGR24, VLC_CODEC_RGB24, 0xff0000,0xff00,0xff },
449 { V4L2_PIX_FMT_RGB24, VLC_CODEC_RGB24, 0xff,0xff00,0xff0000 },
450 { V4L2_PIX_FMT_BGR32, VLC_CODEC_RGB32, 0xff0000,0xff00,0xff },
451 { V4L2_PIX_FMT_RGB32, VLC_CODEC_RGB32, 0xff,0xff00,0xff0000 },
452 { V4L2_PIX_FMT_YUYV, VLC_CODEC_YUYV, 0, 0, 0 },
453 { V4L2_PIX_FMT_UYVY, VLC_CODEC_UYVY, 0, 0, 0 },
454 { V4L2_PIX_FMT_Y41P, VLC_FOURCC('I','4','1','N'), 0, 0, 0 },
455 { V4L2_PIX_FMT_YUV422P, VLC_CODEC_I422, 0, 0, 0 },
456 { V4L2_PIX_FMT_YVU420, VLC_CODEC_YV12, 0, 0, 0 },
457 { V4L2_PIX_FMT_YUV411P, VLC_CODEC_I411, 0, 0, 0 },
458 { V4L2_PIX_FMT_YUV410, VLC_CODEC_I410, 0, 0, 0 },
460 /* Raw data types, not in V4L2 spec but still in videodev2.h and supported
462 { V4L2_PIX_FMT_YUV420, VLC_CODEC_I420, 0, 0, 0 },
463 /* FIXME { V4L2_PIX_FMT_RGB444, VLC_CODEC_RGB32 }, */
465 /* Compressed data types */
466 { V4L2_PIX_FMT_MJPEG, VLC_CODEC_MJPG, 0, 0, 0 },
467 { V4L2_PIX_FMT_JPEG, VLC_CODEC_JPEG, 0, 0, 0 },
469 { V4L2_PIX_FMT_DV, VLC_FOURCC('?','?','?','?') },
470 { V4L2_PIX_FMT_MPEG, VLC_FOURCC('?','?','?','?') },
476 * List of V4L2 chromas were confident enough to use as fallbacks if the
477 * user hasn't provided a --v4l2-chroma value.
479 * Try YUV chromas first, then RGB little endian and MJPEG as last resort.
481 static const uint32_t p_chroma_fallbacks[] =
482 { V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_YVU420, V4L2_PIX_FMT_YUV422P,
483 V4L2_PIX_FMT_YUYV, V4L2_PIX_FMT_UYVY, V4L2_PIX_FMT_BGR24,
484 V4L2_PIX_FMT_BGR32, V4L2_PIX_FMT_MJPEG, V4L2_PIX_FMT_JPEG };
488 const char *psz_name;
492 { "brightness", V4L2_CID_BRIGHTNESS },
493 { "contrast", V4L2_CID_CONTRAST },
494 { "saturation", V4L2_CID_SATURATION },
495 { "hue", V4L2_CID_HUE },
496 { "audio-volume", V4L2_CID_AUDIO_VOLUME },
497 { "audio-balance", V4L2_CID_AUDIO_BALANCE },
498 { "audio-bass", V4L2_CID_AUDIO_BASS },
499 { "audio-treble", V4L2_CID_AUDIO_TREBLE },
500 { "audio-mute", V4L2_CID_AUDIO_MUTE },
501 { "audio-loudness", V4L2_CID_AUDIO_LOUDNESS },
502 { "black-level", V4L2_CID_BLACK_LEVEL },
503 { "auto-white-balance", V4L2_CID_AUTO_WHITE_BALANCE },
504 { "do-white-balance", V4L2_CID_DO_WHITE_BALANCE },
505 { "red-balance", V4L2_CID_RED_BALANCE },
506 { "blue-balance", V4L2_CID_BLUE_BALANCE },
507 { "gamma", V4L2_CID_GAMMA },
508 { "exposure", V4L2_CID_EXPOSURE },
509 { "autogain", V4L2_CID_AUTOGAIN },
510 { "gain", V4L2_CID_GAIN },
511 { "hflip", V4L2_CID_HFLIP },
512 { "vflip", V4L2_CID_VFLIP },
513 { "hcenter", V4L2_CID_HCENTER },
514 { "vcenter", V4L2_CID_VCENTER },
526 char *psz_device; /* Main device from MRL */
529 char *psz_requested_chroma;
536 struct v4l2_capability dev_cap;
539 struct v4l2_input *p_inputs;
540 int i_selected_input;
543 struct v4l2_standard *p_standards;
544 v4l2_std_id i_selected_standard_id;
547 /* V4L2 devices cannot have more than 32 audio inputs */
548 struct v4l2_audio p_audios[32];
549 int i_selected_audio_input;
552 struct v4l2_tuner *p_tuners;
555 struct v4l2_fmtdesc *p_codecs;
557 struct buffer_t *p_buffers;
558 unsigned int i_nbuffers;
562 unsigned int i_aspect;
563 float f_fps; /* <= 0.0 mean to grab at full rate */
564 mtime_t i_video_pts; /* only used when f_fps > 0 */
566 uint32_t i_block_flags;
581 int (*pf_open)(const char *, int, ...);
582 int (*pf_close)( int );
583 int (*pf_dup)( int );
584 int (*pf_ioctl)( int, unsigned long int, ... );
585 ssize_t (*pf_read)( int, void *, size_t );
586 void *(*pf_mmap)( void *, size_t, int, int, int, off_t );
587 int (*pf_munmap)( void *, size_t );
593 static void use_kernel_v4l2( demux_sys_t *p_sys )
595 p_sys->pf_open = utf8_open;
596 p_sys->pf_close = close;
598 p_sys->pf_ioctl = ioctl;
599 p_sys->pf_read = read;
600 p_sys->pf_mmap = mmap;
601 p_sys->pf_munmap = munmap;
602 p_sys->b_libv4l2 = false;
605 static void use_libv4l2( demux_sys_t *p_sys )
607 p_sys->pf_open = v4l2_open;
608 p_sys->pf_close = v4l2_close;
609 p_sys->pf_dup = v4l2_dup;
610 p_sys->pf_ioctl = v4l2_ioctl;
611 p_sys->pf_read = v4l2_read;
612 p_sys->pf_mmap = v4l2_mmap;
613 p_sys->pf_munmap = v4l2_munmap;
614 p_sys->b_libv4l2 = true;
617 # define v4l2_open (p_sys->pf_open)
618 # define v4l2_close (p_sys->pf_close)
619 # define v4l2_dup (p_sys->pf_dup)
620 # define v4l2_ioctl (p_sys->pf_ioctl)
621 # define v4l2_read (p_sys->pf_read)
622 # define v4l2_mmap (p_sys->pf_mmap)
623 # define v4l2_munmap (p_sys->pf_munmap)
625 # define v4l2_open utf8_open
626 # define v4l2_close close
627 # define v4l2_dup dup
628 # define v4l2_ioctl ioctl
629 # define v4l2_read read
630 # define v4l2_mmap mmap
631 # define v4l2_munmap munmap
634 static int FindMainDevice( vlc_object_t *p_this, demux_sys_t *p_sys,
637 /* TODO: if using default device, loop through all /dev/video* until
639 msg_Dbg( p_this, "opening device '%s'", p_sys->psz_device );
640 if( ProbeVideoDev( p_this, p_sys, p_sys->psz_device ) )
642 msg_Dbg( p_this, "'%s' is a video device", p_sys->psz_device );
643 p_sys->i_fd = OpenVideoDev( p_this, p_sys, b_demux );
646 if( p_sys->i_fd < 0 ) return VLC_EGENERIC;
650 /*****************************************************************************
651 * DemuxOpen: opens v4l2 device, access_demux callback
652 *****************************************************************************
654 * url: <video device>::::
656 *****************************************************************************/
657 static int DemuxOpen( vlc_object_t *p_this )
659 demux_t *p_demux = (demux_t*)p_this;
662 /* Only when selected */
663 if( strcmp( p_demux->psz_access, "v4l2" ) )
667 p_demux->pf_control = DemuxControl;
668 p_demux->pf_demux = Demux;
669 p_demux->info.i_update = 0;
670 p_demux->info.i_title = 0;
671 p_demux->info.i_seekpoint = 0;
673 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
674 if( p_sys == NULL ) return VLC_ENOMEM;
676 GetV4L2Params(p_sys, (vlc_object_t *) p_demux);
678 ParseMRL( p_sys, p_demux->psz_path, (vlc_object_t *) p_demux );
681 if( !var_InheritInteger( p_this, CFG_PREFIX "use-libv4l2" ) )
683 msg_Dbg( p_this, "Trying direct kernel v4l2" );
684 use_kernel_v4l2( p_sys );
685 if( FindMainDevice( p_this, p_sys, true ) == VLC_SUCCESS)
689 msg_Dbg( p_this, "Trying libv4l2 wrapper" );
690 use_libv4l2( p_sys );
692 if( FindMainDevice( p_this, p_sys, true ) == VLC_SUCCESS)
695 DemuxClose( p_this );
699 /*****************************************************************************
700 * GetV4L2Params: fill in p_sys parameters (shared by DemuxOpen and AccessOpen)
701 *****************************************************************************/
702 static void GetV4L2Params( demux_sys_t *p_sys, vlc_object_t *p_obj )
704 p_sys->i_video_pts = -1;
706 p_sys->i_selected_standard_id =
707 i_standards_list[var_CreateGetInteger( p_obj, "v4l2-standard" )];
709 p_sys->i_selected_input = var_CreateGetInteger( p_obj, "v4l2-input" );
710 p_sys->i_selected_audio_input =
711 var_CreateGetInteger( p_obj, "v4l2-audio-input" );
713 p_sys->io = var_CreateGetInteger( p_obj, "v4l2-io" );
715 p_sys->i_width = var_CreateGetInteger( p_obj, "v4l2-width" );
716 p_sys->i_height = var_CreateGetInteger( p_obj, "v4l2-height" );
718 var_Create( p_obj, "v4l2-controls-reset", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
720 p_sys->f_fps = var_CreateGetFloat( p_obj, "v4l2-fps" );
721 p_sys->psz_requested_chroma = var_CreateGetString( p_obj, "v4l2-chroma" );
723 p_sys->i_cache = var_CreateGetInteger( p_obj, "v4l2-caching" );
725 p_sys->i_cur_tuner = var_CreateGetInteger( p_obj, "v4l2-tuner" );
726 p_sys->i_frequency = var_CreateGetInteger( p_obj, "v4l2-tuner-frequency" );
727 p_sys->i_audio_mode = var_CreateGetInteger( p_obj, "v4l2-tuner-audio-mode" );
729 p_sys->psz_set_ctrls = var_CreateGetString( p_obj, "v4l2-set-ctrls" );
731 char *psz_aspect = var_CreateGetString( p_obj, "v4l2-aspect-ratio" );
732 char *psz_delim = !EMPTY_STR(psz_aspect) ? strchr( psz_aspect, ':' ) : NULL;
735 p_sys->i_aspect = atoi( psz_aspect ) * VOUT_ASPECT_FACTOR / atoi( psz_delim + 1 );
739 p_sys->i_aspect = 4 * VOUT_ASPECT_FACTOR / 3 ;
744 p_sys->psz_device = NULL;
750 /*****************************************************************************
751 * ParseMRL: parse the options contained in the MRL
752 *****************************************************************************/
753 static void ParseMRL( demux_sys_t *p_sys, char *psz_path, vlc_object_t *p_obj )
755 char *psz_dup = strdup( psz_path );
756 char *psz_parser = psz_dup;
758 while( *psz_parser && *psz_parser != ':' )
763 if( *psz_parser == ':' )
768 *psz_parser++ = '\0';
770 if( !strncmp( psz_parser, "standard=", strlen( "standard=" ) ) )
772 psz_parser += strlen( "standard=" );
774 for( i = 0; i < ARRAY_SIZE(psz_standards_list_text); i++ )
776 const char *psz_value = psz_standards_list_text[i];
777 size_t i_len = strlen( psz_value );
778 if( !strncasecmp( psz_parser, psz_value, i_len ) &&
779 ( psz_parser[i_len] == ':' || psz_parser[i_len] == 0 ) )
781 p_sys->i_selected_standard_id = i_standards_list[i];
787 if( i == ARRAY_SIZE(psz_standards_list_text) )
788 p_sys->i_selected_standard_id = i_standards_list[strtol( psz_parser, &psz_parser, 0 )];
790 else if( !strncmp( psz_parser, "chroma=", strlen( "chroma=" ) ) )
794 psz_parser += strlen( "chroma=" );
795 if( strchr( psz_parser, ':' ) )
797 i_len = strchr( psz_parser, ':' ) - psz_parser;
801 i_len = strlen( psz_parser );
804 free( p_sys->psz_requested_chroma );
805 p_sys->psz_requested_chroma = strndup( psz_parser, i_len );
809 else if( !strncmp( psz_parser, "input=", strlen( "input=" ) ) )
811 p_sys->i_selected_input = strtol( psz_parser + strlen( "input=" ),
814 else if( !strncmp( psz_parser, "audio-input=", strlen( "audio-input=" ) ) )
816 p_sys->i_selected_audio_input = strtol( psz_parser + strlen( "audio-input=" ),
819 else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
821 p_sys->f_fps = us_strtof( psz_parser + strlen( "fps=" ),
824 else if( !strncmp( psz_parser, "io=", strlen( "io=" ) ) )
826 psz_parser += strlen( "io=" );
827 if( !strncmp( psz_parser, "read", strlen( "read" ) ) )
829 p_sys->io = IO_METHOD_READ;
830 psz_parser += strlen( "read" );
832 else if( !strncmp( psz_parser, "mmap", strlen( "mmap" ) ) )
834 p_sys->io = IO_METHOD_MMAP;
835 psz_parser += strlen( "mmap" );
837 else if( !strncmp( psz_parser, "userptr", strlen( "userptr" ) ) )
839 p_sys->io = IO_METHOD_USERPTR;
840 psz_parser += strlen( "userptr" );
842 else if( !strncmp( psz_parser, "auto", strlen( "auto" ) ) )
844 p_sys->io = IO_METHOD_AUTO;
845 psz_parser += strlen( "auto" );
849 p_sys->io = strtol( psz_parser, &psz_parser, 0 );
852 else if( !strncmp( psz_parser, "width=",
853 strlen( "width=" ) ) )
856 strtol( psz_parser + strlen( "width=" ),
859 else if( !strncmp( psz_parser, "height=",
860 strlen( "height=" ) ) )
863 strtol( psz_parser + strlen( "height=" ),
866 else if( !strncmp( psz_parser, "aspect-ratio=",
867 strlen( "aspect-ratio=" ) ) )
869 unsigned int num,den;
870 num = strtol( psz_parser + strlen( "aspect-ratio=" ),
872 den = strtol( psz_parser + strlen( ":" ),
875 p_sys->i_aspect = num * VOUT_ASPECT_FACTOR / den;
877 else if( !strncmp( psz_parser, "controls-reset",
878 strlen( "controls-reset" ) ) )
880 var_SetBool( p_obj, "v4l2-controls-reset", true );
881 psz_parser += strlen( "controls-reset" );
884 else if( !strncmp( psz_parser, "brightness=",
885 strlen( "brightness=" ) ) )
887 var_SetInteger( p_obj, "brightness",
888 strtol( psz_parser + strlen( "brightness=" ),
891 else if( !strncmp( psz_parser, "contrast=",
892 strlen( "contrast=" ) ) )
894 var_SetInteger( p_obj, "contrast",
895 strtol( psz_parser + strlen( "contrast=" ),
898 else if( !strncmp( psz_parser, "saturation=",
899 strlen( "saturation=" ) ) )
901 var_SetInteger( p_obj, "saturation",
902 strtol( psz_parser + strlen( "saturation=" ),
905 else if( !strncmp( psz_parser, "hue=",
908 var_SetInteger( p_obj, "hue",
909 strtol( psz_parser + strlen( "hue=" ),
912 else if( !strncmp( psz_parser, "gamma=",
913 strlen( "gamma=" ) ) )
915 var_SetInteger( p_obj, "gamma",
916 strtol( psz_parser + strlen( "gamma=" ),
920 else if( !strncmp( psz_parser, "caching=", strlen( "caching=" ) ) )
922 p_sys->i_cache = strtol( psz_parser + strlen( "caching=" ),
925 else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
927 p_sys->i_cur_tuner = strtol( psz_parser + strlen( "tuner=" ),
930 else if( !strncmp( psz_parser, "tuner-frequency=", strlen( "tuner-frequency=" ) ) )
932 p_sys->i_frequency = strtol( psz_parser
933 + strlen( "tuner-frequency=" ),
936 else if( !strncmp( psz_parser, "tuner-audio-mode=", strlen( "tuner-audio-mode=" ) ) )
938 p_sys->i_audio_mode = strtol( psz_parser
939 + strlen( "tuner-audio-mode=" ),
942 else if( !strncmp( psz_parser, "set-ctrls=", strlen( "set-ctrls=" )) )
946 psz_parser += strlen( "set-ctrls=" );
947 if( strchr( psz_parser, ':' ) )
949 i_len = strchr( psz_parser, ':' ) - psz_parser;
953 i_len = strlen( psz_parser );
956 p_sys->psz_set_ctrls = strndup( psz_parser, i_len );
960 else if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) )
961 || !strncmp( psz_parser, "samplerate=", strlen( "samplerate=" ) )
962 || !strncmp( psz_parser, "audio-method", strlen( "audio-method" ) )
963 || !strncmp( psz_parser, "stereo", strlen( "stereo" ) )
964 || !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
966 if( strchr( psz_parser, ':' ) )
968 psz_parser = strchr( psz_parser, ':' );
972 psz_parser += strlen( psz_parser );
975 msg_Err( p_obj, AUDIO_DEPRECATED_ERROR );
979 char *psz_unk = strchr( psz_parser, ':' );
981 psz_unk = strndup( psz_parser, psz_unk - psz_parser );
983 psz_unk = strdup( psz_parser);
984 msg_Warn( p_obj, "unknown option %s", psz_unk );
988 while( *psz_parser && *psz_parser != ':' )
993 if( *psz_parser == '\0' )
1002 p_sys->psz_device = strdup( psz_dup );
1004 p_sys->psz_device = strdup( V4L2_DEFAULT );
1008 /*****************************************************************************
1009 * Close: close device, free resources
1010 *****************************************************************************/
1011 static void AccessClose( vlc_object_t *p_this )
1013 access_t *p_access = (access_t *)p_this;
1014 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1016 CommonClose( p_this, p_sys );
1019 static void DemuxClose( vlc_object_t *p_this )
1021 struct v4l2_buffer buf;
1022 enum v4l2_buf_type buf_type;
1025 demux_t *p_demux = (demux_t *)p_this;
1026 demux_sys_t *p_sys = p_demux->p_sys;
1028 /* Stop video capture */
1029 if( p_sys->i_fd >= 0 )
1033 case IO_METHOD_READ:
1037 case IO_METHOD_MMAP:
1038 case IO_METHOD_USERPTR:
1039 /* Some drivers 'hang' internally if this is not done before streamoff */
1040 for( unsigned int i = 0; i < p_sys->i_nbuffers; i++ )
1042 memset( &buf, 0, sizeof(buf) );
1043 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1044 buf.memory = ( p_sys->io == IO_METHOD_USERPTR ) ?
1045 V4L2_MEMORY_USERPTR : V4L2_MEMORY_MMAP;
1046 v4l2_ioctl( p_sys->i_fd, VIDIOC_DQBUF, &buf ); /* ignore result */
1049 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1050 if( v4l2_ioctl( p_sys->i_fd, VIDIOC_STREAMOFF, &buf_type ) < 0 ) {
1051 msg_Err( p_this, "VIDIOC_STREAMOFF failed" );
1061 /* Free Video Buffers */
1062 if( p_sys->p_buffers ) {
1065 case IO_METHOD_READ:
1066 free( p_sys->p_buffers[0].start );
1069 case IO_METHOD_MMAP:
1070 for( i = 0; i < p_sys->i_nbuffers; ++i )
1072 if( v4l2_munmap( p_sys->p_buffers[i].start, p_sys->p_buffers[i].length ) )
1074 msg_Err( p_this, "munmap failed" );
1079 case IO_METHOD_USERPTR:
1080 for( i = 0; i < p_sys->i_nbuffers; ++i )
1082 free( p_sys->p_buffers[i].start );
1089 free( p_sys->p_buffers );
1092 CommonClose( p_this, p_sys );
1095 static void CommonClose( vlc_object_t *p_this, demux_sys_t *p_sys )
1099 if( p_sys->i_fd >= 0 ) v4l2_close( p_sys->i_fd );
1100 free( p_sys->psz_device );
1101 free( p_sys->p_standards );
1102 free( p_sys->p_inputs );
1103 free( p_sys->p_tuners );
1104 free( p_sys->p_codecs );
1105 free( p_sys->psz_requested_chroma );
1106 free( p_sys->psz_set_ctrls );
1111 /*****************************************************************************
1112 * AccessOpen: opens v4l2 device, access callback
1113 *****************************************************************************
1115 * url: <video device>::::
1117 *****************************************************************************/
1118 static int AccessOpen( vlc_object_t * p_this )
1120 access_t *p_access = (access_t*) p_this;
1121 demux_sys_t * p_sys;
1123 /* Only when selected */
1124 if( *p_access->psz_access == '\0' ) return VLC_EGENERIC;
1126 access_InitFields( p_access );
1127 p_sys = calloc( 1, sizeof( demux_sys_t ));
1128 if( !p_sys ) return VLC_ENOMEM;
1129 p_access->p_sys = (access_sys_t*)p_sys;
1131 GetV4L2Params( p_sys, (vlc_object_t *) p_access );
1133 ParseMRL( p_sys, p_access->psz_path, (vlc_object_t *) p_access );
1136 if( !var_InheritInteger( p_this, CFG_PREFIX "use-libv4l2" ) )
1138 msg_Dbg( p_this, "Trying direct kernel v4l2" );
1139 use_kernel_v4l2( p_sys );
1140 if( FindMainDevice( p_this, p_sys, false ) == VLC_SUCCESS)
1142 if( p_sys->io == IO_METHOD_READ )
1144 ACCESS_SET_CALLBACKS( AccessReadStream, NULL, AccessControl, NULL );
1148 ACCESS_SET_CALLBACKS( NULL, AccessRead, AccessControl, NULL );
1154 msg_Dbg( p_this, "Trying libv4l2 wrapper" );
1155 use_libv4l2( p_sys );
1157 if( FindMainDevice( p_this, p_sys, false ) == VLC_SUCCESS )
1159 if( p_sys->io == IO_METHOD_READ )
1161 ACCESS_SET_CALLBACKS( AccessReadStream, NULL, AccessControl, NULL );
1165 ACCESS_SET_CALLBACKS( NULL, AccessRead, AccessControl, NULL );
1170 AccessClose( p_this );
1171 return VLC_EGENERIC;
1174 /*****************************************************************************
1176 *****************************************************************************/
1177 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
1181 /* Special for access_demux */
1182 case DEMUX_CAN_PAUSE:
1183 case DEMUX_CAN_SEEK:
1184 case DEMUX_CAN_CONTROL_PACE:
1185 *va_arg( args, bool * ) = false;
1188 case DEMUX_GET_PTS_DELAY:
1189 *va_arg(args,int64_t *) = (int64_t)p_demux->p_sys->i_cache*1000;
1192 case DEMUX_GET_TIME:
1193 *va_arg( args, int64_t * ) = mdate();
1196 /* TODO implement others */
1198 return VLC_EGENERIC;
1201 return VLC_EGENERIC;
1204 /*****************************************************************************
1205 * AccessControl: access callback
1206 *****************************************************************************/
1207 static int AccessControl( access_t *p_access, int i_query, va_list args )
1209 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1214 case ACCESS_CAN_SEEK:
1215 case ACCESS_CAN_FASTSEEK:
1216 case ACCESS_CAN_PAUSE:
1217 case ACCESS_CAN_CONTROL_PACE:
1218 *va_arg( args, bool* ) = false;
1222 case ACCESS_GET_PTS_DELAY:
1223 *va_arg(args,int64_t *) = (int64_t)p_sys->i_cache*1000;
1227 case ACCESS_SET_PAUSE_STATE:
1231 case ACCESS_GET_TITLE_INFO:
1232 case ACCESS_SET_TITLE:
1233 case ACCESS_SET_SEEKPOINT:
1234 case ACCESS_SET_PRIVATE_ID_STATE:
1235 case ACCESS_GET_CONTENT_TYPE:
1236 case ACCESS_GET_META:
1237 return VLC_EGENERIC;
1240 msg_Warn( p_access, "Unimplemented query in control(%d).", i_query);
1241 return VLC_EGENERIC;
1247 /*****************************************************************************
1248 * AccessRead: access callback
1249 ******************************************************************************/
1250 static block_t *AccessRead( access_t * p_access )
1252 demux_sys_t *p_sys = (demux_sys_t *)p_access->p_sys;
1255 fd.fd = p_sys->i_fd;
1256 fd.events = POLLIN|POLLPRI;
1260 if( poll( &fd, 1, 500 ) ) /* Timeout after 0.5 seconds since I don't know if pf_demux can be blocking. */
1262 if( fd.revents & (POLLIN|POLLPRI) )
1264 return GrabVideo( VLC_OBJECT(p_access), p_sys );
1271 static ssize_t AccessReadStream( access_t * p_access, uint8_t * p_buffer, size_t i_len )
1273 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1277 ufd.fd = p_sys->i_fd;
1278 ufd.events = POLLIN;
1280 if( p_access->info.b_eof )
1285 if( !vlc_object_alive (p_access) )
1290 while( ( i_ret = poll( &ufd, 1, 500 ) ) == 0 );
1294 msg_Err( p_access, "Polling error (%m)." );
1298 i_ret = v4l2_read( p_sys->i_fd, p_buffer, i_len );
1301 p_access->info.b_eof = true;
1303 else if( i_ret > 0 )
1305 p_access->info.i_pos += i_ret;
1311 /*****************************************************************************
1312 * Demux: Processes the audio or video frame
1313 *****************************************************************************/
1314 static int Demux( demux_t *p_demux )
1316 demux_sys_t *p_sys = p_demux->p_sys;
1319 fd.fd = p_sys->i_fd;
1320 fd.events = POLLIN|POLLPRI;
1324 if( poll( &fd, 1, 500 ) ) /* Timeout after 0.5 seconds since I don't know if pf_demux can be blocking. */
1326 if( fd.revents & (POLLIN|POLLPRI) )
1328 block_t *p_block = GrabVideo( VLC_OBJECT(p_demux), p_sys );
1331 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
1332 es_out_Send( p_demux->out, p_sys->p_es, p_block );
1340 /*****************************************************************************
1341 * GrabVideo: Grab a video frame
1342 *****************************************************************************/
1343 static block_t* GrabVideo( vlc_object_t *p_demux, demux_sys_t *p_sys )
1345 block_t *p_block = NULL;
1346 struct v4l2_buffer buf;
1349 if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
1351 mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
1353 /* Did we wait long enough ? (frame rate reduction) */
1354 if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
1357 /* Grab Video Frame */
1360 case IO_METHOD_READ:
1361 i_ret = v4l2_read( p_sys->i_fd, p_sys->p_buffers[0].start, p_sys->p_buffers[0].length );
1369 /* Could ignore EIO, see spec. */
1372 msg_Err( p_demux, "Failed to read frame" );
1377 p_block = ProcessVideoFrame( p_demux, (uint8_t*)p_sys->p_buffers[0].start, i_ret );
1378 if( !p_block ) return 0;
1382 case IO_METHOD_MMAP:
1383 memset( &buf, 0, sizeof(buf) );
1384 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1385 buf.memory = V4L2_MEMORY_MMAP;
1387 /* Wait for next frame */
1388 if (v4l2_ioctl( p_sys->i_fd, VIDIOC_DQBUF, &buf ) < 0 )
1395 /* Could ignore EIO, see spec. */
1398 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
1403 if( buf.index >= p_sys->i_nbuffers ) {
1404 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
1408 p_block = ProcessVideoFrame( p_demux, p_sys->p_buffers[buf.index].start, buf.bytesused );
1409 if( !p_block ) return 0;
1412 if( v4l2_ioctl( p_sys->i_fd, VIDIOC_QBUF, &buf ) < 0 )
1414 msg_Err( p_demux, "Failed to unlock (VIDIOC_QBUF)" );
1415 block_Release( p_block );
1421 case IO_METHOD_USERPTR:
1422 memset( &buf, 0, sizeof(buf) );
1423 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1424 buf.memory = V4L2_MEMORY_USERPTR;
1426 /* Wait for next frame */
1427 if (v4l2_ioctl( p_sys->i_fd, VIDIOC_DQBUF, &buf ) < 0 )
1434 /* Could ignore EIO, see spec. */
1437 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
1444 for( i = 0; i < p_sys->i_nbuffers; i++ )
1446 if( buf.m.userptr == (unsigned long)p_sys->p_buffers[i].start &&
1447 buf.length == p_sys->p_buffers[i].length ) break;
1450 if( i >= p_sys->i_nbuffers )
1452 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
1456 p_block = ProcessVideoFrame( p_demux, (uint8_t*)buf.m.userptr, buf.bytesused );
1457 if( !p_block ) return 0;
1460 if( v4l2_ioctl( p_sys->i_fd, VIDIOC_QBUF, &buf ) < 0 )
1462 msg_Err( p_demux, "Failed to unlock (VIDIOC_QBUF)" );
1463 block_Release( p_block );
1474 p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();
1475 p_block->i_flags |= p_sys->i_block_flags;
1480 /*****************************************************************************
1481 * ProcessVideoFrame: Helper function to take a buffer and copy it into
1483 *****************************************************************************/
1484 static block_t* ProcessVideoFrame( vlc_object_t *p_demux, uint8_t *p_frame, size_t i_size )
1488 if( !p_frame ) return 0;
1491 if( !( p_block = block_New( p_demux, i_size ) ) )
1493 msg_Warn( p_demux, "Cannot get new block" );
1498 memcpy( p_block->p_buffer, p_frame, i_size );
1503 /*****************************************************************************
1504 * Helper function to initalise video IO using the Read method
1505 *****************************************************************************/
1506 static int InitRead( vlc_object_t *p_demux, demux_sys_t *p_sys, unsigned int i_buffer_size )
1510 p_sys->p_buffers = calloc( 1, sizeof( *p_sys->p_buffers ) );
1511 if( !p_sys->p_buffers )
1512 return VLC_EGENERIC;
1514 p_sys->p_buffers[0].length = i_buffer_size;
1515 p_sys->p_buffers[0].start = malloc( i_buffer_size );
1516 if( !p_sys->p_buffers[0].start )
1522 /*****************************************************************************
1523 * Helper function to initalise video IO using the mmap method
1524 *****************************************************************************/
1525 static int InitMmap( vlc_object_t *p_demux, demux_sys_t *p_sys, int i_fd )
1527 struct v4l2_requestbuffers req;
1529 memset( &req, 0, sizeof(req) );
1531 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1532 req.memory = V4L2_MEMORY_MMAP;
1534 if( v4l2_ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1536 msg_Err( p_demux, "device does not support mmap i/o" );
1542 msg_Err( p_demux, "Insufficient buffer memory" );
1546 p_sys->p_buffers = calloc( req.count, sizeof( *p_sys->p_buffers ) );
1547 if( !p_sys->p_buffers )
1550 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < req.count; ++p_sys->i_nbuffers )
1552 struct v4l2_buffer buf;
1554 memset( &buf, 0, sizeof(buf) );
1555 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1556 buf.memory = V4L2_MEMORY_MMAP;
1557 buf.index = p_sys->i_nbuffers;
1559 if( v4l2_ioctl( i_fd, VIDIOC_QUERYBUF, &buf ) < 0 )
1561 msg_Err( p_demux, "VIDIOC_QUERYBUF" );
1565 p_sys->p_buffers[p_sys->i_nbuffers].length = buf.length;
1566 p_sys->p_buffers[p_sys->i_nbuffers].start =
1567 v4l2_mmap( NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, buf.m.offset );
1569 if( p_sys->p_buffers[p_sys->i_nbuffers].start == MAP_FAILED )
1571 msg_Err( p_demux, "mmap failed (%m)" );
1579 return VLC_EGENERIC;
1583 /*****************************************************************************
1584 * Helper function to initalise video IO using the userbuf method
1585 *****************************************************************************/
1586 static int InitUserP( vlc_object_t *p_demux, demux_sys_t *p_sys, int i_fd, unsigned int i_buffer_size )
1588 struct v4l2_requestbuffers req;
1589 unsigned int i_page_size;
1591 i_page_size = sysconf(_SC_PAGESIZE);
1592 i_buffer_size = ( i_buffer_size + i_page_size - 1 ) & ~( i_page_size - 1);
1594 memset( &req, 0, sizeof(req) );
1596 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1597 req.memory = V4L2_MEMORY_USERPTR;
1599 if( v4l2_ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1601 msg_Err( p_demux, "device does not support user pointer i/o" );
1602 return VLC_EGENERIC;
1605 p_sys->p_buffers = calloc( 4, sizeof( *p_sys->p_buffers ) );
1606 if( !p_sys->p_buffers )
1609 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < 4; ++p_sys->i_nbuffers )
1611 p_sys->p_buffers[p_sys->i_nbuffers].length = i_buffer_size;
1612 if( posix_memalign( &p_sys->p_buffers[p_sys->i_nbuffers].start,
1613 /* boundary */ i_page_size, i_buffer_size ) )
1620 free( p_sys->p_buffers );
1621 return VLC_EGENERIC;
1625 /*****************************************************************************
1626 * IsPixelFormatSupported: returns true if the specified V4L2 pixel format is
1627 * in the array of supported formats returned by the driver
1628 *****************************************************************************/
1629 static bool IsPixelFormatSupported( demux_t *p_demux, unsigned int i_pixelformat )
1631 demux_sys_t *p_sys = p_demux->p_sys;
1633 for( int i_index = 0; i_index < p_sys->i_codec; i_index++ )
1635 if( p_sys->p_codecs[i_index].pixelformat == i_pixelformat )
1642 static float GetMaxFrameRate( demux_t *p_demux, int i_fd,
1643 uint32_t i_pixel_format,
1644 uint32_t i_width, uint32_t i_height )
1647 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1649 demux_sys_t *p_sys = p_demux->p_sys;
1651 /* This is new in Linux 2.6.19 */
1652 struct v4l2_frmivalenum frmival;
1653 memset( &frmival, 0, sizeof(frmival) );
1654 frmival.pixel_format = i_pixel_format;
1655 frmival.width = i_width;
1656 frmival.height = i_height;
1657 if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 )
1659 switch( frmival.type )
1661 case V4L2_FRMIVAL_TYPE_DISCRETE:
1663 float f_fps_max = -1;
1666 float f_fps = (float)frmival.discrete.denominator
1667 / (float)frmival.discrete.numerator;
1668 if( f_fps > f_fps_max ) f_fps_max = f_fps;
1670 } while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS,
1674 case V4L2_FRMSIZE_TYPE_STEPWISE:
1675 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
1676 return __MAX( (float)frmival.stepwise.max.denominator
1677 / (float)frmival.stepwise.max.numerator,
1678 (float)frmival.stepwise.min.denominator
1679 / (float)frmival.stepwise.min.numerator );
1686 static float GetAbsoluteMaxFrameRate( demux_t *p_demux, int i_fd,
1687 uint32_t i_pixel_format )
1689 float f_fps_max = -1.;
1690 #ifdef VIDIOC_ENUM_FRAMESIZES
1692 demux_sys_t *p_sys = p_demux->p_sys;
1694 /* This is new in Linux 2.6.19 */
1695 struct v4l2_frmsizeenum frmsize;
1696 memset( &frmsize, 0, sizeof(frmsize) );
1697 frmsize.pixel_format = i_pixel_format;
1698 if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) >= 0 )
1700 switch( frmsize.type )
1702 case V4L2_FRMSIZE_TYPE_DISCRETE:
1706 float f_fps = GetMaxFrameRate( p_demux, i_fd,
1708 frmsize.discrete.width,
1709 frmsize.discrete.height );
1710 if( f_fps > f_fps_max ) f_fps_max = f_fps;
1711 } while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES,
1714 case V4L2_FRMSIZE_TYPE_STEPWISE:
1716 uint32_t i_width = frmsize.stepwise.min_width;
1717 uint32_t i_height = frmsize.stepwise.min_height;
1719 i_width <= frmsize.stepwise.max_width &&
1720 i_height <= frmsize.stepwise.max_width;
1721 i_width += frmsize.stepwise.step_width,
1722 i_height += frmsize.stepwise.step_height )
1724 float f_fps = GetMaxFrameRate( p_demux, i_fd,
1726 i_width, i_height );
1727 if( f_fps > f_fps_max ) f_fps_max = f_fps;
1731 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
1733 msg_Err( p_demux, "GetAbsoluteMaxFrameRate implementation for V4L2_FRMSIZE_TYPE_CONTINUOUS isn't correct" );
1734 f_fps_max = GetMaxFrameRate( p_demux, i_fd,
1736 frmsize.stepwise.max_width,
1737 frmsize.stepwise.max_height );
1745 static void GetMaxDimensions( demux_t *p_demux, int i_fd,
1746 uint32_t i_pixel_format, float f_fps_min,
1747 uint32_t *pi_width, uint32_t *pi_height )
1751 #ifdef VIDIOC_ENUM_FRAMESIZES
1753 demux_sys_t *p_sys = p_demux->p_sys;
1755 /* This is new in Linux 2.6.19 */
1756 struct v4l2_frmsizeenum frmsize;
1757 memset( &frmsize, 0, sizeof(frmsize) );
1758 frmsize.pixel_format = i_pixel_format;
1759 if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) >= 0 )
1761 switch( frmsize.type )
1763 case V4L2_FRMSIZE_TYPE_DISCRETE:
1767 float f_fps = GetMaxFrameRate( p_demux, i_fd,
1769 frmsize.discrete.width,
1770 frmsize.discrete.height );
1771 if( f_fps >= f_fps_min &&
1772 frmsize.discrete.width > *pi_width )
1774 *pi_width = frmsize.discrete.width;
1775 *pi_height = frmsize.discrete.height;
1777 } while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES,
1780 case V4L2_FRMSIZE_TYPE_STEPWISE:
1782 uint32_t i_width = frmsize.stepwise.min_width;
1783 uint32_t i_height = frmsize.stepwise.min_height;
1785 i_width <= frmsize.stepwise.max_width &&
1786 i_height <= frmsize.stepwise.max_width;
1787 i_width += frmsize.stepwise.step_width,
1788 i_height += frmsize.stepwise.step_height )
1790 float f_fps = GetMaxFrameRate( p_demux, i_fd,
1792 i_width, i_height );
1793 if( f_fps >= f_fps_min && i_width > *pi_width )
1795 *pi_width = i_width;
1796 *pi_height = i_height;
1801 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
1803 msg_Err( p_demux, "GetMaxDimension implementation for V4L2_FRMSIZE_TYPE_CONTINUOUS isn't correct" );
1804 float f_fps = GetMaxFrameRate( p_demux, i_fd,
1806 frmsize.stepwise.max_width,
1807 frmsize.stepwise.max_height );
1808 if( f_fps >= f_fps_min &&
1809 frmsize.stepwise.max_width > *pi_width )
1811 *pi_width = frmsize.stepwise.max_width;
1812 *pi_height = frmsize.stepwise.max_height;
1820 /*****************************************************************************
1821 * OpenVideoDev: open and set up the video device and probe for capabilities
1822 *****************************************************************************/
1823 static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
1826 struct v4l2_cropcap cropcap;
1827 struct v4l2_crop crop;
1828 struct v4l2_format fmt;
1830 enum v4l2_buf_type buf_type;
1831 const char *psz_device = p_sys->psz_device;
1834 if( ( i_fd = v4l2_open( psz_device, O_RDWR ) ) < 0 )
1836 msg_Err( p_obj, "cannot open device (%m)" );
1841 /* Note the v4l2_xxx functions are designed so that if they get passed an
1842 unknown fd, the will behave exactly as their regular xxx counterparts,
1843 so if v4l2_fd_open fails, we continue as normal (missing the libv4l2
1844 custom cam format to normal formats conversion). Chances are big we will
1845 still fail then though, as normally v4l2_fd_open only fails if the
1846 device is not a v4l2 device. */
1847 if( p_sys->b_libv4l2 )
1850 libv4l2_fd = v4l2_fd_open( i_fd, V4L2_ENABLE_ENUM_FMT_EMULATION );
1851 if( libv4l2_fd != -1 )
1856 /* Tune the tuner */
1857 if( p_sys->i_frequency >= 0 )
1859 if( p_sys->i_cur_tuner < 0 || p_sys->i_cur_tuner >= p_sys->i_tuner )
1861 msg_Err( p_obj, "invalid tuner %d.", p_sys->i_cur_tuner );
1864 struct v4l2_frequency frequency;
1865 memset( &frequency, 0, sizeof( frequency ) );
1866 frequency.tuner = p_sys->i_cur_tuner;
1867 frequency.type = p_sys->p_tuners[p_sys->i_cur_tuner].type;
1868 frequency.frequency = p_sys->i_frequency / 62.5;
1869 if( v4l2_ioctl( i_fd, VIDIOC_S_FREQUENCY, &frequency ) < 0 )
1871 msg_Err( p_obj, "cannot set tuner frequency (%m)" );
1874 msg_Dbg( p_obj, "Tuner frequency set" );
1877 /* Set the tuner's audio mode */
1878 if( p_sys->i_audio_mode >= 0 )
1880 if( p_sys->i_cur_tuner < 0 || p_sys->i_cur_tuner >= p_sys->i_tuner )
1882 msg_Err( p_obj, "invalid tuner %d.", p_sys->i_cur_tuner );
1885 struct v4l2_tuner tuner;
1886 memset( &tuner, 0, sizeof( tuner ) );
1887 tuner.index = p_sys->i_cur_tuner;
1888 tuner.audmode = p_sys->i_audio_mode;
1889 if( v4l2_ioctl( i_fd, VIDIOC_S_TUNER, &tuner ) < 0 )
1891 msg_Err( p_obj, "cannot set tuner audio mode (%m)" );
1894 msg_Dbg( p_obj, "Tuner audio mode set" );
1897 /* Select standard */
1899 if( p_sys->i_selected_standard_id != V4L2_STD_UNKNOWN )
1901 if( v4l2_ioctl( i_fd, VIDIOC_S_STD, &p_sys->i_selected_standard_id ) < 0 )
1903 msg_Err( p_obj, "cannot set standard (%m)" );
1906 if( v4l2_ioctl( i_fd, VIDIOC_G_STD, &p_sys->i_selected_standard_id ) < 0 )
1908 msg_Err( p_obj, "cannot get standard (%m). This should never happen!" );
1911 msg_Dbg( p_obj, "Set standard to (0x%"PRIx64"):", p_sys->i_selected_standard_id );
1913 for( i_standard = 0; i_standard<p_sys->i_standard; i_standard++)
1915 if( p_sys->p_standards[i_standard].id & p_sys->i_selected_standard_id )
1917 msg_Dbg( p_obj, " %s",
1918 p_sys->p_standards[i_standard].name );
1925 if( p_sys->i_selected_input >= p_sys->i_input )
1927 msg_Warn( p_obj, "invalid input. Using the default one" );
1928 p_sys->i_selected_input = 0;
1931 if( v4l2_ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_selected_input ) < 0 )
1933 msg_Err( p_obj, "cannot set input (%m)" );
1937 /* Set audio input */
1939 if( p_sys->i_audio > 0 )
1941 if( p_sys->i_selected_audio_input < 0
1942 || p_sys->i_selected_audio_input >= p_sys->i_audio )
1944 msg_Warn( p_obj, "invalid audio input. Using the default one" );
1945 p_sys->i_selected_audio_input = 0;
1948 if( v4l2_ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_selected_audio_input] ) < 0 )
1950 msg_Err( p_obj, "cannot set audio input (%m)" );
1953 msg_Dbg( p_obj, "Audio input set to %d", p_sys->i_selected_audio_input );
1957 /* TODO: Move the resolution stuff up here */
1958 /* if MPEG encoder card, no need to do anything else after this */
1959 ControlList( p_obj, p_sys, i_fd,
1960 var_GetBool( p_obj, "v4l2-controls-reset" ), b_demux );
1961 SetAvailControlsByString( p_obj, p_sys, i_fd );
1963 /* Verify device support for the various IO methods */
1966 case IO_METHOD_READ:
1967 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE) )
1969 msg_Err( p_obj, "device does not support read i/o" );
1972 msg_Dbg( p_obj, "using read i/o" );
1975 case IO_METHOD_MMAP:
1976 case IO_METHOD_USERPTR:
1977 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING) )
1979 msg_Err( p_obj, "device does not support streaming i/o" );
1982 if( p_sys->io == IO_METHOD_MMAP )
1983 msg_Dbg( p_obj, "using streaming i/o (mmap)" );
1985 msg_Dbg( p_obj, "using streaming i/o (userptr)" );
1989 msg_Err( p_obj, "io method not supported" );
1993 /* Reset Cropping */
1994 memset( &cropcap, 0, sizeof(cropcap) );
1995 cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1996 if( v4l2_ioctl( i_fd, VIDIOC_CROPCAP, &cropcap ) >= 0 )
1998 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1999 crop.c = cropcap.defrect; /* reset to default */
2000 if( crop.c.width > 0 && crop.c.height > 0 ) /* Fix for fm tuners */
2002 if( v4l2_ioctl( i_fd, VIDIOC_S_CROP, &crop ) < 0 )
2007 /* Cropping not supported. */
2010 /* Errors ignored. */
2017 /* Try and find default resolution if not specified */
2018 memset( &fmt, 0, sizeof(fmt) );
2019 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2021 if( p_sys->i_width <= 0 || p_sys->i_height <= 0 )
2023 /* Use current width and height settings */
2024 if( v4l2_ioctl( i_fd, VIDIOC_G_FMT, &fmt ) < 0 )
2026 msg_Err( p_obj, "Cannot get default width and height." );
2030 msg_Dbg( p_obj, "found default width and height of %ux%u",
2031 fmt.fmt.pix.width, fmt.fmt.pix.height );
2033 if( p_sys->i_width < 0 || p_sys->i_height < 0 )
2035 msg_Dbg( p_obj, "will try to find optimal width and height." );
2040 /* Use user specified width and height */
2041 msg_Dbg( p_obj, "trying specified size %dx%d", p_sys->i_width, p_sys->i_height );
2042 fmt.fmt.pix.width = p_sys->i_width;
2043 fmt.fmt.pix.height = p_sys->i_height;
2046 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2050 demux_t *p_demux = (demux_t *) p_obj;
2052 /* Test and set Chroma */
2053 fmt.fmt.pix.pixelformat = 0;
2054 if( p_sys->psz_requested_chroma && *p_sys->psz_requested_chroma )
2056 /* User specified chroma */
2057 const vlc_fourcc_t i_requested_fourcc =
2058 vlc_fourcc_GetCodecFromString( VIDEO_ES, p_sys->psz_requested_chroma );
2060 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
2062 if( v4l2chroma_to_fourcc[i].i_fourcc == i_requested_fourcc )
2064 fmt.fmt.pix.pixelformat = v4l2chroma_to_fourcc[i].i_v4l2;
2068 /* Try and set user chroma */
2069 bool b_error = !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat );
2070 if( !b_error && fmt.fmt.pix.pixelformat )
2072 if( v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 )
2074 fmt.fmt.pix.field = V4L2_FIELD_ANY;
2075 if( v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 )
2077 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2084 msg_Warn( p_demux, "Driver is unable to use specified chroma %s. Trying defaults.", p_sys->psz_requested_chroma );
2085 fmt.fmt.pix.pixelformat = 0;
2089 /* If no user specified chroma, find best */
2090 /* This also decides if MPEG encoder card or not */
2091 if( !fmt.fmt.pix.pixelformat )
2094 for( i = 0; i < ARRAY_SIZE( p_chroma_fallbacks ); i++ )
2096 fmt.fmt.pix.pixelformat = p_chroma_fallbacks[i];
2097 if( IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) )
2099 if( v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) >= 0 )
2101 fmt.fmt.pix.field = V4L2_FIELD_ANY;
2102 if( v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) >= 0 )
2104 fmt.fmt.pix.field = V4L2_FIELD_NONE;
2107 if( i == ARRAY_SIZE( p_chroma_fallbacks ) )
2109 msg_Warn( p_demux, "Could not select any of the default chromas; attempting to open as MPEG encoder card (access)" );
2114 if( p_sys->i_width < 0 || p_sys->i_height < 0 )
2116 if( p_sys->f_fps <= 0 )
2118 p_sys->f_fps = GetAbsoluteMaxFrameRate( p_demux, i_fd,
2119 fmt.fmt.pix.pixelformat );
2120 msg_Dbg( p_demux, "Found maximum framerate of %f", p_sys->f_fps );
2122 uint32_t i_width, i_height;
2123 GetMaxDimensions( p_demux, i_fd,
2124 fmt.fmt.pix.pixelformat, p_sys->f_fps,
2125 &i_width, &i_height );
2126 if( i_width || i_height )
2128 msg_Dbg( p_demux, "Found optimal dimensions for framerate %f "
2129 "of %ux%u", p_sys->f_fps, i_width, i_height );
2130 fmt.fmt.pix.width = i_width;
2131 fmt.fmt.pix.height = i_height;
2132 if( v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 )
2134 msg_Err( p_obj, "Cannot set size to optimal dimensions "
2135 "%ux%u", i_width, i_height );
2141 msg_Warn( p_obj, "Could not find optimal width and height, "
2142 "falling back to driver default." );
2147 p_sys->i_width = fmt.fmt.pix.width;
2148 p_sys->i_height = fmt.fmt.pix.height;
2150 if( v4l2_ioctl( i_fd, VIDIOC_G_FMT, &fmt ) < 0 ) {;}
2151 /* Print extra info */
2152 msg_Dbg( p_obj, "Driver requires at most %d bytes to store a complete image", fmt.fmt.pix.sizeimage );
2153 /* Check interlacing */
2154 switch( fmt.fmt.pix.field )
2156 case V4L2_FIELD_NONE:
2157 msg_Dbg( p_obj, "Interlacing setting: progressive" );
2159 case V4L2_FIELD_TOP:
2160 msg_Dbg( p_obj, "Interlacing setting: top field only" );
2162 case V4L2_FIELD_BOTTOM:
2163 msg_Dbg( p_obj, "Interlacing setting: bottom field only" );
2165 case V4L2_FIELD_INTERLACED:
2166 msg_Dbg( p_obj, "Interlacing setting: interleaved (bottom top if M/NTSC, top bottom otherwise)" );
2167 if( p_sys->i_selected_standard_id == V4L2_STD_NTSC )
2168 p_sys->i_block_flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST;
2170 p_sys->i_block_flags = BLOCK_FLAG_TOP_FIELD_FIRST;
2172 case V4L2_FIELD_SEQ_TB:
2173 msg_Dbg( p_obj, "Interlacing setting: sequential top bottom (TODO)" );
2175 case V4L2_FIELD_SEQ_BT:
2176 msg_Dbg( p_obj, "Interlacing setting: sequential bottom top (TODO)" );
2178 case V4L2_FIELD_ALTERNATE:
2179 msg_Dbg( p_obj, "Interlacing setting: alternate fields (TODO)" );
2180 p_sys->i_height = p_sys->i_height * 2;
2182 case V4L2_FIELD_INTERLACED_TB:
2183 msg_Dbg( p_obj, "Interlacing setting: interleaved top bottom" );
2184 p_sys->i_block_flags = BLOCK_FLAG_TOP_FIELD_FIRST;
2186 case V4L2_FIELD_INTERLACED_BT:
2187 msg_Dbg( p_obj, "Interlacing setting: interleaved bottom top" );
2188 p_sys->i_block_flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST;
2191 msg_Warn( p_obj, "Interlacing setting: unknown type (%d)",
2192 fmt.fmt.pix.field );
2196 /* Look up final fourcc */
2197 p_sys->i_fourcc = 0;
2198 for( int i = 0; v4l2chroma_to_fourcc[i].i_fourcc != 0; i++ )
2200 if( v4l2chroma_to_fourcc[i].i_v4l2 == fmt.fmt.pix.pixelformat )
2202 p_sys->i_fourcc = v4l2chroma_to_fourcc[i].i_fourcc;
2203 es_format_Init( &es_fmt, VIDEO_ES, p_sys->i_fourcc );
2204 es_fmt.video.i_rmask = v4l2chroma_to_fourcc[i].i_rmask;
2205 es_fmt.video.i_gmask = v4l2chroma_to_fourcc[i].i_gmask;
2206 es_fmt.video.i_bmask = v4l2chroma_to_fourcc[i].i_bmask;
2211 /* Buggy driver paranoia */
2212 i_min = fmt.fmt.pix.width * 2;
2213 if( fmt.fmt.pix.bytesperline < i_min )
2214 fmt.fmt.pix.bytesperline = i_min;
2215 i_min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
2216 if( fmt.fmt.pix.sizeimage < i_min )
2217 fmt.fmt.pix.sizeimage = i_min;
2219 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
2220 /* This is new in Linux 2.6.19 */
2221 /* List supported frame rates */
2222 struct v4l2_frmivalenum frmival;
2223 memset( &frmival, 0, sizeof(frmival) );
2224 frmival.pixel_format = fmt.fmt.pix.pixelformat;
2225 frmival.width = p_sys->i_width;
2226 frmival.height = p_sys->i_height;
2227 if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 )
2230 memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
2231 vlc_fourcc_to_char( p_sys->i_fourcc, &psz_fourcc );
2232 msg_Dbg( p_obj, "supported frame intervals for %4.4s, %dx%d:",
2233 psz_fourcc, frmival.width, frmival.height );
2234 switch( frmival.type )
2236 case V4L2_FRMIVAL_TYPE_DISCRETE:
2239 msg_Dbg( p_obj, " supported frame interval: %d/%d",
2240 frmival.discrete.numerator,
2241 frmival.discrete.denominator );
2243 } while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 );
2245 case V4L2_FRMIVAL_TYPE_STEPWISE:
2246 msg_Dbg( p_obj, " supported frame intervals: %d/%d to "
2247 "%d/%d using %d/%d increments",
2248 frmival.stepwise.min.numerator,
2249 frmival.stepwise.min.denominator,
2250 frmival.stepwise.max.numerator,
2251 frmival.stepwise.max.denominator,
2252 frmival.stepwise.step.numerator,
2253 frmival.stepwise.step.denominator );
2255 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
2256 msg_Dbg( p_obj, " supported frame intervals: %d/%d to %d/%d",
2257 frmival.stepwise.min.numerator,
2258 frmival.stepwise.min.denominator,
2259 frmival.stepwise.max.numerator,
2260 frmival.stepwise.max.denominator );
2267 /* Init IO method */
2270 case IO_METHOD_READ:
2272 if( InitRead( p_obj, p_sys, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
2275 case IO_METHOD_MMAP:
2276 if( InitMmap( p_obj, p_sys, i_fd ) != VLC_SUCCESS ) goto open_failed;
2279 case IO_METHOD_USERPTR:
2280 if( InitUserP( p_obj, p_sys, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
2291 es_fmt.video.i_width = p_sys->i_width;
2292 es_fmt.video.i_height = p_sys->i_height;
2294 /* Get aspect-ratio */
2295 es_fmt.video.i_sar_num = p_sys->i_aspect * es_fmt.video.i_height;
2296 es_fmt.video.i_sar_den = VOUT_ASPECT_FACTOR * es_fmt.video.i_width;
2298 demux_t *p_demux = (demux_t *) p_obj;
2299 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
2300 (char*)&es_fmt.i_codec, es_fmt.video.i_width, es_fmt.video.i_height );
2301 p_sys->p_es = es_out_Add( p_demux->out, &es_fmt );
2308 case IO_METHOD_READ:
2312 case IO_METHOD_MMAP:
2313 for (unsigned int i = 0; i < p_sys->i_nbuffers; ++i)
2315 struct v4l2_buffer buf;
2317 memset( &buf, 0, sizeof(buf) );
2318 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2319 buf.memory = V4L2_MEMORY_MMAP;
2322 if( v4l2_ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
2324 msg_Err( p_obj, "VIDIOC_QBUF failed" );
2329 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2330 if( v4l2_ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
2332 msg_Err( p_obj, "VIDIOC_STREAMON failed" );
2338 case IO_METHOD_USERPTR:
2339 for( unsigned int i = 0; i < p_sys->i_nbuffers; ++i )
2341 struct v4l2_buffer buf;
2343 memset( &buf, 0, sizeof(buf) );
2344 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2345 buf.memory = V4L2_MEMORY_USERPTR;
2347 buf.m.userptr = (unsigned long)p_sys->p_buffers[i].start;
2348 buf.length = p_sys->p_buffers[i].length;
2350 if( v4l2_ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
2352 msg_Err( p_obj, "VIDIOC_QBUF failed" );
2357 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2358 if( v4l2_ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
2360 msg_Err( p_obj, "VIDIOC_STREAMON failed" );
2372 if( p_sys->f_fps >= 0.1 )
2374 msg_Dbg( p_obj, "User set fps=%f", p_sys->f_fps );
2380 if( i_fd >= 0 ) v4l2_close( i_fd );
2385 /*****************************************************************************
2386 * ProbeVideoDev: probe video for capabilities
2387 *****************************************************************************/
2388 static bool ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
2389 const char *psz_device )
2396 if( ( i_fd = v4l2_open( psz_device, O_RDWR ) ) < 0 )
2398 msg_Err( p_obj, "cannot open video device '%s' (%m)", psz_device );
2403 /* Note the v4l2_xxx functions are designed so that if they get passed an
2404 unknown fd, the will behave exactly as their regular xxx counterparts,
2405 so if v4l2_fd_open fails, we continue as normal (missing the libv4l2
2406 custom cam format to normal formats conversion). Chances are big we will
2407 still fail then though, as normally v4l2_fd_open only fails if the
2408 device is not a v4l2 device. */
2409 if( p_sys->b_libv4l2 )
2412 libv4l2_fd = v4l2_fd_open( i_fd, V4L2_ENABLE_ENUM_FMT_EMULATION );
2413 if( libv4l2_fd != -1 )
2418 /* Get device capabilites */
2420 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCAP, &p_sys->dev_cap ) < 0 )
2422 msg_Err( p_obj, "cannot get video capabilities (%m)" );
2426 msg_Dbg( p_obj, "V4L2 device: %s using driver: %s (version: %u.%u.%u) on %s",
2427 p_sys->dev_cap.card,
2428 p_sys->dev_cap.driver,
2429 (p_sys->dev_cap.version >> 16) & 0xFF,
2430 (p_sys->dev_cap.version >> 8) & 0xFF,
2431 p_sys->dev_cap.version & 0xFF,
2432 p_sys->dev_cap.bus_info );
2434 msg_Dbg( p_obj, "the device has the capabilities: (%c) Video Capture, "
2438 ( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE ? 'X':' '),
2439 ( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO ? 'X':' '),
2440 ( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER ? 'X':' '),
2441 ( p_sys->dev_cap.capabilities & V4L2_CAP_RADIO ? 'X':' ') );
2443 msg_Dbg( p_obj, "supported I/O methods are: (%c) Read/Write, "
2445 "(%c) Asynchronous",
2446 ( p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE ? 'X':' ' ),
2447 ( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING ? 'X':' ' ),
2448 ( p_sys->dev_cap.capabilities & V4L2_CAP_ASYNCIO ? 'X':' ' ) );
2450 if( p_sys->io == IO_METHOD_AUTO )
2452 if( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING )
2453 p_sys->io = IO_METHOD_MMAP;
2454 else if( p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE )
2455 p_sys->io = IO_METHOD_READ;
2457 msg_Err( p_obj, "No known I/O method supported" );
2460 if( p_sys->dev_cap.capabilities & V4L2_CAP_RDS_CAPTURE )
2461 msg_Dbg( p_obj, "device supports RDS" );
2463 #ifdef V4L2_CAP_HW_FREQ_SEEK
2464 if( p_sys->dev_cap.capabilities & V4L2_CAP_HW_FREQ_SEEK )
2465 msg_Dbg( p_obj, "device supports hardware frequency seeking" );
2467 if( p_sys->dev_cap.capabilities & V4L2_CAP_VBI_CAPTURE )
2468 msg_Dbg( p_obj, "device support raw VBI capture" );
2470 if( p_sys->dev_cap.capabilities & V4L2_CAP_SLICED_VBI_CAPTURE )
2471 msg_Dbg( p_obj, "device support sliced VBI capture" );
2473 /* Now, enumerate all the video inputs. This is useless at the moment
2474 since we have no way to present that info to the user except with
2477 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2479 struct v4l2_input t_input;
2480 memset( &t_input, 0, sizeof(t_input) );
2482 while( v4l2_ioctl( i_fd, VIDIOC_ENUMINPUT, &t_input ) >= 0 )
2485 t_input.index = p_sys->i_input;
2488 free( p_sys->p_inputs );
2489 p_sys->p_inputs = calloc( 1, p_sys->i_input * sizeof( struct v4l2_input ) );
2490 if( !p_sys->p_inputs ) goto open_failed;
2492 for( i_index = 0; i_index < p_sys->i_input; i_index++ )
2494 p_sys->p_inputs[i_index].index = i_index;
2496 if( v4l2_ioctl( i_fd, VIDIOC_ENUMINPUT, &p_sys->p_inputs[i_index] ) )
2498 msg_Err( p_obj, "cannot get video input characteristics (%m)" );
2501 msg_Dbg( p_obj, "video input %i (%s) has type: %s %c",
2503 p_sys->p_inputs[i_index].name,
2504 p_sys->p_inputs[i_index].type
2505 == V4L2_INPUT_TYPE_TUNER ?
2507 "External analog input",
2508 i_index == p_sys->i_selected_input ? '*' : ' ' );
2512 /* Probe video standards */
2513 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2515 struct v4l2_standard t_standards;
2516 t_standards.index = 0;
2517 p_sys->i_standard = 0;
2518 while( v4l2_ioctl( i_fd, VIDIOC_ENUMSTD, &t_standards ) >=0 )
2520 p_sys->i_standard++;
2521 t_standards.index = p_sys->i_standard;
2524 free( p_sys->p_standards );
2525 p_sys->p_standards = calloc( 1, p_sys->i_standard * sizeof( struct v4l2_standard ) );
2526 if( !p_sys->p_standards ) goto open_failed;
2528 for( i_standard = 0; i_standard < p_sys->i_standard; i_standard++ )
2530 p_sys->p_standards[i_standard].index = i_standard;
2532 if( v4l2_ioctl( i_fd, VIDIOC_ENUMSTD, &p_sys->p_standards[i_standard] ) )
2534 msg_Err( p_obj, "cannot get video input standards (%m)" );
2537 msg_Dbg( p_obj, "video standard %i is: %s %c",
2539 p_sys->p_standards[i_standard].name,
2540 (p_sys->p_standards[i_standard].id & p_sys->i_selected_standard_id) ? '*' : ' ' );
2544 /* initialize the structures for the ioctls */
2545 for( i_index = 0; i_index < 32; i_index++ )
2547 p_sys->p_audios[i_index].index = i_index;
2550 /* Probe audio inputs */
2551 if( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO )
2553 while( p_sys->i_audio < 32 &&
2554 v4l2_ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_audio] ) >= 0 )
2556 if( v4l2_ioctl( i_fd, VIDIOC_G_AUDIO, &p_sys->p_audios[ p_sys->i_audio] ) < 0 )
2558 msg_Err( p_obj, "cannot get audio input characteristics (%m)" );
2562 msg_Dbg( p_obj, "audio input %i (%s) is %s %s %c",
2564 p_sys->p_audios[p_sys->i_audio].name,
2565 p_sys->p_audios[p_sys->i_audio].capability &
2566 V4L2_AUDCAP_STEREO ?
2568 p_sys->p_audios[p_sys->i_audio].capability &
2570 "(Automatic Volume Level supported)" : "",
2571 p_sys->i_audio == p_sys->i_selected_audio_input ? '*' : ' ' );
2577 /* List tuner caps */
2578 if( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER )
2580 struct v4l2_tuner tuner;
2581 memset( &tuner, 0, sizeof(tuner) );
2583 while( v4l2_ioctl( i_fd, VIDIOC_G_TUNER, &tuner ) >= 0 )
2586 memset( &tuner, 0, sizeof(tuner) );
2587 tuner.index = p_sys->i_tuner;
2590 free( p_sys->p_tuners );
2591 p_sys->p_tuners = calloc( 1, p_sys->i_tuner * sizeof( struct v4l2_tuner ) );
2592 if( !p_sys->p_tuners ) goto open_failed;
2594 for( i_index = 0; i_index < p_sys->i_tuner; i_index++ )
2596 p_sys->p_tuners[i_index].index = i_index;
2598 if( v4l2_ioctl( i_fd, VIDIOC_G_TUNER, &p_sys->p_tuners[i_index] ) )
2600 msg_Err( p_obj, "cannot get tuner characteristics (%m)" );
2603 msg_Dbg( p_obj, "tuner %i (%s) has type: %s, "
2604 "frequency range: %.1f %s -> %.1f %s",
2606 p_sys->p_tuners[i_index].name,
2607 p_sys->p_tuners[i_index].type
2608 == V4L2_TUNER_RADIO ?
2609 "Radio" : "Analog TV",
2610 p_sys->p_tuners[i_index].rangelow * 62.5,
2611 p_sys->p_tuners[i_index].capability &
2612 V4L2_TUNER_CAP_LOW ?
2614 p_sys->p_tuners[i_index].rangehigh * 62.5,
2615 p_sys->p_tuners[i_index].capability &
2616 V4L2_TUNER_CAP_LOW ?
2619 struct v4l2_frequency frequency;
2620 memset( &frequency, 0, sizeof( frequency ) );
2621 if( v4l2_ioctl( i_fd, VIDIOC_G_FREQUENCY, &frequency ) < 0 )
2623 msg_Err( p_obj, "cannot get tuner frequency (%m)" );
2626 msg_Dbg( p_obj, "tuner %i (%s) frequency: %.1f %s",
2628 p_sys->p_tuners[i_index].name,
2629 frequency.frequency * 62.5,
2630 p_sys->p_tuners[i_index].capability &
2631 V4L2_TUNER_CAP_LOW ?
2636 /* Probe for available chromas */
2637 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2639 struct v4l2_fmtdesc codec;
2642 memset( &codec, 0, sizeof(codec) );
2643 codec.index = i_index;
2644 codec.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2646 while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FMT, &codec ) >= 0 )
2649 codec.index = i_index;
2652 p_sys->i_codec = i_index;
2654 free( p_sys->p_codecs );
2655 p_sys->p_codecs = calloc( 1, p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
2657 for( i_index = 0; i_index < p_sys->i_codec; i_index++ )
2659 p_sys->p_codecs[i_index].index = i_index;
2660 p_sys->p_codecs[i_index].type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2662 if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FMT, &p_sys->p_codecs[i_index] ) < 0 )
2664 msg_Err( p_obj, "cannot get codec description (%m)" );
2668 /* only print if vlc supports the format */
2669 char psz_fourcc_v4l2[5];
2670 memset( &psz_fourcc_v4l2, 0, sizeof( psz_fourcc_v4l2 ) );
2671 vlc_fourcc_to_char( p_sys->p_codecs[i_index].pixelformat,
2673 bool b_codec_supported = false;
2674 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
2676 if( v4l2chroma_to_fourcc[i].i_v4l2 == p_sys->p_codecs[i_index].pixelformat )
2678 b_codec_supported = true;
2681 memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
2682 vlc_fourcc_to_char( v4l2chroma_to_fourcc[i].i_fourcc,
2684 msg_Dbg( p_obj, "device supports chroma %4.4s [%s, %s]",
2686 p_sys->p_codecs[i_index].description,
2689 #ifdef VIDIOC_ENUM_FRAMESIZES
2690 /* This is new in Linux 2.6.19 */
2691 /* List valid frame sizes for this format */
2692 struct v4l2_frmsizeenum frmsize;
2693 memset( &frmsize, 0, sizeof(frmsize) );
2694 frmsize.pixel_format = p_sys->p_codecs[i_index].pixelformat;
2695 if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) < 0 )
2697 /* Not all devices support this ioctl */
2698 msg_Warn( p_obj, "Unable to query for frame sizes" );
2702 switch( frmsize.type )
2704 case V4L2_FRMSIZE_TYPE_DISCRETE:
2708 " device supports size %dx%d",
2709 frmsize.discrete.width, frmsize.discrete.height );
2711 } while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) >= 0 );
2713 case V4L2_FRMSIZE_TYPE_STEPWISE:
2715 " device supports sizes %dx%d to %dx%d using %dx%d increments",
2716 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
2717 frmsize.stepwise.max_width, frmsize.stepwise.max_height,
2718 frmsize.stepwise.step_width, frmsize.stepwise.step_height );
2720 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
2722 " device supports all sizes %dx%d to %dx%d",
2723 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
2724 frmsize.stepwise.max_width, frmsize.stepwise.max_height );
2731 if( !b_codec_supported )
2734 "device codec %4.4s (%s) not supported",
2736 p_sys->p_codecs[i_index].description );
2742 if( i_fd >= 0 ) v4l2_close( i_fd );
2747 if( i_fd >= 0 ) v4l2_close( i_fd );
2752 static void name2var( unsigned char *name )
2754 for( ; *name; name++ )
2755 *name = (*name == ' ') ? '_' : tolower( *name );
2758 /*****************************************************************************
2759 * Print a user-class v4l2 control's details, create the relevant variable,
2760 * change the value if needed.
2761 *****************************************************************************/
2762 static void ControlListPrint( vlc_object_t *p_obj, demux_sys_t *p_sys, int i_fd,
2763 struct v4l2_queryctrl queryctrl,
2764 bool b_reset, bool b_demux )
2766 struct v4l2_querymenu querymenu;
2773 vlc_value_t val, val2;
2775 if( queryctrl.flags & V4L2_CTRL_FLAG_GRABBED )
2776 msg_Dbg( p_obj, " control is busy" );
2777 if( queryctrl.flags & V4L2_CTRL_FLAG_READ_ONLY )
2778 msg_Dbg( p_obj, " control is read-only" );
2780 for( i = 0; controls[i].psz_name != NULL; i++ )
2781 if( controls[i].i_cid == queryctrl.id ) break;
2783 if( controls[i].psz_name )
2785 psz_name = strdup( controls[i].psz_name );
2786 char psz_cfg_name[40];
2787 sprintf( psz_cfg_name, CFG_PREFIX "%s", psz_name );
2788 i_val = var_CreateGetInteger( p_obj, psz_cfg_name );
2789 var_Destroy( p_obj, psz_cfg_name );
2793 psz_name = strdup( (const char *)queryctrl.name );
2794 name2var( (unsigned char *)psz_name );
2798 switch( queryctrl.type )
2800 case V4L2_CTRL_TYPE_INTEGER:
2801 msg_Dbg( p_obj, " integer control" );
2803 " valid values: %d to %d by steps of %d",
2804 queryctrl.minimum, queryctrl.maximum,
2807 var_Create( p_obj, psz_name,
2808 VLC_VAR_INTEGER | VLC_VAR_HASMIN | VLC_VAR_HASMAX
2809 | VLC_VAR_HASSTEP | VLC_VAR_ISCOMMAND );
2810 val.i_int = queryctrl.minimum;
2811 var_Change( p_obj, psz_name, VLC_VAR_SETMIN, &val, NULL );
2812 val.i_int = queryctrl.maximum;
2813 var_Change( p_obj, psz_name, VLC_VAR_SETMAX, &val, NULL );
2814 val.i_int = queryctrl.step;
2815 var_Change( p_obj, psz_name, VLC_VAR_SETSTEP, &val, NULL );
2817 case V4L2_CTRL_TYPE_BOOLEAN:
2818 msg_Dbg( p_obj, " boolean control" );
2819 var_Create( p_obj, psz_name,
2820 VLC_VAR_BOOL | VLC_VAR_ISCOMMAND );
2822 case V4L2_CTRL_TYPE_MENU:
2823 msg_Dbg( p_obj, " menu control" );
2824 var_Create( p_obj, psz_name,
2825 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE
2826 | VLC_VAR_ISCOMMAND );
2827 memset( &querymenu, 0, sizeof( querymenu ) );
2828 for( i_mid = queryctrl.minimum;
2829 i_mid <= (unsigned)queryctrl.maximum;
2832 querymenu.index = i_mid;
2833 querymenu.id = queryctrl.id;
2834 if( v4l2_ioctl( i_fd, VIDIOC_QUERYMENU, &querymenu ) >= 0 )
2836 msg_Dbg( p_obj, " %d: %s",
2837 querymenu.index, querymenu.name );
2838 val.i_int = querymenu.index;
2839 val2.psz_string = (char *)querymenu.name;
2840 var_Change( p_obj, psz_name,
2841 VLC_VAR_ADDCHOICE, &val, &val2 );
2845 case V4L2_CTRL_TYPE_BUTTON:
2846 msg_Dbg( p_obj, " button control" );
2847 var_Create( p_obj, psz_name,
2848 VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
2850 case V4L2_CTRL_TYPE_CTRL_CLASS:
2851 msg_Dbg( p_obj, " control class" );
2852 var_Create( p_obj, psz_name, VLC_VAR_VOID );
2855 msg_Dbg( p_obj, " unknown control type (FIXME)" );
2860 switch( queryctrl.type )
2862 case V4L2_CTRL_TYPE_INTEGER:
2863 case V4L2_CTRL_TYPE_BOOLEAN:
2864 case V4L2_CTRL_TYPE_MENU:
2866 struct v4l2_control control;
2867 msg_Dbg( p_obj, " default value: %d",
2868 queryctrl.default_value );
2869 memset( &control, 0, sizeof( control ) );
2870 control.id = queryctrl.id;
2871 if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
2873 msg_Dbg( p_obj, " current value: %d", control.value );
2877 i_val = control.value;
2878 if( b_reset && queryctrl.default_value != control.value )
2880 msg_Dbg( p_obj, " reset value to default" );
2881 Control( p_obj, p_sys, i_fd, psz_name,
2882 queryctrl.id, queryctrl.default_value );
2887 Control( p_obj, p_sys, i_fd, psz_name,
2888 queryctrl.id, i_val );
2896 val.psz_string = (char *)queryctrl.name;
2897 var_Change( p_obj, psz_name, VLC_VAR_SETTEXT, &val, NULL );
2898 val.i_int = queryctrl.id;
2899 val2.psz_string = (char *)psz_name;
2900 var_Change( p_obj, "allcontrols", VLC_VAR_ADDCHOICE, &val, &val2 );
2901 /* bad things happen changing MPEG mid-stream
2902 * so don't add to Ext Settings GUI */
2903 if( V4L2_CTRL_ID2CLASS( queryctrl.id ) != V4L2_CTRL_CLASS_MPEG )
2904 var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
2906 switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
2909 var_SetBool( p_obj, psz_name, i_val );
2911 case VLC_VAR_INTEGER:
2912 var_SetInteger( p_obj, psz_name, i_val );
2917 msg_Warn( p_obj, "FIXME: %s %s %d", __FILE__, __func__,
2923 var_AddCallback( p_obj, psz_name,
2924 DemuxControlCallback, (void*)(intptr_t)queryctrl.id );
2926 var_AddCallback( p_obj, psz_name,
2927 AccessControlCallback, (void*)(intptr_t)queryctrl.id );
2932 /*****************************************************************************
2933 * List all user-class v4l2 controls, set them to the user specified
2934 * value and create the relevant variables to enable runtime changes
2935 *****************************************************************************/
2936 static int ControlList( vlc_object_t *p_obj, demux_sys_t *p_sys, int i_fd,
2937 bool b_reset, bool b_demux )
2939 struct v4l2_queryctrl queryctrl;
2942 memset( &queryctrl, 0, sizeof( queryctrl ) );
2944 /* A list of available controls (aka the variable name) will be
2945 * stored as choices in the "allcontrols" variable. We'll thus be able
2946 * to use those to create an appropriate interface
2947 * A list of available controls that can be changed mid-stream will
2948 * be stored in the "controls" variable */
2949 var_Create( p_obj, "controls", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
2950 var_Create( p_obj, "allcontrols", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
2952 var_Create( p_obj, "controls-update", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
2954 /* Add a control to reset all controls to their default values */
2955 vlc_value_t val, val2;
2956 var_Create( p_obj, "controls-reset", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
2957 val.psz_string = _( "Reset controls to default" );
2958 var_Change( p_obj, "controls-reset", VLC_VAR_SETTEXT, &val, NULL );
2960 val2.psz_string = (char *)"controls-reset";
2961 var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
2963 var_AddCallback( p_obj, "controls-reset", DemuxControlResetCallback, NULL );
2965 var_AddCallback( p_obj, "controls-reset", AccessControlResetCallback, NULL );
2967 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
2968 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2970 msg_Dbg( p_obj, "Extended control API supported by v4l2 driver" );
2972 /* List extended controls */
2973 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
2974 while( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2976 if( queryctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS )
2978 msg_Dbg( p_obj, "%s", queryctrl.name );
2979 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
2982 switch( V4L2_CTRL_ID2CLASS( queryctrl.id ) )
2984 case V4L2_CTRL_CLASS_USER:
2985 msg_Dbg( p_obj, "Available control: %s (%x)",
2986 queryctrl.name, queryctrl.id );
2988 case V4L2_CTRL_CLASS_MPEG:
2989 name2var( queryctrl.name );
2990 msg_Dbg( p_obj, "Available MPEG control: %s (%x)",
2991 queryctrl.name, queryctrl.id );
2994 msg_Dbg( p_obj, "Available private control: %s (%x)",
2995 queryctrl.name, queryctrl.id );
2998 ControlListPrint( p_obj, p_sys, i_fd, queryctrl, b_reset, b_demux );
2999 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
3004 msg_Dbg( p_obj, "Extended control API not supported by v4l2 driver" );
3006 /* List public controls */
3007 for( i_cid = V4L2_CID_BASE;
3008 i_cid < V4L2_CID_LASTP1;
3011 queryctrl.id = i_cid;
3012 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3014 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3016 msg_Dbg( p_obj, "Available control: %s (%x)",
3017 queryctrl.name, queryctrl.id );
3018 ControlListPrint( p_obj, p_sys, i_fd, queryctrl,
3023 /* List private controls */
3024 for( i_cid = V4L2_CID_PRIVATE_BASE;
3028 queryctrl.id = i_cid;
3029 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3031 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3033 msg_Dbg( p_obj, "Available private control: %s (%x)",
3034 queryctrl.name, queryctrl.id );
3035 ControlListPrint( p_obj, p_sys, i_fd, queryctrl,
3046 static void SetAvailControlsByString( vlc_object_t *p_obj, demux_sys_t *p_sys,
3049 char *psz_parser = p_sys->psz_set_ctrls;
3050 vlc_value_t val, text, name;
3052 if( psz_parser == NULL )
3055 if( *psz_parser == '{' )
3058 int i_ret = var_Change( p_obj, "allcontrols", VLC_VAR_GETCHOICES,
3062 msg_Err( p_obj, "Oops, can't find 'allcontrols' variable." );
3066 while( *psz_parser && *psz_parser != '}' )
3068 char *psz_delim, *psz_assign;
3070 while( *psz_parser == ',' || *psz_parser == ' ' )
3073 psz_delim = strchr( psz_parser, ',' );
3074 if( psz_delim == NULL )
3075 psz_delim = strchr( psz_parser, '}' );
3076 if( psz_delim == NULL )
3077 psz_delim = psz_parser + strlen( psz_parser );
3079 psz_assign = memchr( psz_parser, '=', psz_delim - psz_parser );
3080 if( psz_assign == NULL )
3082 char *psz_name = strndup( psz_parser, psz_delim - psz_parser );
3083 msg_Err( p_obj, "%s missing '='", psz_name );
3085 psz_parser = psz_delim + 1;
3090 i < val.p_list->i_count ;//&& psz_parser < psz_assign;
3093 const char *psz_var = text.p_list->p_values[i].psz_string;
3094 int i_cid = val.p_list->p_values[i].i_int;
3095 var_Change( p_obj, psz_var, VLC_VAR_GETTEXT, &name, NULL );
3096 const char *psz_name = name.psz_string;
3098 int i_availstrlen = strlen( psz_name );
3099 int i_parsestrlen = psz_assign - psz_parser;
3100 int i_maxstrlen = __MAX( i_availstrlen, i_parsestrlen);
3102 if( !strncasecmp( psz_name, psz_parser, i_maxstrlen ) )
3104 Control( p_obj, p_sys, i_fd, psz_name, i_cid,
3105 strtol( ++psz_assign, &psz_parser, 0) );
3107 free( name.psz_string );
3110 if( psz_parser < psz_assign )
3112 char *psz_name = strndup( psz_parser, psz_assign - psz_parser );
3113 msg_Err( p_obj, "Control %s not available", psz_name );
3115 psz_parser = ( *psz_delim ) ? ( psz_delim + 1 ) : psz_delim;
3118 var_FreeList( &val, &text );
3121 /*****************************************************************************
3122 * Reset all user-class v4l2 controls to their default value
3123 *****************************************************************************/
3124 static int ControlReset( vlc_object_t *p_obj, demux_sys_t *p_sys, int i_fd )
3126 struct v4l2_queryctrl queryctrl;
3128 memset( &queryctrl, 0, sizeof( queryctrl ) );
3130 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
3131 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3133 /* Extended control API supported */
3134 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
3135 while( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3137 if( queryctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS
3138 || V4L2_CTRL_ID2CLASS( queryctrl.id ) == V4L2_CTRL_CLASS_MPEG )
3140 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
3143 struct v4l2_control control;
3144 memset( &control, 0, sizeof( control ) );
3145 control.id = queryctrl.id;
3146 if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
3147 && queryctrl.default_value != control.value )
3150 for( i = 0; controls[i].psz_name != NULL; i++ )
3151 if( controls[i].i_cid == queryctrl.id ) break;
3152 name2var( queryctrl.name );
3153 Control( p_obj, p_sys, i_fd,
3154 controls[i].psz_name ? controls[i].psz_name
3155 : (const char *)queryctrl.name,
3156 queryctrl.id, queryctrl.default_value );
3158 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
3164 /* public controls */
3165 for( i_cid = V4L2_CID_BASE;
3166 i_cid < V4L2_CID_LASTP1;
3169 queryctrl.id = i_cid;
3170 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3172 struct v4l2_control control;
3173 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3175 memset( &control, 0, sizeof( control ) );
3176 control.id = queryctrl.id;
3177 if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
3178 && queryctrl.default_value != control.value )
3181 for( i = 0; controls[i].psz_name != NULL; i++ )
3182 if( controls[i].i_cid == queryctrl.id ) break;
3183 name2var( queryctrl.name );
3184 Control( p_obj, p_sys, i_fd,
3185 controls[i].psz_name ? controls[i].psz_name
3186 : (const char *)queryctrl.name,
3187 queryctrl.id, queryctrl.default_value );
3192 /* private controls */
3193 for( i_cid = V4L2_CID_PRIVATE_BASE;
3197 queryctrl.id = i_cid;
3198 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3200 struct v4l2_control control;
3201 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3203 memset( &control, 0, sizeof( control ) );
3204 control.id = queryctrl.id;
3205 if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
3206 && queryctrl.default_value != control.value )
3208 name2var( queryctrl.name );
3209 Control( p_obj, p_sys, i_fd, (const char *)queryctrl.name,
3210 queryctrl.id, queryctrl.default_value );
3220 /*****************************************************************************
3221 * Issue user-class v4l2 controls
3222 *****************************************************************************/
3223 static int Control( vlc_object_t *p_obj, demux_sys_t *p_sys, int i_fd,
3224 const char *psz_name, int i_cid, int i_value )
3227 struct v4l2_queryctrl queryctrl;
3228 struct v4l2_control control;
3229 struct v4l2_ext_control ext_control;
3230 struct v4l2_ext_controls ext_controls;
3235 memset( &queryctrl, 0, sizeof( queryctrl ) );
3237 queryctrl.id = i_cid;
3239 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) < 0
3240 || queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3242 msg_Dbg( p_obj, "%s (%x) control is not supported.", psz_name, i_cid );
3243 return VLC_EGENERIC;
3246 memset( &control, 0, sizeof( control ) );
3247 memset( &ext_control, 0, sizeof( ext_control ) );
3248 memset( &ext_controls, 0, sizeof( ext_controls ) );
3250 ext_control.id = i_cid;
3251 ext_controls.ctrl_class = V4L2_CTRL_ID2CLASS( i_cid );
3252 ext_controls.count = 1;
3253 ext_controls.controls = &ext_control;
3257 if( i_value >= queryctrl.minimum && i_value <= queryctrl.maximum )
3259 ext_control.value = i_value;
3260 if( v4l2_ioctl( i_fd, VIDIOC_S_EXT_CTRLS, &ext_controls ) < 0 )
3262 control.value = i_value;
3263 if( v4l2_ioctl( i_fd, VIDIOC_S_CTRL, &control ) < 0 )
3265 msg_Err( p_obj, "unable to set %s (%x) to %d (%m)",
3266 psz_name, i_cid, i_value );
3267 return VLC_EGENERIC;
3269 i_ret = v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control );
3273 i_ret = v4l2_ioctl( i_fd, VIDIOC_G_EXT_CTRLS, &ext_controls );
3274 control.value = ext_control.value;
3281 msg_Dbg( p_obj, "video %s: %d", psz_name, control.value );
3282 switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
3285 val.b_bool = control.value;
3286 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
3287 var_TriggerCallback( p_obj, "controls-update" );
3289 case VLC_VAR_INTEGER:
3290 val.i_int = control.value;
3291 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
3292 var_TriggerCallback( p_obj, "controls-update" );
3299 /*****************************************************************************
3300 * On the fly change settings callback
3301 *****************************************************************************/
3302 static int DemuxControlCallback( vlc_object_t *p_this,
3303 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3307 demux_t *p_demux = (demux_t*)p_this;
3308 demux_sys_t *p_sys = p_demux->p_sys;
3309 int i_cid = (long int)p_data;
3311 int i_fd = p_sys->i_fd;
3314 return VLC_EGENERIC;
3316 Control( p_this, p_sys, i_fd, psz_var, i_cid, newval.i_int );
3318 return VLC_EGENERIC;
3321 static int DemuxControlResetCallback( vlc_object_t *p_this,
3322 const char *psz_var, vlc_value_t oldval, vlc_value_t newval, void *p_data )
3324 (void)psz_var; (void)oldval; (void)newval; (void)p_data;
3325 demux_t *p_demux = (demux_t*)p_this;
3326 demux_sys_t *p_sys = p_demux->p_sys;
3328 int i_fd = p_sys->i_fd;
3331 return VLC_EGENERIC;
3333 ControlReset( p_this, p_sys, i_fd );
3335 return VLC_EGENERIC;
3338 static int AccessControlCallback( vlc_object_t *p_this,
3339 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3343 access_t *p_access = (access_t *)p_this;
3344 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
3345 int i_cid = (long int)p_data;
3347 int i_fd = p_sys->i_fd;
3350 return VLC_EGENERIC;
3352 Control( p_this, p_sys, i_fd, psz_var, i_cid, newval.i_int );
3354 return VLC_EGENERIC;
3357 static int AccessControlResetCallback( vlc_object_t *p_this,
3358 const char *psz_var, vlc_value_t oldval, vlc_value_t newval, void *p_data )
3360 (void)psz_var; (void)oldval; (void)newval; (void)p_data;
3361 access_t *p_access = (access_t *)p_this;
3362 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
3364 int i_fd = p_sys->i_fd;
3367 return VLC_EGENERIC;
3369 ControlReset( p_this, p_sys, i_fd );
3371 return VLC_EGENERIC;