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