| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 // | |
| 5 // This file contains an implementation of VideoDecodeAccelerator | |
| 6 // that utilizes hardware video decoders, which expose Video4Linux 2 API | |
| 7 // (http://linuxtv.org/downloads/v4l-dvb-apis/). | |
| 8 | |
| 9 #ifndef CONTENT_COMMON_GPU_MEDIA_V4L2_VIDEO_DECODE_ACCELERATOR_H_ | |
| 10 #define CONTENT_COMMON_GPU_MEDIA_V4L2_VIDEO_DECODE_ACCELERATOR_H_ | |
| 11 | |
| 12 #include <stddef.h> | |
| 13 #include <stdint.h> | |
| 14 | |
| 15 #include <memory> | |
| 16 #include <queue> | |
| 17 #include <vector> | |
| 18 | |
| 19 #include "base/callback_forward.h" | |
| 20 #include "base/macros.h" | |
| 21 #include "base/memory/linked_ptr.h" | |
| 22 #include "base/memory/ref_counted.h" | |
| 23 #include "base/synchronization/waitable_event.h" | |
| 24 #include "base/threading/thread.h" | |
| 25 #include "content/common/content_export.h" | |
| 26 #include "content/common/gpu/media/gpu_video_decode_accelerator_helpers.h" | |
| 27 #include "content/common/gpu/media/v4l2_device.h" | |
| 28 #include "media/base/limits.h" | |
| 29 #include "media/base/video_decoder_config.h" | |
| 30 #include "media/video/picture.h" | |
| 31 #include "media/video/video_decode_accelerator.h" | |
| 32 #include "ui/gfx/geometry/size.h" | |
| 33 #include "ui/gl/gl_bindings.h" | |
| 34 | |
| 35 namespace media { | |
| 36 class H264Parser; | |
| 37 } // namespace media | |
| 38 | |
| 39 namespace content { | |
| 40 // This class handles video accelerators directly through a V4L2 device exported | |
| 41 // by the hardware blocks. | |
| 42 // | |
| 43 // The threading model of this class is driven by the fact that it needs to | |
| 44 // interface two fundamentally different event queues -- the one Chromium | |
| 45 // provides through MessageLoop, and the one driven by the V4L2 devices which | |
| 46 // is waited on with epoll(). There are three threads involved in this class: | |
| 47 // | |
| 48 // * The child thread, which is the main GPU process thread which calls the | |
| 49 // media::VideoDecodeAccelerator entry points. Calls from this thread | |
| 50 // generally do not block (with the exception of Initialize() and Destroy()). | |
| 51 // They post tasks to the decoder_thread_, which actually services the task | |
| 52 // and calls back when complete through the | |
| 53 // media::VideoDecodeAccelerator::Client interface. | |
| 54 // * The decoder_thread_, owned by this class. It services API tasks, through | |
| 55 // the *Task() routines, as well as V4L2 device events, through | |
| 56 // ServiceDeviceTask(). Almost all state modification is done on this thread | |
| 57 // (this doesn't include buffer (re)allocation sequence, see below). | |
| 58 // * The device_poll_thread_, owned by this class. All it does is epoll() on | |
| 59 // the V4L2 in DevicePollTask() and schedule a ServiceDeviceTask() on the | |
| 60 // decoder_thread_ when something interesting happens. | |
| 61 // TODO(sheu): replace this thread with an TYPE_IO decoder_thread_. | |
| 62 // | |
| 63 // Note that this class has (almost) no locks, apart from the pictures_assigned_ | |
| 64 // WaitableEvent. Everything (apart from buffer (re)allocation) is serviced on | |
| 65 // the decoder_thread_, so there are no synchronization issues. | |
| 66 // ... well, there are, but it's a matter of getting messages posted in the | |
| 67 // right order, not fiddling with locks. | |
| 68 // Buffer creation is a two-step process that is serviced partially on the | |
| 69 // Child thread, because we need to wait for the client to provide textures | |
| 70 // for the buffers we allocate. We cannot keep the decoder thread running while | |
| 71 // the client allocates Pictures for us, because we need to REQBUFS first to get | |
| 72 // the required number of output buffers from the device and that cannot be done | |
| 73 // unless we free the previous set of buffers, leaving the decoding in a | |
| 74 // inoperable state for the duration of the wait for Pictures. So to prevent | |
| 75 // subtle races (esp. if we get Reset() in the meantime), we block the decoder | |
| 76 // thread while we wait for AssignPictureBuffers from the client. | |
| 77 class CONTENT_EXPORT V4L2VideoDecodeAccelerator | |
| 78 : public media::VideoDecodeAccelerator { | |
| 79 public: | |
| 80 V4L2VideoDecodeAccelerator( | |
| 81 EGLDisplay egl_display, | |
| 82 const GetGLContextCallback& get_gl_context_cb, | |
| 83 const MakeGLContextCurrentCallback& make_context_current_cb, | |
| 84 const scoped_refptr<V4L2Device>& device); | |
| 85 ~V4L2VideoDecodeAccelerator() override; | |
| 86 | |
| 87 // media::VideoDecodeAccelerator implementation. | |
| 88 // Note: Initialize() and Destroy() are synchronous. | |
| 89 bool Initialize(const Config& config, Client* client) override; | |
| 90 void Decode(const media::BitstreamBuffer& bitstream_buffer) override; | |
| 91 void AssignPictureBuffers( | |
| 92 const std::vector<media::PictureBuffer>& buffers) override; | |
| 93 void ReusePictureBuffer(int32_t picture_buffer_id) override; | |
| 94 void Flush() override; | |
| 95 void Reset() override; | |
| 96 void Destroy() override; | |
| 97 bool TryToSetupDecodeOnSeparateThread( | |
| 98 const base::WeakPtr<Client>& decode_client, | |
| 99 const scoped_refptr<base::SingleThreadTaskRunner>& decode_task_runner) | |
| 100 override; | |
| 101 | |
| 102 static media::VideoDecodeAccelerator::SupportedProfiles | |
| 103 GetSupportedProfiles(); | |
| 104 | |
| 105 private: | |
| 106 // These are rather subjectively tuned. | |
| 107 enum { | |
| 108 kInputBufferCount = 8, | |
| 109 // TODO(posciak): determine input buffer size based on level limits. | |
| 110 // See http://crbug.com/255116. | |
| 111 // Input bitstream buffer size for up to 1080p streams. | |
| 112 kInputBufferMaxSizeFor1080p = 1024 * 1024, | |
| 113 // Input bitstream buffer size for up to 4k streams. | |
| 114 kInputBufferMaxSizeFor4k = 4 * kInputBufferMaxSizeFor1080p, | |
| 115 // Number of output buffers to use for each VDA stage above what's required | |
| 116 // by the decoder (e.g. DPB size, in H264). We need | |
| 117 // media::limits::kMaxVideoFrames to fill up the GpuVideoDecode pipeline, | |
| 118 // and +1 for a frame in transit. | |
| 119 kDpbOutputBufferExtraCount = media::limits::kMaxVideoFrames + 1, | |
| 120 }; | |
| 121 | |
| 122 // Internal state of the decoder. | |
| 123 enum State { | |
| 124 kUninitialized, // Initialize() not yet called. | |
| 125 kInitialized, // Initialize() returned true; ready to start decoding. | |
| 126 kDecoding, // DecodeBufferInitial() successful; decoding frames. | |
| 127 kResetting, // Presently resetting. | |
| 128 kAfterReset, // After Reset(), ready to start decoding again. | |
| 129 kChangingResolution, // Performing resolution change, all remaining | |
| 130 // pre-change frames decoded and processed. | |
| 131 kError, // Error in kDecoding state. | |
| 132 }; | |
| 133 | |
| 134 enum BufferId { | |
| 135 kFlushBufferId = -2 // Buffer id for flush buffer, queued by FlushTask(). | |
| 136 }; | |
| 137 | |
| 138 // Auto-destruction reference for BitstreamBuffer, for message-passing from | |
| 139 // Decode() to DecodeTask(). | |
| 140 struct BitstreamBufferRef; | |
| 141 | |
| 142 // Auto-destruction reference for EGLSync (for message-passing). | |
| 143 struct EGLSyncKHRRef; | |
| 144 | |
| 145 // Record for decoded pictures that can be sent to PictureReady. | |
| 146 struct PictureRecord; | |
| 147 | |
| 148 // Record for input buffers. | |
| 149 struct InputRecord { | |
| 150 InputRecord(); | |
| 151 ~InputRecord(); | |
| 152 bool at_device; // held by device. | |
| 153 void* address; // mmap() address. | |
| 154 size_t length; // mmap() length. | |
| 155 off_t bytes_used; // bytes filled in the mmap() segment. | |
| 156 int32_t input_id; // triggering input_id as given to Decode(). | |
| 157 }; | |
| 158 | |
| 159 // Record for output buffers. | |
| 160 struct OutputRecord { | |
| 161 OutputRecord(); | |
| 162 ~OutputRecord(); | |
| 163 bool at_device; // held by device. | |
| 164 bool at_client; // held by client. | |
| 165 EGLImageKHR egl_image; // EGLImageKHR for the output buffer. | |
| 166 EGLSyncKHR egl_sync; // sync the compositor's use of the EGLImage. | |
| 167 int32_t picture_id; // picture buffer id as returned to PictureReady(). | |
| 168 bool cleared; // Whether the texture is cleared and safe to render | |
| 169 // from. See TextureManager for details. | |
| 170 }; | |
| 171 | |
| 172 // | |
| 173 // Decoding tasks, to be run on decode_thread_. | |
| 174 // | |
| 175 | |
| 176 // Enqueue a BitstreamBuffer to decode. This will enqueue a buffer to the | |
| 177 // decoder_input_queue_, then queue a DecodeBufferTask() to actually decode | |
| 178 // the buffer. | |
| 179 void DecodeTask(const media::BitstreamBuffer& bitstream_buffer); | |
| 180 | |
| 181 // Decode from the buffers queued in decoder_input_queue_. Calls | |
| 182 // DecodeBufferInitial() or DecodeBufferContinue() as appropriate. | |
| 183 void DecodeBufferTask(); | |
| 184 // Advance to the next fragment that begins a frame. | |
| 185 bool AdvanceFrameFragment(const uint8_t* data, size_t size, size_t* endpos); | |
| 186 // Schedule another DecodeBufferTask() if we're behind. | |
| 187 void ScheduleDecodeBufferTaskIfNeeded(); | |
| 188 | |
| 189 // Return true if we should continue to schedule DecodeBufferTask()s after | |
| 190 // completion. Store the amount of input actually consumed in |endpos|. | |
| 191 bool DecodeBufferInitial(const void* data, size_t size, size_t* endpos); | |
| 192 bool DecodeBufferContinue(const void* data, size_t size); | |
| 193 | |
| 194 // Accumulate data for the next frame to decode. May return false in | |
| 195 // non-error conditions; for example when pipeline is full and should be | |
| 196 // retried later. | |
| 197 bool AppendToInputFrame(const void* data, size_t size); | |
| 198 // Flush data for one decoded frame. | |
| 199 bool FlushInputFrame(); | |
| 200 | |
| 201 // Service I/O on the V4L2 devices. This task should only be scheduled from | |
| 202 // DevicePollTask(). If |event_pending| is true, one or more events | |
| 203 // on file descriptor are pending. | |
| 204 void ServiceDeviceTask(bool event_pending); | |
| 205 // Handle the various device queues. | |
| 206 void Enqueue(); | |
| 207 void Dequeue(); | |
| 208 | |
| 209 // Return true if there is a resolution change event pending. | |
| 210 bool DequeueResolutionChangeEvent(); | |
| 211 | |
| 212 // Enqueue a buffer on the corresponding queue. | |
| 213 bool EnqueueInputRecord(); | |
| 214 bool EnqueueOutputRecord(); | |
| 215 | |
| 216 // Process a ReusePictureBuffer() API call. The API call create an EGLSync | |
| 217 // object on the main (GPU process) thread; we will record this object so we | |
| 218 // can wait on it before reusing the buffer. | |
| 219 void ReusePictureBufferTask(int32_t picture_buffer_id, | |
| 220 std::unique_ptr<EGLSyncKHRRef> egl_sync_ref); | |
| 221 | |
| 222 // Flush() task. Child thread should not submit any more buffers until it | |
| 223 // receives the NotifyFlushDone callback. This task will schedule an empty | |
| 224 // BitstreamBufferRef (with input_id == kFlushBufferId) to perform the flush. | |
| 225 void FlushTask(); | |
| 226 // Notify the client of a flush completion, if required. This should be | |
| 227 // called any time a relevant queue could potentially be emptied: see | |
| 228 // function definition. | |
| 229 void NotifyFlushDoneIfNeeded(); | |
| 230 | |
| 231 // Reset() task. This task will schedule a ResetDoneTask() that will send | |
| 232 // the NotifyResetDone callback, then set the decoder state to kResetting so | |
| 233 // that all intervening tasks will drain. | |
| 234 void ResetTask(); | |
| 235 // ResetDoneTask() will set the decoder state back to kAfterReset, so | |
| 236 // subsequent decoding can continue. | |
| 237 void ResetDoneTask(); | |
| 238 | |
| 239 // Device destruction task. | |
| 240 void DestroyTask(); | |
| 241 | |
| 242 // Start |device_poll_thread_|. | |
| 243 bool StartDevicePoll(); | |
| 244 | |
| 245 // Stop |device_poll_thread_|. | |
| 246 bool StopDevicePoll(); | |
| 247 | |
| 248 bool StopInputStream(); | |
| 249 bool StopOutputStream(); | |
| 250 | |
| 251 void StartResolutionChange(); | |
| 252 void FinishResolutionChange(); | |
| 253 | |
| 254 // Try to get output format and visible size, detected after parsing the | |
| 255 // beginning of the stream. Sets |again| to true if more parsing is needed. | |
| 256 // |visible_size| could be nullptr and ignored. | |
| 257 bool GetFormatInfo(struct v4l2_format* format, | |
| 258 gfx::Size* visible_size, | |
| 259 bool* again); | |
| 260 // Create output buffers for the given |format| and |visible_size|. | |
| 261 bool CreateBuffersForFormat(const struct v4l2_format& format, | |
| 262 const gfx::Size& visible_size); | |
| 263 | |
| 264 // Try to get |visible_size|. Return visible size, or, if querying it is not | |
| 265 // supported or produces invalid size, return |coded_size| instead. | |
| 266 gfx::Size GetVisibleSize(const gfx::Size& coded_size); | |
| 267 | |
| 268 // | |
| 269 // Device tasks, to be run on device_poll_thread_. | |
| 270 // | |
| 271 | |
| 272 // The device task. | |
| 273 void DevicePollTask(bool poll_device); | |
| 274 | |
| 275 // | |
| 276 // Safe from any thread. | |
| 277 // | |
| 278 | |
| 279 // Error notification (using PostTask() to child thread, if necessary). | |
| 280 void NotifyError(Error error); | |
| 281 | |
| 282 // Set the decoder_state_ to kError and notify the client (if necessary). | |
| 283 void SetErrorState(Error error); | |
| 284 | |
| 285 // | |
| 286 // Other utility functions. Called on decoder_thread_, unless | |
| 287 // decoder_thread_ is not yet started, in which case the child thread can call | |
| 288 // these (e.g. in Initialize() or Destroy()). | |
| 289 // | |
| 290 | |
| 291 // Create the buffers we need. | |
| 292 bool CreateInputBuffers(); | |
| 293 bool CreateOutputBuffers(); | |
| 294 | |
| 295 // Set input and output formats before starting decode. | |
| 296 bool SetupFormats(); | |
| 297 | |
| 298 // | |
| 299 // Methods run on child thread. | |
| 300 // | |
| 301 | |
| 302 // Destroy buffers. | |
| 303 void DestroyInputBuffers(); | |
| 304 // In contrast to DestroyInputBuffers, which is called only from destructor, | |
| 305 // we call DestroyOutputBuffers also during playback, on resolution change. | |
| 306 // Even if anything fails along the way, we still want to go on and clean | |
| 307 // up as much as possible, so return false if this happens, so that the | |
| 308 // caller can error out on resolution change. | |
| 309 bool DestroyOutputBuffers(); | |
| 310 void ResolutionChangeDestroyBuffers(); | |
| 311 | |
| 312 // Send decoded pictures to PictureReady. | |
| 313 void SendPictureReady(); | |
| 314 | |
| 315 // Callback that indicates a picture has been cleared. | |
| 316 void PictureCleared(); | |
| 317 | |
| 318 // Our original calling task runner for the child thread. | |
| 319 scoped_refptr<base::SingleThreadTaskRunner> child_task_runner_; | |
| 320 | |
| 321 // Task runner Decode() and PictureReady() run on. | |
| 322 scoped_refptr<base::SingleThreadTaskRunner> decode_task_runner_; | |
| 323 | |
| 324 // WeakPtr<> pointing to |this| for use in posting tasks from the decoder or | |
| 325 // device worker threads back to the child thread. Because the worker threads | |
| 326 // are members of this class, any task running on those threads is guaranteed | |
| 327 // that this object is still alive. As a result, tasks posted from the child | |
| 328 // thread to the decoder or device thread should use base::Unretained(this), | |
| 329 // and tasks posted the other way should use |weak_this_|. | |
| 330 base::WeakPtr<V4L2VideoDecodeAccelerator> weak_this_; | |
| 331 | |
| 332 // To expose client callbacks from VideoDecodeAccelerator. | |
| 333 // NOTE: all calls to these objects *MUST* be executed on | |
| 334 // child_task_runner_. | |
| 335 std::unique_ptr<base::WeakPtrFactory<Client>> client_ptr_factory_; | |
| 336 base::WeakPtr<Client> client_; | |
| 337 // Callbacks to |decode_client_| must be executed on |decode_task_runner_|. | |
| 338 base::WeakPtr<Client> decode_client_; | |
| 339 | |
| 340 // | |
| 341 // Decoder state, owned and operated by decoder_thread_. | |
| 342 // Before decoder_thread_ has started, the decoder state is managed by | |
| 343 // the child (main) thread. After decoder_thread_ has started, the decoder | |
| 344 // thread should be the only one managing these. | |
| 345 // | |
| 346 | |
| 347 // This thread services tasks posted from the VDA API entry points by the | |
| 348 // child thread and device service callbacks posted from the device thread. | |
| 349 base::Thread decoder_thread_; | |
| 350 // Decoder state machine state. | |
| 351 State decoder_state_; | |
| 352 // BitstreamBuffer we're presently reading. | |
| 353 std::unique_ptr<BitstreamBufferRef> decoder_current_bitstream_buffer_; | |
| 354 // The V4L2Device this class is operating upon. | |
| 355 scoped_refptr<V4L2Device> device_; | |
| 356 // FlushTask() and ResetTask() should not affect buffers that have been | |
| 357 // queued afterwards. For flushing or resetting the pipeline then, we will | |
| 358 // delay these buffers until after the flush or reset completes. | |
| 359 int decoder_delay_bitstream_buffer_id_; | |
| 360 // Input buffer we're presently filling. | |
| 361 int decoder_current_input_buffer_; | |
| 362 // We track the number of buffer decode tasks we have scheduled, since each | |
| 363 // task execution should complete one buffer. If we fall behind (due to | |
| 364 // resource backpressure, etc.), we'll have to schedule more to catch up. | |
| 365 int decoder_decode_buffer_tasks_scheduled_; | |
| 366 // Picture buffers held by the client. | |
| 367 int decoder_frames_at_client_; | |
| 368 // Are we flushing? | |
| 369 bool decoder_flushing_; | |
| 370 // Got a reset request while we were performing resolution change. | |
| 371 bool resolution_change_reset_pending_; | |
| 372 // Input queue for decoder_thread_: BitstreamBuffers in. | |
| 373 std::queue<linked_ptr<BitstreamBufferRef> > decoder_input_queue_; | |
| 374 // For H264 decode, hardware requires that we send it frame-sized chunks. | |
| 375 // We'll need to parse the stream. | |
| 376 std::unique_ptr<media::H264Parser> decoder_h264_parser_; | |
| 377 // Set if the decoder has a pending incomplete frame in an input buffer. | |
| 378 bool decoder_partial_frame_pending_; | |
| 379 | |
| 380 // | |
| 381 // Hardware state and associated queues. Since decoder_thread_ services | |
| 382 // the hardware, decoder_thread_ owns these too. | |
| 383 // output_buffer_map_, free_output_buffers_ and output_planes_count_ are an | |
| 384 // exception during the buffer (re)allocation sequence, when the | |
| 385 // decoder_thread_ is blocked briefly while the Child thread manipulates | |
| 386 // them. | |
| 387 // | |
| 388 | |
| 389 // Completed decode buffers. | |
| 390 std::queue<int> input_ready_queue_; | |
| 391 | |
| 392 // Input buffer state. | |
| 393 bool input_streamon_; | |
| 394 // Input buffers enqueued to device. | |
| 395 int input_buffer_queued_count_; | |
| 396 // Input buffers ready to use, as a LIFO since we don't care about ordering. | |
| 397 std::vector<int> free_input_buffers_; | |
| 398 // Mapping of int index to input buffer record. | |
| 399 std::vector<InputRecord> input_buffer_map_; | |
| 400 | |
| 401 // Output buffer state. | |
| 402 bool output_streamon_; | |
| 403 // Output buffers enqueued to device. | |
| 404 int output_buffer_queued_count_; | |
| 405 // Output buffers ready to use, as a FIFO since we want oldest-first to hide | |
| 406 // synchronization latency with GL. | |
| 407 std::queue<int> free_output_buffers_; | |
| 408 // Mapping of int index to output buffer record. | |
| 409 std::vector<OutputRecord> output_buffer_map_; | |
| 410 // Required size of DPB for decoding. | |
| 411 int output_dpb_size_; | |
| 412 | |
| 413 // Number of planes (i.e. separate memory buffers) for output. | |
| 414 size_t output_planes_count_; | |
| 415 | |
| 416 // Pictures that are ready but not sent to PictureReady yet. | |
| 417 std::queue<PictureRecord> pending_picture_ready_; | |
| 418 | |
| 419 // The number of pictures that are sent to PictureReady and will be cleared. | |
| 420 int picture_clearing_count_; | |
| 421 | |
| 422 // Used by the decoder thread to wait for AssignPictureBuffers to arrive | |
| 423 // to avoid races with potential Reset requests. | |
| 424 base::WaitableEvent pictures_assigned_; | |
| 425 | |
| 426 // Output picture coded size. | |
| 427 gfx::Size coded_size_; | |
| 428 | |
| 429 // Output picture visible size. | |
| 430 gfx::Size visible_size_; | |
| 431 | |
| 432 // | |
| 433 // The device polling thread handles notifications of V4L2 device changes. | |
| 434 // | |
| 435 | |
| 436 // The thread. | |
| 437 base::Thread device_poll_thread_; | |
| 438 | |
| 439 // | |
| 440 // Other state, held by the child (main) thread. | |
| 441 // | |
| 442 | |
| 443 // EGL state | |
| 444 EGLDisplay egl_display_; | |
| 445 | |
| 446 // Callback to get current GLContext. | |
| 447 GetGLContextCallback get_gl_context_cb_; | |
| 448 // Callback to set the correct gl context. | |
| 449 MakeGLContextCurrentCallback make_context_current_cb_; | |
| 450 | |
| 451 // The codec we'll be decoding for. | |
| 452 media::VideoCodecProfile video_profile_; | |
| 453 // Chosen output format. | |
| 454 uint32_t output_format_fourcc_; | |
| 455 | |
| 456 // Input format V4L2 fourccs this class supports. | |
| 457 static const uint32_t supported_input_fourccs_[]; | |
| 458 | |
| 459 // The WeakPtrFactory for |weak_this_|. | |
| 460 base::WeakPtrFactory<V4L2VideoDecodeAccelerator> weak_this_factory_; | |
| 461 | |
| 462 DISALLOW_COPY_AND_ASSIGN(V4L2VideoDecodeAccelerator); | |
| 463 }; | |
| 464 | |
| 465 } // namespace content | |
| 466 | |
| 467 #endif // CONTENT_COMMON_GPU_MEDIA_V4L2_VIDEO_DECODE_ACCELERATOR_H_ | |
| OLD | NEW |