Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / common / gpu / media / h264_dpb.cc
blob1220d591fc545d04e9bbc634d4d29e94346066cf
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.
5 #include <algorithm>
7 #include "base/logging.h"
8 #include "base/stl_util.h"
9 #include "content/common/gpu/media/h264_dpb.h"
11 namespace content {
13 H264Picture::H264Picture()
14 : top_field_order_cnt(0),
15 bottom_field_order_cnt(0),
16 pic_order_cnt(0),
17 pic_order_cnt_msb(0),
18 pic_order_cnt_lsb(0),
19 pic_num(0),
20 long_term_pic_num(0),
21 frame_num(0),
22 frame_num_offset(0),
23 frame_num_wrap(0),
24 long_term_frame_idx(0),
25 type(media::H264SliceHeader::kPSlice),
26 idr(false),
27 ref(false),
28 long_term(false),
29 outputted(false),
30 mem_mgmt_5(false),
31 field(FIELD_NONE),
32 long_term_reference_flag(false),
33 adaptive_ref_pic_marking_mode_flag(false),
34 dpb_position(0) {
35 memset(&ref_pic_marking, 0, sizeof(ref_pic_marking));
38 H264Picture::~H264Picture() {
41 V4L2H264Picture* H264Picture::AsV4L2H264Picture() {
42 return nullptr;
45 VaapiH264Picture* H264Picture::AsVaapiH264Picture() {
46 return nullptr;
49 H264DPB::H264DPB() : max_num_pics_(0) {}
50 H264DPB::~H264DPB() {}
52 void H264DPB::Clear() {
53 pics_.clear();
56 void H264DPB::set_max_num_pics(size_t max_num_pics) {
57 DCHECK_LE(max_num_pics, kDPBMaxSize);
58 max_num_pics_ = max_num_pics;
59 if (pics_.size() > max_num_pics_)
60 pics_.resize(max_num_pics_);
63 void H264DPB::UpdatePicPositions() {
64 size_t i = 0;
65 for (auto& pic : pics_) {
66 pic->dpb_position = i;
67 ++i;
71 void H264DPB::DeleteByPOC(int poc) {
72 for (H264Picture::Vector::iterator it = pics_.begin();
73 it != pics_.end(); ++it) {
74 if ((*it)->pic_order_cnt == poc) {
75 pics_.erase(it);
76 UpdatePicPositions();
77 return;
80 NOTREACHED() << "Missing POC: " << poc;
83 void H264DPB::DeleteUnused() {
84 for (H264Picture::Vector::iterator it = pics_.begin(); it != pics_.end(); ) {
85 if ((*it)->outputted && !(*it)->ref)
86 it = pics_.erase(it);
87 else
88 ++it;
90 UpdatePicPositions();
93 void H264DPB::StorePic(const scoped_refptr<H264Picture>& pic) {
94 DCHECK_LT(pics_.size(), max_num_pics_);
95 DVLOG(3) << "Adding PicNum: " << pic->pic_num << " ref: " << (int)pic->ref
96 << " longterm: " << (int)pic->long_term << " to DPB";
97 pic->dpb_position = pics_.size();
98 pics_.push_back(pic);
101 int H264DPB::CountRefPics() {
102 int ret = 0;
103 for (size_t i = 0; i < pics_.size(); ++i) {
104 if (pics_[i]->ref)
105 ++ret;
107 return ret;
110 void H264DPB::MarkAllUnusedForRef() {
111 for (size_t i = 0; i < pics_.size(); ++i)
112 pics_[i]->ref = false;
115 scoped_refptr<H264Picture> H264DPB::GetShortRefPicByPicNum(int pic_num) {
116 for (const auto& pic : pics_) {
117 if (pic->ref && !pic->long_term && pic->pic_num == pic_num)
118 return pic;
121 DVLOG(1) << "Missing short ref pic num: " << pic_num;
122 return nullptr;
125 scoped_refptr<H264Picture> H264DPB::GetLongRefPicByLongTermPicNum(int pic_num) {
126 for (const auto& pic : pics_) {
127 if (pic->ref && pic->long_term && pic->long_term_pic_num == pic_num)
128 return pic;
131 DVLOG(1) << "Missing long term pic num: " << pic_num;
132 return nullptr;
135 scoped_refptr<H264Picture> H264DPB::GetLowestFrameNumWrapShortRefPic() {
136 scoped_refptr<H264Picture> ret;
137 for (const auto& pic : pics_) {
138 if (pic->ref && !pic->long_term &&
139 (!ret || pic->frame_num_wrap < ret->frame_num_wrap))
140 ret = pic;
142 return ret;
145 void H264DPB::GetNotOutputtedPicsAppending(H264Picture::Vector* out) {
146 for (const auto& pic : pics_) {
147 if (!pic->outputted)
148 out->push_back(pic);
152 void H264DPB::GetShortTermRefPicsAppending(H264Picture::Vector* out) {
153 for (const auto& pic : pics_) {
154 if (pic->ref && !pic->long_term)
155 out->push_back(pic);
159 void H264DPB::GetLongTermRefPicsAppending(H264Picture::Vector* out) {
160 for (const auto& pic : pics_) {
161 if (pic->ref && pic->long_term)
162 out->push_back(pic);
166 } // namespace content