From f07003c2ec18286013a7a0d9c34680514bd1cff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= Date: Fri, 18 Oct 2019 09:52:52 +0200 Subject: [PATCH] Avoid Realloc in LibvpxVp8Encoder Instead, compute the needed buffer size for the encoded VP8 frame prior to copying the data. Bug: chromium:1012256 Change-Id: I3dda921f6d8f974731d2138bc5f6166cfdb9a321 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/157303 Commit-Queue: Niels Moller Reviewed-by: Philip Eliasson Cr-Commit-Position: refs/heads/master@{#29531} --- .../codecs/vp8/libvpx_vp8_encoder.cc | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/modules/video_coding/codecs/vp8/libvpx_vp8_encoder.cc b/modules/video_coding/codecs/vp8/libvpx_vp8_encoder.cc index 556ac60479..42d13a02da 100644 --- a/modules/video_coding/codecs/vp8/libvpx_vp8_encoder.cc +++ b/modules/video_coding/codecs/vp8/libvpx_vp8_encoder.cc @@ -1131,19 +1131,28 @@ int LibvpxVp8Encoder::GetEncodedPartitions(const VideoFrame& input_image, CodecSpecificInfo codec_specific; const vpx_codec_cx_pkt_t* pkt = NULL; + size_t encoded_size = 0; + while ((pkt = libvpx_->codec_get_cx_data(&encoders_[encoder_idx], &iter)) != + NULL) { + if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) { + encoded_size += pkt->data.frame.sz; + } + } + // TODO(nisse): Introduce some buffer cache or buffer pool, to reduce // allocations and/or copy operations. - auto buffer = EncodedImageBuffer::Create(); + auto buffer = EncodedImageBuffer::Create(encoded_size); + iter = NULL; + size_t encoded_pos = 0; while ((pkt = libvpx_->codec_get_cx_data(&encoders_[encoder_idx], &iter)) != NULL) { switch (pkt->kind) { case VPX_CODEC_CX_FRAME_PKT: { - const size_t size = buffer->size(); - const size_t new_size = pkt->data.frame.sz + size; - buffer->Realloc(new_size); - memcpy(&buffer->data()[size], pkt->data.frame.buf, + RTC_CHECK_LE(encoded_pos + pkt->data.frame.sz, buffer->size()); + memcpy(&buffer->data()[encoded_pos], pkt->data.frame.buf, pkt->data.frame.sz); + encoded_pos += pkt->data.frame.sz; break; } default: @@ -1157,6 +1166,7 @@ int LibvpxVp8Encoder::GetEncodedPartitions(const VideoFrame& input_image, VideoFrameType::kVideoFrameKey; } encoded_images_[encoder_idx].SetEncodedData(buffer); + encoded_images_[encoder_idx].set_size(encoded_pos); encoded_images_[encoder_idx].SetSpatialIndex(stream_idx); PopulateCodecSpecific(&codec_specific, *pkt, stream_idx, encoder_idx, input_image.timestamp());