Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2011 The LibYuv Project Authors. All rights reserved. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license |
michael@0 | 5 | * that can be found in the LICENSE file in the root of the source |
michael@0 | 6 | * tree. An additional intellectual property rights grant can be found |
michael@0 | 7 | * in the file PATENTS. All contributing project authors may |
michael@0 | 8 | * be found in the AUTHORS file in the root of the source tree. |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #include <stdlib.h> |
michael@0 | 12 | |
michael@0 | 13 | #include "libyuv/convert.h" |
michael@0 | 14 | |
michael@0 | 15 | #include "libyuv/format_conversion.h" |
michael@0 | 16 | #include "libyuv/video_common.h" |
michael@0 | 17 | |
michael@0 | 18 | #ifdef __cplusplus |
michael@0 | 19 | namespace libyuv { |
michael@0 | 20 | extern "C" { |
michael@0 | 21 | #endif |
michael@0 | 22 | |
michael@0 | 23 | // Convert camera sample to I420 with cropping, rotation and vertical flip. |
michael@0 | 24 | // src_width is used for source stride computation |
michael@0 | 25 | // src_height is used to compute location of planes, and indicate inversion |
michael@0 | 26 | // sample_size is measured in bytes and is the size of the frame. |
michael@0 | 27 | // With MJPEG it is the compressed size of the frame. |
michael@0 | 28 | LIBYUV_API |
michael@0 | 29 | int ConvertToI420(const uint8* sample, |
michael@0 | 30 | size_t sample_size, |
michael@0 | 31 | uint8* y, int y_stride, |
michael@0 | 32 | uint8* u, int u_stride, |
michael@0 | 33 | uint8* v, int v_stride, |
michael@0 | 34 | int crop_x, int crop_y, |
michael@0 | 35 | int src_width, int src_height, |
michael@0 | 36 | int crop_width, int crop_height, |
michael@0 | 37 | enum RotationMode rotation, |
michael@0 | 38 | uint32 fourcc) { |
michael@0 | 39 | uint32 format = CanonicalFourCC(fourcc); |
michael@0 | 40 | int aligned_src_width = (src_width + 1) & ~1; |
michael@0 | 41 | const uint8* src; |
michael@0 | 42 | const uint8* src_uv; |
michael@0 | 43 | int abs_src_height = (src_height < 0) ? -src_height : src_height; |
michael@0 | 44 | int inv_crop_height = (crop_height < 0) ? -crop_height : crop_height; |
michael@0 | 45 | int r = 0; |
michael@0 | 46 | LIBYUV_BOOL need_buf = (rotation && format != FOURCC_I420 && |
michael@0 | 47 | format != FOURCC_NV12 && format != FOURCC_NV21 && |
michael@0 | 48 | format != FOURCC_YU12 && format != FOURCC_YV12) || y == sample; |
michael@0 | 49 | uint8* tmp_y = y; |
michael@0 | 50 | uint8* tmp_u = u; |
michael@0 | 51 | uint8* tmp_v = v; |
michael@0 | 52 | int tmp_y_stride = y_stride; |
michael@0 | 53 | int tmp_u_stride = u_stride; |
michael@0 | 54 | int tmp_v_stride = v_stride; |
michael@0 | 55 | uint8* rotate_buffer = NULL; |
michael@0 | 56 | int abs_crop_height = (crop_height < 0) ? -crop_height : crop_height; |
michael@0 | 57 | |
michael@0 | 58 | if (!y || !u || !v || !sample || |
michael@0 | 59 | src_width <= 0 || crop_width <= 0 || |
michael@0 | 60 | src_height == 0 || crop_height == 0) { |
michael@0 | 61 | return -1; |
michael@0 | 62 | } |
michael@0 | 63 | if (src_height < 0) { |
michael@0 | 64 | inv_crop_height = -inv_crop_height; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | // One pass rotation is available for some formats. For the rest, convert |
michael@0 | 68 | // to I420 (with optional vertical flipping) into a temporary I420 buffer, |
michael@0 | 69 | // and then rotate the I420 to the final destination buffer. |
michael@0 | 70 | // For in-place conversion, if destination y is same as source sample, |
michael@0 | 71 | // also enable temporary buffer. |
michael@0 | 72 | if (need_buf) { |
michael@0 | 73 | int y_size = crop_width * abs_crop_height; |
michael@0 | 74 | int uv_size = ((crop_width + 1) / 2) * ((abs_crop_height + 1) / 2); |
michael@0 | 75 | rotate_buffer = (uint8*)malloc(y_size + uv_size * 2); |
michael@0 | 76 | if (!rotate_buffer) { |
michael@0 | 77 | return 1; // Out of memory runtime error. |
michael@0 | 78 | } |
michael@0 | 79 | y = rotate_buffer; |
michael@0 | 80 | u = y + y_size; |
michael@0 | 81 | v = u + uv_size; |
michael@0 | 82 | y_stride = crop_width; |
michael@0 | 83 | u_stride = v_stride = ((crop_width + 1) / 2); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | switch (format) { |
michael@0 | 87 | // Single plane formats |
michael@0 | 88 | case FOURCC_YUY2: |
michael@0 | 89 | src = sample + (aligned_src_width * crop_y + crop_x) * 2; |
michael@0 | 90 | r = YUY2ToI420(src, aligned_src_width * 2, |
michael@0 | 91 | y, y_stride, |
michael@0 | 92 | u, u_stride, |
michael@0 | 93 | v, v_stride, |
michael@0 | 94 | crop_width, inv_crop_height); |
michael@0 | 95 | break; |
michael@0 | 96 | case FOURCC_UYVY: |
michael@0 | 97 | src = sample + (aligned_src_width * crop_y + crop_x) * 2; |
michael@0 | 98 | r = UYVYToI420(src, aligned_src_width * 2, |
michael@0 | 99 | y, y_stride, |
michael@0 | 100 | u, u_stride, |
michael@0 | 101 | v, v_stride, |
michael@0 | 102 | crop_width, inv_crop_height); |
michael@0 | 103 | break; |
michael@0 | 104 | case FOURCC_RGBP: |
michael@0 | 105 | src = sample + (src_width * crop_y + crop_x) * 2; |
michael@0 | 106 | r = RGB565ToI420(src, src_width * 2, |
michael@0 | 107 | y, y_stride, |
michael@0 | 108 | u, u_stride, |
michael@0 | 109 | v, v_stride, |
michael@0 | 110 | crop_width, inv_crop_height); |
michael@0 | 111 | break; |
michael@0 | 112 | case FOURCC_RGBO: |
michael@0 | 113 | src = sample + (src_width * crop_y + crop_x) * 2; |
michael@0 | 114 | r = ARGB1555ToI420(src, src_width * 2, |
michael@0 | 115 | y, y_stride, |
michael@0 | 116 | u, u_stride, |
michael@0 | 117 | v, v_stride, |
michael@0 | 118 | crop_width, inv_crop_height); |
michael@0 | 119 | break; |
michael@0 | 120 | case FOURCC_R444: |
michael@0 | 121 | src = sample + (src_width * crop_y + crop_x) * 2; |
michael@0 | 122 | r = ARGB4444ToI420(src, src_width * 2, |
michael@0 | 123 | y, y_stride, |
michael@0 | 124 | u, u_stride, |
michael@0 | 125 | v, v_stride, |
michael@0 | 126 | crop_width, inv_crop_height); |
michael@0 | 127 | break; |
michael@0 | 128 | case FOURCC_24BG: |
michael@0 | 129 | src = sample + (src_width * crop_y + crop_x) * 3; |
michael@0 | 130 | r = RGB24ToI420(src, src_width * 3, |
michael@0 | 131 | y, y_stride, |
michael@0 | 132 | u, u_stride, |
michael@0 | 133 | v, v_stride, |
michael@0 | 134 | crop_width, inv_crop_height); |
michael@0 | 135 | break; |
michael@0 | 136 | case FOURCC_RAW: |
michael@0 | 137 | src = sample + (src_width * crop_y + crop_x) * 3; |
michael@0 | 138 | r = RAWToI420(src, src_width * 3, |
michael@0 | 139 | y, y_stride, |
michael@0 | 140 | u, u_stride, |
michael@0 | 141 | v, v_stride, |
michael@0 | 142 | crop_width, inv_crop_height); |
michael@0 | 143 | break; |
michael@0 | 144 | case FOURCC_ARGB: |
michael@0 | 145 | src = sample + (src_width * crop_y + crop_x) * 4; |
michael@0 | 146 | r = ARGBToI420(src, src_width * 4, |
michael@0 | 147 | y, y_stride, |
michael@0 | 148 | u, u_stride, |
michael@0 | 149 | v, v_stride, |
michael@0 | 150 | crop_width, inv_crop_height); |
michael@0 | 151 | break; |
michael@0 | 152 | case FOURCC_BGRA: |
michael@0 | 153 | src = sample + (src_width * crop_y + crop_x) * 4; |
michael@0 | 154 | r = BGRAToI420(src, src_width * 4, |
michael@0 | 155 | y, y_stride, |
michael@0 | 156 | u, u_stride, |
michael@0 | 157 | v, v_stride, |
michael@0 | 158 | crop_width, inv_crop_height); |
michael@0 | 159 | break; |
michael@0 | 160 | case FOURCC_ABGR: |
michael@0 | 161 | src = sample + (src_width * crop_y + crop_x) * 4; |
michael@0 | 162 | r = ABGRToI420(src, src_width * 4, |
michael@0 | 163 | y, y_stride, |
michael@0 | 164 | u, u_stride, |
michael@0 | 165 | v, v_stride, |
michael@0 | 166 | crop_width, inv_crop_height); |
michael@0 | 167 | break; |
michael@0 | 168 | case FOURCC_RGBA: |
michael@0 | 169 | src = sample + (src_width * crop_y + crop_x) * 4; |
michael@0 | 170 | r = RGBAToI420(src, src_width * 4, |
michael@0 | 171 | y, y_stride, |
michael@0 | 172 | u, u_stride, |
michael@0 | 173 | v, v_stride, |
michael@0 | 174 | crop_width, inv_crop_height); |
michael@0 | 175 | break; |
michael@0 | 176 | // TODO(fbarchard): Support cropping Bayer by odd numbers |
michael@0 | 177 | // by adjusting fourcc. |
michael@0 | 178 | case FOURCC_BGGR: |
michael@0 | 179 | src = sample + (src_width * crop_y + crop_x); |
michael@0 | 180 | r = BayerBGGRToI420(src, src_width, |
michael@0 | 181 | y, y_stride, |
michael@0 | 182 | u, u_stride, |
michael@0 | 183 | v, v_stride, |
michael@0 | 184 | crop_width, inv_crop_height); |
michael@0 | 185 | break; |
michael@0 | 186 | case FOURCC_GBRG: |
michael@0 | 187 | src = sample + (src_width * crop_y + crop_x); |
michael@0 | 188 | r = BayerGBRGToI420(src, src_width, |
michael@0 | 189 | y, y_stride, |
michael@0 | 190 | u, u_stride, |
michael@0 | 191 | v, v_stride, |
michael@0 | 192 | crop_width, inv_crop_height); |
michael@0 | 193 | break; |
michael@0 | 194 | case FOURCC_GRBG: |
michael@0 | 195 | src = sample + (src_width * crop_y + crop_x); |
michael@0 | 196 | r = BayerGRBGToI420(src, src_width, |
michael@0 | 197 | y, y_stride, |
michael@0 | 198 | u, u_stride, |
michael@0 | 199 | v, v_stride, |
michael@0 | 200 | crop_width, inv_crop_height); |
michael@0 | 201 | break; |
michael@0 | 202 | case FOURCC_RGGB: |
michael@0 | 203 | src = sample + (src_width * crop_y + crop_x); |
michael@0 | 204 | r = BayerRGGBToI420(src, src_width, |
michael@0 | 205 | y, y_stride, |
michael@0 | 206 | u, u_stride, |
michael@0 | 207 | v, v_stride, |
michael@0 | 208 | crop_width, inv_crop_height); |
michael@0 | 209 | break; |
michael@0 | 210 | case FOURCC_I400: |
michael@0 | 211 | src = sample + src_width * crop_y + crop_x; |
michael@0 | 212 | r = I400ToI420(src, src_width, |
michael@0 | 213 | y, y_stride, |
michael@0 | 214 | u, u_stride, |
michael@0 | 215 | v, v_stride, |
michael@0 | 216 | crop_width, inv_crop_height); |
michael@0 | 217 | break; |
michael@0 | 218 | // Biplanar formats |
michael@0 | 219 | case FOURCC_NV12: |
michael@0 | 220 | src = sample + (src_width * crop_y + crop_x); |
michael@0 | 221 | src_uv = sample + aligned_src_width * (src_height + crop_y / 2) + crop_x; |
michael@0 | 222 | r = NV12ToI420Rotate(src, src_width, |
michael@0 | 223 | src_uv, aligned_src_width, |
michael@0 | 224 | y, y_stride, |
michael@0 | 225 | u, u_stride, |
michael@0 | 226 | v, v_stride, |
michael@0 | 227 | crop_width, inv_crop_height, rotation); |
michael@0 | 228 | break; |
michael@0 | 229 | case FOURCC_NV21: |
michael@0 | 230 | src = sample + (src_width * crop_y + crop_x); |
michael@0 | 231 | src_uv = sample + aligned_src_width * (src_height + crop_y / 2) + crop_x; |
michael@0 | 232 | // Call NV12 but with u and v parameters swapped. |
michael@0 | 233 | r = NV12ToI420Rotate(src, src_width, |
michael@0 | 234 | src_uv, aligned_src_width, |
michael@0 | 235 | y, y_stride, |
michael@0 | 236 | v, v_stride, |
michael@0 | 237 | u, u_stride, |
michael@0 | 238 | crop_width, inv_crop_height, rotation); |
michael@0 | 239 | break; |
michael@0 | 240 | case FOURCC_M420: |
michael@0 | 241 | src = sample + (src_width * crop_y) * 12 / 8 + crop_x; |
michael@0 | 242 | r = M420ToI420(src, src_width, |
michael@0 | 243 | y, y_stride, |
michael@0 | 244 | u, u_stride, |
michael@0 | 245 | v, v_stride, |
michael@0 | 246 | crop_width, inv_crop_height); |
michael@0 | 247 | break; |
michael@0 | 248 | case FOURCC_Q420: |
michael@0 | 249 | src = sample + (src_width + aligned_src_width * 2) * crop_y + crop_x; |
michael@0 | 250 | src_uv = sample + (src_width + aligned_src_width * 2) * crop_y + |
michael@0 | 251 | src_width + crop_x * 2; |
michael@0 | 252 | r = Q420ToI420(src, src_width * 3, |
michael@0 | 253 | src_uv, src_width * 3, |
michael@0 | 254 | y, y_stride, |
michael@0 | 255 | u, u_stride, |
michael@0 | 256 | v, v_stride, |
michael@0 | 257 | crop_width, inv_crop_height); |
michael@0 | 258 | break; |
michael@0 | 259 | // Triplanar formats |
michael@0 | 260 | case FOURCC_I420: |
michael@0 | 261 | case FOURCC_YU12: |
michael@0 | 262 | case FOURCC_YV12: { |
michael@0 | 263 | const uint8* src_y = sample + (src_width * crop_y + crop_x); |
michael@0 | 264 | const uint8* src_u; |
michael@0 | 265 | const uint8* src_v; |
michael@0 | 266 | int halfwidth = (src_width + 1) / 2; |
michael@0 | 267 | int halfheight = (abs_src_height + 1) / 2; |
michael@0 | 268 | if (format == FOURCC_YV12) { |
michael@0 | 269 | src_v = sample + src_width * abs_src_height + |
michael@0 | 270 | (halfwidth * crop_y + crop_x) / 2; |
michael@0 | 271 | src_u = sample + src_width * abs_src_height + |
michael@0 | 272 | halfwidth * (halfheight + crop_y / 2) + crop_x / 2; |
michael@0 | 273 | } else { |
michael@0 | 274 | src_u = sample + src_width * abs_src_height + |
michael@0 | 275 | (halfwidth * crop_y + crop_x) / 2; |
michael@0 | 276 | src_v = sample + src_width * abs_src_height + |
michael@0 | 277 | halfwidth * (halfheight + crop_y / 2) + crop_x / 2; |
michael@0 | 278 | } |
michael@0 | 279 | r = I420Rotate(src_y, src_width, |
michael@0 | 280 | src_u, halfwidth, |
michael@0 | 281 | src_v, halfwidth, |
michael@0 | 282 | y, y_stride, |
michael@0 | 283 | u, u_stride, |
michael@0 | 284 | v, v_stride, |
michael@0 | 285 | crop_width, inv_crop_height, rotation); |
michael@0 | 286 | break; |
michael@0 | 287 | } |
michael@0 | 288 | case FOURCC_I422: |
michael@0 | 289 | case FOURCC_YV16: { |
michael@0 | 290 | const uint8* src_y = sample + src_width * crop_y + crop_x; |
michael@0 | 291 | const uint8* src_u; |
michael@0 | 292 | const uint8* src_v; |
michael@0 | 293 | int halfwidth = (src_width + 1) / 2; |
michael@0 | 294 | if (format == FOURCC_YV16) { |
michael@0 | 295 | src_v = sample + src_width * abs_src_height + |
michael@0 | 296 | halfwidth * crop_y + crop_x / 2; |
michael@0 | 297 | src_u = sample + src_width * abs_src_height + |
michael@0 | 298 | halfwidth * (abs_src_height + crop_y) + crop_x / 2; |
michael@0 | 299 | } else { |
michael@0 | 300 | src_u = sample + src_width * abs_src_height + |
michael@0 | 301 | halfwidth * crop_y + crop_x / 2; |
michael@0 | 302 | src_v = sample + src_width * abs_src_height + |
michael@0 | 303 | halfwidth * (abs_src_height + crop_y) + crop_x / 2; |
michael@0 | 304 | } |
michael@0 | 305 | r = I422ToI420(src_y, src_width, |
michael@0 | 306 | src_u, halfwidth, |
michael@0 | 307 | src_v, halfwidth, |
michael@0 | 308 | y, y_stride, |
michael@0 | 309 | u, u_stride, |
michael@0 | 310 | v, v_stride, |
michael@0 | 311 | crop_width, inv_crop_height); |
michael@0 | 312 | break; |
michael@0 | 313 | } |
michael@0 | 314 | case FOURCC_I444: |
michael@0 | 315 | case FOURCC_YV24: { |
michael@0 | 316 | const uint8* src_y = sample + src_width * crop_y + crop_x; |
michael@0 | 317 | const uint8* src_u; |
michael@0 | 318 | const uint8* src_v; |
michael@0 | 319 | if (format == FOURCC_YV24) { |
michael@0 | 320 | src_v = sample + src_width * (abs_src_height + crop_y) + crop_x; |
michael@0 | 321 | src_u = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x; |
michael@0 | 322 | } else { |
michael@0 | 323 | src_u = sample + src_width * (abs_src_height + crop_y) + crop_x; |
michael@0 | 324 | src_v = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x; |
michael@0 | 325 | } |
michael@0 | 326 | r = I444ToI420(src_y, src_width, |
michael@0 | 327 | src_u, src_width, |
michael@0 | 328 | src_v, src_width, |
michael@0 | 329 | y, y_stride, |
michael@0 | 330 | u, u_stride, |
michael@0 | 331 | v, v_stride, |
michael@0 | 332 | crop_width, inv_crop_height); |
michael@0 | 333 | break; |
michael@0 | 334 | } |
michael@0 | 335 | case FOURCC_I411: { |
michael@0 | 336 | int quarterwidth = (src_width + 3) / 4; |
michael@0 | 337 | const uint8* src_y = sample + src_width * crop_y + crop_x; |
michael@0 | 338 | const uint8* src_u = sample + src_width * abs_src_height + |
michael@0 | 339 | quarterwidth * crop_y + crop_x / 4; |
michael@0 | 340 | const uint8* src_v = sample + src_width * abs_src_height + |
michael@0 | 341 | quarterwidth * (abs_src_height + crop_y) + crop_x / 4; |
michael@0 | 342 | r = I411ToI420(src_y, src_width, |
michael@0 | 343 | src_u, quarterwidth, |
michael@0 | 344 | src_v, quarterwidth, |
michael@0 | 345 | y, y_stride, |
michael@0 | 346 | u, u_stride, |
michael@0 | 347 | v, v_stride, |
michael@0 | 348 | crop_width, inv_crop_height); |
michael@0 | 349 | break; |
michael@0 | 350 | } |
michael@0 | 351 | #ifdef HAVE_JPEG |
michael@0 | 352 | case FOURCC_MJPG: |
michael@0 | 353 | r = MJPGToI420(sample, sample_size, |
michael@0 | 354 | y, y_stride, |
michael@0 | 355 | u, u_stride, |
michael@0 | 356 | v, v_stride, |
michael@0 | 357 | src_width, abs_src_height, crop_width, inv_crop_height); |
michael@0 | 358 | break; |
michael@0 | 359 | #endif |
michael@0 | 360 | default: |
michael@0 | 361 | r = -1; // unknown fourcc - return failure code. |
michael@0 | 362 | } |
michael@0 | 363 | |
michael@0 | 364 | if (need_buf) { |
michael@0 | 365 | if (!r) { |
michael@0 | 366 | r = I420Rotate(y, y_stride, |
michael@0 | 367 | u, u_stride, |
michael@0 | 368 | v, v_stride, |
michael@0 | 369 | tmp_y, tmp_y_stride, |
michael@0 | 370 | tmp_u, tmp_u_stride, |
michael@0 | 371 | tmp_v, tmp_v_stride, |
michael@0 | 372 | crop_width, abs_crop_height, rotation); |
michael@0 | 373 | } |
michael@0 | 374 | free(rotate_buffer); |
michael@0 | 375 | } |
michael@0 | 376 | |
michael@0 | 377 | return r; |
michael@0 | 378 | } |
michael@0 | 379 | |
michael@0 | 380 | #ifdef __cplusplus |
michael@0 | 381 | } // extern "C" |
michael@0 | 382 | } // namespace libyuv |
michael@0 | 383 | #endif |