OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 VideoDecoderAccelerator |
| 6 // that utilizes hardware video decoder present on Intel CPUs. |
| 7 |
| 8 #ifndef CONTENT_COMMON_GPU_MEDIA_VAAPI_VIDEO_DECODE_ACCELERATOR_H_ |
| 9 #define CONTENT_COMMON_GPU_MEDIA_VAAPI_VIDEO_DECODE_ACCELERATOR_H_ |
| 10 |
| 11 #include "vaapi_h264_decoder.h" |
| 12 |
| 13 #include <queue> |
| 14 #include <utility> |
| 15 #include <vector> |
| 16 |
| 17 #include <GL/glx.h> |
| 18 |
| 19 #include "base/logging.h" |
| 20 #include "base/memory/ref_counted.h" |
| 21 #include "base/message_loop.h" |
| 22 #include "base/shared_memory.h" |
| 23 #include "base/synchronization/condition_variable.h" |
| 24 #include "base/synchronization/lock.h" |
| 25 #include "base/threading/non_thread_safe.h" |
| 26 #include "base/threading/thread.h" |
| 27 #include "media/base/bitstream_buffer.h" |
| 28 #include "media/video/picture.h" |
| 29 #include "media/video/video_decode_accelerator.h" |
| 30 |
| 31 // Class to provide video decode acceleration for Intel systems with hardware |
| 32 // support for it, and on which libva is available. |
| 33 // Decoding tasks are performed in a separate decoding thread. |
| 34 class VaapiVideoDecodeAccelerator : public media::VideoDecodeAccelerator { |
| 35 public: |
| 36 |
| 37 VaapiVideoDecodeAccelerator(Client* client); |
| 38 |
| 39 // media::VideoDecodeAccelerator implementation. |
| 40 virtual bool Initialize(media::VideoCodecProfile profile) OVERRIDE; |
| 41 virtual void Decode(const media::BitstreamBuffer& bitstream_buffer) OVERRIDE; |
| 42 virtual void AssignPictureBuffers( |
| 43 const std::vector<media::PictureBuffer>& buffers) OVERRIDE; |
| 44 virtual void ReusePictureBuffer(int32 picture_buffer_id) OVERRIDE; |
| 45 virtual void Flush() OVERRIDE; |
| 46 virtual void Reset() OVERRIDE; |
| 47 virtual void Destroy() OVERRIDE; |
| 48 |
| 49 // Used by user of this class to pass X/GLX state. |
| 50 void SetGlxState(Display* x_display, GLXContext glx_context); |
| 51 |
| 52 private: |
| 53 virtual ~VaapiVideoDecodeAccelerator(); |
| 54 |
| 55 void NotifyInitializeDone(); |
| 56 |
| 57 // Notify the client that the input buffer has been consumed. |
| 58 void NotifyInputBufferRead(int input_buffer_id); |
| 59 |
| 60 // Ensure data has been synced with the output texture and notify |
| 61 // the client it is ready for displaying. |
| 62 void SyncAndNotifyPictureReady(int32 input_id, int32 output_id); |
| 63 |
| 64 // Posted by the decoder thread to notify VAVDA that the decoder has |
| 65 // initially parsed the stream and is ready to decode. If the pictures have |
| 66 // not yet been requested, it will request the client to provide |num_pics| |
| 67 // textures of given |size| and wait for them, otherwise will post |
| 68 // a DecodeTask directly. |
| 69 void ReadyToDecode(int num_pics, const gfx::Size& size); |
| 70 |
| 71 // Notify the client that an error has occurred and decoding cannot continue. |
| 72 void NotifyError(Error error); |
| 73 |
| 74 // Map the received input buffer into this process' address space and |
| 75 // queue it for decode. |
| 76 void MapAndQueueNewInputBuffer( |
| 77 const media::BitstreamBuffer& bitstream_buffer); |
| 78 |
| 79 // Get a new input buffer from the queue and set it up in decoder. This will |
| 80 // sleep if no input buffers are available. Return true if a new buffer has |
| 81 // been set up, false if an early exit has been requested (due to initiated |
| 82 // reset/flush/destroy). |
| 83 bool GetInputBuffer(); |
| 84 |
| 85 // Signal the client that the current buffer has been read and can be |
| 86 // returned. Will also release the mapping. |
| 87 void ReturnCurrInputBuffer(); |
| 88 |
| 89 // Get and set up one or more output buffers in the decoder. This will sleep |
| 90 // if no buffers are available. Return true if buffers have been set up or |
| 91 // false if an early exit has been requested (due to initiated |
| 92 // reset/flush/destroy). |
| 93 bool GetOutputBuffers(); |
| 94 |
| 95 // Initial decode task: get the decoder to the point in the stream from which |
| 96 // it can start/continue decoding. Does not require output buffers and does |
| 97 // not produce output frames. Called either when starting with a new stream |
| 98 // or when playback is to be resumed following a seek. |
| 99 void InitialDecodeTask(); |
| 100 |
| 101 // Decoding task. Will continue decoding given input buffers and sleep |
| 102 // waiting for input/output as needed. Will exit if a reset/flush/destroy |
| 103 // is requested. |
| 104 void DecodeTask(); |
| 105 |
| 106 // Scheduled after receiving a flush request and executed after the current |
| 107 // decoding task finishes decoding pending inputs. Makes the decoder return |
| 108 // all remaining output pictures and puts it in an idle state, ready |
| 109 // to resume if needed and schedules a FinishFlush. |
| 110 void FlushTask(); |
| 111 |
| 112 // Scheduled by the FlushTask after decoder is flushed to put VAVDA into idle |
| 113 // state and notify the client that flushing has been finished. |
| 114 void FinishFlush(); |
| 115 |
| 116 // Scheduled after receiving a reset request and executed after the current |
| 117 // decoding task finishes decoding the current frame. Puts the decoder into |
| 118 // an idle state, ready to resume if needed, discarding decoded but not yet |
| 119 // outputted pictures (decoder keeps ownership of their associated picture |
| 120 // buffers). Schedules a FinishReset afterwards. |
| 121 void ResetTask(); |
| 122 |
| 123 // Scheduled by ResetTask after it's done putting VAVDA into an idle state. |
| 124 // Drops remaining input buffers and notifies the client that reset has been |
| 125 // finished. |
| 126 void FinishReset(); |
| 127 |
| 128 // Scheduled on the decoder thread after receiving a Destroy() call from the |
| 129 // client, executed after the current decoding task finishes decoding the |
| 130 // current frame, ignoring any remaining inputs. Cleans up the decoder and |
| 131 // frees all resources. |
| 132 void DestroyTask(); |
| 133 |
| 134 // Scheduled by DestroyTask after it's done destroying the decoder, puts |
| 135 // VAVDA into an uninitialized state. |
| 136 void FinishDestroy(); |
| 137 |
| 138 // Client-provided X/GLX state. |
| 139 Display* x_display_; |
| 140 GLXContext glx_context_; |
| 141 |
| 142 // VAVDA state. |
| 143 enum State { |
| 144 // Initialize() not called yet or failed. |
| 145 kUninitialized, |
| 146 // Initialize() succeeded, no initial decode and no pictures requested. |
| 147 kInitialized, |
| 148 // Initial decode finished, requested pictures and waiting for them. |
| 149 kPicturesRequested, |
| 150 // Everything initialized, pictures received and assigned, in decoding. |
| 151 kDecoding, |
| 152 // Resetting, waiting for decoder to finish current task and cleanup. |
| 153 kResetting, |
| 154 // Flushing, waiting for decoder to finish current task and cleanup. |
| 155 kFlushing, |
| 156 // Idle, decoder in state ready to resume decoding. |
| 157 kIdle, |
| 158 // Destroying, waiting for the decoder to finish current task. |
| 159 kDestroying, |
| 160 }; |
| 161 |
| 162 State state_; |
| 163 |
| 164 // Protects input and output buffer queues and state_. |
| 165 base::Lock lock_; |
| 166 |
| 167 // An input buffer awaiting consumption, provided by the client. |
| 168 struct InputBuffer { |
| 169 int32 id; |
| 170 size_t size; |
| 171 scoped_ptr<base::SharedMemory> shm; |
| 172 }; |
| 173 |
| 174 // Queue for incoming input buffers. |
| 175 typedef std::queue<linked_ptr<InputBuffer> > InputBuffers; |
| 176 InputBuffers input_buffers_; |
| 177 // Signalled when input buffers are queued onto the input_buffers_ queue. |
| 178 base::ConditionVariable input_ready_; |
| 179 |
| 180 // Current input buffer at decoder. |
| 181 linked_ptr<InputBuffer> curr_input_buffer_; |
| 182 |
| 183 // Queue for incoming input buffers. |
| 184 typedef std::queue<int32> OutputBuffers; |
| 185 OutputBuffers output_buffers_; |
| 186 // Signalled when output buffers are queued onto the output_buffers_ queue. |
| 187 base::ConditionVariable output_ready_; |
| 188 |
| 189 // ChildThread's message loop |
| 190 MessageLoop* message_loop_; |
| 191 |
| 192 // To expose client callbacks from VideoDecodeAccelerator. |
| 193 // NOTE: all calls to this object *MUST* be executed on message_loop_. |
| 194 Client* client_; |
| 195 |
| 196 base::Thread decoder_thread_; |
| 197 content::VaapiH264Decoder decoder_; |
| 198 |
| 199 // Callback passed to the decoder, which it will use to signal readiness |
| 200 // of an output picture to be displayed. |
| 201 void OutputPicCallback(int32 input_id, int32 output_id); |
| 202 |
| 203 DISALLOW_COPY_AND_ASSIGN(VaapiVideoDecodeAccelerator); |
| 204 }; |
| 205 |
| 206 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_VIDEO_DECODE_ACCELERATOR_H_ |
| 207 |
OLD | NEW |