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

#include "libyuv/convert_from.h"

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

LIBYUV_API
int I420ToNV21(const uint8* src_y, int src_stride_y,
               const uint8* src_u, int src_stride_u,
               const uint8* src_v, int src_stride_v,
               uint8* dst_y, int dst_stride_y,
               uint8* dst_vu, int dst_stride_vu,
               int width, int height) {
  return I420ToNV12(src_y, src_stride_y,
                    src_v, src_stride_v,
                    src_u, src_stride_u,
                    dst_y, src_stride_y,
                    dst_vu, dst_stride_vu,
                    width, height);
}

453 454 455 456 457 458 459
// Convert I422 to RGBA with matrix
static int I420ToRGBAMatrix(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,
                            const struct YuvConstants* yuvconstants,
                            int width, int height) {
460 461 462 463 464
  int y;
  void (*I422ToRGBARow)(const uint8* y_buf,
                        const uint8* u_buf,
                        const uint8* v_buf,
                        uint8* rgb_buf,
465
                        const struct YuvConstants* yuvconstants,
466
                        int width) = I422ToRGBARow_C;
467
  if (!src_y || !src_u || !src_v || !dst_rgba ||
468 469 470 471 472 473 474 475 476
      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;
  }
477
#if defined(HAS_I422TORGBAROW_SSSE3)
478
  if (TestCpuFlag(kCpuHasSSSE3)) {
479 480
    I422ToRGBARow = I422ToRGBARow_Any_SSSE3;
    if (IS_ALIGNED(width, 8)) {
481
      I422ToRGBARow = I422ToRGBARow_SSSE3;
482 483
    }
  }
484
#endif
485
#if defined(HAS_I422TORGBAROW_AVX2)
486
  if (TestCpuFlag(kCpuHasAVX2)) {
487 488 489 490 491 492
    I422ToRGBARow = I422ToRGBARow_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      I422ToRGBARow = I422ToRGBARow_AVX2;
    }
  }
#endif
493
#if defined(HAS_I422TORGBAROW_NEON)
494
  if (TestCpuFlag(kCpuHasNEON)) {
495 496 497 498 499
    I422ToRGBARow = I422ToRGBARow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToRGBARow = I422ToRGBARow_NEON;
    }
  }
500
#endif
501 502 503 504 505 506 507 508 509
#if defined(HAS_I422TORGBAROW_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_rgba, 4) && IS_ALIGNED(dst_stride_rgba, 4)) {
    I422ToRGBARow = I422ToRGBARow_MIPS_DSPR2;
  }
#endif
510

511
  for (y = 0; y < height; ++y) {
512
    I422ToRGBARow(src_y, src_u, src_v, dst_rgba, yuvconstants, width);
513 514 515 516 517 518 519 520 521 522
    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;
}

523
// Convert I420 to RGBA.
524
LIBYUV_API
525 526 527 528 529 530 531 532 533
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) {
  return I420ToRGBAMatrix(src_y, src_stride_y,
                          src_u, src_stride_u,
                          src_v, src_stride_v,
                          dst_rgba, dst_stride_rgba,
534
                          &kYuvI601Constants,
535 536 537 538 539 540 541 542 543 544 545 546 547 548
                          width, height);
}

// Convert I420 to BGRA.
LIBYUV_API
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) {
  return I420ToRGBAMatrix(src_y, src_stride_y,
                          src_v, src_stride_v,  // Swap U and V
                          src_u, src_stride_u,
                          dst_bgra, dst_stride_bgra,
549
                          &kYvuI601Constants,  // Use Yvu matrix
550 551 552 553 554 555 556 557 558 559
                          width, height);
}

// Convert I420 to RGB24 with matrix
static int I420ToRGB24Matrix(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_rgb24, int dst_stride_rgb24,
                             const struct YuvConstants* yuvconstants,
                             int width, int height) {
560 561 562 563 564
  int y;
  void (*I422ToRGB24Row)(const uint8* y_buf,
                         const uint8* u_buf,
                         const uint8* v_buf,
                         uint8* rgb_buf,
565
                         const struct YuvConstants* yuvconstants,
566
                         int width) = I422ToRGB24Row_C;
567
  if (!src_y || !src_u || !src_v || !dst_rgb24 ||
568 569 570
      width <= 0 || height == 0) {
    return -1;
  }
571 572 573
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
574 575 576
    dst_rgb24 = dst_rgb24 + (height - 1) * dst_stride_rgb24;
    dst_stride_rgb24 = -dst_stride_rgb24;
  }
577
#if defined(HAS_I422TORGB24ROW_SSSE3)
578
  if (TestCpuFlag(kCpuHasSSSE3)) {
579
    I422ToRGB24Row = I422ToRGB24Row_Any_SSSE3;
580
    if (IS_ALIGNED(width, 8)) {
581
      I422ToRGB24Row = I422ToRGB24Row_SSSE3;
582 583
    }
  }
584
#endif
585 586 587 588 589 590 591 592
#if defined(HAS_I422TORGB24ROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2)) {
    I422ToRGB24Row = I422ToRGB24Row_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      I422ToRGB24Row = I422ToRGB24Row_AVX2;
    }
  }
#endif
593
#if defined(HAS_I422TORGB24ROW_NEON)
594
  if (TestCpuFlag(kCpuHasNEON)) {
595 596 597 598 599
    I422ToRGB24Row = I422ToRGB24Row_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToRGB24Row = I422ToRGB24Row_NEON;
    }
  }
600
#endif
601

602
  for (y = 0; y < height; ++y) {
603
    I422ToRGB24Row(src_y, src_u, src_v, dst_rgb24, yuvconstants, width);
604
    dst_rgb24 += dst_stride_rgb24;
605 606 607 608 609 610 611 612 613
    src_y += src_stride_y;
    if (y & 1) {
      src_u += src_stride_u;
      src_v += src_stride_v;
    }
  }
  return 0;
}

614
// Convert I420 to RGB24.
615
LIBYUV_API
616
int I420ToRGB24(const uint8* src_y, int src_stride_y,
617 618
                const uint8* src_u, int src_stride_u,
                const uint8* src_v, int src_stride_v,
619
                uint8* dst_rgb24, int dst_stride_rgb24,
620
                int width, int height) {
621 622 623 624
  return I420ToRGB24Matrix(src_y, src_stride_y,
                           src_u, src_stride_u,
                           src_v, src_stride_v,
                           dst_rgb24, dst_stride_rgb24,
625
                           &kYuvI601Constants,
626 627
                           width, height);
}
628

629 630 631 632 633 634 635 636 637 638 639
// Convert I420 to RAW.
LIBYUV_API
int I420ToRAW(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_raw, int dst_stride_raw,
              int width, int height) {
  return I420ToRGB24Matrix(src_y, src_stride_y,
                           src_v, src_stride_v,  // Swap U and V
                           src_u, src_stride_u,
                           dst_raw, dst_stride_raw,
640
                           &kYvuI601Constants,  // Use Yvu matrix
641
                           width, height);
642 643
}

644
// Convert I420 to ARGB1555.
645
LIBYUV_API
646 647 648 649 650
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) {
651 652 653 654 655
  int y;
  void (*I422ToARGB1555Row)(const uint8* y_buf,
                            const uint8* u_buf,
                            const uint8* v_buf,
                            uint8* rgb_buf,
656
                            const struct YuvConstants* yuvconstants,
657
                            int width) = I422ToARGB1555Row_C;
658
  if (!src_y || !src_u || !src_v || !dst_argb1555 ||
659 660 661
      width <= 0 || height == 0) {
    return -1;
  }
662 663 664
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
665 666
    dst_argb1555 = dst_argb1555 + (height - 1) * dst_stride_argb1555;
    dst_stride_argb1555 = -dst_stride_argb1555;
667
  }
668
#if defined(HAS_I422TOARGB1555ROW_SSSE3)
669
  if (TestCpuFlag(kCpuHasSSSE3)) {
670
    I422ToARGB1555Row = I422ToARGB1555Row_Any_SSSE3;
671
    if (IS_ALIGNED(width, 8)) {
672
      I422ToARGB1555Row = I422ToARGB1555Row_SSSE3;
673
    }
674
  }
675
#endif
676 677 678 679 680 681 682 683
#if defined(HAS_I422TOARGB1555ROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2)) {
    I422ToARGB1555Row = I422ToARGB1555Row_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      I422ToARGB1555Row = I422ToARGB1555Row_AVX2;
    }
  }
#endif
684
#if defined(HAS_I422TOARGB1555ROW_NEON)
685
  if (TestCpuFlag(kCpuHasNEON)) {
686
    I422ToARGB1555Row = I422ToARGB1555Row_Any_NEON;
687
    if (IS_ALIGNED(width, 8)) {
688
      I422ToARGB1555Row = I422ToARGB1555Row_NEON;
689 690
    }
  }
691
#endif
692

693
  for (y = 0; y < height; ++y) {
694
    I422ToARGB1555Row(src_y, src_u, src_v, dst_argb1555, &kYuvI601Constants,
695
                      width);
696
    dst_argb1555 += dst_stride_argb1555;
697 698 699 700 701 702 703 704 705
    src_y += src_stride_y;
    if (y & 1) {
      src_u += src_stride_u;
      src_v += src_stride_v;
    }
  }
  return 0;
}

706 707

// Convert I420 to ARGB4444.
708
LIBYUV_API
709
int I420ToARGB4444(const uint8* src_y, int src_stride_y,
710 711
                   const uint8* src_u, int src_stride_u,
                   const uint8* src_v, int src_stride_v,
712
                   uint8* dst_argb4444, int dst_stride_argb4444,
713
                   int width, int height) {
714 715 716 717 718
  int y;
  void (*I422ToARGB4444Row)(const uint8* y_buf,
                            const uint8* u_buf,
                            const uint8* v_buf,
                            uint8* rgb_buf,
719
                            const struct YuvConstants* yuvconstants,
720
                            int width) = I422ToARGB4444Row_C;
721
  if (!src_y || !src_u || !src_v || !dst_argb4444 ||
722 723 724
      width <= 0 || height == 0) {
    return -1;
  }
725 726 727
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
728 729
    dst_argb4444 = dst_argb4444 + (height - 1) * dst_stride_argb4444;
    dst_stride_argb4444 = -dst_stride_argb4444;
730
  }
731
#if defined(HAS_I422TOARGB4444ROW_SSSE3)
732
  if (TestCpuFlag(kCpuHasSSSE3)) {
733
    I422ToARGB4444Row = I422ToARGB4444Row_Any_SSSE3;
734
    if (IS_ALIGNED(width, 8)) {
735
      I422ToARGB4444Row = I422ToARGB4444Row_SSSE3;
736
    }
737
  }
738
#endif
739 740 741 742 743 744 745 746
#if defined(HAS_I422TOARGB4444ROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2)) {
    I422ToARGB4444Row = I422ToARGB4444Row_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      I422ToARGB4444Row = I422ToARGB4444Row_AVX2;
    }
  }
#endif
747
#if defined(HAS_I422TOARGB4444ROW_NEON)
748
  if (TestCpuFlag(kCpuHasNEON)) {
749 750 751
    I422ToARGB4444Row = I422ToARGB4444Row_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToARGB4444Row = I422ToARGB4444Row_NEON;
752 753
    }
  }
754
#endif
755

756
  for (y = 0; y < height; ++y) {
757
    I422ToARGB4444Row(src_y, src_u, src_v, dst_argb4444, &kYuvI601Constants,
758
                      width);
759
    dst_argb4444 += dst_stride_argb4444;
760 761 762 763 764 765 766 767 768
    src_y += src_stride_y;
    if (y & 1) {
      src_u += src_stride_u;
      src_v += src_stride_v;
    }
  }
  return 0;
}

769
// Convert I420 to RGB565.
770
LIBYUV_API
771 772 773 774 775
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) {
776 777 778 779 780
  int y;
  void (*I422ToRGB565Row)(const uint8* y_buf,
                          const uint8* u_buf,
                          const uint8* v_buf,
                          uint8* rgb_buf,
781
                          const struct YuvConstants* yuvconstants,
782
                          int width) = I422ToRGB565Row_C;
783
  if (!src_y || !src_u || !src_v || !dst_rgb565 ||
784 785 786
      width <= 0 || height == 0) {
    return -1;
  }
787 788 789
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
790 791
    dst_rgb565 = dst_rgb565 + (height - 1) * dst_stride_rgb565;
    dst_stride_rgb565 = -dst_stride_rgb565;
792
  }
793
#if defined(HAS_I422TORGB565ROW_SSSE3)
794
  if (TestCpuFlag(kCpuHasSSSE3)) {
795
    I422ToRGB565Row = I422ToRGB565Row_Any_SSSE3;
796
    if (IS_ALIGNED(width, 8)) {
797
      I422ToRGB565Row = I422ToRGB565Row_SSSE3;
798
    }
799
  }
800
#endif
801 802 803 804 805 806 807 808
#if defined(HAS_I422TORGB565ROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2)) {
    I422ToRGB565Row = I422ToRGB565Row_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      I422ToRGB565Row = I422ToRGB565Row_AVX2;
    }
  }
#endif
809
#if defined(HAS_I422TORGB565ROW_NEON)
810
  if (TestCpuFlag(kCpuHasNEON)) {
811 812 813
    I422ToRGB565Row = I422ToRGB565Row_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToRGB565Row = I422ToRGB565Row_NEON;
814 815
    }
  }
816
#endif
817

818
  for (y = 0; y < height; ++y) {
819
    I422ToRGB565Row(src_y, src_u, src_v, dst_rgb565, &kYuvI601Constants, width);
820
    dst_rgb565 += dst_stride_rgb565;
821 822 823 824 825 826 827 828 829
    src_y += src_stride_y;
    if (y & 1) {
      src_u += src_stride_u;
      src_v += src_stride_v;
    }
  }
  return 0;
}

830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
// Ordered 8x8 dither for 888 to 565.  Values from 0 to 7.
static const uint8 kDither565_4x4[16] = {
  0, 4, 1, 5,
  6, 2, 7, 3,
  1, 5, 0, 4,
  7, 3, 6, 2,
};

// Convert I420 to RGB565 with dithering.
LIBYUV_API
int I420ToRGB565Dither(const uint8* src_y, int src_stride_y,
                       const uint8* src_u, int src_stride_u,
                       const uint8* src_v, int src_stride_v,
                       uint8* dst_rgb565, int dst_stride_rgb565,
                       const uint8* dither4x4, int width, int height) {
  int y;
  void (*I422ToARGBRow)(const uint8* y_buf,
                        const uint8* u_buf,
                        const uint8* v_buf,
                        uint8* rgb_buf,
850
                        const struct YuvConstants* yuvconstants,
851 852
                        int width) = I422ToARGBRow_C;
  void (*ARGBToRGB565DitherRow)(const uint8* src_argb, uint8* dst_rgb,
853
      const uint32 dither4, int width) = ARGBToRGB565DitherRow_C;
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
  if (!src_y || !src_u || !src_v || !dst_rgb565 ||
      width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_rgb565 = dst_rgb565 + (height - 1) * dst_stride_rgb565;
    dst_stride_rgb565 = -dst_stride_rgb565;
  }
  if (!dither4x4) {
    dither4x4 = kDither565_4x4;
  }
#if defined(HAS_I422TOARGBROW_SSSE3)
  if (TestCpuFlag(kCpuHasSSSE3)) {
    I422ToARGBRow = I422ToARGBRow_Any_SSSE3;
    if (IS_ALIGNED(width, 8)) {
      I422ToARGBRow = I422ToARGBRow_SSSE3;
    }
  }
#endif
#if defined(HAS_I422TOARGBROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2)) {
    I422ToARGBRow = I422ToARGBRow_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      I422ToARGBRow = I422ToARGBRow_AVX2;
    }
  }
#endif
#if defined(HAS_I422TOARGBROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    I422ToARGBRow = I422ToARGBRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToARGBRow = I422ToARGBRow_NEON;
    }
  }
#endif
#if defined(HAS_I422TOARGBROW_MIPS_DSPR2)
  if (TestCpuFlag(kCpuHasMIPS_DSPR2) && IS_ALIGNED(width, 4) &&
      IS_ALIGNED(src_y, 4) && IS_ALIGNED(src_stride_y, 4) &&
      IS_ALIGNED(src_u, 2) && IS_ALIGNED(src_stride_u, 2) &&
      IS_ALIGNED(src_v, 2) && IS_ALIGNED(src_stride_v, 2)) {
    I422ToARGBRow = I422ToARGBRow_MIPS_DSPR2;
  }
#endif
#if defined(HAS_ARGBTORGB565DITHERROW_SSE2)
  if (TestCpuFlag(kCpuHasSSE2)) {
    ARGBToRGB565DitherRow = ARGBToRGB565DitherRow_Any_SSE2;
    if (IS_ALIGNED(width, 4)) {
      ARGBToRGB565DitherRow = ARGBToRGB565DitherRow_SSE2;
    }
  }
#endif
#if defined(HAS_ARGBTORGB565DITHERROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2)) {
    ARGBToRGB565DitherRow = ARGBToRGB565DitherRow_Any_AVX2;
    if (IS_ALIGNED(width, 8)) {
      ARGBToRGB565DitherRow = ARGBToRGB565DitherRow_AVX2;
    }
  }
914 915 916 917 918 919 920 921
#endif
#if defined(HAS_ARGBTORGB565DITHERROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    ARGBToRGB565DitherRow = ARGBToRGB565DitherRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      ARGBToRGB565DitherRow = ARGBToRGB565DitherRow_NEON;
    }
  }
922 923 924 925 926
#endif
  {
    // Allocate a row of argb.
    align_buffer_64(row_argb, width * 4);
    for (y = 0; y < height; ++y) {
927
      I422ToARGBRow(src_y, src_u, src_v, row_argb, &kYuvI601Constants, width);
928 929 930 931 932 933 934 935 936 937 938 939 940 941
      ARGBToRGB565DitherRow(row_argb, dst_rgb565,
                            *(uint32*)(dither4x4 + ((y & 3) << 2)), width);
      dst_rgb565 += dst_stride_rgb565;
      src_y += src_stride_y;
      if (y & 1) {
        src_u += src_stride_u;
        src_v += src_stride_v;
      }
    }
    free_aligned_buffer_64(row_argb);
  }
  return 0;
}

942
// Convert I420 to specified format
943
LIBYUV_API
944 945 946 947 948
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,
949 950
                    uint32 fourcc) {
  uint32 format = CanonicalFourCC(fourcc);
951
  int r = 0;
952 953
  if (!y || !u|| !v || !dst_sample ||
      width <= 0 || height == 0) {
954 955 956 957 958
    return -1;
  }
  switch (format) {
    // Single plane formats
    case FOURCC_YUY2:
959 960 961 962 963 964
      r = I420ToYUY2(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample,
                     dst_sample_stride ? dst_sample_stride : width * 2,
                     width, height);
965 966
      break;
    case FOURCC_UYVY:
967
      r = I420ToUYVY(y, y_stride,
968 969 970 971 972 973
                     u, u_stride,
                     v, v_stride,
                     dst_sample,
                     dst_sample_stride ? dst_sample_stride : width * 2,
                     width, height);
      break;
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
    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;
998
    case FOURCC_24BG:
999 1000 1001 1002 1003 1004
      r = I420ToRGB24(y, y_stride,
                      u, u_stride,
                      v, v_stride,
                      dst_sample,
                      dst_sample_stride ? dst_sample_stride : width * 3,
                      width, height);
1005 1006
      break;
    case FOURCC_RAW:
1007 1008 1009 1010 1011 1012
      r = I420ToRAW(y, y_stride,
                    u, u_stride,
                    v, v_stride,
                    dst_sample,
                    dst_sample_stride ? dst_sample_stride : width * 3,
                    width, height);
1013 1014
      break;
    case FOURCC_ARGB:
1015 1016 1017 1018 1019 1020
      r = I420ToARGB(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample,
                     dst_sample_stride ? dst_sample_stride : width * 4,
                     width, height);
1021 1022
      break;
    case FOURCC_BGRA:
1023 1024 1025 1026 1027 1028
      r = I420ToBGRA(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample,
                     dst_sample_stride ? dst_sample_stride : width * 4,
                     width, height);
1029 1030
      break;
    case FOURCC_ABGR:
1031 1032 1033 1034 1035 1036
      r = I420ToABGR(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample,
                     dst_sample_stride ? dst_sample_stride : width * 4,
                     width, height);
1037 1038 1039 1040 1041 1042 1043 1044
      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);
1045 1046
      break;
    case FOURCC_I400:
1047 1048 1049 1050
      r = I400Copy(y, y_stride,
                   dst_sample,
                   dst_sample_stride ? dst_sample_stride : width,
                   width, height);
1051
      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_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;
    }
1076
    // TODO(fbarchard): Add M420.
1077 1078 1079
    // Triplanar formats
    // TODO(fbarchard): halfstride instead of halfwidth
    case FOURCC_I420:
1080
    case FOURCC_YU12:
1081 1082 1083 1084 1085
    case FOURCC_YV12: {
      int halfwidth = (width + 1) / 2;
      int halfheight = (height + 1) / 2;
      uint8* dst_u;
      uint8* dst_v;
1086
      if (format == FOURCC_YV12) {
1087 1088
        dst_v = dst_sample + width * height;
        dst_u = dst_v + halfwidth * halfheight;
1089 1090 1091
      } else {
        dst_u = dst_sample + width * height;
        dst_v = dst_u + halfwidth * halfheight;
1092
      }
1093 1094 1095 1096 1097 1098 1099
      r = I420Copy(y, y_stride,
                   u, u_stride,
                   v, v_stride,
                   dst_sample, width,
                   dst_u, halfwidth,
                   dst_v, halfwidth,
                   width, height);
1100 1101 1102 1103 1104 1105 1106
      break;
    }
    case FOURCC_I422:
    case FOURCC_YV16: {
      int halfwidth = (width + 1) / 2;
      uint8* dst_u;
      uint8* dst_v;
1107
      if (format == FOURCC_YV16) {
1108 1109
        dst_v = dst_sample + width * height;
        dst_u = dst_v + halfwidth * height;
1110 1111 1112
      } else {
        dst_u = dst_sample + width * height;
        dst_v = dst_u + halfwidth * height;
1113
      }
1114 1115 1116 1117 1118 1119 1120
      r = I420ToI422(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample, width,
                     dst_u, halfwidth,
                     dst_v, halfwidth,
                     width, height);
1121 1122 1123 1124 1125 1126
      break;
    }
    case FOURCC_I444:
    case FOURCC_YV24: {
      uint8* dst_u;
      uint8* dst_v;
1127
      if (format == FOURCC_YV24) {
1128 1129
        dst_v = dst_sample + width * height;
        dst_u = dst_v + width * height;
1130 1131 1132
      } else {
        dst_u = dst_sample + width * height;
        dst_v = dst_u + width * height;
1133
      }
1134 1135 1136 1137 1138 1139 1140
      r = I420ToI444(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample, width,
                     dst_u, width,
                     dst_v, width,
                     width, height);
1141 1142
      break;
    }
1143 1144 1145 1146
    case FOURCC_I411: {
      int quarterwidth = (width + 3) / 4;
      uint8* dst_u = dst_sample + width * height;
      uint8* dst_v = dst_u + quarterwidth * height;
1147 1148 1149 1150 1151 1152 1153
      r = I420ToI411(y, y_stride,
                     u, u_stride,
                     v, v_stride,
                     dst_sample, width,
                     dst_u, quarterwidth,
                     dst_v, quarterwidth,
                     width, height);
1154 1155
      break;
    }
1156 1157 1158 1159 1160

    // Formats not supported - MJPG, biplanar, some rgb formats.
    default:
      return -1;  // unknown fourcc - return failure code.
  }
1161
  return r;
1162 1163 1164 1165 1166 1167
}

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