Avi Drissman | d387f092 | 2022-09-14 20:51:31 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
markdittmer | 6e70beb8 | 2016-05-02 05:40:47 | [diff] [blame] | 5 | #include "media/gpu/vp9_decoder.h" |
mostynb | 6682b1c4 | 2016-04-19 10:17:30 | [diff] [blame] | 6 | |
| 7 | #include <memory> |
| 8 | |
Hirokazu Honda | 10df95d | 2019-07-19 19:31:43 | [diff] [blame] | 9 | #include "base/feature_list.h" |
Avi Drissman | d70f89a | 2023-01-11 23:52:55 | [diff] [blame] | 10 | #include "base/functional/bind.h" |
mostynb | 6682b1c4 | 2016-04-19 10:17:30 | [diff] [blame] | 11 | #include "base/logging.h" |
Hirokazu Honda | 967b919 | 2019-08-27 00:59:42 | [diff] [blame] | 12 | #include "build/build_config.h" |
Yuta Hijikata | 29f6781 | 2020-10-24 01:15:44 | [diff] [blame] | 13 | #include "build/chromeos_buildflags.h" |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 14 | #include "media/base/limits.h" |
Hirokazu Honda | 10df95d | 2019-07-19 19:31:43 | [diff] [blame] | 15 | #include "media/base/media_switches.h" |
mark a. foltz | 36b42b396 | 2023-10-24 16:58:51 | [diff] [blame] | 16 | #include "media/base/platform_features.h" |
markdittmer | 6e70beb8 | 2016-05-02 05:40:47 | [diff] [blame] | 17 | #include "media/gpu/vp9_decoder.h" |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 18 | |
markdittmer | 6e70beb8 | 2016-05-02 05:40:47 | [diff] [blame] | 19 | namespace media { |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 20 | |
Hirokazu Honda | 10df95d | 2019-07-19 19:31:43 | [diff] [blame] | 21 | namespace { |
Hirokazu Honda | fb2681c | 2021-10-29 03:53:59 | [diff] [blame] | 22 | bool GetSpatialLayerFrameSize(const DecoderBuffer& decoder_buffer, |
| 23 | std::vector<uint32_t>& frame_sizes) { |
| 24 | frame_sizes.clear(); |
| 25 | |
Jeffrey Kardatzke | a8feeae | 2023-08-15 20:10:16 | [diff] [blame] | 26 | if (!decoder_buffer.has_side_data() || |
| 27 | decoder_buffer.side_data()->spatial_layers.empty()) { |
Hirokazu Honda | fb2681c | 2021-10-29 03:53:59 | [diff] [blame] | 28 | return true; |
Jeffrey Kardatzke | a8feeae | 2023-08-15 20:10:16 | [diff] [blame] | 29 | } |
Hirokazu Honda | fb2681c | 2021-10-29 03:53:59 | [diff] [blame] | 30 | |
Hirokazu Honda | bbf7f40 | 2022-04-04 08:13:49 | [diff] [blame] | 31 | bool enable_vp9_ksvc = |
mark a. foltz | 36b42b396 | 2023-10-24 16:58:51 | [diff] [blame] | 32 | // V4L2 stateless decoder does not support VP9 kSVC streams. |
| 33 | // See comments in media::IsVp9kSVCHWDecodingEnabled(). |
| 34 | #if BUILDFLAG(IS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY) |
Hirokazu Honda | bbf7f40 | 2022-04-04 08:13:49 | [diff] [blame] | 35 | false; |
Shuhai Peng | fe10fd0 | 2021-11-11 03:20:59 | [diff] [blame] | 36 | #else |
mark a. foltz | 36b42b396 | 2023-10-24 16:58:51 | [diff] [blame] | 37 | media::IsVp9kSVCHWDecodingEnabled(); |
| 38 | #endif // BUILDFLAG(IS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY) |
Hirokazu Honda | bbf7f40 | 2022-04-04 08:13:49 | [diff] [blame] | 39 | |
| 40 | if (!enable_vp9_ksvc) { |
mark a. foltz | 36b42b396 | 2023-10-24 16:58:51 | [diff] [blame] | 41 | DLOG(ERROR) << "VP9 k-SVC hardware decoding is disabled"; |
Hirokazu Honda | fb2681c | 2021-10-29 03:53:59 | [diff] [blame] | 42 | return false; |
Hirokazu Honda | 10df95d | 2019-07-19 19:31:43 | [diff] [blame] | 43 | } |
| 44 | |
Jeffrey Kardatzke | a8feeae | 2023-08-15 20:10:16 | [diff] [blame] | 45 | size_t num_of_layers = decoder_buffer.side_data()->spatial_layers.size(); |
Hirokazu Honda | 10df95d | 2019-07-19 19:31:43 | [diff] [blame] | 46 | if (num_of_layers > 3u) { |
| 47 | DLOG(WARNING) << "The maximum number of spatial layers in VP9 is three"; |
Hirokazu Honda | fb2681c | 2021-10-29 03:53:59 | [diff] [blame] | 48 | return false; |
Hirokazu Honda | 10df95d | 2019-07-19 19:31:43 | [diff] [blame] | 49 | } |
Hirokazu Honda | fb2681c | 2021-10-29 03:53:59 | [diff] [blame] | 50 | |
Jeffrey Kardatzke | a8feeae | 2023-08-15 20:10:16 | [diff] [blame] | 51 | frame_sizes = decoder_buffer.side_data()->spatial_layers; |
Hirokazu Honda | fb2681c | 2021-10-29 03:53:59 | [diff] [blame] | 52 | return true; |
Hirokazu Honda | 10df95d | 2019-07-19 19:31:43 | [diff] [blame] | 53 | } |
Hirokazu Honda | 7ec3265 | 2019-11-22 09:40:21 | [diff] [blame] | 54 | |
| 55 | VideoCodecProfile VP9ProfileToVideoCodecProfile(uint8_t profile) { |
| 56 | switch (profile) { |
| 57 | case 0: |
| 58 | return VP9PROFILE_PROFILE0; |
| 59 | case 1: |
| 60 | return VP9PROFILE_PROFILE1; |
| 61 | case 2: |
| 62 | return VP9PROFILE_PROFILE2; |
| 63 | case 3: |
| 64 | return VP9PROFILE_PROFILE3; |
| 65 | default: |
| 66 | return VIDEO_CODEC_PROFILE_UNKNOWN; |
| 67 | } |
| 68 | } |
| 69 | |
Hirokazu Honda | 96330378 | 2020-12-03 05:57:04 | [diff] [blame] | 70 | bool IsValidBitDepth(uint8_t bit_depth, VideoCodecProfile profile) { |
| 71 | // Spec 7.2. |
| 72 | switch (profile) { |
| 73 | case VP9PROFILE_PROFILE0: |
| 74 | case VP9PROFILE_PROFILE1: |
| 75 | return bit_depth == 8u; |
| 76 | case VP9PROFILE_PROFILE2: |
| 77 | case VP9PROFILE_PROFILE3: |
| 78 | return bit_depth == 10u || bit_depth == 12u; |
| 79 | default: |
Peter Boström | 0ce6af60 | 2023-05-15 21:27:24 | [diff] [blame] | 80 | NOTREACHED_NORETURN(); |
Hirokazu Honda | 96330378 | 2020-12-03 05:57:04 | [diff] [blame] | 81 | } |
| 82 | } |
Hirokazu Honda | 790d98e | 2020-12-04 03:41:48 | [diff] [blame] | 83 | |
Qiu Jianlin | 2153ac0 | 2022-08-18 04:13:15 | [diff] [blame] | 84 | VideoChromaSampling GetVP9ChromaSampling(const Vp9FrameHeader& frame_header) { |
| 85 | // Spec section 7.2.2 |
| 86 | uint8_t subsampling_x = frame_header.subsampling_x; |
| 87 | uint8_t subsampling_y = frame_header.subsampling_y; |
| 88 | if (subsampling_x == 0 && subsampling_y == 0) { |
| 89 | return VideoChromaSampling::k444; |
| 90 | } else if (subsampling_x == 1u && subsampling_y == 0u) { |
| 91 | return VideoChromaSampling::k422; |
| 92 | } else if (subsampling_x == 1u && subsampling_y == 1u) { |
| 93 | return VideoChromaSampling::k420; |
| 94 | } else { |
| 95 | DLOG(WARNING) << "Unknown chroma sampling format."; |
| 96 | return VideoChromaSampling::kUnknown; |
| 97 | } |
Hirokazu Honda | 790d98e | 2020-12-04 03:41:48 | [diff] [blame] | 98 | } |
Hirokazu Honda | 10df95d | 2019-07-19 19:31:43 | [diff] [blame] | 99 | } // namespace |
| 100 | |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 101 | VP9Decoder::VP9Accelerator::VP9Accelerator() {} |
| 102 | |
| 103 | VP9Decoder::VP9Accelerator::~VP9Accelerator() {} |
| 104 | |
Jeffrey Kardatzke | 3a496536 | 2023-11-29 22:19:44 | [diff] [blame] | 105 | scoped_refptr<VP9Picture> VP9Decoder::VP9Accelerator::CreateVP9PictureSecure( |
| 106 | uint64_t secure_handle) { |
| 107 | return nullptr; |
| 108 | } |
| 109 | |
Fredrik Hubinette | 3cebc62 | 2018-10-27 01:01:12 | [diff] [blame] | 110 | VP9Decoder::VP9Decoder(std::unique_ptr<VP9Accelerator> accelerator, |
Hirokazu Honda | 7ec3265 | 2019-11-22 09:40:21 | [diff] [blame] | 111 | VideoCodecProfile profile, |
Drew Davenport | eb9cb95 | 2023-01-31 00:29:39 | [diff] [blame] | 112 | const VideoColorSpace& container_color_space) |
posciak | 77118c9 | 2016-08-28 13:18:39 | [diff] [blame] | 113 | : state_(kNeedStreamMetadata), |
Fredrik Hubinette | 3cebc62 | 2018-10-27 01:01:12 | [diff] [blame] | 114 | container_color_space_(container_color_space), |
Hirokazu Honda | 7ec3265 | 2019-11-22 09:40:21 | [diff] [blame] | 115 | // TODO(hiroh): Set profile to UNKNOWN. |
| 116 | profile_(profile), |
Miguel Casas | 2d5ecad8 | 2018-02-22 19:03:05 | [diff] [blame] | 117 | accelerator_(std::move(accelerator)), |
Fritz Koenig | a10f1e8 | 2023-11-01 20:41:30 | [diff] [blame] | 118 | parser_(accelerator_->NeedsCompressedHeaderParsed()) {} |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 119 | |
Miguel Casas | 2d5ecad8 | 2018-02-22 19:03:05 | [diff] [blame] | 120 | VP9Decoder::~VP9Decoder() = default; |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 121 | |
Hirokazu Honda | 23132c3 | 2019-07-09 14:31:20 | [diff] [blame] | 122 | void VP9Decoder::SetStream(int32_t id, const DecoderBuffer& decoder_buffer) { |
| 123 | const uint8_t* ptr = decoder_buffer.data(); |
| 124 | const size_t size = decoder_buffer.data_size(); |
| 125 | const DecryptConfig* decrypt_config = decoder_buffer.decrypt_config(); |
| 126 | |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 127 | DCHECK(ptr); |
| 128 | DCHECK(size); |
Pawel Osciak | ec6e21b | 2018-03-19 09:13:06 | [diff] [blame] | 129 | DVLOG(4) << "New input stream id: " << id << " at: " << (void*)ptr |
| 130 | << " size: " << size; |
| 131 | stream_id_ = id; |
Hirokazu Honda | fb2681c | 2021-10-29 03:53:59 | [diff] [blame] | 132 | std::vector<uint32_t> frame_sizes; |
| 133 | if (!GetSpatialLayerFrameSize(decoder_buffer, frame_sizes)) { |
| 134 | SetError(); |
| 135 | return; |
Ted Meyer | 0b35c5fd | 2018-11-27 22:29:29 | [diff] [blame] | 136 | } |
Jeffrey Kardatzke | 3a496536 | 2023-11-29 22:19:44 | [diff] [blame] | 137 | if (decoder_buffer.has_side_data() && |
| 138 | decoder_buffer.side_data()->secure_handle) { |
| 139 | secure_handle_ = decoder_buffer.side_data()->secure_handle; |
| 140 | } else { |
| 141 | secure_handle_ = 0; |
| 142 | } |
Hirokazu Honda | fb2681c | 2021-10-29 03:53:59 | [diff] [blame] | 143 | |
| 144 | parser_.SetStream(ptr, size, frame_sizes, |
| 145 | decrypt_config ? decrypt_config->Clone() : nullptr); |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | bool VP9Decoder::Flush() { |
| 149 | DVLOG(2) << "Decoder flush"; |
| 150 | Reset(); |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | void VP9Decoder::Reset() { |
| 155 | curr_frame_hdr_ = nullptr; |
Jeffrey Kardatzke | 48ebdb69 | 2021-01-11 22:05:02 | [diff] [blame] | 156 | decrypt_config_.reset(); |
Jeffrey Kardatzke | 5e36baf | 2020-11-20 22:17:13 | [diff] [blame] | 157 | pending_pic_.reset(); |
Sreerenj Balachandran | 1beb548 | 2019-04-03 03:40:05 | [diff] [blame] | 158 | |
| 159 | ref_frames_.Clear(); |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 160 | |
| 161 | parser_.Reset(); |
| 162 | |
Jeffrey Kardatzke | 3a496536 | 2023-11-29 22:19:44 | [diff] [blame] | 163 | secure_handle_ = 0; |
| 164 | |
Jose Lopes | 24d1cda8 | 2020-02-21 13:22:36 | [diff] [blame] | 165 | if (state_ == kDecoding) { |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 166 | state_ = kAfterReset; |
Jose Lopes | 24d1cda8 | 2020-02-21 13:22:36 | [diff] [blame] | 167 | } |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | VP9Decoder::DecodeResult VP9Decoder::Decode() { |
Jeffrey Kardatzke | 5e36baf | 2020-11-20 22:17:13 | [diff] [blame] | 171 | while (true) { |
Hirokazu Honda | fb2681c | 2021-10-29 03:53:59 | [diff] [blame] | 172 | if (state_ == kError) |
| 173 | return kDecodeError; |
| 174 | |
Jeffrey Kardatzke | 5e36baf | 2020-11-20 22:17:13 | [diff] [blame] | 175 | // If we have a pending picture to decode, try that first. |
| 176 | if (pending_pic_) { |
Jeffrey Kardatzke | 4ed4e33 | 2020-12-05 00:32:15 | [diff] [blame] | 177 | VP9Accelerator::Status status = |
| 178 | DecodeAndOutputPicture(std::move(pending_pic_)); |
Jeffrey Kardatzke | 5e36baf | 2020-11-20 22:17:13 | [diff] [blame] | 179 | if (status == VP9Accelerator::Status::kFail) { |
Jeffrey Kardatzke | 5e36baf | 2020-11-20 22:17:13 | [diff] [blame] | 180 | SetError(); |
| 181 | return kDecodeError; |
| 182 | } |
| 183 | if (status == VP9Accelerator::Status::kTryAgain) |
| 184 | return kTryAgain; |
Jeffrey Kardatzke | 5e36baf | 2020-11-20 22:17:13 | [diff] [blame] | 185 | } |
| 186 | |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 187 | // Read a new frame header if one is not awaiting decoding already. |
| 188 | if (!curr_frame_hdr_) { |
Hirokazu Honda | 10df95d | 2019-07-19 19:31:43 | [diff] [blame] | 189 | gfx::Size allocate_size; |
xhwang | 60f9667 | 2016-06-14 21:47:53 | [diff] [blame] | 190 | std::unique_ptr<Vp9FrameHeader> hdr(new Vp9FrameHeader()); |
Ted Meyer | 0b35c5fd | 2018-11-27 22:29:29 | [diff] [blame] | 191 | Vp9Parser::Result res = |
Jeffrey Kardatzke | 48ebdb69 | 2021-01-11 22:05:02 | [diff] [blame] | 192 | parser_.ParseNextFrame(hdr.get(), &allocate_size, &decrypt_config_); |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 193 | switch (res) { |
xhwang | 60f9667 | 2016-06-14 21:47:53 | [diff] [blame] | 194 | case Vp9Parser::kOk: |
brusi_roy | 8e715596 | 2016-09-14 18:22:58 | [diff] [blame] | 195 | curr_frame_hdr_ = std::move(hdr); |
Hirokazu Honda | 10df95d | 2019-07-19 19:31:43 | [diff] [blame] | 196 | curr_frame_size_ = allocate_size; |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 197 | break; |
| 198 | |
xhwang | 60f9667 | 2016-06-14 21:47:53 | [diff] [blame] | 199 | case Vp9Parser::kEOStream: |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 200 | return kRanOutOfStreamData; |
| 201 | |
xhwang | 60f9667 | 2016-06-14 21:47:53 | [diff] [blame] | 202 | case Vp9Parser::kInvalidStream: |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 203 | DVLOG(1) << "Error parsing stream"; |
| 204 | SetError(); |
| 205 | return kDecodeError; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | if (state_ != kDecoding) { |
| 210 | // Not kDecoding, so we need a resume point (a keyframe), as we are after |
| 211 | // reset or at the beginning of the stream. Drop anything that is not |
| 212 | // a keyframe in such case, and continue looking for a keyframe. |
Sreerenj Balachandran | 5716330 | 2018-11-28 02:45:57 | [diff] [blame] | 213 | // Only exception is when the stream/sequence starts with an Intra only |
| 214 | // frame. |
| 215 | if (curr_frame_hdr_->IsKeyframe() || |
| 216 | (curr_frame_hdr_->IsIntra() && pic_size_.IsEmpty())) { |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 217 | state_ = kDecoding; |
| 218 | } else { |
| 219 | curr_frame_hdr_.reset(); |
Jeffrey Kardatzke | 48ebdb69 | 2021-01-11 22:05:02 | [diff] [blame] | 220 | decrypt_config_.reset(); |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 221 | continue; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if (curr_frame_hdr_->show_existing_frame) { |
| 226 | // This frame header only instructs us to display one of the |
| 227 | // previously-decoded frames, but has no frame data otherwise. Display |
| 228 | // and continue decoding subsequent frames. |
kcwu | e5a9a629 | 2016-08-17 03:40:02 | [diff] [blame] | 229 | size_t frame_to_show = curr_frame_hdr_->frame_to_show_map_idx; |
Sreerenj Balachandran | 1beb548 | 2019-04-03 03:40:05 | [diff] [blame] | 230 | if (frame_to_show >= kVp9NumRefFrames || |
| 231 | !ref_frames_.GetFrame(frame_to_show)) { |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 232 | DVLOG(1) << "Request to show an invalid frame"; |
| 233 | SetError(); |
| 234 | return kDecodeError; |
| 235 | } |
| 236 | |
Chih-Yu Huang | cd5181e | 2018-10-23 06:33:06 | [diff] [blame] | 237 | // Duplicate the VP9Picture and set the current bitstream id to keep the |
| 238 | // correct timestamp. |
Sreerenj Balachandran | 1beb548 | 2019-04-03 03:40:05 | [diff] [blame] | 239 | scoped_refptr<VP9Picture> pic = |
| 240 | ref_frames_.GetFrame(frame_to_show)->Duplicate(); |
Chih-Yu Huang | cd5181e | 2018-10-23 06:33:06 | [diff] [blame] | 241 | pic->set_bitstream_id(stream_id_); |
Dan Sanders | 9d117762 | 2023-08-15 19:39:11 | [diff] [blame] | 242 | pic->frame_hdr = std::move(curr_frame_hdr_); |
Chih-Yu Huang | cd5181e | 2018-10-23 06:33:06 | [diff] [blame] | 243 | if (!accelerator_->OutputPicture(std::move(pic))) { |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 244 | SetError(); |
| 245 | return kDecodeError; |
| 246 | } |
| 247 | |
Jeffrey Kardatzke | 48ebdb69 | 2021-01-11 22:05:02 | [diff] [blame] | 248 | decrypt_config_.reset(); |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 249 | continue; |
| 250 | } |
| 251 | |
Hirokazu Honda | 10df95d | 2019-07-19 19:31:43 | [diff] [blame] | 252 | gfx::Size new_pic_size = curr_frame_size_; |
Hirokazu Honda | 436cf27b | 2019-05-21 10:44:39 | [diff] [blame] | 253 | gfx::Rect new_render_rect(curr_frame_hdr_->render_width, |
| 254 | curr_frame_hdr_->render_height); |
Hirokazu Honda | cffd0ef | 2020-02-28 03:32:31 | [diff] [blame] | 255 | // For safety, check the validity of render size or leave it as pic size. |
Hirokazu Honda | 436cf27b | 2019-05-21 10:44:39 | [diff] [blame] | 256 | if (!gfx::Rect(new_pic_size).Contains(new_render_rect)) { |
| 257 | DVLOG(1) << "Render size exceeds picture size. render size: " |
| 258 | << new_render_rect.ToString() |
| 259 | << ", picture size: " << new_pic_size.ToString(); |
Hirokazu Honda | cffd0ef | 2020-02-28 03:32:31 | [diff] [blame] | 260 | new_render_rect = gfx::Rect(new_pic_size); |
Hirokazu Honda | 436cf27b | 2019-05-21 10:44:39 | [diff] [blame] | 261 | } |
Hirokazu Honda | 7ec3265 | 2019-11-22 09:40:21 | [diff] [blame] | 262 | VideoCodecProfile new_profile = |
| 263 | VP9ProfileToVideoCodecProfile(curr_frame_hdr_->profile); |
| 264 | if (new_profile == VIDEO_CODEC_PROFILE_UNKNOWN) { |
| 265 | VLOG(1) << "Invalid profile: " << curr_frame_hdr_->profile; |
| 266 | return kDecodeError; |
| 267 | } |
Hirokazu Honda | 96330378 | 2020-12-03 05:57:04 | [diff] [blame] | 268 | if (!IsValidBitDepth(curr_frame_hdr_->bit_depth, new_profile)) { |
| 269 | DVLOG(1) << "Invalid bit depth=" |
| 270 | << base::strict_cast<int>(curr_frame_hdr_->bit_depth) |
| 271 | << ", profile=" << GetProfileName(new_profile); |
| 272 | return kDecodeError; |
| 273 | } |
Qiu Jianlin | 2153ac0 | 2022-08-18 04:13:15 | [diff] [blame] | 274 | VideoChromaSampling new_chroma_sampling = |
| 275 | GetVP9ChromaSampling(*curr_frame_hdr_); |
| 276 | if (new_chroma_sampling != chroma_sampling_) { |
| 277 | chroma_sampling_ = new_chroma_sampling; |
Qiu Jianlin | 2153ac0 | 2022-08-18 04:13:15 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | if (chroma_sampling_ != VideoChromaSampling::k420) { |
Hirokazu Honda | 790d98e | 2020-12-04 03:41:48 | [diff] [blame] | 281 | DVLOG(1) << "Only YUV 4:2:0 is supported"; |
| 282 | return kDecodeError; |
| 283 | } |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 284 | |
Saifuddin Hitawala | e2e6d48 | 2023-05-31 19:33:28 | [diff] [blame] | 285 | VideoColorSpace new_color_space; |
| 286 | // For VP9, container color spaces override video stream color spaces. |
| 287 | if (container_color_space_.IsSpecified()) { |
| 288 | new_color_space = container_color_space_; |
| 289 | } else if (curr_frame_hdr_->GetColorSpace().IsSpecified()) { |
| 290 | new_color_space = curr_frame_hdr_->GetColorSpace(); |
| 291 | } |
| 292 | |
Hirokazu Honda | 436cf27b | 2019-05-21 10:44:39 | [diff] [blame] | 293 | DCHECK(!new_pic_size.IsEmpty()); |
Miguel Casas | c4d7584 | 2023-04-25 04:37:00 | [diff] [blame] | 294 | |
Dale Curtis | 7164e40 | 2023-10-10 22:18:03 | [diff] [blame] | 295 | bool is_color_space_change = false; |
| 296 | if (base::FeatureList::IsEnabled(kAVDColorSpaceChanges)) { |
| 297 | is_color_space_change = new_color_space.IsSpecified() && |
| 298 | new_color_space != picture_color_space_; |
| 299 | } |
| 300 | |
Miguel Casas | c4d7584 | 2023-04-25 04:37:00 | [diff] [blame] | 301 | const bool is_pic_size_different = new_pic_size != pic_size_; |
| 302 | const bool is_pic_size_larger = new_pic_size.width() > pic_size_.width() || |
| 303 | new_pic_size.height() > pic_size_.height(); |
| 304 | const bool is_new_configuration_different_enough = |
| 305 | (ignore_resolution_changes_to_smaller_for_testing_ |
| 306 | ? is_pic_size_larger |
| 307 | : is_pic_size_different) || |
Dale Curtis | 7164e40 | 2023-10-10 22:18:03 | [diff] [blame] | 308 | new_profile != profile_ || curr_frame_hdr_->bit_depth != bit_depth_ || |
| 309 | is_color_space_change; |
Miguel Casas | c4d7584 | 2023-04-25 04:37:00 | [diff] [blame] | 310 | |
| 311 | if (is_new_configuration_different_enough) { |
Hirokazu Honda | 7ec3265 | 2019-11-22 09:40:21 | [diff] [blame] | 312 | DVLOG(1) << "New profile: " << GetProfileName(new_profile) |
Miguel Casas | c4d7584 | 2023-04-25 04:37:00 | [diff] [blame] | 313 | << ", new resolution: " << new_pic_size.ToString() |
| 314 | << ", new bit depth: " |
Dale Curtis | 7164e40 | 2023-10-10 22:18:03 | [diff] [blame] | 315 | << base::strict_cast<int>(curr_frame_hdr_->bit_depth) |
| 316 | << ", new color space: " << new_color_space.ToString(); |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 317 | |
Sreerenj Balachandran | 5716330 | 2018-11-28 02:45:57 | [diff] [blame] | 318 | if (!curr_frame_hdr_->IsKeyframe() && |
Hirokazu Honda | 7ec3265 | 2019-11-22 09:40:21 | [diff] [blame] | 319 | !(curr_frame_hdr_->IsIntra() && pic_size_.IsEmpty())) { |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 320 | // TODO(posciak): This is doable, but requires a few modifications to |
| 321 | // VDA implementations to allow multiple picture buffer sets in flight. |
Dongseong Hwang | 4f20ebb | 2018-06-07 00:28:20 | [diff] [blame] | 322 | // http://crbug.com/832264 |
Sreerenj Balachandran | 5716330 | 2018-11-28 02:45:57 | [diff] [blame] | 323 | DVLOG(1) << "Resolution change currently supported for keyframes and " |
| 324 | "sequence begins with Intra only when there is no prior " |
| 325 | "frames in the context"; |
Dongseong Hwang | 4f20ebb | 2018-06-07 00:28:20 | [diff] [blame] | 326 | if (++size_change_failure_counter_ > kVPxMaxNumOfSizeChangeFailures) { |
| 327 | SetError(); |
| 328 | return kDecodeError; |
| 329 | } |
| 330 | |
| 331 | curr_frame_hdr_.reset(); |
Jeffrey Kardatzke | 48ebdb69 | 2021-01-11 22:05:02 | [diff] [blame] | 332 | decrypt_config_.reset(); |
Dongseong Hwang | 4f20ebb | 2018-06-07 00:28:20 | [diff] [blame] | 333 | return kRanOutOfStreamData; |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | // TODO(posciak): This requires us to be on a keyframe (see above) and is |
| 337 | // required, because VDA clients expect all surfaces to be returned before |
Hirokazu Honda | 7ec3265 | 2019-11-22 09:40:21 | [diff] [blame] | 338 | // they can cycle surface sets after receiving kConfigChange. |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 339 | // This is only an implementation detail of VDAs and can be improved. |
Sreerenj Balachandran | 1beb548 | 2019-04-03 03:40:05 | [diff] [blame] | 340 | ref_frames_.Clear(); |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 341 | |
| 342 | pic_size_ = new_pic_size; |
Hirokazu Honda | 436cf27b | 2019-05-21 10:44:39 | [diff] [blame] | 343 | visible_rect_ = new_render_rect; |
Hirokazu Honda | 7ec3265 | 2019-11-22 09:40:21 | [diff] [blame] | 344 | profile_ = new_profile; |
Hirokazu Honda | 96330378 | 2020-12-03 05:57:04 | [diff] [blame] | 345 | bit_depth_ = curr_frame_hdr_->bit_depth; |
Saifuddin Hitawala | e2e6d48 | 2023-05-31 19:33:28 | [diff] [blame] | 346 | picture_color_space_ = new_color_space; |
Dongseong Hwang | 4f20ebb | 2018-06-07 00:28:20 | [diff] [blame] | 347 | size_change_failure_counter_ = 0; |
Hirokazu Honda | 7ec3265 | 2019-11-22 09:40:21 | [diff] [blame] | 348 | return kConfigChange; |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 349 | } |
| 350 | |
Jeffrey Kardatzke | 3a496536 | 2023-11-29 22:19:44 | [diff] [blame] | 351 | scoped_refptr<VP9Picture> pic; |
| 352 | if (secure_handle_) { |
| 353 | pic = accelerator_->CreateVP9PictureSecure(secure_handle_); |
| 354 | } else { |
| 355 | pic = accelerator_->CreateVP9Picture(); |
| 356 | } |
Jeffrey Kardatzke | 4ed4e33 | 2020-12-05 00:32:15 | [diff] [blame] | 357 | if (!pic) { |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 358 | return kRanOutOfSurfaces; |
Jose Lopes | 24d1cda8 | 2020-02-21 13:22:36 | [diff] [blame] | 359 | } |
johnylin | 7441087 | 2017-06-19 13:05:30 | [diff] [blame] | 360 | DVLOG(2) << "Render resolution: " << new_render_rect.ToString(); |
| 361 | |
Jeffrey Kardatzke | 4ed4e33 | 2020-12-05 00:32:15 | [diff] [blame] | 362 | pic->set_visible_rect(new_render_rect); |
| 363 | pic->set_bitstream_id(stream_id_); |
Fredrik Hubinette | 3cebc62 | 2018-10-27 01:01:12 | [diff] [blame] | 364 | |
Jeffrey Kardatzke | 48ebdb69 | 2021-01-11 22:05:02 | [diff] [blame] | 365 | pic->set_decrypt_config(std::move(decrypt_config_)); |
Ted Meyer | 0b35c5fd | 2018-11-27 22:29:29 | [diff] [blame] | 366 | |
Saifuddin Hitawala | e2e6d48 | 2023-05-31 19:33:28 | [diff] [blame] | 367 | // Set the color space for the picture. |
| 368 | pic->set_colorspace(picture_color_space_); |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 369 | |
Jeffrey Kardatzke | 4ed4e33 | 2020-12-05 00:32:15 | [diff] [blame] | 370 | pic->frame_hdr = std::move(curr_frame_hdr_); |
Jeffrey Kardatzke | 5e36baf | 2020-11-20 22:17:13 | [diff] [blame] | 371 | |
Jeffrey Kardatzke | 4ed4e33 | 2020-12-05 00:32:15 | [diff] [blame] | 372 | VP9Accelerator::Status status = DecodeAndOutputPicture(std::move(pic)); |
Jeffrey Kardatzke | 5e36baf | 2020-11-20 22:17:13 | [diff] [blame] | 373 | if (status == VP9Accelerator::Status::kFail) { |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 374 | SetError(); |
| 375 | return kDecodeError; |
| 376 | } |
Jeffrey Kardatzke | 5e36baf | 2020-11-20 22:17:13 | [diff] [blame] | 377 | if (status == VP9Accelerator::Status::kTryAgain) |
| 378 | return kTryAgain; |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
Jeffrey Kardatzke | 5e36baf | 2020-11-20 22:17:13 | [diff] [blame] | 382 | VP9Decoder::VP9Accelerator::Status VP9Decoder::DecodeAndOutputPicture( |
| 383 | scoped_refptr<VP9Picture> pic) { |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 384 | DCHECK(!pic_size_.IsEmpty()); |
| 385 | DCHECK(pic->frame_hdr); |
| 386 | |
posciak | 77118c9 | 2016-08-28 13:18:39 | [diff] [blame] | 387 | const Vp9Parser::Context& context = parser_.context(); |
Jeffrey Kardatzke | 5e36baf | 2020-11-20 22:17:13 | [diff] [blame] | 388 | VP9Accelerator::Status status = accelerator_->SubmitDecode( |
Fritz Koenig | 159f69d | 2023-11-03 18:49:23 | [diff] [blame] | 389 | pic, context.segmentation(), context.loop_filter(), ref_frames_); |
Jeffrey Kardatzke | 4ed4e33 | 2020-12-05 00:32:15 | [diff] [blame] | 390 | if (status != VP9Accelerator::Status::kOk) { |
| 391 | if (status == VP9Accelerator::Status::kTryAgain) |
| 392 | pending_pic_ = std::move(pic); |
Jeffrey Kardatzke | 5e36baf | 2020-11-20 22:17:13 | [diff] [blame] | 393 | return status; |
Jeffrey Kardatzke | 4ed4e33 | 2020-12-05 00:32:15 | [diff] [blame] | 394 | } |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 395 | |
| 396 | if (pic->frame_hdr->show_frame) { |
Jeffrey Kardatzke | 5e36baf | 2020-11-20 22:17:13 | [diff] [blame] | 397 | if (!accelerator_->OutputPicture(pic)) |
| 398 | return VP9Accelerator::Status::kFail; |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 399 | } |
| 400 | |
Ted Meyer | 4fac4f6 | 2019-05-08 22:57:15 | [diff] [blame] | 401 | ref_frames_.Refresh(std::move(pic)); |
Jeffrey Kardatzke | 5e36baf | 2020-11-20 22:17:13 | [diff] [blame] | 402 | return status; |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | void VP9Decoder::SetError() { |
| 406 | Reset(); |
| 407 | state_ = kError; |
| 408 | } |
| 409 | |
| 410 | gfx::Size VP9Decoder::GetPicSize() const { |
| 411 | return pic_size_; |
| 412 | } |
| 413 | |
Hirokazu Honda | 436cf27b | 2019-05-21 10:44:39 | [diff] [blame] | 414 | gfx::Rect VP9Decoder::GetVisibleRect() const { |
| 415 | return visible_rect_; |
| 416 | } |
| 417 | |
Hirokazu Honda | 7ec3265 | 2019-11-22 09:40:21 | [diff] [blame] | 418 | VideoCodecProfile VP9Decoder::GetProfile() const { |
| 419 | return profile_; |
| 420 | } |
| 421 | |
Hirokazu Honda | 96330378 | 2020-12-03 05:57:04 | [diff] [blame] | 422 | uint8_t VP9Decoder::GetBitDepth() const { |
| 423 | return bit_depth_; |
| 424 | } |
| 425 | |
Qiu Jianlin | a790b3d | 2022-07-29 03:09:44 | [diff] [blame] | 426 | VideoChromaSampling VP9Decoder::GetChromaSampling() const { |
Qiu Jianlin | 2153ac0 | 2022-08-18 04:13:15 | [diff] [blame] | 427 | return chroma_sampling_; |
Qiu Jianlin | a790b3d | 2022-07-29 03:09:44 | [diff] [blame] | 428 | } |
| 429 | |
Saifuddin Hitawala | e2e6d48 | 2023-05-31 19:33:28 | [diff] [blame] | 430 | VideoColorSpace VP9Decoder::GetVideoColorSpace() const { |
| 431 | return picture_color_space_; |
| 432 | } |
| 433 | |
Arthur Sonzogni | 581b9e8 | 2024-01-30 17:25:15 | [diff] [blame] | 434 | std::optional<gfx::HDRMetadata> VP9Decoder::GetHDRMetadata() const { |
Sida Zhu | c427644 | 2022-12-03 03:54:24 | [diff] [blame] | 435 | // VP9 only allow HDR metadata exists in the container. |
Arthur Sonzogni | 581b9e8 | 2024-01-30 17:25:15 | [diff] [blame] | 436 | return std::nullopt; |
Sida Zhu | c427644 | 2022-12-03 03:54:24 | [diff] [blame] | 437 | } |
| 438 | |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 439 | size_t VP9Decoder::GetRequiredNumOfPictures() const { |
Miguel Casas | 477a706 | 2019-01-04 19:13:45 | [diff] [blame] | 440 | constexpr size_t kPicsInPipeline = limits::kMaxVideoFrames + 1; |
| 441 | return kPicsInPipeline + GetNumReferenceFrames(); |
| 442 | } |
| 443 | |
| 444 | size_t VP9Decoder::GetNumReferenceFrames() const { |
Miguel Casas | 3dd7e56 | 2019-02-14 17:38:46 | [diff] [blame] | 445 | // Maximum number of reference frames |
| 446 | return kVp9NumRefFrames; |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 447 | } |
| 448 | |
markdittmer | 6e70beb8 | 2016-05-02 05:40:47 | [diff] [blame] | 449 | } // namespace media |