1 // Copyright 2020 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 #include <algorithm>
6 
7 #include "base/logging.h"
8 #include "media/gpu/h265_dpb.h"
9 #include "media/video/h265_parser.h"
10 
11 namespace media {
12 
13 H265Picture::H265Picture() = default;
14 H265Picture::~H265Picture() = default;
15 
16 H265DPB::H265DPB() = default;
17 H265DPB::~H265DPB() = default;
18 
AsVaapiH265Picture()19 VaapiH265Picture* H265Picture::AsVaapiH265Picture() {
20   return nullptr;
21 }
22 
set_max_num_pics(size_t max_num_pics)23 void H265DPB::set_max_num_pics(size_t max_num_pics) {
24   DCHECK_LE(max_num_pics, static_cast<size_t>(kMaxDpbSize));
25   max_num_pics_ = max_num_pics;
26   if (pics_.size() > max_num_pics_)
27     pics_.resize(max_num_pics_);
28 }
29 
Clear()30 void H265DPB::Clear() {
31   pics_.clear();
32 }
33 
StorePicture(scoped_refptr<H265Picture> pic,H265Picture::ReferenceType ref)34 void H265DPB::StorePicture(scoped_refptr<H265Picture> pic,
35                            H265Picture::ReferenceType ref) {
36   DCHECK_LT(pics_.size(), max_num_pics_);
37   pic->ref_ = ref;
38   DVLOG(3) << "Adding PicNum: " << pic->pic_order_cnt_val_
39            << " ref: " << static_cast<int>(pic->ref_);
40   pics_.push_back(std::move(pic));
41 }
42 
MarkAllUnusedForReference()43 void H265DPB::MarkAllUnusedForReference() {
44   for (const auto& pic : pics_)
45     pic->ref_ = H265Picture::kUnused;
46 }
47 
DeleteUnused()48 void H265DPB::DeleteUnused() {
49   for (auto it = pics_.begin(); it != pics_.end();) {
50     auto& pic = *it;
51     if ((!pic->pic_output_flag_ || pic->outputted_) &&
52         (pic->ref_ == H265Picture::kUnused)) {
53       std::swap(pic, *(pics_.end() - 1));
54       pics_.pop_back();
55     } else {
56       it++;
57     }
58   }
59 }
60 
GetReferencePicCount()61 int H265DPB::GetReferencePicCount() {
62   int count = 0;
63   for (const auto& pic : pics_) {
64     if (pic->ref_ != H265Picture::kUnused)
65       count++;
66   }
67   return count;
68 }
69 
GetPicByPocAndMark(int poc,H265Picture::ReferenceType ref)70 scoped_refptr<H265Picture> H265DPB::GetPicByPocAndMark(
71     int poc,
72     H265Picture::ReferenceType ref) {
73   return GetPicByPocMaskedAndMark(poc, 0, ref);
74 }
75 
GetPicByPocMaskedAndMark(int poc,int mask,H265Picture::ReferenceType ref)76 scoped_refptr<H265Picture> H265DPB::GetPicByPocMaskedAndMark(
77     int poc,
78     int mask,
79     H265Picture::ReferenceType ref) {
80   for (const auto& pic : pics_) {
81     if ((mask && (pic->pic_order_cnt_val_ & mask) == poc) ||
82         (!mask && pic->pic_order_cnt_val_ == poc)) {
83       pic->ref_ = ref;
84       return pic;
85     }
86   }
87 
88   DVLOG(1) << "Missing " << H265Picture::GetReferenceName(ref)
89            << " ref pic num: " << poc;
90   return nullptr;
91 }
92 
AppendPendingOutputPics(H265Picture::Vector * out)93 void H265DPB::AppendPendingOutputPics(H265Picture::Vector* out) {
94   for (const auto& pic : pics_) {
95     if (pic->pic_output_flag_ && !pic->outputted_)
96       out->push_back(pic);
97   }
98 }
99 
AppendReferencePics(H265Picture::Vector * out)100 void H265DPB::AppendReferencePics(H265Picture::Vector* out) {
101   for (const auto& pic : pics_) {
102     if (pic->ref_ != H265Picture::kUnused)
103       out->push_back(pic);
104   }
105 }
106 
107 }  // namespace media