Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(417)

Side by Side Diff: media/gpu/v4l2_image_processor.cc

Issue 2191263002: V4L2VideoDecodeAccelerator: support external buffer import (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: small refactor Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <errno.h> 5 #include <errno.h>
6 #include <fcntl.h> 6 #include <fcntl.h>
7 #include <linux/videodev2.h> 7 #include <linux/videodev2.h>
8 #include <poll.h> 8 #include <poll.h>
9 #include <string.h> 9 #include <string.h>
10 #include <sys/eventfd.h> 10 #include <sys/eventfd.h>
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 V4L2ImageProcessor::OutputRecord::~OutputRecord() {} 50 V4L2ImageProcessor::OutputRecord::~OutputRecord() {}
51 51
52 V4L2ImageProcessor::JobRecord::JobRecord() : output_buffer_index(-1) {} 52 V4L2ImageProcessor::JobRecord::JobRecord() : output_buffer_index(-1) {}
53 53
54 V4L2ImageProcessor::JobRecord::~JobRecord() {} 54 V4L2ImageProcessor::JobRecord::~JobRecord() {}
55 55
56 V4L2ImageProcessor::V4L2ImageProcessor(const scoped_refptr<V4L2Device>& device) 56 V4L2ImageProcessor::V4L2ImageProcessor(const scoped_refptr<V4L2Device>& device)
57 : input_format_(PIXEL_FORMAT_UNKNOWN), 57 : input_format_(PIXEL_FORMAT_UNKNOWN),
58 output_format_(PIXEL_FORMAT_UNKNOWN), 58 output_format_(PIXEL_FORMAT_UNKNOWN),
59 input_memory_type_(V4L2_MEMORY_USERPTR), 59 input_memory_type_(V4L2_MEMORY_USERPTR),
60 output_memory_type_(V4L2_MEMORY_MMAP),
60 input_format_fourcc_(0), 61 input_format_fourcc_(0),
61 output_format_fourcc_(0), 62 output_format_fourcc_(0),
62 input_planes_count_(0), 63 input_planes_count_(0),
63 output_planes_count_(0), 64 output_planes_count_(0),
64 child_task_runner_(base::ThreadTaskRunnerHandle::Get()), 65 child_task_runner_(base::ThreadTaskRunnerHandle::Get()),
65 device_(device), 66 device_(device),
66 device_thread_("V4L2ImageProcessorThread"), 67 device_thread_("V4L2ImageProcessorThread"),
67 device_poll_thread_("V4L2ImageProcessorDevicePollThread"), 68 device_poll_thread_("V4L2ImageProcessorDevicePollThread"),
68 input_streamon_(false), 69 input_streamon_(false),
69 input_buffer_queued_count_(0), 70 input_buffer_queued_count_(0),
(...skipping 23 matching lines...) Expand all
93 94
94 void V4L2ImageProcessor::NotifyErrorOnChildThread( 95 void V4L2ImageProcessor::NotifyErrorOnChildThread(
95 const base::Closure& error_cb) { 96 const base::Closure& error_cb) {
96 DCHECK(child_task_runner_->BelongsToCurrentThread()); 97 DCHECK(child_task_runner_->BelongsToCurrentThread());
97 error_cb_.Run(); 98 error_cb_.Run();
98 } 99 }
99 100
100 bool V4L2ImageProcessor::Initialize(VideoPixelFormat input_format, 101 bool V4L2ImageProcessor::Initialize(VideoPixelFormat input_format,
101 VideoPixelFormat output_format, 102 VideoPixelFormat output_format,
102 v4l2_memory input_memory_type, 103 v4l2_memory input_memory_type,
104 v4l2_memory output_memory_type,
103 gfx::Size input_visible_size, 105 gfx::Size input_visible_size,
104 gfx::Size input_allocated_size, 106 gfx::Size input_allocated_size,
105 gfx::Size output_visible_size, 107 gfx::Size output_visible_size,
106 gfx::Size output_allocated_size, 108 gfx::Size output_allocated_size,
107 int num_buffers, 109 int num_buffers,
108 const base::Closure& error_cb) { 110 const base::Closure& error_cb) {
109 DCHECK(!error_cb.is_null()); 111 DCHECK(!error_cb.is_null());
110 DCHECK_GT(num_buffers, 0); 112 DCHECK_GT(num_buffers, 0);
111 DCHECK(input_memory_type == V4L2_MEMORY_USERPTR || 113 DCHECK(input_memory_type == V4L2_MEMORY_USERPTR ||
112 input_memory_type == V4L2_MEMORY_DMABUF); 114 input_memory_type == V4L2_MEMORY_DMABUF);
115 DCHECK(output_memory_type == V4L2_MEMORY_MMAP ||
116 output_memory_type == V4L2_MEMORY_DMABUF);
113 error_cb_ = error_cb; 117 error_cb_ = error_cb;
114 118
115 input_format_ = input_format; 119 input_format_ = input_format;
116 output_format_ = output_format; 120 output_format_ = output_format;
117 input_format_fourcc_ = V4L2Device::VideoPixelFormatToV4L2PixFmt(input_format); 121 input_format_fourcc_ = V4L2Device::VideoPixelFormatToV4L2PixFmt(input_format);
118 output_format_fourcc_ = 122 output_format_fourcc_ =
119 V4L2Device::VideoPixelFormatToV4L2PixFmt(output_format); 123 V4L2Device::VideoPixelFormatToV4L2PixFmt(output_format);
120 num_buffers_ = num_buffers; 124 num_buffers_ = num_buffers;
121 125
122 if (!input_format_fourcc_ || !output_format_fourcc_) { 126 if (!input_format_fourcc_ || !output_format_fourcc_) {
123 LOG(ERROR) << "Unrecognized format(s)"; 127 LOG(ERROR) << "Unrecognized format(s)";
124 return false; 128 return false;
125 } 129 }
126 130
127 input_memory_type_ = input_memory_type; 131 input_memory_type_ = input_memory_type;
132 output_memory_type_ = output_memory_type;
128 input_visible_size_ = input_visible_size; 133 input_visible_size_ = input_visible_size;
129 input_allocated_size_ = input_allocated_size; 134 input_allocated_size_ = input_allocated_size;
130 output_visible_size_ = output_visible_size; 135 output_visible_size_ = output_visible_size;
131 output_allocated_size_ = output_allocated_size; 136 output_allocated_size_ = output_allocated_size;
132 137
133 // Capabilities check. 138 // Capabilities check.
134 struct v4l2_capability caps; 139 struct v4l2_capability caps;
135 memset(&caps, 0, sizeof(caps)); 140 memset(&caps, 0, sizeof(caps));
136 const __u32 kCapsRequired = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING; 141 const __u32 kCapsRequired = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
137 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QUERYCAP, &caps); 142 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QUERYCAP, &caps);
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 218
214 *num_planes = format.fmt.pix_mp.num_planes; 219 *num_planes = format.fmt.pix_mp.num_planes;
215 *size = V4L2Device::CodedSizeFromV4L2Format(format); 220 *size = V4L2Device::CodedSizeFromV4L2Format(format);
216 DVLOG(1) << __func__ << ": adjusted output coded size=" << size->ToString() 221 DVLOG(1) << __func__ << ": adjusted output coded size=" << size->ToString()
217 << ", num_planes=" << *num_planes; 222 << ", num_planes=" << *num_planes;
218 return true; 223 return true;
219 } 224 }
220 225
221 void V4L2ImageProcessor::Process(const scoped_refptr<VideoFrame>& frame, 226 void V4L2ImageProcessor::Process(const scoped_refptr<VideoFrame>& frame,
222 int output_buffer_index, 227 int output_buffer_index,
228 std::vector<base::ScopedFD> output_dmabuf_fds,
223 const FrameReadyCB& cb) { 229 const FrameReadyCB& cb) {
224 DVLOG(3) << __func__ << ": ts=" << frame->timestamp().InMilliseconds(); 230 DVLOG(3) << __func__ << ": ts=" << frame->timestamp().InMilliseconds();
231 size_t expected_num_fds =
232 (output_memory_type_ == V4L2_MEMORY_DMABUF ? output_planes_count_ : 0);
233 DCHECK_EQ(expected_num_fds, output_dmabuf_fds.size());
Pawel Osciak 2016/09/12 08:54:26 Interesting, I would've expected this to give an u
wuchengli 2016/09/12 11:56:51 I found this code didn't generate unused warning.
kcwu 2016/09/14 05:38:44 foo is considered as "used" by compiler in if (0)
225 234
226 std::unique_ptr<JobRecord> job_record(new JobRecord()); 235 std::unique_ptr<JobRecord> job_record(new JobRecord());
227 job_record->frame = frame; 236 job_record->frame = frame;
228 job_record->output_buffer_index = output_buffer_index; 237 job_record->output_buffer_index = output_buffer_index;
238 job_record->output_dmabuf_fds = std::move(output_dmabuf_fds);
229 job_record->ready_cb = cb; 239 job_record->ready_cb = cb;
230 240
231 device_thread_.task_runner()->PostTask( 241 device_thread_.task_runner()->PostTask(
232 FROM_HERE, base::Bind(&V4L2ImageProcessor::ProcessTask, 242 FROM_HERE, base::Bind(&V4L2ImageProcessor::ProcessTask,
233 base::Unretained(this), base::Passed(&job_record))); 243 base::Unretained(this), base::Passed(&job_record)));
234 } 244 }
235 245
236 void V4L2ImageProcessor::ProcessTask(std::unique_ptr<JobRecord> job_record) { 246 void V4L2ImageProcessor::ProcessTask(std::unique_ptr<JobRecord> job_record) {
237 int index = job_record->output_buffer_index; 247 int index = job_record->output_buffer_index;
238 DVLOG(3) << __func__ << ": Reusing output buffer, index=" << index; 248 DVLOG(3) << __func__ << ": Reusing output buffer, index=" << index;
239 DCHECK(device_thread_.task_runner()->BelongsToCurrentThread()); 249 DCHECK(device_thread_.task_runner()->BelongsToCurrentThread());
250 OutputRecord& output_record = output_buffer_map_[index];
251 output_record.dmabuf_fds = std::move(job_record->output_dmabuf_fds);
Pawel Osciak 2016/09/12 08:54:26 Probably simpler to just say: output_buffer_map[i
wuchengli 2016/09/12 11:56:51 Done.
240 252
241 EnqueueOutput(index); 253 EnqueueOutput(index);
242 input_queue_.push(make_linked_ptr(job_record.release())); 254 input_queue_.push(make_linked_ptr(job_record.release()));
243 EnqueueInput(); 255 EnqueueInput();
244 } 256 }
245 257
246 void V4L2ImageProcessor::Destroy() { 258 void V4L2ImageProcessor::Destroy() {
247 DVLOG(3) << __func__; 259 DVLOG(3) << __func__;
248 DCHECK(child_task_runner_->BelongsToCurrentThread()); 260 DCHECK(child_task_runner_->BelongsToCurrentThread());
249 261
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 crop.c.left = 0; 380 crop.c.left = 0;
369 crop.c.top = 0; 381 crop.c.top = 0;
370 crop.c.width = base::checked_cast<__u32>(output_visible_size_.width()); 382 crop.c.width = base::checked_cast<__u32>(output_visible_size_.width());
371 crop.c.height = base::checked_cast<__u32>(output_visible_size_.height()); 383 crop.c.height = base::checked_cast<__u32>(output_visible_size_.height());
372 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_CROP, &crop); 384 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_CROP, &crop);
373 385
374 struct v4l2_requestbuffers reqbufs; 386 struct v4l2_requestbuffers reqbufs;
375 memset(&reqbufs, 0, sizeof(reqbufs)); 387 memset(&reqbufs, 0, sizeof(reqbufs));
376 reqbufs.count = num_buffers_; 388 reqbufs.count = num_buffers_;
377 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 389 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
378 reqbufs.memory = V4L2_MEMORY_MMAP; 390 reqbufs.memory = output_memory_type_;
379 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_REQBUFS, &reqbufs); 391 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_REQBUFS, &reqbufs);
380 if (static_cast<int>(reqbufs.count) != num_buffers_) { 392 if (static_cast<int>(reqbufs.count) != num_buffers_) {
381 LOG(ERROR) << "Failed to allocate output buffers. reqbufs.count=" 393 LOG(ERROR) << "Failed to allocate output buffers. reqbufs.count="
382 << reqbufs.count << ", num_buffers=" << num_buffers_; 394 << reqbufs.count << ", num_buffers=" << num_buffers_;
383 return false; 395 return false;
384 } 396 }
385 397
386 DCHECK(output_buffer_map_.empty()); 398 DCHECK(output_buffer_map_.empty());
387 output_buffer_map_.resize(reqbufs.count); 399 output_buffer_map_.resize(reqbufs.count);
388 400
(...skipping 16 matching lines...) Expand all
405 } 417 }
406 418
407 void V4L2ImageProcessor::DestroyOutputBuffers() { 419 void V4L2ImageProcessor::DestroyOutputBuffers() {
408 DCHECK(child_task_runner_->BelongsToCurrentThread()); 420 DCHECK(child_task_runner_->BelongsToCurrentThread());
409 DCHECK(!output_streamon_); 421 DCHECK(!output_streamon_);
410 422
411 struct v4l2_requestbuffers reqbufs; 423 struct v4l2_requestbuffers reqbufs;
412 memset(&reqbufs, 0, sizeof(reqbufs)); 424 memset(&reqbufs, 0, sizeof(reqbufs));
413 reqbufs.count = 0; 425 reqbufs.count = 0;
414 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 426 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
415 reqbufs.memory = V4L2_MEMORY_MMAP; 427 reqbufs.memory = output_memory_type_;
416 IOCTL_OR_LOG_ERROR(VIDIOC_REQBUFS, &reqbufs); 428 IOCTL_OR_LOG_ERROR(VIDIOC_REQBUFS, &reqbufs);
417 429
418 output_buffer_map_.clear(); 430 output_buffer_map_.clear();
419 } 431 }
420 432
421 void V4L2ImageProcessor::DevicePollTask(bool poll_device) { 433 void V4L2ImageProcessor::DevicePollTask(bool poll_device) {
422 DCHECK(device_poll_thread_.task_runner()->BelongsToCurrentThread()); 434 DCHECK(device_poll_thread_.task_runner()->BelongsToCurrentThread());
423 435
424 bool event_pending; 436 bool event_pending;
425 if (!device_->Poll(poll_device, &event_pending)) { 437 if (!device_->Poll(poll_device, &event_pending)) {
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
540 input_buffer_queued_count_--; 552 input_buffer_queued_count_--;
541 } 553 }
542 554
543 // Dequeue completed output (VIDEO_CAPTURE) buffers, recycle to the free list. 555 // Dequeue completed output (VIDEO_CAPTURE) buffers, recycle to the free list.
544 // Return the finished buffer to the client via the job ready callback. 556 // Return the finished buffer to the client via the job ready callback.
545 while (output_buffer_queued_count_ > 0) { 557 while (output_buffer_queued_count_ > 0) {
546 DCHECK(output_streamon_); 558 DCHECK(output_streamon_);
547 memset(&dqbuf, 0, sizeof(dqbuf)); 559 memset(&dqbuf, 0, sizeof(dqbuf));
548 memset(&planes, 0, sizeof(planes)); 560 memset(&planes, 0, sizeof(planes));
549 dqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 561 dqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
550 dqbuf.memory = V4L2_MEMORY_MMAP; 562 dqbuf.memory = output_memory_type_;
551 dqbuf.m.planes = planes; 563 dqbuf.m.planes = planes;
552 dqbuf.length = output_planes_count_; 564 dqbuf.length = output_planes_count_;
553 if (device_->Ioctl(VIDIOC_DQBUF, &dqbuf) != 0) { 565 if (device_->Ioctl(VIDIOC_DQBUF, &dqbuf) != 0) {
554 if (errno == EAGAIN) { 566 if (errno == EAGAIN) {
555 // EAGAIN if we're just out of buffers to dequeue. 567 // EAGAIN if we're just out of buffers to dequeue.
556 break; 568 break;
557 } 569 }
558 PLOG(ERROR) << "ioctl() failed: VIDIOC_DQBUF"; 570 PLOG(ERROR) << "ioctl() failed: VIDIOC_DQBUF";
559 NotifyError(); 571 NotifyError();
560 return; 572 return;
561 } 573 }
562 OutputRecord& output_record = output_buffer_map_[dqbuf.index]; 574 OutputRecord& output_record = output_buffer_map_[dqbuf.index];
563 DCHECK(output_record.at_device); 575 DCHECK(output_record.at_device);
564 output_record.at_device = false; 576 output_record.at_device = false;
577 output_record.dmabuf_fds.clear();
565 output_buffer_queued_count_--; 578 output_buffer_queued_count_--;
566 579
567 // Jobs are always processed in FIFO order. 580 // Jobs are always processed in FIFO order.
568 DCHECK(!running_jobs_.empty()); 581 DCHECK(!running_jobs_.empty());
569 linked_ptr<JobRecord> job_record = running_jobs_.front(); 582 linked_ptr<JobRecord> job_record = running_jobs_.front();
570 running_jobs_.pop(); 583 running_jobs_.pop();
571 584
572 DVLOG(3) << "Processing finished, returning frame, index=" << dqbuf.index; 585 DVLOG(3) << "Processing finished, returning frame, index=" << dqbuf.index;
573 586
574 child_task_runner_->PostTask( 587 child_task_runner_->PostTask(
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
627 DCHECK_LT(static_cast<size_t>(index), output_buffer_map_.size()); 640 DCHECK_LT(static_cast<size_t>(index), output_buffer_map_.size());
628 // Enqueue an output (VIDEO_CAPTURE) buffer. 641 // Enqueue an output (VIDEO_CAPTURE) buffer.
629 OutputRecord& output_record = output_buffer_map_[index]; 642 OutputRecord& output_record = output_buffer_map_[index];
630 DCHECK(!output_record.at_device); 643 DCHECK(!output_record.at_device);
631 struct v4l2_buffer qbuf; 644 struct v4l2_buffer qbuf;
632 struct v4l2_plane qbuf_planes[VIDEO_MAX_PLANES]; 645 struct v4l2_plane qbuf_planes[VIDEO_MAX_PLANES];
633 memset(&qbuf, 0, sizeof(qbuf)); 646 memset(&qbuf, 0, sizeof(qbuf));
634 memset(qbuf_planes, 0, sizeof(qbuf_planes)); 647 memset(qbuf_planes, 0, sizeof(qbuf_planes));
635 qbuf.index = index; 648 qbuf.index = index;
636 qbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 649 qbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
637 qbuf.memory = V4L2_MEMORY_MMAP; 650 qbuf.memory = output_memory_type_;
651 if (output_memory_type_ == V4L2_MEMORY_DMABUF) {
652 for (size_t i = 0; i < output_record.dmabuf_fds.size(); ++i)
653 qbuf_planes[i].m.fd = output_record.dmabuf_fds[i].get();
654 }
638 qbuf.m.planes = qbuf_planes; 655 qbuf.m.planes = qbuf_planes;
639 qbuf.length = output_planes_count_; 656 qbuf.length = output_planes_count_;
640 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QBUF, &qbuf); 657 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QBUF, &qbuf);
641 output_record.at_device = true; 658 output_record.at_device = true;
642 output_buffer_queued_count_++; 659 output_buffer_queued_count_++;
643 return true; 660 return true;
644 } 661 }
645 662
646 bool V4L2ImageProcessor::StartDevicePoll() { 663 bool V4L2ImageProcessor::StartDevicePoll() {
647 DVLOG(3) << __func__ << ": starting device poll"; 664 DVLOG(3) << __func__ << ": starting device poll";
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
712 return true; 729 return true;
713 } 730 }
714 731
715 void V4L2ImageProcessor::FrameReady(const FrameReadyCB& cb, 732 void V4L2ImageProcessor::FrameReady(const FrameReadyCB& cb,
716 int output_buffer_index) { 733 int output_buffer_index) {
717 DCHECK(child_task_runner_->BelongsToCurrentThread()); 734 DCHECK(child_task_runner_->BelongsToCurrentThread());
718 cb.Run(output_buffer_index); 735 cb.Run(output_buffer_index);
719 } 736 }
720 737
721 } // namespace media 738 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698