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