• 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 // This file contains an implementation of an H.264 Decoded Picture Buffer
6 // used in H264 decoders.
7 
8 #ifndef H264_DPB_H_
9 #define H264_DPB_H_
10 
11 #include <stddef.h>
12 
13 #include <vector>
14 
15 #include "base/macros.h"
16 #include "base/memory/ref_counted.h"
17 #include "h264_parser.h"
18 
19 namespace media {
20 
21 class V4L2H264Picture;
22 
23 // A picture (a frame or a field) in the H.264 spec sense.
24 // See spec at http://www.itu.int/rec/T-REC-H.264
25 class H264Picture : public base::RefCounted<H264Picture> {
26  public:
27   using Vector = std::vector<scoped_refptr<H264Picture>>;
28 
29   enum Field {
30     FIELD_NONE,
31     FIELD_TOP,
32     FIELD_BOTTOM,
33   };
34 
35   H264Picture();
36 
37   virtual V4L2H264Picture* AsV4L2H264Picture();
38 
39   // Values calculated per H.264 specification or taken from slice header.
40   // See spec for more details on each (some names have been converted from
41   // CamelCase in spec to Chromium-style names).
42   int pic_order_cnt_type;
43   int top_field_order_cnt;
44   int bottom_field_order_cnt;
45   int pic_order_cnt;
46   int pic_order_cnt_msb;
47   int pic_order_cnt_lsb;
48   int delta_pic_order_cnt_bottom;
49   int delta_pic_order_cnt0;
50   int delta_pic_order_cnt1;
51 
52   int pic_num;
53   int long_term_pic_num;
54   int frame_num;  // from slice header
55   int frame_num_offset;
56   int frame_num_wrap;
57   int long_term_frame_idx;
58 
59   H264SliceHeader::Type type;
60   int nal_ref_idc;
61   bool idr;        // IDR picture?
62   int idr_pic_id;  // Valid only if idr == true.
63   bool ref;        // reference picture?
64   bool long_term;  // long term reference picture?
65   bool outputted;
66   // Does memory management op 5 needs to be executed after this
67   // picture has finished decoding?
68   bool mem_mgmt_5;
69 
70   // Created by the decoding process for gaps in frame_num.
71   // Not for decode or output.
72   bool nonexisting;
73 
74   Field field;
75 
76   // Values from slice_hdr to be used during reference marking and
77   // memory management after finishing this picture.
78   bool long_term_reference_flag;
79   bool adaptive_ref_pic_marking_mode_flag;
80   H264DecRefPicMarking ref_pic_marking[H264SliceHeader::kRefListSize];
81 
82   // Position in DPB (i.e. index in DPB).
83   int dpb_position;
84 
85  protected:
86   friend class base::RefCounted<H264Picture>;
87   virtual ~H264Picture();
88 
89  private:
90   DISALLOW_COPY_AND_ASSIGN(H264Picture);
91 };
92 
93 // DPB - Decoded Picture Buffer.
94 // Stores decoded pictures that will be used for future display
95 // and/or reference.
96 class H264DPB {
97  public:
98   H264DPB();
99   ~H264DPB();
100 
101   void set_max_num_pics(size_t max_num_pics);
max_num_pics()102   size_t max_num_pics() const { return max_num_pics_; }
103 
104   // Remove unused (not reference and already outputted) pictures from DPB
105   // and free it.
106   void DeleteUnused();
107 
108   // Remove a picture by its pic_order_cnt and free it.
109   void DeleteByPOC(int poc);
110 
111   // Clear DPB.
112   void Clear();
113 
114   // Store picture in DPB. DPB takes ownership of its resources.
115   void StorePic(const scoped_refptr<H264Picture>& pic);
116 
117   // Return the number of reference pictures in DPB.
118   int CountRefPics();
119 
120   // Mark all pictures in DPB as unused for reference.
121   void MarkAllUnusedForRef();
122 
123   // Return a short-term reference picture by its pic_num.
124   scoped_refptr<H264Picture> GetShortRefPicByPicNum(int pic_num);
125 
126   // Return a long-term reference picture by its long_term_pic_num.
127   scoped_refptr<H264Picture> GetLongRefPicByLongTermPicNum(int pic_num);
128 
129   // Return the short reference picture with lowest frame_num. Used for sliding
130   // window memory management.
131   scoped_refptr<H264Picture> GetLowestFrameNumWrapShortRefPic();
132 
133   // Append all pictures that have not been outputted yet to the passed |out|
134   // vector, sorted by lowest pic_order_cnt (in output order).
135   void GetNotOutputtedPicsAppending(H264Picture::Vector* out);
136 
137   // Append all short term reference pictures to the passed |out| vector.
138   void GetShortTermRefPicsAppending(H264Picture::Vector* out);
139 
140   // Append all long term reference pictures to the passed |out| vector.
141   void GetLongTermRefPicsAppending(H264Picture::Vector* out);
142 
143   // Iterators for direct access to DPB contents.
144   // Will be invalidated after any of Remove* calls.
begin()145   H264Picture::Vector::iterator begin() { return pics_.begin(); }
end()146   H264Picture::Vector::iterator end() { return pics_.end(); }
begin()147   H264Picture::Vector::const_iterator begin() const { return pics_.begin(); }
end()148   H264Picture::Vector::const_iterator end() const { return pics_.end(); }
rbegin()149   H264Picture::Vector::const_reverse_iterator rbegin() const {
150     return pics_.rbegin();
151   }
rend()152   H264Picture::Vector::const_reverse_iterator rend() const {
153     return pics_.rend();
154   }
155 
size()156   size_t size() const { return pics_.size(); }
IsFull()157   bool IsFull() const { return pics_.size() == max_num_pics_; }
158 
159   // Per H264 spec, increase to 32 if interlaced video is supported.
160   enum {
161     kDPBMaxSize = 16,
162   };
163 
164  private:
165   void UpdatePicPositions();
166 
167   H264Picture::Vector pics_;
168   size_t max_num_pics_;
169 
170   DISALLOW_COPY_AND_ASSIGN(H264DPB);
171 };
172 
173 }  // namespace media
174 
175 #endif  // H264_DPB_H_
176