Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 1 | // Copyright 2019 The Chromium 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 | |
Alexandre Courbot | 400c4bf9 | 2020-06-25 02:32:10 | [diff] [blame] | 5 | #include "media/gpu/v4l2/v4l2_video_decoder.h" |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 6 | |
| 7 | #include <algorithm> |
| 8 | |
| 9 | #include "base/bind.h" |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 10 | #include "base/callback_helpers.h" |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 11 | #include "base/logging.h" |
| 12 | #include "base/memory/ptr_util.h" |
| 13 | #include "base/task/post_task.h" |
Jeffrey Kardatzke | b30b88b | 2020-09-29 02:28:08 | [diff] [blame] | 14 | #include "media/base/limits.h" |
Alexandre Courbot | 139ebe2 | 2019-12-12 01:46:39 | [diff] [blame] | 15 | #include "media/base/video_types.h" |
Chih-Yu Huang | f463589 | 2019-07-11 14:20:29 | [diff] [blame] | 16 | #include "media/base/video_util.h" |
Chih-Yu Huang | 5dc635d6 | 2019-10-25 10:30:39 | [diff] [blame] | 17 | #include "media/gpu/chromeos/dmabuf_video_frame_pool.h" |
Shuo-Peng Liao | 761b0ec7 | 2019-10-09 05:00:20 | [diff] [blame] | 18 | #include "media/gpu/chromeos/fourcc.h" |
Chih-Yu Huang | a4bf834 | 2019-06-27 04:03:44 | [diff] [blame] | 19 | #include "media/gpu/gpu_video_decode_accelerator_helpers.h" |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 20 | #include "media/gpu/macros.h" |
Alexandre Courbot | 071818f | 2020-06-12 07:15:31 | [diff] [blame] | 21 | #include "media/gpu/v4l2/v4l2_video_decoder_backend_stateful.h" |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 22 | #include "media/gpu/v4l2/v4l2_video_decoder_backend_stateless.h" |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 23 | |
| 24 | namespace media { |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | // See http://crbug.com/255116. |
| 29 | constexpr int k1080pArea = 1920 * 1088; |
| 30 | // Input bitstream buffer size for up to 1080p streams. |
| 31 | constexpr size_t kInputBufferMaxSizeFor1080p = 1024 * 1024; |
| 32 | // Input bitstream buffer size for up to 4k streams. |
| 33 | constexpr size_t kInputBufferMaxSizeFor4k = 4 * kInputBufferMaxSizeFor1080p; |
| 34 | constexpr size_t kNumInputBuffers = 16; |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 35 | |
Chih-Yu Huang | a4bf834 | 2019-06-27 04:03:44 | [diff] [blame] | 36 | // Input format V4L2 fourccs this class supports. |
| 37 | constexpr uint32_t kSupportedInputFourccs[] = { |
Alexandre Courbot | 071818f | 2020-06-12 07:15:31 | [diff] [blame] | 38 | V4L2_PIX_FMT_H264_SLICE, V4L2_PIX_FMT_VP8_FRAME, V4L2_PIX_FMT_VP9_FRAME, |
| 39 | V4L2_PIX_FMT_H264, V4L2_PIX_FMT_VP8, V4L2_PIX_FMT_VP9, |
Chih-Yu Huang | a4bf834 | 2019-06-27 04:03:44 | [diff] [blame] | 40 | }; |
| 41 | |
Jeffrey Kardatzke | b30b88b | 2020-09-29 02:28:08 | [diff] [blame] | 42 | // Number of output buffers to use for each VD stage above what's required by |
| 43 | // the decoder (e.g. DPB size, in H264). We need limits::kMaxVideoFrames to |
| 44 | // fill up the GpuVideoDecode pipeline, and +1 for a frame in transit. |
| 45 | constexpr size_t kDpbOutputBufferExtraCount = limits::kMaxVideoFrames + 1; |
| 46 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 47 | } // namespace |
| 48 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 49 | // static |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 50 | std::unique_ptr<DecoderInterface> V4L2VideoDecoder::Create( |
Chih-Yu Huang | fc16380 | 2019-09-02 06:17:48 | [diff] [blame] | 51 | scoped_refptr<base::SequencedTaskRunner> decoder_task_runner, |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 52 | base::WeakPtr<DecoderInterface::Client> client) { |
Chih-Yu Huang | a35c28a | 2019-10-21 02:43:09 | [diff] [blame] | 53 | DCHECK(decoder_task_runner->RunsTasksInCurrentSequence()); |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 54 | DCHECK(client); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 55 | |
| 56 | scoped_refptr<V4L2Device> device = V4L2Device::Create(); |
| 57 | if (!device) { |
| 58 | VLOGF(1) << "Failed to create V4L2 device."; |
| 59 | return nullptr; |
| 60 | } |
| 61 | |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 62 | return base::WrapUnique<DecoderInterface>(new V4L2VideoDecoder( |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 63 | std::move(decoder_task_runner), std::move(client), std::move(device))); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 64 | } |
| 65 | |
Chih-Yu Huang | a4bf834 | 2019-06-27 04:03:44 | [diff] [blame] | 66 | // static |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 67 | SupportedVideoDecoderConfigs V4L2VideoDecoder::GetSupportedConfigs() { |
Chih-Yu Huang | a4bf834 | 2019-06-27 04:03:44 | [diff] [blame] | 68 | scoped_refptr<V4L2Device> device = V4L2Device::Create(); |
| 69 | if (!device) |
| 70 | return SupportedVideoDecoderConfigs(); |
| 71 | |
| 72 | return ConvertFromSupportedProfiles( |
| 73 | device->GetSupportedDecodeProfiles(base::size(kSupportedInputFourccs), |
| 74 | kSupportedInputFourccs), |
| 75 | false); |
| 76 | } |
| 77 | |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 78 | V4L2VideoDecoder::V4L2VideoDecoder( |
Chih-Yu Huang | fc16380 | 2019-09-02 06:17:48 | [diff] [blame] | 79 | scoped_refptr<base::SequencedTaskRunner> decoder_task_runner, |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 80 | base::WeakPtr<DecoderInterface::Client> client, |
| 81 | scoped_refptr<V4L2Device> device) |
| 82 | : DecoderInterface(std::move(decoder_task_runner), std::move(client)), |
| 83 | device_(std::move(device)), |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 84 | weak_this_factory_(this) { |
Chih-Yu Huang | a35c28a | 2019-10-21 02:43:09 | [diff] [blame] | 85 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 86 | VLOGF(2); |
Chih-Yu Huang | a35c28a | 2019-10-21 02:43:09 | [diff] [blame] | 87 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 88 | weak_this_ = weak_this_factory_.GetWeakPtr(); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 89 | } |
| 90 | |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 91 | V4L2VideoDecoder::~V4L2VideoDecoder() { |
Alexandre Courbot | dcb6ebb | 2019-06-25 04:25:36 | [diff] [blame] | 92 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 93 | DVLOGF(2); |
| 94 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 95 | // Call all pending decode callback. |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 96 | if (backend_) { |
| 97 | backend_->ClearPendingRequests(DecodeStatus::ABORTED); |
| 98 | backend_ = nullptr; |
| 99 | } |
Chih-Yu Huang | 30b5fca | 2019-09-24 09:03:34 | [diff] [blame] | 100 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 101 | // Stop and Destroy device. |
Alexandre Courbot | 9270ddc | 2020-06-09 14:37:16 | [diff] [blame] | 102 | StopStreamV4L2Queue(true); |
Chih-Yu Huang | 1c5d421 | 2019-08-01 07:04:47 | [diff] [blame] | 103 | if (input_queue_) { |
| 104 | input_queue_->DeallocateBuffers(); |
| 105 | input_queue_ = nullptr; |
| 106 | } |
| 107 | if (output_queue_) { |
| 108 | output_queue_->DeallocateBuffers(); |
| 109 | output_queue_ = nullptr; |
| 110 | } |
Francois Buergisser | 7fee4d1 | 2019-10-03 09:22:32 | [diff] [blame] | 111 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 112 | weak_this_factory_.InvalidateWeakPtrs(); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 113 | } |
| 114 | |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 115 | void V4L2VideoDecoder::Initialize(const VideoDecoderConfig& config, |
| 116 | InitCB init_cb, |
| 117 | const OutputCB& output_cb) { |
Alexandre Courbot | dcb6ebb | 2019-06-25 04:25:36 | [diff] [blame] | 118 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Chih-Yu Huang | a35c28a | 2019-10-21 02:43:09 | [diff] [blame] | 119 | DCHECK(config.IsValidConfig()); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 120 | DCHECK(state_ == State::kUninitialized || state_ == State::kDecoding); |
| 121 | DVLOGF(3); |
| 122 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 123 | // Reset V4L2 device and queue if reinitializing decoder. |
| 124 | if (state_ != State::kUninitialized) { |
Alexandre Courbot | 9270ddc | 2020-06-09 14:37:16 | [diff] [blame] | 125 | if (!StopStreamV4L2Queue(true)) { |
Ted Meyer | 2a41e24 | 2020-03-20 08:01:27 | [diff] [blame] | 126 | std::move(init_cb).Run(StatusCode::kV4l2FailedToStopStreamQueue); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 127 | return; |
| 128 | } |
| 129 | |
| 130 | input_queue_->DeallocateBuffers(); |
| 131 | output_queue_->DeallocateBuffers(); |
| 132 | input_queue_ = nullptr; |
| 133 | output_queue_ = nullptr; |
| 134 | |
| 135 | device_ = V4L2Device::Create(); |
| 136 | if (!device_) { |
| 137 | VLOGF(1) << "Failed to create V4L2 device."; |
Ted Meyer | 2a41e24 | 2020-03-20 08:01:27 | [diff] [blame] | 138 | std::move(init_cb).Run(StatusCode::kV4l2NoDevice); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 139 | return; |
| 140 | } |
| 141 | |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 142 | if (backend_) |
| 143 | backend_ = nullptr; |
| 144 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 145 | SetState(State::kUninitialized); |
| 146 | } |
| 147 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 148 | // Open V4L2 device. |
| 149 | VideoCodecProfile profile = config.profile(); |
Alexandre Courbot | 071818f | 2020-06-12 07:15:31 | [diff] [blame] | 150 | uint32_t input_format_fourcc_stateless = |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 151 | V4L2Device::VideoCodecProfileToV4L2PixFmt(profile, true); |
Alexandre Courbot | 071818f | 2020-06-12 07:15:31 | [diff] [blame] | 152 | if (!input_format_fourcc_stateless || |
| 153 | !device_->Open(V4L2Device::Type::kDecoder, |
| 154 | input_format_fourcc_stateless)) { |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 155 | VLOGF(1) << "Failed to open device for profile: " << profile |
Alexandre Courbot | 071818f | 2020-06-12 07:15:31 | [diff] [blame] | 156 | << " fourcc: " << FourccToString(input_format_fourcc_stateless); |
| 157 | input_format_fourcc_stateless = 0; |
| 158 | } else { |
| 159 | VLOGF(1) << "Found V4L2 device capable of stateless decoding for " |
| 160 | << FourccToString(input_format_fourcc_stateless); |
| 161 | } |
| 162 | |
| 163 | uint32_t input_format_fourcc_stateful = |
| 164 | V4L2Device::VideoCodecProfileToV4L2PixFmt(profile, false); |
| 165 | if (!input_format_fourcc_stateful || |
| 166 | !device_->Open(V4L2Device::Type::kDecoder, |
| 167 | input_format_fourcc_stateful)) { |
| 168 | VLOGF(1) << "Failed to open device for profile: " << profile |
| 169 | << " fourcc: " << FourccToString(input_format_fourcc_stateful); |
| 170 | input_format_fourcc_stateful = 0; |
| 171 | } else { |
| 172 | VLOGF(1) << "Found V4L2 device capable of stateful decoding for " |
| 173 | << FourccToString(input_format_fourcc_stateful); |
| 174 | } |
| 175 | |
| 176 | if (!input_format_fourcc_stateless && !input_format_fourcc_stateful) { |
Ted Meyer | 2a41e24 | 2020-03-20 08:01:27 | [diff] [blame] | 177 | std::move(init_cb).Run(StatusCode::kV4l2NoDecoder); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 178 | return; |
| 179 | } |
| 180 | |
| 181 | struct v4l2_capability caps; |
| 182 | const __u32 kCapsRequired = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING; |
| 183 | if (device_->Ioctl(VIDIOC_QUERYCAP, &caps) || |
| 184 | (caps.capabilities & kCapsRequired) != kCapsRequired) { |
| 185 | VLOGF(1) << "ioctl() failed: VIDIOC_QUERYCAP, " |
| 186 | << "caps check failed: 0x" << std::hex << caps.capabilities; |
Ted Meyer | 2a41e24 | 2020-03-20 08:01:27 | [diff] [blame] | 187 | std::move(init_cb).Run(StatusCode::kV4l2FailedFileCapabilitiesCheck); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 188 | return; |
| 189 | } |
| 190 | |
Hirokazu Honda | 23c6a6a | 2019-07-25 11:14:45 | [diff] [blame] | 191 | pixel_aspect_ratio_ = config.GetPixelAspectRatio(); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 192 | |
Alexandre Courbot | 229c7fe3 | 2019-10-07 06:48:01 | [diff] [blame] | 193 | // Create Input/Output V4L2Queue |
| 194 | input_queue_ = device_->GetQueue(V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE); |
| 195 | output_queue_ = device_->GetQueue(V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE); |
| 196 | if (!input_queue_ || !output_queue_) { |
| 197 | VLOGF(1) << "Failed to create V4L2 queue."; |
Ted Meyer | 2a41e24 | 2020-03-20 08:01:27 | [diff] [blame] | 198 | std::move(init_cb).Run(StatusCode::kV4l2FailedResourceAllocation); |
Alexandre Courbot | 229c7fe3 | 2019-10-07 06:48:01 | [diff] [blame] | 199 | return; |
| 200 | } |
| 201 | |
Alexandre Courbot | 071818f | 2020-06-12 07:15:31 | [diff] [blame] | 202 | uint32_t input_format_fourcc; |
| 203 | if (input_format_fourcc_stateful) { |
| 204 | backend_ = std::make_unique<V4L2StatefulVideoDecoderBackend>( |
| 205 | this, device_, profile, decoder_task_runner_); |
| 206 | input_format_fourcc = input_format_fourcc_stateful; |
| 207 | } else if (input_format_fourcc_stateless) { |
| 208 | backend_ = std::make_unique<V4L2StatelessVideoDecoderBackend>( |
| 209 | this, device_, profile, decoder_task_runner_); |
| 210 | input_format_fourcc = input_format_fourcc_stateless; |
| 211 | } else { |
| 212 | VLOGF(1) << "No backend capable of taking this profile."; |
| 213 | std::move(init_cb).Run(StatusCode::kV4l2FailedResourceAllocation); |
| 214 | return; |
| 215 | } |
| 216 | |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 217 | if (!backend_->Initialize()) { |
Alexandre Courbot | 071818f | 2020-06-12 07:15:31 | [diff] [blame] | 218 | VLOGF(1) << "Failed to initialize backend."; |
Ted Meyer | 2a41e24 | 2020-03-20 08:01:27 | [diff] [blame] | 219 | std::move(init_cb).Run(StatusCode::kV4l2FailedResourceAllocation); |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 220 | return; |
| 221 | } |
| 222 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 223 | // Setup input format. |
| 224 | if (!SetupInputFormat(input_format_fourcc)) { |
| 225 | VLOGF(1) << "Failed to setup input format."; |
Ted Meyer | 2a41e24 | 2020-03-20 08:01:27 | [diff] [blame] | 226 | std::move(init_cb).Run(StatusCode::kV4l2BadFormat); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 227 | return; |
| 228 | } |
| 229 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 230 | if (input_queue_->AllocateBuffers(kNumInputBuffers, V4L2_MEMORY_MMAP) == 0) { |
| 231 | VLOGF(1) << "Failed to allocate input buffer."; |
Ted Meyer | 2a41e24 | 2020-03-20 08:01:27 | [diff] [blame] | 232 | std::move(init_cb).Run(StatusCode::kV4l2FailedResourceAllocation); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 233 | return; |
| 234 | } |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 235 | |
Alexandre Courbot | 62369b5e | 2020-06-11 14:11:15 | [diff] [blame] | 236 | // Start streaming input queue and polling. This is required for the stateful |
| 237 | // decoder, and doesn't hurt for the stateless one. |
| 238 | if (!StartStreamV4L2Queue(false)) { |
| 239 | VLOGF(1) << "Failed to start streaming."; |
| 240 | std::move(init_cb).Run(StatusCode::kV4L2FailedToStartStreamQueue); |
| 241 | return; |
| 242 | } |
| 243 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 244 | // Call init_cb |
| 245 | output_cb_ = output_cb; |
| 246 | SetState(State::kDecoding); |
Ted Meyer | 2a41e24 | 2020-03-20 08:01:27 | [diff] [blame] | 247 | std::move(init_cb).Run(::media::OkStatus()); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 248 | } |
| 249 | |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 250 | bool V4L2VideoDecoder::SetupInputFormat(uint32_t input_format_fourcc) { |
Alexandre Courbot | dcb6ebb | 2019-06-25 04:25:36 | [diff] [blame] | 251 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 252 | DCHECK_EQ(state_, State::kUninitialized); |
| 253 | |
| 254 | // Check if the format is supported. |
| 255 | std::vector<uint32_t> formats = device_->EnumerateSupportedPixelformats( |
| 256 | V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE); |
| 257 | if (std::find(formats.begin(), formats.end(), input_format_fourcc) == |
| 258 | formats.end()) { |
| 259 | DVLOGF(3) << "Input fourcc " << input_format_fourcc |
| 260 | << " not supported by device."; |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | // Determine the input buffer size. |
| 265 | gfx::Size max_size, min_size; |
| 266 | device_->GetSupportedResolution(input_format_fourcc, &min_size, &max_size); |
| 267 | size_t input_size = max_size.GetArea() > k1080pArea |
| 268 | ? kInputBufferMaxSizeFor4k |
| 269 | : kInputBufferMaxSizeFor1080p; |
| 270 | |
| 271 | // Setup the input format. |
Yuki Shiino | 86b64ff | 2019-10-07 05:37:35 | [diff] [blame] | 272 | auto format = |
| 273 | input_queue_->SetFormat(input_format_fourcc, gfx::Size(), input_size); |
| 274 | if (!format) { |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 275 | VPLOGF(1) << "Failed to call IOCTL to set input format."; |
| 276 | return false; |
| 277 | } |
Yuki Shiino | 86b64ff | 2019-10-07 05:37:35 | [diff] [blame] | 278 | DCHECK_EQ(format->fmt.pix_mp.pixelformat, input_format_fourcc); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 279 | |
| 280 | return true; |
| 281 | } |
| 282 | |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 283 | bool V4L2VideoDecoder::SetupOutputFormat(const gfx::Size& size, |
| 284 | const gfx::Rect& visible_rect) { |
Alexandre Courbot | dcb6ebb | 2019-06-25 04:25:36 | [diff] [blame] | 285 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 286 | DVLOGF(3) << "size: " << size.ToString() |
| 287 | << ", visible_rect: " << visible_rect.ToString(); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 288 | |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 289 | // Get the supported output formats and their corresponding negotiated sizes. |
| 290 | std::vector<std::pair<Fourcc, gfx::Size>> candidates; |
| 291 | for (const uint32_t& pixfmt : device_->EnumerateSupportedPixelformats( |
| 292 | V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) { |
Alexandre Courbot | 139ebe2 | 2019-12-12 01:46:39 | [diff] [blame] | 293 | const auto candidate = Fourcc::FromV4L2PixFmt(pixfmt); |
| 294 | if (!candidate) { |
| 295 | DVLOGF(1) << "Pixel format " << FourccToString(pixfmt) |
| 296 | << " is not supported, skipping..."; |
| 297 | continue; |
| 298 | } |
| 299 | |
Hirokazu Honda | 685bf01 | 2019-08-05 01:30:00 | [diff] [blame] | 300 | base::Optional<struct v4l2_format> format = |
Alexandre Courbot | ae212018 | 2020-09-22 17:49:26 | [diff] [blame] | 301 | output_queue_->TryFormat(pixfmt, size, 0); |
Alexandre Courbot | 139ebe2 | 2019-12-12 01:46:39 | [diff] [blame] | 302 | if (!format) |
| 303 | continue; |
| 304 | |
| 305 | gfx::Size adjusted_size(format->fmt.pix_mp.width, |
| 306 | format->fmt.pix_mp.height); |
Alexandre Courbot | 646b4ae | 2020-09-11 12:08:23 | [diff] [blame] | 307 | candidates.emplace_back(*candidate, adjusted_size); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 308 | } |
| 309 | |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 310 | // Ask the pipeline to pick the output format. |
Alexandre Courbot | 0276e8f | 2020-09-14 03:04:06 | [diff] [blame] | 311 | const base::Optional<std::pair<Fourcc, gfx::Size>> output_format = |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 312 | client_->PickDecoderOutputFormat(candidates, visible_rect); |
Alexandre Courbot | 0276e8f | 2020-09-14 03:04:06 | [diff] [blame] | 313 | if (!output_format) { |
| 314 | VLOGF(1) << "Failed to pick an output format."; |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 315 | return false; |
| 316 | } |
Alexandre Courbot | 0276e8f | 2020-09-14 03:04:06 | [diff] [blame] | 317 | Fourcc fourcc = std::move(output_format->first); |
| 318 | gfx::Size picked_size = std::move(output_format->second); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 319 | |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 320 | // We successfully picked the output format. Now setup output format again. |
| 321 | base::Optional<struct v4l2_format> format = |
Alexandre Courbot | 0276e8f | 2020-09-14 03:04:06 | [diff] [blame] | 322 | output_queue_->SetFormat(fourcc.ToV4L2PixFmt(), picked_size, 0); |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 323 | DCHECK(format); |
| 324 | gfx::Size adjusted_size(format->fmt.pix_mp.width, format->fmt.pix_mp.height); |
| 325 | DCHECK_EQ(adjusted_size.width() % 16, 0); |
| 326 | DCHECK_EQ(adjusted_size.height() % 16, 0); |
Alexandre Courbot | 0276e8f | 2020-09-14 03:04:06 | [diff] [blame] | 327 | if (!gfx::Rect(adjusted_size).Contains(gfx::Rect(picked_size))) { |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 328 | VLOGF(1) << "The adjusted coded size (" << adjusted_size.ToString() |
Alexandre Courbot | 0276e8f | 2020-09-14 03:04:06 | [diff] [blame] | 329 | << ") should contains the original coded size(" |
| 330 | << picked_size.ToString() << ")."; |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 331 | return false; |
| 332 | } |
| 333 | |
| 334 | // Got the adjusted size from the V4L2 driver. Now setup the frame pool. |
| 335 | // TODO(akahuang): It is possible there is an allocatable formats among |
| 336 | // candidates, but PickDecoderOutputFormat() selects other non-allocatable |
| 337 | // format. The correct flow is to attach an info to candidates if it is |
| 338 | // created by VideoFramePool. |
| 339 | DmabufVideoFramePool* pool = client_->GetVideoFramePool(); |
| 340 | if (pool) { |
Miguel Casas | 6ee396b | 2020-01-17 21:21:16 | [diff] [blame] | 341 | base::Optional<GpuBufferLayout> layout = pool->Initialize( |
Alexandre Courbot | 0276e8f | 2020-09-14 03:04:06 | [diff] [blame] | 342 | fourcc, adjusted_size, visible_rect, |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 343 | GetNaturalSize(visible_rect, pixel_aspect_ratio_), num_output_frames_); |
| 344 | if (!layout) { |
| 345 | VLOGF(1) << "Failed to setup format to VFPool"; |
| 346 | return false; |
| 347 | } |
| 348 | if (layout->size() != adjusted_size) { |
| 349 | VLOGF(1) << "The size adjusted by VFPool is different from one " |
Alexandre Courbot | 0276e8f | 2020-09-14 03:04:06 | [diff] [blame] | 350 | << "adjusted by a video driver. fourcc: " << fourcc.ToString() |
Alexandre Courbot | 139ebe2 | 2019-12-12 01:46:39 | [diff] [blame] | 351 | << ", (video driver v.s. VFPool) " << adjusted_size.ToString() |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 352 | << " != " << layout->size().ToString(); |
| 353 | return false; |
| 354 | } |
Fritz Koenig | b7b206e | 2020-09-02 17:48:49 | [diff] [blame] | 355 | |
| 356 | if (layout->modifier() && |
| 357 | layout->modifier() != gfx::NativePixmapHandle::kNoModifier) { |
| 358 | base::Optional<struct v4l2_format> modifier_format = |
Alexandre Courbot | 0276e8f | 2020-09-14 03:04:06 | [diff] [blame] | 359 | output_queue_->SetModifierFormat(layout->modifier(), picked_size); |
Fritz Koenig | b7b206e | 2020-09-02 17:48:49 | [diff] [blame] | 360 | if (!modifier_format) |
| 361 | return false; |
| 362 | |
| 363 | gfx::Size size_for_modifier_format(format->fmt.pix_mp.width, |
| 364 | format->fmt.pix_mp.height); |
| 365 | if (size_for_modifier_format != adjusted_size) { |
| 366 | VLOGF(1) |
| 367 | << "Buffers were allocated for " << adjusted_size.ToString() |
| 368 | << " but modifier format is expecting buffers to be allocated for " |
| 369 | << size_for_modifier_format.ToString(); |
| 370 | return false; |
| 371 | } |
| 372 | } |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | return true; |
Chih-Yu Huang | f463589 | 2019-07-11 14:20:29 | [diff] [blame] | 376 | } |
| 377 | |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 378 | void V4L2VideoDecoder::Reset(base::OnceClosure closure) { |
Alexandre Courbot | dcb6ebb | 2019-06-25 04:25:36 | [diff] [blame] | 379 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 380 | DVLOGF(3); |
| 381 | |
Chih-Yu Huang | 1141fdeb | 2019-12-10 06:16:13 | [diff] [blame] | 382 | // Reset callback for resolution change, because the pipeline won't notify |
| 383 | // flushed after reset. |
| 384 | continue_change_resolution_cb_.Reset(); |
| 385 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 386 | // Call all pending decode callback. |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 387 | backend_->ClearPendingRequests(DecodeStatus::ABORTED); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 388 | |
| 389 | // Streamoff V4L2 queues to drop input and output buffers. |
| 390 | // If the queues are streaming before reset, then we need to start streaming |
| 391 | // them after stopping. |
Alexandre Courbot | 9270ddc | 2020-06-09 14:37:16 | [diff] [blame] | 392 | const bool is_input_streaming = input_queue_->IsStreaming(); |
| 393 | const bool is_output_streaming = output_queue_->IsStreaming(); |
| 394 | if (!StopStreamV4L2Queue(true)) |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 395 | return; |
| 396 | |
Alexandre Courbot | 9270ddc | 2020-06-09 14:37:16 | [diff] [blame] | 397 | if (is_input_streaming) { |
| 398 | if (!StartStreamV4L2Queue(is_output_streaming)) |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 399 | return; |
| 400 | } |
| 401 | |
Chih-Yu Huang | 1141fdeb | 2019-12-10 06:16:13 | [diff] [blame] | 402 | // If during flushing, Reset() will abort the following flush tasks. |
| 403 | // Now we are ready to decode new buffer. Go back to decoding state. |
| 404 | SetState(State::kDecoding); |
| 405 | |
Chih-Yu Huang | a35c28a | 2019-10-21 02:43:09 | [diff] [blame] | 406 | std::move(closure).Run(); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 407 | } |
| 408 | |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 409 | void V4L2VideoDecoder::Decode(scoped_refptr<DecoderBuffer> buffer, |
| 410 | DecodeCB decode_cb) { |
Alexandre Courbot | dcb6ebb | 2019-06-25 04:25:36 | [diff] [blame] | 411 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Chih-Yu Huang | 30b5fca | 2019-09-24 09:03:34 | [diff] [blame] | 412 | DCHECK_NE(state_, State::kUninitialized); |
| 413 | |
| 414 | if (state_ == State::kError) { |
Alexandre Courbot | 51ad52a8 | 2019-10-07 04:36:36 | [diff] [blame] | 415 | std::move(decode_cb).Run(DecodeStatus::DECODE_ERROR); |
Chih-Yu Huang | 30b5fca | 2019-09-24 09:03:34 | [diff] [blame] | 416 | return; |
| 417 | } |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 418 | |
Alexandre Courbot | 51ad52a8 | 2019-10-07 04:36:36 | [diff] [blame] | 419 | const int32_t bitstream_id = bitstream_id_generator_.GetNextBitstreamId(); |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 420 | backend_->EnqueueDecodeTask(std::move(buffer), std::move(decode_cb), |
| 421 | bitstream_id); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 422 | } |
| 423 | |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 424 | bool V4L2VideoDecoder::StartStreamV4L2Queue(bool start_output_queue) { |
Alexandre Courbot | dcb6ebb | 2019-06-25 04:25:36 | [diff] [blame] | 425 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 426 | DVLOGF(3); |
| 427 | |
Alexandre Courbot | 9270ddc | 2020-06-09 14:37:16 | [diff] [blame] | 428 | if (!input_queue_->Streamon() || |
| 429 | (start_output_queue && !output_queue_->Streamon())) { |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 430 | VLOGF(1) << "Failed to streamon V4L2 queue."; |
| 431 | SetState(State::kError); |
| 432 | return false; |
| 433 | } |
| 434 | |
Alexandre Courbot | ba021e3 | 2019-09-26 07:24:35 | [diff] [blame] | 435 | if (!device_->StartPolling( |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 436 | base::BindRepeating(&V4L2VideoDecoder::ServiceDeviceTask, weak_this_), |
| 437 | base::BindRepeating(&V4L2VideoDecoder::SetState, weak_this_, |
Alexandre Courbot | ba021e3 | 2019-09-26 07:24:35 | [diff] [blame] | 438 | State::kError))) { |
| 439 | SetState(State::kError); |
| 440 | return false; |
| 441 | } |
| 442 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 443 | return true; |
| 444 | } |
| 445 | |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 446 | bool V4L2VideoDecoder::StopStreamV4L2Queue(bool stop_input_queue) { |
Alexandre Courbot | dcb6ebb | 2019-06-25 04:25:36 | [diff] [blame] | 447 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 448 | DVLOGF(3); |
| 449 | |
Alexandre Courbot | ba021e3 | 2019-09-26 07:24:35 | [diff] [blame] | 450 | if (!device_->StopPolling()) { |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 451 | SetState(State::kError); |
| 452 | return false; |
| 453 | } |
| 454 | |
Chih-Yu Huang | 1c5d421 | 2019-08-01 07:04:47 | [diff] [blame] | 455 | // Streamoff input and output queue. |
Alexandre Courbot | 9270ddc | 2020-06-09 14:37:16 | [diff] [blame] | 456 | if (input_queue_ && stop_input_queue) |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 457 | input_queue_->Streamoff(); |
Chih-Yu Huang | 1c5d421 | 2019-08-01 07:04:47 | [diff] [blame] | 458 | if (output_queue_) |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 459 | output_queue_->Streamoff(); |
Chih-Yu Huang | 1c5d421 | 2019-08-01 07:04:47 | [diff] [blame] | 460 | |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 461 | if (backend_) |
Alexandre Courbot | b6f96b6 | 2020-06-23 02:24:04 | [diff] [blame] | 462 | backend_->OnStreamStopped(stop_input_queue); |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 463 | |
| 464 | return true; |
| 465 | } |
| 466 | |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 467 | void V4L2VideoDecoder::InitiateFlush() { |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 468 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
| 469 | DVLOGF(3); |
| 470 | |
| 471 | SetState(State::kFlushing); |
| 472 | } |
| 473 | |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 474 | void V4L2VideoDecoder::CompleteFlush() { |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 475 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
| 476 | DVLOGF(3); |
| 477 | |
| 478 | SetState(State::kDecoding); |
| 479 | } |
| 480 | |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 481 | void V4L2VideoDecoder::ChangeResolution(gfx::Size pic_size, |
| 482 | gfx::Rect visible_rect, |
| 483 | size_t num_output_frames) { |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 484 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
| 485 | DVLOGF(3); |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 486 | DCHECK(!continue_change_resolution_cb_); |
| 487 | |
| 488 | // After the pipeline flushes all frames, we can start changing resolution. |
| 489 | continue_change_resolution_cb_ = |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 490 | base::BindOnce(&V4L2VideoDecoder::ContinueChangeResolution, weak_this_, |
| 491 | pic_size, visible_rect, num_output_frames); |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 492 | |
| 493 | DCHECK(client_); |
| 494 | client_->PrepareChangeResolution(); |
| 495 | } |
| 496 | |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 497 | void V4L2VideoDecoder::ApplyResolutionChange() { |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 498 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
| 499 | DVLOGF(3); |
| 500 | DCHECK(continue_change_resolution_cb_); |
| 501 | |
Alexandre Courbot | cddb357f | 2020-04-15 03:51:02 | [diff] [blame] | 502 | std::move(continue_change_resolution_cb_).Run(); |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 503 | } |
| 504 | |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 505 | void V4L2VideoDecoder::ContinueChangeResolution( |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 506 | const gfx::Size& pic_size, |
| 507 | const gfx::Rect& visible_rect, |
| 508 | const size_t num_output_frames) { |
| 509 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
| 510 | DVLOGF(3); |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 511 | |
Chih-Yu Huang | 1141fdeb | 2019-12-10 06:16:13 | [diff] [blame] | 512 | // If we already reset, then skip it. |
| 513 | if (state_ == State::kDecoding) |
| 514 | return; |
| 515 | DCHECK_EQ(state_, State::kFlushing); |
| 516 | |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 517 | // Notify |backend_| that changing resolution fails. |
| 518 | // Note: |backend_| is owned by this, using base::Unretained() is safe. |
| 519 | base::ScopedClosureRunner done_caller( |
| 520 | base::BindOnce(&V4L2VideoDecoderBackend::OnChangeResolutionDone, |
| 521 | base::Unretained(backend_.get()), false)); |
| 522 | |
Jeffrey Kardatzke | b30b88b | 2020-09-29 02:28:08 | [diff] [blame] | 523 | DCHECK_GT(num_output_frames, 0u); |
| 524 | num_output_frames_ = num_output_frames + kDpbOutputBufferExtraCount; |
Chih-Yu Huang | bab32ff | 2019-11-01 08:49:21 | [diff] [blame] | 525 | |
Alexandre Courbot | 9270ddc | 2020-06-09 14:37:16 | [diff] [blame] | 526 | // Stateful decoders require the input queue to keep running during resolution |
| 527 | // changes, but stateless ones require it to be stopped. |
| 528 | if (!StopStreamV4L2Queue(backend_->StopInputQueueOnResChange())) |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 529 | return; |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 530 | |
Francois Buergisser | 30e651a | 2019-10-18 16:22:45 | [diff] [blame] | 531 | if (!output_queue_->DeallocateBuffers()) { |
| 532 | SetState(State::kError); |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 533 | return; |
Francois Buergisser | 30e651a | 2019-10-18 16:22:45 | [diff] [blame] | 534 | } |
Francois Buergisser | 30e651a | 2019-10-18 16:22:45 | [diff] [blame] | 535 | |
Jeffrey Kardatzke | b30b88b | 2020-09-29 02:28:08 | [diff] [blame] | 536 | if (!backend_->ApplyResolution(pic_size, visible_rect, num_output_frames_)) { |
Alexandre Courbot | f5936fe | 2020-04-02 07:24:12 | [diff] [blame] | 537 | SetState(State::kError); |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 538 | return; |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 539 | } |
| 540 | |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 541 | if (!SetupOutputFormat(pic_size, visible_rect)) { |
| 542 | VLOGF(1) << "Failed to setup output format."; |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 543 | SetState(State::kError); |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 544 | return; |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 545 | } |
| 546 | |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 547 | v4l2_memory type = |
| 548 | client_->GetVideoFramePool() ? V4L2_MEMORY_DMABUF : V4L2_MEMORY_MMAP; |
| 549 | if (output_queue_->AllocateBuffers(num_output_frames_, type) == 0) { |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 550 | VLOGF(1) << "Failed to request output buffers."; |
| 551 | SetState(State::kError); |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 552 | return; |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 553 | } |
Chih-Yu Huang | bab32ff | 2019-11-01 08:49:21 | [diff] [blame] | 554 | if (output_queue_->AllocatedBuffersCount() != num_output_frames_) { |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 555 | VLOGF(1) << "Could not allocate requested number of output buffers."; |
| 556 | SetState(State::kError); |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 557 | return; |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 558 | } |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 559 | |
Alexandre Courbot | 9270ddc | 2020-06-09 14:37:16 | [diff] [blame] | 560 | if (!StartStreamV4L2Queue(true)) { |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 561 | SetState(State::kError); |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 562 | return; |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 563 | } |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 564 | |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 565 | // Now notify |backend_| that changing resolution is done successfully. |
| 566 | // Note: |backend_| is owned by this, using base::Unretained() is safe. |
| 567 | done_caller.ReplaceClosure( |
| 568 | base::BindOnce(&V4L2VideoDecoderBackend::OnChangeResolutionDone, |
| 569 | base::Unretained(backend_.get()), true)); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 570 | } |
| 571 | |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 572 | void V4L2VideoDecoder::ServiceDeviceTask(bool event) { |
Alexandre Courbot | dcb6ebb | 2019-06-25 04:25:36 | [diff] [blame] | 573 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 574 | DVLOGF(3) << "Number of queued input buffers: " |
| 575 | << input_queue_->QueuedBuffersCount() |
| 576 | << ", Number of queued output buffers: " |
| 577 | << output_queue_->QueuedBuffersCount(); |
| 578 | |
Fritz Koenig | e6283bc | 2020-09-10 15:29:51 | [diff] [blame] | 579 | backend_->OnServiceDeviceTask(event); |
| 580 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 581 | // Dequeue V4L2 output buffer first to reduce output latency. |
| 582 | bool success; |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 583 | while (output_queue_->QueuedBuffersCount() > 0) { |
Alexandre Courbot | 360b5f1 | 2020-06-11 15:25:52 | [diff] [blame] | 584 | V4L2ReadableBufferRef dequeued_buffer; |
| 585 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 586 | std::tie(success, dequeued_buffer) = output_queue_->DequeueBuffer(); |
| 587 | if (!success) { |
| 588 | SetState(State::kError); |
| 589 | return; |
| 590 | } |
| 591 | if (!dequeued_buffer) |
| 592 | break; |
| 593 | |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 594 | backend_->OnOutputBufferDequeued(std::move(dequeued_buffer)); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | // Dequeue V4L2 input buffer. |
| 598 | while (input_queue_->QueuedBuffersCount() > 0) { |
Alexandre Courbot | 360b5f1 | 2020-06-11 15:25:52 | [diff] [blame] | 599 | V4L2ReadableBufferRef dequeued_buffer; |
| 600 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 601 | std::tie(success, dequeued_buffer) = input_queue_->DequeueBuffer(); |
| 602 | if (!success) { |
| 603 | SetState(State::kError); |
| 604 | return; |
| 605 | } |
| 606 | if (!dequeued_buffer) |
| 607 | break; |
| 608 | } |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 609 | } |
| 610 | |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 611 | void V4L2VideoDecoder::OutputFrame(scoped_refptr<VideoFrame> frame, |
| 612 | const gfx::Rect& visible_rect, |
| 613 | base::TimeDelta timestamp) { |
Alexandre Courbot | dcb6ebb | 2019-06-25 04:25:36 | [diff] [blame] | 614 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Fritz Koenig | 9c23409 | 2020-06-18 00:51:29 | [diff] [blame] | 615 | DVLOGF(4) << "timestamp: " << timestamp.InMilliseconds() << " msec"; |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 616 | |
Chih-Yu Huang | e497fcc | 2019-09-25 02:33:11 | [diff] [blame] | 617 | // Set the timestamp at which the decode operation started on the |
| 618 | // |frame|. If the frame has been outputted before (e.g. because of VP9 |
| 619 | // show-existing-frame feature) we can't overwrite the timestamp directly, as |
| 620 | // the original frame might still be in use. Instead we wrap the frame in |
| 621 | // another frame with a different timestamp. |
Chih-Yu Huang | eacec32 | 2019-09-24 05:51:34 | [diff] [blame] | 622 | if (frame->timestamp().is_zero()) |
| 623 | frame->set_timestamp(timestamp); |
| 624 | |
Chih-Yu Huang | f463589 | 2019-07-11 14:20:29 | [diff] [blame] | 625 | if (frame->visible_rect() != visible_rect || |
| 626 | frame->timestamp() != timestamp) { |
| 627 | gfx::Size natural_size = GetNaturalSize(visible_rect, pixel_aspect_ratio_); |
Chih-Yu Huang | ccf4603 | 2019-07-11 11:48:06 | [diff] [blame] | 628 | scoped_refptr<VideoFrame> wrapped_frame = VideoFrame::WrapVideoFrame( |
Ricky Liang | b2075eb | 2019-10-01 07:44:50 | [diff] [blame] | 629 | frame, frame->format(), visible_rect, natural_size); |
Chih-Yu Huang | ccf4603 | 2019-07-11 11:48:06 | [diff] [blame] | 630 | wrapped_frame->set_timestamp(timestamp); |
Chih-Yu Huang | ccf4603 | 2019-07-11 11:48:06 | [diff] [blame] | 631 | |
| 632 | frame = std::move(wrapped_frame); |
| 633 | } |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 634 | |
Chih-Yu Huang | a35c28a | 2019-10-21 02:43:09 | [diff] [blame] | 635 | output_cb_.Run(std::move(frame)); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 636 | } |
| 637 | |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 638 | DmabufVideoFramePool* V4L2VideoDecoder::GetVideoFramePool() const { |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 639 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
| 640 | DVLOGF(4); |
| 641 | |
| 642 | return client_->GetVideoFramePool(); |
| 643 | } |
| 644 | |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 645 | void V4L2VideoDecoder::SetState(State new_state) { |
Alexandre Courbot | dcb6ebb | 2019-06-25 04:25:36 | [diff] [blame] | 646 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 647 | DVLOGF(3) << "Change state from " << static_cast<int>(state_) << " to " |
| 648 | << static_cast<int>(new_state); |
| 649 | |
| 650 | if (state_ == new_state) |
| 651 | return; |
| 652 | if (state_ == State::kError) { |
| 653 | DVLOGF(3) << "Already in kError state."; |
| 654 | return; |
| 655 | } |
| 656 | |
| 657 | // Check if the state transition is valid. |
| 658 | switch (new_state) { |
| 659 | case State::kUninitialized: |
| 660 | if (state_ != State::kDecoding) { |
| 661 | VLOGF(1) << "Should not set to kUninitialized."; |
| 662 | new_state = State::kError; |
| 663 | } |
| 664 | break; |
| 665 | |
| 666 | case State::kDecoding: |
| 667 | break; |
| 668 | |
Chih-Yu Huang | 40adcdf | 2019-07-11 03:37:02 | [diff] [blame] | 669 | case State::kFlushing: |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 670 | if (state_ != State::kDecoding) { |
Chih-Yu Huang | 40adcdf | 2019-07-11 03:37:02 | [diff] [blame] | 671 | VLOGF(1) << "kFlushing should only be set when kDecoding."; |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 672 | new_state = State::kError; |
| 673 | } |
| 674 | break; |
| 675 | |
| 676 | case State::kError: |
| 677 | break; |
| 678 | } |
| 679 | |
| 680 | if (new_state == State::kError) { |
| 681 | VLOGF(1) << "Error occurred."; |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 682 | if (backend_) |
| 683 | backend_->ClearPendingRequests(DecodeStatus::DECODE_ERROR); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 684 | return; |
| 685 | } |
| 686 | state_ = new_state; |
| 687 | return; |
| 688 | } |
| 689 | |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 690 | void V4L2VideoDecoder::OnBackendError() { |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 691 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
| 692 | DVLOGF(2); |
| 693 | |
| 694 | SetState(State::kError); |
| 695 | } |
| 696 | |
Alexandre Courbot | 34f0e09 | 2020-06-25 02:44:38 | [diff] [blame] | 697 | bool V4L2VideoDecoder::IsDecoding() const { |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 698 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
| 699 | DVLOGF(3); |
| 700 | |
| 701 | return state_ == State::kDecoding; |
| 702 | } |
| 703 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 704 | } // namespace media |