1 /*****************************************************************************
2 * video.c : Video4Linux2 input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2009 VLC authors and VideoLAN
5 * Copyright (C) 2011-2012 RĂ©mi Denis-Courmont
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 it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
32 #include <sys/ioctl.h>
36 #include <vlc_common.h>
37 #include <vlc_block.h>
41 static int SetupStandard (vlc_object_t *obj, int fd,
42 const struct v4l2_input *restrict input,
43 v4l2_std_id *restrict std)
45 if (!(input->capabilities & V4L2_IN_CAP_STD))
47 msg_Dbg (obj, "no video standard selection");
51 *std = var_InheritStandard (obj, CFG_PREFIX"standard");
52 if (*std == V4L2_STD_UNKNOWN)
54 msg_Warn (obj, "video standard not set");
56 /* Grab the currently selected standard */
57 if (v4l2_ioctl (fd, VIDIOC_G_STD, std) < 0)
58 msg_Err (obj, "cannot get video standard");
61 if (v4l2_ioctl (fd, VIDIOC_S_STD, std) < 0)
63 msg_Err (obj, "cannot set video standard 0x%"PRIx64": %m", *std);
66 msg_Dbg (obj, "video standard set to 0x%"PRIx64":", *std);
70 static int SetupAudio (vlc_object_t *obj, int fd,
71 const struct v4l2_input *restrict input)
73 if (input->audioset == 0)
75 msg_Dbg (obj, "no audio input available");
78 msg_Dbg (obj, "available audio inputs: 0x%08"PRIX32, input->audioset);
80 uint32_t idx = var_InheritInteger (obj, CFG_PREFIX"audio-input");
81 if (idx == (uint32_t)-1)
83 msg_Dbg (obj, "no audio input selected");
86 if (((1 << idx) & input->audioset) == 0)
88 msg_Warn (obj, "skipped unavailable audio input %"PRIu32, idx);
92 /* TODO: Enumerate other selectable audio inputs. How to expose them? */
93 struct v4l2_audio enumaudio = { .index = idx };
95 if (v4l2_ioctl (fd, VIDIOC_ENUMAUDIO, &enumaudio) < 0)
97 msg_Err (obj, "cannot get audio input %"PRIu32" properties: %m", idx);
101 msg_Dbg (obj, "audio input %s (%"PRIu32") is %s"
102 " (capabilities: 0x%08"PRIX32")", enumaudio.name, enumaudio.index,
103 (enumaudio.capability & V4L2_AUDCAP_STEREO) ? "Stereo" : "Mono",
104 enumaudio.capability);
105 if (enumaudio.capability & V4L2_AUDCAP_AVL)
106 msg_Dbg (obj, " supports Automatic Volume Level");
109 struct v4l2_audio audio = { .index = idx };
111 if (v4l2_ioctl (fd, VIDIOC_S_AUDIO, &audio) < 0)
113 msg_Err (obj, "cannot select audio input %"PRIu32": %m", idx);
116 msg_Dbg (obj, "selected audio input %"PRIu32, idx);
120 int SetupTuner (vlc_object_t *obj, int fd, uint32_t idx)
122 struct v4l2_tuner tuner = { .index = idx };
124 if (v4l2_ioctl (fd, VIDIOC_G_TUNER, &tuner) < 0)
126 msg_Err (obj, "cannot get tuner %"PRIu32" properties: %m", idx);
130 /* NOTE: This is overkill. Only video devices currently work, so the
131 * type is always analog TV. */
132 const char *typename, *mult;
135 case V4L2_TUNER_RADIO:
138 case V4L2_TUNER_ANALOG_TV:
139 typename = "Analog TV";
142 typename = "unknown";
144 mult = (tuner.capability & V4L2_TUNER_CAP_LOW) ? "" : "k";
146 msg_Dbg (obj, "tuner %s (%"PRIu32") is %s", tuner.name, tuner.index,
148 msg_Dbg (obj, " ranges from %u.%u %sHz to %u.%u %sHz",
149 (tuner.rangelow * 125) >> 1, (tuner.rangelow & 1) * 5, mult,
150 (tuner.rangehigh * 125) >> 1, (tuner.rangehigh & 1) * 5,
153 /* TODO: only set video standard if the tuner requires it */
155 /* Configure the audio mode */
156 /* TODO: Ideally, L1 would be selected for stereo tuners, and L1_L2
157 * for mono tuners. When dual-mono is detected after tuning on a stereo
158 * tuner, we would fallback to L1_L2 too. Then we would flag dual-mono
159 * for the audio E/S. Unfortunately, we have no access to the audio E/S
160 * here (it belongs in the slave audio input...). */
161 tuner.audmode = var_InheritInteger (obj, CFG_PREFIX"tuner-audio-mode");
162 memset (tuner.reserved, 0, sizeof (tuner.reserved));
164 if (tuner.capability & V4L2_TUNER_CAP_LANG1)
165 msg_Dbg (obj, " supports primary audio language");
166 else if (tuner.audmode == V4L2_TUNER_MODE_LANG1)
168 msg_Warn (obj, " falling back to stereo mode");
169 tuner.audmode = V4L2_TUNER_MODE_STEREO;
171 if (tuner.capability & V4L2_TUNER_CAP_LANG2)
172 msg_Dbg (obj, " supports secondary audio language or program");
173 if (tuner.capability & V4L2_TUNER_CAP_STEREO)
174 msg_Dbg (obj, " supports stereo audio");
175 else if (tuner.audmode == V4L2_TUNER_MODE_STEREO)
177 msg_Warn (obj, " falling back to mono mode");
178 tuner.audmode = V4L2_TUNER_MODE_MONO;
181 if (v4l2_ioctl (fd, VIDIOC_S_TUNER, &tuner) < 0)
183 msg_Err (obj, "cannot set tuner %"PRIu32" audio mode: %m", idx);
186 msg_Dbg (obj, "tuner %"PRIu32" audio mode %u set", idx, tuner.audmode);
188 /* Tune to the requested frequency */
189 uint32_t freq = var_InheritInteger (obj, CFG_PREFIX"tuner-frequency");
190 if (freq != (uint32_t)-1)
192 struct v4l2_frequency frequency = {
195 .frequency = freq * 125 / 2
198 if (v4l2_ioctl (fd, VIDIOC_S_FREQUENCY, &frequency) < 0)
200 msg_Err (obj, "cannot tune tuner %"PRIu32
201 " to frequency %u %sHz: %m", idx, freq, mult);
204 msg_Dbg (obj, "tuner %"PRIu32" tuned to frequency %"PRIu32" %sHz",
208 msg_Dbg (obj, "tuner not tuned");
212 static int ResetCrop (vlc_object_t *obj, int fd)
214 struct v4l2_cropcap cropcap = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
216 /* In theory, this ioctl() must work for all video capture devices.
217 * In practice, it does not. */
218 if (v4l2_ioctl (fd, VIDIOC_CROPCAP, &cropcap) < 0)
220 msg_Dbg (obj, "cannot get cropping properties: %m");
224 /* Reset to the default cropping rectangle */
225 struct v4l2_crop crop = {
226 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
227 .c = cropcap.defrect,
230 if (v4l2_ioctl (fd, VIDIOC_S_CROP, &crop) < 0)
232 msg_Warn (obj, "cannot reset cropping limits: %m");
238 int SetupInput (vlc_object_t *obj, int fd, v4l2_std_id *std)
240 struct v4l2_input input;
242 input.index = var_InheritInteger (obj, CFG_PREFIX"input");
243 if (v4l2_ioctl (fd, VIDIOC_ENUMINPUT, &input) < 0)
245 msg_Err (obj, "invalid video input %"PRIu32": %m", input.index);
249 const char *typename = "unknown";
252 case V4L2_INPUT_TYPE_TUNER:
255 case V4L2_INPUT_TYPE_CAMERA:
260 msg_Dbg (obj, "video input %s (%"PRIu32") is %s", input.name,
261 input.index, typename);
264 if (v4l2_ioctl (fd, VIDIOC_S_INPUT, &input.index) < 0)
266 msg_Err (obj, "cannot select input %"PRIu32": %m", input.index);
269 msg_Dbg (obj, "selected input %"PRIu32, input.index);
271 SetupStandard (obj, fd, &input, std);
275 case V4L2_INPUT_TYPE_TUNER:
276 msg_Dbg (obj, "tuning required: tuner %"PRIu32, input.tuner);
277 SetupTuner (obj, fd, input.tuner);
279 case V4L2_INPUT_TYPE_CAMERA:
280 msg_Dbg (obj, "no tuning required (analog baseband input)");
283 msg_Err (obj, "unknown input tuning type %"PRIu32, input.type);
284 break; // hopefully we can stream regardless...
287 SetupAudio (obj, fd, &input);
291 /** Compares two V4L2 fractions. */
292 static int64_t fcmp (const struct v4l2_fract *a,
293 const struct v4l2_fract *b)
295 return (uint64_t)a->numerator * b->denominator
296 - (uint64_t)b->numerator * a->denominator;
299 static const struct v4l2_fract infinity = { 1, 0 };
302 * Finds the highest frame rate possible of a certain V4L2 format.
303 * @param fmt V4L2 capture format [IN]
304 * @param it V4L2 frame interval [OUT]
305 * @return 0 on success, -1 on error.
307 static int FindMaxRate (vlc_object_t *obj, int fd,
308 const struct v4l2_format *restrict fmt,
309 struct v4l2_fract *restrict it)
311 struct v4l2_frmivalenum fie = {
312 .pixel_format = fmt->fmt.pix.pixelformat,
313 .width = fmt->fmt.pix.width,
314 .height = fmt->fmt.pix.height,
316 /* Mind that maximum rate means minimum interval */
318 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &fie) < 0)
320 msg_Dbg (obj, " unknown frame intervals: %m");
321 /* Frame intervals cannot be enumerated. Set the format and then
322 * get the streaming parameters to figure out the default frame
323 * interval. This is not necessarily the maximum though. */
324 struct v4l2_format dummy_fmt = *fmt;
325 struct v4l2_streamparm parm = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
327 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &dummy_fmt) < 0
328 || v4l2_ioctl (fd, VIDIOC_G_PARM, &parm) < 0)
334 *it = parm.parm.capture.timeperframe;
335 msg_Dbg (obj, " %s frame interval: %"PRIu32"/%"PRIu32,
336 (parm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME)
337 ? "default" : "constant", it->numerator, it->denominator);
342 case V4L2_FRMIVAL_TYPE_DISCRETE:
346 if (fcmp (&fie.discrete, it) < 0)
350 while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &fie) >= 0);
352 msg_Dbg (obj, " %s frame interval: %"PRIu32"/%"PRIu32,
353 "discrete", it->numerator, it->denominator);
356 case V4L2_FRMIVAL_TYPE_STEPWISE:
357 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
358 msg_Dbg (obj, " frame intervals from %"PRIu32"/%"PRIu32
359 "to %"PRIu32"/%"PRIu32" supported",
360 fie.stepwise.min.numerator, fie.stepwise.min.denominator,
361 fie.stepwise.max.numerator, fie.stepwise.max.denominator);
362 if (fie.type == V4L2_FRMIVAL_TYPE_STEPWISE)
363 msg_Dbg (obj, " with %"PRIu32"/%"PRIu32" step",
364 fie.stepwise.step.numerator,
365 fie.stepwise.step.denominator);
366 *it = fie.stepwise.min;
374 * Finds the best possible frame rate and resolution.
375 * @param fourcc pixel format
376 * @param fmt V4L2 capture format [OUT]
377 * @param parm V4L2 capture streaming parameters [OUT]
378 * @return 0 on success, -1 on failure.
380 int SetupFormat (vlc_object_t *obj, int fd, uint32_t fourcc,
381 struct v4l2_format *restrict fmt,
382 struct v4l2_streamparm *restrict parm)
384 memset (fmt, 0, sizeof (*fmt));
385 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
386 memset (parm, 0, sizeof (*parm));
387 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
389 if (v4l2_ioctl (fd, VIDIOC_G_FMT, fmt) < 0)
391 msg_Err (obj, "cannot get default format: %m");
394 fmt->fmt.pix.pixelformat = fourcc;
396 struct v4l2_frmsizeenum fse = {
397 .pixel_format = fourcc,
399 struct v4l2_fract best_it = infinity;
400 uint64_t best_area = 0;
402 uint32_t width = var_InheritInteger (obj, CFG_PREFIX"width");
403 uint32_t height = var_InheritInteger (obj, CFG_PREFIX"height");
404 if (width > 0 && height > 0)
406 fmt->fmt.pix.width = width;
407 fmt->fmt.pix.height = height;
408 msg_Dbg (obj, " requested frame size: %"PRIu32"x%"PRIu32,
410 FindMaxRate (obj, fd, fmt, &best_it);
413 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &fse) < 0)
415 /* Fallback to current format, try to maximize frame rate */
416 msg_Dbg (obj, " unknown frame sizes: %m");
417 msg_Dbg (obj, " current frame size: %"PRIu32"x%"PRIu32,
418 fmt->fmt.pix.width, fmt->fmt.pix.height);
419 FindMaxRate (obj, fd, fmt, &best_it);
424 case V4L2_FRMSIZE_TYPE_DISCRETE:
427 struct v4l2_fract cur_it;
429 msg_Dbg (obj, " frame size %"PRIu32"x%"PRIu32,
430 fse.discrete.width, fse.discrete.height);
431 FindMaxRate (obj, fd, fmt, &cur_it);
433 int64_t c = fcmp (&cur_it, &best_it);
434 uint64_t area = fse.discrete.width * fse.discrete.height;
435 if (c < 0 || (c == 0 && area > best_area))
439 fmt->fmt.pix.width = fse.discrete.width;
440 fmt->fmt.pix.height = fse.discrete.height;
445 while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &fse) >= 0);
447 msg_Dbg (obj, " best discrete frame size: %"PRIu32"x%"PRIu32,
448 fmt->fmt.pix.width, fmt->fmt.pix.height);
451 case V4L2_FRMSIZE_TYPE_STEPWISE:
452 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
453 msg_Dbg (obj, " frame sizes from %"PRIu32"x%"PRIu32" to "
454 "%"PRIu32"x%"PRIu32" supported",
455 fse.stepwise.min_width, fse.stepwise.min_height,
456 fse.stepwise.max_width, fse.stepwise.max_height);
457 if (fse.type == V4L2_FRMSIZE_TYPE_STEPWISE)
458 msg_Dbg (obj, " with %"PRIu32"x%"PRIu32" steps",
459 fse.stepwise.step_width, fse.stepwise.step_height);
461 /* FIXME: slow and dumb */
462 for (uint32_t width = fse.stepwise.min_width;
463 width <= fse.stepwise.max_width;
464 width += fse.stepwise.step_width)
465 for (uint32_t height = fse.stepwise.min_height;
466 height <= fse.stepwise.max_width;
467 height += fse.stepwise.step_height)
469 struct v4l2_fract cur_it;
471 FindMaxRate (obj, fd, fmt, &cur_it);
473 int64_t c = fcmp (&cur_it, &best_it);
474 uint64_t area = width * height;
476 if (c < 0 || (c == 0 && area > best_area))
480 fmt->fmt.pix.width = width;
481 fmt->fmt.pix.height = height;
485 msg_Dbg (obj, " best frame size: %"PRIu32"x%"PRIu32,
486 fmt->fmt.pix.width, fmt->fmt.pix.height);
490 /* Set the final format */
491 if (v4l2_ioctl (fd, VIDIOC_S_FMT, fmt) < 0)
493 msg_Err (obj, "cannot set format: %m");
497 /* Now that the final format is set, fetch and override parameters */
498 if (v4l2_ioctl (fd, VIDIOC_G_PARM, parm) < 0)
500 msg_Err (obj, "cannot get streaming parameters: %m");
501 memset (parm, 0, sizeof (*parm));
502 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
504 parm->parm.capture.capturemode = 0; /* normal video mode */
505 parm->parm.capture.extendedmode = 0;
506 if (best_it.denominator != 0)
507 parm->parm.capture.timeperframe = best_it;
508 if (v4l2_ioctl (fd, VIDIOC_S_PARM, parm) < 0)
509 msg_Warn (obj, "cannot set streaming parameters: %m");
511 ResetCrop (obj, fd); /* crop depends on frame size */
517 /*****************************************************************************
518 * GrabVideo: Grab a video frame
519 *****************************************************************************/
520 block_t *GrabVideo (vlc_object_t *demux, int fd,
521 const struct buffer_t *restrict bufv)
523 struct v4l2_buffer buf = {
524 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
525 .memory = V4L2_MEMORY_MMAP,
528 /* Wait for next frame */
529 if (v4l2_ioctl (fd, VIDIOC_DQBUF, &buf) < 0)
536 /* Could ignore EIO, see spec. */
539 msg_Err (demux, "dequeue error: %m");
545 block_t *block = block_Alloc (buf.bytesused);
546 if (unlikely(block == NULL))
548 memcpy (block->p_buffer, bufv[buf.index].start, buf.bytesused);
551 if (v4l2_ioctl (fd, VIDIOC_QBUF, &buf) < 0)
553 msg_Err (demux, "queue error: %m");
554 block_Release (block);
561 * Allocates user pointer buffers, and start streaming.
563 int StartUserPtr (vlc_object_t *obj, int fd)
565 struct v4l2_requestbuffers reqbuf = {
566 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
567 .memory = V4L2_MEMORY_USERPTR,
571 if (v4l2_ioctl (fd, VIDIOC_REQBUFS, &reqbuf) < 0)
573 msg_Dbg (obj, "cannot reserve user buffers: %m");
576 if (v4l2_ioctl (fd, VIDIOC_STREAMON, &reqbuf.type) < 0)
578 msg_Err (obj, "cannot start streaming: %m");
585 * Allocates memory-mapped buffers, queues them and start streaming.
586 * @param n requested buffers count [IN], allocated buffers count [OUT]
587 * @return array of allocated buffers (use free()), or NULL on error.
589 struct buffer_t *StartMmap (vlc_object_t *obj, int fd, uint32_t *restrict n)
591 struct v4l2_requestbuffers req = {
593 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
594 .memory = V4L2_MEMORY_MMAP,
597 if (v4l2_ioctl (fd, VIDIOC_REQBUFS, &req) < 0)
599 msg_Err (obj, "cannot allocate buffers: %m" );
605 msg_Err (obj, "cannot allocate enough buffers");
609 struct buffer_t *bufv = malloc (req.count * sizeof (*bufv));
610 if (unlikely(bufv == NULL))
614 while (bufc < req.count)
616 struct v4l2_buffer buf = {
617 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
618 .memory = V4L2_MEMORY_MMAP,
622 if (v4l2_ioctl (fd, VIDIOC_QUERYBUF, &buf) < 0)
624 msg_Err (obj, "cannot query buffer %"PRIu32": %m", bufc);
628 bufv[bufc].start = v4l2_mmap (NULL, buf.length, PROT_READ | PROT_WRITE,
629 MAP_SHARED, fd, buf.m.offset);
630 if (bufv[bufc].start == MAP_FAILED)
632 msg_Err (obj, "cannot map buffer %"PRIu32": %m", bufc);
635 bufv[bufc].length = buf.length;
638 /* Some drivers refuse to queue buffers before they are mapped. Bug? */
639 if (v4l2_ioctl (fd, VIDIOC_QBUF, &buf) < 0)
641 msg_Err (obj, "cannot queue buffer %"PRIu32": %m", bufc);
646 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
647 if (v4l2_ioctl (fd, VIDIOC_STREAMON, &type) < 0)
649 msg_Err (obj, "cannot start streaming: %m");
655 StopMmap (fd, bufv, bufc);
659 void StopMmap (int fd, struct buffer_t *bufv, uint32_t bufc)
661 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
663 /* STREAMOFF implicitly dequeues all buffers */
664 v4l2_ioctl (fd, VIDIOC_STREAMOFF, &type);
665 for (uint32_t i = 0; i < bufc; i++)
666 v4l2_munmap (bufv[i].start, bufv[i].length);