| OLD | NEW |
| 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 "media/gpu/v4l2_video_encode_accelerator.h" | 5 #include "media/gpu/v4l2_video_encode_accelerator.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <linux/videodev2.h> | 8 #include <linux/videodev2.h> |
| 9 #include <poll.h> | 9 #include <poll.h> |
| 10 #include <string.h> | 10 #include <string.h> |
| 11 #include <sys/eventfd.h> | 11 #include <sys/eventfd.h> |
| 12 #include <sys/ioctl.h> | 12 #include <sys/ioctl.h> |
| 13 #include <sys/mman.h> | 13 #include <sys/mman.h> |
| 14 | 14 |
| 15 #include <utility> | 15 #include <utility> |
| 16 | 16 |
| 17 #include "base/bits.h" |
| 17 #include "base/callback.h" | 18 #include "base/callback.h" |
| 18 #include "base/command_line.h" | 19 #include "base/command_line.h" |
| 19 #include "base/macros.h" | 20 #include "base/macros.h" |
| 20 #include "base/numerics/safe_conversions.h" | 21 #include "base/numerics/safe_conversions.h" |
| 21 #include "base/single_thread_task_runner.h" | 22 #include "base/single_thread_task_runner.h" |
| 22 #include "base/threading/thread_task_runner_handle.h" | 23 #include "base/threading/thread_task_runner_handle.h" |
| 23 #include "base/trace_event/trace_event.h" | 24 #include "base/trace_event/trace_event.h" |
| 24 #include "media/base/bind_to_current_loop.h" | 25 #include "media/base/bind_to_current_loop.h" |
| 25 #include "media/base/bitstream_buffer.h" | 26 #include "media/base/bitstream_buffer.h" |
| 26 #include "media/filters/h264_parser.h" | 27 #include "media/filters/h264_parser.h" |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 scoped_refptr<V4L2Device> device = V4L2Device::Create(); | 196 scoped_refptr<V4L2Device> device = V4L2Device::Create(); |
| 196 image_processor_.reset(new V4L2ImageProcessor(device)); | 197 image_processor_.reset(new V4L2ImageProcessor(device)); |
| 197 | 198 |
| 198 // Convert from input_format to device_input_format_, keeping the size | 199 // Convert from input_format to device_input_format_, keeping the size |
| 199 // at visible_size_ and requiring the output buffers to be of at least | 200 // at visible_size_ and requiring the output buffers to be of at least |
| 200 // input_allocated_size_. Unretained is safe because |this| owns image | 201 // input_allocated_size_. Unretained is safe because |this| owns image |
| 201 // processor and there will be no callbacks after processor destroys. | 202 // processor and there will be no callbacks after processor destroys. |
| 202 if (!image_processor_->Initialize( | 203 if (!image_processor_->Initialize( |
| 203 input_format, device_input_format_, V4L2_MEMORY_USERPTR, | 204 input_format, device_input_format_, V4L2_MEMORY_USERPTR, |
| 204 V4L2_MEMORY_MMAP, visible_size_, visible_size_, visible_size_, | 205 V4L2_MEMORY_MMAP, visible_size_, visible_size_, visible_size_, |
| 205 input_allocated_size_, kImageProcBufferCount, | 206 input_coded_size_, kImageProcBufferCount, |
| 206 base::Bind(&V4L2VideoEncodeAccelerator::ImageProcessorError, | 207 base::Bind(&V4L2VideoEncodeAccelerator::ImageProcessorError, |
| 207 base::Unretained(this)))) { | 208 base::Unretained(this)))) { |
| 208 LOG(ERROR) << "Failed initializing image processor"; | 209 LOG(ERROR) << "Failed initializing image processor"; |
| 209 return false; | 210 return false; |
| 210 } | 211 } |
| 211 // The output of image processor is the input of encoder. Output coded | 212 // The output of image processor is the input of encoder. Output coded |
| 212 // width of processor must be the same as input coded width of encoder. | 213 // width of processor must be the same as input coded width of encoder. |
| 213 // Output coded height of processor can be larger but not smaller than the | 214 // Output coded height of processor can be larger but not smaller than the |
| 214 // input coded height of encoder. For example, suppose input size of encoder | 215 // input coded height of encoder. For example, suppose input size of encoder |
| 215 // is 320x193. It is OK if the output of processor is 320x208. | 216 // is 320x193. It is OK if the output of processor is 320x208. |
| 216 if (image_processor_->output_allocated_size().width() != | 217 if (image_processor_->output_allocated_size().width() != |
| 217 input_allocated_size_.width() || | 218 input_coded_size_.width() || |
| 218 image_processor_->output_allocated_size().height() < | 219 image_processor_->output_allocated_size().height() < |
| 219 input_allocated_size_.height()) { | 220 input_coded_size_.height()) { |
| 220 LOG(ERROR) << "Invalid image processor output coded size " | 221 LOG(ERROR) << "Invalid image processor output coded size " |
| 221 << image_processor_->output_allocated_size().ToString() | 222 << image_processor_->output_allocated_size().ToString() |
| 222 << ", encode input coded size is " | 223 << ", encode input coded size is " |
| 223 << input_allocated_size_.ToString(); | 224 << input_coded_size_.ToString(); |
| 224 return false; | 225 return false; |
| 225 } | 226 } |
| 226 | 227 |
| 227 for (int i = 0; i < kImageProcBufferCount; i++) { | 228 for (int i = 0; i < kImageProcBufferCount; i++) { |
| 228 std::vector<base::ScopedFD> fds = | 229 std::vector<base::ScopedFD> fds = |
| 229 image_processor_->GetDmabufsForOutputBuffer(i); | 230 image_processor_->GetDmabufsForOutputBuffer(i); |
| 230 if (fds.size() == 0) { | 231 if (fds.size() == 0) { |
| 231 LOG(ERROR) << __func__ << ": failed to get fds of image processor."; | 232 LOG(ERROR) << __func__ << ": failed to get fds of image processor."; |
| 232 return false; | 233 return false; |
| 233 } | 234 } |
| (...skipping 514 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 748 qbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | 749 qbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; |
| 749 qbuf.m.planes = qbuf_planes; | 750 qbuf.m.planes = qbuf_planes; |
| 750 qbuf.timestamp.tv_sec = static_cast<time_t>(frame->timestamp().InSeconds()); | 751 qbuf.timestamp.tv_sec = static_cast<time_t>(frame->timestamp().InSeconds()); |
| 751 qbuf.timestamp.tv_usec = | 752 qbuf.timestamp.tv_usec = |
| 752 frame->timestamp().InMicroseconds() - | 753 frame->timestamp().InMicroseconds() - |
| 753 frame->timestamp().InSeconds() * base::Time::kMicrosecondsPerSecond; | 754 frame->timestamp().InSeconds() * base::Time::kMicrosecondsPerSecond; |
| 754 | 755 |
| 755 DCHECK_EQ(device_input_format_, frame->format()); | 756 DCHECK_EQ(device_input_format_, frame->format()); |
| 756 for (size_t i = 0; i < input_planes_count_; ++i) { | 757 for (size_t i = 0; i < input_planes_count_; ++i) { |
| 757 qbuf.m.planes[i].bytesused = base::checked_cast<__u32>( | 758 qbuf.m.planes[i].bytesused = base::checked_cast<__u32>( |
| 758 VideoFrame::PlaneSize(frame->format(), i, input_allocated_size_) | 759 VideoFrame::PlaneSize(frame->format(), i, input_coded_size_).GetArea()); |
| 759 .GetArea()); | |
| 760 | 760 |
| 761 switch (input_memory_type_) { | 761 switch (input_memory_type_) { |
| 762 case V4L2_MEMORY_USERPTR: | 762 case V4L2_MEMORY_USERPTR: |
| 763 qbuf.m.planes[i].length = qbuf.m.planes[i].bytesused; | 763 qbuf.m.planes[i].length = base::checked_cast<__u32>( |
| 764 VideoFrame::PlaneSize(frame->format(), i, input_allocated_size_) |
| 765 .GetArea()); |
| 764 qbuf.m.planes[i].m.userptr = | 766 qbuf.m.planes[i].m.userptr = |
| 765 reinterpret_cast<unsigned long>(frame->data(i)); | 767 reinterpret_cast<unsigned long>(frame->data(i)); |
| 766 DCHECK(qbuf.m.planes[i].m.userptr); | 768 DCHECK(qbuf.m.planes[i].m.userptr); |
| 767 break; | 769 break; |
| 768 | 770 |
| 769 case V4L2_MEMORY_DMABUF: | 771 case V4L2_MEMORY_DMABUF: |
| 770 qbuf.m.planes[i].m.fd = frame->dmabuf_fd(i); | 772 qbuf.m.planes[i].m.fd = frame->dmabuf_fd(i); |
| 771 DCHECK_NE(qbuf.m.planes[i].m.fd, -1); | 773 DCHECK_NE(qbuf.m.planes[i].m.fd, -1); |
| 772 break; | 774 break; |
| 773 | 775 |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1049 format.fmt.pix_mp.width = visible_size_.width(); | 1051 format.fmt.pix_mp.width = visible_size_.width(); |
| 1050 format.fmt.pix_mp.height = visible_size_.height(); | 1052 format.fmt.pix_mp.height = visible_size_.height(); |
| 1051 format.fmt.pix_mp.pixelformat = input_format_fourcc; | 1053 format.fmt.pix_mp.pixelformat = input_format_fourcc; |
| 1052 format.fmt.pix_mp.num_planes = input_planes_count; | 1054 format.fmt.pix_mp.num_planes = input_planes_count; |
| 1053 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_FMT, &format); | 1055 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_FMT, &format); |
| 1054 DCHECK_EQ(format.fmt.pix_mp.num_planes, input_planes_count); | 1056 DCHECK_EQ(format.fmt.pix_mp.num_planes, input_planes_count); |
| 1055 } | 1057 } |
| 1056 | 1058 |
| 1057 // Take device-adjusted sizes for allocated size. If the size is adjusted | 1059 // Take device-adjusted sizes for allocated size. If the size is adjusted |
| 1058 // down, it means the input is too big and the hardware does not support it. | 1060 // down, it means the input is too big and the hardware does not support it. |
| 1059 input_allocated_size_ = V4L2Device::CodedSizeFromV4L2Format(format); | 1061 input_coded_size_ = V4L2Device::CodedSizeFromV4L2Format(format); |
| 1060 if (!gfx::Rect(input_allocated_size_).Contains(gfx::Rect(visible_size_))) { | 1062 if (!gfx::Rect(input_coded_size_).Contains(gfx::Rect(visible_size_))) { |
| 1061 DVLOG(1) << "Input size too big " << visible_size_.ToString() | 1063 DVLOG(1) << "Input size too big " << visible_size_.ToString() |
| 1062 << ", adjusted to " << input_allocated_size_.ToString(); | 1064 << ", adjusted to " << input_coded_size_.ToString(); |
| 1063 return false; | 1065 return false; |
| 1064 } | 1066 } |
| 1065 | 1067 |
| 1068 // Make sure the size of each plane is multiple of 128. If the width is |
| 1069 // multiple of 16 and the height is multiple of 32, U or V plane size will |
| 1070 // be multiple of 16 * 32 / 4 = 128. |
| 1071 if (input_coded_size_.width() % 16 != 0) { |
| 1072 LOG(ERROR) << "Input coded width is not multiple of 16"; |
| 1073 return false; |
| 1074 } |
| 1075 input_allocated_size_.set_width(input_coded_size_.width()); |
| 1076 input_allocated_size_.set_height( |
| 1077 base::bits::Align(input_coded_size_.height(), 32)); |
| 1078 DVLOG(1) << "Input coded size=" << input_coded_size_.ToString() |
| 1079 << ", input allocated size=" << input_allocated_size_.ToString(); |
| 1080 |
| 1066 device_input_format_ = input_format; | 1081 device_input_format_ = input_format; |
| 1067 input_planes_count_ = input_planes_count; | 1082 input_planes_count_ = input_planes_count; |
| 1068 return true; | 1083 return true; |
| 1069 } | 1084 } |
| 1070 | 1085 |
| 1071 bool V4L2VideoEncodeAccelerator::SetFormats(VideoPixelFormat input_format, | 1086 bool V4L2VideoEncodeAccelerator::SetFormats(VideoPixelFormat input_format, |
| 1072 VideoCodecProfile output_profile) { | 1087 VideoCodecProfile output_profile) { |
| 1073 DVLOG(3) << "SetFormats()"; | 1088 DVLOG(3) << "SetFormats()"; |
| 1074 DCHECK(child_task_runner_->BelongsToCurrentThread()); | 1089 DCHECK(child_task_runner_->BelongsToCurrentThread()); |
| 1075 DCHECK(!input_streamon_); | 1090 DCHECK(!input_streamon_); |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1333 memset(&reqbufs, 0, sizeof(reqbufs)); | 1348 memset(&reqbufs, 0, sizeof(reqbufs)); |
| 1334 reqbufs.count = 0; | 1349 reqbufs.count = 0; |
| 1335 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | 1350 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
| 1336 reqbufs.memory = V4L2_MEMORY_MMAP; | 1351 reqbufs.memory = V4L2_MEMORY_MMAP; |
| 1337 IOCTL_OR_LOG_ERROR(VIDIOC_REQBUFS, &reqbufs); | 1352 IOCTL_OR_LOG_ERROR(VIDIOC_REQBUFS, &reqbufs); |
| 1338 | 1353 |
| 1339 output_buffer_map_.clear(); | 1354 output_buffer_map_.clear(); |
| 1340 } | 1355 } |
| 1341 | 1356 |
| 1342 } // namespace media | 1357 } // namespace media |
| OLD | NEW |