| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CONTENT_COMMON_GPU_MEDIA_V4L2_SLICE_VIDEO_DECODE_ACCELERATOR_H_ | |
| 6 #define CONTENT_COMMON_GPU_MEDIA_V4L2_SLICE_VIDEO_DECODE_ACCELERATOR_H_ | |
| 7 | |
| 8 #include <linux/videodev2.h> | |
| 9 #include <stddef.h> | |
| 10 #include <stdint.h> | |
| 11 | |
| 12 #include <memory> | |
| 13 #include <queue> | |
| 14 #include <vector> | |
| 15 | |
| 16 #include "base/macros.h" | |
| 17 #include "base/memory/linked_ptr.h" | |
| 18 #include "base/memory/ref_counted.h" | |
| 19 #include "base/memory/weak_ptr.h" | |
| 20 #include "base/synchronization/waitable_event.h" | |
| 21 #include "base/threading/thread.h" | |
| 22 #include "content/common/content_export.h" | |
| 23 #include "content/common/gpu/media/gpu_video_decode_accelerator_helpers.h" | |
| 24 #include "content/common/gpu/media/h264_decoder.h" | |
| 25 #include "content/common/gpu/media/v4l2_device.h" | |
| 26 #include "content/common/gpu/media/vp8_decoder.h" | |
| 27 #include "media/video/video_decode_accelerator.h" | |
| 28 | |
| 29 namespace content { | |
| 30 | |
| 31 // An implementation of VideoDecodeAccelerator that utilizes the V4L2 slice | |
| 32 // level codec API for decoding. The slice level API provides only a low-level | |
| 33 // decoding functionality and requires userspace to provide support for parsing | |
| 34 // the input stream and managing decoder state across frames. | |
| 35 class CONTENT_EXPORT V4L2SliceVideoDecodeAccelerator | |
| 36 : public media::VideoDecodeAccelerator { | |
| 37 public: | |
| 38 class V4L2DecodeSurface; | |
| 39 | |
| 40 V4L2SliceVideoDecodeAccelerator( | |
| 41 const scoped_refptr<V4L2Device>& device, | |
| 42 EGLDisplay egl_display, | |
| 43 const GetGLContextCallback& get_gl_context_cb, | |
| 44 const MakeGLContextCurrentCallback& make_context_current_cb); | |
| 45 ~V4L2SliceVideoDecodeAccelerator() override; | |
| 46 | |
| 47 // media::VideoDecodeAccelerator implementation. | |
| 48 bool Initialize(const Config& config, Client* client) override; | |
| 49 void Decode(const media::BitstreamBuffer& bitstream_buffer) override; | |
| 50 void AssignPictureBuffers( | |
| 51 const std::vector<media::PictureBuffer>& buffers) override; | |
| 52 void ReusePictureBuffer(int32_t picture_buffer_id) override; | |
| 53 void Flush() override; | |
| 54 void Reset() override; | |
| 55 void Destroy() override; | |
| 56 bool TryToSetupDecodeOnSeparateThread( | |
| 57 const base::WeakPtr<Client>& decode_client, | |
| 58 const scoped_refptr<base::SingleThreadTaskRunner>& decode_task_runner) | |
| 59 override; | |
| 60 | |
| 61 static media::VideoDecodeAccelerator::SupportedProfiles | |
| 62 GetSupportedProfiles(); | |
| 63 | |
| 64 private: | |
| 65 class V4L2H264Accelerator; | |
| 66 class V4L2VP8Accelerator; | |
| 67 | |
| 68 // Record for input buffers. | |
| 69 struct InputRecord { | |
| 70 InputRecord(); | |
| 71 int32_t input_id; | |
| 72 void* address; | |
| 73 size_t length; | |
| 74 size_t bytes_used; | |
| 75 bool at_device; | |
| 76 }; | |
| 77 | |
| 78 // Record for output buffers. | |
| 79 struct OutputRecord { | |
| 80 OutputRecord(); | |
| 81 bool at_device; | |
| 82 bool at_client; | |
| 83 int32_t picture_id; | |
| 84 EGLImageKHR egl_image; | |
| 85 EGLSyncKHR egl_sync; | |
| 86 bool cleared; | |
| 87 }; | |
| 88 | |
| 89 // See http://crbug.com/255116. | |
| 90 // Input bitstream buffer size for up to 1080p streams. | |
| 91 const size_t kInputBufferMaxSizeFor1080p = 1024 * 1024; | |
| 92 // Input bitstream buffer size for up to 4k streams. | |
| 93 const size_t kInputBufferMaxSizeFor4k = 4 * kInputBufferMaxSizeFor1080p; | |
| 94 const size_t kNumInputBuffers = 16; | |
| 95 | |
| 96 // Input format V4L2 fourccs this class supports. | |
| 97 static const uint32_t supported_input_fourccs_[]; | |
| 98 | |
| 99 // | |
| 100 // Below methods are used by accelerator implementations. | |
| 101 // | |
| 102 // Append slice data in |data| of size |size| to pending hardware | |
| 103 // input buffer with |index|. This buffer will be submitted for decode | |
| 104 // on the next DecodeSurface(). Return true on success. | |
| 105 bool SubmitSlice(int index, const uint8_t* data, size_t size); | |
| 106 | |
| 107 // Submit controls in |ext_ctrls| to hardware. Return true on success. | |
| 108 bool SubmitExtControls(struct v4l2_ext_controls* ext_ctrls); | |
| 109 | |
| 110 // Decode of |dec_surface| is ready to be submitted and all codec-specific | |
| 111 // settings are set in hardware. | |
| 112 void DecodeSurface(const scoped_refptr<V4L2DecodeSurface>& dec_surface); | |
| 113 | |
| 114 // |dec_surface| is ready to be outputted once decode is finished. | |
| 115 // This can be called before decode is actually done in hardware, and this | |
| 116 // method is responsible for maintaining the ordering, i.e. the surfaces will | |
| 117 // be outputted in the same order as SurfaceReady calls. To do so, the | |
| 118 // surfaces are put on decoder_display_queue_ and sent to output in that | |
| 119 // order once all preceding surfaces are sent. | |
| 120 void SurfaceReady(const scoped_refptr<V4L2DecodeSurface>& dec_surface); | |
| 121 | |
| 122 // | |
| 123 // Internal methods of this class. | |
| 124 // | |
| 125 // Recycle a V4L2 input buffer with |index| after dequeuing from device. | |
| 126 void ReuseInputBuffer(int index); | |
| 127 | |
| 128 // Recycle V4L2 output buffer with |index|. Used as surface release callback. | |
| 129 void ReuseOutputBuffer(int index); | |
| 130 | |
| 131 // Queue a |dec_surface| to device for decoding. | |
| 132 void Enqueue(const scoped_refptr<V4L2DecodeSurface>& dec_surface); | |
| 133 | |
| 134 // Dequeue any V4L2 buffers available and process. | |
| 135 void Dequeue(); | |
| 136 | |
| 137 // V4L2 QBUF helpers. | |
| 138 bool EnqueueInputRecord(int index, uint32_t config_store); | |
| 139 bool EnqueueOutputRecord(int index); | |
| 140 | |
| 141 // Set input and output formats in hardware. | |
| 142 bool SetupFormats(); | |
| 143 | |
| 144 // Create input and output buffers. | |
| 145 bool CreateInputBuffers(); | |
| 146 bool CreateOutputBuffers(); | |
| 147 | |
| 148 // Destroy input buffers. | |
| 149 void DestroyInputBuffers(); | |
| 150 | |
| 151 // Destroy output buffers and release associated resources (textures, | |
| 152 // EGLImages). If |dismiss| is true, also dismissing the associated | |
| 153 // PictureBuffers. | |
| 154 bool DestroyOutputs(bool dismiss); | |
| 155 | |
| 156 // Used by DestroyOutputs. | |
| 157 bool DestroyOutputBuffers(); | |
| 158 | |
| 159 // Dismiss all |picture_buffer_ids| via Client::DismissPictureBuffer() | |
| 160 // and signal |done| after finishing. | |
| 161 void DismissPictures(std::vector<int32_t> picture_buffer_ids, | |
| 162 base::WaitableEvent* done); | |
| 163 | |
| 164 // Task to finish initialization on decoder_thread_. | |
| 165 void InitializeTask(); | |
| 166 | |
| 167 // Surface set change (resolution change) flow. | |
| 168 // If we have no surfaces allocated, just allocate them and return. | |
| 169 // Otherwise mark us as pending for surface set change. | |
| 170 void InitiateSurfaceSetChange(); | |
| 171 // If a surface set change is pending and we are ready, stop the device, | |
| 172 // destroy outputs, releasing resources and dismissing pictures as required, | |
| 173 // followed by allocating a new set for the new resolution/DPB size | |
| 174 // as provided by decoder. Finally, try to resume decoding. | |
| 175 void FinishSurfaceSetChangeIfNeeded(); | |
| 176 | |
| 177 void NotifyError(Error error); | |
| 178 void DestroyTask(); | |
| 179 | |
| 180 // Sets the state to kError and notifies client if needed. | |
| 181 void SetErrorState(Error error); | |
| 182 | |
| 183 // Flush flow when requested by client. | |
| 184 // When Flush() is called, it posts a FlushTask, which checks the input queue. | |
| 185 // If nothing is pending for decode on decoder_input_queue_, we call | |
| 186 // InitiateFlush() directly. Otherwise, we push a dummy BitstreamBufferRef | |
| 187 // onto the decoder_input_queue_ to schedule a flush. When we reach it later | |
| 188 // on, we call InitiateFlush() to perform it at the correct time. | |
| 189 void FlushTask(); | |
| 190 // Tell the decoder to flush all frames, reset it and mark us as scheduled | |
| 191 // for flush, so that we can finish it once all pending decodes are finished. | |
| 192 void InitiateFlush(); | |
| 193 // If all pending frames are decoded and we are waiting to flush, perform it. | |
| 194 // This will send all pending pictures to client and notify the client that | |
| 195 // flush is complete and puts us in a state ready to resume. | |
| 196 void FinishFlushIfNeeded(); | |
| 197 | |
| 198 // Reset flow when requested by client. | |
| 199 // Drop all inputs and reset the decoder and mark us as pending for reset. | |
| 200 void ResetTask(); | |
| 201 // If all pending frames are decoded and we are waiting to reset, perform it. | |
| 202 // This drops all pending outputs (client is not interested anymore), | |
| 203 // notifies the client we are done and puts us in a state ready to resume. | |
| 204 void FinishResetIfNeeded(); | |
| 205 | |
| 206 // Process pending events if any. | |
| 207 void ProcessPendingEventsIfNeeded(); | |
| 208 | |
| 209 // Performed on decoder_thread_ as a consequence of poll() on decoder_thread_ | |
| 210 // returning an event. | |
| 211 void ServiceDeviceTask(); | |
| 212 | |
| 213 // Schedule poll if we have any buffers queued and the poll thread | |
| 214 // is not stopped (on surface set change). | |
| 215 void SchedulePollIfNeeded(); | |
| 216 | |
| 217 // Attempt to start/stop device_poll_thread_. | |
| 218 bool StartDevicePoll(); | |
| 219 bool StopDevicePoll(bool keep_input_state); | |
| 220 | |
| 221 // Ran on device_poll_thread_ to wait for device events. | |
| 222 void DevicePollTask(bool poll_device); | |
| 223 | |
| 224 enum State { | |
| 225 // We are in this state until Initialize() returns successfully. | |
| 226 // We can't post errors to the client in this state yet. | |
| 227 kUninitialized, | |
| 228 // Initialize() returned successfully. | |
| 229 kInitialized, | |
| 230 // This state allows making progress decoding more input stream. | |
| 231 kDecoding, | |
| 232 // Transitional state when we are not decoding any more stream, but are | |
| 233 // performing flush, reset, resolution change or are destroying ourselves. | |
| 234 kIdle, | |
| 235 // Error state, set when sending NotifyError to client. | |
| 236 kError, | |
| 237 }; | |
| 238 | |
| 239 // Buffer id for flush buffer, queued by FlushTask(). | |
| 240 const int kFlushBufferId = -2; | |
| 241 | |
| 242 // Handler for Decode() on decoder_thread_. | |
| 243 void DecodeTask(const media::BitstreamBuffer& bitstream_buffer); | |
| 244 | |
| 245 // Schedule a new DecodeBufferTask if we are decoding. | |
| 246 void ScheduleDecodeBufferTaskIfNeeded(); | |
| 247 | |
| 248 // Main decoder loop. Keep decoding the current buffer in decoder_, asking | |
| 249 // for more stream via TrySetNewBistreamBuffer() if decoder_ requests so, | |
| 250 // and handle other returns from it appropriately. | |
| 251 void DecodeBufferTask(); | |
| 252 | |
| 253 // Check decoder_input_queue_ for any available buffers to decode and | |
| 254 // set the decoder_current_bitstream_buffer_ to the next buffer if one is | |
| 255 // available, taking it off the queue. Also set the current stream pointer | |
| 256 // in decoder_, and return true. | |
| 257 // Return false if no buffers are pending on decoder_input_queue_. | |
| 258 bool TrySetNewBistreamBuffer(); | |
| 259 | |
| 260 // Auto-destruction reference for EGLSync (for message-passing). | |
| 261 struct EGLSyncKHRRef; | |
| 262 void ReusePictureBufferTask(int32_t picture_buffer_id, | |
| 263 std::unique_ptr<EGLSyncKHRRef> egl_sync_ref); | |
| 264 | |
| 265 // Called to actually send |dec_surface| to the client, after it is decoded | |
| 266 // preserving the order in which it was scheduled via SurfaceReady(). | |
| 267 void OutputSurface(const scoped_refptr<V4L2DecodeSurface>& dec_surface); | |
| 268 | |
| 269 // Goes over the |decoder_display_queue_| and sends all buffers from the | |
| 270 // front of the queue that are already decoded to the client, in order. | |
| 271 void TryOutputSurfaces(); | |
| 272 | |
| 273 // Creates a new decode surface or returns nullptr if one is not available. | |
| 274 scoped_refptr<V4L2DecodeSurface> CreateSurface(); | |
| 275 | |
| 276 // Send decoded pictures to PictureReady. | |
| 277 void SendPictureReady(); | |
| 278 | |
| 279 // Callback that indicates a picture has been cleared. | |
| 280 void PictureCleared(); | |
| 281 | |
| 282 size_t input_planes_count_; | |
| 283 size_t output_planes_count_; | |
| 284 | |
| 285 // GPU Child thread task runner. | |
| 286 const scoped_refptr<base::SingleThreadTaskRunner> child_task_runner_; | |
| 287 | |
| 288 // Task runner Decode() and PictureReady() run on. | |
| 289 scoped_refptr<base::SingleThreadTaskRunner> decode_task_runner_; | |
| 290 | |
| 291 // WeakPtr<> pointing to |this| for use in posting tasks from the decoder or | |
| 292 // device worker threads back to the child thread. | |
| 293 base::WeakPtr<V4L2SliceVideoDecodeAccelerator> weak_this_; | |
| 294 | |
| 295 // To expose client callbacks from VideoDecodeAccelerator. | |
| 296 // NOTE: all calls to these objects *MUST* be executed on | |
| 297 // child_task_runner_. | |
| 298 std::unique_ptr<base::WeakPtrFactory<VideoDecodeAccelerator::Client>> | |
| 299 client_ptr_factory_; | |
| 300 base::WeakPtr<VideoDecodeAccelerator::Client> client_; | |
| 301 // Callbacks to |decode_client_| must be executed on |decode_task_runner_|. | |
| 302 base::WeakPtr<Client> decode_client_; | |
| 303 | |
| 304 // V4L2 device in use. | |
| 305 scoped_refptr<V4L2Device> device_; | |
| 306 | |
| 307 // Thread to communicate with the device on. | |
| 308 base::Thread decoder_thread_; | |
| 309 scoped_refptr<base::SingleThreadTaskRunner> decoder_thread_task_runner_; | |
| 310 | |
| 311 // Thread used to poll the device for events. | |
| 312 base::Thread device_poll_thread_; | |
| 313 | |
| 314 // Input queue state. | |
| 315 bool input_streamon_; | |
| 316 // Number of input buffers enqueued to the device. | |
| 317 int input_buffer_queued_count_; | |
| 318 // Input buffers ready to use; LIFO since we don't care about ordering. | |
| 319 std::list<int> free_input_buffers_; | |
| 320 // Mapping of int index to an input buffer record. | |
| 321 std::vector<InputRecord> input_buffer_map_; | |
| 322 | |
| 323 // Output queue state. | |
| 324 bool output_streamon_; | |
| 325 // Number of output buffers enqueued to the device. | |
| 326 int output_buffer_queued_count_; | |
| 327 // Output buffers ready to use. | |
| 328 std::list<int> free_output_buffers_; | |
| 329 // Mapping of int index to an output buffer record. | |
| 330 std::vector<OutputRecord> output_buffer_map_; | |
| 331 | |
| 332 media::VideoCodecProfile video_profile_; | |
| 333 uint32_t output_format_fourcc_; | |
| 334 gfx::Size visible_size_; | |
| 335 gfx::Size coded_size_; | |
| 336 | |
| 337 struct BitstreamBufferRef; | |
| 338 // Input queue of stream buffers coming from the client. | |
| 339 std::queue<linked_ptr<BitstreamBufferRef>> decoder_input_queue_; | |
| 340 // BitstreamBuffer currently being processed. | |
| 341 std::unique_ptr<BitstreamBufferRef> decoder_current_bitstream_buffer_; | |
| 342 | |
| 343 // Queue storing decode surfaces ready to be output as soon as they are | |
| 344 // decoded. The surfaces must be output in order they are queued. | |
| 345 std::queue<scoped_refptr<V4L2DecodeSurface>> decoder_display_queue_; | |
| 346 | |
| 347 // Decoder state. | |
| 348 State state_; | |
| 349 | |
| 350 // If any of these are true, we are waiting for the device to finish decoding | |
| 351 // all previously-queued frames, so we can finish the flush/reset/surface | |
| 352 // change flows. These can stack. | |
| 353 bool decoder_flushing_; | |
| 354 bool decoder_resetting_; | |
| 355 bool surface_set_change_pending_; | |
| 356 | |
| 357 // Hardware accelerators. | |
| 358 // TODO(posciak): Try to have a superclass here if possible. | |
| 359 std::unique_ptr<V4L2H264Accelerator> h264_accelerator_; | |
| 360 std::unique_ptr<V4L2VP8Accelerator> vp8_accelerator_; | |
| 361 | |
| 362 // Codec-specific software decoder in use. | |
| 363 std::unique_ptr<AcceleratedVideoDecoder> decoder_; | |
| 364 | |
| 365 // Surfaces queued to device to keep references to them while decoded. | |
| 366 using V4L2DecodeSurfaceByOutputId = | |
| 367 std::map<int, scoped_refptr<V4L2DecodeSurface>>; | |
| 368 V4L2DecodeSurfaceByOutputId surfaces_at_device_; | |
| 369 | |
| 370 // Surfaces sent to client to keep references to them while displayed. | |
| 371 using V4L2DecodeSurfaceByPictureBufferId = | |
| 372 std::map<int32_t, scoped_refptr<V4L2DecodeSurface>>; | |
| 373 V4L2DecodeSurfaceByPictureBufferId surfaces_at_display_; | |
| 374 | |
| 375 // Record for decoded pictures that can be sent to PictureReady. | |
| 376 struct PictureRecord; | |
| 377 // Pictures that are ready but not sent to PictureReady yet. | |
| 378 std::queue<PictureRecord> pending_picture_ready_; | |
| 379 | |
| 380 // The number of pictures that are sent to PictureReady and will be cleared. | |
| 381 int picture_clearing_count_; | |
| 382 | |
| 383 // Used by the decoder thread to wait for AssignPictureBuffers to arrive | |
| 384 // to avoid races with potential Reset requests. | |
| 385 base::WaitableEvent pictures_assigned_; | |
| 386 | |
| 387 // EGL state | |
| 388 EGLDisplay egl_display_; | |
| 389 | |
| 390 // Callback to get current GLContext. | |
| 391 GetGLContextCallback get_gl_context_cb_; | |
| 392 // Callback to set the correct gl context. | |
| 393 MakeGLContextCurrentCallback make_context_current_cb_; | |
| 394 | |
| 395 // The WeakPtrFactory for |weak_this_|. | |
| 396 base::WeakPtrFactory<V4L2SliceVideoDecodeAccelerator> weak_this_factory_; | |
| 397 | |
| 398 DISALLOW_COPY_AND_ASSIGN(V4L2SliceVideoDecodeAccelerator); | |
| 399 }; | |
| 400 | |
| 401 class V4L2H264Picture; | |
| 402 class V4L2VP8Picture; | |
| 403 | |
| 404 } // namespace content | |
| 405 | |
| 406 #endif // CONTENT_COMMON_GPU_MEDIA_V4L2_SLICE_VIDEO_DECODE_ACCELERATOR_H_ | |
| OLD | NEW |