convert_from.cc 41.5 KB
Newer Older
1
/*
2
 *  Copyright 2012 The LibYuv Project Authors. All rights reserved.
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
8 9 10 11 12 13 14 15 16 17
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "libyuv/convert_from.h"

#include "libyuv/basic_types.h"
#include "libyuv/convert.h"  // For I420Copy
#include "libyuv/cpu_id.h"
#include "libyuv/planar_functions.h"
#include "libyuv/rotate.h"
18
#include "libyuv/scale.h"  // For ScalePlane()
19
#include "libyuv/video_common.h"
20
#include "libyuv/row.h"
21 22 23 24 25 26

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

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#define SUBSAMPLE(v, a, s) (v < 0) ? (-((-v + a) >> s)) : ((v + a) >> s)
static __inline int Abs(int v) {
  return v >= 0 ? v : -v;
}

// I420 To any I4xx YUV format with mirroring.
static int I420ToI4xx(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_y, int dst_stride_y,
                      uint8* dst_u, int dst_stride_u,
                      uint8* dst_v, int dst_stride_v,
                      int src_y_width, int src_y_height,
                      int dst_uv_width, int dst_uv_height) {
  const int dst_y_width = Abs(src_y_width);
  const int dst_y_height = Abs(src_y_height);
  const int src_uv_width = SUBSAMPLE(src_y_width, 1, 1);
  const int src_uv_height = SUBSAMPLE(src_y_height, 1, 1);
45 46 47 48
  if (src_y_width == 0 || src_y_height == 0 ||
      dst_uv_width <= 0 || dst_uv_height <= 0) {
    return -1;
  }
49 50 51 52 53 54 55 56 57 58 59 60
  ScalePlane(src_y, src_stride_y, src_y_width, src_y_height,
             dst_y, dst_stride_y, dst_y_width, dst_y_height,
             kFilterBilinear);
  ScalePlane(src_u, src_stride_u, src_uv_width, src_uv_height,
             dst_u, dst_stride_u, dst_uv_width, dst_uv_height,
             kFilterBilinear);
  ScalePlane(src_v, src_stride_v, src_uv_width, src_uv_height,
             dst_v, dst_stride_v, dst_uv_width, dst_uv_height,
             kFilterBilinear);
  return 0;
}

61 62
// 420 chroma is 1/2 width, 1/2 height
// 422 chroma is 1/2 width, 1x height
63
LIBYUV_API
64 65 66 67 68 69 70
int I420ToI422(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_y, int dst_stride_y,
               uint8* dst_u, int dst_stride_u,
               uint8* dst_v, int dst_stride_v,
               int width, int height) {
71 72 73 74 75 76 77 78 79 80
  const int dst_uv_width = (Abs(width) + 1) >> 1;
  const int dst_uv_height = Abs(height);
  return I420ToI4xx(src_y, src_stride_y,
                    src_u, src_stride_u,
                    src_v, src_stride_v,
                    dst_y, dst_stride_y,
                    dst_u, dst_stride_u,
                    dst_v, dst_stride_v,
                    width, height,
                    dst_uv_width, dst_uv_height);
81 82
}

83 84
// 420 chroma is 1/2 width, 1/2 height
// 444 chroma is 1x width, 1x height
85
LIBYUV_API
86 87 88 89 90 91 92
int I420ToI444(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_y, int dst_stride_y,
               uint8* dst_u, int dst_stride_u,
               uint8* dst_v, int dst_stride_v,
               int width, int height) {
93 94 95 96 97 98 99 100 101 102
  const int dst_uv_width = Abs(width);
  const int dst_uv_height = Abs(height);
  return I420ToI4xx(src_y, src_stride_y,
                    src_u, src_stride_u,
                    src_v, src_stride_v,
                    dst_y, dst_stride_y,
                    dst_u, dst_stride_u,
                    dst_v, dst_stride_v,
                    width, height,
                    dst_uv_width, dst_uv_height);
103 104
}

105 106
// 420 chroma is 1/2 width, 1/2 height
// 411 chroma is 1/4 width, 1x height
107
LIBYUV_API
108 109 110 111 112 113 114
int I420ToI411(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_y, int dst_stride_y,
               uint8* dst_u, int dst_stride_u,
               uint8* dst_v, int dst_stride_v,
               int width, int height) {
115 116 117 118 119 120 121 122 123 124
  const int dst_uv_width = (Abs(width) + 3) >> 2;
  const int dst_uv_height = Abs(height);
  return I420ToI4xx(src_y, src_stride_y,
                    src_u, src_stride_u,
                    src_v, src_stride_v,
                    dst_y, dst_stride_y,
                    dst_u, dst_stride_u,
                    dst_v, dst_stride_v,
                    width, height,
                    dst_uv_width, dst_uv_height);
125 126
}

127
// Copy to I400. Source can be I420,422,444,400,NV12,NV21
128
LIBYUV_API
129 130 131
int I400Copy(const uint8* src_y, int src_stride_y,
             uint8* dst_y, int dst_stride_y,
             int width, int height) {
132 133 134 135
  if (!src_y || !dst_y ||
      width <= 0 || height == 0) {
    return -1;
  }
136 137 138 139 140 141 142 143 144 145
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    src_y = src_y + (height - 1) * src_stride_y;
    src_stride_y = -src_stride_y;
  }
  CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  return 0;
}

146
LIBYUV_API
147 148 149
int I422ToYUY2(const uint8* src_y, int src_stride_y,
               const uint8* src_u, int src_stride_u,
               const uint8* src_v, int src_stride_v,
150
               uint8* dst_yuy2, int dst_stride_yuy2,
151
               int width, int height) {
152 153 154 155
  int y;
  void (*I422ToYUY2Row)(const uint8* src_y, const uint8* src_u,
                        const uint8* src_v, uint8* dst_yuy2, int width) =
      I422ToYUY2Row_C;
156
  if (!src_y || !src_u || !src_v || !dst_yuy2 ||
157
      width <= 0 || height == 0) {
158 159 160 161 162
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
163 164
    dst_yuy2 = dst_yuy2 + (height - 1) * dst_stride_yuy2;
    dst_stride_yuy2 = -dst_stride_yuy2;
165
  }
166
  // Coalesce rows.
167 168 169 170
  if (src_stride_y == width &&
      src_stride_u * 2 == width &&
      src_stride_v * 2 == width &&
      dst_stride_yuy2 == width * 2) {
171 172 173
    width *= height;
    height = 1;
    src_stride_y = src_stride_u = src_stride_v = dst_stride_yuy2 = 0;
174
  }
175
#if defined(HAS_I422TOYUY2ROW_SSE2)
176
  if (TestCpuFlag(kCpuHasSSE2)) {
177 178 179 180
    I422ToYUY2Row = I422ToYUY2Row_Any_SSE2;
    if (IS_ALIGNED(width, 16)) {
      I422ToYUY2Row = I422ToYUY2Row_SSE2;
    }
181
  }
182 183
#endif
#if defined(HAS_I422TOYUY2ROW_NEON)
184
  if (TestCpuFlag(kCpuHasNEON)) {
185 186 187 188
    I422ToYUY2Row = I422ToYUY2Row_Any_NEON;
    if (IS_ALIGNED(width, 16)) {
      I422ToYUY2Row = I422ToYUY2Row_NEON;
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
189
  }
190 191
#endif

192
  for (y = 0; y < height; ++y) {
193
    I422ToYUY2Row(src_y, src_u, src_v, dst_yuy2, width);
194 195 196
    src_y += src_stride_y;
    src_u += src_stride_u;
    src_v += src_stride_v;
197
    dst_yuy2 += dst_stride_yuy2;
198 199 200 201
  }
  return 0;
}

202
LIBYUV_API
203 204 205
int I420ToYUY2(const uint8* src_y, int src_stride_y,
               const uint8* src_u, int src_stride_u,
               const uint8* src_v, int src_stride_v,
206
               uint8* dst_yuy2, int dst_stride_yuy2,
207
               int width, int height) {
208 209 210 211
  int y;
  void (*I422ToYUY2Row)(const uint8* src_y, const uint8* src_u,
                        const uint8* src_v, uint8* dst_yuy2, int width) =
      I422ToYUY2Row_C;
212
  if (!src_y || !src_u || !src_v || !dst_yuy2 ||
213
      width <= 0 || height == 0) {
214 215 216 217 218
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
219 220
    dst_yuy2 = dst_yuy2 + (height - 1) * dst_stride_yuy2;
    dst_stride_yuy2 = -dst_stride_yuy2;
221
  }
222
#if defined(HAS_I422TOYUY2ROW_SSE2)
223
  if (TestCpuFlag(kCpuHasSSE2)) {
224 225 226 227
    I422ToYUY2Row = I422ToYUY2Row_Any_SSE2;
    if (IS_ALIGNED(width, 16)) {
      I422ToYUY2Row = I422ToYUY2Row_SSE2;
    }
228
  }
229 230
#endif
#if defined(HAS_I422TOYUY2ROW_NEON)
231
  if (TestCpuFlag(kCpuHasNEON)) {
232 233 234 235
    I422ToYUY2Row = I422ToYUY2Row_Any_NEON;
    if (IS_ALIGNED(width, 16)) {
      I422ToYUY2Row = I422ToYUY2Row_NEON;
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
236
  }
237 238
#endif

239
  for (y = 0; y < height - 1; y += 2) {
240
    I422ToYUY2Row(src_y, src_u, src_v, dst_yuy2, width);
241
    I422ToYUY2Row(src_y + src_stride_y, src_u, src_v,
242
                  dst_yuy2 + dst_stride_yuy2, width);
243 244 245
    src_y += src_stride_y * 2;
    src_u += src_stride_u;
    src_v += src_stride_v;
246
    dst_yuy2 += dst_stride_yuy2 * 2;
247 248
  }
  if (height & 1) {
249
    I422ToYUY2Row(src_y, src_u, src_v, dst_yuy2, width);
250 251 252 253
  }
  return 0;
}

254
LIBYUV_API
255 256 257
int I422ToUYVY(const uint8* src_y, int src_stride_y,
               const uint8* src_u, int src_stride_u,
               const uint8* src_v, int src_stride_v,
258
               uint8* dst_uyvy, int dst_stride_uyvy,
259
               int width, int height) {
260 261 262 263
  int y;
  void (*I422ToUYVYRow)(const uint8* src_y, const uint8* src_u,
                        const uint8* src_v, uint8* dst_uyvy, int width) =
      I422ToUYVYRow_C;
264
  if (!src_y || !src_u || !src_v || !dst_uyvy ||
265
      width <= 0 || height == 0) {
266 267 268 269 270
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
271 272
    dst_uyvy = dst_uyvy + (height - 1) * dst_stride_uyvy;
    dst_stride_uyvy = -dst_stride_uyvy;
273
  }
274
  // Coalesce rows.
275 276 277 278
  if (src_stride_y == width &&
      src_stride_u * 2 == width &&
      src_stride_v * 2 == width &&
      dst_stride_uyvy == width * 2) {
279 280 281
    width *= height;
    height = 1;
    src_stride_y = src_stride_u = src_stride_v = dst_stride_uyvy = 0;
282
  }
283
#if defined(HAS_I422TOUYVYROW_SSE2)
284
  if (TestCpuFlag(kCpuHasSSE2)) {
285 286 287 288
    I422ToUYVYRow = I422ToUYVYRow_Any_SSE2;
    if (IS_ALIGNED(width, 16)) {
      I422ToUYVYRow = I422ToUYVYRow_SSE2;
    }
289
  }
290 291
#endif
#if defined(HAS_I422TOUYVYROW_NEON)
292
  if (TestCpuFlag(kCpuHasNEON)) {
293 294 295 296
    I422ToUYVYRow = I422ToUYVYRow_Any_NEON;
    if (IS_ALIGNED(width, 16)) {
      I422ToUYVYRow = I422ToUYVYRow_NEON;
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
297
  }
298 299
#endif

300
  for (y = 0; y < height; ++y) {
301
    I422ToUYVYRow(src_y, src_u, src_v, dst_uyvy, width);
302 303 304
    src_y += src_stride_y;
    src_u += src_stride_u;
    src_v += src_stride_v;
305
    dst_uyvy += dst_stride_uyvy;
306 307 308 309
  }
  return 0;
}

310
LIBYUV_API
311 312 313
int I420ToUYVY(const uint8* src_y, int src_stride_y,
               const uint8* src_u, int src_stride_u,
               const uint8* src_v, int src_stride_v,
314
               uint8* dst_uyvy, int dst_stride_uyvy,
315
               int width, int height) {
316 317 318 319
  int y;
  void (*I422ToUYVYRow)(const uint8* src_y, const uint8* src_u,
                        const uint8* src_v, uint8* dst_uyvy, int width) =
      I422ToUYVYRow_C;
320
  if (!src_y || !src_u || !src_v || !dst_uyvy ||
321
      width <= 0 || height == 0) {
322 323 324 325 326
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
327 328
    dst_uyvy = dst_uyvy + (height - 1) * dst_stride_uyvy;
    dst_stride_uyvy = -dst_stride_uyvy;
329
  }
330
#if defined(HAS_I422TOUYVYROW_SSE2)
331
  if (TestCpuFlag(kCpuHasSSE2)) {
332 333 334 335
    I422ToUYVYRow = I422ToUYVYRow_Any_SSE2;
    if (IS_ALIGNED(width, 16)) {
      I422ToUYVYRow = I422ToUYVYRow_SSE2;
    }
336
  }
337 338
#endif
#if defined(HAS_I422TOUYVYROW_NEON)
339
  if (TestCpuFlag(kCpuHasNEON)) {
340 341 342 343
    I422ToUYVYRow = I422ToUYVYRow_Any_NEON;
    if (IS_ALIGNED(width, 16)) {
      I422ToUYVYRow = I422ToUYVYRow_NEON;
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
344
  }
345 346
#endif

347
  for (y = 0; y < height - 1; y += 2) {
348
    I422ToUYVYRow(src_y, src_u, src_v, dst_uyvy, width);
349
    I422ToUYVYRow(src_y + src_stride_y, src_u, src_v,
350
                  dst_uyvy + dst_stride_uyvy, width);
351 352 353
    src_y += src_stride_y * 2;
    src_u += src_stride_u;
    src_v += src_stride_v;
354
    dst_uyvy += dst_stride_uyvy * 2;
355 356
  }
  if (height & 1) {
357
    I422ToUYVYRow(src_y, src_u, src_v, dst_uyvy, width);
358 359 360 361
  }
  return 0;
}

362 363 364 365 366 367 368
LIBYUV_API
int I420ToNV12(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_y, int dst_stride_y,
               uint8* dst_uv, int dst_stride_uv,
               int width, int height) {
369 370 371 372 373 374
  int y;
  void (*MergeUVRow_)(const uint8* src_u, const uint8* src_v, uint8* dst_uv,
      int width) = MergeUVRow_C;
  // Coalesce rows.
  int halfwidth = (width + 1) >> 1;
  int halfheight = (height + 1) >> 1;
375 376 377 378 379 380 381
  if (!src_y || !src_u || !src_v || !dst_y || !dst_uv ||
      width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
382
    halfheight = (height + 1) >> 1;
383 384 385 386 387
    dst_y = dst_y + (height - 1) * dst_stride_y;
    dst_uv = dst_uv + (halfheight - 1) * dst_stride_uv;
    dst_stride_y = -dst_stride_y;
    dst_stride_uv = -dst_stride_uv;
  }
388
  if (src_stride_y == width &&
389
      dst_stride_y == width) {
390
    width *= height;
391
    height = 1;
392
    src_stride_y = dst_stride_y = 0;
393
  }
394 395 396 397 398
  // Coalesce rows.
  if (src_stride_u == halfwidth &&
      src_stride_v == halfwidth &&
      dst_stride_uv == halfwidth * 2) {
    halfwidth *= halfheight;
399
    halfheight = 1;
400
    src_stride_u = src_stride_v = dst_stride_uv = 0;
401
  }
402
#if defined(HAS_MERGEUVROW_SSE2)
403
  if (TestCpuFlag(kCpuHasSSE2)) {
404
    MergeUVRow_ = MergeUVRow_Any_SSE2;
405
    if (IS_ALIGNED(halfwidth, 16)) {
406
      MergeUVRow_ = MergeUVRow_SSE2;
407
    }
408
  }
409
#endif
410
#if defined(HAS_MERGEUVROW_AVX2)
411
  if (TestCpuFlag(kCpuHasAVX2)) {
412
    MergeUVRow_ = MergeUVRow_Any_AVX2;
413
    if (IS_ALIGNED(halfwidth, 32)) {
414
      MergeUVRow_ = MergeUVRow_AVX2;
415
    }
416 417
  }
#endif
418
#if defined(HAS_MERGEUVROW_NEON)
419
  if (TestCpuFlag(kCpuHasNEON)) {
420
    MergeUVRow_ = MergeUVRow_Any_NEON;
421
    if (IS_ALIGNED(halfwidth, 16)) {
422
      MergeUVRow_ = MergeUVRow_NEON;
423 424 425 426
    }
  }
#endif

427
  CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
428
  for (y = 0; y < halfheight; ++y) {
429
    // Merge a row of U and V into a row of UV.
430
    MergeUVRow_(src_u, src_v, dst_uv, halfwidth);
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
    src_u += src_stride_u;
    src_v += src_stride_v;
    dst_uv += dst_stride_uv;
  }
  return 0;
}

LIBYUV_API
int I420ToNV21(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_y, int dst_stride_y,
               uint8* dst_vu, int dst_stride_vu,
               int width, int height) {
  return I420ToNV12(src_y, src_stride_y,
                    src_v, src_stride_v,
                    src_u, src_stride_u,
                    dst_y, src_stride_y,
                    dst_vu, dst_stride_vu,
                    width, height);
}

453
// Convert I420 to ARGB.
454
LIBYUV_API
455 456 457 458 459
int I420ToARGB(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) {
460 461 462 463 464
  int y;
  void (*I422ToARGBRow)(const uint8* y_buf,
                        const uint8* u_buf,
                        const uint8* v_buf,
                        uint8* rgb_buf,
465
                        struct YuvConstants* yuvconstants,
466
                        int width) = I422ToARGBRow_C;
467 468 469 470
  if (!src_y || !src_u || !src_v || !dst_argb ||
      width <= 0 || height == 0) {
    return -1;
  }
471 472 473 474 475 476
  // 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;
  }
477
#if defined(HAS_I422TOARGBROW_SSSE3)
478
  if (TestCpuFlag(kCpuHasSSSE3)) {
479
    I422ToARGBRow = I422ToARGBRow_Any_SSSE3;
480
    if (IS_ALIGNED(width, 8)) {
481
      I422ToARGBRow = I422ToARGBRow_SSSE3;
482 483
    }
  }
484 485
#endif
#if defined(HAS_I422TOARGBROW_AVX2)
486
  if (TestCpuFlag(kCpuHasAVX2)) {
487 488 489 490 491 492 493
    I422ToARGBRow = I422ToARGBRow_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      I422ToARGBRow = I422ToARGBRow_AVX2;
    }
  }
#endif
#if defined(HAS_I422TOARGBROW_NEON)
494
  if (TestCpuFlag(kCpuHasNEON)) {
495 496 497 498 499
    I422ToARGBRow = I422ToARGBRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToARGBRow = I422ToARGBRow_NEON;
    }
  }
500 501
#endif
#if defined(HAS_I422TOARGBROW_MIPS_DSPR2)
502 503 504 505 506 507 508
  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;
  }
509 510
#endif

511
  for (y = 0; y < height; ++y) {
512
    I422ToARGBRow(src_y, src_u, src_v, dst_argb, &kYuvConstants, width);
513 514 515 516 517 518 519 520 521 522 523
    dst_argb += dst_stride_argb;
    src_y += src_stride_y;
    if (y & 1) {
      src_u += src_stride_u;
      src_v += src_stride_v;
    }
  }
  return 0;
}

// Convert I420 to BGRA.
524
LIBYUV_API
525 526 527 528 529
int I420ToBGRA(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_bgra, int dst_stride_bgra,
               int width, int height) {
530 531 532 533 534
  int y;
  void (*I422ToBGRARow)(const uint8* y_buf,
                        const uint8* u_buf,
                        const uint8* v_buf,
                        uint8* rgb_buf,
535
                        struct YuvConstants* yuvconstants,
536
                        int width) = I422ToBGRARow_C;
537
  if (!src_y || !src_u || !src_v || !dst_bgra ||
538 539 540
      width <= 0 || height == 0) {
    return -1;
  }
541 542 543 544 545 546
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_bgra = dst_bgra + (height - 1) * dst_stride_bgra;
    dst_stride_bgra = -dst_stride_bgra;
  }
547
#if defined(HAS_I422TOBGRAROW_SSSE3)
548
  if (TestCpuFlag(kCpuHasSSSE3)) {
549
    I422ToBGRARow = I422ToBGRARow_Any_SSSE3;
550
    if (IS_ALIGNED(width, 8)) {
551
      I422ToBGRARow = I422ToBGRARow_SSSE3;
552 553
    }
  }
554
#endif
555
#if defined(HAS_I422TOBGRAROW_AVX2)
556
  if (TestCpuFlag(kCpuHasAVX2)) {
557 558 559 560 561 562
    I422ToBGRARow = I422ToBGRARow_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      I422ToBGRARow = I422ToBGRARow_AVX2;
    }
  }
#endif
563
#if defined(HAS_I422TOBGRAROW_NEON)
564
  if (TestCpuFlag(kCpuHasNEON)) {
565 566 567 568 569
    I422ToBGRARow = I422ToBGRARow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToBGRARow = I422ToBGRARow_NEON;
    }
  }
570 571
#endif
#if defined(HAS_I422TOBGRAROW_MIPS_DSPR2)
572 573 574 575 576 577 578
  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_bgra, 4) && IS_ALIGNED(dst_stride_bgra, 4)) {
    I422ToBGRARow = I422ToBGRARow_MIPS_DSPR2;
  }
579 580
#endif

581
  for (y = 0; y < height; ++y) {
582
    I422ToBGRARow(src_y, src_u, src_v, dst_bgra, &kYuvConstants, width);
583 584 585 586 587 588 589 590 591 592 593
    dst_bgra += dst_stride_bgra;
    src_y += src_stride_y;
    if (y & 1) {
      src_u += src_stride_u;
      src_v += src_stride_v;
    }
  }
  return 0;
}

// Convert I420 to ABGR.
594
LIBYUV_API
595 596 597 598 599
int I420ToABGR(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_abgr, int dst_stride_abgr,
               int width, int height) {
600 601 602 603 604
  int y;
  void (*I422ToABGRRow)(const uint8* y_buf,
                        const uint8* u_buf,
                        const uint8* v_buf,
                        uint8* rgb_buf,
605
                        struct YuvConstants* yuvconstants,
606
                        int width) = I422ToABGRRow_C;
607
  if (!src_y || !src_u || !src_v || !dst_abgr ||
608 609 610
      width <= 0 || height == 0) {
    return -1;
  }
611 612 613 614 615 616
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_abgr = dst_abgr + (height - 1) * dst_stride_abgr;
    dst_stride_abgr = -dst_stride_abgr;
  }
617
#if defined(HAS_I422TOABGRROW_SSSE3)
618
  if (TestCpuFlag(kCpuHasSSSE3)) {
619
    I422ToABGRRow = I422ToABGRRow_Any_SSSE3;
620
    if (IS_ALIGNED(width, 8)) {
621
      I422ToABGRRow = I422ToABGRRow_SSSE3;
622 623
    }
  }
624
#endif
625
#if defined(HAS_I422TOABGRROW_AVX2)
626
  if (TestCpuFlag(kCpuHasAVX2)) {
627 628 629 630 631 632
    I422ToABGRRow = I422ToABGRRow_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      I422ToABGRRow = I422ToABGRRow_AVX2;
    }
  }
#endif
633
#if defined(HAS_I422TOABGRROW_NEON)
634
  if (TestCpuFlag(kCpuHasNEON)) {
635 636 637 638 639
    I422ToABGRRow = I422ToABGRRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToABGRRow = I422ToABGRRow_NEON;
    }
  }
640 641
#endif

642
  for (y = 0; y < height; ++y) {
643
    I422ToABGRRow(src_y, src_u, src_v, dst_abgr, &kYuvConstants, width);
644 645 646 647 648 649 650 651 652 653
    dst_abgr += dst_stride_abgr;
    src_y += src_stride_y;
    if (y & 1) {
      src_u += src_stride_u;
      src_v += src_stride_v;
    }
  }
  return 0;
}

654
// Convert I420 to RGBA.
655
LIBYUV_API
656 657 658 659 660
int I420ToRGBA(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_rgba, int dst_stride_rgba,
               int width, int height) {
661 662 663 664 665
  int y;
  void (*I422ToRGBARow)(const uint8* y_buf,
                        const uint8* u_buf,
                        const uint8* v_buf,
                        uint8* rgb_buf,
666
                        struct YuvConstants* yuvconstants,
667
                        int width) = I422ToRGBARow_C;
668
  if (!src_y || !src_u || !src_v || !dst_rgba ||
669 670 671 672 673 674 675 676 677
      width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_rgba = dst_rgba + (height - 1) * dst_stride_rgba;
    dst_stride_rgba = -dst_stride_rgba;
  }
678
#if defined(HAS_I422TORGBAROW_SSSE3)
679
  if (TestCpuFlag(kCpuHasSSSE3)) {
680 681
    I422ToRGBARow = I422ToRGBARow_Any_SSSE3;
    if (IS_ALIGNED(width, 8)) {
682
      I422ToRGBARow = I422ToRGBARow_SSSE3;
683 684
    }
  }
685
#endif
686
#if defined(HAS_I422TORGBAROW_AVX2)
687
  if (TestCpuFlag(kCpuHasAVX2)) {
688 689 690 691 692 693
    I422ToRGBARow = I422ToRGBARow_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      I422ToRGBARow = I422ToRGBARow_AVX2;
    }
  }
#endif
694
#if defined(HAS_I422TORGBAROW_NEON)
695
  if (TestCpuFlag(kCpuHasNEON)) {
696 697 698 699 700
    I422ToRGBARow = I422ToRGBARow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToRGBARow = I422ToRGBARow_NEON;
    }
  }
701 702
#endif

703
  for (y = 0; y < height; ++y) {
704
    I422ToRGBARow(src_y, src_u, src_v, dst_rgba, &kYuvConstants, width);
705 706 707 708 709 710 711 712 713 714
    dst_rgba += dst_stride_rgba;
    src_y += src_stride_y;
    if (y & 1) {
      src_u += src_stride_u;
      src_v += src_stride_v;
    }
  }
  return 0;
}

715
// Convert I420 to RGB24.
716
LIBYUV_API
717 718 719
int I420ToRGB24(const uint8* src_y, int src_stride_y,
                const uint8* src_u, int src_stride_u,
                const uint8* src_v, int src_stride_v,
720
                uint8* dst_rgb24, int dst_stride_rgb24,
721
                int width, int height) {
722 723 724 725 726
  int y;
  void (*I422ToRGB24Row)(const uint8* y_buf,
                         const uint8* u_buf,
                         const uint8* v_buf,
                         uint8* rgb_buf,
727
                         struct YuvConstants* yuvconstants,
728
                         int width) = I422ToRGB24Row_C;
729
  if (!src_y || !src_u || !src_v || !dst_rgb24 ||
730 731 732
      width <= 0 || height == 0) {
    return -1;
  }
733 734 735
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
736 737 738
    dst_rgb24 = dst_rgb24 + (height - 1) * dst_stride_rgb24;
    dst_stride_rgb24 = -dst_stride_rgb24;
  }
739
#if defined(HAS_I422TORGB24ROW_SSSE3)
740
  if (TestCpuFlag(kCpuHasSSSE3)) {
741
    I422ToRGB24Row = I422ToRGB24Row_Any_SSSE3;
742
    if (IS_ALIGNED(width, 8)) {
743
      I422ToRGB24Row = I422ToRGB24Row_SSSE3;
744 745
    }
  }
746
#endif
747 748 749 750 751 752 753 754
#if defined(HAS_I422TORGB24ROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2)) {
    I422ToRGB24Row = I422ToRGB24Row_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      I422ToRGB24Row = I422ToRGB24Row_AVX2;
    }
  }
#endif
755
#if defined(HAS_I422TORGB24ROW_NEON)
756
  if (TestCpuFlag(kCpuHasNEON)) {
757 758 759 760 761
    I422ToRGB24Row = I422ToRGB24Row_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToRGB24Row = I422ToRGB24Row_NEON;
    }
  }
762
#endif
763

764
  for (y = 0; y < height; ++y) {
765
    I422ToRGB24Row(src_y, src_u, src_v, dst_rgb24, &kYuvConstants, width);
766
    dst_rgb24 += dst_stride_rgb24;
767 768 769 770 771 772 773 774 775 776
    src_y += src_stride_y;
    if (y & 1) {
      src_u += src_stride_u;
      src_v += src_stride_v;
    }
  }
  return 0;
}

// Convert I420 to RAW.
777
LIBYUV_API
778
int I420ToRAW(const uint8* src_y, int src_stride_y,
779 780 781 782
                const uint8* src_u, int src_stride_u,
                const uint8* src_v, int src_stride_v,
                uint8* dst_raw, int dst_stride_raw,
                int width, int height) {
783 784 785 786 787
  int y;
  void (*I422ToRAWRow)(const uint8* y_buf,
                       const uint8* u_buf,
                       const uint8* v_buf,
                       uint8* rgb_buf,
788
                       struct YuvConstants* yuvconstants,
789
                       int width) = I422ToRAWRow_C;
790
  if (!src_y || !src_u || !src_v || !dst_raw ||
791 792 793
      width <= 0 || height == 0) {
    return -1;
  }
794 795 796
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
797 798 799
    dst_raw = dst_raw + (height - 1) * dst_stride_raw;
    dst_stride_raw = -dst_stride_raw;
  }
800
#if defined(HAS_I422TORAWROW_SSSE3)
801
  if (TestCpuFlag(kCpuHasSSSE3)) {
802
    I422ToRAWRow = I422ToRAWRow_Any_SSSE3;
803
    if (IS_ALIGNED(width, 8)) {
804
      I422ToRAWRow = I422ToRAWRow_SSSE3;
805 806
    }
  }
807
#endif
808 809 810 811 812 813 814 815
#if defined(HAS_I422TORAWROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2)) {
    I422ToRAWRow = I422ToRAWRow_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      I422ToRAWRow = I422ToRAWRow_AVX2;
    }
  }
#endif
816
#if defined(HAS_I422TORAWROW_NEON)
817
  if (TestCpuFlag(kCpuHasNEON)) {
818 819 820 821 822
    I422ToRAWRow = I422ToRAWRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToRAWRow = I422ToRAWRow_NEON;
    }
  }
823
#endif
824

825
  for (y = 0; y < height; ++y) {
826
    I422ToRAWRow(src_y, src_u, src_v, dst_raw, &kYuvConstants, width);
827
    dst_raw += dst_stride_raw;
828 829 830 831 832 833 834 835 836
    src_y += src_stride_y;
    if (y & 1) {
      src_u += src_stride_u;
      src_v += src_stride_v;
    }
  }
  return 0;
}

837
// Convert I420 to ARGB1555.
838
LIBYUV_API
839 840 841 842 843
int I420ToARGB1555(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_argb1555, int dst_stride_argb1555,
                   int width, int height) {
844 845 846 847 848
  int y;
  void (*I422ToARGB1555Row)(const uint8* y_buf,
                            const uint8* u_buf,
                            const uint8* v_buf,
                            uint8* rgb_buf,
849
                            struct YuvConstants* yuvconstants,
850
                            int width) = I422ToARGB1555Row_C;
851
  if (!src_y || !src_u || !src_v || !dst_argb1555 ||
852 853 854
      width <= 0 || height == 0) {
    return -1;
  }
855 856 857
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
858 859
    dst_argb1555 = dst_argb1555 + (height - 1) * dst_stride_argb1555;
    dst_stride_argb1555 = -dst_stride_argb1555;
860
  }
861
#if defined(HAS_I422TOARGB1555ROW_SSSE3)
862
  if (TestCpuFlag(kCpuHasSSSE3)) {
863
    I422ToARGB1555Row = I422ToARGB1555Row_Any_SSSE3;
864
    if (IS_ALIGNED(width, 8)) {
865
      I422ToARGB1555Row = I422ToARGB1555Row_SSSE3;
866
    }
867
  }
868
#endif
869 870 871 872 873 874 875 876
#if defined(HAS_I422TOARGB1555ROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2)) {
    I422ToARGB1555Row = I422ToARGB1555Row_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      I422ToARGB1555Row = I422ToARGB1555Row_AVX2;
    }
  }
#endif
877
#if defined(HAS_I422TOARGB1555ROW_NEON)
878
  if (TestCpuFlag(kCpuHasNEON)) {
879
    I422ToARGB1555Row = I422ToARGB1555Row_Any_NEON;
880
    if (IS_ALIGNED(width, 8)) {
881
      I422ToARGB1555Row = I422ToARGB1555Row_NEON;
882 883
    }
  }
884
#endif
885

886
  for (y = 0; y < height; ++y) {
887
    I422ToARGB1555Row(src_y, src_u, src_v, dst_argb1555, &kYuvConstants, width);
888
    dst_argb1555 += dst_stride_argb1555;
889 890 891 892 893 894 895 896 897
    src_y += src_stride_y;
    if (y & 1) {
      src_u += src_stride_u;
      src_v += src_stride_v;
    }
  }
  return 0;
}

898 899

// Convert I420 to ARGB4444.
900
LIBYUV_API
901
int I420ToARGB4444(const uint8* src_y, int src_stride_y,
902 903
                   const uint8* src_u, int src_stride_u,
                   const uint8* src_v, int src_stride_v,
904
                   uint8* dst_argb4444, int dst_stride_argb4444,
905
                   int width, int height) {
906 907 908 909 910
  int y;
  void (*I422ToARGB4444Row)(const uint8* y_buf,
                            const uint8* u_buf,
                            const uint8* v_buf,
                            uint8* rgb_buf,
911
                            struct YuvConstants* yuvconstants,
912
                            int width) = I422ToARGB4444Row_C;
913
  if (!src_y || !src_u || !src_v || !dst_argb4444 ||
914 915 916
      width <= 0 || height == 0) {
    return -1;
  }
917 918 919
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
920 921
    dst_argb4444 = dst_argb4444 + (height - 1) * dst_stride_argb4444;
    dst_stride_argb4444 = -dst_stride_argb4444;
922
  }
923
#if defined(HAS_I422TOARGB4444ROW_SSSE3)
924
  if (TestCpuFlag(kCpuHasSSSE3)) {
925
    I422ToARGB4444Row = I422ToARGB4444Row_Any_SSSE3;
926
    if (IS_ALIGNED(width, 8)) {
927
      I422ToARGB4444Row = I422ToARGB4444Row_SSSE3;
928
    }
929
  }
930
#endif
931 932 933 934 935 936 937 938
#if defined(HAS_I422TOARGB4444ROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2)) {
    I422ToARGB4444Row = I422ToARGB4444Row_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      I422ToARGB4444Row = I422ToARGB4444Row_AVX2;
    }
  }
#endif
939
#if defined(HAS_I422TOARGB4444ROW_NEON)
940
  if (TestCpuFlag(kCpuHasNEON)) {
941 942 943
    I422ToARGB4444Row = I422ToARGB4444Row_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToARGB4444Row = I422ToARGB4444Row_NEON;
944 945
    }
  }
946
#endif
947

948
  for (y = 0; y < height; ++y) {
949
    I422ToARGB4444Row(src_y, src_u, src_v, dst_argb4444, &kYuvConstants, width);
950
    dst_argb4444 += dst_stride_argb4444;
951 952 953 954 955 956 957 958 959
    src_y += src_stride_y;
    if (y & 1) {
      src_u += src_stride_u;
      src_v += src_stride_v;
    }
  }
  return 0;
}

960
// Convert I420 to RGB565.
961
LIBYUV_API
962 963 964 965 966
int I420ToRGB565(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_rgb565, int dst_stride_rgb565,
                 int width, int height) {
967 968 969 970 971
  int y;
  void (*I422ToRGB565Row)(const uint8* y_buf,
                          const uint8* u_buf,
                          const uint8* v_buf,
                          uint8* rgb_buf,
972
                          struct YuvConstants* yuvconstants,
973
                          int width) = I422ToRGB565Row_C;
974
  if (!src_y || !src_u || !src_v || !dst_rgb565 ||
975 976 977
      width <= 0 || height == 0) {
    return -1;
  }
978 979 980
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
981 982
    dst_rgb565 = dst_rgb565 + (height - 1) * dst_stride_rgb565;
    dst_stride_rgb565 = -dst_stride_rgb565;
983
  }
984
#if defined(HAS_I422TORGB565ROW_SSSE3)
985
  if (TestCpuFlag(kCpuHasSSSE3)) {
986
    I422ToRGB565Row = I422ToRGB565Row_Any_SSSE3;
987
    if (IS_ALIGNED(width, 8)) {
988
      I422ToRGB565Row = I422ToRGB565Row_SSSE3;
989
    }
990
  }
991
#endif
992 993 994 995 996 997 998 999
#if defined(HAS_I422TORGB565ROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2)) {
    I422ToRGB565Row = I422ToRGB565Row_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      I422ToRGB565Row = I422ToRGB565Row_AVX2;
    }
  }
#endif
1000
#if defined(HAS_I422TORGB565ROW_NEON)
1001
  if (TestCpuFlag(kCpuHasNEON)) {
1002 1003 1004
    I422ToRGB565Row = I422ToRGB565Row_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToRGB565Row = I422ToRGB565Row_NEON;
1005 1006
    }
  }
1007
#endif
1008

1009
  for (y = 0; y < height; ++y) {
1010
    I422ToRGB565Row(src_y, src_u, src_v, dst_rgb565, &kYuvConstants, width);
1011
    dst_rgb565 += dst_stride_rgb565;
1012 1013 1014 1015 1016 1017 1018 1019 1020
    src_y += src_stride_y;
    if (y & 1) {
      src_u += src_stride_u;
      src_v += src_stride_v;
    }
  }
  return 0;
}

1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
// Ordered 8x8 dither for 888 to 565.  Values from 0 to 7.
static const uint8 kDither565_4x4[16] = {
  0, 4, 1, 5,
  6, 2, 7, 3,
  1, 5, 0, 4,
  7, 3, 6, 2,
};

// Convert I420 to RGB565 with dithering.
LIBYUV_API
int I420ToRGB565Dither(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_rgb565, int dst_stride_rgb565,
                       const uint8* dither4x4, int width, int height) {
  int y;
  void (*I422ToARGBRow)(const uint8* y_buf,
                        const uint8* u_buf,
                        const uint8* v_buf,
                        uint8* rgb_buf,
1041
                        struct YuvConstants* yuvconstants,
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
                        int width) = I422ToARGBRow_C;
  void (*ARGBToRGB565DitherRow)(const uint8* src_argb, uint8* dst_rgb,
      const uint32 dither4, int pix) = ARGBToRGB565DitherRow_C;
  if (!src_y || !src_u || !src_v || !dst_rgb565 ||
      width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_rgb565 = dst_rgb565 + (height - 1) * dst_stride_rgb565;
    dst_stride_rgb565 = -dst_stride_rgb565;
  }
  if (!dither4x4) {
    dither4x4 = kDither565_4x4;
  }
#if defined(HAS_I422TOARGBROW_SSSE3)
  if (TestCpuFlag(kCpuHasSSSE3)) {
    I422ToARGBRow = I422ToARGBRow_Any_SSSE3;
    if (IS_ALIGNED(width, 8)) {
      I422ToARGBRow = I422ToARGBRow_SSSE3;
    }
  }
#endif
#if defined(HAS_I422TOARGBROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2)) {
    I422ToARGBRow = I422ToARGBRow_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      I422ToARGBRow = I422ToARGBRow_AVX2;
    }
  }
#endif
#if defined(HAS_I422TOARGBROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    I422ToARGBRow = I422ToARGBRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToARGBRow = I422ToARGBRow_NEON;
    }
  }
#endif
#if defined(HAS_I422TOARGBROW_MIPS_DSPR2)
  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)) {
    I422ToARGBRow = I422ToARGBRow_MIPS_DSPR2;
  }
#endif
#if defined(HAS_ARGBTORGB565DITHERROW_SSE2)
  if (TestCpuFlag(kCpuHasSSE2)) {
    ARGBToRGB565DitherRow = ARGBToRGB565DitherRow_Any_SSE2;
    if (IS_ALIGNED(width, 4)) {
      ARGBToRGB565DitherRow = ARGBToRGB565DitherRow_SSE2;
    }
  }
#endif
#if defined(HAS_ARGBTORGB565DITHERROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2)) {
    ARGBToRGB565DitherRow = ARGBToRGB565DitherRow_Any_AVX2;
    if (IS_ALIGNED(width, 8)) {
      ARGBToRGB565DitherRow = ARGBToRGB565DitherRow_AVX2;
    }
  }
1105 1106 1107 1108 1109 1110 1111 1112
#endif
#if defined(HAS_ARGBTORGB565DITHERROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    ARGBToRGB565DitherRow = ARGBToRGB565DitherRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      ARGBToRGB565DitherRow = ARGBToRGB565DitherRow_NEON;
    }
  }
1113 1114 1115 1116 1117
#endif
  {
    // Allocate a row of argb.
    align_buffer_64(row_argb, width * 4);
    for (y = 0; y < height; ++y) {
1118
      I422ToARGBRow(src_y, src_u, src_v, row_argb, &kYuvConstants, width);
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
      ARGBToRGB565DitherRow(row_argb, dst_rgb565,
                            *(uint32*)(dither4x4 + ((y & 3) << 2)), width);
      dst_rgb565 += dst_stride_rgb565;
      src_y += src_stride_y;
      if (y & 1) {
        src_u += src_stride_u;
        src_v += src_stride_v;
      }
    }
    free_aligned_buffer_64(row_argb);
  }
  return 0;
}

1133
// Convert I420 to specified format
1134
LIBYUV_API
1135 1136 1137 1138 1139
int ConvertFromI420(const uint8* y, int y_stride,
                    const uint8* u, int u_stride,
                    const uint8* v, int v_stride,
                    uint8* dst_sample, int dst_sample_stride,
                    int width, int height,
1140 1141
                    uint32 fourcc) {
  uint32 format = CanonicalFourCC(fourcc);
1142
  int r = 0;
1143 1144
  if (!y || !u|| !v || !dst_sample ||
      width <= 0 || height == 0) {
1145 1146 1147 1148 1149
    return -1;
  }
  switch (format) {
    // Single plane formats
    case FOURCC_YUY2:
1150 1151 1152 1153 1154 1155
      r = I420ToYUY2(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample,
                     dst_sample_stride ? dst_sample_stride : width * 2,
                     width, height);
1156 1157
      break;
    case FOURCC_UYVY:
1158
      r = I420ToUYVY(y, y_stride,
1159 1160 1161 1162 1163 1164
                     u, u_stride,
                     v, v_stride,
                     dst_sample,
                     dst_sample_stride ? dst_sample_stride : width * 2,
                     width, height);
      break;
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
    case FOURCC_RGBP:
      r = I420ToRGB565(y, y_stride,
                       u, u_stride,
                       v, v_stride,
                       dst_sample,
                       dst_sample_stride ? dst_sample_stride : width * 2,
                       width, height);
      break;
    case FOURCC_RGBO:
      r = I420ToARGB1555(y, y_stride,
                         u, u_stride,
                         v, v_stride,
                         dst_sample,
                         dst_sample_stride ? dst_sample_stride : width * 2,
                         width, height);
      break;
    case FOURCC_R444:
      r = I420ToARGB4444(y, y_stride,
                         u, u_stride,
                         v, v_stride,
                         dst_sample,
                         dst_sample_stride ? dst_sample_stride : width * 2,
                         width, height);
      break;
1189
    case FOURCC_24BG:
1190 1191 1192 1193 1194 1195
      r = I420ToRGB24(y, y_stride,
                      u, u_stride,
                      v, v_stride,
                      dst_sample,
                      dst_sample_stride ? dst_sample_stride : width * 3,
                      width, height);
1196 1197
      break;
    case FOURCC_RAW:
1198 1199 1200 1201 1202 1203
      r = I420ToRAW(y, y_stride,
                    u, u_stride,
                    v, v_stride,
                    dst_sample,
                    dst_sample_stride ? dst_sample_stride : width * 3,
                    width, height);
1204 1205
      break;
    case FOURCC_ARGB:
1206 1207 1208 1209 1210 1211
      r = I420ToARGB(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample,
                     dst_sample_stride ? dst_sample_stride : width * 4,
                     width, height);
1212 1213
      break;
    case FOURCC_BGRA:
1214 1215 1216 1217 1218 1219
      r = I420ToBGRA(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample,
                     dst_sample_stride ? dst_sample_stride : width * 4,
                     width, height);
1220 1221
      break;
    case FOURCC_ABGR:
1222 1223 1224 1225 1226 1227
      r = I420ToABGR(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample,
                     dst_sample_stride ? dst_sample_stride : width * 4,
                     width, height);
1228 1229 1230 1231 1232 1233 1234 1235
      break;
    case FOURCC_RGBA:
      r = I420ToRGBA(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample,
                     dst_sample_stride ? dst_sample_stride : width * 4,
                     width, height);
1236 1237
      break;
    case FOURCC_I400:
1238 1239 1240 1241
      r = I400Copy(y, y_stride,
                   dst_sample,
                   dst_sample_stride ? dst_sample_stride : width,
                   width, height);
1242
      break;
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
    case FOURCC_NV12: {
      uint8* dst_uv = dst_sample + width * height;
      r = I420ToNV12(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample,
                     dst_sample_stride ? dst_sample_stride : width,
                     dst_uv,
                     dst_sample_stride ? dst_sample_stride : width,
                     width, height);
      break;
    }
    case FOURCC_NV21: {
      uint8* dst_vu = dst_sample + width * height;
      r = I420ToNV21(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample,
                     dst_sample_stride ? dst_sample_stride : width,
                     dst_vu,
                     dst_sample_stride ? dst_sample_stride : width,
                     width, height);
      break;
    }
1267
    // TODO(fbarchard): Add M420.
1268 1269 1270
    // Triplanar formats
    // TODO(fbarchard): halfstride instead of halfwidth
    case FOURCC_I420:
1271
    case FOURCC_YU12:
1272 1273 1274 1275 1276
    case FOURCC_YV12: {
      int halfwidth = (width + 1) / 2;
      int halfheight = (height + 1) / 2;
      uint8* dst_u;
      uint8* dst_v;
1277
      if (format == FOURCC_YV12) {
1278 1279
        dst_v = dst_sample + width * height;
        dst_u = dst_v + halfwidth * halfheight;
1280 1281 1282
      } else {
        dst_u = dst_sample + width * height;
        dst_v = dst_u + halfwidth * halfheight;
1283
      }
1284 1285 1286 1287 1288 1289 1290
      r = I420Copy(y, y_stride,
                   u, u_stride,
                   v, v_stride,
                   dst_sample, width,
                   dst_u, halfwidth,
                   dst_v, halfwidth,
                   width, height);
1291 1292 1293 1294 1295 1296 1297
      break;
    }
    case FOURCC_I422:
    case FOURCC_YV16: {
      int halfwidth = (width + 1) / 2;
      uint8* dst_u;
      uint8* dst_v;
1298
      if (format == FOURCC_YV16) {
1299 1300
        dst_v = dst_sample + width * height;
        dst_u = dst_v + halfwidth * height;
1301 1302 1303
      } else {
        dst_u = dst_sample + width * height;
        dst_v = dst_u + halfwidth * height;
1304
      }
1305 1306 1307 1308 1309 1310 1311
      r = I420ToI422(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample, width,
                     dst_u, halfwidth,
                     dst_v, halfwidth,
                     width, height);
1312 1313 1314 1315 1316 1317
      break;
    }
    case FOURCC_I444:
    case FOURCC_YV24: {
      uint8* dst_u;
      uint8* dst_v;
1318
      if (format == FOURCC_YV24) {
1319 1320
        dst_v = dst_sample + width * height;
        dst_u = dst_v + width * height;
1321 1322 1323
      } else {
        dst_u = dst_sample + width * height;
        dst_v = dst_u + width * height;
1324
      }
1325 1326 1327 1328 1329 1330 1331
      r = I420ToI444(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample, width,
                     dst_u, width,
                     dst_v, width,
                     width, height);
1332 1333
      break;
    }
1334 1335 1336 1337
    case FOURCC_I411: {
      int quarterwidth = (width + 3) / 4;
      uint8* dst_u = dst_sample + width * height;
      uint8* dst_v = dst_u + quarterwidth * height;
1338 1339 1340 1341 1342 1343 1344
      r = I420ToI411(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample, width,
                     dst_u, quarterwidth,
                     dst_v, quarterwidth,
                     width, height);
1345 1346
      break;
    }
1347 1348 1349 1350 1351

    // Formats not supported - MJPG, biplanar, some rgb formats.
    default:
      return -1;  // unknown fourcc - return failure code.
  }
1352
  return r;
1353 1354 1355 1356 1357 1358
}

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