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