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