• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "common_video/libyuv/include/webrtc_libyuv.h"
12 
13 #include <cstdint>
14 
15 #include "api/video/i420_buffer.h"
16 #include "common_video/include/video_frame_buffer.h"
17 #include "rtc_base/checks.h"
18 #include "third_party/libyuv/include/libyuv.h"
19 
20 namespace webrtc {
21 
CalcBufferSize(VideoType type,int width,int height)22 size_t CalcBufferSize(VideoType type, int width, int height) {
23   RTC_DCHECK_GE(width, 0);
24   RTC_DCHECK_GE(height, 0);
25   size_t buffer_size = 0;
26   switch (type) {
27     case VideoType::kI420:
28     case VideoType::kIYUV:
29     case VideoType::kYV12:
30     case VideoType::kNV12: {
31       int half_width = (width + 1) >> 1;
32       int half_height = (height + 1) >> 1;
33       buffer_size = width * height + half_width * half_height * 2;
34       break;
35     }
36     case VideoType::kRGB565:
37     case VideoType::kYUY2:
38     case VideoType::kUYVY:
39       buffer_size = width * height * 2;
40       break;
41     case VideoType::kRGB24:
42       buffer_size = width * height * 3;
43       break;
44     case VideoType::kBGRA:
45     case VideoType::kARGB:
46       buffer_size = width * height * 4;
47       break;
48     default:
49       RTC_DCHECK_NOTREACHED();
50       break;
51   }
52   return buffer_size;
53 }
54 
ExtractBuffer(const rtc::scoped_refptr<I420BufferInterface> & input_frame,size_t size,uint8_t * buffer)55 int ExtractBuffer(const rtc::scoped_refptr<I420BufferInterface>& input_frame,
56                   size_t size,
57                   uint8_t* buffer) {
58   RTC_DCHECK(buffer);
59   if (!input_frame)
60     return -1;
61   int width = input_frame->width();
62   int height = input_frame->height();
63   size_t length = CalcBufferSize(VideoType::kI420, width, height);
64   if (size < length) {
65     return -1;
66   }
67 
68   int chroma_width = input_frame->ChromaWidth();
69   int chroma_height = input_frame->ChromaHeight();
70 
71   libyuv::I420Copy(input_frame->DataY(), input_frame->StrideY(),
72                    input_frame->DataU(), input_frame->StrideU(),
73                    input_frame->DataV(), input_frame->StrideV(), buffer, width,
74                    buffer + width * height, chroma_width,
75                    buffer + width * height + chroma_width * chroma_height,
76                    chroma_width, width, height);
77 
78   return static_cast<int>(length);
79 }
80 
ExtractBuffer(const VideoFrame & input_frame,size_t size,uint8_t * buffer)81 int ExtractBuffer(const VideoFrame& input_frame, size_t size, uint8_t* buffer) {
82   return ExtractBuffer(input_frame.video_frame_buffer()->ToI420(), size,
83                        buffer);
84 }
85 
ConvertVideoType(VideoType video_type)86 int ConvertVideoType(VideoType video_type) {
87   switch (video_type) {
88     case VideoType::kUnknown:
89       return libyuv::FOURCC_ANY;
90     case VideoType::kI420:
91       return libyuv::FOURCC_I420;
92     case VideoType::kIYUV:  // same as VideoType::kYV12
93     case VideoType::kYV12:
94       return libyuv::FOURCC_YV12;
95     case VideoType::kRGB24:
96       return libyuv::FOURCC_24BG;
97     case VideoType::kRGB565:
98       return libyuv::FOURCC_RGBP;
99     case VideoType::kYUY2:
100       return libyuv::FOURCC_YUY2;
101     case VideoType::kUYVY:
102       return libyuv::FOURCC_UYVY;
103     case VideoType::kMJPEG:
104       return libyuv::FOURCC_MJPG;
105     case VideoType::kARGB:
106       return libyuv::FOURCC_ARGB;
107     case VideoType::kBGRA:
108       return libyuv::FOURCC_BGRA;
109     case VideoType::kNV12:
110       return libyuv::FOURCC_NV12;
111   }
112   RTC_DCHECK_NOTREACHED();
113   return libyuv::FOURCC_ANY;
114 }
115 
ConvertFromI420(const VideoFrame & src_frame,VideoType dst_video_type,int dst_sample_size,uint8_t * dst_frame)116 int ConvertFromI420(const VideoFrame& src_frame,
117                     VideoType dst_video_type,
118                     int dst_sample_size,
119                     uint8_t* dst_frame) {
120   rtc::scoped_refptr<I420BufferInterface> i420_buffer =
121       src_frame.video_frame_buffer()->ToI420();
122   return libyuv::ConvertFromI420(
123       i420_buffer->DataY(), i420_buffer->StrideY(), i420_buffer->DataU(),
124       i420_buffer->StrideU(), i420_buffer->DataV(), i420_buffer->StrideV(),
125       dst_frame, dst_sample_size, src_frame.width(), src_frame.height(),
126       ConvertVideoType(dst_video_type));
127 }
128 
ScaleI420ABuffer(const I420ABufferInterface & buffer,int target_width,int target_height)129 rtc::scoped_refptr<I420ABufferInterface> ScaleI420ABuffer(
130     const I420ABufferInterface& buffer,
131     int target_width,
132     int target_height) {
133   rtc::scoped_refptr<I420Buffer> yuv_buffer =
134       I420Buffer::Create(target_width, target_height);
135   yuv_buffer->ScaleFrom(buffer);
136   rtc::scoped_refptr<I420Buffer> axx_buffer =
137       I420Buffer::Create(target_width, target_height);
138   libyuv::ScalePlane(buffer.DataA(), buffer.StrideA(), buffer.width(),
139                      buffer.height(), axx_buffer->MutableDataY(),
140                      axx_buffer->StrideY(), target_width, target_height,
141                      libyuv::kFilterBox);
142   rtc::scoped_refptr<I420ABufferInterface> merged_buffer = WrapI420ABuffer(
143       yuv_buffer->width(), yuv_buffer->height(), yuv_buffer->DataY(),
144       yuv_buffer->StrideY(), yuv_buffer->DataU(), yuv_buffer->StrideU(),
145       yuv_buffer->DataV(), yuv_buffer->StrideV(), axx_buffer->DataY(),
146       axx_buffer->StrideY(),
147       // To keep references alive.
148       [yuv_buffer, axx_buffer] {});
149   return merged_buffer;
150 }
151 
ScaleVideoFrameBuffer(const I420BufferInterface & source,int dst_width,int dst_height)152 rtc::scoped_refptr<I420BufferInterface> ScaleVideoFrameBuffer(
153     const I420BufferInterface& source,
154     int dst_width,
155     int dst_height) {
156   rtc::scoped_refptr<I420Buffer> scaled_buffer =
157       I420Buffer::Create(dst_width, dst_height);
158   scaled_buffer->ScaleFrom(source);
159   return scaled_buffer;
160 }
161 
I420SSE(const I420BufferInterface & ref_buffer,const I420BufferInterface & test_buffer)162 double I420SSE(const I420BufferInterface& ref_buffer,
163                const I420BufferInterface& test_buffer) {
164   RTC_DCHECK_EQ(ref_buffer.width(), test_buffer.width());
165   RTC_DCHECK_EQ(ref_buffer.height(), test_buffer.height());
166   const uint64_t width = test_buffer.width();
167   const uint64_t height = test_buffer.height();
168   const uint64_t sse_y = libyuv::ComputeSumSquareErrorPlane(
169       ref_buffer.DataY(), ref_buffer.StrideY(), test_buffer.DataY(),
170       test_buffer.StrideY(), width, height);
171   const int width_uv = (width + 1) >> 1;
172   const int height_uv = (height + 1) >> 1;
173   const uint64_t sse_u = libyuv::ComputeSumSquareErrorPlane(
174       ref_buffer.DataU(), ref_buffer.StrideU(), test_buffer.DataU(),
175       test_buffer.StrideU(), width_uv, height_uv);
176   const uint64_t sse_v = libyuv::ComputeSumSquareErrorPlane(
177       ref_buffer.DataV(), ref_buffer.StrideV(), test_buffer.DataV(),
178       test_buffer.StrideV(), width_uv, height_uv);
179   const double samples = width * height + 2 * (width_uv * height_uv);
180   const double sse = sse_y + sse_u + sse_v;
181   return sse / (samples * 255.0 * 255.0);
182 }
183 
184 // Compute PSNR for an I420A frame (all planes). Can upscale test frame.
I420APSNR(const I420ABufferInterface & ref_buffer,const I420ABufferInterface & test_buffer)185 double I420APSNR(const I420ABufferInterface& ref_buffer,
186                  const I420ABufferInterface& test_buffer) {
187   RTC_DCHECK_GE(ref_buffer.width(), test_buffer.width());
188   RTC_DCHECK_GE(ref_buffer.height(), test_buffer.height());
189   if ((ref_buffer.width() != test_buffer.width()) ||
190       (ref_buffer.height() != test_buffer.height())) {
191     rtc::scoped_refptr<I420ABufferInterface> scaled_buffer =
192         ScaleI420ABuffer(test_buffer, ref_buffer.width(), ref_buffer.height());
193     return I420APSNR(ref_buffer, *scaled_buffer);
194   }
195   const int width = test_buffer.width();
196   const int height = test_buffer.height();
197   const uint64_t sse_y = libyuv::ComputeSumSquareErrorPlane(
198       ref_buffer.DataY(), ref_buffer.StrideY(), test_buffer.DataY(),
199       test_buffer.StrideY(), width, height);
200   const int width_uv = (width + 1) >> 1;
201   const int height_uv = (height + 1) >> 1;
202   const uint64_t sse_u = libyuv::ComputeSumSquareErrorPlane(
203       ref_buffer.DataU(), ref_buffer.StrideU(), test_buffer.DataU(),
204       test_buffer.StrideU(), width_uv, height_uv);
205   const uint64_t sse_v = libyuv::ComputeSumSquareErrorPlane(
206       ref_buffer.DataV(), ref_buffer.StrideV(), test_buffer.DataV(),
207       test_buffer.StrideV(), width_uv, height_uv);
208   const uint64_t sse_a = libyuv::ComputeSumSquareErrorPlane(
209       ref_buffer.DataA(), ref_buffer.StrideA(), test_buffer.DataA(),
210       test_buffer.StrideA(), width, height);
211   const uint64_t samples = 2 * (uint64_t)width * (uint64_t)height +
212                            2 * ((uint64_t)width_uv * (uint64_t)height_uv);
213   const uint64_t sse = sse_y + sse_u + sse_v + sse_a;
214   const double psnr = libyuv::SumSquareErrorToPsnr(sse, samples);
215   return (psnr > kPerfectPSNR) ? kPerfectPSNR : psnr;
216 }
217 
218 // Compute PSNR for an I420A frame (all planes)
I420APSNR(const VideoFrame * ref_frame,const VideoFrame * test_frame)219 double I420APSNR(const VideoFrame* ref_frame, const VideoFrame* test_frame) {
220   if (!ref_frame || !test_frame)
221     return -1;
222   RTC_DCHECK(ref_frame->video_frame_buffer()->type() ==
223              VideoFrameBuffer::Type::kI420A);
224   RTC_DCHECK(test_frame->video_frame_buffer()->type() ==
225              VideoFrameBuffer::Type::kI420A);
226   return I420APSNR(*ref_frame->video_frame_buffer()->GetI420A(),
227                    *test_frame->video_frame_buffer()->GetI420A());
228 }
229 
230 // Compute PSNR for an I420 frame (all planes). Can upscale test frame.
I420PSNR(const I420BufferInterface & ref_buffer,const I420BufferInterface & test_buffer)231 double I420PSNR(const I420BufferInterface& ref_buffer,
232                 const I420BufferInterface& test_buffer) {
233   RTC_DCHECK_GE(ref_buffer.width(), test_buffer.width());
234   RTC_DCHECK_GE(ref_buffer.height(), test_buffer.height());
235   if ((ref_buffer.width() != test_buffer.width()) ||
236       (ref_buffer.height() != test_buffer.height())) {
237     rtc::scoped_refptr<I420Buffer> scaled_buffer =
238         I420Buffer::Create(ref_buffer.width(), ref_buffer.height());
239     scaled_buffer->ScaleFrom(test_buffer);
240     return I420PSNR(ref_buffer, *scaled_buffer);
241   }
242   double psnr = libyuv::I420Psnr(
243       ref_buffer.DataY(), ref_buffer.StrideY(), ref_buffer.DataU(),
244       ref_buffer.StrideU(), ref_buffer.DataV(), ref_buffer.StrideV(),
245       test_buffer.DataY(), test_buffer.StrideY(), test_buffer.DataU(),
246       test_buffer.StrideU(), test_buffer.DataV(), test_buffer.StrideV(),
247       test_buffer.width(), test_buffer.height());
248   // LibYuv sets the max psnr value to 128, we restrict it here.
249   // In case of 0 mse in one frame, 128 can skew the results significantly.
250   return (psnr > kPerfectPSNR) ? kPerfectPSNR : psnr;
251 }
252 
253 // Compute PSNR for an I420 frame (all planes)
I420PSNR(const VideoFrame * ref_frame,const VideoFrame * test_frame)254 double I420PSNR(const VideoFrame* ref_frame, const VideoFrame* test_frame) {
255   if (!ref_frame || !test_frame)
256     return -1;
257   return I420PSNR(*ref_frame->video_frame_buffer()->ToI420(),
258                   *test_frame->video_frame_buffer()->ToI420());
259 }
260 
I420WeightedPSNR(const I420BufferInterface & ref_buffer,const I420BufferInterface & test_buffer)261 double I420WeightedPSNR(const I420BufferInterface& ref_buffer,
262                         const I420BufferInterface& test_buffer) {
263   RTC_DCHECK_GE(ref_buffer.width(), test_buffer.width());
264   RTC_DCHECK_GE(ref_buffer.height(), test_buffer.height());
265   if ((ref_buffer.width() != test_buffer.width()) ||
266       (ref_buffer.height() != test_buffer.height())) {
267     rtc::scoped_refptr<I420Buffer> scaled_ref_buffer =
268         I420Buffer::Create(test_buffer.width(), test_buffer.height());
269     scaled_ref_buffer->ScaleFrom(ref_buffer);
270     return I420WeightedPSNR(*scaled_ref_buffer, test_buffer);
271   }
272 
273   // Luma.
274   int width_y = test_buffer.width();
275   int height_y = test_buffer.height();
276   uint64_t sse_y = libyuv::ComputeSumSquareErrorPlane(
277       ref_buffer.DataY(), ref_buffer.StrideY(), test_buffer.DataY(),
278       test_buffer.StrideY(), width_y, height_y);
279   uint64_t num_samples_y = (uint64_t)width_y * (uint64_t)height_y;
280   double psnr_y = libyuv::SumSquareErrorToPsnr(sse_y, num_samples_y);
281 
282   // Chroma.
283   int width_uv = (width_y + 1) >> 1;
284   int height_uv = (height_y + 1) >> 1;
285   uint64_t sse_u = libyuv::ComputeSumSquareErrorPlane(
286       ref_buffer.DataU(), ref_buffer.StrideU(), test_buffer.DataU(),
287       test_buffer.StrideU(), width_uv, height_uv);
288   uint64_t num_samples_uv = (uint64_t)width_uv * (uint64_t)height_uv;
289   double psnr_u = libyuv::SumSquareErrorToPsnr(sse_u, num_samples_uv);
290   uint64_t sse_v = libyuv::ComputeSumSquareErrorPlane(
291       ref_buffer.DataV(), ref_buffer.StrideV(), test_buffer.DataV(),
292       test_buffer.StrideV(), width_uv, height_uv);
293   double psnr_v = libyuv::SumSquareErrorToPsnr(sse_v, num_samples_uv);
294 
295   // Weights from Ohm et. al 2012.
296   double psnr_yuv = (6.0 * psnr_y + psnr_u + psnr_v) / 8.0;
297   return (psnr_yuv > kPerfectPSNR) ? kPerfectPSNR : psnr_yuv;
298 }
299 
300 // Compute SSIM for an I420A frame (all planes). Can upscale test frame.
I420ASSIM(const I420ABufferInterface & ref_buffer,const I420ABufferInterface & test_buffer)301 double I420ASSIM(const I420ABufferInterface& ref_buffer,
302                  const I420ABufferInterface& test_buffer) {
303   RTC_DCHECK_GE(ref_buffer.width(), test_buffer.width());
304   RTC_DCHECK_GE(ref_buffer.height(), test_buffer.height());
305   if ((ref_buffer.width() != test_buffer.width()) ||
306       (ref_buffer.height() != test_buffer.height())) {
307     rtc::scoped_refptr<I420ABufferInterface> scaled_buffer =
308         ScaleI420ABuffer(test_buffer, ref_buffer.width(), ref_buffer.height());
309     return I420ASSIM(ref_buffer, *scaled_buffer);
310   }
311   const double yuv_ssim = libyuv::I420Ssim(
312       ref_buffer.DataY(), ref_buffer.StrideY(), ref_buffer.DataU(),
313       ref_buffer.StrideU(), ref_buffer.DataV(), ref_buffer.StrideV(),
314       test_buffer.DataY(), test_buffer.StrideY(), test_buffer.DataU(),
315       test_buffer.StrideU(), test_buffer.DataV(), test_buffer.StrideV(),
316       test_buffer.width(), test_buffer.height());
317   const double a_ssim = libyuv::CalcFrameSsim(
318       ref_buffer.DataA(), ref_buffer.StrideA(), test_buffer.DataA(),
319       test_buffer.StrideA(), test_buffer.width(), test_buffer.height());
320   return (yuv_ssim + (a_ssim * 0.8)) / 1.8;
321 }
322 
323 // Compute SSIM for an I420A frame (all planes)
I420ASSIM(const VideoFrame * ref_frame,const VideoFrame * test_frame)324 double I420ASSIM(const VideoFrame* ref_frame, const VideoFrame* test_frame) {
325   if (!ref_frame || !test_frame)
326     return -1;
327   RTC_DCHECK(ref_frame->video_frame_buffer()->type() ==
328              VideoFrameBuffer::Type::kI420A);
329   RTC_DCHECK(test_frame->video_frame_buffer()->type() ==
330              VideoFrameBuffer::Type::kI420A);
331   return I420ASSIM(*ref_frame->video_frame_buffer()->GetI420A(),
332                    *test_frame->video_frame_buffer()->GetI420A());
333 }
334 
335 // Compute SSIM for an I420 frame (all planes). Can upscale test_buffer.
I420SSIM(const I420BufferInterface & ref_buffer,const I420BufferInterface & test_buffer)336 double I420SSIM(const I420BufferInterface& ref_buffer,
337                 const I420BufferInterface& test_buffer) {
338   RTC_DCHECK_GE(ref_buffer.width(), test_buffer.width());
339   RTC_DCHECK_GE(ref_buffer.height(), test_buffer.height());
340   if ((ref_buffer.width() != test_buffer.width()) ||
341       (ref_buffer.height() != test_buffer.height())) {
342     rtc::scoped_refptr<I420Buffer> scaled_buffer =
343         I420Buffer::Create(ref_buffer.width(), ref_buffer.height());
344     scaled_buffer->ScaleFrom(test_buffer);
345     return I420SSIM(ref_buffer, *scaled_buffer);
346   }
347   return libyuv::I420Ssim(
348       ref_buffer.DataY(), ref_buffer.StrideY(), ref_buffer.DataU(),
349       ref_buffer.StrideU(), ref_buffer.DataV(), ref_buffer.StrideV(),
350       test_buffer.DataY(), test_buffer.StrideY(), test_buffer.DataU(),
351       test_buffer.StrideU(), test_buffer.DataV(), test_buffer.StrideV(),
352       test_buffer.width(), test_buffer.height());
353 }
354 
I420SSIM(const VideoFrame * ref_frame,const VideoFrame * test_frame)355 double I420SSIM(const VideoFrame* ref_frame, const VideoFrame* test_frame) {
356   if (!ref_frame || !test_frame)
357     return -1;
358   return I420SSIM(*ref_frame->video_frame_buffer()->ToI420(),
359                   *test_frame->video_frame_buffer()->ToI420());
360 }
361 
NV12Scale(uint8_t * tmp_buffer,const uint8_t * src_y,int src_stride_y,const uint8_t * src_uv,int src_stride_uv,int src_width,int src_height,uint8_t * dst_y,int dst_stride_y,uint8_t * dst_uv,int dst_stride_uv,int dst_width,int dst_height)362 void NV12Scale(uint8_t* tmp_buffer,
363                const uint8_t* src_y,
364                int src_stride_y,
365                const uint8_t* src_uv,
366                int src_stride_uv,
367                int src_width,
368                int src_height,
369                uint8_t* dst_y,
370                int dst_stride_y,
371                uint8_t* dst_uv,
372                int dst_stride_uv,
373                int dst_width,
374                int dst_height) {
375   const int src_chroma_width = (src_width + 1) / 2;
376   const int src_chroma_height = (src_height + 1) / 2;
377 
378   if (src_width == dst_width && src_height == dst_height) {
379     // No scaling.
380     libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, src_width,
381                       src_height);
382     libyuv::CopyPlane(src_uv, src_stride_uv, dst_uv, dst_stride_uv,
383                       src_chroma_width * 2, src_chroma_height);
384     return;
385   }
386 
387   // Scaling.
388   // Allocate temporary memory for spitting UV planes and scaling them.
389   const int dst_chroma_width = (dst_width + 1) / 2;
390   const int dst_chroma_height = (dst_height + 1) / 2;
391 
392   uint8_t* const src_u = tmp_buffer;
393   uint8_t* const src_v = src_u + src_chroma_width * src_chroma_height;
394   uint8_t* const dst_u = src_v + src_chroma_width * src_chroma_height;
395   uint8_t* const dst_v = dst_u + dst_chroma_width * dst_chroma_height;
396 
397   // Split source UV plane into separate U and V plane using the temporary data.
398   libyuv::SplitUVPlane(src_uv, src_stride_uv, src_u, src_chroma_width, src_v,
399                        src_chroma_width, src_chroma_width, src_chroma_height);
400 
401   // Scale the planes.
402   libyuv::I420Scale(
403       src_y, src_stride_y, src_u, src_chroma_width, src_v, src_chroma_width,
404       src_width, src_height, dst_y, dst_stride_y, dst_u, dst_chroma_width,
405       dst_v, dst_chroma_width, dst_width, dst_height, libyuv::kFilterBox);
406 
407   // Merge the UV planes into the destination.
408   libyuv::MergeUVPlane(dst_u, dst_chroma_width, dst_v, dst_chroma_width, dst_uv,
409                        dst_stride_uv, dst_chroma_width, dst_chroma_height);
410 }
411 
412 NV12ToI420Scaler::NV12ToI420Scaler() = default;
413 NV12ToI420Scaler::~NV12ToI420Scaler() = default;
414 
NV12ToI420Scale(const uint8_t * src_y,int src_stride_y,const uint8_t * src_uv,int src_stride_uv,int src_width,int src_height,uint8_t * dst_y,int dst_stride_y,uint8_t * dst_u,int dst_stride_u,uint8_t * dst_v,int dst_stride_v,int dst_width,int dst_height)415 void NV12ToI420Scaler::NV12ToI420Scale(const uint8_t* src_y,
416                                        int src_stride_y,
417                                        const uint8_t* src_uv,
418                                        int src_stride_uv,
419                                        int src_width,
420                                        int src_height,
421                                        uint8_t* dst_y,
422                                        int dst_stride_y,
423                                        uint8_t* dst_u,
424                                        int dst_stride_u,
425                                        uint8_t* dst_v,
426                                        int dst_stride_v,
427                                        int dst_width,
428                                        int dst_height) {
429   if (src_width == dst_width && src_height == dst_height) {
430     // No scaling.
431     tmp_uv_planes_.clear();
432     tmp_uv_planes_.shrink_to_fit();
433     libyuv::NV12ToI420(src_y, src_stride_y, src_uv, src_stride_uv, dst_y,
434                        dst_stride_y, dst_u, dst_stride_u, dst_v, dst_stride_v,
435                        src_width, src_height);
436     return;
437   }
438 
439   // Scaling.
440   // Allocate temporary memory for spitting UV planes.
441   const int src_uv_width = (src_width + 1) / 2;
442   const int src_uv_height = (src_height + 1) / 2;
443   tmp_uv_planes_.resize(src_uv_width * src_uv_height * 2);
444   tmp_uv_planes_.shrink_to_fit();
445 
446   // Split source UV plane into separate U and V plane using the temporary data.
447   uint8_t* const src_u = tmp_uv_planes_.data();
448   uint8_t* const src_v = tmp_uv_planes_.data() + src_uv_width * src_uv_height;
449   libyuv::SplitUVPlane(src_uv, src_stride_uv, src_u, src_uv_width, src_v,
450                        src_uv_width, src_uv_width, src_uv_height);
451 
452   // Scale the planes into the destination.
453   libyuv::I420Scale(src_y, src_stride_y, src_u, src_uv_width, src_v,
454                     src_uv_width, src_width, src_height, dst_y, dst_stride_y,
455                     dst_u, dst_stride_u, dst_v, dst_stride_v, dst_width,
456                     dst_height, libyuv::kFilterBox);
457 }
458 
459 }  // namespace webrtc
460