| 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 // This file contains an implementation of VideoDecodeAccelerator | 5 // This file contains an implementation of VideoDecodeAccelerator |
| 6 // that utilizes hardware video decoders, which expose Video4Linux 2 API | 6 // that utilizes hardware video decoders, which expose Video4Linux 2 API |
| 7 // (http://linuxtv.org/downloads/v4l-dvb-apis/). | 7 // (http://linuxtv.org/downloads/v4l-dvb-apis/). |
| 8 | 8 |
| 9 #ifndef MEDIA_GPU_V4L2_VIDEO_DECODE_ACCELERATOR_H_ | 9 #ifndef MEDIA_GPU_V4L2_VIDEO_DECODE_ACCELERATOR_H_ |
| 10 #define MEDIA_GPU_V4L2_VIDEO_DECODE_ACCELERATOR_H_ | 10 #define MEDIA_GPU_V4L2_VIDEO_DECODE_ACCELERATOR_H_ |
| 11 | 11 |
| 12 #include <stddef.h> | 12 #include <stddef.h> |
| 13 #include <stdint.h> | 13 #include <stdint.h> |
| 14 | 14 |
| 15 #include <list> |
| 15 #include <memory> | 16 #include <memory> |
| 16 #include <queue> | 17 #include <queue> |
| 17 #include <vector> | 18 #include <vector> |
| 18 | 19 |
| 19 #include "base/callback_forward.h" | 20 #include "base/callback_forward.h" |
| 20 #include "base/macros.h" | 21 #include "base/macros.h" |
| 21 #include "base/memory/linked_ptr.h" | 22 #include "base/memory/linked_ptr.h" |
| 22 #include "base/memory/ref_counted.h" | 23 #include "base/memory/ref_counted.h" |
| 23 #include "base/synchronization/waitable_event.h" | 24 #include "base/synchronization/waitable_event.h" |
| 24 #include "base/threading/thread.h" | 25 #include "base/threading/thread.h" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 const GetGLContextCallback& get_gl_context_cb, | 93 const GetGLContextCallback& get_gl_context_cb, |
| 93 const MakeGLContextCurrentCallback& make_context_current_cb, | 94 const MakeGLContextCurrentCallback& make_context_current_cb, |
| 94 const scoped_refptr<V4L2Device>& device); | 95 const scoped_refptr<V4L2Device>& device); |
| 95 ~V4L2VideoDecodeAccelerator() override; | 96 ~V4L2VideoDecodeAccelerator() override; |
| 96 | 97 |
| 97 // VideoDecodeAccelerator implementation. | 98 // VideoDecodeAccelerator implementation. |
| 98 // Note: Initialize() and Destroy() are synchronous. | 99 // Note: Initialize() and Destroy() are synchronous. |
| 99 bool Initialize(const Config& config, Client* client) override; | 100 bool Initialize(const Config& config, Client* client) override; |
| 100 void Decode(const BitstreamBuffer& bitstream_buffer) override; | 101 void Decode(const BitstreamBuffer& bitstream_buffer) override; |
| 101 void AssignPictureBuffers(const std::vector<PictureBuffer>& buffers) override; | 102 void AssignPictureBuffers(const std::vector<PictureBuffer>& buffers) override; |
| 103 void ImportBufferForPicture( |
| 104 int32_t picture_buffer_id, |
| 105 const gfx::GpuMemoryBufferHandle& gpu_memory_buffer_handles) override; |
| 102 void ReusePictureBuffer(int32_t picture_buffer_id) override; | 106 void ReusePictureBuffer(int32_t picture_buffer_id) override; |
| 103 void Flush() override; | 107 void Flush() override; |
| 104 void Reset() override; | 108 void Reset() override; |
| 105 void Destroy() override; | 109 void Destroy() override; |
| 106 bool TryToSetupDecodeOnSeparateThread( | 110 bool TryToSetupDecodeOnSeparateThread( |
| 107 const base::WeakPtr<Client>& decode_client, | 111 const base::WeakPtr<Client>& decode_client, |
| 108 const scoped_refptr<base::SingleThreadTaskRunner>& decode_task_runner) | 112 const scoped_refptr<base::SingleThreadTaskRunner>& decode_task_runner) |
| 109 override; | 113 override; |
| 110 | 114 |
| 111 static VideoDecodeAccelerator::SupportedProfiles GetSupportedProfiles(); | 115 static VideoDecodeAccelerator::SupportedProfiles GetSupportedProfiles(); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 | 180 |
| 177 // Record for output buffers. | 181 // Record for output buffers. |
| 178 struct OutputRecord { | 182 struct OutputRecord { |
| 179 OutputRecord(); | 183 OutputRecord(); |
| 180 OutputRecord(OutputRecord&&) = default; | 184 OutputRecord(OutputRecord&&) = default; |
| 181 ~OutputRecord(); | 185 ~OutputRecord(); |
| 182 OutputRecordState state; | 186 OutputRecordState state; |
| 183 EGLImageKHR egl_image; // EGLImageKHR for the output buffer. | 187 EGLImageKHR egl_image; // EGLImageKHR for the output buffer. |
| 184 EGLSyncKHR egl_sync; // sync the compositor's use of the EGLImage. | 188 EGLSyncKHR egl_sync; // sync the compositor's use of the EGLImage. |
| 185 int32_t picture_id; // picture buffer id as returned to PictureReady(). | 189 int32_t picture_id; // picture buffer id as returned to PictureReady(). |
| 190 GLuint texture_id; |
| 186 bool cleared; // Whether the texture is cleared and safe to render | 191 bool cleared; // Whether the texture is cleared and safe to render |
| 187 // from. See TextureManager for details. | 192 // from. See TextureManager for details. |
| 188 // Exported fds for image processor to import. | 193 // Input fds of the processor. Exported from the decoder. |
| 189 std::vector<base::ScopedFD> fds; | 194 std::vector<base::ScopedFD> processor_input_fds; |
| 195 // Output fds of the processor. Used only when OutputMode is IMPORT. |
| 196 std::vector<base::ScopedFD> processor_output_fds; |
| 190 }; | 197 }; |
| 191 | 198 |
| 192 // | 199 // |
| 193 // Decoding tasks, to be run on decode_thread_. | 200 // Decoding tasks, to be run on decode_thread_. |
| 194 // | 201 // |
| 195 | 202 |
| 196 // Enqueue a BitstreamBuffer to decode. This will enqueue a buffer to the | 203 // Enqueue a BitstreamBuffer to decode. This will enqueue a buffer to the |
| 197 // decoder_input_queue_, then queue a DecodeBufferTask() to actually decode | 204 // decoder_input_queue_, then queue a DecodeBufferTask() to actually decode |
| 198 // the buffer. | 205 // the buffer. |
| 199 void DecodeTask(const BitstreamBuffer& bitstream_buffer); | 206 void DecodeTask(const BitstreamBuffer& bitstream_buffer); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 215 // non-error conditions; for example when pipeline is full and should be | 222 // non-error conditions; for example when pipeline is full and should be |
| 216 // retried later. | 223 // retried later. |
| 217 bool AppendToInputFrame(const void* data, size_t size); | 224 bool AppendToInputFrame(const void* data, size_t size); |
| 218 // Flush data for one decoded frame. | 225 // Flush data for one decoded frame. |
| 219 bool FlushInputFrame(); | 226 bool FlushInputFrame(); |
| 220 | 227 |
| 221 // Allocate V4L2 buffers and assign them to |buffers| provided by the client | 228 // Allocate V4L2 buffers and assign them to |buffers| provided by the client |
| 222 // via AssignPictureBuffers() on decoder thread. | 229 // via AssignPictureBuffers() on decoder thread. |
| 223 void AssignPictureBuffersTask(const std::vector<PictureBuffer>& buffers); | 230 void AssignPictureBuffersTask(const std::vector<PictureBuffer>& buffers); |
| 224 | 231 |
| 225 // Create EGLImages bound to textures in |buffers| for given | 232 // Use buffer backed by dmabuf file descriptors in |dmabuf_fds| for the |
| 226 // |output_format_fourcc| and |output_planes_count|. | 233 // OutputRecord associated with |picture_buffer_id|, taking ownership of the |
| 227 void CreateEGLImages(const std::vector<media::PictureBuffer>& buffers, | 234 // file descriptors. |stride| is the number of bytes from one row of pixels |
| 228 uint32_t output_format_fourcc, | 235 // to the next row. |
| 229 size_t output_planes_count); | 236 void ImportBufferForPictureTask(int32_t picture_buffer_id, |
| 237 std::vector<base::ScopedFD> dmabuf_fds, |
| 238 int32_t stride); |
| 230 | 239 |
| 231 // Assign |egl_images| to previously-allocated V4L2 buffers in | 240 // Create an EGLImage for the buffer associated with V4L2 |buffer_index| and |
| 232 // output_buffer_map_ and picture ids from |buffers| and finish the resolution | 241 // for |picture_buffer_id|, backed by dmabuf file descriptors in |
| 233 // change sequence. | 242 // |passed_dmabuf_fds|, taking ownership of them. |
| 234 void AssignEGLImages(const std::vector<media::PictureBuffer>& buffers, | 243 // The buffer should be bound to |texture_id| and is of |size| and format |
| 235 const std::vector<EGLImageKHR>& egl_images); | 244 // described by |fourcc|. |
| 245 void CreateEGLImageFor(size_t buffer_index, |
| 246 int32_t picture_buffer_id, |
| 247 std::vector<base::ScopedFD> dmabuf_fds, |
| 248 GLuint texture_id, |
| 249 const gfx::Size& size, |
| 250 uint32_t fourcc); |
| 251 |
| 252 // Take the EGLImage |egl_image|, created for |picture_buffer_id|, and use it |
| 253 // for OutputRecord at |buffer_index|. The buffer is backed by |
| 254 // |passed_dmabuf_fds|, and the OutputRecord takes ownership of them. |
| 255 void AssignEGLImage(size_t buffer_index, |
| 256 int32_t picture_buffer_id, |
| 257 EGLImageKHR egl_image, |
| 258 std::vector<base::ScopedFD> dmabuf_fds); |
| 236 | 259 |
| 237 // Service I/O on the V4L2 devices. This task should only be scheduled from | 260 // Service I/O on the V4L2 devices. This task should only be scheduled from |
| 238 // DevicePollTask(). If |event_pending| is true, one or more events | 261 // DevicePollTask(). If |event_pending| is true, one or more events |
| 239 // on file descriptor are pending. | 262 // on file descriptor are pending. |
| 240 void ServiceDeviceTask(bool event_pending); | 263 void ServiceDeviceTask(bool event_pending); |
| 241 // Handle the various device queues. | 264 // Handle the various device queues. |
| 242 void Enqueue(); | 265 void Enqueue(); |
| 243 void Dequeue(); | 266 void Dequeue(); |
| 244 | 267 |
| 245 // Return true if there is a resolution change event pending. | 268 // Return true if there is a resolution change event pending. |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 | 363 |
| 341 // Set input and output formats before starting decode. | 364 // Set input and output formats before starting decode. |
| 342 bool SetupFormats(); | 365 bool SetupFormats(); |
| 343 // Return a usable input format of image processor. Return 0 if not found. | 366 // Return a usable input format of image processor. Return 0 if not found. |
| 344 uint32_t FindImageProcessorInputFormat(); | 367 uint32_t FindImageProcessorInputFormat(); |
| 345 // Return a usable output format of image processor. Return 0 if not found. | 368 // Return a usable output format of image processor. Return 0 if not found. |
| 346 uint32_t FindImageProcessorOutputFormat(); | 369 uint32_t FindImageProcessorOutputFormat(); |
| 347 // Reset image processor and drop all processing frames. | 370 // Reset image processor and drop all processing frames. |
| 348 bool ResetImageProcessor(); | 371 bool ResetImageProcessor(); |
| 349 | 372 |
| 373 bool CreateImageProcessor(); |
| 374 // Send a frame to the image processor to process. The index of decoder |
| 375 // output buffer is |output_buffer_index| and its id is |bitstream_buffer_id|. |
| 376 bool ProcessFrame(int32_t bitstream_buffer_id, int output_buffer_index); |
| 377 |
| 350 // | 378 // |
| 351 // Methods run on child thread. | 379 // Methods run on child thread. |
| 352 // | 380 // |
| 353 | 381 |
| 354 // Send decoded pictures to PictureReady. | 382 // Send decoded pictures to PictureReady. |
| 355 void SendPictureReady(); | 383 void SendPictureReady(); |
| 356 | 384 |
| 357 // Callback that indicates a picture has been cleared. | 385 // Callback that indicates a picture has been cleared. |
| 358 void PictureCleared(); | 386 void PictureCleared(); |
| 359 | 387 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 // Before decoder_thread_ has started, the decoder state is managed by | 419 // Before decoder_thread_ has started, the decoder state is managed by |
| 392 // the child (main) thread. After decoder_thread_ has started, the decoder | 420 // the child (main) thread. After decoder_thread_ has started, the decoder |
| 393 // thread should be the only one managing these. | 421 // thread should be the only one managing these. |
| 394 // | 422 // |
| 395 | 423 |
| 396 // This thread services tasks posted from the VDA API entry points by the | 424 // This thread services tasks posted from the VDA API entry points by the |
| 397 // child thread and device service callbacks posted from the device thread. | 425 // child thread and device service callbacks posted from the device thread. |
| 398 base::Thread decoder_thread_; | 426 base::Thread decoder_thread_; |
| 399 // Decoder state machine state. | 427 // Decoder state machine state. |
| 400 State decoder_state_; | 428 State decoder_state_; |
| 429 |
| 430 Config::OutputMode output_mode_; |
| 431 |
| 401 // BitstreamBuffer we're presently reading. | 432 // BitstreamBuffer we're presently reading. |
| 402 std::unique_ptr<BitstreamBufferRef> decoder_current_bitstream_buffer_; | 433 std::unique_ptr<BitstreamBufferRef> decoder_current_bitstream_buffer_; |
| 403 // The V4L2Device this class is operating upon. | 434 // The V4L2Device this class is operating upon. |
| 404 scoped_refptr<V4L2Device> device_; | 435 scoped_refptr<V4L2Device> device_; |
| 405 // FlushTask() and ResetTask() should not affect buffers that have been | 436 // FlushTask() and ResetTask() should not affect buffers that have been |
| 406 // queued afterwards. For flushing or resetting the pipeline then, we will | 437 // queued afterwards. For flushing or resetting the pipeline then, we will |
| 407 // delay these buffers until after the flush or reset completes. | 438 // delay these buffers until after the flush or reset completes. |
| 408 int decoder_delay_bitstream_buffer_id_; | 439 int decoder_delay_bitstream_buffer_id_; |
| 409 // Input buffer we're presently filling. | 440 // Input buffer we're presently filling. |
| 410 int decoder_current_input_buffer_; | 441 int decoder_current_input_buffer_; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 std::vector<int> free_input_buffers_; | 478 std::vector<int> free_input_buffers_; |
| 448 // Mapping of int index to input buffer record. | 479 // Mapping of int index to input buffer record. |
| 449 std::vector<InputRecord> input_buffer_map_; | 480 std::vector<InputRecord> input_buffer_map_; |
| 450 | 481 |
| 451 // Output buffer state. | 482 // Output buffer state. |
| 452 bool output_streamon_; | 483 bool output_streamon_; |
| 453 // Output buffers enqueued to device. | 484 // Output buffers enqueued to device. |
| 454 int output_buffer_queued_count_; | 485 int output_buffer_queued_count_; |
| 455 // Output buffers ready to use, as a FIFO since we want oldest-first to hide | 486 // Output buffers ready to use, as a FIFO since we want oldest-first to hide |
| 456 // synchronization latency with GL. | 487 // synchronization latency with GL. |
| 457 std::queue<int> free_output_buffers_; | 488 std::list<int> free_output_buffers_; |
| 458 // Mapping of int index to output buffer record. | 489 // Mapping of int index to output buffer record. |
| 459 std::vector<OutputRecord> output_buffer_map_; | 490 std::vector<OutputRecord> output_buffer_map_; |
| 460 // Required size of DPB for decoding. | 491 // Required size of DPB for decoding. |
| 461 int output_dpb_size_; | 492 int output_dpb_size_; |
| 462 | 493 |
| 463 // Number of planes (i.e. separate memory buffers) for output. | 494 // Number of planes (i.e. separate memory buffers) for output. |
| 464 size_t output_planes_count_; | 495 size_t output_planes_count_; |
| 465 | 496 |
| 466 // Pictures that are ready but not sent to PictureReady yet. | 497 // Pictures that are ready but not sent to PictureReady yet. |
| 467 std::queue<PictureRecord> pending_picture_ready_; | 498 std::queue<PictureRecord> pending_picture_ready_; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 | 554 |
| 524 // The WeakPtrFactory for |weak_this_|. | 555 // The WeakPtrFactory for |weak_this_|. |
| 525 base::WeakPtrFactory<V4L2VideoDecodeAccelerator> weak_this_factory_; | 556 base::WeakPtrFactory<V4L2VideoDecodeAccelerator> weak_this_factory_; |
| 526 | 557 |
| 527 DISALLOW_COPY_AND_ASSIGN(V4L2VideoDecodeAccelerator); | 558 DISALLOW_COPY_AND_ASSIGN(V4L2VideoDecodeAccelerator); |
| 528 }; | 559 }; |
| 529 | 560 |
| 530 } // namespace media | 561 } // namespace media |
| 531 | 562 |
| 532 #endif // MEDIA_GPU_V4L2_VIDEO_DECODE_ACCELERATOR_H_ | 563 #endif // MEDIA_GPU_V4L2_VIDEO_DECODE_ACCELERATOR_H_ |
| OLD | NEW |