Coverage Report

Created: 2026-12-01 07:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libavif/ext/libyuv/source/row_common.cc
Line
Count
Source
1
/*
2
 *  Copyright 2011 The LibYuv Project Authors. All rights reserved.
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
 *  that can be found in the LICENSE file in the root of the source
6
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS. All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
9
 */
10
11
#include "libyuv/row.h"
12
13
#include <assert.h>
14
#include <string.h>  // For memcpy and memset.
15
16
#include "libyuv/basic_types.h"
17
#include "libyuv/convert_argb.h"       // For kYuvI601Constants
18
#include "libyuv/convert_from_argb.h"  // For ArgbConstants
19
20
#ifdef __cplusplus
21
namespace libyuv {
22
extern "C" {
23
#endif
24
25
#ifdef __cplusplus
26
76.0k
#define STATIC_CAST(type, expr) static_cast<type>(expr)
27
#else
28
#define STATIC_CAST(type, expr) (type)(expr)
29
#endif
30
31
// This macro controls YUV to RGB using unsigned math to extend range of
32
// YUV to RGB coefficients to 0 to 4 instead of 0 to 2 for more accuracy on B:
33
// LIBYUV_UNLIMITED_DATA
34
35
// Macros to enable unlimited data for each colorspace
36
// LIBYUV_UNLIMITED_BT601
37
// LIBYUV_UNLIMITED_BT709
38
// LIBYUV_UNLIMITED_BT2020
39
40
// llvm x86 is poor at ternary operator, so use branchless min/max.
41
42
#define USE_BRANCHLESS 1
43
#if defined(USE_BRANCHLESS)
44
58.2k
static __inline int32_t clamp0(int32_t v) {
45
58.2k
  return -(v >= 0) & v;
46
58.2k
}
47
// TODO(fbarchard): make clamp255 preserve negative values.
48
58.2k
static __inline int32_t clamp255(int32_t v) {
49
58.2k
  return (-(v >= 255) | v) & 255;
50
58.2k
}
51
52
0
static __inline int32_t clamp1023(int32_t v) {
53
0
  return (-(v >= 1023) | v) & 1023;
54
0
}
55
56
// clamp to max
57
0
static __inline int32_t ClampMax(int32_t v, int32_t max) {
58
0
  return (-(v >= max) | v) & max;
59
0
}
60
61
0
static __inline uint32_t Abs(int32_t v) {
62
0
  int m = -(v < 0);
63
0
  return (v + m) ^ m;
64
0
}
65
#else   // USE_BRANCHLESS
66
static __inline int32_t clamp0(int32_t v) {
67
  return (v < 0) ? 0 : v;
68
}
69
70
static __inline int32_t clamp255(int32_t v) {
71
  return (v > 255) ? 255 : v;
72
}
73
74
static __inline int32_t clamp1023(int32_t v) {
75
  return (v > 1023) ? 1023 : v;
76
}
77
78
static __inline int32_t ClampMax(int32_t v, int32_t max) {
79
  return (v > max) ? max : v;
80
}
81
82
static __inline uint32_t Abs(int32_t v) {
83
  return (v < 0) ? -v : v;
84
}
85
#endif  // USE_BRANCHLESS
86
58.2k
static __inline uint32_t Clamp(int32_t val) {
87
58.2k
  int v = clamp0(val);
88
58.2k
  return (uint32_t)(clamp255(v));
89
58.2k
}
90
91
0
static __inline uint32_t Clamp10(int32_t val) {
92
0
  int v = clamp0(val);
93
0
  return (uint32_t)(clamp1023(v));
94
0
}
95
96
// Little Endian
97
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || \
98
    defined(_M_IX86) || defined(__arm__) || defined(_M_ARM) ||     \
99
    (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
100
0
static inline void WRITEWORD(uint8_t* p, uint32_t v) {
101
0
  memcpy(p, &v, 4);
102
0
}
103
#else
104
static inline void WRITEWORD(uint8_t* p, uint32_t v) {
105
  p[0] = (uint8_t)(v & 255);
106
  p[1] = (uint8_t)((v >> 8) & 255);
107
  p[2] = (uint8_t)((v >> 16) & 255);
108
  p[3] = (uint8_t)((v >> 24) & 255);
109
}
110
#endif
111
112
0
void RGB24ToARGBRow_C(const uint8_t* src_rgb24, uint8_t* dst_argb, int width) {
113
0
  int x;
114
0
  for (x = 0; x < width; ++x) {
115
0
    uint8_t b = src_rgb24[0];
116
0
    uint8_t g = src_rgb24[1];
117
0
    uint8_t r = src_rgb24[2];
118
0
    dst_argb[0] = b;
119
0
    dst_argb[1] = g;
120
0
    dst_argb[2] = r;
121
0
    dst_argb[3] = 255u;
122
0
    dst_argb += 4;
123
0
    src_rgb24 += 3;
124
0
  }
125
0
}
126
127
0
void RAWToARGBRow_C(const uint8_t* src_raw, uint8_t* dst_argb, int width) {
128
0
  int x;
129
0
  for (x = 0; x < width; ++x) {
130
0
    uint8_t r = src_raw[0];
131
0
    uint8_t g = src_raw[1];
132
0
    uint8_t b = src_raw[2];
133
0
    dst_argb[0] = b;
134
0
    dst_argb[1] = g;
135
0
    dst_argb[2] = r;
136
0
    dst_argb[3] = 255u;
137
0
    dst_argb += 4;
138
0
    src_raw += 3;
139
0
  }
140
0
}
141
142
0
void RAWToRGBARow_C(const uint8_t* src_raw, uint8_t* dst_rgba, int width) {
143
0
  int x;
144
0
  for (x = 0; x < width; ++x) {
145
0
    uint8_t r = src_raw[0];
146
0
    uint8_t g = src_raw[1];
147
0
    uint8_t b = src_raw[2];
148
0
    dst_rgba[0] = 255u;
149
0
    dst_rgba[1] = b;
150
0
    dst_rgba[2] = g;
151
0
    dst_rgba[3] = r;
152
0
    dst_rgba += 4;
153
0
    src_raw += 3;
154
0
  }
155
0
}
156
157
0
void RAWToRGB24Row_C(const uint8_t* src_raw, uint8_t* dst_rgb24, int width) {
158
0
  int x;
159
0
  for (x = 0; x < width; ++x) {
160
0
    uint8_t r = src_raw[0];
161
0
    uint8_t g = src_raw[1];
162
0
    uint8_t b = src_raw[2];
163
0
    dst_rgb24[0] = b;
164
0
    dst_rgb24[1] = g;
165
0
    dst_rgb24[2] = r;
166
0
    dst_rgb24 += 3;
167
0
    src_raw += 3;
168
0
  }
169
0
}
170
171
void RGB565ToARGBRow_C(const uint8_t* src_rgb565,
172
                       uint8_t* dst_argb,
173
0
                       int width) {
174
0
  int x;
175
0
  for (x = 0; x < width; ++x) {
176
0
    uint8_t b = STATIC_CAST(uint8_t, src_rgb565[0] & 0x1f);
177
0
    uint8_t g = STATIC_CAST(
178
0
        uint8_t, (src_rgb565[0] >> 5) | ((src_rgb565[1] & 0x07) << 3));
179
0
    uint8_t r = STATIC_CAST(uint8_t, src_rgb565[1] >> 3);
180
0
    dst_argb[0] = STATIC_CAST(uint8_t, (b << 3) | (b >> 2));
181
0
    dst_argb[1] = STATIC_CAST(uint8_t, (g << 2) | (g >> 4));
182
0
    dst_argb[2] = STATIC_CAST(uint8_t, (r << 3) | (r >> 2));
183
0
    dst_argb[3] = 255u;
184
0
    dst_argb += 4;
185
0
    src_rgb565 += 2;
186
0
  }
187
0
}
188
189
void ARGB1555ToARGBRow_C(const uint8_t* src_argb1555,
190
                         uint8_t* dst_argb,
191
0
                         int width) {
192
0
  int x;
193
0
  for (x = 0; x < width; ++x) {
194
0
    uint8_t b = STATIC_CAST(uint8_t, src_argb1555[0] & 0x1f);
195
0
    uint8_t g = STATIC_CAST(
196
0
        uint8_t, (src_argb1555[0] >> 5) | ((src_argb1555[1] & 0x03) << 3));
197
0
    uint8_t r = STATIC_CAST(uint8_t, (src_argb1555[1] & 0x7c) >> 2);
198
0
    uint8_t a = STATIC_CAST(uint8_t, src_argb1555[1] >> 7);
199
0
    dst_argb[0] = STATIC_CAST(uint8_t, (b << 3) | (b >> 2));
200
0
    dst_argb[1] = STATIC_CAST(uint8_t, (g << 3) | (g >> 2));
201
0
    dst_argb[2] = STATIC_CAST(uint8_t, (r << 3) | (r >> 2));
202
0
    dst_argb[3] = -a;
203
0
    dst_argb += 4;
204
0
    src_argb1555 += 2;
205
0
  }
206
0
}
207
208
void ARGB4444ToARGBRow_C(const uint8_t* src_argb4444,
209
                         uint8_t* dst_argb,
210
0
                         int width) {
211
0
  int x;
212
0
  for (x = 0; x < width; ++x) {
213
0
    uint8_t b = STATIC_CAST(uint8_t, src_argb4444[0] & 0x0f);
214
0
    uint8_t g = STATIC_CAST(uint8_t, src_argb4444[0] >> 4);
215
0
    uint8_t r = STATIC_CAST(uint8_t, src_argb4444[1] & 0x0f);
216
0
    uint8_t a = STATIC_CAST(uint8_t, src_argb4444[1] >> 4);
217
0
    dst_argb[0] = STATIC_CAST(uint8_t, (b << 4) | b);
218
0
    dst_argb[1] = STATIC_CAST(uint8_t, (g << 4) | g);
219
0
    dst_argb[2] = STATIC_CAST(uint8_t, (r << 4) | r);
220
0
    dst_argb[3] = STATIC_CAST(uint8_t, (a << 4) | a);
221
0
    dst_argb += 4;
222
0
    src_argb4444 += 2;
223
0
  }
224
0
}
225
226
0
void AR30ToARGBRow_C(const uint8_t* src_ar30, uint8_t* dst_argb, int width) {
227
0
  int x;
228
0
  for (x = 0; x < width; ++x) {
229
0
    uint32_t ar30;
230
0
    memcpy(&ar30, src_ar30, sizeof ar30);
231
0
    uint32_t b = (ar30 >> 2) & 0xff;
232
0
    uint32_t g = (ar30 >> 12) & 0xff;
233
0
    uint32_t r = (ar30 >> 22) & 0xff;
234
0
    uint32_t a = (ar30 >> 30) * 0x55;  // Replicate 2 bits to 8 bits.
235
0
    *(uint32_t*)(dst_argb) = b | (g << 8) | (r << 16) | (a << 24);
236
0
    dst_argb += 4;
237
0
    src_ar30 += 4;
238
0
  }
239
0
}
240
241
0
void AR30ToABGRRow_C(const uint8_t* src_ar30, uint8_t* dst_abgr, int width) {
242
0
  int x;
243
0
  for (x = 0; x < width; ++x) {
244
0
    uint32_t ar30;
245
0
    memcpy(&ar30, src_ar30, sizeof ar30);
246
0
    uint32_t b = (ar30 >> 2) & 0xff;
247
0
    uint32_t g = (ar30 >> 12) & 0xff;
248
0
    uint32_t r = (ar30 >> 22) & 0xff;
249
0
    uint32_t a = (ar30 >> 30) * 0x55;  // Replicate 2 bits to 8 bits.
250
0
    *(uint32_t*)(dst_abgr) = r | (g << 8) | (b << 16) | (a << 24);
251
0
    dst_abgr += 4;
252
0
    src_ar30 += 4;
253
0
  }
254
0
}
255
256
0
void AR30ToAB30Row_C(const uint8_t* src_ar30, uint8_t* dst_ab30, int width) {
257
0
  int x;
258
0
  for (x = 0; x < width; ++x) {
259
0
    uint32_t ar30;
260
0
    memcpy(&ar30, src_ar30, sizeof ar30);
261
0
    uint32_t b = ar30 & 0x3ff;
262
0
    uint32_t ga = ar30 & 0xc00ffc00;
263
0
    uint32_t r = (ar30 >> 20) & 0x3ff;
264
0
    *(uint32_t*)(dst_ab30) = r | ga | (b << 20);
265
0
    dst_ab30 += 4;
266
0
    src_ar30 += 4;
267
0
  }
268
0
}
269
270
0
void ARGBToABGRRow_C(const uint8_t* src_argb, uint8_t* dst_abgr, int width) {
271
0
  int x;
272
0
  for (x = 0; x < width; ++x) {
273
0
    uint8_t b = src_argb[0];
274
0
    uint8_t g = src_argb[1];
275
0
    uint8_t r = src_argb[2];
276
0
    uint8_t a = src_argb[3];
277
0
    dst_abgr[0] = r;
278
0
    dst_abgr[1] = g;
279
0
    dst_abgr[2] = b;
280
0
    dst_abgr[3] = a;
281
0
    dst_abgr += 4;
282
0
    src_argb += 4;
283
0
  }
284
0
}
285
286
0
void ARGBToBGRARow_C(const uint8_t* src_argb, uint8_t* dst_bgra, int width) {
287
0
  int x;
288
0
  for (x = 0; x < width; ++x) {
289
0
    uint8_t b = src_argb[0];
290
0
    uint8_t g = src_argb[1];
291
0
    uint8_t r = src_argb[2];
292
0
    uint8_t a = src_argb[3];
293
0
    dst_bgra[0] = a;
294
0
    dst_bgra[1] = r;
295
0
    dst_bgra[2] = g;
296
0
    dst_bgra[3] = b;
297
0
    dst_bgra += 4;
298
0
    src_argb += 4;
299
0
  }
300
0
}
301
302
0
void ARGBToRGBARow_C(const uint8_t* src_argb, uint8_t* dst_rgba, int width) {
303
0
  int x;
304
0
  for (x = 0; x < width; ++x) {
305
0
    uint8_t b = src_argb[0];
306
0
    uint8_t g = src_argb[1];
307
0
    uint8_t r = src_argb[2];
308
0
    uint8_t a = src_argb[3];
309
0
    dst_rgba[0] = a;
310
0
    dst_rgba[1] = b;
311
0
    dst_rgba[2] = g;
312
0
    dst_rgba[3] = r;
313
0
    dst_rgba += 4;
314
0
    src_argb += 4;
315
0
  }
316
0
}
317
318
0
void ARGBToRGB24Row_C(const uint8_t* src_argb, uint8_t* dst_rgb, int width) {
319
0
  int x;
320
0
  for (x = 0; x < width; ++x) {
321
0
    uint8_t b = src_argb[0];
322
0
    uint8_t g = src_argb[1];
323
0
    uint8_t r = src_argb[2];
324
0
    dst_rgb[0] = b;
325
0
    dst_rgb[1] = g;
326
0
    dst_rgb[2] = r;
327
0
    dst_rgb += 3;
328
0
    src_argb += 4;
329
0
  }
330
0
}
331
332
0
void ARGBToRAWRow_C(const uint8_t* src_argb, uint8_t* dst_rgb, int width) {
333
0
  int x;
334
0
  for (x = 0; x < width; ++x) {
335
0
    uint8_t b = src_argb[0];
336
0
    uint8_t g = src_argb[1];
337
0
    uint8_t r = src_argb[2];
338
0
    dst_rgb[0] = r;
339
0
    dst_rgb[1] = g;
340
0
    dst_rgb[2] = b;
341
0
    dst_rgb += 3;
342
0
    src_argb += 4;
343
0
  }
344
0
}
345
346
0
void RGBAToARGBRow_C(const uint8_t* src_rgba, uint8_t* dst_argb, int width) {
347
0
  int x;
348
0
  for (x = 0; x < width; ++x) {
349
0
    uint8_t a = src_rgba[0];
350
0
    uint8_t b = src_rgba[1];
351
0
    uint8_t g = src_rgba[2];
352
0
    uint8_t r = src_rgba[3];
353
0
    dst_argb[0] = b;
354
0
    dst_argb[1] = g;
355
0
    dst_argb[2] = r;
356
0
    dst_argb[3] = a;
357
0
    dst_argb += 4;
358
0
    src_rgba += 4;
359
0
  }
360
0
}
361
362
0
void ARGBToRGB565Row_C(const uint8_t* src_argb, uint8_t* dst_rgb, int width) {
363
0
  int x;
364
0
  for (x = 0; x < width - 1; x += 2) {
365
0
    uint8_t b0 = src_argb[0] >> 3;
366
0
    uint8_t g0 = src_argb[1] >> 2;
367
0
    uint8_t r0 = src_argb[2] >> 3;
368
0
    uint8_t b1 = src_argb[4] >> 3;
369
0
    uint8_t g1 = src_argb[5] >> 2;
370
0
    uint8_t r1 = src_argb[6] >> 3;
371
0
    WRITEWORD(dst_rgb, b0 | (g0 << 5) | (r0 << 11) | (b1 << 16) | (g1 << 21) |
372
0
                           (r1 << 27));
373
0
    dst_rgb += 4;
374
0
    src_argb += 8;
375
0
  }
376
0
  if (width & 1) {
377
0
    uint8_t b0 = src_argb[0] >> 3;
378
0
    uint8_t g0 = src_argb[1] >> 2;
379
0
    uint8_t r0 = src_argb[2] >> 3;
380
0
    *(uint16_t*)(dst_rgb) = STATIC_CAST(uint16_t, b0 | (g0 << 5) | (r0 << 11));
381
0
  }
382
0
}
383
384
// dither4 is a row of 4 values from 4x4 dither matrix.
385
// The 4x4 matrix contains values to increase RGB.  When converting to
386
// fewer bits (565) this provides an ordered dither.
387
// The order in the 4x4 matrix in first byte is upper left.
388
// The 4 values are passed as an int, then referenced as an array, so
389
// endian will not affect order of the original matrix.  But the dither4
390
// will containing the first pixel in the lower byte for little endian
391
// or the upper byte for big endian.
392
void ARGBToRGB565DitherRow_C(const uint8_t* src_argb,
393
                             uint8_t* dst_rgb,
394
                             uint32_t dither4,
395
0
                             int width) {
396
0
  int x;
397
0
  for (x = 0; x < width - 1; x += 2) {
398
0
    int dither0 = ((const unsigned char*)(&dither4))[x & 3];
399
0
    int dither1 = ((const unsigned char*)(&dither4))[(x + 1) & 3];
400
0
    uint8_t b0 = STATIC_CAST(uint8_t, clamp255(src_argb[0] + dither0) >> 3);
401
0
    uint8_t g0 = STATIC_CAST(uint8_t, clamp255(src_argb[1] + dither0) >> 2);
402
0
    uint8_t r0 = STATIC_CAST(uint8_t, clamp255(src_argb[2] + dither0) >> 3);
403
0
    uint8_t b1 = STATIC_CAST(uint8_t, clamp255(src_argb[4] + dither1) >> 3);
404
0
    uint8_t g1 = STATIC_CAST(uint8_t, clamp255(src_argb[5] + dither1) >> 2);
405
0
    uint8_t r1 = STATIC_CAST(uint8_t, clamp255(src_argb[6] + dither1) >> 3);
406
0
    *(uint16_t*)(dst_rgb + 0) =
407
0
        STATIC_CAST(uint16_t, b0 | (g0 << 5) | (r0 << 11));
408
0
    *(uint16_t*)(dst_rgb + 2) =
409
0
        STATIC_CAST(uint16_t, b1 | (g1 << 5) | (r1 << 11));
410
0
    dst_rgb += 4;
411
0
    src_argb += 8;
412
0
  }
413
0
  if (width & 1) {
414
0
    int dither0 = ((const unsigned char*)(&dither4))[(width - 1) & 3];
415
0
    uint8_t b0 = STATIC_CAST(uint8_t, clamp255(src_argb[0] + dither0) >> 3);
416
0
    uint8_t g0 = STATIC_CAST(uint8_t, clamp255(src_argb[1] + dither0) >> 2);
417
0
    uint8_t r0 = STATIC_CAST(uint8_t, clamp255(src_argb[2] + dither0) >> 3);
418
0
    *(uint16_t*)(dst_rgb) = STATIC_CAST(uint16_t, b0 | (g0 << 5) | (r0 << 11));
419
0
  }
420
0
}
421
422
0
void ARGBToARGB1555Row_C(const uint8_t* src_argb, uint8_t* dst_rgb, int width) {
423
0
  int x;
424
0
  for (x = 0; x < width - 1; x += 2) {
425
0
    uint8_t b0 = src_argb[0] >> 3;
426
0
    uint8_t g0 = src_argb[1] >> 3;
427
0
    uint8_t r0 = src_argb[2] >> 3;
428
0
    uint8_t a0 = src_argb[3] >> 7;
429
0
    uint8_t b1 = src_argb[4] >> 3;
430
0
    uint8_t g1 = src_argb[5] >> 3;
431
0
    uint8_t r1 = src_argb[6] >> 3;
432
0
    uint8_t a1 = src_argb[7] >> 7;
433
0
    *(uint16_t*)(dst_rgb + 0) =
434
0
        STATIC_CAST(uint16_t, b0 | (g0 << 5) | (r0 << 10) | (a0 << 15));
435
0
    *(uint16_t*)(dst_rgb + 2) =
436
0
        STATIC_CAST(uint16_t, b1 | (g1 << 5) | (r1 << 10) | (a1 << 15));
437
0
    dst_rgb += 4;
438
0
    src_argb += 8;
439
0
  }
440
0
  if (width & 1) {
441
0
    uint8_t b0 = src_argb[0] >> 3;
442
0
    uint8_t g0 = src_argb[1] >> 3;
443
0
    uint8_t r0 = src_argb[2] >> 3;
444
0
    uint8_t a0 = src_argb[3] >> 7;
445
0
    *(uint16_t*)(dst_rgb) =
446
0
        STATIC_CAST(uint16_t, b0 | (g0 << 5) | (r0 << 10) | (a0 << 15));
447
0
  }
448
0
}
449
450
0
void ARGBToARGB4444Row_C(const uint8_t* src_argb, uint8_t* dst_rgb, int width) {
451
0
  int x;
452
0
  for (x = 0; x < width - 1; x += 2) {
453
0
    uint8_t b0 = src_argb[0] >> 4;
454
0
    uint8_t g0 = src_argb[1] >> 4;
455
0
    uint8_t r0 = src_argb[2] >> 4;
456
0
    uint8_t a0 = src_argb[3] >> 4;
457
0
    uint8_t b1 = src_argb[4] >> 4;
458
0
    uint8_t g1 = src_argb[5] >> 4;
459
0
    uint8_t r1 = src_argb[6] >> 4;
460
0
    uint8_t a1 = src_argb[7] >> 4;
461
0
    *(uint16_t*)(dst_rgb + 0) =
462
0
        STATIC_CAST(uint16_t, b0 | (g0 << 4) | (r0 << 8) | (a0 << 12));
463
0
    *(uint16_t*)(dst_rgb + 2) =
464
0
        STATIC_CAST(uint16_t, b1 | (g1 << 4) | (r1 << 8) | (a1 << 12));
465
0
    dst_rgb += 4;
466
0
    src_argb += 8;
467
0
  }
468
0
  if (width & 1) {
469
0
    uint8_t b0 = src_argb[0] >> 4;
470
0
    uint8_t g0 = src_argb[1] >> 4;
471
0
    uint8_t r0 = src_argb[2] >> 4;
472
0
    uint8_t a0 = src_argb[3] >> 4;
473
0
    *(uint16_t*)(dst_rgb) =
474
0
        STATIC_CAST(uint16_t, b0 | (g0 << 4) | (r0 << 8) | (a0 << 12));
475
0
  }
476
0
}
477
478
0
void ABGRToAR30Row_C(const uint8_t* src_abgr, uint8_t* dst_ar30, int width) {
479
0
  int x;
480
0
  for (x = 0; x < width; ++x) {
481
0
    uint32_t r0 = (src_abgr[0] >> 6) | ((uint32_t)(src_abgr[0]) << 2);
482
0
    uint32_t g0 = (src_abgr[1] >> 6) | ((uint32_t)(src_abgr[1]) << 2);
483
0
    uint32_t b0 = (src_abgr[2] >> 6) | ((uint32_t)(src_abgr[2]) << 2);
484
0
    uint32_t a0 = (src_abgr[3] >> 6);
485
0
    *(uint32_t*)(dst_ar30) =
486
0
        STATIC_CAST(uint32_t, b0 | (g0 << 10) | (r0 << 20) | (a0 << 30));
487
0
    dst_ar30 += 4;
488
0
    src_abgr += 4;
489
0
  }
490
0
}
491
492
0
void ARGBToAR30Row_C(const uint8_t* src_argb, uint8_t* dst_ar30, int width) {
493
0
  int x;
494
0
  for (x = 0; x < width; ++x) {
495
0
    uint32_t b0 = (src_argb[0] >> 6) | ((uint32_t)(src_argb[0]) << 2);
496
0
    uint32_t g0 = (src_argb[1] >> 6) | ((uint32_t)(src_argb[1]) << 2);
497
0
    uint32_t r0 = (src_argb[2] >> 6) | ((uint32_t)(src_argb[2]) << 2);
498
0
    uint32_t a0 = (src_argb[3] >> 6);
499
0
    *(uint32_t*)(dst_ar30) =
500
0
        STATIC_CAST(uint32_t, b0 | (g0 << 10) | (r0 << 20) | (a0 << 30));
501
0
    dst_ar30 += 4;
502
0
    src_argb += 4;
503
0
  }
504
0
}
505
506
0
void ARGBToAR64Row_C(const uint8_t* src_argb, uint16_t* dst_ar64, int width) {
507
0
  int x;
508
0
  for (x = 0; x < width; ++x) {
509
0
    uint16_t b = src_argb[0] * 0x0101;
510
0
    uint16_t g = src_argb[1] * 0x0101;
511
0
    uint16_t r = src_argb[2] * 0x0101;
512
0
    uint16_t a = src_argb[3] * 0x0101;
513
0
    dst_ar64[0] = b;
514
0
    dst_ar64[1] = g;
515
0
    dst_ar64[2] = r;
516
0
    dst_ar64[3] = a;
517
0
    dst_ar64 += 4;
518
0
    src_argb += 4;
519
0
  }
520
0
}
521
522
0
void ARGBToAB64Row_C(const uint8_t* src_argb, uint16_t* dst_ab64, int width) {
523
0
  int x;
524
0
  for (x = 0; x < width; ++x) {
525
0
    uint16_t b = src_argb[0] * 0x0101;
526
0
    uint16_t g = src_argb[1] * 0x0101;
527
0
    uint16_t r = src_argb[2] * 0x0101;
528
0
    uint16_t a = src_argb[3] * 0x0101;
529
0
    dst_ab64[0] = r;
530
0
    dst_ab64[1] = g;
531
0
    dst_ab64[2] = b;
532
0
    dst_ab64[3] = a;
533
0
    dst_ab64 += 4;
534
0
    src_argb += 4;
535
0
  }
536
0
}
537
538
0
void AR64ToARGBRow_C(const uint16_t* src_ar64, uint8_t* dst_argb, int width) {
539
0
  int x;
540
0
  for (x = 0; x < width; ++x) {
541
0
    uint8_t b = src_ar64[0] >> 8;
542
0
    uint8_t g = src_ar64[1] >> 8;
543
0
    uint8_t r = src_ar64[2] >> 8;
544
0
    uint8_t a = src_ar64[3] >> 8;
545
0
    dst_argb[0] = b;
546
0
    dst_argb[1] = g;
547
0
    dst_argb[2] = r;
548
0
    dst_argb[3] = a;
549
0
    dst_argb += 4;
550
0
    src_ar64 += 4;
551
0
  }
552
0
}
553
554
0
void AB64ToARGBRow_C(const uint16_t* src_ab64, uint8_t* dst_argb, int width) {
555
0
  int x;
556
0
  for (x = 0; x < width; ++x) {
557
0
    uint8_t r = src_ab64[0] >> 8;
558
0
    uint8_t g = src_ab64[1] >> 8;
559
0
    uint8_t b = src_ab64[2] >> 8;
560
0
    uint8_t a = src_ab64[3] >> 8;
561
0
    dst_argb[0] = b;
562
0
    dst_argb[1] = g;
563
0
    dst_argb[2] = r;
564
0
    dst_argb[3] = a;
565
0
    dst_argb += 4;
566
0
    src_ab64 += 4;
567
0
  }
568
0
}
569
570
0
void AR64ToAB64Row_C(const uint16_t* src_ar64, uint16_t* dst_ab64, int width) {
571
0
  int x;
572
0
  for (x = 0; x < width; ++x) {
573
0
    uint16_t b = src_ar64[0];
574
0
    uint16_t g = src_ar64[1];
575
0
    uint16_t r = src_ar64[2];
576
0
    uint16_t a = src_ar64[3];
577
0
    dst_ab64[0] = r;
578
0
    dst_ab64[1] = g;
579
0
    dst_ab64[2] = b;
580
0
    dst_ab64[3] = a;
581
0
    dst_ab64 += 4;
582
0
    src_ar64 += 4;
583
0
  }
584
0
}
585
586
// TODO(fbarchard): Make shuffle compatible with SIMD versions
587
void AR64ShuffleRow_C(const uint8_t* src_ar64,
588
                      uint8_t* dst_ar64,
589
                      const uint8_t* shuffler,
590
0
                      int width) {
591
0
  const uint16_t* src_ar64_16 = (const uint16_t*)src_ar64;
592
0
  uint16_t* dst_ar64_16 = (uint16_t*)dst_ar64;
593
0
  int index0 = shuffler[0] / 2;
594
0
  int index1 = shuffler[2] / 2;
595
0
  int index2 = shuffler[4] / 2;
596
0
  int index3 = shuffler[6] / 2;
597
  // Shuffle a row of AR64.
598
0
  int x;
599
0
  for (x = 0; x < width / 2; ++x) {
600
    // To support in-place conversion.
601
0
    uint16_t b = src_ar64_16[index0];
602
0
    uint16_t g = src_ar64_16[index1];
603
0
    uint16_t r = src_ar64_16[index2];
604
0
    uint16_t a = src_ar64_16[index3];
605
0
    dst_ar64_16[0] = b;
606
0
    dst_ar64_16[1] = g;
607
0
    dst_ar64_16[2] = r;
608
0
    dst_ar64_16[3] = a;
609
0
    src_ar64_16 += 4;
610
0
    dst_ar64_16 += 4;
611
0
  }
612
0
}
613
// BT601 8 bit Y:
614
// b 0.114 * 219 = 24.966  = 25
615
// g 0.587 * 219 = 128.553 = 129
616
// r 0.299 * 219 = 65.481  = 66
617
// BT601 8 bit U:
618
// b  0.875  * 128 = 112.0    = 112
619
// g -0.5781 * 128 = −73.9968 = -74
620
// r -0.2969 * 128 = −38.0032 = -38
621
// BT601 8 bit V:
622
// b -0.1406 * 128 = −17.9968 = -18
623
// g -0.7344 * 128 = −94.0032 = -94
624
// r  0.875  * 128 = 112.0    = 112
625
0
static __inline uint8_t RGBToY(uint8_t r, uint8_t g, uint8_t b) {
626
0
  return STATIC_CAST(uint8_t, (66 * r + 129 * g + 25 * b + 0x1080) >> 8);
627
0
}
628
0
static __inline uint8_t RGBToU(uint8_t r, uint8_t g, uint8_t b) {
629
0
  return STATIC_CAST(uint8_t, (112 * b - 74 * g - 38 * r + 0x8000) >> 8);
630
0
}
631
0
static __inline uint8_t RGBToV(uint8_t r, uint8_t g, uint8_t b) {
632
0
  return STATIC_CAST(uint8_t, (112 * r - 94 * g - 18 * b + 0x8000) >> 8);
633
0
}
634
0
#define AVGB(a, b) (((a) + (b) + 1) >> 1)
635
636
#define MAKEROWY(NAME, R, G, B, BPP)                                       \
637
0
  void NAME##ToYRow_C(const uint8_t* src_rgb, uint8_t* dst_y, int width) { \
638
0
    int x;                                                                 \
639
0
    for (x = 0; x < width; ++x) {                                          \
640
0
      dst_y[0] = RGBToY(src_rgb[R], src_rgb[G], src_rgb[B]);               \
641
0
      src_rgb += BPP;                                                      \
642
0
      dst_y += 1;                                                          \
643
0
    }                                                                      \
644
0
  }                                                                        \
Unexecuted instantiation: ARGBToYRow_C
Unexecuted instantiation: BGRAToYRow_C
Unexecuted instantiation: ABGRToYRow_C
Unexecuted instantiation: RGBAToYRow_C
645
  void NAME##ToUVRow_C(const uint8_t* src_rgb, int src_stride_rgb,         \
646
0
                       uint8_t* dst_u, uint8_t* dst_v, int width) {        \
647
0
    const uint8_t* src_rgb1 = src_rgb + src_stride_rgb;                    \
648
0
    int x;                                                                 \
649
0
    for (x = 0; x < width - 1; x += 2) {                                   \
650
0
      uint8_t ab = (src_rgb[B] + src_rgb[B + BPP] + src_rgb1[B] +          \
651
0
                    src_rgb1[B + BPP] + 2) >>                              \
652
0
                   2;                                                      \
653
0
      uint8_t ag = (src_rgb[G] + src_rgb[G + BPP] + src_rgb1[G] +          \
654
0
                    src_rgb1[G + BPP] + 2) >>                              \
655
0
                   2;                                                      \
656
0
      uint8_t ar = (src_rgb[R] + src_rgb[R + BPP] + src_rgb1[R] +          \
657
0
                    src_rgb1[R + BPP] + 2) >>                              \
658
0
                   2;                                                      \
659
0
      dst_u[0] = RGBToU(ar, ag, ab);                                       \
660
0
      dst_v[0] = RGBToV(ar, ag, ab);                                       \
661
0
      src_rgb += BPP * 2;                                                  \
662
0
      src_rgb1 += BPP * 2;                                                 \
663
0
      dst_u += 1;                                                          \
664
0
      dst_v += 1;                                                          \
665
0
    }                                                                      \
666
0
    if (width & 1) {                                                       \
667
0
      uint8_t ab = (src_rgb[B] + src_rgb1[B] + 1) >> 1;                    \
668
0
      uint8_t ag = (src_rgb[G] + src_rgb1[G] + 1) >> 1;                    \
669
0
      uint8_t ar = (src_rgb[R] + src_rgb1[R] + 1) >> 1;                    \
670
0
      dst_u[0] = RGBToU(ar, ag, ab);                                       \
671
0
      dst_v[0] = RGBToV(ar, ag, ab);                                       \
672
0
    }                                                                      \
673
0
  }
Unexecuted instantiation: ARGBToUVRow_C
Unexecuted instantiation: BGRAToUVRow_C
Unexecuted instantiation: ABGRToUVRow_C
Unexecuted instantiation: RGBAToUVRow_C
674
675
MAKEROWY(ARGB, 2, 1, 0, 4)
676
MAKEROWY(BGRA, 1, 2, 3, 4)
677
MAKEROWY(ABGR, 0, 1, 2, 4)
678
MAKEROWY(RGBA, 3, 2, 1, 4)
679
#undef MAKEROWY
680
681
// JPeg uses BT.601-1 full range
682
// y =  0.29900 * r + 0.58700 * g + 0.11400 * b
683
// u = -0.16874 * r - 0.33126 * g + 0.50000 * b  + center
684
// v =  0.50000 * r - 0.41869 * g - 0.08131 * b  + center
685
// JPeg 8 bit Y:
686
// b 0.11400 * 256 = 29.184 = 29
687
// g 0.58700 * 256 = 150.272 = 150
688
// r 0.29900 * 256 = 76.544 = 77
689
// JPeg 8 bit U:
690
// b  0.50000 * 256 = 128.0 = 128
691
// g -0.33126 * 256 = −84.80256 = -85
692
// r -0.16874 * 256 = −43.19744 = -43
693
// JPeg 8 bit V:
694
// b -0.08131 * 256 = −20.81536 = -21
695
// g -0.41869 * 256 = −107.18464 = -107
696
// r  0.50000 * 256 = 128.0 = 128
697
698
// 8 bit
699
0
static __inline uint8_t RGBToYJ(uint8_t r, uint8_t g, uint8_t b) {
700
0
  return (77 * r + 150 * g + 29 * b + 128) >> 8;
701
0
}
702
0
static __inline uint8_t RGBToUJ(uint8_t r, uint8_t g, uint8_t b) {
703
0
  return (128 * b - 85 * g - 43 * r + 0x8000) >> 8;
704
0
}
705
0
static __inline uint8_t RGBToVJ(uint8_t r, uint8_t g, uint8_t b) {
706
0
  return (128 * r - 107 * g - 21 * b + 0x8000) >> 8;
707
0
}
708
709
// ARGBToYJ_C and ARGBToUVJ_C
710
#define MAKEROWYJ(NAME, R, G, B, BPP)                                       \
711
0
  void NAME##ToYJRow_C(const uint8_t* src_rgb, uint8_t* dst_y, int width) { \
712
0
    int x;                                                                  \
713
0
    for (x = 0; x < width; ++x) {                                           \
714
0
      dst_y[0] = RGBToYJ(src_rgb[R], src_rgb[G], src_rgb[B]);               \
715
0
      src_rgb += BPP;                                                       \
716
0
      dst_y += 1;                                                           \
717
0
    }                                                                       \
718
0
  }                                                                         \
Unexecuted instantiation: ARGBToYJRow_C
Unexecuted instantiation: ABGRToYJRow_C
Unexecuted instantiation: RGBAToYJRow_C
719
  void NAME##ToUVJRow_C(const uint8_t* src_rgb, int src_stride_rgb,         \
720
0
                        uint8_t* dst_u, uint8_t* dst_v, int width) {        \
721
0
    const uint8_t* src_rgb1 = src_rgb + src_stride_rgb;                     \
722
0
    int x;                                                                  \
723
0
    for (x = 0; x < width - 1; x += 2) {                                    \
724
0
      uint8_t ab = (src_rgb[B] + src_rgb[B + BPP] + src_rgb1[B] +           \
725
0
                    src_rgb1[B + BPP] + 2) >>                               \
726
0
                   2;                                                       \
727
0
      uint8_t ag = (src_rgb[G] + src_rgb[G + BPP] + src_rgb1[G] +           \
728
0
                    src_rgb1[G + BPP] + 2) >>                               \
729
0
                   2;                                                       \
730
0
      uint8_t ar = (src_rgb[R] + src_rgb[R + BPP] + src_rgb1[R] +           \
731
0
                    src_rgb1[R + BPP] + 2) >>                               \
732
0
                   2;                                                       \
733
0
      dst_u[0] = RGBToUJ(ar, ag, ab);                                       \
734
0
      dst_v[0] = RGBToVJ(ar, ag, ab);                                       \
735
0
      src_rgb += BPP * 2;                                                   \
736
0
      src_rgb1 += BPP * 2;                                                  \
737
0
      dst_u += 1;                                                           \
738
0
      dst_v += 1;                                                           \
739
0
    }                                                                       \
740
0
    if (width & 1) {                                                        \
741
0
      uint16_t ab = (src_rgb[B] + src_rgb1[B] + 1) >> 1;                    \
742
0
      uint16_t ag = (src_rgb[G] + src_rgb1[G] + 1) >> 1;                    \
743
0
      uint16_t ar = (src_rgb[R] + src_rgb1[R] + 1) >> 1;                    \
744
0
      dst_u[0] = RGBToUJ(ar, ag, ab);                                       \
745
0
      dst_v[0] = RGBToVJ(ar, ag, ab);                                       \
746
0
    }                                                                       \
747
0
  }
Unexecuted instantiation: ARGBToUVJRow_C
Unexecuted instantiation: ABGRToUVJRow_C
Unexecuted instantiation: RGBAToUVJRow_C
748
749
MAKEROWYJ(ARGB, 2, 1, 0, 4)
750
MAKEROWYJ(ABGR, 0, 1, 2, 4)
751
MAKEROWYJ(RGBA, 3, 2, 1, 4)
752
#undef MAKEROWYJ
753
754
static __inline uint8_t RGBToYMatrix(uint8_t b0,
755
                                     uint8_t b1,
756
                                     uint8_t b2,
757
                                     uint8_t b3,
758
0
                                     const struct ArgbConstants* c) {
759
0
  return (c->kRGBToY[0] * b0 + c->kRGBToY[1] * b1 + c->kRGBToY[2] * b2 +
760
0
          c->kRGBToY[3] * b3 + c->kAddY[0]) >>
761
0
         8;
762
0
}
763
static __inline uint8_t RGBToUMatrix(uint8_t b0,
764
                                     uint8_t b1,
765
                                     uint8_t b2,
766
                                     uint8_t b3,
767
0
                                     const struct ArgbConstants* c) {
768
0
  return (c->kAddUV[0] - (c->kRGBToU[0] * b0 + c->kRGBToU[1] * b1 +
769
0
                          c->kRGBToU[2] * b2 + c->kRGBToU[3] * b3)) >>
770
0
         8;
771
0
}
772
static __inline uint8_t RGBToVMatrix(uint8_t b0,
773
                                     uint8_t b1,
774
                                     uint8_t b2,
775
                                     uint8_t b3,
776
0
                                     const struct ArgbConstants* c) {
777
0
  return (c->kAddUV[0] - (c->kRGBToV[0] * b0 + c->kRGBToV[1] * b1 +
778
0
                          c->kRGBToV[2] * b2 + c->kRGBToV[3] * b3)) >>
779
0
         8;
780
0
}
781
782
void ARGBToYMatrixRow_C(const uint8_t* src_argb,
783
                        uint8_t* dst_y,
784
                        int width,
785
0
                        const struct ArgbConstants* c) {
786
0
  int x;
787
0
  for (x = 0; x < width; ++x) {
788
0
    dst_y[0] =
789
0
        RGBToYMatrix(src_argb[0], src_argb[1], src_argb[2], src_argb[3], c);
790
0
    src_argb += 4;
791
0
    dst_y += 1;
792
0
  }
793
0
}
794
795
void ARGBToUVMatrixRow_C(const uint8_t* src_argb,
796
                         int src_stride_argb,
797
                         uint8_t* dst_u,
798
                         uint8_t* dst_v,
799
                         int width,
800
0
                         const struct ArgbConstants* c) {
801
0
  const uint8_t* src_argb1 = src_argb + src_stride_argb;
802
0
  int x;
803
0
  for (x = 0; x < width - 1; x += 2) {
804
0
    uint8_t b0 =
805
0
        (src_argb[0] + src_argb[4] + src_argb1[0] + src_argb1[4] + 2) >> 2;
806
0
    uint8_t b1 =
807
0
        (src_argb[1] + src_argb[5] + src_argb1[1] + src_argb1[5] + 2) >> 2;
808
0
    uint8_t b2 =
809
0
        (src_argb[2] + src_argb[6] + src_argb1[2] + src_argb1[6] + 2) >> 2;
810
0
    uint8_t b3 =
811
0
        (src_argb[3] + src_argb[7] + src_argb1[3] + src_argb1[7] + 2) >> 2;
812
0
    dst_u[0] = RGBToUMatrix(b0, b1, b2, b3, c);
813
0
    dst_v[0] = RGBToVMatrix(b0, b1, b2, b3, c);
814
0
    src_argb += 8;
815
0
    src_argb1 += 8;
816
0
    dst_u += 1;
817
0
    dst_v += 1;
818
0
  }
819
0
  if (width & 1) {
820
0
    uint8_t b0 = (src_argb[0] + src_argb1[0] + 1) >> 1;
821
0
    uint8_t b1 = (src_argb[1] + src_argb1[1] + 1) >> 1;
822
0
    uint8_t b2 = (src_argb[2] + src_argb1[2] + 1) >> 1;
823
0
    uint8_t b3 = (src_argb[3] + src_argb1[3] + 1) >> 1;
824
0
    dst_u[0] = RGBToUMatrix(b0, b1, b2, b3, c);
825
0
    dst_v[0] = RGBToVMatrix(b0, b1, b2, b3, c);
826
0
  }
827
0
}
828
829
void ARGBToUV444MatrixRow_C(const uint8_t* src_argb,
830
                            uint8_t* dst_u,
831
                            uint8_t* dst_v,
832
                            int width,
833
0
                            const struct ArgbConstants* c) {
834
0
  int x;
835
0
  for (x = 0; x < width; ++x) {
836
0
    dst_u[0] =
837
0
        RGBToUMatrix(src_argb[0], src_argb[1], src_argb[2], src_argb[3], c);
838
0
    dst_v[0] =
839
0
        RGBToVMatrix(src_argb[0], src_argb[1], src_argb[2], src_argb[3], c);
840
0
    src_argb += 4;
841
0
    dst_u += 1;
842
0
    dst_v += 1;
843
0
  }
844
0
}
845
846
0
void RGB565ToYRow_C(const uint8_t* src_rgb565, uint8_t* dst_y, int width) {
847
0
  int x;
848
0
  for (x = 0; x < width; ++x) {
849
0
    uint8_t b = src_rgb565[0] & 0x1f;
850
0
    uint8_t g = STATIC_CAST(
851
0
        uint8_t, (src_rgb565[0] >> 5) | ((src_rgb565[1] & 0x07) << 3));
852
0
    uint8_t r = src_rgb565[1] >> 3;
853
0
    b = STATIC_CAST(uint8_t, (b << 3) | (b >> 2));
854
0
    g = STATIC_CAST(uint8_t, (g << 2) | (g >> 4));
855
0
    r = STATIC_CAST(uint8_t, (r << 3) | (r >> 2));
856
0
    dst_y[0] = RGBToY(r, g, b);
857
0
    src_rgb565 += 2;
858
0
    dst_y += 1;
859
0
  }
860
0
}
861
862
0
void ARGB1555ToYRow_C(const uint8_t* src_argb1555, uint8_t* dst_y, int width) {
863
0
  int x;
864
0
  for (x = 0; x < width; ++x) {
865
0
    uint8_t b = src_argb1555[0] & 0x1f;
866
0
    uint8_t g = STATIC_CAST(
867
0
        uint8_t, (src_argb1555[0] >> 5) | ((src_argb1555[1] & 0x03) << 3));
868
0
    uint8_t r = (src_argb1555[1] & 0x7c) >> 2;
869
0
    b = STATIC_CAST(uint8_t, (b << 3) | (b >> 2));
870
0
    g = STATIC_CAST(uint8_t, (g << 3) | (g >> 2));
871
0
    r = STATIC_CAST(uint8_t, (r << 3) | (r >> 2));
872
0
    dst_y[0] = RGBToY(r, g, b);
873
0
    src_argb1555 += 2;
874
0
    dst_y += 1;
875
0
  }
876
0
}
877
878
0
void ARGB4444ToYRow_C(const uint8_t* src_argb4444, uint8_t* dst_y, int width) {
879
0
  int x;
880
0
  for (x = 0; x < width; ++x) {
881
0
    uint8_t b = src_argb4444[0] & 0x0f;
882
0
    uint8_t g = src_argb4444[0] >> 4;
883
0
    uint8_t r = src_argb4444[1] & 0x0f;
884
0
    b = STATIC_CAST(uint8_t, (b << 4) | b);
885
0
    g = STATIC_CAST(uint8_t, (g << 4) | g);
886
0
    r = STATIC_CAST(uint8_t, (r << 4) | r);
887
0
    dst_y[0] = RGBToY(r, g, b);
888
0
    src_argb4444 += 2;
889
0
    dst_y += 1;
890
0
  }
891
0
}
892
893
void RGB565ToUVRow_C(const uint8_t* src_rgb565,
894
                     int src_stride_rgb565,
895
                     uint8_t* dst_u,
896
                     uint8_t* dst_v,
897
0
                     int width) {
898
0
  const uint8_t* next_rgb565 = src_rgb565 + src_stride_rgb565;
899
0
  int x;
900
0
  for (x = 0; x < width - 1; x += 2) {
901
0
    uint8_t b0 = STATIC_CAST(uint8_t, src_rgb565[0] & 0x1f);
902
0
    uint8_t g0 = STATIC_CAST(
903
0
        uint8_t, (src_rgb565[0] >> 5) | ((src_rgb565[1] & 0x07) << 3));
904
0
    uint8_t r0 = STATIC_CAST(uint8_t, src_rgb565[1] >> 3);
905
0
    uint8_t b1 = STATIC_CAST(uint8_t, src_rgb565[2] & 0x1f);
906
0
    uint8_t g1 = STATIC_CAST(
907
0
        uint8_t, (src_rgb565[2] >> 5) | ((src_rgb565[3] & 0x07) << 3));
908
0
    uint8_t r1 = STATIC_CAST(uint8_t, src_rgb565[3] >> 3);
909
0
    uint8_t b2 = STATIC_CAST(uint8_t, next_rgb565[0] & 0x1f);
910
0
    uint8_t g2 = STATIC_CAST(
911
0
        uint8_t, (next_rgb565[0] >> 5) | ((next_rgb565[1] & 0x07) << 3));
912
0
    uint8_t r2 = STATIC_CAST(uint8_t, next_rgb565[1] >> 3);
913
0
    uint8_t b3 = STATIC_CAST(uint8_t, next_rgb565[2] & 0x1f);
914
0
    uint8_t g3 = STATIC_CAST(
915
0
        uint8_t, (next_rgb565[2] >> 5) | ((next_rgb565[3] & 0x07) << 3));
916
0
    uint8_t r3 = STATIC_CAST(uint8_t, next_rgb565[3] >> 3);
917
918
0
    b0 = STATIC_CAST(uint8_t, (b0 << 3) | (b0 >> 2));
919
0
    g0 = STATIC_CAST(uint8_t, (g0 << 2) | (g0 >> 4));
920
0
    r0 = STATIC_CAST(uint8_t, (r0 << 3) | (r0 >> 2));
921
0
    b1 = STATIC_CAST(uint8_t, (b1 << 3) | (b1 >> 2));
922
0
    g1 = STATIC_CAST(uint8_t, (g1 << 2) | (g1 >> 4));
923
0
    r1 = STATIC_CAST(uint8_t, (r1 << 3) | (r1 >> 2));
924
0
    b2 = STATIC_CAST(uint8_t, (b2 << 3) | (b2 >> 2));
925
0
    g2 = STATIC_CAST(uint8_t, (g2 << 2) | (g2 >> 4));
926
0
    r2 = STATIC_CAST(uint8_t, (r2 << 3) | (r2 >> 2));
927
0
    b3 = STATIC_CAST(uint8_t, (b3 << 3) | (b3 >> 2));
928
0
    g3 = STATIC_CAST(uint8_t, (g3 << 2) | (g3 >> 4));
929
0
    r3 = STATIC_CAST(uint8_t, (r3 << 3) | (r3 >> 2));
930
931
0
    uint8_t b = (b0 + b1 + b2 + b3 + 2) >> 2;
932
0
    uint8_t g = (g0 + g1 + g2 + g3 + 2) >> 2;
933
0
    uint8_t r = (r0 + r1 + r2 + r3 + 2) >> 2;
934
0
    dst_u[0] = RGBToU(r, g, b);
935
0
    dst_v[0] = RGBToV(r, g, b);
936
937
0
    src_rgb565 += 4;
938
0
    next_rgb565 += 4;
939
0
    dst_u += 1;
940
0
    dst_v += 1;
941
0
  }
942
0
  if (width & 1) {
943
0
    uint8_t b0 = STATIC_CAST(uint8_t, src_rgb565[0] & 0x1f);
944
0
    uint8_t g0 = STATIC_CAST(
945
0
        uint8_t, (src_rgb565[0] >> 5) | ((src_rgb565[1] & 0x07) << 3));
946
0
    uint8_t r0 = STATIC_CAST(uint8_t, src_rgb565[1] >> 3);
947
0
    uint8_t b2 = STATIC_CAST(uint8_t, next_rgb565[0] & 0x1f);
948
0
    uint8_t g2 = STATIC_CAST(
949
0
        uint8_t, (next_rgb565[0] >> 5) | ((next_rgb565[1] & 0x07) << 3));
950
0
    uint8_t r2 = STATIC_CAST(uint8_t, next_rgb565[1] >> 3);
951
0
    b0 = STATIC_CAST(uint8_t, (b0 << 3) | (b0 >> 2));
952
0
    g0 = STATIC_CAST(uint8_t, (g0 << 2) | (g0 >> 4));
953
0
    r0 = STATIC_CAST(uint8_t, (r0 << 3) | (r0 >> 2));
954
0
    b2 = STATIC_CAST(uint8_t, (b2 << 3) | (b2 >> 2));
955
0
    g2 = STATIC_CAST(uint8_t, (g2 << 2) | (g2 >> 4));
956
0
    r2 = STATIC_CAST(uint8_t, (r2 << 3) | (r2 >> 2));
957
958
0
    uint8_t ab = AVGB(b0, b2);
959
0
    uint8_t ag = AVGB(g0, g2);
960
0
    uint8_t ar = AVGB(r0, r2);
961
0
    dst_u[0] = RGBToU(ar, ag, ab);
962
0
    dst_v[0] = RGBToV(ar, ag, ab);
963
0
  }
964
0
}
965
966
void ARGB1555ToUVRow_C(const uint8_t* src_argb1555,
967
                       int src_stride_argb1555,
968
                       uint8_t* dst_u,
969
                       uint8_t* dst_v,
970
0
                       int width) {
971
0
  const uint8_t* next_argb1555 = src_argb1555 + src_stride_argb1555;
972
0
  int x;
973
0
  for (x = 0; x < width - 1; x += 2) {
974
0
    uint8_t b0 = STATIC_CAST(uint8_t, src_argb1555[0] & 0x1f);
975
0
    uint8_t g0 = STATIC_CAST(
976
0
        uint8_t, (src_argb1555[0] >> 5) | ((src_argb1555[1] & 0x03) << 3));
977
0
    uint8_t r0 = STATIC_CAST(uint8_t, (src_argb1555[1] & 0x7c) >> 2);
978
0
    uint8_t b1 = STATIC_CAST(uint8_t, src_argb1555[2] & 0x1f);
979
0
    uint8_t g1 = STATIC_CAST(
980
0
        uint8_t, (src_argb1555[2] >> 5) | ((src_argb1555[3] & 0x03) << 3));
981
0
    uint8_t r1 = STATIC_CAST(uint8_t, (src_argb1555[3] & 0x7c) >> 2);
982
0
    uint8_t b2 = STATIC_CAST(uint8_t, next_argb1555[0] & 0x1f);
983
0
    uint8_t g2 = STATIC_CAST(
984
0
        uint8_t, (next_argb1555[0] >> 5) | ((next_argb1555[1] & 0x03) << 3));
985
0
    uint8_t r2 = STATIC_CAST(uint8_t, (next_argb1555[1] & 0x7c) >> 2);
986
0
    uint8_t b3 = STATIC_CAST(uint8_t, next_argb1555[2] & 0x1f);
987
0
    uint8_t g3 = STATIC_CAST(
988
0
        uint8_t, (next_argb1555[2] >> 5) | ((next_argb1555[3] & 0x03) << 3));
989
0
    uint8_t r3 = STATIC_CAST(uint8_t, (next_argb1555[3] & 0x7c) >> 2);
990
991
0
    b0 = STATIC_CAST(uint8_t, (b0 << 3) | (b0 >> 2));
992
0
    g0 = STATIC_CAST(uint8_t, (g0 << 3) | (g0 >> 2));
993
0
    r0 = STATIC_CAST(uint8_t, (r0 << 3) | (r0 >> 2));
994
0
    b1 = STATIC_CAST(uint8_t, (b1 << 3) | (b1 >> 2));
995
0
    g1 = STATIC_CAST(uint8_t, (g1 << 3) | (g1 >> 2));
996
0
    r1 = STATIC_CAST(uint8_t, (r1 << 3) | (r1 >> 2));
997
0
    b2 = STATIC_CAST(uint8_t, (b2 << 3) | (b2 >> 2));
998
0
    g2 = STATIC_CAST(uint8_t, (g2 << 3) | (g2 >> 2));
999
0
    r2 = STATIC_CAST(uint8_t, (r2 << 3) | (r2 >> 2));
1000
0
    b3 = STATIC_CAST(uint8_t, (b3 << 3) | (b3 >> 2));
1001
0
    g3 = STATIC_CAST(uint8_t, (g3 << 3) | (g3 >> 2));
1002
0
    r3 = STATIC_CAST(uint8_t, (r3 << 3) | (r3 >> 2));
1003
1004
0
    uint8_t b = (b0 + b1 + b2 + b3 + 2) >> 2;
1005
0
    uint8_t g = (g0 + g1 + g2 + g3 + 2) >> 2;
1006
0
    uint8_t r = (r0 + r1 + r2 + r3 + 2) >> 2;
1007
0
    dst_u[0] = RGBToU(r, g, b);
1008
0
    dst_v[0] = RGBToV(r, g, b);
1009
1010
0
    src_argb1555 += 4;
1011
0
    next_argb1555 += 4;
1012
0
    dst_u += 1;
1013
0
    dst_v += 1;
1014
0
  }
1015
0
  if (width & 1) {
1016
0
    uint8_t b0 = STATIC_CAST(uint8_t, src_argb1555[0] & 0x1f);
1017
0
    uint8_t g0 = STATIC_CAST(
1018
0
        uint8_t, (src_argb1555[0] >> 5) | ((src_argb1555[1] & 0x03) << 3));
1019
0
    uint8_t r0 = STATIC_CAST(uint8_t, (src_argb1555[1] & 0x7c) >> 2);
1020
0
    uint8_t b2 = STATIC_CAST(uint8_t, next_argb1555[0] & 0x1f);
1021
0
    uint8_t g2 = STATIC_CAST(
1022
0
        uint8_t, (next_argb1555[0] >> 5) | ((next_argb1555[1] & 0x03) << 3));
1023
0
    uint8_t r2 = STATIC_CAST(uint8_t, (next_argb1555[1] & 0x7c) >> 2);
1024
1025
0
    b0 = STATIC_CAST(uint8_t, (b0 << 3) | (b0 >> 2));
1026
0
    g0 = STATIC_CAST(uint8_t, (g0 << 3) | (g0 >> 2));
1027
0
    r0 = STATIC_CAST(uint8_t, (r0 << 3) | (r0 >> 2));
1028
0
    b2 = STATIC_CAST(uint8_t, (b2 << 3) | (b2 >> 2));
1029
0
    g2 = STATIC_CAST(uint8_t, (g2 << 3) | (g2 >> 2));
1030
0
    r2 = STATIC_CAST(uint8_t, (r2 << 3) | (r2 >> 2));
1031
1032
0
    uint8_t ab = AVGB(b0, b2);
1033
0
    uint8_t ag = AVGB(g0, g2);
1034
0
    uint8_t ar = AVGB(r0, r2);
1035
0
    dst_u[0] = RGBToU(ar, ag, ab);
1036
0
    dst_v[0] = RGBToV(ar, ag, ab);
1037
0
  }
1038
0
}
1039
1040
void ARGB4444ToUVRow_C(const uint8_t* src_argb4444,
1041
                       int src_stride_argb4444,
1042
                       uint8_t* dst_u,
1043
                       uint8_t* dst_v,
1044
0
                       int width) {
1045
0
  const uint8_t* next_argb4444 = src_argb4444 + src_stride_argb4444;
1046
0
  int x;
1047
0
  for (x = 0; x < width - 1; x += 2) {
1048
0
    uint8_t b0 = src_argb4444[0] & 0x0f;
1049
0
    uint8_t g0 = src_argb4444[0] >> 4;
1050
0
    uint8_t r0 = src_argb4444[1] & 0x0f;
1051
0
    uint8_t b1 = src_argb4444[2] & 0x0f;
1052
0
    uint8_t g1 = src_argb4444[2] >> 4;
1053
0
    uint8_t r1 = src_argb4444[3] & 0x0f;
1054
0
    uint8_t b2 = next_argb4444[0] & 0x0f;
1055
0
    uint8_t g2 = next_argb4444[0] >> 4;
1056
0
    uint8_t r2 = next_argb4444[1] & 0x0f;
1057
0
    uint8_t b3 = next_argb4444[2] & 0x0f;
1058
0
    uint8_t g3 = next_argb4444[2] >> 4;
1059
0
    uint8_t r3 = next_argb4444[3] & 0x0f;
1060
1061
0
    b0 = STATIC_CAST(uint8_t, (b0 << 4) | b0);
1062
0
    g0 = STATIC_CAST(uint8_t, (g0 << 4) | g0);
1063
0
    r0 = STATIC_CAST(uint8_t, (r0 << 4) | r0);
1064
0
    b1 = STATIC_CAST(uint8_t, (b1 << 4) | b1);
1065
0
    g1 = STATIC_CAST(uint8_t, (g1 << 4) | g1);
1066
0
    r1 = STATIC_CAST(uint8_t, (r1 << 4) | r1);
1067
0
    b2 = STATIC_CAST(uint8_t, (b2 << 4) | b2);
1068
0
    g2 = STATIC_CAST(uint8_t, (g2 << 4) | g2);
1069
0
    r2 = STATIC_CAST(uint8_t, (r2 << 4) | r2);
1070
0
    b3 = STATIC_CAST(uint8_t, (b3 << 4) | b3);
1071
0
    g3 = STATIC_CAST(uint8_t, (g3 << 4) | g3);
1072
0
    r3 = STATIC_CAST(uint8_t, (r3 << 4) | r3);
1073
1074
0
    uint8_t b = (b0 + b1 + b2 + b3 + 2) >> 2;
1075
0
    uint8_t g = (g0 + g1 + g2 + g3 + 2) >> 2;
1076
0
    uint8_t r = (r0 + r1 + r2 + r3 + 2) >> 2;
1077
0
    dst_u[0] = RGBToU(r, g, b);
1078
0
    dst_v[0] = RGBToV(r, g, b);
1079
1080
0
    src_argb4444 += 4;
1081
0
    next_argb4444 += 4;
1082
0
    dst_u += 1;
1083
0
    dst_v += 1;
1084
0
  }
1085
0
  if (width & 1) {
1086
0
    uint8_t b0 = src_argb4444[0] & 0x0f;
1087
0
    uint8_t g0 = src_argb4444[0] >> 4;
1088
0
    uint8_t r0 = src_argb4444[1] & 0x0f;
1089
0
    uint8_t b2 = next_argb4444[0] & 0x0f;
1090
0
    uint8_t g2 = next_argb4444[0] >> 4;
1091
0
    uint8_t r2 = next_argb4444[1] & 0x0f;
1092
1093
0
    b0 = STATIC_CAST(uint8_t, (b0 << 4) | b0);
1094
0
    g0 = STATIC_CAST(uint8_t, (g0 << 4) | g0);
1095
0
    r0 = STATIC_CAST(uint8_t, (r0 << 4) | r0);
1096
0
    b2 = STATIC_CAST(uint8_t, (b2 << 4) | b2);
1097
0
    g2 = STATIC_CAST(uint8_t, (g2 << 4) | g2);
1098
0
    r2 = STATIC_CAST(uint8_t, (r2 << 4) | r2);
1099
1100
0
    uint8_t ab = AVGB(b0, b2);
1101
0
    uint8_t ag = AVGB(g0, g2);
1102
0
    uint8_t ar = AVGB(r0, r2);
1103
0
    dst_u[0] = RGBToU(ar, ag, ab);
1104
0
    dst_v[0] = RGBToV(ar, ag, ab);
1105
0
  }
1106
0
}
1107
1108
0
void ARGBGrayRow_C(const uint8_t* src_argb, uint8_t* dst_argb, int width) {
1109
0
  int x;
1110
0
  for (x = 0; x < width; ++x) {
1111
0
    uint8_t y = RGBToYJ(src_argb[2], src_argb[1], src_argb[0]);
1112
0
    dst_argb[2] = dst_argb[1] = dst_argb[0] = y;
1113
0
    dst_argb[3] = src_argb[3];
1114
0
    dst_argb += 4;
1115
0
    src_argb += 4;
1116
0
  }
1117
0
}
1118
1119
// Convert a row of image to Sepia tone.
1120
0
void ARGBSepiaRow_C(uint8_t* dst_argb, int width) {
1121
0
  int x;
1122
0
  for (x = 0; x < width; ++x) {
1123
0
    int b = dst_argb[0];
1124
0
    int g = dst_argb[1];
1125
0
    int r = dst_argb[2];
1126
0
    int sb = (b * 17 + g * 68 + r * 35) >> 7;
1127
0
    int sg = (b * 22 + g * 88 + r * 45) >> 7;
1128
0
    int sr = (b * 24 + g * 98 + r * 50) >> 7;
1129
    // b does not over flow. a is preserved from original.
1130
0
    dst_argb[0] = STATIC_CAST(uint8_t, sb);
1131
0
    dst_argb[1] = STATIC_CAST(uint8_t, clamp255(sg));
1132
0
    dst_argb[2] = STATIC_CAST(uint8_t, clamp255(sr));
1133
0
    dst_argb += 4;
1134
0
  }
1135
0
}
1136
1137
// Apply color matrix to a row of image. Matrix is signed.
1138
// TODO(fbarchard): Consider adding rounding (+32).
1139
void ARGBColorMatrixRow_C(const uint8_t* src_argb,
1140
                          uint8_t* dst_argb,
1141
                          const int8_t* matrix_argb,
1142
0
                          int width) {
1143
0
  int x;
1144
0
  for (x = 0; x < width; ++x) {
1145
0
    int b = src_argb[0];
1146
0
    int g = src_argb[1];
1147
0
    int r = src_argb[2];
1148
0
    int a = src_argb[3];
1149
0
    int sb = (b * matrix_argb[0] + g * matrix_argb[1] + r * matrix_argb[2] +
1150
0
              a * matrix_argb[3]) >>
1151
0
             6;
1152
0
    int sg = (b * matrix_argb[4] + g * matrix_argb[5] + r * matrix_argb[6] +
1153
0
              a * matrix_argb[7]) >>
1154
0
             6;
1155
0
    int sr = (b * matrix_argb[8] + g * matrix_argb[9] + r * matrix_argb[10] +
1156
0
              a * matrix_argb[11]) >>
1157
0
             6;
1158
0
    int sa = (b * matrix_argb[12] + g * matrix_argb[13] + r * matrix_argb[14] +
1159
0
              a * matrix_argb[15]) >>
1160
0
             6;
1161
0
    dst_argb[0] = STATIC_CAST(uint8_t, Clamp(sb));
1162
0
    dst_argb[1] = STATIC_CAST(uint8_t, Clamp(sg));
1163
0
    dst_argb[2] = STATIC_CAST(uint8_t, Clamp(sr));
1164
0
    dst_argb[3] = STATIC_CAST(uint8_t, Clamp(sa));
1165
0
    src_argb += 4;
1166
0
    dst_argb += 4;
1167
0
  }
1168
0
}
1169
1170
// Apply color table to a row of image.
1171
void ARGBColorTableRow_C(uint8_t* dst_argb,
1172
                         const uint8_t* table_argb,
1173
0
                         int width) {
1174
0
  int x;
1175
0
  for (x = 0; x < width; ++x) {
1176
0
    int b = dst_argb[0];
1177
0
    int g = dst_argb[1];
1178
0
    int r = dst_argb[2];
1179
0
    int a = dst_argb[3];
1180
0
    dst_argb[0] = table_argb[b * 4 + 0];
1181
0
    dst_argb[1] = table_argb[g * 4 + 1];
1182
0
    dst_argb[2] = table_argb[r * 4 + 2];
1183
0
    dst_argb[3] = table_argb[a * 4 + 3];
1184
0
    dst_argb += 4;
1185
0
  }
1186
0
}
1187
1188
// Apply color table to a row of image.
1189
void RGBColorTableRow_C(uint8_t* dst_argb,
1190
                        const uint8_t* table_argb,
1191
0
                        int width) {
1192
0
  int x;
1193
0
  for (x = 0; x < width; ++x) {
1194
0
    int b = dst_argb[0];
1195
0
    int g = dst_argb[1];
1196
0
    int r = dst_argb[2];
1197
0
    dst_argb[0] = table_argb[b * 4 + 0];
1198
0
    dst_argb[1] = table_argb[g * 4 + 1];
1199
0
    dst_argb[2] = table_argb[r * 4 + 2];
1200
0
    dst_argb += 4;
1201
0
  }
1202
0
}
1203
1204
void ARGBQuantizeRow_C(uint8_t* dst_argb,
1205
                       int scale,
1206
                       int interval_size,
1207
                       int interval_offset,
1208
0
                       int width) {
1209
0
  int x;
1210
0
  for (x = 0; x < width; ++x) {
1211
0
    int b = dst_argb[0];
1212
0
    int g = dst_argb[1];
1213
0
    int r = dst_argb[2];
1214
0
    dst_argb[0] = STATIC_CAST(
1215
0
        uint8_t, (b * scale >> 16) * interval_size + interval_offset);
1216
0
    dst_argb[1] = STATIC_CAST(
1217
0
        uint8_t, (g * scale >> 16) * interval_size + interval_offset);
1218
0
    dst_argb[2] = STATIC_CAST(
1219
0
        uint8_t, (r * scale >> 16) * interval_size + interval_offset);
1220
0
    dst_argb += 4;
1221
0
  }
1222
0
}
1223
1224
0
#define REPEAT8(v) (v) | ((v) << 8)
1225
0
#define SHADE(f, v) v* f >> 24
1226
1227
void ARGBShadeRow_C(const uint8_t* src_argb,
1228
                    uint8_t* dst_argb,
1229
                    int width,
1230
0
                    uint32_t value) {
1231
0
  const uint32_t b_scale = REPEAT8(value & 0xff);
1232
0
  const uint32_t g_scale = REPEAT8((value >> 8) & 0xff);
1233
0
  const uint32_t r_scale = REPEAT8((value >> 16) & 0xff);
1234
0
  const uint32_t a_scale = REPEAT8(value >> 24);
1235
1236
0
  int i;
1237
0
  for (i = 0; i < width; ++i) {
1238
0
    const uint32_t b = REPEAT8(src_argb[0]);
1239
0
    const uint32_t g = REPEAT8(src_argb[1]);
1240
0
    const uint32_t r = REPEAT8(src_argb[2]);
1241
0
    const uint32_t a = REPEAT8(src_argb[3]);
1242
0
    dst_argb[0] = SHADE(b, b_scale);
1243
0
    dst_argb[1] = SHADE(g, g_scale);
1244
0
    dst_argb[2] = SHADE(r, r_scale);
1245
0
    dst_argb[3] = SHADE(a, a_scale);
1246
0
    src_argb += 4;
1247
0
    dst_argb += 4;
1248
0
  }
1249
0
}
1250
#undef REPEAT8
1251
#undef SHADE
1252
1253
void ARGBMultiplyRow_C(const uint8_t* src_argb,
1254
                       const uint8_t* src_argb1,
1255
                       uint8_t* dst_argb,
1256
0
                       int width) {
1257
0
  int i;
1258
0
  for (i = 0; i < width; ++i) {
1259
0
    const uint32_t b = src_argb[0];
1260
0
    const uint32_t g = src_argb[1];
1261
0
    const uint32_t r = src_argb[2];
1262
0
    const uint32_t a = src_argb[3];
1263
0
    const uint32_t b_scale = src_argb1[0];
1264
0
    const uint32_t g_scale = src_argb1[1];
1265
0
    const uint32_t r_scale = src_argb1[2];
1266
0
    const uint32_t a_scale = src_argb1[3];
1267
0
    dst_argb[0] = STATIC_CAST(uint8_t, (b * b_scale + 128) >> 8);
1268
0
    dst_argb[1] = STATIC_CAST(uint8_t, (g * g_scale + 128) >> 8);
1269
0
    dst_argb[2] = STATIC_CAST(uint8_t, (r * r_scale + 128) >> 8);
1270
0
    dst_argb[3] = STATIC_CAST(uint8_t, (a * a_scale + 128) >> 8);
1271
0
    src_argb += 4;
1272
0
    src_argb1 += 4;
1273
0
    dst_argb += 4;
1274
0
  }
1275
0
}
1276
1277
#define SHADE(f, v) clamp255(v + f)
1278
1279
void ARGBAddRow_C(const uint8_t* src_argb,
1280
                  const uint8_t* src_argb1,
1281
                  uint8_t* dst_argb,
1282
0
                  int width) {
1283
0
  int i;
1284
0
  for (i = 0; i < width; ++i) {
1285
0
    const int b = src_argb[0];
1286
0
    const int g = src_argb[1];
1287
0
    const int r = src_argb[2];
1288
0
    const int a = src_argb[3];
1289
0
    const int b_add = src_argb1[0];
1290
0
    const int g_add = src_argb1[1];
1291
0
    const int r_add = src_argb1[2];
1292
0
    const int a_add = src_argb1[3];
1293
0
    dst_argb[0] = STATIC_CAST(uint8_t, SHADE(b, b_add));
1294
0
    dst_argb[1] = STATIC_CAST(uint8_t, SHADE(g, g_add));
1295
0
    dst_argb[2] = STATIC_CAST(uint8_t, SHADE(r, r_add));
1296
0
    dst_argb[3] = STATIC_CAST(uint8_t, SHADE(a, a_add));
1297
0
    src_argb += 4;
1298
0
    src_argb1 += 4;
1299
0
    dst_argb += 4;
1300
0
  }
1301
0
}
1302
#undef SHADE
1303
1304
#define SHADE(f, v) clamp0(f - v)
1305
1306
void ARGBSubtractRow_C(const uint8_t* src_argb,
1307
                       const uint8_t* src_argb1,
1308
                       uint8_t* dst_argb,
1309
0
                       int width) {
1310
0
  int i;
1311
0
  for (i = 0; i < width; ++i) {
1312
0
    const int b = src_argb[0];
1313
0
    const int g = src_argb[1];
1314
0
    const int r = src_argb[2];
1315
0
    const int a = src_argb[3];
1316
0
    const int b_sub = src_argb1[0];
1317
0
    const int g_sub = src_argb1[1];
1318
0
    const int r_sub = src_argb1[2];
1319
0
    const int a_sub = src_argb1[3];
1320
0
    dst_argb[0] = STATIC_CAST(uint8_t, SHADE(b, b_sub));
1321
0
    dst_argb[1] = STATIC_CAST(uint8_t, SHADE(g, g_sub));
1322
0
    dst_argb[2] = STATIC_CAST(uint8_t, SHADE(r, r_sub));
1323
0
    dst_argb[3] = STATIC_CAST(uint8_t, SHADE(a, a_sub));
1324
0
    src_argb += 4;
1325
0
    src_argb1 += 4;
1326
0
    dst_argb += 4;
1327
0
  }
1328
0
}
1329
#undef SHADE
1330
1331
// Sobel functions which mimics SSSE3.
1332
void SobelXRow_C(const uint8_t* src_y0,
1333
                 const uint8_t* src_y1,
1334
                 const uint8_t* src_y2,
1335
                 uint8_t* dst_sobelx,
1336
0
                 int width) {
1337
0
  int i;
1338
0
  for (i = 0; i < width; ++i) {
1339
0
    int a = src_y0[i];
1340
0
    int b = src_y1[i];
1341
0
    int c = src_y2[i];
1342
0
    int a_sub = src_y0[i + 2];
1343
0
    int b_sub = src_y1[i + 2];
1344
0
    int c_sub = src_y2[i + 2];
1345
0
    int a_diff = a - a_sub;
1346
0
    int b_diff = b - b_sub;
1347
0
    int c_diff = c - c_sub;
1348
0
    int sobel = Abs(a_diff + b_diff * 2 + c_diff);
1349
0
    dst_sobelx[i] = (uint8_t)(clamp255(sobel));
1350
0
  }
1351
0
}
1352
1353
void SobelYRow_C(const uint8_t* src_y0,
1354
                 const uint8_t* src_y1,
1355
                 uint8_t* dst_sobely,
1356
0
                 int width) {
1357
0
  int i;
1358
0
  for (i = 0; i < width; ++i) {
1359
0
    int a = src_y0[i + 0];
1360
0
    int b = src_y0[i + 1];
1361
0
    int c = src_y0[i + 2];
1362
0
    int a_sub = src_y1[i + 0];
1363
0
    int b_sub = src_y1[i + 1];
1364
0
    int c_sub = src_y1[i + 2];
1365
0
    int a_diff = a - a_sub;
1366
0
    int b_diff = b - b_sub;
1367
0
    int c_diff = c - c_sub;
1368
0
    int sobel = Abs(a_diff + b_diff * 2 + c_diff);
1369
0
    dst_sobely[i] = (uint8_t)(clamp255(sobel));
1370
0
  }
1371
0
}
1372
1373
void SobelRow_C(const uint8_t* src_sobelx,
1374
                const uint8_t* src_sobely,
1375
                uint8_t* dst_argb,
1376
0
                int width) {
1377
0
  int i;
1378
0
  for (i = 0; i < width; ++i) {
1379
0
    int r = src_sobelx[i];
1380
0
    int b = src_sobely[i];
1381
0
    int s = clamp255(r + b);
1382
0
    dst_argb[0] = (uint8_t)(s);
1383
0
    dst_argb[1] = (uint8_t)(s);
1384
0
    dst_argb[2] = (uint8_t)(s);
1385
0
    dst_argb[3] = (uint8_t)(255u);
1386
0
    dst_argb += 4;
1387
0
  }
1388
0
}
1389
1390
void SobelToPlaneRow_C(const uint8_t* src_sobelx,
1391
                       const uint8_t* src_sobely,
1392
                       uint8_t* dst_y,
1393
0
                       int width) {
1394
0
  int i;
1395
0
  for (i = 0; i < width; ++i) {
1396
0
    int r = src_sobelx[i];
1397
0
    int b = src_sobely[i];
1398
0
    int s = clamp255(r + b);
1399
0
    dst_y[i] = (uint8_t)(s);
1400
0
  }
1401
0
}
1402
1403
void SobelXYRow_C(const uint8_t* src_sobelx,
1404
                  const uint8_t* src_sobely,
1405
                  uint8_t* dst_argb,
1406
0
                  int width) {
1407
0
  int i;
1408
0
  for (i = 0; i < width; ++i) {
1409
0
    int r = src_sobelx[i];
1410
0
    int b = src_sobely[i];
1411
0
    int g = clamp255(r + b);
1412
0
    dst_argb[0] = (uint8_t)(b);
1413
0
    dst_argb[1] = (uint8_t)(g);
1414
0
    dst_argb[2] = (uint8_t)(r);
1415
0
    dst_argb[3] = (uint8_t)(255u);
1416
0
    dst_argb += 4;
1417
0
  }
1418
0
}
1419
1420
0
void J400ToARGBRow_C(const uint8_t* src_y, uint8_t* dst_argb, int width) {
1421
  // Copy a Y to RGB.
1422
0
  int x;
1423
0
  for (x = 0; x < width; ++x) {
1424
0
    uint8_t y = src_y[0];
1425
0
    dst_argb[2] = dst_argb[1] = dst_argb[0] = y;
1426
0
    dst_argb[3] = 255u;
1427
0
    dst_argb += 4;
1428
0
    ++src_y;
1429
0
  }
1430
0
}
1431
1432
// Macros to create SIMD specific yuv to rgb conversion constants.
1433
1434
// clang-format off
1435
1436
#if defined(__aarch64__) || defined(__arm__) || defined(__riscv)
1437
// Bias values include subtract 128 from U and V, bias from Y and rounding.
1438
// For B and R bias is negative. For G bias is positive.
1439
#define YUVCONSTANTSBODY(YG, YB, UB, UG, VG, VR)                             \
1440
  {{UB, VR, UG, VG, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},                     \
1441
   {YG, (UB * 128 - YB), (UG * 128 + VG * 128 + YB), (VR * 128 - YB), YB, 0, \
1442
    0, 0}}
1443
#else
1444
#define YUVCONSTANTSBODY(YG, YB, UB, UG, VG, VR)                     \
1445
  {{UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0,          \
1446
    UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0},         \
1447
   {UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG,  \
1448
    UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG}, \
1449
   {0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR,          \
1450
    0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR},         \
1451
   {YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG}, \
1452
   {YB, YB, YB, YB, YB, YB, YB, YB, YB, YB, YB, YB, YB, YB, YB, YB}}
1453
#endif
1454
1455
#if defined(__aarch64__) || defined(__arm__) || defined(__riscv)
1456
#define ARGBCONSTANTSBODY(Y0, Y1, Y2, Y3, U0, U1, U2, U3, V0, V1, V2, V3, AY, \
1457
                          AUV)                                                \
1458
  {{Y0, Y1, Y2, Y3, Y0, Y1, Y2, Y3, Y0, Y1, Y2, Y3, Y0, Y1, Y2, Y3},          \
1459
   {U0, U1, U2, U3, U0, U1, U2, U3, U0, U1, U2, U3, U0, U1, U2, U3},          \
1460
   {V0, V1, V2, V3, V0, V1, V2, V3, V0, V1, V2, V3, V0, V1, V2, V3},          \
1461
   {AY, AY, AY, AY, AY, AY, AY, AY},                                          \
1462
   {AUV, AUV, AUV, AUV, AUV, AUV, AUV, AUV}}
1463
#else
1464
#define ARGBCONSTANTSBODY(Y0, Y1, Y2, Y3, U0, U1, U2, U3, V0, V1, V2, V3, AY, \
1465
                          AUV)                                                \
1466
  {{Y0, Y1, Y2, Y3, Y0, Y1, Y2, Y3, Y0, Y1, Y2, Y3, Y0, Y1, Y2, Y3,           \
1467
    Y0, Y1, Y2, Y3, Y0, Y1, Y2, Y3, Y0, Y1, Y2, Y3, Y0, Y1, Y2, Y3},          \
1468
   {U0, U1, U2, U3, U0, U1, U2, U3, U0, U1, U2, U3, U0, U1, U2, U3,           \
1469
    U0, U1, U2, U3, U0, U1, U2, U3, U0, U1, U2, U3, U0, U1, U2, U3},          \
1470
   {V0, V1, V2, V3, V0, V1, V2, V3, V0, V1, V2, V3, V0, V1, V2, V3,           \
1471
    V0, V1, V2, V3, V0, V1, V2, V3, V0, V1, V2, V3, V0, V1, V2, V3},          \
1472
   {AY, AY, AY, AY, AY, AY, AY, AY, AY, AY, AY, AY, AY, AY, AY, AY},          \
1473
   {AUV, AUV, AUV, AUV, AUV, AUV, AUV, AUV, AUV, AUV, AUV, AUV, AUV, AUV,     \
1474
    AUV, AUV}}
1475
#endif
1476
1477
// clang-format on
1478
1479
#define MAKEYUVCONSTANTS(name, YG, YB, UB, UG, VG, VR)            \
1480
  const struct YuvConstants SIMD_ALIGNED(kYuv##name##Constants) = \
1481
      YUVCONSTANTSBODY(YG, YB, UB, UG, VG, VR);                   \
1482
  const struct YuvConstants SIMD_ALIGNED(kYvu##name##Constants) = \
1483
      YUVCONSTANTSBODY(YG, YB, VR, VG, UG, UB);
1484
1485
#define MAKEARGBCONSTANTS(name, RY, GY, BY, RU, GU, BU, RV, GV, BV, AY, AUV) \
1486
  extern const struct ArgbConstants SIMD_ALIGNED(kArgb##name##Constants) =   \
1487
      ARGBCONSTANTSBODY(BY, GY, RY, 0, -(BU), -(GU), -(RU), 0, -(BV), -(GV), \
1488
                        -(RV), 0, AY, AUV);                                  \
1489
  extern const struct ArgbConstants SIMD_ALIGNED(kAbgr##name##Constants) =   \
1490
      ARGBCONSTANTSBODY(RY, GY, BY, 0, -(RU), -(GU), -(BU), 0, -(RV), -(GV), \
1491
                        -(BV), 0, AY, AUV);                                  \
1492
  extern const struct ArgbConstants SIMD_ALIGNED(kRgba##name##Constants) =   \
1493
      ARGBCONSTANTSBODY(0, BY, GY, RY, 0, -(BU), -(GU), -(RU), 0, -(BV),     \
1494
                        -(GV), -(RV), AY, AUV);                              \
1495
  extern const struct ArgbConstants SIMD_ALIGNED(kBgra##name##Constants) =   \
1496
      ARGBCONSTANTSBODY(0, RY, GY, BY, 0, -(RU), -(GU), -(BU), 0, -(RV),     \
1497
                        -(GV), -(BV), AY, AUV);
1498
1499
// BT.601 limited range RGB to YUV coefficients
1500
// RY = round(0.299 * 219 / 255 * 256) = 66
1501
// GY = round(0.587 * 219 / 255 * 256) = 129
1502
// BY = round(0.114 * 219 / 255 * 256) = 25
1503
// BU = round(0.500 * 224 / 255 * 256) = 112
1504
// RU = round(-0.299 / (1 - 0.114) * 112.4) = -38
1505
// GU = round(-0.587 / (1 - 0.114) * 112.4) = -74
1506
// RV = 112
1507
// GV = round(-0.587 / (1 - 0.299) * 112.4) = -94
1508
// BV = round(-0.114 / (1 - 0.299) * 112.4) = -18
1509
// AY = 16 * 256 + 128 = 4224
1510
// AUV = 128 * 256 = 32768
1511
MAKEARGBCONSTANTS(I601, 66, 129, 25, -38, -74, 112, 112, -94, -18, 4224, 32768)
1512
1513
// BT.601 full range RGB to YUV coefficients (aka JPEG)
1514
// RY = round(0.299 * 256) = 77
1515
// GY = round(0.587 * 256) = 150
1516
// BY = round(0.114 * 256) = 29
1517
// BU = 128
1518
// RU = round(-0.299 / (1 - 0.114) * 128) = -43
1519
// GU = round(-0.587 / (1 - 0.114) * 128) = -85
1520
// RV = 128
1521
// GV = round(-0.587 / (1 - 0.299) * 128) = -107
1522
// BV = round(-0.114 / (1 - 0.299) * 128) = -21
1523
// AY = 128
1524
// AUV = 32768
1525
MAKEARGBCONSTANTS(JPEG, 77, 150, 29, -43, -85, 128, 128, -107, -21, 128, 32768)
1526
1527
// BT.709 limited range RGB to YUV coefficients
1528
// RY = round(0.2126 * 219 / 255 * 256) = 47
1529
// GY = round(0.7152 * 219 / 255 * 256) = 157
1530
// BY = round(0.0722 * 219 / 255 * 256) = 16
1531
// BU = round(0.500 * 224 / 255 * 256) = 112
1532
// RU = round(-0.2126 / (1 - 0.0722) * 112.4) = -26
1533
// GU = round(-0.7152 / (1 - 0.0722) * 112.4) = -86
1534
// RV = 112
1535
// GV = round(-0.7152 / (1 - 0.2126) * 112.4) = -102
1536
// BV = round(-0.0722 / (1 - 0.2126) * 112.4) = -10
1537
// AY = 16 * 256 + 128 = 4224
1538
// AUV = 128 * 256 = 32768
1539
MAKEARGBCONSTANTS(H709, 47, 157, 16, -26, -86, 112, 112, -102, -10, 4224, 32768)
1540
1541
// BT.709 full range RGB to YUV coefficients
1542
// RY = round(0.2126 * 256) = 54
1543
// GY = round(0.7152 * 256) = 183
1544
// BY = round(0.0722 * 256) = 19
1545
// BU = 128
1546
// RU = round(-0.2126 / (1 - 0.0722) * 128) = -29
1547
// GU = round(-0.7152 / (1 - 0.0722) * 128) = -99
1548
// RV = 128
1549
// GV = round(-0.7152 / (1 - 0.2126) * 128) = -116
1550
// BV = round(-0.0722 / (1 - 0.2126) * 128) = -12
1551
// AY = 128
1552
// AUV = 32768
1553
MAKEARGBCONSTANTS(F709, 54, 183, 19, -29, -99, 128, 128, -116, -12, 128, 32768)
1554
1555
// BT.2020 limited range RGB to YUV coefficients
1556
// RY = round(0.2627 * 219 / 255 * 256) = 58
1557
// GY = round(0.6780 * 219 / 255 * 256) = 149
1558
// BY = round(0.0593 * 219 / 255 * 256) = 13
1559
// BU = 112
1560
// RU = round(-0.2627 / (1 - 0.0593) * 112.4) = -31
1561
// GU = round(-0.6780 / (1 - 0.0593) * 112.4) = -81
1562
// RV = 112
1563
// GV = round(-0.6780 / (1 - 0.2627) * 112.4) = -103
1564
// BV = round(-0.0593 / (1 - 0.2627) * 112.4) = -9
1565
// AY = 16 * 256 + 128 = 4224
1566
// AUV = 128 * 256 = 32768
1567
MAKEARGBCONSTANTS(U2020, 59, 148, 13, -31, -81, 112, 112, -103, -9, 4224, 32768)
1568
1569
// BT.2020 full range RGB to YUV coefficients
1570
// RY = round(0.2627 * 256) = 67
1571
// GY = round(0.6780 * 256) = 174
1572
// BY = round(0.0593 * 256) = 15
1573
// BU = 128
1574
// RU = round(-0.2627 / (1 - 0.0593) * 128) = -36
1575
// GU = round(-0.6780 / (1 - 0.0593) * 128) = -92
1576
// RV = 128
1577
// GV = round(-0.6780 / (1 - 0.2627) * 128) = -118
1578
// BV = round(-0.0593 / (1 - 0.2627) * 128) = -10
1579
// AY = 128
1580
// AUV = 32768
1581
MAKEARGBCONSTANTS(V2020, 67, 174, 15, -36, -92, 128, 128, -118, -10, 128, 32768)
1582
1583
// TODO(fbarchard): Generate SIMD structures from float matrix.
1584
1585
// BT.601 limited range YUV to RGB reference
1586
//  R = (Y - 16) * 1.164             + V * 1.596
1587
//  G = (Y - 16) * 1.164 - U * 0.391 - V * 0.813
1588
//  B = (Y - 16) * 1.164 + U * 2.018
1589
// KR = 0.299; KB = 0.114
1590
1591
// U and V contributions to R,G,B.
1592
#if defined(LIBYUV_UNLIMITED_DATA) || defined(LIBYUV_UNLIMITED_BT601)
1593
#define UB 129 /* round(2.018 * 64) */
1594
#else
1595
#define UB 128 /* max(128, round(2.018 * 64)) */
1596
#endif
1597
#define UG 25  /* round(0.391 * 64) */
1598
#define VG 52  /* round(0.813 * 64) */
1599
#define VR 102 /* round(1.596 * 64) */
1600
1601
// Y contribution to R,G,B.  Scale and bias.
1602
#define YG 18997 /* round(1.164 * 64 * 256 * 256 / 257) */
1603
#define YB -1160 /* 1.164 * 64 * -16 + 64 / 2 */
1604
1605
MAKEYUVCONSTANTS(I601, YG, YB, UB, UG, VG, VR)
1606
1607
#undef YG
1608
#undef YB
1609
#undef UB
1610
#undef UG
1611
#undef VG
1612
#undef VR
1613
1614
// BT.601 full range YUV to RGB reference (aka JPEG)
1615
// *  R = Y               + V * 1.40200
1616
// *  G = Y - U * 0.34414 - V * 0.71414
1617
// *  B = Y + U * 1.77200
1618
// KR = 0.299; KB = 0.114
1619
1620
// U and V contributions to R,G,B.
1621
#define UB 113 /* round(1.77200 * 64) */
1622
#define UG 22  /* round(0.34414 * 64) */
1623
#define VG 46  /* round(0.71414 * 64) */
1624
#define VR 90  /* round(1.40200 * 64) */
1625
1626
// Y contribution to R,G,B.  Scale and bias.
1627
#define YG 16320 /* round(1.000 * 64 * 256 * 256 / 257) */
1628
#define YB 32    /* 64 / 2 */
1629
1630
MAKEYUVCONSTANTS(JPEG, YG, YB, UB, UG, VG, VR)
1631
1632
#undef YG
1633
#undef YB
1634
#undef UB
1635
#undef UG
1636
#undef VG
1637
#undef VR
1638
1639
// BT.709 limited range YUV to RGB reference
1640
//  R = (Y - 16) * 1.164             + V * 1.793
1641
//  G = (Y - 16) * 1.164 - U * 0.213 - V * 0.533
1642
//  B = (Y - 16) * 1.164 + U * 2.112
1643
//  KR = 0.2126, KB = 0.0722
1644
1645
// U and V contributions to R,G,B.
1646
#if defined(LIBYUV_UNLIMITED_DATA) || defined(LIBYUV_UNLIMITED_BT709)
1647
#define UB 135 /* round(2.112 * 64) */
1648
#else
1649
#define UB 128 /* max(128, round(2.112 * 64)) */
1650
#endif
1651
#define UG 14  /* round(0.213 * 64) */
1652
#define VG 34  /* round(0.533 * 64) */
1653
#define VR 115 /* round(1.793 * 64) */
1654
1655
// Y contribution to R,G,B.  Scale and bias.
1656
#define YG 18997 /* round(1.164 * 64 * 256 * 256 / 257) */
1657
#define YB -1160 /* 1.164 * 64 * -16 + 64 / 2 */
1658
1659
MAKEYUVCONSTANTS(H709, YG, YB, UB, UG, VG, VR)
1660
1661
#undef YG
1662
#undef YB
1663
#undef UB
1664
#undef UG
1665
#undef VG
1666
#undef VR
1667
1668
// BT.709 full range YUV to RGB reference
1669
//  R = Y               + V * 1.5748
1670
//  G = Y - U * 0.18732 - V * 0.46812
1671
//  B = Y + U * 1.8556
1672
//  KR = 0.2126, KB = 0.0722
1673
1674
// U and V contributions to R,G,B.
1675
#define UB 119 /* round(1.8556 * 64) */
1676
#define UG 12  /* round(0.18732 * 64) */
1677
#define VG 30  /* round(0.46812 * 64) */
1678
#define VR 101 /* round(1.5748 * 64) */
1679
1680
// Y contribution to R,G,B.  Scale and bias.  (same as jpeg)
1681
#define YG 16320 /* round(1 * 64 * 256 * 256 / 257) */
1682
#define YB 32    /* 64 / 2 */
1683
1684
MAKEYUVCONSTANTS(F709, YG, YB, UB, UG, VG, VR)
1685
1686
#undef YG
1687
#undef YB
1688
#undef UB
1689
#undef UG
1690
#undef VG
1691
#undef VR
1692
1693
// BT.2020 limited range YUV to RGB reference
1694
//  R = (Y - 16) * 1.164384                + V * 1.67867
1695
//  G = (Y - 16) * 1.164384 - U * 0.187326 - V * 0.65042
1696
//  B = (Y - 16) * 1.164384 + U * 2.14177
1697
// KR = 0.2627; KB = 0.0593
1698
1699
// U and V contributions to R,G,B.
1700
#if defined(LIBYUV_UNLIMITED_DATA) || defined(LIBYUV_UNLIMITED_BT2020)
1701
#define UB 137 /* round(2.142 * 64) */
1702
#else
1703
#define UB 128 /* max(128, round(2.142 * 64)) */
1704
#endif
1705
#define UG 12  /* round(0.187326 * 64) */
1706
#define VG 42  /* round(0.65042 * 64) */
1707
#define VR 107 /* round(1.67867 * 64) */
1708
1709
// Y contribution to R,G,B.  Scale and bias.
1710
#define YG 19003 /* round(1.164384 * 64 * 256 * 256 / 257) */
1711
#define YB -1160 /* 1.164384 * 64 * -16 + 64 / 2 */
1712
1713
MAKEYUVCONSTANTS(2020, YG, YB, UB, UG, VG, VR)
1714
1715
#undef YG
1716
#undef YB
1717
#undef UB
1718
#undef UG
1719
#undef VG
1720
#undef VR
1721
1722
// BT.2020 full range YUV to RGB reference
1723
//  R = Y                + V * 1.474600
1724
//  G = Y - U * 0.164553 - V * 0.571353
1725
//  B = Y + U * 1.881400
1726
// KR = 0.2627; KB = 0.0593
1727
1728
#define UB 120 /* round(1.881400 * 64) */
1729
#define UG 11  /* round(0.164553 * 64) */
1730
#define VG 37  /* round(0.571353 * 64) */
1731
#define VR 94  /* round(1.474600 * 64) */
1732
1733
// Y contribution to R,G,B.  Scale and bias.  (same as jpeg)
1734
#define YG 16320 /* round(1 * 64 * 256 * 256 / 257) */
1735
#define YB 32    /* 64 / 2 */
1736
1737
MAKEYUVCONSTANTS(V2020, YG, YB, UB, UG, VG, VR)
1738
1739
#undef YG
1740
#undef YB
1741
#undef UB
1742
#undef UG
1743
#undef VG
1744
#undef VR
1745
1746
#undef BB
1747
#undef BG
1748
#undef BR
1749
1750
#undef MAKEYUVCONSTANTS
1751
1752
#if defined(__aarch64__) || defined(__arm__) || defined(__riscv)
1753
#define LOAD_YUV_CONSTANTS                 \
1754
  int ub = yuvconstants->kUVCoeff[0];      \
1755
  int vr = yuvconstants->kUVCoeff[1];      \
1756
  int ug = yuvconstants->kUVCoeff[2];      \
1757
  int vg = yuvconstants->kUVCoeff[3];      \
1758
  int yg = yuvconstants->kRGBCoeffBias[0]; \
1759
  int bb = yuvconstants->kRGBCoeffBias[1]; \
1760
  int bg = yuvconstants->kRGBCoeffBias[2]; \
1761
  int br = yuvconstants->kRGBCoeffBias[3]
1762
1763
#define CALC_RGB16                         \
1764
  int32_t y1 = (uint32_t)(y32 * yg) >> 16; \
1765
  int b16 = y1 + (u * ub) - bb;            \
1766
  int g16 = y1 + bg - (u * ug + v * vg);   \
1767
  int r16 = y1 + (v * vr) - br
1768
#else
1769
#define LOAD_YUV_CONSTANTS           \
1770
18.8k
  int ub = yuvconstants->kUVToB[0];  \
1771
18.8k
  int ug = yuvconstants->kUVToG[0];  \
1772
18.8k
  int vg = yuvconstants->kUVToG[1];  \
1773
18.8k
  int vr = yuvconstants->kUVToR[1];  \
1774
18.8k
  int yg = yuvconstants->kYToRgb[0]; \
1775
18.8k
  int yb = yuvconstants->kYBiasToRgb[0]
1776
1777
#define CALC_RGB16                                \
1778
18.8k
  int32_t y1 = ((uint32_t)(y32 * yg) >> 16) + yb; \
1779
18.8k
  int8_t ui = (int8_t)u;                          \
1780
18.8k
  int8_t vi = (int8_t)v;                          \
1781
18.8k
  ui -= 0x80;                                     \
1782
18.8k
  vi -= 0x80;                                     \
1783
18.8k
  int b16 = y1 + (ui * ub);                       \
1784
18.8k
  int g16 = y1 - (ui * ug + vi * vg);             \
1785
18.8k
  int r16 = y1 + (vi * vr)
1786
#endif
1787
1788
// C reference code that mimics the YUV assembly.
1789
// Reads 8 bit YUV and leaves result as 16 bit.
1790
static __inline void YuvPixel(uint8_t y,
1791
                              uint8_t u,
1792
                              uint8_t v,
1793
                              uint8_t* b,
1794
                              uint8_t* g,
1795
                              uint8_t* r,
1796
18.8k
                              const struct YuvConstants* yuvconstants) {
1797
18.8k
  LOAD_YUV_CONSTANTS;
1798
18.8k
  uint32_t y32 = y * 0x0101;
1799
18.8k
  CALC_RGB16;
1800
18.8k
  *b = STATIC_CAST(uint8_t, Clamp((int32_t)(b16) >> 6));
1801
18.8k
  *g = STATIC_CAST(uint8_t, Clamp((int32_t)(g16) >> 6));
1802
18.8k
  *r = STATIC_CAST(uint8_t, Clamp((int32_t)(r16) >> 6));
1803
18.8k
}
1804
1805
// Reads 8 bit YUV and leaves result as 16 bit.
1806
static __inline void YuvPixel8_16(uint8_t y,
1807
                                  uint8_t u,
1808
                                  uint8_t v,
1809
                                  int* b,
1810
                                  int* g,
1811
                                  int* r,
1812
0
                                  const struct YuvConstants* yuvconstants) {
1813
0
  LOAD_YUV_CONSTANTS;
1814
0
  uint32_t y32 = y * 0x0101;
1815
0
  CALC_RGB16;
1816
0
  *b = b16;
1817
0
  *g = g16;
1818
0
  *r = r16;
1819
0
}
1820
1821
// C reference code that mimics the YUV 16 bit assembly.
1822
// Reads 10 bit YUV and leaves result as 16 bit.
1823
static __inline void YuvPixel10_16(uint16_t y,
1824
                                   uint16_t u,
1825
                                   uint16_t v,
1826
                                   int* b,
1827
                                   int* g,
1828
                                   int* r,
1829
0
                                   const struct YuvConstants* yuvconstants) {
1830
0
  LOAD_YUV_CONSTANTS;
1831
0
  uint32_t y32 = (y << 6) | (y >> 4);
1832
0
  u = STATIC_CAST(uint8_t, clamp255(u >> 2));
1833
0
  v = STATIC_CAST(uint8_t, clamp255(v >> 2));
1834
0
  CALC_RGB16;
1835
0
  *b = b16;
1836
0
  *g = g16;
1837
0
  *r = r16;
1838
0
}
1839
1840
// C reference code that mimics the YUV 16 bit assembly.
1841
// Reads 12 bit YUV and leaves result as 16 bit.
1842
static __inline void YuvPixel12_16(int16_t y,
1843
                                   int16_t u,
1844
                                   int16_t v,
1845
                                   int* b,
1846
                                   int* g,
1847
                                   int* r,
1848
0
                                   const struct YuvConstants* yuvconstants) {
1849
0
  LOAD_YUV_CONSTANTS;
1850
0
  uint32_t y32 = (y << 4) | (y >> 8);
1851
0
  u = STATIC_CAST(uint8_t, clamp255(u >> 4));
1852
0
  v = STATIC_CAST(uint8_t, clamp255(v >> 4));
1853
0
  CALC_RGB16;
1854
0
  *b = b16;
1855
0
  *g = g16;
1856
0
  *r = r16;
1857
0
}
1858
1859
// C reference code that mimics the YUV 10 bit assembly.
1860
// Reads 10 bit YUV and clamps down to 8 bit RGB.
1861
static __inline void YuvPixel10(uint16_t y,
1862
                                uint16_t u,
1863
                                uint16_t v,
1864
                                uint8_t* b,
1865
                                uint8_t* g,
1866
                                uint8_t* r,
1867
0
                                const struct YuvConstants* yuvconstants) {
1868
0
  int b16;
1869
0
  int g16;
1870
0
  int r16;
1871
0
  YuvPixel10_16(y, u, v, &b16, &g16, &r16, yuvconstants);
1872
0
  *b = STATIC_CAST(uint8_t, Clamp(b16 >> 6));
1873
0
  *g = STATIC_CAST(uint8_t, Clamp(g16 >> 6));
1874
0
  *r = STATIC_CAST(uint8_t, Clamp(r16 >> 6));
1875
0
}
1876
1877
// C reference code that mimics the YUV 12 bit assembly.
1878
// Reads 12 bit YUV and clamps down to 8 bit RGB.
1879
static __inline void YuvPixel12(uint16_t y,
1880
                                uint16_t u,
1881
                                uint16_t v,
1882
                                uint8_t* b,
1883
                                uint8_t* g,
1884
                                uint8_t* r,
1885
0
                                const struct YuvConstants* yuvconstants) {
1886
0
  int b16;
1887
0
  int g16;
1888
0
  int r16;
1889
0
  YuvPixel12_16(y, u, v, &b16, &g16, &r16, yuvconstants);
1890
0
  *b = STATIC_CAST(uint8_t, Clamp(b16 >> 6));
1891
0
  *g = STATIC_CAST(uint8_t, Clamp(g16 >> 6));
1892
0
  *r = STATIC_CAST(uint8_t, Clamp(r16 >> 6));
1893
0
}
1894
1895
// C reference code that mimics the YUV 16 bit assembly.
1896
// Reads 16 bit YUV and leaves result as 8 bit.
1897
static __inline void YuvPixel16_8(uint16_t y,
1898
                                  uint16_t u,
1899
                                  uint16_t v,
1900
                                  uint8_t* b,
1901
                                  uint8_t* g,
1902
                                  uint8_t* r,
1903
0
                                  const struct YuvConstants* yuvconstants) {
1904
0
  LOAD_YUV_CONSTANTS;
1905
0
  uint32_t y32 = y;
1906
0
  u = STATIC_CAST(uint16_t, clamp255(u >> 8));
1907
0
  v = STATIC_CAST(uint16_t, clamp255(v >> 8));
1908
0
  CALC_RGB16;
1909
0
  *b = STATIC_CAST(uint8_t, Clamp((int32_t)(b16) >> 6));
1910
0
  *g = STATIC_CAST(uint8_t, Clamp((int32_t)(g16) >> 6));
1911
0
  *r = STATIC_CAST(uint8_t, Clamp((int32_t)(r16) >> 6));
1912
0
}
1913
1914
// C reference code that mimics the YUV 16 bit assembly.
1915
// Reads 16 bit YUV and leaves result as 16 bit.
1916
static __inline void YuvPixel16_16(uint16_t y,
1917
                                   uint16_t u,
1918
                                   uint16_t v,
1919
                                   int* b,
1920
                                   int* g,
1921
                                   int* r,
1922
0
                                   const struct YuvConstants* yuvconstants) {
1923
0
  LOAD_YUV_CONSTANTS;
1924
0
  uint32_t y32 = y;
1925
0
  u = STATIC_CAST(uint16_t, clamp255(u >> 8));
1926
0
  v = STATIC_CAST(uint16_t, clamp255(v >> 8));
1927
0
  CALC_RGB16;
1928
0
  *b = b16;
1929
0
  *g = g16;
1930
0
  *r = r16;
1931
0
}
1932
1933
// C reference code that mimics the YUV assembly.
1934
// Reads 8 bit YUV and leaves result as 8 bit.
1935
static __inline void YPixel(uint8_t y,
1936
                            uint8_t* b,
1937
                            uint8_t* g,
1938
                            uint8_t* r,
1939
0
                            const struct YuvConstants* yuvconstants) {
1940
#if defined(__aarch64__) || defined(__arm__) || defined(__riscv)
1941
  int yg = yuvconstants->kRGBCoeffBias[0];
1942
  int ygb = yuvconstants->kRGBCoeffBias[4];
1943
#else
1944
0
  int ygb = yuvconstants->kYBiasToRgb[0];
1945
0
  int yg = yuvconstants->kYToRgb[0];
1946
0
#endif
1947
0
  uint32_t y1 = (uint32_t)(y * 0x0101 * yg) >> 16;
1948
0
  uint8_t b8 = STATIC_CAST(uint8_t, Clamp(((int32_t)(y1) + ygb) >> 6));
1949
0
  *b = b8;
1950
0
  *g = b8;
1951
0
  *r = b8;
1952
0
}
1953
1954
void I444ToARGBRow_C(const uint8_t* src_y,
1955
                     const uint8_t* src_u,
1956
                     const uint8_t* src_v,
1957
                     uint8_t* rgb_buf,
1958
                     const struct YuvConstants* yuvconstants,
1959
0
                     int width) {
1960
0
  int x;
1961
0
  for (x = 0; x < width; ++x) {
1962
0
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
1963
0
             rgb_buf + 2, yuvconstants);
1964
0
    rgb_buf[3] = 255;
1965
0
    src_y += 1;
1966
0
    src_u += 1;
1967
0
    src_v += 1;
1968
0
    rgb_buf += 4;  // Advance 1 pixel.
1969
0
  }
1970
0
}
1971
1972
void I444ToRGB24Row_C(const uint8_t* src_y,
1973
                      const uint8_t* src_u,
1974
                      const uint8_t* src_v,
1975
                      uint8_t* rgb_buf,
1976
                      const struct YuvConstants* yuvconstants,
1977
0
                      int width) {
1978
0
  int x;
1979
0
  for (x = 0; x < width; ++x) {
1980
0
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
1981
0
             rgb_buf + 2, yuvconstants);
1982
0
    src_y += 1;
1983
0
    src_u += 1;
1984
0
    src_v += 1;
1985
0
    rgb_buf += 3;  // Advance 1 pixel.
1986
0
  }
1987
0
}
1988
1989
// Also used for 420
1990
void I422ToARGBRow_C(const uint8_t* src_y,
1991
                     const uint8_t* src_u,
1992
                     const uint8_t* src_v,
1993
                     uint8_t* rgb_buf,
1994
                     const struct YuvConstants* yuvconstants,
1995
0
                     int width) {
1996
0
  int x;
1997
0
  for (x = 0; x < width - 1; x += 2) {
1998
0
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
1999
0
             rgb_buf + 2, yuvconstants);
2000
0
    rgb_buf[3] = 255;
2001
0
    YuvPixel(src_y[1], src_u[0], src_v[0], rgb_buf + 4, rgb_buf + 5,
2002
0
             rgb_buf + 6, yuvconstants);
2003
0
    rgb_buf[7] = 255;
2004
0
    src_y += 2;
2005
0
    src_u += 1;
2006
0
    src_v += 1;
2007
0
    rgb_buf += 8;  // Advance 2 pixels.
2008
0
  }
2009
0
  if (width & 1) {
2010
0
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
2011
0
             rgb_buf + 2, yuvconstants);
2012
0
    rgb_buf[3] = 255;
2013
0
  }
2014
0
}
2015
2016
// 10 bit YUV to ARGB
2017
void I210ToARGBRow_C(const uint16_t* src_y,
2018
                     const uint16_t* src_u,
2019
                     const uint16_t* src_v,
2020
                     uint8_t* rgb_buf,
2021
                     const struct YuvConstants* yuvconstants,
2022
0
                     int width) {
2023
0
  int x;
2024
0
  for (x = 0; x < width - 1; x += 2) {
2025
0
    YuvPixel10(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
2026
0
               rgb_buf + 2, yuvconstants);
2027
0
    rgb_buf[3] = 255;
2028
0
    YuvPixel10(src_y[1], src_u[0], src_v[0], rgb_buf + 4, rgb_buf + 5,
2029
0
               rgb_buf + 6, yuvconstants);
2030
0
    rgb_buf[7] = 255;
2031
0
    src_y += 2;
2032
0
    src_u += 1;
2033
0
    src_v += 1;
2034
0
    rgb_buf += 8;  // Advance 2 pixels.
2035
0
  }
2036
0
  if (width & 1) {
2037
0
    YuvPixel10(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
2038
0
               rgb_buf + 2, yuvconstants);
2039
0
    rgb_buf[3] = 255;
2040
0
  }
2041
0
}
2042
2043
void I410ToARGBRow_C(const uint16_t* src_y,
2044
                     const uint16_t* src_u,
2045
                     const uint16_t* src_v,
2046
                     uint8_t* rgb_buf,
2047
                     const struct YuvConstants* yuvconstants,
2048
0
                     int width) {
2049
0
  int x;
2050
0
  for (x = 0; x < width; ++x) {
2051
0
    YuvPixel10(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
2052
0
               rgb_buf + 2, yuvconstants);
2053
0
    rgb_buf[3] = 255;
2054
0
    src_y += 1;
2055
0
    src_u += 1;
2056
0
    src_v += 1;
2057
0
    rgb_buf += 4;  // Advance 1 pixels.
2058
0
  }
2059
0
}
2060
2061
void I210AlphaToARGBRow_C(const uint16_t* src_y,
2062
                          const uint16_t* src_u,
2063
                          const uint16_t* src_v,
2064
                          const uint16_t* src_a,
2065
                          uint8_t* rgb_buf,
2066
                          const struct YuvConstants* yuvconstants,
2067
0
                          int width) {
2068
0
  int x;
2069
0
  for (x = 0; x < width - 1; x += 2) {
2070
0
    YuvPixel10(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
2071
0
               rgb_buf + 2, yuvconstants);
2072
0
    rgb_buf[3] = STATIC_CAST(uint8_t, clamp255(src_a[0] >> 2));
2073
0
    YuvPixel10(src_y[1], src_u[0], src_v[0], rgb_buf + 4, rgb_buf + 5,
2074
0
               rgb_buf + 6, yuvconstants);
2075
0
    rgb_buf[7] = STATIC_CAST(uint8_t, clamp255(src_a[1] >> 2));
2076
0
    src_y += 2;
2077
0
    src_u += 1;
2078
0
    src_v += 1;
2079
0
    src_a += 2;
2080
0
    rgb_buf += 8;  // Advance 2 pixels.
2081
0
  }
2082
0
  if (width & 1) {
2083
0
    YuvPixel10(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
2084
0
               rgb_buf + 2, yuvconstants);
2085
0
    rgb_buf[3] = STATIC_CAST(uint8_t, clamp255(src_a[0] >> 2));
2086
0
  }
2087
0
}
2088
2089
void I410AlphaToARGBRow_C(const uint16_t* src_y,
2090
                          const uint16_t* src_u,
2091
                          const uint16_t* src_v,
2092
                          const uint16_t* src_a,
2093
                          uint8_t* rgb_buf,
2094
                          const struct YuvConstants* yuvconstants,
2095
0
                          int width) {
2096
0
  int x;
2097
0
  for (x = 0; x < width; ++x) {
2098
0
    YuvPixel10(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
2099
0
               rgb_buf + 2, yuvconstants);
2100
0
    rgb_buf[3] = STATIC_CAST(uint8_t, clamp255(src_a[0] >> 2));
2101
0
    src_y += 1;
2102
0
    src_u += 1;
2103
0
    src_v += 1;
2104
0
    src_a += 1;
2105
0
    rgb_buf += 4;  // Advance 1 pixels.
2106
0
  }
2107
0
}
2108
2109
// 12 bit YUV to ARGB
2110
void I212ToARGBRow_C(const uint16_t* src_y,
2111
                     const uint16_t* src_u,
2112
                     const uint16_t* src_v,
2113
                     uint8_t* rgb_buf,
2114
                     const struct YuvConstants* yuvconstants,
2115
0
                     int width) {
2116
0
  int x;
2117
0
  for (x = 0; x < width - 1; x += 2) {
2118
0
    YuvPixel12(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
2119
0
               rgb_buf + 2, yuvconstants);
2120
0
    rgb_buf[3] = 255;
2121
0
    YuvPixel12(src_y[1], src_u[0], src_v[0], rgb_buf + 4, rgb_buf + 5,
2122
0
               rgb_buf + 6, yuvconstants);
2123
0
    rgb_buf[7] = 255;
2124
0
    src_y += 2;
2125
0
    src_u += 1;
2126
0
    src_v += 1;
2127
0
    rgb_buf += 8;  // Advance 2 pixels.
2128
0
  }
2129
0
  if (width & 1) {
2130
0
    YuvPixel12(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
2131
0
               rgb_buf + 2, yuvconstants);
2132
0
    rgb_buf[3] = 255;
2133
0
  }
2134
0
}
2135
2136
0
static void StoreAR30(uint8_t* rgb_buf, int b, int g, int r) {
2137
0
  uint32_t ar30;
2138
0
  b = b >> 4;  // convert 8 bit 10.6 to 10 bit.
2139
0
  g = g >> 4;
2140
0
  r = r >> 4;
2141
0
  b = Clamp10(b);
2142
0
  g = Clamp10(g);
2143
0
  r = Clamp10(r);
2144
0
  ar30 = b | ((uint32_t)g << 10) | ((uint32_t)r << 20) | 0xc0000000;
2145
0
  (*(uint32_t*)rgb_buf) = ar30;
2146
0
}
2147
2148
// 10 bit YUV to 10 bit AR30
2149
void I210ToAR30Row_C(const uint16_t* src_y,
2150
                     const uint16_t* src_u,
2151
                     const uint16_t* src_v,
2152
                     uint8_t* rgb_buf,
2153
                     const struct YuvConstants* yuvconstants,
2154
0
                     int width) {
2155
0
  int x;
2156
0
  int b;
2157
0
  int g;
2158
0
  int r;
2159
0
  for (x = 0; x < width - 1; x += 2) {
2160
0
    YuvPixel10_16(src_y[0], src_u[0], src_v[0], &b, &g, &r, yuvconstants);
2161
0
    StoreAR30(rgb_buf, b, g, r);
2162
0
    YuvPixel10_16(src_y[1], src_u[0], src_v[0], &b, &g, &r, yuvconstants);
2163
0
    StoreAR30(rgb_buf + 4, b, g, r);
2164
0
    src_y += 2;
2165
0
    src_u += 1;
2166
0
    src_v += 1;
2167
0
    rgb_buf += 8;  // Advance 2 pixels.
2168
0
  }
2169
0
  if (width & 1) {
2170
0
    YuvPixel10_16(src_y[0], src_u[0], src_v[0], &b, &g, &r, yuvconstants);
2171
0
    StoreAR30(rgb_buf, b, g, r);
2172
0
  }
2173
0
}
2174
2175
// 12 bit YUV to 10 bit AR30
2176
void I212ToAR30Row_C(const uint16_t* src_y,
2177
                     const uint16_t* src_u,
2178
                     const uint16_t* src_v,
2179
                     uint8_t* rgb_buf,
2180
                     const struct YuvConstants* yuvconstants,
2181
0
                     int width) {
2182
0
  int x;
2183
0
  int b;
2184
0
  int g;
2185
0
  int r;
2186
0
  for (x = 0; x < width - 1; x += 2) {
2187
0
    YuvPixel12_16(src_y[0], src_u[0], src_v[0], &b, &g, &r, yuvconstants);
2188
0
    StoreAR30(rgb_buf, b, g, r);
2189
0
    YuvPixel12_16(src_y[1], src_u[0], src_v[0], &b, &g, &r, yuvconstants);
2190
0
    StoreAR30(rgb_buf + 4, b, g, r);
2191
0
    src_y += 2;
2192
0
    src_u += 1;
2193
0
    src_v += 1;
2194
0
    rgb_buf += 8;  // Advance 2 pixels.
2195
0
  }
2196
0
  if (width & 1) {
2197
0
    YuvPixel12_16(src_y[0], src_u[0], src_v[0], &b, &g, &r, yuvconstants);
2198
0
    StoreAR30(rgb_buf, b, g, r);
2199
0
  }
2200
0
}
2201
2202
void I410ToAR30Row_C(const uint16_t* src_y,
2203
                     const uint16_t* src_u,
2204
                     const uint16_t* src_v,
2205
                     uint8_t* rgb_buf,
2206
                     const struct YuvConstants* yuvconstants,
2207
0
                     int width) {
2208
0
  int x;
2209
0
  int b;
2210
0
  int g;
2211
0
  int r;
2212
0
  for (x = 0; x < width; ++x) {
2213
0
    YuvPixel10_16(src_y[0], src_u[0], src_v[0], &b, &g, &r, yuvconstants);
2214
0
    StoreAR30(rgb_buf, b, g, r);
2215
0
    src_y += 1;
2216
0
    src_u += 1;
2217
0
    src_v += 1;
2218
0
    rgb_buf += 4;  // Advance 1 pixel.
2219
0
  }
2220
0
}
2221
2222
// P210 has 10 bits in msb of 16 bit NV12 style layout.
2223
void P210ToARGBRow_C(const uint16_t* src_y,
2224
                     const uint16_t* src_uv,
2225
                     uint8_t* dst_argb,
2226
                     const struct YuvConstants* yuvconstants,
2227
0
                     int width) {
2228
0
  int x;
2229
0
  for (x = 0; x < width - 1; x += 2) {
2230
0
    YuvPixel16_8(src_y[0], src_uv[0], src_uv[1], dst_argb + 0, dst_argb + 1,
2231
0
                 dst_argb + 2, yuvconstants);
2232
0
    dst_argb[3] = 255;
2233
0
    YuvPixel16_8(src_y[1], src_uv[0], src_uv[1], dst_argb + 4, dst_argb + 5,
2234
0
                 dst_argb + 6, yuvconstants);
2235
0
    dst_argb[7] = 255;
2236
0
    src_y += 2;
2237
0
    src_uv += 2;
2238
0
    dst_argb += 8;  // Advance 2 pixels.
2239
0
  }
2240
0
  if (width & 1) {
2241
0
    YuvPixel16_8(src_y[0], src_uv[0], src_uv[1], dst_argb + 0, dst_argb + 1,
2242
0
                 dst_argb + 2, yuvconstants);
2243
0
    dst_argb[3] = 255;
2244
0
  }
2245
0
}
2246
2247
void P410ToARGBRow_C(const uint16_t* src_y,
2248
                     const uint16_t* src_uv,
2249
                     uint8_t* dst_argb,
2250
                     const struct YuvConstants* yuvconstants,
2251
0
                     int width) {
2252
0
  int x;
2253
0
  for (x = 0; x < width; ++x) {
2254
0
    YuvPixel16_8(src_y[0], src_uv[0], src_uv[1], dst_argb + 0, dst_argb + 1,
2255
0
                 dst_argb + 2, yuvconstants);
2256
0
    dst_argb[3] = 255;
2257
0
    src_y += 1;
2258
0
    src_uv += 2;
2259
0
    dst_argb += 4;  // Advance 1 pixels.
2260
0
  }
2261
0
}
2262
2263
void P210ToAR30Row_C(const uint16_t* src_y,
2264
                     const uint16_t* src_uv,
2265
                     uint8_t* dst_ar30,
2266
                     const struct YuvConstants* yuvconstants,
2267
0
                     int width) {
2268
0
  int x;
2269
0
  int b;
2270
0
  int g;
2271
0
  int r;
2272
0
  for (x = 0; x < width - 1; x += 2) {
2273
0
    YuvPixel16_16(src_y[0], src_uv[0], src_uv[1], &b, &g, &r, yuvconstants);
2274
0
    StoreAR30(dst_ar30, b, g, r);
2275
0
    YuvPixel16_16(src_y[1], src_uv[0], src_uv[1], &b, &g, &r, yuvconstants);
2276
0
    StoreAR30(dst_ar30 + 4, b, g, r);
2277
0
    src_y += 2;
2278
0
    src_uv += 2;
2279
0
    dst_ar30 += 8;  // Advance 2 pixels.
2280
0
  }
2281
0
  if (width & 1) {
2282
0
    YuvPixel16_16(src_y[0], src_uv[0], src_uv[1], &b, &g, &r, yuvconstants);
2283
0
    StoreAR30(dst_ar30, b, g, r);
2284
0
  }
2285
0
}
2286
2287
void P410ToAR30Row_C(const uint16_t* src_y,
2288
                     const uint16_t* src_uv,
2289
                     uint8_t* dst_ar30,
2290
                     const struct YuvConstants* yuvconstants,
2291
0
                     int width) {
2292
0
  int x;
2293
0
  int b;
2294
0
  int g;
2295
0
  int r;
2296
0
  for (x = 0; x < width; ++x) {
2297
0
    YuvPixel16_16(src_y[0], src_uv[0], src_uv[1], &b, &g, &r, yuvconstants);
2298
0
    StoreAR30(dst_ar30, b, g, r);
2299
0
    src_y += 1;
2300
0
    src_uv += 2;
2301
0
    dst_ar30 += 4;  // Advance 1 pixel.
2302
0
  }
2303
0
}
2304
2305
// 8 bit YUV to 10 bit AR30
2306
// Uses same code as 10 bit YUV bit shifts the 8 bit values up to 10 bits.
2307
void I422ToAR30Row_C(const uint8_t* src_y,
2308
                     const uint8_t* src_u,
2309
                     const uint8_t* src_v,
2310
                     uint8_t* rgb_buf,
2311
                     const struct YuvConstants* yuvconstants,
2312
0
                     int width) {
2313
0
  int x;
2314
0
  int b;
2315
0
  int g;
2316
0
  int r;
2317
0
  for (x = 0; x < width - 1; x += 2) {
2318
0
    YuvPixel8_16(src_y[0], src_u[0], src_v[0], &b, &g, &r, yuvconstants);
2319
0
    StoreAR30(rgb_buf, b, g, r);
2320
0
    YuvPixel8_16(src_y[1], src_u[0], src_v[0], &b, &g, &r, yuvconstants);
2321
0
    StoreAR30(rgb_buf + 4, b, g, r);
2322
0
    src_y += 2;
2323
0
    src_u += 1;
2324
0
    src_v += 1;
2325
0
    rgb_buf += 8;  // Advance 2 pixels.
2326
0
  }
2327
0
  if (width & 1) {
2328
0
    YuvPixel8_16(src_y[0], src_u[0], src_v[0], &b, &g, &r, yuvconstants);
2329
0
    StoreAR30(rgb_buf, b, g, r);
2330
0
  }
2331
0
}
2332
2333
void I444AlphaToARGBRow_C(const uint8_t* src_y,
2334
                          const uint8_t* src_u,
2335
                          const uint8_t* src_v,
2336
                          const uint8_t* src_a,
2337
                          uint8_t* rgb_buf,
2338
                          const struct YuvConstants* yuvconstants,
2339
0
                          int width) {
2340
0
  int x;
2341
0
  for (x = 0; x < width; ++x) {
2342
0
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
2343
0
             rgb_buf + 2, yuvconstants);
2344
0
    rgb_buf[3] = src_a[0];
2345
0
    src_y += 1;
2346
0
    src_u += 1;
2347
0
    src_v += 1;
2348
0
    src_a += 1;
2349
0
    rgb_buf += 4;  // Advance 1 pixel.
2350
0
  }
2351
0
}
2352
2353
void I422AlphaToARGBRow_C(const uint8_t* src_y,
2354
                          const uint8_t* src_u,
2355
                          const uint8_t* src_v,
2356
                          const uint8_t* src_a,
2357
                          uint8_t* rgb_buf,
2358
                          const struct YuvConstants* yuvconstants,
2359
0
                          int width) {
2360
0
  int x;
2361
0
  for (x = 0; x < width - 1; x += 2) {
2362
0
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
2363
0
             rgb_buf + 2, yuvconstants);
2364
0
    rgb_buf[3] = src_a[0];
2365
0
    YuvPixel(src_y[1], src_u[0], src_v[0], rgb_buf + 4, rgb_buf + 5,
2366
0
             rgb_buf + 6, yuvconstants);
2367
0
    rgb_buf[7] = src_a[1];
2368
0
    src_y += 2;
2369
0
    src_u += 1;
2370
0
    src_v += 1;
2371
0
    src_a += 2;
2372
0
    rgb_buf += 8;  // Advance 2 pixels.
2373
0
  }
2374
0
  if (width & 1) {
2375
0
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
2376
0
             rgb_buf + 2, yuvconstants);
2377
0
    rgb_buf[3] = src_a[0];
2378
0
  }
2379
0
}
2380
2381
void I422ToRGB24Row_C(const uint8_t* src_y,
2382
                      const uint8_t* src_u,
2383
                      const uint8_t* src_v,
2384
                      uint8_t* rgb_buf,
2385
                      const struct YuvConstants* yuvconstants,
2386
0
                      int width) {
2387
0
  int x;
2388
0
  for (x = 0; x < width - 1; x += 2) {
2389
0
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
2390
0
             rgb_buf + 2, yuvconstants);
2391
0
    YuvPixel(src_y[1], src_u[0], src_v[0], rgb_buf + 3, rgb_buf + 4,
2392
0
             rgb_buf + 5, yuvconstants);
2393
0
    src_y += 2;
2394
0
    src_u += 1;
2395
0
    src_v += 1;
2396
0
    rgb_buf += 6;  // Advance 2 pixels.
2397
0
  }
2398
0
  if (width & 1) {
2399
0
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
2400
0
             rgb_buf + 2, yuvconstants);
2401
0
  }
2402
0
}
2403
2404
void I422ToARGB4444Row_C(const uint8_t* src_y,
2405
                         const uint8_t* src_u,
2406
                         const uint8_t* src_v,
2407
                         uint8_t* dst_argb4444,
2408
                         const struct YuvConstants* yuvconstants,
2409
0
                         int width) {
2410
0
  uint8_t b0;
2411
0
  uint8_t g0;
2412
0
  uint8_t r0;
2413
0
  uint8_t b1;
2414
0
  uint8_t g1;
2415
0
  uint8_t r1;
2416
0
  int x;
2417
0
  for (x = 0; x < width - 1; x += 2) {
2418
0
    YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
2419
0
    YuvPixel(src_y[1], src_u[0], src_v[0], &b1, &g1, &r1, yuvconstants);
2420
0
    b0 = b0 >> 4;
2421
0
    g0 = g0 >> 4;
2422
0
    r0 = r0 >> 4;
2423
0
    b1 = b1 >> 4;
2424
0
    g1 = g1 >> 4;
2425
0
    r1 = r1 >> 4;
2426
0
    *(uint16_t*)(dst_argb4444 + 0) =
2427
0
        STATIC_CAST(uint16_t, b0 | (g0 << 4) | (r0 << 8) | 0xf000);
2428
0
    *(uint16_t*)(dst_argb4444 + 2) =
2429
0
        STATIC_CAST(uint16_t, b1 | (g1 << 4) | (r1 << 8) | 0xf000);
2430
0
    src_y += 2;
2431
0
    src_u += 1;
2432
0
    src_v += 1;
2433
0
    dst_argb4444 += 4;  // Advance 2 pixels.
2434
0
  }
2435
0
  if (width & 1) {
2436
0
    YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
2437
0
    b0 = b0 >> 4;
2438
0
    g0 = g0 >> 4;
2439
0
    r0 = r0 >> 4;
2440
0
    *(uint16_t*)(dst_argb4444) =
2441
0
        STATIC_CAST(uint16_t, b0 | (g0 << 4) | (r0 << 8) | 0xf000);
2442
0
  }
2443
0
}
2444
2445
void I422ToARGB1555Row_C(const uint8_t* src_y,
2446
                         const uint8_t* src_u,
2447
                         const uint8_t* src_v,
2448
                         uint8_t* dst_argb1555,
2449
                         const struct YuvConstants* yuvconstants,
2450
0
                         int width) {
2451
0
  uint8_t b0;
2452
0
  uint8_t g0;
2453
0
  uint8_t r0;
2454
0
  uint8_t b1;
2455
0
  uint8_t g1;
2456
0
  uint8_t r1;
2457
0
  int x;
2458
0
  for (x = 0; x < width - 1; x += 2) {
2459
0
    YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
2460
0
    YuvPixel(src_y[1], src_u[0], src_v[0], &b1, &g1, &r1, yuvconstants);
2461
0
    b0 = b0 >> 3;
2462
0
    g0 = g0 >> 3;
2463
0
    r0 = r0 >> 3;
2464
0
    b1 = b1 >> 3;
2465
0
    g1 = g1 >> 3;
2466
0
    r1 = r1 >> 3;
2467
0
    *(uint16_t*)(dst_argb1555 + 0) =
2468
0
        STATIC_CAST(uint16_t, b0 | (g0 << 5) | (r0 << 10) | 0x8000);
2469
0
    *(uint16_t*)(dst_argb1555 + 2) =
2470
0
        STATIC_CAST(uint16_t, b1 | (g1 << 5) | (r1 << 10) | 0x8000);
2471
0
    src_y += 2;
2472
0
    src_u += 1;
2473
0
    src_v += 1;
2474
0
    dst_argb1555 += 4;  // Advance 2 pixels.
2475
0
  }
2476
0
  if (width & 1) {
2477
0
    YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
2478
0
    b0 = b0 >> 3;
2479
0
    g0 = g0 >> 3;
2480
0
    r0 = r0 >> 3;
2481
0
    *(uint16_t*)(dst_argb1555) =
2482
0
        STATIC_CAST(uint16_t, b0 | (g0 << 5) | (r0 << 10) | 0x8000);
2483
0
  }
2484
0
}
2485
2486
void I422ToRGB565Row_C(const uint8_t* src_y,
2487
                       const uint8_t* src_u,
2488
                       const uint8_t* src_v,
2489
                       uint8_t* dst_rgb565,
2490
                       const struct YuvConstants* yuvconstants,
2491
4.16k
                       int width) {
2492
4.16k
  uint8_t b0;
2493
4.16k
  uint8_t g0;
2494
4.16k
  uint8_t r0;
2495
4.16k
  uint8_t b1;
2496
4.16k
  uint8_t g1;
2497
4.16k
  uint8_t r1;
2498
4.16k
  int x;
2499
12.2k
  for (x = 0; x < width - 1; x += 2) {
2500
8.04k
    YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
2501
8.04k
    YuvPixel(src_y[1], src_u[0], src_v[0], &b1, &g1, &r1, yuvconstants);
2502
8.04k
    b0 = b0 >> 3;
2503
8.04k
    g0 = g0 >> 2;
2504
8.04k
    r0 = r0 >> 3;
2505
8.04k
    b1 = b1 >> 3;
2506
8.04k
    g1 = g1 >> 2;
2507
8.04k
    r1 = r1 >> 3;
2508
8.04k
    *(uint16_t*)(dst_rgb565 + 0) =
2509
8.04k
        STATIC_CAST(uint16_t, b0 | (g0 << 5) | (r0 << 11));
2510
8.04k
    *(uint16_t*)(dst_rgb565 + 2) =
2511
8.04k
        STATIC_CAST(uint16_t, b1 | (g1 << 5) | (r1 << 11));
2512
8.04k
    src_y += 2;
2513
8.04k
    src_u += 1;
2514
8.04k
    src_v += 1;
2515
8.04k
    dst_rgb565 += 4;  // Advance 2 pixels.
2516
8.04k
  }
2517
4.16k
  if (width & 1) {
2518
3.47k
    YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
2519
3.47k
    b0 = b0 >> 3;
2520
3.47k
    g0 = g0 >> 2;
2521
3.47k
    r0 = r0 >> 3;
2522
3.47k
    *(uint16_t*)(dst_rgb565 + 0) =
2523
3.47k
        STATIC_CAST(uint16_t, b0 | (g0 << 5) | (r0 << 11));
2524
3.47k
  }
2525
4.16k
}
2526
2527
void NV12ToARGBRow_C(const uint8_t* src_y,
2528
                     const uint8_t* src_uv,
2529
                     uint8_t* rgb_buf,
2530
                     const struct YuvConstants* yuvconstants,
2531
0
                     int width) {
2532
0
  int x;
2533
0
  for (x = 0; x < width - 1; x += 2) {
2534
0
    YuvPixel(src_y[0], src_uv[0], src_uv[1], rgb_buf + 0, rgb_buf + 1,
2535
0
             rgb_buf + 2, yuvconstants);
2536
0
    rgb_buf[3] = 255;
2537
0
    YuvPixel(src_y[1], src_uv[0], src_uv[1], rgb_buf + 4, rgb_buf + 5,
2538
0
             rgb_buf + 6, yuvconstants);
2539
0
    rgb_buf[7] = 255;
2540
0
    src_y += 2;
2541
0
    src_uv += 2;
2542
0
    rgb_buf += 8;  // Advance 2 pixels.
2543
0
  }
2544
0
  if (width & 1) {
2545
0
    YuvPixel(src_y[0], src_uv[0], src_uv[1], rgb_buf + 0, rgb_buf + 1,
2546
0
             rgb_buf + 2, yuvconstants);
2547
0
    rgb_buf[3] = 255;
2548
0
  }
2549
0
}
2550
2551
void NV21ToARGBRow_C(const uint8_t* src_y,
2552
                     const uint8_t* src_vu,
2553
                     uint8_t* rgb_buf,
2554
                     const struct YuvConstants* yuvconstants,
2555
0
                     int width) {
2556
0
  int x;
2557
0
  for (x = 0; x < width - 1; x += 2) {
2558
0
    YuvPixel(src_y[0], src_vu[1], src_vu[0], rgb_buf + 0, rgb_buf + 1,
2559
0
             rgb_buf + 2, yuvconstants);
2560
0
    rgb_buf[3] = 255;
2561
0
    YuvPixel(src_y[1], src_vu[1], src_vu[0], rgb_buf + 4, rgb_buf + 5,
2562
0
             rgb_buf + 6, yuvconstants);
2563
0
    rgb_buf[7] = 255;
2564
0
    src_y += 2;
2565
0
    src_vu += 2;
2566
0
    rgb_buf += 8;  // Advance 2 pixels.
2567
0
  }
2568
0
  if (width & 1) {
2569
0
    YuvPixel(src_y[0], src_vu[1], src_vu[0], rgb_buf + 0, rgb_buf + 1,
2570
0
             rgb_buf + 2, yuvconstants);
2571
0
    rgb_buf[3] = 255;
2572
0
  }
2573
0
}
2574
2575
void NV12ToRGB24Row_C(const uint8_t* src_y,
2576
                      const uint8_t* src_uv,
2577
                      uint8_t* rgb_buf,
2578
                      const struct YuvConstants* yuvconstants,
2579
0
                      int width) {
2580
0
  int x;
2581
0
  for (x = 0; x < width - 1; x += 2) {
2582
0
    YuvPixel(src_y[0], src_uv[0], src_uv[1], rgb_buf + 0, rgb_buf + 1,
2583
0
             rgb_buf + 2, yuvconstants);
2584
0
    YuvPixel(src_y[1], src_uv[0], src_uv[1], rgb_buf + 3, rgb_buf + 4,
2585
0
             rgb_buf + 5, yuvconstants);
2586
0
    src_y += 2;
2587
0
    src_uv += 2;
2588
0
    rgb_buf += 6;  // Advance 2 pixels.
2589
0
  }
2590
0
  if (width & 1) {
2591
0
    YuvPixel(src_y[0], src_uv[0], src_uv[1], rgb_buf + 0, rgb_buf + 1,
2592
0
             rgb_buf + 2, yuvconstants);
2593
0
  }
2594
0
}
2595
2596
void NV21ToRGB24Row_C(const uint8_t* src_y,
2597
                      const uint8_t* src_vu,
2598
                      uint8_t* rgb_buf,
2599
                      const struct YuvConstants* yuvconstants,
2600
0
                      int width) {
2601
0
  int x;
2602
0
  for (x = 0; x < width - 1; x += 2) {
2603
0
    YuvPixel(src_y[0], src_vu[1], src_vu[0], rgb_buf + 0, rgb_buf + 1,
2604
0
             rgb_buf + 2, yuvconstants);
2605
0
    YuvPixel(src_y[1], src_vu[1], src_vu[0], rgb_buf + 3, rgb_buf + 4,
2606
0
             rgb_buf + 5, yuvconstants);
2607
0
    src_y += 2;
2608
0
    src_vu += 2;
2609
0
    rgb_buf += 6;  // Advance 2 pixels.
2610
0
  }
2611
0
  if (width & 1) {
2612
0
    YuvPixel(src_y[0], src_vu[1], src_vu[0], rgb_buf + 0, rgb_buf + 1,
2613
0
             rgb_buf + 2, yuvconstants);
2614
0
  }
2615
0
}
2616
2617
void NV12ToRGB565Row_C(const uint8_t* src_y,
2618
                       const uint8_t* src_uv,
2619
                       uint8_t* dst_rgb565,
2620
                       const struct YuvConstants* yuvconstants,
2621
0
                       int width) {
2622
0
  uint8_t b0;
2623
0
  uint8_t g0;
2624
0
  uint8_t r0;
2625
0
  uint8_t b1;
2626
0
  uint8_t g1;
2627
0
  uint8_t r1;
2628
0
  int x;
2629
0
  for (x = 0; x < width - 1; x += 2) {
2630
0
    YuvPixel(src_y[0], src_uv[0], src_uv[1], &b0, &g0, &r0, yuvconstants);
2631
0
    YuvPixel(src_y[1], src_uv[0], src_uv[1], &b1, &g1, &r1, yuvconstants);
2632
0
    b0 = b0 >> 3;
2633
0
    g0 = g0 >> 2;
2634
0
    r0 = r0 >> 3;
2635
0
    b1 = b1 >> 3;
2636
0
    g1 = g1 >> 2;
2637
0
    r1 = r1 >> 3;
2638
0
    *(uint16_t*)(dst_rgb565 + 0) = STATIC_CAST(uint16_t, b0) |
2639
0
                                   STATIC_CAST(uint16_t, g0 << 5) |
2640
0
                                   STATIC_CAST(uint16_t, r0 << 11);
2641
0
    *(uint16_t*)(dst_rgb565 + 2) = STATIC_CAST(uint16_t, b1) |
2642
0
                                   STATIC_CAST(uint16_t, g1 << 5) |
2643
0
                                   STATIC_CAST(uint16_t, r1 << 11);
2644
0
    src_y += 2;
2645
0
    src_uv += 2;
2646
0
    dst_rgb565 += 4;  // Advance 2 pixels.
2647
0
  }
2648
0
  if (width & 1) {
2649
0
    YuvPixel(src_y[0], src_uv[0], src_uv[1], &b0, &g0, &r0, yuvconstants);
2650
0
    b0 = b0 >> 3;
2651
0
    g0 = g0 >> 2;
2652
0
    r0 = r0 >> 3;
2653
0
    *(uint16_t*)(dst_rgb565) = STATIC_CAST(uint16_t, b0) |
2654
0
                               STATIC_CAST(uint16_t, g0 << 5) |
2655
0
                               STATIC_CAST(uint16_t, r0 << 11);
2656
0
  }
2657
0
}
2658
2659
void YUY2ToARGBRow_C(const uint8_t* src_yuy2,
2660
                     uint8_t* rgb_buf,
2661
                     const struct YuvConstants* yuvconstants,
2662
0
                     int width) {
2663
0
  int x;
2664
0
  for (x = 0; x < width - 1; x += 2) {
2665
0
    YuvPixel(src_yuy2[0], src_yuy2[1], src_yuy2[3], rgb_buf + 0, rgb_buf + 1,
2666
0
             rgb_buf + 2, yuvconstants);
2667
0
    rgb_buf[3] = 255;
2668
0
    YuvPixel(src_yuy2[2], src_yuy2[1], src_yuy2[3], rgb_buf + 4, rgb_buf + 5,
2669
0
             rgb_buf + 6, yuvconstants);
2670
0
    rgb_buf[7] = 255;
2671
0
    src_yuy2 += 4;
2672
0
    rgb_buf += 8;  // Advance 2 pixels.
2673
0
  }
2674
0
  if (width & 1) {
2675
0
    YuvPixel(src_yuy2[0], src_yuy2[1], src_yuy2[3], rgb_buf + 0, rgb_buf + 1,
2676
0
             rgb_buf + 2, yuvconstants);
2677
0
    rgb_buf[3] = 255;
2678
0
  }
2679
0
}
2680
2681
void UYVYToARGBRow_C(const uint8_t* src_uyvy,
2682
                     uint8_t* rgb_buf,
2683
                     const struct YuvConstants* yuvconstants,
2684
0
                     int width) {
2685
0
  int x;
2686
0
  for (x = 0; x < width - 1; x += 2) {
2687
0
    YuvPixel(src_uyvy[1], src_uyvy[0], src_uyvy[2], rgb_buf + 0, rgb_buf + 1,
2688
0
             rgb_buf + 2, yuvconstants);
2689
0
    rgb_buf[3] = 255;
2690
0
    YuvPixel(src_uyvy[3], src_uyvy[0], src_uyvy[2], rgb_buf + 4, rgb_buf + 5,
2691
0
             rgb_buf + 6, yuvconstants);
2692
0
    rgb_buf[7] = 255;
2693
0
    src_uyvy += 4;
2694
0
    rgb_buf += 8;  // Advance 2 pixels.
2695
0
  }
2696
0
  if (width & 1) {
2697
0
    YuvPixel(src_uyvy[1], src_uyvy[0], src_uyvy[2], rgb_buf + 0, rgb_buf + 1,
2698
0
             rgb_buf + 2, yuvconstants);
2699
0
    rgb_buf[3] = 255;
2700
0
  }
2701
0
}
2702
2703
void I422ToRGBARow_C(const uint8_t* src_y,
2704
                     const uint8_t* src_u,
2705
                     const uint8_t* src_v,
2706
                     uint8_t* rgb_buf,
2707
                     const struct YuvConstants* yuvconstants,
2708
0
                     int width) {
2709
0
  int x;
2710
0
  for (x = 0; x < width - 1; x += 2) {
2711
0
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 1, rgb_buf + 2,
2712
0
             rgb_buf + 3, yuvconstants);
2713
0
    rgb_buf[0] = 255;
2714
0
    YuvPixel(src_y[1], src_u[0], src_v[0], rgb_buf + 5, rgb_buf + 6,
2715
0
             rgb_buf + 7, yuvconstants);
2716
0
    rgb_buf[4] = 255;
2717
0
    src_y += 2;
2718
0
    src_u += 1;
2719
0
    src_v += 1;
2720
0
    rgb_buf += 8;  // Advance 2 pixels.
2721
0
  }
2722
0
  if (width & 1) {
2723
0
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 1, rgb_buf + 2,
2724
0
             rgb_buf + 3, yuvconstants);
2725
0
    rgb_buf[0] = 255;
2726
0
  }
2727
0
}
2728
2729
void I400ToARGBRow_C(const uint8_t* src_y,
2730
                     uint8_t* rgb_buf,
2731
                     const struct YuvConstants* yuvconstants,
2732
0
                     int width) {
2733
0
  int x;
2734
0
  for (x = 0; x < width - 1; x += 2) {
2735
0
    YPixel(src_y[0], rgb_buf + 0, rgb_buf + 1, rgb_buf + 2, yuvconstants);
2736
0
    rgb_buf[3] = 255;
2737
0
    YPixel(src_y[1], rgb_buf + 4, rgb_buf + 5, rgb_buf + 6, yuvconstants);
2738
0
    rgb_buf[7] = 255;
2739
0
    src_y += 2;
2740
0
    rgb_buf += 8;  // Advance 2 pixels.
2741
0
  }
2742
0
  if (width & 1) {
2743
0
    YPixel(src_y[0], rgb_buf + 0, rgb_buf + 1, rgb_buf + 2, yuvconstants);
2744
0
    rgb_buf[3] = 255;
2745
0
  }
2746
0
}
2747
2748
0
void MirrorRow_C(const uint8_t* src, uint8_t* dst, int width) {
2749
0
  int x;
2750
0
  src += width - 1;
2751
0
  for (x = 0; x < width - 1; x += 2) {
2752
0
    dst[x] = src[0];
2753
0
    dst[x + 1] = src[-1];
2754
0
    src -= 2;
2755
0
  }
2756
0
  if (width & 1) {
2757
0
    dst[width - 1] = src[0];
2758
0
  }
2759
0
}
2760
2761
0
void MirrorRow_16_C(const uint16_t* src, uint16_t* dst, int width) {
2762
0
  int x;
2763
0
  src += width - 1;
2764
0
  for (x = 0; x < width - 1; x += 2) {
2765
0
    dst[x] = src[0];
2766
0
    dst[x + 1] = src[-1];
2767
0
    src -= 2;
2768
0
  }
2769
0
  if (width & 1) {
2770
0
    dst[width - 1] = src[0];
2771
0
  }
2772
0
}
2773
2774
0
void MirrorUVRow_C(const uint8_t* src_uv, uint8_t* dst_uv, int width) {
2775
0
  int x;
2776
0
  src_uv += (width - 1) << 1;
2777
0
  for (x = 0; x < width; ++x) {
2778
0
    dst_uv[0] = src_uv[0];
2779
0
    dst_uv[1] = src_uv[1];
2780
0
    src_uv -= 2;
2781
0
    dst_uv += 2;
2782
0
  }
2783
0
}
2784
2785
void MirrorSplitUVRow_C(const uint8_t* src_uv,
2786
                        uint8_t* dst_u,
2787
                        uint8_t* dst_v,
2788
0
                        int width) {
2789
0
  int x;
2790
0
  src_uv += (width - 1) << 1;
2791
0
  for (x = 0; x < width - 1; x += 2) {
2792
0
    dst_u[x] = src_uv[0];
2793
0
    dst_u[x + 1] = src_uv[-2];
2794
0
    dst_v[x] = src_uv[1];
2795
0
    dst_v[x + 1] = src_uv[-2 + 1];
2796
0
    src_uv -= 4;
2797
0
  }
2798
0
  if (width & 1) {
2799
0
    dst_u[width - 1] = src_uv[0];
2800
0
    dst_v[width - 1] = src_uv[1];
2801
0
  }
2802
0
}
2803
2804
0
void ARGBMirrorRow_C(const uint8_t* src, uint8_t* dst, int width) {
2805
0
  int x;
2806
0
  const uint32_t* src32 = (const uint32_t*)(src);
2807
0
  uint32_t* dst32 = (uint32_t*)(dst);
2808
0
  src32 += width - 1;
2809
0
  for (x = 0; x < width - 1; x += 2) {
2810
0
    dst32[x] = src32[0];
2811
0
    dst32[x + 1] = src32[-1];
2812
0
    src32 -= 2;
2813
0
  }
2814
0
  if (width & 1) {
2815
0
    dst32[width - 1] = src32[0];
2816
0
  }
2817
0
}
2818
2819
0
void RGB24MirrorRow_C(const uint8_t* src_rgb24, uint8_t* dst_rgb24, int width) {
2820
0
  int x;
2821
0
  src_rgb24 += (ptrdiff_t)width * 3 - 3;
2822
0
  for (x = 0; x < width; ++x) {
2823
0
    uint8_t b = src_rgb24[0];
2824
0
    uint8_t g = src_rgb24[1];
2825
0
    uint8_t r = src_rgb24[2];
2826
0
    dst_rgb24[0] = b;
2827
0
    dst_rgb24[1] = g;
2828
0
    dst_rgb24[2] = r;
2829
0
    src_rgb24 -= 3;
2830
0
    dst_rgb24 += 3;
2831
0
  }
2832
0
}
2833
2834
void SplitUVRow_C(const uint8_t* src_uv,
2835
                  uint8_t* dst_u,
2836
                  uint8_t* dst_v,
2837
0
                  int width) {
2838
0
  int x;
2839
0
  for (x = 0; x < width - 1; x += 2) {
2840
0
    dst_u[x] = src_uv[0];
2841
0
    dst_u[x + 1] = src_uv[2];
2842
0
    dst_v[x] = src_uv[1];
2843
0
    dst_v[x + 1] = src_uv[3];
2844
0
    src_uv += 4;
2845
0
  }
2846
0
  if (width & 1) {
2847
0
    dst_u[width - 1] = src_uv[0];
2848
0
    dst_v[width - 1] = src_uv[1];
2849
0
  }
2850
0
}
2851
2852
void MergeUVRow_C(const uint8_t* src_u,
2853
                  const uint8_t* src_v,
2854
                  uint8_t* dst_uv,
2855
0
                  int width) {
2856
0
  int x;
2857
0
  for (x = 0; x < width - 1; x += 2) {
2858
0
    dst_uv[0] = src_u[x];
2859
0
    dst_uv[1] = src_v[x];
2860
0
    dst_uv[2] = src_u[x + 1];
2861
0
    dst_uv[3] = src_v[x + 1];
2862
0
    dst_uv += 4;
2863
0
  }
2864
0
  if (width & 1) {
2865
0
    dst_uv[0] = src_u[width - 1];
2866
0
    dst_uv[1] = src_v[width - 1];
2867
0
  }
2868
0
}
2869
2870
void DetileRow_C(const uint8_t* src,
2871
                 ptrdiff_t src_tile_stride,
2872
                 uint8_t* dst,
2873
0
                 int width) {
2874
0
  int x;
2875
0
  for (x = 0; x < width - 15; x += 16) {
2876
0
    memcpy(dst, src, 16);
2877
0
    dst += 16;
2878
0
    src += src_tile_stride;
2879
0
  }
2880
0
  if (width & 15) {
2881
0
    memcpy(dst, src, width & 15);
2882
0
  }
2883
0
}
2884
2885
void DetileRow_16_C(const uint16_t* src,
2886
                    ptrdiff_t src_tile_stride,
2887
                    uint16_t* dst,
2888
0
                    int width) {
2889
0
  int x;
2890
0
  for (x = 0; x < width - 15; x += 16) {
2891
0
    memcpy(dst, src, 16 * sizeof(uint16_t));
2892
0
    dst += 16;
2893
0
    src += src_tile_stride;
2894
0
  }
2895
0
  if (width & 15) {
2896
0
    memcpy(dst, src, (width & 15) * sizeof(uint16_t));
2897
0
  }
2898
0
}
2899
2900
void DetileSplitUVRow_C(const uint8_t* src_uv,
2901
                        ptrdiff_t src_tile_stride,
2902
                        uint8_t* dst_u,
2903
                        uint8_t* dst_v,
2904
0
                        int width) {
2905
0
  int x;
2906
0
  for (x = 0; x < width - 15; x += 16) {
2907
0
    SplitUVRow_C(src_uv, dst_u, dst_v, 8);
2908
0
    dst_u += 8;
2909
0
    dst_v += 8;
2910
0
    src_uv += src_tile_stride;
2911
0
  }
2912
0
  if (width & 15) {
2913
0
    SplitUVRow_C(src_uv, dst_u, dst_v, ((width & 15) + 1) / 2);
2914
0
  }
2915
0
}
2916
2917
void DetileToYUY2_C(const uint8_t* src_y,
2918
                    ptrdiff_t src_y_tile_stride,
2919
                    const uint8_t* src_uv,
2920
                    ptrdiff_t src_uv_tile_stride,
2921
                    uint8_t* dst_yuy2,
2922
0
                    int width) {
2923
0
  for (int x = 0; x < width - 15; x += 16) {
2924
0
    for (int i = 0; i < 8; i++) {
2925
0
      dst_yuy2[0] = src_y[0];
2926
0
      dst_yuy2[1] = src_uv[0];
2927
0
      dst_yuy2[2] = src_y[1];
2928
0
      dst_yuy2[3] = src_uv[1];
2929
0
      dst_yuy2 += 4;
2930
0
      src_y += 2;
2931
0
      src_uv += 2;
2932
0
    }
2933
0
    src_y += src_y_tile_stride - 16;
2934
0
    src_uv += src_uv_tile_stride - 16;
2935
0
  }
2936
0
}
2937
2938
// Unpack MT2T into tiled P010 64 pixels at a time. MT2T's bitstream is encoded
2939
// in 80 byte blocks representing 64 pixels each. The first 16 bytes of the
2940
// block contain all of the lower 2 bits of each pixel packed together, and the
2941
// next 64 bytes represent all the upper 8 bits of the pixel. The lower bits are
2942
// packed into 1x4 blocks, whereas the upper bits are packed in normal raster
2943
// order.
2944
0
void UnpackMT2T_C(const uint8_t* src, uint16_t* dst, size_t size) {
2945
0
  for (size_t i = 0; i < size; i += 80) {
2946
0
    const uint8_t* src_lower_bits = src;
2947
0
    const uint8_t* src_upper_bits = src + 16;
2948
2949
0
    for (int j = 0; j < 4; j++) {
2950
0
      for (int k = 0; k < 16; k++) {
2951
0
        *dst++ = ((src_lower_bits[k] >> (j * 2)) & 0x3) << 6 |
2952
0
                 (uint16_t)*src_upper_bits << 8 |
2953
0
                 (uint16_t)*src_upper_bits >> 2;
2954
0
        src_upper_bits++;
2955
0
      }
2956
0
    }
2957
2958
0
    src += 80;
2959
0
  }
2960
0
}
2961
2962
void SplitRGBRow_C(const uint8_t* src_rgb,
2963
                   uint8_t* dst_r,
2964
                   uint8_t* dst_g,
2965
                   uint8_t* dst_b,
2966
0
                   int width) {
2967
0
  int x;
2968
0
  for (x = 0; x < width; ++x) {
2969
0
    dst_r[x] = src_rgb[0];
2970
0
    dst_g[x] = src_rgb[1];
2971
0
    dst_b[x] = src_rgb[2];
2972
0
    src_rgb += 3;
2973
0
  }
2974
0
}
2975
2976
void MergeRGBRow_C(const uint8_t* src_r,
2977
                   const uint8_t* src_g,
2978
                   const uint8_t* src_b,
2979
                   uint8_t* dst_rgb,
2980
0
                   int width) {
2981
0
  int x;
2982
0
  for (x = 0; x < width; ++x) {
2983
0
    dst_rgb[0] = src_r[x];
2984
0
    dst_rgb[1] = src_g[x];
2985
0
    dst_rgb[2] = src_b[x];
2986
0
    dst_rgb += 3;
2987
0
  }
2988
0
}
2989
2990
void SplitARGBRow_C(const uint8_t* src_argb,
2991
                    uint8_t* dst_r,
2992
                    uint8_t* dst_g,
2993
                    uint8_t* dst_b,
2994
                    uint8_t* dst_a,
2995
0
                    int width) {
2996
0
  int x;
2997
0
  for (x = 0; x < width; ++x) {
2998
0
    dst_b[x] = src_argb[0];
2999
0
    dst_g[x] = src_argb[1];
3000
0
    dst_r[x] = src_argb[2];
3001
0
    dst_a[x] = src_argb[3];
3002
0
    src_argb += 4;
3003
0
  }
3004
0
}
3005
3006
void MergeARGBRow_C(const uint8_t* src_r,
3007
                    const uint8_t* src_g,
3008
                    const uint8_t* src_b,
3009
                    const uint8_t* src_a,
3010
                    uint8_t* dst_argb,
3011
0
                    int width) {
3012
0
  int x;
3013
0
  for (x = 0; x < width; ++x) {
3014
0
    dst_argb[0] = src_b[x];
3015
0
    dst_argb[1] = src_g[x];
3016
0
    dst_argb[2] = src_r[x];
3017
0
    dst_argb[3] = src_a[x];
3018
0
    dst_argb += 4;
3019
0
  }
3020
0
}
3021
3022
void MergeXR30Row_C(const uint16_t* src_r,
3023
                    const uint16_t* src_g,
3024
                    const uint16_t* src_b,
3025
                    uint8_t* dst_ar30,
3026
                    int depth,
3027
0
                    int width) {
3028
0
  assert(depth >= 10);
3029
0
  assert(depth <= 16);
3030
0
  int x;
3031
0
  int shift = depth - 10;
3032
0
  uint32_t* dst_ar30_32 = (uint32_t*)dst_ar30;
3033
0
  for (x = 0; x < width; ++x) {
3034
0
    uint32_t r = clamp1023(src_r[x] >> shift);
3035
0
    uint32_t g = clamp1023(src_g[x] >> shift);
3036
0
    uint32_t b = clamp1023(src_b[x] >> shift);
3037
0
    dst_ar30_32[x] = b | (g << 10) | (r << 20) | 0xc0000000;
3038
0
  }
3039
0
}
3040
3041
void MergeAR64Row_C(const uint16_t* src_r,
3042
                    const uint16_t* src_g,
3043
                    const uint16_t* src_b,
3044
                    const uint16_t* src_a,
3045
                    uint16_t* dst_ar64,
3046
                    int depth,
3047
0
                    int width) {
3048
0
  assert(depth >= 1);
3049
0
  assert(depth <= 16);
3050
0
  int x;
3051
0
  int shift = 16 - depth;
3052
0
  int max = (1 << depth) - 1;
3053
0
  for (x = 0; x < width; ++x) {
3054
0
    dst_ar64[0] = STATIC_CAST(uint16_t, ClampMax(src_b[x], max) << shift);
3055
0
    dst_ar64[1] = STATIC_CAST(uint16_t, ClampMax(src_g[x], max) << shift);
3056
0
    dst_ar64[2] = STATIC_CAST(uint16_t, ClampMax(src_r[x], max) << shift);
3057
0
    dst_ar64[3] = STATIC_CAST(uint16_t, ClampMax(src_a[x], max) << shift);
3058
0
    dst_ar64 += 4;
3059
0
  }
3060
0
}
3061
3062
void MergeARGB16To8Row_C(const uint16_t* src_r,
3063
                         const uint16_t* src_g,
3064
                         const uint16_t* src_b,
3065
                         const uint16_t* src_a,
3066
                         uint8_t* dst_argb,
3067
                         int depth,
3068
0
                         int width) {
3069
0
  assert(depth >= 8);
3070
0
  assert(depth <= 16);
3071
0
  int x;
3072
0
  int shift = depth - 8;
3073
0
  for (x = 0; x < width; ++x) {
3074
0
    dst_argb[0] = STATIC_CAST(uint8_t, clamp255(src_b[x] >> shift));
3075
0
    dst_argb[1] = STATIC_CAST(uint8_t, clamp255(src_g[x] >> shift));
3076
0
    dst_argb[2] = STATIC_CAST(uint8_t, clamp255(src_r[x] >> shift));
3077
0
    dst_argb[3] = STATIC_CAST(uint8_t, clamp255(src_a[x] >> shift));
3078
0
    dst_argb += 4;
3079
0
  }
3080
0
}
3081
3082
void MergeXR64Row_C(const uint16_t* src_r,
3083
                    const uint16_t* src_g,
3084
                    const uint16_t* src_b,
3085
                    uint16_t* dst_ar64,
3086
                    int depth,
3087
0
                    int width) {
3088
0
  assert(depth >= 1);
3089
0
  assert(depth <= 16);
3090
0
  int x;
3091
0
  int shift = 16 - depth;
3092
0
  int max = (1 << depth) - 1;
3093
0
  for (x = 0; x < width; ++x) {
3094
0
    dst_ar64[0] = STATIC_CAST(uint16_t, ClampMax(src_b[x], max) << shift);
3095
0
    dst_ar64[1] = STATIC_CAST(uint16_t, ClampMax(src_g[x], max) << shift);
3096
0
    dst_ar64[2] = STATIC_CAST(uint16_t, ClampMax(src_r[x], max) << shift);
3097
0
    dst_ar64[3] = 0xffff;
3098
0
    dst_ar64 += 4;
3099
0
  }
3100
0
}
3101
3102
void MergeXRGB16To8Row_C(const uint16_t* src_r,
3103
                         const uint16_t* src_g,
3104
                         const uint16_t* src_b,
3105
                         uint8_t* dst_argb,
3106
                         int depth,
3107
0
                         int width) {
3108
0
  assert(depth >= 8);
3109
0
  assert(depth <= 16);
3110
0
  int x;
3111
0
  int shift = depth - 8;
3112
0
  for (x = 0; x < width; ++x) {
3113
0
    dst_argb[0] = STATIC_CAST(uint8_t, clamp255(src_b[x] >> shift));
3114
0
    dst_argb[1] = STATIC_CAST(uint8_t, clamp255(src_g[x] >> shift));
3115
0
    dst_argb[2] = STATIC_CAST(uint8_t, clamp255(src_r[x] >> shift));
3116
0
    dst_argb[3] = 0xff;
3117
0
    dst_argb += 4;
3118
0
  }
3119
0
}
3120
3121
void SplitXRGBRow_C(const uint8_t* src_argb,
3122
                    uint8_t* dst_r,
3123
                    uint8_t* dst_g,
3124
                    uint8_t* dst_b,
3125
0
                    int width) {
3126
0
  int x;
3127
0
  for (x = 0; x < width; ++x) {
3128
0
    dst_b[x] = src_argb[0];
3129
0
    dst_g[x] = src_argb[1];
3130
0
    dst_r[x] = src_argb[2];
3131
0
    src_argb += 4;
3132
0
  }
3133
0
}
3134
3135
void MergeXRGBRow_C(const uint8_t* src_r,
3136
                    const uint8_t* src_g,
3137
                    const uint8_t* src_b,
3138
                    uint8_t* dst_argb,
3139
0
                    int width) {
3140
0
  int x;
3141
0
  for (x = 0; x < width; ++x) {
3142
0
    dst_argb[0] = src_b[x];
3143
0
    dst_argb[1] = src_g[x];
3144
0
    dst_argb[2] = src_r[x];
3145
0
    dst_argb[3] = 255;
3146
0
    dst_argb += 4;
3147
0
  }
3148
0
}
3149
3150
// Convert lsb formats to msb, depending on sample depth.
3151
void MergeUVRow_16_C(const uint16_t* src_u,
3152
                     const uint16_t* src_v,
3153
                     uint16_t* dst_uv,
3154
                     int depth,
3155
0
                     int width) {
3156
0
  int shift = 16 - depth;
3157
0
  assert(depth >= 8);
3158
0
  assert(depth <= 16);
3159
0
  int x;
3160
0
  for (x = 0; x < width; ++x) {
3161
0
    dst_uv[0] = STATIC_CAST(uint16_t, src_u[x] << shift);
3162
0
    dst_uv[1] = STATIC_CAST(uint16_t, src_v[x] << shift);
3163
0
    dst_uv += 2;
3164
0
  }
3165
0
}
3166
3167
// Convert msb formats to lsb, depending on sample depth.
3168
void SplitUVRow_16_C(const uint16_t* src_uv,
3169
                     uint16_t* dst_u,
3170
                     uint16_t* dst_v,
3171
                     int depth,
3172
0
                     int width) {
3173
0
  int shift = 16 - depth;
3174
0
  int x;
3175
0
  assert(depth >= 8);
3176
0
  assert(depth <= 16);
3177
0
  for (x = 0; x < width; ++x) {
3178
0
    dst_u[x] = src_uv[0] >> shift;
3179
0
    dst_v[x] = src_uv[1] >> shift;
3180
0
    src_uv += 2;
3181
0
  }
3182
0
}
3183
3184
void MultiplyRow_16_C(const uint16_t* src_y,
3185
                      uint16_t* dst_y,
3186
                      int scale,
3187
0
                      int width) {
3188
0
  int x;
3189
0
  for (x = 0; x < width; ++x) {
3190
0
    dst_y[x] = STATIC_CAST(uint16_t, src_y[x] * scale);
3191
0
  }
3192
0
}
3193
3194
void DivideRow_16_C(const uint16_t* src_y,
3195
                    uint16_t* dst_y,
3196
                    int scale,
3197
0
                    int width) {
3198
0
  int x;
3199
0
  for (x = 0; x < width; ++x) {
3200
0
    dst_y[x] = (src_y[x] * scale) >> 16;
3201
0
  }
3202
0
}
3203
3204
// Use scale to convert lsb formats to msb, depending how many bits there are:
3205
// 32768 = 9 bits
3206
// 16384 = 10 bits
3207
// 4096 = 12 bits
3208
// 256 = 16 bits
3209
// TODO(fbarchard): change scale to bits
3210
#define C16TO8(v, scale) clamp255(((v) * (scale)) >> 16)
3211
3212
void Convert16To8Row_C(const uint16_t* src_y,
3213
                       uint8_t* dst_y,
3214
                       int scale,
3215
0
                       int width) {
3216
0
  int x;
3217
0
  assert(scale >= 256);
3218
0
  assert(scale <= 32768);
3219
3220
0
  for (x = 0; x < width; ++x) {
3221
0
    dst_y[x] = STATIC_CAST(uint8_t, C16TO8(src_y[x], scale));
3222
0
  }
3223
0
}
3224
3225
// Use scale to convert lsb formats to msb, depending how many bits there are:
3226
// 1024 = 10 bits
3227
void Convert8To16Row_C(const uint8_t* src_y,
3228
                       uint16_t* dst_y,
3229
                       int scale,
3230
0
                       int width) {
3231
0
  int x;
3232
0
  scale *= 0x0101;  // replicates the byte.
3233
0
  for (x = 0; x < width; ++x) {
3234
0
    dst_y[x] = (src_y[x] * scale) >> 16;
3235
0
  }
3236
0
}
3237
3238
// Use scale to convert J420 to I420
3239
// scale parameter is 8.8 fixed point but limited to 0 to 255
3240
// Function is based on DivideRow, but adds a bias
3241
// Does not clamp
3242
void Convert8To8Row_C(const uint8_t* src_y,
3243
                      uint8_t* dst_y,
3244
                      int scale,
3245
                      int bias,
3246
0
                      int width) {
3247
0
  int x;
3248
0
  assert(scale >= 0);
3249
0
  assert(scale <= 255);
3250
3251
0
  for (x = 0; x < width; ++x) {
3252
0
    dst_y[x] = ((src_y[x] * scale) >> 8) + bias;
3253
0
  }
3254
0
}
3255
3256
0
void CopyRow_C(const uint8_t* src, uint8_t* dst, int count) {
3257
0
  memcpy(dst, src, count);
3258
0
}
3259
3260
0
void CopyRow_16_C(const uint16_t* src, uint16_t* dst, int count) {
3261
0
  memcpy(dst, src, (size_t)count * 2);
3262
0
}
3263
3264
0
void SetRow_C(uint8_t* dst, uint8_t v8, int width) {
3265
0
  memset(dst, v8, width);
3266
0
}
3267
3268
0
void ARGBSetRow_C(uint8_t* dst_argb, uint32_t v32, int width) {
3269
0
  int x;
3270
0
  for (x = 0; x < width; ++x) {
3271
0
    memcpy(dst_argb + x * sizeof v32, &v32, sizeof v32);
3272
0
  }
3273
0
}
3274
3275
// Filter 2 rows of YUY2 UV's (422) into U and V (420).
3276
void YUY2ToUVRow_C(const uint8_t* src_yuy2,
3277
                   int src_stride_yuy2,
3278
                   uint8_t* dst_u,
3279
                   uint8_t* dst_v,
3280
0
                   int width) {
3281
  // Output a row of UV values, filtering 2 rows of YUY2.
3282
0
  int x;
3283
0
  for (x = 0; x < width; x += 2) {
3284
0
    const uint8_t* src_yuy2_next = src_yuy2 + src_stride_yuy2;
3285
0
    dst_u[0] = (src_yuy2[1] + src_yuy2_next[1] + 1) >> 1;
3286
0
    dst_v[0] = (src_yuy2[3] + src_yuy2_next[3] + 1) >> 1;
3287
0
    src_yuy2 += 4;
3288
0
    dst_u += 1;
3289
0
    dst_v += 1;
3290
0
  }
3291
0
}
3292
3293
// Filter 2 rows of YUY2 UV's (422) into UV (NV12).
3294
void YUY2ToNVUVRow_C(const uint8_t* src_yuy2,
3295
                     int src_stride_yuy2,
3296
                     uint8_t* dst_uv,
3297
0
                     int width) {
3298
  // Output a row of UV values, filtering 2 rows of YUY2.
3299
0
  int x;
3300
0
  for (x = 0; x < width; x += 2) {
3301
0
    const uint8_t* src_yuy2_next = src_yuy2 + src_stride_yuy2;
3302
0
    dst_uv[0] = (src_yuy2[1] + src_yuy2_next[1] + 1) >> 1;
3303
0
    dst_uv[1] = (src_yuy2[3] + src_yuy2_next[3] + 1) >> 1;
3304
0
    src_yuy2 += 4;
3305
0
    dst_uv += 2;
3306
0
  }
3307
0
}
3308
3309
// Copy row of YUY2 UV's (422) into U and V (422).
3310
void YUY2ToUV422Row_C(const uint8_t* src_yuy2,
3311
                      uint8_t* dst_u,
3312
                      uint8_t* dst_v,
3313
0
                      int width) {
3314
  // Output a row of UV values.
3315
0
  int x;
3316
0
  for (x = 0; x < width; x += 2) {
3317
0
    dst_u[0] = src_yuy2[1];
3318
0
    dst_v[0] = src_yuy2[3];
3319
0
    src_yuy2 += 4;
3320
0
    dst_u += 1;
3321
0
    dst_v += 1;
3322
0
  }
3323
0
}
3324
3325
// Copy row of YUY2 Y's (422) into Y (420/422).
3326
0
void YUY2ToYRow_C(const uint8_t* src_yuy2, uint8_t* dst_y, int width) {
3327
  // Output a row of Y values.
3328
0
  int x;
3329
0
  for (x = 0; x < width - 1; x += 2) {
3330
0
    dst_y[x] = src_yuy2[0];
3331
0
    dst_y[x + 1] = src_yuy2[2];
3332
0
    src_yuy2 += 4;
3333
0
  }
3334
0
  if (width & 1) {
3335
0
    dst_y[width - 1] = src_yuy2[0];
3336
0
  }
3337
0
}
3338
3339
// Filter 2 rows of UYVY UV's (422) into U and V (420).
3340
void UYVYToUVRow_C(const uint8_t* src_uyvy,
3341
                   int src_stride_uyvy,
3342
                   uint8_t* dst_u,
3343
                   uint8_t* dst_v,
3344
0
                   int width) {
3345
  // Output a row of UV values.
3346
0
  int x;
3347
0
  for (x = 0; x < width; x += 2) {
3348
0
    const uint8_t* src_uyvy_next = src_uyvy + src_stride_uyvy;
3349
0
    dst_u[0] = (src_uyvy[0] + src_uyvy_next[0] + 1) >> 1;
3350
0
    dst_v[0] = (src_uyvy[2] + src_uyvy_next[2] + 1) >> 1;
3351
0
    src_uyvy += 4;
3352
0
    dst_u += 1;
3353
0
    dst_v += 1;
3354
0
  }
3355
0
}
3356
3357
// Copy row of UYVY UV's (422) into U and V (422).
3358
void UYVYToUV422Row_C(const uint8_t* src_uyvy,
3359
                      uint8_t* dst_u,
3360
                      uint8_t* dst_v,
3361
0
                      int width) {
3362
  // Output a row of UV values.
3363
0
  int x;
3364
0
  for (x = 0; x < width; x += 2) {
3365
0
    dst_u[0] = src_uyvy[0];
3366
0
    dst_v[0] = src_uyvy[2];
3367
0
    src_uyvy += 4;
3368
0
    dst_u += 1;
3369
0
    dst_v += 1;
3370
0
  }
3371
0
}
3372
3373
// Copy row of UYVY Y's (422) into Y (420/422).
3374
0
void UYVYToYRow_C(const uint8_t* src_uyvy, uint8_t* dst_y, int width) {
3375
  // Output a row of Y values.
3376
0
  int x;
3377
0
  for (x = 0; x < width - 1; x += 2) {
3378
0
    dst_y[x] = src_uyvy[1];
3379
0
    dst_y[x + 1] = src_uyvy[3];
3380
0
    src_uyvy += 4;
3381
0
  }
3382
0
  if (width & 1) {
3383
0
    dst_y[width - 1] = src_uyvy[1];
3384
0
  }
3385
0
}
3386
3387
#define BLEND(f, b, a) clamp255((((256 - a) * b) >> 8) + f)
3388
3389
// Blend src_argb over src_argb1 and store to dst_argb.
3390
// dst_argb may be src_argb or src_argb1.
3391
// This code mimics the SSSE3 version for better testability.
3392
void ARGBBlendRow_C(const uint8_t* src_argb,
3393
                    const uint8_t* src_argb1,
3394
                    uint8_t* dst_argb,
3395
0
                    int width) {
3396
0
  int x;
3397
0
  for (x = 0; x < width - 1; x += 2) {
3398
0
    uint32_t fb = src_argb[0];
3399
0
    uint32_t fg = src_argb[1];
3400
0
    uint32_t fr = src_argb[2];
3401
0
    uint32_t a = src_argb[3];
3402
0
    uint32_t bb = src_argb1[0];
3403
0
    uint32_t bg = src_argb1[1];
3404
0
    uint32_t br = src_argb1[2];
3405
0
    dst_argb[0] = STATIC_CAST(uint8_t, BLEND(fb, bb, a));
3406
0
    dst_argb[1] = STATIC_CAST(uint8_t, BLEND(fg, bg, a));
3407
0
    dst_argb[2] = STATIC_CAST(uint8_t, BLEND(fr, br, a));
3408
0
    dst_argb[3] = 255u;
3409
3410
0
    fb = src_argb[4 + 0];
3411
0
    fg = src_argb[4 + 1];
3412
0
    fr = src_argb[4 + 2];
3413
0
    a = src_argb[4 + 3];
3414
0
    bb = src_argb1[4 + 0];
3415
0
    bg = src_argb1[4 + 1];
3416
0
    br = src_argb1[4 + 2];
3417
0
    dst_argb[4 + 0] = STATIC_CAST(uint8_t, BLEND(fb, bb, a));
3418
0
    dst_argb[4 + 1] = STATIC_CAST(uint8_t, BLEND(fg, bg, a));
3419
0
    dst_argb[4 + 2] = STATIC_CAST(uint8_t, BLEND(fr, br, a));
3420
0
    dst_argb[4 + 3] = 255u;
3421
0
    src_argb += 8;
3422
0
    src_argb1 += 8;
3423
0
    dst_argb += 8;
3424
0
  }
3425
3426
0
  if (width & 1) {
3427
0
    uint32_t fb = src_argb[0];
3428
0
    uint32_t fg = src_argb[1];
3429
0
    uint32_t fr = src_argb[2];
3430
0
    uint32_t a = src_argb[3];
3431
0
    uint32_t bb = src_argb1[0];
3432
0
    uint32_t bg = src_argb1[1];
3433
0
    uint32_t br = src_argb1[2];
3434
0
    dst_argb[0] = STATIC_CAST(uint8_t, BLEND(fb, bb, a));
3435
0
    dst_argb[1] = STATIC_CAST(uint8_t, BLEND(fg, bg, a));
3436
0
    dst_argb[2] = STATIC_CAST(uint8_t, BLEND(fr, br, a));
3437
0
    dst_argb[3] = 255u;
3438
0
  }
3439
0
}
3440
#undef BLEND
3441
3442
0
#define UBLEND(f, b, a) (((a) * f) + ((255 - a) * b) + 255) >> 8
3443
void BlendPlaneRow_C(const uint8_t* src0,
3444
                     const uint8_t* src1,
3445
                     const uint8_t* alpha,
3446
                     uint8_t* dst,
3447
0
                     int width) {
3448
0
  int x;
3449
0
  for (x = 0; x < width - 1; x += 2) {
3450
0
    dst[0] = UBLEND(src0[0], src1[0], alpha[0]);
3451
0
    dst[1] = UBLEND(src0[1], src1[1], alpha[1]);
3452
0
    src0 += 2;
3453
0
    src1 += 2;
3454
0
    alpha += 2;
3455
0
    dst += 2;
3456
0
  }
3457
0
  if (width & 1) {
3458
0
    dst[0] = UBLEND(src0[0], src1[0], alpha[0]);
3459
0
  }
3460
0
}
3461
#undef UBLEND
3462
3463
0
#define ATTENUATE(f, a) (f * a + 255) >> 8
3464
3465
// Multiply source RGB by alpha and store to destination.
3466
0
void ARGBAttenuateRow_C(const uint8_t* src_argb, uint8_t* dst_argb, int width) {
3467
0
  int i;
3468
0
  for (i = 0; i < width - 1; i += 2) {
3469
0
    uint32_t b = src_argb[0];
3470
0
    uint32_t g = src_argb[1];
3471
0
    uint32_t r = src_argb[2];
3472
0
    uint32_t a = src_argb[3];
3473
0
    dst_argb[0] = ATTENUATE(b, a);
3474
0
    dst_argb[1] = ATTENUATE(g, a);
3475
0
    dst_argb[2] = ATTENUATE(r, a);
3476
0
    dst_argb[3] = STATIC_CAST(uint8_t, a);
3477
0
    b = src_argb[4];
3478
0
    g = src_argb[5];
3479
0
    r = src_argb[6];
3480
0
    a = src_argb[7];
3481
0
    dst_argb[4] = ATTENUATE(b, a);
3482
0
    dst_argb[5] = ATTENUATE(g, a);
3483
0
    dst_argb[6] = ATTENUATE(r, a);
3484
0
    dst_argb[7] = STATIC_CAST(uint8_t, a);
3485
0
    src_argb += 8;
3486
0
    dst_argb += 8;
3487
0
  }
3488
3489
0
  if (width & 1) {
3490
0
    const uint32_t b = src_argb[0];
3491
0
    const uint32_t g = src_argb[1];
3492
0
    const uint32_t r = src_argb[2];
3493
0
    const uint32_t a = src_argb[3];
3494
0
    dst_argb[0] = ATTENUATE(b, a);
3495
0
    dst_argb[1] = ATTENUATE(g, a);
3496
0
    dst_argb[2] = ATTENUATE(r, a);
3497
0
    dst_argb[3] = STATIC_CAST(uint8_t, a);
3498
0
  }
3499
0
}
3500
#undef ATTENUATE
3501
3502
// Divide source RGB by alpha and store to destination.
3503
// b = (b * 255 + (a / 2)) / a;
3504
// g = (g * 255 + (a / 2)) / a;
3505
// r = (r * 255 + (a / 2)) / a;
3506
// Reciprocal method is off by 1 on some values. ie 125
3507
// 8.8 fixed point inverse table with 1.0 in upper short and 1 / a in lower.
3508
#define T(a) 0x01000000 + (0x10000 / a)
3509
const uint32_t fixed_invtbl8[256] = {
3510
    0x01000000, 0x0100ffff, T(0x02), T(0x03),   T(0x04), T(0x05), T(0x06),
3511
    T(0x07),    T(0x08),    T(0x09), T(0x0a),   T(0x0b), T(0x0c), T(0x0d),
3512
    T(0x0e),    T(0x0f),    T(0x10), T(0x11),   T(0x12), T(0x13), T(0x14),
3513
    T(0x15),    T(0x16),    T(0x17), T(0x18),   T(0x19), T(0x1a), T(0x1b),
3514
    T(0x1c),    T(0x1d),    T(0x1e), T(0x1f),   T(0x20), T(0x21), T(0x22),
3515
    T(0x23),    T(0x24),    T(0x25), T(0x26),   T(0x27), T(0x28), T(0x29),
3516
    T(0x2a),    T(0x2b),    T(0x2c), T(0x2d),   T(0x2e), T(0x2f), T(0x30),
3517
    T(0x31),    T(0x32),    T(0x33), T(0x34),   T(0x35), T(0x36), T(0x37),
3518
    T(0x38),    T(0x39),    T(0x3a), T(0x3b),   T(0x3c), T(0x3d), T(0x3e),
3519
    T(0x3f),    T(0x40),    T(0x41), T(0x42),   T(0x43), T(0x44), T(0x45),
3520
    T(0x46),    T(0x47),    T(0x48), T(0x49),   T(0x4a), T(0x4b), T(0x4c),
3521
    T(0x4d),    T(0x4e),    T(0x4f), T(0x50),   T(0x51), T(0x52), T(0x53),
3522
    T(0x54),    T(0x55),    T(0x56), T(0x57),   T(0x58), T(0x59), T(0x5a),
3523
    T(0x5b),    T(0x5c),    T(0x5d), T(0x5e),   T(0x5f), T(0x60), T(0x61),
3524
    T(0x62),    T(0x63),    T(0x64), T(0x65),   T(0x66), T(0x67), T(0x68),
3525
    T(0x69),    T(0x6a),    T(0x6b), T(0x6c),   T(0x6d), T(0x6e), T(0x6f),
3526
    T(0x70),    T(0x71),    T(0x72), T(0x73),   T(0x74), T(0x75), T(0x76),
3527
    T(0x77),    T(0x78),    T(0x79), T(0x7a),   T(0x7b), T(0x7c), T(0x7d),
3528
    T(0x7e),    T(0x7f),    T(0x80), T(0x81),   T(0x82), T(0x83), T(0x84),
3529
    T(0x85),    T(0x86),    T(0x87), T(0x88),   T(0x89), T(0x8a), T(0x8b),
3530
    T(0x8c),    T(0x8d),    T(0x8e), T(0x8f),   T(0x90), T(0x91), T(0x92),
3531
    T(0x93),    T(0x94),    T(0x95), T(0x96),   T(0x97), T(0x98), T(0x99),
3532
    T(0x9a),    T(0x9b),    T(0x9c), T(0x9d),   T(0x9e), T(0x9f), T(0xa0),
3533
    T(0xa1),    T(0xa2),    T(0xa3), T(0xa4),   T(0xa5), T(0xa6), T(0xa7),
3534
    T(0xa8),    T(0xa9),    T(0xaa), T(0xab),   T(0xac), T(0xad), T(0xae),
3535
    T(0xaf),    T(0xb0),    T(0xb1), T(0xb2),   T(0xb3), T(0xb4), T(0xb5),
3536
    T(0xb6),    T(0xb7),    T(0xb8), T(0xb9),   T(0xba), T(0xbb), T(0xbc),
3537
    T(0xbd),    T(0xbe),    T(0xbf), T(0xc0),   T(0xc1), T(0xc2), T(0xc3),
3538
    T(0xc4),    T(0xc5),    T(0xc6), T(0xc7),   T(0xc8), T(0xc9), T(0xca),
3539
    T(0xcb),    T(0xcc),    T(0xcd), T(0xce),   T(0xcf), T(0xd0), T(0xd1),
3540
    T(0xd2),    T(0xd3),    T(0xd4), T(0xd5),   T(0xd6), T(0xd7), T(0xd8),
3541
    T(0xd9),    T(0xda),    T(0xdb), T(0xdc),   T(0xdd), T(0xde), T(0xdf),
3542
    T(0xe0),    T(0xe1),    T(0xe2), T(0xe3),   T(0xe4), T(0xe5), T(0xe6),
3543
    T(0xe7),    T(0xe8),    T(0xe9), T(0xea),   T(0xeb), T(0xec), T(0xed),
3544
    T(0xee),    T(0xef),    T(0xf0), T(0xf1),   T(0xf2), T(0xf3), T(0xf4),
3545
    T(0xf5),    T(0xf6),    T(0xf7), T(0xf8),   T(0xf9), T(0xfa), T(0xfb),
3546
    T(0xfc),    T(0xfd),    T(0xfe), 0x01000100};
3547
#undef T
3548
3549
// This code mimics the Intel SIMD version for better testability.
3550
#define UNATTENUATE(f, ia) clamp255(((f | (f << 8)) * ia) >> 16)
3551
3552
// mimics the Intel SIMD code for exactness.
3553
void ARGBUnattenuateRow_C(const uint8_t* src_argb,
3554
                          uint8_t* dst_argb,
3555
0
                          int width) {
3556
0
  int i;
3557
0
  for (i = 0; i < width; ++i) {
3558
0
    uint32_t b = src_argb[0];
3559
0
    uint32_t g = src_argb[1];
3560
0
    uint32_t r = src_argb[2];
3561
0
    const uint32_t a = src_argb[3];
3562
0
    const uint32_t ia = fixed_invtbl8[a] & 0xffff;  // 8.8 fixed point
3563
3564
    // Clamping should not be necessary but is free in assembly.
3565
0
    dst_argb[0] = STATIC_CAST(uint8_t, UNATTENUATE(b, ia));
3566
0
    dst_argb[1] = STATIC_CAST(uint8_t, UNATTENUATE(g, ia));
3567
0
    dst_argb[2] = STATIC_CAST(uint8_t, UNATTENUATE(r, ia));
3568
0
    dst_argb[3] = STATIC_CAST(uint8_t, a);
3569
0
    src_argb += 4;
3570
0
    dst_argb += 4;
3571
0
  }
3572
0
}
3573
3574
void ComputeCumulativeSumRow_C(const uint8_t* row,
3575
                               int32_t* cumsum,
3576
                               const int32_t* previous_cumsum,
3577
0
                               int width) {
3578
0
  int32_t row_sum[4] = {0, 0, 0, 0};
3579
0
  int x;
3580
0
  for (x = 0; x < width; ++x) {
3581
0
    row_sum[0] += row[x * 4 + 0];
3582
0
    row_sum[1] += row[x * 4 + 1];
3583
0
    row_sum[2] += row[x * 4 + 2];
3584
0
    row_sum[3] += row[x * 4 + 3];
3585
0
    cumsum[x * 4 + 0] = row_sum[0] + previous_cumsum[x * 4 + 0];
3586
0
    cumsum[x * 4 + 1] = row_sum[1] + previous_cumsum[x * 4 + 1];
3587
0
    cumsum[x * 4 + 2] = row_sum[2] + previous_cumsum[x * 4 + 2];
3588
0
    cumsum[x * 4 + 3] = row_sum[3] + previous_cumsum[x * 4 + 3];
3589
0
  }
3590
0
}
3591
3592
void CumulativeSumToAverageRow_C(const int32_t* tl,
3593
                                 const int32_t* bl,
3594
                                 int w,
3595
                                 int area,
3596
                                 uint8_t* dst,
3597
0
                                 int count) {
3598
0
  float ooa;
3599
0
  int i;
3600
0
  assert(area != 0);
3601
3602
0
  ooa = 1.0f / STATIC_CAST(float, area);
3603
0
  for (i = 0; i < count; ++i) {
3604
0
    dst[0] =
3605
0
        (uint8_t)(STATIC_CAST(float, bl[w + 0] + tl[0] - bl[0] - tl[w + 0]) *
3606
0
                  ooa);
3607
0
    dst[1] =
3608
0
        (uint8_t)(STATIC_CAST(float, bl[w + 1] + tl[1] - bl[1] - tl[w + 1]) *
3609
0
                  ooa);
3610
0
    dst[2] =
3611
0
        (uint8_t)(STATIC_CAST(float, bl[w + 2] + tl[2] - bl[2] - tl[w + 2]) *
3612
0
                  ooa);
3613
0
    dst[3] =
3614
0
        (uint8_t)(STATIC_CAST(float, bl[w + 3] + tl[3] - bl[3] - tl[w + 3]) *
3615
0
                  ooa);
3616
0
    dst += 4;
3617
0
    tl += 4;
3618
0
    bl += 4;
3619
0
  }
3620
0
}
3621
3622
// Copy pixels from rotated source to destination row with a slope.
3623
LIBYUV_API
3624
void ARGBAffineRow_C(const uint8_t* src_argb,
3625
                     int src_argb_stride,
3626
                     uint8_t* dst_argb,
3627
                     const float* uv_dudv,
3628
0
                     int width) {
3629
0
  int i;
3630
  // Render a row of pixels from source into a buffer.
3631
0
  float uv[2];
3632
0
  uv[0] = uv_dudv[0];
3633
0
  uv[1] = uv_dudv[1];
3634
0
  for (i = 0; i < width; ++i) {
3635
0
    int x = (int)(uv[0]);
3636
0
    int y = (int)(uv[1]);
3637
0
    *(uint32_t*)(dst_argb) =
3638
0
        *(const uint32_t*)(src_argb + (ptrdiff_t)y * src_argb_stride +
3639
0
                           (ptrdiff_t)x * 4);
3640
0
    dst_argb += 4;
3641
0
    uv[0] += uv_dudv[2];
3642
0
    uv[1] += uv_dudv[3];
3643
0
  }
3644
0
}
3645
3646
// Blend 2 rows into 1.
3647
static void HalfRow_C(const uint8_t* src_uv,
3648
                      ptrdiff_t src_uv_stride,
3649
                      uint8_t* dst_uv,
3650
0
                      int width) {
3651
0
  int x;
3652
0
  for (x = 0; x < width; ++x) {
3653
0
    dst_uv[x] = (src_uv[x] + src_uv[src_uv_stride + x] + 1) >> 1;
3654
0
  }
3655
0
}
3656
3657
static void HalfRow_16_C(const uint16_t* src_uv,
3658
                         ptrdiff_t src_uv_stride,
3659
                         uint16_t* dst_uv,
3660
0
                         int width) {
3661
0
  int x;
3662
0
  for (x = 0; x < width; ++x) {
3663
0
    dst_uv[x] = (src_uv[x] + src_uv[src_uv_stride + x] + 1) >> 1;
3664
0
  }
3665
0
}
3666
3667
void HalfRow_16To8_C(const uint16_t* src_uv,
3668
                     ptrdiff_t src_uv_stride,
3669
                     uint8_t* dst_uv,
3670
                     int scale,
3671
0
                     int width) {
3672
0
  int x;
3673
0
  for (x = 0; x < width; ++x) {
3674
0
    dst_uv[x] = STATIC_CAST(
3675
0
        uint8_t,
3676
0
        C16TO8((src_uv[x] + src_uv[src_uv_stride + x] + 1) >> 1, scale));
3677
0
  }
3678
0
}
3679
3680
void HalfWidthRow_16To8_C(const uint16_t* src_uv,
3681
                          ptrdiff_t src_uv_stride,
3682
                          uint8_t* dst_uv,
3683
                          int scale,
3684
0
                          int width) {
3685
0
  const uint16_t* s = src_uv;
3686
0
  const uint16_t* t = src_uv + src_uv_stride;
3687
0
  int x;
3688
0
  for (x = 0; x < width - 1; x += 2) {
3689
0
    dst_uv[0] = STATIC_CAST(
3690
0
        uint8_t,
3691
0
        C16TO8((s[0] + s[1] + t[0] + t[1] + 2) >> 2, scale));
3692
0
    dst_uv[1] = STATIC_CAST(
3693
0
        uint8_t,
3694
0
        C16TO8((s[2] + s[3] + t[2] + t[3] + 2) >> 2, scale));
3695
0
    dst_uv += 2;
3696
0
    s += 4;
3697
0
    t += 4;
3698
0
  }
3699
0
  if (width & 1) {
3700
0
    dst_uv[0] = STATIC_CAST(
3701
0
        uint8_t,
3702
0
        C16TO8((s[0] + s[1] + t[0] + t[1] + 2) >> 2, scale));
3703
0
  }
3704
0
}
3705
3706
void HalfWidthRow_16To8_Odd_C(const uint16_t* src_uv,
3707
                              ptrdiff_t src_uv_stride,
3708
                              uint8_t* dst_uv,
3709
                              int scale,
3710
0
                              int width) {
3711
0
  const uint16_t* s = src_uv;
3712
0
  const uint16_t* t = src_uv + src_uv_stride;
3713
0
  int x;
3714
0
  width -= 1;
3715
0
  for (x = 0; x < width - 1; x += 2) {
3716
0
    dst_uv[0] = STATIC_CAST(
3717
0
        uint8_t,
3718
0
        C16TO8((s[0] + s[1] + t[0] + t[1] + 2) >> 2, scale));
3719
0
    dst_uv[1] = STATIC_CAST(
3720
0
        uint8_t,
3721
0
        C16TO8((s[2] + s[3] + t[2] + t[3] + 2) >> 2, scale));
3722
0
    dst_uv += 2;
3723
0
    s += 4;
3724
0
    t += 4;
3725
0
  }
3726
0
  if (width & 1) {
3727
0
    dst_uv[0] = STATIC_CAST(
3728
0
        uint8_t,
3729
0
        C16TO8((s[0] + s[1] + t[0] + t[1] + 2) >> 2, scale));
3730
0
    dst_uv += 1;
3731
0
    s += 2;
3732
0
    t += 2;
3733
0
  }
3734
0
  dst_uv[0] = STATIC_CAST(uint8_t, C16TO8((s[0] + t[0] + 1) >> 1, scale));
3735
0
}
3736
3737
// C version 2x2 -> 2x1.
3738
void InterpolateRow_C(uint8_t* dst_ptr,
3739
                      const uint8_t* src_ptr,
3740
                      ptrdiff_t src_stride,
3741
                      int width,
3742
0
                      int source_y_fraction) {
3743
0
  int y1_fraction = source_y_fraction;
3744
0
  int y0_fraction = 256 - y1_fraction;
3745
0
  const uint8_t* src_ptr1 = src_ptr + src_stride;
3746
0
  int x;
3747
0
  assert(source_y_fraction >= 0);
3748
0
  assert(source_y_fraction < 256);
3749
3750
0
  if (y1_fraction == 0) {
3751
0
    memcpy(dst_ptr, src_ptr, width);
3752
0
    return;
3753
0
  }
3754
0
  if (y1_fraction == 128) {
3755
0
    HalfRow_C(src_ptr, src_stride, dst_ptr, width);
3756
0
    return;
3757
0
  }
3758
0
  for (x = 0; x < width; ++x) {
3759
0
    dst_ptr[0] = STATIC_CAST(
3760
0
        uint8_t,
3761
0
        (src_ptr[0] * y0_fraction + src_ptr1[0] * y1_fraction + 128) >> 8);
3762
0
    ++src_ptr;
3763
0
    ++src_ptr1;
3764
0
    ++dst_ptr;
3765
0
  }
3766
0
}
3767
3768
// C version 2x2 -> 2x1.
3769
void InterpolateRow_16_C(uint16_t* dst_ptr,
3770
                         const uint16_t* src_ptr,
3771
                         ptrdiff_t src_stride,
3772
                         int width,
3773
0
                         int source_y_fraction) {
3774
0
  int y1_fraction = source_y_fraction;
3775
0
  int y0_fraction = 256 - y1_fraction;
3776
0
  const uint16_t* src_ptr1 = src_ptr + src_stride;
3777
0
  int x;
3778
0
  assert(source_y_fraction >= 0);
3779
0
  assert(source_y_fraction < 256);
3780
3781
0
  if (y1_fraction == 0) {
3782
0
    memcpy(dst_ptr, src_ptr, (size_t)width * 2);
3783
0
    return;
3784
0
  }
3785
0
  if (y1_fraction == 128) {
3786
0
    HalfRow_16_C(src_ptr, src_stride, dst_ptr, width);
3787
0
    return;
3788
0
  }
3789
0
  for (x = 0; x < width; ++x) {
3790
0
    dst_ptr[0] = STATIC_CAST(
3791
0
        uint16_t,
3792
0
        (src_ptr[0] * y0_fraction + src_ptr1[0] * y1_fraction + 128) >> 8);
3793
0
    ++src_ptr;
3794
0
    ++src_ptr1;
3795
0
    ++dst_ptr;
3796
0
  }
3797
0
}
3798
3799
// Use first 4 shuffler values to reorder ARGB channels.
3800
void ARGBShuffleRow_C(const uint8_t* src_argb,
3801
                      uint8_t* dst_argb,
3802
                      const uint8_t* shuffler,
3803
0
                      int width) {
3804
0
  int index0 = shuffler[0];
3805
0
  int index1 = shuffler[1];
3806
0
  int index2 = shuffler[2];
3807
0
  int index3 = shuffler[3];
3808
  // Shuffle a row of ARGB.
3809
0
  int x;
3810
0
  for (x = 0; x < width; ++x) {
3811
    // To support in-place conversion.
3812
0
    uint8_t b = src_argb[index0];
3813
0
    uint8_t g = src_argb[index1];
3814
0
    uint8_t r = src_argb[index2];
3815
0
    uint8_t a = src_argb[index3];
3816
0
    dst_argb[0] = b;
3817
0
    dst_argb[1] = g;
3818
0
    dst_argb[2] = r;
3819
0
    dst_argb[3] = a;
3820
0
    src_argb += 4;
3821
0
    dst_argb += 4;
3822
0
  }
3823
0
}
3824
3825
void I422ToYUY2Row_C(const uint8_t* src_y,
3826
                     const uint8_t* src_u,
3827
                     const uint8_t* src_v,
3828
                     uint8_t* dst_frame,
3829
0
                     int width) {
3830
0
  int x;
3831
0
  for (x = 0; x < width - 1; x += 2) {
3832
0
    dst_frame[0] = src_y[0];
3833
0
    dst_frame[1] = src_u[0];
3834
0
    dst_frame[2] = src_y[1];
3835
0
    dst_frame[3] = src_v[0];
3836
0
    dst_frame += 4;
3837
0
    src_y += 2;
3838
0
    src_u += 1;
3839
0
    src_v += 1;
3840
0
  }
3841
0
  if (width & 1) {
3842
0
    dst_frame[0] = src_y[0];
3843
0
    dst_frame[1] = src_u[0];
3844
0
    dst_frame[2] = 0;
3845
0
    dst_frame[3] = src_v[0];
3846
0
  }
3847
0
}
3848
3849
void I422ToUYVYRow_C(const uint8_t* src_y,
3850
                     const uint8_t* src_u,
3851
                     const uint8_t* src_v,
3852
                     uint8_t* dst_frame,
3853
0
                     int width) {
3854
0
  int x;
3855
0
  for (x = 0; x < width - 1; x += 2) {
3856
0
    dst_frame[0] = src_u[0];
3857
0
    dst_frame[1] = src_y[0];
3858
0
    dst_frame[2] = src_v[0];
3859
0
    dst_frame[3] = src_y[1];
3860
0
    dst_frame += 4;
3861
0
    src_y += 2;
3862
0
    src_u += 1;
3863
0
    src_v += 1;
3864
0
  }
3865
0
  if (width & 1) {
3866
0
    dst_frame[0] = src_u[0];
3867
0
    dst_frame[1] = src_y[0];
3868
0
    dst_frame[2] = src_v[0];
3869
0
    dst_frame[3] = 0;
3870
0
  }
3871
0
}
3872
3873
void ARGBPolynomialRow_C(const uint8_t* src_argb,
3874
                         uint8_t* dst_argb,
3875
                         const float* poly,
3876
0
                         int width) {
3877
0
  int i;
3878
0
  for (i = 0; i < width; ++i) {
3879
0
    float b = (float)(src_argb[0]);
3880
0
    float g = (float)(src_argb[1]);
3881
0
    float r = (float)(src_argb[2]);
3882
0
    float a = (float)(src_argb[3]);
3883
0
    float b2 = b * b;
3884
0
    float g2 = g * g;
3885
0
    float r2 = r * r;
3886
0
    float a2 = a * a;
3887
0
    float db = poly[0] + poly[4] * b;
3888
0
    float dg = poly[1] + poly[5] * g;
3889
0
    float dr = poly[2] + poly[6] * r;
3890
0
    float da = poly[3] + poly[7] * a;
3891
0
    float b3 = b2 * b;
3892
0
    float g3 = g2 * g;
3893
0
    float r3 = r2 * r;
3894
0
    float a3 = a2 * a;
3895
0
    db += poly[8] * b2;
3896
0
    dg += poly[9] * g2;
3897
0
    dr += poly[10] * r2;
3898
0
    da += poly[11] * a2;
3899
0
    db += poly[12] * b3;
3900
0
    dg += poly[13] * g3;
3901
0
    dr += poly[14] * r3;
3902
0
    da += poly[15] * a3;
3903
3904
0
    dst_argb[0] = STATIC_CAST(uint8_t, Clamp((int32_t)(db)));
3905
0
    dst_argb[1] = STATIC_CAST(uint8_t, Clamp((int32_t)(dg)));
3906
0
    dst_argb[2] = STATIC_CAST(uint8_t, Clamp((int32_t)(dr)));
3907
0
    dst_argb[3] = STATIC_CAST(uint8_t, Clamp((int32_t)(da)));
3908
0
    src_argb += 4;
3909
0
    dst_argb += 4;
3910
0
  }
3911
0
}
3912
3913
// Samples assumed to be unsigned in low 9, 10 or 12 bits.  Scale factor
3914
// adjust the source integer range to the half float range desired.
3915
3916
// This magic constant is 2^-112. Multiplying by this
3917
// is the same as subtracting 112 from the exponent, which
3918
// is the difference in exponent bias between 32-bit and
3919
// 16-bit floats. Once we've done this subtraction, we can
3920
// simply extract the low bits of the exponent and the high
3921
// bits of the mantissa from our float and we're done.
3922
3923
// Work around GCC 7 punning warning -Wstrict-aliasing
3924
#if defined(__GNUC__)
3925
typedef uint32_t __attribute__((__may_alias__)) uint32_alias_t;
3926
#else
3927
typedef uint32_t uint32_alias_t;
3928
#endif
3929
3930
void HalfFloatRow_C(const uint16_t* src,
3931
                    uint16_t* dst,
3932
                    float scale,
3933
0
                    int width) {
3934
0
  int i;
3935
0
  float mult = 1.9259299444e-34f * scale;
3936
0
  for (i = 0; i < width; ++i) {
3937
0
    float value = src[i] * mult;
3938
0
    dst[i] = (uint16_t)((*(const uint32_alias_t*)&value) >> 13);
3939
0
  }
3940
0
}
3941
3942
0
void ByteToFloatRow_C(const uint8_t* src, float* dst, float scale, int width) {
3943
0
  int i;
3944
0
  for (i = 0; i < width; ++i) {
3945
0
    float value = src[i] * scale;
3946
0
    dst[i] = value;
3947
0
  }
3948
0
}
3949
3950
void ARGBLumaColorTableRow_C(const uint8_t* src_argb,
3951
                             uint8_t* dst_argb,
3952
                             int width,
3953
                             const uint8_t* luma,
3954
0
                             uint32_t lumacoeff) {
3955
0
  uint32_t bc = lumacoeff & 0xff;
3956
0
  uint32_t gc = (lumacoeff >> 8) & 0xff;
3957
0
  uint32_t rc = (lumacoeff >> 16) & 0xff;
3958
3959
0
  int i;
3960
0
  for (i = 0; i < width - 1; i += 2) {
3961
    // Luminance in rows, color values in columns.
3962
0
    const uint8_t* luma0 =
3963
0
        ((src_argb[0] * bc + src_argb[1] * gc + src_argb[2] * rc) & 0x7F00u) +
3964
0
        luma;
3965
0
    const uint8_t* luma1;
3966
0
    dst_argb[0] = luma0[src_argb[0]];
3967
0
    dst_argb[1] = luma0[src_argb[1]];
3968
0
    dst_argb[2] = luma0[src_argb[2]];
3969
0
    dst_argb[3] = src_argb[3];
3970
0
    luma1 =
3971
0
        ((src_argb[4] * bc + src_argb[5] * gc + src_argb[6] * rc) & 0x7F00u) +
3972
0
        luma;
3973
0
    dst_argb[4] = luma1[src_argb[4]];
3974
0
    dst_argb[5] = luma1[src_argb[5]];
3975
0
    dst_argb[6] = luma1[src_argb[6]];
3976
0
    dst_argb[7] = src_argb[7];
3977
0
    src_argb += 8;
3978
0
    dst_argb += 8;
3979
0
  }
3980
0
  if (width & 1) {
3981
    // Luminance in rows, color values in columns.
3982
0
    const uint8_t* luma0 =
3983
0
        ((src_argb[0] * bc + src_argb[1] * gc + src_argb[2] * rc) & 0x7F00u) +
3984
0
        luma;
3985
0
    dst_argb[0] = luma0[src_argb[0]];
3986
0
    dst_argb[1] = luma0[src_argb[1]];
3987
0
    dst_argb[2] = luma0[src_argb[2]];
3988
0
    dst_argb[3] = src_argb[3];
3989
0
  }
3990
0
}
3991
3992
0
void ARGBCopyAlphaRow_C(const uint8_t* src, uint8_t* dst, int width) {
3993
0
  int i;
3994
0
  for (i = 0; i < width - 1; i += 2) {
3995
0
    dst[3] = src[3];
3996
0
    dst[7] = src[7];
3997
0
    dst += 8;
3998
0
    src += 8;
3999
0
  }
4000
0
  if (width & 1) {
4001
0
    dst[3] = src[3];
4002
0
  }
4003
0
}
4004
4005
0
void ARGBExtractAlphaRow_C(const uint8_t* src_argb, uint8_t* dst_a, int width) {
4006
0
  int i;
4007
0
  for (i = 0; i < width - 1; i += 2) {
4008
0
    dst_a[0] = src_argb[3];
4009
0
    dst_a[1] = src_argb[7];
4010
0
    dst_a += 2;
4011
0
    src_argb += 8;
4012
0
  }
4013
0
  if (width & 1) {
4014
0
    dst_a[0] = src_argb[3];
4015
0
  }
4016
0
}
4017
4018
0
void ARGBCopyYToAlphaRow_C(const uint8_t* src, uint8_t* dst, int width) {
4019
0
  int i;
4020
0
  for (i = 0; i < width - 1; i += 2) {
4021
0
    dst[3] = src[0];
4022
0
    dst[7] = src[1];
4023
0
    dst += 8;
4024
0
    src += 2;
4025
0
  }
4026
0
  if (width & 1) {
4027
0
    dst[3] = src[0];
4028
0
  }
4029
0
}
4030
4031
// Maximum temporary width for wrappers to process at a time, in pixels.
4032
7.05M
#define MAXTWIDTH 2048
4033
4034
#if !(defined(_MSC_VER) && !defined(__clang__) && defined(_M_IX86)) && \
4035
    defined(HAS_I422TORGB565ROW_SSSE3) && !defined(LIBYUV_ENABLE_ROWWIN)
4036
// row_win.cc has asm version, but GCC uses 2 step wrapper.
4037
void I422ToRGB565Row_SSSE3(const uint8_t* src_y,
4038
                           const uint8_t* src_u,
4039
                           const uint8_t* src_v,
4040
                           uint8_t* dst_rgb565,
4041
                           const struct YuvConstants* yuvconstants,
4042
                           int width) {
4043
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4044
  while (width > 0) {
4045
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4046
    I422ToARGBRow_SSSE3(src_y, src_u, src_v, row, yuvconstants, twidth);
4047
    ARGBToRGB565Row_SSE2(row, dst_rgb565, twidth);
4048
    src_y += twidth;
4049
    src_u += twidth / 2;
4050
    src_v += twidth / 2;
4051
    dst_rgb565 += twidth * 2;
4052
    width -= twidth;
4053
  }
4054
}
4055
#endif
4056
4057
#if defined(HAS_I422TOARGB1555ROW_SSSE3)
4058
void I422ToARGB1555Row_SSSE3(const uint8_t* src_y,
4059
                             const uint8_t* src_u,
4060
                             const uint8_t* src_v,
4061
                             uint8_t* dst_argb1555,
4062
                             const struct YuvConstants* yuvconstants,
4063
                             int width) {
4064
  // Row buffer for intermediate ARGB pixels.
4065
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4066
  while (width > 0) {
4067
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4068
    I422ToARGBRow_SSSE3(src_y, src_u, src_v, row, yuvconstants, twidth);
4069
    ARGBToARGB1555Row_SSE2(row, dst_argb1555, twidth);
4070
    src_y += twidth;
4071
    src_u += twidth / 2;
4072
    src_v += twidth / 2;
4073
    dst_argb1555 += twidth * 2;
4074
    width -= twidth;
4075
  }
4076
}
4077
#endif
4078
4079
#if defined(HAS_I422TOARGB4444ROW_SSSE3)
4080
void I422ToARGB4444Row_SSSE3(const uint8_t* src_y,
4081
                             const uint8_t* src_u,
4082
                             const uint8_t* src_v,
4083
                             uint8_t* dst_argb4444,
4084
                             const struct YuvConstants* yuvconstants,
4085
                             int width) {
4086
  // Row buffer for intermediate ARGB pixels.
4087
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4088
  while (width > 0) {
4089
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4090
    I422ToARGBRow_SSSE3(src_y, src_u, src_v, row, yuvconstants, twidth);
4091
    ARGBToARGB4444Row_SSE2(row, dst_argb4444, twidth);
4092
    src_y += twidth;
4093
    src_u += twidth / 2;
4094
    src_v += twidth / 2;
4095
    dst_argb4444 += twidth * 2;
4096
    width -= twidth;
4097
  }
4098
}
4099
#endif
4100
4101
#if defined(HAS_NV12TORGB565ROW_SSSE3)
4102
void NV12ToRGB565Row_SSSE3(const uint8_t* src_y,
4103
                           const uint8_t* src_uv,
4104
                           uint8_t* dst_rgb565,
4105
                           const struct YuvConstants* yuvconstants,
4106
                           int width) {
4107
  // Row buffer for intermediate ARGB pixels.
4108
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4109
  while (width > 0) {
4110
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4111
    NV12ToARGBRow_SSSE3(src_y, src_uv, row, yuvconstants, twidth);
4112
    ARGBToRGB565Row_SSE2(row, dst_rgb565, twidth);
4113
    src_y += twidth;
4114
    src_uv += twidth;
4115
    dst_rgb565 += twidth * 2;
4116
    width -= twidth;
4117
  }
4118
}
4119
#endif
4120
4121
#if defined(HAS_NV12TORGB24ROW_SSSE3)
4122
void NV12ToRGB24Row_SSSE3(const uint8_t* src_y,
4123
                          const uint8_t* src_uv,
4124
                          uint8_t* dst_rgb24,
4125
                          const struct YuvConstants* yuvconstants,
4126
0
                          int width) {
4127
  // Row buffer for intermediate ARGB pixels.
4128
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4129
0
  while (width > 0) {
4130
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4131
0
    NV12ToARGBRow_SSSE3(src_y, src_uv, row, yuvconstants, twidth);
4132
0
    ARGBToRGB24Row_SSSE3(row, dst_rgb24, twidth);
4133
0
    src_y += twidth;
4134
0
    src_uv += twidth;
4135
0
    dst_rgb24 += twidth * 3;
4136
0
    width -= twidth;
4137
0
  }
4138
0
}
4139
#endif
4140
4141
#if defined(HAS_NV21TORGB24ROW_SSSE3)
4142
void NV21ToRGB24Row_SSSE3(const uint8_t* src_y,
4143
                          const uint8_t* src_vu,
4144
                          uint8_t* dst_rgb24,
4145
                          const struct YuvConstants* yuvconstants,
4146
0
                          int width) {
4147
  // Row buffer for intermediate ARGB pixels.
4148
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4149
0
  while (width > 0) {
4150
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4151
0
    NV21ToARGBRow_SSSE3(src_y, src_vu, row, yuvconstants, twidth);
4152
0
    ARGBToRGB24Row_SSSE3(row, dst_rgb24, twidth);
4153
0
    src_y += twidth;
4154
0
    src_vu += twidth;
4155
0
    dst_rgb24 += twidth * 3;
4156
0
    width -= twidth;
4157
0
  }
4158
0
}
4159
#endif
4160
4161
#if defined(HAS_NV12TOARGBROW_AVX2) && defined(HAS_ARGBTORGB24ROW_AVX2)
4162
void NV12ToRGB24Row_AVX2(const uint8_t* src_y,
4163
                         const uint8_t* src_uv,
4164
                         uint8_t* dst_rgb24,
4165
                         const struct YuvConstants* yuvconstants,
4166
0
                         int width) {
4167
  // Row buffer for intermediate ARGB pixels.
4168
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4169
0
  while (width > 0) {
4170
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4171
0
    NV12ToARGBRow_AVX2(src_y, src_uv, row, yuvconstants, twidth);
4172
0
    ARGBToRGB24Row_AVX2(row, dst_rgb24, twidth);
4173
0
    src_y += twidth;
4174
0
    src_uv += twidth;
4175
0
    dst_rgb24 += twidth * 3;
4176
0
    width -= twidth;
4177
0
  }
4178
0
}
4179
#endif
4180
4181
#if defined(HAS_NV21TOARGBROW_AVX2) && defined(HAS_ARGBTORGB24ROW_AVX2)
4182
void NV21ToRGB24Row_AVX2(const uint8_t* src_y,
4183
                         const uint8_t* src_vu,
4184
                         uint8_t* dst_rgb24,
4185
                         const struct YuvConstants* yuvconstants,
4186
0
                         int width) {
4187
  // Row buffer for intermediate ARGB pixels.
4188
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4189
0
  while (width > 0) {
4190
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4191
0
    NV21ToARGBRow_AVX2(src_y, src_vu, row, yuvconstants, twidth);
4192
0
    ARGBToRGB24Row_AVX2(row, dst_rgb24, twidth);
4193
0
    src_y += twidth;
4194
0
    src_vu += twidth;
4195
0
    dst_rgb24 += twidth * 3;
4196
0
    width -= twidth;
4197
0
  }
4198
0
}
4199
#endif
4200
4201
#if defined(HAS_I422TOARGBROW_AVX2) && defined(HAS_ARGBTORGB565ROW_AVX2)
4202
void I422ToRGB565Row_AVX2(const uint8_t* src_y,
4203
                          const uint8_t* src_u,
4204
                          const uint8_t* src_v,
4205
                          uint8_t* dst_rgb565,
4206
                          const struct YuvConstants* yuvconstants,
4207
                          int width) {
4208
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4209
  while (width > 0) {
4210
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4211
    I422ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth);
4212
    ARGBToRGB565Row_AVX2(row, dst_rgb565, twidth);
4213
    src_y += twidth;
4214
    src_u += twidth / 2;
4215
    src_v += twidth / 2;
4216
    dst_rgb565 += twidth * 2;
4217
    width -= twidth;
4218
  }
4219
}
4220
#endif
4221
4222
#if defined(HAS_I422TOARGBROW_AVX2) && defined(HAS_ARGBTOARGB1555ROW_AVX2)
4223
void I422ToARGB1555Row_AVX2(const uint8_t* src_y,
4224
                            const uint8_t* src_u,
4225
                            const uint8_t* src_v,
4226
                            uint8_t* dst_argb1555,
4227
                            const struct YuvConstants* yuvconstants,
4228
                            int width) {
4229
  // Row buffer for intermediate ARGB pixels.
4230
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4231
  while (width > 0) {
4232
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4233
    I422ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth);
4234
    ARGBToARGB1555Row_AVX2(row, dst_argb1555, twidth);
4235
    src_y += twidth;
4236
    src_u += twidth / 2;
4237
    src_v += twidth / 2;
4238
    dst_argb1555 += twidth * 2;
4239
    width -= twidth;
4240
  }
4241
}
4242
#endif
4243
4244
#if defined(HAS_I422TOARGBROW_AVX2) && defined(HAS_ARGBTOARGB4444ROW_AVX2)
4245
void I422ToARGB4444Row_AVX2(const uint8_t* src_y,
4246
                            const uint8_t* src_u,
4247
                            const uint8_t* src_v,
4248
                            uint8_t* dst_argb4444,
4249
                            const struct YuvConstants* yuvconstants,
4250
                            int width) {
4251
  // Row buffer for intermediate ARGB pixels.
4252
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4253
  while (width > 0) {
4254
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4255
    I422ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth);
4256
    ARGBToARGB4444Row_AVX2(row, dst_argb4444, twidth);
4257
    src_y += twidth;
4258
    src_u += twidth / 2;
4259
    src_v += twidth / 2;
4260
    dst_argb4444 += twidth * 2;
4261
    width -= twidth;
4262
  }
4263
}
4264
#endif
4265
4266
4267
4268
#if defined(HAS_I444TOARGBROW_AVX2) && defined(HAS_ARGBTORGB24ROW_AVX2)
4269
void I444ToRGB24Row_AVX2(const uint8_t* src_y,
4270
                         const uint8_t* src_u,
4271
                         const uint8_t* src_v,
4272
                         uint8_t* dst_rgb24,
4273
                         const struct YuvConstants* yuvconstants,
4274
6.18k
                         int width) {
4275
  // Row buffer for intermediate ARGB pixels.
4276
6.18k
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4277
12.7k
  while (width > 0) {
4278
6.55k
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4279
6.55k
    I444ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth);
4280
6.55k
    ARGBToRGB24Row_AVX2(row, dst_rgb24, twidth);
4281
6.55k
    src_y += twidth;
4282
6.55k
    src_u += twidth;
4283
6.55k
    src_v += twidth;
4284
6.55k
    dst_rgb24 += twidth * 3;
4285
6.55k
    width -= twidth;
4286
6.55k
  }
4287
6.18k
}
4288
#endif
4289
4290
#if defined(HAS_NV12TOARGBROW_AVX2) && defined(HAS_ARGBTORGB565ROW_AVX2)
4291
void NV12ToRGB565Row_AVX2(const uint8_t* src_y,
4292
                          const uint8_t* src_uv,
4293
                          uint8_t* dst_rgb565,
4294
                          const struct YuvConstants* yuvconstants,
4295
                          int width) {
4296
  // Row buffer for intermediate ARGB pixels.
4297
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4298
  while (width > 0) {
4299
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4300
    NV12ToARGBRow_AVX2(src_y, src_uv, row, yuvconstants, twidth);
4301
    ARGBToRGB565Row_AVX2(row, dst_rgb565, twidth);
4302
    src_y += twidth;
4303
    src_uv += twidth;
4304
    dst_rgb565 += twidth * 2;
4305
    width -= twidth;
4306
  }
4307
}
4308
#endif
4309
4310
0
float ScaleSumSamples_C(const float* src, float* dst, float scale, int width) {
4311
0
  float fsum = 0.f;
4312
0
  int i;
4313
0
  for (i = 0; i < width; ++i) {
4314
0
    float v = *src++;
4315
0
    fsum += v * v;
4316
0
    *dst++ = v * scale;
4317
0
  }
4318
0
  return fsum;
4319
0
}
4320
4321
0
float ScaleMaxSamples_C(const float* src, float* dst, float scale, int width) {
4322
0
  float fmax = 0.f;
4323
0
  int i;
4324
0
  for (i = 0; i < width; ++i) {
4325
0
    float v = *src++;
4326
0
    float vs = v * scale;
4327
0
    fmax = (v > fmax) ? v : fmax;
4328
0
    *dst++ = vs;
4329
0
  }
4330
0
  return fmax;
4331
0
}
4332
4333
0
void ScaleSamples_C(const float* src, float* dst, float scale, int width) {
4334
0
  int i;
4335
0
  for (i = 0; i < width; ++i) {
4336
0
    *dst++ = *src++ * scale;
4337
0
  }
4338
0
}
4339
4340
0
void GaussRow_C(const uint32_t* src, uint16_t* dst, int width) {
4341
0
  int i;
4342
0
  for (i = 0; i < width; ++i) {
4343
0
    *dst++ = STATIC_CAST(
4344
0
        uint16_t,
4345
0
        (src[0] + src[1] * 4 + src[2] * 6 + src[3] * 4 + src[4] + 128) >> 8);
4346
0
    ++src;
4347
0
  }
4348
0
}
4349
4350
// filter 5 rows with 1, 4, 6, 4, 1 coefficients to produce 1 row.
4351
void GaussCol_C(const uint16_t* src0,
4352
                const uint16_t* src1,
4353
                const uint16_t* src2,
4354
                const uint16_t* src3,
4355
                const uint16_t* src4,
4356
                uint32_t* dst,
4357
0
                int width) {
4358
0
  int i;
4359
0
  for (i = 0; i < width; ++i) {
4360
0
    *dst++ = *src0++ + *src1++ * 4 + *src2++ * 6 + *src3++ * 4 + *src4++;
4361
0
  }
4362
0
}
4363
4364
0
void GaussRow_F32_C(const float* src, float* dst, int width) {
4365
0
  int i;
4366
0
  for (i = 0; i < width; ++i) {
4367
0
    *dst++ = (src[0] + src[1] * 4 + src[2] * 6 + src[3] * 4 + src[4]) *
4368
0
             (1.0f / 256.0f);
4369
0
    ++src;
4370
0
  }
4371
0
}
4372
4373
// filter 5 rows with 1, 4, 6, 4, 1 coefficients to produce 1 row.
4374
void GaussCol_F32_C(const float* src0,
4375
                    const float* src1,
4376
                    const float* src2,
4377
                    const float* src3,
4378
                    const float* src4,
4379
                    float* dst,
4380
0
                    int width) {
4381
0
  int i;
4382
0
  for (i = 0; i < width; ++i) {
4383
0
    *dst++ = *src0++ + *src1++ * 4 + *src2++ * 6 + *src3++ * 4 + *src4++;
4384
0
  }
4385
0
}
4386
4387
// Convert biplanar NV21 to packed YUV24
4388
void NV21ToYUV24Row_C(const uint8_t* src_y,
4389
                      const uint8_t* src_vu,
4390
                      uint8_t* dst_yuv24,
4391
0
                      int width) {
4392
0
  int x;
4393
0
  for (x = 0; x < width - 1; x += 2) {
4394
0
    dst_yuv24[0] = src_vu[0];  // V
4395
0
    dst_yuv24[1] = src_vu[1];  // U
4396
0
    dst_yuv24[2] = src_y[0];   // Y0
4397
0
    dst_yuv24[3] = src_vu[0];  // V
4398
0
    dst_yuv24[4] = src_vu[1];  // U
4399
0
    dst_yuv24[5] = src_y[1];   // Y1
4400
0
    src_y += 2;
4401
0
    src_vu += 2;
4402
0
    dst_yuv24 += 6;  // Advance 2 pixels.
4403
0
  }
4404
0
  if (width & 1) {
4405
0
    dst_yuv24[0] = src_vu[0];  // V
4406
0
    dst_yuv24[1] = src_vu[1];  // U
4407
0
    dst_yuv24[2] = src_y[0];   // Y0
4408
0
  }
4409
0
}
4410
4411
// Filter 2 rows of AYUV UV's (444) into UV (420).
4412
// AYUV is VUYA in memory.  UV for NV12 is UV order in memory.
4413
void AYUVToUVRow_C(const uint8_t* src_ayuv,
4414
                   int src_stride_ayuv,
4415
                   uint8_t* dst_uv,
4416
0
                   int width) {
4417
  // Output a row of UV values, filtering 2x2 rows of AYUV.
4418
0
  int x;
4419
0
  for (x = 0; x < width - 1; x += 2) {
4420
0
    const uint8_t* src_ayuv_next = src_ayuv + src_stride_ayuv;
4421
0
    dst_uv[0] =
4422
0
        (src_ayuv[1] + src_ayuv[5] + src_ayuv_next[1] + src_ayuv_next[5] + 2) >>
4423
0
        2;
4424
0
    dst_uv[1] =
4425
0
        (src_ayuv[0] + src_ayuv[4] + src_ayuv_next[0] + src_ayuv_next[4] + 2) >>
4426
0
        2;
4427
0
    src_ayuv += 8;
4428
0
    dst_uv += 2;
4429
0
  }
4430
0
  if (width & 1) {
4431
0
    const uint8_t* src_ayuv_next = src_ayuv + src_stride_ayuv;
4432
0
    dst_uv[0] = (src_ayuv[1] + src_ayuv_next[1] + 1) >> 1;
4433
0
    dst_uv[1] = (src_ayuv[0] + src_ayuv_next[0] + 1) >> 1;
4434
0
  }
4435
0
}
4436
4437
// Filter 2 rows of AYUV UV's (444) into VU (420).
4438
void AYUVToVURow_C(const uint8_t* src_ayuv,
4439
                   int src_stride_ayuv,
4440
                   uint8_t* dst_vu,
4441
0
                   int width) {
4442
  // Output a row of VU values, filtering 2x2 rows of AYUV.
4443
0
  int x;
4444
0
  for (x = 0; x < width - 1; x += 2) {
4445
0
    const uint8_t* src_ayuv_next = src_ayuv + src_stride_ayuv;
4446
0
    dst_vu[0] =
4447
0
        (src_ayuv[0] + src_ayuv[4] + src_ayuv_next[0] + src_ayuv_next[4] + 2) >>
4448
0
        2;
4449
0
    dst_vu[1] =
4450
0
        (src_ayuv[1] + src_ayuv[5] + src_ayuv_next[1] + src_ayuv_next[5] + 2) >>
4451
0
        2;
4452
0
    src_ayuv += 8;
4453
0
    dst_vu += 2;
4454
0
  }
4455
0
  if (width & 1) {
4456
0
    const uint8_t* src_ayuv_next = src_ayuv + src_stride_ayuv;
4457
0
    dst_vu[0] = (src_ayuv[0] + src_ayuv_next[0] + 1) >> 1;
4458
0
    dst_vu[1] = (src_ayuv[1] + src_ayuv_next[1] + 1) >> 1;
4459
0
  }
4460
0
}
4461
4462
// Copy row of AYUV Y's into Y
4463
0
void AYUVToYRow_C(const uint8_t* src_ayuv, uint8_t* dst_y, int width) {
4464
  // Output a row of Y values.
4465
0
  int x;
4466
0
  for (x = 0; x < width; ++x) {
4467
0
    dst_y[x] = src_ayuv[2];  // v,u,y,a
4468
0
    src_ayuv += 4;
4469
0
  }
4470
0
}
4471
4472
// Convert UV plane of NV12 to VU of NV21.
4473
0
void SwapUVRow_C(const uint8_t* src_uv, uint8_t* dst_vu, int width) {
4474
0
  int x;
4475
0
  for (x = 0; x < width; ++x) {
4476
0
    uint8_t u = src_uv[0];
4477
0
    uint8_t v = src_uv[1];
4478
0
    dst_vu[0] = v;
4479
0
    dst_vu[1] = u;
4480
0
    src_uv += 2;
4481
0
    dst_vu += 2;
4482
0
  }
4483
0
}
4484
4485
void HalfMergeUVRow_C(const uint8_t* src_u,
4486
                      int src_stride_u,
4487
                      const uint8_t* src_v,
4488
                      int src_stride_v,
4489
                      uint8_t* dst_uv,
4490
0
                      int width) {
4491
0
  int x;
4492
0
  for (x = 0; x < width - 1; x += 2) {
4493
0
    const uint8_t* src_u_next = src_u + src_stride_u;
4494
0
    dst_uv[0] = (src_u[0] + src_u[1] + src_u_next[0] + src_u_next[1] + 2) >> 2;
4495
0
    const uint8_t* src_v_next = src_v + src_stride_v;
4496
0
    dst_uv[1] = (src_v[0] + src_v[1] + src_v_next[0] + src_v_next[1] + 2) >> 2;
4497
0
    src_u += 2;
4498
0
    src_v += 2;
4499
0
    dst_uv += 2;
4500
0
  }
4501
0
  if (width & 1) {
4502
0
    const uint8_t* src_u_next = src_u + src_stride_u;
4503
0
    dst_uv[0] = (src_u[0] + src_u_next[0] + 1) >> 1;
4504
0
    const uint8_t* src_v_next = src_v + src_stride_v;
4505
0
    dst_uv[1] = (src_v[0] + src_v_next[0] + 1) >> 1;
4506
0
  }
4507
0
}
4508
4509
#undef STATIC_CAST
4510
4511
void RGBToYMatrixRow_C(const uint8_t* src_rgb,
4512
                       uint8_t* dst_y,
4513
                       int width,
4514
0
                       const struct ArgbConstants* c) {
4515
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4516
0
  while (width > 0) {
4517
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4518
0
    RGB24ToARGBRow_C(src_rgb, row, twidth);
4519
0
    ARGBToYMatrixRow_C(row, dst_y, twidth, c);
4520
0
    src_rgb += twidth * 3;
4521
0
    dst_y += twidth;
4522
0
    width -= twidth;
4523
0
  }
4524
0
}
4525
4526
void RGBToUVMatrixRow_C(const uint8_t* src_rgb,
4527
                        int src_stride_rgb,
4528
                        uint8_t* dst_u,
4529
                        uint8_t* dst_v,
4530
                        int width,
4531
0
                        const struct ArgbConstants* c) {
4532
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4 * 2]);
4533
0
  while (width > 0) {
4534
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4535
0
    RGB24ToARGBRow_C(src_rgb, row, twidth);
4536
0
    RGB24ToARGBRow_C(src_rgb + src_stride_rgb, row + MAXTWIDTH * 4, twidth);
4537
0
    ARGBToUVMatrixRow_C(row, MAXTWIDTH * 4, dst_u, dst_v, twidth, c);
4538
0
    src_rgb += twidth * 3;
4539
0
    dst_u += twidth / 2;
4540
0
    dst_v += twidth / 2;
4541
0
    width -= twidth;
4542
0
  }
4543
0
}
4544
4545
void RGBToUV444MatrixRow_C(const uint8_t* src_rgb,
4546
                           uint8_t* dst_u,
4547
                           uint8_t* dst_v,
4548
                           int width,
4549
0
                           const struct ArgbConstants* c) {
4550
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4551
0
  while (width > 0) {
4552
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4553
0
    RGB24ToARGBRow_C(src_rgb, row, twidth);
4554
0
    ARGBToUV444MatrixRow_C(row, dst_u, dst_v, twidth, c);
4555
0
    src_rgb += twidth * 3;
4556
0
    dst_u += twidth;
4557
0
    dst_v += twidth;
4558
0
    width -= twidth;
4559
0
  }
4560
0
}
4561
4562
#if defined(HAS_ARGBTOYMATRIXROW_SSSE3) && defined(HAS_RGB24TOARGBROW_SSSE3)
4563
void RGBToYMatrixRow_SSSE3(const uint8_t* src_rgb,
4564
                           uint8_t* dst_y,
4565
                           int width,
4566
0
                           const struct ArgbConstants* c) {
4567
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4568
0
  while (width > 0) {
4569
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4570
0
    RGB24ToARGBRow_SSSE3(src_rgb, row, twidth);
4571
0
    ARGBToYMatrixRow_SSSE3(row, dst_y, twidth, c);
4572
0
    src_rgb += twidth * 3;
4573
0
    dst_y += twidth;
4574
0
    width -= twidth;
4575
0
  }
4576
0
}
4577
#endif
4578
4579
#if defined(HAS_ARGBTOYMATRIXROW_AVX2) && defined(HAS_RGB24TOARGBROW_AVX2)
4580
void RGBToYMatrixRow_AVX2(const uint8_t* src_rgb,
4581
                          uint8_t* dst_y,
4582
                          int width,
4583
2.32M
                          const struct ArgbConstants* c) {
4584
2.32M
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4585
5.25M
  while (width > 0) {
4586
2.92M
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4587
2.92M
    RGB24ToARGBRow_AVX2(src_rgb, row, twidth);
4588
2.92M
    ARGBToYMatrixRow_AVX2(row, dst_y, twidth, c);
4589
2.92M
    src_rgb += twidth * 3;
4590
2.92M
    dst_y += twidth;
4591
2.92M
    width -= twidth;
4592
2.92M
  }
4593
2.32M
}
4594
#endif
4595
4596
#if defined(HAS_ARGBTOUVMATRIXROW_SSSE3) && defined(HAS_RGB24TOARGBROW_SSSE3)
4597
void RGBToUVMatrixRow_SSSE3(const uint8_t* src_rgb,
4598
                            int src_stride_rgb,
4599
                            uint8_t* dst_u,
4600
                            uint8_t* dst_v,
4601
                            int width,
4602
0
                            const struct ArgbConstants* c) {
4603
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4 * 2]);
4604
0
  while (width > 0) {
4605
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4606
0
    RGB24ToARGBRow_SSSE3(src_rgb, row, twidth);
4607
0
    RGB24ToARGBRow_SSSE3(src_rgb + src_stride_rgb, row + MAXTWIDTH * 4, twidth);
4608
0
    ARGBToUVMatrixRow_SSSE3(row, MAXTWIDTH * 4, dst_u, dst_v, twidth, c);
4609
0
    src_rgb += twidth * 3;
4610
0
    dst_u += twidth / 2;
4611
0
    dst_v += twidth / 2;
4612
0
    width -= twidth;
4613
0
  }
4614
0
}
4615
#endif
4616
4617
#if defined(HAS_ARGBTOUVMATRIXROW_AVX2) && defined(HAS_RGB24TOARGBROW_AVX2)
4618
void RGBToUVMatrixRow_AVX2(const uint8_t* src_rgb,
4619
                           int src_stride_rgb,
4620
                           uint8_t* dst_u,
4621
                           uint8_t* dst_v,
4622
                           int width,
4623
443k
                           const struct ArgbConstants* c) {
4624
443k
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4 * 2]);
4625
969k
  while (width > 0) {
4626
525k
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4627
525k
    RGB24ToARGBRow_AVX2(src_rgb, row, twidth);
4628
525k
    RGB24ToARGBRow_AVX2(src_rgb + src_stride_rgb, row + MAXTWIDTH * 4, twidth);
4629
525k
    ARGBToUVMatrixRow_AVX2(row, MAXTWIDTH * 4, dst_u, dst_v, twidth, c);
4630
525k
    src_rgb += twidth * 3;
4631
525k
    dst_u += twidth / 2;
4632
525k
    dst_v += twidth / 2;
4633
525k
    width -= twidth;
4634
525k
  }
4635
443k
}
4636
#endif
4637
4638
#if defined(HAS_ARGBTOUVMATRIXROW_AVX512BW) && \
4639
    defined(HAS_RGB24TOARGBROW_AVX512BW)
4640
void RGBToUVMatrixRow_AVX512BW(const uint8_t* src_rgb,
4641
                               int src_stride_rgb,
4642
                               uint8_t* dst_u,
4643
                               uint8_t* dst_v,
4644
                               int width,
4645
0
                               const struct ArgbConstants* c) {
4646
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4 * 2]);
4647
0
  while (width > 0) {
4648
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4649
0
    RGB24ToARGBRow_AVX512BW(src_rgb, row, twidth);
4650
0
    RGB24ToARGBRow_AVX512BW(src_rgb + src_stride_rgb, row + MAXTWIDTH * 4,
4651
0
                            twidth);
4652
0
    ARGBToUVMatrixRow_AVX512BW(row, MAXTWIDTH * 4, dst_u, dst_v, twidth, c);
4653
0
    src_rgb += twidth * 3;
4654
0
    dst_u += twidth / 2;
4655
0
    dst_v += twidth / 2;
4656
0
    width -= twidth;
4657
0
  }
4658
0
}
4659
#endif
4660
4661
#if defined(HAS_ARGBTOUV444MATRIXROW_SSSE3) && \
4662
    defined(HAS_RGB24TOARGBROW_SSSE3)
4663
void RGBToUV444MatrixRow_SSSE3(const uint8_t* src_rgb,
4664
                               uint8_t* dst_u,
4665
                               uint8_t* dst_v,
4666
                               int width,
4667
0
                               const struct ArgbConstants* c) {
4668
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4669
0
  while (width > 0) {
4670
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4671
0
    RGB24ToARGBRow_SSSE3(src_rgb, row, twidth);
4672
0
    ARGBToUV444MatrixRow_SSSE3(row, dst_u, dst_v, twidth, c);
4673
0
    src_rgb += twidth * 3;
4674
0
    dst_u += twidth;
4675
0
    dst_v += twidth;
4676
0
    width -= twidth;
4677
0
  }
4678
0
}
4679
#endif
4680
4681
#if defined(HAS_ARGBTOUV444MATRIXROW_AVX2) && \
4682
    defined(HAS_RGB24TOARGBROW_AVX2)
4683
void RGBToUV444MatrixRow_AVX2(const uint8_t* src_rgb,
4684
                              uint8_t* dst_u,
4685
                              uint8_t* dst_v,
4686
                              int width,
4687
1.43M
                              const struct ArgbConstants* c) {
4688
1.43M
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4689
3.08M
  while (width > 0) {
4690
1.64M
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4691
1.64M
    RGB24ToARGBRow_AVX2(src_rgb, row, twidth);
4692
1.64M
    ARGBToUV444MatrixRow_AVX2(row, dst_u, dst_v, twidth, c);
4693
1.64M
    src_rgb += twidth * 3;
4694
1.64M
    dst_u += twidth;
4695
1.64M
    dst_v += twidth;
4696
1.64M
    width -= twidth;
4697
1.64M
  }
4698
1.43M
}
4699
#endif
4700
4701
#if defined(HAS_ARGBTOUV444MATRIXROW_AVX512BW) && \
4702
    defined(HAS_RGB24TOARGBROW_AVX512BW)
4703
void RGBToUV444MatrixRow_AVX512BW(const uint8_t* src_rgb,
4704
                                  uint8_t* dst_u,
4705
                                  uint8_t* dst_v,
4706
                                  int width,
4707
0
                                  const struct ArgbConstants* c) {
4708
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4709
0
  while (width > 0) {
4710
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4711
0
    RGB24ToARGBRow_AVX512BW(src_rgb, row, twidth);
4712
0
    ARGBToUV444MatrixRow_AVX512BW(row, dst_u, dst_v, twidth, c);
4713
0
    src_rgb += twidth * 3;
4714
0
    dst_u += twidth;
4715
0
    dst_v += twidth;
4716
0
    width -= twidth;
4717
0
  }
4718
0
}
4719
#endif
4720
4721
void RGB565ToYMatrixRow_C(const uint8_t* src_rgb565,
4722
                          uint8_t* dst_y,
4723
                          int width,
4724
0
                          const struct ArgbConstants* c) {
4725
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4726
0
  while (width > 0) {
4727
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4728
0
    RGB565ToARGBRow_C(src_rgb565, row, twidth);
4729
0
    ARGBToYMatrixRow_C(row, dst_y, twidth, c);
4730
0
    src_rgb565 += twidth * 2;
4731
0
    dst_y += twidth;
4732
0
    width -= twidth;
4733
0
  }
4734
0
}
4735
4736
void RGB565ToUVMatrixRow_C(const uint8_t* src_rgb565,
4737
                           int src_stride_rgb565,
4738
                           uint8_t* dst_u,
4739
                           uint8_t* dst_v,
4740
                           int width,
4741
0
                           const struct ArgbConstants* c) {
4742
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4 * 2]);
4743
0
  while (width > 0) {
4744
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4745
0
    RGB565ToARGBRow_C(src_rgb565, row, twidth);
4746
0
    RGB565ToARGBRow_C(src_rgb565 + src_stride_rgb565, row + MAXTWIDTH * 4,
4747
0
                      twidth);
4748
0
    ARGBToUVMatrixRow_C(row, MAXTWIDTH * 4, dst_u, dst_v, twidth, c);
4749
0
    src_rgb565 += twidth * 2;
4750
0
    dst_u += twidth / 2;
4751
0
    dst_v += twidth / 2;
4752
0
    width -= twidth;
4753
0
  }
4754
0
}
4755
4756
#if defined(HAS_ARGBTOYMATRIXROW_AVX2) && defined(HAS_RGB565TOARGBROW_AVX2)
4757
void RGB565ToYMatrixRow_AVX2(const uint8_t* src_rgb565,
4758
                             uint8_t* dst_y,
4759
                             int width,
4760
0
                             const struct ArgbConstants* c) {
4761
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4762
0
  while (width > 0) {
4763
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4764
0
    RGB565ToARGBRow_AVX2(src_rgb565, row, twidth);
4765
0
    ARGBToYMatrixRow_AVX2(row, dst_y, twidth, c);
4766
0
    src_rgb565 += twidth * 2;
4767
0
    dst_y += twidth;
4768
0
    width -= twidth;
4769
0
  }
4770
0
}
4771
#endif
4772
4773
#if defined(HAS_ARGBTOUVMATRIXROW_AVX2) && defined(HAS_RGB565TOARGBROW_AVX2)
4774
void RGB565ToUVMatrixRow_AVX2(const uint8_t* src_rgb565,
4775
                              int src_stride_rgb565,
4776
                              uint8_t* dst_u,
4777
                              uint8_t* dst_v,
4778
                              int width,
4779
0
                              const struct ArgbConstants* c) {
4780
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4 * 2]);
4781
0
  while (width > 0) {
4782
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4783
0
    RGB565ToARGBRow_AVX2(src_rgb565, row, twidth);
4784
0
    RGB565ToARGBRow_AVX2(src_rgb565 + src_stride_rgb565, row + MAXTWIDTH * 4,
4785
0
                         twidth);
4786
0
    ARGBToUVMatrixRow_AVX2(row, MAXTWIDTH * 4, dst_u, dst_v, twidth, c);
4787
0
    src_rgb565 += twidth * 2;
4788
0
    dst_u += twidth / 2;
4789
0
    dst_v += twidth / 2;
4790
0
    width -= twidth;
4791
0
  }
4792
0
}
4793
#endif
4794
4795
#if defined(HAS_RGB565TOARGBROW_NEON) && defined(HAS_ARGBTOYMATRIXROW_NEON)
4796
void RGB565ToYMatrixRow_NEON(const uint8_t* src_rgb565,
4797
                             uint8_t* dst_y,
4798
                             int width,
4799
                             const struct ArgbConstants* c) {
4800
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4801
  while (width > 0) {
4802
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4803
    RGB565ToARGBRow_NEON(src_rgb565, row, twidth);
4804
    ARGBToYMatrixRow_NEON(row, dst_y, twidth, c);
4805
    src_rgb565 += twidth * 2;
4806
    dst_y += twidth;
4807
    width -= twidth;
4808
  }
4809
}
4810
#endif
4811
4812
#if defined(HAS_RGB565TOARGBROW_NEON) && defined(HAS_ARGBTOUVMATRIXROW_NEON)
4813
void RGB565ToUVMatrixRow_NEON(const uint8_t* src_rgb565,
4814
                              int src_stride_rgb565,
4815
                              uint8_t* dst_u,
4816
                              uint8_t* dst_v,
4817
                              int width,
4818
                              const struct ArgbConstants* c) {
4819
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4 * 2]);
4820
  while (width > 0) {
4821
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4822
    RGB565ToARGBRow_NEON(src_rgb565, row, twidth);
4823
    RGB565ToARGBRow_NEON(src_rgb565 + src_stride_rgb565, row + MAXTWIDTH * 4,
4824
                         twidth);
4825
    ARGBToUVMatrixRow_NEON(row, MAXTWIDTH * 4, dst_u, dst_v, twidth, c);
4826
    src_rgb565 += twidth * 2;
4827
    dst_u += twidth / 2;
4828
    dst_v += twidth / 2;
4829
    width -= twidth;
4830
  }
4831
}
4832
#endif
4833
4834
void ARGB1555ToYMatrixRow_C(const uint8_t* src_argb1555,
4835
                            uint8_t* dst_y,
4836
                            int width,
4837
0
                            const struct ArgbConstants* c) {
4838
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4839
0
  while (width > 0) {
4840
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4841
0
    ARGB1555ToARGBRow_C(src_argb1555, row, twidth);
4842
0
    ARGBToYMatrixRow_C(row, dst_y, twidth, c);
4843
0
    src_argb1555 += twidth * 2;
4844
0
    dst_y += twidth;
4845
0
    width -= twidth;
4846
0
  }
4847
0
}
4848
4849
void ARGB1555ToUVMatrixRow_C(const uint8_t* src_argb1555,
4850
                             int src_stride_argb1555,
4851
                             uint8_t* dst_u,
4852
                             uint8_t* dst_v,
4853
                             int width,
4854
0
                             const struct ArgbConstants* c) {
4855
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4 * 2]);
4856
0
  while (width > 0) {
4857
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4858
0
    ARGB1555ToARGBRow_C(src_argb1555, row, twidth);
4859
0
    ARGB1555ToARGBRow_C(src_argb1555 + src_stride_argb1555, row + MAXTWIDTH * 4,
4860
0
                        twidth);
4861
0
    ARGBToUVMatrixRow_C(row, MAXTWIDTH * 4, dst_u, dst_v, twidth, c);
4862
0
    src_argb1555 += twidth * 2;
4863
0
    dst_u += twidth / 2;
4864
0
    dst_v += twidth / 2;
4865
0
    width -= twidth;
4866
0
  }
4867
0
}
4868
4869
void ARGB4444ToYMatrixRow_C(const uint8_t* src_argb4444,
4870
                            uint8_t* dst_y,
4871
                            int width,
4872
0
                            const struct ArgbConstants* c) {
4873
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4874
0
  while (width > 0) {
4875
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4876
0
    ARGB4444ToARGBRow_C(src_argb4444, row, twidth);
4877
0
    ARGBToYMatrixRow_C(row, dst_y, twidth, c);
4878
0
    src_argb4444 += twidth * 2;
4879
0
    dst_y += twidth;
4880
0
    width -= twidth;
4881
0
  }
4882
0
}
4883
4884
void ARGB4444ToUVMatrixRow_C(const uint8_t* src_argb4444,
4885
                             int src_stride_argb4444,
4886
                             uint8_t* dst_u,
4887
                             uint8_t* dst_v,
4888
                             int width,
4889
0
                             const struct ArgbConstants* c) {
4890
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4 * 2]);
4891
0
  while (width > 0) {
4892
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4893
0
    ARGB4444ToARGBRow_C(src_argb4444, row, twidth);
4894
0
    ARGB4444ToARGBRow_C(src_argb4444 + src_stride_argb4444, row + MAXTWIDTH * 4,
4895
0
                        twidth);
4896
0
    ARGBToUVMatrixRow_C(row, MAXTWIDTH * 4, dst_u, dst_v, twidth, c);
4897
0
    src_argb4444 += twidth * 2;
4898
0
    dst_u += twidth / 2;
4899
0
    dst_v += twidth / 2;
4900
0
    width -= twidth;
4901
0
  }
4902
0
}
4903
4904
#if defined(HAS_ARGBTOYMATRIXROW_AVX2)
4905
#if defined(HAS_ARGB1555TOARGBROW_AVX2)
4906
void ARGB1555ToYMatrixRow_AVX2(const uint8_t* src_argb1555,
4907
                               uint8_t* dst_y,
4908
                               int width,
4909
0
                               const struct ArgbConstants* c) {
4910
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4911
0
  while (width > 0) {
4912
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4913
0
    ARGB1555ToARGBRow_AVX2(src_argb1555, row, twidth);
4914
0
    ARGBToYMatrixRow_AVX2(row, dst_y, twidth, c);
4915
0
    src_argb1555 += twidth * 2;
4916
0
    dst_y += twidth;
4917
0
    width -= twidth;
4918
0
  }
4919
0
}
4920
#endif
4921
4922
#if defined(HAS_ARGB4444TOARGBROW_AVX2)
4923
void ARGB4444ToYMatrixRow_AVX2(const uint8_t* src_argb4444,
4924
                               uint8_t* dst_y,
4925
                               int width,
4926
0
                               const struct ArgbConstants* c) {
4927
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4928
0
  while (width > 0) {
4929
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4930
0
    ARGB4444ToARGBRow_AVX2(src_argb4444, row, twidth);
4931
0
    ARGBToYMatrixRow_AVX2(row, dst_y, twidth, c);
4932
0
    src_argb4444 += twidth * 2;
4933
0
    dst_y += twidth;
4934
0
    width -= twidth;
4935
0
  }
4936
0
}
4937
#endif
4938
#endif
4939
4940
#if defined(HAS_ARGBTOUVMATRIXROW_AVX2)
4941
#if defined(HAS_ARGB1555TOARGBROW_AVX2)
4942
void ARGB1555ToUVMatrixRow_AVX2(const uint8_t* src_argb1555,
4943
                                int src_stride_argb1555,
4944
                                uint8_t* dst_u,
4945
                                uint8_t* dst_v,
4946
                                int width,
4947
0
                                const struct ArgbConstants* c) {
4948
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4 * 2]);
4949
0
  while (width > 0) {
4950
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4951
0
    ARGB1555ToARGBRow_AVX2(src_argb1555, row, twidth);
4952
0
    ARGB1555ToARGBRow_AVX2(src_argb1555 + src_stride_argb1555,
4953
0
                           row + MAXTWIDTH * 4, twidth);
4954
0
    ARGBToUVMatrixRow_AVX2(row, MAXTWIDTH * 4, dst_u, dst_v, twidth, c);
4955
0
    src_argb1555 += twidth * 2;
4956
0
    dst_u += twidth / 2;
4957
0
    dst_v += twidth / 2;
4958
0
    width -= twidth;
4959
0
  }
4960
0
}
4961
#endif
4962
4963
#if defined(HAS_ARGB4444TOARGBROW_AVX2)
4964
void ARGB4444ToUVMatrixRow_AVX2(const uint8_t* src_argb4444,
4965
                                int src_stride_argb4444,
4966
                                uint8_t* dst_u,
4967
                                uint8_t* dst_v,
4968
                                int width,
4969
0
                                const struct ArgbConstants* c) {
4970
0
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4 * 2]);
4971
0
  while (width > 0) {
4972
0
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4973
0
    ARGB4444ToARGBRow_AVX2(src_argb4444, row, twidth);
4974
0
    ARGB4444ToARGBRow_AVX2(src_argb4444 + src_stride_argb4444,
4975
0
                           row + MAXTWIDTH * 4, twidth);
4976
0
    ARGBToUVMatrixRow_AVX2(row, MAXTWIDTH * 4, dst_u, dst_v, twidth, c);
4977
0
    src_argb4444 += twidth * 2;
4978
0
    dst_u += twidth / 2;
4979
0
    dst_v += twidth / 2;
4980
0
    width -= twidth;
4981
0
  }
4982
0
}
4983
#endif
4984
#endif
4985
4986
#if defined(HAS_ARGBTOYMATRIXROW_NEON) && defined(HAS_ARGB1555TOARGBROW_NEON)
4987
void ARGB1555ToYMatrixRow_NEON(const uint8_t* src_argb1555,
4988
                               uint8_t* dst_y,
4989
                               int width,
4990
                               const struct ArgbConstants* c) {
4991
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
4992
  while (width > 0) {
4993
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
4994
    ARGB1555ToARGBRow_NEON(src_argb1555, row, twidth);
4995
    ARGBToYMatrixRow_NEON(row, dst_y, twidth, c);
4996
    src_argb1555 += twidth * 2;
4997
    dst_y += twidth;
4998
    width -= twidth;
4999
  }
5000
}
5001
#endif
5002
5003
#if defined(HAS_ARGBTOYMATRIXROW_NEON) && defined(HAS_ARGB4444TOARGBROW_NEON)
5004
void ARGB4444ToYMatrixRow_NEON(const uint8_t* src_argb4444,
5005
                               uint8_t* dst_y,
5006
                               int width,
5007
                               const struct ArgbConstants* c) {
5008
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
5009
  while (width > 0) {
5010
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
5011
    ARGB4444ToARGBRow_NEON(src_argb4444, row, twidth);
5012
    ARGBToYMatrixRow_NEON(row, dst_y, twidth, c);
5013
    src_argb4444 += twidth * 2;
5014
    dst_y += twidth;
5015
    width -= twidth;
5016
  }
5017
}
5018
#endif
5019
5020
#if defined(HAS_ARGBTOUVMATRIXROW_NEON) && defined(HAS_ARGB1555TOARGBROW_NEON)
5021
void ARGB1555ToUVMatrixRow_NEON(const uint8_t* src_argb1555,
5022
                                int src_stride_argb1555,
5023
                                uint8_t* dst_u,
5024
                                uint8_t* dst_v,
5025
                                int width,
5026
                                const struct ArgbConstants* c) {
5027
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4 * 2]);
5028
  while (width > 0) {
5029
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
5030
    ARGB1555ToARGBRow_NEON(src_argb1555, row, twidth);
5031
    ARGB1555ToARGBRow_NEON(src_argb1555 + src_stride_argb1555,
5032
                           row + MAXTWIDTH * 4, twidth);
5033
    ARGBToUVMatrixRow_NEON(row, MAXTWIDTH * 4, dst_u, dst_v, twidth, c);
5034
    src_argb1555 += twidth * 2;
5035
    dst_u += twidth / 2;
5036
    dst_v += twidth / 2;
5037
    width -= twidth;
5038
  }
5039
}
5040
#endif
5041
5042
#if defined(HAS_ARGBTOUVMATRIXROW_NEON) && defined(HAS_ARGB4444TOARGBROW_NEON)
5043
void ARGB4444ToUVMatrixRow_NEON(const uint8_t* src_argb4444,
5044
                                int src_stride_argb4444,
5045
                                uint8_t* dst_u,
5046
                                uint8_t* dst_v,
5047
                                int width,
5048
                                const struct ArgbConstants* c) {
5049
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4 * 2]);
5050
  while (width > 0) {
5051
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
5052
    ARGB4444ToARGBRow_NEON(src_argb4444, row, twidth);
5053
    ARGB4444ToARGBRow_NEON(src_argb4444 + src_stride_argb4444,
5054
                           row + MAXTWIDTH * 4, twidth);
5055
    ARGBToUVMatrixRow_NEON(row, MAXTWIDTH * 4, dst_u, dst_v, twidth, c);
5056
    src_argb4444 += twidth * 2;
5057
    dst_u += twidth / 2;
5058
    dst_v += twidth / 2;
5059
    width -= twidth;
5060
  }
5061
}
5062
#endif
5063
5064
#ifdef __cplusplus
5065
}  // extern "C"
5066
}  // namespace libyuv
5067
#endif