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 #ifndef CONTENT_COMMON_GPU_MEDIA_H264_DECODER_H_
6 #define CONTENT_COMMON_GPU_MEDIA_H264_DECODER_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "content/common/content_export.h"
13 #include "content/common/gpu/media/accelerated_video_decoder.h"
14 #include "content/common/gpu/media/h264_dpb.h"
15 #include "media/base/limits.h"
16 #include "media/filters/h264_parser.h"
17 #include "ui/gfx/geometry/size.h"
21 // Clients of this class are expected to pass H264 Annex-B byte stream
22 // and are expected to provide an implementation of H264Accelerator for
23 // offloading final steps of the decoding process.
25 // This class must be created, called and destroyed on a single thread, and
26 // does nothing internally on any other thread.
27 class CONTENT_EXPORT H264Decoder
: public AcceleratedVideoDecoder
{
29 class CONTENT_EXPORT H264Accelerator
{
32 virtual ~H264Accelerator();
34 // Create a new H264Picture that the decoder client can use for decoding
35 // and pass back to this accelerator for decoding or reference.
36 // When the picture is no longer needed by decoder, it will just drop
37 // its reference to it, and it may do so at any time.
38 // Note that this may return nullptr if accelerator is not able to provide
39 // any new pictures at given time. The decoder is expected to handle
40 // this situation as normal and return from Decode() with kRanOutOfSurfaces.
41 virtual scoped_refptr
<H264Picture
> CreateH264Picture() = 0;
43 // Submit metadata for the current frame, providing the current |sps| and
44 // |pps| for it, |dpb| has to contain all the pictures in DPB for current
45 // frame, and |ref_pic_p0/b0/b1| as specified in the H264 spec. Note that
46 // depending on the frame type, either p0, or b0 and b1 are used. |pic|
47 // contains information about the picture for the current frame.
48 // Note that this does not run decode in the accelerator and the decoder
49 // is expected to follow this call with one or more SubmitSlice() calls
50 // before calling SubmitDecode().
51 // Return true if successful.
52 virtual bool SubmitFrameMetadata(const media::H264SPS
* sps
,
53 const media::H264PPS
* pps
,
55 const H264Picture::Vector
& ref_pic_listp0
,
56 const H264Picture::Vector
& ref_pic_listb0
,
57 const H264Picture::Vector
& ref_pic_listb1
,
58 const scoped_refptr
<H264Picture
>& pic
) = 0;
60 // Submit one slice for the current frame, passing the current |pps| and
61 // |pic| (same as in SubmitFrameMetadata()), the parsed header for the
62 // current slice in |slice_hdr|, and the reordered |ref_pic_listX|,
64 // |data| pointing to the full slice (including the unparsed header| of
66 // This must be called one or more times per frame, before SubmitDecode().
67 // Note that |data| does not have to remain valid after this call returns.
68 // Return true if successful.
69 virtual bool SubmitSlice(const media::H264PPS
* pps
,
70 const media::H264SliceHeader
* slice_hdr
,
71 const H264Picture::Vector
& ref_pic_list0
,
72 const H264Picture::Vector
& ref_pic_list1
,
73 const scoped_refptr
<H264Picture
>& pic
,
77 // Execute the decode in hardware for |pic|, using all the slices and
78 // metadata submitted via SubmitFrameMetadata() and SubmitSlice() since
79 // the previous call to SubmitDecode().
80 // Return true if successful.
81 virtual bool SubmitDecode(const scoped_refptr
<H264Picture
>& pic
) = 0;
83 // Schedule output (display) of |pic|. Note that returning from this
84 // method does not mean that |pic| has already been outputted (displayed),
85 // but guarantees that all pictures will be outputted in the same order
86 // as this method was called for them. Decoder may drop its reference
87 // to |pic| after calling this method.
88 // Return true if successful.
89 virtual bool OutputPicture(const scoped_refptr
<H264Picture
>& pic
) = 0;
91 // Reset any current state that may be cached in the accelerator, dropping
92 // any cached parameters/slices that have not been committed yet.
93 virtual void Reset() = 0;
96 DISALLOW_COPY_AND_ASSIGN(H264Accelerator
);
99 H264Decoder(H264Accelerator
* accelerator
);
100 ~H264Decoder() override
;
102 // content::AcceleratedVideoDecoder implementation.
103 bool Flush() override WARN_UNUSED_RESULT
;
104 void Reset() override
;
105 void SetStream(const uint8_t* ptr
, size_t size
) override
;
106 DecodeResult
Decode() override WARN_UNUSED_RESULT
;
107 gfx::Size
GetPicSize() const override
;
108 size_t GetRequiredNumOfPictures() const override
;
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 PreprocessSlice(media::H264SliceHeader
* slice_hdr
);
134 bool ProcessSlice(media::H264SliceHeader
* slice_hdr
);
136 // Initialize the current picture according to data in |slice_hdr|.
137 bool InitCurrPicture(media::H264SliceHeader
* slice_hdr
);
139 // Calculate picture order counts for the new picture
140 // on initialization of a new frame (see spec).
141 bool CalculatePicOrderCounts(media::H264SliceHeader
* slice_hdr
);
143 // Update PicNum values in pictures stored in DPB on creation of new
145 void UpdatePicNums();
147 bool UpdateMaxNumReorderFrames(const media::H264SPS
* sps
);
149 // Prepare reference picture lists for the current frame.
150 void PrepareRefPicLists(media::H264SliceHeader
* slice_hdr
);
151 // Prepare reference picture lists for the given slice.
152 bool ModifyReferencePicLists(media::H264SliceHeader
* slice_hdr
,
153 H264Picture::Vector
* ref_pic_list0
,
154 H264Picture::Vector
* ref_pic_list1
);
156 // Construct initial reference picture lists for use in decoding of
157 // P and B pictures (see 8.2.4 in spec).
158 void ConstructReferencePicListsP(media::H264SliceHeader
* slice_hdr
);
159 void ConstructReferencePicListsB(media::H264SliceHeader
* slice_hdr
);
161 // Helper functions for reference list construction, per spec.
162 int PicNumF(const scoped_refptr
<H264Picture
>& pic
);
163 int LongTermPicNumF(const scoped_refptr
<H264Picture
>& pic
);
165 // Perform the reference picture lists' modification (reordering), as
166 // specified in spec (8.2.4).
168 // |list| indicates list number and should be either 0 or 1.
169 bool ModifyReferencePicList(media::H264SliceHeader
* slice_hdr
,
171 H264Picture::Vector
* ref_pic_listx
);
173 // Perform reference picture memory management operations (marking/unmarking
174 // of reference pictures, long term picture management, discarding, etc.).
175 // See 8.2.5 in spec.
176 bool HandleMemoryManagementOps();
177 void ReferencePictureMarking();
179 // Start processing a new frame.
180 bool StartNewFrame(media::H264SliceHeader
* slice_hdr
);
182 // All data for a frame received, process it and decode.
183 bool FinishPrevFrameIfPresent();
185 // Called after decoding, performs all operations to be done after decoding,
186 // including DPB management, reference picture marking and memory management
188 // This will also output a picture if one is ready for output.
189 bool FinishPicture();
191 // Clear DPB contents and remove all surfaces in DPB from *in_use_ list.
192 // Cleared pictures will be made available for decode, unless they are
193 // at client waiting to be displayed.
196 // Commits all pending data for HW decoder and starts HW decoder.
197 bool DecodePicture();
199 // Notifies client that a picture is ready for output.
200 void OutputPic(scoped_refptr
<H264Picture
> pic
);
202 // Output all pictures in DPB that have not been outputted yet.
203 bool OutputAllRemainingPics();
209 media::H264Parser parser_
;
214 // Picture currently being processed/decoded.
215 scoped_refptr
<H264Picture
> curr_pic_
;
217 // Reference picture lists, constructed for each frame.
218 H264Picture::Vector ref_pic_list_p0_
;
219 H264Picture::Vector ref_pic_list_b0_
;
220 H264Picture::Vector ref_pic_list_b1_
;
222 // Global state values, needed in decoding. See spec.
223 int max_pic_order_cnt_lsb_
;
226 int max_long_term_frame_idx_
;
227 size_t max_num_reorder_frames_
;
231 int prev_frame_num_offset_
;
232 bool prev_has_memmgmnt5_
;
234 // Values related to previously decoded reference picture.
235 bool prev_ref_has_memmgmnt5_
;
236 int prev_ref_top_field_order_cnt_
;
237 int prev_ref_pic_order_cnt_msb_
;
238 int prev_ref_pic_order_cnt_lsb_
;
239 H264Picture::Field prev_ref_field_
;
241 // Currently active SPS and PPS.
245 // Current NALU and slice header being processed.
246 scoped_ptr
<media::H264NALU
> curr_nalu_
;
247 scoped_ptr
<media::H264SliceHeader
> curr_slice_hdr_
;
249 // Output picture size.
252 // PicOrderCount of the previously outputted frame.
253 int last_output_poc_
;
255 H264Accelerator
* accelerator_
;
257 DISALLOW_COPY_AND_ASSIGN(H264Decoder
);
260 } // namespace content
262 #endif // CONTENT_COMMON_GPU_MEDIA_H264_DECODER_H_