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 "libyuv/convert_argb.h" |
michael@0 | 12 | |
michael@0 | 13 | #include "libyuv/cpu_id.h" |
michael@0 | 14 | #include "libyuv/format_conversion.h" |
michael@0 | 15 | #ifdef HAVE_JPEG |
michael@0 | 16 | #include "libyuv/mjpeg_decoder.h" |
michael@0 | 17 | #endif |
michael@0 | 18 | #include "libyuv/rotate_argb.h" |
michael@0 | 19 | #include "libyuv/row.h" |
michael@0 | 20 | #include "libyuv/video_common.h" |
michael@0 | 21 | |
michael@0 | 22 | #ifdef __cplusplus |
michael@0 | 23 | namespace libyuv { |
michael@0 | 24 | extern "C" { |
michael@0 | 25 | #endif |
michael@0 | 26 | |
michael@0 | 27 | // Convert camera sample to I420 with cropping, rotation and vertical flip. |
michael@0 | 28 | // src_width is used for source stride computation |
michael@0 | 29 | // src_height is used to compute location of planes, and indicate inversion |
michael@0 | 30 | // sample_size is measured in bytes and is the size of the frame. |
michael@0 | 31 | // With MJPEG it is the compressed size of the frame. |
michael@0 | 32 | LIBYUV_API |
michael@0 | 33 | int ConvertToARGB(const uint8* sample, size_t sample_size, |
michael@0 | 34 | uint8* crop_argb, int argb_stride, |
michael@0 | 35 | int crop_x, int crop_y, |
michael@0 | 36 | int src_width, int src_height, |
michael@0 | 37 | int crop_width, int crop_height, |
michael@0 | 38 | enum RotationMode rotation, |
michael@0 | 39 | uint32 fourcc) { |
michael@0 | 40 | uint32 format = CanonicalFourCC(fourcc); |
michael@0 | 41 | int aligned_src_width = (src_width + 1) & ~1; |
michael@0 | 42 | const uint8* src; |
michael@0 | 43 | const uint8* src_uv; |
michael@0 | 44 | int abs_src_height = (src_height < 0) ? -src_height : src_height; |
michael@0 | 45 | int inv_crop_height = (crop_height < 0) ? -crop_height : crop_height; |
michael@0 | 46 | int r = 0; |
michael@0 | 47 | |
michael@0 | 48 | // One pass rotation is available for some formats. For the rest, convert |
michael@0 | 49 | // to I420 (with optional vertical flipping) into a temporary I420 buffer, |
michael@0 | 50 | // and then rotate the I420 to the final destination buffer. |
michael@0 | 51 | // For in-place conversion, if destination crop_argb is same as source sample, |
michael@0 | 52 | // also enable temporary buffer. |
michael@0 | 53 | LIBYUV_BOOL need_buf = (rotation && format != FOURCC_ARGB) || |
michael@0 | 54 | crop_argb == sample; |
michael@0 | 55 | uint8* tmp_argb = crop_argb; |
michael@0 | 56 | int tmp_argb_stride = argb_stride; |
michael@0 | 57 | uint8* rotate_buffer = NULL; |
michael@0 | 58 | int abs_crop_height = (crop_height < 0) ? -crop_height : crop_height; |
michael@0 | 59 | |
michael@0 | 60 | if (crop_argb == NULL || sample == NULL || |
michael@0 | 61 | src_width <= 0 || crop_width <= 0 || |
michael@0 | 62 | src_height == 0 || crop_height == 0) { |
michael@0 | 63 | return -1; |
michael@0 | 64 | } |
michael@0 | 65 | if (src_height < 0) { |
michael@0 | 66 | inv_crop_height = -inv_crop_height; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | if (need_buf) { |
michael@0 | 70 | int argb_size = crop_width * abs_crop_height * 4; |
michael@0 | 71 | rotate_buffer = (uint8*)malloc(argb_size); |
michael@0 | 72 | if (!rotate_buffer) { |
michael@0 | 73 | return 1; // Out of memory runtime error. |
michael@0 | 74 | } |
michael@0 | 75 | crop_argb = rotate_buffer; |
michael@0 | 76 | argb_stride = crop_width; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | switch (format) { |
michael@0 | 80 | // Single plane formats |
michael@0 | 81 | case FOURCC_YUY2: |
michael@0 | 82 | src = sample + (aligned_src_width * crop_y + crop_x) * 2; |
michael@0 | 83 | r = YUY2ToARGB(src, aligned_src_width * 2, |
michael@0 | 84 | crop_argb, argb_stride, |
michael@0 | 85 | crop_width, inv_crop_height); |
michael@0 | 86 | break; |
michael@0 | 87 | case FOURCC_UYVY: |
michael@0 | 88 | src = sample + (aligned_src_width * crop_y + crop_x) * 2; |
michael@0 | 89 | r = UYVYToARGB(src, aligned_src_width * 2, |
michael@0 | 90 | crop_argb, argb_stride, |
michael@0 | 91 | crop_width, inv_crop_height); |
michael@0 | 92 | break; |
michael@0 | 93 | case FOURCC_24BG: |
michael@0 | 94 | src = sample + (src_width * crop_y + crop_x) * 3; |
michael@0 | 95 | r = RGB24ToARGB(src, src_width * 3, |
michael@0 | 96 | crop_argb, argb_stride, |
michael@0 | 97 | crop_width, inv_crop_height); |
michael@0 | 98 | break; |
michael@0 | 99 | case FOURCC_RAW: |
michael@0 | 100 | src = sample + (src_width * crop_y + crop_x) * 3; |
michael@0 | 101 | r = RAWToARGB(src, src_width * 3, |
michael@0 | 102 | crop_argb, argb_stride, |
michael@0 | 103 | crop_width, inv_crop_height); |
michael@0 | 104 | break; |
michael@0 | 105 | case FOURCC_ARGB: |
michael@0 | 106 | src = sample + (src_width * crop_y + crop_x) * 4; |
michael@0 | 107 | r = ARGBToARGB(src, src_width * 4, |
michael@0 | 108 | crop_argb, argb_stride, |
michael@0 | 109 | crop_width, inv_crop_height); |
michael@0 | 110 | break; |
michael@0 | 111 | case FOURCC_BGRA: |
michael@0 | 112 | src = sample + (src_width * crop_y + crop_x) * 4; |
michael@0 | 113 | r = BGRAToARGB(src, src_width * 4, |
michael@0 | 114 | crop_argb, argb_stride, |
michael@0 | 115 | crop_width, inv_crop_height); |
michael@0 | 116 | break; |
michael@0 | 117 | case FOURCC_ABGR: |
michael@0 | 118 | src = sample + (src_width * crop_y + crop_x) * 4; |
michael@0 | 119 | r = ABGRToARGB(src, src_width * 4, |
michael@0 | 120 | crop_argb, argb_stride, |
michael@0 | 121 | crop_width, inv_crop_height); |
michael@0 | 122 | break; |
michael@0 | 123 | case FOURCC_RGBA: |
michael@0 | 124 | src = sample + (src_width * crop_y + crop_x) * 4; |
michael@0 | 125 | r = RGBAToARGB(src, src_width * 4, |
michael@0 | 126 | crop_argb, argb_stride, |
michael@0 | 127 | crop_width, inv_crop_height); |
michael@0 | 128 | break; |
michael@0 | 129 | case FOURCC_RGBP: |
michael@0 | 130 | src = sample + (src_width * crop_y + crop_x) * 2; |
michael@0 | 131 | r = RGB565ToARGB(src, src_width * 2, |
michael@0 | 132 | crop_argb, argb_stride, |
michael@0 | 133 | crop_width, inv_crop_height); |
michael@0 | 134 | break; |
michael@0 | 135 | case FOURCC_RGBO: |
michael@0 | 136 | src = sample + (src_width * crop_y + crop_x) * 2; |
michael@0 | 137 | r = ARGB1555ToARGB(src, src_width * 2, |
michael@0 | 138 | crop_argb, argb_stride, |
michael@0 | 139 | crop_width, inv_crop_height); |
michael@0 | 140 | break; |
michael@0 | 141 | case FOURCC_R444: |
michael@0 | 142 | src = sample + (src_width * crop_y + crop_x) * 2; |
michael@0 | 143 | r = ARGB4444ToARGB(src, src_width * 2, |
michael@0 | 144 | crop_argb, argb_stride, |
michael@0 | 145 | crop_width, inv_crop_height); |
michael@0 | 146 | break; |
michael@0 | 147 | // TODO(fbarchard): Support cropping Bayer by odd numbers |
michael@0 | 148 | // by adjusting fourcc. |
michael@0 | 149 | case FOURCC_BGGR: |
michael@0 | 150 | src = sample + (src_width * crop_y + crop_x); |
michael@0 | 151 | r = BayerBGGRToARGB(src, src_width, |
michael@0 | 152 | crop_argb, argb_stride, |
michael@0 | 153 | crop_width, inv_crop_height); |
michael@0 | 154 | break; |
michael@0 | 155 | |
michael@0 | 156 | case FOURCC_GBRG: |
michael@0 | 157 | src = sample + (src_width * crop_y + crop_x); |
michael@0 | 158 | r = BayerGBRGToARGB(src, src_width, |
michael@0 | 159 | crop_argb, argb_stride, |
michael@0 | 160 | crop_width, inv_crop_height); |
michael@0 | 161 | break; |
michael@0 | 162 | |
michael@0 | 163 | case FOURCC_GRBG: |
michael@0 | 164 | src = sample + (src_width * crop_y + crop_x); |
michael@0 | 165 | r = BayerGRBGToARGB(src, src_width, |
michael@0 | 166 | crop_argb, argb_stride, |
michael@0 | 167 | crop_width, inv_crop_height); |
michael@0 | 168 | break; |
michael@0 | 169 | |
michael@0 | 170 | case FOURCC_RGGB: |
michael@0 | 171 | src = sample + (src_width * crop_y + crop_x); |
michael@0 | 172 | r = BayerRGGBToARGB(src, src_width, |
michael@0 | 173 | crop_argb, argb_stride, |
michael@0 | 174 | crop_width, inv_crop_height); |
michael@0 | 175 | break; |
michael@0 | 176 | |
michael@0 | 177 | case FOURCC_I400: |
michael@0 | 178 | src = sample + src_width * crop_y + crop_x; |
michael@0 | 179 | r = I400ToARGB(src, src_width, |
michael@0 | 180 | crop_argb, argb_stride, |
michael@0 | 181 | crop_width, inv_crop_height); |
michael@0 | 182 | break; |
michael@0 | 183 | |
michael@0 | 184 | // Biplanar formats |
michael@0 | 185 | case FOURCC_NV12: |
michael@0 | 186 | src = sample + (src_width * crop_y + crop_x); |
michael@0 | 187 | src_uv = sample + aligned_src_width * (src_height + crop_y / 2) + crop_x; |
michael@0 | 188 | r = NV12ToARGB(src, src_width, |
michael@0 | 189 | src_uv, aligned_src_width, |
michael@0 | 190 | crop_argb, argb_stride, |
michael@0 | 191 | crop_width, inv_crop_height); |
michael@0 | 192 | break; |
michael@0 | 193 | case FOURCC_NV21: |
michael@0 | 194 | src = sample + (src_width * crop_y + crop_x); |
michael@0 | 195 | src_uv = sample + aligned_src_width * (src_height + crop_y / 2) + crop_x; |
michael@0 | 196 | // Call NV12 but with u and v parameters swapped. |
michael@0 | 197 | r = NV21ToARGB(src, src_width, |
michael@0 | 198 | src_uv, aligned_src_width, |
michael@0 | 199 | crop_argb, argb_stride, |
michael@0 | 200 | crop_width, inv_crop_height); |
michael@0 | 201 | break; |
michael@0 | 202 | case FOURCC_M420: |
michael@0 | 203 | src = sample + (src_width * crop_y) * 12 / 8 + crop_x; |
michael@0 | 204 | r = M420ToARGB(src, src_width, |
michael@0 | 205 | crop_argb, argb_stride, |
michael@0 | 206 | crop_width, inv_crop_height); |
michael@0 | 207 | break; |
michael@0 | 208 | // case FOURCC_Q420: |
michael@0 | 209 | // src = sample + (src_width + aligned_src_width * 2) * crop_y + crop_x; |
michael@0 | 210 | // src_uv = sample + (src_width + aligned_src_width * 2) * crop_y + |
michael@0 | 211 | // src_width + crop_x * 2; |
michael@0 | 212 | // r = Q420ToARGB(src, src_width * 3, |
michael@0 | 213 | // src_uv, src_width * 3, |
michael@0 | 214 | // crop_argb, argb_stride, |
michael@0 | 215 | // crop_width, inv_crop_height); |
michael@0 | 216 | // break; |
michael@0 | 217 | // Triplanar formats |
michael@0 | 218 | case FOURCC_I420: |
michael@0 | 219 | case FOURCC_YU12: |
michael@0 | 220 | case FOURCC_YV12: { |
michael@0 | 221 | const uint8* src_y = sample + (src_width * crop_y + crop_x); |
michael@0 | 222 | const uint8* src_u; |
michael@0 | 223 | const uint8* src_v; |
michael@0 | 224 | int halfwidth = (src_width + 1) / 2; |
michael@0 | 225 | int halfheight = (abs_src_height + 1) / 2; |
michael@0 | 226 | if (format == FOURCC_YV12) { |
michael@0 | 227 | src_v = sample + src_width * abs_src_height + |
michael@0 | 228 | (halfwidth * crop_y + crop_x) / 2; |
michael@0 | 229 | src_u = sample + src_width * abs_src_height + |
michael@0 | 230 | halfwidth * (halfheight + crop_y / 2) + crop_x / 2; |
michael@0 | 231 | } else { |
michael@0 | 232 | src_u = sample + src_width * abs_src_height + |
michael@0 | 233 | (halfwidth * crop_y + crop_x) / 2; |
michael@0 | 234 | src_v = sample + src_width * abs_src_height + |
michael@0 | 235 | halfwidth * (halfheight + crop_y / 2) + crop_x / 2; |
michael@0 | 236 | } |
michael@0 | 237 | r = I420ToARGB(src_y, src_width, |
michael@0 | 238 | src_u, halfwidth, |
michael@0 | 239 | src_v, halfwidth, |
michael@0 | 240 | crop_argb, argb_stride, |
michael@0 | 241 | crop_width, inv_crop_height); |
michael@0 | 242 | break; |
michael@0 | 243 | } |
michael@0 | 244 | case FOURCC_I422: |
michael@0 | 245 | case FOURCC_YV16: { |
michael@0 | 246 | const uint8* src_y = sample + src_width * crop_y + crop_x; |
michael@0 | 247 | const uint8* src_u; |
michael@0 | 248 | const uint8* src_v; |
michael@0 | 249 | int halfwidth = (src_width + 1) / 2; |
michael@0 | 250 | if (format == FOURCC_YV16) { |
michael@0 | 251 | src_v = sample + src_width * abs_src_height + |
michael@0 | 252 | halfwidth * crop_y + crop_x / 2; |
michael@0 | 253 | src_u = sample + src_width * abs_src_height + |
michael@0 | 254 | halfwidth * (abs_src_height + crop_y) + crop_x / 2; |
michael@0 | 255 | } else { |
michael@0 | 256 | src_u = sample + src_width * abs_src_height + |
michael@0 | 257 | halfwidth * crop_y + crop_x / 2; |
michael@0 | 258 | src_v = sample + src_width * abs_src_height + |
michael@0 | 259 | halfwidth * (abs_src_height + crop_y) + crop_x / 2; |
michael@0 | 260 | } |
michael@0 | 261 | r = I422ToARGB(src_y, src_width, |
michael@0 | 262 | src_u, halfwidth, |
michael@0 | 263 | src_v, halfwidth, |
michael@0 | 264 | crop_argb, argb_stride, |
michael@0 | 265 | crop_width, inv_crop_height); |
michael@0 | 266 | break; |
michael@0 | 267 | } |
michael@0 | 268 | case FOURCC_I444: |
michael@0 | 269 | case FOURCC_YV24: { |
michael@0 | 270 | const uint8* src_y = sample + src_width * crop_y + crop_x; |
michael@0 | 271 | const uint8* src_u; |
michael@0 | 272 | const uint8* src_v; |
michael@0 | 273 | if (format == FOURCC_YV24) { |
michael@0 | 274 | src_v = sample + src_width * (abs_src_height + crop_y) + crop_x; |
michael@0 | 275 | src_u = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x; |
michael@0 | 276 | } else { |
michael@0 | 277 | src_u = sample + src_width * (abs_src_height + crop_y) + crop_x; |
michael@0 | 278 | src_v = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x; |
michael@0 | 279 | } |
michael@0 | 280 | r = I444ToARGB(src_y, src_width, |
michael@0 | 281 | src_u, src_width, |
michael@0 | 282 | src_v, src_width, |
michael@0 | 283 | crop_argb, argb_stride, |
michael@0 | 284 | crop_width, inv_crop_height); |
michael@0 | 285 | break; |
michael@0 | 286 | } |
michael@0 | 287 | case FOURCC_I411: { |
michael@0 | 288 | int quarterwidth = (src_width + 3) / 4; |
michael@0 | 289 | const uint8* src_y = sample + src_width * crop_y + crop_x; |
michael@0 | 290 | const uint8* src_u = sample + src_width * abs_src_height + |
michael@0 | 291 | quarterwidth * crop_y + crop_x / 4; |
michael@0 | 292 | const uint8* src_v = sample + src_width * abs_src_height + |
michael@0 | 293 | quarterwidth * (abs_src_height + crop_y) + crop_x / 4; |
michael@0 | 294 | r = I411ToARGB(src_y, src_width, |
michael@0 | 295 | src_u, quarterwidth, |
michael@0 | 296 | src_v, quarterwidth, |
michael@0 | 297 | crop_argb, argb_stride, |
michael@0 | 298 | crop_width, inv_crop_height); |
michael@0 | 299 | break; |
michael@0 | 300 | } |
michael@0 | 301 | #ifdef HAVE_JPEG |
michael@0 | 302 | case FOURCC_MJPG: |
michael@0 | 303 | r = MJPGToARGB(sample, sample_size, |
michael@0 | 304 | crop_argb, argb_stride, |
michael@0 | 305 | src_width, abs_src_height, crop_width, inv_crop_height); |
michael@0 | 306 | break; |
michael@0 | 307 | #endif |
michael@0 | 308 | default: |
michael@0 | 309 | r = -1; // unknown fourcc - return failure code. |
michael@0 | 310 | } |
michael@0 | 311 | |
michael@0 | 312 | if (need_buf) { |
michael@0 | 313 | if (!r) { |
michael@0 | 314 | r = ARGBRotate(crop_argb, argb_stride, |
michael@0 | 315 | tmp_argb, tmp_argb_stride, |
michael@0 | 316 | crop_width, abs_crop_height, rotation); |
michael@0 | 317 | } |
michael@0 | 318 | free(rotate_buffer); |
michael@0 | 319 | } |
michael@0 | 320 | |
michael@0 | 321 | return r; |
michael@0 | 322 | } |
michael@0 | 323 | |
michael@0 | 324 | #ifdef __cplusplus |
michael@0 | 325 | } // extern "C" |
michael@0 | 326 | } // namespace libyuv |
michael@0 | 327 | #endif |