OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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 <dlfcn.h> | |
6 #include <errno.h> | |
7 #include <fcntl.h> | |
8 #include <linux/videodev2.h> | |
9 #include <poll.h> | |
10 #include <sys/eventfd.h> | |
11 #include <sys/ioctl.h> | |
12 #include <sys/mman.h> | |
13 | |
14 #include "base/bind.h" | |
15 #include "base/debug/trace_event.h" | |
16 #include "base/message_loop.h" | |
17 #include "base/message_loop_proxy.h" | |
18 #include "base/shared_memory.h" | |
19 #include "content/common/gpu/media/exynos_video_decode_accelerator.h" | |
20 #include "content/common/gpu/media/h264_parser.h" | |
21 #include "third_party/angle/include/GLES2/gl2.h" | |
22 | |
23 namespace content { | |
24 | |
25 #define EXYNOS_MFC_DEVICE "/dev/mfc-dec" | |
26 #define EXYNOS_GSC_DEVICE "/dev/gsc1" | |
27 #define EXYNOS_MALI_DRIVER "libmali.so" | |
28 | |
29 #define NOTIFY_ERROR(x) \ | |
30 do { \ | |
31 SetDecoderState(kError); \ | |
32 LOG(ERROR) << "calling NotifyError(): " << x; \ | |
33 NotifyError(x); \ | |
34 } while (0) | |
35 | |
36 #define IOCTL_OR_ERROR_RETURN(fd, type, arg) \ | |
37 do { \ | |
38 if (ioctl(fd, type, arg) != 0) { \ | |
39 DPLOG(ERROR) << __func__ << "(): ioctl() failed: " << #type; \ | |
40 NOTIFY_ERROR(PLATFORM_FAILURE); \ | |
41 return; \ | |
42 } \ | |
43 } while (0) | |
44 | |
45 #define IOCTL_OR_ERROR_RETURN_FALSE(fd, type, arg) \ | |
46 do { \ | |
47 if (ioctl(fd, type, arg) != 0) { \ | |
48 DPLOG(ERROR) << __func__ << "(): ioctl() failed: " << #type; \ | |
49 NOTIFY_ERROR(PLATFORM_FAILURE); \ | |
50 return false; \ | |
51 } \ | |
52 } while (0) | |
53 | |
54 typedef void* GLeglImageOES; | |
55 static void* libmali_handle = NULL; | |
56 static EGLBoolean(*mali_egl_image_get_buffer_ext_phandle)( | |
57 EGLImageKHR, EGLint*, void*) = NULL; | |
58 static EGLImageKHR(*egl_create_image_khr)( | |
59 EGLDisplay, EGLContext, EGLenum, EGLClientBuffer, const EGLint*) = NULL; | |
60 static EGLBoolean(*egl_destroy_image_khr)( | |
61 EGLDisplay, EGLImageKHR) = NULL; | |
62 static EGLSyncKHR(*egl_create_sync_khr)( | |
63 EGLDisplay, EGLenum, const EGLint*) = NULL; | |
64 static EGLBoolean(*egl_destroy_sync_khr)( | |
65 EGLDisplay, EGLSyncKHR) = NULL; | |
66 static EGLint(*egl_client_wait_sync_khr)( | |
67 EGLDisplay, EGLSyncKHR, EGLint, EGLTimeKHR) = NULL; | |
68 static void(*gl_egl_image_target_texture_2d_oes)( | |
69 GLenum, GLeglImageOES) = NULL; | |
70 | |
71 ExynosVideoDecodeAccelerator::BitstreamBufferRef::BitstreamBufferRef( | |
72 base::WeakPtr<Client>& client, | |
73 scoped_refptr<base::MessageLoopProxy>& client_message_loop_proxy, | |
74 base::SharedMemory* shm, size_t size, int32 input_id) | |
75 : client(client), | |
76 client_message_loop_proxy(client_message_loop_proxy), | |
77 shm(shm), | |
78 size(size), | |
79 bytes_used(0), | |
80 input_id(input_id) { | |
81 } | |
82 | |
83 ExynosVideoDecodeAccelerator::BitstreamBufferRef::~BitstreamBufferRef() { | |
84 if (input_id != -1 && client_message_loop_proxy != NULL) | |
85 client_message_loop_proxy->PostTask(FROM_HERE, base::Bind( | |
86 &Client::NotifyEndOfBitstreamBuffer, client, input_id)); | |
87 } | |
88 | |
89 ExynosVideoDecodeAccelerator::PictureBufferArrayRef::PictureBufferArrayRef( | |
90 EGLDisplay egl_display, EGLImageKHR egl_images[], int egl_image_fds[], | |
91 int32 client_ids[], int egl_images_count) | |
92 : egl_display(egl_display), | |
93 egl_images(egl_images), | |
94 egl_image_fds(egl_image_fds), | |
95 client_ids(client_ids), | |
96 egl_images_count(egl_images_count) { | |
97 } | |
98 | |
99 ExynosVideoDecodeAccelerator::PictureBufferArrayRef::~PictureBufferArrayRef() { | |
100 DCHECK_EQ(egl_images != NULL, egl_image_fds != NULL); | |
101 if (egl_images == NULL) | |
102 return; | |
103 | |
104 for (int i = 0; i < egl_images_count; ++i) { | |
105 if (egl_images[i] != EGL_NO_IMAGE_KHR) | |
106 egl_destroy_image_khr(egl_display, egl_images[i]); | |
107 if (egl_image_fds[i] != -1) | |
108 close(egl_image_fds[i]); | |
109 } | |
110 } | |
111 | |
112 ExynosVideoDecodeAccelerator::EGLSyncKHRRef::EGLSyncKHRRef( | |
113 EGLDisplay egl_display, EGLSyncKHR egl_sync) | |
114 : egl_display(egl_display), | |
115 egl_sync(egl_sync) { | |
116 } | |
117 | |
118 ExynosVideoDecodeAccelerator::EGLSyncKHRRef::~EGLSyncKHRRef() { | |
119 if (egl_sync != EGL_NO_SYNC_KHR) | |
120 egl_destroy_sync_khr(egl_display, egl_sync); | |
121 } | |
122 | |
123 ExynosVideoDecodeAccelerator::MfcInputRecord::MfcInputRecord() | |
124 : at_device(false), | |
125 address(NULL), | |
126 length(0), | |
127 bytes_used(0), | |
128 input_id(-1) { | |
129 } | |
130 | |
131 ExynosVideoDecodeAccelerator::MfcInputRecord::~MfcInputRecord() { | |
132 } | |
133 | |
134 ExynosVideoDecodeAccelerator::MfcOutputRecord::MfcOutputRecord() | |
135 : at_device(false), | |
136 input_id(-1) { | |
137 bytes_used[0] = 0; | |
138 bytes_used[1] = 0; | |
139 address[0] = NULL; | |
140 address[1] = NULL; | |
141 length[0] = 0; | |
142 length[1] = 0; | |
143 } | |
144 | |
145 ExynosVideoDecodeAccelerator::MfcOutputRecord::~MfcOutputRecord() { | |
146 } | |
147 | |
148 ExynosVideoDecodeAccelerator::GscInputRecord::GscInputRecord() | |
149 : at_device(false), | |
150 mfc_output(-1) { | |
151 } | |
152 | |
153 ExynosVideoDecodeAccelerator::GscInputRecord::~GscInputRecord() { | |
154 } | |
155 | |
156 ExynosVideoDecodeAccelerator::GscOutputRecord::GscOutputRecord() | |
157 : at_device(false), | |
158 at_client(false), | |
159 fd(-1), | |
160 egl_image(EGL_NO_IMAGE_KHR), | |
161 egl_sync(EGL_NO_SYNC_KHR), | |
162 picture_id(-1) { | |
163 } | |
164 | |
165 ExynosVideoDecodeAccelerator::GscOutputRecord::~GscOutputRecord() { | |
166 } | |
167 | |
168 ExynosVideoDecodeAccelerator::ExynosVideoDecodeAccelerator( | |
169 EGLDisplay egl_display, | |
170 EGLContext egl_context, | |
171 Client* client, | |
172 const base::Callback<bool(void)>& make_context_current) | |
173 : child_message_loop_proxy_(base::MessageLoopProxy::current()), | |
174 weak_this_(base::AsWeakPtr(this)), | |
175 client_ptr_factory_(client), | |
176 client_(client_ptr_factory_.GetWeakPtr()), | |
177 decoder_thread_("ExynosDecoderThread"), | |
178 decoder_state_(kUninitialized), | |
179 decoder_current_bitstream_buffer_(NULL), | |
180 decoder_current_input_buffer_(-1), | |
181 decoder_decode_buffer_tasks_scheduled_(0), | |
182 decoder_frames_at_client_(0), | |
183 decoder_notify_flush_requested_(false), | |
184 mfc_fd_(-1), | |
185 mfc_input_streamon_(false), | |
186 mfc_input_buffer_count_(0), | |
187 mfc_input_buffer_queued_count_(0), | |
188 mfc_output_streamon_(false), | |
189 mfc_output_buffer_count_(0), | |
190 mfc_output_buffer_queued_count_(0), | |
191 mfc_output_buffer_pixelformat_(0), | |
192 gsc_fd_(-1), | |
193 gsc_input_streamon_(false), | |
194 gsc_input_buffer_count_(0), | |
195 gsc_input_buffer_queued_count_(0), | |
196 gsc_output_streamon_(false), | |
197 gsc_output_buffer_count_(0), | |
198 gsc_output_buffer_queued_count_(0), | |
199 frame_buffer_size_(0, 0), | |
200 device_poll_thread_("ExynosDevicePollThread"), | |
201 device_poll_interrupt_fd_(-1), | |
202 make_context_current_(make_context_current), | |
203 egl_display_(egl_display), | |
204 egl_context_(egl_context), | |
205 video_profile_(media::VIDEO_CODEC_PROFILE_UNKNOWN) { | |
206 } | |
207 | |
208 ExynosVideoDecodeAccelerator::~ExynosVideoDecodeAccelerator() { | |
209 // Nuke the entire site from orbit -- it's the only way to be sure. | |
210 if (device_poll_interrupt_fd_ != -1) { | |
211 close(device_poll_interrupt_fd_); | |
212 device_poll_interrupt_fd_ = -1; | |
213 } | |
214 if (gsc_fd_ != -1) { | |
215 DestroyGscInputBuffers(); | |
216 DestroyGscOutputBuffers(); | |
217 close(gsc_fd_); | |
218 gsc_fd_ = -1; | |
219 } | |
220 if (mfc_fd_ != -1) { | |
221 DestroyMfcInputBuffers(); | |
222 DestroyMfcOutputBuffers(); | |
223 close(mfc_fd_); | |
224 mfc_fd_ = -1; | |
225 } | |
226 | |
227 // These maps have members that should be manually destroyed, e.g. file | |
228 // descriptors, mmap() segments, etc. | |
229 DCHECK(mfc_input_buffer_map_.empty()); | |
230 DCHECK(mfc_output_buffer_map_.empty()); | |
231 DCHECK(gsc_input_buffer_map_.empty()); | |
232 DCHECK(gsc_output_buffer_map_.empty()); | |
233 } | |
234 | |
235 bool ExynosVideoDecodeAccelerator::Initialize( | |
236 media::VideoCodecProfile profile) { | |
237 DVLOG(3) << "Initialize()"; | |
238 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); | |
239 DCHECK_EQ(decoder_state_, kUninitialized); | |
240 | |
241 switch (profile) { | |
242 case media::H264PROFILE_BASELINE: | |
243 DVLOG(2) << "Initialize(): profile H264PROFILE_BASELINE"; | |
244 break; | |
245 case media::H264PROFILE_MAIN: | |
246 DVLOG(2) << "Initialize(): profile H264PROFILE_MAIN"; | |
247 break; | |
248 case media::H264PROFILE_HIGH: | |
249 DVLOG(2) << "Initialize(): profile H264PROFILE_HIGH"; | |
250 break; | |
251 case media::VP8PROFILE_MAIN: | |
252 DVLOG(2) << "Initialize(): profile VP8PROFILE_MAIN"; | |
253 break; | |
254 default: | |
255 LOG(ERROR) << "Initialize(): unsupported profile=" << profile; | |
Pawel Osciak
2012/12/27 16:38:37
This is probably too verbose (learned from VAVDA r
sheu
2013/01/02 20:47:38
Done.
| |
256 return false; | |
257 }; | |
258 video_profile_ = profile; | |
259 | |
260 static bool sandbox_initialized = PostSandboxInitialization(); | |
261 if (!sandbox_initialized) { | |
262 DLOG(ERROR) << "Initialize(): PostSandboxInitialization() failed"; | |
263 NOTIFY_ERROR(PLATFORM_FAILURE); | |
264 return false; | |
265 } | |
266 | |
267 if (egl_display_ == EGL_NO_DISPLAY) { | |
268 DLOG(ERROR) << "Initialize(): could not get EGLDisplay"; | |
269 NOTIFY_ERROR(PLATFORM_FAILURE); | |
270 return false; | |
271 } | |
272 | |
273 if (egl_context_ == EGL_NO_CONTEXT) { | |
274 DLOG(ERROR) << "Initialize(): could not get EGLContext"; | |
275 NOTIFY_ERROR(PLATFORM_FAILURE); | |
276 return false; | |
277 } | |
278 | |
279 // Open the video devices. | |
280 DVLOG(2) << "Initialize(): opening MFC device: " << EXYNOS_MFC_DEVICE; | |
281 mfc_fd_ = open(EXYNOS_MFC_DEVICE, O_RDWR | O_NONBLOCK | O_CLOEXEC); | |
282 if (mfc_fd_ == -1) { | |
283 DPLOG(ERROR) << "Initialize(): could not open MFC device: " | |
284 << EXYNOS_MFC_DEVICE; | |
285 NOTIFY_ERROR(PLATFORM_FAILURE); | |
286 return false; | |
287 } | |
288 DVLOG(2) << "Initialize(): opening GSC device: " << EXYNOS_GSC_DEVICE; | |
289 gsc_fd_ = open(EXYNOS_GSC_DEVICE, O_RDWR | O_NONBLOCK | O_CLOEXEC); | |
290 if (gsc_fd_ == -1) { | |
291 DPLOG(ERROR) << "Initialize(): could not open GSC device: " | |
292 << EXYNOS_GSC_DEVICE; | |
293 NOTIFY_ERROR(PLATFORM_FAILURE); | |
294 return false; | |
295 } | |
296 | |
297 // Create the interrupt fd. | |
298 DCHECK_EQ(device_poll_interrupt_fd_, -1); | |
299 device_poll_interrupt_fd_ = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); | |
300 if (device_poll_interrupt_fd_ == -1) { | |
301 DPLOG(ERROR) << "Initialize(): eventfd() failed"; | |
302 NOTIFY_ERROR(PLATFORM_FAILURE); | |
303 return false; | |
304 } | |
305 | |
306 // Capabilities check. | |
307 struct v4l2_capability caps; | |
308 const __u32 kCapsRequired = | |
309 V4L2_CAP_VIDEO_CAPTURE_MPLANE | | |
310 V4L2_CAP_VIDEO_OUTPUT_MPLANE | | |
311 V4L2_CAP_STREAMING; | |
312 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_QUERYCAP, &caps); | |
313 if ((caps.capabilities & kCapsRequired) != kCapsRequired) { | |
314 DLOG(ERROR) << "Initialize(): ioctl() failed: VIDIOC_QUERYCAP" | |
315 ", caps check failed: 0x" << std::hex << caps.capabilities; | |
316 NOTIFY_ERROR(PLATFORM_FAILURE); | |
317 return false; | |
318 } | |
319 IOCTL_OR_ERROR_RETURN_FALSE(gsc_fd_, VIDIOC_QUERYCAP, &caps); | |
320 if ((caps.capabilities & kCapsRequired) != kCapsRequired) { | |
321 DLOG(ERROR) << "Initialize(): ioctl() failed: VIDIOC_QUERYCAP" | |
322 ", caps check failed: 0x" << std::hex << caps.capabilities; | |
323 NOTIFY_ERROR(PLATFORM_FAILURE); | |
324 return false; | |
325 } | |
326 | |
327 // Some random ioctls that Exynos requires. | |
328 struct v4l2_control control; | |
329 memset(&control, 0, sizeof(control)); | |
330 control.id = V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY; // also VP8 | |
331 control.value = 8; // Magic number from Samsung folks. | |
332 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_S_CTRL, &control); | |
333 | |
334 if (!make_context_current_.Run()) { | |
335 DLOG(ERROR) << "Initialize(): could not make context current"; | |
336 NOTIFY_ERROR(PLATFORM_FAILURE); | |
337 return false; | |
338 } | |
339 | |
340 if (!CreateMfcInputBuffers()) | |
341 return false; | |
342 | |
343 // MFC output format has to be setup before streaming starts. | |
344 struct v4l2_format format; | |
345 memset(&format, 0, sizeof(format)); | |
346 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
347 format.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12MT_16X16; | |
348 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_S_FMT, &format); | |
349 | |
350 // Initialize format-specific bits. | |
351 if (video_profile_ >= media::H264PROFILE_MIN && | |
352 video_profile_ <= media::H264PROFILE_MAX) { | |
353 decoder_h264_parser_.reset(new content::H264Parser()); | |
354 } | |
355 | |
356 if (!decoder_thread_.Start()) { | |
357 DLOG(ERROR) << "Initialize(): decoder thread failed to start"; | |
358 NOTIFY_ERROR(PLATFORM_FAILURE); | |
359 return false; | |
360 } | |
361 | |
362 SetDecoderState(kInitialized); | |
363 | |
364 child_message_loop_proxy_->PostTask(FROM_HERE, base::Bind( | |
365 &Client::NotifyInitializeDone, client_)); | |
366 return true; | |
367 } | |
368 | |
369 void ExynosVideoDecodeAccelerator::Decode( | |
370 const media::BitstreamBuffer& bitstream_buffer) { | |
371 DVLOG(1) << "Decode(): input_id=" << bitstream_buffer.id() | |
372 << ", size=" << bitstream_buffer.size(); | |
373 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); | |
374 | |
375 scoped_ptr<BitstreamBufferRef> bitstream_record(new BitstreamBufferRef( | |
376 client_, child_message_loop_proxy_, | |
377 new base::SharedMemory(bitstream_buffer.handle(), true), | |
378 bitstream_buffer.size(), bitstream_buffer.id())); | |
379 if (!bitstream_record->shm->Map(bitstream_buffer.size())) { | |
380 DLOG(ERROR) << "Decode(): could not map bitstream_buffer"; | |
381 NOTIFY_ERROR(UNREADABLE_INPUT); | |
382 return; | |
383 } | |
384 DVLOG(3) << "Decode(): mapped to addr=" << bitstream_record->shm->memory(); | |
385 | |
386 // DecodeTask() will take care of running a DecodeBufferTask(). | |
387 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
388 &ExynosVideoDecodeAccelerator::DecodeTask, base::Unretained(this), | |
389 base::Passed(&bitstream_record))); | |
390 } | |
391 | |
392 void ExynosVideoDecodeAccelerator::AssignPictureBuffers( | |
393 const std::vector<media::PictureBuffer>& buffers) { | |
394 DVLOG(3) << "AssignPictureBuffers(): buffer_count=" << buffers.size(); | |
395 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); | |
396 | |
397 if (!make_context_current_.Run()) { | |
398 DLOG(ERROR) << "AssignPictureBuffers(): could not make context current"; | |
399 NOTIFY_ERROR(PLATFORM_FAILURE); | |
400 return; | |
401 } | |
402 | |
403 DCHECK_EQ(gsc_output_buffer_count_, static_cast<int>(buffers.size())); | |
404 scoped_ptr<PictureBufferArrayRef> pic_buffers_ref( | |
405 new PictureBufferArrayRef( | |
406 egl_display_, new EGLImageKHR[buffers.size()], | |
407 new int[buffers.size()], new int32[buffers.size()], buffers.size())); | |
408 for (int i = 0; i < pic_buffers_ref->egl_images_count; ++i) { | |
409 pic_buffers_ref->egl_images[i] = EGL_NO_IMAGE_KHR; | |
410 pic_buffers_ref->egl_image_fds[i] = -1; | |
411 pic_buffers_ref->client_ids[i] = -1; | |
412 } | |
413 | |
414 const static EGLint kImageAttrs[] = { | |
415 EGL_IMAGE_PRESERVED_KHR, 0, | |
416 EGL_NONE, | |
417 }; | |
418 Display* x_display = base::MessagePumpForUI::GetDefaultXDisplay(); | |
419 glActiveTexture(GL_TEXTURE0); | |
420 for (int i = 0; i < pic_buffers_ref->egl_images_count; ++i) { | |
421 // Create the X pixmap and then create an EGLImageKHR from it, so we can | |
422 // get dma_buf backing. | |
423 Pixmap pixmap = XCreatePixmap(x_display, RootWindow(x_display, 0), | |
424 buffers[i].size().width(), buffers[i].size().height(), 32); | |
425 if (!pixmap) { | |
426 DLOG(ERROR) << "AssignPictureBuffers(): could not create X pixmap"; | |
427 NOTIFY_ERROR(PLATFORM_FAILURE); | |
428 return; | |
429 } | |
430 glBindTexture(GL_TEXTURE_2D, buffers[i].texture_id()); | |
431 EGLImageKHR egl_image; | |
432 egl_image = egl_create_image_khr( | |
433 egl_display_, EGL_NO_CONTEXT, EGL_NATIVE_PIXMAP_KHR, | |
434 (EGLClientBuffer)pixmap, kImageAttrs); | |
435 // We can free the X pixmap immediately -- according to the | |
436 // EGL_KHR_image_base spec, the backing storage does not go away until the | |
437 // last referencing EGLImage is destroyed. | |
438 XFreePixmap(x_display, pixmap); | |
439 if (egl_image == EGL_NO_IMAGE_KHR) { | |
440 DLOG(ERROR) << "AssignPictureBuffers(): could not create EGLImageKHR"; | |
441 NOTIFY_ERROR(PLATFORM_FAILURE); | |
442 return; | |
443 } | |
444 pic_buffers_ref->egl_images[i] = egl_image; | |
445 int fd; | |
446 if (!mali_egl_image_get_buffer_ext_phandle( | |
447 pic_buffers_ref->egl_images[i], NULL, &fd)) { | |
448 DLOG(ERROR) << "AssignPictureBuffers(): " | |
449 << "could not get EGLImageKHR dmabuf fd"; | |
450 NOTIFY_ERROR(PLATFORM_FAILURE); | |
451 return; | |
452 } | |
453 pic_buffers_ref->egl_image_fds[i] = fd; | |
454 gl_egl_image_target_texture_2d_oes(GL_TEXTURE_2D, egl_image); | |
455 pic_buffers_ref->client_ids[i] = buffers[i].id(); | |
456 } | |
457 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
458 &ExynosVideoDecodeAccelerator::AssignPictureBuffersTask, | |
459 base::Unretained(this), base::Passed(&pic_buffers_ref))); | |
460 } | |
461 | |
462 void ExynosVideoDecodeAccelerator::ReusePictureBuffer(int32 picture_buffer_id) { | |
463 DVLOG(3) << "ReusePictureBuffer(): picture_buffer_id=" << picture_buffer_id; | |
464 // Must be run on child thread, as we'll insert a sync in the EGL context. | |
465 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); | |
466 | |
467 if (!make_context_current_.Run()) { | |
468 DLOG(ERROR) << "ReusePictureBuffer(): could not make context current"; | |
469 NOTIFY_ERROR(PLATFORM_FAILURE); | |
470 return; | |
471 } | |
472 | |
473 EGLSyncKHR egl_sync; | |
474 egl_sync = egl_create_sync_khr(egl_display_, EGL_SYNC_FENCE_KHR, NULL); | |
475 if (egl_sync == EGL_NO_SYNC_KHR) { | |
476 DLOG(ERROR) << "ReusePictureBuffer(): eglCreateSyncKHR() failed"; | |
477 NOTIFY_ERROR(PLATFORM_FAILURE); | |
478 return; | |
479 } | |
480 | |
481 scoped_ptr<EGLSyncKHRRef> egl_sync_ref(new EGLSyncKHRRef( | |
482 egl_display_, egl_sync)); | |
483 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
484 &ExynosVideoDecodeAccelerator::ReusePictureBufferTask, | |
485 base::Unretained(this), picture_buffer_id, base::Passed(&egl_sync_ref))); | |
486 } | |
487 | |
488 void ExynosVideoDecodeAccelerator::Flush() { | |
489 DVLOG(3) << "Flush()"; | |
490 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); | |
491 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
492 &ExynosVideoDecodeAccelerator::FlushTask, base::Unretained(this))); | |
493 } | |
494 | |
495 void ExynosVideoDecodeAccelerator::Reset() { | |
496 DVLOG(3) << "Reset()"; | |
497 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); | |
498 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
499 &ExynosVideoDecodeAccelerator::ResetTask, base::Unretained(this))); | |
500 } | |
501 | |
502 void ExynosVideoDecodeAccelerator::Destroy() { | |
503 DVLOG(3) << "Destroy()"; | |
504 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); | |
505 | |
506 // We're destroying; cancel all callbacks. | |
507 client_ptr_factory_.InvalidateWeakPtrs(); | |
508 | |
509 // If the decoder thread is running, destroy using posted task. | |
510 if (decoder_thread_.message_loop() != NULL) { | |
511 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
512 &ExynosVideoDecodeAccelerator::DestroyTask, base::Unretained(this))); | |
513 // DestroyTask() will cause the decoder_thread_ to flush all tasks. | |
514 decoder_thread_.Stop(); | |
515 } else { | |
516 // Otherwise, call the destroy task directly. | |
517 DestroyTask(); | |
518 } | |
519 | |
520 // Set to kError state just in case. | |
521 SetDecoderState(kError); | |
522 | |
523 delete this; | |
524 } | |
525 | |
526 // static | |
527 void ExynosVideoDecodeAccelerator::PreSandboxInitialization() { | |
528 DVLOG(3) << "PreSandboxInitialization()"; | |
529 dlerror(); | |
530 libmali_handle = dlopen(EXYNOS_MALI_DRIVER, RTLD_LAZY | RTLD_LOCAL); | |
531 if (libmali_handle == NULL) { | |
532 DPLOG(ERROR) << "failed to dlopen() " << EXYNOS_MALI_DRIVER | |
533 << ": " << dlerror(); | |
534 } | |
535 } | |
536 | |
537 // static | |
538 bool ExynosVideoDecodeAccelerator::PostSandboxInitialization() { | |
539 DVLOG(3) << "PostSandboxInitialization()"; | |
540 if (libmali_handle == NULL) { | |
541 DLOG(ERROR) << "PostSandboxInitialization(): no " << EXYNOS_MALI_DRIVER | |
542 << " driver handle"; | |
543 return false; | |
544 } | |
545 | |
546 dlerror(); | |
547 mali_egl_image_get_buffer_ext_phandle = | |
548 reinterpret_cast<EGLBoolean(*)(EGLImageKHR, EGLint*, void*)>( | |
549 dlsym(libmali_handle, "mali_egl_image_get_buffer_ext_phandle")); | |
550 if (mali_egl_image_get_buffer_ext_phandle == NULL) { | |
551 DPLOG(ERROR) << "PostSandboxInitialization(): failed to dlsym()" | |
552 << " mali_egl_image_get_buffer_ext_phandle: " << dlerror(); | |
553 return false; | |
554 } | |
555 | |
556 egl_create_image_khr = | |
557 reinterpret_cast<EGLImageKHR(*)(EGLDisplay, EGLContext, EGLenum, | |
558 EGLClientBuffer, const EGLint*)>( | |
559 dlsym(libmali_handle, "eglCreateImageKHR")); | |
560 if (egl_create_image_khr == NULL) { | |
561 DPLOG(ERROR) << "PostSandBoxInitialization(): failed to dlsym()" | |
562 << " eglCreateImageKHR: " << dlerror(); | |
563 return false; | |
564 } | |
565 | |
566 egl_destroy_image_khr = | |
567 reinterpret_cast<EGLBoolean(*)(EGLDisplay, EGLImageKHR)>( | |
568 dlsym(libmali_handle, "eglDestroyImageKHR")); | |
569 if (egl_destroy_image_khr == NULL) { | |
570 DPLOG(ERROR) << "PostSandBoxInitialization(): failed to dlsym()" | |
571 << " eglDestroyImageKHR: " << dlerror(); | |
572 return false; | |
573 } | |
574 | |
575 egl_create_sync_khr = | |
576 reinterpret_cast<EGLSyncKHR(*)(EGLDisplay, EGLenum, const EGLint*)>( | |
577 dlsym(libmali_handle, "eglCreateSyncKHR")); | |
578 if (egl_create_sync_khr == NULL) { | |
579 DPLOG(ERROR) << "PostSandboxInitialization(): failed to dlsym()" | |
580 << " eglCreateSyncKHR: " << dlerror(); | |
581 return false; | |
582 } | |
583 | |
584 egl_destroy_sync_khr = | |
585 reinterpret_cast<EGLBoolean(*)(EGLDisplay, EGLSyncKHR)>( | |
586 dlsym(libmali_handle, "eglDestroySyncKHR")); | |
587 if (egl_destroy_sync_khr == NULL) { | |
588 DPLOG(ERROR) << "PostSandboxInitialization(): failed to dlsym()" | |
589 << " eglDestroySyncKHR: " << dlerror(); | |
590 return false; | |
591 } | |
592 | |
593 egl_client_wait_sync_khr = | |
594 reinterpret_cast<EGLint(*)(EGLDisplay, EGLSyncKHR, EGLint, EGLTimeKHR)>( | |
595 dlsym(libmali_handle, "eglClientWaitSyncKHR")); | |
596 if (egl_client_wait_sync_khr == NULL) { | |
597 DPLOG(ERROR) << "PostSandboxInitialization(): failed to dlsym()" | |
598 << " eglClientWaitSyncKHR: " << dlerror(); | |
599 return false; | |
600 } | |
601 | |
602 gl_egl_image_target_texture_2d_oes = | |
603 reinterpret_cast<void(*)(GLenum, GLeglImageOES)>( | |
604 dlsym(libmali_handle, "glEGLImageTargetTexture2DOES")); | |
605 if (gl_egl_image_target_texture_2d_oes == NULL) { | |
606 DPLOG(ERROR) << "PostSandboxInitialization(): failed to dlsym()" | |
607 << "glEGLImageTargetTexture2DOES: " << dlerror(); | |
608 return false; | |
609 } | |
610 | |
611 return true; | |
612 } | |
613 | |
614 void ExynosVideoDecodeAccelerator::DecodeTask( | |
615 scoped_ptr<BitstreamBufferRef> bitstream_record) { | |
616 DVLOG(3) << "DecodeTask(): input_id=" << bitstream_record->input_id; | |
617 DCHECK_EQ(decoder_thread_.message_loop(), MessageLoop::current()); | |
618 DCHECK_NE(decoder_state_, kUninitialized); | |
619 TRACE_EVENT1("Video Decoder", "EVDA::DecodeTask", "input_id", | |
620 bitstream_record->input_id); | |
621 | |
622 if (decoder_state_ == kResetting) { | |
623 DVLOG(2) << "DecodeTask(): early out: kResetting state"; | |
624 return; | |
625 } else if (decoder_state_ == kError) { | |
626 DVLOG(2) << "DecodeTask(): early out: kError state"; | |
627 return; | |
628 } | |
629 | |
630 decoder_input_queue_.push_front( | |
631 linked_ptr<BitstreamBufferRef>(bitstream_record.release())); | |
632 decoder_decode_buffer_tasks_scheduled_++; | |
633 DecodeBufferTask(); | |
634 } | |
635 | |
636 void ExynosVideoDecodeAccelerator::DecodeBufferTask() { | |
637 DVLOG(3) << "DecodeBufferTask()"; | |
638 DCHECK_EQ(decoder_thread_.message_loop(), MessageLoop::current()); | |
639 DCHECK_NE(decoder_state_, kUninitialized); | |
640 TRACE_EVENT0("Video Decoder", "EVDA::DecodeBufferTask"); | |
641 | |
642 decoder_decode_buffer_tasks_scheduled_--; | |
643 | |
644 if (decoder_state_ == kResetting) { | |
645 DVLOG(2) << "DecodeBufferTask(): early out: kResetting state"; | |
646 return; | |
647 } else if (decoder_state_ == kError) { | |
648 DVLOG(2) << "DecodeBufferTask(): early out: kError state"; | |
649 return; | |
650 } | |
651 | |
652 if (decoder_current_bitstream_buffer_ == NULL) { | |
653 if (decoder_input_queue_.empty()) { | |
654 // We're waiting for a new buffer -- exit without scheduling a new task. | |
655 return; | |
656 } | |
657 // Setup to use the next buffer. | |
658 decoder_current_bitstream_buffer_.reset( | |
659 decoder_input_queue_.back().release()); | |
660 decoder_input_queue_.pop_back(); | |
661 DVLOG(3) << "DecodeBufferTask(): reading input_id=" | |
662 << decoder_current_bitstream_buffer_->input_id | |
663 << ", addr=" << decoder_current_bitstream_buffer_->shm->memory() | |
664 << ", size=" << decoder_current_bitstream_buffer_->size; | |
665 } | |
666 bool schedule_task = false; | |
667 const size_t size = decoder_current_bitstream_buffer_->size; | |
668 size_t decoded_size; | |
669 if (size == 0) { | |
670 const int32 input_id = decoder_current_bitstream_buffer_->input_id; | |
671 decoded_size = 0; | |
672 if (input_id != -1) { | |
673 // This is a buffer queued from the client that has zero size. Skip. | |
674 schedule_task = true; | |
675 } else { | |
676 // This is a buffer of zero size, queued to flush the pipe. Flush. | |
677 DCHECK_EQ(decoder_current_bitstream_buffer_->shm.get(), | |
678 static_cast<base::SharedMemory*>(NULL)); | |
679 // Flush the current input, enqueue an empty buffer, then flush that down. | |
680 if (!FlushInputFrame() || !AppendToInputFrame(NULL, 0) | |
681 || !FlushInputFrame()) { | |
682 schedule_task = false; | |
683 } else { | |
684 DVLOG(2) << "DecodeBufferTask(): enqueued flush buffer"; | |
685 schedule_task = true; | |
686 } | |
687 } | |
688 } else { | |
689 // This is a buffer queued from the client, with actual `contents. Decode. | |
690 const void* const data = | |
691 reinterpret_cast<const uint8*>( | |
692 decoder_current_bitstream_buffer_->shm->memory()) + | |
693 decoder_current_bitstream_buffer_->bytes_used; | |
694 const size_t data_size = | |
695 decoder_current_bitstream_buffer_->size - | |
696 decoder_current_bitstream_buffer_->bytes_used; | |
697 if (!FindFrameFragment(data, data_size, &decoded_size)) | |
698 return; | |
699 switch (decoder_state_) { | |
700 case kInitialized: | |
701 case kAfterReset: | |
702 schedule_task = DecodeBufferInitial(data, decoded_size, &decoded_size); | |
703 break; | |
704 case kDecoding: | |
705 schedule_task = DecodeBufferContinue(data, decoded_size, &decoded_size); | |
706 break; | |
707 default: | |
708 NOTIFY_ERROR(ILLEGAL_STATE); | |
709 return; | |
710 } | |
711 } | |
712 if (decoder_state_ == kError) { | |
713 // Failed during decode. | |
714 return; | |
715 } | |
716 | |
717 if (schedule_task) { | |
718 decoder_current_bitstream_buffer_->bytes_used += decoded_size; | |
719 if (decoder_current_bitstream_buffer_->bytes_used == | |
720 decoder_current_bitstream_buffer_->size) { | |
721 // Our current bitstream buffer is done; return it. | |
722 int32 input_id = decoder_current_bitstream_buffer_->input_id; | |
723 DVLOG(3) << "DecodeBufferTask(): finished input_id=" << input_id; | |
724 // BitstreamBufferRef destructor calls NotifyEndOfBitstreamBuffer(). | |
725 decoder_current_bitstream_buffer_.reset(); | |
726 } | |
727 ScheduleDecodeBufferTaskIfNeeded(); | |
728 } | |
729 } | |
730 | |
731 bool ExynosVideoDecodeAccelerator::FindFrameFragment( | |
732 const void* data, | |
733 size_t size, | |
734 size_t* endpos) { | |
735 if (video_profile_ >= media::H264PROFILE_MIN && | |
736 video_profile_ <= media::H264PROFILE_MAX) { | |
737 // For H264, we need to feed HW one frame at a time. This is going to take | |
738 // some parsing of our input stream. | |
739 decoder_h264_parser_->SetStream(reinterpret_cast<const uint8*>(data), size); | |
740 content::H264NALU nalu; | |
741 content::H264Parser::Result result; | |
742 | |
743 // Find the first NAL. | |
744 result = decoder_h264_parser_->AdvanceToNextNALU(&nalu); | |
745 if (result == content::H264Parser::kInvalidStream || | |
746 result == content::H264Parser::kUnsupportedStream) | |
747 return false; | |
748 *endpos = (nalu.data + nalu.size) - reinterpret_cast<const uint8*>(data); | |
749 if (result == content::H264Parser::kEOStream) | |
750 return true; | |
751 | |
752 // Keep on peeking the next NALs while they don't indicate a frame | |
753 // boundary. | |
754 for (;;) { | |
755 result = decoder_h264_parser_->AdvanceToNextNALU(&nalu); | |
756 if (result == content::H264Parser::kInvalidStream || | |
757 result == content::H264Parser::kUnsupportedStream) | |
758 return false; | |
759 if (result == content::H264Parser::kEOStream) | |
760 return true; | |
761 switch (nalu.nal_unit_type) { | |
762 case content::H264NALU::kNonIDRSlice: | |
763 case content::H264NALU::kIDRSlice: | |
764 // For these two, if the "first_mb_in_slice" field is zero, start a | |
765 // new frame and return. This field is Exp-Golomb coded starting on | |
766 // the eighth data bit of the NAL; a zero value is encoded with a | |
767 // leading '1' bit in the byte, which we can detect as the byte being | |
768 // (unsigned) greater than or equal to 0x80. | |
769 if (nalu.data[1] >= 0x80) | |
770 return true; | |
771 break; | |
772 case content::H264NALU::kSPS: | |
773 case content::H264NALU::kPPS: | |
774 case content::H264NALU::kEOSeq: | |
775 case content::H264NALU::kEOStream: | |
776 // These unconditionally signal a frame boundary. | |
777 return true; | |
778 default: | |
779 // For all others, keep going. | |
780 break; | |
781 } | |
782 *endpos = (nalu.data + nalu.size) - reinterpret_cast<const uint8*>(data); | |
783 } | |
784 NOTREACHED(); | |
785 return false; | |
786 } else { | |
787 DCHECK_GE(video_profile_, media::VP8PROFILE_MIN); | |
788 DCHECK_LE(video_profile_, media::VP8PROFILE_MAX); | |
789 // For VP8, we can just dump the entire buffer. No fragmentation needed. | |
790 *endpos = size; | |
791 return true; | |
792 } | |
793 } | |
794 | |
795 void ExynosVideoDecodeAccelerator::ScheduleDecodeBufferTaskIfNeeded() { | |
796 // If we're behind on tasks, schedule another one. | |
797 int buffers_to_decode = decoder_input_queue_.size(); | |
798 if (decoder_current_bitstream_buffer_ != NULL) | |
799 buffers_to_decode++; | |
800 if (decoder_decode_buffer_tasks_scheduled_ < buffers_to_decode) { | |
801 decoder_decode_buffer_tasks_scheduled_++; | |
802 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
803 &ExynosVideoDecodeAccelerator::DecodeBufferTask, | |
804 base::Unretained(this))); | |
805 } | |
806 } | |
807 | |
808 bool ExynosVideoDecodeAccelerator::DecodeBufferInitial( | |
809 const void* data, size_t size, size_t* endpos) { | |
810 DVLOG(3) << "DecodeBufferInitial(): data=" << data << ", size=" << size; | |
811 DCHECK_EQ(decoder_thread_.message_loop(), MessageLoop::current()); | |
812 DCHECK_NE(decoder_state_, kUninitialized); | |
813 DCHECK_NE(decoder_state_, kDecoding); | |
814 DCHECK(!device_poll_thread_.IsRunning()); | |
815 // Initial decode. We haven't been able to get output stream format info yet. | |
816 // Get it, and start decoding. | |
817 | |
818 // Copy in and send to HW. | |
819 if (!AppendToInputFrame(data, size) || !FlushInputFrame()) | |
820 return false; | |
821 | |
822 // Recycle buffers. | |
823 DequeueMfc(); | |
824 | |
825 // Check and see if we have format info yet. | |
826 struct v4l2_format format; | |
827 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
828 if (ioctl(mfc_fd_, VIDIOC_G_FMT, &format) != 0) { | |
829 if (errno == EINVAL) { | |
830 // We will get EINVAL if we haven't seen sufficient stream to decode the | |
831 // format. Return true and schedule the next buffer. | |
832 *endpos = size; | |
833 return true; | |
834 } else { | |
835 DPLOG(ERROR) << "DecodeBufferInitial(): ioctl() failed: VIDIOC_G_FMT"; | |
836 NOTIFY_ERROR(PLATFORM_FAILURE); | |
837 return false; | |
838 } | |
839 } | |
840 | |
841 // Run this initialization only on first startup. | |
842 if (decoder_state_ == kInitialized) { | |
843 DVLOG(3) << "DecodeBufferInitial(): running one-time initialization"; | |
844 // Success! Setup our parameters. | |
845 CHECK_EQ(format.fmt.pix_mp.num_planes, 2); | |
846 // We don't handle midstream resizes right now. | |
847 if (!frame_buffer_size_.IsEmpty() && frame_buffer_size_ != | |
848 gfx::Size(format.fmt.pix_mp.width, frame_buffer_size_.height())) { | |
849 // We don't handle midstream resizes right now. | |
850 NOTIMPLEMENTED(); | |
851 NOTIFY_ERROR(UNREADABLE_INPUT); | |
852 return false; | |
853 } | |
854 frame_buffer_size_.SetSize( | |
855 format.fmt.pix_mp.width, format.fmt.pix_mp.height); | |
856 mfc_output_buffer_size_[0] = format.fmt.pix_mp.plane_fmt[0].sizeimage; | |
857 mfc_output_buffer_size_[1] = format.fmt.pix_mp.plane_fmt[1].sizeimage; | |
858 mfc_output_buffer_pixelformat_ = format.fmt.pix_mp.pixelformat; | |
859 | |
860 // Create our other buffers. | |
861 if (!CreateMfcOutputBuffers() || !CreateGscInputBuffers() || | |
862 !CreateGscOutputBuffers()) | |
863 return false; | |
864 | |
865 // MFC expects to process the initial buffer once during stream init to | |
866 // configure stream parameters, but will not consume the steam data on that | |
867 // iteration. Subsequent iterations (including after reset) do not require | |
868 // the stream init step. | |
869 *endpos = 0; | |
870 } else { | |
871 *endpos = size; | |
872 } | |
873 | |
874 // StartDevicePoll will raise the error if there is one. | |
875 if (!StartDevicePoll()) | |
876 return false; | |
877 | |
878 decoder_state_ = kDecoding; | |
879 ScheduleDecodeBufferTaskIfNeeded(); | |
880 return true; | |
881 } | |
882 | |
883 bool ExynosVideoDecodeAccelerator::DecodeBufferContinue( | |
884 const void* data, size_t size, size_t* endpos) { | |
885 DVLOG(3) << "DecodeBufferContinue(): data=" << data << ", size=" << size; | |
886 DCHECK_EQ(decoder_thread_.message_loop(), MessageLoop::current()); | |
887 DCHECK_EQ(decoder_state_, kDecoding); | |
888 | |
889 // We've already setup our output stream parameters, so just keep on truckin'. | |
890 *endpos = size; | |
891 // Both of these calls will set kError state if they fail. | |
892 return (AppendToInputFrame(data, size) && FlushInputFrame()); | |
893 } | |
894 | |
895 bool ExynosVideoDecodeAccelerator::AppendToInputFrame( | |
896 const void* data, size_t size) { | |
897 DVLOG(3) << "AppendToInputFrame()"; | |
898 DCHECK_EQ(decoder_thread_.message_loop(), MessageLoop::current()); | |
899 DCHECK_NE(decoder_state_, kUninitialized); | |
900 DCHECK_NE(decoder_state_, kResetting); | |
901 DCHECK_NE(decoder_state_, kError); | |
902 // This routine can handle data == NULL and size == 0, which occurs when | |
903 // we queue an empty buffer for the purposes of flushing the pipe. | |
904 | |
905 // Flush if we're too big | |
906 if (decoder_current_input_buffer_ != -1) { | |
907 MfcInputRecord& input_record = | |
908 mfc_input_buffer_map_[decoder_current_input_buffer_]; | |
909 if (input_record.bytes_used + size > input_record.length) { | |
910 if (!FlushInputFrame()) | |
911 return false; | |
912 decoder_current_input_buffer_ = -1; | |
913 } | |
914 } | |
915 | |
916 // Try to get an available input buffer | |
917 if (decoder_current_input_buffer_ == -1) { | |
918 if (mfc_free_input_buffers_.empty()) { | |
919 // See if we can get more free buffers from HW | |
920 DequeueMfc(); | |
921 if (mfc_free_input_buffers_.empty()) { | |
922 // Nope! | |
923 DVLOG(2) << "AppendToInputFrame(): stalled for input buffers"; | |
924 return false; | |
925 } | |
926 } | |
927 decoder_current_input_buffer_ = mfc_free_input_buffers_.back(); | |
928 mfc_free_input_buffers_.pop_back(); | |
929 MfcInputRecord& input_record = | |
930 mfc_input_buffer_map_[decoder_current_input_buffer_]; | |
931 DCHECK_EQ(input_record.bytes_used, 0); | |
932 DCHECK_EQ(input_record.input_id, -1); | |
933 DCHECK(decoder_current_bitstream_buffer_ != NULL); | |
934 input_record.input_id = decoder_current_bitstream_buffer_->input_id; | |
935 } | |
936 | |
937 if (size == 0) { | |
938 // If we asked for an empty buffer, return now. We return only after | |
939 // getting the next input buffer, since we might actually want an empty | |
940 // input buffer for flushing purposes. | |
941 return true; | |
942 } | |
943 DCHECK(data != NULL); | |
944 | |
945 // Copy in to the buffer. | |
946 MfcInputRecord& input_record = | |
947 mfc_input_buffer_map_[decoder_current_input_buffer_]; | |
948 if (size > input_record.length - input_record.bytes_used) { | |
949 LOG(ERROR) << "AppendToInputFrame(): over-size frame, erroring"; | |
950 NOTIFY_ERROR(UNREADABLE_INPUT); | |
951 return false; | |
952 } | |
953 memcpy((char*)input_record.address + input_record.bytes_used, data, size); | |
954 input_record.bytes_used += size; | |
955 | |
956 return true; | |
957 } | |
958 | |
959 bool ExynosVideoDecodeAccelerator::FlushInputFrame() { | |
960 DVLOG(3) << "FlushInputFrame()"; | |
961 DCHECK_EQ(decoder_thread_.message_loop(), MessageLoop::current()); | |
962 DCHECK_NE(decoder_state_, kUninitialized); | |
963 DCHECK_NE(decoder_state_, kResetting); | |
964 DCHECK_NE(decoder_state_, kError); | |
965 | |
966 if (decoder_current_input_buffer_ == -1) | |
967 return true; | |
968 | |
969 MfcInputRecord& input_record = | |
970 mfc_input_buffer_map_[decoder_current_input_buffer_]; | |
971 // An empty buffer with input_id == -1 is a "flush buffer" that we don't want | |
972 // to skip. | |
973 if (input_record.bytes_used == 0 && input_record.input_id != -1) { | |
974 input_record.input_id = -1; | |
975 mfc_free_input_buffers_.push_back(decoder_current_input_buffer_); | |
976 decoder_current_input_buffer_ = -1; | |
977 return true; | |
978 } | |
979 | |
980 // Queue it to MFC. | |
981 mfc_input_ready_queue_.push_back(decoder_current_input_buffer_); | |
982 decoder_current_input_buffer_ = -1; | |
983 DVLOG(3) << "FlushInputFrame(): submitting input_id=" | |
984 << input_record.input_id; | |
985 // Kick the MFC once since there's new available input for it. | |
986 EnqueueMfc(); | |
987 | |
988 return (decoder_state_ != kError); | |
989 } | |
990 | |
991 void ExynosVideoDecodeAccelerator::AssignPictureBuffersTask( | |
992 scoped_ptr<PictureBufferArrayRef> pic_buffers) { | |
993 DVLOG(3) << "AssignPictureBuffersTask()"; | |
994 DCHECK_EQ(decoder_thread_.message_loop(), MessageLoop::current()); | |
995 DCHECK_NE(decoder_state_, kUninitialized); | |
996 TRACE_EVENT0("Video Decoder", "EVDA::AssignPictureBuffersTask"); | |
997 | |
998 // We run AssignPictureBuffersTask even if we're in kResetting. | |
999 if (decoder_state_ == kError) { | |
1000 DVLOG(2) << "AssignPictureBuffersTask(): early out: kError state"; | |
1001 return; | |
1002 } | |
1003 | |
1004 DCHECK_EQ(pic_buffers->egl_images_count, | |
1005 static_cast<int>(gsc_output_buffer_map_.size())); | |
1006 for (size_t i = 0; i < gsc_output_buffer_map_.size(); ++i) { | |
1007 // We should be blank right now. | |
1008 GscOutputRecord& output_record = gsc_output_buffer_map_[i]; | |
1009 DCHECK_EQ(output_record.fd, -1); | |
1010 DCHECK_EQ(output_record.egl_image, EGL_NO_IMAGE_KHR); | |
1011 DCHECK_EQ(output_record.egl_sync, EGL_NO_SYNC_KHR); | |
1012 DCHECK_EQ(output_record.picture_id, -1); | |
1013 output_record.fd = pic_buffers->egl_image_fds[i]; | |
1014 output_record.egl_image = pic_buffers->egl_images[i]; | |
1015 output_record.picture_id = pic_buffers->client_ids[i]; | |
1016 | |
1017 // Take ownership of the EGLImage and fd. | |
1018 pic_buffers->egl_images[i] = EGL_NO_IMAGE_KHR; | |
1019 pic_buffers->egl_image_fds[i] = -1; | |
1020 // And add this buffer to the free list. | |
1021 gsc_free_output_buffers_.push_front(i); | |
1022 } | |
1023 | |
1024 // We got buffers! Kick the GSC. | |
1025 EnqueueGsc(); | |
1026 } | |
1027 | |
1028 void ExynosVideoDecodeAccelerator::ServiceDeviceTask() { | |
1029 DVLOG(3) << "ServiceDeviceTask()"; | |
1030 DCHECK_EQ(decoder_thread_.message_loop(), MessageLoop::current()); | |
1031 DCHECK_NE(decoder_state_, kUninitialized); | |
1032 DCHECK_NE(decoder_state_, kInitialized); | |
1033 DCHECK_NE(decoder_state_, kAfterReset); | |
1034 TRACE_EVENT0("Video Decoder", "EVDA::ServiceDeviceTask"); | |
1035 | |
1036 if (decoder_state_ == kResetting) { | |
1037 DVLOG(2) << "ServiceDeviceTask(): early out: kResetting state"; | |
1038 return; | |
1039 } else if (decoder_state_ == kError) { | |
1040 DVLOG(2) << "ServiceDeviceTask(): early out: kError state"; | |
1041 return; | |
1042 } | |
1043 | |
1044 DequeueMfc(); | |
1045 DequeueGsc(); | |
1046 EnqueueMfc(); | |
1047 EnqueueGsc(); | |
1048 | |
1049 // Clear the interrupt fd. | |
1050 if (!ClearDevicePollInterrupt()) | |
1051 return; | |
1052 | |
1053 unsigned int poll_fds = 0; | |
1054 // Add MFC fd, if we should poll on it. | |
1055 // MFC can be polled as soon as either input or output buffers are queued. | |
1056 if (mfc_input_buffer_queued_count_ + mfc_output_buffer_queued_count_ > 0) | |
1057 poll_fds |= kPollMfc; | |
1058 // Add GSC fd, if we should poll on it. | |
1059 // GSC has to wait until both input and output buffers are queued. | |
1060 if (gsc_input_buffer_queued_count_ > 0 && gsc_output_buffer_queued_count_ > 0) | |
1061 poll_fds |= kPollGsc; | |
1062 | |
1063 // ServiceDeviceTask() should only ever be scheduled from DevicePollTask(), | |
1064 // so either: | |
1065 // * device_poll_thread_ is running normally | |
1066 // * device_poll_thread_ scheduled us, but then a ResetTask() or DestroyTask() | |
1067 // shut it down, in which case we're either in kResetting or kError states | |
1068 // respectively, and we should have early-outed already. | |
1069 DCHECK(device_poll_thread_.message_loop()); | |
1070 // Queue the DevicePollTask() now. | |
1071 device_poll_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
1072 &ExynosVideoDecodeAccelerator::DevicePollTask, | |
1073 base::Unretained(this), | |
1074 poll_fds)); | |
1075 | |
1076 DVLOG(1) << "ServiceDeviceTask(): buffer counts: DEC[" | |
1077 << decoder_input_queue_.size() << "->" | |
1078 << mfc_input_ready_queue_.size() << "] => MFC[" | |
1079 << mfc_free_input_buffers_.size() << "+" | |
1080 << mfc_input_buffer_queued_count_ << "/" | |
1081 << mfc_input_buffer_count_ << "->" | |
1082 << mfc_free_output_buffers_.size() << "+" | |
1083 << mfc_output_buffer_queued_count_ << "/" | |
1084 << mfc_output_buffer_count_ << "] => " | |
1085 << mfc_output_gsc_input_queue_.size() << " => GSC[" | |
1086 << gsc_free_input_buffers_.size() << "+" | |
1087 << gsc_input_buffer_queued_count_ << "/" | |
1088 << gsc_input_buffer_count_ << "->" | |
1089 << gsc_free_output_buffers_.size() << "+" | |
1090 << gsc_output_buffer_queued_count_ << "/" | |
1091 << gsc_output_buffer_count_ << "] => VDA[" | |
1092 << decoder_frames_at_client_ << "]"; | |
1093 | |
1094 ScheduleDecodeBufferTaskIfNeeded(); | |
1095 } | |
1096 | |
1097 void ExynosVideoDecodeAccelerator::EnqueueMfc() { | |
1098 DVLOG(3) << "EnqueueMfc()"; | |
1099 DCHECK_EQ(decoder_thread_.message_loop(), MessageLoop::current()); | |
1100 DCHECK_NE(decoder_state_, kUninitialized); | |
1101 TRACE_EVENT0("Video Decoder", "EVDA::EnqueueMfc"); | |
1102 | |
1103 // Drain the pipe of completed decode buffers. | |
1104 const int old_mfc_inputs_queued_ = mfc_input_buffer_queued_count_; | |
1105 while (!mfc_input_ready_queue_.empty()) { | |
1106 if (!EnqueueMfcInputRecord()) | |
1107 return; | |
1108 } | |
1109 if (old_mfc_inputs_queued_ == 0 && mfc_input_buffer_queued_count_ != 0) { | |
1110 // We just started up a previously empty queue. | |
1111 // Queue state changed; signal interrupt. | |
1112 if (!SetDevicePollInterrupt()) | |
1113 return; | |
1114 // Start VIDIOC_STREAMON if we haven't yet. | |
1115 if (!mfc_input_streamon_) { | |
1116 __u32 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1117 IOCTL_OR_ERROR_RETURN(mfc_fd_, VIDIOC_STREAMON, &type); | |
1118 mfc_input_streamon_ = true; | |
1119 } | |
1120 } | |
1121 | |
1122 // Enqueue all the MFC outputs we can. | |
1123 const int old_mfc_outputs_queued_ = mfc_output_buffer_queued_count_; | |
1124 while (!mfc_free_output_buffers_.empty()) { | |
1125 if (!EnqueueMfcOutputRecord()) | |
1126 return; | |
1127 } | |
1128 if (old_mfc_outputs_queued_ == 0 && mfc_output_buffer_queued_count_ != 0) { | |
1129 // We just started up a previously empty queue. | |
1130 // Queue state changed; signal interrupt. | |
1131 if (!SetDevicePollInterrupt()) | |
1132 return; | |
1133 // Start VIDIOC_STREAMON if we haven't yet. | |
1134 if (!mfc_output_streamon_) { | |
1135 __u32 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1136 IOCTL_OR_ERROR_RETURN(mfc_fd_, VIDIOC_STREAMON, &type); | |
1137 mfc_output_streamon_ = true; | |
1138 } | |
1139 } | |
1140 } | |
1141 | |
1142 void ExynosVideoDecodeAccelerator::DequeueMfc() { | |
1143 DVLOG(3) << "DequeueMfc()"; | |
1144 DCHECK_EQ(decoder_thread_.message_loop(), MessageLoop::current()); | |
1145 DCHECK_NE(decoder_state_, kUninitialized); | |
1146 TRACE_EVENT0("Video Decoder", "EVDA::DequeueMfc"); | |
1147 | |
1148 // Dequeue completed MFC input (VIDEO_OUTPUT) buffers, and recycle to the free | |
1149 // list. | |
1150 struct v4l2_buffer dqbuf; | |
1151 struct v4l2_plane planes[2]; | |
1152 while (mfc_input_buffer_queued_count_ > 0) { | |
1153 DCHECK(mfc_input_streamon_); | |
1154 memset(&dqbuf, 0, sizeof(dqbuf)); | |
1155 dqbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1156 dqbuf.memory = V4L2_MEMORY_MMAP; | |
1157 if (ioctl(mfc_fd_, VIDIOC_DQBUF, &dqbuf) != 0) { | |
1158 if (errno == EAGAIN) { | |
1159 // EAGAIN if we're just out of buffers to dequeue. | |
1160 break; | |
1161 } | |
1162 DPLOG(ERROR) << "DequeueMfc(): ioctl() failed: VIDIOC_DQBUF"; | |
1163 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1164 return; | |
1165 } | |
1166 MfcInputRecord& input_record = mfc_input_buffer_map_[dqbuf.index]; | |
1167 DCHECK(input_record.at_device); | |
1168 mfc_free_input_buffers_.push_back(dqbuf.index); | |
1169 input_record.at_device = false; | |
1170 input_record.bytes_used = 0; | |
1171 input_record.input_id = -1; | |
1172 mfc_input_buffer_queued_count_--; | |
1173 } | |
1174 | |
1175 // Dequeue completed MFC output (VIDEO_CAPTURE) buffers, and queue to the | |
1176 // completed queue. | |
1177 while (mfc_output_buffer_queued_count_ > 0) { | |
1178 DCHECK(mfc_output_streamon_); | |
1179 memset(&dqbuf, 0, sizeof(dqbuf)); | |
1180 memset(planes, 0, sizeof(planes)); | |
1181 dqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1182 dqbuf.memory = V4L2_MEMORY_MMAP; | |
1183 dqbuf.m.planes = planes; | |
1184 dqbuf.length = 2; | |
1185 if (ioctl(mfc_fd_, VIDIOC_DQBUF, &dqbuf) != 0) { | |
1186 if (errno == EAGAIN) { | |
1187 // EAGAIN if we're just out of buffers to dequeue. | |
1188 break; | |
1189 } | |
1190 DPLOG(ERROR) << "DequeueMfc(): ioctl() failed: VIDIOC_DQBUF"; | |
1191 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1192 return; | |
1193 } | |
1194 const long int input_id = dqbuf.timestamp.tv_sec; | |
1195 MfcOutputRecord& output_record = mfc_output_buffer_map_[dqbuf.index]; | |
1196 DCHECK(output_record.at_device); | |
1197 if (dqbuf.m.planes[0].bytesused + dqbuf.m.planes[1].bytesused == 0) { | |
1198 // This is an empty output buffer returned as part of a flush. | |
1199 mfc_free_output_buffers_.push_back(dqbuf.index); | |
1200 output_record.at_device = false; | |
1201 output_record.input_id = -1; | |
1202 output_record.bytes_used[0] = 0; | |
1203 output_record.bytes_used[1] = 0; | |
1204 } else { | |
1205 // This is an output buffer with contents to pass down the pipe. | |
1206 mfc_output_gsc_input_queue_.push_front(dqbuf.index); | |
1207 output_record.at_device = false; | |
1208 output_record.input_id = input_id; | |
1209 output_record.bytes_used[0] = dqbuf.m.planes[0].bytesused; | |
1210 output_record.bytes_used[1] = dqbuf.m.planes[1].bytesused; | |
1211 DVLOG(3) << "DequeueMfc(): dequeued input_id=" << input_id; | |
1212 // We don't count this output buffer dequeued yet, or add it to the free | |
1213 // list, as it has data GSC needs to process. | |
1214 | |
1215 // We have new frames in mfc_output_gsc_input_queue_. Kick the pipe. | |
1216 SetDevicePollInterrupt(); | |
1217 } | |
1218 mfc_output_buffer_queued_count_--; | |
1219 } | |
1220 | |
1221 NotifyFlushDoneIfNeeded(); | |
1222 } | |
1223 | |
1224 void ExynosVideoDecodeAccelerator::EnqueueGsc() { | |
1225 DVLOG(3) << "EnqueueGsc()"; | |
1226 DCHECK_EQ(decoder_thread_.message_loop(), MessageLoop::current()); | |
1227 DCHECK_NE(decoder_state_, kUninitialized); | |
1228 DCHECK_NE(decoder_state_, kInitialized); | |
1229 TRACE_EVENT0("Video Decoder", "EVDA::EnqueueGsc"); | |
1230 | |
1231 // Drain the pipe of completed MFC output buffers. | |
1232 const int old_gsc_inputs_queued_ = gsc_input_buffer_queued_count_; | |
1233 while (!mfc_output_gsc_input_queue_.empty()) { | |
1234 if (gsc_free_input_buffers_.empty()) | |
1235 break; | |
1236 if (!EnqueueGscInputRecord()) | |
1237 return; | |
1238 } | |
1239 if (old_gsc_inputs_queued_ == 0 && gsc_input_buffer_queued_count_ != 0) { | |
1240 // We just started up a previously empty queue. | |
1241 // Queue state changed; signal interrupt. | |
1242 if (!SetDevicePollInterrupt()) | |
1243 return; | |
1244 // Start VIDIOC_STREAMON if we haven't yet. | |
1245 if (!gsc_input_streamon_) { | |
1246 __u32 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1247 IOCTL_OR_ERROR_RETURN(gsc_fd_, VIDIOC_STREAMON, &type); | |
1248 gsc_input_streamon_ = true; | |
1249 } | |
1250 } | |
1251 | |
1252 // Enqueue a GSC output, only if we need one | |
1253 if (gsc_input_buffer_queued_count_ != 0 && | |
1254 gsc_output_buffer_queued_count_ == 0 && | |
1255 !gsc_free_output_buffers_.empty()) { | |
1256 const int old_gsc_outputs_queued_ = gsc_output_buffer_queued_count_; | |
1257 if (!EnqueueGscOutputRecord()) | |
1258 return; | |
1259 if (old_gsc_outputs_queued_ == 0 && gsc_output_buffer_queued_count_ != 0) { | |
1260 // We just started up a previously empty queue. | |
1261 // Queue state changed; signal interrupt. | |
1262 if (!SetDevicePollInterrupt()) | |
1263 return; | |
1264 // Start VIDIOC_STREAMON if we haven't yet. | |
1265 if (!gsc_output_streamon_) { | |
1266 __u32 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1267 IOCTL_OR_ERROR_RETURN(gsc_fd_, VIDIOC_STREAMON, &type); | |
1268 gsc_output_streamon_ = true; | |
1269 } | |
1270 } | |
1271 } | |
1272 // Bug check: GSC is liable to race conditions if more than one buffer is | |
1273 // simultaneously queued. | |
1274 DCHECK_GE(1, gsc_output_buffer_queued_count_); | |
1275 } | |
1276 | |
1277 void ExynosVideoDecodeAccelerator::DequeueGsc() { | |
1278 DVLOG(3) << "DequeueGsc()"; | |
1279 DCHECK_EQ(decoder_thread_.message_loop(), MessageLoop::current()); | |
1280 DCHECK_NE(decoder_state_, kUninitialized); | |
1281 DCHECK_NE(decoder_state_, kInitialized); | |
1282 DCHECK_NE(decoder_state_, kAfterReset); | |
1283 TRACE_EVENT0("Video Decoder", "EVDA::DequeueGsc"); | |
1284 | |
1285 // Dequeue completed GSC input (VIDEO_OUTPUT) buffers, and recycle to the free | |
1286 // list. Also recycle the corresponding MFC output buffers at this time. | |
1287 struct v4l2_buffer dqbuf; | |
1288 while (gsc_input_buffer_queued_count_ > 0) { | |
1289 DCHECK(gsc_input_streamon_); | |
1290 memset(&dqbuf, 0, sizeof(dqbuf)); | |
1291 dqbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1292 dqbuf.memory = V4L2_MEMORY_DMABUF; | |
1293 if (ioctl(gsc_fd_, VIDIOC_DQBUF, &dqbuf) != 0) { | |
1294 if (errno == EAGAIN) { | |
1295 // EAGAIN if we're just out of buffers to dequeue. | |
1296 break; | |
1297 } | |
1298 DPLOG(ERROR) << "DequeueGsc(): ioctl() failed: VIDIOC_DQBUF"; | |
1299 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1300 return; | |
1301 } | |
1302 GscInputRecord& input_record = gsc_input_buffer_map_[dqbuf.index]; | |
1303 MfcOutputRecord& output_record = | |
1304 mfc_output_buffer_map_[input_record.mfc_output]; | |
1305 DCHECK(input_record.at_device); | |
1306 gsc_free_input_buffers_.push_back(dqbuf.index); | |
1307 mfc_free_output_buffers_.push_back(input_record.mfc_output); | |
1308 input_record.at_device = false; | |
1309 input_record.mfc_output = -1; | |
1310 output_record.input_id = -1; | |
1311 gsc_input_buffer_queued_count_--; | |
1312 } | |
1313 | |
1314 // Dequeue completed GSC output (VIDEO_CAPTURE) buffers, and send them off to | |
1315 // the client. Don't recycle to its free list yet -- we can't do that until | |
1316 // ReusePictureBuffer() returns it to us. | |
1317 while (gsc_output_buffer_queued_count_ > 0) { | |
1318 DCHECK(gsc_output_streamon_); | |
1319 memset(&dqbuf, 0, sizeof(dqbuf)); | |
1320 dqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1321 dqbuf.memory = V4L2_MEMORY_DMABUF; | |
1322 if (ioctl(gsc_fd_, VIDIOC_DQBUF, &dqbuf) != 0) { | |
1323 if (errno == EAGAIN) { | |
1324 // EAGAIN if we're just out of buffers to dequeue. | |
1325 break; | |
1326 } | |
1327 DPLOG(ERROR) << "DequeueGsc(): ioctl() failed: VIDIOC_DQBUF"; | |
1328 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1329 return; | |
1330 } | |
1331 GscOutputRecord& output_record = gsc_output_buffer_map_[dqbuf.index]; | |
1332 DCHECK(output_record.at_device); | |
1333 DCHECK(!output_record.at_client); | |
1334 DCHECK_EQ(output_record.egl_sync, EGL_NO_SYNC_KHR); | |
1335 output_record.at_device = false; | |
1336 output_record.at_client = true; | |
1337 gsc_output_buffer_queued_count_--; | |
1338 child_message_loop_proxy_->PostTask(FROM_HERE, base::Bind( | |
1339 &Client::PictureReady, client_, media::Picture( | |
1340 output_record.picture_id, dqbuf.timestamp.tv_sec))); | |
1341 decoder_frames_at_client_++; | |
1342 } | |
1343 | |
1344 NotifyFlushDoneIfNeeded(); | |
1345 } | |
1346 | |
1347 void ExynosVideoDecodeAccelerator::NotifyFlushDoneIfNeeded() { | |
1348 if (!decoder_notify_flush_requested_) | |
1349 return; | |
1350 | |
1351 // Pipeline is empty when: | |
1352 // * Decoder input holding queue is empty. | |
1353 // * There is no currently filling input buffer. | |
1354 // * MFC input holding queue is empty. | |
1355 // * All MFC input (VIDEO_OUTPUT) buffers are returned. | |
1356 // * MFC -> GSC holding queue is empty. | |
1357 // * All GSC input (VIDEO_OUTPUT) buffers are returned. | |
1358 if (decoder_current_input_buffer_ != -1) | |
1359 return; | |
1360 if ((decoder_input_queue_.size() + mfc_input_ready_queue_.size() + | |
1361 mfc_input_buffer_queued_count_ + mfc_output_gsc_input_queue_.size() + | |
1362 gsc_input_buffer_queued_count_) != 0) | |
1363 return; | |
1364 | |
1365 child_message_loop_proxy_->PostTask(FROM_HERE, base::Bind( | |
1366 &Client::NotifyFlushDone, client_)); | |
1367 decoder_notify_flush_requested_ = false; | |
1368 } | |
1369 | |
1370 bool ExynosVideoDecodeAccelerator::EnqueueMfcInputRecord() { | |
1371 DVLOG(3) << "EnqueueMfcInputRecord()"; | |
1372 DCHECK(!mfc_input_ready_queue_.empty()); | |
1373 | |
1374 // Enqueue a MFC input (VIDEO_OUTPUT) buffer. | |
1375 const int buffer = mfc_input_ready_queue_.back(); | |
1376 MfcInputRecord& input_record = mfc_input_buffer_map_[buffer]; | |
1377 DCHECK(!input_record.at_device); | |
1378 struct v4l2_buffer qbuf; | |
1379 struct v4l2_plane qbuf_plane; | |
1380 memset(&qbuf, 0, sizeof(qbuf)); | |
1381 memset(&qbuf_plane, 0, sizeof(qbuf_plane)); | |
1382 qbuf.index = buffer; | |
1383 qbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1384 qbuf.timestamp.tv_sec = input_record.input_id; | |
1385 qbuf.memory = V4L2_MEMORY_MMAP; | |
1386 qbuf.m.planes = &qbuf_plane; | |
1387 qbuf.m.planes[0].bytesused = input_record.bytes_used; | |
1388 qbuf.length = 1; | |
1389 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_QBUF, &qbuf); | |
1390 mfc_input_ready_queue_.pop_back(); | |
1391 input_record.at_device = true; | |
1392 mfc_input_buffer_queued_count_++; | |
1393 DVLOG(3) << "EnqueueMfcInputRecord(): enqueued input_id=" | |
1394 << input_record.input_id; | |
1395 return true; | |
1396 } | |
1397 | |
1398 bool ExynosVideoDecodeAccelerator::EnqueueMfcOutputRecord() { | |
1399 DVLOG(3) << "EnqueueMfcOutputRecord()"; | |
1400 DCHECK(!mfc_free_output_buffers_.empty()); | |
1401 | |
1402 // Enqueue a MFC output (VIDEO_CAPTURE) buffer. | |
1403 const int buffer = mfc_free_output_buffers_.back(); | |
1404 MfcOutputRecord& output_record = mfc_output_buffer_map_[buffer]; | |
1405 DCHECK(!output_record.at_device); | |
1406 DCHECK_EQ(output_record.input_id, -1); | |
1407 struct v4l2_buffer qbuf; | |
1408 struct v4l2_plane qbuf_planes[2]; | |
1409 memset(&qbuf, 0, sizeof(qbuf)); | |
1410 memset(qbuf_planes, 0, sizeof(qbuf_planes)); | |
1411 qbuf.index = buffer; | |
1412 qbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1413 qbuf.memory = V4L2_MEMORY_MMAP; | |
1414 qbuf.m.planes = qbuf_planes; | |
1415 qbuf.length = 2; | |
1416 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_QBUF, &qbuf); | |
1417 mfc_free_output_buffers_.pop_back(); | |
1418 output_record.at_device = true; | |
1419 mfc_output_buffer_queued_count_++; | |
1420 return true; | |
1421 } | |
1422 | |
1423 bool ExynosVideoDecodeAccelerator::EnqueueGscInputRecord() { | |
1424 DVLOG(3) << "EnqueueGscInputRecord()"; | |
1425 DCHECK(!gsc_free_input_buffers_.empty()); | |
1426 | |
1427 // Enqueue a GSC input (VIDEO_OUTPUT) buffer for a complete MFC output | |
1428 // (VIDEO_CAPTURE) buffer. | |
1429 const int mfc_buffer = mfc_output_gsc_input_queue_.back(); | |
1430 const int gsc_buffer = gsc_free_input_buffers_.back(); | |
1431 MfcOutputRecord& output_record = mfc_output_buffer_map_[mfc_buffer]; | |
1432 DCHECK(!output_record.at_device); | |
1433 GscInputRecord& input_record = gsc_input_buffer_map_[gsc_buffer]; | |
1434 DCHECK(!input_record.at_device); | |
1435 DCHECK_EQ(input_record.mfc_output, -1); | |
1436 struct v4l2_buffer qbuf; | |
1437 struct v4l2_plane qbuf_planes[2]; | |
1438 memset(&qbuf, 0, sizeof(qbuf)); | |
1439 memset(qbuf_planes, 0, sizeof(qbuf_planes)); | |
1440 qbuf.index = gsc_buffer; | |
1441 qbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1442 qbuf.timestamp.tv_sec = output_record.input_id; | |
1443 qbuf.memory = V4L2_MEMORY_USERPTR; | |
1444 qbuf.m.planes = qbuf_planes; | |
1445 qbuf.m.planes[0].bytesused = output_record.bytes_used[0]; | |
1446 qbuf.m.planes[0].length = mfc_output_buffer_size_[0]; | |
1447 qbuf.m.planes[0].m.userptr = (unsigned long)output_record.address[0]; | |
1448 qbuf.m.planes[1].bytesused = output_record.bytes_used[1]; | |
1449 qbuf.m.planes[1].length = mfc_output_buffer_size_[1]; | |
1450 qbuf.m.planes[1].m.userptr = (unsigned long)output_record.address[1]; | |
1451 qbuf.length = 2; | |
1452 IOCTL_OR_ERROR_RETURN_FALSE(gsc_fd_, VIDIOC_QBUF, &qbuf); | |
1453 mfc_output_gsc_input_queue_.pop_back(); | |
1454 gsc_free_input_buffers_.pop_back(); | |
1455 input_record.at_device = true; | |
1456 input_record.mfc_output = mfc_buffer; | |
1457 output_record.bytes_used[0] = 0; | |
1458 output_record.bytes_used[1] = 0; | |
1459 gsc_input_buffer_queued_count_++; | |
1460 DVLOG(3) << "EnqueueGscInputRecord(): enqueued input_id=" | |
1461 << output_record.input_id; | |
1462 return true; | |
1463 } | |
1464 | |
1465 bool ExynosVideoDecodeAccelerator::EnqueueGscOutputRecord() { | |
1466 DVLOG(3) << "EnqueueGscOutputRecord()"; | |
1467 DCHECK(!gsc_free_output_buffers_.empty()); | |
1468 | |
1469 // Enqueue a GSC output (VIDEO_CAPTURE) buffer. | |
1470 const int buffer = gsc_free_output_buffers_.back(); | |
1471 GscOutputRecord& output_record = gsc_output_buffer_map_[buffer]; | |
1472 DCHECK(!output_record.at_device); | |
1473 DCHECK(!output_record.at_client); | |
1474 if (output_record.egl_sync != EGL_NO_SYNC_KHR) { | |
1475 TRACE_EVENT0( | |
1476 "Video Decoder", | |
1477 "EVDA::EnqueueGscOutputRecord: eglClientWaitSyncKHR"); | |
1478 // If we have to wait for completion, wait. Note that | |
1479 // gsc_free_output_buffers_ is a FIFO queue, so we always wait on the | |
1480 // buffer that has been in th queue the longest. | |
1481 egl_client_wait_sync_khr(egl_display_, output_record.egl_sync, 0, | |
1482 EGL_FOREVER_KHR); | |
1483 egl_destroy_sync_khr(egl_display_, output_record.egl_sync); | |
1484 output_record.egl_sync = EGL_NO_SYNC_KHR; | |
1485 } | |
1486 struct v4l2_buffer qbuf; | |
1487 struct v4l2_plane qbuf_plane; | |
1488 memset(&qbuf, 0, sizeof(qbuf)); | |
1489 memset(&qbuf_plane, 0, sizeof(qbuf_plane)); | |
1490 qbuf.index = buffer; | |
1491 qbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1492 qbuf.memory = V4L2_MEMORY_DMABUF; | |
1493 qbuf.m.planes = &qbuf_plane; | |
1494 qbuf.m.planes[0].m.fd = output_record.fd; | |
1495 qbuf.length = 1; | |
1496 IOCTL_OR_ERROR_RETURN_FALSE(gsc_fd_, VIDIOC_QBUF, &qbuf); | |
1497 gsc_free_output_buffers_.pop_back(); | |
1498 output_record.at_device = true; | |
1499 gsc_output_buffer_queued_count_++; | |
1500 return true; | |
1501 } | |
1502 | |
1503 void ExynosVideoDecodeAccelerator::ReusePictureBufferTask( | |
1504 int32 picture_buffer_id, scoped_ptr<EGLSyncKHRRef> egl_sync_ref) { | |
1505 DVLOG(3) << "ReusePictureBufferTask(): picture_buffer_id=" | |
1506 << picture_buffer_id; | |
1507 DCHECK_EQ(decoder_thread_.message_loop(), MessageLoop::current()); | |
1508 TRACE_EVENT0("Video Decoder", "EVDA::ReusePictureBufferTask"); | |
1509 | |
1510 // We run ReusePictureBufferTask even if we're in kResetting. | |
1511 if (decoder_state_ == kError) { | |
1512 DVLOG(2) << "ReusePictureBufferTask(): early out: kError state"; | |
1513 return; | |
1514 } | |
1515 | |
1516 size_t index; | |
1517 for (index = 0; index < gsc_output_buffer_map_.size(); ++index) | |
1518 if (gsc_output_buffer_map_[index].picture_id == picture_buffer_id) | |
1519 break; | |
1520 | |
1521 if (index >= gsc_output_buffer_map_.size()) { | |
1522 DLOG(ERROR) << "ReusePictureBufferTask(): picture_buffer_id not found"; | |
1523 NOTIFY_ERROR(INVALID_ARGUMENT); | |
1524 return; | |
1525 } | |
1526 | |
1527 GscOutputRecord& output_record = gsc_output_buffer_map_[index]; | |
1528 DCHECK(!output_record.at_device); | |
1529 DCHECK(output_record.at_client); | |
1530 DCHECK_EQ(output_record.egl_sync, EGL_NO_SYNC_KHR); | |
1531 output_record.at_client = false; | |
1532 output_record.egl_sync = egl_sync_ref->egl_sync; | |
1533 gsc_free_output_buffers_.push_front(index); | |
1534 decoder_frames_at_client_--; | |
1535 // Take ownership of the EGLSync. | |
1536 egl_sync_ref->egl_sync = EGL_NO_SYNC_KHR; | |
1537 // We got a buffer back, so kick the GSC. | |
1538 EnqueueGsc(); | |
1539 } | |
1540 | |
1541 void ExynosVideoDecodeAccelerator::FlushTask() { | |
1542 DVLOG(3) << "FlushTask()"; | |
1543 TRACE_EVENT0("Video Decoder", "EVDA::FlushTask"); | |
1544 | |
1545 // Flush the currently-building frame. | |
1546 if (decoder_state_ == kResetting) { | |
1547 DVLOG(2) << "FlushTask(): early out: kResetting state"; | |
1548 return; | |
1549 } else if (decoder_state_ == kError) { | |
1550 DVLOG(2) << "FlushTask(): early out: kError state"; | |
1551 return; | |
1552 } | |
1553 | |
1554 // Queue up an empty buffer -- this triggers the flush. | |
1555 decoder_input_queue_.push_front(linked_ptr<BitstreamBufferRef>( | |
1556 new BitstreamBufferRef(client_, child_message_loop_proxy_, NULL, 0, -1))); | |
1557 // We'll flag that we want a flush-finished notification, and just return. | |
1558 decoder_notify_flush_requested_ = true; | |
1559 | |
1560 ScheduleDecodeBufferTaskIfNeeded(); | |
1561 } | |
1562 | |
1563 void ExynosVideoDecodeAccelerator::ResetTask() { | |
1564 DVLOG(3) << "ResetTask()"; | |
1565 DCHECK_EQ(decoder_thread_.message_loop(), MessageLoop::current()); | |
1566 TRACE_EVENT0("Video Decoder", "EVDA::ResetTask"); | |
1567 | |
1568 if (decoder_state_ == kError) { | |
1569 DVLOG(2) << "DecodeTask(): early out: kError state"; | |
1570 return; | |
1571 } | |
1572 | |
1573 // We stop streaming, but we _don't_ destroy our buffers. | |
1574 if (!StopDevicePoll()) | |
1575 return; | |
1576 | |
1577 decoder_current_bitstream_buffer_.reset(); | |
1578 decoder_input_queue_.clear(); | |
1579 | |
1580 decoder_current_input_buffer_ = -1; | |
1581 decoder_decode_buffer_tasks_scheduled_ = 0; | |
1582 decoder_notify_flush_requested_ = false; | |
1583 | |
1584 // Mark that we're resetting, then enqueue a ResetDoneTask(). All intervening | |
1585 // jobs will early-out in the kResetting state. | |
1586 decoder_state_ = kResetting; | |
1587 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
1588 &ExynosVideoDecodeAccelerator::ResetDoneTask, base::Unretained(this))); | |
1589 } | |
1590 | |
1591 void ExynosVideoDecodeAccelerator::ResetDoneTask() { | |
1592 DVLOG(3) << "ResetDoneTask()"; | |
1593 DCHECK_EQ(decoder_thread_.message_loop(), MessageLoop::current()); | |
1594 TRACE_EVENT0("Video DecodeR", "EVDA::ResetDoneTask"); | |
1595 | |
1596 if (decoder_state_ == kError) { | |
1597 DVLOG(2) << "DecodeTask(): early out: kError state"; | |
1598 return; | |
1599 } | |
1600 | |
1601 // Reset format-specific bits. | |
1602 if (video_profile_ >= media::H264PROFILE_MIN && | |
1603 video_profile_ <= media::H264PROFILE_MAX) { | |
1604 decoder_h264_parser_.reset(new content::H264Parser()); | |
1605 } | |
1606 | |
1607 // Jobs drained, we're finished resetting. | |
1608 DCHECK_EQ(decoder_state_, kResetting); | |
1609 decoder_state_ = kAfterReset; | |
1610 child_message_loop_proxy_->PostTask(FROM_HERE, base::Bind( | |
1611 &Client::NotifyResetDone, client_)); | |
1612 } | |
1613 | |
1614 void ExynosVideoDecodeAccelerator::DestroyTask() { | |
1615 DVLOG(3) << "DestroyTask()"; | |
1616 TRACE_EVENT0("Video Decoder", "EVDA::DestroyTask"); | |
1617 | |
1618 // DestroyTask() should run regardless of decoder_state_. | |
1619 | |
1620 // Stop streaming and the device_poll_thread_. | |
1621 StopDevicePoll(); | |
1622 | |
1623 decoder_current_bitstream_buffer_.reset(); | |
1624 decoder_current_input_buffer_ = -1; | |
1625 decoder_decode_buffer_tasks_scheduled_ = 0; | |
1626 decoder_frames_at_client_ = 0; | |
1627 decoder_notify_flush_requested_ = false; | |
1628 decoder_input_queue_.clear(); | |
1629 | |
1630 // Set our state to kError. This will cause all subsequent tasks to | |
1631 // early-exit. | |
1632 decoder_state_ = kError; | |
1633 } | |
1634 | |
1635 bool ExynosVideoDecodeAccelerator::StartDevicePoll() { | |
1636 DVLOG(3) << "StartDevicePoll()"; | |
1637 DCHECK_EQ(decoder_thread_.message_loop(), MessageLoop::current()); | |
1638 | |
1639 // Early-out if we're already running. | |
1640 if (device_poll_thread_.IsRunning()) { | |
1641 DVLOG(2) << "StartDevicePoll(): early out: " | |
1642 << "device poll thread already running"; | |
1643 return true; | |
1644 } | |
1645 | |
1646 // Start up the device poll thread and schedule its first DevicePollTask(). | |
1647 if (!device_poll_thread_.Start()) { | |
1648 DLOG(ERROR) << "StartDevicePoll(): Device thread failed to start"; | |
1649 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1650 return false; | |
1651 } | |
1652 device_poll_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
1653 &ExynosVideoDecodeAccelerator::DevicePollTask, | |
1654 base::Unretained(this), | |
1655 0)); | |
1656 | |
1657 return true; | |
1658 } | |
1659 | |
1660 bool ExynosVideoDecodeAccelerator::StopDevicePoll() { | |
1661 DVLOG(3) << "StopDevicePoll()"; | |
1662 DCHECK_EQ(decoder_thread_.message_loop(), MessageLoop::current()); | |
1663 | |
1664 // Signal the DevicePollTask() to stop, and stop the device poll thread. | |
1665 if (!SetDevicePollInterrupt()) | |
1666 return false; | |
1667 device_poll_thread_.Stop(); | |
1668 // Clear the interrupt now, to be sure. | |
1669 if (!ClearDevicePollInterrupt()) | |
1670 return false; | |
1671 | |
1672 // Stop streaming. | |
1673 if (mfc_input_streamon_) { | |
1674 __u32 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1675 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_STREAMOFF, &type); | |
1676 } | |
1677 mfc_input_streamon_ = false; | |
1678 if (mfc_output_streamon_) { | |
1679 __u32 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1680 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_STREAMOFF, &type); | |
1681 } | |
1682 mfc_output_streamon_ = false; | |
1683 if (gsc_input_streamon_) { | |
1684 __u32 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1685 IOCTL_OR_ERROR_RETURN_FALSE(gsc_fd_, VIDIOC_STREAMOFF, &type); | |
1686 } | |
1687 gsc_input_streamon_ = false; | |
1688 if (gsc_output_streamon_) { | |
1689 __u32 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1690 IOCTL_OR_ERROR_RETURN_FALSE(gsc_fd_, VIDIOC_STREAMOFF, &type); | |
1691 } | |
1692 gsc_output_streamon_ = false; | |
1693 | |
1694 // Reset all our accounting info. | |
1695 mfc_input_ready_queue_.clear(); | |
1696 mfc_free_input_buffers_.clear(); | |
1697 DCHECK_EQ(mfc_input_buffer_count_, | |
1698 static_cast<int>(mfc_input_buffer_map_.size())); | |
1699 for (size_t i = 0; i < mfc_input_buffer_map_.size(); ++i) { | |
1700 mfc_free_input_buffers_.push_back(i); | |
1701 mfc_input_buffer_map_[i].at_device = false; | |
1702 mfc_input_buffer_map_[i].bytes_used = 0; | |
1703 mfc_input_buffer_map_[i].input_id = -1; | |
1704 } | |
1705 mfc_input_buffer_queued_count_ = 0; | |
1706 mfc_free_output_buffers_.clear(); | |
1707 DCHECK_EQ(mfc_output_buffer_count_, | |
1708 static_cast<int>(mfc_output_buffer_map_.size())); | |
1709 for (size_t i = 0; i < mfc_output_buffer_map_.size(); ++i) { | |
1710 mfc_free_output_buffers_.push_back(i); | |
1711 mfc_output_buffer_map_[i].at_device = false; | |
1712 mfc_output_buffer_map_[i].input_id = -1; | |
1713 } | |
1714 mfc_output_buffer_queued_count_ = 0; | |
1715 mfc_output_gsc_input_queue_.clear(); | |
1716 gsc_free_input_buffers_.clear(); | |
1717 DCHECK_EQ(gsc_input_buffer_count_, | |
1718 static_cast<int>(gsc_input_buffer_map_.size())); | |
1719 for (size_t i = 0; i < gsc_input_buffer_map_.size(); ++i) { | |
1720 gsc_free_input_buffers_.push_back(i); | |
1721 gsc_input_buffer_map_[i].at_device = false; | |
1722 gsc_input_buffer_map_[i].mfc_output = -1; | |
1723 } | |
1724 gsc_input_buffer_queued_count_ = 0; | |
1725 gsc_free_output_buffers_.clear(); | |
1726 DCHECK_EQ(gsc_output_buffer_count_, | |
1727 static_cast<int>(gsc_output_buffer_map_.size())); | |
1728 for (size_t i = 0; i < gsc_output_buffer_map_.size(); ++i) { | |
1729 // Only mark those free that aren't being held by the VDA. | |
1730 if (!gsc_output_buffer_map_[i].at_client) { | |
1731 gsc_free_output_buffers_.push_back(i); | |
1732 gsc_output_buffer_map_[i].at_device = false; | |
1733 } | |
1734 } | |
1735 gsc_output_buffer_queued_count_ = 0; | |
1736 | |
1737 DVLOG(3) << "StopDevicePoll(): device poll stopped"; | |
1738 return true; | |
1739 } | |
1740 | |
1741 bool ExynosVideoDecodeAccelerator::SetDevicePollInterrupt() { | |
1742 DVLOG(3) << "SetDevicePollInterrupt()"; | |
1743 ssize_t ret; | |
1744 const uint64 buf = 1; | |
1745 ret = write(device_poll_interrupt_fd_, &buf, sizeof(buf)); | |
1746 if (ret == -1) { | |
1747 DPLOG(ERROR) << "SetDevicePollInterrupt(): write() failed"; | |
1748 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1749 return false; | |
1750 } | |
1751 return true; | |
1752 } | |
1753 | |
1754 bool ExynosVideoDecodeAccelerator::ClearDevicePollInterrupt() { | |
1755 DVLOG(3) << "ClearDevicePollInterrupt()"; | |
1756 int ret; | |
1757 uint64 buf; | |
1758 ret = read(device_poll_interrupt_fd_, &buf, sizeof(buf)); | |
1759 if (ret == -1) { | |
1760 if (errno == EAGAIN) { | |
1761 // No interrupt flag set, and we're reading nonblocking. Not an error. | |
1762 return true; | |
1763 } else { | |
1764 DPLOG(ERROR) << "ClearDevicePollInterrupt(): read() failed"; | |
1765 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1766 return false; | |
1767 } | |
1768 } | |
1769 return true; | |
1770 } | |
1771 | |
1772 void ExynosVideoDecodeAccelerator::DevicePollTask(unsigned int poll_fds) { | |
1773 DVLOG(3) << "DevicePollTask()"; | |
1774 DCHECK_EQ(device_poll_thread_.message_loop(), MessageLoop::current()); | |
1775 TRACE_EVENT0("Video Decoder", "EVDA::DevicePollTask"); | |
1776 | |
1777 // This routine just polls and the set of device fds, and schedules a | |
1778 // ServiceDeviceTask() on decoder_thread_ when processing needs to occur. | |
1779 // Other threads may notify this task to return early by writing to | |
1780 // device_poll_interrupt_fd_. | |
1781 struct pollfd pollfds[3]; | |
1782 nfds_t nfds; | |
1783 int ret; | |
1784 | |
1785 // Add device_poll_interrupt_fd_; | |
1786 pollfds[0].fd = device_poll_interrupt_fd_; | |
1787 pollfds[0].events = POLLIN | POLLERR; | |
1788 nfds = 1; | |
1789 | |
1790 if (poll_fds & kPollMfc) { | |
1791 DVLOG(3) << "DevicePollTask(): adding MFC to poll() set"; | |
1792 pollfds[nfds].fd = mfc_fd_; | |
1793 pollfds[nfds].events = POLLIN | POLLOUT | POLLERR; | |
1794 nfds++; | |
1795 } | |
1796 // Add GSC fd, if we should poll on it. | |
1797 // GSC has to wait until both input and output buffers are queued. | |
1798 if (poll_fds & kPollGsc) { | |
1799 DVLOG(3) << "DevicePollTask(): adding GSC to poll() set"; | |
1800 pollfds[nfds].fd = gsc_fd_; | |
1801 pollfds[nfds].events = POLLIN | POLLOUT | POLLERR; | |
1802 nfds++; | |
1803 } | |
1804 | |
1805 // Poll it! | |
1806 do { | |
1807 ret = poll(pollfds, nfds, -1); | |
1808 } while (ret < 1 && errno == EINTR); | |
1809 if (ret == -1) { | |
1810 DPLOG(ERROR) << "DevicePollTask(): poll() failed"; | |
1811 NOTIFY_ERROR(PLATFORM_FAILURE); | |
1812 return; | |
1813 } | |
1814 | |
1815 // All processing should happen on ServiceDeviceTask(), since we shouldn't | |
1816 // touch decoder state from this thread. | |
1817 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
1818 &ExynosVideoDecodeAccelerator::ServiceDeviceTask, | |
1819 base::Unretained(this))); | |
1820 } | |
1821 | |
1822 void ExynosVideoDecodeAccelerator::NotifyError(Error error) { | |
1823 DVLOG(2) << "NotifyError()"; | |
1824 | |
1825 if (!child_message_loop_proxy_->BelongsToCurrentThread()) { | |
1826 child_message_loop_proxy_->PostTask(FROM_HERE, base::Bind( | |
1827 &ExynosVideoDecodeAccelerator::NotifyError, weak_this_, error)); | |
1828 return; | |
1829 } | |
1830 | |
1831 if (client_) { | |
1832 client_->NotifyError(error); | |
1833 client_ptr_factory_.InvalidateWeakPtrs(); | |
1834 } | |
1835 } | |
1836 | |
1837 void ExynosVideoDecodeAccelerator::SetDecoderState(State state) { | |
1838 DVLOG(3) << "SetDecoderState(): state=%d" << state; | |
1839 | |
1840 // We can touch decoder_state_ only if this is the decoder thread or the | |
1841 // decoder thread isn't running. | |
1842 if (decoder_thread_.message_loop() != NULL && | |
1843 decoder_thread_.message_loop() != MessageLoop::current()) { | |
1844 decoder_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
1845 &ExynosVideoDecodeAccelerator::SetDecoderState, | |
1846 base::Unretained(this), state)); | |
1847 } else { | |
1848 decoder_state_ = state; | |
1849 } | |
1850 } | |
1851 | |
1852 bool ExynosVideoDecodeAccelerator::CreateMfcInputBuffers() { | |
1853 DVLOG(3) << "CreateMfcInputBuffers()"; | |
1854 // We always run this as we prepare to initialize. | |
1855 DCHECK_EQ(decoder_state_, kUninitialized); | |
1856 DCHECK(!mfc_input_streamon_); | |
1857 DCHECK_EQ(mfc_input_buffer_count_, 0); | |
1858 | |
1859 __u32 pixelformat = 0; | |
1860 if (video_profile_ >= media::H264PROFILE_MIN && | |
1861 video_profile_ <= media::H264PROFILE_MAX) { | |
1862 pixelformat = V4L2_PIX_FMT_H264; | |
1863 } else if (video_profile_ >= media::VP8PROFILE_MIN && | |
1864 video_profile_ <= media::VP8PROFILE_MAX) { | |
1865 pixelformat = V4L2_PIX_FMT_VP8; | |
1866 } else { | |
1867 NOTREACHED(); | |
1868 } | |
1869 | |
1870 struct v4l2_format format; | |
1871 memset(&format, 0, sizeof(format)); | |
1872 format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1873 format.fmt.pix_mp.pixelformat = pixelformat; | |
1874 format.fmt.pix_mp.plane_fmt[0].sizeimage = kMfcInputBufferMaxSize; | |
1875 format.fmt.pix_mp.num_planes = 1; | |
1876 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_S_FMT, &format); | |
1877 | |
1878 struct v4l2_requestbuffers reqbufs; | |
1879 memset(&reqbufs, 0, sizeof(reqbufs)); | |
1880 reqbufs.count = kMfcInputBufferCount; | |
1881 reqbufs.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1882 reqbufs.memory = V4L2_MEMORY_MMAP; | |
1883 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_REQBUFS, &reqbufs); | |
1884 mfc_input_buffer_count_ = reqbufs.count; | |
1885 mfc_input_buffer_map_.resize(mfc_input_buffer_count_); | |
1886 for (int i = 0; i < mfc_input_buffer_count_; ++i) { | |
1887 mfc_free_input_buffers_.push_back(i); | |
1888 | |
1889 // Query for the MEMORY_MMAP pointer. | |
1890 struct v4l2_plane planes[1]; | |
1891 struct v4l2_buffer buffer; | |
1892 memset(&buffer, 0, sizeof(buffer)); | |
1893 memset(planes, 0, sizeof(planes)); | |
1894 buffer.index = i; | |
1895 buffer.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1896 buffer.memory = V4L2_MEMORY_MMAP; | |
1897 buffer.m.planes = planes; | |
1898 buffer.length = 1; | |
1899 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_QUERYBUF, &buffer); | |
1900 void* address = mmap(NULL, buffer.m.planes[0].length, | |
1901 PROT_READ | PROT_WRITE, MAP_SHARED, mfc_fd_, | |
1902 buffer.m.planes[0].m.mem_offset); | |
1903 if (address == MAP_FAILED) { | |
1904 DPLOG(ERROR) << "CreateMfcInputBuffers(): mmap() failed"; | |
1905 return false; | |
1906 } | |
1907 mfc_input_buffer_map_[i].address = address; | |
1908 mfc_input_buffer_map_[i].length = buffer.m.planes[0].length; | |
1909 } | |
1910 | |
1911 return true; | |
1912 } | |
1913 | |
1914 bool ExynosVideoDecodeAccelerator::CreateMfcOutputBuffers() { | |
1915 DVLOG(3) << "CreateMfcOutputBuffers()"; | |
1916 DCHECK_EQ(decoder_state_, kInitialized); | |
1917 DCHECK(!mfc_output_streamon_); | |
1918 DCHECK_EQ(mfc_output_buffer_count_, 0); | |
1919 | |
1920 // Number of MFC output buffers we need. | |
1921 struct v4l2_control ctrl; | |
1922 memset(&ctrl, 0, sizeof(ctrl)); | |
1923 ctrl.id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE; | |
1924 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_G_CTRL, &ctrl); | |
1925 | |
1926 // Output format setup in Initialize(). | |
1927 | |
1928 // Allocate the output buffers. | |
1929 struct v4l2_requestbuffers reqbufs; | |
1930 memset(&reqbufs, 0, sizeof(reqbufs)); | |
1931 reqbufs.count = ctrl.value + kMfcOutputBufferExtraCount; | |
1932 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1933 reqbufs.memory = V4L2_MEMORY_MMAP; | |
1934 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_REQBUFS, &reqbufs); | |
1935 | |
1936 // Fill our free-buffers list, and create DMABUFs from them. | |
1937 mfc_output_buffer_count_ = reqbufs.count; | |
1938 mfc_output_buffer_map_.resize(mfc_output_buffer_count_); | |
1939 for (int i = 0; i < mfc_output_buffer_count_; ++i) { | |
1940 mfc_free_output_buffers_.push_back(i); | |
1941 | |
1942 // Query for the MEMORY_MMAP pointer. | |
1943 struct v4l2_plane planes[2]; | |
1944 struct v4l2_buffer buffer; | |
1945 memset(&buffer, 0, sizeof(buffer)); | |
1946 memset(planes, 0, sizeof(planes)); | |
1947 buffer.index = i; | |
1948 buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1949 buffer.memory = V4L2_MEMORY_MMAP; | |
1950 buffer.m.planes = planes; | |
1951 buffer.length = 2; | |
1952 IOCTL_OR_ERROR_RETURN_FALSE(mfc_fd_, VIDIOC_QUERYBUF, &buffer); | |
1953 | |
1954 // Get their user memory for GSC input. | |
1955 for (int j = 0; j < 2; ++j) { | |
1956 void* address = mmap(NULL, buffer.m.planes[j].length, | |
1957 PROT_READ | PROT_WRITE, MAP_SHARED, mfc_fd_, | |
1958 buffer.m.planes[j].m.mem_offset); | |
1959 if (address == MAP_FAILED) { | |
1960 DPLOG(ERROR) << "CreateMfcInputBuffers(): mmap() failed"; | |
1961 return false; | |
1962 } | |
1963 mfc_output_buffer_map_[i].address[j] = address; | |
1964 mfc_output_buffer_map_[i].length[j] = buffer.m.planes[j].length; | |
1965 } | |
1966 } | |
1967 | |
1968 return true; | |
1969 } | |
1970 | |
1971 bool ExynosVideoDecodeAccelerator::CreateGscInputBuffers() { | |
1972 DVLOG(3) << "CreateGscInputBuffers()"; | |
1973 DCHECK_EQ(decoder_state_, kInitialized); | |
1974 DCHECK(!gsc_input_streamon_); | |
1975 DCHECK_EQ(gsc_input_buffer_count_, 0); | |
1976 | |
1977 struct v4l2_format format; | |
1978 memset(&format, 0, sizeof(format)); | |
1979 format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1980 format.fmt.pix_mp.width = frame_buffer_size_.width(); | |
1981 format.fmt.pix_mp.height = frame_buffer_size_.height(); | |
1982 DCHECK_EQ(mfc_output_buffer_pixelformat_, V4L2_PIX_FMT_NV12MT_16X16); | |
1983 format.fmt.pix_mp.pixelformat = mfc_output_buffer_pixelformat_; | |
1984 format.fmt.pix_mp.plane_fmt[0].sizeimage = mfc_output_buffer_size_[0]; | |
1985 format.fmt.pix_mp.plane_fmt[1].sizeimage = mfc_output_buffer_size_[1]; | |
1986 // NV12MT_16X16 is a tiled format for which bytesperline doesn't make too much | |
1987 // sense. Convention seems to be to assume 8bpp for these tiled formats. | |
1988 format.fmt.pix_mp.plane_fmt[0].bytesperline = frame_buffer_size_.width(); | |
1989 format.fmt.pix_mp.plane_fmt[1].bytesperline = frame_buffer_size_.width(); | |
1990 format.fmt.pix_mp.num_planes = 2; | |
1991 IOCTL_OR_ERROR_RETURN_FALSE(gsc_fd_, VIDIOC_S_FMT, &format); | |
1992 | |
1993 struct v4l2_control control; | |
1994 memset(&control, 0, sizeof(control)); | |
1995 control.id = V4L2_CID_ROTATE; | |
1996 control.value = 0; | |
1997 IOCTL_OR_ERROR_RETURN_FALSE(gsc_fd_, VIDIOC_S_CTRL, &control); | |
1998 | |
1999 memset(&control, 0, sizeof(control)); | |
2000 control.id = V4L2_CID_HFLIP; | |
2001 control.value = 0; | |
2002 IOCTL_OR_ERROR_RETURN_FALSE(gsc_fd_, VIDIOC_S_CTRL, &control); | |
2003 | |
2004 memset(&control, 0, sizeof(control)); | |
2005 control.id = V4L2_CID_VFLIP; | |
2006 control.value = 0; | |
2007 IOCTL_OR_ERROR_RETURN_FALSE(gsc_fd_, VIDIOC_S_CTRL, &control); | |
2008 | |
2009 memset(&control, 0, sizeof(control)); | |
2010 control.id = V4L2_CID_GLOBAL_ALPHA; | |
2011 control.value = 255; | |
2012 IOCTL_OR_ERROR_RETURN_FALSE(gsc_fd_, VIDIOC_S_CTRL, &control); | |
2013 | |
2014 struct v4l2_requestbuffers reqbufs; | |
2015 memset(&reqbufs, 0, sizeof(reqbufs)); | |
2016 reqbufs.count = kGscInputBufferCount; | |
2017 reqbufs.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
2018 reqbufs.memory = V4L2_MEMORY_USERPTR; | |
2019 IOCTL_OR_ERROR_RETURN_FALSE(gsc_fd_, VIDIOC_REQBUFS, &reqbufs); | |
2020 | |
2021 gsc_input_buffer_count_ = reqbufs.count; | |
2022 gsc_input_buffer_map_.resize(gsc_input_buffer_count_); | |
2023 for (int i = 0; i < gsc_input_buffer_count_; ++i) { | |
2024 gsc_free_input_buffers_.push_back(i); | |
2025 gsc_input_buffer_map_[i].mfc_output = -1; | |
2026 } | |
2027 | |
2028 return true; | |
2029 } | |
2030 | |
2031 bool ExynosVideoDecodeAccelerator::CreateGscOutputBuffers() { | |
2032 DVLOG(3) << "CreateGscOutputBuffers()"; | |
2033 DCHECK_EQ(decoder_state_, kInitialized); | |
2034 DCHECK(!gsc_output_streamon_); | |
2035 DCHECK_EQ(gsc_output_buffer_count_, 0); | |
2036 | |
2037 // GSC outputs into the EGLImages we create from the textures we are | |
2038 // assigned. Assume RGBA8888 format. | |
2039 struct v4l2_format format; | |
2040 memset(&format, 0, sizeof(format)); | |
2041 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
2042 format.fmt.pix_mp.width = frame_buffer_size_.width(); | |
2043 format.fmt.pix_mp.height = frame_buffer_size_.height(); | |
2044 format.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_RGB32; | |
2045 format.fmt.pix_mp.plane_fmt[0].sizeimage = | |
2046 frame_buffer_size_.width() * frame_buffer_size_.height() * 4; | |
2047 format.fmt.pix_mp.plane_fmt[0].bytesperline = frame_buffer_size_.width() * 4; | |
2048 format.fmt.pix_mp.num_planes = 1; | |
2049 IOCTL_OR_ERROR_RETURN_FALSE(gsc_fd_, VIDIOC_S_FMT, &format); | |
2050 | |
2051 struct v4l2_requestbuffers reqbufs; | |
2052 memset(&reqbufs, 0, sizeof(reqbufs)); | |
2053 reqbufs.count = kGscOutputBufferCount; | |
2054 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
2055 reqbufs.memory = V4L2_MEMORY_DMABUF; | |
2056 IOCTL_OR_ERROR_RETURN_FALSE(gsc_fd_, VIDIOC_REQBUFS, &reqbufs); | |
2057 | |
2058 // We don't actually fill in the freelist or the map here. That happens once | |
2059 // we have actual usable buffers, after AssignPictureBuffers(); | |
2060 gsc_output_buffer_count_ = reqbufs.count; | |
2061 gsc_output_buffer_map_.resize(gsc_output_buffer_count_); | |
2062 | |
2063 DVLOG(3) << "CreateGscOutputBuffers(): ProvidePictureBuffers(): " | |
2064 << "buffer_count=" << gsc_output_buffer_count_ | |
2065 << ", width=" << frame_buffer_size_.width() | |
2066 << ", height=" << frame_buffer_size_.height(); | |
2067 child_message_loop_proxy_->PostTask(FROM_HERE, base::Bind( | |
2068 &Client::ProvidePictureBuffers, client_, gsc_output_buffer_count_, | |
2069 gfx::Size(frame_buffer_size_.width(), frame_buffer_size_.height()), | |
2070 GL_TEXTURE_2D)); | |
2071 | |
2072 return true; | |
2073 } | |
2074 | |
2075 void ExynosVideoDecodeAccelerator::DestroyMfcInputBuffers() { | |
2076 DVLOG(3) << "DestroyMfcInputBuffers()"; | |
2077 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); | |
2078 DCHECK(!mfc_input_streamon_); | |
2079 | |
2080 for (size_t i = 0; i < mfc_input_buffer_map_.size(); ++i) { | |
2081 if (mfc_input_buffer_map_[i].address != NULL) { | |
2082 munmap(mfc_input_buffer_map_[i].address, | |
2083 mfc_input_buffer_map_[i].length); | |
2084 } | |
2085 } | |
2086 | |
2087 struct v4l2_requestbuffers reqbufs; | |
2088 memset(&reqbufs, 0, sizeof(reqbufs)); | |
2089 reqbufs.count = 0; | |
2090 reqbufs.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
2091 reqbufs.memory = V4L2_MEMORY_MMAP; | |
2092 if (ioctl(mfc_fd_, VIDIOC_REQBUFS, &reqbufs) != 0) | |
2093 DPLOG(ERROR) << "DestroyMfcInputBuffers(): ioctl() failed: VIDIOC_REQBUFS"; | |
2094 | |
2095 mfc_input_buffer_map_.clear(); | |
2096 mfc_free_input_buffers_.clear(); | |
2097 mfc_input_buffer_count_ = 0; | |
2098 } | |
2099 | |
2100 void ExynosVideoDecodeAccelerator::DestroyMfcOutputBuffers() { | |
2101 DVLOG(3) << "DestroyMfcOutputBuffers()"; | |
2102 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); | |
2103 DCHECK(!mfc_output_streamon_); | |
2104 | |
2105 for (size_t i = 0; i < mfc_output_buffer_map_.size(); ++i) { | |
2106 if (mfc_output_buffer_map_[i].address[0] != NULL) | |
2107 munmap(mfc_output_buffer_map_[i].address[0], | |
2108 mfc_output_buffer_map_[i].length[0]); | |
2109 if (mfc_output_buffer_map_[i].address[1] != NULL) | |
2110 munmap(mfc_output_buffer_map_[i].address[1], | |
2111 mfc_output_buffer_map_[i].length[1]); | |
2112 } | |
2113 | |
2114 struct v4l2_requestbuffers reqbufs; | |
2115 memset(&reqbufs, 0, sizeof(reqbufs)); | |
2116 reqbufs.count = 0; | |
2117 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
2118 reqbufs.memory = V4L2_MEMORY_MMAP; | |
2119 if (ioctl(mfc_fd_, VIDIOC_REQBUFS, &reqbufs) != 0) | |
2120 DPLOG(ERROR) << "DestroyMfcOutputBuffers() ioctl() failed: VIDIOC_REQBUFS"; | |
2121 | |
2122 mfc_output_buffer_map_.clear(); | |
2123 mfc_free_output_buffers_.clear(); | |
2124 mfc_output_buffer_count_ = 0; | |
2125 } | |
2126 | |
2127 void ExynosVideoDecodeAccelerator::DestroyGscInputBuffers() { | |
2128 DVLOG(3) << "DestroyGscInputBuffers()"; | |
2129 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); | |
2130 DCHECK(!gsc_input_streamon_); | |
2131 | |
2132 struct v4l2_requestbuffers reqbufs; | |
2133 memset(&reqbufs, 0, sizeof(reqbufs)); | |
2134 reqbufs.count = 0; | |
2135 reqbufs.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
2136 reqbufs.memory = V4L2_MEMORY_DMABUF; | |
2137 if (ioctl(gsc_fd_, VIDIOC_REQBUFS, &reqbufs) != 0) | |
2138 DPLOG(ERROR) << "DestroyGscInputBuffers(): ioctl() failed: VIDIOC_REQBUFS"; | |
2139 | |
2140 gsc_input_buffer_map_.clear(); | |
2141 gsc_free_input_buffers_.clear(); | |
2142 gsc_input_buffer_count_ = 0; | |
2143 } | |
2144 | |
2145 void ExynosVideoDecodeAccelerator::DestroyGscOutputBuffers() { | |
2146 DVLOG(3) << "DestroyGscOutputBuffers()"; | |
2147 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); | |
2148 DCHECK(!gsc_output_streamon_); | |
2149 | |
2150 if (gsc_output_buffer_map_.size() != 0) { | |
2151 if (!make_context_current_.Run()) | |
2152 DLOG(ERROR) << "DestroyGscOutputBuffers(): " | |
2153 << "could not make context current"; | |
2154 | |
2155 size_t i = 0; | |
2156 do { | |
2157 GscOutputRecord& output_record = gsc_output_buffer_map_[i]; | |
2158 if (output_record.fd != -1) | |
2159 close(output_record.fd); | |
2160 if (output_record.egl_image != EGL_NO_IMAGE_KHR) | |
2161 egl_destroy_image_khr(egl_display_, output_record.egl_image); | |
2162 if (output_record.egl_sync != EGL_NO_SYNC_KHR) | |
2163 egl_destroy_sync_khr(egl_display_, output_record.egl_sync); | |
2164 if (client_) | |
2165 client_->DismissPictureBuffer(output_record.picture_id); | |
2166 ++i; | |
2167 } while (i < gsc_output_buffer_map_.size()); | |
2168 } | |
2169 | |
2170 struct v4l2_requestbuffers reqbufs; | |
2171 memset(&reqbufs, 0, sizeof(reqbufs)); | |
2172 reqbufs.count = 0; | |
2173 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
2174 reqbufs.memory = V4L2_MEMORY_DMABUF; | |
2175 if (ioctl(gsc_fd_, VIDIOC_REQBUFS, &reqbufs) != 0) | |
2176 DPLOG(ERROR) << "DestroyGscOutputBuffers(): ioctl() failed: VIDIOC_REQBUFS"; | |
2177 | |
2178 gsc_output_buffer_map_.clear(); | |
2179 gsc_free_output_buffers_.clear(); | |
2180 gsc_output_buffer_count_ = 0; | |
2181 } | |
2182 | |
2183 } // namespace content | |
OLD | NEW |