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 #ifndef CONTENT_COMMON_GPU_MEDIA_H264_DECODER_H_ | |
6 #define CONTENT_COMMON_GPU_MEDIA_H264_DECODER_H_ | |
7 | |
8 #include <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <memory> | |
12 #include <vector> | |
13 | |
14 #include "base/macros.h" | |
15 #include "base/memory/ref_counted.h" | |
16 #include "content/common/content_export.h" | |
17 #include "content/common/gpu/media/accelerated_video_decoder.h" | |
18 #include "content/common/gpu/media/h264_dpb.h" | |
19 #include "media/base/limits.h" | |
20 #include "media/filters/h264_parser.h" | |
21 #include "ui/gfx/geometry/size.h" | |
22 | |
23 namespace content { | |
24 | |
25 // Clients of this class are expected to pass H264 Annex-B byte stream | |
26 // and are expected to provide an implementation of H264Accelerator for | |
27 // offloading final steps of the decoding process. | |
28 // | |
29 // This class must be created, called and destroyed on a single thread, and | |
30 // does nothing internally on any other thread. | |
31 class CONTENT_EXPORT H264Decoder : public AcceleratedVideoDecoder { | |
32 public: | |
33 class CONTENT_EXPORT H264Accelerator { | |
34 public: | |
35 H264Accelerator(); | |
36 virtual ~H264Accelerator(); | |
37 | |
38 // Create a new H264Picture that the decoder client can use for decoding | |
39 // and pass back to this accelerator for decoding or reference. | |
40 // When the picture is no longer needed by decoder, it will just drop | |
41 // its reference to it, and it may do so at any time. | |
42 // Note that this may return nullptr if accelerator is not able to provide | |
43 // any new pictures at given time. The decoder is expected to handle | |
44 // this situation as normal and return from Decode() with kRanOutOfSurfaces. | |
45 virtual scoped_refptr<H264Picture> CreateH264Picture() = 0; | |
46 | |
47 // Submit metadata for the current frame, providing the current |sps| and | |
48 // |pps| for it, |dpb| has to contain all the pictures in DPB for current | |
49 // frame, and |ref_pic_p0/b0/b1| as specified in the H264 spec. Note that | |
50 // depending on the frame type, either p0, or b0 and b1 are used. |pic| | |
51 // contains information about the picture for the current frame. | |
52 // Note that this does not run decode in the accelerator and the decoder | |
53 // is expected to follow this call with one or more SubmitSlice() calls | |
54 // before calling SubmitDecode(). | |
55 // Return true if successful. | |
56 virtual bool SubmitFrameMetadata(const media::H264SPS* sps, | |
57 const media::H264PPS* pps, | |
58 const H264DPB& dpb, | |
59 const H264Picture::Vector& ref_pic_listp0, | |
60 const H264Picture::Vector& ref_pic_listb0, | |
61 const H264Picture::Vector& ref_pic_listb1, | |
62 const scoped_refptr<H264Picture>& pic) = 0; | |
63 | |
64 // Submit one slice for the current frame, passing the current |pps| and | |
65 // |pic| (same as in SubmitFrameMetadata()), the parsed header for the | |
66 // current slice in |slice_hdr|, and the reordered |ref_pic_listX|, | |
67 // as per H264 spec. | |
68 // |data| pointing to the full slice (including the unparsed header| of | |
69 // |size| in bytes. | |
70 // This must be called one or more times per frame, before SubmitDecode(). | |
71 // Note that |data| does not have to remain valid after this call returns. | |
72 // Return true if successful. | |
73 virtual bool SubmitSlice(const media::H264PPS* pps, | |
74 const media::H264SliceHeader* slice_hdr, | |
75 const H264Picture::Vector& ref_pic_list0, | |
76 const H264Picture::Vector& ref_pic_list1, | |
77 const scoped_refptr<H264Picture>& pic, | |
78 const uint8_t* data, | |
79 size_t size) = 0; | |
80 | |
81 // Execute the decode in hardware for |pic|, using all the slices and | |
82 // metadata submitted via SubmitFrameMetadata() and SubmitSlice() since | |
83 // the previous call to SubmitDecode(). | |
84 // Return true if successful. | |
85 virtual bool SubmitDecode(const scoped_refptr<H264Picture>& pic) = 0; | |
86 | |
87 // Schedule output (display) of |pic|. Note that returning from this | |
88 // method does not mean that |pic| has already been outputted (displayed), | |
89 // but guarantees that all pictures will be outputted in the same order | |
90 // as this method was called for them. Decoder may drop its reference | |
91 // to |pic| after calling this method. | |
92 // Return true if successful. | |
93 virtual bool OutputPicture(const scoped_refptr<H264Picture>& pic) = 0; | |
94 | |
95 // Reset any current state that may be cached in the accelerator, dropping | |
96 // any cached parameters/slices that have not been committed yet. | |
97 virtual void Reset() = 0; | |
98 | |
99 private: | |
100 DISALLOW_COPY_AND_ASSIGN(H264Accelerator); | |
101 }; | |
102 | |
103 H264Decoder(H264Accelerator* accelerator); | |
104 ~H264Decoder() override; | |
105 | |
106 // content::AcceleratedVideoDecoder implementation. | |
107 bool Flush() override WARN_UNUSED_RESULT; | |
108 void Reset() override; | |
109 void SetStream(const uint8_t* ptr, size_t size) override; | |
110 DecodeResult Decode() override WARN_UNUSED_RESULT; | |
111 gfx::Size GetPicSize() const override; | |
112 size_t GetRequiredNumOfPictures() const override; | |
113 | |
114 private: | |
115 // We need to keep at most kDPBMaxSize pictures in DPB for | |
116 // reference/to display later and an additional one for the one currently | |
117 // being decoded. We also ask for some additional ones since VDA needs | |
118 // to accumulate a few ready-to-output pictures before it actually starts | |
119 // displaying and giving them back. +2 instead of +1 because of subjective | |
120 // smoothness improvement during testing. | |
121 enum { | |
122 kPicsInPipeline = media::limits::kMaxVideoFrames + 2, | |
123 kMaxNumReqPictures = H264DPB::kDPBMaxSize + kPicsInPipeline, | |
124 }; | |
125 | |
126 // Internal state of the decoder. | |
127 enum State { | |
128 kNeedStreamMetadata, // After initialization, need an SPS. | |
129 kDecoding, // Ready to decode from any point. | |
130 kAfterReset, // After Reset(), need a resume point. | |
131 kError, // Error in decode, can't continue. | |
132 }; | |
133 | |
134 // Process H264 stream structures. | |
135 bool ProcessSPS(int sps_id, bool* need_new_buffers); | |
136 // Process current slice header to discover if we need to start a new picture, | |
137 // finishing up the current one. | |
138 bool PreprocessCurrentSlice(); | |
139 // Process current slice as a slice of the current picture. | |
140 bool ProcessCurrentSlice(); | |
141 | |
142 // Return true if we need to start a new picture. | |
143 bool IsNewPrimaryCodedPicture(const media::H264SliceHeader* slice_hdr) const; | |
144 | |
145 // Initialize the current picture according to data in |slice_hdr|. | |
146 bool InitCurrPicture(const media::H264SliceHeader* slice_hdr); | |
147 | |
148 // Initialize |pic| as a "non-existing" picture (see spec) with |frame_num|, | |
149 // to be used for frame gap concealment. | |
150 bool InitNonexistingPicture(scoped_refptr<H264Picture> pic, int frame_num); | |
151 | |
152 // Calculate picture order counts for |pic| on initialization | |
153 // of a new frame (see spec). | |
154 bool CalculatePicOrderCounts(scoped_refptr<H264Picture> pic); | |
155 | |
156 // Update PicNum values in pictures stored in DPB on creation of | |
157 // a picture with |frame_num|. | |
158 void UpdatePicNums(int frame_num); | |
159 | |
160 bool UpdateMaxNumReorderFrames(const media::H264SPS* sps); | |
161 | |
162 // Prepare reference picture lists for the current frame. | |
163 void PrepareRefPicLists(const media::H264SliceHeader* slice_hdr); | |
164 // Prepare reference picture lists for the given slice. | |
165 bool ModifyReferencePicLists(const media::H264SliceHeader* slice_hdr, | |
166 H264Picture::Vector* ref_pic_list0, | |
167 H264Picture::Vector* ref_pic_list1); | |
168 | |
169 // Construct initial reference picture lists for use in decoding of | |
170 // P and B pictures (see 8.2.4 in spec). | |
171 void ConstructReferencePicListsP(const media::H264SliceHeader* slice_hdr); | |
172 void ConstructReferencePicListsB(const media::H264SliceHeader* slice_hdr); | |
173 | |
174 // Helper functions for reference list construction, per spec. | |
175 int PicNumF(const scoped_refptr<H264Picture>& pic); | |
176 int LongTermPicNumF(const scoped_refptr<H264Picture>& pic); | |
177 | |
178 // Perform the reference picture lists' modification (reordering), as | |
179 // specified in spec (8.2.4). | |
180 // | |
181 // |list| indicates list number and should be either 0 or 1. | |
182 bool ModifyReferencePicList(const media::H264SliceHeader* slice_hdr, | |
183 int list, | |
184 H264Picture::Vector* ref_pic_listx); | |
185 | |
186 // Perform reference picture memory management operations (marking/unmarking | |
187 // of reference pictures, long term picture management, discarding, etc.). | |
188 // See 8.2.5 in spec. | |
189 bool HandleMemoryManagementOps(scoped_refptr<H264Picture> pic); | |
190 bool ReferencePictureMarking(scoped_refptr<H264Picture> pic); | |
191 bool SlidingWindowPictureMarking(); | |
192 | |
193 // Handle a gap in frame_num in the stream up to |frame_num|, by creating | |
194 // "non-existing" pictures (see spec). | |
195 bool HandleFrameNumGap(int frame_num); | |
196 | |
197 // Start processing a new frame. | |
198 bool StartNewFrame(const media::H264SliceHeader* slice_hdr); | |
199 | |
200 // All data for a frame received, process it and decode. | |
201 bool FinishPrevFrameIfPresent(); | |
202 | |
203 // Called after we are done processing |pic|. Performs all operations to be | |
204 // done after decoding, including DPB management, reference picture marking | |
205 // and memory management operations. | |
206 // This will also output pictures if any have become ready to be outputted | |
207 // after processing |pic|. | |
208 bool FinishPicture(scoped_refptr<H264Picture> pic); | |
209 | |
210 // Clear DPB contents and remove all surfaces in DPB from *in_use_ list. | |
211 // Cleared pictures will be made available for decode, unless they are | |
212 // at client waiting to be displayed. | |
213 void ClearDPB(); | |
214 | |
215 // Commits all pending data for HW decoder and starts HW decoder. | |
216 bool DecodePicture(); | |
217 | |
218 // Notifies client that a picture is ready for output. | |
219 void OutputPic(scoped_refptr<H264Picture> pic); | |
220 | |
221 // Output all pictures in DPB that have not been outputted yet. | |
222 bool OutputAllRemainingPics(); | |
223 | |
224 // Decoder state. | |
225 State state_; | |
226 | |
227 // Parser in use. | |
228 media::H264Parser parser_; | |
229 | |
230 // DPB in use. | |
231 H264DPB dpb_; | |
232 | |
233 // Picture currently being processed/decoded. | |
234 scoped_refptr<H264Picture> curr_pic_; | |
235 | |
236 // Reference picture lists, constructed for each frame. | |
237 H264Picture::Vector ref_pic_list_p0_; | |
238 H264Picture::Vector ref_pic_list_b0_; | |
239 H264Picture::Vector ref_pic_list_b1_; | |
240 | |
241 // Global state values, needed in decoding. See spec. | |
242 int max_frame_num_; | |
243 int max_pic_num_; | |
244 int max_long_term_frame_idx_; | |
245 size_t max_num_reorder_frames_; | |
246 | |
247 int prev_frame_num_; | |
248 int prev_ref_frame_num_; | |
249 int prev_frame_num_offset_; | |
250 bool prev_has_memmgmnt5_; | |
251 | |
252 // Values related to previously decoded reference picture. | |
253 bool prev_ref_has_memmgmnt5_; | |
254 int prev_ref_top_field_order_cnt_; | |
255 int prev_ref_pic_order_cnt_msb_; | |
256 int prev_ref_pic_order_cnt_lsb_; | |
257 H264Picture::Field prev_ref_field_; | |
258 | |
259 // Currently active SPS and PPS. | |
260 int curr_sps_id_; | |
261 int curr_pps_id_; | |
262 | |
263 // Current NALU and slice header being processed. | |
264 std::unique_ptr<media::H264NALU> curr_nalu_; | |
265 std::unique_ptr<media::H264SliceHeader> curr_slice_hdr_; | |
266 | |
267 // Output picture size. | |
268 gfx::Size pic_size_; | |
269 | |
270 // PicOrderCount of the previously outputted frame. | |
271 int last_output_poc_; | |
272 | |
273 H264Accelerator* accelerator_; | |
274 | |
275 DISALLOW_COPY_AND_ASSIGN(H264Decoder); | |
276 }; | |
277 | |
278 } // namespace content | |
279 | |
280 #endif // CONTENT_COMMON_GPU_MEDIA_H264_DECODER_H_ | |
OLD | NEW |