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