| 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 #ifndef MEDIA_GPU_V4L2_VIDEO_ENCODE_ACCELERATOR_H_ | 5 #ifndef MEDIA_GPU_V4L2_VIDEO_ENCODE_ACCELERATOR_H_ |
| 6 #define MEDIA_GPU_V4L2_VIDEO_ENCODE_ACCELERATOR_H_ | 6 #define MEDIA_GPU_V4L2_VIDEO_ENCODE_ACCELERATOR_H_ |
| 7 | 7 |
| 8 #include <linux/videodev2.h> | 8 #include <linux/videodev2.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 #include <stdint.h> | 10 #include <stdint.h> |
| 11 | 11 |
| 12 #include <list> | |
| 13 #include <memory> | 12 #include <memory> |
| 13 #include <queue> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/files/scoped_file.h" | 16 #include "base/files/scoped_file.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/memory/linked_ptr.h" | |
| 19 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 20 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
| 21 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 22 #include "media/gpu/media_gpu_export.h" | 21 #include "media/gpu/media_gpu_export.h" |
| 23 #include "media/gpu/v4l2_device.h" | 22 #include "media/gpu/v4l2_device.h" |
| 24 #include "media/gpu/v4l2_image_processor.h" | 23 #include "media/gpu/v4l2_image_processor.h" |
| 25 #include "media/video/video_encode_accelerator.h" | 24 #include "media/video/video_encode_accelerator.h" |
| 26 #include "ui/gfx/geometry/size.h" | 25 #include "ui/gfx/geometry/size.h" |
| 27 | 26 |
| 28 namespace media { | 27 namespace media { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 ~InputRecord(); | 70 ~InputRecord(); |
| 72 bool at_device; | 71 bool at_device; |
| 73 scoped_refptr<VideoFrame> frame; | 72 scoped_refptr<VideoFrame> frame; |
| 74 }; | 73 }; |
| 75 | 74 |
| 76 // Record for output buffers. | 75 // Record for output buffers. |
| 77 struct OutputRecord { | 76 struct OutputRecord { |
| 78 OutputRecord(); | 77 OutputRecord(); |
| 79 ~OutputRecord(); | 78 ~OutputRecord(); |
| 80 bool at_device; | 79 bool at_device; |
| 81 linked_ptr<BitstreamBufferRef> buffer_ref; | 80 std::unique_ptr<BitstreamBufferRef> buffer_ref; |
| 82 void* address; | 81 void* address; |
| 83 size_t length; | 82 size_t length; |
| 84 }; | 83 }; |
| 85 | 84 |
| 86 struct ImageProcessorInputRecord { | 85 struct ImageProcessorInputRecord { |
| 87 ImageProcessorInputRecord(); | 86 ImageProcessorInputRecord(); |
| 88 ~ImageProcessorInputRecord(); | 87 ~ImageProcessorInputRecord(); |
| 89 scoped_refptr<VideoFrame> frame; | 88 scoped_refptr<VideoFrame> frame; |
| 90 bool force_keyframe; | 89 bool force_keyframe; |
| 91 }; | 90 }; |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 bool CreateInputBuffers(); | 193 bool CreateInputBuffers(); |
| 195 bool CreateOutputBuffers(); | 194 bool CreateOutputBuffers(); |
| 196 | 195 |
| 197 // Destroy these buffers. | 196 // Destroy these buffers. |
| 198 void DestroyInputBuffers(); | 197 void DestroyInputBuffers(); |
| 199 void DestroyOutputBuffers(); | 198 void DestroyOutputBuffers(); |
| 200 | 199 |
| 201 // Set controls in |ctrls| and return true if successful. | 200 // Set controls in |ctrls| and return true if successful. |
| 202 bool SetExtCtrls(std::vector<struct v4l2_ext_control> ctrls); | 201 bool SetExtCtrls(std::vector<struct v4l2_ext_control> ctrls); |
| 203 | 202 |
| 203 // Return true if a V4L2 control of |ctrl_id| is supported by the device, |
| 204 // false otherwise. |
| 205 bool IsCtrlExposed(uint32_t ctrl_id); |
| 206 |
| 204 // Recycle output buffer of image processor with |output_buffer_index|. | 207 // Recycle output buffer of image processor with |output_buffer_index|. |
| 205 void ReuseImageProcessorOutputBuffer(int output_buffer_index); | 208 void ReuseImageProcessorOutputBuffer(int output_buffer_index); |
| 206 | 209 |
| 210 // Copy encoded stream data from an output V4L2 buffer at |bitstream_data| |
| 211 // of size |bitstream_size| into a BitstreamBuffer referenced by |buffer_ref|, |
| 212 // injecting stream headers if required. Return the size in bytes of the |
| 213 // resulting stream in the destination buffer. |
| 214 size_t CopyIntoOutputBuffer(const uint8_t* bitstream_data, |
| 215 size_t bitstream_size, |
| 216 std::unique_ptr<BitstreamBufferRef> buffer_ref); |
| 217 |
| 207 // Our original calling task runner for the child thread. | 218 // Our original calling task runner for the child thread. |
| 208 const scoped_refptr<base::SingleThreadTaskRunner> child_task_runner_; | 219 const scoped_refptr<base::SingleThreadTaskRunner> child_task_runner_; |
| 209 | 220 |
| 210 gfx::Size visible_size_; | 221 gfx::Size visible_size_; |
| 211 // Input allocated size required by the device. | 222 // Input allocated size required by the device. |
| 212 gfx::Size input_allocated_size_; | 223 gfx::Size input_allocated_size_; |
| 213 size_t output_buffer_byte_size_; | 224 size_t output_buffer_byte_size_; |
| 214 | 225 |
| 215 // Formats for input frames and the output stream. | 226 // Formats for input frames and the output stream. |
| 216 VideoPixelFormat device_input_format_; | 227 VideoPixelFormat device_input_format_; |
| 217 size_t input_planes_count_; | 228 size_t input_planes_count_; |
| 218 uint32_t output_format_fourcc_; | 229 uint32_t output_format_fourcc_; |
| 219 | 230 |
| 220 // | 231 // |
| 221 // Encoder state, owned and operated by encoder_thread_. | 232 // Encoder state, owned and operated by encoder_thread_. |
| 222 // Before encoder_thread_ has started, the encoder state is managed by | 233 // Before encoder_thread_ has started, the encoder state is managed by |
| 223 // the child (main) thread. After encoder_thread_ has started, the encoder | 234 // the child (main) thread. After encoder_thread_ has started, the encoder |
| 224 // thread should be the only one managing these. | 235 // thread should be the only one managing these. |
| 225 // | 236 // |
| 226 | 237 |
| 227 // Encoder state. | 238 // Encoder state. |
| 228 State encoder_state_; | 239 State encoder_state_; |
| 229 | 240 |
| 230 // We need to provide the stream header with every keyframe, to allow | 241 // For H264, for resilience, we prepend each IDR with SPS and PPS. Some |
| 231 // midstream decoding restarts. Store it here. | 242 // devices support this via the V4L2_CID_MPEG_VIDEO_H264_SPS_PPS_BEFORE_IDR |
| 232 std::unique_ptr<uint8_t[]> stream_header_; | 243 // control. For devices that don't, we cache the latest SPS and PPS and inject |
| 233 size_t stream_header_size_; | 244 // them into the stream before every IDR. |
| 245 bool inject_sps_and_pps_ = false; |
| 246 // Cached SPS (without H.264 start code). |
| 247 std::vector<uint8_t> cached_sps_; |
| 248 // Cached PPS (without H.264 start code). |
| 249 std::vector<uint8_t> cached_pps_; |
| 250 // Size in bytes required to inject cached SPS and PPS, including H.264 |
| 251 // start codes. |
| 252 size_t cached_h264_header_size_ = 0; |
| 234 | 253 |
| 235 // Video frames ready to be encoded. | 254 // Video frames ready to be encoded. |
| 236 std::queue<scoped_refptr<VideoFrame>> encoder_input_queue_; | 255 std::queue<scoped_refptr<VideoFrame>> encoder_input_queue_; |
| 237 | 256 |
| 238 // Encoder device. | 257 // Encoder device. |
| 239 scoped_refptr<V4L2Device> device_; | 258 scoped_refptr<V4L2Device> device_; |
| 240 | 259 |
| 241 // Input queue state. | 260 // Input queue state. |
| 242 bool input_streamon_; | 261 bool input_streamon_; |
| 243 // Input buffers enqueued to device. | 262 // Input buffers enqueued to device. |
| 244 int input_buffer_queued_count_; | 263 int input_buffer_queued_count_; |
| 245 // Input buffers ready to use; LIFO since we don't care about ordering. | 264 // Input buffers ready to use; LIFO since we don't care about ordering. |
| 246 std::vector<int> free_input_buffers_; | 265 std::vector<int> free_input_buffers_; |
| 247 // Mapping of int index to input buffer record. | 266 // Mapping of int index to input buffer record. |
| 248 std::vector<InputRecord> input_buffer_map_; | 267 std::vector<InputRecord> input_buffer_map_; |
| 249 v4l2_memory input_memory_type_; | 268 v4l2_memory input_memory_type_; |
| 250 | 269 |
| 251 // Output queue state. | 270 // Output queue state. |
| 252 bool output_streamon_; | 271 bool output_streamon_; |
| 253 // Output buffers enqueued to device. | 272 // Output buffers enqueued to device. |
| 254 int output_buffer_queued_count_; | 273 int output_buffer_queued_count_; |
| 255 // Output buffers ready to use; LIFO since we don't care about ordering. | 274 // Output buffers ready to use; LIFO since we don't care about ordering. |
| 256 std::vector<int> free_output_buffers_; | 275 std::vector<int> free_output_buffers_; |
| 257 // Mapping of int index to output buffer record. | 276 // Mapping of int index to output buffer record. |
| 258 std::vector<OutputRecord> output_buffer_map_; | 277 std::vector<OutputRecord> output_buffer_map_; |
| 259 | 278 |
| 260 // Bitstream buffers ready to be used to return encoded output, as a LIFO | 279 // Bitstream buffers ready to be used to return encoded output, as a LIFO |
| 261 // since we don't care about ordering. | 280 // since we don't care about ordering. |
| 262 std::vector<linked_ptr<BitstreamBufferRef>> encoder_output_queue_; | 281 std::vector<std::unique_ptr<BitstreamBufferRef>> encoder_output_queue_; |
| 263 | 282 |
| 264 // Image processor, if one is in use. | 283 // Image processor, if one is in use. |
| 265 std::unique_ptr<V4L2ImageProcessor> image_processor_; | 284 std::unique_ptr<V4L2ImageProcessor> image_processor_; |
| 266 // Indexes of free image processor output buffers. Only accessed on child | 285 // Indexes of free image processor output buffers. Only accessed on child |
| 267 // thread. | 286 // thread. |
| 268 std::vector<int> free_image_processor_output_buffers_; | 287 std::vector<int> free_image_processor_output_buffers_; |
| 269 // Video frames ready to be processed. Only accessed on child thread. | 288 // Video frames ready to be processed. Only accessed on child thread. |
| 270 std::queue<ImageProcessorInputRecord> image_processor_input_queue_; | 289 std::queue<ImageProcessorInputRecord> image_processor_input_queue_; |
| 271 // Mapping of int index to fds of image processor output buffer. | 290 // Mapping of int index to fds of image processor output buffer. |
| 272 std::vector<std::vector<base::ScopedFD>> image_processor_output_buffer_map_; | 291 std::vector<std::vector<base::ScopedFD>> image_processor_output_buffer_map_; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 291 // as both threads will not outlive this object. | 310 // as both threads will not outlive this object. |
| 292 base::WeakPtr<V4L2VideoEncodeAccelerator> weak_this_; | 311 base::WeakPtr<V4L2VideoEncodeAccelerator> weak_this_; |
| 293 base::WeakPtrFactory<V4L2VideoEncodeAccelerator> weak_this_ptr_factory_; | 312 base::WeakPtrFactory<V4L2VideoEncodeAccelerator> weak_this_ptr_factory_; |
| 294 | 313 |
| 295 DISALLOW_COPY_AND_ASSIGN(V4L2VideoEncodeAccelerator); | 314 DISALLOW_COPY_AND_ASSIGN(V4L2VideoEncodeAccelerator); |
| 296 }; | 315 }; |
| 297 | 316 |
| 298 } // namespace media | 317 } // namespace media |
| 299 | 318 |
| 300 #endif // MEDIA_GPU_V4L2_VIDEO_ENCODE_ACCELERATOR_H_ | 319 #endif // MEDIA_GPU_V4L2_VIDEO_ENCODE_ACCELERATOR_H_ |
| OLD | NEW |