Return WrappedI444Buffer in VP9Impl

This CL allows VP9Impl to return a WrappedI444Buffer if the image format is
VPX_IMG_FMT_I444.

Needs to be checked in after Chromium CL 2876363003 is checked in.

BUG=webrtc:7506

Review-Url: https://codereview.webrtc.org/2927943003
Cr-Commit-Position: refs/heads/master@{#18727}
diff --git a/webrtc/common_video/include/video_frame_buffer.h b/webrtc/common_video/include/video_frame_buffer.h
index 6b578bc..e3835d3 100644
--- a/webrtc/common_video/include/video_frame_buffer.h
+++ b/webrtc/common_video/include/video_frame_buffer.h
@@ -45,6 +45,7 @@
   const int height_;
 };
 
+// Deprecated. Please use WrapI420Buffer(...) instead.
 class WrappedI420Buffer : public I420BufferInterface {
  public:
   WrappedI420Buffer(int width,
@@ -81,6 +82,40 @@
   rtc::Callback0<void> no_longer_used_cb_;
 };
 
+rtc::scoped_refptr<I420BufferInterface> WrapI420Buffer(
+    int width,
+    int height,
+    const uint8_t* y_plane,
+    int y_stride,
+    const uint8_t* u_plane,
+    int u_stride,
+    const uint8_t* v_plane,
+    int v_stride,
+    const rtc::Callback0<void>& no_longer_used);
+
+rtc::scoped_refptr<I444BufferInterface> WrapI444Buffer(
+    int width,
+    int height,
+    const uint8_t* y_plane,
+    int y_stride,
+    const uint8_t* u_plane,
+    int u_stride,
+    const uint8_t* v_plane,
+    int v_stride,
+    const rtc::Callback0<void>& no_longer_used);
+
+rtc::scoped_refptr<PlanarYuvBuffer> WrapYuvBuffer(
+    VideoFrameBuffer::Type type,
+    int width,
+    int height,
+    const uint8_t* y_plane,
+    int y_stride,
+    const uint8_t* u_plane,
+    int u_stride,
+    const uint8_t* v_plane,
+    int v_stride,
+    const rtc::Callback0<void>& no_longer_used);
+
 }  // namespace webrtc
 
 #endif  // WEBRTC_COMMON_VIDEO_INCLUDE_VIDEO_FRAME_BUFFER_H_
diff --git a/webrtc/common_video/video_frame_buffer.cc b/webrtc/common_video/video_frame_buffer.cc
index 5f339a5..7ebff44 100644
--- a/webrtc/common_video/video_frame_buffer.cc
+++ b/webrtc/common_video/video_frame_buffer.cc
@@ -124,4 +124,115 @@
   return v_stride_;
 }
 
+// Template to implement a wrapped buffer for a I4??BufferInterface.
+template <typename Base>
+class WrappedYuvBuffer : public Base {
+ public:
+  WrappedYuvBuffer(int width,
+                   int height,
+                   const uint8_t* y_plane,
+                   int y_stride,
+                   const uint8_t* u_plane,
+                   int u_stride,
+                   const uint8_t* v_plane,
+                   int v_stride,
+                   const rtc::Callback0<void>& no_longer_used)
+      : width_(width),
+        height_(height),
+        y_plane_(y_plane),
+        u_plane_(u_plane),
+        v_plane_(v_plane),
+        y_stride_(y_stride),
+        u_stride_(u_stride),
+        v_stride_(v_stride),
+        no_longer_used_cb_(no_longer_used) {}
+
+  int width() const override { return width_; }
+
+  int height() const override { return height_; }
+
+  const uint8_t* DataY() const override { return y_plane_; }
+
+  const uint8_t* DataU() const override { return u_plane_; }
+
+  const uint8_t* DataV() const override { return v_plane_; }
+
+  int StrideY() const override { return y_stride_; }
+
+  int StrideU() const override { return u_stride_; }
+
+  int StrideV() const override { return v_stride_; }
+
+ private:
+  friend class rtc::RefCountedObject<WrappedYuvBuffer>;
+
+  ~WrappedYuvBuffer() override { no_longer_used_cb_(); }
+
+  const int width_;
+  const int height_;
+  const uint8_t* const y_plane_;
+  const uint8_t* const u_plane_;
+  const uint8_t* const v_plane_;
+  const int y_stride_;
+  const int u_stride_;
+  const int v_stride_;
+  rtc::Callback0<void> no_longer_used_cb_;
+};
+
+rtc::scoped_refptr<I420BufferInterface> WrapI420Buffer(
+    int width,
+    int height,
+    const uint8_t* y_plane,
+    int y_stride,
+    const uint8_t* u_plane,
+    int u_stride,
+    const uint8_t* v_plane,
+    int v_stride,
+    const rtc::Callback0<void>& no_longer_used) {
+  return rtc::scoped_refptr<I420BufferInterface>(
+      new rtc::RefCountedObject<WrappedYuvBuffer<I420BufferInterface>>(
+          width, height, y_plane, y_stride, u_plane, u_stride, v_plane,
+          v_stride, no_longer_used));
+}
+
+rtc::scoped_refptr<I444BufferInterface> WrapI444Buffer(
+    int width,
+    int height,
+    const uint8_t* y_plane,
+    int y_stride,
+    const uint8_t* u_plane,
+    int u_stride,
+    const uint8_t* v_plane,
+    int v_stride,
+    const rtc::Callback0<void>& no_longer_used) {
+  return rtc::scoped_refptr<I444BufferInterface>(
+      new rtc::RefCountedObject<WrappedYuvBuffer<I444BufferInterface>>(
+          width, height, y_plane, y_stride, u_plane, u_stride, v_plane,
+          v_stride, no_longer_used));
+}
+
+rtc::scoped_refptr<PlanarYuvBuffer> WrapYuvBuffer(
+    VideoFrameBuffer::Type type,
+    int width,
+    int height,
+    const uint8_t* y_plane,
+    int y_stride,
+    const uint8_t* u_plane,
+    int u_stride,
+    const uint8_t* v_plane,
+    int v_stride,
+    const rtc::Callback0<void>& no_longer_used) {
+  switch (type) {
+    case VideoFrameBuffer::Type::kI420:
+      return WrapI420Buffer(width, height, y_plane, y_stride, u_plane, u_stride,
+                            v_plane, v_stride, no_longer_used);
+    case VideoFrameBuffer::Type::kI444:
+      return WrapI444Buffer(width, height, y_plane, y_stride, u_plane, u_stride,
+                            v_plane, v_stride, no_longer_used);
+    default:
+      FATAL() << "Unexpected frame buffer type.";
+      return nullptr;
+  }
+}
+
 }  // namespace webrtc
diff --git a/webrtc/sdk/android/src/jni/native_handle_impl.cc b/webrtc/sdk/android/src/jni/native_handle_impl.cc
index e2f33b7..907c90e 100644
--- a/webrtc/sdk/android/src/jni/native_handle_impl.cc
+++ b/webrtc/sdk/android/src/jni/native_handle_impl.cc
@@ -189,10 +189,9 @@
   uint8_t* u_data = y_data + height() * stride;
   uint8_t* v_data = u_data + stride/2;
 
-  rtc::scoped_refptr<webrtc::I420BufferInterface> copy =
-      new rtc::RefCountedObject<webrtc::WrappedI420Buffer>(
-          width(), height(), y_data, stride, u_data, stride, v_data, stride,
-          rtc::Bind(&webrtc::AlignedFree, yuv_data.release()));
+  rtc::scoped_refptr<webrtc::I420BufferInterface> copy = webrtc::WrapI420Buffer(
+      width(), height(), y_data, stride, u_data, stride, v_data, stride,
+      rtc::Bind(&webrtc::AlignedFree, yuv_data.release()));
 
   JNIEnv* jni = AttachCurrentThreadIfNeeded();
   ScopedLocalRefFrame local_ref_frame(jni);