blob: 84873c804c950b86f1cbe165e91022f492eb7bb1 [file] [log] [blame]
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +09001// Copyright 2017 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.
4//
5// resolution detectors
6
Hirokazu Honda491b1682019-12-06 14:03:36 +09007#if defined(USE_V4L2_CODEC)
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +09008#include <linux/videodev2.h>
Tom Hughes0e816952020-08-24 18:04:24 -07009#endif // defined(USE_V4L2_CODEC)
Hirokazu Honda491b1682019-12-06 14:03:36 +090010
11#if defined(USE_VAAPI)
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +090012#include <va/va.h>
Tom Hughes0e816952020-08-24 18:04:24 -070013#endif // defined(USE_VAAPI)
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +090014
15#include "label_detect.h"
16
Hirokazu Honda491b1682019-12-06 14:03:36 +090017#if defined(USE_VAAPI) || defined(USE_V4L2_CODEC)
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +090018static const int32_t width_4k = 3840;
19static const int32_t height_4k = 2160;
Hirokazu Honda491b1682019-12-06 14:03:36 +090020#endif
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +090021
Hirokazu Honda491b1682019-12-06 14:03:36 +090022#if defined(USE_VAAPI)
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +090023#if VA_CHECK_VERSION(0, 35, 0)
Hirokazu Honda491b1682019-12-06 14:03:36 +090024
25static const char kDRMDevicePattern[] = "/dev/dri/renderD*";
26
Pin-chih Lin5195fe12019-03-06 21:20:15 +080027static const VAProfile va_profiles_h264[] = {
Tom Hughes0e816952020-08-24 18:04:24 -070028 VAProfileH264Baseline, VAProfileH264Main, VAProfileH264High,
29 VAProfileH264ConstrainedBaseline, VAProfileNone};
Pin-chih Lin5195fe12019-03-06 21:20:15 +080030
Tom Hughes0e816952020-08-24 18:04:24 -070031static const VAProfile va_profiles_vp8[] = {VAProfileVP8Version0_3,
32 VAProfileNone};
Pin-chih Lin5195fe12019-03-06 21:20:15 +080033
Tom Hughes0e816952020-08-24 18:04:24 -070034static const VAProfile va_profiles_vp9[] = {VAProfileVP9Profile0,
35 VAProfileNone};
Pin-chih Lin5195fe12019-03-06 21:20:15 +080036
Steve Cho79784862021-01-22 13:59:00 -080037static const VAProfile va_profiles_av1[] = {VAProfileAV1Profile0,
38 VAProfileNone};
39
Jeffrey Kardatzkeeab7cc02021-02-24 12:53:14 -080040static const VAProfile va_profiles_hevc[] = {VAProfileHEVCMain, VAProfileNone};
41
42static const VAProfile va_profiles_hevc_10bpp[] = {VAProfileHEVCMain10,
43 VAProfileNone};
44
Pin-chih Lin5195fe12019-03-06 21:20:15 +080045/* Determines if a VAAPI device associated with given |fd| supports
46 * |va_profiles| for |va_entrypoint|, and its maximum resolution is larger
47 * than 3840x2160.
48 */
Tom Hughes0e816952020-08-24 18:04:24 -070049static bool is_vaapi_4k_device(int fd,
50 const VAProfile* va_profiles,
Steve Cho79784862021-01-22 13:59:00 -080051 VAEntrypoint va_entrypoint,
52 bool is_10bpp) {
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +090053 int32_t resolution_width = 0;
54 int32_t resolution_height = 0;
Steve Cho79784862021-01-22 13:59:00 -080055 const unsigned int va_format =
56 is_10bpp ? VA_RT_FORMAT_YUV420_10 : VA_RT_FORMAT_YUV420;
57
58 if (is_vaapi_support_formats(fd, va_profiles, va_entrypoint, va_format)) {
59 if (get_vaapi_max_resolution(fd, va_profiles, va_entrypoint, va_format,
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +090060 &resolution_width, &resolution_height)) {
61 return resolution_width >= width_4k && resolution_height >= height_4k;
62 }
63 }
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +090064 return false;
65}
66
Pin-chih Lin5195fe12019-03-06 21:20:15 +080067// Determines if is_vaapi_4k_device() for H264 decoding.
68static bool is_vaapi_4k_device_dec_h264(int fd) {
Steve Cho79784862021-01-22 13:59:00 -080069 return is_vaapi_4k_device(fd, va_profiles_h264, VAEntrypointVLD, false);
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +090070}
71
Pin-chih Lin5195fe12019-03-06 21:20:15 +080072// Determines if is_vaapi_4k_device() for H264 encoding.
73static bool is_vaapi_4k_device_enc_h264(int fd) {
Steve Cho79784862021-01-22 13:59:00 -080074 return (
75 is_vaapi_4k_device(fd, va_profiles_h264, VAEntrypointEncSlice, false) ||
76 is_vaapi_4k_device(fd, va_profiles_h264, VAEntrypointEncSliceLP, false));
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +090077}
78
Pin-chih Lin5195fe12019-03-06 21:20:15 +080079// Determines if is_vaapi_4k_device() for VP8 decoding.
80static bool is_vaapi_4k_device_dec_vp8(int fd) {
Steve Cho79784862021-01-22 13:59:00 -080081 return is_vaapi_4k_device(fd, va_profiles_vp8, VAEntrypointVLD, false);
Pin-chih Lin5195fe12019-03-06 21:20:15 +080082}
83
84// Determines if is_vaapi_4k_device() for VP8 encoding.
85static bool is_vaapi_4k_device_enc_vp8(int fd) {
Steve Cho79784862021-01-22 13:59:00 -080086 return (
87 is_vaapi_4k_device(fd, va_profiles_vp8, VAEntrypointEncSlice, false) ||
88 is_vaapi_4k_device(fd, va_profiles_vp8, VAEntrypointEncSliceLP, false));
Pin-chih Lin5195fe12019-03-06 21:20:15 +080089}
90
91// Determines if is_vaapi_4k_device() for VP9 decoding.
92static bool is_vaapi_4k_device_dec_vp9(int fd) {
Steve Cho79784862021-01-22 13:59:00 -080093 return is_vaapi_4k_device(fd, va_profiles_vp9, VAEntrypointVLD, false);
Pin-chih Lin5195fe12019-03-06 21:20:15 +080094}
95
96// Determines if is_vaapi_4k_device() for VP9 encoding.
97static bool is_vaapi_4k_device_enc_vp9(int fd) {
Steve Cho79784862021-01-22 13:59:00 -080098 return (
99 is_vaapi_4k_device(fd, va_profiles_vp9, VAEntrypointEncSlice, false) ||
100 is_vaapi_4k_device(fd, va_profiles_vp9, VAEntrypointEncSliceLP, false));
101}
102
103// Determines if is_vaapi_4k_device() for AV1 decoding.
104static bool is_vaapi_4k_device_dec_av1(int fd) {
105 return is_vaapi_4k_device(fd, va_profiles_av1, VAEntrypointVLD, false);
106}
107
108// Determines if is_vaapi_4k_device() for AV1 decoding 10BPP.
109static bool is_vaapi_4k_device_dec_av1_10bpp(int fd) {
110 return is_vaapi_4k_device(fd, va_profiles_av1, VAEntrypointVLD, true);
Pin-chih Lin5195fe12019-03-06 21:20:15 +0800111}
Jeffrey Kardatzkeeab7cc02021-02-24 12:53:14 -0800112
113// Determines if is_vaapi_4k_device() for HEVC decoding.
114static bool is_vaapi_4k_device_dec_hevc(int fd) {
115 return is_vaapi_4k_device(fd, va_profiles_hevc, VAEntrypointVLD, false);
116}
117
118// Determines if is_vaapi_4k_device() for HEVC decoding 10BPP.
119static bool is_vaapi_4k_device_dec_hevc_10bpp(int fd) {
120 return is_vaapi_4k_device(fd, va_profiles_hevc_10bpp, VAEntrypointVLD, true);
121}
Tom Hughes0e816952020-08-24 18:04:24 -0700122#endif // VA_CHECK_VERSION(0, 38, 1)
123#endif // defined(USE_VAAPI)
Hirokazu Honda491b1682019-12-06 14:03:36 +0900124
125#if defined(USE_V4L2_CODEC)
126
127static const char kVideoDevicePattern[] = "/dev/video*";
Pin-chih Lin5195fe12019-03-06 21:20:15 +0800128
129/* Determined if a V4L2 device associated with given |fd| supports |pix_fmt|
130 * for |buf_type|, and its maximum resolution is larger than 3840x2160.
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +0900131 */
Tom Hughes0e816952020-08-24 18:04:24 -0700132static bool is_v4l2_4k_device(int fd,
133 enum v4l2_buf_type buf_type,
Pin-chih Lin5195fe12019-03-06 21:20:15 +0800134 uint32_t pix_fmt) {
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +0900135 int32_t resolution_width;
136 int32_t resolution_height;
137 if (!is_hw_video_acc_device(fd)) {
138 return false;
139 }
Pin-chih Lin5195fe12019-03-06 21:20:15 +0800140 if (is_v4l2_support_format(fd, buf_type, pix_fmt)) {
141 if (get_v4l2_max_resolution(fd, pix_fmt, &resolution_width,
142 &resolution_height)) {
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +0900143 return resolution_width >= width_4k && resolution_height >= height_4k;
144 }
145 }
146 return false;
147}
148
Pin-chih Lin5195fe12019-03-06 21:20:15 +0800149// Determines if is_v4l2_4k_device() for H264 decoding.
150static bool is_v4l2_4k_device_dec_h264(int fd) {
151 return is_v4l2_4k_device(fd, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
152 V4L2_PIX_FMT_H264) ||
153 is_v4l2_4k_device(fd, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
154 V4L2_PIX_FMT_H264_SLICE);
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +0900155}
156
Pin-chih Lin5195fe12019-03-06 21:20:15 +0800157// Determines if is_v4l2_4k_device() for H264 encoding.
158static bool is_v4l2_4k_device_enc_h264(int fd) {
159 return is_v4l2_4k_device(fd, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
160 V4L2_PIX_FMT_H264);
161}
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +0900162
Pin-chih Lin5195fe12019-03-06 21:20:15 +0800163// Determines if is_v4l2_4k_device() for VP8 decoding.
164static bool is_v4l2_4k_device_dec_vp8(int fd) {
165 return is_v4l2_4k_device(fd, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
166 V4L2_PIX_FMT_VP8) ||
167 is_v4l2_4k_device(fd, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
168 V4L2_PIX_FMT_VP8_FRAME);
169}
170
171// Determines if is_v4l2_4k_device() for VP8 encoding.
172static bool is_v4l2_4k_device_enc_vp8(int fd) {
173 return is_v4l2_4k_device(fd, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
174 V4L2_PIX_FMT_VP8);
175}
176
177// Determines if is_v4l2_4k_device() for VP9 decoding.
178static bool is_v4l2_4k_device_dec_vp9(int fd) {
179 return is_v4l2_4k_device(fd, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
180 V4L2_PIX_FMT_VP9) ||
181 is_v4l2_4k_device(fd, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
182 V4L2_PIX_FMT_VP9_FRAME);
183}
184
185// Determines if is_v4l2_4k_device() for VP9 encoding.
186static bool is_v4l2_4k_device_enc_vp9(int fd) {
187 return is_v4l2_4k_device(fd, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
188 V4L2_PIX_FMT_VP9);
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +0900189}
Tom Hughes0e816952020-08-24 18:04:24 -0700190#endif // defined(USE_V4L2_CODEC)
Hirokazu Honda491b1682019-12-06 14:03:36 +0900191
Hirokazu Hondab0328c32017-10-02 13:18:37 +0900192/* Determines "4k_video_h264". Return true, if either the VAAPI device
193 * supports 4k resolution H264 decoding, has decoding entry point,
194 * and input YUV420 formats. Or there is a
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +0900195 * /dev/video* device supporting 4k resolution H264 decoding.
196 */
Hirokazu Hondab0328c32017-10-02 13:18:37 +0900197bool detect_4k_device_h264(void) {
Hirokazu Honda491b1682019-12-06 14:03:36 +0900198#if defined(USE_VAAPI)
199 if (is_any_device(kDRMDevicePattern, is_vaapi_4k_device_dec_h264))
200 return true;
Tom Hughes0e816952020-08-24 18:04:24 -0700201#endif // defined(USE_VAAPI)
Hirokazu Honda491b1682019-12-06 14:03:36 +0900202
203#if defined(USE_V4L2_CODEC)
204 if (is_any_device(kVideoDevicePattern, is_v4l2_4k_device_dec_h264))
205 return true;
Tom Hughes0e816952020-08-24 18:04:24 -0700206#endif // defined(USE_V4L2_CODEC)
Hirokazu Honda491b1682019-12-06 14:03:36 +0900207
208 return false;
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +0900209}
210
Hirokazu Hondab0328c32017-10-02 13:18:37 +0900211/* Determines "4k_video_vp8". Return true, if either the VAAPI device
212 * supports 4k resolution VP8 decoding, has decoding entry point,
213 * and input YUV420 formats. Or there is a
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +0900214 * /dev/video* device supporting 4k resolution VP8 decoding.
215 */
Hirokazu Hondab0328c32017-10-02 13:18:37 +0900216bool detect_4k_device_vp8(void) {
Hirokazu Honda491b1682019-12-06 14:03:36 +0900217#if defined(USE_VAAPI)
218 if (is_any_device(kDRMDevicePattern, is_vaapi_4k_device_dec_vp8))
219 return true;
Tom Hughes0e816952020-08-24 18:04:24 -0700220#endif // defined(USE_VAAPI)
Hirokazu Honda491b1682019-12-06 14:03:36 +0900221
222#if defined(USE_V4L2_CODEC)
223 if (is_any_device(kVideoDevicePattern, is_v4l2_4k_device_dec_vp8))
224 return true;
Tom Hughes0e816952020-08-24 18:04:24 -0700225#endif // defined(USE_V4L2_CODEC)
Hirokazu Honda491b1682019-12-06 14:03:36 +0900226
227 return false;
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +0900228}
229
Hirokazu Hondab0328c32017-10-02 13:18:37 +0900230/* Determines "4k_video_vp9". Return true, if either the VAAPI device
231 * supports 4k resolution VP9 decoding, has decoding entry point,
232 * and input YUV420 formats. Or there is a
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +0900233 * /dev/video* device supporting 4k resolution VP9 decoding.
234 */
Hirokazu Hondab0328c32017-10-02 13:18:37 +0900235bool detect_4k_device_vp9(void) {
Hirokazu Honda491b1682019-12-06 14:03:36 +0900236#if defined(USE_VAAPI)
237 if (is_any_device(kDRMDevicePattern, is_vaapi_4k_device_dec_vp9))
238 return true;
Tom Hughes0e816952020-08-24 18:04:24 -0700239#endif // defined(USE_VAAPI)
Hirokazu Honda491b1682019-12-06 14:03:36 +0900240
241#if defined(USE_V4L2_CODEC)
242 if (is_any_device(kVideoDevicePattern, is_v4l2_4k_device_dec_vp9))
243 return true;
Tom Hughes0e816952020-08-24 18:04:24 -0700244#endif // defined(USE_V4L2_CODEC)
Hirokazu Honda491b1682019-12-06 14:03:36 +0900245
246 return false;
Pin-chih Lin5195fe12019-03-06 21:20:15 +0800247}
248
Steve Cho79784862021-01-22 13:59:00 -0800249/* Determines "4k_video_av1". Return true, if either the VAAPI device
250 * supports 4k resolution AV1 decoding, has decoding entry point,
251 * and input YUV420 formats.
252 */
253bool detect_4k_device_av1(void) {
254#if defined(USE_VAAPI)
255 if (is_any_device(kDRMDevicePattern, is_vaapi_4k_device_dec_av1))
256 return true;
257#endif // defined(USE_VAAPI)
258
259 return false;
260}
261
262/* Determines "4k_video_av1_10bpp". Return true, if either the VAAPI device
263 * supports 4k resolution AV1 10BPP decoding, has decoding entry point,
264 * and input YUV420 formats.
265 */
266bool detect_4k_device_av1_10bpp(void) {
267#if defined(USE_VAAPI)
268 if (is_any_device(kDRMDevicePattern, is_vaapi_4k_device_dec_av1_10bpp))
269 return true;
270#endif // defined(USE_VAAPI)
271
272 return false;
273}
274
Jeffrey Kardatzkeeab7cc02021-02-24 12:53:14 -0800275/* Determines "4k_video_hevc". Return true, if the VAAPI device supports 4k
276 * resolution HEVC main decoding, has decoding entry point, and outputs YUV420
277 * format.
278 */
279bool detect_4k_device_hevc(void) {
280#if defined(USE_VAAPI)
281 if (is_any_device(kDRMDevicePattern, is_vaapi_4k_device_dec_hevc))
282 return true;
283#endif // defined(USE_VAAPI)
284
285 return false;
286}
287
288/* Determines "4k_video_hevc_10bpp". Return true, if the VAAPI device supports
289 * 4k resolution HEVC main10 10BPP decoding, has decoding entry point, and
290 * outputs YUV420 format.
291 */
292bool detect_4k_device_hevc_10bpp(void) {
293#if defined(USE_VAAPI)
294 if (is_any_device(kDRMDevicePattern, is_vaapi_4k_device_dec_hevc_10bpp))
295 return true;
296#endif // defined(USE_VAAPI)
297
298 return false;
299}
300
Pin-chih Lin5195fe12019-03-06 21:20:15 +0800301/* Determines "4k_video_enc_h264". Return true, if either the VAAPI device
302 * supports 4k resolution H264 encoding, has encoding entry point,
303 * and input YUV420 formats. Or there is a
304 * /dev/video* device supporting 4k resolution H264 encoding.
305 */
306bool detect_4k_device_enc_h264(void) {
Hirokazu Honda491b1682019-12-06 14:03:36 +0900307#if defined(USE_VAAPI)
308 if (is_any_device(kDRMDevicePattern, is_vaapi_4k_device_enc_h264))
309 return true;
Tom Hughes0e816952020-08-24 18:04:24 -0700310#endif // defined(USE_VAAPI)
Hirokazu Honda491b1682019-12-06 14:03:36 +0900311
312#if defined(USE_V4L2_CODEC)
313 if (is_any_device(kVideoDevicePattern, is_v4l2_4k_device_enc_h264))
314 return true;
Tom Hughes0e816952020-08-24 18:04:24 -0700315#endif // defined(USE_V4L2_CODEC)
Hirokazu Honda491b1682019-12-06 14:03:36 +0900316
317 return false;
Pin-chih Lin5195fe12019-03-06 21:20:15 +0800318}
319
320/* Determines "4k_video_enc_vp8". Return true, if either the VAAPI device
321 * supports 4k resolution VP8 encoding, has encoding entry point,
322 * and input YUV420 formats. Or there is a
323 * /dev/video* device supporting 4k resolution VP8 encoding.
324 */
325bool detect_4k_device_enc_vp8(void) {
Hirokazu Honda491b1682019-12-06 14:03:36 +0900326#if defined(USE_VAAPI)
327 if (is_any_device(kDRMDevicePattern, is_vaapi_4k_device_enc_vp8))
328 return true;
Tom Hughes0e816952020-08-24 18:04:24 -0700329#endif // defined(USE_VAAPI)
Hirokazu Honda491b1682019-12-06 14:03:36 +0900330
331#if defined(USE_V4L2_CODEC)
332 if (is_any_device(kVideoDevicePattern, is_v4l2_4k_device_enc_vp8))
333 return true;
Tom Hughes0e816952020-08-24 18:04:24 -0700334#endif // defined(USE_V4L2_CODEC)
Hirokazu Honda491b1682019-12-06 14:03:36 +0900335
336 return false;
Pin-chih Lin5195fe12019-03-06 21:20:15 +0800337}
338
339/* Determines "4k_video_enc_vp9". Return true, if either the VAAPI device
340 * supports 4k resolution VP9 encoding, has encoding entry point,
341 * and input YUV420 formats. Or there is a
342 * /dev/video* device supporting 4k resolution VP9 encoding.
343 */
344bool detect_4k_device_enc_vp9(void) {
Hirokazu Honda491b1682019-12-06 14:03:36 +0900345#if defined(USE_VAAPI)
346 if (is_any_device(kDRMDevicePattern, is_vaapi_4k_device_enc_vp9))
347 return true;
Tom Hughes0e816952020-08-24 18:04:24 -0700348#endif // defined(USE_VAAPI)
Hirokazu Honda491b1682019-12-06 14:03:36 +0900349
350#if defined(USE_V4L2_CODEC)
351 if (is_any_device(kVideoDevicePattern, is_v4l2_4k_device_enc_vp9))
352 return true;
Tom Hughes0e816952020-08-24 18:04:24 -0700353#endif // defined(USE_V4L2_CODEC)
Hirokazu Honda491b1682019-12-06 14:03:36 +0900354
355 return false;
Hirokazu Hondad92e9ac2017-09-20 11:33:59 +0900356}