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