h264_dpb.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #include <string.h>
  5. #include <algorithm>
  6. #include "base/logging.h"
  7. #include "base/notreached.h"
  8. #include "media/gpu/h264_dpb.h"
  9. namespace media {
  10. H264Picture::H264Picture()
  11. : pic_order_cnt_type(0),
  12. top_field_order_cnt(0),
  13. bottom_field_order_cnt(0),
  14. pic_order_cnt(0),
  15. pic_order_cnt_msb(0),
  16. pic_order_cnt_lsb(0),
  17. delta_pic_order_cnt_bottom(0),
  18. delta_pic_order_cnt0(0),
  19. delta_pic_order_cnt1(0),
  20. pic_num(0),
  21. long_term_pic_num(0),
  22. frame_num(0),
  23. frame_num_offset(0),
  24. frame_num_wrap(0),
  25. long_term_frame_idx(0),
  26. type(H264SliceHeader::kPSlice),
  27. nal_ref_idc(0),
  28. idr(false),
  29. idr_pic_id(0),
  30. ref(false),
  31. ref_pic_list_modification_flag_l0(0),
  32. abs_diff_pic_num_minus1(0),
  33. long_term(false),
  34. outputted(false),
  35. mem_mgmt_5(false),
  36. nonexisting(false),
  37. field(FIELD_NONE),
  38. long_term_reference_flag(false),
  39. adaptive_ref_pic_marking_mode_flag(false),
  40. dpb_position(0) {
  41. memset(&ref_pic_marking, 0, sizeof(ref_pic_marking));
  42. }
  43. H264Picture::~H264Picture() = default;
  44. V4L2H264Picture* H264Picture::AsV4L2H264Picture() {
  45. return nullptr;
  46. }
  47. VaapiH264Picture* H264Picture::AsVaapiH264Picture() {
  48. return nullptr;
  49. }
  50. D3D11H264Picture* H264Picture::AsD3D11H264Picture() {
  51. return nullptr;
  52. }
  53. H264DPB::H264DPB() : max_num_pics_(0) {}
  54. H264DPB::~H264DPB() = default;
  55. void H264DPB::Clear() {
  56. pics_.clear();
  57. }
  58. void H264DPB::set_max_num_pics(size_t max_num_pics) {
  59. DCHECK_LE(max_num_pics, static_cast<size_t>(kDPBMaxSize));
  60. max_num_pics_ = max_num_pics;
  61. if (pics_.size() > max_num_pics_)
  62. pics_.resize(max_num_pics_);
  63. }
  64. void H264DPB::UpdatePicPositions() {
  65. size_t i = 0;
  66. for (auto& pic : pics_) {
  67. pic->dpb_position = i;
  68. ++i;
  69. }
  70. }
  71. void H264DPB::Delete(scoped_refptr<H264Picture> pic) {
  72. for (auto it = pics_.begin(); it != pics_.end(); ++it) {
  73. if ((*it) == pic) {
  74. pics_.erase(it);
  75. UpdatePicPositions();
  76. return;
  77. }
  78. }
  79. NOTREACHED() << "Missing pic with POC: " << pic->pic_order_cnt;
  80. }
  81. void H264DPB::DeleteUnused() {
  82. for (auto it = pics_.begin(); it != pics_.end();) {
  83. if ((*it)->outputted && !(*it)->ref)
  84. it = pics_.erase(it);
  85. else
  86. ++it;
  87. }
  88. UpdatePicPositions();
  89. }
  90. void H264DPB::StorePic(scoped_refptr<H264Picture> pic) {
  91. DCHECK_LT(pics_.size(), max_num_pics_);
  92. DVLOG(3) << "Adding PicNum: " << pic->pic_num << " ref: " << (int)pic->ref
  93. << " longterm: " << (int)pic->long_term << " to DPB";
  94. pic->dpb_position = pics_.size();
  95. pics_.push_back(std::move(pic));
  96. }
  97. int H264DPB::CountRefPics() {
  98. int ret = 0;
  99. for (size_t i = 0; i < pics_.size(); ++i) {
  100. if (pics_[i]->ref)
  101. ++ret;
  102. }
  103. return ret;
  104. }
  105. void H264DPB::MarkAllUnusedForRef() {
  106. for (size_t i = 0; i < pics_.size(); ++i)
  107. pics_[i]->ref = false;
  108. }
  109. scoped_refptr<H264Picture> H264DPB::GetShortRefPicByPicNum(int pic_num) {
  110. for (const auto& pic : pics_) {
  111. if (pic->ref && !pic->long_term && pic->pic_num == pic_num)
  112. return pic;
  113. }
  114. DVLOG(1) << "Missing short ref pic num: " << pic_num;
  115. return nullptr;
  116. }
  117. scoped_refptr<H264Picture> H264DPB::GetLongRefPicByLongTermPicNum(int pic_num) {
  118. for (const auto& pic : pics_) {
  119. if (pic->ref && pic->long_term && pic->long_term_pic_num == pic_num)
  120. return pic;
  121. }
  122. DVLOG(1) << "Missing long term pic num: " << pic_num;
  123. return nullptr;
  124. }
  125. scoped_refptr<H264Picture> H264DPB::GetLowestFrameNumWrapShortRefPic() {
  126. scoped_refptr<H264Picture> ret;
  127. for (const auto& pic : pics_) {
  128. if (pic->ref && !pic->long_term &&
  129. (!ret || pic->frame_num_wrap < ret->frame_num_wrap))
  130. ret = pic;
  131. }
  132. return ret;
  133. }
  134. void H264DPB::GetNotOutputtedPicsAppending(H264Picture::Vector* out) {
  135. for (const auto& pic : pics_) {
  136. if (!pic->outputted)
  137. out->push_back(pic);
  138. }
  139. }
  140. void H264DPB::GetShortTermRefPicsAppending(H264Picture::Vector* out) {
  141. for (const auto& pic : pics_) {
  142. if (pic->ref && !pic->long_term)
  143. out->push_back(pic);
  144. }
  145. }
  146. void H264DPB::GetLongTermRefPicsAppending(H264Picture::Vector* out) {
  147. for (const auto& pic : pics_) {
  148. if (pic->ref && pic->long_term)
  149. out->push_back(pic);
  150. }
  151. }
  152. } // namespace media