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