webrtc視頻幀率控制算法機制(二)--目標(biāo)幀率丟幀

前言

上一篇文章的丟幀是依據(jù)編碼后的碼率和目標(biāo)碼率來決定丟幀,

http://www.reibang.com/p/e2a9740b9877

而本文介紹的丟幀依據(jù)是目標(biāo)幀率酿箭。

由此可對丟幀策略分類如下:
  • 編碼后的碼率和目標(biāo)碼率來決定丟幀
  • 目標(biāo)幀率決定丟幀

調(diào)用流程

前處理模塊調(diào)用丟幀處理

ViEEncoder::DeliverFrame
->int32_t VideoProcessingModuleImpl::PreprocessFrame
->int32_t VPMFramePreprocessor::PreprocessFrame

PreprocessFrame前處理先進行丟幀處理,然后進行重采樣處理葡兑。丟幀處理中没咙,每來一幀先進行輸入幀率統(tǒng)計,然后進行丟幀判斷以蕴。輸入幀率統(tǒng)計和上一篇碼率決定丟幀輸入丟幀統(tǒng)計算法一樣糙麦。

int32_t VPMFramePreprocessor::PreprocessFrame(const I420VideoFrame& frame,
    I420VideoFrame** processed_frame) {
  if (frame.IsZeroSize()) {
    return VPM_PARAMETER_ERROR;
  }

  vd_->UpdateIncomingframe_rate(); //更新輸入幀率

  if (vd_->DropFrame()) {//判斷是否丟幀
    return 1;  // drop 1 frame
  }

  // Resizing incoming frame if needed. Otherwise, remains NULL.
  // We are not allowed to resample the input frame (must make a copy of it).
  *processed_frame = NULL;
  if (spatial_resampler_->ApplyResample(frame.width(), frame.height()))  {
    int32_t ret = spatial_resampler_->ResampleFrame(frame, &resampled_frame_);
    if (ret != VPM_OK) return ret;
    *processed_frame = &resampled_frame_;
  }

  // Perform content analysis on the frame to be encoded.
  if (enable_ca_) {
    // Compute new metrics every |kSkipFramesCA| frames, starting with
    // the first frame.
    if (frame_cnt_ % kSkipFrameCA == 0) {
      if (*processed_frame == NULL)  {
        content_metrics_ = ca_->ComputeContentMetrics(frame);
      } else {
        content_metrics_ = ca_->ComputeContentMetrics(resampled_frame_);
      }
    }
    ++frame_cnt_;
  }
  return VPM_OK;
}```

##輸入幀率統(tǒng)計
每來一幀都將此時的時間作為樣本,記錄在滑動窗口為kFrameCountHistory_size的incoming_frame_times_數(shù)組中丛肮。并進行ProcessIncomingframe_rate輸入幀率的計算赡磅。

void VPMVideoDecimator::UpdateIncomingframe_rate() {
int64_t now = TickTime::MillisecondTimestamp();
if (incoming_frame_times_[0] == 0) {
// First no shift.
} else {
// Shift.
for (int i = kFrameCountHistory_size - 2; i >= 0; i--) {
incoming_frame_times_[i+1] = incoming_frame_times_[i];
}
}
incoming_frame_times_[0] = now;
ProcessIncomingframe_rate(now);
}```

統(tǒng)計最多不超過2秒鐘的樣本,計算輸入幀率腾供。

void VPMVideoDecimator::ProcessIncomingframe_rate(int64_t now) {
  int32_t num = 0;
  int32_t nrOfFrames = 0;
  for (num = 1; num < (kFrameCountHistory_size - 1); num++) {
    // Don't use data older than 2sec.
    if (incoming_frame_times_[num] <= 0 ||
        now - incoming_frame_times_[num] > kFrameHistoryWindowMs) {
      break;
    } else {
      nrOfFrames++;
    }
  }
  if (num > 1) {
    int64_t diff = now - incoming_frame_times_[num-1];
    incoming_frame_rate_ = 1.0;
    if (diff > 0) {
      incoming_frame_rate_ = nrOfFrames * 1000.0f / static_cast<float>(diff);
    }
  } else {
    incoming_frame_rate_ = static_cast<float>(nrOfFrames);
  }
}```

##目標(biāo)幀率丟幀核心
一仆邓、當(dāng)2 * overshoot < (int32_t) incomingframe_rate,即輸入幀率大于目標(biāo)幀率伴鳖,小于2倍目標(biāo)幀率的情況下节值。具體細節(jié)看注釋,這里假設(shè)incomingframe_rate=20榜聂,target_frame_rate_=15搞疗。具體就是實現(xiàn)均勻丟幀。

bool VPMVideoDecimator::DropFrame() {
if (!enable_temporal_decimation_) return false;

if (incoming_frame_rate_ <= 0) return false;

const uint32_t incomingframe_rate =
static_cast<uint32_t>(incoming_frame_rate_ + 0.5f);

if (target_frame_rate_ == 0) return true;

bool drop = false;
if (incomingframe_rate > target_frame_rate_) {//輸入幀率大于目標(biāo)幀率
int32_t overshoot =
overshoot_modifier_ + (incomingframe_rate - target_frame_rate_);//20-15=5须肆,超出幀率
if (overshoot < 0) {
overshoot = 0;
overshoot_modifier_ = 0;
}

if (overshoot && 2 * overshoot < (int32_t) incomingframe_rate) {//2*5<20,即20<2*15匿乃,輸入幀率小于2倍目標(biāo)幀率
  if (drop_count_) {  // Just got here so drop to be sure.
      drop_count_ = 0;
      return true;
  }
  const uint32_t dropVar = incomingframe_rate / overshoot;//20/5=4,丟幀比率

  if (keep_count_ >= dropVar) {//均勻丟幀
      drop = true;
      overshoot_modifier_ = -((int32_t) incomingframe_rate % overshoot) / 3;//-(20%5)/3=0豌汇,修正overshoot
      keep_count_ = 1;//重置保留幀數(shù)
  } else {
      keep_count_++;//保留幀數(shù)
  }
} else {
  keep_count_ = 0;
  const uint32_t dropVar = overshoot / target_frame_rate_;
  if (drop_count_ < dropVar) {
      drop = true;
      drop_count_++;
  } else {
      overshoot_modifier_ = overshoot % target_frame_rate_;
      drop = false;
      drop_count_ = 0;
  }
}

}
return drop;
}```

二幢炸、當(dāng)2 * overshoot >=(int32_t) incomingframe_rate時,即輸入幀率大于等于2倍目標(biāo)幀率拒贱,此時宛徊,均勻丟幀每次丟1幀以上,具體丟幀方法和上一部分略有區(qū)別逻澳。

bool VPMVideoDecimator::DropFrame() {
  if (!enable_temporal_decimation_) return false;

  if (incoming_frame_rate_ <= 0) return false;

  const uint32_t incomingframe_rate =
      static_cast<uint32_t>(incoming_frame_rate_ + 0.5f);

  if (target_frame_rate_ == 0) return true;

  bool drop = false;
  if (incomingframe_rate > target_frame_rate_) {//輸入幀率大于目標(biāo)幀率
    int32_t overshoot =
        overshoot_modifier_ + (incomingframe_rate - target_frame_rate_); //30-10=20闸天,超出幀率
    if (overshoot < 0) {
      overshoot = 0;
      overshoot_modifier_ = 0;
    }

    if (overshoot && 2 * overshoot < (int32_t) incomingframe_rate) {  //2*20>30,即30>2*10斜做,輸入幀率大于2倍目標(biāo)幀率
      if (drop_count_) {  // Just got here so drop to be sure.
          drop_count_ = 0;
          return true;
      }
      const uint32_t dropVar = incomingframe_rate / overshoot;

      if (keep_count_ >= dropVar) {
          drop = true;
          overshoot_modifier_ = -((int32_t) incomingframe_rate % overshoot) / 3;
          keep_count_ = 1;
      } else {
          keep_count_++;
      }
    } else {
      keep_count_ = 0;
      const uint32_t dropVar = overshoot / target_frame_rate_; //20/10=2苞氮,丟幀比率
      if (drop_count_ < dropVar) {//一次丟1幀以上
          drop = true;
          drop_count_++;
      } else {
          overshoot_modifier_ = overshoot % target_frame_rate_;  //20%10=0,overshoot修正
          drop = false;
          drop_count_ = 0;
      }
    }
  }
  return drop;
}```
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末瓤逼,一起剝皮案震驚了整個濱河市笼吟,隨后出現(xiàn)的幾起案子库物,更是在濱河造成了極大的恐慌,老刑警劉巖贷帮,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件艳狐,死亡現(xiàn)場離奇詭異,居然都是意外死亡皿桑,警方通過查閱死者的電腦和手機毫目,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來诲侮,“玉大人镀虐,你說我怎么就攤上這事」敌鳎” “怎么了刮便?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長绽慈。 經(jīng)常有香客問我恨旱,道長,這世上最難降的妖魔是什么坝疼? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任搜贤,我火速辦了婚禮,結(jié)果婚禮上钝凶,老公的妹妹穿的比我還像新娘仪芒。我一直安慰自己,他們只是感情好耕陷,可當(dāng)我...
    茶點故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布掂名。 她就那樣靜靜地躺著,像睡著了一般哟沫。 火紅的嫁衣襯著肌膚如雪饺蔑。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天嗜诀,我揣著相機與錄音猾警,去河邊找鬼。 笑死裹虫,一個胖子當(dāng)著我的面吹牛肿嘲,可吹牛的內(nèi)容都是我干的融击。 我是一名探鬼主播筑公,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼尊浪!你這毒婦竟也來了匣屡?” 一聲冷哼從身側(cè)響起封救,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎捣作,沒想到半個月后誉结,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡券躁,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年惩坑,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片也拜。...
    茶點故事閱讀 40,852評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡以舒,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出慢哈,到底是詐尸還是另有隱情蔓钟,我是刑警寧澤,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布卵贱,位于F島的核電站滥沫,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏键俱。R本人自食惡果不足惜兰绣,卻給世界環(huán)境...
    茶點故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望编振。 院中可真熱鬧狭魂,春花似錦、人聲如沸党觅。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽杯瞻。三九已至镐牺,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間魁莉,已是汗流浹背睬涧。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留旗唁,地道東北人畦浓。 一個月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像检疫,于是被迫代替她去往敵國和親讶请。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,851評論 2 361

推薦閱讀更多精彩內(nèi)容