1From 093ba11869de97c40942e6fc0366a514e8102ee2 Mon Sep 17 00:00:00 2001
2From: Maksim Sisov <msisov@igalia.com>
3Date: Wed, 31 Jul 2019 09:56:24 +0300
4Subject: [PATCH 01/14] Add support for V4L2VDA on Linux
5
6This patch enables hardware assisted video decoding via the
7Chromium V4L2VDA. Including changes when Linux is used. In
8order to use this, use_linux_v4l2_only flag should be set
9to true.
10
11Signed-off-by: Ryo Kodama <ryo.kodama.vz@renesas.com>
12
13fixup! avoid building not declared formats
14
15"FRAME", "_SLICE",  "V4L2_PIX_FMT_VP9" are not defined in mainline
16 Linux headers. This patch avoids building these formats.
17
18Signed-off-by: Ryo Kodama <ryo.kodama.vz@renesas.com>
19
20fixup! add V4L2_PIX_FMT_VP9 support back again as it is now
21included in mainline Linux kernel. This allows VP9 codec
22to work with upstream kernel and v4l2 vda. Tested on db820c
23with Venus v4l2 driver.
24
25Signed-off-by: Peter Griffin <peter.griffin@linaro.org>
26Signed-off-by: Stanimir Varbanov <stanimir.varbanov@linaro.org>
27Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
28---
29 .../gpu_mjpeg_decode_accelerator_factory.cc   |  3 +-
30 media/gpu/BUILD.gn                            |  1 +
31 media/gpu/args.gni                            |  4 ++
32 .../gpu_video_decode_accelerator_factory.cc   |  8 +++
33 .../gpu_video_decode_accelerator_factory.h    |  2 +
34 media/gpu/v4l2/BUILD.gn                       | 48 ++++++++------
35 media/gpu/v4l2/generic_v4l2_device.cc         |  4 ++
36 media/gpu/v4l2/v4l2_device.cc                 | 66 +++++++++++++++++++
37 media/gpu/v4l2/v4l2_video_decoder.cc          |  7 ++
38 9 files changed, 121 insertions(+), 22 deletions(-)
39
40diff --git a/components/chromeos_camera/gpu_mjpeg_decode_accelerator_factory.cc b/components/chromeos_camera/gpu_mjpeg_decode_accelerator_factory.cc
41index 3772b8ef..dece6b77 100644
42--- a/components/chromeos_camera/gpu_mjpeg_decode_accelerator_factory.cc
43+++ b/components/chromeos_camera/gpu_mjpeg_decode_accelerator_factory.cc
44@@ -13,7 +13,8 @@
45 #include "media/base/media_switches.h"
46 #include "media/gpu/buildflags.h"
47
48-#if BUILDFLAG(USE_V4L2_CODEC) && defined(ARCH_CPU_ARM_FAMILY)
49+#if BUILDFLAG(USE_V4L2_CODEC) && defined(ARCH_CPU_ARM_FAMILY) && \
50+    !BUILDFLAG(USE_LINUX_V4L2)
51 #define USE_V4L2_MJPEG_DECODE_ACCELERATOR
52 #endif
53
54diff --git a/media/gpu/BUILD.gn b/media/gpu/BUILD.gn
55index 98bf1fff..fed26fd4 100644
56--- a/media/gpu/BUILD.gn
57+++ b/media/gpu/BUILD.gn
58@@ -20,6 +20,7 @@ buildflag_header("buildflags") {
59     "USE_VAAPI_IMAGE_CODECS=$use_vaapi_image_codecs",
60     "USE_V4L2_CODEC=$use_v4l2_codec",
61     "USE_LIBV4L2=$use_v4lplugin",
62+    "USE_LINUX_V4L2=$use_linux_v4l2_only",
63     "USE_VAAPI_X11=$use_vaapi_x11",
64   ]
65 }
66diff --git a/media/gpu/args.gni b/media/gpu/args.gni
67index 2f538439..bc47d2ad 100644
68--- a/media/gpu/args.gni
69+++ b/media/gpu/args.gni
70@@ -21,6 +21,10 @@ declare_args() {
71   # platforms which have v4l2 hardware encoder / decoder.
72   use_v4l2_codec = false
73
74+  # Indicates that only definitions available in the mainline linux kernel
75+  # will be used.
76+  use_linux_v4l2_only = false
77+
78   # Indicates if Video4Linux2 AML encoder is used. This is used for AML
79   # platforms which have v4l2 hardware encoder
80   use_v4l2_codec_aml = false
81diff --git a/media/gpu/gpu_video_decode_accelerator_factory.cc b/media/gpu/gpu_video_decode_accelerator_factory.cc
82index 0e55ef47..2096dc61 100644
83--- a/media/gpu/gpu_video_decode_accelerator_factory.cc
84+++ b/media/gpu/gpu_video_decode_accelerator_factory.cc
85@@ -29,7 +29,9 @@
86 #include "ui/gl/gl_implementation.h"
87 #elif BUILDFLAG(USE_V4L2_CODEC)
88 #include "media/gpu/v4l2/v4l2_device.h"
89+#if !BUILDFLAG(USE_LINUX_V4L2)
90 #include "media/gpu/v4l2/v4l2_slice_video_decode_accelerator.h"
91+#endif
92 #include "media/gpu/v4l2/v4l2_video_decode_accelerator.h"
93 #include "ui/gl/gl_surface_egl.h"
94 #endif
95@@ -64,10 +66,12 @@ gpu::VideoDecodeAcceleratorCapabilities GetDecoderCapabilitiesInternal(
96   GpuVideoAcceleratorUtil::InsertUniqueDecodeProfiles(
97       V4L2VideoDecodeAccelerator::GetSupportedProfiles(),
98       &capabilities.supported_profiles);
99+#if !BUILDFLAG(USE_LINUX_V4L2)
100   GpuVideoAcceleratorUtil::InsertUniqueDecodeProfiles(
101       V4L2SliceVideoDecodeAccelerator::GetSupportedProfiles(),
102       &capabilities.supported_profiles);
103 #endif
104+#endif
105 #elif defined(OS_MAC)
106   capabilities.supported_profiles =
107       VTVideoDecodeAccelerator::GetSupportedProfiles(workarounds);
108@@ -146,8 +150,10 @@ GpuVideoDecodeAcceleratorFactory::CreateVDA(
109     &GpuVideoDecodeAcceleratorFactory::CreateVaapiVDA,
110 #elif BUILDFLAG(USE_V4L2_CODEC)
111     &GpuVideoDecodeAcceleratorFactory::CreateV4L2VDA,
112+#if !BUILDFLAG(USE_LINUX_V4L2)
113     &GpuVideoDecodeAcceleratorFactory::CreateV4L2SliceVDA,
114 #endif
115+#endif
116
117 #if defined(OS_MAC)
118     &GpuVideoDecodeAcceleratorFactory::CreateVTVDA,
119@@ -207,6 +213,7 @@ GpuVideoDecodeAcceleratorFactory::CreateV4L2VDA(
120   return decoder;
121 }
122
123+#if !BUILDFLAG(USE_LINUX_V4L2)
124 std::unique_ptr<VideoDecodeAccelerator>
125 GpuVideoDecodeAcceleratorFactory::CreateV4L2SliceVDA(
126     const gpu::GpuDriverBugWorkarounds& /*workarounds*/,
127@@ -222,6 +229,7 @@ GpuVideoDecodeAcceleratorFactory::CreateV4L2SliceVDA(
128   return decoder;
129 }
130 #endif
131+#endif
132
133 #if defined(OS_MAC)
134 std::unique_ptr<VideoDecodeAccelerator>
135diff --git a/media/gpu/gpu_video_decode_accelerator_factory.h b/media/gpu/gpu_video_decode_accelerator_factory.h
136index a80cac21..1021af34 100644
137--- a/media/gpu/gpu_video_decode_accelerator_factory.h
138+++ b/media/gpu/gpu_video_decode_accelerator_factory.h
139@@ -104,11 +104,13 @@ class MEDIA_GPU_EXPORT GpuVideoDecodeAcceleratorFactory {
140       const gpu::GpuDriverBugWorkarounds& workarounds,
141       const gpu::GpuPreferences& gpu_preferences,
142       MediaLog* media_log) const;
143+#if !BUILDFLAG(USE_LINUX_V4L2)
144   std::unique_ptr<VideoDecodeAccelerator> CreateV4L2SliceVDA(
145       const gpu::GpuDriverBugWorkarounds& workarounds,
146       const gpu::GpuPreferences& gpu_preferences,
147       MediaLog* media_log) const;
148 #endif
149+#endif
150 #if defined(OS_MAC)
151   std::unique_ptr<VideoDecodeAccelerator> CreateVTVDA(
152       const gpu::GpuDriverBugWorkarounds& workarounds,
153diff --git a/media/gpu/v4l2/BUILD.gn b/media/gpu/v4l2/BUILD.gn
154index 0322bbf7..3e637653 100644
155--- a/media/gpu/v4l2/BUILD.gn
156+++ b/media/gpu/v4l2/BUILD.gn
157@@ -28,9 +28,6 @@ source_set("v4l2") {
158     "buffer_affinity_tracker.h",
159     "generic_v4l2_device.cc",
160     "generic_v4l2_device.h",
161-    "v4l2_decode_surface.cc",
162-    "v4l2_decode_surface.h",
163-    "v4l2_decode_surface_handler.h",
164     "v4l2_device.cc",
165     "v4l2_device.h",
166     "v4l2_device_poller.cc",
167@@ -39,8 +36,6 @@ source_set("v4l2") {
168     "v4l2_framerate_control.h",
169     "v4l2_image_processor_backend.cc",
170     "v4l2_image_processor_backend.h",
171-    "v4l2_slice_video_decode_accelerator.cc",
172-    "v4l2_slice_video_decode_accelerator.h",
173     "v4l2_stateful_workaround.cc",
174     "v4l2_stateful_workaround.h",
175     "v4l2_status.h",
176@@ -50,30 +45,41 @@ source_set("v4l2") {
177     "v4l2_vda_helpers.h",
178     "v4l2_video_decode_accelerator.cc",
179     "v4l2_video_decode_accelerator.h",
180-    "v4l2_video_decoder.cc",
181-    "v4l2_video_decoder.h",
182     "v4l2_video_decoder_backend.cc",
183     "v4l2_video_decoder_backend.h",
184     "v4l2_video_decoder_backend_stateful.cc",
185     "v4l2_video_decoder_backend_stateful.h",
186-    "v4l2_video_decoder_backend_stateless.cc",
187-    "v4l2_video_decoder_backend_stateless.h",
188-    "v4l2_video_decoder_delegate_h264.cc",
189-    "v4l2_video_decoder_delegate_h264.h",
190-    "v4l2_video_decoder_delegate_h264_legacy.cc",
191-    "v4l2_video_decoder_delegate_h264_legacy.h",
192-    "v4l2_video_decoder_delegate_vp8.cc",
193-    "v4l2_video_decoder_delegate_vp8.h",
194-    "v4l2_video_decoder_delegate_vp8_legacy.cc",
195-    "v4l2_video_decoder_delegate_vp8_legacy.h",
196-    "v4l2_video_decoder_delegate_vp9_chromium.cc",
197-    "v4l2_video_decoder_delegate_vp9_chromium.h",
198-    "v4l2_video_decoder_delegate_vp9_legacy.cc",
199-    "v4l2_video_decoder_delegate_vp9_legacy.h",
200     "v4l2_video_encode_accelerator.cc",
201     "v4l2_video_encode_accelerator.h",
202   ]
203
204+  if (!use_linux_v4l2_only) {
205+    sources += [
206+      "v4l2_decode_surface.cc",
207+      "v4l2_decode_surface.h",
208+      "v4l2_decode_surface_handler.h",
209+      "v4l2_slice_video_decode_accelerator.cc",
210+      "v4l2_slice_video_decode_accelerator.h",
211+      "v4l2_video_decoder.cc",
212+      "v4l2_video_decoder.h",
213+      "v4l2_video_decoder_backend_stateless.cc",
214+      "v4l2_video_decoder_backend_stateless.h",
215+      "v4l2_video_decoder_delegate_h264.cc",
216+      "v4l2_video_decoder_delegate_h264.h",
217+      "v4l2_video_decoder_delegate_h264_legacy.cc",
218+      "v4l2_video_decoder_delegate_h264_legacy.h",
219+      "v4l2_video_decoder_delegate_vp8.cc",
220+      "v4l2_video_decoder_delegate_vp8.h",
221+      "v4l2_video_decoder_delegate_vp8_legacy.cc",
222+      "v4l2_video_decoder_delegate_vp8_legacy.h",
223+      "v4l2_video_decoder_delegate_vp9_chromium.cc",
224+      "v4l2_video_decoder_delegate_vp9_chromium.h",
225+      "v4l2_video_decoder_delegate_vp9_legacy.cc",
226+      "v4l2_video_decoder_delegate_vp9_legacy.h",
227+    ]
228+  }
229+
230+
231   libs = [
232     "EGL",
233     "GLESv2",
234diff --git a/media/gpu/v4l2/generic_v4l2_device.cc b/media/gpu/v4l2/generic_v4l2_device.cc
235index b64c9bd0..92ec1b60 100644
236--- a/media/gpu/v4l2/generic_v4l2_device.cc
237+++ b/media/gpu/v4l2/generic_v4l2_device.cc
238@@ -441,7 +441,11 @@ bool GenericV4L2Device::OpenDevicePath(const std::string& path, Type type) {
239     return false;
240
241 #if BUILDFLAG(USE_LIBV4L2)
242+#if BUILDFLAG(USE_LINUX_V4L2)
243+  if (
244+#else
245   if (type == Type::kEncoder &&
246+#endif
247       HANDLE_EINTR(v4l2_fd_open(device_fd_.get(), V4L2_DISABLE_CONVERSION)) !=
248           -1) {
249     DVLOGF(3) << "Using libv4l2 for " << path;
250diff --git a/media/gpu/v4l2/v4l2_device.cc b/media/gpu/v4l2/v4l2_device.cc
251index ee99d095..98a0d51f 100644
252--- a/media/gpu/v4l2/v4l2_device.cc
253+++ b/media/gpu/v4l2/v4l2_device.cc
254@@ -853,7 +853,9 @@ void V4L2WritableBufferRef::SetConfigStore(uint32_t config_store) {
255   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
256   DCHECK(buffer_data_);
257
258+#if !BUILDFLAG(USE_LINUX_V4L2)
259   buffer_data_->v4l2_buffer_.config_store = config_store;
260+#endif
261 }
262
263 V4L2ReadableBuffer::V4L2ReadableBuffer(const struct v4l2_buffer& v4l2_buffer,
264@@ -996,10 +998,12 @@ V4L2Queue::V4L2Queue(scoped_refptr<V4L2Device> dev,
265     return;
266   }
267
268+#if !BUILDFLAG(USE_LINUX_V4L2)
269   if (reqbufs.capabilities & V4L2_BUF_CAP_SUPPORTS_REQUESTS) {
270     supports_requests_ = true;
271     DVLOGF(4) << "Queue supports request API.";
272   }
273+#endif
274 }
275
276 V4L2Queue::~V4L2Queue() {
277@@ -1527,6 +1531,23 @@ scoped_refptr<V4L2Device> V4L2Device::Create() {
278 // static
279 uint32_t V4L2Device::VideoCodecProfileToV4L2PixFmt(VideoCodecProfile profile,
280                                                    bool slice_based) {
281+#if BUILDFLAG(USE_LINUX_V4L2)
282+  if (slice_based) {
283+    LOG(ERROR) << "Slice not supported";
284+    return 0;
285+  }
286+
287+  if (profile >= H264PROFILE_MIN && profile <= H264PROFILE_MAX) {
288+    return V4L2_PIX_FMT_H264;
289+  } else if (profile >= VP8PROFILE_MIN && profile <= VP8PROFILE_MAX) {
290+    return V4L2_PIX_FMT_VP8;
291+  } else if (profile >= VP9PROFILE_MIN && profile <= VP9PROFILE_MAX) {
292+    return V4L2_PIX_FMT_VP9;
293+  } else {
294+    DVLOGF(1) << "Unsupported profile: " << GetProfileName(profile);
295+    return 0;
296+  }
297+#else
298   if (profile >= H264PROFILE_MIN && profile <= H264PROFILE_MAX) {
299     if (slice_based)
300       return V4L2_PIX_FMT_H264_SLICE;
301@@ -1546,8 +1567,10 @@ uint32_t V4L2Device::VideoCodecProfileToV4L2PixFmt(VideoCodecProfile profile,
302     DVLOGF(1) << "Unsupported profile: " << GetProfileName(profile);
303     return 0;
304   }
305+#endif
306 }
307
308+#if !BUILDFLAG(USE_LINUX_V4L2)
309 namespace {
310
311 VideoCodecProfile V4L2ProfileToVideoCodecProfile(VideoCodec codec,
312@@ -1594,9 +1617,11 @@ VideoCodecProfile V4L2ProfileToVideoCodecProfile(VideoCodec codec,
313 }
314
315 }  // namespace
316+#endif
317
318 std::vector<VideoCodecProfile> V4L2Device::V4L2PixFmtToVideoCodecProfiles(
319     uint32_t pix_fmt) {
320+#if !BUILDFLAG(USE_LINUX_V4L2)
321   auto get_supported_profiles = [this](
322                                     VideoCodec codec,
323                                     std::vector<VideoCodecProfile>* profiles) {
324@@ -1667,6 +1692,27 @@ std::vector<VideoCodecProfile> V4L2Device::V4L2PixFmtToVideoCodecProfiles(
325       VLOGF(1) << "Unhandled pixelformat " << FourccToString(pix_fmt);
326       return {};
327   }
328+#else
329+  std::vector<VideoCodecProfile> profiles;
330+  switch (pix_fmt) {
331+    case V4L2_PIX_FMT_H264:
332+      profiles = {
333+          H264PROFILE_BASELINE,
334+          H264PROFILE_MAIN,
335+          H264PROFILE_HIGH,
336+      };
337+      break;
338+    case V4L2_PIX_FMT_VP8:
339+      profiles = {VP8PROFILE_ANY};
340+      break;
341+    case V4L2_PIX_FMT_VP9:
342+      profiles = {VP9PROFILE_PROFILE0};
343+      break;
344+    default:
345+      VLOGF(1) << "Unhandled pixelformat " << FourccToString(pix_fmt);
346+      return {};
347+  }
348+#endif
349
350   // Erase duplicated profiles.
351   std::sort(profiles.begin(), profiles.end());
352@@ -2334,10 +2380,14 @@ bool V4L2Request::ApplyCtrls(struct v4l2_ext_controls* ctrls) {
353     return false;
354   }
355
356+#if !BUILDFLAG(USE_LINUX_V4L2)
357   ctrls->which = V4L2_CTRL_WHICH_REQUEST_VAL;
358   ctrls->request_fd = request_fd_.get();
359
360   return true;
361+#else
362+  return false;
363+#endif
364 }
365
366 bool V4L2Request::ApplyQueueBuffer(struct v4l2_buffer* buffer) {
367@@ -2349,10 +2399,14 @@ bool V4L2Request::ApplyQueueBuffer(struct v4l2_buffer* buffer) {
368     return false;
369   }
370
371+#if !BUILDFLAG(USE_LINUX_V4L2)
372   buffer->flags |= V4L2_BUF_FLAG_REQUEST_FD;
373   buffer->request_fd = request_fd_.get();
374
375   return true;
376+#else
377+  return false;
378+#endif
379 }
380
381 bool V4L2Request::Submit() {
382@@ -2363,7 +2417,11 @@ bool V4L2Request::Submit() {
383     return false;
384   }
385
386+#if !BUILDFLAG(USE_LINUX_V4L2)
387   return HANDLE_EINTR(ioctl(request_fd_.get(), MEDIA_REQUEST_IOC_QUEUE)) == 0;
388+#else
389+  return false;
390+#endif
391 }
392
393 bool V4L2Request::IsCompleted() {
394@@ -2406,6 +2464,7 @@ bool V4L2Request::Reset() {
395     return false;
396   }
397
398+#if !BUILDFLAG(USE_LINUX_V4L2)
399   // Reinit the request to make sure we can use it for a new submission.
400   if (HANDLE_EINTR(ioctl(request_fd_.get(), MEDIA_REQUEST_IOC_REINIT)) < 0) {
401     VPLOGF(1) << "Failed to reinit request.";
402@@ -2413,6 +2472,9 @@ bool V4L2Request::Reset() {
403   }
404
405   return true;
406+#else
407+  return false;
408+#endif
409 }
410
411 V4L2RequestRefBase::V4L2RequestRefBase(V4L2RequestRefBase&& req_base) {
412@@ -2487,6 +2549,7 @@ V4L2RequestsQueue::~V4L2RequestsQueue() {
413 absl::optional<base::ScopedFD> V4L2RequestsQueue::CreateRequestFD() {
414   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
415
416+#if !BUILDFLAG(USE_LINUX_V4L2)
417   int request_fd;
418   int ret = HANDLE_EINTR(
419         ioctl(media_fd_.get(), MEDIA_IOC_REQUEST_ALLOC, &request_fd));
420@@ -2496,6 +2559,9 @@ absl::optional<base::ScopedFD> V4L2RequestsQueue::CreateRequestFD() {
421   }
422
423   return base::ScopedFD(request_fd);
424+#else
425+  return absl::nullopt;
426+#endif
427 }
428
429 absl::optional<V4L2RequestRef> V4L2RequestsQueue::GetFreeRequest() {
430diff --git a/media/gpu/v4l2/v4l2_video_decoder.cc b/media/gpu/v4l2/v4l2_video_decoder.cc
431index f3545a54..1b07bf79 100644
432--- a/media/gpu/v4l2/v4l2_video_decoder.cc
433+++ b/media/gpu/v4l2/v4l2_video_decoder.cc
434@@ -23,7 +23,10 @@
435 #include "media/gpu/macros.h"
436 #include "media/gpu/v4l2/v4l2_status.h"
437 #include "media/gpu/v4l2/v4l2_video_decoder_backend_stateful.h"
438+
439+#if !BUILDFLAG(USE_LINUX_V4L2)
440 #include "media/gpu/v4l2/v4l2_video_decoder_backend_stateless.h"
441+#endif
442
443 namespace media {
444
445@@ -39,7 +42,9 @@ constexpr size_t kNumInputBuffers = 8;
446
447 // Input format V4L2 fourccs this class supports.
448 constexpr uint32_t kSupportedInputFourccs[] = {
449+#if !BUILDFLAG(USE_LINUX_V4L2)
450     V4L2_PIX_FMT_H264_SLICE, V4L2_PIX_FMT_VP8_FRAME, V4L2_PIX_FMT_VP9_FRAME,
451+#endif
452     V4L2_PIX_FMT_H264,       V4L2_PIX_FMT_VP8,       V4L2_PIX_FMT_VP9,
453 };
454
455@@ -298,6 +303,7 @@ V4L2Status V4L2VideoDecoder::InitializeBackend() {
456              << " and fourcc: " << FourccToString(input_format_fourcc);
457     backend_ = std::make_unique<V4L2StatefulVideoDecoderBackend>(
458         this, device_, profile_, decoder_task_runner_);
459+#if !BUILDFLAG(USE_LINUX_V4L2)
460   } else {
461     DCHECK_EQ(preferred_api_and_format.first, kStateless);
462     VLOGF(1) << "Using a stateless API for profile: "
463@@ -305,6 +311,7 @@ V4L2Status V4L2VideoDecoder::InitializeBackend() {
464              << " and fourcc: " << FourccToString(input_format_fourcc);
465     backend_ = std::make_unique<V4L2StatelessVideoDecoderBackend>(
466         this, device_, profile_, decoder_task_runner_);
467+#endif
468   }
469
470   if (!backend_->Initialize()) {
471--
4722.20.1
473
474