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