media/libyuv/source/format_conversion.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libyuv/source/format_conversion.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,538 @@
     1.4 +/*
     1.5 + *  Copyright 2011 The LibYuv Project Authors. All rights reserved.
     1.6 + *
     1.7 + *  Use of this source code is governed by a BSD-style license
     1.8 + *  that can be found in the LICENSE file in the root of the source
     1.9 + *  tree. An additional intellectual property rights grant can be found
    1.10 + *  in the file PATENTS. All contributing project authors may
    1.11 + *  be found in the AUTHORS file in the root of the source tree.
    1.12 + */
    1.13 +
    1.14 +#include "libyuv/format_conversion.h"
    1.15 +
    1.16 +#include "libyuv/basic_types.h"
    1.17 +#include "libyuv/cpu_id.h"
    1.18 +#include "libyuv/video_common.h"
    1.19 +#include "libyuv/row.h"
    1.20 +
    1.21 +#ifdef __cplusplus
    1.22 +namespace libyuv {
    1.23 +extern "C" {
    1.24 +#endif
    1.25 +
    1.26 +// generate a selector mask useful for pshufb
    1.27 +static uint32 GenerateSelector(int select0, int select1) {
    1.28 +  return (uint32)(select0) |
    1.29 +         (uint32)((select1 + 4) << 8) |
    1.30 +         (uint32)((select0 + 8) << 16) |
    1.31 +         (uint32)((select1 + 12) << 24);
    1.32 +}
    1.33 +
    1.34 +static int MakeSelectors(const int blue_index,
    1.35 +                         const int green_index,
    1.36 +                         const int red_index,
    1.37 +                         uint32 dst_fourcc_bayer,
    1.38 +                         uint32* index_map) {
    1.39 +  // Now build a lookup table containing the indices for the four pixels in each
    1.40 +  // 2x2 Bayer grid.
    1.41 +  switch (dst_fourcc_bayer) {
    1.42 +    case FOURCC_BGGR:
    1.43 +      index_map[0] = GenerateSelector(blue_index, green_index);
    1.44 +      index_map[1] = GenerateSelector(green_index, red_index);
    1.45 +      break;
    1.46 +    case FOURCC_GBRG:
    1.47 +      index_map[0] = GenerateSelector(green_index, blue_index);
    1.48 +      index_map[1] = GenerateSelector(red_index, green_index);
    1.49 +      break;
    1.50 +    case FOURCC_RGGB:
    1.51 +      index_map[0] = GenerateSelector(red_index, green_index);
    1.52 +      index_map[1] = GenerateSelector(green_index, blue_index);
    1.53 +      break;
    1.54 +    case FOURCC_GRBG:
    1.55 +      index_map[0] = GenerateSelector(green_index, red_index);
    1.56 +      index_map[1] = GenerateSelector(blue_index, green_index);
    1.57 +      break;
    1.58 +    default:
    1.59 +      return -1;  // Bad FourCC
    1.60 +  }
    1.61 +  return 0;
    1.62 +}
    1.63 +
    1.64 +// Converts 32 bit ARGB to Bayer RGB formats.
    1.65 +LIBYUV_API
    1.66 +int ARGBToBayer(const uint8* src_argb, int src_stride_argb,
    1.67 +                uint8* dst_bayer, int dst_stride_bayer,
    1.68 +                int width, int height,
    1.69 +                uint32 dst_fourcc_bayer) {
    1.70 +  if (height < 0) {
    1.71 +    height = -height;
    1.72 +    src_argb = src_argb + (height - 1) * src_stride_argb;
    1.73 +    src_stride_argb = -src_stride_argb;
    1.74 +  }
    1.75 +  void (*ARGBToBayerRow)(const uint8* src_argb, uint8* dst_bayer,
    1.76 +                         uint32 selector, int pix) = ARGBToBayerRow_C;
    1.77 +#if defined(HAS_ARGBTOBAYERROW_SSSE3)
    1.78 +  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8 &&
    1.79 +      IS_ALIGNED(src_argb, 16) && IS_ALIGNED(src_stride_argb, 16)) {
    1.80 +    ARGBToBayerRow = ARGBToBayerRow_Any_SSSE3;
    1.81 +    if (IS_ALIGNED(width, 8)) {
    1.82 +      ARGBToBayerRow = ARGBToBayerRow_SSSE3;
    1.83 +    }
    1.84 +  }
    1.85 +#elif defined(HAS_ARGBTOBAYERROW_NEON)
    1.86 +  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    1.87 +    ARGBToBayerRow = ARGBToBayerRow_Any_NEON;
    1.88 +    if (IS_ALIGNED(width, 8)) {
    1.89 +      ARGBToBayerRow = ARGBToBayerRow_NEON;
    1.90 +    }
    1.91 +  }
    1.92 +#endif
    1.93 +  const int blue_index = 0;  // Offsets for ARGB format
    1.94 +  const int green_index = 1;
    1.95 +  const int red_index = 2;
    1.96 +  uint32 index_map[2];
    1.97 +  if (MakeSelectors(blue_index, green_index, red_index,
    1.98 +                    dst_fourcc_bayer, index_map)) {
    1.99 +    return -1;  // Bad FourCC
   1.100 +  }
   1.101 +
   1.102 +  for (int y = 0; y < height; ++y) {
   1.103 +    ARGBToBayerRow(src_argb, dst_bayer, index_map[y & 1], width);
   1.104 +    src_argb += src_stride_argb;
   1.105 +    dst_bayer += dst_stride_bayer;
   1.106 +  }
   1.107 +  return 0;
   1.108 +}
   1.109 +
   1.110 +#define AVG(a, b) (((a) + (b)) >> 1)
   1.111 +
   1.112 +static void BayerRowBG(const uint8* src_bayer0, int src_stride_bayer,
   1.113 +                       uint8* dst_argb, int pix) {
   1.114 +  const uint8* src_bayer1 = src_bayer0 + src_stride_bayer;
   1.115 +  uint8 g = src_bayer0[1];
   1.116 +  uint8 r = src_bayer1[1];
   1.117 +  for (int x = 0; x < pix - 2; x += 2) {
   1.118 +    dst_argb[0] = src_bayer0[0];
   1.119 +    dst_argb[1] = AVG(g, src_bayer0[1]);
   1.120 +    dst_argb[2] = AVG(r, src_bayer1[1]);
   1.121 +    dst_argb[3] = 255U;
   1.122 +    dst_argb[4] = AVG(src_bayer0[0], src_bayer0[2]);
   1.123 +    dst_argb[5] = src_bayer0[1];
   1.124 +    dst_argb[6] = src_bayer1[1];
   1.125 +    dst_argb[7] = 255U;
   1.126 +    g = src_bayer0[1];
   1.127 +    r = src_bayer1[1];
   1.128 +    src_bayer0 += 2;
   1.129 +    src_bayer1 += 2;
   1.130 +    dst_argb += 8;
   1.131 +  }
   1.132 +  dst_argb[0] = src_bayer0[0];
   1.133 +  dst_argb[1] = AVG(g, src_bayer0[1]);
   1.134 +  dst_argb[2] = AVG(r, src_bayer1[1]);
   1.135 +  dst_argb[3] = 255U;
   1.136 +  if (!(pix & 1)) {
   1.137 +    dst_argb[4] = src_bayer0[0];
   1.138 +    dst_argb[5] = src_bayer0[1];
   1.139 +    dst_argb[6] = src_bayer1[1];
   1.140 +    dst_argb[7] = 255U;
   1.141 +  }
   1.142 +}
   1.143 +
   1.144 +static void BayerRowRG(const uint8* src_bayer0, int src_stride_bayer,
   1.145 +                       uint8* dst_argb, int pix) {
   1.146 +  const uint8* src_bayer1 = src_bayer0 + src_stride_bayer;
   1.147 +  uint8 g = src_bayer0[1];
   1.148 +  uint8 b = src_bayer1[1];
   1.149 +  for (int x = 0; x < pix - 2; x += 2) {
   1.150 +    dst_argb[0] = AVG(b, src_bayer1[1]);
   1.151 +    dst_argb[1] = AVG(g, src_bayer0[1]);
   1.152 +    dst_argb[2] = src_bayer0[0];
   1.153 +    dst_argb[3] = 255U;
   1.154 +    dst_argb[4] = src_bayer1[1];
   1.155 +    dst_argb[5] = src_bayer0[1];
   1.156 +    dst_argb[6] = AVG(src_bayer0[0], src_bayer0[2]);
   1.157 +    dst_argb[7] = 255U;
   1.158 +    g = src_bayer0[1];
   1.159 +    b = src_bayer1[1];
   1.160 +    src_bayer0 += 2;
   1.161 +    src_bayer1 += 2;
   1.162 +    dst_argb += 8;
   1.163 +  }
   1.164 +  dst_argb[0] = AVG(b, src_bayer1[1]);
   1.165 +  dst_argb[1] = AVG(g, src_bayer0[1]);
   1.166 +  dst_argb[2] = src_bayer0[0];
   1.167 +  dst_argb[3] = 255U;
   1.168 +  if (!(pix & 1)) {
   1.169 +    dst_argb[4] = src_bayer1[1];
   1.170 +    dst_argb[5] = src_bayer0[1];
   1.171 +    dst_argb[6] = src_bayer0[0];
   1.172 +    dst_argb[7] = 255U;
   1.173 +  }
   1.174 +}
   1.175 +
   1.176 +static void BayerRowGB(const uint8* src_bayer0, int src_stride_bayer,
   1.177 +                       uint8* dst_argb, int pix) {
   1.178 +  const uint8* src_bayer1 = src_bayer0 + src_stride_bayer;
   1.179 +  uint8 b = src_bayer0[1];
   1.180 +  for (int x = 0; x < pix - 2; x += 2) {
   1.181 +    dst_argb[0] = AVG(b, src_bayer0[1]);
   1.182 +    dst_argb[1] = src_bayer0[0];
   1.183 +    dst_argb[2] = src_bayer1[0];
   1.184 +    dst_argb[3] = 255U;
   1.185 +    dst_argb[4] = src_bayer0[1];
   1.186 +    dst_argb[5] = AVG(src_bayer0[0], src_bayer0[2]);
   1.187 +    dst_argb[6] = AVG(src_bayer1[0], src_bayer1[2]);
   1.188 +    dst_argb[7] = 255U;
   1.189 +    b = src_bayer0[1];
   1.190 +    src_bayer0 += 2;
   1.191 +    src_bayer1 += 2;
   1.192 +    dst_argb += 8;
   1.193 +  }
   1.194 +  dst_argb[0] = AVG(b, src_bayer0[1]);
   1.195 +  dst_argb[1] = src_bayer0[0];
   1.196 +  dst_argb[2] = src_bayer1[0];
   1.197 +  dst_argb[3] = 255U;
   1.198 +  if (!(pix & 1)) {
   1.199 +    dst_argb[4] = src_bayer0[1];
   1.200 +    dst_argb[5] = src_bayer0[0];
   1.201 +    dst_argb[6] = src_bayer1[0];
   1.202 +    dst_argb[7] = 255U;
   1.203 +  }
   1.204 +}
   1.205 +
   1.206 +static void BayerRowGR(const uint8* src_bayer0, int src_stride_bayer,
   1.207 +                       uint8* dst_argb, int pix) {
   1.208 +  const uint8* src_bayer1 = src_bayer0 + src_stride_bayer;
   1.209 +  uint8 r = src_bayer0[1];
   1.210 +  for (int x = 0; x < pix - 2; x += 2) {
   1.211 +    dst_argb[0] = src_bayer1[0];
   1.212 +    dst_argb[1] = src_bayer0[0];
   1.213 +    dst_argb[2] = AVG(r, src_bayer0[1]);
   1.214 +    dst_argb[3] = 255U;
   1.215 +    dst_argb[4] = AVG(src_bayer1[0], src_bayer1[2]);
   1.216 +    dst_argb[5] = AVG(src_bayer0[0], src_bayer0[2]);
   1.217 +    dst_argb[6] = src_bayer0[1];
   1.218 +    dst_argb[7] = 255U;
   1.219 +    r = src_bayer0[1];
   1.220 +    src_bayer0 += 2;
   1.221 +    src_bayer1 += 2;
   1.222 +    dst_argb += 8;
   1.223 +  }
   1.224 +  dst_argb[0] = src_bayer1[0];
   1.225 +  dst_argb[1] = src_bayer0[0];
   1.226 +  dst_argb[2] = AVG(r, src_bayer0[1]);
   1.227 +  dst_argb[3] = 255U;
   1.228 +  if (!(pix & 1)) {
   1.229 +    dst_argb[4] = src_bayer1[0];
   1.230 +    dst_argb[5] = src_bayer0[0];
   1.231 +    dst_argb[6] = src_bayer0[1];
   1.232 +    dst_argb[7] = 255U;
   1.233 +  }
   1.234 +}
   1.235 +
   1.236 +// Converts any Bayer RGB format to ARGB.
   1.237 +LIBYUV_API
   1.238 +int BayerToARGB(const uint8* src_bayer, int src_stride_bayer,
   1.239 +                uint8* dst_argb, int dst_stride_argb,
   1.240 +                int width, int height,
   1.241 +                uint32 src_fourcc_bayer) {
   1.242 +  if (height < 0) {
   1.243 +    height = -height;
   1.244 +    dst_argb = dst_argb + (height - 1) * dst_stride_argb;
   1.245 +    dst_stride_argb = -dst_stride_argb;
   1.246 +  }
   1.247 +  void (*BayerRow0)(const uint8* src_bayer, int src_stride_bayer,
   1.248 +                    uint8* dst_argb, int pix);
   1.249 +  void (*BayerRow1)(const uint8* src_bayer, int src_stride_bayer,
   1.250 +                    uint8* dst_argb, int pix);
   1.251 +  switch (src_fourcc_bayer) {
   1.252 +    case FOURCC_BGGR:
   1.253 +      BayerRow0 = BayerRowBG;
   1.254 +      BayerRow1 = BayerRowGR;
   1.255 +      break;
   1.256 +    case FOURCC_GBRG:
   1.257 +      BayerRow0 = BayerRowGB;
   1.258 +      BayerRow1 = BayerRowRG;
   1.259 +      break;
   1.260 +    case FOURCC_GRBG:
   1.261 +      BayerRow0 = BayerRowGR;
   1.262 +      BayerRow1 = BayerRowBG;
   1.263 +      break;
   1.264 +    case FOURCC_RGGB:
   1.265 +      BayerRow0 = BayerRowRG;
   1.266 +      BayerRow1 = BayerRowGB;
   1.267 +      break;
   1.268 +    default:
   1.269 +      return -1;    // Bad FourCC
   1.270 +  }
   1.271 +
   1.272 +  for (int y = 0; y < height - 1; y += 2) {
   1.273 +    BayerRow0(src_bayer, src_stride_bayer, dst_argb, width);
   1.274 +    BayerRow1(src_bayer + src_stride_bayer, -src_stride_bayer,
   1.275 +              dst_argb + dst_stride_argb, width);
   1.276 +    src_bayer += src_stride_bayer * 2;
   1.277 +    dst_argb += dst_stride_argb * 2;
   1.278 +  }
   1.279 +  if (height & 1) {
   1.280 +    BayerRow0(src_bayer, src_stride_bayer, dst_argb, width);
   1.281 +  }
   1.282 +  return 0;
   1.283 +}
   1.284 +
   1.285 +// Converts any Bayer RGB format to ARGB.
   1.286 +LIBYUV_API
   1.287 +int BayerToI420(const uint8* src_bayer, int src_stride_bayer,
   1.288 +                uint8* dst_y, int dst_stride_y,
   1.289 +                uint8* dst_u, int dst_stride_u,
   1.290 +                uint8* dst_v, int dst_stride_v,
   1.291 +                int width, int height,
   1.292 +                uint32 src_fourcc_bayer) {
   1.293 +  // Negative height means invert the image.
   1.294 +  if (height < 0) {
   1.295 +    height = -height;
   1.296 +    int halfheight = (height + 1) >> 1;
   1.297 +    dst_y = dst_y + (height - 1) * dst_stride_y;
   1.298 +    dst_u = dst_u + (halfheight - 1) * dst_stride_u;
   1.299 +    dst_v = dst_v + (halfheight - 1) * dst_stride_v;
   1.300 +    dst_stride_y = -dst_stride_y;
   1.301 +    dst_stride_u = -dst_stride_u;
   1.302 +    dst_stride_v = -dst_stride_v;
   1.303 +  }
   1.304 +  void (*BayerRow0)(const uint8* src_bayer, int src_stride_bayer,
   1.305 +                    uint8* dst_argb, int pix);
   1.306 +  void (*BayerRow1)(const uint8* src_bayer, int src_stride_bayer,
   1.307 +                    uint8* dst_argb, int pix);
   1.308 +
   1.309 +  void (*ARGBToUVRow)(const uint8* src_argb0, int src_stride_argb,
   1.310 +                      uint8* dst_u, uint8* dst_v, int width) = ARGBToUVRow_C;
   1.311 +  void (*ARGBToYRow)(const uint8* src_argb, uint8* dst_y, int pix) =
   1.312 +      ARGBToYRow_C;
   1.313 +#if defined(HAS_ARGBTOYROW_SSSE3) && defined(HAS_ARGBTOUVROW_SSSE3)
   1.314 +  if (TestCpuFlag(kCpuHasSSSE3) && width >= 16) {
   1.315 +    ARGBToUVRow = ARGBToUVRow_Any_SSSE3;
   1.316 +    ARGBToYRow = ARGBToYRow_Any_SSSE3;
   1.317 +    if (IS_ALIGNED(width, 16)) {
   1.318 +      ARGBToYRow = ARGBToYRow_Unaligned_SSSE3;
   1.319 +      ARGBToUVRow = ARGBToUVRow_SSSE3;
   1.320 +      if (IS_ALIGNED(dst_y, 16) && IS_ALIGNED(dst_stride_y, 16)) {
   1.321 +        ARGBToYRow = ARGBToYRow_SSSE3;
   1.322 +      }
   1.323 +    }
   1.324 +  }
   1.325 +#elif defined(HAS_ARGBTOYROW_NEON)
   1.326 +  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
   1.327 +    ARGBToYRow = ARGBToYRow_Any_NEON;
   1.328 +    if (IS_ALIGNED(width, 8)) {
   1.329 +      ARGBToYRow = ARGBToYRow_NEON;
   1.330 +    }
   1.331 +    if (width >= 16) {
   1.332 +      ARGBToUVRow = ARGBToUVRow_Any_NEON;
   1.333 +      if (IS_ALIGNED(width, 16)) {
   1.334 +        ARGBToUVRow = ARGBToUVRow_NEON;
   1.335 +      }
   1.336 +    }
   1.337 +  }
   1.338 +#endif
   1.339 +
   1.340 +  switch (src_fourcc_bayer) {
   1.341 +    case FOURCC_BGGR:
   1.342 +      BayerRow0 = BayerRowBG;
   1.343 +      BayerRow1 = BayerRowGR;
   1.344 +      break;
   1.345 +    case FOURCC_GBRG:
   1.346 +      BayerRow0 = BayerRowGB;
   1.347 +      BayerRow1 = BayerRowRG;
   1.348 +      break;
   1.349 +    case FOURCC_GRBG:
   1.350 +      BayerRow0 = BayerRowGR;
   1.351 +      BayerRow1 = BayerRowBG;
   1.352 +      break;
   1.353 +    case FOURCC_RGGB:
   1.354 +      BayerRow0 = BayerRowRG;
   1.355 +      BayerRow1 = BayerRowGB;
   1.356 +      break;
   1.357 +    default:
   1.358 +      return -1;  // Bad FourCC
   1.359 +  }
   1.360 +
   1.361 +  // Allocate 2 rows of ARGB.
   1.362 +  const int kRowSize = (width * 4 + 15) & ~15;
   1.363 +  align_buffer_64(row, kRowSize * 2);
   1.364 +  for (int y = 0; y < height - 1; y += 2) {
   1.365 +    BayerRow0(src_bayer, src_stride_bayer, row, width);
   1.366 +    BayerRow1(src_bayer + src_stride_bayer, -src_stride_bayer,
   1.367 +              row + kRowSize, width);
   1.368 +    ARGBToUVRow(row, kRowSize, dst_u, dst_v, width);
   1.369 +    ARGBToYRow(row, dst_y, width);
   1.370 +    ARGBToYRow(row + kRowSize, dst_y + dst_stride_y, width);
   1.371 +    src_bayer += src_stride_bayer * 2;
   1.372 +    dst_y += dst_stride_y * 2;
   1.373 +    dst_u += dst_stride_u;
   1.374 +    dst_v += dst_stride_v;
   1.375 +  }
   1.376 +  if (height & 1) {
   1.377 +    BayerRow0(src_bayer, src_stride_bayer, row, width);
   1.378 +    ARGBToUVRow(row, 0, dst_u, dst_v, width);
   1.379 +    ARGBToYRow(row, dst_y, width);
   1.380 +  }
   1.381 +  free_aligned_buffer_64(row);
   1.382 +  return 0;
   1.383 +}
   1.384 +
   1.385 +// Convert I420 to Bayer.
   1.386 +LIBYUV_API
   1.387 +int I420ToBayer(const uint8* src_y, int src_stride_y,
   1.388 +                const uint8* src_u, int src_stride_u,
   1.389 +                const uint8* src_v, int src_stride_v,
   1.390 +                uint8* dst_bayer, int dst_stride_bayer,
   1.391 +                int width, int height,
   1.392 +                uint32 dst_fourcc_bayer) {
   1.393 +  // Negative height means invert the image.
   1.394 +  if (height < 0) {
   1.395 +    height = -height;
   1.396 +    int halfheight = (height + 1) >> 1;
   1.397 +    src_y = src_y + (height - 1) * src_stride_y;
   1.398 +    src_u = src_u + (halfheight - 1) * src_stride_u;
   1.399 +    src_v = src_v + (halfheight - 1) * src_stride_v;
   1.400 +    src_stride_y = -src_stride_y;
   1.401 +    src_stride_u = -src_stride_u;
   1.402 +    src_stride_v = -src_stride_v;
   1.403 +  }
   1.404 +  void (*I422ToARGBRow)(const uint8* y_buf,
   1.405 +                        const uint8* u_buf,
   1.406 +                        const uint8* v_buf,
   1.407 +                        uint8* rgb_buf,
   1.408 +                        int width) = I422ToARGBRow_C;
   1.409 +#if defined(HAS_I422TOARGBROW_SSSE3)
   1.410 +  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) {
   1.411 +    I422ToARGBRow = I422ToARGBRow_Any_SSSE3;
   1.412 +    if (IS_ALIGNED(width, 8)) {
   1.413 +      I422ToARGBRow = I422ToARGBRow_SSSE3;
   1.414 +    }
   1.415 +  }
   1.416 +#endif
   1.417 +#if defined(HAS_I422TOARGBROW_AVX2)
   1.418 +  if (TestCpuFlag(kCpuHasAVX2) && width >= 16) {
   1.419 +    I422ToARGBRow = I422ToARGBRow_Any_AVX2;
   1.420 +    if (IS_ALIGNED(width, 16)) {
   1.421 +      I422ToARGBRow = I422ToARGBRow_AVX2;
   1.422 +    }
   1.423 +  }
   1.424 +#endif
   1.425 +#if defined(HAS_I422TOARGBROW_NEON)
   1.426 +  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
   1.427 +    I422ToARGBRow = I422ToARGBRow_Any_NEON;
   1.428 +    if (IS_ALIGNED(width, 8)) {
   1.429 +      I422ToARGBRow = I422ToARGBRow_NEON;
   1.430 +    }
   1.431 +  }
   1.432 +#endif
   1.433 +#if defined(HAS_I422TOARGBROW_MIPS_DSPR2)
   1.434 +  if (TestCpuFlag(kCpuHasMIPS_DSPR2) && IS_ALIGNED(width, 4) &&
   1.435 +      IS_ALIGNED(src_y, 4) && IS_ALIGNED(src_stride_y, 4) &&
   1.436 +      IS_ALIGNED(src_u, 2) && IS_ALIGNED(src_stride_u, 2) &&
   1.437 +      IS_ALIGNED(src_v, 2) && IS_ALIGNED(src_stride_v, 2)) {
   1.438 +    I422ToARGBRow = I422ToARGBRow_MIPS_DSPR2;
   1.439 +  }
   1.440 +#endif
   1.441 +
   1.442 +  void (*ARGBToBayerRow)(const uint8* src_argb, uint8* dst_bayer,
   1.443 +                         uint32 selector, int pix) = ARGBToBayerRow_C;
   1.444 +#if defined(HAS_ARGBTOBAYERROW_SSSE3)
   1.445 +  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) {
   1.446 +    ARGBToBayerRow = ARGBToBayerRow_Any_SSSE3;
   1.447 +    if (IS_ALIGNED(width, 8)) {
   1.448 +      ARGBToBayerRow = ARGBToBayerRow_SSSE3;
   1.449 +    }
   1.450 +  }
   1.451 +#elif defined(HAS_ARGBTOBAYERROW_NEON)
   1.452 +  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
   1.453 +    ARGBToBayerRow = ARGBToBayerRow_Any_NEON;
   1.454 +    if (IS_ALIGNED(width, 8)) {
   1.455 +      ARGBToBayerRow = ARGBToBayerRow_NEON;
   1.456 +    }
   1.457 +  }
   1.458 +#endif
   1.459 +
   1.460 +  const int blue_index = 0;  // Offsets for ARGB format
   1.461 +  const int green_index = 1;
   1.462 +  const int red_index = 2;
   1.463 +  uint32 index_map[2];
   1.464 +  if (MakeSelectors(blue_index, green_index, red_index,
   1.465 +                    dst_fourcc_bayer, index_map)) {
   1.466 +    return -1;  // Bad FourCC
   1.467 +  }
   1.468 +  // Allocate a row of ARGB.
   1.469 +  align_buffer_64(row, width * 4);
   1.470 +  for (int y = 0; y < height; ++y) {
   1.471 +    I422ToARGBRow(src_y, src_u, src_v, row, width);
   1.472 +    ARGBToBayerRow(row, dst_bayer, index_map[y & 1], width);
   1.473 +    dst_bayer += dst_stride_bayer;
   1.474 +    src_y += src_stride_y;
   1.475 +    if (y & 1) {
   1.476 +      src_u += src_stride_u;
   1.477 +      src_v += src_stride_v;
   1.478 +    }
   1.479 +  }
   1.480 +  free_aligned_buffer_64(row);
   1.481 +  return 0;
   1.482 +}
   1.483 +
   1.484 +#define MAKEBAYERFOURCC(BAYER)                                                 \
   1.485 +LIBYUV_API                                                                     \
   1.486 +int Bayer##BAYER##ToI420(const uint8* src_bayer, int src_stride_bayer,         \
   1.487 +                         uint8* dst_y, int dst_stride_y,                       \
   1.488 +                         uint8* dst_u, int dst_stride_u,                       \
   1.489 +                         uint8* dst_v, int dst_stride_v,                       \
   1.490 +                         int width, int height) {                              \
   1.491 +  return BayerToI420(src_bayer, src_stride_bayer,                              \
   1.492 +                     dst_y, dst_stride_y,                                      \
   1.493 +                     dst_u, dst_stride_u,                                      \
   1.494 +                     dst_v, dst_stride_v,                                      \
   1.495 +                     width, height,                                            \
   1.496 +                     FOURCC_##BAYER);                                          \
   1.497 +}                                                                              \
   1.498 +                                                                               \
   1.499 +LIBYUV_API                                                                     \
   1.500 +int I420ToBayer##BAYER(const uint8* src_y, int src_stride_y,                   \
   1.501 +                       const uint8* src_u, int src_stride_u,                   \
   1.502 +                       const uint8* src_v, int src_stride_v,                   \
   1.503 +                       uint8* dst_bayer, int dst_stride_bayer,                 \
   1.504 +                       int width, int height) {                                \
   1.505 +  return I420ToBayer(src_y, src_stride_y,                                      \
   1.506 +                     src_u, src_stride_u,                                      \
   1.507 +                     src_v, src_stride_v,                                      \
   1.508 +                     dst_bayer, dst_stride_bayer,                              \
   1.509 +                     width, height,                                            \
   1.510 +                     FOURCC_##BAYER);                                          \
   1.511 +}                                                                              \
   1.512 +                                                                               \
   1.513 +LIBYUV_API                                                                     \
   1.514 +int ARGBToBayer##BAYER(const uint8* src_argb, int src_stride_argb,             \
   1.515 +                       uint8* dst_bayer, int dst_stride_bayer,                 \
   1.516 +                       int width, int height) {                                \
   1.517 +  return ARGBToBayer(src_argb, src_stride_argb,                                \
   1.518 +                     dst_bayer, dst_stride_bayer,                              \
   1.519 +                     width, height,                                            \
   1.520 +                     FOURCC_##BAYER);                                          \
   1.521 +}                                                                              \
   1.522 +                                                                               \
   1.523 +LIBYUV_API                                                                     \
   1.524 +int Bayer##BAYER##ToARGB(const uint8* src_bayer, int src_stride_bayer,         \
   1.525 +                         uint8* dst_argb, int dst_stride_argb,                 \
   1.526 +                         int width, int height) {                              \
   1.527 +  return BayerToARGB(src_bayer, src_stride_bayer,                              \
   1.528 +                     dst_argb, dst_stride_argb,                                \
   1.529 +                     width, height,                                            \
   1.530 +                     FOURCC_##BAYER);                                          \
   1.531 +}
   1.532 +
   1.533 +MAKEBAYERFOURCC(BGGR)
   1.534 +MAKEBAYERFOURCC(GBRG)
   1.535 +MAKEBAYERFOURCC(GRBG)
   1.536 +MAKEBAYERFOURCC(RGGB)
   1.537 +
   1.538 +#ifdef __cplusplus
   1.539 +}  // extern "C"
   1.540 +}  // namespace libyuv
   1.541 +#endif

mercurial