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