convert_argb.cc 27.6 KB
Newer Older
fbarchard@google.com's avatar
fbarchard@google.com committed
1
/*
2
 *  Copyright 2011 The LibYuv Project Authors. All rights reserved.
fbarchard@google.com's avatar
fbarchard@google.com committed
3 4 5 6
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS. All contributing project authors may
fbarchard@google.com's avatar
fbarchard@google.com committed
8 9 10 11 12 13 14 15 16 17 18
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "libyuv/convert_argb.h"

#include "libyuv/cpu_id.h"
#include "libyuv/format_conversion.h"
#ifdef HAVE_JPEG
#include "libyuv/mjpeg_decoder.h"
#endif
#include "libyuv/rotate_argb.h"
19
#include "libyuv/row.h"
20
#include "libyuv/video_common.h"
fbarchard@google.com's avatar
fbarchard@google.com committed
21 22 23 24 25 26 27

#ifdef __cplusplus
namespace libyuv {
extern "C" {
#endif

// Copy ARGB with optional flipping
28
LIBYUV_API
fbarchard@google.com's avatar
fbarchard@google.com committed
29 30 31
int ARGBCopy(const uint8* src_argb, int src_stride_argb,
             uint8* dst_argb, int dst_stride_argb,
             int width, int height) {
32
  if (!src_argb || !dst_argb ||
fbarchard@google.com's avatar
fbarchard@google.com committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
      width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    src_argb = src_argb + (height - 1) * src_stride_argb;
    src_stride_argb = -src_stride_argb;
  }

  CopyPlane(src_argb, src_stride_argb, dst_argb, dst_stride_argb,
            width * 4, height);
  return 0;
}

// Convert I444 to ARGB.
49
LIBYUV_API
fbarchard@google.com's avatar
fbarchard@google.com committed
50 51 52 53 54
int I444ToARGB(const uint8* src_y, int src_stride_y,
               const uint8* src_u, int src_stride_u,
               const uint8* src_v, int src_stride_v,
               uint8* dst_argb, int dst_stride_argb,
               int width, int height) {
55 56 57 58 59 60
  int y;
  void (*I444ToARGBRow)(const uint8* y_buf,
                        const uint8* u_buf,
                        const uint8* v_buf,
                        uint8* rgb_buf,
                        int width) = I444ToARGBRow_C;
61 62 63
  if (!src_y || !src_u || !src_v ||
      !dst_argb ||
      width <= 0 || height == 0) {
64 65
    return -1;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
66 67 68 69 70 71
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_argb = dst_argb + (height - 1) * dst_stride_argb;
    dst_stride_argb = -dst_stride_argb;
  }
72
  // Coalesce rows.
73 74 75 76
  if (src_stride_y == width &&
      src_stride_u == width &&
      src_stride_v == width &&
      dst_stride_argb == width * 4) {
77 78 79
    width *= height;
    height = 1;
    src_stride_y = src_stride_u = src_stride_v = dst_stride_argb = 0;
80
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
81 82 83 84 85 86 87 88 89 90
#if defined(HAS_I444TOARGBROW_SSSE3)
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) {
    I444ToARGBRow = I444ToARGBRow_Any_SSSE3;
    if (IS_ALIGNED(width, 8)) {
      I444ToARGBRow = I444ToARGBRow_Unaligned_SSSE3;
      if (IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
        I444ToARGBRow = I444ToARGBRow_SSSE3;
      }
    }
  }
91 92 93 94 95 96 97
#elif defined(HAS_I444TOARGBROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    I444ToARGBRow = I444ToARGBRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I444ToARGBRow = I444ToARGBRow_NEON;
    }
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
98 99
#endif

100
  for (y = 0; y < height; ++y) {
fbarchard@google.com's avatar
fbarchard@google.com committed
101 102 103 104 105 106 107 108 109 110
    I444ToARGBRow(src_y, src_u, src_v, dst_argb, width);
    dst_argb += dst_stride_argb;
    src_y += src_stride_y;
    src_u += src_stride_u;
    src_v += src_stride_v;
  }
  return 0;
}

// Convert I422 to ARGB.
111
LIBYUV_API
fbarchard@google.com's avatar
fbarchard@google.com committed
112 113 114 115 116
int I422ToARGB(const uint8* src_y, int src_stride_y,
               const uint8* src_u, int src_stride_u,
               const uint8* src_v, int src_stride_v,
               uint8* dst_argb, int dst_stride_argb,
               int width, int height) {
117 118 119 120 121 122
  int y;
  void (*I422ToARGBRow)(const uint8* y_buf,
                        const uint8* u_buf,
                        const uint8* v_buf,
                        uint8* rgb_buf,
                        int width) = I422ToARGBRow_C;
123 124 125 126 127
  if (!src_y || !src_u || !src_v ||
      !dst_argb ||
      width <= 0 || height == 0) {
    return -1;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
128 129 130 131 132 133
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_argb = dst_argb + (height - 1) * dst_stride_argb;
    dst_stride_argb = -dst_stride_argb;
  }
134
  // Coalesce rows.
135 136 137 138
  if (src_stride_y == width &&
      src_stride_u * 2 == width &&
      src_stride_v * 2 == width &&
      dst_stride_argb == width * 4) {
139 140 141
    width *= height;
    height = 1;
    src_stride_y = src_stride_u = src_stride_v = dst_stride_argb = 0;
142
  }
143
#if defined(HAS_I422TOARGBROW_SSSE3)
fbarchard@google.com's avatar
fbarchard@google.com committed
144 145 146 147 148 149 150 151 152
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) {
    I422ToARGBRow = I422ToARGBRow_Any_SSSE3;
    if (IS_ALIGNED(width, 8)) {
      I422ToARGBRow = I422ToARGBRow_Unaligned_SSSE3;
      if (IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
        I422ToARGBRow = I422ToARGBRow_SSSE3;
      }
    }
  }
153 154 155 156 157 158 159 160 161 162
#endif
#if defined(HAS_I422TOARGBROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2) && width >= 16) {
    I422ToARGBRow = I422ToARGBRow_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      I422ToARGBRow = I422ToARGBRow_AVX2;
    }
  }
#endif
#if defined(HAS_I422TOARGBROW_NEON)
163 164 165 166 167 168
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    I422ToARGBRow = I422ToARGBRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToARGBRow = I422ToARGBRow_NEON;
    }
  }
169 170
#endif
#if defined(HAS_I422TOARGBROW_MIPS_DSPR2)
171 172 173 174 175 176 177
  if (TestCpuFlag(kCpuHasMIPS_DSPR2) && IS_ALIGNED(width, 4) &&
      IS_ALIGNED(src_y, 4) && IS_ALIGNED(src_stride_y, 4) &&
      IS_ALIGNED(src_u, 2) && IS_ALIGNED(src_stride_u, 2) &&
      IS_ALIGNED(src_v, 2) && IS_ALIGNED(src_stride_v, 2) &&
      IS_ALIGNED(dst_argb, 4) && IS_ALIGNED(dst_stride_argb, 4)) {
    I422ToARGBRow = I422ToARGBRow_MIPS_DSPR2;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
178 179
#endif

180
  for (y = 0; y < height; ++y) {
fbarchard@google.com's avatar
fbarchard@google.com committed
181 182 183 184 185 186 187 188 189 190
    I422ToARGBRow(src_y, src_u, src_v, dst_argb, width);
    dst_argb += dst_stride_argb;
    src_y += src_stride_y;
    src_u += src_stride_u;
    src_v += src_stride_v;
  }
  return 0;
}

// Convert I411 to ARGB.
191
LIBYUV_API
fbarchard@google.com's avatar
fbarchard@google.com committed
192 193 194 195 196
int I411ToARGB(const uint8* src_y, int src_stride_y,
               const uint8* src_u, int src_stride_u,
               const uint8* src_v, int src_stride_v,
               uint8* dst_argb, int dst_stride_argb,
               int width, int height) {
197 198 199 200 201 202
  int y;
  void (*I411ToARGBRow)(const uint8* y_buf,
                        const uint8* u_buf,
                        const uint8* v_buf,
                        uint8* rgb_buf,
                        int width) = I411ToARGBRow_C;
203 204 205 206 207
  if (!src_y || !src_u || !src_v ||
      !dst_argb ||
      width <= 0 || height == 0) {
    return -1;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
208 209 210 211 212 213
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_argb = dst_argb + (height - 1) * dst_stride_argb;
    dst_stride_argb = -dst_stride_argb;
  }
214
  // Coalesce rows.
215 216 217 218
  if (src_stride_y == width &&
      src_stride_u * 4 == width &&
      src_stride_v * 4 == width &&
      dst_stride_argb == width * 4) {
219 220 221
    width *= height;
    height = 1;
    src_stride_y = src_stride_u = src_stride_v = dst_stride_argb = 0;
222
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
223 224 225 226 227 228 229 230 231 232
#if defined(HAS_I411TOARGBROW_SSSE3)
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) {
    I411ToARGBRow = I411ToARGBRow_Any_SSSE3;
    if (IS_ALIGNED(width, 8)) {
      I411ToARGBRow = I411ToARGBRow_Unaligned_SSSE3;
      if (IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
        I411ToARGBRow = I411ToARGBRow_SSSE3;
      }
    }
  }
233 234 235 236 237 238 239
#elif defined(HAS_I411TOARGBROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    I411ToARGBRow = I411ToARGBRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I411ToARGBRow = I411ToARGBRow_NEON;
    }
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
240 241
#endif

242
  for (y = 0; y < height; ++y) {
fbarchard@google.com's avatar
fbarchard@google.com committed
243 244 245 246 247 248 249 250 251 252
    I411ToARGBRow(src_y, src_u, src_v, dst_argb, width);
    dst_argb += dst_stride_argb;
    src_y += src_stride_y;
    src_u += src_stride_u;
    src_v += src_stride_v;
  }
  return 0;
}

// Convert I400 to ARGB.
253
LIBYUV_API
fbarchard@google.com's avatar
fbarchard@google.com committed
254 255 256
int I400ToARGB_Reference(const uint8* src_y, int src_stride_y,
                         uint8* dst_argb, int dst_stride_argb,
                         int width, int height) {
257 258 259 260
  int y;
  void (*YToARGBRow)(const uint8* y_buf,
                     uint8* rgb_buf,
                     int width) = YToARGBRow_C;
261 262 263 264
  if (!src_y || !dst_argb ||
      width <= 0 || height == 0) {
    return -1;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
265 266 267 268 269 270
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_argb = dst_argb + (height - 1) * dst_stride_argb;
    dst_stride_argb = -dst_stride_argb;
  }
271
  // Coalesce rows.
272 273
  if (src_stride_y == width &&
      dst_stride_argb == width * 4) {
274 275 276
    width *= height;
    height = 1;
    src_stride_y = dst_stride_argb = 0;
277
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
278
#if defined(HAS_YTOARGBROW_SSE2)
279
  if (TestCpuFlag(kCpuHasSSE2) && width >= 8 &&
fbarchard@google.com's avatar
fbarchard@google.com committed
280
      IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
281 282 283 284
    YToARGBRow = YToARGBRow_Any_SSE2;
    if (IS_ALIGNED(width, 8)) {
      YToARGBRow = YToARGBRow_SSE2;
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
285
  }
286
#elif defined(HAS_YTOARGBROW_NEON)
287 288 289 290 291
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    YToARGBRow = YToARGBRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      YToARGBRow = YToARGBRow_NEON;
    }
292
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
293 294
#endif

295
  for (y = 0; y < height; ++y) {
fbarchard@google.com's avatar
fbarchard@google.com committed
296 297 298 299 300 301 302 303
    YToARGBRow(src_y, dst_argb, width);
    dst_argb += dst_stride_argb;
    src_y += src_stride_y;
  }
  return 0;
}

// Convert I400 to ARGB.
304
LIBYUV_API
fbarchard@google.com's avatar
fbarchard@google.com committed
305 306 307
int I400ToARGB(const uint8* src_y, int src_stride_y,
               uint8* dst_argb, int dst_stride_argb,
               int width, int height) {
308 309 310
  int y;
  void (*I400ToARGBRow)(const uint8* src_y, uint8* dst_argb, int pix) =
      I400ToARGBRow_C;
311 312 313 314 315
  if (!src_y || !dst_argb ||
      width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
fbarchard@google.com's avatar
fbarchard@google.com committed
316 317 318 319 320
  if (height < 0) {
    height = -height;
    src_y = src_y + (height - 1) * src_stride_y;
    src_stride_y = -src_stride_y;
  }
321
  // Coalesce rows.
322 323
  if (src_stride_y == width &&
      dst_stride_argb == width * 4) {
324 325 326
    width *= height;
    height = 1;
    src_stride_y = dst_stride_argb = 0;
327
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
328
#if defined(HAS_I400TOARGBROW_SSE2)
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
  if (TestCpuFlag(kCpuHasSSE2) && width >= 8) {
    I400ToARGBRow = I400ToARGBRow_Any_SSE2;
    if (IS_ALIGNED(width, 8)) {
      I400ToARGBRow = I400ToARGBRow_Unaligned_SSE2;
      if (IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
        I400ToARGBRow = I400ToARGBRow_SSE2;
      }
    }
  }
#elif defined(HAS_I400TOARGBROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    I400ToARGBRow = I400ToARGBRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I400ToARGBRow = I400ToARGBRow_NEON;
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
344 345
  }
#endif
346
  for (y = 0; y < height; ++y) {
fbarchard@google.com's avatar
fbarchard@google.com committed
347 348 349 350 351 352 353
    I400ToARGBRow(src_y, dst_argb, width);
    src_y += src_stride_y;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

fbarchard@google.com's avatar
fbarchard@google.com committed
354
// Shuffle table for converting BGRA to ARGB.
355
static uvec8 kShuffleMaskBGRAToARGB = {
fbarchard@google.com's avatar
fbarchard@google.com committed
356 357 358 359
  3u, 2u, 1u, 0u, 7u, 6u, 5u, 4u, 11u, 10u, 9u, 8u, 15u, 14u, 13u, 12u
};

// Shuffle table for converting ABGR to ARGB.
360
static uvec8 kShuffleMaskABGRToARGB = {
fbarchard@google.com's avatar
fbarchard@google.com committed
361 362 363 364
  2u, 1u, 0u, 3u, 6u, 5u, 4u, 7u, 10u, 9u, 8u, 11u, 14u, 13u, 12u, 15u
};

// Shuffle table for converting RGBA to ARGB.
365
static uvec8 kShuffleMaskRGBAToARGB = {
fbarchard@google.com's avatar
fbarchard@google.com committed
366 367 368
  1u, 2u, 3u, 0u, 5u, 6u, 7u, 4u, 9u, 10u, 11u, 8u, 13u, 14u, 15u, 12u
};

369
// Convert BGRA to ARGB.
370
LIBYUV_API
371
int BGRAToARGB(const uint8* src_bgra, int src_stride_bgra,
fbarchard@google.com's avatar
fbarchard@google.com committed
372 373
               uint8* dst_argb, int dst_stride_argb,
               int width, int height) {
fbarchard@google.com's avatar
fbarchard@google.com committed
374 375
  return ARGBShuffle(src_bgra, src_stride_bgra,
                     dst_argb, dst_stride_argb,
376
                     (const uint8*)(&kShuffleMaskBGRAToARGB),
fbarchard@google.com's avatar
fbarchard@google.com committed
377
                     width, height);
fbarchard@google.com's avatar
fbarchard@google.com committed
378 379
}

380 381 382 383 384 385 386 387 388 389 390
// Convert ARGB to BGRA (same as BGRAToARGB).
LIBYUV_API
int ARGBToBGRA(const uint8* src_bgra, int src_stride_bgra,
               uint8* dst_argb, int dst_stride_argb,
               int width, int height) {
  return ARGBShuffle(src_bgra, src_stride_bgra,
                     dst_argb, dst_stride_argb,
                     (const uint8*)(&kShuffleMaskBGRAToARGB),
                     width, height);
}

391
// Convert ABGR to ARGB.
392
LIBYUV_API
393
int ABGRToARGB(const uint8* src_abgr, int src_stride_abgr,
fbarchard@google.com's avatar
fbarchard@google.com committed
394 395
               uint8* dst_argb, int dst_stride_argb,
               int width, int height) {
fbarchard@google.com's avatar
fbarchard@google.com committed
396 397
  return ARGBShuffle(src_abgr, src_stride_abgr,
                     dst_argb, dst_stride_argb,
398
                     (const uint8*)(&kShuffleMaskABGRToARGB),
fbarchard@google.com's avatar
fbarchard@google.com committed
399
                     width, height);
fbarchard@google.com's avatar
fbarchard@google.com committed
400 401
}

402 403 404 405 406 407 408 409 410 411 412
// Convert ARGB to ABGR to (same as ABGRToARGB).
LIBYUV_API
int ARGBToABGR(const uint8* src_abgr, int src_stride_abgr,
               uint8* dst_argb, int dst_stride_argb,
               int width, int height) {
  return ARGBShuffle(src_abgr, src_stride_abgr,
                     dst_argb, dst_stride_argb,
                     (const uint8*)(&kShuffleMaskABGRToARGB),
                     width, height);
}

413
// Convert RGBA to ARGB.
414
LIBYUV_API
415 416 417
int RGBAToARGB(const uint8* src_rgba, int src_stride_rgba,
               uint8* dst_argb, int dst_stride_argb,
               int width, int height) {
fbarchard@google.com's avatar
fbarchard@google.com committed
418 419
  return ARGBShuffle(src_rgba, src_stride_rgba,
                     dst_argb, dst_stride_argb,
420
                     (const uint8*)(&kShuffleMaskRGBAToARGB),
fbarchard@google.com's avatar
fbarchard@google.com committed
421
                     width, height);
422 423
}

424
// Convert RGB24 to ARGB.
425
LIBYUV_API
426
int RGB24ToARGB(const uint8* src_rgb24, int src_stride_rgb24,
427 428
                uint8* dst_argb, int dst_stride_argb,
                int width, int height) {
429 430 431
  int y;
  void (*RGB24ToARGBRow)(const uint8* src_rgb, uint8* dst_argb, int pix) =
      RGB24ToARGBRow_C;
432
  if (!src_rgb24 || !dst_argb ||
433 434 435 436
      width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
fbarchard@google.com's avatar
fbarchard@google.com committed
437 438
  if (height < 0) {
    height = -height;
439 440
    src_rgb24 = src_rgb24 + (height - 1) * src_stride_rgb24;
    src_stride_rgb24 = -src_stride_rgb24;
fbarchard@google.com's avatar
fbarchard@google.com committed
441
  }
442
  // Coalesce rows.
443 444
  if (src_stride_rgb24 == width * 3 &&
      dst_stride_argb == width * 4) {
445 446 447
    width *= height;
    height = 1;
    src_stride_rgb24 = dst_stride_argb = 0;
448
  }
449
#if defined(HAS_RGB24TOARGBROW_SSSE3)
450
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 16 &&
fbarchard@google.com's avatar
fbarchard@google.com committed
451
      IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
452 453 454 455
    RGB24ToARGBRow = RGB24ToARGBRow_Any_SSSE3;
    if (IS_ALIGNED(width, 16)) {
      RGB24ToARGBRow = RGB24ToARGBRow_SSSE3;
    }
456 457
  }
#elif defined(HAS_RGB24TOARGBROW_NEON)
458 459 460 461 462
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    RGB24ToARGBRow = RGB24ToARGBRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      RGB24ToARGBRow = RGB24ToARGBRow_NEON;
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
463 464 465
  }
#endif

466
  for (y = 0; y < height; ++y) {
467 468
    RGB24ToARGBRow(src_rgb24, dst_argb, width);
    src_rgb24 += src_stride_rgb24;
fbarchard@google.com's avatar
fbarchard@google.com committed
469 470 471 472 473
    dst_argb += dst_stride_argb;
  }
  return 0;
}

474
// Convert RAW to ARGB.
475
LIBYUV_API
476
int RAWToARGB(const uint8* src_raw, int src_stride_raw,
477 478
              uint8* dst_argb, int dst_stride_argb,
              int width, int height) {
479 480 481
  int y;
  void (*RAWToARGBRow)(const uint8* src_rgb, uint8* dst_argb, int pix) =
      RAWToARGBRow_C;
482
  if (!src_raw || !dst_argb ||
483 484 485 486
      width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
fbarchard@google.com's avatar
fbarchard@google.com committed
487 488
  if (height < 0) {
    height = -height;
489 490
    src_raw = src_raw + (height - 1) * src_stride_raw;
    src_stride_raw = -src_stride_raw;
fbarchard@google.com's avatar
fbarchard@google.com committed
491
  }
492
  // Coalesce rows.
493 494
  if (src_stride_raw == width * 3 &&
      dst_stride_argb == width * 4) {
495 496 497
    width *= height;
    height = 1;
    src_stride_raw = dst_stride_argb = 0;
498
  }
499
#if defined(HAS_RAWTOARGBROW_SSSE3)
500
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 16 &&
fbarchard@google.com's avatar
fbarchard@google.com committed
501
      IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
502 503 504 505
    RAWToARGBRow = RAWToARGBRow_Any_SSSE3;
    if (IS_ALIGNED(width, 16)) {
      RAWToARGBRow = RAWToARGBRow_SSSE3;
    }
506 507
  }
#elif defined(HAS_RAWTOARGBROW_NEON)
508 509 510 511 512
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    RAWToARGBRow = RAWToARGBRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      RAWToARGBRow = RAWToARGBRow_NEON;
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
513 514 515
  }
#endif

516
  for (y = 0; y < height; ++y) {
517 518
    RAWToARGBRow(src_raw, dst_argb, width);
    src_raw += src_stride_raw;
fbarchard@google.com's avatar
fbarchard@google.com committed
519 520 521 522 523 524
    dst_argb += dst_stride_argb;
  }
  return 0;
}

// Convert RGB565 to ARGB.
525
LIBYUV_API
fbarchard@google.com's avatar
fbarchard@google.com committed
526 527 528
int RGB565ToARGB(const uint8* src_rgb565, int src_stride_rgb565,
                 uint8* dst_argb, int dst_stride_argb,
                 int width, int height) {
529 530 531
  int y;
  void (*RGB565ToARGBRow)(const uint8* src_rgb565, uint8* dst_argb, int pix) =
      RGB565ToARGBRow_C;
532 533 534 535 536
  if (!src_rgb565 || !dst_argb ||
      width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
fbarchard@google.com's avatar
fbarchard@google.com committed
537 538 539 540 541
  if (height < 0) {
    height = -height;
    src_rgb565 = src_rgb565 + (height - 1) * src_stride_rgb565;
    src_stride_rgb565 = -src_stride_rgb565;
  }
542
  // Coalesce rows.
543 544
  if (src_stride_rgb565 == width * 2 &&
      dst_stride_argb == width * 4) {
545 546 547
    width *= height;
    height = 1;
    src_stride_rgb565 = dst_stride_argb = 0;
548
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
549
#if defined(HAS_RGB565TOARGBROW_SSE2)
550
  if (TestCpuFlag(kCpuHasSSE2) && width >= 8 &&
fbarchard@google.com's avatar
fbarchard@google.com committed
551
      IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
552 553 554 555 556 557 558 559 560 561 562
    RGB565ToARGBRow = RGB565ToARGBRow_Any_SSE2;
    if (IS_ALIGNED(width, 8)) {
      RGB565ToARGBRow = RGB565ToARGBRow_SSE2;
    }
  }
#elif defined(HAS_RGB565TOARGBROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    RGB565ToARGBRow = RGB565ToARGBRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      RGB565ToARGBRow = RGB565ToARGBRow_NEON;
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
563 564 565
  }
#endif

566
  for (y = 0; y < height; ++y) {
fbarchard@google.com's avatar
fbarchard@google.com committed
567 568 569 570 571 572 573 574
    RGB565ToARGBRow(src_rgb565, dst_argb, width);
    src_rgb565 += src_stride_rgb565;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

// Convert ARGB1555 to ARGB.
575
LIBYUV_API
fbarchard@google.com's avatar
fbarchard@google.com committed
576 577 578
int ARGB1555ToARGB(const uint8* src_argb1555, int src_stride_argb1555,
                   uint8* dst_argb, int dst_stride_argb,
                   int width, int height) {
579 580 581
  int y;
  void (*ARGB1555ToARGBRow)(const uint8* src_argb1555, uint8* dst_argb,
      int pix) = ARGB1555ToARGBRow_C;
582
  if (!src_argb1555 || !dst_argb ||
583
      width <= 0 || height == 0) {
584 585 586
    return -1;
  }
  // Negative height means invert the image.
fbarchard@google.com's avatar
fbarchard@google.com committed
587 588 589 590 591
  if (height < 0) {
    height = -height;
    src_argb1555 = src_argb1555 + (height - 1) * src_stride_argb1555;
    src_stride_argb1555 = -src_stride_argb1555;
  }
592
  // Coalesce rows.
593 594
  if (src_stride_argb1555 == width * 2 &&
      dst_stride_argb == width * 4) {
595 596 597
    width *= height;
    height = 1;
    src_stride_argb1555 = dst_stride_argb = 0;
598
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
599
#if defined(HAS_ARGB1555TOARGBROW_SSE2)
600
  if (TestCpuFlag(kCpuHasSSE2) && width >= 8 &&
fbarchard@google.com's avatar
fbarchard@google.com committed
601
      IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
602 603 604 605 606 607 608 609 610 611 612
    ARGB1555ToARGBRow = ARGB1555ToARGBRow_Any_SSE2;
    if (IS_ALIGNED(width, 8)) {
      ARGB1555ToARGBRow = ARGB1555ToARGBRow_SSE2;
    }
  }
#elif defined(HAS_ARGB1555TOARGBROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    ARGB1555ToARGBRow = ARGB1555ToARGBRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      ARGB1555ToARGBRow = ARGB1555ToARGBRow_NEON;
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
613 614 615
  }
#endif

616
  for (y = 0; y < height; ++y) {
fbarchard@google.com's avatar
fbarchard@google.com committed
617 618 619 620 621 622 623 624
    ARGB1555ToARGBRow(src_argb1555, dst_argb, width);
    src_argb1555 += src_stride_argb1555;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

// Convert ARGB4444 to ARGB.
625
LIBYUV_API
fbarchard@google.com's avatar
fbarchard@google.com committed
626 627 628
int ARGB4444ToARGB(const uint8* src_argb4444, int src_stride_argb4444,
                   uint8* dst_argb, int dst_stride_argb,
                   int width, int height) {
629 630 631
  int y;
  void (*ARGB4444ToARGBRow)(const uint8* src_argb4444, uint8* dst_argb,
      int pix) = ARGB4444ToARGBRow_C;
632 633 634 635 636
  if (!src_argb4444 || !dst_argb ||
      width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
fbarchard@google.com's avatar
fbarchard@google.com committed
637 638 639 640 641
  if (height < 0) {
    height = -height;
    src_argb4444 = src_argb4444 + (height - 1) * src_stride_argb4444;
    src_stride_argb4444 = -src_stride_argb4444;
  }
642
  // Coalesce rows.
643 644
  if (src_stride_argb4444 == width * 2 &&
      dst_stride_argb == width * 4) {
645 646 647
    width *= height;
    height = 1;
    src_stride_argb4444 = dst_stride_argb = 0;
648
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
649
#if defined(HAS_ARGB4444TOARGBROW_SSE2)
650
  if (TestCpuFlag(kCpuHasSSE2) && width >= 8 &&
fbarchard@google.com's avatar
fbarchard@google.com committed
651
      IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
652 653 654 655 656 657 658 659 660 661 662
    ARGB4444ToARGBRow = ARGB4444ToARGBRow_Any_SSE2;
    if (IS_ALIGNED(width, 8)) {
      ARGB4444ToARGBRow = ARGB4444ToARGBRow_SSE2;
    }
  }
#elif defined(HAS_ARGB4444TOARGBROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    ARGB4444ToARGBRow = ARGB4444ToARGBRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      ARGB4444ToARGBRow = ARGB4444ToARGBRow_NEON;
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
663 664 665
  }
#endif

666
  for (y = 0; y < height; ++y) {
fbarchard@google.com's avatar
fbarchard@google.com committed
667 668 669 670 671 672 673 674
    ARGB4444ToARGBRow(src_argb4444, dst_argb, width);
    src_argb4444 += src_stride_argb4444;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

// Convert NV12 to ARGB.
675
LIBYUV_API
fbarchard@google.com's avatar
fbarchard@google.com committed
676 677 678 679
int NV12ToARGB(const uint8* src_y, int src_stride_y,
               const uint8* src_uv, int src_stride_uv,
               uint8* dst_argb, int dst_stride_argb,
               int width, int height) {
680 681 682 683 684
  int y;
  void (*NV12ToARGBRow)(const uint8* y_buf,
                        const uint8* uv_buf,
                        uint8* rgb_buf,
                        int width) = NV12ToARGBRow_C;
685 686 687 688
  if (!src_y || !src_uv || !dst_argb ||
      width <= 0 || height == 0) {
    return -1;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_argb = dst_argb + (height - 1) * dst_stride_argb;
    dst_stride_argb = -dst_stride_argb;
  }
#if defined(HAS_NV12TOARGBROW_SSSE3)
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) {
    NV12ToARGBRow = NV12ToARGBRow_Any_SSSE3;
    if (IS_ALIGNED(width, 8)) {
      NV12ToARGBRow = NV12ToARGBRow_Unaligned_SSSE3;
      if (IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
        NV12ToARGBRow = NV12ToARGBRow_SSSE3;
      }
    }
  }
705
#elif defined(HAS_NV12TOARGBROW_NEON)
706 707 708 709 710 711 712
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    NV12ToARGBRow = NV12ToARGBRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      NV12ToARGBRow = NV12ToARGBRow_NEON;
    }
  }
#endif
fbarchard@google.com's avatar
fbarchard@google.com committed
713

714
  for (y = 0; y < height; ++y) {
fbarchard@google.com's avatar
fbarchard@google.com committed
715 716 717 718 719 720 721 722 723 724 725
    NV12ToARGBRow(src_y, src_uv, dst_argb, width);
    dst_argb += dst_stride_argb;
    src_y += src_stride_y;
    if (y & 1) {
      src_uv += src_stride_uv;
    }
  }
  return 0;
}

// Convert NV21 to ARGB.
726
LIBYUV_API
fbarchard@google.com's avatar
fbarchard@google.com committed
727
int NV21ToARGB(const uint8* src_y, int src_stride_y,
728
               const uint8* src_uv, int src_stride_uv,
fbarchard@google.com's avatar
fbarchard@google.com committed
729 730
               uint8* dst_argb, int dst_stride_argb,
               int width, int height) {
731 732 733 734 735
  int y;
  void (*NV21ToARGBRow)(const uint8* y_buf,
                        const uint8* uv_buf,
                        uint8* rgb_buf,
                        int width) = NV21ToARGBRow_C;
736
  if (!src_y || !src_uv || !dst_argb ||
737 738 739
      width <= 0 || height == 0) {
    return -1;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_argb = dst_argb + (height - 1) * dst_stride_argb;
    dst_stride_argb = -dst_stride_argb;
  }
#if defined(HAS_NV21TOARGBROW_SSSE3)
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) {
    NV21ToARGBRow = NV21ToARGBRow_Any_SSSE3;
    if (IS_ALIGNED(width, 8)) {
      NV21ToARGBRow = NV21ToARGBRow_Unaligned_SSSE3;
      if (IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
        NV21ToARGBRow = NV21ToARGBRow_SSSE3;
      }
    }
  }
#endif
757 758 759 760 761 762 763 764
#if defined(HAS_NV21TOARGBROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    NV21ToARGBRow = NV21ToARGBRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      NV21ToARGBRow = NV21ToARGBRow_NEON;
    }
  }
#endif
fbarchard@google.com's avatar
fbarchard@google.com committed
765

766
  for (y = 0; y < height; ++y) {
767
    NV21ToARGBRow(src_y, src_uv, dst_argb, width);
fbarchard@google.com's avatar
fbarchard@google.com committed
768 769 770
    dst_argb += dst_stride_argb;
    src_y += src_stride_y;
    if (y & 1) {
771
      src_uv += src_stride_uv;
fbarchard@google.com's avatar
fbarchard@google.com committed
772 773 774 775 776 777
    }
  }
  return 0;
}

// Convert M420 to ARGB.
778
LIBYUV_API
fbarchard@google.com's avatar
fbarchard@google.com committed
779 780 781
int M420ToARGB(const uint8* src_m420, int src_stride_m420,
               uint8* dst_argb, int dst_stride_argb,
               int width, int height) {
782 783 784 785 786
  int y;
  void (*NV12ToARGBRow)(const uint8* y_buf,
                        const uint8* uv_buf,
                        uint8* rgb_buf,
                        int width) = NV12ToARGBRow_C;
787 788 789 790
  if (!src_m420 || !dst_argb ||
      width <= 0 || height == 0) {
    return -1;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_argb = dst_argb + (height - 1) * dst_stride_argb;
    dst_stride_argb = -dst_stride_argb;
  }
#if defined(HAS_NV12TOARGBROW_SSSE3)
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) {
    NV12ToARGBRow = NV12ToARGBRow_Any_SSSE3;
    if (IS_ALIGNED(width, 8)) {
      NV12ToARGBRow = NV12ToARGBRow_Unaligned_SSSE3;
      if (IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
        NV12ToARGBRow = NV12ToARGBRow_SSSE3;
      }
    }
  }
807 808 809 810 811 812 813
#elif defined(HAS_NV12TOARGBROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    NV12ToARGBRow = NV12ToARGBRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      NV12ToARGBRow = NV12ToARGBRow_NEON;
    }
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
814 815
#endif

816
  for (y = 0; y < height - 1; y += 2) {
fbarchard@google.com's avatar
fbarchard@google.com committed
817 818 819 820 821 822 823 824 825 826 827 828 829
    NV12ToARGBRow(src_m420, src_m420 + src_stride_m420 * 2, dst_argb, width);
    NV12ToARGBRow(src_m420 + src_stride_m420, src_m420 + src_stride_m420 * 2,
                  dst_argb + dst_stride_argb, width);
    dst_argb += dst_stride_argb * 2;
    src_m420 += src_stride_m420 * 3;
  }
  if (height & 1) {
    NV12ToARGBRow(src_m420, src_m420 + src_stride_m420 * 2, dst_argb, width);
  }
  return 0;
}

// Convert YUY2 to ARGB.
830
LIBYUV_API
fbarchard@google.com's avatar
fbarchard@google.com committed
831 832 833
int YUY2ToARGB(const uint8* src_yuy2, int src_stride_yuy2,
               uint8* dst_argb, int dst_stride_argb,
               int width, int height) {
834 835 836
  int y;
  void (*YUY2ToARGBRow)(const uint8* src_yuy2, uint8* dst_argb, int pix) =
      YUY2ToARGBRow_C;
837 838 839 840
  if (!src_yuy2 || !dst_argb ||
      width <= 0 || height == 0) {
    return -1;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
841 842 843 844 845 846
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    src_yuy2 = src_yuy2 + (height - 1) * src_stride_yuy2;
    src_stride_yuy2 = -src_stride_yuy2;
  }
847
  // Coalesce rows.
848
  if (src_stride_yuy2 == width * 2 &&
849
      dst_stride_argb == width * 4) {
850 851 852
    width *= height;
    height = 1;
    src_stride_yuy2 = dst_stride_argb = 0;
853
  }
854
#if defined(HAS_YUY2TOARGBROW_SSSE3)
fbarchard@google.com's avatar
fbarchard@google.com committed
855
  // Posix is 16, Windows is 8.
856
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 16) {
857
    YUY2ToARGBRow = YUY2ToARGBRow_Any_SSSE3;
fbarchard@google.com's avatar
fbarchard@google.com committed
858
    if (IS_ALIGNED(width, 16)) {
859
      YUY2ToARGBRow = YUY2ToARGBRow_Unaligned_SSSE3;
860
      if (IS_ALIGNED(src_yuy2, 16) && IS_ALIGNED(src_stride_yuy2, 16) &&
861
          IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
862
        YUY2ToARGBRow = YUY2ToARGBRow_SSSE3;
863 864 865
      }
    }
  }
866
#elif defined(HAS_YUY2TOARGBROW_NEON)
867
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
868
    YUY2ToARGBRow = YUY2ToARGBRow_Any_NEON;
869
    if (IS_ALIGNED(width, 8)) {
870
      YUY2ToARGBRow = YUY2ToARGBRow_NEON;
fbarchard@google.com's avatar
fbarchard@google.com committed
871 872 873
    }
  }
#endif
874
  for (y = 0; y < height; ++y) {
875
    YUY2ToARGBRow(src_yuy2, dst_argb, width);
fbarchard@google.com's avatar
fbarchard@google.com committed
876 877 878 879 880 881 882
    src_yuy2 += src_stride_yuy2;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

// Convert UYVY to ARGB.
883
LIBYUV_API
fbarchard@google.com's avatar
fbarchard@google.com committed
884 885 886
int UYVYToARGB(const uint8* src_uyvy, int src_stride_uyvy,
               uint8* dst_argb, int dst_stride_argb,
               int width, int height) {
887 888 889
  int y;
  void (*UYVYToARGBRow)(const uint8* src_uyvy, uint8* dst_argb, int pix) =
      UYVYToARGBRow_C;
890 891 892 893
  if (!src_uyvy || !dst_argb ||
      width <= 0 || height == 0) {
    return -1;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
894 895 896 897 898 899
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    src_uyvy = src_uyvy + (height - 1) * src_stride_uyvy;
    src_stride_uyvy = -src_stride_uyvy;
  }
900
  // Coalesce rows.
901
  if (src_stride_uyvy == width * 2 &&
902
      dst_stride_argb == width * 4) {
903 904 905
    width *= height;
    height = 1;
    src_stride_uyvy = dst_stride_argb = 0;
906
  }
907
#if defined(HAS_UYVYTOARGBROW_SSSE3)
fbarchard@google.com's avatar
fbarchard@google.com committed
908
  // Posix is 16, Windows is 8.
909
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 16) {
910
    UYVYToARGBRow = UYVYToARGBRow_Any_SSSE3;
fbarchard@google.com's avatar
fbarchard@google.com committed
911
    if (IS_ALIGNED(width, 16)) {
912
      UYVYToARGBRow = UYVYToARGBRow_Unaligned_SSSE3;
913
      if (IS_ALIGNED(src_uyvy, 16) && IS_ALIGNED(src_stride_uyvy, 16) &&
914
          IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
915
        UYVYToARGBRow = UYVYToARGBRow_SSSE3;
916 917 918
      }
    }
  }
919
#elif defined(HAS_UYVYTOARGBROW_NEON)
920
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
921
    UYVYToARGBRow = UYVYToARGBRow_Any_NEON;
922
    if (IS_ALIGNED(width, 8)) {
923
      UYVYToARGBRow = UYVYToARGBRow_NEON;
fbarchard@google.com's avatar
fbarchard@google.com committed
924 925 926
    }
  }
#endif
927
  for (y = 0; y < height; ++y) {
928
    UYVYToARGBRow(src_uyvy, dst_argb, width);
fbarchard@google.com's avatar
fbarchard@google.com committed
929 930 931 932 933 934 935 936 937 938
    src_uyvy += src_stride_uyvy;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

#ifdef __cplusplus
}  // extern "C"
}  // namespace libyuv
#endif