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