convert_from.cc 39.6 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
  // Coalesce rows.
241 242 243 244
  if (src_stride_y == width &&
      src_stride_u * 2 == width &&
      src_stride_v * 2 == width &&
      dst_stride_yuy2 == width * 2) {
245 246 247
    width *= height;
    height = 1;
    src_stride_y = src_stride_u = src_stride_v = dst_stride_yuy2 = 0;
248
  }
249
  void (*I422ToYUY2Row)(const uint8* src_y, const uint8* src_u,
250
                        const uint8* src_v, uint8* dst_yuy2, int width) =
251 252
      I422ToYUY2Row_C;
#if defined(HAS_I422TOYUY2ROW_SSE2)
253
  if (TestCpuFlag(kCpuHasSSE2) && width >= 16) {
254 255 256 257
    I422ToYUY2Row = I422ToYUY2Row_Any_SSE2;
    if (IS_ALIGNED(width, 16)) {
      I422ToYUY2Row = I422ToYUY2Row_SSE2;
    }
258
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
259
#elif defined(HAS_I422TOYUY2ROW_NEON)
260 261 262 263 264
  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
265
  }
266 267 268
#endif

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

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

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

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

  for (int y = 0; y < height; ++y) {
373
    I422ToUYVYRow(src_y, src_u, src_v, dst_uyvy, width);
374 375 376
    src_y += src_stride_y;
    src_u += src_stride_u;
    src_v += src_stride_v;
377
    dst_uyvy += dst_stride_uyvy;
378 379 380 381
  }
  return 0;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

915 916

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

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

966
// Convert I420 to RGB565.
967
LIBYUV_API
968 969 970 971 972 973
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 ||
974 975 976
      width <= 0 || height == 0) {
    return -1;
  }
977 978 979
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
980 981
    dst_rgb565 = dst_rgb565 + (height - 1) * dst_stride_rgb565;
    dst_stride_rgb565 = -dst_stride_rgb565;
982
  }
983 984 985 986 987 988
  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)
989 990
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8
#if defined(__x86_64__) || defined(__i386__)
991
      && width * 4 <= kMaxStride
992 993
#endif
      ) {
994
    I422ToRGB565Row = I422ToRGB565Row_Any_SSSE3;
995
    if (IS_ALIGNED(width, 8)) {
996
      I422ToRGB565Row = I422ToRGB565Row_SSSE3;
997
    }
998
  }
999 1000 1001 1002 1003
#elif defined(HAS_I422TORGB565ROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    I422ToRGB565Row = I422ToRGB565Row_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToRGB565Row = I422ToRGB565Row_NEON;
1004 1005
    }
  }
1006
#endif
1007 1008

  for (int y = 0; y < height; ++y) {
1009 1010
    I422ToRGB565Row(src_y, src_u, src_v, dst_rgb565, width);
    dst_rgb565 += dst_stride_rgb565;
1011 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;
}

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

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

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