h264_dpb.h 5.5 KB

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