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