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