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/callback.h" | 17 #include "base/callback.h" |
18 #include "base/command_line.h" | 18 #include "base/command_line.h" |
19 #include "base/macros.h" | 19 #include "base/macros.h" |
20 #include "base/numerics/safe_conversions.h" | 20 #include "base/numerics/safe_conversions.h" |
21 #include "base/single_thread_task_runner.h" | 21 #include "base/single_thread_task_runner.h" |
22 #include "base/threading/thread_task_runner_handle.h" | 22 #include "base/threading/thread_task_runner_handle.h" |
23 #include "base/trace_event/trace_event.h" | 23 #include "base/trace_event/trace_event.h" |
24 #include "media/base/bind_to_current_loop.h" | 24 #include "media/base/bind_to_current_loop.h" |
25 #include "media/base/bitstream_buffer.h" | 25 #include "media/base/bitstream_buffer.h" |
| 26 #include "media/filters/h264_parser.h" |
26 #include "media/gpu/shared_memory_region.h" | 27 #include "media/gpu/shared_memory_region.h" |
27 | 28 |
28 #define NOTIFY_ERROR(x) \ | 29 #define NOTIFY_ERROR(x) \ |
29 do { \ | 30 do { \ |
30 LOG(ERROR) << "Setting error state:" << x; \ | 31 LOG(ERROR) << "Setting error state:" << x; \ |
31 SetErrorState(x); \ | 32 SetErrorState(x); \ |
32 } while (0) | 33 } while (0) |
33 | 34 |
34 #define IOCTL_OR_ERROR_RETURN_VALUE(type, arg, value, type_str) \ | 35 #define IOCTL_OR_ERROR_RETURN_VALUE(type, arg, value, type_str) \ |
35 do { \ | 36 do { \ |
36 if (device_->Ioctl(type, arg) != 0) { \ | 37 if (device_->Ioctl(type, arg) != 0) { \ |
37 PLOG(ERROR) << __func__ << "(): ioctl() failed: " << type_str; \ | 38 PLOG(ERROR) << __func__ << "(): ioctl() failed: " << type_str; \ |
38 NOTIFY_ERROR(kPlatformFailureError); \ | 39 NOTIFY_ERROR(kPlatformFailureError); \ |
39 return value; \ | 40 return value; \ |
40 } \ | 41 } \ |
41 } while (0) | 42 } while (0) |
42 | 43 |
43 #define IOCTL_OR_ERROR_RETURN(type, arg) \ | 44 #define IOCTL_OR_ERROR_RETURN(type, arg) \ |
44 IOCTL_OR_ERROR_RETURN_VALUE(type, arg, ((void)0), #type) | 45 IOCTL_OR_ERROR_RETURN_VALUE(type, arg, ((void)0), #type) |
45 | 46 |
46 #define IOCTL_OR_ERROR_RETURN_FALSE(type, arg) \ | 47 #define IOCTL_OR_ERROR_RETURN_FALSE(type, arg) \ |
47 IOCTL_OR_ERROR_RETURN_VALUE(type, arg, false, #type) | 48 IOCTL_OR_ERROR_RETURN_VALUE(type, arg, false, #type) |
48 | 49 |
49 #define IOCTL_OR_LOG_ERROR(type, arg) \ | 50 #define IOCTL_OR_LOG_ERROR(type, arg) \ |
50 do { \ | 51 do { \ |
51 if (device_->Ioctl(type, arg) != 0) \ | 52 if (device_->Ioctl(type, arg) != 0) \ |
52 PLOG(ERROR) << __func__ << "(): ioctl() failed: " << #type; \ | 53 PLOG(ERROR) << __func__ << "(): ioctl() failed: " << #type; \ |
53 } while (0) | 54 } while (0) |
54 | 55 |
| 56 namespace { |
| 57 const uint8_t kH264StartCode[] = {0, 0, 0, 1}; |
| 58 const size_t kH264StartCodeSize = sizeof(kH264StartCode); |
| 59 |
| 60 // Copy a H.264 NALU of size |src_size| (without start code), located at |src|, |
| 61 // into a buffer starting at |dst| of size |dst_size|, prepending it with |
| 62 // a H.264 start code (as long as both fit). After copying, update |dst| to |
| 63 // point to the address immediately after the copied data, and update |dst_size| |
| 64 // to contain remaining destination buffer size. |
| 65 static void CopyNALUPrependingStartCode(const uint8_t* src, |
| 66 size_t src_size, |
| 67 uint8_t** dst, |
| 68 size_t* dst_size) { |
| 69 size_t size_to_copy = kH264StartCodeSize + src_size; |
| 70 if (size_to_copy > *dst_size) { |
| 71 DVLOG(1) << "Could not copy a NALU, not enough space in destination buffer"; |
| 72 return; |
| 73 } |
| 74 |
| 75 memcpy(*dst, kH264StartCode, kH264StartCodeSize); |
| 76 memcpy(*dst + kH264StartCodeSize, src, src_size); |
| 77 |
| 78 *dst += size_to_copy; |
| 79 *dst_size -= size_to_copy; |
| 80 } |
| 81 } // namespace |
| 82 |
55 namespace media { | 83 namespace media { |
56 | 84 |
57 struct V4L2VideoEncodeAccelerator::BitstreamBufferRef { | 85 struct V4L2VideoEncodeAccelerator::BitstreamBufferRef { |
58 BitstreamBufferRef(int32_t id, std::unique_ptr<SharedMemoryRegion> shm) | 86 BitstreamBufferRef(int32_t id, std::unique_ptr<SharedMemoryRegion> shm) |
59 : id(id), shm(std::move(shm)) {} | 87 : id(id), shm(std::move(shm)) {} |
60 const int32_t id; | 88 const int32_t id; |
61 const std::unique_ptr<SharedMemoryRegion> shm; | 89 const std::unique_ptr<SharedMemoryRegion> shm; |
62 }; | 90 }; |
63 | 91 |
64 V4L2VideoEncodeAccelerator::InputRecord::InputRecord() : at_device(false) {} | 92 V4L2VideoEncodeAccelerator::InputRecord::InputRecord() : at_device(false) {} |
65 | 93 |
66 V4L2VideoEncodeAccelerator::InputRecord::~InputRecord() {} | 94 V4L2VideoEncodeAccelerator::InputRecord::~InputRecord() {} |
67 | 95 |
68 V4L2VideoEncodeAccelerator::OutputRecord::OutputRecord() | 96 V4L2VideoEncodeAccelerator::OutputRecord::OutputRecord() |
69 : at_device(false), address(NULL), length(0) {} | 97 : at_device(false), address(nullptr), length(0) {} |
70 | 98 |
71 V4L2VideoEncodeAccelerator::OutputRecord::~OutputRecord() {} | 99 V4L2VideoEncodeAccelerator::OutputRecord::~OutputRecord() {} |
72 | 100 |
73 V4L2VideoEncodeAccelerator::ImageProcessorInputRecord:: | 101 V4L2VideoEncodeAccelerator::ImageProcessorInputRecord:: |
74 ImageProcessorInputRecord() | 102 ImageProcessorInputRecord() |
75 : force_keyframe(false) {} | 103 : force_keyframe(false) {} |
76 | 104 |
77 V4L2VideoEncodeAccelerator::ImageProcessorInputRecord:: | 105 V4L2VideoEncodeAccelerator::ImageProcessorInputRecord:: |
78 ~ImageProcessorInputRecord() {} | 106 ~ImageProcessorInputRecord() {} |
79 | 107 |
80 V4L2VideoEncodeAccelerator::V4L2VideoEncodeAccelerator( | 108 V4L2VideoEncodeAccelerator::V4L2VideoEncodeAccelerator( |
81 const scoped_refptr<V4L2Device>& device) | 109 const scoped_refptr<V4L2Device>& device) |
82 : child_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 110 : child_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
83 output_buffer_byte_size_(0), | 111 output_buffer_byte_size_(0), |
84 device_input_format_(PIXEL_FORMAT_UNKNOWN), | 112 device_input_format_(PIXEL_FORMAT_UNKNOWN), |
85 input_planes_count_(0), | 113 input_planes_count_(0), |
86 output_format_fourcc_(0), | 114 output_format_fourcc_(0), |
87 encoder_state_(kUninitialized), | 115 encoder_state_(kUninitialized), |
88 stream_header_size_(0), | |
89 device_(device), | 116 device_(device), |
90 input_streamon_(false), | 117 input_streamon_(false), |
91 input_buffer_queued_count_(0), | 118 input_buffer_queued_count_(0), |
92 input_memory_type_(V4L2_MEMORY_USERPTR), | 119 input_memory_type_(V4L2_MEMORY_USERPTR), |
93 output_streamon_(false), | 120 output_streamon_(false), |
94 output_buffer_queued_count_(0), | 121 output_buffer_queued_count_(0), |
95 encoder_thread_("V4L2EncoderThread"), | 122 encoder_thread_("V4L2EncoderThread"), |
96 device_poll_thread_("V4L2EncoderDevicePollThread"), | 123 device_poll_thread_("V4L2EncoderDevicePollThread"), |
97 weak_this_ptr_factory_(this) { | 124 weak_this_ptr_factory_(this) { |
98 weak_this_ = weak_this_ptr_factory_.GetWeakPtr(); | 125 weak_this_ = weak_this_ptr_factory_.GetWeakPtr(); |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 DCHECK(child_task_runner_->BelongsToCurrentThread()); | 422 DCHECK(child_task_runner_->BelongsToCurrentThread()); |
396 DVLOG(3) << __func__ << ": output_buffer_index=" << output_buffer_index; | 423 DVLOG(3) << __func__ << ": output_buffer_index=" << output_buffer_index; |
397 free_image_processor_output_buffers_.push_back(output_buffer_index); | 424 free_image_processor_output_buffers_.push_back(output_buffer_index); |
398 if (!image_processor_input_queue_.empty()) { | 425 if (!image_processor_input_queue_.empty()) { |
399 ImageProcessorInputRecord record = image_processor_input_queue_.front(); | 426 ImageProcessorInputRecord record = image_processor_input_queue_.front(); |
400 image_processor_input_queue_.pop(); | 427 image_processor_input_queue_.pop(); |
401 Encode(record.frame, record.force_keyframe); | 428 Encode(record.frame, record.force_keyframe); |
402 } | 429 } |
403 } | 430 } |
404 | 431 |
| 432 size_t V4L2VideoEncodeAccelerator::CopyIntoOutputBuffer( |
| 433 const uint8_t* bitstream_data, |
| 434 size_t bitstream_size, |
| 435 std::unique_ptr<BitstreamBufferRef> buffer_ref) { |
| 436 uint8_t* dst_ptr = static_cast<uint8_t*>(buffer_ref->shm->memory()); |
| 437 size_t remaining_dst_size = buffer_ref->shm->size(); |
| 438 |
| 439 if (!inject_sps_and_pps_) { |
| 440 if (bitstream_size <= remaining_dst_size) { |
| 441 memcpy(dst_ptr, bitstream_data, bitstream_size); |
| 442 return bitstream_size; |
| 443 } else { |
| 444 DVLOG(1) << "Output data did not fit in the BitstreamBuffer"; |
| 445 return 0; |
| 446 } |
| 447 } |
| 448 |
| 449 // Cache the newest SPS and PPS found in the stream, and inject them before |
| 450 // each IDR found. |
| 451 H264Parser parser; |
| 452 parser.SetStream(bitstream_data, bitstream_size); |
| 453 H264NALU nalu; |
| 454 |
| 455 while (parser.AdvanceToNextNALU(&nalu) == H264Parser::kOk) { |
| 456 // nalu.size is always without the start code, regardless of the NALU type. |
| 457 if (nalu.size + kH264StartCodeSize > remaining_dst_size) { |
| 458 DVLOG(1) << "Output data did not fit in the BitstreamBuffer"; |
| 459 break; |
| 460 } |
| 461 |
| 462 switch (nalu.nal_unit_type) { |
| 463 case H264NALU::kSPS: |
| 464 cached_sps_.resize(nalu.size); |
| 465 memcpy(cached_sps_.data(), nalu.data, nalu.size); |
| 466 cached_h264_header_size_ = |
| 467 cached_sps_.size() + cached_pps_.size() + 2 * kH264StartCodeSize; |
| 468 break; |
| 469 |
| 470 case H264NALU::kPPS: |
| 471 cached_pps_.resize(nalu.size); |
| 472 memcpy(cached_pps_.data(), nalu.data, nalu.size); |
| 473 cached_h264_header_size_ = |
| 474 cached_sps_.size() + cached_pps_.size() + 2 * kH264StartCodeSize; |
| 475 break; |
| 476 |
| 477 case H264NALU::kIDRSlice: |
| 478 // Only inject if we have both headers cached, and enough space for both |
| 479 // the headers and the NALU itself. |
| 480 if (cached_sps_.empty() || cached_pps_.empty() || |
| 481 cached_h264_header_size_ + nalu.size + kH264StartCodeSize > |
| 482 remaining_dst_size) { |
| 483 DVLOG(1) << "Not enough space to inject a stream header before IDR"; |
| 484 break; |
| 485 } |
| 486 |
| 487 CopyNALUPrependingStartCode(cached_sps_.data(), cached_sps_.size(), |
| 488 &dst_ptr, &remaining_dst_size); |
| 489 CopyNALUPrependingStartCode(cached_pps_.data(), cached_pps_.size(), |
| 490 &dst_ptr, &remaining_dst_size); |
| 491 DVLOG(2) << "Stream header injected before IDR"; |
| 492 break; |
| 493 } |
| 494 |
| 495 CopyNALUPrependingStartCode(nalu.data, nalu.size, &dst_ptr, |
| 496 &remaining_dst_size); |
| 497 } |
| 498 |
| 499 return buffer_ref->shm->size() - remaining_dst_size; |
| 500 } |
| 501 |
405 void V4L2VideoEncodeAccelerator::EncodeTask( | 502 void V4L2VideoEncodeAccelerator::EncodeTask( |
406 const scoped_refptr<VideoFrame>& frame, | 503 const scoped_refptr<VideoFrame>& frame, |
407 bool force_keyframe) { | 504 bool force_keyframe) { |
408 DVLOG(3) << "EncodeTask(): force_keyframe=" << force_keyframe; | 505 DVLOG(3) << "EncodeTask(): force_keyframe=" << force_keyframe; |
409 DCHECK(encoder_thread_.task_runner()->BelongsToCurrentThread()); | 506 DCHECK(encoder_thread_.task_runner()->BelongsToCurrentThread()); |
410 DCHECK_NE(encoder_state_, kUninitialized); | 507 DCHECK_NE(encoder_state_, kUninitialized); |
411 | 508 |
412 if (encoder_state_ == kError) { | 509 if (encoder_state_ == kError) { |
413 DVLOG(2) << "EncodeTask(): early out: kError state"; | 510 DVLOG(2) << "EncodeTask(): early out: kError state"; |
414 return; | 511 return; |
(...skipping 19 matching lines...) Expand all Loading... |
434 return; | 531 return; |
435 } | 532 } |
436 } | 533 } |
437 } | 534 } |
438 | 535 |
439 void V4L2VideoEncodeAccelerator::UseOutputBitstreamBufferTask( | 536 void V4L2VideoEncodeAccelerator::UseOutputBitstreamBufferTask( |
440 std::unique_ptr<BitstreamBufferRef> buffer_ref) { | 537 std::unique_ptr<BitstreamBufferRef> buffer_ref) { |
441 DVLOG(3) << "UseOutputBitstreamBufferTask(): id=" << buffer_ref->id; | 538 DVLOG(3) << "UseOutputBitstreamBufferTask(): id=" << buffer_ref->id; |
442 DCHECK(encoder_thread_.task_runner()->BelongsToCurrentThread()); | 539 DCHECK(encoder_thread_.task_runner()->BelongsToCurrentThread()); |
443 | 540 |
444 encoder_output_queue_.push_back( | 541 encoder_output_queue_.push_back(std::move(buffer_ref)); |
445 linked_ptr<BitstreamBufferRef>(buffer_ref.release())); | |
446 Enqueue(); | 542 Enqueue(); |
447 | 543 |
448 if (encoder_state_ == kInitialized) { | 544 if (encoder_state_ == kInitialized) { |
449 // Finish setting up our OUTPUT queue. See: Initialize(). | 545 // Finish setting up our OUTPUT queue. See: Initialize(). |
450 // VIDIOC_REQBUFS on OUTPUT queue. | 546 // VIDIOC_REQBUFS on OUTPUT queue. |
451 if (!CreateInputBuffers()) | 547 if (!CreateInputBuffers()) |
452 return; | 548 return; |
453 if (!StartDevicePoll()) | 549 if (!StartDevicePoll()) |
454 return; | 550 return; |
455 encoder_state_ = kEncoding; | 551 encoder_state_ = kEncoding; |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
610 // EAGAIN if we're just out of buffers to dequeue. | 706 // EAGAIN if we're just out of buffers to dequeue. |
611 break; | 707 break; |
612 } | 708 } |
613 PLOG(ERROR) << "Dequeue(): ioctl() failed: VIDIOC_DQBUF"; | 709 PLOG(ERROR) << "Dequeue(): ioctl() failed: VIDIOC_DQBUF"; |
614 NOTIFY_ERROR(kPlatformFailureError); | 710 NOTIFY_ERROR(kPlatformFailureError); |
615 return; | 711 return; |
616 } | 712 } |
617 const bool key_frame = ((dqbuf.flags & V4L2_BUF_FLAG_KEYFRAME) != 0); | 713 const bool key_frame = ((dqbuf.flags & V4L2_BUF_FLAG_KEYFRAME) != 0); |
618 OutputRecord& output_record = output_buffer_map_[dqbuf.index]; | 714 OutputRecord& output_record = output_buffer_map_[dqbuf.index]; |
619 DCHECK(output_record.at_device); | 715 DCHECK(output_record.at_device); |
620 DCHECK(output_record.buffer_ref.get()); | 716 DCHECK(output_record.buffer_ref); |
621 | 717 |
622 void* output_data = output_record.address; | 718 int32_t bitstream_buffer_id = output_record.buffer_ref->id; |
623 size_t output_size = dqbuf.m.planes[0].bytesused; | 719 size_t output_data_size = CopyIntoOutputBuffer( |
624 // This shouldn't happen, but just in case. We should be able to recover | 720 static_cast<uint8_t*>(output_record.address), |
625 // after next keyframe after showing some corruption. | 721 base::checked_cast<size_t>(dqbuf.m.planes[0].bytesused), |
626 DCHECK_LE(output_size, output_buffer_byte_size_); | 722 std::move(output_record.buffer_ref)); |
627 if (output_size > output_buffer_byte_size_) | |
628 output_size = output_buffer_byte_size_; | |
629 uint8_t* target_data = | |
630 reinterpret_cast<uint8_t*>(output_record.buffer_ref->shm->memory()); | |
631 if (output_format_fourcc_ == V4L2_PIX_FMT_H264) { | |
632 if (stream_header_size_ == 0) { | |
633 // Assume that the first buffer dequeued is the stream header. | |
634 stream_header_size_ = output_size; | |
635 stream_header_.reset(new uint8_t[stream_header_size_]); | |
636 memcpy(stream_header_.get(), output_data, stream_header_size_); | |
637 } | |
638 if (key_frame && | |
639 output_buffer_byte_size_ - stream_header_size_ >= output_size) { | |
640 // Insert stream header before every keyframe. | |
641 memcpy(target_data, stream_header_.get(), stream_header_size_); | |
642 memcpy(target_data + stream_header_size_, output_data, output_size); | |
643 output_size += stream_header_size_; | |
644 } else { | |
645 memcpy(target_data, output_data, output_size); | |
646 } | |
647 } else { | |
648 memcpy(target_data, output_data, output_size); | |
649 } | |
650 | 723 |
651 DVLOG(3) << "Dequeue(): returning " | 724 DVLOG(3) << "Dequeue(): returning " |
652 << "bitstream_buffer_id=" << output_record.buffer_ref->id | 725 << "bitstream_buffer_id=" << bitstream_buffer_id |
653 << ", size=" << output_size | 726 << ", size=" << output_data_size << ", key_frame=" << key_frame; |
654 << ", key_frame=" << key_frame; | 727 |
655 child_task_runner_->PostTask( | 728 child_task_runner_->PostTask( |
656 FROM_HERE, | 729 FROM_HERE, base::Bind(&Client::BitstreamBufferReady, client_, |
657 base::Bind( | 730 bitstream_buffer_id, output_data_size, key_frame, |
658 &Client::BitstreamBufferReady, client_, | 731 base::TimeDelta::FromMicroseconds( |
659 output_record.buffer_ref->id, output_size, key_frame, | 732 dqbuf.timestamp.tv_usec + |
660 base::TimeDelta::FromMicroseconds( | 733 dqbuf.timestamp.tv_sec * |
661 dqbuf.timestamp.tv_usec + | 734 base::Time::kMicrosecondsPerSecond))); |
662 dqbuf.timestamp.tv_sec * base::Time::kMicrosecondsPerSecond))); | 735 |
663 output_record.at_device = false; | 736 output_record.at_device = false; |
664 output_record.buffer_ref.reset(); | |
665 free_output_buffers_.push_back(dqbuf.index); | 737 free_output_buffers_.push_back(dqbuf.index); |
666 output_buffer_queued_count_--; | 738 output_buffer_queued_count_--; |
667 } | 739 } |
668 } | 740 } |
669 | 741 |
670 bool V4L2VideoEncodeAccelerator::EnqueueInputRecord() { | 742 bool V4L2VideoEncodeAccelerator::EnqueueInputRecord() { |
671 DVLOG(3) << "EnqueueInputRecord()"; | 743 DVLOG(3) << "EnqueueInputRecord()"; |
672 DCHECK(!free_input_buffers_.empty()); | 744 DCHECK(!free_input_buffers_.empty()); |
673 DCHECK(!encoder_input_queue_.empty()); | 745 DCHECK(!encoder_input_queue_.empty()); |
674 | 746 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
725 input_buffer_queued_count_++; | 797 input_buffer_queued_count_++; |
726 return true; | 798 return true; |
727 } | 799 } |
728 | 800 |
729 bool V4L2VideoEncodeAccelerator::EnqueueOutputRecord() { | 801 bool V4L2VideoEncodeAccelerator::EnqueueOutputRecord() { |
730 DVLOG(3) << "EnqueueOutputRecord()"; | 802 DVLOG(3) << "EnqueueOutputRecord()"; |
731 DCHECK(!free_output_buffers_.empty()); | 803 DCHECK(!free_output_buffers_.empty()); |
732 DCHECK(!encoder_output_queue_.empty()); | 804 DCHECK(!encoder_output_queue_.empty()); |
733 | 805 |
734 // Enqueue an output (VIDEO_CAPTURE) buffer. | 806 // Enqueue an output (VIDEO_CAPTURE) buffer. |
735 linked_ptr<BitstreamBufferRef> output_buffer = encoder_output_queue_.back(); | |
736 const int index = free_output_buffers_.back(); | 807 const int index = free_output_buffers_.back(); |
737 OutputRecord& output_record = output_buffer_map_[index]; | 808 OutputRecord& output_record = output_buffer_map_[index]; |
738 DCHECK(!output_record.at_device); | 809 DCHECK(!output_record.at_device); |
739 DCHECK(!output_record.buffer_ref.get()); | 810 DCHECK(!output_record.buffer_ref); |
740 struct v4l2_buffer qbuf; | 811 struct v4l2_buffer qbuf; |
741 struct v4l2_plane qbuf_planes[1]; | 812 struct v4l2_plane qbuf_planes[1]; |
742 memset(&qbuf, 0, sizeof(qbuf)); | 813 memset(&qbuf, 0, sizeof(qbuf)); |
743 memset(qbuf_planes, 0, sizeof(qbuf_planes)); | 814 memset(qbuf_planes, 0, sizeof(qbuf_planes)); |
744 qbuf.index = index; | 815 qbuf.index = index; |
745 qbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | 816 qbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
746 qbuf.memory = V4L2_MEMORY_MMAP; | 817 qbuf.memory = V4L2_MEMORY_MMAP; |
747 qbuf.m.planes = qbuf_planes; | 818 qbuf.m.planes = qbuf_planes; |
748 qbuf.length = 1; | 819 qbuf.length = 1; |
749 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QBUF, &qbuf); | 820 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QBUF, &qbuf); |
750 output_record.at_device = true; | 821 output_record.at_device = true; |
751 output_record.buffer_ref = output_buffer; | 822 output_record.buffer_ref = std::move(encoder_output_queue_.back()); |
752 encoder_output_queue_.pop_back(); | 823 encoder_output_queue_.pop_back(); |
753 free_output_buffers_.pop_back(); | 824 free_output_buffers_.pop_back(); |
754 output_buffer_queued_count_++; | 825 output_buffer_queued_count_++; |
755 return true; | 826 return true; |
756 } | 827 } |
757 | 828 |
758 bool V4L2VideoEncodeAccelerator::StartDevicePoll() { | 829 bool V4L2VideoEncodeAccelerator::StartDevicePoll() { |
759 DVLOG(3) << "StartDevicePoll()"; | 830 DVLOG(3) << "StartDevicePoll()"; |
760 DCHECK(encoder_thread_.task_runner()->BelongsToCurrentThread()); | 831 DCHECK(encoder_thread_.task_runner()->BelongsToCurrentThread()); |
761 DCHECK(!device_poll_thread_.IsRunning()); | 832 DCHECK(!device_poll_thread_.IsRunning()); |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1037 // The width and height might be adjusted by driver. | 1108 // The width and height might be adjusted by driver. |
1038 // Need to read it back and set to visible_size_. | 1109 // Need to read it back and set to visible_size_. |
1039 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_G_CROP, &crop); | 1110 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_G_CROP, &crop); |
1040 visible_size_.SetSize(crop.c.width, crop.c.height); | 1111 visible_size_.SetSize(crop.c.width, crop.c.height); |
1041 DVLOG(3) << "After adjusted by driver, visible_size_=" | 1112 DVLOG(3) << "After adjusted by driver, visible_size_=" |
1042 << visible_size_.ToString(); | 1113 << visible_size_.ToString(); |
1043 | 1114 |
1044 return true; | 1115 return true; |
1045 } | 1116 } |
1046 | 1117 |
| 1118 bool V4L2VideoEncodeAccelerator::IsCtrlExposed(uint32_t ctrl_id) { |
| 1119 struct v4l2_queryctrl query_ctrl; |
| 1120 memset(&query_ctrl, 0, sizeof(query_ctrl)); |
| 1121 query_ctrl.id = ctrl_id; |
| 1122 |
| 1123 return device_->Ioctl(VIDIOC_QUERYCTRL, &query_ctrl) == 0; |
| 1124 } |
| 1125 |
1047 bool V4L2VideoEncodeAccelerator::SetExtCtrls( | 1126 bool V4L2VideoEncodeAccelerator::SetExtCtrls( |
1048 std::vector<struct v4l2_ext_control> ctrls) { | 1127 std::vector<struct v4l2_ext_control> ctrls) { |
1049 struct v4l2_ext_controls ext_ctrls; | 1128 struct v4l2_ext_controls ext_ctrls; |
1050 memset(&ext_ctrls, 0, sizeof(ext_ctrls)); | 1129 memset(&ext_ctrls, 0, sizeof(ext_ctrls)); |
1051 ext_ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG; | 1130 ext_ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG; |
1052 ext_ctrls.count = ctrls.size(); | 1131 ext_ctrls.count = ctrls.size(); |
1053 ext_ctrls.controls = &ctrls[0]; | 1132 ext_ctrls.controls = &ctrls[0]; |
1054 return device_->Ioctl(VIDIOC_S_EXT_CTRLS, &ext_ctrls) == 0; | 1133 return device_->Ioctl(VIDIOC_S_EXT_CTRLS, &ext_ctrls) == 0; |
1055 } | 1134 } |
1056 | 1135 |
1057 bool V4L2VideoEncodeAccelerator::InitControls() { | 1136 bool V4L2VideoEncodeAccelerator::InitControls() { |
1058 std::vector<struct v4l2_ext_control> ctrls; | 1137 std::vector<struct v4l2_ext_control> ctrls; |
1059 struct v4l2_ext_control ctrl; | 1138 struct v4l2_ext_control ctrl; |
1060 | 1139 |
1061 // Enable frame-level bitrate control. This is the only mandatory control. | 1140 // Enable frame-level bitrate control. This is the only mandatory control. |
1062 memset(&ctrl, 0, sizeof(ctrl)); | 1141 memset(&ctrl, 0, sizeof(ctrl)); |
1063 ctrl.id = V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE; | 1142 ctrl.id = V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE; |
1064 ctrl.value = 1; | 1143 ctrl.value = 1; |
1065 ctrls.push_back(ctrl); | 1144 ctrls.push_back(ctrl); |
1066 if (!SetExtCtrls(ctrls)) { | 1145 if (!SetExtCtrls(ctrls)) { |
1067 LOG(ERROR) << "Failed enabling bitrate control"; | 1146 LOG(ERROR) << "Failed enabling bitrate control"; |
1068 NOTIFY_ERROR(kPlatformFailureError); | 1147 NOTIFY_ERROR(kPlatformFailureError); |
1069 return false; | 1148 return false; |
1070 } | 1149 } |
1071 | 1150 |
1072 // Optional controls. | |
1073 ctrls.clear(); | 1151 ctrls.clear(); |
1074 if (output_format_fourcc_ == V4L2_PIX_FMT_H264) { | 1152 if (output_format_fourcc_ == V4L2_PIX_FMT_H264) { |
| 1153 #ifndef V4L2_CID_MPEG_VIDEO_H264_SPS_PPS_BEFORE_IDR |
| 1154 #define V4L2_CID_MPEG_VIDEO_H264_SPS_PPS_BEFORE_IDR (V4L2_CID_MPEG_BASE + 388) |
| 1155 #endif |
| 1156 // Request to inject SPS and PPS before each IDR, if the device supports |
| 1157 // that feature. Otherwise we'll have to cache and inject ourselves. |
| 1158 if (IsCtrlExposed(V4L2_CID_MPEG_VIDEO_H264_SPS_PPS_BEFORE_IDR)) { |
| 1159 memset(&ctrl, 0, sizeof(ctrl)); |
| 1160 ctrl.id = V4L2_CID_MPEG_VIDEO_H264_SPS_PPS_BEFORE_IDR; |
| 1161 ctrl.value = 1; |
| 1162 ctrls.push_back(ctrl); |
| 1163 if (!SetExtCtrls(ctrls)) { |
| 1164 NOTIFY_ERROR(kPlatformFailureError); |
| 1165 return false; |
| 1166 } |
| 1167 ctrls.clear(); |
| 1168 inject_sps_and_pps_ = false; |
| 1169 DVLOG(1) << "Device supports injecting SPS+PPS before each IDR"; |
| 1170 } else { |
| 1171 inject_sps_and_pps_ = true; |
| 1172 DVLOG(1) << "Will inject SPS+PPS before each IDR, unsupported by device"; |
| 1173 } |
| 1174 |
| 1175 // Optional controls. |
1075 // No B-frames, for lowest decoding latency. | 1176 // No B-frames, for lowest decoding latency. |
1076 memset(&ctrl, 0, sizeof(ctrl)); | 1177 memset(&ctrl, 0, sizeof(ctrl)); |
1077 ctrl.id = V4L2_CID_MPEG_VIDEO_B_FRAMES; | 1178 ctrl.id = V4L2_CID_MPEG_VIDEO_B_FRAMES; |
1078 ctrl.value = 0; | 1179 ctrl.value = 0; |
1079 ctrls.push_back(ctrl); | 1180 ctrls.push_back(ctrl); |
1080 | 1181 |
1081 // Quantization parameter maximum value (for variable bitrate control). | 1182 // Quantization parameter maximum value (for variable bitrate control). |
1082 memset(&ctrl, 0, sizeof(ctrl)); | 1183 memset(&ctrl, 0, sizeof(ctrl)); |
1083 ctrl.id = V4L2_CID_MPEG_VIDEO_H264_MAX_QP; | 1184 ctrl.id = V4L2_CID_MPEG_VIDEO_H264_MAX_QP; |
1084 ctrl.value = 51; | 1185 ctrl.value = 51; |
1085 ctrls.push_back(ctrl); | 1186 ctrls.push_back(ctrl); |
1086 | 1187 |
1087 // Use H.264 level 4.0 to match the supported max resolution. | 1188 // Use H.264 level 4.0 to match the supported max resolution. |
1088 memset(&ctrl, 0, sizeof(ctrl)); | 1189 memset(&ctrl, 0, sizeof(ctrl)); |
1089 ctrl.id = V4L2_CID_MPEG_VIDEO_H264_LEVEL; | 1190 ctrl.id = V4L2_CID_MPEG_VIDEO_H264_LEVEL; |
1090 ctrl.value = V4L2_MPEG_VIDEO_H264_LEVEL_4_0; | 1191 ctrl.value = V4L2_MPEG_VIDEO_H264_LEVEL_4_0; |
1091 ctrls.push_back(ctrl); | 1192 ctrls.push_back(ctrl); |
1092 | 1193 |
1093 // Separate stream header so we can cache it and insert into the stream. | 1194 // Ask not to put SPS and PPS into separate bitstream buffers. |
1094 memset(&ctrl, 0, sizeof(ctrl)); | 1195 memset(&ctrl, 0, sizeof(ctrl)); |
1095 ctrl.id = V4L2_CID_MPEG_VIDEO_HEADER_MODE; | 1196 ctrl.id = V4L2_CID_MPEG_VIDEO_HEADER_MODE; |
1096 ctrl.value = V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE; | 1197 ctrl.value = V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME; |
1097 ctrls.push_back(ctrl); | 1198 ctrls.push_back(ctrl); |
1098 } | 1199 } |
1099 | 1200 |
1100 // Enable macroblock-level bitrate control. | 1201 // Enable macroblock-level bitrate control. |
1101 memset(&ctrl, 0, sizeof(ctrl)); | 1202 memset(&ctrl, 0, sizeof(ctrl)); |
1102 ctrl.id = V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE; | 1203 ctrl.id = V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE; |
1103 ctrl.value = 1; | 1204 ctrl.value = 1; |
1104 ctrls.push_back(ctrl); | 1205 ctrls.push_back(ctrl); |
1105 | 1206 |
1106 // Disable periodic key frames. | 1207 // Disable periodic key frames. |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1171 DCHECK(!output_streamon_); | 1272 DCHECK(!output_streamon_); |
1172 | 1273 |
1173 struct v4l2_requestbuffers reqbufs; | 1274 struct v4l2_requestbuffers reqbufs; |
1174 memset(&reqbufs, 0, sizeof(reqbufs)); | 1275 memset(&reqbufs, 0, sizeof(reqbufs)); |
1175 reqbufs.count = kOutputBufferCount; | 1276 reqbufs.count = kOutputBufferCount; |
1176 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | 1277 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
1177 reqbufs.memory = V4L2_MEMORY_MMAP; | 1278 reqbufs.memory = V4L2_MEMORY_MMAP; |
1178 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_REQBUFS, &reqbufs); | 1279 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_REQBUFS, &reqbufs); |
1179 | 1280 |
1180 DCHECK(output_buffer_map_.empty()); | 1281 DCHECK(output_buffer_map_.empty()); |
1181 output_buffer_map_.resize(reqbufs.count); | 1282 output_buffer_map_ = std::vector<OutputRecord>(reqbufs.count); |
1182 for (size_t i = 0; i < output_buffer_map_.size(); ++i) { | 1283 for (size_t i = 0; i < output_buffer_map_.size(); ++i) { |
1183 struct v4l2_plane planes[1]; | 1284 struct v4l2_plane planes[1]; |
1184 struct v4l2_buffer buffer; | 1285 struct v4l2_buffer buffer; |
1185 memset(&buffer, 0, sizeof(buffer)); | 1286 memset(&buffer, 0, sizeof(buffer)); |
1186 memset(planes, 0, sizeof(planes)); | 1287 memset(planes, 0, sizeof(planes)); |
1187 buffer.index = i; | 1288 buffer.index = i; |
1188 buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | 1289 buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
1189 buffer.memory = V4L2_MEMORY_MMAP; | 1290 buffer.memory = V4L2_MEMORY_MMAP; |
1190 buffer.m.planes = planes; | 1291 buffer.m.planes = planes; |
1191 buffer.length = arraysize(planes); | 1292 buffer.length = arraysize(planes); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1239 reqbufs.count = 0; | 1340 reqbufs.count = 0; |
1240 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | 1341 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
1241 reqbufs.memory = V4L2_MEMORY_MMAP; | 1342 reqbufs.memory = V4L2_MEMORY_MMAP; |
1242 IOCTL_OR_LOG_ERROR(VIDIOC_REQBUFS, &reqbufs); | 1343 IOCTL_OR_LOG_ERROR(VIDIOC_REQBUFS, &reqbufs); |
1243 | 1344 |
1244 output_buffer_map_.clear(); | 1345 output_buffer_map_.clear(); |
1245 free_output_buffers_.clear(); | 1346 free_output_buffers_.clear(); |
1246 } | 1347 } |
1247 | 1348 |
1248 } // namespace media | 1349 } // namespace media |
OLD | NEW |