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