Line data Source code
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 "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
12 : #include "libyuv/planar_functions.h"
13 :
14 : #include <string.h>
15 : #include <limits>
16 :
17 : #include "webrtc/base/checks.h"
18 : // TODO(nisse): Only needed for the deprecated ConvertToI420.
19 : #include "webrtc/api/video/i420_buffer.h"
20 :
21 : // NOTE(ajm): Path provided by gn.
22 : #include "libyuv.h" // NOLINT
23 :
24 : namespace webrtc {
25 :
26 0 : VideoType RawVideoTypeToCommonVideoVideoType(RawVideoType type) {
27 0 : switch (type) {
28 : case kVideoI420:
29 0 : return kI420;
30 : case kVideoIYUV:
31 0 : return kIYUV;
32 : case kVideoRGB24:
33 0 : return kRGB24;
34 : case kVideoARGB:
35 0 : return kARGB;
36 : case kVideoARGB4444:
37 0 : return kARGB4444;
38 : case kVideoRGB565:
39 0 : return kRGB565;
40 : case kVideoARGB1555:
41 0 : return kARGB1555;
42 : case kVideoYUY2:
43 0 : return kYUY2;
44 : case kVideoYV12:
45 0 : return kYV12;
46 : case kVideoUYVY:
47 0 : return kUYVY;
48 : case kVideoNV21:
49 0 : return kNV21;
50 : case kVideoNV12:
51 0 : return kNV12;
52 : case kVideoBGRA:
53 0 : return kBGRA;
54 : case kVideoMJPEG:
55 0 : return kMJPG;
56 : default:
57 0 : RTC_NOTREACHED();
58 : }
59 0 : return kUnknown;
60 : }
61 :
62 0 : size_t CalcBufferSize(VideoType type, int width, int height) {
63 0 : RTC_DCHECK_GE(width, 0);
64 0 : RTC_DCHECK_GE(height, 0);
65 : // Verify we won't overflow; max is width*height*4
66 : // 0x7FFF * 0x7FFF * 4 = < 0x100000000
67 0 : RTC_DCHECK_LE(width, 0x7FFF); // guarantees no overflow and cheaper than multiply
68 0 : RTC_DCHECK_LE(height, 0x7FFF);
69 0 : RTC_DCHECK_GE(std::numeric_limits<std::size_t>::max(), 0xFFFFFFFF);
70 : // assert() is debug only
71 0 : if (width > 0x7FFF || height > 0x7FFF) {
72 0 : return SIZE_MAX; // very likely forces OOM
73 : }
74 :
75 0 : size_t buffer_size = 0;
76 0 : switch (type) {
77 : case kI420:
78 : case kNV12:
79 : case kNV21:
80 : case kIYUV:
81 : case kYV12: {
82 0 : int half_width = (width + 1) >> 1;
83 0 : int half_height = (height + 1) >> 1;
84 0 : buffer_size = width * height + half_width * half_height * 2;
85 0 : break;
86 : }
87 : case kARGB4444:
88 : case kRGB565:
89 : case kARGB1555:
90 : case kYUY2:
91 : case kUYVY:
92 0 : buffer_size = width * height * 2;
93 0 : break;
94 : case kRGB24:
95 0 : buffer_size = width * height * 3;
96 0 : break;
97 : case kBGRA:
98 : case kARGB:
99 0 : buffer_size = width * height * 4;
100 0 : break;
101 : default:
102 0 : RTC_NOTREACHED();
103 0 : break;
104 : }
105 0 : return buffer_size;
106 : }
107 :
108 0 : static int PrintPlane(const uint8_t* buf,
109 : int width,
110 : int height,
111 : int stride,
112 : FILE* file) {
113 0 : for (int i = 0; i < height; i++, buf += stride) {
114 0 : if (fwrite(buf, 1, width, file) != static_cast<unsigned int>(width))
115 0 : return -1;
116 : }
117 0 : return 0;
118 : }
119 :
120 : // TODO(nisse): Belongs with the test code?
121 0 : int PrintVideoFrame(const VideoFrameBuffer& frame, FILE* file) {
122 0 : int width = frame.width();
123 0 : int height = frame.height();
124 0 : int chroma_width = (width + 1) / 2;
125 0 : int chroma_height = (height + 1) / 2;
126 :
127 0 : if (PrintPlane(frame.DataY(), width, height,
128 0 : frame.StrideY(), file) < 0) {
129 0 : return -1;
130 : }
131 0 : if (PrintPlane(frame.DataU(),
132 : chroma_width, chroma_height,
133 0 : frame.StrideU(), file) < 0) {
134 0 : return -1;
135 : }
136 0 : if (PrintPlane(frame.DataV(),
137 : chroma_width, chroma_height,
138 0 : frame.StrideV(), file) < 0) {
139 0 : return -1;
140 : }
141 0 : return 0;
142 : }
143 :
144 0 : int PrintVideoFrame(const VideoFrame& frame, FILE* file) {
145 0 : return PrintVideoFrame(*frame.video_frame_buffer(), file);
146 : }
147 :
148 0 : int ExtractBuffer(const rtc::scoped_refptr<VideoFrameBuffer>& input_frame,
149 : size_t size,
150 : uint8_t* buffer) {
151 0 : RTC_DCHECK(buffer);
152 0 : if (!input_frame)
153 0 : return -1;
154 0 : int width = input_frame->width();
155 0 : int height = input_frame->height();
156 0 : size_t length = CalcBufferSize(kI420, width, height);
157 0 : if (size < length) {
158 0 : return -1;
159 : }
160 :
161 0 : int chroma_width = (width + 1) / 2;
162 0 : int chroma_height = (height + 1) / 2;
163 :
164 0 : libyuv::I420Copy(input_frame->DataY(),
165 0 : input_frame->StrideY(),
166 0 : input_frame->DataU(),
167 0 : input_frame->StrideU(),
168 0 : input_frame->DataV(),
169 0 : input_frame->StrideV(),
170 : buffer, width,
171 0 : buffer + width*height, chroma_width,
172 0 : buffer + width*height + chroma_width*chroma_height,
173 : chroma_width,
174 0 : width, height);
175 :
176 0 : return static_cast<int>(length);
177 : }
178 :
179 0 : int ExtractBuffer(const VideoFrame& input_frame, size_t size, uint8_t* buffer) {
180 0 : return ExtractBuffer(input_frame.video_frame_buffer(), size, buffer);
181 : }
182 :
183 0 : int ConvertNV12ToRGB565(const uint8_t* src_frame,
184 : uint8_t* dst_frame,
185 : int width, int height) {
186 0 : int abs_height = (height < 0) ? -height : height;
187 0 : const uint8_t* yplane = src_frame;
188 0 : const uint8_t* uvInterlaced = src_frame + (width * abs_height);
189 :
190 0 : return libyuv::NV12ToRGB565(yplane, width,
191 0 : uvInterlaced, (width + 1) >> 1,
192 : dst_frame, width,
193 0 : width, height);
194 : }
195 :
196 0 : int ConvertRGB24ToARGB(const uint8_t* src_frame, uint8_t* dst_frame,
197 : int width, int height, int dst_stride) {
198 0 : if (dst_stride == 0)
199 0 : dst_stride = width;
200 : return libyuv::RGB24ToARGB(src_frame, width,
201 : dst_frame, dst_stride,
202 0 : width, height);
203 : }
204 :
205 0 : libyuv::RotationMode ConvertRotationMode(VideoRotation rotation) {
206 0 : switch (rotation) {
207 : case kVideoRotation_0:
208 0 : return libyuv::kRotate0;
209 : case kVideoRotation_90:
210 0 : return libyuv::kRotate90;
211 : case kVideoRotation_180:
212 0 : return libyuv::kRotate180;
213 : case kVideoRotation_270:
214 0 : return libyuv::kRotate270;
215 : }
216 0 : RTC_NOTREACHED();
217 0 : return libyuv::kRotate0;
218 : }
219 :
220 0 : int ConvertVideoType(VideoType video_type) {
221 0 : switch (video_type) {
222 : case kUnknown:
223 0 : return libyuv::FOURCC_ANY;
224 : case kI420:
225 0 : return libyuv::FOURCC_I420;
226 : case kIYUV: // same as KYV12
227 : case kYV12:
228 0 : return libyuv::FOURCC_YV12;
229 : case kRGB24:
230 0 : return libyuv::FOURCC_24BG;
231 : case kABGR:
232 0 : return libyuv::FOURCC_ABGR;
233 : case kRGB565:
234 0 : return libyuv::FOURCC_RGBP;
235 : case kYUY2:
236 0 : return libyuv::FOURCC_YUY2;
237 : case kUYVY:
238 0 : return libyuv::FOURCC_UYVY;
239 : case kMJPG:
240 0 : return libyuv::FOURCC_MJPG;
241 : case kNV21:
242 0 : return libyuv::FOURCC_NV21;
243 : case kNV12:
244 0 : return libyuv::FOURCC_NV12;
245 : case kARGB:
246 0 : return libyuv::FOURCC_ARGB;
247 : case kBGRA:
248 0 : return libyuv::FOURCC_BGRA;
249 : case kARGB4444:
250 0 : return libyuv::FOURCC_R444;
251 : case kARGB1555:
252 0 : return libyuv::FOURCC_RGBO;
253 : }
254 0 : RTC_NOTREACHED();
255 0 : return libyuv::FOURCC_ANY;
256 : }
257 :
258 : // TODO(nisse): Delete this wrapper, let callers use libyuv directly.
259 0 : int ConvertToI420(VideoType src_video_type,
260 : const uint8_t* src_frame,
261 : int crop_x,
262 : int crop_y,
263 : int src_width,
264 : int src_height,
265 : size_t sample_size,
266 : VideoRotation rotation,
267 : I420Buffer* dst_buffer) {
268 0 : int dst_width = dst_buffer->width();
269 0 : int dst_height = dst_buffer->height();
270 : // LibYuv expects pre-rotation values for dst.
271 : // Stride values should correspond to the destination values.
272 0 : if (rotation == kVideoRotation_90 || rotation == kVideoRotation_270) {
273 0 : std::swap(dst_width, dst_height);
274 : }
275 0 : return libyuv::ConvertToI420(
276 : src_frame, sample_size,
277 0 : dst_buffer->MutableDataY(), dst_buffer->StrideY(),
278 0 : dst_buffer->MutableDataU(), dst_buffer->StrideU(),
279 0 : dst_buffer->MutableDataV(), dst_buffer->StrideV(),
280 : crop_x, crop_y,
281 : src_width, src_height,
282 : dst_width, dst_height,
283 : ConvertRotationMode(rotation),
284 0 : ConvertVideoType(src_video_type));
285 : }
286 :
287 0 : int ConvertFromI420(const VideoFrame& src_frame,
288 : VideoType dst_video_type,
289 : int dst_sample_size,
290 : uint8_t* dst_frame) {
291 0 : return libyuv::ConvertFromI420(
292 0 : src_frame.video_frame_buffer()->DataY(),
293 0 : src_frame.video_frame_buffer()->StrideY(),
294 0 : src_frame.video_frame_buffer()->DataU(),
295 0 : src_frame.video_frame_buffer()->StrideU(),
296 0 : src_frame.video_frame_buffer()->DataV(),
297 0 : src_frame.video_frame_buffer()->StrideV(),
298 : dst_frame, dst_sample_size,
299 : src_frame.width(), src_frame.height(),
300 0 : ConvertVideoType(dst_video_type));
301 : }
302 :
303 : // Compute PSNR for an I420 frame (all planes)
304 0 : double I420PSNR(const VideoFrameBuffer& ref_buffer,
305 : const VideoFrameBuffer& test_buffer) {
306 0 : if ((ref_buffer.width() != test_buffer.width()) ||
307 0 : (ref_buffer.height() != test_buffer.height()))
308 0 : return -1;
309 0 : else if (ref_buffer.width() < 0 || ref_buffer.height() < 0)
310 0 : return -1;
311 :
312 0 : double psnr = libyuv::I420Psnr(ref_buffer.DataY(), ref_buffer.StrideY(),
313 0 : ref_buffer.DataU(), ref_buffer.StrideU(),
314 0 : ref_buffer.DataV(), ref_buffer.StrideV(),
315 0 : test_buffer.DataY(), test_buffer.StrideY(),
316 0 : test_buffer.DataU(), test_buffer.StrideU(),
317 0 : test_buffer.DataV(), test_buffer.StrideV(),
318 0 : test_buffer.width(), test_buffer.height());
319 : // LibYuv sets the max psnr value to 128, we restrict it here.
320 : // In case of 0 mse in one frame, 128 can skew the results significantly.
321 0 : return (psnr > kPerfectPSNR) ? kPerfectPSNR : psnr;
322 : }
323 :
324 : // Compute PSNR for an I420 frame (all planes)
325 0 : double I420PSNR(const VideoFrame* ref_frame, const VideoFrame* test_frame) {
326 0 : if (!ref_frame || !test_frame)
327 0 : return -1;
328 0 : return I420PSNR(*ref_frame->video_frame_buffer(),
329 0 : *test_frame->video_frame_buffer());
330 : }
331 :
332 : // Compute SSIM for an I420 frame (all planes)
333 0 : double I420SSIM(const VideoFrameBuffer& ref_buffer,
334 : const VideoFrameBuffer& test_buffer) {
335 0 : if ((ref_buffer.width() != test_buffer.width()) ||
336 0 : (ref_buffer.height() != test_buffer.height()))
337 0 : return -1;
338 0 : else if (ref_buffer.width() < 0 || ref_buffer.height() < 0)
339 0 : return -1;
340 :
341 0 : return libyuv::I420Ssim(ref_buffer.DataY(), ref_buffer.StrideY(),
342 0 : ref_buffer.DataU(), ref_buffer.StrideU(),
343 0 : ref_buffer.DataV(), ref_buffer.StrideV(),
344 0 : test_buffer.DataY(), test_buffer.StrideY(),
345 0 : test_buffer.DataU(), test_buffer.StrideU(),
346 0 : test_buffer.DataV(), test_buffer.StrideV(),
347 0 : test_buffer.width(), test_buffer.height());
348 : }
349 0 : double I420SSIM(const VideoFrame* ref_frame, const VideoFrame* test_frame) {
350 0 : if (!ref_frame || !test_frame)
351 0 : return -1;
352 0 : return I420SSIM(*ref_frame->video_frame_buffer(),
353 0 : *test_frame->video_frame_buffer());
354 : }
355 :
356 0 : void NV12Scale(std::vector<uint8_t>* tmp_buffer,
357 : const uint8_t* src_y, int src_stride_y,
358 : const uint8_t* src_uv, int src_stride_uv,
359 : int src_width, int src_height,
360 : uint8_t* dst_y, int dst_stride_y,
361 : uint8_t* dst_uv, int dst_stride_uv,
362 : int dst_width, int dst_height) {
363 0 : const int src_chroma_width = (src_width + 1) / 2;
364 0 : const int src_chroma_height = (src_height + 1) / 2;
365 :
366 0 : if (src_width == dst_width && src_height == dst_height) {
367 : // No scaling.
368 0 : tmp_buffer->clear();
369 0 : tmp_buffer->shrink_to_fit();
370 : libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, src_width,
371 0 : src_height);
372 0 : libyuv::CopyPlane(src_uv, src_stride_uv, dst_uv, dst_stride_uv,
373 0 : src_chroma_width * 2, src_chroma_height);
374 0 : return;
375 : }
376 :
377 : // Scaling.
378 : // Allocate temporary memory for spitting UV planes and scaling them.
379 0 : const int dst_chroma_width = (dst_width + 1) / 2;
380 0 : const int dst_chroma_height = (dst_height + 1) / 2;
381 0 : tmp_buffer->resize(src_chroma_width * src_chroma_height * 2 +
382 0 : dst_chroma_width * dst_chroma_height * 2);
383 0 : tmp_buffer->shrink_to_fit();
384 :
385 0 : uint8_t* const src_u = tmp_buffer->data();
386 0 : uint8_t* const src_v = src_u + src_chroma_width * src_chroma_height;
387 0 : uint8_t* const dst_u = src_v + src_chroma_width * src_chroma_height;
388 0 : uint8_t* const dst_v = dst_u + dst_chroma_width * dst_chroma_height;
389 :
390 : // Split source UV plane into separate U and V plane using the temporary data.
391 : libyuv::SplitUVPlane(src_uv, src_stride_uv,
392 : src_u, src_chroma_width,
393 : src_v, src_chroma_width,
394 0 : src_chroma_width, src_chroma_height);
395 :
396 : // Scale the planes.
397 : libyuv::I420Scale(src_y, src_stride_y,
398 : src_u, src_chroma_width,
399 : src_v, src_chroma_width,
400 : src_width, src_height,
401 : dst_y, dst_stride_y,
402 : dst_u, dst_chroma_width,
403 : dst_v, dst_chroma_width,
404 : dst_width, dst_height,
405 0 : libyuv::kFilterBox);
406 :
407 : // Merge the UV planes into the destination.
408 : libyuv::MergeUVPlane(dst_u, dst_chroma_width,
409 : dst_v, dst_chroma_width,
410 : dst_uv, dst_stride_uv,
411 0 : dst_chroma_width, dst_chroma_height);
412 : }
413 :
414 0 : void NV12ToI420Scaler::NV12ToI420Scale(
415 : const uint8_t* src_y, int src_stride_y,
416 : const uint8_t* src_uv, int src_stride_uv,
417 : int src_width, int src_height,
418 : uint8_t* dst_y, int dst_stride_y,
419 : uint8_t* dst_u, int dst_stride_u,
420 : uint8_t* dst_v, int dst_stride_v,
421 : int dst_width, int dst_height) {
422 0 : if (src_width == dst_width && src_height == dst_height) {
423 : // No scaling.
424 0 : tmp_uv_planes_.clear();
425 0 : tmp_uv_planes_.shrink_to_fit();
426 : libyuv::NV12ToI420(
427 : src_y, src_stride_y,
428 : src_uv, src_stride_uv,
429 : dst_y, dst_stride_y,
430 : dst_u, dst_stride_u,
431 : dst_v, dst_stride_v,
432 0 : src_width, src_height);
433 0 : return;
434 : }
435 :
436 : // Scaling.
437 : // Allocate temporary memory for spitting UV planes.
438 0 : const int src_uv_width = (src_width + 1) / 2;
439 0 : const int src_uv_height = (src_height + 1) / 2;
440 0 : tmp_uv_planes_.resize(src_uv_width * src_uv_height * 2);
441 0 : tmp_uv_planes_.shrink_to_fit();
442 :
443 : // Split source UV plane into separate U and V plane using the temporary data.
444 0 : uint8_t* const src_u = tmp_uv_planes_.data();
445 0 : uint8_t* const src_v = tmp_uv_planes_.data() + src_uv_width * src_uv_height;
446 : libyuv::SplitUVPlane(src_uv, src_stride_uv,
447 : src_u, src_uv_width,
448 : src_v, src_uv_width,
449 0 : src_uv_width, src_uv_height);
450 :
451 : // Scale the planes into the destination.
452 : libyuv::I420Scale(src_y, src_stride_y,
453 : src_u, src_uv_width,
454 : src_v, src_uv_width,
455 : src_width, src_height,
456 : dst_y, dst_stride_y,
457 : dst_u, dst_stride_u,
458 : dst_v, dst_stride_v,
459 : dst_width, dst_height,
460 0 : libyuv::kFilterBox);
461 : }
462 :
463 : } // namespace webrtc
|