[go: nahoru, domu]

blob: b24f2466846e28485dd17b23372c2a57e0deb302 [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
20VP9Decoder::VP9Decoder(VP9Accelerator* accelerator)
posciak77118c92016-08-28 13:18:3921 : state_(kNeedStreamMetadata),
22 accelerator_(accelerator),
23 parser_(accelerator->IsFrameContextRequired()) {
xhwang60f96672016-06-14 21:47:5324 ref_frames_.resize(kVp9NumRefFrames);
posciakd94b2b082015-09-18 04:03:4025}
26
27VP9Decoder::~VP9Decoder() {}
28
29void VP9Decoder::SetStream(const uint8_t* ptr, size_t size) {
30 DCHECK(ptr);
31 DCHECK(size);
32
33 DVLOG(4) << "New input stream at: " << (void*)ptr << " size: " << size;
34 parser_.SetStream(ptr, size);
35}
36
37bool VP9Decoder::Flush() {
38 DVLOG(2) << "Decoder flush";
39 Reset();
40 return true;
41}
42
43void VP9Decoder::Reset() {
44 curr_frame_hdr_ = nullptr;
45 for (auto& ref_frame : ref_frames_)
46 ref_frame = nullptr;
47
48 parser_.Reset();
49
50 if (state_ == kDecoding)
51 state_ = kAfterReset;
52}
53
54VP9Decoder::DecodeResult VP9Decoder::Decode() {
55 while (1) {
56 // Read a new frame header if one is not awaiting decoding already.
57 if (!curr_frame_hdr_) {
xhwang60f96672016-06-14 21:47:5358 std::unique_ptr<Vp9FrameHeader> hdr(new Vp9FrameHeader());
posciak77118c92016-08-28 13:18:3959 Vp9Parser::Result res = parser_.ParseNextFrame(hdr.get());
posciakd94b2b082015-09-18 04:03:4060 switch (res) {
xhwang60f96672016-06-14 21:47:5361 case Vp9Parser::kOk:
brusi_roy8e7155962016-09-14 18:22:5862 curr_frame_hdr_ = std::move(hdr);
posciakd94b2b082015-09-18 04:03:4063 break;
64
xhwang60f96672016-06-14 21:47:5365 case Vp9Parser::kEOStream:
posciakd94b2b082015-09-18 04:03:4066 return kRanOutOfStreamData;
67
xhwang60f96672016-06-14 21:47:5368 case Vp9Parser::kInvalidStream:
posciakd94b2b082015-09-18 04:03:4069 DVLOG(1) << "Error parsing stream";
70 SetError();
71 return kDecodeError;
kcwue5a9a6292016-08-17 03:40:0272
73 case Vp9Parser::kAwaitingRefresh:
posciak77118c92016-08-28 13:18:3974 DVLOG(4) << "Awaiting context update";
75 return kNeedContextUpdate;
posciakd94b2b082015-09-18 04:03:4076 }
77 }
78
79 if (state_ != kDecoding) {
80 // Not kDecoding, so we need a resume point (a keyframe), as we are after
81 // reset or at the beginning of the stream. Drop anything that is not
82 // a keyframe in such case, and continue looking for a keyframe.
83 if (curr_frame_hdr_->IsKeyframe()) {
84 state_ = kDecoding;
85 } else {
86 curr_frame_hdr_.reset();
87 continue;
88 }
89 }
90
91 if (curr_frame_hdr_->show_existing_frame) {
92 // This frame header only instructs us to display one of the
93 // previously-decoded frames, but has no frame data otherwise. Display
94 // and continue decoding subsequent frames.
kcwue5a9a6292016-08-17 03:40:0295 size_t frame_to_show = curr_frame_hdr_->frame_to_show_map_idx;
posciakd94b2b082015-09-18 04:03:4096 if (frame_to_show >= ref_frames_.size() || !ref_frames_[frame_to_show]) {
97 DVLOG(1) << "Request to show an invalid frame";
98 SetError();
99 return kDecodeError;
100 }
101
102 if (!accelerator_->OutputPicture(ref_frames_[frame_to_show])) {
103 SetError();
104 return kDecodeError;
105 }
106
107 curr_frame_hdr_.reset();
108 continue;
109 }
110
kcwue5a9a6292016-08-17 03:40:02111 gfx::Size new_pic_size(curr_frame_hdr_->frame_width,
112 curr_frame_hdr_->frame_height);
posciakd94b2b082015-09-18 04:03:40113 DCHECK(!new_pic_size.IsEmpty());
114
115 if (new_pic_size != pic_size_) {
116 DVLOG(1) << "New resolution: " << new_pic_size.ToString();
117
118 if (!curr_frame_hdr_->IsKeyframe()) {
119 // TODO(posciak): This is doable, but requires a few modifications to
120 // VDA implementations to allow multiple picture buffer sets in flight.
121 DVLOG(1) << "Resolution change currently supported for keyframes only";
122 SetError();
123 return kDecodeError;
124 }
125
126 // TODO(posciak): This requires us to be on a keyframe (see above) and is
127 // required, because VDA clients expect all surfaces to be returned before
128 // they can cycle surface sets after receiving kAllocateNewSurfaces.
129 // This is only an implementation detail of VDAs and can be improved.
130 for (auto& ref_frame : ref_frames_)
131 ref_frame = nullptr;
132
133 pic_size_ = new_pic_size;
134 return kAllocateNewSurfaces;
135 }
136
137 scoped_refptr<VP9Picture> pic = accelerator_->CreateVP9Picture();
138 if (!pic)
139 return kRanOutOfSurfaces;
140
141 pic->frame_hdr.reset(curr_frame_hdr_.release());
142
143 if (!DecodeAndOutputPicture(pic)) {
144 SetError();
145 return kDecodeError;
146 }
147 }
148}
149
150void VP9Decoder::RefreshReferenceFrames(const scoped_refptr<VP9Picture>& pic) {
xhwang60f96672016-06-14 21:47:53151 for (size_t i = 0; i < kVp9NumRefFrames; ++i) {
vmpstracfa9222015-10-28 22:42:23152 DCHECK(!pic->frame_hdr->IsKeyframe() || pic->frame_hdr->RefreshFlag(i));
posciakd94b2b082015-09-18 04:03:40153 if (pic->frame_hdr->RefreshFlag(i))
154 ref_frames_[i] = pic;
155 }
156}
157
posciak77118c92016-08-28 13:18:39158void VP9Decoder::UpdateFrameContext(
159 const scoped_refptr<VP9Picture>& pic,
160 const base::Callback<void(const Vp9FrameContext&)>& context_refresh_cb) {
161 DCHECK(!context_refresh_cb.is_null());
162 Vp9FrameContext frame_ctx;
163 memset(&frame_ctx, 0, sizeof(frame_ctx));
164
165 if (!accelerator_->GetFrameContext(pic, &frame_ctx)) {
166 SetError();
167 return;
168 }
169
170 context_refresh_cb.Run(frame_ctx);
171}
172
posciakd94b2b082015-09-18 04:03:40173bool VP9Decoder::DecodeAndOutputPicture(scoped_refptr<VP9Picture> pic) {
174 DCHECK(!pic_size_.IsEmpty());
175 DCHECK(pic->frame_hdr);
176
posciak77118c92016-08-28 13:18:39177 base::Closure done_cb;
178 const auto& context_refresh_cb =
179 parser_.GetContextRefreshCb(pic->frame_hdr->frame_context_idx);
180 if (!context_refresh_cb.is_null())
181 done_cb = base::Bind(&VP9Decoder::UpdateFrameContext,
182 base::Unretained(this), pic, context_refresh_cb);
183
184 const Vp9Parser::Context& context = parser_.context();
185 if (!accelerator_->SubmitDecode(pic, context.segmentation(),
186 context.loop_filter(), ref_frames_, done_cb))
posciakd94b2b082015-09-18 04:03:40187 return false;
188
189 if (pic->frame_hdr->show_frame) {
190 if (!accelerator_->OutputPicture(pic))
191 return false;
192 }
193
194 RefreshReferenceFrames(pic);
195 return true;
196}
197
198void VP9Decoder::SetError() {
199 Reset();
200 state_ = kError;
201}
202
203gfx::Size VP9Decoder::GetPicSize() const {
204 return pic_size_;
205}
206
207size_t VP9Decoder::GetRequiredNumOfPictures() const {
208 // kMaxVideoFrames to keep higher level media pipeline populated, +2 for the
209 // pictures being parsed and decoded currently.
xhwang60f96672016-06-14 21:47:53210 return limits::kMaxVideoFrames + kVp9NumRefFrames + 2;
posciakd94b2b082015-09-18 04:03:40211}
212
markdittmer6e70beb82016-05-02 05:40:47213} // namespace media