OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 | |
5 #include "base/callback_helpers.h" | |
6 #include "base/logging.h" | |
7 #include "base/run_loop.h" | |
8 #include "chrome/gpu/arc_gpu_video_decode_accelerator.h" | |
9 | |
10 // TODO use Pawel's factory instead | |
11 #include "content/public/common/create_vda.h" | |
12 | |
13 #undef DVLOG | |
14 #define DVLOG VLOG | |
15 | |
16 namespace chromeos { | |
17 namespace arc { | |
18 | |
19 ArcGpuVideoDecodeAccelerator::InputRecord::InputRecord( | |
20 int32_t bitstream_buffer_id, | |
21 uint32_t buffer_index, | |
22 int64_t timestamp) | |
23 : bitstream_buffer_id(bitstream_buffer_id), | |
24 buffer_index(buffer_index), | |
25 timestamp(timestamp) {} | |
26 | |
27 ArcGpuVideoDecodeAccelerator::InputBufferInfo::InputBufferInfo() | |
28 : offset(0), length(0) {} | |
29 | |
30 ArcGpuVideoDecodeAccelerator::InputBufferInfo::InputBufferInfo( | |
31 InputBufferInfo&& a) { | |
32 handle = std::move(a.handle); | |
kcwu
2016/03/09 07:58:21
ArcGpuVideoDecodeAccelerator::InputBufferInfo::Inp
Owen Lin
2016/03/14 08:46:59
Removed.
| |
33 offset = a.offset; | |
34 length = a.length; | |
35 } | |
36 | |
37 ArcGpuVideoDecodeAccelerator::InputBufferInfo::~InputBufferInfo() {} | |
38 | |
39 ArcGpuVideoDecodeAccelerator::ArcGpuVideoDecodeAccelerator( | |
40 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | |
41 : pending_eos_output_buffer_(false), | |
42 arc_client_(nullptr), | |
43 next_bitstream_buffer_id_(0), | |
44 io_task_runner_(io_task_runner), | |
45 output_buffer_size_(0) {} | |
46 | |
47 ArcGpuVideoDecodeAccelerator::~ArcGpuVideoDecodeAccelerator() {} | |
48 | |
49 static const size_t MAX_BUFFER_COUNT = 128; | |
kcwu
2016/03/09 07:58:21
why 128?
Is the same reason as kMaxNumberOfInputR
kcwu
2016/03/10 07:27:26
and s/MAX_BUFFER_COUNT/kMaxBufferCount/
Owen Lin
2016/03/14 08:46:59
Done.
Owen Lin
2016/03/14 08:46:59
Not the exactly same purpose but it is also arbitr
| |
50 | |
51 bool ArcGpuVideoDecodeAccelerator::Initialize( | |
52 const Config& config, | |
53 ArcVideoAccelerator::Client* client) { | |
54 DVLOG(5) << "Initialize(device=" << config.device_type | |
55 << ", input_pixel_format=" << config.input_pixel_format | |
56 << ", num_input_buffers=" << config.num_input_buffers << ")"; | |
57 DCHECK(thread_checker_.CalledOnValidThread()); | |
58 if (config.device_type != DEVICE_DECODER) | |
59 return false; | |
60 DCHECK(client); | |
61 DCHECK(!arc_client_); | |
62 arc_client_ = client; | |
63 | |
64 vda_.reset(content::CreateVDA(AsWeakPtr(), io_task_runner_)); | |
kcwu
2016/03/09 07:58:21
base::ThreadTaskRunnerHandle::Get().
Since you exp
Owen Lin
2016/03/14 08:46:59
io_task_runner_ is no longer required. Removed.
| |
65 if (!vda_) { | |
66 DLOG(ERROR) << "Failed to create VDA."; | |
67 return false; | |
68 } | |
69 | |
70 if (config.num_input_buffers > MAX_BUFFER_COUNT) { | |
71 DLOG(ERROR) << "Request too many buffers: " << config.num_input_buffers; | |
72 return false; | |
73 } | |
74 input_buffer_info_.resize(config.num_input_buffers); | |
75 | |
76 media::VideoDecodeAccelerator::Config vda_config; | |
77 switch (config.input_pixel_format) { | |
78 case HAL_PIXEL_FORMAT_H264: | |
79 vda_config.profile = media::H264PROFILE_MAIN; | |
80 break; | |
81 case HAL_PIXEL_FORMAT_VP8: | |
82 vda_config.profile = media::VP8PROFILE_ANY; | |
83 break; | |
84 default: | |
85 DLOG(ERROR) << "Unsupported input format: " << config.input_pixel_format; | |
86 return false; | |
87 } | |
88 vda_config.output_mode = | |
89 media::VideoDecodeAccelerator::Config::OutputMode::IMPORT; | |
90 if (!vda_->Initialize(vda_config, this)) { | |
91 vda_.reset(); | |
92 DLOG(ERROR) << "VDA::Initialize() failed."; | |
93 return false; | |
94 } | |
95 return true; | |
96 } | |
97 | |
98 void ArcGpuVideoDecodeAccelerator::SetNumberOfOutputBuffers(size_t number) { | |
99 DVLOG(5) << "SetNumberOfOutputBuffers(" << number << ")"; | |
100 DCHECK(thread_checker_.CalledOnValidThread()); | |
101 | |
102 if (number >= MAX_BUFFER_COUNT) { | |
kcwu
2016/03/09 07:58:21
> instead of >= ?
Owen Lin
2016/03/14 08:46:59
Done.
| |
103 DLOG(ERROR) << "Too many buffers: " << number; | |
104 arc_client_->OnError(INVALID_ARGUMENT); | |
105 return; | |
106 } | |
107 | |
108 std::vector<media::PictureBuffer> buffers; | |
109 for (int32_t id = 0, n = number; id < n; ++id) { | |
110 // TODO: Make sure the |coded_size| is what we want. | |
111 buffers.push_back(media::PictureBuffer(id, coded_size_, 0)); | |
112 } | |
113 vda_->AssignPictureBuffers(buffers); | |
114 | |
115 pending_import_buffer_.clear(); | |
116 pending_import_buffer_.resize(number); | |
117 } | |
118 | |
119 void ArcGpuVideoDecodeAccelerator::BindSharedMemory(PortType port, | |
120 uint32_t index, | |
121 int ashmem_fd, | |
122 off_t offset, | |
123 size_t length) { | |
124 DVLOG(5) << "ArcGVDA::BindSharedMemory, offset: " << offset | |
125 << ", length: " << length; | |
126 DCHECK(thread_checker_.CalledOnValidThread()); | |
127 | |
128 // Make sure we will close the file descriptor. | |
129 base::ScopedFD handle(ashmem_fd); | |
130 if (!ValidatePortAndIndex(port, index)) { | |
131 arc_client_->OnError(INVALID_ARGUMENT); | |
132 return; | |
133 } | |
134 if (port != PORT_INPUT) { | |
kcwu
2016/03/09 07:58:21
How about move this check to line 130 (before Vali
Owen Lin
2016/03/14 08:46:59
Done.
| |
135 DLOG(ERROR) << "SharedBuffer is only supported for input"; | |
136 arc_client_->OnError(INVALID_ARGUMENT); | |
137 return; | |
138 } | |
139 InputBufferInfo* input_info = &input_buffer_info_[index]; | |
140 input_info->handle = std::move(handle); | |
141 input_info->offset = offset; | |
142 input_info->length = length; | |
143 } | |
144 | |
145 void ArcGpuVideoDecodeAccelerator::BindDmabuf(PortType port, | |
146 uint32_t index, | |
147 int dmabuf_fd) { | |
148 DCHECK(thread_checker_.CalledOnValidThread()); | |
149 | |
150 // Make sure we will close the file descriptor. | |
151 base::ScopedFD handle(dmabuf_fd); | |
152 if (!ValidatePortAndIndex(port, index)) { | |
153 arc_client_->OnError(INVALID_ARGUMENT); | |
154 return; | |
155 } | |
156 if (port != PORT_OUTPUT) { | |
kcwu
2016/03/09 07:58:21
How about move this check to line 152 (before Vali
Owen Lin
2016/03/14 08:46:59
Done.
| |
157 DLOG(ERROR) << "GraphicBuffer is only supported for input"; | |
158 arc_client_->OnError(INVALID_ARGUMENT); | |
159 return; | |
160 } | |
161 pending_import_buffer_[index] = std::move(handle); | |
162 } | |
163 | |
164 void ArcGpuVideoDecodeAccelerator::UseBuffer(PortType port, | |
165 uint32_t index, | |
166 const BufferMetadata& metadata) { | |
167 DVLOG(5) << "UseBuffer(port=" << port << ", index=" << index | |
168 << ", metadata=(bytes_used=" << metadata.bytes_used | |
169 << ", timestamp=" << metadata.timestamp << "))"; | |
kcwu
2016/03/07 14:04:33
extra )
Owen Lin
2016/03/14 08:46:59
Done.
| |
170 DCHECK(thread_checker_.CalledOnValidThread()); | |
171 if (!ValidatePortAndIndex(port, index)) { | |
172 arc_client_->OnError(INVALID_ARGUMENT); | |
173 return; | |
174 } | |
175 switch (port) { | |
176 case PORT_INPUT: { | |
177 InputBufferInfo* input_info = &input_buffer_info_[index]; | |
178 if (metadata.flags & BUFFER_FLAG_EOS) { | |
179 // TODO(owenlin): Ask VDA to return all output pictures so that we | |
180 // can output an EOS picture when Flush() is done. | |
181 vda_->Flush(); | |
kcwu
2016/03/09 07:58:21
I'm not sure. Should this move to line 196 or retu
Owen Lin
2016/03/14 08:46:59
Move to line 196. We need to send the content to b
| |
182 } | |
183 int32_t bitstream_buffer_id = next_bitstream_buffer_id_; | |
184 // Mask against 30 bits, to avoid (undefined wraparound on signed integer) | |
kcwu
2016/03/09 07:58:21
s/(undefined wraparound on signed integer)/(undefi
Owen Lin
2016/03/14 08:46:59
Done.
| |
185 next_bitstream_buffer_id_ = (next_bitstream_buffer_id_ + 1) & 0x3FFFFFFF; | |
186 SetInputRecord(bitstream_buffer_id, index, metadata.timestamp); | |
187 int dup_fd = HANDLE_EINTR(dup(input_info->handle.get())); | |
188 if (dup_fd < 0) { | |
189 DLOG(ERROR) << "dup() failed."; | |
190 arc_client_->OnError(PLATFORM_FAILURE); | |
191 return; | |
192 } | |
193 vda_->Decode(media::BitstreamBuffer( | |
194 bitstream_buffer_id, base::SharedMemoryHandle(dup_fd, true), | |
195 metadata.bytes_used, input_info->offset)); | |
196 break; | |
197 } | |
198 case PORT_OUTPUT: { | |
199 SendEosIfNeededOrReusePicture(index); | |
200 break; | |
201 } | |
202 default: | |
203 NOTREACHED(); | |
204 } | |
205 } | |
206 | |
207 void ArcGpuVideoDecodeAccelerator::Reset() { | |
208 DCHECK(thread_checker_.CalledOnValidThread()); | |
209 base::RunLoop loop; | |
210 reset_done_callback_ = loop.QuitClosure(); | |
211 DCHECK(vda_); | |
212 vda_->Reset(); | |
213 base::MessageLoop::ScopedNestableTaskAllower allow( | |
214 base::MessageLoop::current()); | |
215 // Wait for the ResetDone callback. | |
216 loop.Run(); | |
217 } | |
218 | |
219 void ArcGpuVideoDecodeAccelerator::ProvidePictureBuffers( | |
220 size_t requested_num_of_buffers, | |
221 const gfx::Size& dimensions, | |
222 uint32_t texture_target) { | |
223 DVLOG(5) << "ProvidePictureBuffers(" | |
224 << "requested_num_of_buffers=" << requested_num_of_buffers | |
225 << ", dimensions=" << dimensions.ToString() << ")"; | |
226 DCHECK(thread_checker_.CalledOnValidThread()); | |
227 coded_size_ = dimensions; | |
228 // TODO(owenlin): use VDA::GetOutputFormat() here and calculate correct | |
229 // |image_size|. | |
230 VideoFormat video_format; | |
231 video_format.image_size = dimensions.GetArea() * 3 / 2; | |
232 output_buffer_size_ = video_format.image_size; | |
233 video_format.min_num_buffers = requested_num_of_buffers; | |
234 video_format.coded_width = dimensions.width(); | |
235 video_format.coded_height = dimensions.height(); | |
236 // TODO(owenlin): How to get visible size? | |
237 video_format.crop_top = 0; | |
238 video_format.crop_left = 0; | |
239 video_format.crop_width = dimensions.width(); | |
240 video_format.crop_height = dimensions.height(); | |
241 arc_client_->OnOutputFormatChanged(video_format); | |
242 } | |
243 | |
244 void ArcGpuVideoDecodeAccelerator::DismissPictureBuffer( | |
245 int32_t picture_buffer) { | |
246 // no-op | |
kcwu
2016/03/09 07:58:21
why can it be no-op?
Owen Lin
2016/03/14 08:46:59
VDA has its owned FDs of the picture buffers and w
| |
247 } | |
248 | |
249 void ArcGpuVideoDecodeAccelerator::PictureReady(const media::Picture& picture) { | |
250 DVLOG(5) << "PictureReady(picture_buffer_id=" << picture.picture_buffer_id() | |
251 << ", bitstream_buffer_id=" << picture.bitstream_buffer_id(); | |
252 DCHECK(thread_checker_.CalledOnValidThread()); | |
253 | |
254 // Empty buffer, returned in Flushing. | |
255 if (picture.bitstream_buffer_id() == -1) { | |
kcwu
2016/03/09 07:58:21
where is "-1" coming from?
I don't find it in this
Owen Lin
2016/03/14 08:46:59
It would be defined in the new Flush(bool return_b
| |
256 buffers_pending_eos_.push(picture.picture_buffer_id()); | |
257 return; | |
258 } | |
259 InputRecord* input_record = FindInputRecord(picture.bitstream_buffer_id()); | |
260 if (input_record == nullptr) { | |
261 DLOG(ERROR) << "Cannot find for bitstream buffer id: " | |
262 << picture.bitstream_buffer_id(); | |
263 arc_client_->OnError(PLATFORM_FAILURE); | |
264 return; | |
265 } | |
266 | |
267 BufferMetadata metadata; | |
268 metadata.timestamp = input_record->timestamp; | |
269 metadata.bytes_used = output_buffer_size_; | |
270 arc_client_->OnBufferDone(PORT_OUTPUT, picture.picture_buffer_id(), metadata); | |
271 } | |
272 | |
273 void ArcGpuVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer( | |
274 int32_t bitstream_buffer_id) { | |
275 DVLOG(5) << "NotifyEndOfBitstreamBuffer(" << bitstream_buffer_id << ")"; | |
276 DCHECK(thread_checker_.CalledOnValidThread()); | |
277 InputRecord* input_record = FindInputRecord(bitstream_buffer_id); | |
278 if (input_record == nullptr) { | |
279 arc_client_->OnError(PLATFORM_FAILURE); | |
280 return; | |
281 } | |
282 arc_client_->OnBufferDone(PORT_INPUT, input_record->buffer_index, | |
283 BufferMetadata()); | |
284 } | |
285 | |
286 void ArcGpuVideoDecodeAccelerator::NotifyFlushDone() { | |
287 DCHECK(thread_checker_.CalledOnValidThread()); | |
288 pending_eos_output_buffer_ = true; | |
289 while (!buffers_pending_eos_.empty()) { | |
290 SendEosIfNeededOrReusePicture(buffers_pending_eos_.front()); | |
291 buffers_pending_eos_.pop(); | |
292 } | |
293 } | |
294 | |
295 void ArcGpuVideoDecodeAccelerator::NotifyResetDone() { | |
296 DCHECK(thread_checker_.CalledOnValidThread()); | |
297 base::ResetAndReturn(&reset_done_callback_).Run(); | |
298 } | |
299 | |
300 static ArcVideoAccelerator::Error ConvertErrorCode( | |
301 media::VideoDecodeAccelerator::Error error) { | |
302 switch (error) { | |
303 case media::VideoDecodeAccelerator::ILLEGAL_STATE: | |
304 return ArcVideoAccelerator::ILLEGAL_STATE; | |
305 case media::VideoDecodeAccelerator::INVALID_ARGUMENT: | |
306 return ArcVideoAccelerator::INVALID_ARGUMENT; | |
307 case media::VideoDecodeAccelerator::UNREADABLE_INPUT: | |
308 return ArcVideoAccelerator::UNREADABLE_INPUT; | |
309 case media::VideoDecodeAccelerator::PLATFORM_FAILURE: | |
310 return ArcVideoAccelerator::PLATFORM_FAILURE; | |
311 default: | |
312 DLOG(ERROR) << "Unknown error: " << error; | |
313 return ArcVideoAccelerator::PLATFORM_FAILURE; | |
314 } | |
315 } | |
316 | |
317 void ArcGpuVideoDecodeAccelerator::NotifyError( | |
318 media::VideoDecodeAccelerator::Error error) { | |
319 DCHECK(thread_checker_.CalledOnValidThread()); | |
320 arc_client_->OnError(ConvertErrorCode(error)); | |
321 } | |
322 | |
323 void ArcGpuVideoDecodeAccelerator::SendEosIfNeededOrReusePicture( | |
324 uint32_t index) { | |
325 if (pending_eos_output_buffer_) { | |
326 BufferMetadata metadata; | |
327 metadata.flags = BUFFER_FLAG_EOS; | |
328 arc_client_->OnBufferDone(PORT_OUTPUT, index, metadata); | |
329 pending_eos_output_buffer_ = false; | |
330 } else { | |
331 if (pending_import_buffer_[index].is_valid()) { | |
332 std::vector<base::ScopedFD> dmabuf_fds; | |
333 dmabuf_fds.push_back(std::move(pending_import_buffer_[index])); | |
334 vda_->ImportBufferForPicture(index, std::move(dmabuf_fds)); | |
335 DCHECK(!pending_import_buffer_[index].is_valid()); | |
336 } else { | |
337 vda_->ReusePictureBuffer(index); | |
338 } | |
339 } | |
340 } | |
341 | |
342 void ArcGpuVideoDecodeAccelerator::SetInputRecord(int32_t bitstream_buffer_id, | |
343 uint32_t buffer_index, | |
344 int64_t timestamp) { | |
345 input_records_.push_front( | |
346 InputRecord(bitstream_buffer_id, buffer_index, timestamp)); | |
347 // The same value copied from media::GpuVideoDecoder. | |
348 const size_t kMaxNumberOfInputRecords = 128; | |
349 if (input_records_.size() > kMaxNumberOfInputRecords) | |
350 input_records_.pop_back(); | |
351 } | |
352 | |
353 ArcGpuVideoDecodeAccelerator::InputRecord* | |
354 ArcGpuVideoDecodeAccelerator::FindInputRecord(int32_t bitstream_buffer_id) { | |
355 for (auto& record : input_records_) { | |
356 if (record.bitstream_buffer_id == bitstream_buffer_id) | |
357 return &record; | |
358 } | |
359 return nullptr; | |
360 } | |
361 | |
362 bool ArcGpuVideoDecodeAccelerator::ValidatePortAndIndex(PortType port, | |
363 uint32_t index) { | |
364 switch (port) { | |
365 case PORT_INPUT: | |
366 if (index >= input_buffer_info_.size()) { | |
367 DLOG(ERROR) << "Invalid index: " << index; | |
368 return false; | |
369 } | |
370 return true; | |
371 case PORT_OUTPUT: | |
372 if (index >= pending_import_buffer_.size()) { | |
373 DLOG(ERROR) << "Invalid index: " << index; | |
374 return false; | |
375 } | |
376 return true; | |
377 default: | |
378 DLOG(ERROR) << "Invalid port: " << port; | |
379 return false; | |
380 } | |
381 } | |
382 | |
383 } // namespace arc | |
384 } // namespace chromeos | |
OLD | NEW |