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" |
mostynb | 6682b1c4 | 2016-04-19 10:17:30 | [diff] [blame] | 10 | #include "base/logging.h" |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 11 | #include "media/base/limits.h" |
markdittmer | 6e70beb8 | 2016-05-02 05:40:47 | [diff] [blame] | 12 | #include "media/gpu/vp9_decoder.h" |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 13 | |
markdittmer | 6e70beb8 | 2016-05-02 05:40:47 | [diff] [blame] | 14 | namespace media { |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 15 | |
| 16 | VP9Decoder::VP9Accelerator::VP9Accelerator() {} |
| 17 | |
| 18 | VP9Decoder::VP9Accelerator::~VP9Accelerator() {} |
| 19 | |
Fredrik Hubinette | 3cebc62 | 2018-10-27 01:01:12 | [diff] [blame] | 20 | VP9Decoder::VP9Decoder(std::unique_ptr<VP9Accelerator> accelerator, |
| 21 | const VideoColorSpace& container_color_space) |
posciak | 77118c9 | 2016-08-28 13:18:39 | [diff] [blame] | 22 | : state_(kNeedStreamMetadata), |
Fredrik Hubinette | 3cebc62 | 2018-10-27 01:01:12 | [diff] [blame] | 23 | container_color_space_(container_color_space), |
Miguel Casas | 2d5ecad8 | 2018-02-22 19:03:05 | [diff] [blame] | 24 | accelerator_(std::move(accelerator)), |
| 25 | parser_(accelerator_->IsFrameContextRequired()) { |
xhwang | 60f9667 | 2016-06-14 21:47:53 | [diff] [blame] | 26 | ref_frames_.resize(kVp9NumRefFrames); |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 27 | } |
| 28 | |
Miguel Casas | 2d5ecad8 | 2018-02-22 19:03:05 | [diff] [blame] | 29 | VP9Decoder::~VP9Decoder() = default; |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 30 | |
Miguel Casas | 22094b8 | 2018-05-22 21:16:48 | [diff] [blame] | 31 | void VP9Decoder::SetStream(int32_t id, |
| 32 | const uint8_t* ptr, |
| 33 | size_t size, |
| 34 | const DecryptConfig* decrypt_config) { |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 35 | DCHECK(ptr); |
| 36 | DCHECK(size); |
Pawel Osciak | ec6e21b | 2018-03-19 09:13:06 | [diff] [blame] | 37 | DVLOG(4) << "New input stream id: " << id << " at: " << (void*)ptr |
| 38 | << " size: " << size; |
| 39 | stream_id_ = id; |
Ted Meyer | 0b35c5fd | 2018-11-27 22:29:29 | [diff] [blame] | 40 | if (decrypt_config) { |
| 41 | parser_.SetStream(ptr, size, decrypt_config->Clone()); |
| 42 | } else { |
| 43 | parser_.SetStream(ptr, size, nullptr); |
| 44 | } |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | bool VP9Decoder::Flush() { |
| 48 | DVLOG(2) << "Decoder flush"; |
| 49 | Reset(); |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | void VP9Decoder::Reset() { |
| 54 | curr_frame_hdr_ = nullptr; |
| 55 | for (auto& ref_frame : ref_frames_) |
| 56 | ref_frame = nullptr; |
| 57 | |
| 58 | parser_.Reset(); |
| 59 | |
| 60 | if (state_ == kDecoding) |
| 61 | state_ = kAfterReset; |
| 62 | } |
| 63 | |
| 64 | VP9Decoder::DecodeResult VP9Decoder::Decode() { |
| 65 | while (1) { |
| 66 | // Read a new frame header if one is not awaiting decoding already. |
Ted Meyer | 0b35c5fd | 2018-11-27 22:29:29 | [diff] [blame] | 67 | std::unique_ptr<DecryptConfig> decrypt_config; |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 68 | if (!curr_frame_hdr_) { |
xhwang | 60f9667 | 2016-06-14 21:47:53 | [diff] [blame] | 69 | std::unique_ptr<Vp9FrameHeader> hdr(new Vp9FrameHeader()); |
Ted Meyer | 0b35c5fd | 2018-11-27 22:29:29 | [diff] [blame] | 70 | Vp9Parser::Result res = |
| 71 | parser_.ParseNextFrame(hdr.get(), &decrypt_config); |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 72 | switch (res) { |
xhwang | 60f9667 | 2016-06-14 21:47:53 | [diff] [blame] | 73 | case Vp9Parser::kOk: |
brusi_roy | 8e715596 | 2016-09-14 18:22:58 | [diff] [blame] | 74 | curr_frame_hdr_ = std::move(hdr); |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 75 | break; |
| 76 | |
xhwang | 60f9667 | 2016-06-14 21:47:53 | [diff] [blame] | 77 | case Vp9Parser::kEOStream: |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 78 | return kRanOutOfStreamData; |
| 79 | |
xhwang | 60f9667 | 2016-06-14 21:47:53 | [diff] [blame] | 80 | case Vp9Parser::kInvalidStream: |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 81 | DVLOG(1) << "Error parsing stream"; |
| 82 | SetError(); |
| 83 | return kDecodeError; |
kcwu | e5a9a629 | 2016-08-17 03:40:02 | [diff] [blame] | 84 | |
| 85 | case Vp9Parser::kAwaitingRefresh: |
posciak | 77118c9 | 2016-08-28 13:18:39 | [diff] [blame] | 86 | DVLOG(4) << "Awaiting context update"; |
| 87 | return kNeedContextUpdate; |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | if (state_ != kDecoding) { |
| 92 | // Not kDecoding, so we need a resume point (a keyframe), as we are after |
| 93 | // reset or at the beginning of the stream. Drop anything that is not |
| 94 | // a keyframe in such case, and continue looking for a keyframe. |
Sreerenj Balachandran | 5716330 | 2018-11-28 02:45:57 | [diff] [blame] | 95 | // Only exception is when the stream/sequence starts with an Intra only |
| 96 | // frame. |
| 97 | if (curr_frame_hdr_->IsKeyframe() || |
| 98 | (curr_frame_hdr_->IsIntra() && pic_size_.IsEmpty())) { |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 99 | state_ = kDecoding; |
| 100 | } else { |
| 101 | curr_frame_hdr_.reset(); |
| 102 | continue; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (curr_frame_hdr_->show_existing_frame) { |
| 107 | // This frame header only instructs us to display one of the |
| 108 | // previously-decoded frames, but has no frame data otherwise. Display |
| 109 | // and continue decoding subsequent frames. |
kcwu | e5a9a629 | 2016-08-17 03:40:02 | [diff] [blame] | 110 | size_t frame_to_show = curr_frame_hdr_->frame_to_show_map_idx; |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 111 | if (frame_to_show >= ref_frames_.size() || !ref_frames_[frame_to_show]) { |
| 112 | DVLOG(1) << "Request to show an invalid frame"; |
| 113 | SetError(); |
| 114 | return kDecodeError; |
| 115 | } |
| 116 | |
Chih-Yu Huang | cd5181e | 2018-10-23 06:33:06 | [diff] [blame] | 117 | // Duplicate the VP9Picture and set the current bitstream id to keep the |
| 118 | // correct timestamp. |
| 119 | scoped_refptr<VP9Picture> pic = ref_frames_[frame_to_show]->Duplicate(); |
| 120 | if (pic == nullptr) { |
| 121 | DVLOG(1) << "Failed to duplicate the VP9Picture."; |
| 122 | SetError(); |
| 123 | return kDecodeError; |
| 124 | } |
| 125 | pic->set_bitstream_id(stream_id_); |
| 126 | if (!accelerator_->OutputPicture(std::move(pic))) { |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 127 | SetError(); |
| 128 | return kDecodeError; |
| 129 | } |
| 130 | |
| 131 | curr_frame_hdr_.reset(); |
| 132 | continue; |
| 133 | } |
| 134 | |
kcwu | e5a9a629 | 2016-08-17 03:40:02 | [diff] [blame] | 135 | gfx::Size new_pic_size(curr_frame_hdr_->frame_width, |
| 136 | curr_frame_hdr_->frame_height); |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 137 | DCHECK(!new_pic_size.IsEmpty()); |
| 138 | |
| 139 | if (new_pic_size != pic_size_) { |
| 140 | DVLOG(1) << "New resolution: " << new_pic_size.ToString(); |
| 141 | |
Sreerenj Balachandran | 5716330 | 2018-11-28 02:45:57 | [diff] [blame] | 142 | if (!curr_frame_hdr_->IsKeyframe() && |
| 143 | (curr_frame_hdr_->IsIntra() && pic_size_.IsEmpty())) { |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 144 | // TODO(posciak): This is doable, but requires a few modifications to |
| 145 | // VDA implementations to allow multiple picture buffer sets in flight. |
Dongseong Hwang | 4f20ebb | 2018-06-07 00:28:20 | [diff] [blame] | 146 | // http://crbug.com/832264 |
Sreerenj Balachandran | 5716330 | 2018-11-28 02:45:57 | [diff] [blame] | 147 | DVLOG(1) << "Resolution change currently supported for keyframes and " |
| 148 | "sequence begins with Intra only when there is no prior " |
| 149 | "frames in the context"; |
Dongseong Hwang | 4f20ebb | 2018-06-07 00:28:20 | [diff] [blame] | 150 | if (++size_change_failure_counter_ > kVPxMaxNumOfSizeChangeFailures) { |
| 151 | SetError(); |
| 152 | return kDecodeError; |
| 153 | } |
| 154 | |
| 155 | curr_frame_hdr_.reset(); |
| 156 | return kRanOutOfStreamData; |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // TODO(posciak): This requires us to be on a keyframe (see above) and is |
| 160 | // required, because VDA clients expect all surfaces to be returned before |
| 161 | // they can cycle surface sets after receiving kAllocateNewSurfaces. |
| 162 | // This is only an implementation detail of VDAs and can be improved. |
| 163 | for (auto& ref_frame : ref_frames_) |
| 164 | ref_frame = nullptr; |
| 165 | |
| 166 | pic_size_ = new_pic_size; |
Dongseong Hwang | 4f20ebb | 2018-06-07 00:28:20 | [diff] [blame] | 167 | size_change_failure_counter_ = 0; |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 168 | return kAllocateNewSurfaces; |
| 169 | } |
| 170 | |
| 171 | scoped_refptr<VP9Picture> pic = accelerator_->CreateVP9Picture(); |
| 172 | if (!pic) |
| 173 | return kRanOutOfSurfaces; |
| 174 | |
johnylin | 7441087 | 2017-06-19 13:05:30 | [diff] [blame] | 175 | gfx::Rect new_render_rect(curr_frame_hdr_->render_width, |
| 176 | curr_frame_hdr_->render_height); |
| 177 | // For safety, check the validity of render size or leave it as (0, 0). |
| 178 | if (!gfx::Rect(pic_size_).Contains(new_render_rect)) { |
| 179 | DVLOG(1) << "Render size exceeds picture size. render size: " |
| 180 | << new_render_rect.ToString() |
| 181 | << ", picture size: " << pic_size_.ToString(); |
| 182 | new_render_rect = gfx::Rect(); |
| 183 | } |
| 184 | DVLOG(2) << "Render resolution: " << new_render_rect.ToString(); |
| 185 | |
Pawel Osciak | ec6e21b | 2018-03-19 09:13:06 | [diff] [blame] | 186 | pic->set_visible_rect(new_render_rect); |
| 187 | pic->set_bitstream_id(stream_id_); |
Fredrik Hubinette | 3cebc62 | 2018-10-27 01:01:12 | [diff] [blame] | 188 | |
Ted Meyer | 0b35c5fd | 2018-11-27 22:29:29 | [diff] [blame] | 189 | pic->set_decrypt_config(std::move(decrypt_config)); |
| 190 | |
Fredrik Hubinette | 3cebc62 | 2018-10-27 01:01:12 | [diff] [blame] | 191 | // For VP9, container color spaces override video stream color spaces. |
| 192 | if (container_color_space_.IsSpecified()) { |
| 193 | pic->set_colorspace(container_color_space_); |
| 194 | } else if (curr_frame_hdr_) { |
| 195 | pic->set_colorspace(curr_frame_hdr_->GetColorSpace()); |
| 196 | } |
kokihoon | fb941e9 | 2018-07-10 18:04:24 | [diff] [blame] | 197 | pic->frame_hdr = std::move(curr_frame_hdr_); |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 198 | |
| 199 | if (!DecodeAndOutputPicture(pic)) { |
| 200 | SetError(); |
| 201 | return kDecodeError; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | void VP9Decoder::RefreshReferenceFrames(const scoped_refptr<VP9Picture>& pic) { |
xhwang | 60f9667 | 2016-06-14 21:47:53 | [diff] [blame] | 207 | for (size_t i = 0; i < kVp9NumRefFrames; ++i) { |
vmpstr | acfa922 | 2015-10-28 22:42:23 | [diff] [blame] | 208 | DCHECK(!pic->frame_hdr->IsKeyframe() || pic->frame_hdr->RefreshFlag(i)); |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 209 | if (pic->frame_hdr->RefreshFlag(i)) |
| 210 | ref_frames_[i] = pic; |
| 211 | } |
| 212 | } |
| 213 | |
posciak | 77118c9 | 2016-08-28 13:18:39 | [diff] [blame] | 214 | void VP9Decoder::UpdateFrameContext( |
| 215 | const scoped_refptr<VP9Picture>& pic, |
| 216 | const base::Callback<void(const Vp9FrameContext&)>& context_refresh_cb) { |
Dale Curtis | e2516381 | 2018-09-21 22:13:39 | [diff] [blame] | 217 | DCHECK(context_refresh_cb); |
posciak | 77118c9 | 2016-08-28 13:18:39 | [diff] [blame] | 218 | Vp9FrameContext frame_ctx; |
| 219 | memset(&frame_ctx, 0, sizeof(frame_ctx)); |
| 220 | |
| 221 | if (!accelerator_->GetFrameContext(pic, &frame_ctx)) { |
| 222 | SetError(); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | context_refresh_cb.Run(frame_ctx); |
| 227 | } |
| 228 | |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 229 | bool VP9Decoder::DecodeAndOutputPicture(scoped_refptr<VP9Picture> pic) { |
| 230 | DCHECK(!pic_size_.IsEmpty()); |
| 231 | DCHECK(pic->frame_hdr); |
| 232 | |
posciak | 77118c9 | 2016-08-28 13:18:39 | [diff] [blame] | 233 | base::Closure done_cb; |
| 234 | const auto& context_refresh_cb = |
| 235 | parser_.GetContextRefreshCb(pic->frame_hdr->frame_context_idx); |
Dale Curtis | e2516381 | 2018-09-21 22:13:39 | [diff] [blame] | 236 | if (context_refresh_cb) |
posciak | 77118c9 | 2016-08-28 13:18:39 | [diff] [blame] | 237 | done_cb = base::Bind(&VP9Decoder::UpdateFrameContext, |
| 238 | base::Unretained(this), pic, context_refresh_cb); |
| 239 | |
| 240 | const Vp9Parser::Context& context = parser_.context(); |
| 241 | if (!accelerator_->SubmitDecode(pic, context.segmentation(), |
| 242 | context.loop_filter(), ref_frames_, done_cb)) |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 243 | return false; |
| 244 | |
| 245 | if (pic->frame_hdr->show_frame) { |
| 246 | if (!accelerator_->OutputPicture(pic)) |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | RefreshReferenceFrames(pic); |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | void VP9Decoder::SetError() { |
| 255 | Reset(); |
| 256 | state_ = kError; |
| 257 | } |
| 258 | |
| 259 | gfx::Size VP9Decoder::GetPicSize() const { |
| 260 | return pic_size_; |
| 261 | } |
| 262 | |
| 263 | size_t VP9Decoder::GetRequiredNumOfPictures() const { |
Miguel Casas | 477a706 | 2019-01-04 19:13:45 | [diff] [blame^] | 264 | constexpr size_t kPicsInPipeline = limits::kMaxVideoFrames + 1; |
| 265 | return kPicsInPipeline + GetNumReferenceFrames(); |
| 266 | } |
| 267 | |
| 268 | size_t VP9Decoder::GetNumReferenceFrames() const { |
| 269 | // Maximum number of reference frames needed plus one for the one being |
| 270 | // currently egressed. |
| 271 | return kVp9NumRefFrames + 1; |
posciak | d94b2b08 | 2015-09-18 04:03:40 | [diff] [blame] | 272 | } |
| 273 | |
markdittmer | 6e70beb8 | 2016-05-02 05:40:47 | [diff] [blame] | 274 | } // namespace media |