[go: nahoru, domu]

blob: 92f4af087947e0eae96aaeafaa5318314069657b [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"
Hirokazu Honda10df95d2019-07-19 19:31:4310#include "base/feature_list.h"
mostynb6682b1c42016-04-19 10:17:3011#include "base/logging.h"
Hirokazu Honda967b9192019-08-27 00:59:4212#include "build/build_config.h"
Yuta Hijikata29f67812020-10-24 01:15:4413#include "build/chromeos_buildflags.h"
posciakd94b2b082015-09-18 04:03:4014#include "media/base/limits.h"
Hirokazu Honda10df95d2019-07-19 19:31:4315#include "media/base/media_switches.h"
markdittmer6e70beb82016-05-02 05:40:4716#include "media/gpu/vp9_decoder.h"
posciakd94b2b082015-09-18 04:03:4017
markdittmer6e70beb82016-05-02 05:40:4718namespace media {
posciakd94b2b082015-09-18 04:03:4019
Hirokazu Honda10df95d2019-07-19 19:31:4320namespace {
21std::vector<uint32_t> GetSpatialLayerFrameSize(
22 const DecoderBuffer& decoder_buffer) {
Yuta Hijikata29f67812020-10-24 01:15:4423#if defined(ARCH_CPU_X86_FAMILY) && BUILDFLAG(IS_ASH)
Hirokazu Honda10df95d2019-07-19 19:31:4324 const uint32_t* cue_data =
25 reinterpret_cast<const uint32_t*>(decoder_buffer.side_data());
Jose Lopes24d1cda82020-02-21 13:22:3626 if (!cue_data) {
Hirokazu Honda10df95d2019-07-19 19:31:4327 return {};
Jose Lopes24d1cda82020-02-21 13:22:3628 }
Hirokazu Honda10df95d2019-07-19 19:31:4329 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 Hijikata29f67812020-10-24 01:15:4440#endif // defined(ARCH_CPU_X86_FAMILY) && BUILDFLAG(IS_ASH)
Hirokazu Honda967b9192019-08-27 00:59:4241 return {};
Hirokazu Honda10df95d2019-07-19 19:31:4342}
Hirokazu Honda7ec32652019-11-22 09:40:2143
44VideoCodecProfile 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 Honda10df95d2019-07-19 19:31:4359} // namespace
60
posciakd94b2b082015-09-18 04:03:4061VP9Decoder::VP9Accelerator::VP9Accelerator() {}
62
63VP9Decoder::VP9Accelerator::~VP9Accelerator() {}
64
Fredrik Hubinette3cebc622018-10-27 01:01:1265VP9Decoder::VP9Decoder(std::unique_ptr<VP9Accelerator> accelerator,
Hirokazu Honda7ec32652019-11-22 09:40:2166 VideoCodecProfile profile,
Fredrik Hubinette3cebc622018-10-27 01:01:1267 const VideoColorSpace& container_color_space)
posciak77118c92016-08-28 13:18:3968 : state_(kNeedStreamMetadata),
Fredrik Hubinette3cebc622018-10-27 01:01:1269 container_color_space_(container_color_space),
Hirokazu Honda7ec32652019-11-22 09:40:2170 // TODO(hiroh): Set profile to UNKNOWN.
71 profile_(profile),
Miguel Casas2d5ecad82018-02-22 19:03:0572 accelerator_(std::move(accelerator)),
Hirokazu Honda7ec32652019-11-22 09:40:2173 parser_(accelerator_->IsFrameContextRequired()) {}
posciakd94b2b082015-09-18 04:03:4074
Miguel Casas2d5ecad82018-02-22 19:03:0575VP9Decoder::~VP9Decoder() = default;
posciakd94b2b082015-09-18 04:03:4076
Hirokazu Honda23132c32019-07-09 14:31:2077void VP9Decoder::SetStream(int32_t id, const DecoderBuffer& decoder_buffer) {
78 const uint8_t* ptr = decoder_buffer.data();
79 const size_t size = decoder_buffer.data_size();
80 const DecryptConfig* decrypt_config = decoder_buffer.decrypt_config();
81
posciakd94b2b082015-09-18 04:03:4082 DCHECK(ptr);
83 DCHECK(size);
Pawel Osciakec6e21b2018-03-19 09:13:0684 DVLOG(4) << "New input stream id: " << id << " at: " << (void*)ptr
85 << " size: " << size;
86 stream_id_ = id;
Ted Meyer0b35c5fd2018-11-27 22:29:2987 if (decrypt_config) {
Hirokazu Honda10df95d2019-07-19 19:31:4388 parser_.SetStream(ptr, size, GetSpatialLayerFrameSize(decoder_buffer),
89 decrypt_config->Clone());
Ted Meyer0b35c5fd2018-11-27 22:29:2990 } else {
Hirokazu Honda10df95d2019-07-19 19:31:4391 parser_.SetStream(ptr, size, GetSpatialLayerFrameSize(decoder_buffer),
92 nullptr);
Ted Meyer0b35c5fd2018-11-27 22:29:2993 }
posciakd94b2b082015-09-18 04:03:4094}
95
96bool VP9Decoder::Flush() {
97 DVLOG(2) << "Decoder flush";
98 Reset();
99 return true;
100}
101
102void VP9Decoder::Reset() {
103 curr_frame_hdr_ = nullptr;
Jeffrey Kardatzke5e36baf2020-11-20 22:17:13104 pending_pic_.reset();
Sreerenj Balachandran1beb5482019-04-03 03:40:05105
106 ref_frames_.Clear();
posciakd94b2b082015-09-18 04:03:40107
108 parser_.Reset();
109
Jose Lopes24d1cda82020-02-21 13:22:36110 if (state_ == kDecoding) {
posciakd94b2b082015-09-18 04:03:40111 state_ = kAfterReset;
Jose Lopes24d1cda82020-02-21 13:22:36112 }
posciakd94b2b082015-09-18 04:03:40113}
114
115VP9Decoder::DecodeResult VP9Decoder::Decode() {
Jeffrey Kardatzke5e36baf2020-11-20 22:17:13116 while (true) {
117 // If we have a pending picture to decode, try that first.
118 if (pending_pic_) {
119 VP9Accelerator::Status status = DecodeAndOutputPicture(pending_pic_);
120 if (status == VP9Accelerator::Status::kFail) {
121 pending_pic_.reset();
122 SetError();
123 return kDecodeError;
124 }
125 if (status == VP9Accelerator::Status::kTryAgain)
126 return kTryAgain;
127
128 pending_pic_.reset();
129 }
130
posciakd94b2b082015-09-18 04:03:40131 // Read a new frame header if one is not awaiting decoding already.
Ted Meyer0b35c5fd2018-11-27 22:29:29132 std::unique_ptr<DecryptConfig> decrypt_config;
posciakd94b2b082015-09-18 04:03:40133 if (!curr_frame_hdr_) {
Hirokazu Honda10df95d2019-07-19 19:31:43134 gfx::Size allocate_size;
xhwang60f96672016-06-14 21:47:53135 std::unique_ptr<Vp9FrameHeader> hdr(new Vp9FrameHeader());
Ted Meyer0b35c5fd2018-11-27 22:29:29136 Vp9Parser::Result res =
Hirokazu Honda10df95d2019-07-19 19:31:43137 parser_.ParseNextFrame(hdr.get(), &allocate_size, &decrypt_config);
posciakd94b2b082015-09-18 04:03:40138 switch (res) {
xhwang60f96672016-06-14 21:47:53139 case Vp9Parser::kOk:
brusi_roy8e7155962016-09-14 18:22:58140 curr_frame_hdr_ = std::move(hdr);
Hirokazu Honda10df95d2019-07-19 19:31:43141 curr_frame_size_ = allocate_size;
posciakd94b2b082015-09-18 04:03:40142 break;
143
xhwang60f96672016-06-14 21:47:53144 case Vp9Parser::kEOStream:
posciakd94b2b082015-09-18 04:03:40145 return kRanOutOfStreamData;
146
xhwang60f96672016-06-14 21:47:53147 case Vp9Parser::kInvalidStream:
posciakd94b2b082015-09-18 04:03:40148 DVLOG(1) << "Error parsing stream";
149 SetError();
150 return kDecodeError;
kcwue5a9a6292016-08-17 03:40:02151
152 case Vp9Parser::kAwaitingRefresh:
posciak77118c92016-08-28 13:18:39153 DVLOG(4) << "Awaiting context update";
154 return kNeedContextUpdate;
posciakd94b2b082015-09-18 04:03:40155 }
156 }
157
158 if (state_ != kDecoding) {
159 // Not kDecoding, so we need a resume point (a keyframe), as we are after
160 // reset or at the beginning of the stream. Drop anything that is not
161 // a keyframe in such case, and continue looking for a keyframe.
Sreerenj Balachandran57163302018-11-28 02:45:57162 // Only exception is when the stream/sequence starts with an Intra only
163 // frame.
164 if (curr_frame_hdr_->IsKeyframe() ||
165 (curr_frame_hdr_->IsIntra() && pic_size_.IsEmpty())) {
posciakd94b2b082015-09-18 04:03:40166 state_ = kDecoding;
167 } else {
168 curr_frame_hdr_.reset();
169 continue;
170 }
171 }
172
173 if (curr_frame_hdr_->show_existing_frame) {
174 // This frame header only instructs us to display one of the
175 // previously-decoded frames, but has no frame data otherwise. Display
176 // and continue decoding subsequent frames.
kcwue5a9a6292016-08-17 03:40:02177 size_t frame_to_show = curr_frame_hdr_->frame_to_show_map_idx;
Sreerenj Balachandran1beb5482019-04-03 03:40:05178 if (frame_to_show >= kVp9NumRefFrames ||
179 !ref_frames_.GetFrame(frame_to_show)) {
posciakd94b2b082015-09-18 04:03:40180 DVLOG(1) << "Request to show an invalid frame";
181 SetError();
182 return kDecodeError;
183 }
184
Chih-Yu Huangcd5181e2018-10-23 06:33:06185 // Duplicate the VP9Picture and set the current bitstream id to keep the
186 // correct timestamp.
Sreerenj Balachandran1beb5482019-04-03 03:40:05187 scoped_refptr<VP9Picture> pic =
188 ref_frames_.GetFrame(frame_to_show)->Duplicate();
Chih-Yu Huangcd5181e2018-10-23 06:33:06189 if (pic == nullptr) {
190 DVLOG(1) << "Failed to duplicate the VP9Picture.";
191 SetError();
192 return kDecodeError;
193 }
194 pic->set_bitstream_id(stream_id_);
195 if (!accelerator_->OutputPicture(std::move(pic))) {
posciakd94b2b082015-09-18 04:03:40196 SetError();
197 return kDecodeError;
198 }
199
200 curr_frame_hdr_.reset();
201 continue;
202 }
203
Hirokazu Honda10df95d2019-07-19 19:31:43204 gfx::Size new_pic_size = curr_frame_size_;
Hirokazu Honda436cf27b2019-05-21 10:44:39205 gfx::Rect new_render_rect(curr_frame_hdr_->render_width,
206 curr_frame_hdr_->render_height);
Hirokazu Hondacffd0ef2020-02-28 03:32:31207 // For safety, check the validity of render size or leave it as pic size.
Hirokazu Honda436cf27b2019-05-21 10:44:39208 if (!gfx::Rect(new_pic_size).Contains(new_render_rect)) {
209 DVLOG(1) << "Render size exceeds picture size. render size: "
210 << new_render_rect.ToString()
211 << ", picture size: " << new_pic_size.ToString();
Hirokazu Hondacffd0ef2020-02-28 03:32:31212 new_render_rect = gfx::Rect(new_pic_size);
Hirokazu Honda436cf27b2019-05-21 10:44:39213 }
Hirokazu Honda7ec32652019-11-22 09:40:21214 VideoCodecProfile new_profile =
215 VP9ProfileToVideoCodecProfile(curr_frame_hdr_->profile);
216 if (new_profile == VIDEO_CODEC_PROFILE_UNKNOWN) {
217 VLOG(1) << "Invalid profile: " << curr_frame_hdr_->profile;
218 return kDecodeError;
219 }
posciakd94b2b082015-09-18 04:03:40220
Hirokazu Honda436cf27b2019-05-21 10:44:39221 DCHECK(!new_pic_size.IsEmpty());
Hirokazu Honda7ec32652019-11-22 09:40:21222 if (new_pic_size != pic_size_ || new_profile != profile_) {
223 DVLOG(1) << "New profile: " << GetProfileName(new_profile)
224 << ", New resolution: " << new_pic_size.ToString();
posciakd94b2b082015-09-18 04:03:40225
Sreerenj Balachandran57163302018-11-28 02:45:57226 if (!curr_frame_hdr_->IsKeyframe() &&
Hirokazu Honda7ec32652019-11-22 09:40:21227 !(curr_frame_hdr_->IsIntra() && pic_size_.IsEmpty())) {
posciakd94b2b082015-09-18 04:03:40228 // TODO(posciak): This is doable, but requires a few modifications to
229 // VDA implementations to allow multiple picture buffer sets in flight.
Dongseong Hwang4f20ebb2018-06-07 00:28:20230 // http://crbug.com/832264
Sreerenj Balachandran57163302018-11-28 02:45:57231 DVLOG(1) << "Resolution change currently supported for keyframes and "
232 "sequence begins with Intra only when there is no prior "
233 "frames in the context";
Dongseong Hwang4f20ebb2018-06-07 00:28:20234 if (++size_change_failure_counter_ > kVPxMaxNumOfSizeChangeFailures) {
235 SetError();
236 return kDecodeError;
237 }
238
239 curr_frame_hdr_.reset();
240 return kRanOutOfStreamData;
posciakd94b2b082015-09-18 04:03:40241 }
242
243 // TODO(posciak): This requires us to be on a keyframe (see above) and is
244 // required, because VDA clients expect all surfaces to be returned before
Hirokazu Honda7ec32652019-11-22 09:40:21245 // they can cycle surface sets after receiving kConfigChange.
posciakd94b2b082015-09-18 04:03:40246 // This is only an implementation detail of VDAs and can be improved.
Sreerenj Balachandran1beb5482019-04-03 03:40:05247 ref_frames_.Clear();
posciakd94b2b082015-09-18 04:03:40248
249 pic_size_ = new_pic_size;
Hirokazu Honda436cf27b2019-05-21 10:44:39250 visible_rect_ = new_render_rect;
Hirokazu Honda7ec32652019-11-22 09:40:21251 profile_ = new_profile;
Dongseong Hwang4f20ebb2018-06-07 00:28:20252 size_change_failure_counter_ = 0;
Hirokazu Honda7ec32652019-11-22 09:40:21253 return kConfigChange;
posciakd94b2b082015-09-18 04:03:40254 }
255
Jeffrey Kardatzke5e36baf2020-11-20 22:17:13256 pending_pic_ = accelerator_->CreateVP9Picture();
257 if (!pending_pic_) {
posciakd94b2b082015-09-18 04:03:40258 return kRanOutOfSurfaces;
Jose Lopes24d1cda82020-02-21 13:22:36259 }
johnylin74410872017-06-19 13:05:30260 DVLOG(2) << "Render resolution: " << new_render_rect.ToString();
261
Jeffrey Kardatzke5e36baf2020-11-20 22:17:13262 pending_pic_->set_visible_rect(new_render_rect);
263 pending_pic_->set_bitstream_id(stream_id_);
Fredrik Hubinette3cebc622018-10-27 01:01:12264
Jeffrey Kardatzke5e36baf2020-11-20 22:17:13265 pending_pic_->set_decrypt_config(std::move(decrypt_config));
Ted Meyer0b35c5fd2018-11-27 22:29:29266
Fredrik Hubinette3cebc622018-10-27 01:01:12267 // For VP9, container color spaces override video stream color spaces.
Jeffrey Kardatzke5e36baf2020-11-20 22:17:13268 if (container_color_space_.IsSpecified())
269 pending_pic_->set_colorspace(container_color_space_);
270 else if (curr_frame_hdr_)
271 pending_pic_->set_colorspace(curr_frame_hdr_->GetColorSpace());
posciakd94b2b082015-09-18 04:03:40272
Jeffrey Kardatzke5e36baf2020-11-20 22:17:13273 pending_pic_->frame_hdr = std::move(curr_frame_hdr_);
274
275 VP9Accelerator::Status status = DecodeAndOutputPicture(pending_pic_);
276 if (status == VP9Accelerator::Status::kFail) {
277 pending_pic_.reset();
posciakd94b2b082015-09-18 04:03:40278 SetError();
279 return kDecodeError;
280 }
Jeffrey Kardatzke5e36baf2020-11-20 22:17:13281 if (status == VP9Accelerator::Status::kTryAgain)
282 return kTryAgain;
283
284 pending_pic_.reset();
posciakd94b2b082015-09-18 04:03:40285 }
286}
287
posciak77118c92016-08-28 13:18:39288void VP9Decoder::UpdateFrameContext(
Ted Meyer4fac4f62019-05-08 22:57:15289 scoped_refptr<VP9Picture> pic,
Jose Lopes24d1cda82020-02-21 13:22:36290 Vp9Parser::ContextRefreshCallback context_refresh_cb) {
Dale Curtise25163812018-09-21 22:13:39291 DCHECK(context_refresh_cb);
posciak77118c92016-08-28 13:18:39292 Vp9FrameContext frame_ctx;
293 memset(&frame_ctx, 0, sizeof(frame_ctx));
294
Ted Meyer4fac4f62019-05-08 22:57:15295 if (!accelerator_->GetFrameContext(std::move(pic), &frame_ctx)) {
posciak77118c92016-08-28 13:18:39296 SetError();
297 return;
298 }
299
Jose Lopes24d1cda82020-02-21 13:22:36300 std::move(context_refresh_cb).Run(frame_ctx);
posciak77118c92016-08-28 13:18:39301}
302
Jeffrey Kardatzke5e36baf2020-11-20 22:17:13303VP9Decoder::VP9Accelerator::Status VP9Decoder::DecodeAndOutputPicture(
304 scoped_refptr<VP9Picture> pic) {
posciakd94b2b082015-09-18 04:03:40305 DCHECK(!pic_size_.IsEmpty());
306 DCHECK(pic->frame_hdr);
307
Chih-Yu Huangcc6b1b92019-12-19 08:57:16308 base::OnceClosure done_cb;
Jose Lopes24d1cda82020-02-21 13:22:36309 Vp9Parser::ContextRefreshCallback context_refresh_cb =
posciak77118c92016-08-28 13:18:39310 parser_.GetContextRefreshCb(pic->frame_hdr->frame_context_idx);
Jose Lopes24d1cda82020-02-21 13:22:36311 if (context_refresh_cb) {
312 done_cb =
313 base::BindOnce(&VP9Decoder::UpdateFrameContext, base::Unretained(this),
314 pic, std::move(context_refresh_cb));
315 }
posciak77118c92016-08-28 13:18:39316
317 const Vp9Parser::Context& context = parser_.context();
Jeffrey Kardatzke5e36baf2020-11-20 22:17:13318 VP9Accelerator::Status status = accelerator_->SubmitDecode(
319 pic, context.segmentation(), context.loop_filter(), ref_frames_,
320 std::move(done_cb));
321 if (status != VP9Accelerator::Status::kOk)
322 return status;
posciakd94b2b082015-09-18 04:03:40323
324 if (pic->frame_hdr->show_frame) {
Jeffrey Kardatzke5e36baf2020-11-20 22:17:13325 if (!accelerator_->OutputPicture(pic))
326 return VP9Accelerator::Status::kFail;
posciakd94b2b082015-09-18 04:03:40327 }
328
Ted Meyer4fac4f62019-05-08 22:57:15329 ref_frames_.Refresh(std::move(pic));
Jeffrey Kardatzke5e36baf2020-11-20 22:17:13330 return status;
posciakd94b2b082015-09-18 04:03:40331}
332
333void VP9Decoder::SetError() {
334 Reset();
335 state_ = kError;
336}
337
338gfx::Size VP9Decoder::GetPicSize() const {
339 return pic_size_;
340}
341
Hirokazu Honda436cf27b2019-05-21 10:44:39342gfx::Rect VP9Decoder::GetVisibleRect() const {
343 return visible_rect_;
344}
345
Hirokazu Honda7ec32652019-11-22 09:40:21346VideoCodecProfile VP9Decoder::GetProfile() const {
347 return profile_;
348}
349
posciakd94b2b082015-09-18 04:03:40350size_t VP9Decoder::GetRequiredNumOfPictures() const {
Miguel Casas477a7062019-01-04 19:13:45351 constexpr size_t kPicsInPipeline = limits::kMaxVideoFrames + 1;
352 return kPicsInPipeline + GetNumReferenceFrames();
353}
354
355size_t VP9Decoder::GetNumReferenceFrames() const {
Miguel Casas3dd7e562019-02-14 17:38:46356 // Maximum number of reference frames
357 return kVp9NumRefFrames;
posciakd94b2b082015-09-18 04:03:40358}
359
markdittmer6e70beb82016-05-02 05:40:47360} // namespace media