[go: nahoru, domu]

blob: c364812962f0118b50cdb7f6d68e5e67da204135 [file] [log] [blame]
posciakd94b2b082015-09-18 04:03:401// 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
markdittmer6e70beb82016-05-02 05:40:475#include "media/gpu/vp9_decoder.h"
mostynb6682b1c42016-04-19 10:17:306
7#include <memory>
8
posciak77118c92016-08-28 13:18:399#include "base/bind.h"
mostynb6682b1c42016-04-19 10:17:3010#include "base/logging.h"
posciakd94b2b082015-09-18 04:03:4011#include "media/base/limits.h"
markdittmer6e70beb82016-05-02 05:40:4712#include "media/gpu/vp9_decoder.h"
posciakd94b2b082015-09-18 04:03:4013
markdittmer6e70beb82016-05-02 05:40:4714namespace media {
posciakd94b2b082015-09-18 04:03:4015
16VP9Decoder::VP9Accelerator::VP9Accelerator() {}
17
18VP9Decoder::VP9Accelerator::~VP9Accelerator() {}
19
Miguel Casas2d5ecad82018-02-22 19:03:0520VP9Decoder::VP9Decoder(std::unique_ptr<VP9Accelerator> accelerator)
posciak77118c92016-08-28 13:18:3921 : state_(kNeedStreamMetadata),
Miguel Casas2d5ecad82018-02-22 19:03:0522 accelerator_(std::move(accelerator)),
23 parser_(accelerator_->IsFrameContextRequired()) {
xhwang60f96672016-06-14 21:47:5324 ref_frames_.resize(kVp9NumRefFrames);
posciakd94b2b082015-09-18 04:03:4025}
26
Miguel Casas2d5ecad82018-02-22 19:03:0527VP9Decoder::~VP9Decoder() = default;
posciakd94b2b082015-09-18 04:03:4028
Miguel Casas22094b82018-05-22 21:16:4829void VP9Decoder::SetStream(int32_t id,
30 const uint8_t* ptr,
31 size_t size,
32 const DecryptConfig* decrypt_config) {
posciakd94b2b082015-09-18 04:03:4033 DCHECK(ptr);
34 DCHECK(size);
Miguel Casas22094b82018-05-22 21:16:4835 if (decrypt_config) {
36 NOTIMPLEMENTED();
37 state_ = kError;
38 return;
39 }
Pawel Osciakec6e21b2018-03-19 09:13:0640 DVLOG(4) << "New input stream id: " << id << " at: " << (void*)ptr
41 << " size: " << size;
42 stream_id_ = id;
posciakd94b2b082015-09-18 04:03:4043 parser_.SetStream(ptr, size);
44}
45
46bool VP9Decoder::Flush() {
47 DVLOG(2) << "Decoder flush";
48 Reset();
49 return true;
50}
51
52void 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
63VP9Decoder::DecodeResult VP9Decoder::Decode() {
64 while (1) {
65 // Read a new frame header if one is not awaiting decoding already.
66 if (!curr_frame_hdr_) {
xhwang60f96672016-06-14 21:47:5367 std::unique_ptr<Vp9FrameHeader> hdr(new Vp9FrameHeader());
posciak77118c92016-08-28 13:18:3968 Vp9Parser::Result res = parser_.ParseNextFrame(hdr.get());
posciakd94b2b082015-09-18 04:03:4069 switch (res) {
xhwang60f96672016-06-14 21:47:5370 case Vp9Parser::kOk:
brusi_roy8e7155962016-09-14 18:22:5871 curr_frame_hdr_ = std::move(hdr);
posciakd94b2b082015-09-18 04:03:4072 break;
73
xhwang60f96672016-06-14 21:47:5374 case Vp9Parser::kEOStream:
posciakd94b2b082015-09-18 04:03:4075 return kRanOutOfStreamData;
76
xhwang60f96672016-06-14 21:47:5377 case Vp9Parser::kInvalidStream:
posciakd94b2b082015-09-18 04:03:4078 DVLOG(1) << "Error parsing stream";
79 SetError();
80 return kDecodeError;
kcwue5a9a6292016-08-17 03:40:0281
82 case Vp9Parser::kAwaitingRefresh:
posciak77118c92016-08-28 13:18:3983 DVLOG(4) << "Awaiting context update";
84 return kNeedContextUpdate;
posciakd94b2b082015-09-18 04:03:4085 }
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.
kcwue5a9a6292016-08-17 03:40:02104 size_t frame_to_show = curr_frame_hdr_->frame_to_show_map_idx;
posciakd94b2b082015-09-18 04:03:40105 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
111 if (!accelerator_->OutputPicture(ref_frames_[frame_to_show])) {
112 SetError();
113 return kDecodeError;
114 }
115
116 curr_frame_hdr_.reset();
117 continue;
118 }
119
kcwue5a9a6292016-08-17 03:40:02120 gfx::Size new_pic_size(curr_frame_hdr_->frame_width,
121 curr_frame_hdr_->frame_height);
posciakd94b2b082015-09-18 04:03:40122 DCHECK(!new_pic_size.IsEmpty());
123
124 if (new_pic_size != pic_size_) {
125 DVLOG(1) << "New resolution: " << new_pic_size.ToString();
126
127 if (!curr_frame_hdr_->IsKeyframe()) {
128 // TODO(posciak): This is doable, but requires a few modifications to
129 // VDA implementations to allow multiple picture buffer sets in flight.
Dongseong Hwang4f20ebb2018-06-07 00:28:20130 // http://crbug.com/832264
posciakd94b2b082015-09-18 04:03:40131 DVLOG(1) << "Resolution change currently supported for keyframes only";
Dongseong Hwang4f20ebb2018-06-07 00:28:20132 if (++size_change_failure_counter_ > kVPxMaxNumOfSizeChangeFailures) {
133 SetError();
134 return kDecodeError;
135 }
136
137 curr_frame_hdr_.reset();
138 return kRanOutOfStreamData;
posciakd94b2b082015-09-18 04:03:40139 }
140
141 // TODO(posciak): This requires us to be on a keyframe (see above) and is
142 // required, because VDA clients expect all surfaces to be returned before
143 // they can cycle surface sets after receiving kAllocateNewSurfaces.
144 // This is only an implementation detail of VDAs and can be improved.
145 for (auto& ref_frame : ref_frames_)
146 ref_frame = nullptr;
147
148 pic_size_ = new_pic_size;
Dongseong Hwang4f20ebb2018-06-07 00:28:20149 size_change_failure_counter_ = 0;
posciakd94b2b082015-09-18 04:03:40150 return kAllocateNewSurfaces;
151 }
152
153 scoped_refptr<VP9Picture> pic = accelerator_->CreateVP9Picture();
154 if (!pic)
155 return kRanOutOfSurfaces;
156
johnylin74410872017-06-19 13:05:30157 gfx::Rect new_render_rect(curr_frame_hdr_->render_width,
158 curr_frame_hdr_->render_height);
159 // For safety, check the validity of render size or leave it as (0, 0).
160 if (!gfx::Rect(pic_size_).Contains(new_render_rect)) {
161 DVLOG(1) << "Render size exceeds picture size. render size: "
162 << new_render_rect.ToString()
163 << ", picture size: " << pic_size_.ToString();
164 new_render_rect = gfx::Rect();
165 }
166 DVLOG(2) << "Render resolution: " << new_render_rect.ToString();
167
Pawel Osciakec6e21b2018-03-19 09:13:06168 pic->set_visible_rect(new_render_rect);
169 pic->set_bitstream_id(stream_id_);
posciakd94b2b082015-09-18 04:03:40170 pic->frame_hdr.reset(curr_frame_hdr_.release());
171
172 if (!DecodeAndOutputPicture(pic)) {
173 SetError();
174 return kDecodeError;
175 }
176 }
177}
178
179void VP9Decoder::RefreshReferenceFrames(const scoped_refptr<VP9Picture>& pic) {
xhwang60f96672016-06-14 21:47:53180 for (size_t i = 0; i < kVp9NumRefFrames; ++i) {
vmpstracfa9222015-10-28 22:42:23181 DCHECK(!pic->frame_hdr->IsKeyframe() || pic->frame_hdr->RefreshFlag(i));
posciakd94b2b082015-09-18 04:03:40182 if (pic->frame_hdr->RefreshFlag(i))
183 ref_frames_[i] = pic;
184 }
185}
186
posciak77118c92016-08-28 13:18:39187void VP9Decoder::UpdateFrameContext(
188 const scoped_refptr<VP9Picture>& pic,
189 const base::Callback<void(const Vp9FrameContext&)>& context_refresh_cb) {
190 DCHECK(!context_refresh_cb.is_null());
191 Vp9FrameContext frame_ctx;
192 memset(&frame_ctx, 0, sizeof(frame_ctx));
193
194 if (!accelerator_->GetFrameContext(pic, &frame_ctx)) {
195 SetError();
196 return;
197 }
198
199 context_refresh_cb.Run(frame_ctx);
200}
201
posciakd94b2b082015-09-18 04:03:40202bool VP9Decoder::DecodeAndOutputPicture(scoped_refptr<VP9Picture> pic) {
203 DCHECK(!pic_size_.IsEmpty());
204 DCHECK(pic->frame_hdr);
205
posciak77118c92016-08-28 13:18:39206 base::Closure done_cb;
207 const auto& context_refresh_cb =
208 parser_.GetContextRefreshCb(pic->frame_hdr->frame_context_idx);
209 if (!context_refresh_cb.is_null())
210 done_cb = base::Bind(&VP9Decoder::UpdateFrameContext,
211 base::Unretained(this), pic, context_refresh_cb);
212
213 const Vp9Parser::Context& context = parser_.context();
214 if (!accelerator_->SubmitDecode(pic, context.segmentation(),
215 context.loop_filter(), ref_frames_, done_cb))
posciakd94b2b082015-09-18 04:03:40216 return false;
217
218 if (pic->frame_hdr->show_frame) {
219 if (!accelerator_->OutputPicture(pic))
220 return false;
221 }
222
223 RefreshReferenceFrames(pic);
224 return true;
225}
226
227void VP9Decoder::SetError() {
228 Reset();
229 state_ = kError;
230}
231
232gfx::Size VP9Decoder::GetPicSize() const {
233 return pic_size_;
234}
235
236size_t VP9Decoder::GetRequiredNumOfPictures() const {
237 // kMaxVideoFrames to keep higher level media pipeline populated, +2 for the
238 // pictures being parsed and decoded currently.
xhwang60f96672016-06-14 21:47:53239 return limits::kMaxVideoFrames + kVp9NumRefFrames + 2;
posciakd94b2b082015-09-18 04:03:40240}
241
markdittmer6e70beb82016-05-02 05:40:47242} // namespace media