• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 H264_DECODER_H_
6 #define 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 "accelerated_video_decoder.h"
17 #include "h264_dpb.h"
18 #include "h264_parser.h"
19 #include "size.h"
20 
21 namespace media {
22 
23 // Clients of this class are expected to pass H264 Annex-B byte stream
24 // and are expected to provide an implementation of H264Accelerator for
25 // offloading final steps of the decoding process.
26 //
27 // This class must be created, called and destroyed on a single thread, and
28 // does nothing internally on any other thread.
29 class H264Decoder : public AcceleratedVideoDecoder {
30  public:
31   class H264Accelerator {
32    public:
33     H264Accelerator();
34     virtual ~H264Accelerator();
35 
36     // Create a new H264Picture that the decoder client can use for decoding
37     // and pass back to this accelerator for decoding or reference.
38     // When the picture is no longer needed by decoder, it will just drop
39     // its reference to it, and it may do so at any time.
40     // Note that this may return nullptr if accelerator is not able to provide
41     // any new pictures at given time. The decoder is expected to handle
42     // this situation as normal and return from Decode() with kRanOutOfSurfaces.
43     virtual scoped_refptr<H264Picture> CreateH264Picture() = 0;
44 
45     // Submit metadata for the current frame, providing the current |sps| and
46     // |pps| for it, |dpb| has to contain all the pictures in DPB for current
47     // frame, and |ref_pic_p0/b0/b1| as specified in the H264 spec. Note that
48     // depending on the frame type, either p0, or b0 and b1 are used. |pic|
49     // contains information about the picture for the current frame.
50     // Note that this does not run decode in the accelerator and the decoder
51     // is expected to follow this call with one or more SubmitSlice() calls
52     // before calling SubmitDecode().
53     // Return true if successful.
54     virtual bool SubmitFrameMetadata(const H264SPS* sps,
55                                      const H264PPS* pps,
56                                      const H264DPB& dpb,
57                                      const H264Picture::Vector& ref_pic_listp0,
58                                      const H264Picture::Vector& ref_pic_listb0,
59                                      const H264Picture::Vector& ref_pic_listb1,
60                                      const scoped_refptr<H264Picture>& pic) = 0;
61 
62     // Submit one slice for the current frame, passing the current |pps| and
63     // |pic| (same as in SubmitFrameMetadata()), the parsed header for the
64     // current slice in |slice_hdr|, and the reordered |ref_pic_listX|,
65     // as per H264 spec.
66     // |data| pointing to the full slice (including the unparsed header| of
67     // |size| in bytes.
68     // This must be called one or more times per frame, before SubmitDecode().
69     // Note that |data| does not have to remain valid after this call returns.
70     // Return true if successful.
71     virtual bool SubmitSlice(const H264PPS* pps,
72                              const H264SliceHeader* slice_hdr,
73                              const H264Picture::Vector& ref_pic_list0,
74                              const H264Picture::Vector& ref_pic_list1,
75                              const scoped_refptr<H264Picture>& pic,
76                              const uint8_t* data,
77                              size_t size) = 0;
78 
79     // Execute the decode in hardware for |pic|, using all the slices and
80     // metadata submitted via SubmitFrameMetadata() and SubmitSlice() since
81     // the previous call to SubmitDecode().
82     // Return true if successful.
83     virtual bool SubmitDecode(const scoped_refptr<H264Picture>& pic) = 0;
84 
85     // Schedule output (display) of |pic|. Note that returning from this
86     // method does not mean that |pic| has already been outputted (displayed),
87     // but guarantees that all pictures will be outputted in the same order
88     // as this method was called for them. Decoder may drop its reference
89     // to |pic| after calling this method.
90     // Return true if successful.
91     virtual bool OutputPicture(const scoped_refptr<H264Picture>& pic) = 0;
92 
93     // Reset any current state that may be cached in the accelerator, dropping
94     // any cached parameters/slices that have not been committed yet.
95     virtual void Reset() = 0;
96 
97    private:
98     DISALLOW_COPY_AND_ASSIGN(H264Accelerator);
99   };
100 
101   H264Decoder(H264Accelerator* accelerator);
102   ~H264Decoder() override;
103 
104   // AcceleratedVideoDecoder implementation.
105   bool Flush() override WARN_UNUSED_RESULT;
106   void Reset() override;
107   void SetStream(const uint8_t* ptr, size_t size) override;
108   DecodeResult Decode() override WARN_UNUSED_RESULT;
109   Size GetPicSize() const override;
110   size_t GetRequiredNumOfPictures() const override;
111 
112  private:
113   // We need to keep at most kDPBMaxSize pictures in DPB for
114   // reference/to display later and an additional one for the one currently
115   // being decoded. We also ask for some additional ones since VDA needs
116   // to accumulate a few ready-to-output pictures before it actually starts
117   // displaying and giving them back. +2 instead of +1 because of subjective
118   // smoothness improvement during testing.
119   enum {
120     // TODO(johnylin): see if we could get rid of kMaxVideoFrames.
121     kMaxVideoFrames = 4,
122     kPicsInPipeline = 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 H264SliceHeader* slice_hdr) const;
144 
145   // Initialize the current picture according to data in |slice_hdr|.
146   bool InitCurrPicture(const 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 H264SPS* sps);
161 
162   // Prepare reference picture lists for the current frame.
163   void PrepareRefPicLists(const H264SliceHeader* slice_hdr);
164   // Prepare reference picture lists for the given slice.
165   bool ModifyReferencePicLists(const 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 H264SliceHeader* slice_hdr);
172   void ConstructReferencePicListsB(const 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 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 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   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<H264NALU> curr_nalu_;
265   std::unique_ptr<H264SliceHeader> curr_slice_hdr_;
266 
267   // Output picture size.
268   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 media
279 
280 #endif  // H264_DECODER_H_
281