blob: fcd78dfed320a91708f5618221f12f2391cb0635 [file] [log] [blame]
Kuang-che Wudb9c20b2014-04-17 16:14:18 +08001// Copyright 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Kuang-che Wudb9c20b2014-04-17 16:14:18 +08004#include <linux/videodev2.h>
Kuang-che Wuca2bd582014-07-22 18:24:45 +08005#include <stdio.h>
6#include <string.h>
Kuang-che Wudb9c20b2014-04-17 16:14:18 +08007
8#include "label_detect.h"
9
10/* Returns true if device fd supports given format of buffer type.
11 * Example of buf_type: V4L2_BUF_TYPE_VIDEO_OUTPUT,
12 * V4L2_BUF_TYPE_VIDEO_CAPTURE.
13 * */
Tom Hughes0e816952020-08-24 18:04:24 -070014bool is_v4l2_support_format(int fd,
15 enum v4l2_buf_type buf_type,
16 uint32_t fourcc) {
Kuang-che Wudb9c20b2014-04-17 16:14:18 +080017 int i;
18 bool found = false;
19 char fourcc_str[5];
20
21 convert_fourcc_to_str(fourcc, fourcc_str);
22 TRACE("is_v4l2_support_format(%s)\n", fourcc_str);
Tom Hughes0e816952020-08-24 18:04:24 -070023 for (i = 0;; ++i) {
Kuang-che Wudb9c20b2014-04-17 16:14:18 +080024 struct v4l2_fmtdesc format_desc;
25 memset(&format_desc, 0, sizeof(format_desc));
Tom Hughes0e816952020-08-24 18:04:24 -070026 format_desc.type = (enum v4l2_buf_type)buf_type;
Kuang-che Wudb9c20b2014-04-17 16:14:18 +080027 format_desc.index = i;
28 if (-1 == do_ioctl(fd, VIDIOC_ENUM_FMT, &format_desc)) {
29 break;
30 }
31 convert_fourcc_to_str(format_desc.pixelformat, fourcc_str);
32 TRACE("%s supported\n", fourcc_str);
33 if (format_desc.pixelformat == fourcc) {
34 found = true;
35 /* continue the loop in order to output all supported formats */
36 }
37 }
38 TRACE("is_v4l2_support_format: %s\n", found ? "true" : "false");
39 return found;
40}
41
henryhsu5c4b6642015-08-13 14:15:00 +080042/* Returns true if device fd is V4L2 video encode/decode device. */
Kuang-che Wudb9c20b2014-04-17 16:14:18 +080043bool is_hw_video_acc_device(int fd) {
44 struct v4l2_capability cap;
45 int ret = do_ioctl(fd, VIDIOC_QUERYCAP, &cap);
46 if (ret == 0) {
Wu-Cheng Li853d0532016-05-19 13:19:01 +080047 if ((cap.capabilities & V4L2_CAP_STREAMING) &&
48 (cap.capabilities & V4L2_CAP_VIDEO_M2M_MPLANE)) {
Kuang-che Wudb9c20b2014-04-17 16:14:18 +080049 TRACE("is_hw_video_acc_device: true\n");
50 return true;
51 }
52 }
53 TRACE("is_hw_video_acc_device: false\n");
54 return false;
55}
henryhsu5c4b6642015-08-13 14:15:00 +080056
57/* Returns true if device fd is V4L2 jpeg encode/decode device. */
58bool is_hw_jpeg_acc_device(int fd) {
59 struct v4l2_capability cap;
60 int ret = do_ioctl(fd, VIDIOC_QUERYCAP, &cap);
61 if (ret == 0) {
henryhsu5f8cd9d2015-08-17 14:15:04 +080062 if ((cap.capabilities & V4L2_CAP_STREAMING) &&
Kuang-che Wu1307bd42017-01-03 17:54:42 +080063 (cap.capabilities & V4L2_CAP_VIDEO_M2M_MPLANE)) {
henryhsu5c4b6642015-08-13 14:15:00 +080064 TRACE("is_hw_jpeg_acc_device: true\n");
65 return true;
66 }
67 }
68 TRACE("is_hw_jpeg_acc_device: false\n");
69 return false;
70}
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +090071
72/* Returns success or failure of getting resolution. The maximum resolution is
73 returned through arguments. */
Tom Hughes0e816952020-08-24 18:04:24 -070074bool get_v4l2_max_resolution(int fd,
75 uint32_t fourcc,
76 int32_t* const resolution_width,
77 int32_t* const resolution_height) {
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +090078 *resolution_width = 0;
79 *resolution_height = 0;
80
81 struct v4l2_frmsizeenum frame_size;
82 memset(&frame_size, 0, sizeof(frame_size));
83 frame_size.pixel_format = fourcc;
84
85 for (; do_ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &frame_size) == 0;
86 ++frame_size.index) {
87 if (frame_size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
88 if (frame_size.discrete.width >= *resolution_width &&
89 frame_size.discrete.height >= *resolution_height) {
90 *resolution_width = frame_size.discrete.width;
91 *resolution_height = frame_size.discrete.height;
92 }
93 } else if (frame_size.type == V4L2_FRMSIZE_TYPE_STEPWISE ||
94 frame_size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
95 *resolution_width = frame_size.stepwise.max_width;
96 *resolution_height = frame_size.stepwise.max_height;
97 break;
98 }
99 }
100 return *resolution_width > 0 && *resolution_height > 0;
101}