convert_from.cc 39.7 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 18
 *  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/format_conversion.h"
#include "libyuv/planar_functions.h"
#include "libyuv/rotate.h"
19
#include "libyuv/scale.h"  // For ScalePlane()
20
#include "libyuv/video_common.h"
21
#include "libyuv/row.h"
22 23 24 25 26 27

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

28
LIBYUV_API
29 30 31 32 33 34 35
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) {
36 37 38 39 40
  if (!src_y || !src_u || !src_v ||
      !dst_y || !dst_u || !dst_v ||
      width <= 0 || height == 0) {
    return -1;
  }
41 42 43 44 45 46 47 48 49 50
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_y = dst_y + (height - 1) * dst_stride_y;
    dst_u = dst_u + (height - 1) * dst_stride_u;
    dst_v = dst_v + (height - 1) * dst_stride_v;
    dst_stride_y = -dst_stride_y;
    dst_stride_u = -dst_stride_u;
    dst_stride_v = -dst_stride_v;
  }
51 52
  int halfwidth = (width + 1) >> 1;
  void (*CopyRow)(const uint8* src, uint8* dst, int width) = CopyRow_C;
fbarchard@google.com's avatar
fbarchard@google.com committed
53
#if defined(HAS_COPYROW_X86)
54 55
  if (IS_ALIGNED(halfwidth, 4)) {
    CopyRow = CopyRow_X86;
fbarchard@google.com's avatar
fbarchard@google.com committed
56 57
  }
#endif
58
#if defined(HAS_COPYROW_SSE2)
fbarchard@google.com's avatar
fbarchard@google.com committed
59 60 61 62 63 64 65 66
  if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(halfwidth, 32) &&
      IS_ALIGNED(src_u, 16) && IS_ALIGNED(src_stride_u, 16) &&
      IS_ALIGNED(src_v, 16) && IS_ALIGNED(src_stride_v, 16) &&
      IS_ALIGNED(dst_u, 16) && IS_ALIGNED(dst_stride_u, 16) &&
      IS_ALIGNED(dst_v, 16) && IS_ALIGNED(dst_stride_v, 16)) {
    CopyRow = CopyRow_SSE2;
  }
#endif
67 68 69
#if defined(HAS_COPYROW_ERMS)
  if (TestCpuFlag(kCpuHasERMS)) {
    CopyRow = CopyRow_ERMS;
fbarchard@google.com's avatar
fbarchard@google.com committed
70
  }
71
#endif
fbarchard@google.com's avatar
fbarchard@google.com committed
72 73 74
#if defined(HAS_COPYROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(halfwidth, 32)) {
    CopyRow = CopyRow_NEON;
75 76
  }
#endif
77 78 79 80 81
#if defined(HAS_COPYROW_MIPS)
  if (TestCpuFlag(kCpuHasMIPS)) {
    CopyRow = CopyRow_MIPS;
  }
#endif
82

83 84 85 86 87 88 89 90
  // Copy Y plane
  if (dst_y) {
    CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  }

  // UpSample U plane.
  int y;
  for (y = 0; y < height - 1; y += 2) {
91 92
    CopyRow(src_u, dst_u, halfwidth);
    CopyRow(src_u, dst_u + dst_stride_u, halfwidth);
93 94 95 96
    src_u += src_stride_u;
    dst_u += dst_stride_u * 2;
  }
  if (height & 1) {
97
    CopyRow(src_u, dst_u, halfwidth);
98 99 100 101
  }

  // UpSample V plane.
  for (y = 0; y < height - 1; y += 2) {
102 103
    CopyRow(src_v, dst_v, halfwidth);
    CopyRow(src_v, dst_v + dst_stride_v, halfwidth);
104 105 106 107
    src_v += src_stride_v;
    dst_v += dst_stride_v * 2;
  }
  if (height & 1) {
108
    CopyRow(src_v, dst_v, halfwidth);
109 110 111 112
  }
  return 0;
}

113
// TODO(fbarchard): Enable bilinear when fast enough or specialized upsampler.
114
LIBYUV_API
115 116 117 118 119 120 121
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) {
122 123 124 125 126
  if (!src_y || !src_u|| !src_v ||
      !dst_y || !dst_u || !dst_v ||
      width <= 0 || height == 0) {
    return -1;
  }
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_y = dst_y + (height - 1) * dst_stride_y;
    dst_u = dst_u + (height - 1) * dst_stride_u;
    dst_v = dst_v + (height - 1) * dst_stride_v;
    dst_stride_y = -dst_stride_y;
    dst_stride_u = -dst_stride_u;
    dst_stride_v = -dst_stride_v;
  }

  // Copy Y plane
  if (dst_y) {
    CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  }

  int halfwidth = (width + 1) >> 1;
  int halfheight = (height + 1) >> 1;

146 147 148 149
  // Upsample U plane from from 1/2 width, 1/2 height to 1x width, 1x height.
  ScalePlane(src_u, src_stride_u, halfwidth, halfheight,
             dst_u, dst_stride_u, width, height,
             kFilterNone);
150 151

  // Upsample V plane.
152 153 154
  ScalePlane(src_v, src_stride_v, halfwidth, halfheight,
             dst_v, dst_stride_v, width, height,
             kFilterNone);
155 156 157
  return 0;
}

158 159
// 420 chroma is 1/2 width, 1/2 height
// 411 chroma is 1/4 width, 1x height
160
LIBYUV_API
161 162 163 164 165 166 167
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) {
168 169 170 171 172
  if (!src_y || !src_u || !src_v ||
      !dst_y || !dst_u || !dst_v ||
      width <= 0 || height == 0) {
    return -1;
  }
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_y = dst_y + (height - 1) * dst_stride_y;
    dst_u = dst_u + (height - 1) * dst_stride_u;
    dst_v = dst_v + (height - 1) * dst_stride_v;
    dst_stride_y = -dst_stride_y;
    dst_stride_u = -dst_stride_u;
    dst_stride_v = -dst_stride_v;
  }

  // Copy Y plane
  if (dst_y) {
    CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  }

  int halfwidth = (width + 1) >> 1;
  int halfheight = (height + 1) >> 1;
  int quarterwidth = (width + 3) >> 2;

193 194
  // Resample U plane from 1/2 width, 1/2 height to 1/4 width, 1x height
  ScalePlane(src_u, src_stride_u, halfwidth, halfheight,
fbarchard@google.com's avatar
fbarchard@google.com committed
195
             dst_u, dst_stride_u, quarterwidth, height,
196
             kFilterNone);
197 198

  // Resample V plane.
199
  ScalePlane(src_v, src_stride_v, halfwidth, halfheight,
fbarchard@google.com's avatar
fbarchard@google.com committed
200
             dst_v, dst_stride_v, quarterwidth, height,
201
             kFilterNone);
202 203 204
  return 0;
}

205
// Copy to I400. Source can be I420,422,444,400,NV12,NV21
206
LIBYUV_API
207 208 209
int I400Copy(const uint8* src_y, int src_stride_y,
             uint8* dst_y, int dst_stride_y,
             int width, int height) {
210 211 212 213
  if (!src_y || !dst_y ||
      width <= 0 || height == 0) {
    return -1;
  }
214 215 216 217 218 219 220 221 222 223
  // 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;
}

224
LIBYUV_API
225 226 227
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,
228
               uint8* dst_yuy2, int dst_stride_yuy2,
229
               int width, int height) {
230
  if (!src_y || !src_u || !src_v || !dst_yuy2 ||
231
      width <= 0 || height == 0) {
232 233 234 235 236
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
237 238
    dst_yuy2 = dst_yuy2 + (height - 1) * dst_stride_yuy2;
    dst_stride_yuy2 = -dst_stride_yuy2;
239
  }
240 241 242 243 244 245 246 247 248 249 250
  // Coalesce contiguous rows.
  if (src_stride_y == width &&
      src_stride_u * 2 == width &&
      src_stride_v * 2 == width &&
      dst_stride_yuy2 == width * 2) {
    return I422ToYUY2(src_y, 0,
                      src_u, 0,
                      src_v, 0,
                      dst_yuy2, 0,
                      width * height, 1);
  }
251
  void (*I422ToYUY2Row)(const uint8* src_y, const uint8* src_u,
252
                        const uint8* src_v, uint8* dst_yuy2, int width) =
253 254
      I422ToYUY2Row_C;
#if defined(HAS_I422TOYUY2ROW_SSE2)
255
  if (TestCpuFlag(kCpuHasSSE2) && width >= 16) {
256 257 258 259
    I422ToYUY2Row = I422ToYUY2Row_Any_SSE2;
    if (IS_ALIGNED(width, 16)) {
      I422ToYUY2Row = I422ToYUY2Row_SSE2;
    }
260
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
261
#elif defined(HAS_I422TOYUY2ROW_NEON)
262 263 264 265 266
  if (TestCpuFlag(kCpuHasNEON) && width >= 16) {
    I422ToYUY2Row = I422ToYUY2Row_Any_NEON;
    if (IS_ALIGNED(width, 16)) {
      I422ToYUY2Row = I422ToYUY2Row_NEON;
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
267
  }
268 269 270
#endif

  for (int y = 0; y < height; ++y) {
271
    I422ToYUY2Row(src_y, src_u, src_v, dst_yuy2, width);
272 273 274
    src_y += src_stride_y;
    src_u += src_stride_u;
    src_v += src_stride_v;
275
    dst_yuy2 += dst_stride_yuy2;
276 277 278 279
  }
  return 0;
}

280
LIBYUV_API
281 282 283
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,
284
               uint8* dst_yuy2, int dst_stride_yuy2,
285
               int width, int height) {
286
  if (!src_y || !src_u || !src_v || !dst_yuy2 ||
287
      width <= 0 || height == 0) {
288 289 290 291 292
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
293 294
    dst_yuy2 = dst_yuy2 + (height - 1) * dst_stride_yuy2;
    dst_stride_yuy2 = -dst_stride_yuy2;
295
  }
296
  void (*I422ToYUY2Row)(const uint8* src_y, const uint8* src_u,
297
                        const uint8* src_v, uint8* dst_yuy2, int width) =
298 299
      I422ToYUY2Row_C;
#if defined(HAS_I422TOYUY2ROW_SSE2)
300
  if (TestCpuFlag(kCpuHasSSE2) && width >= 16) {
301 302 303 304
    I422ToYUY2Row = I422ToYUY2Row_Any_SSE2;
    if (IS_ALIGNED(width, 16)) {
      I422ToYUY2Row = I422ToYUY2Row_SSE2;
    }
305
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
306
#elif defined(HAS_I422TOYUY2ROW_NEON)
307 308 309 310 311
  if (TestCpuFlag(kCpuHasNEON) && width >= 16) {
    I422ToYUY2Row = I422ToYUY2Row_Any_NEON;
    if (IS_ALIGNED(width, 16)) {
      I422ToYUY2Row = I422ToYUY2Row_NEON;
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
312
  }
313 314 315
#endif

  for (int y = 0; y < height - 1; y += 2) {
316
    I422ToYUY2Row(src_y, src_u, src_v, dst_yuy2, width);
317
    I422ToYUY2Row(src_y + src_stride_y, src_u, src_v,
318
                  dst_yuy2 + dst_stride_yuy2, width);
319 320 321
    src_y += src_stride_y * 2;
    src_u += src_stride_u;
    src_v += src_stride_v;
322
    dst_yuy2 += dst_stride_yuy2 * 2;
323 324
  }
  if (height & 1) {
325
    I422ToYUY2Row(src_y, src_u, src_v, dst_yuy2, width);
326 327 328 329
  }
  return 0;
}

330
LIBYUV_API
331 332 333
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,
334
               uint8* dst_uyvy, int dst_stride_uyvy,
335
               int width, int height) {
336
  if (!src_y || !src_u || !src_v || !dst_uyvy ||
337
      width <= 0 || height == 0) {
338 339 340 341 342
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
343 344
    dst_uyvy = dst_uyvy + (height - 1) * dst_stride_uyvy;
    dst_stride_uyvy = -dst_stride_uyvy;
345
  }
346 347 348 349 350 351 352 353 354 355 356
  // Coalesce contiguous rows.
  if (src_stride_y == width &&
      src_stride_u * 2 == width &&
      src_stride_v * 2 == width &&
      dst_stride_uyvy == width * 2) {
    return I422ToUYVY(src_y, 0,
                      src_u, 0,
                      src_v, 0,
                      dst_uyvy, 0,
                      width * height, 1);
  }
357
  void (*I422ToUYVYRow)(const uint8* src_y, const uint8* src_u,
358
                        const uint8* src_v, uint8* dst_uyvy, int width) =
359 360
      I422ToUYVYRow_C;
#if defined(HAS_I422TOUYVYROW_SSE2)
361
  if (TestCpuFlag(kCpuHasSSE2) && width >= 16) {
362 363 364 365
    I422ToUYVYRow = I422ToUYVYRow_Any_SSE2;
    if (IS_ALIGNED(width, 16)) {
      I422ToUYVYRow = I422ToUYVYRow_SSE2;
    }
366
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
367
#elif defined(HAS_I422TOUYVYROW_NEON)
368 369 370 371 372
  if (TestCpuFlag(kCpuHasNEON) && width >= 16) {
    I422ToUYVYRow = I422ToUYVYRow_Any_NEON;
    if (IS_ALIGNED(width, 16)) {
      I422ToUYVYRow = I422ToUYVYRow_NEON;
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
373
  }
374 375 376
#endif

  for (int y = 0; y < height; ++y) {
377
    I422ToUYVYRow(src_y, src_u, src_v, dst_uyvy, width);
378 379 380
    src_y += src_stride_y;
    src_u += src_stride_u;
    src_v += src_stride_v;
381
    dst_uyvy += dst_stride_uyvy;
382 383 384 385
  }
  return 0;
}

386
LIBYUV_API
387 388 389
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,
390
               uint8* dst_uyvy, int dst_stride_uyvy,
391
               int width, int height) {
392
  if (!src_y || !src_u || !src_v || !dst_uyvy ||
393
      width <= 0 || height == 0) {
394 395 396 397 398
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
399 400
    dst_uyvy = dst_uyvy + (height - 1) * dst_stride_uyvy;
    dst_stride_uyvy = -dst_stride_uyvy;
401
  }
402
  void (*I422ToUYVYRow)(const uint8* src_y, const uint8* src_u,
403
                        const uint8* src_v, uint8* dst_uyvy, int width) =
404 405
      I422ToUYVYRow_C;
#if defined(HAS_I422TOUYVYROW_SSE2)
406
  if (TestCpuFlag(kCpuHasSSE2) && width >= 16) {
407 408 409 410
    I422ToUYVYRow = I422ToUYVYRow_Any_SSE2;
    if (IS_ALIGNED(width, 16)) {
      I422ToUYVYRow = I422ToUYVYRow_SSE2;
    }
411
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
412
#elif defined(HAS_I422TOUYVYROW_NEON)
413 414 415 416 417
  if (TestCpuFlag(kCpuHasNEON) && width >= 16) {
    I422ToUYVYRow = I422ToUYVYRow_Any_NEON;
    if (IS_ALIGNED(width, 16)) {
      I422ToUYVYRow = I422ToUYVYRow_NEON;
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
418
  }
419 420 421
#endif

  for (int y = 0; y < height - 1; y += 2) {
422
    I422ToUYVYRow(src_y, src_u, src_v, dst_uyvy, width);
423
    I422ToUYVYRow(src_y + src_stride_y, src_u, src_v,
424
                  dst_uyvy + dst_stride_uyvy, width);
425 426 427
    src_y += src_stride_y * 2;
    src_u += src_stride_u;
    src_v += src_stride_v;
428
    dst_uyvy += dst_stride_uyvy * 2;
429 430
  }
  if (height & 1) {
431
    I422ToUYVYRow(src_y, src_u, src_v, dst_uyvy, width);
432 433 434 435
  }
  return 0;
}

436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
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) {
  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;
    int halfheight = (height + 1) >> 1;
    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;
  }
456
  // Coalesce contiguous rows.
457
  int halfwidth = (width + 1) >> 1;
458 459
  int halfheight = (height + 1) >> 1;
  if (src_stride_y == width &&
460
      dst_stride_y == width) {
461 462
    width = width * height;
    height = 1;
463 464 465 466
  }
  if (src_stride_u * 2 == width &&
      src_stride_v * 2 == width &&
      dst_stride_uv == width) {
467 468 469
    halfwidth = halfwidth * halfheight;
    halfheight = 1;
  }
470
  void (*MergeUVRow_)(const uint8* src_u, const uint8* src_v, uint8* dst_uv,
471
                      int width) = MergeUVRow_C;
472
#if defined(HAS_MERGEUVROW_SSE2)
473
  if (TestCpuFlag(kCpuHasSSE2) && halfwidth >= 16) {
474
    MergeUVRow_ = MergeUVRow_Any_SSE2;
475
    if (IS_ALIGNED(halfwidth, 16)) {
476
      MergeUVRow_ = MergeUVRow_Unaligned_SSE2;
477 478 479
      if (IS_ALIGNED(src_u, 16) && IS_ALIGNED(src_stride_u, 16) &&
          IS_ALIGNED(src_v, 16) && IS_ALIGNED(src_stride_v, 16) &&
          IS_ALIGNED(dst_uv, 16) && IS_ALIGNED(dst_stride_uv, 16)) {
480
        MergeUVRow_ = MergeUVRow_SSE2;
481 482
      }
    }
483
  }
484
#endif
485
#if defined(HAS_MERGEUVROW_AVX2)
486
  if (TestCpuFlag(kCpuHasAVX2) && halfwidth >= 32) {
487
    MergeUVRow_ = MergeUVRow_Any_AVX2;
488
    if (IS_ALIGNED(halfwidth, 32)) {
489
      MergeUVRow_ = MergeUVRow_AVX2;
490
    }
491 492
  }
#endif
493
#if defined(HAS_MERGEUVROW_NEON)
494
  if (TestCpuFlag(kCpuHasNEON) && halfwidth >= 16) {
495
    MergeUVRow_ = MergeUVRow_Any_NEON;
496
    if (IS_ALIGNED(halfwidth, 16)) {
497
      MergeUVRow_ = MergeUVRow_NEON;
498 499 500 501
    }
  }
#endif

502 503
  CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  for (int y = 0; y < halfheight; ++y) {
504
    // Merge a row of U and V into a row of UV.
505
    MergeUVRow_(src_u, src_v, dst_uv, halfwidth);
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
    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);
}

528
// Convert I420 to ARGB.
529
LIBYUV_API
530 531 532 533 534
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) {
535 536 537 538
  if (!src_y || !src_u || !src_v || !dst_argb ||
      width <= 0 || height == 0) {
    return -1;
  }
539 540 541 542 543 544
  // 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;
  }
545
  void (*I422ToARGBRow)(const uint8* y_buf,
546 547 548
                        const uint8* u_buf,
                        const uint8* v_buf,
                        uint8* rgb_buf,
549
                        int width) = I422ToARGBRow_C;
550
#if defined(HAS_I422TOARGBROW_SSSE3)
551
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) {
552
    I422ToARGBRow = I422ToARGBRow_Any_SSSE3;
553
    if (IS_ALIGNED(width, 8)) {
554
      I422ToARGBRow = I422ToARGBRow_Unaligned_SSSE3;
555
      if (IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
556
        I422ToARGBRow = I422ToARGBRow_SSSE3;
557
      }
558 559
    }
  }
560 561 562 563 564 565 566 567 568 569
#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)
570 571 572 573 574 575
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    I422ToARGBRow = I422ToARGBRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToARGBRow = I422ToARGBRow_NEON;
    }
  }
576 577
#endif
#if defined(HAS_I422TOARGBROW_MIPS_DSPR2)
578 579 580 581 582 583 584
  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;
  }
585 586
#endif

587
  for (int y = 0; y < height; ++y) {
588
    I422ToARGBRow(src_y, src_u, src_v, dst_argb, width);
589 590 591 592 593 594 595 596 597 598 599
    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.
600
LIBYUV_API
601 602 603 604 605
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) {
606
  if (!src_y || !src_u || !src_v || !dst_bgra ||
607 608 609
      width <= 0 || height == 0) {
    return -1;
  }
610 611 612 613 614 615
  // 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;
  }
616
  void (*I422ToBGRARow)(const uint8* y_buf,
617 618 619
                        const uint8* u_buf,
                        const uint8* v_buf,
                        uint8* rgb_buf,
620
                        int width) = I422ToBGRARow_C;
621
#if defined(HAS_I422TOBGRAROW_SSSE3)
622
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) {
623
    I422ToBGRARow = I422ToBGRARow_Any_SSSE3;
624
    if (IS_ALIGNED(width, 8)) {
625
      I422ToBGRARow = I422ToBGRARow_Unaligned_SSSE3;
626
      if (IS_ALIGNED(dst_bgra, 16) && IS_ALIGNED(dst_stride_bgra, 16)) {
627
        I422ToBGRARow = I422ToBGRARow_SSSE3;
628
      }
629 630
    }
  }
631 632 633 634 635 636 637
#elif defined(HAS_I422TOBGRAROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    I422ToBGRARow = I422ToBGRARow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToBGRARow = I422ToBGRARow_NEON;
    }
  }
638 639 640 641 642 643 644 645
#elif defined(HAS_I422TOBGRAROW_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) &&
      IS_ALIGNED(dst_bgra, 4) && IS_ALIGNED(dst_stride_bgra, 4)) {
    I422ToBGRARow = I422ToBGRARow_MIPS_DSPR2;
  }
646 647
#endif

648
  for (int y = 0; y < height; ++y) {
649
    I422ToBGRARow(src_y, src_u, src_v, dst_bgra, width);
650 651 652 653 654 655 656 657 658 659 660
    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.
661
LIBYUV_API
662 663 664 665 666
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) {
667
  if (!src_y || !src_u || !src_v || !dst_abgr ||
668 669 670
      width <= 0 || height == 0) {
    return -1;
  }
671 672 673 674 675 676
  // 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;
  }
677
  void (*I422ToABGRRow)(const uint8* y_buf,
678 679 680
                        const uint8* u_buf,
                        const uint8* v_buf,
                        uint8* rgb_buf,
681
                        int width) = I422ToABGRRow_C;
682
#if defined(HAS_I422TOABGRROW_SSSE3)
683
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) {
684
    I422ToABGRRow = I422ToABGRRow_Any_SSSE3;
685
    if (IS_ALIGNED(width, 8)) {
686
      I422ToABGRRow = I422ToABGRRow_Unaligned_SSSE3;
687
      if (IS_ALIGNED(dst_abgr, 16) && IS_ALIGNED(dst_stride_abgr, 16)) {
688
        I422ToABGRRow = I422ToABGRRow_SSSE3;
689
      }
690 691
    }
  }
692 693 694 695 696 697 698
#elif defined(HAS_I422TOABGRROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    I422ToABGRRow = I422ToABGRRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToABGRRow = I422ToABGRRow_NEON;
    }
  }
699 700
#endif

701
  for (int y = 0; y < height; ++y) {
702
    I422ToABGRRow(src_y, src_u, src_v, dst_abgr, width);
703 704 705 706 707 708 709 710 711 712
    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;
}

713
// Convert I420 to RGBA.
714
LIBYUV_API
715 716 717 718 719
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) {
720
  if (!src_y || !src_u || !src_v || !dst_rgba ||
721 722 723 724 725 726 727 728 729 730 731 732 733 734
      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;
  }
  void (*I422ToRGBARow)(const uint8* y_buf,
                        const uint8* u_buf,
                        const uint8* v_buf,
                        uint8* rgb_buf,
                        int width) = I422ToRGBARow_C;
735
#if defined(HAS_I422TORGBAROW_SSSE3)
736 737 738 739 740 741 742 743 744
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) {
    I422ToRGBARow = I422ToRGBARow_Any_SSSE3;
    if (IS_ALIGNED(width, 8)) {
      I422ToRGBARow = I422ToRGBARow_Unaligned_SSSE3;
      if (IS_ALIGNED(dst_rgba, 16) && IS_ALIGNED(dst_stride_rgba, 16)) {
        I422ToRGBARow = I422ToRGBARow_SSSE3;
      }
    }
  }
745 746 747 748 749 750 751
#elif defined(HAS_I422TORGBAROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    I422ToRGBARow = I422ToRGBARow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToRGBARow = I422ToRGBARow_NEON;
    }
  }
752 753 754 755 756 757 758 759 760 761 762 763 764 765
#endif

  for (int y = 0; y < height; ++y) {
    I422ToRGBARow(src_y, src_u, src_v, dst_rgba, width);
    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;
}

766
// Convert I420 to RGB24.
767
LIBYUV_API
768 769 770
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,
771
                uint8* dst_rgb24, int dst_stride_rgb24,
772
                int width, int height) {
773
  if (!src_y || !src_u || !src_v || !dst_rgb24 ||
774 775 776
      width <= 0 || height == 0) {
    return -1;
  }
777 778 779
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
780 781 782 783
    dst_rgb24 = dst_rgb24 + (height - 1) * dst_stride_rgb24;
    dst_stride_rgb24 = -dst_stride_rgb24;
  }
  void (*I422ToRGB24Row)(const uint8* y_buf,
784 785 786 787 788
                        const uint8* u_buf,
                        const uint8* v_buf,
                        uint8* rgb_buf,
                        int width) = I422ToRGB24Row_C;
#if defined(HAS_I422TORGB24ROW_SSSE3)
789 790
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) {
    I422ToRGB24Row = I422ToRGB24Row_Any_SSSE3;
791
    if (IS_ALIGNED(width, 8)) {
792
      I422ToRGB24Row = I422ToRGB24Row_SSSE3;
793 794
    }
  }
795 796 797 798 799 800 801
#elif defined(HAS_I422TORGB24ROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    I422ToRGB24Row = I422ToRGB24Row_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToRGB24Row = I422ToRGB24Row_NEON;
    }
  }
802
#endif
803 804

  for (int y = 0; y < height; ++y) {
805 806
    I422ToRGB24Row(src_y, src_u, src_v, dst_rgb24, width);
    dst_rgb24 += dst_stride_rgb24;
807 808 809 810 811 812 813 814 815 816
    src_y += src_stride_y;
    if (y & 1) {
      src_u += src_stride_u;
      src_v += src_stride_v;
    }
  }
  return 0;
}

// Convert I420 to RAW.
817
LIBYUV_API
818
int I420ToRAW(const uint8* src_y, int src_stride_y,
819 820 821 822 823
                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) {
  if (!src_y || !src_u || !src_v || !dst_raw ||
824 825 826
      width <= 0 || height == 0) {
    return -1;
  }
827 828 829
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
830 831 832 833
    dst_raw = dst_raw + (height - 1) * dst_stride_raw;
    dst_stride_raw = -dst_stride_raw;
  }
  void (*I422ToRAWRow)(const uint8* y_buf,
834 835 836 837 838
                        const uint8* u_buf,
                        const uint8* v_buf,
                        uint8* rgb_buf,
                        int width) = I422ToRAWRow_C;
#if defined(HAS_I422TORAWROW_SSSE3)
839 840
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) {
    I422ToRAWRow = I422ToRAWRow_Any_SSSE3;
841
    if (IS_ALIGNED(width, 8)) {
842
      I422ToRAWRow = I422ToRAWRow_SSSE3;
843 844
    }
  }
845 846 847 848 849 850 851
#elif defined(HAS_I422TORAWROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    I422ToRAWRow = I422ToRAWRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToRAWRow = I422ToRAWRow_NEON;
    }
  }
852
#endif
853 854

  for (int y = 0; y < height; ++y) {
855 856
    I422ToRAWRow(src_y, src_u, src_v, dst_raw, width);
    dst_raw += dst_stride_raw;
857 858 859 860 861 862 863 864 865
    src_y += src_stride_y;
    if (y & 1) {
      src_u += src_stride_u;
      src_v += src_stride_v;
    }
  }
  return 0;
}

866
// Convert I420 to ARGB1555.
867
LIBYUV_API
868 869 870 871 872 873
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) {
  if (!src_y || !src_u || !src_v || !dst_argb1555 ||
874 875 876
      width <= 0 || height == 0) {
    return -1;
  }
877 878 879
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
880 881
    dst_argb1555 = dst_argb1555 + (height - 1) * dst_stride_argb1555;
    dst_stride_argb1555 = -dst_stride_argb1555;
882
  }
883
  void (*I422ToARGB1555Row)(const uint8* y_buf,
884 885 886
                          const uint8* u_buf,
                          const uint8* v_buf,
                          uint8* rgb_buf,
887 888
                          int width) = I422ToARGB1555Row_C;
#if defined(HAS_I422TOARGB1555ROW_SSSE3)
889
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8 && width <= kMaxStride * 4) {
890
    I422ToARGB1555Row = I422ToARGB1555Row_Any_SSSE3;
891
    if (IS_ALIGNED(width, 8)) {
892
      I422ToARGB1555Row = I422ToARGB1555Row_SSSE3;
893
    }
894
  }
895
#elif defined(HAS_I422TOARGB1555ROW_NEON)
896
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
897
    I422ToARGB1555Row = I422ToARGB1555Row_Any_NEON;
898
    if (IS_ALIGNED(width, 8)) {
899
      I422ToARGB1555Row = I422ToARGB1555Row_NEON;
900 901
    }
  }
902
#endif
903 904

  for (int y = 0; y < height; ++y) {
905 906
    I422ToARGB1555Row(src_y, src_u, src_v, dst_argb1555, width);
    dst_argb1555 += dst_stride_argb1555;
907 908 909 910 911 912 913 914 915
    src_y += src_stride_y;
    if (y & 1) {
      src_u += src_stride_u;
      src_v += src_stride_v;
    }
  }
  return 0;
}

916 917

// Convert I420 to ARGB4444.
918
LIBYUV_API
919
int I420ToARGB4444(const uint8* src_y, int src_stride_y,
920 921
                   const uint8* src_u, int src_stride_u,
                   const uint8* src_v, int src_stride_v,
922
                   uint8* dst_argb4444, int dst_stride_argb4444,
923
                   int width, int height) {
924
  if (!src_y || !src_u || !src_v || !dst_argb4444 ||
925 926 927
      width <= 0 || height == 0) {
    return -1;
  }
928 929 930
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
931 932
    dst_argb4444 = dst_argb4444 + (height - 1) * dst_stride_argb4444;
    dst_stride_argb4444 = -dst_stride_argb4444;
933
  }
934 935 936 937 938 939
  void (*I422ToARGB4444Row)(const uint8* y_buf,
                          const uint8* u_buf,
                          const uint8* v_buf,
                          uint8* rgb_buf,
                          int width) = I422ToARGB4444Row_C;
#if defined(HAS_I422TOARGB4444ROW_SSSE3)
940
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8 && width <= kMaxStride * 4) {
941
    I422ToARGB4444Row = I422ToARGB4444Row_Any_SSSE3;
942
    if (IS_ALIGNED(width, 8)) {
943
      I422ToARGB4444Row = I422ToARGB4444Row_SSSE3;
944
    }
945
  }
946 947 948 949 950
#elif defined(HAS_I422TOARGB4444ROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    I422ToARGB4444Row = I422ToARGB4444Row_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToARGB4444Row = I422ToARGB4444Row_NEON;
951 952
    }
  }
953
#endif
954 955

  for (int y = 0; y < height; ++y) {
956 957
    I422ToARGB4444Row(src_y, src_u, src_v, dst_argb4444, width);
    dst_argb4444 += dst_stride_argb4444;
958 959 960 961 962 963 964 965 966
    src_y += src_stride_y;
    if (y & 1) {
      src_u += src_stride_u;
      src_v += src_stride_v;
    }
  }
  return 0;
}

967
// Convert I420 to RGB565.
968
LIBYUV_API
969 970 971 972 973 974
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) {
  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 985 986 987 988 989
  void (*I422ToRGB565Row)(const uint8* y_buf,
                          const uint8* u_buf,
                          const uint8* v_buf,
                          uint8* rgb_buf,
                          int width) = I422ToRGB565Row_C;
#if defined(HAS_I422TORGB565ROW_SSSE3)
990 991 992 993 994
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8
#if defined(__x86_64__) || defined(__i386__)
      && width <= kMaxStride * 4
#endif
      ) {
995
    I422ToRGB565Row = I422ToRGB565Row_Any_SSSE3;
996
    if (IS_ALIGNED(width, 8)) {
997
      I422ToRGB565Row = I422ToRGB565Row_SSSE3;
998
    }
999
  }
1000 1001 1002 1003 1004
#elif defined(HAS_I422TORGB565ROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    I422ToRGB565Row = I422ToRGB565Row_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToRGB565Row = I422ToRGB565Row_NEON;
1005 1006
    }
  }
1007
#endif
1008 1009

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

// Convert I420 to specified format
1022
LIBYUV_API
1023 1024 1025 1026 1027
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,
1028 1029
                    uint32 fourcc) {
  uint32 format = CanonicalFourCC(fourcc);
1030 1031
  if (!y || !u|| !v || !dst_sample ||
      width <= 0 || height == 0) {
1032 1033
    return -1;
  }
1034
  int r = 0;
1035 1036 1037
  switch (format) {
    // Single plane formats
    case FOURCC_YUY2:
1038 1039 1040 1041 1042 1043
      r = I420ToYUY2(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample,
                     dst_sample_stride ? dst_sample_stride : width * 2,
                     width, height);
1044 1045
      break;
    case FOURCC_UYVY:
1046
      r = I420ToUYVY(y, y_stride,
1047 1048 1049 1050 1051 1052
                     u, u_stride,
                     v, v_stride,
                     dst_sample,
                     dst_sample_stride ? dst_sample_stride : width * 2,
                     width, height);
      break;
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
    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;
1077
    case FOURCC_24BG:
1078 1079 1080 1081 1082 1083
      r = I420ToRGB24(y, y_stride,
                      u, u_stride,
                      v, v_stride,
                      dst_sample,
                      dst_sample_stride ? dst_sample_stride : width * 3,
                      width, height);
1084 1085
      break;
    case FOURCC_RAW:
1086 1087 1088 1089 1090 1091
      r = I420ToRAW(y, y_stride,
                    u, u_stride,
                    v, v_stride,
                    dst_sample,
                    dst_sample_stride ? dst_sample_stride : width * 3,
                    width, height);
1092 1093
      break;
    case FOURCC_ARGB:
1094 1095 1096 1097 1098 1099
      r = I420ToARGB(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample,
                     dst_sample_stride ? dst_sample_stride : width * 4,
                     width, height);
1100 1101
      break;
    case FOURCC_BGRA:
1102 1103 1104 1105 1106 1107
      r = I420ToBGRA(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample,
                     dst_sample_stride ? dst_sample_stride : width * 4,
                     width, height);
1108 1109
      break;
    case FOURCC_ABGR:
1110 1111 1112 1113 1114 1115
      r = I420ToABGR(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample,
                     dst_sample_stride ? dst_sample_stride : width * 4,
                     width, height);
1116 1117 1118 1119 1120 1121 1122 1123
      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);
1124 1125
      break;
    case FOURCC_BGGR:
1126 1127 1128 1129 1130 1131
      r = I420ToBayerBGGR(y, y_stride,
                          u, u_stride,
                          v, v_stride,
                          dst_sample,
                          dst_sample_stride ? dst_sample_stride : width,
                          width, height);
1132 1133
      break;
    case FOURCC_GBRG:
1134 1135 1136 1137 1138 1139
      r = I420ToBayerGBRG(y, y_stride,
                          u, u_stride,
                          v, v_stride,
                          dst_sample,
                          dst_sample_stride ? dst_sample_stride : width,
                          width, height);
1140 1141
      break;
    case FOURCC_GRBG:
1142 1143 1144 1145 1146 1147
      r = I420ToBayerGRBG(y, y_stride,
                          u, u_stride,
                          v, v_stride,
                          dst_sample,
                          dst_sample_stride ? dst_sample_stride : width,
                          width, height);
1148 1149
      break;
    case FOURCC_RGGB:
1150 1151 1152 1153 1154 1155
      r = I420ToBayerRGGB(y, y_stride,
                          u, u_stride,
                          v, v_stride,
                          dst_sample,
                          dst_sample_stride ? dst_sample_stride : width,
                          width, height);
1156 1157
      break;
    case FOURCC_I400:
1158 1159 1160 1161
      r = I400Copy(y, y_stride,
                   dst_sample,
                   dst_sample_stride ? dst_sample_stride : width,
                   width, height);
1162
      break;
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
    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;
    }
    // TODO(fbarchard): Add M420 and Q420.
1188 1189 1190
    // Triplanar formats
    // TODO(fbarchard): halfstride instead of halfwidth
    case FOURCC_I420:
1191
    case FOURCC_YU12:
1192 1193 1194 1195 1196
    case FOURCC_YV12: {
      int halfwidth = (width + 1) / 2;
      int halfheight = (height + 1) / 2;
      uint8* dst_u;
      uint8* dst_v;
1197
      if (format == FOURCC_YV12) {
1198 1199
        dst_v = dst_sample + width * height;
        dst_u = dst_v + halfwidth * halfheight;
1200 1201 1202
      } else {
        dst_u = dst_sample + width * height;
        dst_v = dst_u + halfwidth * halfheight;
1203
      }
1204 1205 1206 1207 1208 1209 1210
      r = I420Copy(y, y_stride,
                   u, u_stride,
                   v, v_stride,
                   dst_sample, width,
                   dst_u, halfwidth,
                   dst_v, halfwidth,
                   width, height);
1211 1212 1213 1214 1215 1216 1217
      break;
    }
    case FOURCC_I422:
    case FOURCC_YV16: {
      int halfwidth = (width + 1) / 2;
      uint8* dst_u;
      uint8* dst_v;
1218
      if (format == FOURCC_YV16) {
1219 1220
        dst_v = dst_sample + width * height;
        dst_u = dst_v + halfwidth * height;
1221 1222 1223
      } else {
        dst_u = dst_sample + width * height;
        dst_v = dst_u + halfwidth * height;
1224
      }
1225 1226 1227 1228 1229 1230 1231
      r = I420ToI422(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample, width,
                     dst_u, halfwidth,
                     dst_v, halfwidth,
                     width, height);
1232 1233 1234 1235 1236 1237
      break;
    }
    case FOURCC_I444:
    case FOURCC_YV24: {
      uint8* dst_u;
      uint8* dst_v;
1238
      if (format == FOURCC_YV24) {
1239 1240
        dst_v = dst_sample + width * height;
        dst_u = dst_v + width * height;
1241 1242 1243
      } else {
        dst_u = dst_sample + width * height;
        dst_v = dst_u + width * height;
1244
      }
1245 1246 1247 1248 1249 1250 1251
      r = I420ToI444(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample, width,
                     dst_u, width,
                     dst_v, width,
                     width, height);
1252 1253
      break;
    }
1254 1255 1256 1257
    case FOURCC_I411: {
      int quarterwidth = (width + 3) / 4;
      uint8* dst_u = dst_sample + width * height;
      uint8* dst_v = dst_u + quarterwidth * height;
1258 1259 1260 1261 1262 1263 1264
      r = I420ToI411(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample, width,
                     dst_u, quarterwidth,
                     dst_v, quarterwidth,
                     width, height);
1265 1266
      break;
    }
1267 1268 1269 1270 1271

    // Formats not supported - MJPG, biplanar, some rgb formats.
    default:
      return -1;  // unknown fourcc - return failure code.
  }
1272
  return r;
1273 1274 1275 1276 1277 1278
}

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