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> |
(...skipping 23 matching lines...) Expand all Loading... |
34 namespace media { | 34 namespace media { |
35 | 35 |
36 // This class handles video encode acceleration by interfacing with a V4L2 | 36 // This class handles video encode acceleration by interfacing with a V4L2 |
37 // device exposed by the codec hardware driver. The threading model of this | 37 // device exposed by the codec hardware driver. The threading model of this |
38 // class is the same as in the V4L2VideoDecodeAccelerator (from which class this | 38 // class is the same as in the V4L2VideoDecodeAccelerator (from which class this |
39 // was designed). | 39 // was designed). |
40 // This class may try to instantiate and use a V4L2ImageProcessor for input | 40 // This class may try to instantiate and use a V4L2ImageProcessor for input |
41 // format conversion, if the input format requested via Initialize() is not | 41 // format conversion, if the input format requested via Initialize() is not |
42 // accepted by the hardware codec. | 42 // accepted by the hardware codec. |
43 class MEDIA_GPU_EXPORT V4L2VideoEncodeAccelerator | 43 class MEDIA_GPU_EXPORT V4L2VideoEncodeAccelerator |
44 : public media::VideoEncodeAccelerator { | 44 : public VideoEncodeAccelerator { |
45 public: | 45 public: |
46 explicit V4L2VideoEncodeAccelerator(const scoped_refptr<V4L2Device>& device); | 46 explicit V4L2VideoEncodeAccelerator(const scoped_refptr<V4L2Device>& device); |
47 ~V4L2VideoEncodeAccelerator() override; | 47 ~V4L2VideoEncodeAccelerator() override; |
48 | 48 |
49 // media::VideoEncodeAccelerator implementation. | 49 // VideoEncodeAccelerator implementation. |
50 media::VideoEncodeAccelerator::SupportedProfiles GetSupportedProfiles() | 50 VideoEncodeAccelerator::SupportedProfiles GetSupportedProfiles() override; |
51 override; | 51 bool Initialize(VideoPixelFormat format, |
52 bool Initialize(media::VideoPixelFormat format, | |
53 const gfx::Size& input_visible_size, | 52 const gfx::Size& input_visible_size, |
54 media::VideoCodecProfile output_profile, | 53 VideoCodecProfile output_profile, |
55 uint32_t initial_bitrate, | 54 uint32_t initial_bitrate, |
56 Client* client) override; | 55 Client* client) override; |
57 void Encode(const scoped_refptr<media::VideoFrame>& frame, | 56 void Encode(const scoped_refptr<VideoFrame>& frame, |
58 bool force_keyframe) override; | 57 bool force_keyframe) override; |
59 void UseOutputBitstreamBuffer(const media::BitstreamBuffer& buffer) override; | 58 void UseOutputBitstreamBuffer(const BitstreamBuffer& buffer) override; |
60 void RequestEncodingParametersChange(uint32_t bitrate, | 59 void RequestEncodingParametersChange(uint32_t bitrate, |
61 uint32_t framerate) override; | 60 uint32_t framerate) override; |
62 void Destroy() override; | 61 void Destroy() override; |
63 | 62 |
64 private: | 63 private: |
65 // Auto-destroy reference for BitstreamBuffer, for tracking buffers passed to | 64 // Auto-destroy reference for BitstreamBuffer, for tracking buffers passed to |
66 // this instance. | 65 // this instance. |
67 struct BitstreamBufferRef; | 66 struct BitstreamBufferRef; |
68 | 67 |
69 // Record for codec input buffers. | 68 // Record for codec input buffers. |
70 struct InputRecord { | 69 struct InputRecord { |
71 InputRecord(); | 70 InputRecord(); |
72 ~InputRecord(); | 71 ~InputRecord(); |
73 bool at_device; | 72 bool at_device; |
74 scoped_refptr<media::VideoFrame> frame; | 73 scoped_refptr<VideoFrame> frame; |
75 }; | 74 }; |
76 | 75 |
77 // Record for output buffers. | 76 // Record for output buffers. |
78 struct OutputRecord { | 77 struct OutputRecord { |
79 OutputRecord(); | 78 OutputRecord(); |
80 ~OutputRecord(); | 79 ~OutputRecord(); |
81 bool at_device; | 80 bool at_device; |
82 linked_ptr<BitstreamBufferRef> buffer_ref; | 81 linked_ptr<BitstreamBufferRef> buffer_ref; |
83 void* address; | 82 void* address; |
84 size_t length; | 83 size_t length; |
85 }; | 84 }; |
86 | 85 |
87 struct ImageProcessorInputRecord { | 86 struct ImageProcessorInputRecord { |
88 ImageProcessorInputRecord(); | 87 ImageProcessorInputRecord(); |
89 ~ImageProcessorInputRecord(); | 88 ~ImageProcessorInputRecord(); |
90 scoped_refptr<media::VideoFrame> frame; | 89 scoped_refptr<VideoFrame> frame; |
91 bool force_keyframe; | 90 bool force_keyframe; |
92 }; | 91 }; |
93 | 92 |
94 enum { | 93 enum { |
95 kInitialFramerate = 30, | 94 kInitialFramerate = 30, |
96 // These are rather subjectively tuned. | 95 // These are rather subjectively tuned. |
97 kInputBufferCount = 2, | 96 kInputBufferCount = 2, |
98 kOutputBufferCount = 2, | 97 kOutputBufferCount = 2, |
99 kImageProcBufferCount = 2, | 98 kImageProcBufferCount = 2, |
100 kOutputBufferSize = (2 * 1024 * 1024), | 99 kOutputBufferSize = (2 * 1024 * 1024), |
(...skipping 16 matching lines...) Expand all Loading... |
117 base::TimeDelta timestamp, | 116 base::TimeDelta timestamp, |
118 int output_buffer_index); | 117 int output_buffer_index); |
119 | 118 |
120 // Error callback for handling image processor errors. | 119 // Error callback for handling image processor errors. |
121 void ImageProcessorError(); | 120 void ImageProcessorError(); |
122 | 121 |
123 // | 122 // |
124 // Encoding tasks, to be run on encode_thread_. | 123 // Encoding tasks, to be run on encode_thread_. |
125 // | 124 // |
126 | 125 |
127 void EncodeTask(const scoped_refptr<media::VideoFrame>& frame, | 126 void EncodeTask(const scoped_refptr<VideoFrame>& frame, bool force_keyframe); |
128 bool force_keyframe); | |
129 | 127 |
130 // Add a BitstreamBuffer to the queue of buffers ready to be used for encoder | 128 // Add a BitstreamBuffer to the queue of buffers ready to be used for encoder |
131 // output. | 129 // output. |
132 void UseOutputBitstreamBufferTask( | 130 void UseOutputBitstreamBufferTask( |
133 std::unique_ptr<BitstreamBufferRef> buffer_ref); | 131 std::unique_ptr<BitstreamBufferRef> buffer_ref); |
134 | 132 |
135 // Device destruction task. | 133 // Device destruction task. |
136 void DestroyTask(); | 134 void DestroyTask(); |
137 | 135 |
138 // Service I/O on the V4L2 devices. This task should only be scheduled from | 136 // Service I/O on the V4L2 devices. This task should only be scheduled from |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 // Other utility functions. Called on encoder_thread_, unless | 169 // Other utility functions. Called on encoder_thread_, unless |
172 // encoder_thread_ is not yet started, in which case the child thread can call | 170 // encoder_thread_ is not yet started, in which case the child thread can call |
173 // these (e.g. in Initialize() or Destroy()). | 171 // these (e.g. in Initialize() or Destroy()). |
174 // | 172 // |
175 | 173 |
176 // Change encoding parameters. | 174 // Change encoding parameters. |
177 void RequestEncodingParametersChangeTask(uint32_t bitrate, | 175 void RequestEncodingParametersChangeTask(uint32_t bitrate, |
178 uint32_t framerate); | 176 uint32_t framerate); |
179 | 177 |
180 // Set up formats and initialize the device for them. | 178 // Set up formats and initialize the device for them. |
181 bool SetFormats(media::VideoPixelFormat input_format, | 179 bool SetFormats(VideoPixelFormat input_format, |
182 media::VideoCodecProfile output_profile); | 180 VideoCodecProfile output_profile); |
183 | 181 |
184 // Try to set up the device to the input format we were Initialized() with, | 182 // Try to set up the device to the input format we were Initialized() with, |
185 // or if the device doesn't support it, use one it can support, so that we | 183 // or if the device doesn't support it, use one it can support, so that we |
186 // can later instantiate a V4L2ImageProcessor to convert to it. | 184 // can later instantiate a V4L2ImageProcessor to convert to it. |
187 bool NegotiateInputFormat(media::VideoPixelFormat input_format); | 185 bool NegotiateInputFormat(VideoPixelFormat input_format); |
188 | 186 |
189 // Set up the device to the output format requested in Initialize(). | 187 // Set up the device to the output format requested in Initialize(). |
190 bool SetOutputFormat(media::VideoCodecProfile output_profile); | 188 bool SetOutputFormat(VideoCodecProfile output_profile); |
191 | 189 |
192 // Initialize device controls with default values. | 190 // Initialize device controls with default values. |
193 bool InitControls(); | 191 bool InitControls(); |
194 | 192 |
195 // Create the buffers we need. | 193 // Create the buffers we need. |
196 bool CreateInputBuffers(); | 194 bool CreateInputBuffers(); |
197 bool CreateOutputBuffers(); | 195 bool CreateOutputBuffers(); |
198 | 196 |
199 // Destroy these buffers. | 197 // Destroy these buffers. |
200 void DestroyInputBuffers(); | 198 void DestroyInputBuffers(); |
201 void DestroyOutputBuffers(); | 199 void DestroyOutputBuffers(); |
202 | 200 |
203 // Set controls in |ctrls| and return true if successful. | 201 // Set controls in |ctrls| and return true if successful. |
204 bool SetExtCtrls(std::vector<struct v4l2_ext_control> ctrls); | 202 bool SetExtCtrls(std::vector<struct v4l2_ext_control> ctrls); |
205 | 203 |
206 // Recycle output buffer of image processor with |output_buffer_index|. | 204 // Recycle output buffer of image processor with |output_buffer_index|. |
207 void ReuseImageProcessorOutputBuffer(int output_buffer_index); | 205 void ReuseImageProcessorOutputBuffer(int output_buffer_index); |
208 | 206 |
209 // Our original calling task runner for the child thread. | 207 // Our original calling task runner for the child thread. |
210 const scoped_refptr<base::SingleThreadTaskRunner> child_task_runner_; | 208 const scoped_refptr<base::SingleThreadTaskRunner> child_task_runner_; |
211 | 209 |
212 gfx::Size visible_size_; | 210 gfx::Size visible_size_; |
213 // Input allocated size required by the device. | 211 // Input allocated size required by the device. |
214 gfx::Size input_allocated_size_; | 212 gfx::Size input_allocated_size_; |
215 size_t output_buffer_byte_size_; | 213 size_t output_buffer_byte_size_; |
216 | 214 |
217 // Formats for input frames and the output stream. | 215 // Formats for input frames and the output stream. |
218 media::VideoPixelFormat device_input_format_; | 216 VideoPixelFormat device_input_format_; |
219 size_t input_planes_count_; | 217 size_t input_planes_count_; |
220 uint32_t output_format_fourcc_; | 218 uint32_t output_format_fourcc_; |
221 | 219 |
222 // | 220 // |
223 // Encoder state, owned and operated by encoder_thread_. | 221 // Encoder state, owned and operated by encoder_thread_. |
224 // Before encoder_thread_ has started, the encoder state is managed by | 222 // Before encoder_thread_ has started, the encoder state is managed by |
225 // the child (main) thread. After encoder_thread_ has started, the encoder | 223 // the child (main) thread. After encoder_thread_ has started, the encoder |
226 // thread should be the only one managing these. | 224 // thread should be the only one managing these. |
227 // | 225 // |
228 | 226 |
229 // Encoder state. | 227 // Encoder state. |
230 State encoder_state_; | 228 State encoder_state_; |
231 | 229 |
232 // We need to provide the stream header with every keyframe, to allow | 230 // We need to provide the stream header with every keyframe, to allow |
233 // midstream decoding restarts. Store it here. | 231 // midstream decoding restarts. Store it here. |
234 std::unique_ptr<uint8_t[]> stream_header_; | 232 std::unique_ptr<uint8_t[]> stream_header_; |
235 size_t stream_header_size_; | 233 size_t stream_header_size_; |
236 | 234 |
237 // Video frames ready to be encoded. | 235 // Video frames ready to be encoded. |
238 std::queue<scoped_refptr<media::VideoFrame>> encoder_input_queue_; | 236 std::queue<scoped_refptr<VideoFrame>> encoder_input_queue_; |
239 | 237 |
240 // Encoder device. | 238 // Encoder device. |
241 scoped_refptr<V4L2Device> device_; | 239 scoped_refptr<V4L2Device> device_; |
242 | 240 |
243 // Input queue state. | 241 // Input queue state. |
244 bool input_streamon_; | 242 bool input_streamon_; |
245 // Input buffers enqueued to device. | 243 // Input buffers enqueued to device. |
246 int input_buffer_queued_count_; | 244 int input_buffer_queued_count_; |
247 // Input buffers ready to use; LIFO since we don't care about ordering. | 245 // Input buffers ready to use; LIFO since we don't care about ordering. |
248 std::vector<int> free_input_buffers_; | 246 std::vector<int> free_input_buffers_; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 // as both threads will not outlive this object. | 291 // as both threads will not outlive this object. |
294 base::WeakPtr<V4L2VideoEncodeAccelerator> weak_this_; | 292 base::WeakPtr<V4L2VideoEncodeAccelerator> weak_this_; |
295 base::WeakPtrFactory<V4L2VideoEncodeAccelerator> weak_this_ptr_factory_; | 293 base::WeakPtrFactory<V4L2VideoEncodeAccelerator> weak_this_ptr_factory_; |
296 | 294 |
297 DISALLOW_COPY_AND_ASSIGN(V4L2VideoEncodeAccelerator); | 295 DISALLOW_COPY_AND_ASSIGN(V4L2VideoEncodeAccelerator); |
298 }; | 296 }; |
299 | 297 |
300 } // namespace media | 298 } // namespace media |
301 | 299 |
302 #endif // MEDIA_GPU_V4L2_VIDEO_ENCODE_ACCELERATOR_H_ | 300 #endif // MEDIA_GPU_V4L2_VIDEO_ENCODE_ACCELERATOR_H_ |
OLD | NEW |