Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(269)

Side by Side Diff: content/common/gpu/media/vaapi_h264_decoder.h

Issue 9814001: Add VAVDA, the VAAPI Video Decode Accelerator for Intel CPUs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 a class that provides H264 decode
6 // support for use with VAAPI hardware video decode acceleration on Intel
7 // systems.
8
9 #ifndef CONTENT_COMMON_GPU_MEDIA_VAAPI_H264_DECODER_H_
10 #define CONTENT_COMMON_GPU_MEDIA_VAAPI_H264_DECODER_H_
11
12 #include <GL/glx.h>
13
14 #include <queue>
15
16 #include "base/callback_forward.h"
17 #include "base/lazy_instance.h"
18 #include "base/memory/linked_ptr.h"
19 #include "base/memory/scoped_ptr.h"
20 #include "content/common/gpu/media/h264_dpb.h"
21 #include "content/common/gpu/media/h264_parser.h"
22 #include "media/base/video_decoder_config.h"
23 #include "media/base/limits.h"
24 #include "third_party/libva/va/va.h"
25
26 namespace content {
27
28 // An H264 decoder for use for VA-API-specific decoding. Provides features not
29 // supported by libva, including stream parsing, reference picture management
30 // and other operations not supported by the HW codec.
31 //
32 // Provides functionality to allow plugging VAAPI HW acceleration into the
33 // VDA framework.
34 //
35 // Clients of this class are expected to pass H264 Annex-B byte stream and
36 // will receive decoded pictures via client-provided |OutputPicCB|.
37 //
38 // If used in multi-threaded environment, some of the functions have to be
39 // called on the child thread, i.e. the main thread of the GPU process
40 // (the one that has the GLX context passed to Initialize() set as current).
41 // This is essential so that the GLX calls can work properly.
42 // Decoder thread, on the other hand, does not require a GLX context and should
43 // be the same as the one on which Decode*() functions are called.
44 class VaapiH264Decoder {
45 public:
46 // Callback invoked on the client when a picture is to be displayed.
47 // Callee has to call PutPicToTexture() for the given picture before
48 // displaying it, to ensure the contents have been synced properly.
49 // Arguments: input buffer id, output buffer id (both provided by the client
50 // at the time of Decode() and AssignPictureBuffer() calls).
51 typedef base::Callback<void(int32, int32)> OutputPicCB;
52
53 // Decode result codes.
54 enum DecResult {
55 kDecodeError, // Error while decoding.
56 // TODO posciak: unsupported streams are currently treated as error
57 // in decoding; in future it could perhaps be possible to fall back
58 // to software decoding instead.
59 // kStreamError, // Error in stream.
60 kReadyToDecode, // Successfully initialized.
61 kDecodedFrame, // Successfully decoded a frame.
62 kNeedMoreStreamData, // Need more stream data to decode the next frame.
63 kNoOutputAvailable, // Waiting for the client to free up output surfaces.
64 };
65
66 VaapiH264Decoder();
67 // Should be called on the GLX thread, for the surface cleanup to work
68 // properly.
69 ~VaapiH264Decoder();
70
71 // Initializes and sets up libva connection and GL/X11 resources.
72 // Must be called on the GLX thread with |glx_context| being current and
73 // with decoder thread not yet running.
74 // |output_pic_cb| will be called to notify when a picture can be displayed.
75 bool Initialize(media::VideoCodecProfile profile,
76 Display* x_display,
77 GLXContext glx_context,
78 const OutputPicCB& output_pic_cb) WARN_UNUSED_RESULT;
79 void Destroy();
80
81 // Notify the decoder that this output buffer has been consumed and
82 // can be reused (overwritten).
83 // Must be run on the decoder thread.
84 void ReusePictureBuffer(int32 picture_buffer_id);
85
86 // Give a new picture buffer (texture) to decoder for use.
87 // Must be run on the GLX thread with decoder thread not yet running.
88 bool AssignPictureBuffer(int32 picture_buffer_id, uint32 texture_id)
89 WARN_UNUSED_RESULT;
90
91 // Sync the data so that the texture for given |picture_buffer_id| can
92 // be displayed.
93 // Must be run on the GLX thread.
94 bool PutPicToTexture(int32 picture_buffer_id) WARN_UNUSED_RESULT;
95
96 // Have the decoder flush its state and trigger output of all previously
97 // decoded pictures via OutputPicCB.
98 // Returns false if any of the resulting invocations of the callback fail.
99 bool Flush() WARN_UNUSED_RESULT;
100
101 // Called while decoding.
102 // Stop decoding, discarding all remaining input/output, but do not flush
103 // state, so the playback of the same stream can be resumed (possibly from
104 // another location).
105 void Reset();
106
107 // Set current stream data pointer to |ptr| and |size|.
108 // Must be run on decoder thread.
109 void SetStream(uint8* ptr, size_t size);
110
111 // Start parsing stream to detect picture sizes. Does not produce any
112 // decoded pictures and can be called without providing output textures.
113 // Also to be used after Reset() to find a suitable location in the
114 // stream to resume playback from.
115 DecResult DecodeInitial(int32 input_id) WARN_UNUSED_RESULT;
116
117 // Runs until a frame is decoded or end of provided stream data buffer
118 // is reached. Decoded pictures will be returned asynchronously via
119 // OutputPicCB.
120 DecResult DecodeOneFrame(int32 input_id) WARN_UNUSED_RESULT;
121
122 // Return dimensions for output buffer (texture) allocation.
123 // Valid only after a successful DecodeInitial().
124 int pic_height() { return pic_height_; }
125 int pic_width() { return pic_width_; }
126
127 // Return the number of output pictures required for decoding.
128 // Valid after a successful DecodeInitial().
129 static size_t GetRequiredNumOfPictures();
130
131 private:
132 // We need to keep at least kDPBMaxSize pictures in DPB for
133 // reference/to display later and an additional one for the one currently
134 // being decoded. We also ask for some additional ones since VDA needs
135 // to accumulate a few ready-to-output pictures before it actually starts
136 // displaying and giving them back. +2 instead of +1 because of subjective
137 // smoothness improvement during testing.
138 enum { kNumReqPictures = H264DPB::kDPBMaxSize +
139 media::limits::kMaxVideoFrames + 2 };
140
141 // Internal state of the decoder.
142 enum State {
143 kUninitialized, // Initialize() not yet called.
144 kInitialized, // Initialize() called, pictures requested.
145 kDecoding, // DecodeInitial() successful, output surfaces allocated.
146 kAfterReset, // After Reset() during decoding.
147 kError, // Error in kDecoding state.
148 };
149
150 // Get usable framebuffer configuration for use in binding textures
151 // or return false on failure.
152 bool InitializeFBConfig();
153
154 // Process H264 stream structures.
155 bool ProcessSPS(int sps_id);
156 bool ProcessPPS(int pps_id);
157 bool ProcessSlice(H264SliceHeader* slice_hdr);
158
159 // Initialize the current picture according to data in |slice_hdr|.
160 bool InitCurrPicture(H264SliceHeader* slice_hdr);
161
162 // Calculate picture order counts for the new picture
163 // on initialization of a new frame (see spec).
164 bool CalculatePicOrderCounts(H264SliceHeader* slice_hdr);
165
166 // Update PicNum values in pictures stored in DPB on creation of new
167 // frame (see spec).
168 void UpdatePicNums();
169
170 // Construct initial reference picture lists for use in decoding of
171 // P and B pictures (see 8.2.4 in spec).
172 void ConstructReferencePicListsP(H264SliceHeader* slice_hdr);
173 void ConstructReferencePicListsB(H264SliceHeader* slice_hdr);
174
175 // Helper functions for reference list construction, per spec.
176 int PicNumF(H264Picture *pic);
177 int LongTermPicNumF(H264Picture *pic);
178
179 // Perform the reference picture lists' modification (reordering), as
180 // specified in spec (8.2.4).
181 //
182 // |list| indicates list number and should be either 0 or 1.
183 bool ModifyReferencePicList(H264SliceHeader *slice_hdr, int list);
184
185 // Perform reference picture memory management operations (marking/unmarking
186 // of reference pictures, long term picture management, discarding, etc.).
187 // See 8.2.5 in spec.
188 bool HandleMemoryManagementOps();
189 void ReferencePictureMarking();
190
191 // Start processing a new frame.
192 bool StartNewFrame(H264SliceHeader* slice_hdr);
193
194 // All data for a frame received, process it and decode.
195 bool FinishPrevFrameIfPresent();
196
197 // Called after decoding, performs all operations to be done after decoding,
198 // including DPB management, reference picture marking and memory management
199 // operations.
200 // This will also output a picture if one is ready for output.
201 bool FinishPicture();
202
203 // Convert VideoCodecProfile to VAProfile and set it as active.
204 bool SetProfile(media::VideoCodecProfile profile);
205
206 // Vaapi-related functions.
207
208 // Allocates VASurfaces and creates a VAContext for them.
209 bool CreateVASurfaces();
210
211 // Destroys allocated VASurfaces and related VAContext.
212 void DestroyVASurfaces();
213
214 // These queue up data for HW decoder to be committed on running HW decode.
215 bool SendPPS();
216 bool SendIQMatrix();
217 bool SendVASliceParam(H264SliceHeader* slice_hdr);
218 bool SendSliceData(const uint8* ptr, size_t size);
219 bool QueueSlice(H264SliceHeader* slice_hdr);
220
221 // Helper methods for filling HW structures.
222 void FillVAPicture(VAPictureH264 *va_pic, H264Picture* pic);
223 int FillVARefFramesFromDPB(VAPictureH264 *va_pics, int num_pics);
224
225 // Commits all pending data for HW decoder and starts HW decoder.
226 bool DecodePicture();
227
228 // Notifies client that a picture is ready for output.
229 bool OutputPic(H264Picture* pic);
230
231 State state_;
232
233 // A frame has been sent to hardware as the result of the last
234 // DecodeOneFrame() call.
235 bool frame_ready_at_hw_;
236
237 // Parser in use.
238 H264Parser parser_;
239
240 // DPB in use.
241 H264DPB dpb_;
242
243 // Picture currently being processed/decoded.
244 scoped_ptr<H264Picture> curr_pic_;
245
246 // Reference picture lists, constructed for each picture before decoding.
247 // Those lists are not owners of the pointers (DPB is).
248 H264Picture::PtrVector ref_pic_list0_;
249 H264Picture::PtrVector ref_pic_list1_;
250
251 // Global state values, needed in decoding. See spec.
252 int max_pic_order_cnt_lsb_;
253 int max_frame_num_;
254 int max_pic_num_;
255 int max_long_term_frame_idx_;
256
257 int frame_num_;
258 int prev_frame_num_;
259 int prev_frame_num_offset_;
260
261 // Values related to previously decoded reference picture.
262 bool prev_ref_has_memmgmnt5_;
263 int prev_ref_top_field_order_cnt_;
264 int prev_ref_pic_order_cnt_msb_;
265 int prev_ref_pic_order_cnt_lsb_;
266 H264Picture::Field prev_ref_field_;
267
268 // Currently active SPS and PPS.
269 int curr_sps_id_;
270 int curr_pps_id_;
271
272 // Output picture size.
273 int pic_width_;
274 int pic_height_;
275
276 // Data queued up for HW decoder, to be commited on next HW decode.
277 std::queue<VABufferID> pending_slice_bufs_;
278 std::queue<VABufferID> pending_va_bufs_;
279
280 // Manages binding of a client-provided output buffer (texture) to VASurface.
281 class DecodeSurface;
282
283 // Maps output_buffer_id to a decode surface. Used to look up surfaces
284 // on requests from the client.
285 typedef std::map<int32, linked_ptr<DecodeSurface> > DecodeSurfaces;
286 DecodeSurfaces decode_surfaces_;
287
288 // Number of decode surface currently available for decoding.
289 int num_available_decode_surfaces_;
290
291 // Maps decode surfaces to PicOrderCount, used to look up output buffers
292 // when a decision to output a picture has been made.
293 typedef std::map<int, DecodeSurface*> POCToDecodeSurfaces;
294 POCToDecodeSurfaces poc_to_decode_surfaces_;
295
296 // Find an available surface and assign it to given PicOrderCnt |poc|,
297 // removing it from the available surfaces pool. Return true if a surface
298 // has been found, false otherwise.
299 bool AssignSurfaceToPoC(int poc);
300
301 // Unassign a surface from |poc| and return a pointer to it, or NULL if there
302 // is no surface associated with given |poc|. Note that this does not make
303 // the surface available for reuse - as this can only happen after client
304 // returns the surface via ReusePictureBuffer() - but only removes its
305 // association with given |poc|.
306 DecodeSurface* UnassignSurfaceFromPoC(int poc);
307
308 // The id of current input buffer, which will be associated with an
309 // output picture if a frame is decoded successfully.
310 int32 curr_input_id_;
311
312 // X/GLX handles.
313 Display* x_display_;
314 GLXContext parent_glx_context_;
315 GLXFBConfig fb_config_;
316
317 // VA handles.
318 VADisplay va_display_;
319 VAConfigID va_config_id_;
320 VAContextID va_context_id_;
321 VAProfile profile_;
322
323 // Allocated VASurfaces.
324 VASurfaceID va_surface_ids_[kNumReqPictures];
325
326 // Called by decoder when a picture should be outputted.
327 OutputPicCB output_pic_cb_;
328
329 DISALLOW_COPY_AND_ASSIGN(VaapiH264Decoder);
330 };
331
332 } // namespace content
333
334 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_H264_DECODER_H_
335
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698