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