用 libyuv 縮放 yuv420p

參考 webrtc 的實現(xiàn)

int Scaler::Scale(const VideoFrame& src_frame, VideoFrame* dst_frame) {
  assert(dst_frame);
  if (src_frame.IsZeroSize())
    return -1;
  if (!set_)
    return -2;
  // Making sure that destination frame is of sufficient size.
  dst_frame->set_video_frame_buffer(
      buffer_pool_.CreateBuffer(dst_width_, dst_height_));
  // We want to preserve aspect ratio instead of stretching the frame.
  // Therefore, we need to crop the source frame. Calculate the largest center
  // aligned region of the source frame that can be used.
  const int cropped_src_width =
      std::min(src_width_, dst_width_ * src_height_ / dst_height_);
  const int cropped_src_height =
      std::min(src_height_, dst_height_ * src_width_ / dst_width_);
  // Make sure the offsets are even to avoid rounding errors for the U/V planes.
  const int src_offset_x = ((src_width_ - cropped_src_width) / 2) & ~1;
  const int src_offset_y = ((src_height_ - cropped_src_height) / 2) & ~1;
  const uint8_t* y_ptr = src_frame.buffer(kYPlane) +
                         src_offset_y * src_frame.stride(kYPlane) +
                         src_offset_x;
  const uint8_t* u_ptr = src_frame.buffer(kUPlane) +
                         src_offset_y / 2 * src_frame.stride(kUPlane) +
                         src_offset_x / 2;
  const uint8_t* v_ptr = src_frame.buffer(kVPlane) +
                         src_offset_y / 2 * src_frame.stride(kVPlane) +
                         src_offset_x / 2;
  return libyuv::I420Scale(y_ptr,
                           src_frame.stride(kYPlane),
                           u_ptr,
                           src_frame.stride(kUPlane),
                           v_ptr,
                           src_frame.stride(kVPlane),
                           cropped_src_width, cropped_src_height,
                           dst_frame->buffer(kYPlane),
                           dst_frame->stride(kYPlane),
                           dst_frame->buffer(kUPlane),
                           dst_frame->stride(kUPlane),
                           dst_frame->buffer(kVPlane),
                           dst_frame->stride(kVPlane),
                           dst_width_, dst_height_,
                           libyuv::FilterMode(method_));
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容