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