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.
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
9 #ifndef CONTENT_COMMON_GPU_MEDIA_VAAPI_H264_DECODER_H_
10 #define CONTENT_COMMON_GPU_MEDIA_VAAPI_H264_DECODER_H_
14 #include "base/callback_forward.h"
15 #include "base/memory/linked_ptr.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "content/common/gpu/media/h264_dpb.h"
19 #include "content/common/gpu/media/h264_parser.h"
20 #include "content/common/gpu/media/vaapi_wrapper.h"
21 #include "media/base/limits.h"
25 // An H264 decoder that utilizes VA-API. Provides features not supported by
26 // the VA-API userspace library (libva), including stream parsing, reference
27 // picture management and other operations not supported by the HW codec.
29 // Provides functionality to allow plugging VAAPI HW acceleration into the
32 // Clients of this class are expected to pass H264 Annex-B byte stream and
33 // will receive decoded surfaces via client-provided |OutputPicCB|.
35 // This class must be created, called and destroyed on a single thread, and
36 // does nothing internally on any other thread.
37 class VaapiH264Decoder
{
39 // Callback invoked on the client when a surface is to be displayed.
40 // Arguments: input buffer id provided at the time of Decode()
41 // and VASurface to output.
42 typedef base::Callback
<
43 void(int32
, const scoped_refptr
<VASurface
>&)> OutputPicCB
;
45 enum VAVDAH264DecoderFailure
{
46 FRAME_MBS_ONLY_FLAG_NOT_ONE
= 0,
47 GAPS_IN_FRAME_NUM
= 1,
48 MID_STREAM_RESOLUTION_CHANGE
= 2,
49 INTERLACED_STREAM
= 3,
51 VAVDA_H264_DECODER_FAILURES_MAX
,
54 // Callback to report errors for UMA purposes, not used to return errors
56 typedef base::Callback
<void(VAVDAH264DecoderFailure error
)>
59 // Decode result codes.
61 kDecodeError
, // Error while decoding.
62 // TODO posciak: unsupported streams are currently treated as error
63 // in decoding; in future it could perhaps be possible to fall back
64 // to software decoding instead.
65 // kStreamError, // Error in stream.
66 kAllocateNewSurfaces
, // Need a new set of surfaces to be allocated.
67 kRanOutOfStreamData
, // Need more stream data to proceed.
68 kRanOutOfSurfaces
, // Waiting for the client to free up output surfaces.
71 // |vaapi_wrapper| should be initialized.
72 // |output_pic_cb| notifies the client a surface is to be displayed.
73 // |report_error_to_uma_cb| called on errors for UMA purposes, not used
74 // to report errors to clients.
75 VaapiH264Decoder(VaapiWrapper
* vaapi_wrapper
,
76 const OutputPicCB
& output_pic_cb
,
77 const ReportErrorToUmaCB
& report_error_to_uma_cb
);
81 // Have the decoder flush its state and trigger output of all previously
82 // decoded surfaces via OutputPicCB. Return false on failure.
83 bool Flush() WARN_UNUSED_RESULT
;
85 // To be called during decoding.
86 // Stop (pause) decoding, discarding all remaining inputs and outputs,
87 // but do not flush decoder state, so that the playback can be resumed later,
88 // possibly from a different location.
91 // Set current stream data pointer to |ptr| and |size|. Output surfaces
92 // that are decoded from data in this stream chunk are to be returned along
93 // with the given |input_id|.
94 void SetStream(uint8
* ptr
, size_t size
, int32 input_id
);
96 // Try to decode more of the stream, returning decoded frames asynchronously
97 // via output_pic_cb_. Return when more stream is needed, when we run out
98 // of free surfaces, when we need a new set of them, or when an error occurs.
99 DecResult
Decode() WARN_UNUSED_RESULT
;
101 // Return dimensions/required number of output surfaces that client should
102 // be ready to provide for the decoder to function properly.
103 // To be used after Decode() returns kNeedNewSurfaces.
104 gfx::Size
GetPicSize() { return pic_size_
; }
105 size_t GetRequiredNumOfPictures();
107 // To be used by the client to feed decoder with output surfaces.
108 void ReuseSurface(const scoped_refptr
<VASurface
>& va_surface
);
111 // We need to keep at most kDPBMaxSize pictures in DPB for
112 // reference/to display later and an additional one for the one currently
113 // being decoded. We also ask for some additional ones since VDA needs
114 // to accumulate a few ready-to-output pictures before it actually starts
115 // displaying and giving them back. +2 instead of +1 because of subjective
116 // smoothness improvement during testing.
118 kPicsInPipeline
= media::limits::kMaxVideoFrames
+ 2,
119 kMaxNumReqPictures
= H264DPB::kDPBMaxSize
+ kPicsInPipeline
,
122 // Internal state of the decoder.
124 kNeedStreamMetadata
, // After initialization, need an SPS.
125 kDecoding
, // Ready to decode from any point.
126 kAfterReset
, // After Reset(), need a resume point.
127 kError
, // Error in decode, can't continue.
130 // Process H264 stream structures.
131 bool ProcessSPS(int sps_id
, bool* need_new_buffers
);
132 bool ProcessPPS(int pps_id
);
133 bool ProcessSlice(H264SliceHeader
* slice_hdr
);
135 // Initialize the current picture according to data in |slice_hdr|.
136 bool InitCurrPicture(H264SliceHeader
* slice_hdr
);
138 // Calculate picture order counts for the new picture
139 // on initialization of a new frame (see spec).
140 bool CalculatePicOrderCounts(H264SliceHeader
* slice_hdr
);
142 // Update PicNum values in pictures stored in DPB on creation of new
144 void UpdatePicNums();
146 // Construct initial reference picture lists for use in decoding of
147 // P and B pictures (see 8.2.4 in spec).
148 void ConstructReferencePicListsP(H264SliceHeader
* slice_hdr
);
149 void ConstructReferencePicListsB(H264SliceHeader
* slice_hdr
);
151 // Helper functions for reference list construction, per spec.
152 int PicNumF(H264Picture
*pic
);
153 int LongTermPicNumF(H264Picture
*pic
);
155 // Perform the reference picture lists' modification (reordering), as
156 // specified in spec (8.2.4).
158 // |list| indicates list number and should be either 0 or 1.
159 bool ModifyReferencePicList(H264SliceHeader
*slice_hdr
, int list
);
161 // Perform reference picture memory management operations (marking/unmarking
162 // of reference pictures, long term picture management, discarding, etc.).
163 // See 8.2.5 in spec.
164 bool HandleMemoryManagementOps();
165 void ReferencePictureMarking();
167 // Start processing a new frame.
168 bool StartNewFrame(H264SliceHeader
* slice_hdr
);
170 // All data for a frame received, process it and decode.
171 bool FinishPrevFrameIfPresent();
173 // Called after decoding, performs all operations to be done after decoding,
174 // including DPB management, reference picture marking and memory management
176 // This will also output a picture if one is ready for output.
177 bool FinishPicture();
179 // Clear DPB contents and remove all surfaces in DPB from *in_use_ list.
180 // Cleared pictures will be made available for decode, unless they are
181 // at client waiting to be displayed.
184 // These queue up data for HW decoder to be committed on running HW decode.
187 bool SendVASliceParam(H264SliceHeader
* slice_hdr
);
188 bool SendSliceData(const uint8
* ptr
, size_t size
);
189 bool QueueSlice(H264SliceHeader
* slice_hdr
);
191 // Helper methods for filling HW structures.
192 void FillVAPicture(VAPictureH264
*va_pic
, H264Picture
* pic
);
193 int FillVARefFramesFromDPB(VAPictureH264
*va_pics
, int num_pics
);
195 // Commits all pending data for HW decoder and starts HW decoder.
196 bool DecodePicture();
198 // Notifies client that a picture is ready for output.
199 bool OutputPic(H264Picture
* pic
);
201 // Output all pictures in DPB that have not been outputted yet.
202 bool OutputAllRemainingPics();
204 // Represents a frame being decoded. Will always have a VASurface
205 // assigned to it, which will eventually contain decoded picture data.
208 // Assign an available surface to the given PicOrderCnt |poc|,
209 // removing it from the available surfaces pool. Return true if a surface
210 // has been found, false otherwise.
211 bool AssignSurfaceToPoC(int32 input_id
, int poc
);
213 // Indicate that a surface is no longer needed by decoder.
214 void UnassignSurfaceFromPoC(int poc
);
216 // Return DecodeSurface assigned to |poc|.
217 DecodeSurface
* DecodeSurfaceByPoC(int poc
);
228 // Picture currently being processed/decoded.
229 scoped_ptr
<H264Picture
> curr_pic_
;
231 // Reference picture lists, constructed for each picture before decoding.
232 // Those lists are not owners of the pointers (DPB is).
233 H264Picture::PtrVector ref_pic_list0_
;
234 H264Picture::PtrVector ref_pic_list1_
;
236 // Global state values, needed in decoding. See spec.
237 int max_pic_order_cnt_lsb_
;
240 int max_long_term_frame_idx_
;
244 int prev_frame_num_offset_
;
245 bool prev_has_memmgmnt5_
;
247 // Values related to previously decoded reference picture.
248 bool prev_ref_has_memmgmnt5_
;
249 int prev_ref_top_field_order_cnt_
;
250 int prev_ref_pic_order_cnt_msb_
;
251 int prev_ref_pic_order_cnt_lsb_
;
252 H264Picture::Field prev_ref_field_
;
254 // Currently active SPS and PPS.
258 // Output picture size.
261 // Maps H.264 PicOrderCount to currently used DecodeSurfaces;
262 typedef std::map
<int, linked_ptr
<DecodeSurface
> > DecSurfacesInUse
;
263 DecSurfacesInUse decode_surfaces_in_use_
;
265 // Unused VA surfaces returned by client, ready to be reused.
266 std::vector
<scoped_refptr
<VASurface
> > available_va_surfaces_
;
268 // The id of current input buffer, which will be associated with an
269 // output surface when a frame is successfully decoded.
270 int32 curr_input_id_
;
272 VaapiWrapper
* vaapi_wrapper_
;
274 // Called by decoder when a surface should be outputted.
275 OutputPicCB output_pic_cb_
;
277 // Called to report decoding error to UMA, not used to indicate errors
279 ReportErrorToUmaCB report_error_to_uma_cb_
;
281 // PicOrderCount of the previously outputted frame.
282 int last_output_poc_
;
284 // Maximum size of DPB required by codec level.
287 DISALLOW_COPY_AND_ASSIGN(VaapiH264Decoder
);
290 } // namespace content
292 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_H264_DECODER_H_