Delete VCMEncodedFrame::VerifyAndAllocate

And mark EncodedImage::Allocate as deprecated.

Bug: webrtc:9378
Change-Id: I03ce907fa6b87803ddb72f548f60a9bf1b7c317d
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/155163
Reviewed-by: Philip Eliasson <philipel@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29383}
diff --git a/api/video/BUILD.gn b/api/video/BUILD.gn
index af2b897..0da9c6c 100644
--- a/api/video/BUILD.gn
+++ b/api/video/BUILD.gn
@@ -114,6 +114,7 @@
     "..:scoped_refptr",
     "../..:webrtc_common",
     "../../rtc_base:checks",
+    "../../rtc_base:deprecation",
     "../../rtc_base:rtc_base_approved",
     "../../rtc_base/system:rtc_export",
     "//third_party/abseil-cpp/absl/types:optional",
diff --git a/api/video/encoded_image.h b/api/video/encoded_image.h
index a980ef7..91f2e0f 100644
--- a/api/video/encoded_image.h
+++ b/api/video/encoded_image.h
@@ -28,6 +28,7 @@
 #include "api/video/video_timing.h"
 #include "common_types.h"  // NOLINT(build/include)
 #include "rtc_base/checks.h"
+#include "rtc_base/deprecation.h"
 #include "rtc_base/ref_count.h"
 #include "rtc_base/system/rtc_export.h"
 
@@ -55,6 +56,7 @@
 // Basic implementation of EncodedImageBufferInterface.
 class EncodedImageBuffer : public EncodedImageBufferInterface {
  public:
+  static rtc::scoped_refptr<EncodedImageBuffer> Create() { return Create(0); }
   static rtc::scoped_refptr<EncodedImageBuffer> Create(size_t size);
   static rtc::scoped_refptr<EncodedImageBuffer> Create(const uint8_t* data,
                                                        size_t size);
@@ -146,6 +148,7 @@
 
   // TODO(bugs.webrtc.org/9378): Delete; this method implies realloc, which
   // should not be generally supported by the EncodedImageBufferInterface.
+  RTC_DEPRECATED
   void Allocate(size_t capacity);
 
   void SetEncodedData(
diff --git a/call/rtp_video_sender_unittest.cc b/call/rtp_video_sender_unittest.cc
index 17ee9c6..39d25e4 100644
--- a/call/rtp_video_sender_unittest.cc
+++ b/call/rtp_video_sender_unittest.cc
@@ -521,14 +521,13 @@
                                  kPayloadType, {});
   test.router()->SetActive(true);
 
-  constexpr uint8_t kPayload = 'a';
+  const uint8_t kPayload[1] = {'a'};
   EncodedImage encoded_image;
   encoded_image.SetTimestamp(1);
   encoded_image.capture_time_ms_ = 2;
   encoded_image._frameType = VideoFrameType::kVideoFrameKey;
-  encoded_image.Allocate(1);
-  encoded_image.data()[0] = kPayload;
-  encoded_image.set_size(1);
+  encoded_image.SetEncodedData(
+      EncodedImageBuffer::Create(kPayload, sizeof(kPayload)));
   encoded_image.SetSpatialIndex(0);
 
   CodecSpecificInfo codec_specific;
diff --git a/modules/video_coding/codecs/vp8/libvpx_vp8_encoder.cc b/modules/video_coding/codecs/vp8/libvpx_vp8_encoder.cc
index d4f18e0..ee5fd43 100644
--- a/modules/video_coding/codecs/vp8/libvpx_vp8_encoder.cc
+++ b/modules/video_coding/codecs/vp8/libvpx_vp8_encoder.cc
@@ -1136,16 +1136,20 @@
     encoded_images_[encoder_idx]._frameType = VideoFrameType::kVideoFrameDelta;
     CodecSpecificInfo codec_specific;
     const vpx_codec_cx_pkt_t* pkt = NULL;
+
+    // TODO(nisse): Introduce some buffer cache or buffer pool, to reduce
+    // allocations and/or copy operations.
+    auto buffer = EncodedImageBuffer::Create();
+
     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 = encoded_images_[encoder_idx].size();
+          const size_t size = buffer->size();
           const size_t new_size = pkt->data.frame.sz + size;
-          encoded_images_[encoder_idx].Allocate(new_size);
-          memcpy(&encoded_images_[encoder_idx].data()[size],
-                 pkt->data.frame.buf, pkt->data.frame.sz);
-          encoded_images_[encoder_idx].set_size(new_size);
+          buffer->Realloc(new_size);
+          memcpy(&buffer->data()[size], pkt->data.frame.buf,
+                 pkt->data.frame.sz);
           break;
         }
         default:
@@ -1158,6 +1162,7 @@
           encoded_images_[encoder_idx]._frameType =
               VideoFrameType::kVideoFrameKey;
         }
+        encoded_images_[encoder_idx].SetEncodedData(buffer);
         encoded_images_[encoder_idx].SetSpatialIndex(stream_idx);
         PopulateCodecSpecific(&codec_specific, *pkt, stream_idx, encoder_idx,
                               input_image.timestamp());
diff --git a/modules/video_coding/encoded_frame.cc b/modules/video_coding/encoded_frame.cc
index bbbd9bc..1e9e374 100644
--- a/modules/video_coding/encoded_frame.cc
+++ b/modules/video_coding/encoded_frame.cc
@@ -159,14 +159,4 @@
   }
 }
 
-void VCMEncodedFrame::VerifyAndAllocate(size_t minimumSize) {
-  size_t old_capacity = capacity();
-  if (minimumSize > old_capacity) {
-    // TODO(nisse): EncodedImage::Allocate is implemented as a realloc
-    // operation, and is deprecated. Refactor to use EncodedImageBuffer::Realloc
-    // instead.
-    Allocate(minimumSize);
-  }
-}
-
 }  // namespace webrtc
diff --git a/modules/video_coding/encoded_frame.h b/modules/video_coding/encoded_frame.h
index 27ad107..b29ff63 100644
--- a/modules/video_coding/encoded_frame.h
+++ b/modules/video_coding/encoded_frame.h
@@ -110,16 +110,6 @@
     _codecSpecificInfo = *codec_specific;
   }
 
-  /**
-   * Verifies that current allocated buffer size is larger than or equal to the
-   * input size.
-   * If the current buffer size is smaller, a new allocation is made and the old
-   * buffer data
-   * is copied to the new buffer.
-   * Buffer size is updated to minimumSize.
-   */
-  void VerifyAndAllocate(size_t minimumSize);
-
  protected:
   void Reset();
 
diff --git a/modules/video_coding/frame_buffer2_unittest.cc b/modules/video_coding/frame_buffer2_unittest.cc
index d96960e..09300fb 100644
--- a/modules/video_coding/frame_buffer2_unittest.cc
+++ b/modules/video_coding/frame_buffer2_unittest.cc
@@ -171,8 +171,7 @@
     frame->inter_layer_predicted = inter_layer_predicted;
     frame->is_last_spatial_layer = last_spatial_layer;
     // Add some data to buffer.
-    frame->VerifyAndAllocate(frame_size_bytes);
-    frame->set_size(frame_size_bytes);
+    frame->SetEncodedData(EncodedImageBuffer::Create(frame_size_bytes));
     for (size_t r = 0; r < references.size(); ++r)
       frame->references[r] = references[r];
     return frame;
@@ -585,8 +584,7 @@
 
   {
     std::unique_ptr<FrameObjectFake> frame(new FrameObjectFake());
-    frame->VerifyAndAllocate(kFrameSize);
-    frame->set_size(kFrameSize);
+    frame->SetEncodedData(EncodedImageBuffer::Create(kFrameSize));
     frame->id.picture_id = pid;
     frame->id.spatial_layer = 0;
     frame->SetTimestamp(ts);