1 /*****************************************************************************
2 * video.c : Video4Linux2 input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2009 the VideoLAN team
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
13 * it 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)
44 #ifdef V4L2_IN_CAP_STD
45 if (!(input->capabilities & V4L2_IN_CAP_STD))
47 msg_Dbg (obj, "no video standard selection");
52 msg_Dbg (obj, "video standard selection unknown");
54 v4l2_std_id std = var_InheritStandard (obj, CFG_PREFIX"standard");
55 if (std == V4L2_STD_UNKNOWN)
57 msg_Warn (obj, "video standard not set");
60 if (v4l2_ioctl (fd, VIDIOC_S_STD, &std) < 0)
62 msg_Err (obj, "cannot set video standard 0x%"PRIx64": %m", std);
65 msg_Dbg (obj, "video standard set to 0x%"PRIx64":", std);
69 static int SetupAudio (vlc_object_t *obj, int fd,
70 const struct v4l2_input *restrict input)
72 if (input->audioset == 0)
74 msg_Dbg (obj, "no audio input available");
77 msg_Dbg (obj, "available audio inputs: 0x%08"PRIX32, input->audioset);
79 uint32_t idx = var_InheritInteger (obj, CFG_PREFIX"audio-input");
80 if (idx == (uint32_t)-1)
82 msg_Dbg (obj, "no audio input selected");
85 if (((1 << idx) & input->audioset) == 0)
87 msg_Warn (obj, "skipped unavailable audio input %"PRIu32, idx);
91 /* TODO: Enumerate other selectable audio inputs. How to expose them? */
92 struct v4l2_audio enumaudio = { .index = idx };
94 if (v4l2_ioctl (fd, VIDIOC_ENUMAUDIO, &enumaudio) < 0)
96 msg_Err (obj, "cannot get audio input %"PRIu32" properties: %m", idx);
100 msg_Dbg (obj, "audio input %s (%"PRIu32") is %s"
101 " (capabilities: 0x%08"PRIX32")", enumaudio.name, enumaudio.index,
102 (enumaudio.capability & V4L2_AUDCAP_STEREO) ? "Stereo" : "Mono",
103 enumaudio.capability);
104 if (enumaudio.capability & V4L2_AUDCAP_AVL)
105 msg_Dbg (obj, " supports Automatic Volume Level");
108 struct v4l2_audio audio = { .index = idx };
110 if (v4l2_ioctl (fd, VIDIOC_S_AUDIO, &audio) < 0)
112 msg_Err (obj, "cannot select audio input %"PRIu32": %m", idx);
115 msg_Dbg (obj, "selected audio input %"PRIu32, idx);
119 static int SetupTuner (vlc_object_t *obj, int fd,
120 const struct v4l2_input *restrict input)
124 case V4L2_INPUT_TYPE_TUNER:
125 msg_Dbg (obj, "tuning required: tuner %"PRIu32, input->tuner);
127 case V4L2_INPUT_TYPE_CAMERA:
128 msg_Dbg (obj, "no tuning required (analog baseband input)");
131 msg_Err (obj, "unknown input tuning type %"PRIu32, input->type);
132 return 0; // hopefully we can stream regardless...
135 struct v4l2_tuner tuner = { .index = input->tuner };
137 if (v4l2_ioctl (fd, VIDIOC_G_TUNER, &tuner) < 0)
139 msg_Err (obj, "cannot get tuner %"PRIu32" properties: %m",
144 /* NOTE: This is overkill. Only video devices currently work, so the
145 * type is always analog TV. */
146 const char *typename, *mult;
149 case V4L2_TUNER_RADIO:
152 case V4L2_TUNER_ANALOG_TV:
153 typename = "Analog TV";
156 typename = "unknown";
158 mult = (tuner.capability & V4L2_TUNER_CAP_LOW) ? "" : "k";
160 msg_Dbg (obj, "tuner %s (%"PRIu32") is %s", tuner.name, tuner.index,
162 msg_Dbg (obj, " ranges from %u.%u %sHz to %u.%u %sHz",
163 (tuner.rangelow * 125) >> 1, (tuner.rangelow & 1) * 5, mult,
164 (tuner.rangehigh * 125) >> 1, (tuner.rangehigh & 1) * 5,
167 /* TODO: only set video standard if the tuner requires it */
169 /* Configure the audio mode */
170 /* TODO: Ideally, L1 would be selected for stereo tuners, and L1_L2
171 * for mono tuners. When dual-mono is detected after tuning on a stereo
172 * tuner, we would fallback to L1_L2 too. Then we would flag dual-mono
173 * for the audio E/S. Unfortunately, we have no access to the audio E/S
174 * here (it belongs in the slave audio input...). */
175 tuner.audmode = var_InheritInteger (obj, CFG_PREFIX"tuner-audio-mode");
176 memset (tuner.reserved, 0, sizeof (tuner.reserved));
178 if (tuner.capability & V4L2_TUNER_CAP_LANG1)
179 msg_Dbg (obj, " supports primary audio language");
180 else if (tuner.audmode == V4L2_TUNER_MODE_LANG1)
182 msg_Warn (obj, " falling back to stereo mode");
183 tuner.audmode = V4L2_TUNER_MODE_STEREO;
185 if (tuner.capability & V4L2_TUNER_CAP_LANG2)
186 msg_Dbg (obj, " supports secondary audio language or program");
187 if (tuner.capability & V4L2_TUNER_CAP_STEREO)
188 msg_Dbg (obj, " supports stereo audio");
189 else if (tuner.audmode == V4L2_TUNER_MODE_STEREO)
191 msg_Warn (obj, " falling back to mono mode");
192 tuner.audmode = V4L2_TUNER_MODE_MONO;
195 if (v4l2_ioctl (fd, VIDIOC_S_TUNER, &tuner) < 0)
197 msg_Err (obj, "cannot set tuner %"PRIu32" audio mode: %m",
201 msg_Dbg (obj, "tuner %"PRIu32" audio mode %u set", input->tuner,
204 /* Tune to the requested frequency */
205 uint32_t freq = var_InheritInteger (obj, CFG_PREFIX"tuner-frequency");
206 if (freq != (uint32_t)-1)
208 struct v4l2_frequency frequency = {
209 .tuner = input->tuner,
210 .type = V4L2_TUNER_ANALOG_TV,
211 .frequency = freq * 125 / 2
214 if (v4l2_ioctl (fd, VIDIOC_S_FREQUENCY, &frequency) < 0)
216 msg_Err (obj, "cannot tuner tuner %u to frequency %u %sHz: %m",
217 input->tuner, freq, mult);
220 msg_Dbg (obj, "tuner %"PRIu32" tuned to frequency %"PRIu32" %sHz",
221 input->tuner, freq, mult);
224 msg_Dbg (obj, "tuner not tuned");
228 static int ResetCrop (vlc_object_t *obj, int fd)
230 struct v4l2_cropcap cropcap = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
232 /* In theory, this ioctl() must work for all video capture devices.
233 * In practice, it does not. */
234 if (v4l2_ioctl (fd, VIDIOC_CROPCAP, &cropcap) < 0)
236 msg_Warn (obj, "cannot get cropping properties: %m");
240 /* Reset to the default cropping rectangle */
241 struct v4l2_crop crop = {
242 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
243 .c = cropcap.defrect,
246 if (v4l2_ioctl (fd, VIDIOC_S_CROP, &crop) < 0)
248 msg_Warn (obj, "cannot reset cropping limits: %m");
254 int SetupInput (vlc_object_t *obj, int fd)
256 struct v4l2_input input;
258 input.index = var_InheritInteger (obj, CFG_PREFIX"input");
259 if (v4l2_ioctl (fd, VIDIOC_ENUMINPUT, &input) < 0)
261 msg_Err (obj, "invalid video input %"PRIu32": %m", input.index);
265 const char *typename = "unknown";
268 case V4L2_INPUT_TYPE_TUNER:
271 case V4L2_INPUT_TYPE_CAMERA:
276 msg_Dbg (obj, "video input %s (%"PRIu32") is %s", input.name,
277 input.index, typename);
280 if (v4l2_ioctl (fd, VIDIOC_S_INPUT, &input.index) < 0)
282 msg_Err (obj, "cannot select input %"PRIu32": %m", input.index);
285 msg_Dbg (obj, "selected input %"PRIu32, input.index);
287 SetupStandard (obj, fd, &input);
288 SetupTuner (obj, fd, &input);
289 SetupAudio (obj, fd, &input);
293 /** Compares two V4L2 fractions. */
294 static int64_t fcmp (const struct v4l2_fract *a,
295 const struct v4l2_fract *b)
297 return (uint64_t)a->numerator * b->denominator
298 - (uint64_t)b->numerator * a->denominator;
301 static const struct v4l2_fract infinity = { 1, 0 };
304 * Finds the highest frame rate possible of a certain V4L2 format.
305 * @param fmt V4L2 capture format [IN]
306 * @param it V4L2 frame interval [OUT]
307 * @return 0 on success, -1 on error.
309 static int FindMaxRate (vlc_object_t *obj, int fd,
310 const struct v4l2_format *restrict fmt,
311 struct v4l2_fract *restrict it)
313 struct v4l2_frmivalenum fie = {
314 .pixel_format = fmt->fmt.pix.pixelformat,
315 .width = fmt->fmt.pix.width,
316 .height = fmt->fmt.pix.height,
318 /* Mind that maximum rate means minimum interval */
320 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &fie) < 0)
322 msg_Dbg (obj, " unknown frame intervals: %m");
323 /* Frame intervals cannot be enumerated. Set the format and then
324 * get the streaming parameters to figure out the default frame
325 * interval. This is not necessarily the maximum though. */
326 struct v4l2_format dummy_fmt = *fmt;
327 struct v4l2_streamparm parm = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
329 if (v4l2_ioctl (fd, VIDIOC_S_FMT, &dummy_fmt) < 0
330 || v4l2_ioctl (fd, VIDIOC_G_PARM, &parm) < 0)
336 *it = parm.parm.capture.timeperframe;
337 msg_Dbg (obj, " %s frame interval: %"PRIu32"/%"PRIu32,
338 (parm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME)
339 ? "default" : "constant", it->numerator, it->denominator);
344 case V4L2_FRMIVAL_TYPE_DISCRETE:
348 if (fcmp (&fie.discrete, it) < 0)
352 while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMEINTERVALS, &fie) >= 0);
354 msg_Dbg (obj, " %s frame interval: %"PRIu32"/%"PRIu32,
355 "discrete", it->numerator, it->denominator);
358 case V4L2_FRMIVAL_TYPE_STEPWISE:
359 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
360 msg_Dbg (obj, " frame intervals from %"PRIu32"/%"PRIu32
361 "to %"PRIu32"/%"PRIu32" supported",
362 fie.stepwise.min.numerator, fie.stepwise.min.denominator,
363 fie.stepwise.max.numerator, fie.stepwise.max.denominator);
364 if (fie.type == V4L2_FRMIVAL_TYPE_STEPWISE)
365 msg_Dbg (obj, " with %"PRIu32"/%"PRIu32" step",
366 fie.stepwise.step.numerator,
367 fie.stepwise.step.denominator);
368 *it = fie.stepwise.min;
376 * Finds the best possible frame rate and resolution.
377 * @param fourcc pixel format
378 * @param fmt V4L2 capture format [OUT]
379 * @param parm V4L2 capture streaming parameters [OUT]
380 * @return 0 on success, -1 on failure.
382 int SetupFormat (vlc_object_t *obj, int fd, uint32_t fourcc,
383 struct v4l2_format *restrict fmt,
384 struct v4l2_streamparm *restrict parm)
386 memset (fmt, 0, sizeof (*fmt));
387 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
388 memset (parm, 0, sizeof (*parm));
389 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
391 if (v4l2_ioctl (fd, VIDIOC_G_FMT, fmt) < 0)
393 msg_Err (obj, "cannot get default format: %m");
396 fmt->fmt.pix.pixelformat = fourcc;
398 struct v4l2_frmsizeenum fse = {
399 .pixel_format = fourcc,
401 struct v4l2_fract best_it = infinity;
402 uint64_t best_area = 0;
404 uint32_t width = var_InheritInteger (obj, CFG_PREFIX"width");
405 uint32_t height = var_InheritInteger (obj, CFG_PREFIX"height");
406 if (width > 0 && height > 0)
408 fmt->fmt.pix.width = width;
409 fmt->fmt.pix.height = height;
410 msg_Dbg (obj, " requested frame size: %"PRIu32"x%"PRIu32,
412 FindMaxRate (obj, fd, fmt, &best_it);
415 if (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &fse) < 0)
417 /* Fallback to current format, try to maximize frame rate */
418 msg_Dbg (obj, " unknown frame sizes: %m");
419 msg_Dbg (obj, " current frame size: %"PRIu32"x%"PRIu32,
420 fmt->fmt.pix.width, fmt->fmt.pix.height);
421 FindMaxRate (obj, fd, fmt, &best_it);
426 case V4L2_FRMSIZE_TYPE_DISCRETE:
429 struct v4l2_fract cur_it;
431 msg_Dbg (obj, " frame size %"PRIu32"x%"PRIu32,
432 fse.discrete.width, fse.discrete.height);
433 FindMaxRate (obj, fd, fmt, &cur_it);
435 int64_t c = fcmp (&cur_it, &best_it);
436 uint64_t area = fse.discrete.width * fse.discrete.height;
437 if (c < 0 || (c == 0 && area > best_area))
441 fmt->fmt.pix.width = fse.discrete.width;
442 fmt->fmt.pix.height = fse.discrete.height;
447 while (v4l2_ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &fse) >= 0);
449 msg_Dbg (obj, " best discrete frame size: %"PRIu32"x%"PRIu32,
450 fmt->fmt.pix.width, fmt->fmt.pix.height);
453 case V4L2_FRMSIZE_TYPE_STEPWISE:
454 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
455 msg_Dbg (obj, " frame sizes from %"PRIu32"x%"PRIu32" to "
456 "%"PRIu32"x%"PRIu32" supported",
457 fse.stepwise.min_width, fse.stepwise.min_height,
458 fse.stepwise.max_width, fse.stepwise.max_height);
459 if (fse.type == V4L2_FRMSIZE_TYPE_STEPWISE)
460 msg_Dbg (obj, " with %"PRIu32"x%"PRIu32" steps",
461 fse.stepwise.step_width, fse.stepwise.step_height);
463 /* FIXME: slow and dumb */
464 for (uint32_t width = fse.stepwise.min_width;
465 width <= fse.stepwise.max_width;
466 width += fse.stepwise.step_width)
467 for (uint32_t height = fse.stepwise.min_height;
468 height <= fse.stepwise.max_width;
469 height += fse.stepwise.step_height)
471 struct v4l2_fract cur_it;
473 FindMaxRate (obj, fd, fmt, &cur_it);
475 int64_t c = fcmp (&cur_it, &best_it);
476 uint64_t area = width * height;
478 if (c < 0 || (c == 0 && area > best_area))
482 fmt->fmt.pix.width = width;
483 fmt->fmt.pix.height = height;
487 msg_Dbg (obj, " best frame size: %"PRIu32"x%"PRIu32,
488 fmt->fmt.pix.width, fmt->fmt.pix.height);
492 /* Set the final format */
493 if (v4l2_ioctl (fd, VIDIOC_S_FMT, fmt) < 0)
495 msg_Err (obj, "cannot set format: %m");
499 /* Now that the final format is set, fetch and override parameters */
500 if (v4l2_ioctl (fd, VIDIOC_G_PARM, parm) < 0)
502 msg_Err (obj, "cannot get streaming parameters: %m");
505 parm->parm.capture.capturemode = 0; /* normal video mode */
506 parm->parm.capture.extendedmode = 0;
507 if (best_it.denominator != 0)
508 parm->parm.capture.timeperframe = best_it;
509 if (v4l2_ioctl (fd, VIDIOC_S_PARM, parm) < 0)
510 msg_Warn (obj, "cannot set streaming parameters: %m");
512 ResetCrop (obj, fd); /* crop depends on frame size */
518 /*****************************************************************************
519 * GrabVideo: Grab a video frame
520 *****************************************************************************/
521 block_t *GrabVideo (vlc_object_t *demux, int fd,
522 const struct buffer_t *restrict bufv)
524 struct v4l2_buffer buf = {
525 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
526 .memory = V4L2_MEMORY_MMAP,
529 /* Wait for next frame */
530 if (v4l2_ioctl (fd, VIDIOC_DQBUF, &buf) < 0)
537 /* Could ignore EIO, see spec. */
540 msg_Err (demux, "dequeue error: %m");
546 block_t *block = block_Alloc (buf.bytesused);
547 if (unlikely(block == NULL))
549 memcpy (block->p_buffer, bufv[buf.index].start, buf.bytesused);
552 if (v4l2_ioctl (fd, VIDIOC_QBUF, &buf) < 0)
554 msg_Err (demux, "queue error: %m");
555 block_Release (block);
562 * Allocates memory-mapped buffers, queues them and start streaming.
563 * @param n requested buffers count [IN], allocated buffers count [OUT]
564 * @return array of allocated buffers (use free()), or NULL on error.
566 struct buffer_t *StartMmap (vlc_object_t *obj, int fd, uint32_t *restrict n)
568 struct v4l2_requestbuffers req = {
570 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
571 .memory = V4L2_MEMORY_MMAP,
574 if (v4l2_ioctl (fd, VIDIOC_REQBUFS, &req) < 0)
576 msg_Err (obj, "cannot allocate buffers: %m" );
582 msg_Err (obj, "cannot allocate enough buffers");
586 struct buffer_t *bufv = malloc (req.count * sizeof (*bufv));
587 if (unlikely(bufv == NULL))
591 while (bufc < req.count)
593 struct v4l2_buffer buf = {
594 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
595 .memory = V4L2_MEMORY_MMAP,
599 if (v4l2_ioctl (fd, VIDIOC_QUERYBUF, &buf) < 0)
601 msg_Err (obj, "cannot query buffer %"PRIu32": %m", bufc);
605 bufv[bufc].start = v4l2_mmap (NULL, buf.length, PROT_READ | PROT_WRITE,
606 MAP_SHARED, fd, buf.m.offset);
607 if (bufv[bufc].start == MAP_FAILED)
609 msg_Err (obj, "cannot map buffer %"PRIu32": %m", bufc);
612 bufv[bufc].length = buf.length;
615 /* Some drivers refuse to queue buffers before they are mapped. Bug? */
616 if (v4l2_ioctl (fd, VIDIOC_QBUF, &buf) < 0)
618 msg_Err (obj, "cannot queue buffer %"PRIu32": %m", bufc);
623 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
624 if (v4l2_ioctl (fd, VIDIOC_STREAMON, &type) < 0)
626 msg_Err (obj, "cannot start streaming: %m");
632 StopMmap (fd, bufv, bufc);
636 void StopMmap (int fd, struct buffer_t *bufv, uint32_t bufc)
638 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
640 /* STREAMOFF implicitly dequeues all buffers */
641 v4l2_ioctl (fd, VIDIOC_STREAMOFF, &type);
642 for (uint32_t i = bufc; i < bufc; i++)
643 v4l2_munmap (bufv[i].start, bufv[i].length);