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